1 /* $NetBSD: pcap-netfilter-linux.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
4 * Copyright (c) 2011 Jakub Zawadzki
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: pcap-netfilter-linux.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
42 #ifdef NEED_STRERROR_H
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
55 #include <netinet/in.h>
56 #include <linux/types.h>
58 #include <linux/netlink.h>
59 #include <linux/netfilter.h>
60 #include <linux/netfilter/nfnetlink.h>
61 #include <linux/netfilter/nfnetlink_log.h>
62 #include <linux/netfilter/nfnetlink_queue.h>
64 /* NOTE: if your program drops privilages after pcap_activate() it WON'T work with nfqueue.
65 * It took me quite some time to debug ;/
67 * Sending any data to nfnetlink socket requires CAP_NET_ADMIN privilages,
68 * and in nfqueue we need to send verdict reply after recving packet.
70 * In tcpdump you can disable dropping privilages with -Z root
73 #include "pcap-netfilter-linux.h"
75 #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
77 #define NFLOG_IFACE "nflog"
78 #define NFQUEUE_IFACE "nfqueue"
80 typedef enum { OTHER
= -1, NFLOG
, NFQUEUE
} nftype_t
;
83 * Private data for capturing on Linux netfilter sockets.
85 struct pcap_netfilter
{
86 u_int packets_read
; /* count of packets read with recvfrom() */
89 static int nfqueue_send_verdict(const pcap_t
*handle
, u_int16_t group_id
, u_int32_t id
, u_int32_t verdict
);
92 netfilter_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
94 struct pcap_netfilter
*handlep
= handle
->priv
;
95 const unsigned char *buf
;
99 /* ignore interrupt system call error */
101 len
= recv(handle
->fd
, handle
->buffer
, handle
->bufsize
, 0);
102 if (handle
->break_loop
) {
103 handle
->break_loop
= 0;
106 } while ((len
== -1) && (errno
== EINTR
));
109 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't receive packet %d:%s", errno
, pcap_strerror(errno
));
113 buf
= handle
->buffer
;
114 while (len
>= NLMSG_SPACE(0)) {
115 const struct nlmsghdr
*nlh
= (const struct nlmsghdr
*) buf
;
117 nftype_t type
= OTHER
;
119 if (nlh
->nlmsg_len
< sizeof(struct nlmsghdr
) || len
< nlh
->nlmsg_len
) {
120 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Message truncated: (got: %d) (nlmsg_len: %u)", len
, nlh
->nlmsg_len
);
124 if (NFNL_SUBSYS_ID(nlh
->nlmsg_type
) == NFNL_SUBSYS_ULOG
&&
125 NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFULNL_MSG_PACKET
)
127 else if (NFNL_SUBSYS_ID(nlh
->nlmsg_type
) == NFNL_SUBSYS_QUEUE
&&
128 NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFQNL_MSG_PACKET
)
132 const unsigned char *payload
= NULL
;
133 struct pcap_pkthdr pkth
;
135 const struct nfgenmsg
*nfg
= NULL
;
138 if (handle
->linktype
!= DLT_NFLOG
) {
139 const struct nfattr
*payload_attr
= NULL
;
141 if (nlh
->nlmsg_len
< HDR_LENGTH
) {
142 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Malformed message: (nlmsg_len: %u)", nlh
->nlmsg_len
);
146 nfg
= NLMSG_DATA(nlh
);
147 if (nlh
->nlmsg_len
> HDR_LENGTH
) {
148 struct nfattr
*attr
= NFM_NFA(nfg
);
149 int attr_len
= nlh
->nlmsg_len
- NLMSG_ALIGN(HDR_LENGTH
);
151 while (NFA_OK(attr
, attr_len
)) {
152 if (type
== NFQUEUE
) {
153 switch (NFA_TYPE(attr
)) {
154 case NFQA_PACKET_HDR
:
156 const struct nfqnl_msg_packet_hdr
*pkt_hdr
= (const struct nfqnl_msg_packet_hdr
*) NFA_DATA(attr
);
158 id
= ntohl(pkt_hdr
->packet_id
);
166 } else if (type
== NFLOG
) {
167 switch (NFA_TYPE(attr
)) {
173 attr
= NFA_NEXT(attr
, attr_len
);
178 payload
= NFA_DATA(payload_attr
);
179 pkth
.len
= pkth
.caplen
= NFA_PAYLOAD(payload_attr
);
183 payload
= NLMSG_DATA(nlh
);
184 pkth
.caplen
= pkth
.len
= nlh
->nlmsg_len
-NLMSG_ALIGN(sizeof(struct nlmsghdr
));
188 /* pkth.caplen = min (payload_len, handle->snapshot); */
190 gettimeofday(&pkth
.ts
, NULL
);
191 if (handle
->fcode
.bf_insns
== NULL
||
192 bpf_filter(handle
->fcode
.bf_insns
, payload
, pkth
.len
, pkth
.caplen
))
194 handlep
->packets_read
++;
195 callback(user
, &pkth
, payload
);
200 if (type
== NFQUEUE
) {
201 /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
202 /* if type == NFQUEUE, handle->linktype is always != DLT_NFLOG,
203 so nfg is always initialized to NLMSG_DATA(nlh). */
205 nfqueue_send_verdict(handle
, ntohs(nfg
->res_id
), id
, NF_ACCEPT
);
209 msg_len
= NLMSG_ALIGN(nlh
->nlmsg_len
);
220 netfilter_set_datalink(pcap_t
*handle
, int dlt
)
222 handle
->linktype
= dlt
;
227 netfilter_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
229 struct pcap_netfilter
*handlep
= handle
->priv
;
231 stats
->ps_recv
= handlep
->packets_read
;
233 stats
->ps_ifdrop
= 0;
238 netfilter_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
240 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on netfilter devices");
251 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
)
253 char buf
[1024] __attribute__ ((aligned
));
255 struct nlmsghdr
*nlh
= (struct nlmsghdr
*) buf
;
256 struct nfgenmsg
*nfg
= (struct nfgenmsg
*) (buf
+ sizeof(struct nlmsghdr
));
258 struct sockaddr_nl snl
;
259 static unsigned int seq_id
;
265 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nfgenmsg
));
266 nlh
->nlmsg_type
= msg_type
;
267 nlh
->nlmsg_flags
= NLM_F_REQUEST
| (ack
? NLM_F_ACK
: 0);
268 nlh
->nlmsg_pid
= 0; /* to kernel */
269 nlh
->nlmsg_seq
= seq_id
;
271 nfg
->nfgen_family
= family
;
272 nfg
->version
= NFNETLINK_V0
;
273 nfg
->res_id
= htons(res_id
);
276 struct nfattr
*nfa
= (struct nfattr
*) (buf
+ NLMSG_ALIGN(nlh
->nlmsg_len
));
278 nfa
->nfa_type
= mynfa
->nfa_type
;
279 nfa
->nfa_len
= NFA_LENGTH(mynfa
->nfa_len
);
280 memcpy(NFA_DATA(nfa
), mynfa
->data
, mynfa
->nfa_len
);
281 nlh
->nlmsg_len
= NLMSG_ALIGN(nlh
->nlmsg_len
) + NFA_ALIGN(nfa
->nfa_len
);
284 memset(&snl
, 0, sizeof(snl
));
285 snl
.nl_family
= AF_NETLINK
;
287 if (sendto(handle
->fd
, nlh
, nlh
->nlmsg_len
, 0, (struct sockaddr
*) &snl
, sizeof(snl
)) == -1)
293 /* waiting for reply loop */
295 socklen_t addrlen
= sizeof(snl
);
298 /* ignore interrupt system call error */
300 len
= recvfrom(handle
->fd
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &snl
, &addrlen
);
301 } while ((len
== -1) && (errno
== EINTR
));
306 if (addrlen
!= sizeof(snl
) || snl
.nl_family
!= AF_NETLINK
) {
311 nlh
= (struct nlmsghdr
*) buf
;
312 if (snl
.nl_pid
!= 0 || seq_id
!= nlh
->nlmsg_seq
) /* if not from kernel or wrong sequence skip */
315 while (len
>= NLMSG_SPACE(0) && NLMSG_OK(nlh
, len
)) {
316 if (nlh
->nlmsg_type
== NLMSG_ERROR
|| (nlh
->nlmsg_type
== NLMSG_DONE
&& nlh
->nlmsg_flags
& NLM_F_MULTI
)) {
317 if (nlh
->nlmsg_len
< NLMSG_ALIGN(sizeof(struct nlmsgerr
))) {
321 errno
= -(*((int *)NLMSG_DATA(nlh
)));
322 return (errno
== 0) ? 0 : -1;
324 nlh
= NLMSG_NEXT(nlh
, len
);
328 return -1; /* never here */
332 nflog_send_config_msg(const pcap_t
*handle
, u_int8_t family
, u_int16_t group_id
, const struct my_nfattr
*mynfa
)
334 return netfilter_send_config_msg(handle
, (NFNL_SUBSYS_ULOG
<< 8) | NFULNL_MSG_CONFIG
, 1, family
, group_id
, mynfa
);
338 nflog_send_config_cmd(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t cmd
, u_int8_t family
)
340 struct nfulnl_msg_config_cmd msg
;
341 struct my_nfattr nfa
;
346 nfa
.nfa_type
= NFULA_CFG_CMD
;
347 nfa
.nfa_len
= sizeof(msg
);
349 return nflog_send_config_msg(handle
, family
, group_id
, &nfa
);
353 nflog_send_config_mode(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t copy_mode
, u_int32_t copy_range
)
355 struct nfulnl_msg_config_mode msg
;
356 struct my_nfattr nfa
;
358 msg
.copy_range
= htonl(copy_range
);
359 msg
.copy_mode
= copy_mode
;
362 nfa
.nfa_type
= NFULA_CFG_MODE
;
363 nfa
.nfa_len
= sizeof(msg
);
365 return nflog_send_config_msg(handle
, AF_UNSPEC
, group_id
, &nfa
);
369 nfqueue_send_verdict(const pcap_t
*handle
, u_int16_t group_id
, u_int32_t id
, u_int32_t verdict
)
371 struct nfqnl_msg_verdict_hdr msg
;
372 struct my_nfattr nfa
;
375 msg
.verdict
= htonl(verdict
);
378 nfa
.nfa_type
= NFQA_VERDICT_HDR
;
379 nfa
.nfa_len
= sizeof(msg
);
381 return netfilter_send_config_msg(handle
, (NFNL_SUBSYS_QUEUE
<< 8) | NFQNL_MSG_VERDICT
, 0, AF_UNSPEC
, group_id
, &nfa
);
385 nfqueue_send_config_msg(const pcap_t
*handle
, u_int8_t family
, u_int16_t group_id
, const struct my_nfattr
*mynfa
)
387 return netfilter_send_config_msg(handle
, (NFNL_SUBSYS_QUEUE
<< 8) | NFQNL_MSG_CONFIG
, 1, family
, group_id
, mynfa
);
391 nfqueue_send_config_cmd(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t cmd
, u_int16_t pf
)
393 struct nfqnl_msg_config_cmd msg
;
394 struct my_nfattr nfa
;
400 nfa
.nfa_type
= NFQA_CFG_CMD
;
401 nfa
.nfa_len
= sizeof(msg
);
403 return nfqueue_send_config_msg(handle
, AF_UNSPEC
, group_id
, &nfa
);
407 nfqueue_send_config_mode(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t copy_mode
, u_int32_t copy_range
)
409 struct nfqnl_msg_config_params msg
;
410 struct my_nfattr nfa
;
412 msg
.copy_range
= htonl(copy_range
);
413 msg
.copy_mode
= copy_mode
;
416 nfa
.nfa_type
= NFQA_CFG_PARAMS
;
417 nfa
.nfa_len
= sizeof(msg
);
419 return nfqueue_send_config_msg(handle
, AF_UNSPEC
, group_id
, &nfa
);
423 netfilter_activate(pcap_t
* handle
)
425 const char *dev
= handle
->opt
.source
;
426 unsigned short groups
[32];
428 nftype_t type
= OTHER
;
431 if (strncmp(dev
, NFLOG_IFACE
, strlen(NFLOG_IFACE
)) == 0) {
432 dev
+= strlen(NFLOG_IFACE
);
435 } else if (strncmp(dev
, NFQUEUE_IFACE
, strlen(NFQUEUE_IFACE
)) == 0) {
436 dev
+= strlen(NFQUEUE_IFACE
);
440 if (type
!= OTHER
&& *dev
== ':') {
446 if (group_count
== 32) {
447 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
448 "Maximum 32 netfilter groups! dev: %s",
453 group_id
= strtol(dev
, &end_dev
, 0);
454 if (end_dev
!= dev
) {
455 if (group_id
< 0 || group_id
> 65535) {
456 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
457 "Netfilter group range from 0 to 65535 (got %ld)",
462 groups
[group_count
++] = (unsigned short) group_id
;
471 if (type
== OTHER
|| *dev
) {
472 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
473 "Can't get netfilter group(s) index from %s",
478 /* if no groups, add default: 0 */
484 /* Initialize some components of the pcap structure. */
485 handle
->bufsize
= 128 + handle
->snapshot
;
487 handle
->read_op
= netfilter_read_linux
;
488 handle
->inject_op
= netfilter_inject_linux
;
489 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
490 handle
->setdirection_op
= NULL
;
491 handle
->set_datalink_op
= netfilter_set_datalink
;
492 handle
->getnonblock_op
= pcap_getnonblock_fd
;
493 handle
->setnonblock_op
= pcap_setnonblock_fd
;
494 handle
->stats_op
= netfilter_stats_linux
;
496 /* Create netlink socket */
497 handle
->fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_NETFILTER
);
498 if (handle
->fd
< 0) {
499 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't create raw socket %d:%s", errno
, pcap_strerror(errno
));
504 handle
->linktype
= DLT_NFLOG
;
505 handle
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
506 if (handle
->dlt_list
!= NULL
) {
507 handle
->dlt_list
[0] = DLT_NFLOG
;
508 handle
->dlt_list
[1] = DLT_IPV4
;
509 handle
->dlt_count
= 2;
513 handle
->linktype
= DLT_IPV4
;
515 handle
->buffer
= malloc(handle
->bufsize
);
516 if (!handle
->buffer
) {
517 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate dump buffer: %s", pcap_strerror(errno
));
522 if (nflog_send_config_cmd(handle
, 0, NFULNL_CFG_CMD_PF_UNBIND
, AF_INET
) < 0) {
523 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno
));
527 if (nflog_send_config_cmd(handle
, 0, NFULNL_CFG_CMD_PF_BIND
, AF_INET
) < 0) {
528 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno
));
532 /* Bind socket to the nflog groups */
533 for (i
= 0; i
< group_count
; i
++) {
534 if (nflog_send_config_cmd(handle
, groups
[i
], NFULNL_CFG_CMD_BIND
, AF_UNSPEC
) < 0) {
535 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't listen on group group index: %s", pcap_strerror(errno
));
539 if (nflog_send_config_mode(handle
, groups
[i
], NFULNL_COPY_PACKET
, handle
->snapshot
) < 0) {
540 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno
));
546 if (nfqueue_send_config_cmd(handle
, 0, NFQNL_CFG_CMD_PF_UNBIND
, AF_INET
) < 0) {
547 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno
));
551 if (nfqueue_send_config_cmd(handle
, 0, NFQNL_CFG_CMD_PF_BIND
, AF_INET
) < 0) {
552 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno
));
556 /* Bind socket to the nfqueue groups */
557 for (i
= 0; i
< group_count
; i
++) {
558 if (nfqueue_send_config_cmd(handle
, groups
[i
], NFQNL_CFG_CMD_BIND
, AF_UNSPEC
) < 0) {
559 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't listen on group group index: %s", pcap_strerror(errno
));
563 if (nfqueue_send_config_mode(handle
, groups
[i
], NFQNL_COPY_PACKET
, handle
->snapshot
) < 0) {
564 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno
));
570 if (handle
->opt
.rfmon
) {
572 * Monitor mode doesn't apply to netfilter devices.
574 pcap_cleanup_live_common(handle
);
575 return PCAP_ERROR_RFMON_NOTSUP
;
578 if (handle
->opt
.buffer_size
!= 0) {
580 * Set the socket buffer size to the specified value.
582 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_RCVBUF
, &handle
->opt
.buffer_size
, sizeof(handle
->opt
.buffer_size
)) == -1) {
583 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "SO_RCVBUF: %s", pcap_strerror(errno
));
588 handle
->selectable_fd
= handle
->fd
;
592 pcap_cleanup_live_common(handle
);
597 netfilter_create(const char *device
, char *ebuf
, int *is_ours
)
602 /* Does this look like an netfilter device? */
603 cp
= strrchr(device
, '/');
607 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
608 if (strncmp(cp
, NFLOG_IFACE
, sizeof NFLOG_IFACE
- 1) == 0)
609 cp
+= sizeof NFLOG_IFACE
- 1;
610 else if (strncmp(cp
, NFQUEUE_IFACE
, sizeof NFQUEUE_IFACE
- 1) == 0)
611 cp
+= sizeof NFQUEUE_IFACE
- 1;
613 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
619 * Yes - is that either the end of the name, or is it followed
622 if (*cp
!= ':' && *cp
!= '\0') {
628 /* OK, it's probably ours. */
631 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_netfilter
));
635 p
->activate_op
= netfilter_activate
;
640 netfilter_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
644 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_NETFILTER
);
646 /* if netlink is not supported this is not fatal */
647 if (errno
== EAFNOSUPPORT
|| errno
== EPROTONOSUPPORT
)
649 snprintf(err_str
, PCAP_ERRBUF_SIZE
, "Can't open netlink socket %d:%s",
650 errno
, pcap_strerror(errno
));
655 if (pcap_add_if(alldevsp
, NFLOG_IFACE
, 0, "Linux netfilter log (NFLOG) interface", err_str
) < 0)
657 if (pcap_add_if(alldevsp
, NFQUEUE_IFACE
, 0, "Linux netfilter queue (NFQUEUE) interface", err_str
) < 0)