1 /* $NetBSD: pcap-netfilter-linux.c,v 1.8 2024/09/02 15:33:37 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2011 Jakub Zawadzki 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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. 19 * 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. 31 */ 32 33 #include <sys/cdefs.h> 34 __RCSID("$NetBSD: pcap-netfilter-linux.c,v 1.8 2024/09/02 15:33:37 christos Exp $"); 35 36 #include <config.h> 37 38 #include "pcap-int.h" 39 #include "diag-control.h" 40 41 #ifdef NEED_STRERROR_H 42 #include "strerror.h" 43 #endif 44 45 #include <errno.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <string.h> 49 #include <sys/socket.h> 50 #include <arpa/inet.h> 51 52 #include <time.h> 53 #include <sys/time.h> 54 #include <netinet/in.h> 55 #include <linux/types.h> 56 57 #include <linux/netlink.h> 58 #include <linux/netfilter.h> 59 #include <linux/netfilter/nfnetlink.h> 60 #include <linux/netfilter/nfnetlink_log.h> 61 #include <linux/netfilter/nfnetlink_queue.h> 62 63 /* NOTE: if your program drops privileges after pcap_activate() it WON'T work with nfqueue. 64 * It took me quite some time to debug ;/ 65 * 66 * Sending any data to nfnetlink socket requires CAP_NET_ADMIN privileges, 67 * and in nfqueue we need to send verdict reply after receiving packet. 68 * 69 * In tcpdump you can disable dropping privileges with -Z root 70 */ 71 72 #include "pcap-netfilter-linux.h" 73 74 #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) 75 76 #define NFLOG_IFACE "nflog" 77 #define NFQUEUE_IFACE "nfqueue" 78 79 typedef enum { OTHER = -1, NFLOG, NFQUEUE } nftype_t; 80 81 /* 82 * Private data for capturing on Linux netfilter sockets. 83 */ 84 struct pcap_netfilter { 85 u_int packets_read; /* count of packets read with recvfrom() */ 86 u_int packets_nobufs; /* ENOBUFS counter */ 87 }; 88 89 static int nfqueue_send_verdict(const pcap_t *handle, uint16_t group_id, u_int32_t id, u_int32_t verdict); 90 91 92 static int 93 netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) 94 { 95 struct pcap_netfilter *handlep = handle->priv; 96 register u_char *bp, *ep; 97 int count = 0; 98 ssize_t len; 99 100 /* 101 * Has "pcap_breakloop()" been called? 102 */ 103 if (handle->break_loop) { 104 /* 105 * Yes - clear the flag that indicates that it 106 * has, and return PCAP_ERROR_BREAK to indicate 107 * that we were told to break out of the loop. 108 */ 109 handle->break_loop = 0; 110 return PCAP_ERROR_BREAK; 111 } 112 len = handle->cc; 113 if (len == 0) { 114 /* 115 * The buffer is empty; refill it. 116 * 117 * We ignore EINTR, as that might just be due to a signal 118 * being delivered - if the signal should interrupt the 119 * loop, the signal handler should call pcap_breakloop() 120 * to set handle->break_loop (we ignore it on other 121 * platforms as well). 122 */ 123 do { 124 len = recv(handle->fd, handle->buffer, handle->bufsize, 0); 125 if (handle->break_loop) { 126 handle->break_loop = 0; 127 return PCAP_ERROR_BREAK; 128 } 129 if (len == -1 && errno == ENOBUFS) 130 handlep->packets_nobufs++; 131 } while ((len == -1) && (errno == EINTR || errno == ENOBUFS)); 132 133 if (len < 0) { 134 pcapint_fmt_errmsg_for_errno(handle->errbuf, 135 PCAP_ERRBUF_SIZE, errno, "Can't receive packet"); 136 return PCAP_ERROR; 137 } 138 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 + len; 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 = (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) || (u_int)len < nlh->nlmsg_len) { 193 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Message truncated: (got: %zd) (nlmsg_len: %u)", len, 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 = (int)(ep - bp); 302 if (handle->cc < 0) 303 handle->cc = 0; 304 return count; 305 } 306 } 307 308 handle->cc = 0; 309 return count; 310 } 311 312 static int 313 netfilter_set_datalink(pcap_t *handle, int dlt) 314 { 315 handle->linktype = dlt; 316 return 0; 317 } 318 319 static int 320 netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats) 321 { 322 struct pcap_netfilter *handlep = handle->priv; 323 324 stats->ps_recv = handlep->packets_read; 325 stats->ps_drop = handlep->packets_nobufs; 326 stats->ps_ifdrop = 0; 327 return 0; 328 } 329 330 static int 331 netfilter_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_) 332 { 333 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 334 "Packet injection is not supported on netfilter devices"); 335 return (-1); 336 } 337 338 struct my_nfattr { 339 uint16_t nfa_len; 340 uint16_t nfa_type; 341 void *data; 342 }; 343 344 static int 345 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) 346 { 347 char buf[1024] __attribute__ ((aligned)); 348 memset(buf, 0, sizeof(buf)); 349 350 struct nlmsghdr *nlh = (struct nlmsghdr *) buf; 351 struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr)); 352 353 struct sockaddr_nl snl; 354 static unsigned int seq_id; 355 356 if (!seq_id) 357 DIAG_OFF_NARROWING 358 seq_id = time(NULL); 359 DIAG_ON_NARROWING 360 ++seq_id; 361 362 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg)); 363 nlh->nlmsg_type = msg_type; 364 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0); 365 nlh->nlmsg_pid = 0; /* to kernel */ 366 nlh->nlmsg_seq = seq_id; 367 368 nfg->nfgen_family = family; 369 nfg->version = NFNETLINK_V0; 370 nfg->res_id = htons(res_id); 371 372 if (mynfa) { 373 struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len)); 374 375 nfa->nfa_type = mynfa->nfa_type; 376 nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len); 377 memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len); 378 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len); 379 } 380 381 memset(&snl, 0, sizeof(snl)); 382 snl.nl_family = AF_NETLINK; 383 384 if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1) 385 return -1; 386 387 if (!ack) 388 return 0; 389 390 /* waiting for reply loop */ 391 do { 392 socklen_t addrlen = sizeof(snl); 393 int len; 394 395 /* ignore interrupt system call error */ 396 do { 397 /* 398 * The buffer is not so big that its size won't 399 * fit into an int. 400 */ 401 len = (int)recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen); 402 } while ((len == -1) && (errno == EINTR)); 403 404 if (len <= 0) 405 return len; 406 407 if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) { 408 errno = EINVAL; 409 return -1; 410 } 411 412 nlh = (struct nlmsghdr *) buf; 413 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */ 414 continue; 415 416 while ((u_int)len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, (u_int)len)) { 417 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) { 418 if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) { 419 errno = EBADMSG; 420 return -1; 421 } 422 errno = -(*((int *)NLMSG_DATA(nlh))); 423 return (errno == 0) ? 0 : -1; 424 } 425 nlh = NLMSG_NEXT(nlh, len); 426 } 427 } while (1); 428 429 return -1; /* never here */ 430 } 431 432 static int 433 nflog_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa) 434 { 435 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa); 436 } 437 438 static int 439 nflog_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int8_t family) 440 { 441 struct nfulnl_msg_config_cmd msg; 442 struct my_nfattr nfa; 443 444 msg.command = cmd; 445 446 nfa.data = &msg; 447 nfa.nfa_type = NFULA_CFG_CMD; 448 nfa.nfa_len = sizeof(msg); 449 450 return nflog_send_config_msg(handle, family, group_id, &nfa); 451 } 452 453 static int 454 nflog_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range) 455 { 456 struct nfulnl_msg_config_mode msg; 457 struct my_nfattr nfa; 458 459 msg.copy_range = htonl(copy_range); 460 msg.copy_mode = copy_mode; 461 462 nfa.data = &msg; 463 nfa.nfa_type = NFULA_CFG_MODE; 464 nfa.nfa_len = sizeof(msg); 465 466 return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa); 467 } 468 469 static int 470 nfqueue_send_verdict(const pcap_t *handle, uint16_t group_id, u_int32_t id, u_int32_t verdict) 471 { 472 struct nfqnl_msg_verdict_hdr msg; 473 struct my_nfattr nfa; 474 475 msg.id = htonl(id); 476 msg.verdict = htonl(verdict); 477 478 nfa.data = &msg; 479 nfa.nfa_type = NFQA_VERDICT_HDR; 480 nfa.nfa_len = sizeof(msg); 481 482 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa); 483 } 484 485 static int 486 nfqueue_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa) 487 { 488 return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa); 489 } 490 491 static int 492 nfqueue_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int16_t pf) 493 { 494 struct nfqnl_msg_config_cmd msg; 495 struct my_nfattr nfa; 496 497 msg.command = cmd; 498 msg.pf = htons(pf); 499 500 nfa.data = &msg; 501 nfa.nfa_type = NFQA_CFG_CMD; 502 nfa.nfa_len = sizeof(msg); 503 504 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa); 505 } 506 507 static int 508 nfqueue_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range) 509 { 510 struct nfqnl_msg_config_params msg; 511 struct my_nfattr nfa; 512 513 msg.copy_range = htonl(copy_range); 514 msg.copy_mode = copy_mode; 515 516 nfa.data = &msg; 517 nfa.nfa_type = NFQA_CFG_PARAMS; 518 nfa.nfa_len = sizeof(msg); 519 520 return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa); 521 } 522 523 static int 524 netfilter_activate(pcap_t* handle) 525 { 526 const char *dev = handle->opt.device; 527 unsigned short groups[32]; 528 int group_count = 0; 529 nftype_t type = OTHER; 530 int i; 531 532 if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) { 533 dev += strlen(NFLOG_IFACE); 534 type = NFLOG; 535 536 } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) { 537 dev += strlen(NFQUEUE_IFACE); 538 type = NFQUEUE; 539 } 540 541 if (type != OTHER && *dev == ':') { 542 dev++; 543 while (*dev) { 544 long int group_id; 545 char *end_dev; 546 547 if (group_count == 32) { 548 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 549 "Maximum 32 netfilter groups! dev: %s", 550 handle->opt.device); 551 return PCAP_ERROR; 552 } 553 554 group_id = strtol(dev, &end_dev, 0); 555 if (end_dev != dev) { 556 if (group_id < 0 || group_id > 65535) { 557 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 558 "Netfilter group range from 0 to 65535 (got %ld)", 559 group_id); 560 return PCAP_ERROR; 561 } 562 563 groups[group_count++] = (unsigned short) group_id; 564 dev = end_dev; 565 } 566 if (*dev != ',') 567 break; 568 dev++; 569 } 570 } 571 572 if (type == OTHER || *dev) { 573 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 574 "Can't get netfilter group(s) index from %s", 575 handle->opt.device); 576 return PCAP_ERROR; 577 } 578 579 /* if no groups, add default: 0 */ 580 if (!group_count) { 581 groups[0] = 0; 582 group_count = 1; 583 } 584 585 /* 586 * Turn a negative snapshot value (invalid), a snapshot value of 587 * 0 (unspecified), or a value bigger than the normal maximum 588 * value, into the maximum allowed value. 589 * 590 * If some application really *needs* a bigger snapshot 591 * length, we should just increase MAXIMUM_SNAPLEN. 592 */ 593 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) 594 handle->snapshot = MAXIMUM_SNAPLEN; 595 596 /* Initialize some components of the pcap structure. */ 597 handle->bufsize = 128 + handle->snapshot; 598 handle->offset = 0; 599 handle->read_op = netfilter_read_linux; 600 handle->inject_op = netfilter_inject_linux; 601 handle->setfilter_op = pcapint_install_bpf_program; /* no kernel filtering */ 602 handle->setdirection_op = NULL; 603 handle->set_datalink_op = netfilter_set_datalink; 604 handle->getnonblock_op = pcapint_getnonblock_fd; 605 handle->setnonblock_op = pcapint_setnonblock_fd; 606 handle->stats_op = netfilter_stats_linux; 607 608 /* Create netlink socket */ 609 handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER); 610 if (handle->fd < 0) { 611 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 612 errno, "Can't create raw socket"); 613 return PCAP_ERROR; 614 } 615 616 if (type == NFLOG) { 617 handle->linktype = DLT_NFLOG; 618 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 619 if (handle->dlt_list == NULL) { 620 pcapint_fmt_errmsg_for_errno(handle->errbuf, 621 PCAP_ERRBUF_SIZE, errno, 622 "Can't allocate DLT list"); 623 goto close_fail; 624 } 625 handle->dlt_list[0] = DLT_NFLOG; 626 handle->dlt_list[1] = DLT_IPV4; 627 handle->dlt_count = 2; 628 } else 629 handle->linktype = DLT_IPV4; 630 631 handle->buffer = malloc(handle->bufsize); 632 if (!handle->buffer) { 633 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 634 errno, "Can't allocate dump buffer"); 635 goto close_fail; 636 } 637 638 if (type == NFLOG) { 639 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) { 640 pcapint_fmt_errmsg_for_errno(handle->errbuf, 641 PCAP_ERRBUF_SIZE, errno, 642 "NFULNL_CFG_CMD_PF_UNBIND"); 643 goto close_fail; 644 } 645 646 if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) { 647 pcapint_fmt_errmsg_for_errno(handle->errbuf, 648 PCAP_ERRBUF_SIZE, errno, "NFULNL_CFG_CMD_PF_BIND"); 649 goto close_fail; 650 } 651 652 /* Bind socket to the nflog groups */ 653 for (i = 0; i < group_count; i++) { 654 if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) { 655 pcapint_fmt_errmsg_for_errno(handle->errbuf, 656 PCAP_ERRBUF_SIZE, errno, 657 "Can't listen on group index"); 658 goto close_fail; 659 } 660 661 if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) { 662 pcapint_fmt_errmsg_for_errno(handle->errbuf, 663 PCAP_ERRBUF_SIZE, errno, 664 "NFULNL_COPY_PACKET"); 665 goto close_fail; 666 } 667 } 668 669 } else { 670 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) { 671 pcapint_fmt_errmsg_for_errno(handle->errbuf, 672 PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_UNBIND"); 673 goto close_fail; 674 } 675 676 if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) { 677 pcapint_fmt_errmsg_for_errno(handle->errbuf, 678 PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_BIND"); 679 goto close_fail; 680 } 681 682 /* Bind socket to the nfqueue groups */ 683 for (i = 0; i < group_count; i++) { 684 if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) { 685 pcapint_fmt_errmsg_for_errno(handle->errbuf, 686 PCAP_ERRBUF_SIZE, errno, 687 "Can't listen on group index"); 688 goto close_fail; 689 } 690 691 if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) { 692 pcapint_fmt_errmsg_for_errno(handle->errbuf, 693 PCAP_ERRBUF_SIZE, errno, 694 "NFQNL_COPY_PACKET"); 695 goto close_fail; 696 } 697 } 698 } 699 700 if (handle->opt.rfmon) { 701 /* 702 * Monitor mode doesn't apply to netfilter devices. 703 */ 704 pcapint_cleanup_live_common(handle); 705 return PCAP_ERROR_RFMON_NOTSUP; 706 } 707 708 if (handle->opt.buffer_size != 0) { 709 /* 710 * Set the socket buffer size to the specified value. 711 */ 712 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) { 713 pcapint_fmt_errmsg_for_errno(handle->errbuf, 714 PCAP_ERRBUF_SIZE, errno, "SO_RCVBUF"); 715 goto close_fail; 716 } 717 } 718 719 handle->selectable_fd = handle->fd; 720 return 0; 721 722 close_fail: 723 pcapint_cleanup_live_common(handle); 724 return PCAP_ERROR; 725 } 726 727 pcap_t * 728 netfilter_create(const char *device, char *ebuf, int *is_ours) 729 { 730 const char *cp; 731 pcap_t *p; 732 733 /* Does this look like an netfilter device? */ 734 cp = strrchr(device, '/'); 735 if (cp == NULL) 736 cp = device; 737 738 /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */ 739 if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0) 740 cp += sizeof NFLOG_IFACE - 1; 741 else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0) 742 cp += sizeof NFQUEUE_IFACE - 1; 743 else { 744 /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */ 745 *is_ours = 0; 746 return NULL; 747 } 748 749 /* 750 * Yes - is that either the end of the name, or is it followed 751 * by a colon? 752 */ 753 if (*cp != ':' && *cp != '\0') { 754 /* Nope */ 755 *is_ours = 0; 756 return NULL; 757 } 758 759 /* OK, it's probably ours. */ 760 *is_ours = 1; 761 762 p = PCAP_CREATE_COMMON(ebuf, struct pcap_netfilter); 763 if (p == NULL) 764 return (NULL); 765 766 p->activate_op = netfilter_activate; 767 return (p); 768 } 769 770 int 771 netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str) 772 { 773 int sock; 774 775 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER); 776 if (sock < 0) { 777 /* if netlink is not supported this is not fatal */ 778 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) 779 return 0; 780 pcapint_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, 781 errno, "Can't open netlink socket"); 782 return -1; 783 } 784 close(sock); 785 786 /* 787 * The notion of "connected" vs. "disconnected" doesn't apply. 788 * XXX - what about "up" and "running"? 789 */ 790 if (pcapint_add_dev(devlistp, NFLOG_IFACE, 791 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, 792 "Linux netfilter log (NFLOG) interface", err_str) == NULL) 793 return -1; 794 if (pcapint_add_dev(devlistp, NFQUEUE_IFACE, 795 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, 796 "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL) 797 return -1; 798 return 0; 799 } 800