blob: aa8edbba777234de947ce8e89a59286947ff8b19 [file] [log] [blame]
JP Abgrall511eca32014-02-12 13:46:45 -08001/*
2 * Copyright (c) 2011 Jakub Zawadzki
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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
16 * permission.
17 *
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.
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "pcap-int.h"
36
37#ifdef NEED_STRERROR_H
38#include "strerror.h"
39#endif
40
41#include <errno.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <string.h>
45#include <sys/socket.h>
46#include <arpa/inet.h>
47
48#include <time.h>
49#include <sys/time.h>
50#include <netinet/in.h>
51#include <linux/types.h>
52
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>
58
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 ;/
61 *
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.
64 *
65 * In tcpdump you can disable dropping privilages with -Z root
66 */
67
68#include "pcap-netfilter-linux.h"
69
70#define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
71
72#define NFLOG_IFACE "nflog"
73#define NFQUEUE_IFACE "nfqueue"
74
75typedef enum { OTHER = -1, NFLOG, NFQUEUE } nftype_t;
76
77/*
78 * Private data for capturing on Linux netfilter sockets.
79 */
80struct pcap_netfilter {
81 u_int packets_read; /* count of packets read with recvfrom() */
82};
83
84static int nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict);
85
86static int
87netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
88{
89 struct pcap_netfilter *handlep = handle->priv;
90 const unsigned char *buf;
91 int count = 0;
92 int len;
93
94 /* ignore interrupt system call error */
95 do {
96 len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
97 if (handle->break_loop) {
98 handle->break_loop = 0;
99 return -2;
100 }
101 } while ((len == -1) && (errno == EINTR));
102
103 if (len < 0) {
104 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno));
105 return -1;
106 }
107
108 buf = handle->buffer;
109 while (len >= NLMSG_SPACE(0)) {
110 const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf;
111 u_int32_t msg_len;
112 nftype_t type = OTHER;
113
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);
116 return -1;
117 }
118
119 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
120 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
121 type = NFLOG;
122
123 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
124 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
125 type = NFQUEUE;
126
127 if (type != OTHER) {
128 const unsigned char *payload = NULL;
129 struct pcap_pkthdr pkth;
130
131 const struct nfgenmsg *nfg;
132 int id = 0;
133
134 if (handle->linktype != DLT_NFLOG) {
135 const struct nfattr *payload_attr = NULL;
136
137 if (nlh->nlmsg_len < HDR_LENGTH) {
138 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Malformed message: (nlmsg_len: %u)", nlh->nlmsg_len);
139 return -1;
140 }
141
142 nfg = NLMSG_DATA(nlh);
143 if (nlh->nlmsg_len > HDR_LENGTH) {
144 struct nfattr *attr = NFM_NFA(nfg);
145 int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(HDR_LENGTH);
146
147 while (NFA_OK(attr, attr_len)) {
148 if (type == NFQUEUE) {
149 switch (NFA_TYPE(attr)) {
150 case NFQA_PACKET_HDR:
151 {
152 const struct nfqnl_msg_packet_hdr *pkt_hdr = (const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
153
154 id = ntohl(pkt_hdr->packet_id);
155 break;
156 }
157 case NFQA_PAYLOAD:
158 payload_attr = attr;
159 break;
160 }
161
162 } else if (type == NFLOG) {
163 switch (NFA_TYPE(attr)) {
164 case NFULA_PAYLOAD:
165 payload_attr = attr;
166 break;
167 }
168 }
169 attr = NFA_NEXT(attr, attr_len);
170 }
171 }
172
173 if (payload_attr) {
174 payload = NFA_DATA(payload_attr);
175 pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
176 }
177
178 } else {
179 payload = NLMSG_DATA(nlh);
180 pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
181 }
182
183 if (payload) {
184 /* pkth.caplen = min (payload_len, handle->snapshot); */
185
186 gettimeofday(&pkth.ts, NULL);
187 if (handle->fcode.bf_insns == NULL ||
188 bpf_filter(handle->fcode.bf_insns, payload, pkth.len, pkth.caplen))
189 {
190 handlep->packets_read++;
191 callback(user, &pkth, payload);
192 count++;
193 }
194 }
195
196 if (type == NFQUEUE) {
197 /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
198 nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
199 }
200 }
201
202 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
203 if (msg_len > len)
204 msg_len = len;
205
206 len -= msg_len;
207 buf += msg_len;
208 }
209 return count;
210}
211
212static int
213netfilter_set_datalink(pcap_t *handle, int dlt)
214{
215 handle->linktype = dlt;
216 return 0;
217}
218
219static int
220netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
221{
222 struct pcap_netfilter *handlep = handle->priv;
223
224 stats->ps_recv = handlep->packets_read;
225 stats->ps_drop = 0;
226 stats->ps_ifdrop = 0;
227 return 0;
228}
229
230static int
231netfilter_inject_linux(pcap_t *handle, const void *buf, size_t size)
232{
233 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on netfilter devices");
234 return (-1);
235}
236
237struct my_nfattr {
238 u_int16_t nfa_len;
239 u_int16_t nfa_type;
240 void *data;
241};
242
243static int
244netfilter_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{
246 char buf[1024] __attribute__ ((aligned));
247
248 struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
249 struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr));
250
251 struct sockaddr_nl snl;
252 static unsigned int seq_id;
253
254 if (!seq_id)
255 seq_id = time(NULL);
256 ++seq_id;
257
258 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg));
259 nlh->nlmsg_type = msg_type;
260 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
261 nlh->nlmsg_pid = 0; /* to kernel */
262 nlh->nlmsg_seq = seq_id;
263
264 nfg->nfgen_family = family;
265 nfg->version = NFNETLINK_V0;
266 nfg->res_id = htons(res_id);
267
268 if (mynfa) {
269 struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
270
271 nfa->nfa_type = mynfa->nfa_type;
272 nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len);
273 memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len);
274 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
275 }
276
277 memset(&snl, 0, sizeof(snl));
278 snl.nl_family = AF_NETLINK;
279
280 if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1)
281 return -1;
282
283 if (!ack)
284 return 0;
285
286 /* waiting for reply loop */
287 do {
288 socklen_t addrlen = sizeof(snl);
289 int len;
290
291 /* ignore interrupt system call error */
292 do {
293 len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
294 } while ((len == -1) && (errno == EINTR));
295
296 if (len <= 0)
297 return len;
298
299 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
300 errno = EINVAL;
301 return -1;
302 }
303
304 nlh = (struct nlmsghdr *) buf;
305 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
306 continue;
307
308 while (len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, len)) {
309 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
310 if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) {
311 errno = EBADMSG;
312 return -1;
313 }
314 errno = -(*((int *)NLMSG_DATA(nlh)));
315 return (errno == 0) ? 0 : -1;
316 }
317 nlh = NLMSG_NEXT(nlh, len);
318 }
319 } while (1);
320
321 return -1; /* never here */
322}
323
324static int
325nflog_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
326{
327 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa);
328}
329
330static int
331nflog_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int8_t family)
332{
333 struct nfulnl_msg_config_cmd msg;
334 struct my_nfattr nfa;
335
336 msg.command = cmd;
337
338 nfa.data = &msg;
339 nfa.nfa_type = NFULA_CFG_CMD;
340 nfa.nfa_len = sizeof(msg);
341
342 return nflog_send_config_msg(handle, family, group_id, &nfa);
343}
344
345static int
346nflog_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
347{
348 struct nfulnl_msg_config_mode msg;
349 struct my_nfattr nfa;
350
351 msg.copy_range = htonl(copy_range);
352 msg.copy_mode = copy_mode;
353
354 nfa.data = &msg;
355 nfa.nfa_type = NFULA_CFG_MODE;
356 nfa.nfa_len = sizeof(msg);
357
358 return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
359}
360
361static int
362nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict)
363{
364 struct nfqnl_msg_verdict_hdr msg;
365 struct my_nfattr nfa;
366
367 msg.id = htonl(id);
368 msg.verdict = htonl(verdict);
369
370 nfa.data = &msg;
371 nfa.nfa_type = NFQA_VERDICT_HDR;
372 nfa.nfa_len = sizeof(msg);
373
374 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa);
375}
376
377static int
378nfqueue_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
379{
380 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa);
381}
382
383static int
384nfqueue_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int16_t pf)
385{
386 struct nfqnl_msg_config_cmd msg;
387 struct my_nfattr nfa;
388
389 msg.command = cmd;
390 msg.pf = htons(pf);
391
392 nfa.data = &msg;
393 nfa.nfa_type = NFQA_CFG_CMD;
394 nfa.nfa_len = sizeof(msg);
395
396 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
397}
398
399static int
400nfqueue_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
401{
402 struct nfqnl_msg_config_params msg;
403 struct my_nfattr nfa;
404
405 msg.copy_range = htonl(copy_range);
406 msg.copy_mode = copy_mode;
407
408 nfa.data = &msg;
409 nfa.nfa_type = NFQA_CFG_PARAMS;
410 nfa.nfa_len = sizeof(msg);
411
412 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
413}
414
415static int
416netfilter_activate(pcap_t* handle)
417{
418 const char *dev = handle->opt.source;
419 unsigned short groups[32];
420 int group_count = 0;
421 nftype_t type = OTHER;
422 int i;
423
424 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
425 dev += strlen(NFLOG_IFACE);
426 type = NFLOG;
427
428 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
429 dev += strlen(NFQUEUE_IFACE);
430 type = NFQUEUE;
431 }
432
433 if (type != OTHER && *dev == ':') {
434 dev++;
435 while (*dev) {
436 long int group_id;
437 char *end_dev;
438
439 if (group_count == 32) {
440 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
441 "Maximum 32 netfilter groups! dev: %s",
442 handle->opt.source);
443 return PCAP_ERROR;
444 }
445
446 group_id = strtol(dev, &end_dev, 0);
447 if (end_dev != dev) {
448 if (group_id < 0 || group_id > 65535) {
449 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
450 "Netfilter group range from 0 to 65535 (got %ld)",
451 group_id);
452 return PCAP_ERROR;
453 }
454
455 groups[group_count++] = (unsigned short) group_id;
456 dev = end_dev;
457 }
458 if (*dev != ',')
459 break;
460 dev++;
461 }
462 }
463
464 if (type == OTHER || *dev) {
465 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
466 "Can't get netfilter group(s) index from %s",
467 handle->opt.source);
468 return PCAP_ERROR;
469 }
470
471 /* if no groups, add default: 0 */
472 if (!group_count) {
473 groups[0] = 0;
474 group_count = 1;
475 }
476
477 /* Initialize some components of the pcap structure. */
478 handle->bufsize = 128 + handle->snapshot;
479 handle->offset = 0;
480 handle->read_op = netfilter_read_linux;
481 handle->inject_op = netfilter_inject_linux;
482 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
483 handle->setdirection_op = NULL;
484 handle->set_datalink_op = netfilter_set_datalink;
485 handle->getnonblock_op = pcap_getnonblock_fd;
486 handle->setnonblock_op = pcap_setnonblock_fd;
487 handle->stats_op = netfilter_stats_linux;
488
489 /* Create netlink socket */
490 handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
491 if (handle->fd < 0) {
492 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno));
493 return PCAP_ERROR;
494 }
495
496 if (type == NFLOG) {
497 handle->linktype = DLT_NFLOG;
498 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
499 if (handle->dlt_list != NULL) {
500 handle->dlt_list[0] = DLT_NFLOG;
501 handle->dlt_list[1] = DLT_IPV4;
502 handle->dlt_count = 2;
503 }
504
505 } else
506 handle->linktype = DLT_IPV4;
507
508 handle->buffer = malloc(handle->bufsize);
509 if (!handle->buffer) {
510 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", pcap_strerror(errno));
511 goto close_fail;
512 }
513
514 if (type == NFLOG) {
515 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
516 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
517 goto close_fail;
518 }
519
520 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
521 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
522 goto close_fail;
523 }
524
525 /* Bind socket to the nflog groups */
526 for (i = 0; i < group_count; i++) {
527 if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
528 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
529 goto close_fail;
530 }
531
532 if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) {
533 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno));
534 goto close_fail;
535 }
536 }
537
538 } else {
539 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
540 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
541 goto close_fail;
542 }
543
544 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
545 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
546 goto close_fail;
547 }
548
549 /* Bind socket to the nfqueue groups */
550 for (i = 0; i < group_count; i++) {
551 if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
552 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
553 goto close_fail;
554 }
555
556 if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) {
557 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno));
558 goto close_fail;
559 }
560 }
561 }
562
563 if (handle->opt.rfmon) {
564 /*
565 * Monitor mode doesn't apply to netfilter devices.
566 */
567 pcap_cleanup_live_common(handle);
568 return PCAP_ERROR_RFMON_NOTSUP;
569 }
570
571 if (handle->opt.buffer_size != 0) {
572 /*
573 * Set the socket buffer size to the specified value.
574 */
575 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) {
576 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "SO_RCVBUF: %s", pcap_strerror(errno));
577 goto close_fail;
578 }
579 }
580
581 handle->selectable_fd = handle->fd;
582 return 0;
583
584close_fail:
585 pcap_cleanup_live_common(handle);
586 return PCAP_ERROR;
587}
588
589pcap_t *
590netfilter_create(const char *device, char *ebuf, int *is_ours)
591{
592 const char *cp;
593 pcap_t *p;
594
595 /* Does this look like an netfilter device? */
596 cp = strrchr(device, '/');
597 if (cp == NULL)
598 cp = device;
599
600 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
601 if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0)
602 cp += sizeof NFLOG_IFACE - 1;
603 else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0)
604 cp += sizeof NFQUEUE_IFACE - 1;
605 else {
606 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
607 *is_ours = 0;
608 return NULL;
609 }
610
611 /*
612 * Yes - is that either the end of the name, or is it followed
613 * by a colon?
614 */
615 if (*cp != ':' && *cp != '\0') {
616 /* Nope */
617 *is_ours = 0;
618 return NULL;
619 }
620
621 /* OK, it's probably ours. */
622 *is_ours = 1;
623
624 p = pcap_create_common(device, ebuf, sizeof (struct pcap_netfilter));
625 if (p == NULL)
626 return (NULL);
627
628 p->activate_op = netfilter_activate;
629 return (p);
630}
631
632int
633netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
634{
635 int sock;
636
637 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
638 if (sock < 0) {
639 /* if netlink is not supported this is not fatal */
640 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
641 return 0;
642 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s",
643 errno, pcap_strerror(errno));
644 return -1;
645 }
646 close(sock);
647
648 if (pcap_add_if(alldevsp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) < 0)
649 return -1;
650 if (pcap_add_if(alldevsp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) < 0)
651 return -1;
652 return 0;
653}