1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/if_arp.h>
18#include <linux/init.h>
19#include <linux/ip.h>
20#include <linux/ipv6.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
23#include <linux/netfilter_bridge.h>
24#include <net/netlink.h>
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/nfnetlink_log.h>
27#include <linux/netfilter/nf_conntrack_common.h>
28#include <linux/spinlock.h>
29#include <linux/sysctl.h>
30#include <linux/proc_fs.h>
31#include <linux/security.h>
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <net/sock.h>
35#include <net/netfilter/nf_log.h>
36#include <net/netns/generic.h>
37
38#include <linux/atomic.h>
39#include <linux/refcount.h>
40
41
42#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
43#include "../bridge/br_private.h"
44#endif
45
46#if IS_ENABLED(CONFIG_NF_CONNTRACK)
47#include <net/netfilter/nf_conntrack.h>
48#endif
49
50#define NFULNL_COPY_DISABLED 0xff
51#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
52#define NFULNL_TIMEOUT_DEFAULT 100
53#define NFULNL_QTHRESH_DEFAULT 100
54
55#define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
56
57#define PRINTR(x, args...) do { if (net_ratelimit()) \
58 printk(x, ## args); } while (0);
59
60struct nfulnl_instance {
61 struct hlist_node hlist;
62 spinlock_t lock;
63 refcount_t use;
64
65 unsigned int qlen;
66 struct sk_buff *skb;
67 struct timer_list timer;
68 struct net *net;
69 struct user_namespace *peer_user_ns;
70 u32 peer_portid;
71
72
73 unsigned int flushtimeout;
74 unsigned int nlbufsiz;
75 unsigned int qthreshold;
76 u_int32_t copy_range;
77 u_int32_t seq;
78 u_int16_t group_num;
79 u_int16_t flags;
80 u_int8_t copy_mode;
81 struct rcu_head rcu;
82};
83
84#define INSTANCE_BUCKETS 16
85
86static unsigned int nfnl_log_net_id __read_mostly;
87
88struct nfnl_log_net {
89 spinlock_t instances_lock;
90 struct hlist_head instance_table[INSTANCE_BUCKETS];
91 atomic_t global_seq;
92};
93
94static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
95{
96 return net_generic(net, nfnl_log_net_id);
97}
98
99static inline u_int8_t instance_hashfn(u_int16_t group_num)
100{
101 return ((group_num & 0xff) % INSTANCE_BUCKETS);
102}
103
104static struct nfulnl_instance *
105__instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
106{
107 struct hlist_head *head;
108 struct nfulnl_instance *inst;
109
110 head = &log->instance_table[instance_hashfn(group_num)];
111 hlist_for_each_entry_rcu(inst, head, hlist) {
112 if (inst->group_num == group_num)
113 return inst;
114 }
115 return NULL;
116}
117
118static inline void
119instance_get(struct nfulnl_instance *inst)
120{
121 refcount_inc(&inst->use);
122}
123
124static struct nfulnl_instance *
125instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
126{
127 struct nfulnl_instance *inst;
128
129 rcu_read_lock_bh();
130 inst = __instance_lookup(log, group_num);
131 if (inst && !refcount_inc_not_zero(&inst->use))
132 inst = NULL;
133 rcu_read_unlock_bh();
134
135 return inst;
136}
137
138static void nfulnl_instance_free_rcu(struct rcu_head *head)
139{
140 struct nfulnl_instance *inst =
141 container_of(head, struct nfulnl_instance, rcu);
142
143 put_net(inst->net);
144 kfree(inst);
145 module_put(THIS_MODULE);
146}
147
148static void
149instance_put(struct nfulnl_instance *inst)
150{
151 if (inst && refcount_dec_and_test(&inst->use))
152 call_rcu(&inst->rcu, nfulnl_instance_free_rcu);
153}
154
155static void nfulnl_timer(struct timer_list *t);
156
157static struct nfulnl_instance *
158instance_create(struct net *net, u_int16_t group_num,
159 u32 portid, struct user_namespace *user_ns)
160{
161 struct nfulnl_instance *inst;
162 struct nfnl_log_net *log = nfnl_log_pernet(net);
163 int err;
164
165 spin_lock_bh(&log->instances_lock);
166 if (__instance_lookup(log, group_num)) {
167 err = -EEXIST;
168 goto out_unlock;
169 }
170
171 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
172 if (!inst) {
173 err = -ENOMEM;
174 goto out_unlock;
175 }
176
177 if (!try_module_get(THIS_MODULE)) {
178 kfree(inst);
179 err = -EAGAIN;
180 goto out_unlock;
181 }
182
183 INIT_HLIST_NODE(&inst->hlist);
184 spin_lock_init(&inst->lock);
185
186 refcount_set(&inst->use, 2);
187
188 timer_setup(&inst->timer, nfulnl_timer, 0);
189
190 inst->net = get_net(net);
191 inst->peer_user_ns = user_ns;
192 inst->peer_portid = portid;
193 inst->group_num = group_num;
194
195 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
196 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
197 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
198 inst->copy_mode = NFULNL_COPY_PACKET;
199 inst->copy_range = NFULNL_COPY_RANGE_MAX;
200
201 hlist_add_head_rcu(&inst->hlist,
202 &log->instance_table[instance_hashfn(group_num)]);
203
204
205 spin_unlock_bh(&log->instances_lock);
206
207 return inst;
208
209out_unlock:
210 spin_unlock_bh(&log->instances_lock);
211 return ERR_PTR(err);
212}
213
214static void __nfulnl_flush(struct nfulnl_instance *inst);
215
216
217static void
218__instance_destroy(struct nfulnl_instance *inst)
219{
220
221 hlist_del_rcu(&inst->hlist);
222
223
224
225 spin_lock(&inst->lock);
226
227
228 inst->copy_mode = NFULNL_COPY_DISABLED;
229
230 if (inst->skb)
231 __nfulnl_flush(inst);
232 spin_unlock(&inst->lock);
233
234
235 instance_put(inst);
236}
237
238static inline void
239instance_destroy(struct nfnl_log_net *log,
240 struct nfulnl_instance *inst)
241{
242 spin_lock_bh(&log->instances_lock);
243 __instance_destroy(inst);
244 spin_unlock_bh(&log->instances_lock);
245}
246
247static int
248nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
249 unsigned int range)
250{
251 int status = 0;
252
253 spin_lock_bh(&inst->lock);
254
255 switch (mode) {
256 case NFULNL_COPY_NONE:
257 case NFULNL_COPY_META:
258 inst->copy_mode = mode;
259 inst->copy_range = 0;
260 break;
261
262 case NFULNL_COPY_PACKET:
263 inst->copy_mode = mode;
264 if (range == 0)
265 range = NFULNL_COPY_RANGE_MAX;
266 inst->copy_range = min_t(unsigned int,
267 range, NFULNL_COPY_RANGE_MAX);
268 break;
269
270 default:
271 status = -EINVAL;
272 break;
273 }
274
275 spin_unlock_bh(&inst->lock);
276
277 return status;
278}
279
280static int
281nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
282{
283 int status;
284
285 spin_lock_bh(&inst->lock);
286 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
287 status = -ERANGE;
288 else if (nlbufsiz > 131072)
289 status = -ERANGE;
290 else {
291 inst->nlbufsiz = nlbufsiz;
292 status = 0;
293 }
294 spin_unlock_bh(&inst->lock);
295
296 return status;
297}
298
299static void
300nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
301{
302 spin_lock_bh(&inst->lock);
303 inst->flushtimeout = timeout;
304 spin_unlock_bh(&inst->lock);
305}
306
307static void
308nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
309{
310 spin_lock_bh(&inst->lock);
311 inst->qthreshold = qthresh;
312 spin_unlock_bh(&inst->lock);
313}
314
315static int
316nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
317{
318 spin_lock_bh(&inst->lock);
319 inst->flags = flags;
320 spin_unlock_bh(&inst->lock);
321
322 return 0;
323}
324
325static struct sk_buff *
326nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
327 unsigned int pkt_size)
328{
329 struct sk_buff *skb;
330 unsigned int n;
331
332
333
334
335 n = max(inst_size, pkt_size);
336 skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
337 if (!skb) {
338 if (n > pkt_size) {
339
340
341
342 skb = alloc_skb(pkt_size, GFP_ATOMIC);
343 }
344 }
345
346 return skb;
347}
348
349static void
350__nfulnl_send(struct nfulnl_instance *inst)
351{
352 if (inst->qlen > 1) {
353 struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
354 NLMSG_DONE,
355 sizeof(struct nfgenmsg),
356 0);
357 if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
358 inst->skb->len, skb_tailroom(inst->skb))) {
359 kfree_skb(inst->skb);
360 goto out;
361 }
362 }
363 nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid);
364out:
365 inst->qlen = 0;
366 inst->skb = NULL;
367}
368
369static void
370__nfulnl_flush(struct nfulnl_instance *inst)
371{
372
373 if (del_timer(&inst->timer))
374 instance_put(inst);
375 if (inst->skb)
376 __nfulnl_send(inst);
377}
378
379static void
380nfulnl_timer(struct timer_list *t)
381{
382 struct nfulnl_instance *inst = from_timer(inst, t, timer);
383
384 spin_lock_bh(&inst->lock);
385 if (inst->skb)
386 __nfulnl_send(inst);
387 spin_unlock_bh(&inst->lock);
388 instance_put(inst);
389}
390
391static u32 nfulnl_get_bridge_size(const struct sk_buff *skb)
392{
393 u32 size = 0;
394
395 if (!skb_mac_header_was_set(skb))
396 return 0;
397
398 if (skb_vlan_tag_present(skb)) {
399 size += nla_total_size(0);
400 size += nla_total_size(sizeof(u16));
401 size += nla_total_size(sizeof(u16));
402 }
403
404 if (skb->network_header > skb->mac_header)
405 size += nla_total_size(skb->network_header - skb->mac_header);
406
407 return size;
408}
409
410static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff *skb)
411{
412 if (!skb_mac_header_was_set(skb))
413 return 0;
414
415 if (skb_vlan_tag_present(skb)) {
416 struct nlattr *nest;
417
418 nest = nla_nest_start(inst->skb, NFULA_VLAN);
419 if (!nest)
420 goto nla_put_failure;
421
422 if (nla_put_be16(inst->skb, NFULA_VLAN_TCI, htons(skb->vlan_tci)) ||
423 nla_put_be16(inst->skb, NFULA_VLAN_PROTO, skb->vlan_proto))
424 goto nla_put_failure;
425
426 nla_nest_end(inst->skb, nest);
427 }
428
429 if (skb->mac_header < skb->network_header) {
430 int len = (int)(skb->network_header - skb->mac_header);
431
432 if (nla_put(inst->skb, NFULA_L2HDR, len, skb_mac_header(skb)))
433 goto nla_put_failure;
434 }
435
436 return 0;
437
438nla_put_failure:
439 return -1;
440}
441
442
443
444static inline int
445__build_packet_message(struct nfnl_log_net *log,
446 struct nfulnl_instance *inst,
447 const struct sk_buff *skb,
448 unsigned int data_len,
449 u_int8_t pf,
450 unsigned int hooknum,
451 const struct net_device *indev,
452 const struct net_device *outdev,
453 const char *prefix, unsigned int plen,
454 const struct nfnl_ct_hook *nfnl_ct,
455 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
456{
457 struct nfulnl_msg_packet_hdr pmsg;
458 struct nlmsghdr *nlh;
459 sk_buff_data_t old_tail = inst->skb->tail;
460 struct sock *sk;
461 const unsigned char *hwhdrp;
462
463 nlh = nfnl_msg_put(inst->skb, 0, 0,
464 nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
465 0, pf, NFNETLINK_V0, htons(inst->group_num));
466 if (!nlh)
467 return -1;
468
469 memset(&pmsg, 0, sizeof(pmsg));
470 pmsg.hw_protocol = skb->protocol;
471 pmsg.hook = hooknum;
472
473 if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
474 goto nla_put_failure;
475
476 if (prefix &&
477 nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
478 goto nla_put_failure;
479
480 if (indev) {
481#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
482 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
483 htonl(indev->ifindex)))
484 goto nla_put_failure;
485#else
486 if (pf == PF_BRIDGE) {
487
488
489
490 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
491 htonl(indev->ifindex)) ||
492
493
494
495
496 nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
497 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
498 goto nla_put_failure;
499 } else {
500 struct net_device *physindev;
501
502
503
504 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
505 htonl(indev->ifindex)))
506 goto nla_put_failure;
507
508 physindev = nf_bridge_get_physindev(skb);
509 if (physindev &&
510 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
511 htonl(physindev->ifindex)))
512 goto nla_put_failure;
513 }
514#endif
515 }
516
517 if (outdev) {
518#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
519 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
520 htonl(outdev->ifindex)))
521 goto nla_put_failure;
522#else
523 if (pf == PF_BRIDGE) {
524
525
526
527 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
528 htonl(outdev->ifindex)) ||
529
530
531
532
533 nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
534 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
535 goto nla_put_failure;
536 } else {
537 struct net_device *physoutdev;
538
539
540
541 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
542 htonl(outdev->ifindex)))
543 goto nla_put_failure;
544
545 physoutdev = nf_bridge_get_physoutdev(skb);
546 if (physoutdev &&
547 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
548 htonl(physoutdev->ifindex)))
549 goto nla_put_failure;
550 }
551#endif
552 }
553
554 if (skb->mark &&
555 nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
556 goto nla_put_failure;
557
558 if (indev && skb->dev &&
559 skb_mac_header_was_set(skb) &&
560 skb_mac_header_len(skb) != 0) {
561 struct nfulnl_msg_packet_hw phw;
562 int len;
563
564 memset(&phw, 0, sizeof(phw));
565 len = dev_parse_header(skb, phw.hw_addr);
566 if (len > 0) {
567 phw.hw_addrlen = htons(len);
568 if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
569 goto nla_put_failure;
570 }
571 }
572
573 if (indev && skb_mac_header_was_set(skb)) {
574 if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
575 nla_put_be16(inst->skb, NFULA_HWLEN,
576 htons(skb->dev->hard_header_len)))
577 goto nla_put_failure;
578
579 hwhdrp = skb_mac_header(skb);
580
581 if (skb->dev->type == ARPHRD_SIT)
582 hwhdrp -= ETH_HLEN;
583
584 if (hwhdrp >= skb->head &&
585 nla_put(inst->skb, NFULA_HWHEADER,
586 skb->dev->hard_header_len, hwhdrp))
587 goto nla_put_failure;
588 }
589
590 if (hooknum <= NF_INET_FORWARD && skb->tstamp) {
591 struct nfulnl_msg_packet_timestamp ts;
592 struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
593 ts.sec = cpu_to_be64(kts.tv_sec);
594 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
595
596 if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
597 goto nla_put_failure;
598 }
599
600
601 sk = skb->sk;
602 if (sk && sk_fullsock(sk)) {
603 read_lock_bh(&sk->sk_callback_lock);
604 if (sk->sk_socket && sk->sk_socket->file) {
605 struct file *file = sk->sk_socket->file;
606 const struct cred *cred = file->f_cred;
607 struct user_namespace *user_ns = inst->peer_user_ns;
608 __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
609 __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
610 read_unlock_bh(&sk->sk_callback_lock);
611 if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
612 nla_put_be32(inst->skb, NFULA_GID, gid))
613 goto nla_put_failure;
614 } else
615 read_unlock_bh(&sk->sk_callback_lock);
616 }
617
618
619 if ((inst->flags & NFULNL_CFG_F_SEQ) &&
620 nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
621 goto nla_put_failure;
622
623
624 if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
625 nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
626 htonl(atomic_inc_return(&log->global_seq))))
627 goto nla_put_failure;
628
629 if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
630 NFULA_CT, NFULA_CT_INFO) < 0)
631 goto nla_put_failure;
632
633 if ((pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE) &&
634 nfulnl_put_bridge(inst, skb) < 0)
635 goto nla_put_failure;
636
637 if (data_len) {
638 struct nlattr *nla;
639 int size = nla_attr_size(data_len);
640
641 if (skb_tailroom(inst->skb) < nla_total_size(data_len))
642 goto nla_put_failure;
643
644 nla = skb_put(inst->skb, nla_total_size(data_len));
645 nla->nla_type = NFULA_PAYLOAD;
646 nla->nla_len = size;
647
648 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
649 BUG();
650 }
651
652 nlh->nlmsg_len = inst->skb->tail - old_tail;
653 return 0;
654
655nla_put_failure:
656 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
657 return -1;
658}
659
660static const struct nf_loginfo default_loginfo = {
661 .type = NF_LOG_TYPE_ULOG,
662 .u = {
663 .ulog = {
664 .copy_len = 0xffff,
665 .group = 0,
666 .qthreshold = 1,
667 },
668 },
669};
670
671
672static void
673nfulnl_log_packet(struct net *net,
674 u_int8_t pf,
675 unsigned int hooknum,
676 const struct sk_buff *skb,
677 const struct net_device *in,
678 const struct net_device *out,
679 const struct nf_loginfo *li_user,
680 const char *prefix)
681{
682 size_t size;
683 unsigned int data_len;
684 struct nfulnl_instance *inst;
685 const struct nf_loginfo *li;
686 unsigned int qthreshold;
687 unsigned int plen = 0;
688 struct nfnl_log_net *log = nfnl_log_pernet(net);
689 const struct nfnl_ct_hook *nfnl_ct = NULL;
690 struct nf_conn *ct = NULL;
691 enum ip_conntrack_info ctinfo;
692
693 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
694 li = li_user;
695 else
696 li = &default_loginfo;
697
698 inst = instance_lookup_get(log, li->u.ulog.group);
699 if (!inst)
700 return;
701
702 if (prefix)
703 plen = strlen(prefix) + 1;
704
705
706
707
708 size = nlmsg_total_size(sizeof(struct nfgenmsg))
709 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
710 + nla_total_size(sizeof(u_int32_t))
711 + nla_total_size(sizeof(u_int32_t))
712#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
713 + nla_total_size(sizeof(u_int32_t))
714 + nla_total_size(sizeof(u_int32_t))
715#endif
716 + nla_total_size(sizeof(u_int32_t))
717 + nla_total_size(sizeof(u_int32_t))
718 + nla_total_size(sizeof(u_int32_t))
719 + nla_total_size(plen)
720 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
721 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
722 + nla_total_size(sizeof(struct nfgenmsg));
723
724 if (in && skb_mac_header_was_set(skb)) {
725 size += nla_total_size(skb->dev->hard_header_len)
726 + nla_total_size(sizeof(u_int16_t))
727 + nla_total_size(sizeof(u_int16_t));
728 }
729
730 spin_lock_bh(&inst->lock);
731
732 if (inst->flags & NFULNL_CFG_F_SEQ)
733 size += nla_total_size(sizeof(u_int32_t));
734 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
735 size += nla_total_size(sizeof(u_int32_t));
736#if IS_ENABLED(CONFIG_NF_CONNTRACK)
737 if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
738 nfnl_ct = rcu_dereference(nfnl_ct_hook);
739 if (nfnl_ct != NULL) {
740 ct = nf_ct_get(skb, &ctinfo);
741 if (ct != NULL)
742 size += nfnl_ct->build_size(ct);
743 }
744 }
745#endif
746 if (pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE)
747 size += nfulnl_get_bridge_size(skb);
748
749 qthreshold = inst->qthreshold;
750
751 if (li->u.ulog.qthreshold)
752 if (qthreshold > li->u.ulog.qthreshold)
753 qthreshold = li->u.ulog.qthreshold;
754
755
756 switch (inst->copy_mode) {
757 case NFULNL_COPY_META:
758 case NFULNL_COPY_NONE:
759 data_len = 0;
760 break;
761
762 case NFULNL_COPY_PACKET:
763 data_len = inst->copy_range;
764 if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
765 (li->u.ulog.copy_len < data_len))
766 data_len = li->u.ulog.copy_len;
767
768 if (data_len > skb->len)
769 data_len = skb->len;
770
771 size += nla_total_size(data_len);
772 break;
773
774 case NFULNL_COPY_DISABLED:
775 default:
776 goto unlock_and_release;
777 }
778
779 if (inst->skb && size > skb_tailroom(inst->skb)) {
780
781
782 __nfulnl_flush(inst);
783 }
784
785 if (!inst->skb) {
786 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
787 inst->nlbufsiz, size);
788 if (!inst->skb)
789 goto alloc_failure;
790 }
791
792 inst->qlen++;
793
794 __build_packet_message(log, inst, skb, data_len, pf,
795 hooknum, in, out, prefix, plen,
796 nfnl_ct, ct, ctinfo);
797
798 if (inst->qlen >= qthreshold)
799 __nfulnl_flush(inst);
800
801
802 else if (!timer_pending(&inst->timer)) {
803 instance_get(inst);
804 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
805 add_timer(&inst->timer);
806 }
807
808unlock_and_release:
809 spin_unlock_bh(&inst->lock);
810 instance_put(inst);
811 return;
812
813alloc_failure:
814
815 goto unlock_and_release;
816}
817
818static int
819nfulnl_rcv_nl_event(struct notifier_block *this,
820 unsigned long event, void *ptr)
821{
822 struct netlink_notify *n = ptr;
823 struct nfnl_log_net *log = nfnl_log_pernet(n->net);
824
825 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
826 int i;
827
828
829 spin_lock_bh(&log->instances_lock);
830 for (i = 0; i < INSTANCE_BUCKETS; i++) {
831 struct hlist_node *t2;
832 struct nfulnl_instance *inst;
833 struct hlist_head *head = &log->instance_table[i];
834
835 hlist_for_each_entry_safe(inst, t2, head, hlist) {
836 if (n->portid == inst->peer_portid)
837 __instance_destroy(inst);
838 }
839 }
840 spin_unlock_bh(&log->instances_lock);
841 }
842 return NOTIFY_DONE;
843}
844
845static struct notifier_block nfulnl_rtnl_notifier = {
846 .notifier_call = nfulnl_rcv_nl_event,
847};
848
849static int nfulnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info,
850 const struct nlattr * const nfula[])
851{
852 return -ENOTSUPP;
853}
854
855static struct nf_logger nfulnl_logger __read_mostly = {
856 .name = "nfnetlink_log",
857 .type = NF_LOG_TYPE_ULOG,
858 .logfn = nfulnl_log_packet,
859 .me = THIS_MODULE,
860};
861
862static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
863 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
864 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
865 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
866 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
867 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
868 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
869};
870
871static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
872 const struct nlattr * const nfula[])
873{
874 struct nfnl_log_net *log = nfnl_log_pernet(info->net);
875 u_int16_t group_num = ntohs(info->nfmsg->res_id);
876 struct nfulnl_msg_config_cmd *cmd = NULL;
877 struct nfulnl_instance *inst;
878 u16 flags = 0;
879 int ret = 0;
880
881 if (nfula[NFULA_CFG_CMD]) {
882 u_int8_t pf = info->nfmsg->nfgen_family;
883 cmd = nla_data(nfula[NFULA_CFG_CMD]);
884
885
886 switch (cmd->command) {
887 case NFULNL_CFG_CMD_PF_BIND:
888 return nf_log_bind_pf(info->net, pf, &nfulnl_logger);
889 case NFULNL_CFG_CMD_PF_UNBIND:
890 nf_log_unbind_pf(info->net, pf);
891 return 0;
892 }
893 }
894
895 inst = instance_lookup_get(log, group_num);
896 if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
897 ret = -EPERM;
898 goto out_put;
899 }
900
901
902
903
904 if (nfula[NFULA_CFG_FLAGS]) {
905 flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
906
907 if ((flags & NFULNL_CFG_F_CONNTRACK) &&
908 !rcu_access_pointer(nfnl_ct_hook)) {
909#ifdef CONFIG_MODULES
910 nfnl_unlock(NFNL_SUBSYS_ULOG);
911 request_module("ip_conntrack_netlink");
912 nfnl_lock(NFNL_SUBSYS_ULOG);
913 if (rcu_access_pointer(nfnl_ct_hook)) {
914 ret = -EAGAIN;
915 goto out_put;
916 }
917#endif
918 ret = -EOPNOTSUPP;
919 goto out_put;
920 }
921 }
922
923 if (cmd != NULL) {
924 switch (cmd->command) {
925 case NFULNL_CFG_CMD_BIND:
926 if (inst) {
927 ret = -EBUSY;
928 goto out_put;
929 }
930
931 inst = instance_create(info->net, group_num,
932 NETLINK_CB(skb).portid,
933 sk_user_ns(NETLINK_CB(skb).sk));
934 if (IS_ERR(inst)) {
935 ret = PTR_ERR(inst);
936 goto out;
937 }
938 break;
939 case NFULNL_CFG_CMD_UNBIND:
940 if (!inst) {
941 ret = -ENODEV;
942 goto out;
943 }
944
945 instance_destroy(log, inst);
946 goto out_put;
947 default:
948 ret = -ENOTSUPP;
949 goto out_put;
950 }
951 } else if (!inst) {
952 ret = -ENODEV;
953 goto out;
954 }
955
956 if (nfula[NFULA_CFG_MODE]) {
957 struct nfulnl_msg_config_mode *params =
958 nla_data(nfula[NFULA_CFG_MODE]);
959
960 nfulnl_set_mode(inst, params->copy_mode,
961 ntohl(params->copy_range));
962 }
963
964 if (nfula[NFULA_CFG_TIMEOUT]) {
965 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
966
967 nfulnl_set_timeout(inst, ntohl(timeout));
968 }
969
970 if (nfula[NFULA_CFG_NLBUFSIZ]) {
971 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
972
973 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
974 }
975
976 if (nfula[NFULA_CFG_QTHRESH]) {
977 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
978
979 nfulnl_set_qthresh(inst, ntohl(qthresh));
980 }
981
982 if (nfula[NFULA_CFG_FLAGS])
983 nfulnl_set_flags(inst, flags);
984
985out_put:
986 instance_put(inst);
987out:
988 return ret;
989}
990
991static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
992 [NFULNL_MSG_PACKET] = {
993 .call = nfulnl_recv_unsupp,
994 .type = NFNL_CB_MUTEX,
995 .attr_count = NFULA_MAX,
996 },
997 [NFULNL_MSG_CONFIG] = {
998 .call = nfulnl_recv_config,
999 .type = NFNL_CB_MUTEX,
1000 .attr_count = NFULA_CFG_MAX,
1001 .policy = nfula_cfg_policy
1002 },
1003};
1004
1005static const struct nfnetlink_subsystem nfulnl_subsys = {
1006 .name = "log",
1007 .subsys_id = NFNL_SUBSYS_ULOG,
1008 .cb_count = NFULNL_MSG_MAX,
1009 .cb = nfulnl_cb,
1010};
1011
1012#ifdef CONFIG_PROC_FS
1013struct iter_state {
1014 struct seq_net_private p;
1015 unsigned int bucket;
1016};
1017
1018static struct hlist_node *get_first(struct net *net, struct iter_state *st)
1019{
1020 struct nfnl_log_net *log;
1021 if (!st)
1022 return NULL;
1023
1024 log = nfnl_log_pernet(net);
1025
1026 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1027 struct hlist_head *head = &log->instance_table[st->bucket];
1028
1029 if (!hlist_empty(head))
1030 return rcu_dereference_bh(hlist_first_rcu(head));
1031 }
1032 return NULL;
1033}
1034
1035static struct hlist_node *get_next(struct net *net, struct iter_state *st,
1036 struct hlist_node *h)
1037{
1038 h = rcu_dereference_bh(hlist_next_rcu(h));
1039 while (!h) {
1040 struct nfnl_log_net *log;
1041 struct hlist_head *head;
1042
1043 if (++st->bucket >= INSTANCE_BUCKETS)
1044 return NULL;
1045
1046 log = nfnl_log_pernet(net);
1047 head = &log->instance_table[st->bucket];
1048 h = rcu_dereference_bh(hlist_first_rcu(head));
1049 }
1050 return h;
1051}
1052
1053static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
1054 loff_t pos)
1055{
1056 struct hlist_node *head;
1057 head = get_first(net, st);
1058
1059 if (head)
1060 while (pos && (head = get_next(net, st, head)))
1061 pos--;
1062 return pos ? NULL : head;
1063}
1064
1065static void *seq_start(struct seq_file *s, loff_t *pos)
1066 __acquires(rcu_bh)
1067{
1068 rcu_read_lock_bh();
1069 return get_idx(seq_file_net(s), s->private, *pos);
1070}
1071
1072static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1073{
1074 (*pos)++;
1075 return get_next(seq_file_net(s), s->private, v);
1076}
1077
1078static void seq_stop(struct seq_file *s, void *v)
1079 __releases(rcu_bh)
1080{
1081 rcu_read_unlock_bh();
1082}
1083
1084static int seq_show(struct seq_file *s, void *v)
1085{
1086 const struct nfulnl_instance *inst = v;
1087
1088 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1089 inst->group_num,
1090 inst->peer_portid, inst->qlen,
1091 inst->copy_mode, inst->copy_range,
1092 inst->flushtimeout, refcount_read(&inst->use));
1093
1094 return 0;
1095}
1096
1097static const struct seq_operations nful_seq_ops = {
1098 .start = seq_start,
1099 .next = seq_next,
1100 .stop = seq_stop,
1101 .show = seq_show,
1102};
1103#endif
1104
1105static int __net_init nfnl_log_net_init(struct net *net)
1106{
1107 unsigned int i;
1108 struct nfnl_log_net *log = nfnl_log_pernet(net);
1109#ifdef CONFIG_PROC_FS
1110 struct proc_dir_entry *proc;
1111 kuid_t root_uid;
1112 kgid_t root_gid;
1113#endif
1114
1115 for (i = 0; i < INSTANCE_BUCKETS; i++)
1116 INIT_HLIST_HEAD(&log->instance_table[i]);
1117 spin_lock_init(&log->instances_lock);
1118
1119#ifdef CONFIG_PROC_FS
1120 proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter,
1121 &nful_seq_ops, sizeof(struct iter_state));
1122 if (!proc)
1123 return -ENOMEM;
1124
1125 root_uid = make_kuid(net->user_ns, 0);
1126 root_gid = make_kgid(net->user_ns, 0);
1127 if (uid_valid(root_uid) && gid_valid(root_gid))
1128 proc_set_user(proc, root_uid, root_gid);
1129#endif
1130 return 0;
1131}
1132
1133static void __net_exit nfnl_log_net_exit(struct net *net)
1134{
1135 struct nfnl_log_net *log = nfnl_log_pernet(net);
1136 unsigned int i;
1137
1138#ifdef CONFIG_PROC_FS
1139 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
1140#endif
1141 nf_log_unset(net, &nfulnl_logger);
1142 for (i = 0; i < INSTANCE_BUCKETS; i++)
1143 WARN_ON_ONCE(!hlist_empty(&log->instance_table[i]));
1144}
1145
1146static struct pernet_operations nfnl_log_net_ops = {
1147 .init = nfnl_log_net_init,
1148 .exit = nfnl_log_net_exit,
1149 .id = &nfnl_log_net_id,
1150 .size = sizeof(struct nfnl_log_net),
1151};
1152
1153static int __init nfnetlink_log_init(void)
1154{
1155 int status;
1156
1157 status = register_pernet_subsys(&nfnl_log_net_ops);
1158 if (status < 0) {
1159 pr_err("failed to register pernet ops\n");
1160 goto out;
1161 }
1162
1163 netlink_register_notifier(&nfulnl_rtnl_notifier);
1164 status = nfnetlink_subsys_register(&nfulnl_subsys);
1165 if (status < 0) {
1166 pr_err("failed to create netlink socket\n");
1167 goto cleanup_netlink_notifier;
1168 }
1169
1170 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1171 if (status < 0) {
1172 pr_err("failed to register logger\n");
1173 goto cleanup_subsys;
1174 }
1175
1176 return status;
1177
1178cleanup_subsys:
1179 nfnetlink_subsys_unregister(&nfulnl_subsys);
1180cleanup_netlink_notifier:
1181 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1182 unregister_pernet_subsys(&nfnl_log_net_ops);
1183out:
1184 return status;
1185}
1186
1187static void __exit nfnetlink_log_fini(void)
1188{
1189 nfnetlink_subsys_unregister(&nfulnl_subsys);
1190 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1191 unregister_pernet_subsys(&nfnl_log_net_ops);
1192 nf_log_unregister(&nfulnl_logger);
1193}
1194
1195MODULE_DESCRIPTION("netfilter userspace logging");
1196MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1197MODULE_LICENSE("GPL");
1198MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1199MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1200MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1201MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
1202MODULE_ALIAS_NF_LOGGER(3, 1);
1203MODULE_ALIAS_NF_LOGGER(5, 1);
1204
1205module_init(nfnetlink_log_init);
1206module_exit(nfnetlink_log_fini);
1207