]> The Tcpdump Group git mirrors - libpcap/blob - pcap-netfilter-linux.c
Merge pull request #585 from rolandd/master
[libpcap] / pcap-netfilter-linux.c
1 /*
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
75 typedef enum { OTHER = -1, NFLOG, NFQUEUE } nftype_t;
76
77 /*
78 * Private data for capturing on Linux netfilter sockets.
79 */
80 struct pcap_netfilter {
81 u_int packets_read; /* count of packets read with recvfrom() */
82 u_int packets_nobufs; /* ENOBUFS counter */
83 };
84
85 static int nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict);
86
87
88 static int
89 netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
90 {
91 struct pcap_netfilter *handlep = handle->priv;
92 register u_char *bp, *ep;
93 int count = 0;
94 int len;
95
96 /*
97 * Has "pcap_breakloop()" been called?
98 */
99 if (handle->break_loop) {
100 /*
101 * Yes - clear the flag that indicates that it
102 * has, and return PCAP_ERROR_BREAK to indicate
103 * that we were told to break out of the loop.
104 */
105 handle->break_loop = 0;
106 return PCAP_ERROR_BREAK;
107 }
108 len = handle->cc;
109 if (len == 0) {
110 /*
111 * The buffer is empty; refill it.
112 *
113 * We ignore EINTR, as that might just be due to a signal
114 * being delivered - if the signal should interrupt the
115 * loop, the signal handler should call pcap_breakloop()
116 * to set handle->break_loop (we ignore it on other
117 * platforms as well).
118 */
119 do {
120 len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
121 if (handle->break_loop) {
122 handle->break_loop = 0;
123 return PCAP_ERROR_BREAK;
124 }
125 if (errno == ENOBUFS)
126 handlep->packets_nobufs++;
127 } while ((len == -1) && (errno == EINTR || errno == ENOBUFS));
128
129 if (len < 0) {
130 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno));
131 return PCAP_ERROR;
132 }
133
134 bp = (unsigned char *)handle->buffer;
135 } else
136 bp = handle->bp;
137 ep = bp + len;
138 while (bp < ep) {
139 const struct nlmsghdr *nlh = (const struct nlmsghdr *) bp;
140 u_int32_t msg_len;
141 nftype_t type = OTHER;
142 /*
143 * Has "pcap_breakloop()" been called?
144 * If so, return immediately - if we haven't read any
145 * packets, clear the flag and return PCAP_ERROR_BREAK
146 * to indicate that we were told to break out of the loop,
147 * otherwise leave the flag set, so that the *next* call
148 * will break out of the loop without having read any
149 * packets, and return the number of packets we've
150 * processed so far.
151 */
152 if (handle->break_loop) {
153 handle->bp = bp;
154 handle->cc = ep - bp;
155 if (count == 0) {
156 handle->break_loop = 0;
157 return PCAP_ERROR_BREAK;
158 } else
159 return count;
160 }
161 if (ep - bp < NLMSG_SPACE(0)) {
162 /*
163 * There's less than one netlink message left
164 * in the buffer. Give up.
165 */
166 break;
167 }
168
169 if (nlh->nlmsg_len < sizeof(struct nlmsghdr) || (u_int)len < nlh->nlmsg_len) {
170 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Message truncated: (got: %d) (nlmsg_len: %u)", len, nlh->nlmsg_len);
171 return -1;
172 }
173
174 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
175 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
176 type = NFLOG;
177 else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
178 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
179 type = NFQUEUE;
180
181 if (type != OTHER) {
182 const unsigned char *payload = NULL;
183 struct pcap_pkthdr pkth;
184
185 const struct nfgenmsg *nfg = NULL;
186 int id = 0;
187
188 if (handle->linktype != DLT_NFLOG) {
189 const struct nfattr *payload_attr = NULL;
190
191 if (nlh->nlmsg_len < HDR_LENGTH) {
192 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Malformed message: (nlmsg_len: %u)", nlh->nlmsg_len);
193 return -1;
194 }
195
196 nfg = NLMSG_DATA(nlh);
197 if (nlh->nlmsg_len > HDR_LENGTH) {
198 struct nfattr *attr = NFM_NFA(nfg);
199 int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(HDR_LENGTH);
200
201 while (NFA_OK(attr, attr_len)) {
202 if (type == NFQUEUE) {
203 switch (NFA_TYPE(attr)) {
204 case NFQA_PACKET_HDR:
205 {
206 const struct nfqnl_msg_packet_hdr *pkt_hdr = (const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
207
208 id = ntohl(pkt_hdr->packet_id);
209 break;
210 }
211 case NFQA_PAYLOAD:
212 payload_attr = attr;
213 break;
214 }
215
216 } else if (type == NFLOG) {
217 switch (NFA_TYPE(attr)) {
218 case NFULA_PAYLOAD:
219 payload_attr = attr;
220 break;
221 }
222 }
223 attr = NFA_NEXT(attr, attr_len);
224 }
225 }
226
227 if (payload_attr) {
228 payload = NFA_DATA(payload_attr);
229 pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
230 }
231
232 } else {
233 payload = NLMSG_DATA(nlh);
234 pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
235 }
236
237 if (payload) {
238 /* pkth.caplen = min (payload_len, handle->snapshot); */
239
240 gettimeofday(&pkth.ts, NULL);
241 if (handle->fcode.bf_insns == NULL ||
242 bpf_filter(handle->fcode.bf_insns, payload, pkth.len, pkth.caplen))
243 {
244 handlep->packets_read++;
245 callback(user, &pkth, payload);
246 count++;
247 }
248 }
249
250 if (type == NFQUEUE) {
251 /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
252 /* if type == NFQUEUE, handle->linktype is always != DLT_NFLOG,
253 so nfg is always initialized to NLMSG_DATA(nlh). */
254 if (nfg != NULL)
255 nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
256 }
257 }
258
259 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
260 /*
261 * If the message length would run past the end of the
262 * buffer, truncate it to the remaining space in the
263 * buffer.
264 */
265 if (msg_len > ep - bp)
266 msg_len = ep - bp;
267
268 bp += msg_len;
269 if (count >= max_packets && !PACKET_COUNT_IS_UNLIMITED(max_packets)) {
270 handle->bp = bp;
271 handle->cc = ep - bp;
272 if (handle->cc < 0)
273 handle->cc = 0;
274 return count;
275 }
276 }
277
278 handle->cc = 0;
279 return count;
280 }
281
282 static int
283 netfilter_set_datalink(pcap_t *handle, int dlt)
284 {
285 handle->linktype = dlt;
286 return 0;
287 }
288
289 static int
290 netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
291 {
292 struct pcap_netfilter *handlep = handle->priv;
293
294 stats->ps_recv = handlep->packets_read;
295 stats->ps_drop = handlep->packets_nobufs;
296 stats->ps_ifdrop = 0;
297 return 0;
298 }
299
300 static int
301 netfilter_inject_linux(pcap_t *handle, const void *buf, size_t size)
302 {
303 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on netfilter devices");
304 return (-1);
305 }
306
307 struct my_nfattr {
308 u_int16_t nfa_len;
309 u_int16_t nfa_type;
310 void *data;
311 };
312
313 static int
314 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)
315 {
316 char buf[1024] __attribute__ ((aligned));
317
318 struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
319 struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr));
320
321 struct sockaddr_nl snl;
322 static unsigned int seq_id;
323
324 if (!seq_id)
325 seq_id = time(NULL);
326 ++seq_id;
327
328 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg));
329 nlh->nlmsg_type = msg_type;
330 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
331 nlh->nlmsg_pid = 0; /* to kernel */
332 nlh->nlmsg_seq = seq_id;
333
334 nfg->nfgen_family = family;
335 nfg->version = NFNETLINK_V0;
336 nfg->res_id = htons(res_id);
337
338 if (mynfa) {
339 struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
340
341 nfa->nfa_type = mynfa->nfa_type;
342 nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len);
343 memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len);
344 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
345 }
346
347 memset(&snl, 0, sizeof(snl));
348 snl.nl_family = AF_NETLINK;
349
350 if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1)
351 return -1;
352
353 if (!ack)
354 return 0;
355
356 /* waiting for reply loop */
357 do {
358 socklen_t addrlen = sizeof(snl);
359 int len;
360
361 /* ignore interrupt system call error */
362 do {
363 len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
364 } while ((len == -1) && (errno == EINTR));
365
366 if (len <= 0)
367 return len;
368
369 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
370 errno = EINVAL;
371 return -1;
372 }
373
374 nlh = (struct nlmsghdr *) buf;
375 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
376 continue;
377
378 while ((u_int)len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, len)) {
379 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
380 if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) {
381 errno = EBADMSG;
382 return -1;
383 }
384 errno = -(*((int *)NLMSG_DATA(nlh)));
385 return (errno == 0) ? 0 : -1;
386 }
387 nlh = NLMSG_NEXT(nlh, len);
388 }
389 } while (1);
390
391 return -1; /* never here */
392 }
393
394 static int
395 nflog_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
396 {
397 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa);
398 }
399
400 static int
401 nflog_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int8_t family)
402 {
403 struct nfulnl_msg_config_cmd msg;
404 struct my_nfattr nfa;
405
406 msg.command = cmd;
407
408 nfa.data = &msg;
409 nfa.nfa_type = NFULA_CFG_CMD;
410 nfa.nfa_len = sizeof(msg);
411
412 return nflog_send_config_msg(handle, family, group_id, &nfa);
413 }
414
415 static int
416 nflog_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
417 {
418 struct nfulnl_msg_config_mode msg;
419 struct my_nfattr nfa;
420
421 msg.copy_range = htonl(copy_range);
422 msg.copy_mode = copy_mode;
423
424 nfa.data = &msg;
425 nfa.nfa_type = NFULA_CFG_MODE;
426 nfa.nfa_len = sizeof(msg);
427
428 return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
429 }
430
431 static int
432 nfqueue_send_verdict(const pcap_t *handle, u_int16_t group_id, u_int32_t id, u_int32_t verdict)
433 {
434 struct nfqnl_msg_verdict_hdr msg;
435 struct my_nfattr nfa;
436
437 msg.id = htonl(id);
438 msg.verdict = htonl(verdict);
439
440 nfa.data = &msg;
441 nfa.nfa_type = NFQA_VERDICT_HDR;
442 nfa.nfa_len = sizeof(msg);
443
444 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa);
445 }
446
447 static int
448 nfqueue_send_config_msg(const pcap_t *handle, u_int8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
449 {
450 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa);
451 }
452
453 static int
454 nfqueue_send_config_cmd(const pcap_t *handle, u_int16_t group_id, u_int8_t cmd, u_int16_t pf)
455 {
456 struct nfqnl_msg_config_cmd msg;
457 struct my_nfattr nfa;
458
459 msg.command = cmd;
460 msg.pf = htons(pf);
461
462 nfa.data = &msg;
463 nfa.nfa_type = NFQA_CFG_CMD;
464 nfa.nfa_len = sizeof(msg);
465
466 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
467 }
468
469 static int
470 nfqueue_send_config_mode(const pcap_t *handle, u_int16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
471 {
472 struct nfqnl_msg_config_params msg;
473 struct my_nfattr nfa;
474
475 msg.copy_range = htonl(copy_range);
476 msg.copy_mode = copy_mode;
477
478 nfa.data = &msg;
479 nfa.nfa_type = NFQA_CFG_PARAMS;
480 nfa.nfa_len = sizeof(msg);
481
482 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
483 }
484
485 static int
486 netfilter_activate(pcap_t* handle)
487 {
488 const char *dev = handle->opt.device;
489 unsigned short groups[32];
490 int group_count = 0;
491 nftype_t type = OTHER;
492 int i;
493
494 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
495 dev += strlen(NFLOG_IFACE);
496 type = NFLOG;
497
498 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
499 dev += strlen(NFQUEUE_IFACE);
500 type = NFQUEUE;
501 }
502
503 if (type != OTHER && *dev == ':') {
504 dev++;
505 while (*dev) {
506 long int group_id;
507 char *end_dev;
508
509 if (group_count == 32) {
510 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
511 "Maximum 32 netfilter groups! dev: %s",
512 handle->opt.device);
513 return PCAP_ERROR;
514 }
515
516 group_id = strtol(dev, &end_dev, 0);
517 if (end_dev != dev) {
518 if (group_id < 0 || group_id > 65535) {
519 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
520 "Netfilter group range from 0 to 65535 (got %ld)",
521 group_id);
522 return PCAP_ERROR;
523 }
524
525 groups[group_count++] = (unsigned short) group_id;
526 dev = end_dev;
527 }
528 if (*dev != ',')
529 break;
530 dev++;
531 }
532 }
533
534 if (type == OTHER || *dev) {
535 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
536 "Can't get netfilter group(s) index from %s",
537 handle->opt.device);
538 return PCAP_ERROR;
539 }
540
541 /* if no groups, add default: 0 */
542 if (!group_count) {
543 groups[0] = 0;
544 group_count = 1;
545 }
546
547 /*
548 * Turn a negative snapshot value (invalid), a snapshot value of
549 * 0 (unspecified), or a value bigger than the normal maximum
550 * value, into the maximum allowed value.
551 *
552 * If some application really *needs* a bigger snapshot
553 * length, we should just increase MAXIMUM_SNAPLEN.
554 */
555 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
556 handle->snapshot = MAXIMUM_SNAPLEN;
557
558 /* Initialize some components of the pcap structure. */
559 handle->bufsize = 128 + handle->snapshot;
560 handle->offset = 0;
561 handle->read_op = netfilter_read_linux;
562 handle->inject_op = netfilter_inject_linux;
563 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
564 handle->setdirection_op = NULL;
565 handle->set_datalink_op = netfilter_set_datalink;
566 handle->getnonblock_op = pcap_getnonblock_fd;
567 handle->setnonblock_op = pcap_setnonblock_fd;
568 handle->stats_op = netfilter_stats_linux;
569
570 /* Create netlink socket */
571 handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
572 if (handle->fd < 0) {
573 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno));
574 return PCAP_ERROR;
575 }
576
577 if (type == NFLOG) {
578 handle->linktype = DLT_NFLOG;
579 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
580 if (handle->dlt_list != NULL) {
581 handle->dlt_list[0] = DLT_NFLOG;
582 handle->dlt_list[1] = DLT_IPV4;
583 handle->dlt_count = 2;
584 }
585
586 } else
587 handle->linktype = DLT_IPV4;
588
589 handle->buffer = malloc(handle->bufsize);
590 if (!handle->buffer) {
591 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", pcap_strerror(errno));
592 goto close_fail;
593 }
594
595 if (type == NFLOG) {
596 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
597 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
598 goto close_fail;
599 }
600
601 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
602 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
603 goto close_fail;
604 }
605
606 /* Bind socket to the nflog groups */
607 for (i = 0; i < group_count; i++) {
608 if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
609 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
610 goto close_fail;
611 }
612
613 if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) {
614 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno));
615 goto close_fail;
616 }
617 }
618
619 } else {
620 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
621 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno));
622 goto close_fail;
623 }
624
625 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
626 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno));
627 goto close_fail;
628 }
629
630 /* Bind socket to the nfqueue groups */
631 for (i = 0; i < group_count; i++) {
632 if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
633 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno));
634 goto close_fail;
635 }
636
637 if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) {
638 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno));
639 goto close_fail;
640 }
641 }
642 }
643
644 if (handle->opt.rfmon) {
645 /*
646 * Monitor mode doesn't apply to netfilter devices.
647 */
648 pcap_cleanup_live_common(handle);
649 return PCAP_ERROR_RFMON_NOTSUP;
650 }
651
652 if (handle->opt.buffer_size != 0) {
653 /*
654 * Set the socket buffer size to the specified value.
655 */
656 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) {
657 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "SO_RCVBUF: %s", pcap_strerror(errno));
658 goto close_fail;
659 }
660 }
661
662 handle->selectable_fd = handle->fd;
663 return 0;
664
665 close_fail:
666 pcap_cleanup_live_common(handle);
667 return PCAP_ERROR;
668 }
669
670 pcap_t *
671 netfilter_create(const char *device, char *ebuf, int *is_ours)
672 {
673 const char *cp;
674 pcap_t *p;
675
676 /* Does this look like an netfilter device? */
677 cp = strrchr(device, '/');
678 if (cp == NULL)
679 cp = device;
680
681 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
682 if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0)
683 cp += sizeof NFLOG_IFACE - 1;
684 else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0)
685 cp += sizeof NFQUEUE_IFACE - 1;
686 else {
687 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
688 *is_ours = 0;
689 return NULL;
690 }
691
692 /*
693 * Yes - is that either the end of the name, or is it followed
694 * by a colon?
695 */
696 if (*cp != ':' && *cp != '\0') {
697 /* Nope */
698 *is_ours = 0;
699 return NULL;
700 }
701
702 /* OK, it's probably ours. */
703 *is_ours = 1;
704
705 p = pcap_create_common(ebuf, sizeof (struct pcap_netfilter));
706 if (p == NULL)
707 return (NULL);
708
709 p->activate_op = netfilter_activate;
710 return (p);
711 }
712
713 int
714 netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str)
715 {
716 int sock;
717
718 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
719 if (sock < 0) {
720 /* if netlink is not supported this is not fatal */
721 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
722 return 0;
723 pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s",
724 errno, pcap_strerror(errno));
725 return -1;
726 }
727 close(sock);
728
729 if (add_dev(devlistp, NFLOG_IFACE, 0, "Linux netfilter log (NFLOG) interface", err_str) == NULL)
730 return -1;
731 if (add_dev(devlistp, NFQUEUE_IFACE, 0, "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL)
732 return -1;
733 return 0;
734 }