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 
nfnl_lock(__u8 subsys_id)60 void nfnl_lock(__u8 subsys_id)
61 {
62 	mutex_lock(&table[subsys_id].mutex);
63 }
64 EXPORT_SYMBOL_GPL(nfnl_lock);
65 
nfnl_unlock(__u8 subsys_id)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
lockdep_nfnl_is_held(u8 subsys_id)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 
nfnetlink_subsys_register(const struct nfnetlink_subsystem * n)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 
nfnetlink_subsys_unregister(const struct nfnetlink_subsystem * n)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 
nfnetlink_get_subsys(u16 type)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 *
nfnetlink_find_client(u16 type,const struct nfnetlink_subsystem * ss)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 
nfnetlink_has_listeners(struct net * net,unsigned int group)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 
nfnetlink_send(struct sk_buff * skb,struct net * net,u32 portid,unsigned int group,int echo,gfp_t flags)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 
nfnetlink_set_err(struct net * net,u32 portid,u32 group,int error)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 
nfnetlink_unicast(struct sk_buff * skb,struct net * net,u32 portid,int flags)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. */
nfnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)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 
nfnl_err_add(struct list_head * list,struct nlmsghdr * nlh,int err,const struct netlink_ext_ack * extack)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 
nfnl_err_del(struct nfnl_err * nfnl_err)265 static void nfnl_err_del(struct nfnl_err *nfnl_err)
266 {
267 	list_del(&nfnl_err->head);
268 	kfree(nfnl_err);
269 }
270 
nfnl_err_reset(struct list_head * err_list)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 
nfnl_err_deliver(struct list_head * err_list,struct sk_buff * skb)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 
nfnetlink_rcv_batch(struct sk_buff * skb,struct nlmsghdr * nlh,u16 subsys_id,u32 genid)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->valid_genid || !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 (!try_module_get(ss->owner)) {
341 		nfnl_unlock(subsys_id);
342 		netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
343 		return kfree_skb(skb);
344 	}
345 
346 	if (!ss->valid_genid(net, genid)) {
347 		module_put(ss->owner);
348 		nfnl_unlock(subsys_id);
349 		netlink_ack(oskb, nlh, -ERESTART, NULL);
350 		return kfree_skb(skb);
351 	}
352 
353 	nfnl_unlock(subsys_id);
354 
355 	while (skb->len >= nlmsg_total_size(0)) {
356 		int msglen, type;
357 
358 		if (fatal_signal_pending(current)) {
359 			nfnl_err_reset(&err_list);
360 			err = -EINTR;
361 			status = NFNL_BATCH_FAILURE;
362 			goto done;
363 		}
364 
365 		memset(&extack, 0, sizeof(extack));
366 		nlh = nlmsg_hdr(skb);
367 		err = 0;
368 
369 		if (nlh->nlmsg_len < NLMSG_HDRLEN ||
370 		    skb->len < nlh->nlmsg_len ||
371 		    nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
372 			nfnl_err_reset(&err_list);
373 			status |= NFNL_BATCH_FAILURE;
374 			goto done;
375 		}
376 
377 		/* Only requests are handled by the kernel */
378 		if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
379 			err = -EINVAL;
380 			goto ack;
381 		}
382 
383 		type = nlh->nlmsg_type;
384 		if (type == NFNL_MSG_BATCH_BEGIN) {
385 			/* Malformed: Batch begin twice */
386 			nfnl_err_reset(&err_list);
387 			status |= NFNL_BATCH_FAILURE;
388 			goto done;
389 		} else if (type == NFNL_MSG_BATCH_END) {
390 			status |= NFNL_BATCH_DONE;
391 			goto done;
392 		} else if (type < NLMSG_MIN_TYPE) {
393 			err = -EINVAL;
394 			goto ack;
395 		}
396 
397 		/* We only accept a batch with messages for the same
398 		 * subsystem.
399 		 */
400 		if (NFNL_SUBSYS_ID(type) != subsys_id) {
401 			err = -EINVAL;
402 			goto ack;
403 		}
404 
405 		nc = nfnetlink_find_client(type, ss);
406 		if (!nc) {
407 			err = -EINVAL;
408 			goto ack;
409 		}
410 
411 		{
412 			int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
413 			u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
414 			struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
415 			struct nlattr *attr = (void *)nlh + min_len;
416 			int attrlen = nlh->nlmsg_len - min_len;
417 
418 			/* Sanity-check NFTA_MAX_ATTR */
419 			if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
420 				err = -ENOMEM;
421 				goto ack;
422 			}
423 
424 			err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
425 					attrlen, ss->cb[cb_id].policy, NULL);
426 			if (err < 0)
427 				goto ack;
428 
429 			if (nc->call_batch) {
430 				err = nc->call_batch(net, net->nfnl, skb, nlh,
431 						     (const struct nlattr **)cda,
432 						     &extack);
433 			}
434 
435 			/* The lock was released to autoload some module, we
436 			 * have to abort and start from scratch using the
437 			 * original skb.
438 			 */
439 			if (err == -EAGAIN) {
440 				status |= NFNL_BATCH_REPLAY;
441 				goto done;
442 			}
443 		}
444 ack:
445 		if (nlh->nlmsg_flags & NLM_F_ACK || err) {
446 			/* Errors are delivered once the full batch has been
447 			 * processed, this avoids that the same error is
448 			 * reported several times when replaying the batch.
449 			 */
450 			if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
451 				/* We failed to enqueue an error, reset the
452 				 * list of errors and send OOM to userspace
453 				 * pointing to the batch header.
454 				 */
455 				nfnl_err_reset(&err_list);
456 				netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
457 					    NULL);
458 				status |= NFNL_BATCH_FAILURE;
459 				goto done;
460 			}
461 			/* We don't stop processing the batch on errors, thus,
462 			 * userspace gets all the errors that the batch
463 			 * triggers.
464 			 */
465 			if (err)
466 				status |= NFNL_BATCH_FAILURE;
467 		}
468 
469 		msglen = NLMSG_ALIGN(nlh->nlmsg_len);
470 		if (msglen > skb->len)
471 			msglen = skb->len;
472 		skb_pull(skb, msglen);
473 	}
474 done:
475 	if (status & NFNL_BATCH_REPLAY) {
476 		ss->abort(net, oskb);
477 		nfnl_err_reset(&err_list);
478 		kfree_skb(skb);
479 		module_put(ss->owner);
480 		goto replay;
481 	} else if (status == NFNL_BATCH_DONE) {
482 		err = ss->commit(net, oskb);
483 		if (err == -EAGAIN) {
484 			status |= NFNL_BATCH_REPLAY;
485 			goto done;
486 		} else if (err) {
487 			ss->abort(net, oskb);
488 			netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
489 		}
490 	} else {
491 		ss->abort(net, oskb);
492 	}
493 	if (ss->cleanup)
494 		ss->cleanup(net);
495 
496 	nfnl_err_deliver(&err_list, oskb);
497 	kfree_skb(skb);
498 	module_put(ss->owner);
499 }
500 
501 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
502 	[NFNL_BATCH_GENID]	= { .type = NLA_U32 },
503 };
504 
nfnetlink_rcv_skb_batch(struct sk_buff * skb,struct nlmsghdr * nlh)505 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
506 {
507 	int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
508 	struct nlattr *attr = (void *)nlh + min_len;
509 	struct nlattr *cda[NFNL_BATCH_MAX + 1];
510 	int attrlen = nlh->nlmsg_len - min_len;
511 	struct nfgenmsg *nfgenmsg;
512 	int msglen, err;
513 	u32 gen_id = 0;
514 	u16 res_id;
515 
516 	msglen = NLMSG_ALIGN(nlh->nlmsg_len);
517 	if (msglen > skb->len)
518 		msglen = skb->len;
519 
520 	if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
521 		return;
522 
523 	err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
524 			NULL);
525 	if (err < 0) {
526 		netlink_ack(skb, nlh, err, NULL);
527 		return;
528 	}
529 	if (cda[NFNL_BATCH_GENID])
530 		gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
531 
532 	nfgenmsg = nlmsg_data(nlh);
533 	skb_pull(skb, msglen);
534 	/* Work around old nft using host byte order */
535 	if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
536 		res_id = NFNL_SUBSYS_NFTABLES;
537 	else
538 		res_id = ntohs(nfgenmsg->res_id);
539 
540 	nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
541 }
542 
nfnetlink_rcv(struct sk_buff * skb)543 static void nfnetlink_rcv(struct sk_buff *skb)
544 {
545 	struct nlmsghdr *nlh = nlmsg_hdr(skb);
546 
547 	if (skb->len < NLMSG_HDRLEN ||
548 	    nlh->nlmsg_len < NLMSG_HDRLEN ||
549 	    skb->len < nlh->nlmsg_len)
550 		return;
551 
552 	if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
553 		netlink_ack(skb, nlh, -EPERM, NULL);
554 		return;
555 	}
556 
557 	if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
558 		nfnetlink_rcv_skb_batch(skb, nlh);
559 	else
560 		netlink_rcv_skb(skb, nfnetlink_rcv_msg);
561 }
562 
563 #ifdef CONFIG_MODULES
nfnetlink_bind(struct net * net,int group)564 static int nfnetlink_bind(struct net *net, int group)
565 {
566 	const struct nfnetlink_subsystem *ss;
567 	int type;
568 
569 	if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
570 		return 0;
571 
572 	type = nfnl_group2type[group];
573 
574 	rcu_read_lock();
575 	ss = nfnetlink_get_subsys(type << 8);
576 	rcu_read_unlock();
577 	if (!ss)
578 		request_module("nfnetlink-subsys-%d", type);
579 	return 0;
580 }
581 #endif
582 
nfnetlink_net_init(struct net * net)583 static int __net_init nfnetlink_net_init(struct net *net)
584 {
585 	struct sock *nfnl;
586 	struct netlink_kernel_cfg cfg = {
587 		.groups	= NFNLGRP_MAX,
588 		.input	= nfnetlink_rcv,
589 #ifdef CONFIG_MODULES
590 		.bind	= nfnetlink_bind,
591 #endif
592 	};
593 
594 	nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
595 	if (!nfnl)
596 		return -ENOMEM;
597 	net->nfnl_stash = nfnl;
598 	rcu_assign_pointer(net->nfnl, nfnl);
599 	return 0;
600 }
601 
nfnetlink_net_exit_batch(struct list_head * net_exit_list)602 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
603 {
604 	struct net *net;
605 
606 	list_for_each_entry(net, net_exit_list, exit_list)
607 		RCU_INIT_POINTER(net->nfnl, NULL);
608 	synchronize_net();
609 	list_for_each_entry(net, net_exit_list, exit_list)
610 		netlink_kernel_release(net->nfnl_stash);
611 }
612 
613 static struct pernet_operations nfnetlink_net_ops = {
614 	.init		= nfnetlink_net_init,
615 	.exit_batch	= nfnetlink_net_exit_batch,
616 };
617 
nfnetlink_init(void)618 static int __init nfnetlink_init(void)
619 {
620 	int i;
621 
622 	for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
623 		BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
624 
625 	for (i=0; i<NFNL_SUBSYS_COUNT; i++)
626 		mutex_init(&table[i].mutex);
627 
628 	return register_pernet_subsys(&nfnetlink_net_ops);
629 }
630 
nfnetlink_exit(void)631 static void __exit nfnetlink_exit(void)
632 {
633 	unregister_pernet_subsys(&nfnetlink_net_ops);
634 }
635 module_init(nfnetlink_init);
636 module_exit(nfnetlink_exit);
637