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