GNU Linux-libre 4.19.281-gnu1
[releases.git] / net / netfilter / nfnetlink.c
1 /* Netfilter messages via netlink socket. Allows for user space
2  * protocol helpers and general trouble making from userspace.
3  *
4  * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5  * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org>
7  *
8  * Initial netfilter messages via netlink development funded and
9  * generally made possible by Network Robots, Inc. (www.networkrobots.com)
10  *
11  * Further development of this code funded by Astaro AG (http://www.astaro.com)
12  *
13  * This software may be used and distributed according to the terms
14  * of the GNU General Public License, incorporated herein by reference.
15  */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
24 #include <linux/skbuff.h>
25 #include <linux/uaccess.h>
26 #include <net/sock.h>
27 #include <linux/init.h>
28 #include <linux/sched/signal.h>
29
30 #include <net/netlink.h>
31 #include <linux/netfilter/nfnetlink.h>
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
35 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
36
37 #define nfnl_dereference_protected(id) \
38         rcu_dereference_protected(table[(id)].subsys, \
39                                   lockdep_nfnl_is_held((id)))
40
41 #define NFNL_MAX_ATTR_COUNT     32
42
43 static struct {
44         struct mutex                            mutex;
45         const struct nfnetlink_subsystem __rcu  *subsys;
46 } table[NFNL_SUBSYS_COUNT];
47
48 static const int nfnl_group2type[NFNLGRP_MAX+1] = {
49         [NFNLGRP_CONNTRACK_NEW]         = NFNL_SUBSYS_CTNETLINK,
50         [NFNLGRP_CONNTRACK_UPDATE]      = NFNL_SUBSYS_CTNETLINK,
51         [NFNLGRP_CONNTRACK_DESTROY]     = NFNL_SUBSYS_CTNETLINK,
52         [NFNLGRP_CONNTRACK_EXP_NEW]     = NFNL_SUBSYS_CTNETLINK_EXP,
53         [NFNLGRP_CONNTRACK_EXP_UPDATE]  = NFNL_SUBSYS_CTNETLINK_EXP,
54         [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
55         [NFNLGRP_NFTABLES]              = NFNL_SUBSYS_NFTABLES,
56         [NFNLGRP_ACCT_QUOTA]            = NFNL_SUBSYS_ACCT,
57         [NFNLGRP_NFTRACE]               = NFNL_SUBSYS_NFTABLES,
58 };
59
60 void nfnl_lock(__u8 subsys_id)
61 {
62         mutex_lock(&table[subsys_id].mutex);
63 }
64 EXPORT_SYMBOL_GPL(nfnl_lock);
65
66 void nfnl_unlock(__u8 subsys_id)
67 {
68         mutex_unlock(&table[subsys_id].mutex);
69 }
70 EXPORT_SYMBOL_GPL(nfnl_unlock);
71
72 #ifdef CONFIG_PROVE_LOCKING
73 bool lockdep_nfnl_is_held(u8 subsys_id)
74 {
75         return lockdep_is_held(&table[subsys_id].mutex);
76 }
77 EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
78 #endif
79
80 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
81 {
82         u8 cb_id;
83
84         /* Sanity-check attr_count size to avoid stack buffer overflow. */
85         for (cb_id = 0; cb_id < n->cb_count; cb_id++)
86                 if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
87                         return -EINVAL;
88
89         nfnl_lock(n->subsys_id);
90         if (table[n->subsys_id].subsys) {
91                 nfnl_unlock(n->subsys_id);
92                 return -EBUSY;
93         }
94         rcu_assign_pointer(table[n->subsys_id].subsys, n);
95         nfnl_unlock(n->subsys_id);
96
97         return 0;
98 }
99 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
100
101 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
102 {
103         nfnl_lock(n->subsys_id);
104         table[n->subsys_id].subsys = NULL;
105         nfnl_unlock(n->subsys_id);
106         synchronize_rcu();
107         return 0;
108 }
109 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
110
111 static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
112 {
113         u8 subsys_id = NFNL_SUBSYS_ID(type);
114
115         if (subsys_id >= NFNL_SUBSYS_COUNT)
116                 return NULL;
117
118         return rcu_dereference(table[subsys_id].subsys);
119 }
120
121 static inline const struct nfnl_callback *
122 nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
123 {
124         u8 cb_id = NFNL_MSG_TYPE(type);
125
126         if (cb_id >= ss->cb_count)
127                 return NULL;
128
129         return &ss->cb[cb_id];
130 }
131
132 int nfnetlink_has_listeners(struct net *net, unsigned int group)
133 {
134         return netlink_has_listeners(net->nfnl, group);
135 }
136 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
137
138 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
139                    unsigned int group, int echo, gfp_t flags)
140 {
141         return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags);
142 }
143 EXPORT_SYMBOL_GPL(nfnetlink_send);
144
145 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
146 {
147         return netlink_set_err(net->nfnl, portid, group, error);
148 }
149 EXPORT_SYMBOL_GPL(nfnetlink_set_err);
150
151 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid)
152 {
153         int err;
154
155         err = nlmsg_unicast(net->nfnl, skb, portid);
156         if (err == -EAGAIN)
157                 err = -ENOBUFS;
158
159         return err;
160 }
161 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
162
163 /* Process one complete nfnetlink message. */
164 static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
165                              struct netlink_ext_ack *extack)
166 {
167         struct net *net = sock_net(skb->sk);
168         const struct nfnl_callback *nc;
169         const struct nfnetlink_subsystem *ss;
170         int type, err;
171
172         /* All the messages must at least contain nfgenmsg */
173         if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
174                 return 0;
175
176         type = nlh->nlmsg_type;
177 replay:
178         rcu_read_lock();
179         ss = nfnetlink_get_subsys(type);
180         if (!ss) {
181 #ifdef CONFIG_MODULES
182                 rcu_read_unlock();
183                 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
184                 rcu_read_lock();
185                 ss = nfnetlink_get_subsys(type);
186                 if (!ss)
187 #endif
188                 {
189                         rcu_read_unlock();
190                         return -EINVAL;
191                 }
192         }
193
194         nc = nfnetlink_find_client(type, ss);
195         if (!nc) {
196                 rcu_read_unlock();
197                 return -EINVAL;
198         }
199
200         {
201                 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
202                 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
203                 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
204                 struct nlattr *attr = (void *)nlh + min_len;
205                 int attrlen = nlh->nlmsg_len - min_len;
206                 __u8 subsys_id = NFNL_SUBSYS_ID(type);
207
208                 /* Sanity-check NFNL_MAX_ATTR_COUNT */
209                 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
210                         rcu_read_unlock();
211                         return -ENOMEM;
212                 }
213
214                 err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen,
215                                 ss->cb[cb_id].policy, extack);
216                 if (err < 0) {
217                         rcu_read_unlock();
218                         return err;
219                 }
220
221                 if (nc->call_rcu) {
222                         err = nc->call_rcu(net, net->nfnl, skb, nlh,
223                                            (const struct nlattr **)cda,
224                                            extack);
225                         rcu_read_unlock();
226                 } else {
227                         rcu_read_unlock();
228                         nfnl_lock(subsys_id);
229                         if (nfnl_dereference_protected(subsys_id) != ss ||
230                             nfnetlink_find_client(type, ss) != nc)
231                                 err = -EAGAIN;
232                         else if (nc->call)
233                                 err = nc->call(net, net->nfnl, skb, nlh,
234                                                (const struct nlattr **)cda,
235                                                extack);
236                         else
237                                 err = -EINVAL;
238                         nfnl_unlock(subsys_id);
239                 }
240                 if (err == -EAGAIN)
241                         goto replay;
242                 return err;
243         }
244 }
245
246 struct nfnl_err {
247         struct list_head        head;
248         struct nlmsghdr         *nlh;
249         int                     err;
250         struct netlink_ext_ack  extack;
251 };
252
253 static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
254                         const struct netlink_ext_ack *extack)
255 {
256         struct nfnl_err *nfnl_err;
257
258         nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
259         if (nfnl_err == NULL)
260                 return -ENOMEM;
261
262         nfnl_err->nlh = nlh;
263         nfnl_err->err = err;
264         nfnl_err->extack = *extack;
265         list_add_tail(&nfnl_err->head, list);
266
267         return 0;
268 }
269
270 static void nfnl_err_del(struct nfnl_err *nfnl_err)
271 {
272         list_del(&nfnl_err->head);
273         kfree(nfnl_err);
274 }
275
276 static void nfnl_err_reset(struct list_head *err_list)
277 {
278         struct nfnl_err *nfnl_err, *next;
279
280         list_for_each_entry_safe(nfnl_err, next, err_list, head)
281                 nfnl_err_del(nfnl_err);
282 }
283
284 static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
285 {
286         struct nfnl_err *nfnl_err, *next;
287
288         list_for_each_entry_safe(nfnl_err, next, err_list, head) {
289                 netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
290                             &nfnl_err->extack);
291                 nfnl_err_del(nfnl_err);
292         }
293 }
294
295 enum {
296         NFNL_BATCH_FAILURE      = (1 << 0),
297         NFNL_BATCH_DONE         = (1 << 1),
298         NFNL_BATCH_REPLAY       = (1 << 2),
299 };
300
301 static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
302                                 u16 subsys_id, u32 genid)
303 {
304         struct sk_buff *oskb = skb;
305         struct net *net = sock_net(skb->sk);
306         const struct nfnetlink_subsystem *ss;
307         const struct nfnl_callback *nc;
308         struct netlink_ext_ack extack;
309         LIST_HEAD(err_list);
310         u32 status;
311         int err;
312
313         if (subsys_id >= NFNL_SUBSYS_COUNT)
314                 return netlink_ack(skb, nlh, -EINVAL, NULL);
315 replay:
316         status = 0;
317
318         skb = netlink_skb_clone(oskb, GFP_KERNEL);
319         if (!skb)
320                 return netlink_ack(oskb, nlh, -ENOMEM, NULL);
321
322         nfnl_lock(subsys_id);
323         ss = nfnl_dereference_protected(subsys_id);
324         if (!ss) {
325 #ifdef CONFIG_MODULES
326                 nfnl_unlock(subsys_id);
327                 request_module("nfnetlink-subsys-%d", subsys_id);
328                 nfnl_lock(subsys_id);
329                 ss = nfnl_dereference_protected(subsys_id);
330                 if (!ss)
331 #endif
332                 {
333                         nfnl_unlock(subsys_id);
334                         netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
335                         return kfree_skb(skb);
336                 }
337         }
338
339         if (!ss->valid_genid || !ss->commit || !ss->abort) {
340                 nfnl_unlock(subsys_id);
341                 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
342                 return kfree_skb(skb);
343         }
344
345         if (!try_module_get(ss->owner)) {
346                 nfnl_unlock(subsys_id);
347                 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
348                 return kfree_skb(skb);
349         }
350
351         if (!ss->valid_genid(net, genid)) {
352                 module_put(ss->owner);
353                 nfnl_unlock(subsys_id);
354                 netlink_ack(oskb, nlh, -ERESTART, NULL);
355                 return kfree_skb(skb);
356         }
357
358         nfnl_unlock(subsys_id);
359
360         while (skb->len >= nlmsg_total_size(0)) {
361                 int msglen, type;
362
363                 if (fatal_signal_pending(current)) {
364                         nfnl_err_reset(&err_list);
365                         err = -EINTR;
366                         status = NFNL_BATCH_FAILURE;
367                         goto done;
368                 }
369
370                 memset(&extack, 0, sizeof(extack));
371                 nlh = nlmsg_hdr(skb);
372                 err = 0;
373
374                 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
375                     skb->len < nlh->nlmsg_len ||
376                     nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
377                         nfnl_err_reset(&err_list);
378                         status |= NFNL_BATCH_FAILURE;
379                         goto done;
380                 }
381
382                 /* Only requests are handled by the kernel */
383                 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
384                         err = -EINVAL;
385                         goto ack;
386                 }
387
388                 type = nlh->nlmsg_type;
389                 if (type == NFNL_MSG_BATCH_BEGIN) {
390                         /* Malformed: Batch begin twice */
391                         nfnl_err_reset(&err_list);
392                         status |= NFNL_BATCH_FAILURE;
393                         goto done;
394                 } else if (type == NFNL_MSG_BATCH_END) {
395                         status |= NFNL_BATCH_DONE;
396                         goto done;
397                 } else if (type < NLMSG_MIN_TYPE) {
398                         err = -EINVAL;
399                         goto ack;
400                 }
401
402                 /* We only accept a batch with messages for the same
403                  * subsystem.
404                  */
405                 if (NFNL_SUBSYS_ID(type) != subsys_id) {
406                         err = -EINVAL;
407                         goto ack;
408                 }
409
410                 nc = nfnetlink_find_client(type, ss);
411                 if (!nc) {
412                         err = -EINVAL;
413                         goto ack;
414                 }
415
416                 {
417                         int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
418                         u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
419                         struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
420                         struct nlattr *attr = (void *)nlh + min_len;
421                         int attrlen = nlh->nlmsg_len - min_len;
422
423                         /* Sanity-check NFTA_MAX_ATTR */
424                         if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
425                                 err = -ENOMEM;
426                                 goto ack;
427                         }
428
429                         err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
430                                         attrlen, ss->cb[cb_id].policy, NULL);
431                         if (err < 0)
432                                 goto ack;
433
434                         if (nc->call_batch) {
435                                 err = nc->call_batch(net, net->nfnl, skb, nlh,
436                                                      (const struct nlattr **)cda,
437                                                      &extack);
438                         }
439
440                         /* The lock was released to autoload some module, we
441                          * have to abort and start from scratch using the
442                          * original skb.
443                          */
444                         if (err == -EAGAIN) {
445                                 status |= NFNL_BATCH_REPLAY;
446                                 goto done;
447                         }
448                 }
449 ack:
450                 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
451                         /* Errors are delivered once the full batch has been
452                          * processed, this avoids that the same error is
453                          * reported several times when replaying the batch.
454                          */
455                         if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
456                                 /* We failed to enqueue an error, reset the
457                                  * list of errors and send OOM to userspace
458                                  * pointing to the batch header.
459                                  */
460                                 nfnl_err_reset(&err_list);
461                                 netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
462                                             NULL);
463                                 status |= NFNL_BATCH_FAILURE;
464                                 goto done;
465                         }
466                         /* We don't stop processing the batch on errors, thus,
467                          * userspace gets all the errors that the batch
468                          * triggers.
469                          */
470                         if (err)
471                                 status |= NFNL_BATCH_FAILURE;
472                 }
473
474                 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
475                 if (msglen > skb->len)
476                         msglen = skb->len;
477                 skb_pull(skb, msglen);
478         }
479 done:
480         if (status & NFNL_BATCH_REPLAY) {
481                 ss->abort(net, oskb);
482                 nfnl_err_reset(&err_list);
483                 kfree_skb(skb);
484                 module_put(ss->owner);
485                 goto replay;
486         } else if (status == NFNL_BATCH_DONE) {
487                 err = ss->commit(net, oskb);
488                 if (err == -EAGAIN) {
489                         status |= NFNL_BATCH_REPLAY;
490                         goto done;
491                 } else if (err) {
492                         ss->abort(net, oskb);
493                         netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
494                 }
495         } else {
496                 ss->abort(net, oskb);
497         }
498         if (ss->cleanup)
499                 ss->cleanup(net);
500
501         nfnl_err_deliver(&err_list, oskb);
502         kfree_skb(skb);
503         module_put(ss->owner);
504 }
505
506 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
507         [NFNL_BATCH_GENID]      = { .type = NLA_U32 },
508 };
509
510 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
511 {
512         int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
513         struct nlattr *attr = (void *)nlh + min_len;
514         struct nlattr *cda[NFNL_BATCH_MAX + 1];
515         int attrlen = nlh->nlmsg_len - min_len;
516         struct nfgenmsg *nfgenmsg;
517         int msglen, err;
518         u32 gen_id = 0;
519         u16 res_id;
520
521         msglen = NLMSG_ALIGN(nlh->nlmsg_len);
522         if (msglen > skb->len)
523                 msglen = skb->len;
524
525         if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
526                 return;
527
528         err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
529                         NULL);
530         if (err < 0) {
531                 netlink_ack(skb, nlh, err, NULL);
532                 return;
533         }
534         if (cda[NFNL_BATCH_GENID])
535                 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
536
537         nfgenmsg = nlmsg_data(nlh);
538         skb_pull(skb, msglen);
539         /* Work around old nft using host byte order */
540         if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
541                 res_id = NFNL_SUBSYS_NFTABLES;
542         else
543                 res_id = ntohs(nfgenmsg->res_id);
544
545         nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
546 }
547
548 static void nfnetlink_rcv(struct sk_buff *skb)
549 {
550         struct nlmsghdr *nlh = nlmsg_hdr(skb);
551
552         if (skb->len < NLMSG_HDRLEN ||
553             nlh->nlmsg_len < NLMSG_HDRLEN ||
554             skb->len < nlh->nlmsg_len)
555                 return;
556
557         if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
558                 netlink_ack(skb, nlh, -EPERM, NULL);
559                 return;
560         }
561
562         if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
563                 nfnetlink_rcv_skb_batch(skb, nlh);
564         else
565                 netlink_rcv_skb(skb, nfnetlink_rcv_msg);
566 }
567
568 #ifdef CONFIG_MODULES
569 static int nfnetlink_bind(struct net *net, int group)
570 {
571         const struct nfnetlink_subsystem *ss;
572         int type;
573
574         if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
575                 return 0;
576
577         type = nfnl_group2type[group];
578
579         rcu_read_lock();
580         ss = nfnetlink_get_subsys(type << 8);
581         rcu_read_unlock();
582         if (!ss)
583                 request_module_nowait("nfnetlink-subsys-%d", type);
584         return 0;
585 }
586 #endif
587
588 static int __net_init nfnetlink_net_init(struct net *net)
589 {
590         struct sock *nfnl;
591         struct netlink_kernel_cfg cfg = {
592                 .groups = NFNLGRP_MAX,
593                 .input  = nfnetlink_rcv,
594 #ifdef CONFIG_MODULES
595                 .bind   = nfnetlink_bind,
596 #endif
597         };
598
599         nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
600         if (!nfnl)
601                 return -ENOMEM;
602         net->nfnl_stash = nfnl;
603         rcu_assign_pointer(net->nfnl, nfnl);
604         return 0;
605 }
606
607 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
608 {
609         struct net *net;
610
611         list_for_each_entry(net, net_exit_list, exit_list)
612                 RCU_INIT_POINTER(net->nfnl, NULL);
613         synchronize_net();
614         list_for_each_entry(net, net_exit_list, exit_list)
615                 netlink_kernel_release(net->nfnl_stash);
616 }
617
618 static struct pernet_operations nfnetlink_net_ops = {
619         .init           = nfnetlink_net_init,
620         .exit_batch     = nfnetlink_net_exit_batch,
621 };
622
623 static int __init nfnetlink_init(void)
624 {
625         int i;
626
627         for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
628                 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
629
630         for (i=0; i<NFNL_SUBSYS_COUNT; i++)
631                 mutex_init(&table[i].mutex);
632
633         return register_pernet_subsys(&nfnetlink_net_ops);
634 }
635
636 static void __exit nfnetlink_exit(void)
637 {
638         unregister_pernet_subsys(&nfnetlink_net_ops);
639 }
640 module_init(nfnetlink_init);
641 module_exit(nfnetlink_exit);