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