/
libnflog.c
311 lines (259 loc) · 7.71 KB
/
libnflog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright 2014 Google Inc. All Rights Reserved.
* Author: willemb@google.com (Willem de Bruijn)
*
* Netlink support library
* Geared at NETLINK_NETFILTER
*
* Read netfilter nflog output, for instance:
* `iptables -A OUTPUT -j NFLOG --nflog-group=10`
*
* To timestamp every packet, use the xt_time match:
* `iptables -A OUTPUT \
* -m time --timestart 00:00 --timestop 23:59 \
* -j NFLOG --nflog-group=10`
* or even
* `iptables -A OUTPUT -m time -j NFLOG --nflog-group=10`
*
* TODO(willemb): optimize by using mmapped ring.
*/
#define _GNU_SOURCE
#define _BSD_SOURCE /* for be64toh */
#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <linux/netlink.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_log.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libnflog.h"
static int config_group = 10; /* nfnetlink group to follow */
static int config_debug_lvl = 0;
#define IOVLEN 8
#define PKTLEN (1 << 11)
static void __nflog_sendcmd(int fd, uint8_t cmd, void *msg, int msglen,
uint16_t family, uint16_t group_id)
{
static int seq_id;
char buf[1024] __attribute__((aligned));
struct nlmsghdr *nh;
struct nfgenmsg *ng;
struct nfattr *nfa;
int ret;
memset(buf, 0, sizeof(buf));
nh = (void *) buf;
ng = (void *) buf + sizeof(*nh);
nh->nlmsg_len = NLMSG_LENGTH(sizeof(*ng));
nh->nlmsg_type = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nh->nlmsg_pid = 0;
nh->nlmsg_seq = ++seq_id;
ng->nfgen_family = family;
ng->version = NFNETLINK_V0;
ng->res_id = htons(group_id);
nfa = (void *) buf + NLMSG_ALIGN(nh->nlmsg_len);
nfa->nfa_type = cmd;
nfa->nfa_len = NFA_LENGTH(msglen);
memcpy(NFA_DATA(nfa), msg, msglen);
nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
if (send(fd, buf, nh->nlmsg_len, 0) != nh->nlmsg_len)
error(1, errno, "sendcmd");
/* TODO(willemb): handle EINTR */
ret = recv(fd, buf, sizeof(buf), 0);
if (ret == -1)
error(1, errno, "recv ctrl: sock error");
if (ret < NLMSG_OK(nh, ret))
error(1, 0, "recv ctrl: insufficient length");
if (nh->nlmsg_type != NLMSG_ERROR)
error(1, 0, "recv ctrl: unexpected type");
ret = *(int *) NLMSG_DATA(nh);
if (ret)
error(1, ret, "recv ctrl: nflog error");
}
static void nflog_sendcmd(int fd, uint8_t cmd, uint16_t family,
uint16_t group_id)
{
struct nfulnl_msg_config_cmd msg;
memset(&msg, 0, sizeof(msg));
msg.command = cmd;
__nflog_sendcmd(fd, NFULA_CFG_CMD, &msg, sizeof(msg), family, group_id);
}
static void nflog_sendcmd_mode(int fd, uint16_t family, uint16_t group_id,
uint8_t mode, uint32_t value)
{
struct nfulnl_msg_config_mode msg;
memset(&msg, 0, sizeof(msg));
msg.copy_mode = mode;
msg.copy_range = htonl(value);
__nflog_sendcmd(fd, NFULA_CFG_MODE, &msg, sizeof(msg), family, group_id);
}
static void nflog_attach_inet(int fd, unsigned int snaplen)
{
nflog_sendcmd(fd, NFULNL_CFG_CMD_PF_UNBIND, AF_INET, 0);
/* TODO: recv ack */
nflog_sendcmd(fd, NFULNL_CFG_CMD_PF_BIND, AF_INET, 0);
/* TODO: recv ack */
nflog_sendcmd(fd, NFULNL_CFG_CMD_BIND, AF_UNSPEC, config_group);
/* TODO: recv ack */
nflog_sendcmd_mode(fd, AF_UNSPEC, config_group, NFULNL_COPY_PACKET, snaplen);
/* TODO: recv ack */
}
int nflog_init(unsigned int snaplen)
{
struct sockaddr_nl nladdr;
int fd, val;
if (snaplen > PKTLEN)
error(1, 0, "snaplen exceeds pktlen: can cause drops");
fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
if (fd == -1)
error(1, errno, "socket");
val = 1 << 21;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)))
error(1, errno, "setsockopt SO_RCVBUF");
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_groups = 1 << config_group;
if (bind(fd, (void *) &nladdr, sizeof(nladdr)))
error(1, errno, "bind");
nflog_attach_inet(fd, snaplen);
return fd;
}
void nflog_exit(int fd)
{
if (close(fd))
error(1, errno, "close");
}
void nflog_parse(const void *data, unsigned int len, log_fn fn)
{
const struct nlmsghdr *nh;
for (nh = (void *) data; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
const struct nfulnl_msg_packet_timestamp *nf_ts;
const struct nfgenmsg *ng;
const struct nfattr *attr;
uint64_t ts_sec = 0, ts_usec = 0;
const char *pkt;
int plen = 0;
int alen;
if (nh->nlmsg_type == NLMSG_ERROR)
error(1, 0, "netlink error");
if (nh->nlmsg_type == NLMSG_NOOP)
error(1, 0, "netlink noop");
if (nh->nlmsg_len < sizeof(*nh) || len < nh->nlmsg_len) {
fprintf(stderr, "message truncated\n");
continue;
}
ng = NLMSG_DATA(nh);
if (config_debug_lvl)
fprintf(stderr, "P family=%s version=%d group=%d len=%d type=%hu\n",
ng->nfgen_family == AF_INET ? "INET" : "other",
ng->version,
ntohs(ng->res_id),
nh->nlmsg_len,
nh->nlmsg_type);
attr = NFM_NFA(ng);
alen = nh->nlmsg_len - NLMSG_LENGTH(NLMSG_ALIGN(sizeof(*ng)));
while (NFA_OK(attr, alen)) {
switch (NFA_TYPE(attr)) {
case NFULA_PAYLOAD:
pkt = NFA_DATA(attr);
plen = NFA_PAYLOAD(attr);
break;
case NFULA_TIMESTAMP:
nf_ts = NFA_DATA(attr);
ts_sec = be64toh(nf_ts->sec);
ts_usec = be64toh(nf_ts->usec);
break;
case NFULA_GID:
case NFULA_PACKET_HDR:
case NFULA_PREFIX:
case NFULA_IFINDEX_OUTDEV:
case NFULA_UID:
default:
if (config_debug_lvl)
fprintf(stderr, " attr @%lu other type=%d\n",
((unsigned long) attr) - (unsigned long) ng,
NFA_TYPE(attr));
}
attr = NFA_NEXT(attr, alen);
}
if (nh->nlmsg_type == NLMSG_DONE)
break;
fn(pkt, plen, ts_sec, ts_usec);
}
}
int nflog_read(int fd, log_fn fn)
{
static char data[IOVLEN][PKTLEN];
struct mmsghdr msgs[IOVLEN];
struct iovec iovecs[IOVLEN];
int i, len;
memset(msgs, 0, sizeof(msgs));
for (i = 0; i < IOVLEN; i++) {
iovecs[i].iov_base = data[i];
iovecs[i].iov_len = PKTLEN;
msgs[i].msg_hdr.msg_iov = &iovecs[i];
msgs[i].msg_hdr.msg_iovlen = 1;
}
len = recvmmsg(fd, msgs, IOVLEN, MSG_DONTWAIT, NULL);
if (len == -1) {
if (errno == EAGAIN || errno == EINTR)
return 0;
if (errno == ENOBUFS) {
static int report_overflow;
if (!report_overflow) {
report_overflow = 1;
fprintf(stderr, "nflog: socket overflow detected. some packets will be lost (only warning once).\n");
}
return 0;
}
error(1, errno, "recvmsg");
}
if (config_debug_lvl > 1)
fprintf(stderr, "recvmmsg len=%u\n", len);
for (i = 0; i < len; i++)
nflog_parse(data[i], msgs[i].msg_len, fn);
return 1;
}
static int nflog_wait(int fd)
{
struct pollfd pollfd[2];
int len;
do {
memset(&pollfd, 0, sizeof(pollfd));
pollfd[0].events = POLLIN;
pollfd[0].fd = 0;
pollfd[1].events = POLLIN;
pollfd[1].fd = fd;
len = poll(pollfd, 2, 50);
if (len == -1) {
if (errno == EINTR)
continue;
error(1, errno, "poll");
}
if (len && pollfd[0].revents)
return 0;
} while (!len);
return 1;
}
void nflog_loop(int fd, log_fn fn)
{
while (nflog_wait(fd)) {
while (nflog_read(fd, fn)) {}
}
}
void nflog_all(log_fn fn, unsigned int snaplen)
{
int fd;
fd = nflog_init(snaplen);
nflog_loop(fd, fn);
nflog_exit(fd);
}