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