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