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