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