xref: /openbmc/linux/net/netfilter/nfnetlink.c (revision 050e9baa)
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 		      int flags)
153 {
154 	return netlink_unicast(net->nfnl, skb, portid, flags);
155 }
156 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
157 
158 /* Process one complete nfnetlink message. */
159 static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
160 			     struct netlink_ext_ack *extack)
161 {
162 	struct net *net = sock_net(skb->sk);
163 	const struct nfnl_callback *nc;
164 	const struct nfnetlink_subsystem *ss;
165 	int type, err;
166 
167 	/* All the messages must at least contain nfgenmsg */
168 	if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
169 		return 0;
170 
171 	type = nlh->nlmsg_type;
172 replay:
173 	rcu_read_lock();
174 	ss = nfnetlink_get_subsys(type);
175 	if (!ss) {
176 #ifdef CONFIG_MODULES
177 		rcu_read_unlock();
178 		request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
179 		rcu_read_lock();
180 		ss = nfnetlink_get_subsys(type);
181 		if (!ss)
182 #endif
183 		{
184 			rcu_read_unlock();
185 			return -EINVAL;
186 		}
187 	}
188 
189 	nc = nfnetlink_find_client(type, ss);
190 	if (!nc) {
191 		rcu_read_unlock();
192 		return -EINVAL;
193 	}
194 
195 	{
196 		int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
197 		u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
198 		struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
199 		struct nlattr *attr = (void *)nlh + min_len;
200 		int attrlen = nlh->nlmsg_len - min_len;
201 		__u8 subsys_id = NFNL_SUBSYS_ID(type);
202 
203 		/* Sanity-check NFNL_MAX_ATTR_COUNT */
204 		if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
205 			rcu_read_unlock();
206 			return -ENOMEM;
207 		}
208 
209 		err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen,
210 				ss->cb[cb_id].policy, extack);
211 		if (err < 0) {
212 			rcu_read_unlock();
213 			return err;
214 		}
215 
216 		if (nc->call_rcu) {
217 			err = nc->call_rcu(net, net->nfnl, skb, nlh,
218 					   (const struct nlattr **)cda,
219 					   extack);
220 			rcu_read_unlock();
221 		} else {
222 			rcu_read_unlock();
223 			nfnl_lock(subsys_id);
224 			if (nfnl_dereference_protected(subsys_id) != ss ||
225 			    nfnetlink_find_client(type, ss) != nc)
226 				err = -EAGAIN;
227 			else if (nc->call)
228 				err = nc->call(net, net->nfnl, skb, nlh,
229 					       (const struct nlattr **)cda,
230 					       extack);
231 			else
232 				err = -EINVAL;
233 			nfnl_unlock(subsys_id);
234 		}
235 		if (err == -EAGAIN)
236 			goto replay;
237 		return err;
238 	}
239 }
240 
241 struct nfnl_err {
242 	struct list_head	head;
243 	struct nlmsghdr		*nlh;
244 	int			err;
245 	struct netlink_ext_ack	extack;
246 };
247 
248 static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
249 			const struct netlink_ext_ack *extack)
250 {
251 	struct nfnl_err *nfnl_err;
252 
253 	nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
254 	if (nfnl_err == NULL)
255 		return -ENOMEM;
256 
257 	nfnl_err->nlh = nlh;
258 	nfnl_err->err = err;
259 	nfnl_err->extack = *extack;
260 	list_add_tail(&nfnl_err->head, list);
261 
262 	return 0;
263 }
264 
265 static void nfnl_err_del(struct nfnl_err *nfnl_err)
266 {
267 	list_del(&nfnl_err->head);
268 	kfree(nfnl_err);
269 }
270 
271 static void nfnl_err_reset(struct list_head *err_list)
272 {
273 	struct nfnl_err *nfnl_err, *next;
274 
275 	list_for_each_entry_safe(nfnl_err, next, err_list, head)
276 		nfnl_err_del(nfnl_err);
277 }
278 
279 static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
280 {
281 	struct nfnl_err *nfnl_err, *next;
282 
283 	list_for_each_entry_safe(nfnl_err, next, err_list, head) {
284 		netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
285 			    &nfnl_err->extack);
286 		nfnl_err_del(nfnl_err);
287 	}
288 }
289 
290 enum {
291 	NFNL_BATCH_FAILURE	= (1 << 0),
292 	NFNL_BATCH_DONE		= (1 << 1),
293 	NFNL_BATCH_REPLAY	= (1 << 2),
294 };
295 
296 static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
297 				u16 subsys_id, u32 genid)
298 {
299 	struct sk_buff *oskb = skb;
300 	struct net *net = sock_net(skb->sk);
301 	const struct nfnetlink_subsystem *ss;
302 	const struct nfnl_callback *nc;
303 	struct netlink_ext_ack extack;
304 	LIST_HEAD(err_list);
305 	u32 status;
306 	int err;
307 
308 	if (subsys_id >= NFNL_SUBSYS_COUNT)
309 		return netlink_ack(skb, nlh, -EINVAL, NULL);
310 replay:
311 	status = 0;
312 
313 	skb = netlink_skb_clone(oskb, GFP_KERNEL);
314 	if (!skb)
315 		return netlink_ack(oskb, nlh, -ENOMEM, NULL);
316 
317 	nfnl_lock(subsys_id);
318 	ss = nfnl_dereference_protected(subsys_id);
319 	if (!ss) {
320 #ifdef CONFIG_MODULES
321 		nfnl_unlock(subsys_id);
322 		request_module("nfnetlink-subsys-%d", subsys_id);
323 		nfnl_lock(subsys_id);
324 		ss = nfnl_dereference_protected(subsys_id);
325 		if (!ss)
326 #endif
327 		{
328 			nfnl_unlock(subsys_id);
329 			netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
330 			return kfree_skb(skb);
331 		}
332 	}
333 
334 	if (!ss->commit || !ss->abort) {
335 		nfnl_unlock(subsys_id);
336 		netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
337 		return kfree_skb(skb);
338 	}
339 
340 	if (genid && ss->valid_genid && !ss->valid_genid(net, genid)) {
341 		nfnl_unlock(subsys_id);
342 		netlink_ack(oskb, nlh, -ERESTART, NULL);
343 		return kfree_skb(skb);
344 	}
345 
346 	while (skb->len >= nlmsg_total_size(0)) {
347 		int msglen, type;
348 
349 		if (fatal_signal_pending(current)) {
350 			nfnl_err_reset(&err_list);
351 			err = -EINTR;
352 			status = NFNL_BATCH_FAILURE;
353 			goto done;
354 		}
355 
356 		memset(&extack, 0, sizeof(extack));
357 		nlh = nlmsg_hdr(skb);
358 		err = 0;
359 
360 		if (nlh->nlmsg_len < NLMSG_HDRLEN ||
361 		    skb->len < nlh->nlmsg_len ||
362 		    nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
363 			nfnl_err_reset(&err_list);
364 			status |= NFNL_BATCH_FAILURE;
365 			goto done;
366 		}
367 
368 		/* Only requests are handled by the kernel */
369 		if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
370 			err = -EINVAL;
371 			goto ack;
372 		}
373 
374 		type = nlh->nlmsg_type;
375 		if (type == NFNL_MSG_BATCH_BEGIN) {
376 			/* Malformed: Batch begin twice */
377 			nfnl_err_reset(&err_list);
378 			status |= NFNL_BATCH_FAILURE;
379 			goto done;
380 		} else if (type == NFNL_MSG_BATCH_END) {
381 			status |= NFNL_BATCH_DONE;
382 			goto done;
383 		} else if (type < NLMSG_MIN_TYPE) {
384 			err = -EINVAL;
385 			goto ack;
386 		}
387 
388 		/* We only accept a batch with messages for the same
389 		 * subsystem.
390 		 */
391 		if (NFNL_SUBSYS_ID(type) != subsys_id) {
392 			err = -EINVAL;
393 			goto ack;
394 		}
395 
396 		nc = nfnetlink_find_client(type, ss);
397 		if (!nc) {
398 			err = -EINVAL;
399 			goto ack;
400 		}
401 
402 		{
403 			int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
404 			u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
405 			struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
406 			struct nlattr *attr = (void *)nlh + min_len;
407 			int attrlen = nlh->nlmsg_len - min_len;
408 
409 			/* Sanity-check NFTA_MAX_ATTR */
410 			if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
411 				err = -ENOMEM;
412 				goto ack;
413 			}
414 
415 			err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
416 					attrlen, ss->cb[cb_id].policy, NULL);
417 			if (err < 0)
418 				goto ack;
419 
420 			if (nc->call_batch) {
421 				err = nc->call_batch(net, net->nfnl, skb, nlh,
422 						     (const struct nlattr **)cda,
423 						     &extack);
424 			}
425 
426 			/* The lock was released to autoload some module, we
427 			 * have to abort and start from scratch using the
428 			 * original skb.
429 			 */
430 			if (err == -EAGAIN) {
431 				status |= NFNL_BATCH_REPLAY;
432 				goto next;
433 			}
434 		}
435 ack:
436 		if (nlh->nlmsg_flags & NLM_F_ACK || err) {
437 			/* Errors are delivered once the full batch has been
438 			 * processed, this avoids that the same error is
439 			 * reported several times when replaying the batch.
440 			 */
441 			if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
442 				/* We failed to enqueue an error, reset the
443 				 * list of errors and send OOM to userspace
444 				 * pointing to the batch header.
445 				 */
446 				nfnl_err_reset(&err_list);
447 				netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
448 					    NULL);
449 				status |= NFNL_BATCH_FAILURE;
450 				goto done;
451 			}
452 			/* We don't stop processing the batch on errors, thus,
453 			 * userspace gets all the errors that the batch
454 			 * triggers.
455 			 */
456 			if (err)
457 				status |= NFNL_BATCH_FAILURE;
458 		}
459 next:
460 		msglen = NLMSG_ALIGN(nlh->nlmsg_len);
461 		if (msglen > skb->len)
462 			msglen = skb->len;
463 		skb_pull(skb, msglen);
464 	}
465 done:
466 	if (status & NFNL_BATCH_REPLAY) {
467 		ss->abort(net, oskb);
468 		nfnl_err_reset(&err_list);
469 		nfnl_unlock(subsys_id);
470 		kfree_skb(skb);
471 		goto replay;
472 	} else if (status == NFNL_BATCH_DONE) {
473 		err = ss->commit(net, oskb);
474 		if (err == -EAGAIN) {
475 			status |= NFNL_BATCH_REPLAY;
476 			goto done;
477 		} else if (err) {
478 			ss->abort(net, oskb);
479 			netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
480 		}
481 	} else {
482 		ss->abort(net, oskb);
483 	}
484 	if (ss->cleanup)
485 		ss->cleanup(net);
486 
487 	nfnl_err_deliver(&err_list, oskb);
488 	nfnl_unlock(subsys_id);
489 	kfree_skb(skb);
490 }
491 
492 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
493 	[NFNL_BATCH_GENID]	= { .type = NLA_U32 },
494 };
495 
496 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
497 {
498 	int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
499 	struct nlattr *attr = (void *)nlh + min_len;
500 	struct nlattr *cda[NFNL_BATCH_MAX + 1];
501 	int attrlen = nlh->nlmsg_len - min_len;
502 	struct nfgenmsg *nfgenmsg;
503 	int msglen, err;
504 	u32 gen_id = 0;
505 	u16 res_id;
506 
507 	msglen = NLMSG_ALIGN(nlh->nlmsg_len);
508 	if (msglen > skb->len)
509 		msglen = skb->len;
510 
511 	if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
512 		return;
513 
514 	err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
515 			NULL);
516 	if (err < 0) {
517 		netlink_ack(skb, nlh, err, NULL);
518 		return;
519 	}
520 	if (cda[NFNL_BATCH_GENID])
521 		gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
522 
523 	nfgenmsg = nlmsg_data(nlh);
524 	skb_pull(skb, msglen);
525 	/* Work around old nft using host byte order */
526 	if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
527 		res_id = NFNL_SUBSYS_NFTABLES;
528 	else
529 		res_id = ntohs(nfgenmsg->res_id);
530 
531 	nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
532 }
533 
534 static void nfnetlink_rcv(struct sk_buff *skb)
535 {
536 	struct nlmsghdr *nlh = nlmsg_hdr(skb);
537 
538 	if (skb->len < NLMSG_HDRLEN ||
539 	    nlh->nlmsg_len < NLMSG_HDRLEN ||
540 	    skb->len < nlh->nlmsg_len)
541 		return;
542 
543 	if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
544 		netlink_ack(skb, nlh, -EPERM, NULL);
545 		return;
546 	}
547 
548 	if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
549 		nfnetlink_rcv_skb_batch(skb, nlh);
550 	else
551 		netlink_rcv_skb(skb, nfnetlink_rcv_msg);
552 }
553 
554 #ifdef CONFIG_MODULES
555 static int nfnetlink_bind(struct net *net, int group)
556 {
557 	const struct nfnetlink_subsystem *ss;
558 	int type;
559 
560 	if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
561 		return 0;
562 
563 	type = nfnl_group2type[group];
564 
565 	rcu_read_lock();
566 	ss = nfnetlink_get_subsys(type << 8);
567 	rcu_read_unlock();
568 	if (!ss)
569 		request_module("nfnetlink-subsys-%d", type);
570 	return 0;
571 }
572 #endif
573 
574 static int __net_init nfnetlink_net_init(struct net *net)
575 {
576 	struct sock *nfnl;
577 	struct netlink_kernel_cfg cfg = {
578 		.groups	= NFNLGRP_MAX,
579 		.input	= nfnetlink_rcv,
580 #ifdef CONFIG_MODULES
581 		.bind	= nfnetlink_bind,
582 #endif
583 	};
584 
585 	nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
586 	if (!nfnl)
587 		return -ENOMEM;
588 	net->nfnl_stash = nfnl;
589 	rcu_assign_pointer(net->nfnl, nfnl);
590 	return 0;
591 }
592 
593 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
594 {
595 	struct net *net;
596 
597 	list_for_each_entry(net, net_exit_list, exit_list)
598 		RCU_INIT_POINTER(net->nfnl, NULL);
599 	synchronize_net();
600 	list_for_each_entry(net, net_exit_list, exit_list)
601 		netlink_kernel_release(net->nfnl_stash);
602 }
603 
604 static struct pernet_operations nfnetlink_net_ops = {
605 	.init		= nfnetlink_net_init,
606 	.exit_batch	= nfnetlink_net_exit_batch,
607 };
608 
609 static int __init nfnetlink_init(void)
610 {
611 	int i;
612 
613 	for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
614 		BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
615 
616 	for (i=0; i<NFNL_SUBSYS_COUNT; i++)
617 		mutex_init(&table[i].mutex);
618 
619 	return register_pernet_subsys(&nfnetlink_net_ops);
620 }
621 
622 static void __exit nfnetlink_exit(void)
623 {
624 	unregister_pernet_subsys(&nfnetlink_net_ops);
625 }
626 module_init(nfnetlink_init);
627 module_exit(nfnetlink_exit);
628