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 done; 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 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 const struct nfnetlink_subsystem *ss2; 468 469 ss2 = nfnl_dereference_protected(subsys_id); 470 if (ss2 == ss) 471 ss->abort(net, oskb); 472 nfnl_err_reset(&err_list); 473 nfnl_unlock(subsys_id); 474 kfree_skb(skb); 475 goto replay; 476 } else if (status == NFNL_BATCH_DONE) { 477 err = ss->commit(net, oskb); 478 if (err == -EAGAIN) { 479 status |= NFNL_BATCH_REPLAY; 480 goto done; 481 } else if (err) { 482 ss->abort(net, oskb); 483 netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL); 484 } 485 } else { 486 ss->abort(net, oskb); 487 } 488 if (ss->cleanup) 489 ss->cleanup(net); 490 491 nfnl_err_deliver(&err_list, oskb); 492 nfnl_unlock(subsys_id); 493 kfree_skb(skb); 494 } 495 496 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = { 497 [NFNL_BATCH_GENID] = { .type = NLA_U32 }, 498 }; 499 500 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) 501 { 502 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); 503 struct nlattr *attr = (void *)nlh + min_len; 504 struct nlattr *cda[NFNL_BATCH_MAX + 1]; 505 int attrlen = nlh->nlmsg_len - min_len; 506 struct nfgenmsg *nfgenmsg; 507 int msglen, err; 508 u32 gen_id = 0; 509 u16 res_id; 510 511 msglen = NLMSG_ALIGN(nlh->nlmsg_len); 512 if (msglen > skb->len) 513 msglen = skb->len; 514 515 if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) 516 return; 517 518 err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy, 519 NULL); 520 if (err < 0) { 521 netlink_ack(skb, nlh, err, NULL); 522 return; 523 } 524 if (cda[NFNL_BATCH_GENID]) 525 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID])); 526 527 nfgenmsg = nlmsg_data(nlh); 528 skb_pull(skb, msglen); 529 /* Work around old nft using host byte order */ 530 if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES) 531 res_id = NFNL_SUBSYS_NFTABLES; 532 else 533 res_id = ntohs(nfgenmsg->res_id); 534 535 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id); 536 } 537 538 static void nfnetlink_rcv(struct sk_buff *skb) 539 { 540 struct nlmsghdr *nlh = nlmsg_hdr(skb); 541 542 if (skb->len < NLMSG_HDRLEN || 543 nlh->nlmsg_len < NLMSG_HDRLEN || 544 skb->len < nlh->nlmsg_len) 545 return; 546 547 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) { 548 netlink_ack(skb, nlh, -EPERM, NULL); 549 return; 550 } 551 552 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) 553 nfnetlink_rcv_skb_batch(skb, nlh); 554 else 555 netlink_rcv_skb(skb, nfnetlink_rcv_msg); 556 } 557 558 #ifdef CONFIG_MODULES 559 static int nfnetlink_bind(struct net *net, int group) 560 { 561 const struct nfnetlink_subsystem *ss; 562 int type; 563 564 if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX) 565 return 0; 566 567 type = nfnl_group2type[group]; 568 569 rcu_read_lock(); 570 ss = nfnetlink_get_subsys(type << 8); 571 rcu_read_unlock(); 572 if (!ss) 573 request_module("nfnetlink-subsys-%d", type); 574 return 0; 575 } 576 #endif 577 578 static int __net_init nfnetlink_net_init(struct net *net) 579 { 580 struct sock *nfnl; 581 struct netlink_kernel_cfg cfg = { 582 .groups = NFNLGRP_MAX, 583 .input = nfnetlink_rcv, 584 #ifdef CONFIG_MODULES 585 .bind = nfnetlink_bind, 586 #endif 587 }; 588 589 nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg); 590 if (!nfnl) 591 return -ENOMEM; 592 net->nfnl_stash = nfnl; 593 rcu_assign_pointer(net->nfnl, nfnl); 594 return 0; 595 } 596 597 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) 598 { 599 struct net *net; 600 601 list_for_each_entry(net, net_exit_list, exit_list) 602 RCU_INIT_POINTER(net->nfnl, NULL); 603 synchronize_net(); 604 list_for_each_entry(net, net_exit_list, exit_list) 605 netlink_kernel_release(net->nfnl_stash); 606 } 607 608 static struct pernet_operations nfnetlink_net_ops = { 609 .init = nfnetlink_net_init, 610 .exit_batch = nfnetlink_net_exit_batch, 611 }; 612 613 static int __init nfnetlink_init(void) 614 { 615 int i; 616 617 for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++) 618 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE); 619 620 for (i=0; i<NFNL_SUBSYS_COUNT; i++) 621 mutex_init(&table[i].mutex); 622 623 return register_pernet_subsys(&nfnetlink_net_ops); 624 } 625 626 static void __exit nfnetlink_exit(void) 627 { 628 unregister_pernet_subsys(&nfnetlink_net_ops); 629 } 630 module_init(nfnetlink_init); 631 module_exit(nfnetlink_exit); 632