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