2 * Copyright (c) 2011 Jakub Zawadzki
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
37 #ifdef NEED_STRERROR_H
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
50 #include <netinet/in.h>
51 #include <linux/types.h>
53 #include <linux/netlink.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_log.h>
57 #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
59 #define NFLOG_IFACE "nflog"
62 nflog_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
64 const unsigned char *buf
;
68 /* ignore interrupt system call error */
70 len
= recv(handle
->fd
, handle
->buffer
, handle
->bufsize
, 0);
71 if (handle
->break_loop
) {
72 handle
->break_loop
= 0;
75 } while ((len
== -1) && (errno
== EINTR
));
78 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't receive packet %d:%s", errno
, pcap_strerror(errno
));
83 while (len
>= NLMSG_SPACE(0)) {
84 const struct nlmsghdr
*nlh
= (const struct nlmsghdr
*) buf
;
87 if (nlh
->nlmsg_len
< sizeof(struct nlmsghdr
) || len
< nlh
->nlmsg_len
) {
88 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Message truncated: (got: %d) (nlmsg_len: %u)", len
, nlh
->nlmsg_len
);
92 if (NFNL_SUBSYS_ID(nlh
->nlmsg_type
) == NFNL_SUBSYS_ULOG
&&
93 NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFULNL_MSG_PACKET
)
95 const struct nfattr
*payload_attr
= NULL
;
97 if (nlh
->nlmsg_len
< HDR_LENGTH
) {
98 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Malformed message: (nlmsg_len: %u)", nlh
->nlmsg_len
);
102 if (nlh
->nlmsg_len
> HDR_LENGTH
) {
103 struct nfattr
*attr
= NFM_NFA(NLMSG_DATA(nlh
));
104 int attr_len
= nlh
->nlmsg_len
- NLMSG_ALIGN(HDR_LENGTH
);
106 while (NFA_OK(attr
, attr_len
)) {
107 switch (NFA_TYPE(attr
)) {
112 attr
= NFA_NEXT(attr
, attr_len
);
117 struct pcap_pkthdr pkth
;
119 const unsigned char *payload
= NFA_DATA(payload_attr
);
120 int payload_len
= NFA_PAYLOAD(payload_attr
);
122 pkth
.len
= pkth
.caplen
= payload_len
;
123 /* pkth.caplen = min (payload_len, handle->snapshot); */
125 gettimeofday(&pkth
.ts
, NULL
);
126 if (handle
->fcode
.bf_insns
== NULL
||
127 bpf_filter(handle
->fcode
.bf_insns
, payload
, pkth
.len
, pkth
.caplen
))
129 handle
->md
.packets_read
++;
130 callback(user
, &pkth
, payload
);
136 msg_len
= NLMSG_ALIGN(nlh
->nlmsg_len
);
147 netfilter_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
149 stats
->ps_recv
= handle
->md
.packets_read
;
151 stats
->ps_ifdrop
= 0;
156 netfilter_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
158 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on netfilter devices");
169 nflog_send_config_msg(const pcap_t
*handle
, u_int8_t family
, u_int16_t res_id
, const struct my_nfattr
*mynfa
)
171 char buf
[1024] __attribute__ ((aligned
));
173 struct nlmsghdr
*nlh
= (struct nlmsghdr
*) buf
;
174 struct nfgenmsg
*nfg
= (struct nfgenmsg
*) (buf
+ sizeof(struct nlmsghdr
));
176 struct sockaddr_nl snl
;
177 static unsigned int seq_id
;
183 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nfgenmsg
));
184 nlh
->nlmsg_type
= (NFNL_SUBSYS_ULOG
<< 8) | NFULNL_MSG_CONFIG
;
185 nlh
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
186 nlh
->nlmsg_pid
= 0; /* to kernel */
187 nlh
->nlmsg_seq
= seq_id
;
189 nfg
->nfgen_family
= family
;
190 nfg
->version
= NFNETLINK_V0
;
191 nfg
->res_id
= htons(res_id
);
194 struct nfattr
*nfa
= (struct nfattr
*) (buf
+ NLMSG_ALIGN(nlh
->nlmsg_len
));
196 nfa
->nfa_type
= mynfa
->nfa_type
;
197 nfa
->nfa_len
= NFA_LENGTH(mynfa
->nfa_len
);
198 memcpy(NFA_DATA(nfa
), mynfa
->data
, mynfa
->nfa_len
);
199 nlh
->nlmsg_len
= NLMSG_ALIGN(nlh
->nlmsg_len
) + NFA_ALIGN(nfa
->nfa_len
);
202 memset(&snl
, 0, sizeof(snl
));
203 snl
.nl_family
= AF_NETLINK
;
205 if (sendto(handle
->fd
, nlh
, nlh
->nlmsg_len
, 0, (struct sockaddr
*) &snl
, sizeof(snl
)) == -1)
208 /* waiting for reply loop */
210 socklen_t addrlen
= sizeof(snl
);
213 /* ignore interrupt system call error */
215 len
= recvfrom(handle
->fd
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &snl
, &addrlen
);
216 } while ((len
== -1) && (errno
== EINTR
));
221 if (addrlen
!= sizeof(snl
) || snl
.nl_family
!= AF_NETLINK
) {
226 nlh
= (struct nlmsghdr
*) buf
;
227 if (snl
.nl_pid
!= 0 || seq_id
!= nlh
->nlmsg_seq
) /* if not from kernel or wrong sequence skip */
230 while (len
>= NLMSG_SPACE(0) && NLMSG_OK(nlh
, len
)) {
231 if (nlh
->nlmsg_type
== NLMSG_ERROR
|| (nlh
->nlmsg_type
== NLMSG_DONE
&& nlh
->nlmsg_flags
& NLM_F_MULTI
)) {
232 if (nlh
->nlmsg_len
< NLMSG_ALIGN(sizeof(struct nlmsgerr
))) {
236 errno
= -(*((int *)NLMSG_DATA(nlh
)));
237 return (errno
== 0) ? 0 : -1;
239 nlh
= NLMSG_NEXT(nlh
, len
);
243 return -1; /* never here */
247 nflog_send_config_cmd(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t cmd
, u_int8_t family
)
249 struct nfulnl_msg_config_cmd msg
;
250 struct my_nfattr nfa
;
255 nfa
.nfa_type
= NFULA_CFG_CMD
;
256 nfa
.nfa_len
= sizeof(msg
);
258 return nflog_send_config_msg(handle
, family
, group_id
, &nfa
);
262 nflog_send_config_mode(const pcap_t
*handle
, u_int16_t group_id
, u_int8_t copy_mode
, u_int32_t copy_range
)
264 struct nfulnl_msg_config_mode msg
;
265 struct my_nfattr nfa
;
267 msg
.copy_range
= htonl(copy_range
);
268 msg
.copy_mode
= copy_mode
;
271 nfa
.nfa_type
= NFULA_CFG_MODE
;
272 nfa
.nfa_len
= sizeof(msg
);
274 return nflog_send_config_msg(handle
, AF_UNSPEC
, group_id
, &nfa
);
278 nflog_activate(pcap_t
* handle
)
280 const char *dev
= handle
->opt
.source
;
281 unsigned short groups
[32];
285 if (strncmp(dev
, NFLOG_IFACE
, strlen(NFLOG_IFACE
)) == 0) {
286 dev
+= strlen(NFLOG_IFACE
);
288 /* nflog:30,33,42 looks nice, allow it */
296 if (group_count
== 32) {
297 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
298 "Maximum 32 netfilter groups! dev: %s",
303 group_id
= strtol(dev
, &end_dev
, 0);
304 if (end_dev
!= dev
) {
305 if (group_id
< 0 || group_id
> 65535) {
306 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
307 "Netfilter group range from 0 to 65535 (got %ld)",
312 groups
[group_count
++] = (unsigned short) group_id
;
322 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
323 "Can't get netfilter group(s) index from %s",
328 /* if no groups, add default: 0 */
334 /* Initialize some components of the pcap structure. */
335 handle
->bufsize
= 128 + handle
->snapshot
;
337 handle
->linktype
= DLT_IPV4
;
338 handle
->read_op
= nflog_read_linux
;
339 handle
->inject_op
= netfilter_inject_linux
;
340 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
341 handle
->setdirection_op
= NULL
;
342 handle
->set_datalink_op
= NULL
;
343 handle
->getnonblock_op
= pcap_getnonblock_fd
;
344 handle
->setnonblock_op
= pcap_setnonblock_fd
;
345 handle
->stats_op
= netfilter_stats_linux
;
347 /* Create netlink socket */
348 handle
->fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_NETFILTER
);
349 if (handle
->fd
< 0) {
350 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't create raw socket %d:%s", errno
, pcap_strerror(errno
));
354 handle
->buffer
= malloc(handle
->bufsize
);
355 if (!handle
->buffer
) {
356 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate dump buffer: %s", pcap_strerror(errno
));
360 if (nflog_send_config_cmd(handle
, 0, NFULNL_CFG_CMD_PF_UNBIND
, AF_INET
) < 0) {
361 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno
));
365 if (nflog_send_config_cmd(handle
, 0, NFULNL_CFG_CMD_PF_BIND
, AF_INET
) < 0) {
366 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno
));
370 /* Bind socket to the nflog groups */
371 for (i
= 0; i
< group_count
; i
++) {
372 if (nflog_send_config_cmd(handle
, groups
[i
], NFULNL_CFG_CMD_BIND
, AF_UNSPEC
) < 0) {
373 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't listen on group group index: %s", pcap_strerror(errno
));
377 if (nflog_send_config_mode(handle
, groups
[i
], NFULNL_COPY_PACKET
, handle
->snapshot
) < 0) {
378 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno
));
383 if (handle
->opt
.rfmon
) {
385 * Monitor mode doesn't apply to netfilter devices.
387 pcap_cleanup_live_common(handle
);
388 return PCAP_ERROR_RFMON_NOTSUP
;
391 if (handle
->opt
.buffer_size
!= 0) {
393 * Set the socket buffer size to the specified value.
395 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_RCVBUF
, &handle
->opt
.buffer_size
, sizeof(handle
->opt
.buffer_size
)) == -1) {
396 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "SO_RCVBUF: %s", pcap_strerror(errno
));
401 handle
->selectable_fd
= handle
->fd
;
405 pcap_cleanup_live_common(handle
);
410 nflog_create(const char *device
, char *ebuf
)
414 p
= pcap_create_common(device
, ebuf
);
418 p
->activate_op
= nflog_activate
;
423 netfilter_platform_finddevs(pcap_if_t
**alldevsp
, char *err_str
)
425 pcap_if_t
*found_dev
= *alldevsp
;
428 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_NETFILTER
);
430 /* if netlink is not supported this this is not fatal */
431 if (errno
== EAFNOSUPPORT
)
433 snprintf(err_str
, PCAP_ERRBUF_SIZE
, "Can't open netlink socket %d:%s",
434 errno
, pcap_strerror(errno
));
439 if (pcap_add_if(&found_dev
, NFLOG_IFACE
, 0, "Linux netfilter log (NFLOG) interface", err_str
) < 0)