2 * Copyright (c) 2011 Jakub Zawadzki
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #ifdef NEED_STRERROR_H
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
50 #include <netinet/in.h>
51 #include <linux/types.h>
53 #include <linux/netlink.h>
54 #include <linux/netfilter.h>
55 #include <linux/netfilter/nfnetlink.h>
56 #include <linux/netfilter/nfnetlink_log.h>
57 #include <linux/netfilter/nfnetlink_queue.h>
59 /* NOTE: if your program drops privilages after pcap_activate() it WON'T work with nfqueue.
60 * It took me quite some time to debug ;/
62 * Sending any data to nfnetlink socket requires CAP_NET_ADMIN privilages,
63 * and in nfqueue we need to send verdict reply after recving packet.
65 * In tcpdump you can disable dropping privilages with -Z root
68 #include "pcap-netfilter-linux.h"
70 #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
72 #define NFLOG_IFACE "nflog"
73 #define NFQUEUE_IFACE "nfqueue"
75 typedef enum { OTHER = -1, NFLOG, NFQUEUE } nftype_t;
78 * Private data for capturing on Linux netfilter sockets.
80 struct pcap_netfilter {
81 u_int packets_read; /* count of packets read with recvfrom() */
84 static int nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict);
87 netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
89 struct pcap_netfilter *handlep = handle->priv;
90 const unsigned char *buf;
94 /* ignore interrupt system call error */
96 len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
97 if (handle->break_loop) {
98 handle->break_loop = 0;
101 } while ((len == -1) && (errno == EINTR));
104 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno));
108 buf = handle->buffer;
109 while (len >= NLMSG_SPACE(0)) {
110 const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf;
112 nftype_t type = OTHER;
114 if (nlh->nlmsg_len < sizeof(struct nlmsghdr) || len < nlh->nlmsg_len) {
115 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Message truncated: (got: %d) (nlmsg_len: %u)", len, nlh->nlmsg_len);
119 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
120 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
122 else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
123 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
127 const unsigned char *payload = NULL;
128 struct pcap_pkthdr pkth;
130 const struct nfgenmsg *nfg;
133 if (handle->linktype != DLT_NFLOG) {
134 const struct nfattr *payload_attr = NULL;
136 if (nlh->nlmsg_len < HDR_LENGTH) {
137 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Malformed message: (nlmsg_len: %u)", nlh->nlmsg_len);
141 nfg = NLMSG_DATA(nlh);
142 if (nlh->nlmsg_len > HDR_LENGTH) {
143 struct nfattr *attr = NFM_NFA(nfg);
144 int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(HDR_LENGTH);
146 while (NFA_OK(attr, attr_len)) {
147 if (type == NFQUEUE) {
148 switch (NFA_TYPE(attr)) {
149 case NFQA_PACKET_HDR:
151 const struct nfqnl_msg_packet_hdr *pkt_hdr = (const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
153 id = ntohl(pkt_hdr->packet_id);
161 } else if (type == NFLOG) {
162 switch (NFA_TYPE(attr)) {
168 attr = NFA_NEXT(attr, attr_len);
173 payload = NFA_DATA(payload_attr);
174 pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
178 payload = NLMSG_DATA(nlh);
179 pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
183 /* pkth.caplen = min (payload_len, handle->snapshot); */
185 gettimeofday(&pkth.ts, NULL);
186 if (handle->fcode.bf_insns == NULL ||
187 bpf_filter(handle->fcode.bf_insns, payload, pkth.len, pkth.caplen))
189 handlep->packets_read++;
190 callback(user, &pkth, payload);
195 if (type == NFQUEUE) {
196 /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
197 nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
201 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
212 netfilter_set_datalink(pcap_t *handle, int dlt)
214 handle->linktype = dlt;
219 netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
221 struct pcap_netfilter *handlep = handle->priv;
223 stats->ps_recv = handlep->packets_read;
225 stats->ps_ifdrop = 0;
230 netfilter_inject_linux(pcap_t *handle, const void *buf, size_t size)
232 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on netfilter devices");
243 netfilter_send_config_msg(const pcap_t *handle, u_int16_t msg_type, int ack, u_int8_t family, u_int16_t res_id, const struct my_nfattr *mynfa)
245 char buf[1024] __attribute__ ((aligned));
247 struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
248 struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr));
250 struct sockaddr_nl snl;
251 static unsigned int seq_id;
257 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg));
258 nlh->nlmsg_type = msg_type;
259 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
260 nlh->nlmsg_pid = 0; /* to kernel */
261 nlh->nlmsg_seq = seq_id;
263 nfg->nfgen_family = family;
264 nfg->version = NFNETLINK_V0;
265 nfg->res_id = htons(res_id);
268 struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
270 nfa->nfa_type = mynfa->nfa_type;
271 nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len);
272 memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len);
273 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
276 memset(&snl, 0, sizeof(snl));
277 snl.nl_family = AF_NETLINK;
279 if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1)
285 /* waiting for reply loop */
287 socklen_t addrlen = sizeof(snl);
290 /* ignore interrupt system call error */
292 len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
293 } while ((len == -1) && (errno == EINTR));
298 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
303 nlh = (struct nlmsghdr *) buf;
304 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
307 while (len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, len)) {
308 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
309 if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) {
313 errno = -(*((int *)NLMSG_DATA(nlh)));
314 return (errno == 0) ? 0 : -1;
316 nlh = NLMSG_NEXT(nlh, len);
320 return -1; /* never here */
324 nflog_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
326 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa);
330 nflog_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int8_t family)
332 struct nfulnl_msg_config_cmd msg;
333 struct my_nfattr nfa;
338 nfa.nfa_type = NFULA_CFG_CMD;
339 nfa.nfa_len = sizeof(msg);
341 return nflog_send_config_msg(handle, family, group_id, &nfa);
345 nflog_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
347 struct nfulnl_msg_config_mode msg;
348 struct my_nfattr nfa;
350 msg.copy_range = htonl(copy_range);
351 msg.copy_mode = copy_mode;
354 nfa.nfa_type = NFULA_CFG_MODE;
355 nfa.nfa_len = sizeof(msg);
357 return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
361 nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict)
363 struct nfqnl_msg_verdict_hdr msg;
364 struct my_nfattr nfa;
367 msg.verdict = htonl(verdict);
370 nfa.nfa_type = NFQA_VERDICT_HDR;
371 nfa.nfa_len = sizeof(msg);
373 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa);
377 nfqueue_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
379 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa);
383 nfqueue_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int16_t pf)
385 struct nfqnl_msg_config_cmd msg;
386 struct my_nfattr nfa;
392 nfa.nfa_type = NFQA_CFG_CMD;
393 nfa.nfa_len = sizeof(msg);
395 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
399 nfqueue_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
401 struct nfqnl_msg_config_params msg;
402 struct my_nfattr nfa;
404 msg.copy_range = htonl(copy_range);
405 msg.copy_mode = copy_mode;
408 nfa.nfa_type = NFQA_CFG_PARAMS;
409 nfa.nfa_len = sizeof(msg);
411 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
415 netfilter_activate(pcap_t* handle)
417 const char *dev = handle->opt.source;
418 unsigned short groups[32];
420 nftype_t type = OTHER;
423 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
424 dev += strlen(NFLOG_IFACE);
427 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
428 dev += strlen(NFQUEUE_IFACE);
432 if (type != OTHER && *dev == ':') {
438 if (group_count == 32) {
439 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
440 "Maximum 32 netfilter groups! dev: %s",
445 group_id = strtol(dev, &end_dev, 0);
446 if (end_dev != dev) {
447 if (group_id < 0 || group_id > 65535) {
448 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
449 "Netfilter group range from 0 to 65535 (got %ld)",
454 groups[group_count++] = (unsigned short) group_id;
463 if (type == OTHER || *dev) {
464 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
465 "Can't get netfilter group(s) index from %s",
470 /* if no groups, add default: 0 */
476 /* Initialize some components of the pcap structure. */
477 handle->bufsize = 128 + handle->snapshot;
479 handle->read_op = netfilter_read_linux;
480 handle->inject_op = netfilter_inject_linux;
481 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
482 handle->setdirection_op = NULL;
483 handle->set_datalink_op = netfilter_set_datalink;
484 handle->getnonblock_op = pcap_getnonblock_fd;
485 handle->setnonblock_op = pcap_setnonblock_fd;
486 handle->stats_op = netfilter_stats_linux;
488 /* Create netlink socket */
489 handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
490 if (handle->fd < 0) {
491 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno));
496 handle->linktype = DLT_NFLOG;
497 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
498 if (handle->dlt_list != NULL) {
499 handle->dlt_list[0] = DLT_NFLOG;
500 handle->dlt_list[1] = DLT_IPV4;
501 handle->dlt_count = 2;
505 handle->linktype = DLT_IPV4;
507 handle->buffer = malloc(handle->bufsize);
508 if (!handle->buffer) {
509 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", pcap_strerror(errno));
514 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
515 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
519 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
520 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
524 /* Bind socket to the nflog groups */
525 for (i = 0; i < group_count; i++) {
526 if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
527 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
531 if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) {
532 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno));
538 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
539 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
543 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
544 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
548 /* Bind socket to the nfqueue groups */
549 for (i = 0; i < group_count; i++) {
550 if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
551 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
555 if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) {
556 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno));
562 if (handle->opt.rfmon) {
564 * Monitor mode doesn't apply to netfilter devices.
566 pcap_cleanup_live_common(handle);
567 return PCAP_ERROR_RFMON_NOTSUP;
570 if (handle->opt.buffer_size != 0) {
572 * Set the socket buffer size to the specified value.
574 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) {
575 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "SO_RCVBUF: %s", pcap_strerror(errno));
580 handle->selectable_fd = handle->fd;
584 pcap_cleanup_live_common(handle);
589 netfilter_create(const char *device, char *ebuf, int *is_ours)
594 /* Does this look like an netfilter device? */
595 cp = strrchr(device, '/');
599 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
600 if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0)
601 cp += sizeof NFLOG_IFACE - 1;
602 else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0)
603 cp += sizeof NFQUEUE_IFACE - 1;
605 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
611 * Yes - is that either the end of the name, or is it followed
614 if (*cp != ':' && *cp != '\0') {
620 /* OK, it's probably ours. */
623 p = pcap_create_common(device, ebuf, sizeof (struct pcap_netfilter));
627 p->activate_op = netfilter_activate;
632 netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
636 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
638 /* if netlink is not supported this is not fatal */
639 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
641 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s",
642 errno, pcap_strerror(errno));
647 if (pcap_add_if(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) < 0)
649 if (pcap_add_if(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) < 0)