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() */
82 u_int packets_nobufs; /* ENOBUFS counter */
85 static int nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict);
89 netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
91 struct pcap_netfilter *handlep = handle->priv;
92 const unsigned char *buf;
96 /* ignore interrupt system call error */
98 len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
99 if (handle->break_loop) {
100 handle->break_loop = 0;
103 if(errno == ENOBUFS) handlep->packets_nobufs++;
104 } while ((len == -1) && (errno == EINTR || errno == ENOBUFS));
107 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno));
111 buf = (unsigned char *)handle->buffer;
112 while ((u_int)len >= NLMSG_SPACE(0)) {
113 const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf;
115 nftype_t type = OTHER;
117 if (nlh->nlmsg_len < sizeof(struct nlmsghdr) || (u_int)len < nlh->nlmsg_len) {
118 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Message truncated: (got: %d) (nlmsg_len: %u)", len, nlh->nlmsg_len);
122 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
123 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
125 else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
126 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
130 const unsigned char *payload = NULL;
131 struct pcap_pkthdr pkth;
133 const struct nfgenmsg *nfg = NULL;
136 if (handle->linktype != DLT_NFLOG) {
137 const struct nfattr *payload_attr = NULL;
139 if (nlh->nlmsg_len < HDR_LENGTH) {
140 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Malformed message: (nlmsg_len: %u)", nlh->nlmsg_len);
144 nfg = NLMSG_DATA(nlh);
145 if (nlh->nlmsg_len > HDR_LENGTH) {
146 struct nfattr *attr = NFM_NFA(nfg);
147 int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(HDR_LENGTH);
149 while (NFA_OK(attr, attr_len)) {
150 if (type == NFQUEUE) {
151 switch (NFA_TYPE(attr)) {
152 case NFQA_PACKET_HDR:
154 const struct nfqnl_msg_packet_hdr *pkt_hdr = (const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
156 id = ntohl(pkt_hdr->packet_id);
164 } else if (type == NFLOG) {
165 switch (NFA_TYPE(attr)) {
171 attr = NFA_NEXT(attr, attr_len);
176 payload = NFA_DATA(payload_attr);
177 pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
181 payload = NLMSG_DATA(nlh);
182 pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
186 /* pkth.caplen = min (payload_len, handle->snapshot); */
188 gettimeofday(&pkth.ts, NULL);
189 if (handle->fcode.bf_insns == NULL ||
190 bpf_filter(handle->fcode.bf_insns, payload, pkth.len, pkth.caplen))
192 handlep->packets_read++;
193 callback(user, &pkth, payload);
198 if (type == NFQUEUE) {
199 /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
200 /* if type == NFQUEUE, handle->linktype is always != DLT_NFLOG,
201 so nfg is always initialized to NLMSG_DATA(nlh). */
203 nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
207 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
208 if (msg_len > (u_int)len)
209 msg_len = (u_int)len;
218 netfilter_set_datalink(pcap_t *handle, int dlt)
220 handle->linktype = dlt;
225 netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
227 struct pcap_netfilter *handlep = handle->priv;
229 stats->ps_recv = handlep->packets_read;
230 stats->ps_drop = handlep->packets_nobufs;
231 stats->ps_ifdrop = 0;
236 netfilter_inject_linux(pcap_t *handle, const void *buf, size_t size)
238 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on netfilter devices");
249 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)
251 char buf[1024] __attribute__ ((aligned));
253 struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
254 struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr));
256 struct sockaddr_nl snl;
257 static unsigned int seq_id;
263 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg));
264 nlh->nlmsg_type = msg_type;
265 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
266 nlh->nlmsg_pid = 0; /* to kernel */
267 nlh->nlmsg_seq = seq_id;
269 nfg->nfgen_family = family;
270 nfg->version = NFNETLINK_V0;
271 nfg->res_id = htons(res_id);
274 struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
276 nfa->nfa_type = mynfa->nfa_type;
277 nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len);
278 memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len);
279 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
282 memset(&snl, 0, sizeof(snl));
283 snl.nl_family = AF_NETLINK;
285 if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1)
291 /* waiting for reply loop */
293 socklen_t addrlen = sizeof(snl);
296 /* ignore interrupt system call error */
298 len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
299 } while ((len == -1) && (errno == EINTR));
304 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
309 nlh = (struct nlmsghdr *) buf;
310 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
313 while ((u_int)len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, len)) {
314 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
315 if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) {
319 errno = -(*((int *)NLMSG_DATA(nlh)));
320 return (errno == 0) ? 0 : -1;
322 nlh = NLMSG_NEXT(nlh, len);
326 return -1; /* never here */
330 nflog_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
332 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa);
336 nflog_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int8_t family)
338 struct nfulnl_msg_config_cmd msg;
339 struct my_nfattr nfa;
344 nfa.nfa_type = NFULA_CFG_CMD;
345 nfa.nfa_len = sizeof(msg);
347 return nflog_send_config_msg(handle, family, group_id, &nfa);
351 nflog_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
353 struct nfulnl_msg_config_mode msg;
354 struct my_nfattr nfa;
356 msg.copy_range = htonl(copy_range);
357 msg.copy_mode = copy_mode;
360 nfa.nfa_type = NFULA_CFG_MODE;
361 nfa.nfa_len = sizeof(msg);
363 return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
367 nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict)
369 struct nfqnl_msg_verdict_hdr msg;
370 struct my_nfattr nfa;
373 msg.verdict = htonl(verdict);
376 nfa.nfa_type = NFQA_VERDICT_HDR;
377 nfa.nfa_len = sizeof(msg);
379 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa);
383 nfqueue_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
385 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa);
389 nfqueue_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int16_t pf)
391 struct nfqnl_msg_config_cmd msg;
392 struct my_nfattr nfa;
398 nfa.nfa_type = NFQA_CFG_CMD;
399 nfa.nfa_len = sizeof(msg);
401 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
405 nfqueue_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
407 struct nfqnl_msg_config_params msg;
408 struct my_nfattr nfa;
410 msg.copy_range = htonl(copy_range);
411 msg.copy_mode = copy_mode;
414 nfa.nfa_type = NFQA_CFG_PARAMS;
415 nfa.nfa_len = sizeof(msg);
417 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
421 netfilter_activate(pcap_t* handle)
423 const char *dev = handle->opt.device;
424 unsigned short groups[32];
426 nftype_t type = OTHER;
429 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
430 dev += strlen(NFLOG_IFACE);
433 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
434 dev += strlen(NFQUEUE_IFACE);
438 if (type != OTHER && *dev == ':') {
444 if (group_count == 32) {
445 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
446 "Maximum 32 netfilter groups! dev: %s",
451 group_id = strtol(dev, &end_dev, 0);
452 if (end_dev != dev) {
453 if (group_id < 0 || group_id > 65535) {
454 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
455 "Netfilter group range from 0 to 65535 (got %ld)",
460 groups[group_count++] = (unsigned short) group_id;
469 if (type == OTHER || *dev) {
470 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
471 "Can't get netfilter group(s) index from %s",
476 /* if no groups, add default: 0 */
482 /* Initialize some components of the pcap structure. */
483 handle->bufsize = 128 + handle->snapshot;
485 handle->read_op = netfilter_read_linux;
486 handle->inject_op = netfilter_inject_linux;
487 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
488 handle->setdirection_op = NULL;
489 handle->set_datalink_op = netfilter_set_datalink;
490 handle->getnonblock_op = pcap_getnonblock_fd;
491 handle->setnonblock_op = pcap_setnonblock_fd;
492 handle->stats_op = netfilter_stats_linux;
494 /* Create netlink socket */
495 handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
496 if (handle->fd < 0) {
497 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno));
502 handle->linktype = DLT_NFLOG;
503 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
504 if (handle->dlt_list != NULL) {
505 handle->dlt_list[0] = DLT_NFLOG;
506 handle->dlt_list[1] = DLT_IPV4;
507 handle->dlt_count = 2;
511 handle->linktype = DLT_IPV4;
513 handle->buffer = malloc(handle->bufsize);
514 if (!handle->buffer) {
515 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", pcap_strerror(errno));
520 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
521 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
525 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
526 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
530 /* Bind socket to the nflog groups */
531 for (i = 0; i < group_count; i++) {
532 if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
533 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
537 if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) {
538 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno));
544 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
545 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
549 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
550 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
554 /* Bind socket to the nfqueue groups */
555 for (i = 0; i < group_count; i++) {
556 if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
557 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
561 if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) {
562 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno));
568 if (handle->opt.rfmon) {
570 * Monitor mode doesn't apply to netfilter devices.
572 pcap_cleanup_live_common(handle);
573 return PCAP_ERROR_RFMON_NOTSUP;
576 if (handle->opt.buffer_size != 0) {
578 * Set the socket buffer size to the specified value.
580 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) {
581 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "SO_RCVBUF: %s", pcap_strerror(errno));
586 handle->selectable_fd = handle->fd;
590 pcap_cleanup_live_common(handle);
595 netfilter_create(const char *device, char *ebuf, int *is_ours)
600 /* Does this look like an netfilter device? */
601 cp = strrchr(device, '/');
605 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
606 if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0)
607 cp += sizeof NFLOG_IFACE - 1;
608 else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0)
609 cp += sizeof NFQUEUE_IFACE - 1;
611 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
617 * Yes - is that either the end of the name, or is it followed
620 if (*cp != ':' && *cp != '\0') {
626 /* OK, it's probably ours. */
629 p = pcap_create_common(ebuf, sizeof (struct pcap_netfilter));
633 p->activate_op = netfilter_activate;
638 netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
642 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
644 /* if netlink is not supported this is not fatal */
645 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
647 pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s",
648 errno, pcap_strerror(errno));
653 if (pcap_add_if(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) < 0)
655 if (pcap_add_if(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) < 0)