Merge tag 'backlight-for-linus-4.2' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / net / netfilter / nfnetlink_log.c
CommitLineData
0597f268
HW
1/*
2 * This is a module which is used for logging packets to userspace via
3 * nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
f229f6ce 6 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
0597f268
HW
7 *
8 * Based on the old ipv4-only ipt_ULOG.c:
9 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
0597f268 14 */
beacd3e8
ML
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
0597f268
HW
18#include <linux/module.h>
19#include <linux/skbuff.h>
e035edd1 20#include <linux/if_arp.h>
0597f268
HW
21#include <linux/init.h>
22#include <linux/ip.h>
23#include <linux/ipv6.h>
24#include <linux/netdevice.h>
25#include <linux/netfilter.h>
c737b7c4 26#include <linux/netfilter_bridge.h>
573ce260 27#include <net/netlink.h>
0597f268
HW
28#include <linux/netfilter/nfnetlink.h>
29#include <linux/netfilter/nfnetlink_log.h>
30#include <linux/spinlock.h>
31#include <linux/sysctl.h>
32#include <linux/proc_fs.h>
33#include <linux/security.h>
34#include <linux/list.h>
5a0e3ad6 35#include <linux/slab.h>
0597f268 36#include <net/sock.h>
f01ffbd6 37#include <net/netfilter/nf_log.h>
9368a53c 38#include <net/netns/generic.h>
d9e15007 39#include <net/netfilter/nfnetlink_log.h>
0597f268 40
60063497 41#include <linux/atomic.h>
0597f268 42
1109a90c 43#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
fbcd923c
HW
44#include "../bridge/br_private.h"
45#endif
46
c2db2924 47#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
2c6764b7 48#define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
0597f268 49#define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
c1e7dc91
FW
50/* max packet size is limited by 16-bit struct nfattr nfa_len field */
51#define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
0597f268
HW
52
53#define PRINTR(x, args...) do { if (net_ratelimit()) \
54 printk(x, ## args); } while (0);
55
0597f268
HW
56struct nfulnl_instance {
57 struct hlist_node hlist; /* global list of instances */
58 spinlock_t lock;
59 atomic_t use; /* use count */
60
61 unsigned int qlen; /* number of nlmsgs in skb */
62 struct sk_buff *skb; /* pre-allocatd skb */
0597f268 63 struct timer_list timer;
9368a53c 64 struct net *net;
9eea9515 65 struct user_namespace *peer_user_ns; /* User namespace of the peer process */
cc6bc448 66 u32 peer_portid; /* PORTID of the peer process */
0597f268
HW
67
68 /* configurable parameters */
69 unsigned int flushtimeout; /* timeout until queue flush */
70 unsigned int nlbufsiz; /* netlink buffer allocation size */
71 unsigned int qthreshold; /* threshold of the queue */
72 u_int32_t copy_range;
0af5f6c1 73 u_int32_t seq; /* instance-local sequential counter */
0597f268 74 u_int16_t group_num; /* number of this queue */
0af5f6c1 75 u_int16_t flags;
601e68e1 76 u_int8_t copy_mode;
bed1be20 77 struct rcu_head rcu;
0597f268
HW
78};
79
0597f268 80#define INSTANCE_BUCKETS 16
0597f268 81
9368a53c
G
82static int nfnl_log_net_id __read_mostly;
83
84struct nfnl_log_net {
85 spinlock_t instances_lock;
86 struct hlist_head instance_table[INSTANCE_BUCKETS];
87 atomic_t global_seq;
88};
89
90static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
91{
92 return net_generic(net, nfnl_log_net_id);
93}
94
0597f268
HW
95static inline u_int8_t instance_hashfn(u_int16_t group_num)
96{
97 return ((group_num & 0xff) % INSTANCE_BUCKETS);
98}
99
100static struct nfulnl_instance *
9368a53c 101__instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
0597f268
HW
102{
103 struct hlist_head *head;
0597f268
HW
104 struct nfulnl_instance *inst;
105
9368a53c 106 head = &log->instance_table[instance_hashfn(group_num)];
b67bfe0d 107 hlist_for_each_entry_rcu(inst, head, hlist) {
0597f268
HW
108 if (inst->group_num == group_num)
109 return inst;
110 }
111 return NULL;
112}
113
114static inline void
115instance_get(struct nfulnl_instance *inst)
116{
117 atomic_inc(&inst->use);
118}
119
120static struct nfulnl_instance *
9368a53c 121instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
0597f268
HW
122{
123 struct nfulnl_instance *inst;
124
bed1be20 125 rcu_read_lock_bh();
9368a53c 126 inst = __instance_lookup(log, group_num);
f5c5440d
ED
127 if (inst && !atomic_inc_not_zero(&inst->use))
128 inst = NULL;
bed1be20 129 rcu_read_unlock_bh();
0597f268
HW
130
131 return inst;
132}
133
bed1be20
ED
134static void nfulnl_instance_free_rcu(struct rcu_head *head)
135{
9368a53c
G
136 struct nfulnl_instance *inst =
137 container_of(head, struct nfulnl_instance, rcu);
138
139 put_net(inst->net);
140 kfree(inst);
bed1be20
ED
141 module_put(THIS_MODULE);
142}
143
0597f268
HW
144static void
145instance_put(struct nfulnl_instance *inst)
146{
bed1be20
ED
147 if (inst && atomic_dec_and_test(&inst->use))
148 call_rcu_bh(&inst->rcu, nfulnl_instance_free_rcu);
0597f268
HW
149}
150
151static void nfulnl_timer(unsigned long data);
152
153static struct nfulnl_instance *
9368a53c 154instance_create(struct net *net, u_int16_t group_num,
cc6bc448 155 u32 portid, struct user_namespace *user_ns)
0597f268
HW
156{
157 struct nfulnl_instance *inst;
9368a53c 158 struct nfnl_log_net *log = nfnl_log_pernet(net);
baab2ce7 159 int err;
0597f268 160
9368a53c
G
161 spin_lock_bh(&log->instances_lock);
162 if (__instance_lookup(log, group_num)) {
baab2ce7 163 err = -EEXIST;
0597f268
HW
164 goto out_unlock;
165 }
166
10dfdc69 167 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
baab2ce7
PM
168 if (!inst) {
169 err = -ENOMEM;
0597f268 170 goto out_unlock;
baab2ce7 171 }
0597f268 172
aace57e0
MM
173 if (!try_module_get(THIS_MODULE)) {
174 kfree(inst);
baab2ce7 175 err = -EAGAIN;
aace57e0
MM
176 goto out_unlock;
177 }
178
0597f268 179 INIT_HLIST_NODE(&inst->hlist);
181a46a5 180 spin_lock_init(&inst->lock);
0597f268
HW
181 /* needs to be two, since we _put() after creation */
182 atomic_set(&inst->use, 2);
183
e6f689db 184 setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
0597f268 185
9368a53c 186 inst->net = get_net(net);
9eea9515 187 inst->peer_user_ns = user_ns;
15e47304 188 inst->peer_portid = portid;
0597f268
HW
189 inst->group_num = group_num;
190
191 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
192 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
193 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
194 inst->copy_mode = NFULNL_COPY_PACKET;
6b6ec99a 195 inst->copy_range = NFULNL_COPY_RANGE_MAX;
0597f268 196
f5c5440d 197 hlist_add_head_rcu(&inst->hlist,
9368a53c
G
198 &log->instance_table[instance_hashfn(group_num)]);
199
0597f268 200
9368a53c 201 spin_unlock_bh(&log->instances_lock);
0597f268
HW
202
203 return inst;
204
0597f268 205out_unlock:
9368a53c 206 spin_unlock_bh(&log->instances_lock);
baab2ce7 207 return ERR_PTR(err);
0597f268
HW
208}
209
e3567061 210static void __nfulnl_flush(struct nfulnl_instance *inst);
0597f268 211
f5c5440d 212/* called with BH disabled */
0597f268 213static void
9afdb00c 214__instance_destroy(struct nfulnl_instance *inst)
0597f268
HW
215{
216 /* first pull it out of the global list */
f5c5440d 217 hlist_del_rcu(&inst->hlist);
0597f268 218
0597f268
HW
219 /* then flush all pending packets from skb */
220
f5c5440d
ED
221 spin_lock(&inst->lock);
222
223 /* lockless readers wont be able to use us */
224 inst->copy_mode = NFULNL_COPY_DISABLED;
225
e3567061
MM
226 if (inst->skb)
227 __nfulnl_flush(inst);
f5c5440d 228 spin_unlock(&inst->lock);
0597f268
HW
229
230 /* and finally put the refcount */
231 instance_put(inst);
0597f268
HW
232}
233
0597f268 234static inline void
9368a53c
G
235instance_destroy(struct nfnl_log_net *log,
236 struct nfulnl_instance *inst)
0597f268 237{
9368a53c 238 spin_lock_bh(&log->instances_lock);
9afdb00c 239 __instance_destroy(inst);
9368a53c 240 spin_unlock_bh(&log->instances_lock);
0597f268
HW
241}
242
243static int
244nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
245 unsigned int range)
246{
247 int status = 0;
248
249 spin_lock_bh(&inst->lock);
601e68e1 250
0597f268
HW
251 switch (mode) {
252 case NFULNL_COPY_NONE:
253 case NFULNL_COPY_META:
254 inst->copy_mode = mode;
255 inst->copy_range = 0;
256 break;
601e68e1 257
0597f268
HW
258 case NFULNL_COPY_PACKET:
259 inst->copy_mode = mode;
c1e7dc91
FW
260 if (range == 0)
261 range = NFULNL_COPY_RANGE_MAX;
6b6ec99a
MM
262 inst->copy_range = min_t(unsigned int,
263 range, NFULNL_COPY_RANGE_MAX);
0597f268 264 break;
601e68e1 265
0597f268
HW
266 default:
267 status = -EINVAL;
268 break;
269 }
270
271 spin_unlock_bh(&inst->lock);
272
273 return status;
274}
275
276static int
277nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
278{
279 int status;
280
281 spin_lock_bh(&inst->lock);
282 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
283 status = -ERANGE;
284 else if (nlbufsiz > 131072)
285 status = -ERANGE;
286 else {
287 inst->nlbufsiz = nlbufsiz;
288 status = 0;
289 }
290 spin_unlock_bh(&inst->lock);
291
292 return status;
293}
294
295static int
296nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
297{
298 spin_lock_bh(&inst->lock);
299 inst->flushtimeout = timeout;
300 spin_unlock_bh(&inst->lock);
301
302 return 0;
303}
304
305static int
306nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
307{
308 spin_lock_bh(&inst->lock);
309 inst->qthreshold = qthresh;
310 spin_unlock_bh(&inst->lock);
311
312 return 0;
313}
314
0af5f6c1
HW
315static int
316nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
317{
318 spin_lock_bh(&inst->lock);
ee433530 319 inst->flags = flags;
0af5f6c1
HW
320 spin_unlock_bh(&inst->lock);
321
322 return 0;
323}
324
c6a8f648 325static struct sk_buff *
afff14f6
G
326nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
327 unsigned int pkt_size)
0597f268
HW
328{
329 struct sk_buff *skb;
ad2ad0f9 330 unsigned int n;
0597f268 331
0597f268
HW
332 /* alloc skb which should be big enough for a whole multipart
333 * message. WARNING: has to be <= 128k due to slab restrictions */
334
ad2ad0f9 335 n = max(inst_size, pkt_size);
afff14f6 336 skb = nfnetlink_alloc_skb(net, n, peer_portid, GFP_ATOMIC);
0597f268 337 if (!skb) {
ad2ad0f9
PM
338 if (n > pkt_size) {
339 /* try to allocate only as much as we need for current
340 * packet */
0597f268 341
afff14f6 342 skb = nfnetlink_alloc_skb(net, pkt_size,
3ab1f683 343 peer_portid, GFP_ATOMIC);
ad2ad0f9 344 }
0597f268
HW
345 }
346
347 return skb;
348}
349
b51d3fa3 350static void
0597f268
HW
351__nfulnl_send(struct nfulnl_instance *inst)
352{
d550d095
DM
353 if (inst->qlen > 1) {
354 struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
355 NLMSG_DONE,
356 sizeof(struct nfgenmsg),
357 0);
b51d3fa3
HL
358 if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
359 inst->skb->len, skb_tailroom(inst->skb))) {
360 kfree_skb(inst->skb);
d550d095 361 goto out;
b51d3fa3 362 }
d550d095 363 }
b51d3fa3
HL
364 nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
365 MSG_DONTWAIT);
366out:
0597f268
HW
367 inst->qlen = 0;
368 inst->skb = NULL;
0597f268
HW
369}
370
e3567061
MM
371static void
372__nfulnl_flush(struct nfulnl_instance *inst)
373{
374 /* timer holds a reference */
375 if (del_timer(&inst->timer))
376 instance_put(inst);
377 if (inst->skb)
378 __nfulnl_send(inst);
379}
380
c6a8f648
MM
381static void
382nfulnl_timer(unsigned long data)
0597f268 383{
601e68e1 384 struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
0597f268 385
0597f268 386 spin_lock_bh(&inst->lock);
370e6a87
MM
387 if (inst->skb)
388 __nfulnl_send(inst);
0597f268 389 spin_unlock_bh(&inst->lock);
05f7b7b3 390 instance_put(inst);
0597f268
HW
391}
392
0af5f6c1
HW
393/* This is an inline function, we don't really care about a long
394 * list of arguments */
601e68e1 395static inline int
9368a53c
G
396__build_packet_message(struct nfnl_log_net *log,
397 struct nfulnl_instance *inst,
601e68e1 398 const struct sk_buff *skb,
0597f268 399 unsigned int data_len,
76108cea 400 u_int8_t pf,
0597f268
HW
401 unsigned int hooknum,
402 const struct net_device *indev,
403 const struct net_device *outdev,
d7a5c324 404 const char *prefix, unsigned int plen)
0597f268 405{
0597f268
HW
406 struct nfulnl_msg_packet_hdr pmsg;
407 struct nlmsghdr *nlh;
408 struct nfgenmsg *nfmsg;
27a884dc 409 sk_buff_data_t old_tail = inst->skb->tail;
0626af31 410 struct sock *sk;
0c36b48b 411 const unsigned char *hwhdrp;
0597f268 412
d550d095 413 nlh = nlmsg_put(inst->skb, 0, 0,
0597f268 414 NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
d550d095
DM
415 sizeof(struct nfgenmsg), 0);
416 if (!nlh)
417 return -1;
418 nfmsg = nlmsg_data(nlh);
0597f268
HW
419 nfmsg->nfgen_family = pf;
420 nfmsg->version = NFNETLINK_V0;
421 nfmsg->res_id = htons(inst->group_num);
422
e4d091d7 423 memset(&pmsg, 0, sizeof(pmsg));
febf0a43 424 pmsg.hw_protocol = skb->protocol;
0597f268
HW
425 pmsg.hook = hooknum;
426
1db20a52
DM
427 if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
428 goto nla_put_failure;
0597f268 429
1db20a52
DM
430 if (prefix &&
431 nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
432 goto nla_put_failure;
0597f268
HW
433
434 if (indev) {
1109a90c 435#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a52
DM
436 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
437 htonl(indev->ifindex)))
438 goto nla_put_failure;
fbcd923c
HW
439#else
440 if (pf == PF_BRIDGE) {
441 /* Case 1: outdev is physical input device, we need to
442 * look for bridge group (when called from
443 * netfilter_bridge) */
1db20a52
DM
444 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
445 htonl(indev->ifindex)) ||
fbcd923c 446 /* this is the bridge group "brX" */
f350a0a8 447 /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
1db20a52
DM
448 nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
449 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
450 goto nla_put_failure;
fbcd923c 451 } else {
c737b7c4
FW
452 struct net_device *physindev;
453
fbcd923c
HW
454 /* Case 2: indev is bridge group, we need to look for
455 * physical device (when called from ipv4) */
1db20a52
DM
456 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
457 htonl(indev->ifindex)))
458 goto nla_put_failure;
c737b7c4
FW
459
460 physindev = nf_bridge_get_physindev(skb);
461 if (physindev &&
1db20a52 462 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
c737b7c4 463 htonl(physindev->ifindex)))
1db20a52 464 goto nla_put_failure;
fbcd923c
HW
465 }
466#endif
0597f268
HW
467 }
468
469 if (outdev) {
1109a90c 470#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a52
DM
471 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
472 htonl(outdev->ifindex)))
473 goto nla_put_failure;
fbcd923c
HW
474#else
475 if (pf == PF_BRIDGE) {
476 /* Case 1: outdev is physical output device, we need to
477 * look for bridge group (when called from
478 * netfilter_bridge) */
1db20a52
DM
479 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
480 htonl(outdev->ifindex)) ||
fbcd923c 481 /* this is the bridge group "brX" */
f350a0a8 482 /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
1db20a52
DM
483 nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
484 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
485 goto nla_put_failure;
fbcd923c 486 } else {
c737b7c4
FW
487 struct net_device *physoutdev;
488
fbcd923c
HW
489 /* Case 2: indev is a bridge group, we need to look
490 * for physical device (when called from ipv4) */
1db20a52
DM
491 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
492 htonl(outdev->ifindex)))
493 goto nla_put_failure;
c737b7c4
FW
494
495 physoutdev = nf_bridge_get_physoutdev(skb);
496 if (physoutdev &&
1db20a52 497 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
c737b7c4 498 htonl(physoutdev->ifindex)))
1db20a52 499 goto nla_put_failure;
fbcd923c
HW
500 }
501#endif
0597f268
HW
502 }
503
1db20a52
DM
504 if (skb->mark &&
505 nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
506 goto nla_put_failure;
0597f268 507
2c38de4c
NC
508 if (indev && skb->dev &&
509 skb->mac_header != skb->network_header) {
0597f268 510 struct nfulnl_msg_packet_hw phw;
e4d091d7
DC
511 int len;
512
513 memset(&phw, 0, sizeof(phw));
514 len = dev_parse_header(skb, phw.hw_addr);
b95cce35
SH
515 if (len > 0) {
516 phw.hw_addrlen = htons(len);
1db20a52
DM
517 if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
518 goto nla_put_failure;
b95cce35 519 }
0597f268
HW
520 }
521
72961ecf 522 if (indev && skb_mac_header_was_set(skb)) {
2dba62c3 523 if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
1db20a52 524 nla_put_be16(inst->skb, NFULA_HWLEN,
0c36b48b
BH
525 htons(skb->dev->hard_header_len)))
526 goto nla_put_failure;
527
528 hwhdrp = skb_mac_header(skb);
529
530 if (skb->dev->type == ARPHRD_SIT)
531 hwhdrp -= ETH_HLEN;
532
533 if (hwhdrp >= skb->head &&
534 nla_put(inst->skb, NFULA_HWHEADER,
535 skb->dev->hard_header_len, hwhdrp))
1db20a52 536 goto nla_put_failure;
72961ecf
EL
537 }
538
b7aa0bf7 539 if (skb->tstamp.tv64) {
0597f268 540 struct nfulnl_msg_packet_timestamp ts;
b7aa0bf7
ED
541 struct timeval tv = ktime_to_timeval(skb->tstamp);
542 ts.sec = cpu_to_be64(tv.tv_sec);
543 ts.usec = cpu_to_be64(tv.tv_usec);
0597f268 544
1db20a52
DM
545 if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
546 goto nla_put_failure;
0597f268
HW
547 }
548
549 /* UID */
0626af31 550 sk = skb->sk;
a8399231 551 if (sk && sk_fullsock(sk)) {
0626af31
ED
552 read_lock_bh(&sk->sk_callback_lock);
553 if (sk->sk_socket && sk->sk_socket->file) {
554 struct file *file = sk->sk_socket->file;
437589a7
LT
555 const struct cred *cred = file->f_cred;
556 struct user_namespace *user_ns = inst->peer_user_ns;
557 __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
558 __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
0626af31 559 read_unlock_bh(&sk->sk_callback_lock);
1db20a52
DM
560 if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
561 nla_put_be32(inst->skb, NFULA_GID, gid))
562 goto nla_put_failure;
0597f268 563 } else
0626af31 564 read_unlock_bh(&sk->sk_callback_lock);
0597f268
HW
565 }
566
0af5f6c1 567 /* local sequence number */
1db20a52
DM
568 if ((inst->flags & NFULNL_CFG_F_SEQ) &&
569 nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
570 goto nla_put_failure;
0dfedd28 571
0af5f6c1 572 /* global sequence number */
1db20a52
DM
573 if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
574 nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
9368a53c 575 htonl(atomic_inc_return(&log->global_seq))))
1db20a52 576 goto nla_put_failure;
0af5f6c1 577
0597f268 578 if (data_len) {
df6fb868
PM
579 struct nlattr *nla;
580 int size = nla_attr_size(data_len);
0597f268 581
82251615
PNA
582 if (skb_tailroom(inst->skb) < nla_total_size(data_len))
583 goto nla_put_failure;
0597f268 584
df6fb868
PM
585 nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
586 nla->nla_type = NFULA_PAYLOAD;
587 nla->nla_len = size;
0597f268 588
df6fb868 589 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
0597f268
HW
590 BUG();
591 }
601e68e1 592
0597f268
HW
593 nlh->nlmsg_len = inst->skb->tail - old_tail;
594 return 0;
595
df6fb868 596nla_put_failure:
0597f268
HW
597 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
598 return -1;
599}
600
0597f268
HW
601static struct nf_loginfo default_loginfo = {
602 .type = NF_LOG_TYPE_ULOG,
603 .u = {
604 .ulog = {
605 .copy_len = 0xffff,
606 .group = 0,
607 .qthreshold = 1,
608 },
609 },
610};
611
612/* log handler for internal netfilter logging api */
5f7340ef 613void
8cdb46da
HS
614nfulnl_log_packet(struct net *net,
615 u_int8_t pf,
0597f268
HW
616 unsigned int hooknum,
617 const struct sk_buff *skb,
618 const struct net_device *in,
619 const struct net_device *out,
620 const struct nf_loginfo *li_user,
621 const char *prefix)
622{
623 unsigned int size, data_len;
624 struct nfulnl_instance *inst;
625 const struct nf_loginfo *li;
626 unsigned int qthreshold;
d7a5c324 627 unsigned int plen;
9368a53c 628 struct nfnl_log_net *log = nfnl_log_pernet(net);
0597f268 629
601e68e1 630 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
0597f268
HW
631 li = li_user;
632 else
633 li = &default_loginfo;
634
9368a53c 635 inst = instance_lookup_get(log, li->u.ulog.group);
0597f268 636 if (!inst)
0597f268 637 return;
0597f268 638
d7a5c324
PM
639 plen = 0;
640 if (prefix)
881dbfe8 641 plen = strlen(prefix) + 1;
d7a5c324 642
0597f268
HW
643 /* FIXME: do we want to make the size calculation conditional based on
644 * what is actually present? way more branches and checks, but more
645 * memory efficient... */
573ce260 646 size = nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868
PM
647 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
648 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
649 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
1109a90c 650#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
df6fb868
PM
651 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
652 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 653#endif
df6fb868
PM
654 + nla_total_size(sizeof(u_int32_t)) /* mark */
655 + nla_total_size(sizeof(u_int32_t)) /* uid */
76aa1ce1 656 + nla_total_size(sizeof(u_int32_t)) /* gid */
df6fb868
PM
657 + nla_total_size(plen) /* prefix */
658 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
9dfa1dfe
FW
659 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
660 + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
0597f268 661
eeff9bee
PNA
662 if (in && skb_mac_header_was_set(skb)) {
663 size += nla_total_size(skb->dev->hard_header_len)
664 + nla_total_size(sizeof(u_int16_t)) /* hwtype */
665 + nla_total_size(sizeof(u_int16_t)); /* hwlen */
666 }
667
0597f268
HW
668 spin_lock_bh(&inst->lock);
669
0af5f6c1 670 if (inst->flags & NFULNL_CFG_F_SEQ)
df6fb868 671 size += nla_total_size(sizeof(u_int32_t));
0af5f6c1 672 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
df6fb868 673 size += nla_total_size(sizeof(u_int32_t));
0af5f6c1 674
0597f268
HW
675 qthreshold = inst->qthreshold;
676 /* per-rule qthreshold overrides per-instance */
5ca431f9
EL
677 if (li->u.ulog.qthreshold)
678 if (qthreshold > li->u.ulog.qthreshold)
679 qthreshold = li->u.ulog.qthreshold;
680
601e68e1 681
0597f268
HW
682 switch (inst->copy_mode) {
683 case NFULNL_COPY_META:
684 case NFULNL_COPY_NONE:
685 data_len = 0;
686 break;
601e68e1 687
0597f268 688 case NFULNL_COPY_PACKET:
c1e7dc91 689 if (inst->copy_range > skb->len)
0597f268
HW
690 data_len = skb->len;
691 else
692 data_len = inst->copy_range;
601e68e1 693
df6fb868 694 size += nla_total_size(data_len);
0597f268 695 break;
601e68e1 696
f5c5440d 697 case NFULNL_COPY_DISABLED:
0597f268 698 default:
55b5a91e 699 goto unlock_and_release;
0597f268
HW
700 }
701
9dfa1dfe 702 if (inst->skb && size > skb_tailroom(inst->skb)) {
0597f268
HW
703 /* either the queue len is too high or we don't have
704 * enough room in the skb left. flush to userspace. */
e3567061 705 __nfulnl_flush(inst);
55b5a91e 706 }
0597f268 707
55b5a91e 708 if (!inst->skb) {
afff14f6
G
709 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
710 inst->nlbufsiz, size);
55b5a91e 711 if (!inst->skb)
0597f268 712 goto alloc_failure;
0597f268
HW
713 }
714
0597f268
HW
715 inst->qlen++;
716
9368a53c 717 __build_packet_message(log, inst, skb, data_len, pf,
8248779b 718 hooknum, in, out, prefix, plen);
0597f268 719
d63b043d
MM
720 if (inst->qlen >= qthreshold)
721 __nfulnl_flush(inst);
0597f268
HW
722 /* timer_pending always called within inst->lock, so there
723 * is no chance of a race here */
d63b043d 724 else if (!timer_pending(&inst->timer)) {
0597f268
HW
725 instance_get(inst);
726 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
727 add_timer(&inst->timer);
728 }
0597f268 729
ed32abea
MM
730unlock_and_release:
731 spin_unlock_bh(&inst->lock);
732 instance_put(inst);
0597f268
HW
733 return;
734
735alloc_failure:
0597f268 736 /* FIXME: statistics */
ed32abea 737 goto unlock_and_release;
0597f268 738}
5f7340ef 739EXPORT_SYMBOL_GPL(nfulnl_log_packet);
0597f268
HW
740
741static int
742nfulnl_rcv_nl_event(struct notifier_block *this,
743 unsigned long event, void *ptr)
744{
745 struct netlink_notify *n = ptr;
9368a53c 746 struct nfnl_log_net *log = nfnl_log_pernet(n->net);
0597f268 747
dee5817e 748 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
0597f268
HW
749 int i;
750
15e47304 751 /* destroy all instances for this portid */
9368a53c 752 spin_lock_bh(&log->instances_lock);
0597f268 753 for (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d 754 struct hlist_node *t2;
0597f268 755 struct nfulnl_instance *inst;
9368a53c 756 struct hlist_head *head = &log->instance_table[i];
0597f268 757
b67bfe0d 758 hlist_for_each_entry_safe(inst, t2, head, hlist) {
9368a53c 759 if (n->portid == inst->peer_portid)
0597f268
HW
760 __instance_destroy(inst);
761 }
762 }
9368a53c 763 spin_unlock_bh(&log->instances_lock);
0597f268
HW
764 }
765 return NOTIFY_DONE;
766}
767
768static struct notifier_block nfulnl_rtnl_notifier = {
769 .notifier_call = nfulnl_rcv_nl_event,
770};
771
772static int
773nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
774 const struct nlmsghdr *nlh,
775 const struct nlattr * const nfqa[])
0597f268
HW
776{
777 return -ENOTSUPP;
778}
779
ca735b3a 780static struct nf_logger nfulnl_logger __read_mostly = {
0597f268 781 .name = "nfnetlink_log",
5962815a 782 .type = NF_LOG_TYPE_ULOG,
0597f268
HW
783 .logfn = &nfulnl_log_packet,
784 .me = THIS_MODULE,
785};
786
fd8281ad
PM
787static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
788 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
789 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
790 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
791 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
792 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
793 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
0597f268
HW
794};
795
796static int
797nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
798 const struct nlmsghdr *nlh,
799 const struct nlattr * const nfula[])
0597f268 800{
d550d095 801 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
0597f268
HW
802 u_int16_t group_num = ntohs(nfmsg->res_id);
803 struct nfulnl_instance *inst;
b7047a1c 804 struct nfulnl_msg_config_cmd *cmd = NULL;
30e0c6a6 805 struct net *net = sock_net(ctnl);
9368a53c 806 struct nfnl_log_net *log = nfnl_log_pernet(net);
0597f268
HW
807 int ret = 0;
808
b7047a1c
PM
809 if (nfula[NFULA_CFG_CMD]) {
810 u_int8_t pf = nfmsg->nfgen_family;
811 cmd = nla_data(nfula[NFULA_CFG_CMD]);
812
813 /* Commands without queue context */
814 switch (cmd->command) {
815 case NFULNL_CFG_CMD_PF_BIND:
30e0c6a6 816 return nf_log_bind_pf(net, pf, &nfulnl_logger);
b7047a1c 817 case NFULNL_CFG_CMD_PF_UNBIND:
30e0c6a6 818 nf_log_unbind_pf(net, pf);
b7047a1c
PM
819 return 0;
820 }
821 }
822
9368a53c 823 inst = instance_lookup_get(log, group_num);
15e47304 824 if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
c0506365
PM
825 ret = -EPERM;
826 goto out_put;
827 }
828
b7047a1c 829 if (cmd != NULL) {
0597f268
HW
830 switch (cmd->command) {
831 case NFULNL_CFG_CMD_BIND:
832 if (inst) {
833 ret = -EBUSY;
834 goto out_put;
835 }
836
9368a53c 837 inst = instance_create(net, group_num,
15e47304 838 NETLINK_CB(skb).portid,
e32123e5 839 sk_user_ns(NETLINK_CB(skb).sk));
baab2ce7
PM
840 if (IS_ERR(inst)) {
841 ret = PTR_ERR(inst);
f414c16c 842 goto out;
0597f268
HW
843 }
844 break;
845 case NFULNL_CFG_CMD_UNBIND:
846 if (!inst) {
847 ret = -ENODEV;
f414c16c 848 goto out;
0597f268
HW
849 }
850
9368a53c 851 instance_destroy(log, inst);
a49c6503 852 goto out_put;
0597f268 853 default:
cd21f0ac 854 ret = -ENOTSUPP;
0597f268
HW
855 break;
856 }
0597f268
HW
857 }
858
df6fb868 859 if (nfula[NFULA_CFG_MODE]) {
0597f268 860 struct nfulnl_msg_config_mode *params;
df6fb868 861 params = nla_data(nfula[NFULA_CFG_MODE]);
0597f268 862
c0506365
PM
863 if (!inst) {
864 ret = -ENODEV;
865 goto out;
866 }
0597f268 867 nfulnl_set_mode(inst, params->copy_mode,
d1208b99 868 ntohl(params->copy_range));
0597f268
HW
869 }
870
df6fb868 871 if (nfula[NFULA_CFG_TIMEOUT]) {
0dfedd28 872 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
0597f268 873
c0506365
PM
874 if (!inst) {
875 ret = -ENODEV;
876 goto out;
877 }
0597f268
HW
878 nfulnl_set_timeout(inst, ntohl(timeout));
879 }
880
df6fb868 881 if (nfula[NFULA_CFG_NLBUFSIZ]) {
0dfedd28 882 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
0597f268 883
c0506365
PM
884 if (!inst) {
885 ret = -ENODEV;
886 goto out;
887 }
0597f268
HW
888 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
889 }
890
df6fb868 891 if (nfula[NFULA_CFG_QTHRESH]) {
0dfedd28 892 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
0597f268 893
c0506365
PM
894 if (!inst) {
895 ret = -ENODEV;
896 goto out;
897 }
0597f268
HW
898 nfulnl_set_qthresh(inst, ntohl(qthresh));
899 }
900
df6fb868 901 if (nfula[NFULA_CFG_FLAGS]) {
0dfedd28 902 __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
c0506365
PM
903
904 if (!inst) {
905 ret = -ENODEV;
906 goto out;
907 }
ee433530 908 nfulnl_set_flags(inst, ntohs(flags));
0af5f6c1
HW
909 }
910
0597f268
HW
911out_put:
912 instance_put(inst);
dd16704e 913out:
0597f268
HW
914 return ret;
915}
916
7c8d4cb4 917static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
0597f268 918 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
37d2e7a2 919 .attr_count = NFULA_MAX, },
0597f268 920 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
fd8281ad
PM
921 .attr_count = NFULA_CFG_MAX,
922 .policy = nfula_cfg_policy },
0597f268
HW
923};
924
7c8d4cb4 925static const struct nfnetlink_subsystem nfulnl_subsys = {
0597f268
HW
926 .name = "log",
927 .subsys_id = NFNL_SUBSYS_ULOG,
928 .cb_count = NFULNL_MSG_MAX,
0597f268
HW
929 .cb = nfulnl_cb,
930};
931
932#ifdef CONFIG_PROC_FS
933struct iter_state {
9368a53c 934 struct seq_net_private p;
0597f268
HW
935 unsigned int bucket;
936};
937
9368a53c 938static struct hlist_node *get_first(struct net *net, struct iter_state *st)
0597f268 939{
9368a53c 940 struct nfnl_log_net *log;
0597f268
HW
941 if (!st)
942 return NULL;
943
9368a53c
G
944 log = nfnl_log_pernet(net);
945
0597f268 946 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
9368a53c
G
947 struct hlist_head *head = &log->instance_table[st->bucket];
948
949 if (!hlist_empty(head))
950 return rcu_dereference_bh(hlist_first_rcu(head));
0597f268
HW
951 }
952 return NULL;
953}
954
9368a53c
G
955static struct hlist_node *get_next(struct net *net, struct iter_state *st,
956 struct hlist_node *h)
0597f268 957{
0e60ebe0 958 h = rcu_dereference_bh(hlist_next_rcu(h));
0597f268 959 while (!h) {
9368a53c
G
960 struct nfnl_log_net *log;
961 struct hlist_head *head;
962
0597f268
HW
963 if (++st->bucket >= INSTANCE_BUCKETS)
964 return NULL;
965
9368a53c
G
966 log = nfnl_log_pernet(net);
967 head = &log->instance_table[st->bucket];
968 h = rcu_dereference_bh(hlist_first_rcu(head));
0597f268
HW
969 }
970 return h;
971}
972
9368a53c
G
973static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
974 loff_t pos)
0597f268
HW
975{
976 struct hlist_node *head;
9368a53c 977 head = get_first(net, st);
0597f268
HW
978
979 if (head)
9368a53c 980 while (pos && (head = get_next(net, st, head)))
0597f268
HW
981 pos--;
982 return pos ? NULL : head;
983}
984
9368a53c 985static void *seq_start(struct seq_file *s, loff_t *pos)
bed1be20 986 __acquires(rcu_bh)
0597f268 987{
bed1be20 988 rcu_read_lock_bh();
9368a53c 989 return get_idx(seq_file_net(s), s->private, *pos);
0597f268
HW
990}
991
992static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
993{
994 (*pos)++;
9368a53c 995 return get_next(seq_file_net(s), s->private, v);
0597f268
HW
996}
997
998static void seq_stop(struct seq_file *s, void *v)
bed1be20 999 __releases(rcu_bh)
0597f268 1000{
bed1be20 1001 rcu_read_unlock_bh();
0597f268
HW
1002}
1003
1004static int seq_show(struct seq_file *s, void *v)
1005{
1006 const struct nfulnl_instance *inst = v;
1007
20a1d165 1008 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1ca9e417
JP
1009 inst->group_num,
1010 inst->peer_portid, inst->qlen,
1011 inst->copy_mode, inst->copy_range,
1012 inst->flushtimeout, atomic_read(&inst->use));
1013
1014 return 0;
0597f268
HW
1015}
1016
56b3d975 1017static const struct seq_operations nful_seq_ops = {
0597f268
HW
1018 .start = seq_start,
1019 .next = seq_next,
1020 .stop = seq_stop,
1021 .show = seq_show,
1022};
1023
1024static int nful_open(struct inode *inode, struct file *file)
1025{
9368a53c
G
1026 return seq_open_net(inode, file, &nful_seq_ops,
1027 sizeof(struct iter_state));
0597f268
HW
1028}
1029
da7071d7 1030static const struct file_operations nful_file_ops = {
0597f268
HW
1031 .owner = THIS_MODULE,
1032 .open = nful_open,
1033 .read = seq_read,
1034 .llseek = seq_lseek,
9368a53c 1035 .release = seq_release_net,
0597f268
HW
1036};
1037
1038#endif /* PROC_FS */
1039
9368a53c 1040static int __net_init nfnl_log_net_init(struct net *net)
0597f268 1041{
9368a53c
G
1042 unsigned int i;
1043 struct nfnl_log_net *log = nfnl_log_pernet(net);
601e68e1 1044
0597f268 1045 for (i = 0; i < INSTANCE_BUCKETS; i++)
9368a53c
G
1046 INIT_HLIST_HEAD(&log->instance_table[i]);
1047 spin_lock_init(&log->instances_lock);
1048
1049#ifdef CONFIG_PROC_FS
1050 if (!proc_create("nfnetlink_log", 0440,
1051 net->nf.proc_netfilter, &nful_file_ops))
1052 return -ENOMEM;
1053#endif
1054 return 0;
1055}
1056
1057static void __net_exit nfnl_log_net_exit(struct net *net)
1058{
e778f56e 1059#ifdef CONFIG_PROC_FS
9368a53c 1060 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
e778f56e 1061#endif
45c2aff6 1062 nf_log_unset(net, &nfulnl_logger);
9368a53c
G
1063}
1064
1065static struct pernet_operations nfnl_log_net_ops = {
1066 .init = nfnl_log_net_init,
1067 .exit = nfnl_log_net_exit,
1068 .id = &nfnl_log_net_id,
1069 .size = sizeof(struct nfnl_log_net),
1070};
1071
1072static int __init nfnetlink_log_init(void)
1073{
3bfe0498
FR
1074 int status;
1075
1076 status = register_pernet_subsys(&nfnl_log_net_ops);
1077 if (status < 0) {
1078 pr_err("failed to register pernet ops\n");
1079 goto out;
1080 }
601e68e1 1081
0597f268
HW
1082 netlink_register_notifier(&nfulnl_rtnl_notifier);
1083 status = nfnetlink_subsys_register(&nfulnl_subsys);
1084 if (status < 0) {
beacd3e8 1085 pr_err("failed to create netlink socket\n");
0597f268
HW
1086 goto cleanup_netlink_notifier;
1087 }
1088
ca735b3a
EL
1089 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1090 if (status < 0) {
beacd3e8 1091 pr_err("failed to register logger\n");
ca735b3a
EL
1092 goto cleanup_subsys;
1093 }
1094
0597f268
HW
1095 return status;
1096
0597f268 1097cleanup_subsys:
0597f268
HW
1098 nfnetlink_subsys_unregister(&nfulnl_subsys);
1099cleanup_netlink_notifier:
1100 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe0498
FR
1101 unregister_pernet_subsys(&nfnl_log_net_ops);
1102out:
0597f268
HW
1103 return status;
1104}
1105
65b4b4e8 1106static void __exit nfnetlink_log_fini(void)
0597f268 1107{
e92ad99c 1108 nf_log_unregister(&nfulnl_logger);
32292a7f
PM
1109 nfnetlink_subsys_unregister(&nfulnl_subsys);
1110 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe0498 1111 unregister_pernet_subsys(&nfnl_log_net_ops);
0597f268
HW
1112}
1113
1114MODULE_DESCRIPTION("netfilter userspace logging");
1115MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1116MODULE_LICENSE("GPL");
f682faef 1117MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
fab4085f
PNA
1118MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1119MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1120MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
0597f268 1121
65b4b4e8
AM
1122module_init(nfnetlink_log_init);
1123module_exit(nfnetlink_log_fini);
This page took 0.938498 seconds and 5 git commands to generate.