diff --git a/src/source-ipfw.c b/src/source-ipfw.c index b48dd1855..9f2c17450 100644 --- a/src/source-ipfw.c +++ b/src/source-ipfw.c @@ -144,9 +144,12 @@ static TmEcode DecodeIPFWThreadInit(ThreadVars *, const void *, void **); static TmEcode DecodeIPFWThreadDeinit(ThreadVars *tv, void *data); static TmEcode DecodeIPFW(ThreadVars *, Packet *, void *); +#ifdef UNITTESTS +static void SourceIPFWRegisterTests(void); +#endif + /** * \brief Registration Function for RecieveIPFW. - * \todo Unit tests are needed for this module. */ void TmModuleReceiveIPFWRegister (void) { @@ -163,11 +166,13 @@ void TmModuleReceiveIPFWRegister (void) SC_CAP_NET_BIND_SERVICE | SC_CAP_NET_BROADCAST; /** \todo untested */ tmm_modules[TMM_RECEIVEIPFW].flags = TM_FLAG_RECEIVE_TM; +#ifdef UNITTESTS + tmm_modules[TMM_RECEIVEIPFW].RegisterTests = SourceIPFWRegisterTests; +#endif } /** * \brief Registration Function for VerdictIPFW. - * \todo Unit tests are needed for this module. */ void TmModuleVerdictIPFWRegister (void) { @@ -225,6 +230,49 @@ static inline void IPFWMutexUnlock(IPFWQueueVars *nq) #define IP_MAXPACKET 65535 #endif +/** + * \brief Read one packet from a divert socket + * + * \param fd the divert socket + * \param pkt buffer for the packet + * \param size size of the buffer + * \param sin set to the divert sockaddr of this packet. The packet has to be + * reinjected with it, so it is kept with the packet, see + * IPFWSetVerdict() + * + * \retval the length of the packet, or -1 with errno set by recvfrom() + */ +static ssize_t IPFWRecv(int fd, uint8_t *pkt, size_t size, struct sockaddr_in *sin) +{ + socklen_t sinlen = sizeof(*sin); + + memset(sin, 0, sizeof(*sin)); + return recvfrom(fd, pkt, size, 0, (struct sockaddr *)sin, &sinlen); +} + +/** + * \brief Set up a packet that was read from a divert socket + * + * \param p the packet + * \param ptv thread vars of the receive thread + * \param sin the divert sockaddr the packet was received with. It is kept in + * the packet for IPFWSetVerdict() + * \param pkt the packet as read from the socket + * \param pktlen its length + */ +static void IPFWPacketSetup(Packet *p, const IPFWThreadVars *ptv, const struct sockaddr_in *sin, + const uint8_t *pkt, uint32_t pktlen) +{ + PKT_SET_SRC(p, PKT_SRC_WIRE); + + p->datalink = ptv->datalink; + + p->ipfw_v.ipfw_index = ptv->ipfw_index; + p->ipfw_v.ipfw_sin = *sin; + + PacketCopyData(p, pkt, pktlen); +} + TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) { SCEnter(); @@ -232,6 +280,7 @@ TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) IPFWThreadVars *ptv = (IPFWThreadVars *)data; IPFWQueueVars *nq = NULL; uint8_t pkt[IP_MAXPACKET]; + struct sockaddr_in sin; int pktlen=0; struct pollfd IPFWpoll; struct timeval IPFWts; @@ -263,9 +312,7 @@ TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) continue; } - if ((pktlen = recvfrom(nq->fd, pkt, sizeof(pkt), 0, - (struct sockaddr *)&nq->ipfw_sin, - &nq->ipfw_sinlen)) == -1) { + if ((pktlen = IPFWRecv(nq->fd, pkt, sizeof(pkt), &sin)) == -1) { /* We received an error on socket read */ if (errno == EINTR || errno == EWOULDBLOCK) { /* Nothing for us to process */ @@ -287,7 +334,6 @@ TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) if (p == NULL) { SCReturnInt(TM_ECODE_FAILED); } - PKT_SET_SRC(p, PKT_SRC_WIRE); SCLogDebug("Received Packet Len: %d", pktlen); @@ -296,11 +342,7 @@ TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) ptv->pkts++; ptv->bytes += pktlen; - p->datalink = ptv->datalink; - - p->ipfw_v.ipfw_index = ptv->ipfw_index; - - PacketCopyData(p, pkt, pktlen); + IPFWPacketSetup(p, ptv, &sin, pkt, pktlen); SCLogDebug("Packet info: pkt_len: %" PRIu32 " (pkt %02x, pkt_data %02x)", GET_PKT_LEN(p), *pkt, *(GET_PKT_DATA(p))); @@ -330,6 +372,7 @@ TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot) TmEcode ReceiveIPFWThreadInit(ThreadVars *tv, const void *initdata, void **data) { struct timeval timev; + struct sockaddr_in sin; IPFWThreadVars *ntv = (IPFWThreadVars *) initdata; IPFWQueueVars *nq = IPFWGetQueue(ntv->ipfw_index); @@ -360,14 +403,13 @@ TmEcode ReceiveIPFWThreadInit(ThreadVars *tv, const void *initdata, void **data) SCReturnInt(TM_ECODE_FAILED); } - nq->ipfw_sinlen=sizeof(nq->ipfw_sin); - memset(&nq->ipfw_sin, 0, nq->ipfw_sinlen); - nq->ipfw_sin.sin_family = PF_INET; - nq->ipfw_sin.sin_addr.s_addr = INADDR_ANY; - nq->ipfw_sin.sin_port = htons(nq->port_num); + memset(&sin, 0, sizeof(sin)); + sin.sin_family = PF_INET; + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = htons(nq->port_num); /* Bind that SOB */ - if (bind(nq->fd, (struct sockaddr *)&nq->ipfw_sin, nq->ipfw_sinlen) == -1) { + if (bind(nq->fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { SCLogError("Can't bind divert socket on port %d: %s", nq->port_num, strerror(errno)); SCReturnInt(TM_ECODE_FAILED); } @@ -560,8 +602,11 @@ TmEcode IPFWSetVerdict(ThreadVars *tv, IPFWThreadVars *ptv, Packet *p) } #endif + /* Reinject with the sockaddr this packet came with. In autofp the + * receive thread has read other packets from the socket by now. */ IPFWMutexLock(nq); - if (sendto(nq->fd, GET_PKT_DATA(p), GET_PKT_LEN(p), 0,(struct sockaddr *)&nq->ipfw_sin, nq->ipfw_sinlen) == -1) { + if (sendto(nq->fd, GET_PKT_DATA(p), GET_PKT_LEN(p), 0, + (struct sockaddr *)&p->ipfw_v.ipfw_sin, sizeof(p->ipfw_v.ipfw_sin)) == -1) { int r = errno; switch (r) { default: @@ -596,7 +641,6 @@ TmEcode IPFWSetVerdict(ThreadVars *tv, IPFWThreadVars *ptv, Packet *p) /** * \brief This function handles the Verdict processing - * \todo Unit tests are needed for this module. * * * \param tv pointer to ThreadVars @@ -615,8 +659,7 @@ TmEcode VerdictIPFW(ThreadVars *tv, Packet *p, void *data) SCReturnInt(TM_ECODE_OK); } - /* This came from NFQ. - * If this is a tunnel packet we check if we are ready to verdict already. */ + /* If this is a tunnel packet we check if we are ready to verdict already. */ if (PacketIsTunnel(p)) { bool verdict = VerdictTunnelPacket(p); @@ -769,6 +812,279 @@ void *IPFWGetThread(int number) return (void *)&ipfw_t[number]; } +#ifdef UNITTESTS + +/* The tests use UDP sockets on the loopback address in place of a divert + * socket. On a UDP socket sendto() delivers a "reinjected" packet to the + * address in the sockaddr, so the peer that gets a packet shows which + * sockaddr the verdict used. */ + +/** sockets of a test: q stands in for the divert socket of queue 0, a and b + * are two peers that send packets to it */ +typedef struct IPFWTestSockets_ { + int q; + int a; + int b; + struct sockaddr_in q_addr; + struct sockaddr_in b_addr; + /* queue 0's socket and the number of queues before the test */ + int saved_fd; + uint16_t saved_port_num; +} IPFWTestSockets; + +/** + * \brief Whether a failed socket() means this host has no IPv4 socket to test on + * + * \param err errno of the failed socket() call + */ +static bool IPFWTestNoSocket(int err) +{ + return err == EAFNOSUPPORT || err == EPROTONOSUPPORT || err == EPERM || err == EACCES; +} + +/** + * \brief UDP socket bound to the loopback address, with a receive timeout + * + * \param fd set to the socket, or to -1 + * \param addr set to the address the socket is bound to + * + * \retval 1 ready + * \retval 0 this host has no IPv4 socket to test on + * \retval -1 failed + */ +static int IPFWTestSocket(int *fd, struct sockaddr_in *addr) +{ + const struct timeval timev = { .tv_sec = 2, .tv_usec = 0 }; + socklen_t len = sizeof(*addr); + struct sockaddr_in sin; + + *fd = socket(AF_INET, SOCK_DGRAM, 0); + if (*fd == -1) { + const int err = errno; + if (!IPFWTestNoSocket(err)) { + SCLogWarning("can't create an AF_INET socket: %s", strerror(err)); + return -1; + } + SCLogInfo("can't create an AF_INET socket: %s; nothing to test", strerror(err)); + return 0; + } + + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (bind(*fd, (struct sockaddr *)&sin, sizeof(sin)) == -1 || + getsockname(*fd, (struct sockaddr *)addr, &len) == -1 || + setsockopt(*fd, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev)) == -1) { + SCLogWarning("can't set up a UDP socket on the loopback address: %s", strerror(errno)); + close(*fd); + *fd = -1; + return -1; + } + return 1; +} + +static void IPFWTestTeardown(IPFWTestSockets *s) +{ + ipfw_q[0].fd = s->saved_fd; + receive_port_num = s->saved_port_num; + if (s->q != -1) + close(s->q); + if (s->a != -1) + close(s->a); + if (s->b != -1) + close(s->b); +} + +/** + * \brief Open the sockets of a test and make q the socket of queue 0 + * + * \retval 1 ready + * \retval 0 this host has no IPv4 socket to test on + * \retval -1 failed + */ +static int IPFWTestSetup(IPFWTestSockets *s) +{ + struct sockaddr_in a_addr; + + s->saved_fd = ipfw_q[0].fd; + s->saved_port_num = receive_port_num; + s->q = s->a = s->b = -1; + int r = IPFWTestSocket(&s->q, &s->q_addr); + if (r == 1) + r = IPFWTestSocket(&s->a, &a_addr); + if (r == 1) + r = IPFWTestSocket(&s->b, &s->b_addr); + if (r != 1) { + IPFWTestTeardown(s); + return r; + } + + ipfw_q[0].fd = s->q; + receive_port_num = 1; + return 1; +} + +/** + * \brief Send the one-byte packet \a c from \a from to the queue's socket and + * receive it into a new packet of queue 0, as ReceiveIPFWLoop() does + */ +static Packet *IPFWTestReceive(const IPFWTestSockets *s, int from, uint8_t c) +{ + /* queue 0's receive thread */ + const IPFWThreadVars ptv = { .datalink = DLT_RAW, .ipfw_index = 0 }; + struct sockaddr_in sin; + uint8_t buf[8]; + + if (sendto(from, &c, 1, 0, (const struct sockaddr *)&s->q_addr, sizeof(s->q_addr)) != 1) + return NULL; + if (IPFWRecv(s->q, buf, sizeof(buf), &sin) != 1) + return NULL; + + Packet *p = PacketGetFromAlloc(); + if (p == NULL) + return NULL; + IPFWPacketSetup(p, &ptv, &sin, buf, 1); + if (GET_PKT_LEN(p) != 1 || *GET_PKT_DATA(p) != c) { + PacketFree(p); + return NULL; + } + return p; +} + +/** true if the next packet \a fd receives is the one-byte packet \a c */ +static bool IPFWTestGot(int fd, uint8_t c) +{ + uint8_t buf[8]; + + return recv(fd, buf, sizeof(buf), 0) == 1 && buf[0] == c; +} + +/** + * \test A verdict reinjects a packet with the sockaddr that packet was + * received with, not with the one of the packet received last. In the + * autofp runmode the verdict runs in another thread, after the receive + * thread has read more packets from the socket. + */ +static int IPFWSetVerdictTest01(void) +{ + IPFWTestSockets s; + IPFWThreadVars ptv; + memset(&ptv, 0, sizeof(ptv)); + + const int ready = IPFWTestSetup(&s); + FAIL_IF(ready < 0); + if (ready == 0) { + PASS; + } + + /* a packet from a, then one from b; the verdicts follow both */ + Packet *pa = IPFWTestReceive(&s, s.a, 'a'); + FAIL_IF_NULL(pa); + Packet *pb = IPFWTestReceive(&s, s.b, 'b'); + FAIL_IF_NULL(pb); + FAIL_IF(IPFWSetVerdict(NULL, &ptv, pa) != TM_ECODE_OK); + FAIL_IF(IPFWSetVerdict(NULL, &ptv, pb) != TM_ECODE_OK); + FAIL_IF(ptv.accepted != 2); + FAIL_IF(ptv.dropped != 0); + + /* each one went back to the peer it came from */ + FAIL_IF_NOT(IPFWTestGot(s.a, 'a')); + FAIL_IF_NOT(IPFWTestGot(s.b, 'b')); + + PacketFree(pa); + PacketFree(pb); + IPFWTestTeardown(&s); + PASS; +} + +/** + * \test A dropped packet is not reinjected. + */ +static int IPFWSetVerdictTest02(void) +{ + IPFWTestSockets s; + IPFWThreadVars ptv; + memset(&ptv, 0, sizeof(ptv)); + + const int ready = IPFWTestSetup(&s); + FAIL_IF(ready < 0); + if (ready == 0) { + PASS; + } + + /* drop the first packet from a and accept the second */ + Packet *pd = IPFWTestReceive(&s, s.a, 'd'); + FAIL_IF_NULL(pd); + Packet *pe = IPFWTestReceive(&s, s.a, 'e'); + FAIL_IF_NULL(pe); + PacketDrop(pd, ACTION_DROP, PKT_DROP_REASON_RULES); + FAIL_IF(IPFWSetVerdict(NULL, &ptv, pd) != TM_ECODE_OK); + FAIL_IF(IPFWSetVerdict(NULL, &ptv, pe) != TM_ECODE_OK); + FAIL_IF(ptv.accepted != 1); + FAIL_IF(ptv.dropped != 1); + + /* the first packet a gets back is the second one */ + FAIL_IF_NOT(IPFWTestGot(s.a, 'e')); + + PacketFree(pd); + PacketFree(pe); + IPFWTestTeardown(&s); + PASS; +} + +/** + * \test The verdict of a tunnel packet reinjects the root packet, the one + * that was read from the socket, with the root's sockaddr. + */ +static int IPFWSetVerdictTest03(void) +{ + IPFWTestSockets s; + IPFWThreadVars ptv; + memset(&ptv, 0, sizeof(ptv)); + + const int ready = IPFWTestSetup(&s); + FAIL_IF(ready < 0); + if (ready == 0) { + PASS; + } + + /* the root came from a. Its child was never on the socket; give it b's + * sockaddr to show that it is not used. */ + Packet *root = IPFWTestReceive(&s, s.a, 'r'); + FAIL_IF_NULL(root); + Packet *child = PacketGetFromAlloc(); + FAIL_IF_NULL(child); + child->ipfw_v.ipfw_index = 0; + child->ipfw_v.ipfw_sin = s.b_addr; + child->root = root; + child->ttype = PacketTunnelChild; + root->ttype = PacketTunnelRoot; + TUNNEL_INCR_PKT_TPR(child); + + /* no verdict for the root while its child is outstanding */ + FAIL_IF(VerdictIPFW(NULL, root, &ptv) != TM_ECODE_OK); + FAIL_IF(ptv.accepted != 0); + + /* the root is done, the verdict comes with the child */ + PacketTunnelSetVerdicted(root); + FAIL_IF(VerdictIPFW(NULL, child, &ptv) != TM_ECODE_OK); + FAIL_IF(ptv.accepted != 1); + FAIL_IF_NOT(IPFWTestGot(s.a, 'r')); + + PacketFree(child); + PacketFree(root); + IPFWTestTeardown(&s); + PASS; +} + +static void SourceIPFWRegisterTests(void) +{ + UtRegisterTest("IPFWSetVerdictTest01", IPFWSetVerdictTest01); + UtRegisterTest("IPFWSetVerdictTest02", IPFWSetVerdictTest02); + UtRegisterTest("IPFWSetVerdictTest03", IPFWSetVerdictTest03); +} +#endif /* UNITTESTS */ + #endif /* End ifdef IPFW */ /* eof */ diff --git a/src/source-ipfw.h b/src/source-ipfw.h index d74b73e52..d5d666b37 100644 --- a/src/source-ipfw.h +++ b/src/source-ipfw.h @@ -27,10 +27,14 @@ #define IPFW_MAX_QUEUE 16 -/* per packet IPFW vars (Not used) */ +/* per packet IPFW vars */ typedef struct IPFWPacketVars_ { int ipfw_index; + /* divert sockaddr the packet was received with. The packet is reinjected + * with it: it tells the kernel the direction, the interface and where in + * the ruleset to resume. */ + struct sockaddr_in ipfw_sin; } IPFWPacketVars; typedef struct IPFWQueueVars_ @@ -42,8 +46,6 @@ typedef struct IPFWQueueVars_ uint16_t port_num; /* position into the ipfw queue var array */ uint16_t ipfw_index; - struct sockaddr_in ipfw_sin; - socklen_t ipfw_sinlen; #ifdef DBG_PERF int dbg_maxreadsize;