Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / pcap-netfilter-linux.c
blob2cbe158abf1f59568f190313804dfb14d9fde423
1 /* $NetBSD: pcap-netfilter-linux.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
3 /*
4 * Copyright (c) 2011 Jakub Zawadzki
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
18 * permission.
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 $");
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
40 #include "pcap-int.h"
42 #ifdef NEED_STRERROR_H
43 #include "strerror.h"
44 #endif
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
53 #include <time.h>
54 #include <sys/time.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);
91 static int
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;
96 int count = 0;
97 int len;
99 /* ignore interrupt system call error */
100 do {
101 len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
102 if (handle->break_loop) {
103 handle->break_loop = 0;
104 return -2;
106 } while ((len == -1) && (errno == EINTR));
108 if (len < 0) {
109 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno));
110 return -1;
113 buf = handle->buffer;
114 while (len >= NLMSG_SPACE(0)) {
115 const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf;
116 u_int32_t msg_len;
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);
121 return -1;
124 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
125 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
126 type = NFLOG;
127 else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
128 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
129 type = NFQUEUE;
131 if (type != OTHER) {
132 const unsigned char *payload = NULL;
133 struct pcap_pkthdr pkth;
135 const struct nfgenmsg *nfg = NULL;
136 int id = 0;
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);
143 return -1;
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);
159 break;
161 case NFQA_PAYLOAD:
162 payload_attr = attr;
163 break;
166 } else if (type == NFLOG) {
167 switch (NFA_TYPE(attr)) {
168 case NFULA_PAYLOAD:
169 payload_attr = attr;
170 break;
173 attr = NFA_NEXT(attr, attr_len);
177 if (payload_attr) {
178 payload = NFA_DATA(payload_attr);
179 pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
182 } else {
183 payload = NLMSG_DATA(nlh);
184 pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
187 if (payload) {
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);
196 count++;
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). */
204 if (nfg != NULL)
205 nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
209 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
210 if (msg_len > len)
211 msg_len = len;
213 len -= msg_len;
214 buf += msg_len;
216 return count;
219 static int
220 netfilter_set_datalink(pcap_t *handle, int dlt)
222 handle->linktype = dlt;
223 return 0;
226 static int
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;
232 stats->ps_drop = 0;
233 stats->ps_ifdrop = 0;
234 return 0;
237 static int
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");
241 return (-1);
244 struct my_nfattr {
245 u_int16_t nfa_len;
246 u_int16_t nfa_type;
247 void *data;
250 static int
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;
261 if (!seq_id)
262 seq_id = time(NULL);
263 ++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);
275 if (mynfa) {
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)
288 return -1;
290 if (!ack)
291 return 0;
293 /* waiting for reply loop */
294 do {
295 socklen_t addrlen = sizeof(snl);
296 int len;
298 /* ignore interrupt system call error */
299 do {
300 len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
301 } while ((len == -1) && (errno == EINTR));
303 if (len <= 0)
304 return len;
306 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
307 errno = EINVAL;
308 return -1;
311 nlh = (struct nlmsghdr *) buf;
312 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
313 continue;
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))) {
318 errno = EBADMSG;
319 return -1;
321 errno = -(*((int *)NLMSG_DATA(nlh)));
322 return (errno == 0) ? 0 : -1;
324 nlh = NLMSG_NEXT(nlh, len);
326 } while (1);
328 return -1; /* never here */
331 static int
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);
337 static int
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;
343 msg.command = cmd;
345 nfa.data = &msg;
346 nfa.nfa_type = NFULA_CFG_CMD;
347 nfa.nfa_len = sizeof(msg);
349 return nflog_send_config_msg(handle, family, group_id, &nfa);
352 static int
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;
361 nfa.data = &msg;
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);
368 static int
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;
374 msg.id = htonl(id);
375 msg.verdict = htonl(verdict);
377 nfa.data = &msg;
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);
384 static int
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);
390 static int
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;
396 msg.command = cmd;
397 msg.pf = htons(pf);
399 nfa.data = &msg;
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);
406 static int
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;
415 nfa.data = &msg;
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);
422 static int
423 netfilter_activate(pcap_t* handle)
425 const char *dev = handle->opt.source;
426 unsigned short groups[32];
427 int group_count = 0;
428 nftype_t type = OTHER;
429 int i;
431 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
432 dev += strlen(NFLOG_IFACE);
433 type = NFLOG;
435 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
436 dev += strlen(NFQUEUE_IFACE);
437 type = NFQUEUE;
440 if (type != OTHER && *dev == ':') {
441 dev++;
442 while (*dev) {
443 long int group_id;
444 char *end_dev;
446 if (group_count == 32) {
447 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
448 "Maximum 32 netfilter groups! dev: %s",
449 handle->opt.source);
450 return PCAP_ERROR;
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)",
458 group_id);
459 return PCAP_ERROR;
462 groups[group_count++] = (unsigned short) group_id;
463 dev = end_dev;
465 if (*dev != ',')
466 break;
467 dev++;
471 if (type == OTHER || *dev) {
472 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
473 "Can't get netfilter group(s) index from %s",
474 handle->opt.source);
475 return PCAP_ERROR;
478 /* if no groups, add default: 0 */
479 if (!group_count) {
480 groups[0] = 0;
481 group_count = 1;
484 /* Initialize some components of the pcap structure. */
485 handle->bufsize = 128 + handle->snapshot;
486 handle->offset = 0;
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));
500 return PCAP_ERROR;
503 if (type == NFLOG) {
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;
512 } else
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));
518 goto close_fail;
521 if (type == NFLOG) {
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));
524 goto close_fail;
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));
529 goto close_fail;
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));
536 goto close_fail;
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));
541 goto close_fail;
545 } else {
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));
548 goto close_fail;
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));
553 goto close_fail;
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));
560 goto close_fail;
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));
565 goto close_fail;
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));
584 goto close_fail;
588 handle->selectable_fd = handle->fd;
589 return 0;
591 close_fail:
592 pcap_cleanup_live_common(handle);
593 return PCAP_ERROR;
596 pcap_t *
597 netfilter_create(const char *device, char *ebuf, int *is_ours)
599 const char *cp;
600 pcap_t *p;
602 /* Does this look like an netfilter device? */
603 cp = strrchr(device, '/');
604 if (cp == NULL)
605 cp = 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;
612 else {
613 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
614 *is_ours = 0;
615 return NULL;
619 * Yes - is that either the end of the name, or is it followed
620 * by a colon?
622 if (*cp != ':' && *cp != '\0') {
623 /* Nope */
624 *is_ours = 0;
625 return NULL;
628 /* OK, it's probably ours. */
629 *is_ours = 1;
631 p = pcap_create_common(device, ebuf, sizeof (struct pcap_netfilter));
632 if (p == NULL)
633 return (NULL);
635 p->activate_op = netfilter_activate;
636 return (p);
640 netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
642 int sock;
644 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
645 if (sock < 0) {
646 /* if netlink is not supported this is not fatal */
647 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
648 return 0;
649 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s",
650 errno, pcap_strerror(errno));
651 return -1;
653 close(sock);
655 if (pcap_add_if(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) < 0)
656 return -1;
657 if (pcap_add_if(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) < 0)
658 return -1;
659 return 0;