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