FILE
nf_conntrack_netlink.c
File preview with syntax highlighting.
linux-6.19.14/net/netfilter/nf_conntrack_netlink.c
1/* Connection tracking 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-2006 by Harald Welte <laforge@gnumonks.org> 6 * (C) 2003 by Patrick Mchardy <kaber@trash.net> 7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org> 8 * 9 * Initial connection tracking via netlink development funded and 10 * generally made possible by Network Robots, Inc. (www.networkrobots.com) 11 * 12 * Further development of this code funded by Astaro AG (http://www.astaro.com) 13 * 14 * This software may be used and distributed according to the terms 15 * of the GNU General Public License, incorporated herein by reference. 16 */ 17 18#include <linux/init.h> 19#include <linux/module.h> 20#include <linux/kernel.h> 21#include <linux/rculist.h> 22#include <linux/rculist_nulls.h> 23#include <linux/types.h> 24#include <linux/timer.h> 25#include <linux/security.h> 26#include <linux/skbuff.h> 27#include <linux/errno.h> 28#include <linux/netlink.h> 29#include <linux/spinlock.h> 30#include <linux/interrupt.h> 31#include <linux/slab.h> 32#include <linux/siphash.h> 33 34#include <linux/netfilter.h> 35#include <net/netlink.h> 36#include <net/sock.h> 37#include <net/netfilter/nf_conntrack.h> 38#include <net/netfilter/nf_conntrack_core.h> 39#include <net/netfilter/nf_conntrack_expect.h> 40#include <net/netfilter/nf_conntrack_helper.h> 41#include <net/netfilter/nf_conntrack_seqadj.h> 42#include <net/netfilter/nf_conntrack_l4proto.h> 43#include <net/netfilter/nf_conntrack_tuple.h> 44#include <net/netfilter/nf_conntrack_acct.h> 45#include <net/netfilter/nf_conntrack_zones.h> 46#include <net/netfilter/nf_conntrack_timestamp.h> 47#include <net/netfilter/nf_conntrack_labels.h> 48#include <net/netfilter/nf_conntrack_synproxy.h> 49#if IS_ENABLED(CONFIG_NF_NAT) 50#include <net/netfilter/nf_nat.h> 51#include <net/netfilter/nf_nat_helper.h> 52#endif 53 54#include <linux/netfilter/nfnetlink.h> 55#include <linux/netfilter/nfnetlink_conntrack.h> 56 57#include "nf_internals.h" 58 59MODULE_LICENSE("GPL"); 60MODULE_DESCRIPTION("List and change connection tracking table"); 61 62struct ctnetlink_list_dump_ctx { 63 unsigned long last_id; 64 unsigned int cpu; 65 bool done; 66}; 67 68static int ctnetlink_dump_tuples_proto(struct sk_buff *skb, 69 const struct nf_conntrack_tuple *tuple, 70 const struct nf_conntrack_l4proto *l4proto) 71{ 72 int ret = 0; 73 struct nlattr *nest_parms; 74 75 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO); 76 if (!nest_parms) 77 goto nla_put_failure; 78 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum)) 79 goto nla_put_failure; 80 81 if (likely(l4proto->tuple_to_nlattr)) 82 ret = l4proto->tuple_to_nlattr(skb, tuple); 83 84 nla_nest_end(skb, nest_parms); 85 86 return ret; 87 88nla_put_failure: 89 return -1; 90} 91 92static int ipv4_tuple_to_nlattr(struct sk_buff *skb, 93 const struct nf_conntrack_tuple *tuple) 94{ 95 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) || 96 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip)) 97 return -EMSGSIZE; 98 return 0; 99} 100 101static int ipv6_tuple_to_nlattr(struct sk_buff *skb, 102 const struct nf_conntrack_tuple *tuple) 103{ 104 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) || 105 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6)) 106 return -EMSGSIZE; 107 return 0; 108} 109 110static int ctnetlink_dump_tuples_ip(struct sk_buff *skb, 111 const struct nf_conntrack_tuple *tuple) 112{ 113 int ret = 0; 114 struct nlattr *nest_parms; 115 116 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP); 117 if (!nest_parms) 118 goto nla_put_failure; 119 120 switch (tuple->src.l3num) { 121 case NFPROTO_IPV4: 122 ret = ipv4_tuple_to_nlattr(skb, tuple); 123 break; 124 case NFPROTO_IPV6: 125 ret = ipv6_tuple_to_nlattr(skb, tuple); 126 break; 127 } 128 129 nla_nest_end(skb, nest_parms); 130 131 return ret; 132 133nla_put_failure: 134 return -1; 135} 136 137static int ctnetlink_dump_tuples(struct sk_buff *skb, 138 const struct nf_conntrack_tuple *tuple) 139{ 140 const struct nf_conntrack_l4proto *l4proto; 141 int ret; 142 143 rcu_read_lock(); 144 ret = ctnetlink_dump_tuples_ip(skb, tuple); 145 146 if (ret >= 0) { 147 l4proto = nf_ct_l4proto_find(tuple->dst.protonum); 148 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); 149 } 150 rcu_read_unlock(); 151 return ret; 152} 153 154static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype, 155 const struct nf_conntrack_zone *zone, int dir) 156{ 157 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir) 158 return 0; 159 if (nla_put_be16(skb, attrtype, htons(zone->id))) 160 goto nla_put_failure; 161 return 0; 162 163nla_put_failure: 164 return -1; 165} 166 167static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) 168{ 169 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status))) 170 goto nla_put_failure; 171 return 0; 172 173nla_put_failure: 174 return -1; 175} 176 177static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct, 178 bool skip_zero) 179{ 180 long timeout; 181 182 if (nf_ct_is_confirmed(ct)) 183 timeout = nf_ct_expires(ct) / HZ; 184 else 185 timeout = ct->timeout / HZ; 186 187 if (skip_zero && timeout == 0) 188 return 0; 189 190 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout))) 191 goto nla_put_failure; 192 return 0; 193 194nla_put_failure: 195 return -1; 196} 197 198static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct, 199 bool destroy) 200{ 201 const struct nf_conntrack_l4proto *l4proto; 202 struct nlattr *nest_proto; 203 int ret; 204 205 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); 206 if (!l4proto->to_nlattr) 207 return 0; 208 209 nest_proto = nla_nest_start(skb, CTA_PROTOINFO); 210 if (!nest_proto) 211 goto nla_put_failure; 212 213 ret = l4proto->to_nlattr(skb, nest_proto, ct, destroy); 214 215 nla_nest_end(skb, nest_proto); 216 217 return ret; 218 219nla_put_failure: 220 return -1; 221} 222 223static int ctnetlink_dump_helpinfo(struct sk_buff *skb, 224 const struct nf_conn *ct) 225{ 226 struct nlattr *nest_helper; 227 const struct nf_conn_help *help = nfct_help(ct); 228 struct nf_conntrack_helper *helper; 229 230 if (!help) 231 return 0; 232 233 rcu_read_lock(); 234 helper = rcu_dereference(help->helper); 235 if (!helper) 236 goto out; 237 238 nest_helper = nla_nest_start(skb, CTA_HELP); 239 if (!nest_helper) 240 goto nla_put_failure; 241 if (nla_put_string(skb, CTA_HELP_NAME, helper->name)) 242 goto nla_put_failure; 243 244 if (helper->to_nlattr) 245 helper->to_nlattr(skb, ct); 246 247 nla_nest_end(skb, nest_helper); 248out: 249 rcu_read_unlock(); 250 return 0; 251 252nla_put_failure: 253 rcu_read_unlock(); 254 return -1; 255} 256 257static int 258dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct, 259 enum ip_conntrack_dir dir, int type) 260{ 261 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; 262 struct nf_conn_counter *counter = acct->counter; 263 struct nlattr *nest_count; 264 u64 pkts, bytes; 265 266 if (type == IPCTNL_MSG_CT_GET_CTRZERO) { 267 pkts = atomic64_xchg(&counter[dir].packets, 0); 268 bytes = atomic64_xchg(&counter[dir].bytes, 0); 269 } else { 270 pkts = atomic64_read(&counter[dir].packets); 271 bytes = atomic64_read(&counter[dir].bytes); 272 } 273 274 nest_count = nla_nest_start(skb, attr); 275 if (!nest_count) 276 goto nla_put_failure; 277 278 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts), 279 CTA_COUNTERS_PAD) || 280 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes), 281 CTA_COUNTERS_PAD)) 282 goto nla_put_failure; 283 284 nla_nest_end(skb, nest_count); 285 286 return 0; 287 288nla_put_failure: 289 return -1; 290} 291 292static int 293ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type) 294{ 295 struct nf_conn_acct *acct = nf_conn_acct_find(ct); 296 297 if (!acct) 298 return 0; 299 300 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0) 301 return -1; 302 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0) 303 return -1; 304 305 return 0; 306} 307 308static int 309ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct) 310{ 311 struct nlattr *nest_count; 312 const struct nf_conn_tstamp *tstamp; 313 314 tstamp = nf_conn_tstamp_find(ct); 315 if (!tstamp) 316 return 0; 317 318 nest_count = nla_nest_start(skb, CTA_TIMESTAMP); 319 if (!nest_count) 320 goto nla_put_failure; 321 322 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start), 323 CTA_TIMESTAMP_PAD) || 324 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP, 325 cpu_to_be64(tstamp->stop), 326 CTA_TIMESTAMP_PAD))) 327 goto nla_put_failure; 328 nla_nest_end(skb, nest_count); 329 330 return 0; 331 332nla_put_failure: 333 return -1; 334} 335 336#ifdef CONFIG_NF_CONNTRACK_MARK 337static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct, 338 bool dump) 339{ 340 u32 mark = READ_ONCE(ct->mark); 341 342 if (!mark && !dump) 343 return 0; 344 345 if (nla_put_be32(skb, CTA_MARK, htonl(mark))) 346 goto nla_put_failure; 347 return 0; 348 349nla_put_failure: 350 return -1; 351} 352#else 353#define ctnetlink_dump_mark(a, b, c) (0) 354#endif 355 356#ifdef CONFIG_NF_CONNTRACK_SECMARK 357static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct) 358{ 359 struct nlattr *nest_secctx; 360 struct lsm_context ctx; 361 int ret; 362 363 ret = security_secid_to_secctx(ct->secmark, &ctx); 364 if (ret < 0) 365 return 0; 366 367 ret = -1; 368 nest_secctx = nla_nest_start(skb, CTA_SECCTX); 369 if (!nest_secctx) 370 goto nla_put_failure; 371 372 if (nla_put_string(skb, CTA_SECCTX_NAME, ctx.context)) 373 goto nla_put_failure; 374 nla_nest_end(skb, nest_secctx); 375 376 ret = 0; 377nla_put_failure: 378 security_release_secctx(&ctx); 379 return ret; 380} 381#else 382#define ctnetlink_dump_secctx(a, b) (0) 383#endif 384 385#ifdef CONFIG_NF_CONNTRACK_EVENTS 386static int 387ctnetlink_dump_event_timestamp(struct sk_buff *skb, const struct nf_conn *ct) 388{ 389#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP 390 const struct nf_conntrack_ecache *e = nf_ct_ecache_find(ct); 391 392 if (e) { 393 u64 ts = local64_read(&e->timestamp); 394 395 if (ts) 396 return nla_put_be64(skb, CTA_TIMESTAMP_EVENT, 397 cpu_to_be64(ts), CTA_TIMESTAMP_PAD); 398 } 399#endif 400 return 0; 401} 402 403static inline int ctnetlink_label_size(const struct nf_conn *ct) 404{ 405 struct nf_conn_labels *labels = nf_ct_labels_find(ct); 406 407 if (!labels) 408 return 0; 409 return nla_total_size(sizeof(labels->bits)); 410} 411#endif 412 413static int 414ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct) 415{ 416 struct nf_conn_labels *labels = nf_ct_labels_find(ct); 417 unsigned int i; 418 419 if (!labels) 420 return 0; 421 422 i = 0; 423 do { 424 if (labels->bits[i] != 0) 425 return nla_put(skb, CTA_LABELS, sizeof(labels->bits), 426 labels->bits); 427 i++; 428 } while (i < ARRAY_SIZE(labels->bits)); 429 430 return 0; 431} 432 433#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) 434 435static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) 436{ 437 struct nlattr *nest_parms; 438 439 if (!(ct->status & IPS_EXPECTED)) 440 return 0; 441 442 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER); 443 if (!nest_parms) 444 goto nla_put_failure; 445 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) 446 goto nla_put_failure; 447 nla_nest_end(skb, nest_parms); 448 449 return 0; 450 451nla_put_failure: 452 return -1; 453} 454 455static int 456dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type) 457{ 458 struct nlattr *nest_parms; 459 460 nest_parms = nla_nest_start(skb, type); 461 if (!nest_parms) 462 goto nla_put_failure; 463 464 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS, 465 htonl(seq->correction_pos)) || 466 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE, 467 htonl(seq->offset_before)) || 468 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER, 469 htonl(seq->offset_after))) 470 goto nla_put_failure; 471 472 nla_nest_end(skb, nest_parms); 473 474 return 0; 475 476nla_put_failure: 477 return -1; 478} 479 480static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct) 481{ 482 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); 483 struct nf_ct_seqadj *seq; 484 485 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj) 486 return 0; 487 488 spin_lock_bh(&ct->lock); 489 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL]; 490 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1) 491 goto err; 492 493 seq = &seqadj->seq[IP_CT_DIR_REPLY]; 494 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1) 495 goto err; 496 497 spin_unlock_bh(&ct->lock); 498 return 0; 499err: 500 spin_unlock_bh(&ct->lock); 501 return -1; 502} 503 504static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct) 505{ 506 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct); 507 struct nlattr *nest_parms; 508 509 if (!synproxy) 510 return 0; 511 512 nest_parms = nla_nest_start(skb, CTA_SYNPROXY); 513 if (!nest_parms) 514 goto nla_put_failure; 515 516 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) || 517 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) || 518 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff))) 519 goto nla_put_failure; 520 521 nla_nest_end(skb, nest_parms); 522 523 return 0; 524 525nla_put_failure: 526 return -1; 527} 528 529static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) 530{ 531 __be32 id = (__force __be32)nf_ct_get_id(ct); 532 533 if (nla_put_be32(skb, CTA_ID, id)) 534 goto nla_put_failure; 535 return 0; 536 537nla_put_failure: 538 return -1; 539} 540 541static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) 542{ 543 if (nla_put_be32(skb, CTA_USE, htonl(refcount_read(&ct->ct_general.use)))) 544 goto nla_put_failure; 545 return 0; 546 547nla_put_failure: 548 return -1; 549} 550 551/* all these functions access ct->ext. Caller must either hold a reference 552 * on ct or prevent its deletion by holding either the bucket spinlock or 553 * pcpu dying list lock. 554 */ 555static int ctnetlink_dump_extinfo(struct sk_buff *skb, 556 struct nf_conn *ct, u32 type) 557{ 558 if (ctnetlink_dump_acct(skb, ct, type) < 0 || 559 ctnetlink_dump_timestamp(skb, ct) < 0 || 560 ctnetlink_dump_helpinfo(skb, ct) < 0 || 561 ctnetlink_dump_labels(skb, ct) < 0 || 562 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 || 563 ctnetlink_dump_ct_synproxy(skb, ct) < 0) 564 return -1; 565 566 return 0; 567} 568 569static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct) 570{ 571 if (ctnetlink_dump_status(skb, ct) < 0 || 572 ctnetlink_dump_mark(skb, ct, true) < 0 || 573 ctnetlink_dump_secctx(skb, ct) < 0 || 574 ctnetlink_dump_id(skb, ct) < 0 || 575 ctnetlink_dump_use(skb, ct) < 0 || 576 ctnetlink_dump_master(skb, ct) < 0) 577 return -1; 578 579 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) && 580 (ctnetlink_dump_timeout(skb, ct, false) < 0 || 581 ctnetlink_dump_protoinfo(skb, ct, false) < 0)) 582 return -1; 583 584 return 0; 585} 586 587static int 588ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, 589 struct nf_conn *ct, bool extinfo, unsigned int flags) 590{ 591 const struct nf_conntrack_zone *zone; 592 struct nlmsghdr *nlh; 593 struct nlattr *nest_parms; 594 unsigned int event; 595 596 if (portid) 597 flags |= NLM_F_MULTI; 598 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW); 599 nlh = nfnl_msg_put(skb, portid, seq, event, flags, nf_ct_l3num(ct), 600 NFNETLINK_V0, 0); 601 if (!nlh) 602 goto nlmsg_failure; 603 604 zone = nf_ct_zone(ct); 605 606 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG); 607 if (!nest_parms) 608 goto nla_put_failure; 609 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) 610 goto nla_put_failure; 611 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 612 NF_CT_ZONE_DIR_ORIG) < 0) 613 goto nla_put_failure; 614 nla_nest_end(skb, nest_parms); 615 616 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY); 617 if (!nest_parms) 618 goto nla_put_failure; 619 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) 620 goto nla_put_failure; 621 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 622 NF_CT_ZONE_DIR_REPL) < 0) 623 goto nla_put_failure; 624 nla_nest_end(skb, nest_parms); 625 626 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone, 627 NF_CT_DEFAULT_ZONE_DIR) < 0) 628 goto nla_put_failure; 629 630 if (ctnetlink_dump_info(skb, ct) < 0) 631 goto nla_put_failure; 632 if (extinfo && ctnetlink_dump_extinfo(skb, ct, type) < 0) 633 goto nla_put_failure; 634 635 nlmsg_end(skb, nlh); 636 return skb->len; 637 638nlmsg_failure: 639nla_put_failure: 640 nlmsg_cancel(skb, nlh); 641 return -1; 642} 643 644static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = { 645 [CTA_IP_V4_SRC] = { .type = NLA_U32 }, 646 [CTA_IP_V4_DST] = { .type = NLA_U32 }, 647 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 }, 648 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 }, 649}; 650 651#if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS) 652static size_t ctnetlink_proto_size(const struct nf_conn *ct) 653{ 654 const struct nf_conntrack_l4proto *l4proto; 655 size_t len, len4 = 0; 656 657 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1); 658 len *= 3u; /* ORIG, REPLY, MASTER */ 659 660 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); 661 len += l4proto->nlattr_size; 662 if (l4proto->nlattr_tuple_size) { 663 len4 = l4proto->nlattr_tuple_size(); 664 len4 *= 3u; /* ORIG, REPLY, MASTER */ 665 } 666 667 return len + len4; 668} 669 670static inline size_t ctnetlink_acct_size(const struct nf_conn *ct) 671{ 672 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) 673 return 0; 674 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ 675 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ 676 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ 677 ; 678} 679 680static inline int ctnetlink_secctx_size(const struct nf_conn *ct) 681{ 682#ifdef CONFIG_NF_CONNTRACK_SECMARK 683 int ret; 684 685 ret = security_secid_to_secctx(ct->secmark, NULL); 686 if (ret < 0) 687 return 0; 688 689 return nla_total_size(0) /* CTA_SECCTX */ 690 + nla_total_size(sizeof(char) * ret); /* CTA_SECCTX_NAME */ 691#else 692 return 0; 693#endif 694} 695 696static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct) 697{ 698#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP 699 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) 700 return 0; 701 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t)); 702#else 703 return 0; 704#endif 705} 706#endif 707 708#ifdef CONFIG_NF_CONNTRACK_EVENTS 709static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct) 710{ 711 return NLMSG_ALIGN(sizeof(struct nfgenmsg)) 712 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ 713 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ 714 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ 715 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ 716 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ 717 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ 718 + ctnetlink_acct_size(ct) 719 + ctnetlink_timestamp_size(ct) 720 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ 721 + nla_total_size(0) /* CTA_PROTOINFO */ 722 + nla_total_size(0) /* CTA_HELP */ 723 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ 724 + ctnetlink_secctx_size(ct) 725#if IS_ENABLED(CONFIG_NF_NAT) 726 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ 727 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ 728#endif 729#ifdef CONFIG_NF_CONNTRACK_MARK 730 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ 731#endif 732#ifdef CONFIG_NF_CONNTRACK_ZONES 733 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */ 734#endif 735 + ctnetlink_proto_size(ct) 736 + ctnetlink_label_size(ct) 737#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP 738 + nla_total_size(sizeof(u64)) /* CTA_TIMESTAMP_EVENT */ 739#endif 740 ; 741} 742 743static int 744ctnetlink_conntrack_event(unsigned int events, const struct nf_ct_event *item) 745{ 746 const struct nf_conntrack_zone *zone; 747 struct net *net; 748 struct nlmsghdr *nlh; 749 struct nlattr *nest_parms; 750 struct nf_conn *ct = item->ct; 751 struct sk_buff *skb; 752 unsigned int type; 753 unsigned int flags = 0, group; 754 int err; 755 756 if (events & (1 << IPCT_DESTROY)) { 757 type = IPCTNL_MSG_CT_DELETE; 758 group = NFNLGRP_CONNTRACK_DESTROY; 759 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { 760 type = IPCTNL_MSG_CT_NEW; 761 flags = NLM_F_CREATE|NLM_F_EXCL; 762 group = NFNLGRP_CONNTRACK_NEW; 763 } else if (events) { 764 type = IPCTNL_MSG_CT_NEW; 765 group = NFNLGRP_CONNTRACK_UPDATE; 766 } else 767 return 0; 768 769 net = nf_ct_net(ct); 770 if (!item->report && !nfnetlink_has_listeners(net, group)) 771 return 0; 772 773 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); 774 if (skb == NULL) 775 goto errout; 776 777 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type); 778 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, nf_ct_l3num(ct), 779 NFNETLINK_V0, 0); 780 if (!nlh) 781 goto nlmsg_failure; 782 783 zone = nf_ct_zone(ct); 784 785 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG); 786 if (!nest_parms) 787 goto nla_put_failure; 788 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) 789 goto nla_put_failure; 790 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 791 NF_CT_ZONE_DIR_ORIG) < 0) 792 goto nla_put_failure; 793 nla_nest_end(skb, nest_parms); 794 795 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY); 796 if (!nest_parms) 797 goto nla_put_failure; 798 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) 799 goto nla_put_failure; 800 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 801 NF_CT_ZONE_DIR_REPL) < 0) 802 goto nla_put_failure; 803 nla_nest_end(skb, nest_parms); 804 805 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone, 806 NF_CT_DEFAULT_ZONE_DIR) < 0) 807 goto nla_put_failure; 808 809 if (ctnetlink_dump_id(skb, ct) < 0) 810 goto nla_put_failure; 811 812 if (ctnetlink_dump_status(skb, ct) < 0) 813 goto nla_put_failure; 814 815 if (events & (1 << IPCT_DESTROY)) { 816 if (ctnetlink_dump_timeout(skb, ct, true) < 0) 817 goto nla_put_failure; 818 819 if (ctnetlink_dump_acct(skb, ct, type) < 0 || 820 ctnetlink_dump_timestamp(skb, ct) < 0 || 821 ctnetlink_dump_protoinfo(skb, ct, true) < 0) 822 goto nla_put_failure; 823 } else { 824 if (ctnetlink_dump_timeout(skb, ct, false) < 0) 825 goto nla_put_failure; 826 827 if (events & (1 << IPCT_PROTOINFO) && 828 ctnetlink_dump_protoinfo(skb, ct, false) < 0) 829 goto nla_put_failure; 830 831 if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) 832 && ctnetlink_dump_helpinfo(skb, ct) < 0) 833 goto nla_put_failure; 834 835#ifdef CONFIG_NF_CONNTRACK_SECMARK 836 if ((events & (1 << IPCT_SECMARK) || ct->secmark) 837 && ctnetlink_dump_secctx(skb, ct) < 0) 838 goto nla_put_failure; 839#endif 840 if (events & (1 << IPCT_LABEL) && 841 ctnetlink_dump_labels(skb, ct) < 0) 842 goto nla_put_failure; 843 844 if (events & (1 << IPCT_RELATED) && 845 ctnetlink_dump_master(skb, ct) < 0) 846 goto nla_put_failure; 847 848 if (events & (1 << IPCT_SEQADJ) && 849 ctnetlink_dump_ct_seq_adj(skb, ct) < 0) 850 goto nla_put_failure; 851 852 if (events & (1 << IPCT_SYNPROXY) && 853 ctnetlink_dump_ct_synproxy(skb, ct) < 0) 854 goto nla_put_failure; 855 } 856 857#ifdef CONFIG_NF_CONNTRACK_MARK 858 if (ctnetlink_dump_mark(skb, ct, events & (1 << IPCT_MARK))) 859 goto nla_put_failure; 860#endif 861 862 if (ctnetlink_dump_event_timestamp(skb, ct)) 863 goto nla_put_failure; 864 865 nlmsg_end(skb, nlh); 866 err = nfnetlink_send(skb, net, item->portid, group, item->report, 867 GFP_ATOMIC); 868 if (err == -ENOBUFS || err == -EAGAIN) 869 return -ENOBUFS; 870 871 return 0; 872 873nla_put_failure: 874 nlmsg_cancel(skb, nlh); 875nlmsg_failure: 876 kfree_skb(skb); 877errout: 878 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0) 879 return -ENOBUFS; 880 881 return 0; 882} 883#endif /* CONFIG_NF_CONNTRACK_EVENTS */ 884 885static int ctnetlink_done(struct netlink_callback *cb) 886{ 887 kfree(cb->data); 888 return 0; 889} 890 891struct ctnetlink_filter_u32 { 892 u32 val; 893 u32 mask; 894}; 895 896struct ctnetlink_filter { 897 u8 family; 898 bool zone_filter; 899 900 u_int32_t orig_flags; 901 u_int32_t reply_flags; 902 903 struct nf_conntrack_tuple orig; 904 struct nf_conntrack_tuple reply; 905 struct nf_conntrack_zone zone; 906 907 struct ctnetlink_filter_u32 mark; 908 struct ctnetlink_filter_u32 status; 909}; 910 911static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX + 1] = { 912 [CTA_FILTER_ORIG_FLAGS] = NLA_POLICY_MASK(NLA_U32, CTA_FILTER_F_ALL), 913 [CTA_FILTER_REPLY_FLAGS] = NLA_POLICY_MASK(NLA_U32, CTA_FILTER_F_ALL), 914}; 915 916static int ctnetlink_parse_filter(const struct nlattr *attr, 917 struct ctnetlink_filter *filter) 918{ 919 struct nlattr *tb[CTA_FILTER_MAX + 1]; 920 int ret = 0; 921 922 ret = nla_parse_nested(tb, CTA_FILTER_MAX, attr, cta_filter_nla_policy, 923 NULL); 924 if (ret) 925 return ret; 926 927 if (tb[CTA_FILTER_ORIG_FLAGS]) 928 filter->orig_flags = nla_get_u32(tb[CTA_FILTER_ORIG_FLAGS]); 929 930 if (tb[CTA_FILTER_REPLY_FLAGS]) 931 filter->reply_flags = nla_get_u32(tb[CTA_FILTER_REPLY_FLAGS]); 932 933 return 0; 934} 935 936static int ctnetlink_parse_zone(const struct nlattr *attr, 937 struct nf_conntrack_zone *zone); 938static int ctnetlink_parse_tuple_filter(const struct nlattr * const cda[], 939 struct nf_conntrack_tuple *tuple, 940 u32 type, u_int8_t l3num, 941 struct nf_conntrack_zone *zone, 942 u_int32_t flags); 943 944static int ctnetlink_filter_parse_mark(struct ctnetlink_filter_u32 *mark, 945 const struct nlattr * const cda[]) 946{ 947#ifdef CONFIG_NF_CONNTRACK_MARK 948 if (cda[CTA_MARK]) { 949 mark->val = ntohl(nla_get_be32(cda[CTA_MARK])); 950 951 if (cda[CTA_MARK_MASK]) 952 mark->mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK])); 953 else 954 mark->mask = 0xffffffff; 955 } else if (cda[CTA_MARK_MASK]) { 956 return -EINVAL; 957 } 958#endif 959 return 0; 960} 961 962static int ctnetlink_filter_parse_status(struct ctnetlink_filter_u32 *status, 963 const struct nlattr * const cda[]) 964{ 965 if (cda[CTA_STATUS]) { 966 status->val = ntohl(nla_get_be32(cda[CTA_STATUS])); 967 if (cda[CTA_STATUS_MASK]) 968 status->mask = ntohl(nla_get_be32(cda[CTA_STATUS_MASK])); 969 else 970 status->mask = status->val; 971 972 /* status->val == 0? always true, else always false. */ 973 if (status->mask == 0) 974 return -EINVAL; 975 } else if (cda[CTA_STATUS_MASK]) { 976 return -EINVAL; 977 } 978 979 /* CTA_STATUS is NLA_U32, if this fires UAPI needs to be extended */ 980 BUILD_BUG_ON(__IPS_MAX_BIT >= 32); 981 return 0; 982} 983 984static struct ctnetlink_filter * 985ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family) 986{ 987 struct ctnetlink_filter *filter; 988 int err; 989 990#ifndef CONFIG_NF_CONNTRACK_MARK 991 if (cda[CTA_MARK] || cda[CTA_MARK_MASK]) 992 return ERR_PTR(-EOPNOTSUPP); 993#endif 994 995 filter = kzalloc(sizeof(*filter), GFP_KERNEL); 996 if (filter == NULL) 997 return ERR_PTR(-ENOMEM); 998 999 filter->family = family; 1000 1001 err = ctnetlink_filter_parse_mark(&filter->mark, cda); 1002 if (err) 1003 goto err_filter; 1004 1005 err = ctnetlink_filter_parse_status(&filter->status, cda); 1006 if (err) 1007 goto err_filter; 1008 1009 if (cda[CTA_ZONE]) { 1010 err = ctnetlink_parse_zone(cda[CTA_ZONE], &filter->zone); 1011 if (err < 0) 1012 goto err_filter; 1013 filter->zone_filter = true; 1014 } 1015 1016 if (!cda[CTA_FILTER]) 1017 return filter; 1018 1019 err = ctnetlink_parse_filter(cda[CTA_FILTER], filter); 1020 if (err < 0) 1021 goto err_filter; 1022 1023 if (filter->orig_flags) { 1024 if (!cda[CTA_TUPLE_ORIG]) { 1025 err = -EINVAL; 1026 goto err_filter; 1027 } 1028 1029 err = ctnetlink_parse_tuple_filter(cda, &filter->orig, 1030 CTA_TUPLE_ORIG, 1031 filter->family, 1032 &filter->zone, 1033 filter->orig_flags); 1034 if (err < 0) 1035 goto err_filter; 1036 } 1037 1038 if (filter->reply_flags) { 1039 if (!cda[CTA_TUPLE_REPLY]) { 1040 err = -EINVAL; 1041 goto err_filter; 1042 } 1043 1044 err = ctnetlink_parse_tuple_filter(cda, &filter->reply, 1045 CTA_TUPLE_REPLY, 1046 filter->family, 1047 &filter->zone, 1048 filter->reply_flags); 1049 if (err < 0) 1050 goto err_filter; 1051 } 1052 1053 return filter; 1054 1055err_filter: 1056 kfree(filter); 1057 1058 return ERR_PTR(err); 1059} 1060 1061static bool ctnetlink_needs_filter(u8 family, const struct nlattr * const *cda) 1062{ 1063 return family || cda[CTA_MARK] || cda[CTA_FILTER] || cda[CTA_STATUS] || cda[CTA_ZONE]; 1064} 1065 1066static int ctnetlink_start(struct netlink_callback *cb) 1067{ 1068 const struct nlattr * const *cda = cb->data; 1069 struct ctnetlink_filter *filter = NULL; 1070 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); 1071 u8 family = nfmsg->nfgen_family; 1072 1073 if (ctnetlink_needs_filter(family, cda)) { 1074 filter = ctnetlink_alloc_filter(cda, family); 1075 if (IS_ERR(filter)) 1076 return PTR_ERR(filter); 1077 } 1078 1079 cb->data = filter; 1080 return 0; 1081} 1082 1083static int ctnetlink_filter_match_tuple(struct nf_conntrack_tuple *filter_tuple, 1084 struct nf_conntrack_tuple *ct_tuple, 1085 u_int32_t flags, int family) 1086{ 1087 switch (family) { 1088 case NFPROTO_IPV4: 1089 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) && 1090 filter_tuple->src.u3.ip != ct_tuple->src.u3.ip) 1091 return 0; 1092 1093 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) && 1094 filter_tuple->dst.u3.ip != ct_tuple->dst.u3.ip) 1095 return 0; 1096 break; 1097 case NFPROTO_IPV6: 1098 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) && 1099 !ipv6_addr_cmp(&filter_tuple->src.u3.in6, 1100 &ct_tuple->src.u3.in6)) 1101 return 0; 1102 1103 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) && 1104 !ipv6_addr_cmp(&filter_tuple->dst.u3.in6, 1105 &ct_tuple->dst.u3.in6)) 1106 return 0; 1107 break; 1108 } 1109 1110 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) && 1111 filter_tuple->dst.protonum != ct_tuple->dst.protonum) 1112 return 0; 1113 1114 switch (ct_tuple->dst.protonum) { 1115 case IPPROTO_TCP: 1116 case IPPROTO_UDP: 1117 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) && 1118 filter_tuple->src.u.tcp.port != ct_tuple->src.u.tcp.port) 1119 return 0; 1120 1121 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) && 1122 filter_tuple->dst.u.tcp.port != ct_tuple->dst.u.tcp.port) 1123 return 0; 1124 break; 1125 case IPPROTO_ICMP: 1126 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE)) && 1127 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type) 1128 return 0; 1129 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE)) && 1130 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code) 1131 return 0; 1132 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID)) && 1133 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id) 1134 return 0; 1135 break; 1136 case IPPROTO_ICMPV6: 1137 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE)) && 1138 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type) 1139 return 0; 1140 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE)) && 1141 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code) 1142 return 0; 1143 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID)) && 1144 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id) 1145 return 0; 1146 break; 1147 } 1148 1149 return 1; 1150} 1151 1152static int ctnetlink_filter_match(struct nf_conn *ct, void *data) 1153{ 1154 struct ctnetlink_filter *filter = data; 1155 struct nf_conntrack_tuple *tuple; 1156 u32 status; 1157 1158 if (filter == NULL) 1159 goto out; 1160 1161 /* Match entries of a given L3 protocol number. 1162 * If it is not specified, ie. l3proto == 0, 1163 * then match everything. 1164 */ 1165 if (filter->family && nf_ct_l3num(ct) != filter->family) 1166 goto ignore_entry; 1167 1168 if (filter->zone_filter && 1169 !nf_ct_zone_equal_any(ct, &filter->zone)) 1170 goto ignore_entry; 1171 1172 if (filter->orig_flags) { 1173 tuple = nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL); 1174 if (!ctnetlink_filter_match_tuple(&filter->orig, tuple, 1175 filter->orig_flags, 1176 filter->family)) 1177 goto ignore_entry; 1178 } 1179 1180 if (filter->reply_flags) { 1181 tuple = nf_ct_tuple(ct, IP_CT_DIR_REPLY); 1182 if (!ctnetlink_filter_match_tuple(&filter->reply, tuple, 1183 filter->reply_flags, 1184 filter->family)) 1185 goto ignore_entry; 1186 } 1187 1188#ifdef CONFIG_NF_CONNTRACK_MARK 1189 if ((READ_ONCE(ct->mark) & filter->mark.mask) != filter->mark.val) 1190 goto ignore_entry; 1191#endif 1192 status = (u32)READ_ONCE(ct->status); 1193 if ((status & filter->status.mask) != filter->status.val) 1194 goto ignore_entry; 1195 1196out: 1197 return 1; 1198 1199ignore_entry: 1200 return 0; 1201} 1202 1203static unsigned long ctnetlink_get_id(const struct nf_conn *ct) 1204{ 1205 unsigned long id = nf_ct_get_id(ct); 1206 1207 return id ? id : 1; 1208} 1209 1210static int 1211ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) 1212{ 1213 unsigned int flags = cb->data ? NLM_F_DUMP_FILTERED : 0; 1214 struct net *net = sock_net(skb->sk); 1215 unsigned long last_id = cb->args[1]; 1216 struct nf_conntrack_tuple_hash *h; 1217 struct hlist_nulls_node *n; 1218 struct nf_conn *nf_ct_evict[8]; 1219 struct nf_conn *ct; 1220 int res, i; 1221 spinlock_t *lockp; 1222 1223 i = 0; 1224 1225 local_bh_disable(); 1226 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { 1227restart: 1228 while (i) { 1229 i--; 1230 if (nf_ct_should_gc(nf_ct_evict[i])) 1231 nf_ct_kill(nf_ct_evict[i]); 1232 nf_ct_put(nf_ct_evict[i]); 1233 } 1234 1235 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS]; 1236 nf_conntrack_lock(lockp); 1237 if (cb->args[0] >= nf_conntrack_htable_size) { 1238 spin_unlock(lockp); 1239 goto out; 1240 } 1241 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]], 1242 hnnode) { 1243 ct = nf_ct_tuplehash_to_ctrack(h); 1244 if (nf_ct_is_expired(ct)) { 1245 /* need to defer nf_ct_kill() until lock is released */ 1246 if (i < ARRAY_SIZE(nf_ct_evict) && 1247 refcount_inc_not_zero(&ct->ct_general.use)) 1248 nf_ct_evict[i++] = ct; 1249 continue; 1250 } 1251 1252 if (!net_eq(net, nf_ct_net(ct))) 1253 continue; 1254 1255 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) 1256 continue; 1257 1258 if (cb->args[1]) { 1259 if (ctnetlink_get_id(ct) != last_id) 1260 continue; 1261 cb->args[1] = 0; 1262 } 1263 if (!ctnetlink_filter_match(ct, cb->data)) 1264 continue; 1265 1266 res = 1267 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, 1268 cb->nlh->nlmsg_seq, 1269 NFNL_MSG_TYPE(cb->nlh->nlmsg_type), 1270 ct, true, flags); 1271 if (res < 0) { 1272 cb->args[1] = ctnetlink_get_id(ct); 1273 spin_unlock(lockp); 1274 goto out; 1275 } 1276 } 1277 spin_unlock(lockp); 1278 if (cb->args[1]) { 1279 cb->args[1] = 0; 1280 goto restart; 1281 } 1282 } 1283out: 1284 local_bh_enable(); 1285 if (last_id) { 1286 /* nf ct hash resize happened, now clear the leftover. */ 1287 if (cb->args[1] == last_id) 1288 cb->args[1] = 0; 1289 } 1290 1291 while (i) { 1292 i--; 1293 if (nf_ct_should_gc(nf_ct_evict[i])) 1294 nf_ct_kill(nf_ct_evict[i]); 1295 nf_ct_put(nf_ct_evict[i]); 1296 } 1297 1298 return skb->len; 1299} 1300 1301static int ipv4_nlattr_to_tuple(struct nlattr *tb[], 1302 struct nf_conntrack_tuple *t, 1303 u_int32_t flags) 1304{ 1305 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) { 1306 if (!tb[CTA_IP_V4_SRC]) 1307 return -EINVAL; 1308 1309 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]); 1310 } 1311 1312 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) { 1313 if (!tb[CTA_IP_V4_DST]) 1314 return -EINVAL; 1315 1316 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]); 1317 } 1318 1319 return 0; 1320} 1321 1322static int ipv6_nlattr_to_tuple(struct nlattr *tb[], 1323 struct nf_conntrack_tuple *t, 1324 u_int32_t flags) 1325{ 1326 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) { 1327 if (!tb[CTA_IP_V6_SRC]) 1328 return -EINVAL; 1329 1330 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]); 1331 } 1332 1333 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) { 1334 if (!tb[CTA_IP_V6_DST]) 1335 return -EINVAL; 1336 1337 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]); 1338 } 1339 1340 return 0; 1341} 1342 1343static int ctnetlink_parse_tuple_ip(struct nlattr *attr, 1344 struct nf_conntrack_tuple *tuple, 1345 u_int32_t flags) 1346{ 1347 struct nlattr *tb[CTA_IP_MAX+1]; 1348 int ret = 0; 1349 1350 ret = nla_parse_nested_deprecated(tb, CTA_IP_MAX, attr, 1351 cta_ip_nla_policy, NULL); 1352 if (ret < 0) 1353 return ret; 1354 1355 switch (tuple->src.l3num) { 1356 case NFPROTO_IPV4: 1357 ret = ipv4_nlattr_to_tuple(tb, tuple, flags); 1358 break; 1359 case NFPROTO_IPV6: 1360 ret = ipv6_nlattr_to_tuple(tb, tuple, flags); 1361 break; 1362 } 1363 1364 return ret; 1365} 1366 1367static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { 1368 [CTA_PROTO_NUM] = { .type = NLA_U8 }, 1369}; 1370 1371static int ctnetlink_parse_tuple_proto(struct nlattr *attr, 1372 struct nf_conntrack_tuple *tuple, 1373 u_int32_t flags) 1374{ 1375 const struct nf_conntrack_l4proto *l4proto; 1376 struct nlattr *tb[CTA_PROTO_MAX+1]; 1377 int ret = 0; 1378 1379 ret = nla_parse_nested_deprecated(tb, CTA_PROTO_MAX, attr, 1380 proto_nla_policy, NULL); 1381 if (ret < 0) 1382 return ret; 1383 1384 if (!(flags & CTA_FILTER_FLAG(CTA_PROTO_NUM))) 1385 return 0; 1386 1387 if (!tb[CTA_PROTO_NUM]) 1388 return -EINVAL; 1389 1390 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); 1391 1392 rcu_read_lock(); 1393 l4proto = nf_ct_l4proto_find(tuple->dst.protonum); 1394 1395 if (likely(l4proto->nlattr_to_tuple)) { 1396 ret = nla_validate_nested_deprecated(attr, CTA_PROTO_MAX, 1397 l4proto->nla_policy, 1398 NULL); 1399 if (ret == 0) 1400 ret = l4proto->nlattr_to_tuple(tb, tuple, flags); 1401 } 1402 1403 rcu_read_unlock(); 1404 1405 return ret; 1406} 1407 1408static int 1409ctnetlink_parse_zone(const struct nlattr *attr, 1410 struct nf_conntrack_zone *zone) 1411{ 1412 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID, 1413 NF_CT_DEFAULT_ZONE_DIR, 0); 1414#ifdef CONFIG_NF_CONNTRACK_ZONES 1415 if (attr) 1416 zone->id = ntohs(nla_get_be16(attr)); 1417#else 1418 if (attr) 1419 return -EOPNOTSUPP; 1420#endif 1421 return 0; 1422} 1423 1424static int 1425ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type, 1426 struct nf_conntrack_zone *zone) 1427{ 1428 int ret; 1429 1430 if (zone->id != NF_CT_DEFAULT_ZONE_ID) 1431 return -EINVAL; 1432 1433 ret = ctnetlink_parse_zone(attr, zone); 1434 if (ret < 0) 1435 return ret; 1436 1437 if (type == CTA_TUPLE_REPLY) 1438 zone->dir = NF_CT_ZONE_DIR_REPL; 1439 else 1440 zone->dir = NF_CT_ZONE_DIR_ORIG; 1441 1442 return 0; 1443} 1444 1445static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = { 1446 [CTA_TUPLE_IP] = { .type = NLA_NESTED }, 1447 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED }, 1448 [CTA_TUPLE_ZONE] = { .type = NLA_U16 }, 1449}; 1450 1451#define CTA_FILTER_F_ALL_CTA_PROTO \ 1452 (CTA_FILTER_F_CTA_PROTO_SRC_PORT | \ 1453 CTA_FILTER_F_CTA_PROTO_DST_PORT | \ 1454 CTA_FILTER_F_CTA_PROTO_ICMP_TYPE | \ 1455 CTA_FILTER_F_CTA_PROTO_ICMP_CODE | \ 1456 CTA_FILTER_F_CTA_PROTO_ICMP_ID | \ 1457 CTA_FILTER_F_CTA_PROTO_ICMPV6_TYPE | \ 1458 CTA_FILTER_F_CTA_PROTO_ICMPV6_CODE | \ 1459 CTA_FILTER_F_CTA_PROTO_ICMPV6_ID) 1460 1461static int 1462ctnetlink_parse_tuple_filter(const struct nlattr * const cda[], 1463 struct nf_conntrack_tuple *tuple, u32 type, 1464 u_int8_t l3num, struct nf_conntrack_zone *zone, 1465 u_int32_t flags) 1466{ 1467 struct nlattr *tb[CTA_TUPLE_MAX+1]; 1468 int err; 1469 1470 memset(tuple, 0, sizeof(*tuple)); 1471 1472 err = nla_parse_nested_deprecated(tb, CTA_TUPLE_MAX, cda[type], 1473 tuple_nla_policy, NULL); 1474 if (err < 0) 1475 return err; 1476 1477 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6) 1478 return -EOPNOTSUPP; 1479 tuple->src.l3num = l3num; 1480 1481 if (flags & CTA_FILTER_FLAG(CTA_IP_DST) || 1482 flags & CTA_FILTER_FLAG(CTA_IP_SRC)) { 1483 if (!tb[CTA_TUPLE_IP]) 1484 return -EINVAL; 1485 1486 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple, flags); 1487 if (err < 0) 1488 return err; 1489 } 1490 1491 if (flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) { 1492 if (!tb[CTA_TUPLE_PROTO]) 1493 return -EINVAL; 1494 1495 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple, flags); 1496 if (err < 0) 1497 return err; 1498 } else if (flags & CTA_FILTER_FLAG(ALL_CTA_PROTO)) { 1499 /* Can't manage proto flags without a protonum */ 1500 return -EINVAL; 1501 } 1502 1503 if ((flags & CTA_FILTER_FLAG(CTA_TUPLE_ZONE)) && tb[CTA_TUPLE_ZONE]) { 1504 if (!zone) 1505 return -EINVAL; 1506 1507 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE], 1508 type, zone); 1509 if (err < 0) 1510 return err; 1511 } 1512 1513 /* orig and expect tuples get DIR_ORIGINAL */ 1514 if (type == CTA_TUPLE_REPLY) 1515 tuple->dst.dir = IP_CT_DIR_REPLY; 1516 else 1517 tuple->dst.dir = IP_CT_DIR_ORIGINAL; 1518 1519 return 0; 1520} 1521 1522static int 1523ctnetlink_parse_tuple(const struct nlattr * const cda[], 1524 struct nf_conntrack_tuple *tuple, u32 type, 1525 u_int8_t l3num, struct nf_conntrack_zone *zone) 1526{ 1527 return ctnetlink_parse_tuple_filter(cda, tuple, type, l3num, zone, 1528 CTA_FILTER_FLAG(ALL)); 1529} 1530 1531static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { 1532 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING, 1533 .len = NF_CT_HELPER_NAME_LEN - 1 }, 1534}; 1535 1536static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name, 1537 struct nlattr **helpinfo) 1538{ 1539 int err; 1540 struct nlattr *tb[CTA_HELP_MAX+1]; 1541 1542 err = nla_parse_nested_deprecated(tb, CTA_HELP_MAX, attr, 1543 help_nla_policy, NULL); 1544 if (err < 0) 1545 return err; 1546 1547 if (!tb[CTA_HELP_NAME]) 1548 return -EINVAL; 1549 1550 *helper_name = nla_data(tb[CTA_HELP_NAME]); 1551 1552 if (tb[CTA_HELP_INFO]) 1553 *helpinfo = tb[CTA_HELP_INFO]; 1554 1555 return 0; 1556} 1557 1558static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { 1559 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED }, 1560 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED }, 1561 [CTA_STATUS] = { .type = NLA_U32 }, 1562 [CTA_PROTOINFO] = { .type = NLA_NESTED }, 1563 [CTA_HELP] = { .type = NLA_NESTED }, 1564 [CTA_NAT_SRC] = { .type = NLA_NESTED }, 1565 [CTA_TIMEOUT] = { .type = NLA_U32 }, 1566 [CTA_MARK] = { .type = NLA_U32 }, 1567 [CTA_ID] = { .type = NLA_U32 }, 1568 [CTA_NAT_DST] = { .type = NLA_NESTED }, 1569 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, 1570 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED }, 1571 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED }, 1572 [CTA_ZONE] = { .type = NLA_U16 }, 1573 [CTA_MARK_MASK] = { .type = NLA_U32 }, 1574 [CTA_LABELS] = { .type = NLA_BINARY, 1575 .len = NF_CT_LABELS_MAX_SIZE }, 1576 [CTA_LABELS_MASK] = { .type = NLA_BINARY, 1577 .len = NF_CT_LABELS_MAX_SIZE }, 1578 [CTA_FILTER] = { .type = NLA_NESTED }, 1579 [CTA_STATUS_MASK] = { .type = NLA_U32 }, 1580 [CTA_TIMESTAMP_EVENT] = { .type = NLA_REJECT }, 1581}; 1582 1583static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data) 1584{ 1585 return ctnetlink_filter_match(ct, data); 1586} 1587 1588static int ctnetlink_flush_conntrack(struct net *net, 1589 const struct nlattr * const cda[], 1590 u32 portid, int report, u8 family) 1591{ 1592 struct ctnetlink_filter *filter = NULL; 1593 struct nf_ct_iter_data iter = { 1594 .net = net, 1595 .portid = portid, 1596 .report = report, 1597 }; 1598 1599 if (ctnetlink_needs_filter(family, cda)) { 1600 filter = ctnetlink_alloc_filter(cda, family); 1601 if (IS_ERR(filter)) 1602 return PTR_ERR(filter); 1603 1604 iter.data = filter; 1605 } 1606 1607 nf_ct_iterate_cleanup_net(ctnetlink_flush_iterate, &iter); 1608 kfree(filter); 1609 1610 return 0; 1611} 1612 1613static int ctnetlink_del_conntrack(struct sk_buff *skb, 1614 const struct nfnl_info *info, 1615 const struct nlattr * const cda[]) 1616{ 1617 u8 family = info->nfmsg->nfgen_family; 1618 struct nf_conntrack_tuple_hash *h; 1619 struct nf_conntrack_tuple tuple; 1620 struct nf_conntrack_zone zone; 1621 struct nf_conn *ct; 1622 int err; 1623 1624 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); 1625 if (err < 0) 1626 return err; 1627 1628 if (cda[CTA_TUPLE_ORIG] && !cda[CTA_FILTER]) 1629 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, 1630 family, &zone); 1631 else if (cda[CTA_TUPLE_REPLY] && !cda[CTA_FILTER]) 1632 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, 1633 family, &zone); 1634 else { 1635 u8 u3 = info->nfmsg->version || cda[CTA_FILTER] ? family : AF_UNSPEC; 1636 1637 return ctnetlink_flush_conntrack(info->net, cda, 1638 NETLINK_CB(skb).portid, 1639 nlmsg_report(info->nlh), u3); 1640 } 1641 1642 if (err < 0) 1643 return err; 1644 1645 h = nf_conntrack_find_get(info->net, &zone, &tuple); 1646 if (!h) 1647 return -ENOENT; 1648 1649 ct = nf_ct_tuplehash_to_ctrack(h); 1650 1651 if (cda[CTA_ID]) { 1652 __be32 id = nla_get_be32(cda[CTA_ID]); 1653 1654 if (id != (__force __be32)nf_ct_get_id(ct)) { 1655 nf_ct_put(ct); 1656 return -ENOENT; 1657 } 1658 } 1659 1660 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(info->nlh)); 1661 nf_ct_put(ct); 1662 1663 return 0; 1664} 1665 1666static int ctnetlink_get_conntrack(struct sk_buff *skb, 1667 const struct nfnl_info *info, 1668 const struct nlattr * const cda[]) 1669{ 1670 u_int8_t u3 = info->nfmsg->nfgen_family; 1671 struct nf_conntrack_tuple_hash *h; 1672 struct nf_conntrack_tuple tuple; 1673 struct nf_conntrack_zone zone; 1674 struct sk_buff *skb2; 1675 struct nf_conn *ct; 1676 int err; 1677 1678 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 1679 struct netlink_dump_control c = { 1680 .start = ctnetlink_start, 1681 .dump = ctnetlink_dump_table, 1682 .done = ctnetlink_done, 1683 .data = (void *)cda, 1684 }; 1685 1686 return netlink_dump_start(info->sk, skb, info->nlh, &c); 1687 } 1688 1689 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); 1690 if (err < 0) 1691 return err; 1692 1693 if (cda[CTA_TUPLE_ORIG]) 1694 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, 1695 u3, &zone); 1696 else if (cda[CTA_TUPLE_REPLY]) 1697 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, 1698 u3, &zone); 1699 else 1700 return -EINVAL; 1701 1702 if (err < 0) 1703 return err; 1704 1705 h = nf_conntrack_find_get(info->net, &zone, &tuple); 1706 if (!h) 1707 return -ENOENT; 1708 1709 ct = nf_ct_tuplehash_to_ctrack(h); 1710 1711 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1712 if (!skb2) { 1713 nf_ct_put(ct); 1714 return -ENOMEM; 1715 } 1716 1717 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, 1718 info->nlh->nlmsg_seq, 1719 NFNL_MSG_TYPE(info->nlh->nlmsg_type), ct, 1720 true, 0); 1721 nf_ct_put(ct); 1722 if (err <= 0) { 1723 kfree_skb(skb2); 1724 return -ENOMEM; 1725 } 1726 1727 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 1728} 1729 1730#ifdef CONFIG_NF_CONNTRACK_EVENTS 1731static int ctnetlink_dump_one_entry(struct sk_buff *skb, 1732 struct netlink_callback *cb, 1733 struct nf_conn *ct, 1734 bool dying) 1735{ 1736 struct ctnetlink_list_dump_ctx *ctx = (void *)cb->ctx; 1737 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); 1738 u8 l3proto = nfmsg->nfgen_family; 1739 int res; 1740 1741 if (l3proto && nf_ct_l3num(ct) != l3proto) 1742 return 0; 1743 1744 if (ctx->last_id) { 1745 if (ctnetlink_get_id(ct) != ctx->last_id) 1746 return 0; 1747 1748 ctx->last_id = 0; 1749 } 1750 1751 /* We can't dump extension info for the unconfirmed 1752 * list because unconfirmed conntracks can have 1753 * ct->ext reallocated (and thus freed). 1754 * 1755 * In the dying list case ct->ext can't be free'd 1756 * until after we drop pcpu->lock. 1757 */ 1758 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid, 1759 cb->nlh->nlmsg_seq, 1760 NFNL_MSG_TYPE(cb->nlh->nlmsg_type), 1761 ct, dying, 0); 1762 if (res < 0) 1763 ctx->last_id = ctnetlink_get_id(ct); 1764 1765 return res; 1766} 1767#endif 1768 1769static int 1770ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb) 1771{ 1772 return 0; 1773} 1774 1775static int 1776ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb) 1777{ 1778 struct ctnetlink_list_dump_ctx *ctx = (void *)cb->ctx; 1779#ifdef CONFIG_NF_CONNTRACK_EVENTS 1780 const struct net *net = sock_net(skb->sk); 1781 struct nf_conntrack_net_ecache *ecache_net; 1782 unsigned long last_id = ctx->last_id; 1783 struct nf_conntrack_tuple_hash *h; 1784 struct hlist_nulls_node *n; 1785#endif 1786 1787 if (ctx->done) 1788 return 0; 1789 1790 ctx->last_id = 0; 1791 1792#ifdef CONFIG_NF_CONNTRACK_EVENTS 1793 ecache_net = nf_conn_pernet_ecache(net); 1794 spin_lock_bh(&ecache_net->dying_lock); 1795 1796 hlist_nulls_for_each_entry(h, n, &ecache_net->dying_list, hnnode) { 1797 struct nf_conn *ct; 1798 int res; 1799 1800 ct = nf_ct_tuplehash_to_ctrack(h); 1801 if (last_id && last_id != ctnetlink_get_id(ct)) 1802 continue; 1803 1804 res = ctnetlink_dump_one_entry(skb, cb, ct, true); 1805 if (res < 0) { 1806 spin_unlock_bh(&ecache_net->dying_lock); 1807 return skb->len; 1808 } 1809 1810 last_id = 0; 1811 } 1812 1813 spin_unlock_bh(&ecache_net->dying_lock); 1814#endif 1815 ctx->done = true; 1816 1817 return skb->len; 1818} 1819 1820static int ctnetlink_get_ct_dying(struct sk_buff *skb, 1821 const struct nfnl_info *info, 1822 const struct nlattr * const cda[]) 1823{ 1824 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 1825 struct netlink_dump_control c = { 1826 .dump = ctnetlink_dump_dying, 1827 }; 1828 return netlink_dump_start(info->sk, skb, info->nlh, &c); 1829 } 1830 1831 return -EOPNOTSUPP; 1832} 1833 1834static int ctnetlink_get_ct_unconfirmed(struct sk_buff *skb, 1835 const struct nfnl_info *info, 1836 const struct nlattr * const cda[]) 1837{ 1838 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 1839 struct netlink_dump_control c = { 1840 .dump = ctnetlink_dump_unconfirmed, 1841 }; 1842 return netlink_dump_start(info->sk, skb, info->nlh, &c); 1843 } 1844 1845 return -EOPNOTSUPP; 1846} 1847 1848#if IS_ENABLED(CONFIG_NF_NAT) 1849static int 1850ctnetlink_parse_nat_setup(struct nf_conn *ct, 1851 enum nf_nat_manip_type manip, 1852 const struct nlattr *attr) 1853 __must_hold(RCU) 1854{ 1855 const struct nf_nat_hook *nat_hook; 1856 int err; 1857 1858 nat_hook = rcu_dereference(nf_nat_hook); 1859 if (!nat_hook) { 1860#ifdef CONFIG_MODULES 1861 rcu_read_unlock(); 1862 nfnl_unlock(NFNL_SUBSYS_CTNETLINK); 1863 if (request_module("nf-nat") < 0) { 1864 nfnl_lock(NFNL_SUBSYS_CTNETLINK); 1865 rcu_read_lock(); 1866 return -EOPNOTSUPP; 1867 } 1868 nfnl_lock(NFNL_SUBSYS_CTNETLINK); 1869 rcu_read_lock(); 1870 nat_hook = rcu_dereference(nf_nat_hook); 1871 if (nat_hook) 1872 return -EAGAIN; 1873#endif 1874 return -EOPNOTSUPP; 1875 } 1876 1877 err = nat_hook->parse_nat_setup(ct, manip, attr); 1878 if (err == -EAGAIN) { 1879#ifdef CONFIG_MODULES 1880 rcu_read_unlock(); 1881 nfnl_unlock(NFNL_SUBSYS_CTNETLINK); 1882 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) { 1883 nfnl_lock(NFNL_SUBSYS_CTNETLINK); 1884 rcu_read_lock(); 1885 return -EOPNOTSUPP; 1886 } 1887 nfnl_lock(NFNL_SUBSYS_CTNETLINK); 1888 rcu_read_lock(); 1889#else 1890 err = -EOPNOTSUPP; 1891#endif 1892 } 1893 return err; 1894} 1895#endif 1896 1897static int 1898ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) 1899{ 1900 return nf_ct_change_status_common(ct, ntohl(nla_get_be32(cda[CTA_STATUS]))); 1901} 1902 1903static int 1904ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[]) 1905{ 1906#if IS_ENABLED(CONFIG_NF_NAT) 1907 int ret; 1908 1909 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) 1910 return 0; 1911 1912 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST, 1913 cda[CTA_NAT_DST]); 1914 if (ret < 0) 1915 return ret; 1916 1917 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC, 1918 cda[CTA_NAT_SRC]); 1919#else 1920 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC]) 1921 return 0; 1922 return -EOPNOTSUPP; 1923#endif 1924} 1925 1926static int ctnetlink_change_helper(struct nf_conn *ct, 1927 const struct nlattr * const cda[]) 1928{ 1929 struct nf_conntrack_helper *helper; 1930 struct nf_conn_help *help = nfct_help(ct); 1931 char *helpname = NULL; 1932 struct nlattr *helpinfo = NULL; 1933 int err; 1934 1935 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); 1936 if (err < 0) 1937 return err; 1938 1939 /* don't change helper of sibling connections */ 1940 if (ct->master) { 1941 /* If we try to change the helper to the same thing twice, 1942 * treat the second attempt as a no-op instead of returning 1943 * an error. 1944 */ 1945 err = -EBUSY; 1946 if (help) { 1947 rcu_read_lock(); 1948 helper = rcu_dereference(help->helper); 1949 if (helper && !strcmp(helper->name, helpname)) 1950 err = 0; 1951 rcu_read_unlock(); 1952 } 1953 1954 return err; 1955 } 1956 1957 if (!strcmp(helpname, "")) { 1958 if (help && help->helper) { 1959 /* we had a helper before ... */ 1960 nf_ct_remove_expectations(ct); 1961 RCU_INIT_POINTER(help->helper, NULL); 1962 } 1963 1964 return 0; 1965 } 1966 1967 rcu_read_lock(); 1968 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), 1969 nf_ct_protonum(ct)); 1970 if (helper == NULL) { 1971 rcu_read_unlock(); 1972 return -EOPNOTSUPP; 1973 } 1974 1975 if (help) { 1976 if (rcu_access_pointer(help->helper) == helper) { 1977 /* update private helper data if allowed. */ 1978 if (helper->from_nlattr) 1979 helper->from_nlattr(helpinfo, ct); 1980 err = 0; 1981 } else 1982 err = -EBUSY; 1983 } else { 1984 /* we cannot set a helper for an existing conntrack */ 1985 err = -EOPNOTSUPP; 1986 } 1987 1988 rcu_read_unlock(); 1989 return err; 1990} 1991 1992static int ctnetlink_change_timeout(struct nf_conn *ct, 1993 const struct nlattr * const cda[]) 1994{ 1995 return __nf_ct_change_timeout(ct, (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ); 1996} 1997 1998#if defined(CONFIG_NF_CONNTRACK_MARK) 1999static void ctnetlink_change_mark(struct nf_conn *ct, 2000 const struct nlattr * const cda[]) 2001{ 2002 u32 mark, newmark, mask = 0; 2003 2004 if (cda[CTA_MARK_MASK]) 2005 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK])); 2006 2007 mark = ntohl(nla_get_be32(cda[CTA_MARK])); 2008 newmark = (READ_ONCE(ct->mark) & mask) ^ mark; 2009 if (newmark != READ_ONCE(ct->mark)) 2010 WRITE_ONCE(ct->mark, newmark); 2011} 2012#endif 2013 2014static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { 2015 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED }, 2016 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, 2017}; 2018 2019static int ctnetlink_change_protoinfo(struct nf_conn *ct, 2020 const struct nlattr * const cda[]) 2021{ 2022 const struct nlattr *attr = cda[CTA_PROTOINFO]; 2023 const struct nf_conntrack_l4proto *l4proto; 2024 struct nlattr *tb[CTA_PROTOINFO_MAX+1]; 2025 int err = 0; 2026 2027 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_MAX, attr, 2028 protoinfo_policy, NULL); 2029 if (err < 0) 2030 return err; 2031 2032 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); 2033 if (l4proto->from_nlattr) 2034 err = l4proto->from_nlattr(tb, ct); 2035 2036 return err; 2037} 2038 2039static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = { 2040 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 }, 2041 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 }, 2042 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 }, 2043}; 2044 2045static int change_seq_adj(struct nf_ct_seqadj *seq, 2046 const struct nlattr * const attr) 2047{ 2048 int err; 2049 struct nlattr *cda[CTA_SEQADJ_MAX+1]; 2050 2051 err = nla_parse_nested_deprecated(cda, CTA_SEQADJ_MAX, attr, 2052 seqadj_policy, NULL); 2053 if (err < 0) 2054 return err; 2055 2056 if (!cda[CTA_SEQADJ_CORRECTION_POS]) 2057 return -EINVAL; 2058 2059 seq->correction_pos = 2060 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS])); 2061 2062 if (!cda[CTA_SEQADJ_OFFSET_BEFORE]) 2063 return -EINVAL; 2064 2065 seq->offset_before = 2066 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE])); 2067 2068 if (!cda[CTA_SEQADJ_OFFSET_AFTER]) 2069 return -EINVAL; 2070 2071 seq->offset_after = 2072 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER])); 2073 2074 return 0; 2075} 2076 2077static int 2078ctnetlink_change_seq_adj(struct nf_conn *ct, 2079 const struct nlattr * const cda[]) 2080{ 2081 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct); 2082 int ret = 0; 2083 2084 if (!seqadj) 2085 return 0; 2086 2087 spin_lock_bh(&ct->lock); 2088 if (cda[CTA_SEQ_ADJ_ORIG]) { 2089 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL], 2090 cda[CTA_SEQ_ADJ_ORIG]); 2091 if (ret < 0) 2092 goto err; 2093 2094 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); 2095 } 2096 2097 if (cda[CTA_SEQ_ADJ_REPLY]) { 2098 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY], 2099 cda[CTA_SEQ_ADJ_REPLY]); 2100 if (ret < 0) 2101 goto err; 2102 2103 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); 2104 } 2105 2106 spin_unlock_bh(&ct->lock); 2107 return 0; 2108err: 2109 spin_unlock_bh(&ct->lock); 2110 return ret; 2111} 2112 2113static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = { 2114 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 }, 2115 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 }, 2116 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 }, 2117}; 2118 2119static int ctnetlink_change_synproxy(struct nf_conn *ct, 2120 const struct nlattr * const cda[]) 2121{ 2122 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct); 2123 struct nlattr *tb[CTA_SYNPROXY_MAX + 1]; 2124 int err; 2125 2126 if (!synproxy) 2127 return 0; 2128 2129 err = nla_parse_nested_deprecated(tb, CTA_SYNPROXY_MAX, 2130 cda[CTA_SYNPROXY], synproxy_policy, 2131 NULL); 2132 if (err < 0) 2133 return err; 2134 2135 if (!tb[CTA_SYNPROXY_ISN] || 2136 !tb[CTA_SYNPROXY_ITS] || 2137 !tb[CTA_SYNPROXY_TSOFF]) 2138 return -EINVAL; 2139 2140 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN])); 2141 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS])); 2142 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF])); 2143 2144 return 0; 2145} 2146 2147static int 2148ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[]) 2149{ 2150#ifdef CONFIG_NF_CONNTRACK_LABELS 2151 size_t len = nla_len(cda[CTA_LABELS]); 2152 const void *mask = cda[CTA_LABELS_MASK]; 2153 2154 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */ 2155 return -EINVAL; 2156 2157 if (mask) { 2158 if (nla_len(cda[CTA_LABELS_MASK]) == 0 || 2159 nla_len(cda[CTA_LABELS_MASK]) != len) 2160 return -EINVAL; 2161 mask = nla_data(cda[CTA_LABELS_MASK]); 2162 } 2163 2164 len /= sizeof(u32); 2165 2166 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len); 2167#else 2168 return -EOPNOTSUPP; 2169#endif 2170} 2171 2172static int 2173ctnetlink_change_conntrack(struct nf_conn *ct, 2174 const struct nlattr * const cda[]) 2175{ 2176 int err; 2177 2178 /* only allow NAT changes and master assignation for new conntracks */ 2179 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) 2180 return -EOPNOTSUPP; 2181 2182 if (cda[CTA_HELP]) { 2183 err = ctnetlink_change_helper(ct, cda); 2184 if (err < 0) 2185 return err; 2186 } 2187 2188 if (cda[CTA_TIMEOUT]) { 2189 err = ctnetlink_change_timeout(ct, cda); 2190 if (err < 0) 2191 return err; 2192 } 2193 2194 if (cda[CTA_STATUS]) { 2195 err = ctnetlink_change_status(ct, cda); 2196 if (err < 0) 2197 return err; 2198 } 2199 2200 if (cda[CTA_PROTOINFO]) { 2201 err = ctnetlink_change_protoinfo(ct, cda); 2202 if (err < 0) 2203 return err; 2204 } 2205 2206#if defined(CONFIG_NF_CONNTRACK_MARK) 2207 if (cda[CTA_MARK]) 2208 ctnetlink_change_mark(ct, cda); 2209#endif 2210 2211 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { 2212 err = ctnetlink_change_seq_adj(ct, cda); 2213 if (err < 0) 2214 return err; 2215 } 2216 2217 if (cda[CTA_SYNPROXY]) { 2218 err = ctnetlink_change_synproxy(ct, cda); 2219 if (err < 0) 2220 return err; 2221 } 2222 2223 if (cda[CTA_LABELS]) { 2224 err = ctnetlink_attach_labels(ct, cda); 2225 if (err < 0) 2226 return err; 2227 } 2228 2229 return 0; 2230} 2231 2232static struct nf_conn * 2233ctnetlink_create_conntrack(struct net *net, 2234 const struct nf_conntrack_zone *zone, 2235 const struct nlattr * const cda[], 2236 struct nf_conntrack_tuple *otuple, 2237 struct nf_conntrack_tuple *rtuple, 2238 u8 u3) 2239{ 2240 struct nf_conn *ct; 2241 int err = -EINVAL; 2242 struct nf_conntrack_helper *helper; 2243 struct nf_conn_tstamp *tstamp; 2244 u64 timeout; 2245 2246 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC); 2247 if (IS_ERR(ct)) 2248 return ERR_PTR(-ENOMEM); 2249 2250 if (!cda[CTA_TIMEOUT]) 2251 goto err1; 2252 2253 rcu_read_lock(); 2254 if (cda[CTA_HELP]) { 2255 char *helpname = NULL; 2256 struct nlattr *helpinfo = NULL; 2257 2258 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo); 2259 if (err < 0) 2260 goto err2; 2261 2262 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct), 2263 nf_ct_protonum(ct)); 2264 if (helper == NULL) { 2265 rcu_read_unlock(); 2266#ifdef CONFIG_MODULES 2267 if (request_module("nfct-helper-%s", helpname) < 0) { 2268 err = -EOPNOTSUPP; 2269 goto err1; 2270 } 2271 2272 rcu_read_lock(); 2273 helper = __nf_conntrack_helper_find(helpname, 2274 nf_ct_l3num(ct), 2275 nf_ct_protonum(ct)); 2276 if (helper) { 2277 err = -EAGAIN; 2278 goto err2; 2279 } 2280 rcu_read_unlock(); 2281#endif 2282 err = -EOPNOTSUPP; 2283 goto err1; 2284 } else { 2285 struct nf_conn_help *help; 2286 2287 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); 2288 if (help == NULL) { 2289 err = -ENOMEM; 2290 goto err2; 2291 } 2292 /* set private helper data if allowed. */ 2293 if (helper->from_nlattr) 2294 helper->from_nlattr(helpinfo, ct); 2295 2296 /* disable helper auto-assignment for this entry */ 2297 ct->status |= IPS_HELPER; 2298 RCU_INIT_POINTER(help->helper, helper); 2299 } 2300 } 2301 2302 err = ctnetlink_setup_nat(ct, cda); 2303 if (err < 0) 2304 goto err2; 2305 2306 nf_ct_acct_ext_add(ct, GFP_ATOMIC); 2307 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); 2308 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC); 2309 nf_ct_labels_ext_add(ct); 2310 nfct_seqadj_ext_add(ct); 2311 nfct_synproxy_ext_add(ct); 2312 2313 /* we must add conntrack extensions before confirmation. */ 2314 ct->status |= IPS_CONFIRMED; 2315 2316 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ; 2317 __nf_ct_set_timeout(ct, timeout); 2318 2319 if (cda[CTA_STATUS]) { 2320 err = ctnetlink_change_status(ct, cda); 2321 if (err < 0) 2322 goto err2; 2323 } 2324 2325 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) { 2326 err = ctnetlink_change_seq_adj(ct, cda); 2327 if (err < 0) 2328 goto err2; 2329 } 2330 2331 memset(&ct->proto, 0, sizeof(ct->proto)); 2332 if (cda[CTA_PROTOINFO]) { 2333 err = ctnetlink_change_protoinfo(ct, cda); 2334 if (err < 0) 2335 goto err2; 2336 } 2337 2338 if (cda[CTA_SYNPROXY]) { 2339 err = ctnetlink_change_synproxy(ct, cda); 2340 if (err < 0) 2341 goto err2; 2342 } 2343 2344#if defined(CONFIG_NF_CONNTRACK_MARK) 2345 if (cda[CTA_MARK]) 2346 ctnetlink_change_mark(ct, cda); 2347#endif 2348 2349 /* setup master conntrack: this is a confirmed expectation */ 2350 if (cda[CTA_TUPLE_MASTER]) { 2351 struct nf_conntrack_tuple master; 2352 struct nf_conntrack_tuple_hash *master_h; 2353 struct nf_conn *master_ct; 2354 2355 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, 2356 u3, NULL); 2357 if (err < 0) 2358 goto err2; 2359 2360 master_h = nf_conntrack_find_get(net, zone, &master); 2361 if (master_h == NULL) { 2362 err = -ENOENT; 2363 goto err2; 2364 } 2365 master_ct = nf_ct_tuplehash_to_ctrack(master_h); 2366 __set_bit(IPS_EXPECTED_BIT, &ct->status); 2367 ct->master = master_ct; 2368 } 2369 tstamp = nf_conn_tstamp_find(ct); 2370 if (tstamp) 2371 tstamp->start = ktime_get_real_ns(); 2372 2373 err = nf_conntrack_hash_check_insert(ct); 2374 if (err < 0) 2375 goto err3; 2376 2377 rcu_read_unlock(); 2378 2379 return ct; 2380 2381err3: 2382 if (ct->master) 2383 nf_ct_put(ct->master); 2384err2: 2385 rcu_read_unlock(); 2386err1: 2387 nf_conntrack_free(ct); 2388 return ERR_PTR(err); 2389} 2390 2391static int ctnetlink_new_conntrack(struct sk_buff *skb, 2392 const struct nfnl_info *info, 2393 const struct nlattr * const cda[]) 2394{ 2395 struct nf_conntrack_tuple otuple, rtuple; 2396 struct nf_conntrack_tuple_hash *h = NULL; 2397 u_int8_t u3 = info->nfmsg->nfgen_family; 2398 struct nf_conntrack_zone zone; 2399 struct nf_conn *ct; 2400 int err; 2401 2402 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone); 2403 if (err < 0) 2404 return err; 2405 2406 if (cda[CTA_TUPLE_ORIG]) { 2407 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, 2408 u3, &zone); 2409 if (err < 0) 2410 return err; 2411 } 2412 2413 if (cda[CTA_TUPLE_REPLY]) { 2414 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, 2415 u3, &zone); 2416 if (err < 0) 2417 return err; 2418 } 2419 2420 if (cda[CTA_TUPLE_ORIG]) 2421 h = nf_conntrack_find_get(info->net, &zone, &otuple); 2422 else if (cda[CTA_TUPLE_REPLY]) 2423 h = nf_conntrack_find_get(info->net, &zone, &rtuple); 2424 2425 if (h == NULL) { 2426 err = -ENOENT; 2427 if (info->nlh->nlmsg_flags & NLM_F_CREATE) { 2428 enum ip_conntrack_events events; 2429 2430 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY]) 2431 return -EINVAL; 2432 if (otuple.dst.protonum != rtuple.dst.protonum) 2433 return -EINVAL; 2434 2435 ct = ctnetlink_create_conntrack(info->net, &zone, cda, 2436 &otuple, &rtuple, u3); 2437 if (IS_ERR(ct)) 2438 return PTR_ERR(ct); 2439 2440 err = 0; 2441 if (test_bit(IPS_EXPECTED_BIT, &ct->status)) 2442 events = 1 << IPCT_RELATED; 2443 else 2444 events = 1 << IPCT_NEW; 2445 2446 if (cda[CTA_LABELS] && 2447 ctnetlink_attach_labels(ct, cda) == 0) 2448 events |= (1 << IPCT_LABEL); 2449 2450 nf_conntrack_eventmask_report((1 << IPCT_REPLY) | 2451 (1 << IPCT_ASSURED) | 2452 (1 << IPCT_HELPER) | 2453 (1 << IPCT_PROTOINFO) | 2454 (1 << IPCT_SEQADJ) | 2455 (1 << IPCT_MARK) | 2456 (1 << IPCT_SYNPROXY) | 2457 events, 2458 ct, NETLINK_CB(skb).portid, 2459 nlmsg_report(info->nlh)); 2460 nf_ct_put(ct); 2461 } 2462 2463 return err; 2464 } 2465 /* implicit 'else' */ 2466 2467 err = -EEXIST; 2468 ct = nf_ct_tuplehash_to_ctrack(h); 2469 if (!(info->nlh->nlmsg_flags & NLM_F_EXCL)) { 2470 err = ctnetlink_change_conntrack(ct, cda); 2471 if (err == 0) { 2472 nf_conntrack_eventmask_report((1 << IPCT_REPLY) | 2473 (1 << IPCT_ASSURED) | 2474 (1 << IPCT_HELPER) | 2475 (1 << IPCT_LABEL) | 2476 (1 << IPCT_PROTOINFO) | 2477 (1 << IPCT_SEQADJ) | 2478 (1 << IPCT_MARK) | 2479 (1 << IPCT_SYNPROXY), 2480 ct, NETLINK_CB(skb).portid, 2481 nlmsg_report(info->nlh)); 2482 } 2483 } 2484 2485 nf_ct_put(ct); 2486 return err; 2487} 2488 2489static int 2490ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq, 2491 __u16 cpu, const struct ip_conntrack_stat *st) 2492{ 2493 struct nlmsghdr *nlh; 2494 unsigned int flags = portid ? NLM_F_MULTI : 0, event; 2495 2496 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, 2497 IPCTNL_MSG_CT_GET_STATS_CPU); 2498 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC, 2499 NFNETLINK_V0, htons(cpu)); 2500 if (!nlh) 2501 goto nlmsg_failure; 2502 2503 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) || 2504 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) || 2505 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) || 2506 nla_put_be32(skb, CTA_STATS_INSERT_FAILED, 2507 htonl(st->insert_failed)) || 2508 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) || 2509 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) || 2510 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) || 2511 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART, 2512 htonl(st->search_restart)) || 2513 nla_put_be32(skb, CTA_STATS_CLASH_RESOLVE, 2514 htonl(st->clash_resolve)) || 2515 nla_put_be32(skb, CTA_STATS_CHAIN_TOOLONG, 2516 htonl(st->chaintoolong))) 2517 goto nla_put_failure; 2518 2519 nlmsg_end(skb, nlh); 2520 return skb->len; 2521 2522nla_put_failure: 2523nlmsg_failure: 2524 nlmsg_cancel(skb, nlh); 2525 return -1; 2526} 2527 2528static int 2529ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) 2530{ 2531 int cpu; 2532 struct net *net = sock_net(skb->sk); 2533 2534 if (cb->args[0] == nr_cpu_ids) 2535 return 0; 2536 2537 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { 2538 const struct ip_conntrack_stat *st; 2539 2540 if (!cpu_possible(cpu)) 2541 continue; 2542 2543 st = per_cpu_ptr(net->ct.stat, cpu); 2544 if (ctnetlink_ct_stat_cpu_fill_info(skb, 2545 NETLINK_CB(cb->skb).portid, 2546 cb->nlh->nlmsg_seq, 2547 cpu, st) < 0) 2548 break; 2549 } 2550 cb->args[0] = cpu; 2551 2552 return skb->len; 2553} 2554 2555static int ctnetlink_stat_ct_cpu(struct sk_buff *skb, 2556 const struct nfnl_info *info, 2557 const struct nlattr * const cda[]) 2558{ 2559 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 2560 struct netlink_dump_control c = { 2561 .dump = ctnetlink_ct_stat_cpu_dump, 2562 }; 2563 return netlink_dump_start(info->sk, skb, info->nlh, &c); 2564 } 2565 2566 return 0; 2567} 2568 2569static int 2570ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, 2571 struct net *net) 2572{ 2573 unsigned int flags = portid ? NLM_F_MULTI : 0, event; 2574 unsigned int nr_conntracks; 2575 struct nlmsghdr *nlh; 2576 2577 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS); 2578 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC, 2579 NFNETLINK_V0, 0); 2580 if (!nlh) 2581 goto nlmsg_failure; 2582 2583 nr_conntracks = nf_conntrack_count(net); 2584 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks))) 2585 goto nla_put_failure; 2586 2587 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max))) 2588 goto nla_put_failure; 2589 2590 nlmsg_end(skb, nlh); 2591 return skb->len; 2592 2593nla_put_failure: 2594nlmsg_failure: 2595 nlmsg_cancel(skb, nlh); 2596 return -1; 2597} 2598 2599static int ctnetlink_stat_ct(struct sk_buff *skb, const struct nfnl_info *info, 2600 const struct nlattr * const cda[]) 2601{ 2602 struct sk_buff *skb2; 2603 int err; 2604 2605 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2606 if (skb2 == NULL) 2607 return -ENOMEM; 2608 2609 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid, 2610 info->nlh->nlmsg_seq, 2611 NFNL_MSG_TYPE(info->nlh->nlmsg_type), 2612 sock_net(skb->sk)); 2613 if (err <= 0) { 2614 kfree_skb(skb2); 2615 return -ENOMEM; 2616 } 2617 2618 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 2619} 2620 2621static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { 2622 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED }, 2623 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED }, 2624 [CTA_EXPECT_MASK] = { .type = NLA_NESTED }, 2625 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, 2626 [CTA_EXPECT_ID] = { .type = NLA_U32 }, 2627 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING, 2628 .len = NF_CT_HELPER_NAME_LEN - 1 }, 2629 [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, 2630 [CTA_EXPECT_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NF_CT_EXPECT_MASK), 2631 [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, 2632 [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, 2633 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING }, 2634}; 2635 2636static struct nf_conntrack_expect * 2637ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct, 2638 struct nf_conntrack_tuple *tuple, 2639 struct nf_conntrack_tuple *mask); 2640 2641#ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT 2642static size_t 2643ctnetlink_glue_build_size(const struct nf_conn *ct) 2644{ 2645 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ 2646 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ 2647 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ 2648 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ 2649 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ 2650 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ 2651 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ 2652 + nla_total_size(0) /* CTA_PROTOINFO */ 2653 + nla_total_size(0) /* CTA_HELP */ 2654 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ 2655 + ctnetlink_secctx_size(ct) 2656 + ctnetlink_acct_size(ct) 2657 + ctnetlink_timestamp_size(ct) 2658#if IS_ENABLED(CONFIG_NF_NAT) 2659 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ 2660 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ 2661#endif 2662#ifdef CONFIG_NF_CONNTRACK_MARK 2663 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ 2664#endif 2665#ifdef CONFIG_NF_CONNTRACK_ZONES 2666 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */ 2667#endif 2668 + ctnetlink_proto_size(ct) 2669 ; 2670} 2671 2672static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct) 2673{ 2674 const struct nf_conntrack_zone *zone; 2675 struct nlattr *nest_parms; 2676 2677 zone = nf_ct_zone(ct); 2678 2679 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG); 2680 if (!nest_parms) 2681 goto nla_put_failure; 2682 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) 2683 goto nla_put_failure; 2684 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 2685 NF_CT_ZONE_DIR_ORIG) < 0) 2686 goto nla_put_failure; 2687 nla_nest_end(skb, nest_parms); 2688 2689 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY); 2690 if (!nest_parms) 2691 goto nla_put_failure; 2692 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) 2693 goto nla_put_failure; 2694 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone, 2695 NF_CT_ZONE_DIR_REPL) < 0) 2696 goto nla_put_failure; 2697 nla_nest_end(skb, nest_parms); 2698 2699 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone, 2700 NF_CT_DEFAULT_ZONE_DIR) < 0) 2701 goto nla_put_failure; 2702 2703 if (ctnetlink_dump_id(skb, ct) < 0) 2704 goto nla_put_failure; 2705 2706 if (ctnetlink_dump_status(skb, ct) < 0) 2707 goto nla_put_failure; 2708 2709 if (ctnetlink_dump_timeout(skb, ct, false) < 0) 2710 goto nla_put_failure; 2711 2712 if (ctnetlink_dump_protoinfo(skb, ct, false) < 0) 2713 goto nla_put_failure; 2714 2715 if (ctnetlink_dump_acct(skb, ct, IPCTNL_MSG_CT_GET) < 0 || 2716 ctnetlink_dump_timestamp(skb, ct) < 0) 2717 goto nla_put_failure; 2718 2719 if (ctnetlink_dump_helpinfo(skb, ct) < 0) 2720 goto nla_put_failure; 2721 2722#ifdef CONFIG_NF_CONNTRACK_SECMARK 2723 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0) 2724 goto nla_put_failure; 2725#endif 2726 if (ct->master && ctnetlink_dump_master(skb, ct) < 0) 2727 goto nla_put_failure; 2728 2729 if ((ct->status & IPS_SEQ_ADJUST) && 2730 ctnetlink_dump_ct_seq_adj(skb, ct) < 0) 2731 goto nla_put_failure; 2732 2733 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0) 2734 goto nla_put_failure; 2735 2736#ifdef CONFIG_NF_CONNTRACK_MARK 2737 if (ctnetlink_dump_mark(skb, ct, true) < 0) 2738 goto nla_put_failure; 2739#endif 2740 if (ctnetlink_dump_labels(skb, ct) < 0) 2741 goto nla_put_failure; 2742 return 0; 2743 2744nla_put_failure: 2745 return -ENOSPC; 2746} 2747 2748static int 2749ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct, 2750 enum ip_conntrack_info ctinfo, 2751 u_int16_t ct_attr, u_int16_t ct_info_attr) 2752{ 2753 struct nlattr *nest_parms; 2754 2755 nest_parms = nla_nest_start(skb, ct_attr); 2756 if (!nest_parms) 2757 goto nla_put_failure; 2758 2759 if (__ctnetlink_glue_build(skb, ct) < 0) 2760 goto nla_put_failure; 2761 2762 nla_nest_end(skb, nest_parms); 2763 2764 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo))) 2765 goto nla_put_failure; 2766 2767 return 0; 2768 2769nla_put_failure: 2770 return -ENOSPC; 2771} 2772 2773static int 2774ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[]) 2775{ 2776 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); 2777 unsigned long d = ct->status ^ status; 2778 2779 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) 2780 /* SEEN_REPLY bit can only be set */ 2781 return -EBUSY; 2782 2783 if (d & IPS_ASSURED && !(status & IPS_ASSURED)) 2784 /* ASSURED bit can only be set */ 2785 return -EBUSY; 2786 2787 /* This check is less strict than ctnetlink_change_status() 2788 * because callers often flip IPS_EXPECTED bits when sending 2789 * an NFQA_CT attribute to the kernel. So ignore the 2790 * unchangeable bits but do not error out. Also user programs 2791 * are allowed to clear the bits that they are allowed to change. 2792 */ 2793 __nf_ct_change_status(ct, status, ~status); 2794 return 0; 2795} 2796 2797static int 2798ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct) 2799{ 2800 int err; 2801 2802 if (cda[CTA_TIMEOUT]) { 2803 err = ctnetlink_change_timeout(ct, cda); 2804 if (err < 0) 2805 return err; 2806 } 2807 if (cda[CTA_STATUS]) { 2808 err = ctnetlink_update_status(ct, cda); 2809 if (err < 0) 2810 return err; 2811 } 2812 if (cda[CTA_HELP]) { 2813 err = ctnetlink_change_helper(ct, cda); 2814 if (err < 0) 2815 return err; 2816 } 2817 if (cda[CTA_LABELS]) { 2818 err = ctnetlink_attach_labels(ct, cda); 2819 if (err < 0) 2820 return err; 2821 } 2822#if defined(CONFIG_NF_CONNTRACK_MARK) 2823 if (cda[CTA_MARK]) { 2824 ctnetlink_change_mark(ct, cda); 2825 } 2826#endif 2827 return 0; 2828} 2829 2830static int 2831ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct) 2832{ 2833 struct nlattr *cda[CTA_MAX+1]; 2834 int ret; 2835 2836 ret = nla_parse_nested_deprecated(cda, CTA_MAX, attr, ct_nla_policy, 2837 NULL); 2838 if (ret < 0) 2839 return ret; 2840 2841 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct); 2842} 2843 2844static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda, 2845 const struct nf_conn *ct, 2846 struct nf_conntrack_tuple *tuple, 2847 struct nf_conntrack_tuple *mask) 2848{ 2849 int err; 2850 2851 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE, 2852 nf_ct_l3num(ct), NULL); 2853 if (err < 0) 2854 return err; 2855 2856 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK, 2857 nf_ct_l3num(ct), NULL); 2858} 2859 2860static int 2861ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct, 2862 u32 portid, u32 report) 2863{ 2864 struct nlattr *cda[CTA_EXPECT_MAX+1]; 2865 struct nf_conntrack_tuple tuple, mask; 2866 struct nf_conntrack_expect *exp; 2867 int err; 2868 2869 err = nla_parse_nested_deprecated(cda, CTA_EXPECT_MAX, attr, 2870 exp_nla_policy, NULL); 2871 if (err < 0) 2872 return err; 2873 2874 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda, 2875 ct, &tuple, &mask); 2876 if (err < 0) 2877 return err; 2878 2879 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct, 2880 &tuple, &mask); 2881 if (IS_ERR(exp)) 2882 return PTR_ERR(exp); 2883 2884 err = nf_ct_expect_related_report(exp, portid, report, 0); 2885 nf_ct_expect_put(exp); 2886 return err; 2887} 2888 2889static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct, 2890 enum ip_conntrack_info ctinfo, int diff) 2891{ 2892 if (!(ct->status & IPS_NAT_MASK)) 2893 return; 2894 2895 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff); 2896} 2897 2898static const struct nfnl_ct_hook ctnetlink_glue_hook = { 2899 .build_size = ctnetlink_glue_build_size, 2900 .build = ctnetlink_glue_build, 2901 .parse = ctnetlink_glue_parse, 2902 .attach_expect = ctnetlink_glue_attach_expect, 2903 .seq_adjust = ctnetlink_glue_seqadj, 2904}; 2905#endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */ 2906 2907/*********************************************************************** 2908 * EXPECT 2909 ***********************************************************************/ 2910 2911static int ctnetlink_exp_dump_tuple(struct sk_buff *skb, 2912 const struct nf_conntrack_tuple *tuple, 2913 u32 type) 2914{ 2915 struct nlattr *nest_parms; 2916 2917 nest_parms = nla_nest_start(skb, type); 2918 if (!nest_parms) 2919 goto nla_put_failure; 2920 if (ctnetlink_dump_tuples(skb, tuple) < 0) 2921 goto nla_put_failure; 2922 nla_nest_end(skb, nest_parms); 2923 2924 return 0; 2925 2926nla_put_failure: 2927 return -1; 2928} 2929 2930static int ctnetlink_exp_dump_mask(struct sk_buff *skb, 2931 const struct nf_conntrack_tuple *tuple, 2932 const struct nf_conntrack_tuple_mask *mask) 2933{ 2934 const struct nf_conntrack_l4proto *l4proto; 2935 struct nf_conntrack_tuple m; 2936 struct nlattr *nest_parms; 2937 int ret; 2938 2939 memset(&m, 0xFF, sizeof(m)); 2940 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); 2941 m.src.u.all = mask->src.u.all; 2942 m.src.l3num = tuple->src.l3num; 2943 m.dst.protonum = tuple->dst.protonum; 2944 2945 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK); 2946 if (!nest_parms) 2947 goto nla_put_failure; 2948 2949 rcu_read_lock(); 2950 ret = ctnetlink_dump_tuples_ip(skb, &m); 2951 if (ret >= 0) { 2952 l4proto = nf_ct_l4proto_find(tuple->dst.protonum); 2953 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); 2954 } 2955 rcu_read_unlock(); 2956 2957 if (unlikely(ret < 0)) 2958 goto nla_put_failure; 2959 2960 nla_nest_end(skb, nest_parms); 2961 2962 return 0; 2963 2964nla_put_failure: 2965 return -1; 2966} 2967 2968#if IS_ENABLED(CONFIG_NF_NAT) 2969static const union nf_inet_addr any_addr; 2970#endif 2971 2972static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp) 2973{ 2974 static siphash_aligned_key_t exp_id_seed; 2975 unsigned long a, b, c, d; 2976 2977 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed)); 2978 2979 a = (unsigned long)exp; 2980 b = (unsigned long)exp->helper; 2981 c = (unsigned long)exp->master; 2982 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed); 2983 2984#ifdef CONFIG_64BIT 2985 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed); 2986#else 2987 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed); 2988#endif 2989} 2990 2991static int 2992ctnetlink_exp_dump_expect(struct sk_buff *skb, 2993 const struct nf_conntrack_expect *exp) 2994{ 2995 struct nf_conn *master = exp->master; 2996 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ; 2997 struct nf_conntrack_helper *helper; 2998#if IS_ENABLED(CONFIG_NF_NAT) 2999 struct nlattr *nest_parms; 3000 struct nf_conntrack_tuple nat_tuple = {}; 3001#endif 3002 struct nf_ct_helper_expectfn *expfn; 3003 3004 if (timeout < 0) 3005 timeout = 0; 3006 3007 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) 3008 goto nla_put_failure; 3009 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) 3010 goto nla_put_failure; 3011 if (ctnetlink_exp_dump_tuple(skb, 3012 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, 3013 CTA_EXPECT_MASTER) < 0) 3014 goto nla_put_failure; 3015 3016#if IS_ENABLED(CONFIG_NF_NAT) 3017 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) || 3018 exp->saved_proto.all) { 3019 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT); 3020 if (!nest_parms) 3021 goto nla_put_failure; 3022 3023 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir))) 3024 goto nla_put_failure; 3025 3026 nat_tuple.src.l3num = nf_ct_l3num(master); 3027 nat_tuple.src.u3 = exp->saved_addr; 3028 nat_tuple.dst.protonum = nf_ct_protonum(master); 3029 nat_tuple.src.u = exp->saved_proto; 3030 3031 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple, 3032 CTA_EXPECT_NAT_TUPLE) < 0) 3033 goto nla_put_failure; 3034 nla_nest_end(skb, nest_parms); 3035 } 3036#endif 3037 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) || 3038 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) || 3039 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || 3040 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) 3041 goto nla_put_failure; 3042 3043 helper = rcu_dereference(exp->helper); 3044 if (helper && 3045 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name)) 3046 goto nla_put_failure; 3047 3048 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn); 3049 if (expfn != NULL && 3050 nla_put_string(skb, CTA_EXPECT_FN, expfn->name)) 3051 goto nla_put_failure; 3052 3053 return 0; 3054 3055nla_put_failure: 3056 return -1; 3057} 3058 3059static int 3060ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq, 3061 int event, const struct nf_conntrack_expect *exp) 3062{ 3063 struct nlmsghdr *nlh; 3064 unsigned int flags = portid ? NLM_F_MULTI : 0; 3065 3066 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event); 3067 nlh = nfnl_msg_put(skb, portid, seq, event, flags, 3068 exp->tuple.src.l3num, NFNETLINK_V0, 0); 3069 if (!nlh) 3070 goto nlmsg_failure; 3071 3072 if (ctnetlink_exp_dump_expect(skb, exp) < 0) 3073 goto nla_put_failure; 3074 3075 nlmsg_end(skb, nlh); 3076 return skb->len; 3077 3078nlmsg_failure: 3079nla_put_failure: 3080 nlmsg_cancel(skb, nlh); 3081 return -1; 3082} 3083 3084#ifdef CONFIG_NF_CONNTRACK_EVENTS 3085static int 3086ctnetlink_expect_event(unsigned int events, const struct nf_exp_event *item) 3087{ 3088 struct nf_conntrack_expect *exp = item->exp; 3089 struct net *net = nf_ct_exp_net(exp); 3090 struct nlmsghdr *nlh; 3091 struct sk_buff *skb; 3092 unsigned int type, group; 3093 int flags = 0; 3094 3095 if (events & (1 << IPEXP_DESTROY)) { 3096 type = IPCTNL_MSG_EXP_DELETE; 3097 group = NFNLGRP_CONNTRACK_EXP_DESTROY; 3098 } else if (events & (1 << IPEXP_NEW)) { 3099 type = IPCTNL_MSG_EXP_NEW; 3100 flags = NLM_F_CREATE|NLM_F_EXCL; 3101 group = NFNLGRP_CONNTRACK_EXP_NEW; 3102 } else 3103 return 0; 3104 3105 if (!item->report && !nfnetlink_has_listeners(net, group)) 3106 return 0; 3107 3108 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 3109 if (skb == NULL) 3110 goto errout; 3111 3112 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type); 3113 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, 3114 exp->tuple.src.l3num, NFNETLINK_V0, 0); 3115 if (!nlh) 3116 goto nlmsg_failure; 3117 3118 if (ctnetlink_exp_dump_expect(skb, exp) < 0) 3119 goto nla_put_failure; 3120 3121 nlmsg_end(skb, nlh); 3122 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC); 3123 return 0; 3124 3125nla_put_failure: 3126 nlmsg_cancel(skb, nlh); 3127nlmsg_failure: 3128 kfree_skb(skb); 3129errout: 3130 nfnetlink_set_err(net, 0, 0, -ENOBUFS); 3131 return 0; 3132} 3133#endif 3134 3135static unsigned long ctnetlink_exp_id(const struct nf_conntrack_expect *exp) 3136{ 3137 unsigned long id = (unsigned long)exp; 3138 3139 id += nf_ct_get_id(exp->master); 3140 id += exp->class; 3141 3142 return id ? id : 1; 3143} 3144 3145static int 3146ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) 3147{ 3148 struct net *net = sock_net(skb->sk); 3149 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); 3150 u_int8_t l3proto = nfmsg->nfgen_family; 3151 unsigned long last_id = cb->args[1]; 3152 struct nf_conntrack_expect *exp; 3153 3154 rcu_read_lock(); 3155 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { 3156restart: 3157 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]], 3158 hnode) { 3159 if (l3proto && exp->tuple.src.l3num != l3proto) 3160 continue; 3161 3162 if (!net_eq(nf_ct_net(exp->master), net)) 3163 continue; 3164 3165 if (cb->args[1]) { 3166 if (ctnetlink_exp_id(exp) != last_id) 3167 continue; 3168 cb->args[1] = 0; 3169 } 3170 if (ctnetlink_exp_fill_info(skb, 3171 NETLINK_CB(cb->skb).portid, 3172 cb->nlh->nlmsg_seq, 3173 IPCTNL_MSG_EXP_NEW, 3174 exp) < 0) { 3175 cb->args[1] = ctnetlink_exp_id(exp); 3176 goto out; 3177 } 3178 } 3179 if (cb->args[1]) { 3180 cb->args[1] = 0; 3181 goto restart; 3182 } 3183 } 3184out: 3185 rcu_read_unlock(); 3186 return skb->len; 3187} 3188 3189static int 3190ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb) 3191{ 3192 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); 3193 struct nf_conn *ct = cb->data; 3194 struct nf_conn_help *help; 3195 u_int8_t l3proto = nfmsg->nfgen_family; 3196 unsigned long last_id = cb->args[1]; 3197 struct nf_conntrack_expect *exp; 3198 3199 if (cb->args[0]) 3200 return 0; 3201 3202 help = nfct_help(ct); 3203 if (!help) 3204 return 0; 3205 3206 rcu_read_lock(); 3207 3208restart: 3209 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) { 3210 if (l3proto && exp->tuple.src.l3num != l3proto) 3211 continue; 3212 if (cb->args[1]) { 3213 if (ctnetlink_exp_id(exp) != last_id) 3214 continue; 3215 cb->args[1] = 0; 3216 } 3217 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid, 3218 cb->nlh->nlmsg_seq, 3219 IPCTNL_MSG_EXP_NEW, 3220 exp) < 0) { 3221 cb->args[1] = ctnetlink_exp_id(exp); 3222 goto out; 3223 } 3224 } 3225 if (cb->args[1]) { 3226 cb->args[1] = 0; 3227 goto restart; 3228 } 3229 cb->args[0] = 1; 3230out: 3231 rcu_read_unlock(); 3232 return skb->len; 3233} 3234 3235static int ctnetlink_dump_exp_ct_start(struct netlink_callback *cb) 3236{ 3237 struct nf_conn *ct = cb->data; 3238 3239 if (!refcount_inc_not_zero(&ct->ct_general.use)) 3240 return -ENOENT; 3241 return 0; 3242} 3243 3244static int ctnetlink_dump_exp_ct_done(struct netlink_callback *cb) 3245{ 3246 struct nf_conn *ct = cb->data; 3247 3248 if (ct) 3249 nf_ct_put(ct); 3250 return 0; 3251} 3252 3253static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl, 3254 struct sk_buff *skb, 3255 const struct nlmsghdr *nlh, 3256 const struct nlattr * const cda[], 3257 struct netlink_ext_ack *extack) 3258{ 3259 int err; 3260 struct nfgenmsg *nfmsg = nlmsg_data(nlh); 3261 u_int8_t u3 = nfmsg->nfgen_family; 3262 struct nf_conntrack_tuple tuple; 3263 struct nf_conntrack_tuple_hash *h; 3264 struct nf_conn *ct; 3265 struct nf_conntrack_zone zone; 3266 struct netlink_dump_control c = { 3267 .dump = ctnetlink_exp_ct_dump_table, 3268 .start = ctnetlink_dump_exp_ct_start, 3269 .done = ctnetlink_dump_exp_ct_done, 3270 }; 3271 3272 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, 3273 u3, NULL); 3274 if (err < 0) 3275 return err; 3276 3277 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); 3278 if (err < 0) 3279 return err; 3280 3281 h = nf_conntrack_find_get(net, &zone, &tuple); 3282 if (!h) 3283 return -ENOENT; 3284 3285 ct = nf_ct_tuplehash_to_ctrack(h); 3286 /* No expectation linked to this connection tracking. */ 3287 if (!nfct_help(ct)) { 3288 nf_ct_put(ct); 3289 return 0; 3290 } 3291 3292 c.data = ct; 3293 3294 err = netlink_dump_start(ctnl, skb, nlh, &c); 3295 nf_ct_put(ct); 3296 3297 return err; 3298} 3299 3300static int ctnetlink_get_expect(struct sk_buff *skb, 3301 const struct nfnl_info *info, 3302 const struct nlattr * const cda[]) 3303{ 3304 u_int8_t u3 = info->nfmsg->nfgen_family; 3305 struct nf_conntrack_tuple tuple; 3306 struct nf_conntrack_expect *exp; 3307 struct nf_conntrack_zone zone; 3308 struct sk_buff *skb2; 3309 int err; 3310 3311 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 3312 if (cda[CTA_EXPECT_MASTER]) 3313 return ctnetlink_dump_exp_ct(info->net, info->sk, skb, 3314 info->nlh, cda, 3315 info->extack); 3316 else { 3317 struct netlink_dump_control c = { 3318 .dump = ctnetlink_exp_dump_table, 3319 }; 3320 return netlink_dump_start(info->sk, skb, info->nlh, &c); 3321 } 3322 } 3323 3324 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); 3325 if (err < 0) 3326 return err; 3327 3328 if (cda[CTA_EXPECT_TUPLE]) 3329 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, 3330 u3, NULL); 3331 else if (cda[CTA_EXPECT_MASTER]) 3332 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, 3333 u3, NULL); 3334 else 3335 return -EINVAL; 3336 3337 if (err < 0) 3338 return err; 3339 3340 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 3341 if (!skb2) 3342 return -ENOMEM; 3343 3344 spin_lock_bh(&nf_conntrack_expect_lock); 3345 exp = nf_ct_expect_find_get(info->net, &zone, &tuple); 3346 if (!exp) { 3347 spin_unlock_bh(&nf_conntrack_expect_lock); 3348 kfree_skb(skb2); 3349 return -ENOENT; 3350 } 3351 3352 if (cda[CTA_EXPECT_ID]) { 3353 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); 3354 3355 if (id != nf_expect_get_id(exp)) { 3356 nf_ct_expect_put(exp); 3357 spin_unlock_bh(&nf_conntrack_expect_lock); 3358 kfree_skb(skb2); 3359 return -ENOENT; 3360 } 3361 } 3362 3363 rcu_read_lock(); 3364 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid, 3365 info->nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, 3366 exp); 3367 rcu_read_unlock(); 3368 nf_ct_expect_put(exp); 3369 spin_unlock_bh(&nf_conntrack_expect_lock); 3370 3371 if (err <= 0) { 3372 kfree_skb(skb2); 3373 return -ENOMEM; 3374 } 3375 3376 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); 3377} 3378 3379static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data) 3380{ 3381 struct nf_conntrack_helper *helper; 3382 const char *name = data; 3383 3384 helper = rcu_dereference(exp->helper); 3385 if (!helper) 3386 return false; 3387 3388 return strcmp(helper->name, name) == 0; 3389} 3390 3391static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data) 3392{ 3393 return true; 3394} 3395 3396static int ctnetlink_del_expect(struct sk_buff *skb, 3397 const struct nfnl_info *info, 3398 const struct nlattr * const cda[]) 3399{ 3400 u_int8_t u3 = info->nfmsg->nfgen_family; 3401 struct nf_conntrack_expect *exp; 3402 struct nf_conntrack_tuple tuple; 3403 struct nf_conntrack_zone zone; 3404 int err; 3405 3406 if (cda[CTA_EXPECT_TUPLE]) { 3407 /* delete a single expect by tuple */ 3408 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); 3409 if (err < 0) 3410 return err; 3411 3412 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, 3413 u3, NULL); 3414 if (err < 0) 3415 return err; 3416 3417 spin_lock_bh(&nf_conntrack_expect_lock); 3418 3419 /* bump usage count to 2 */ 3420 exp = nf_ct_expect_find_get(info->net, &zone, &tuple); 3421 if (!exp) { 3422 spin_unlock_bh(&nf_conntrack_expect_lock); 3423 return -ENOENT; 3424 } 3425 3426 if (cda[CTA_EXPECT_ID]) { 3427 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); 3428 3429 if (id != nf_expect_get_id(exp)) { 3430 nf_ct_expect_put(exp); 3431 spin_unlock_bh(&nf_conntrack_expect_lock); 3432 return -ENOENT; 3433 } 3434 } 3435 3436 /* after list removal, usage count == 1 */ 3437 if (timer_delete(&exp->timeout)) { 3438 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid, 3439 nlmsg_report(info->nlh)); 3440 nf_ct_expect_put(exp); 3441 } 3442 spin_unlock_bh(&nf_conntrack_expect_lock); 3443 /* have to put what we 'get' above. 3444 * after this line usage count == 0 */ 3445 nf_ct_expect_put(exp); 3446 } else if (cda[CTA_EXPECT_HELP_NAME]) { 3447 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); 3448 3449 nf_ct_expect_iterate_net(info->net, expect_iter_name, name, 3450 NETLINK_CB(skb).portid, 3451 nlmsg_report(info->nlh)); 3452 } else { 3453 /* This basically means we have to flush everything*/ 3454 nf_ct_expect_iterate_net(info->net, expect_iter_all, NULL, 3455 NETLINK_CB(skb).portid, 3456 nlmsg_report(info->nlh)); 3457 } 3458 3459 return 0; 3460} 3461static int 3462ctnetlink_change_expect(struct nf_conntrack_expect *x, 3463 const struct nlattr * const cda[]) 3464{ 3465 if (cda[CTA_EXPECT_TIMEOUT]) { 3466 if (!timer_delete(&x->timeout)) 3467 return -ETIME; 3468 3469 x->timeout.expires = jiffies + 3470 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ; 3471 add_timer(&x->timeout); 3472 } 3473 return 0; 3474} 3475 3476#if IS_ENABLED(CONFIG_NF_NAT) 3477static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { 3478 [CTA_EXPECT_NAT_DIR] = NLA_POLICY_MAX(NLA_BE32, IP_CT_DIR_REPLY), 3479 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED }, 3480}; 3481#endif 3482 3483static int 3484ctnetlink_parse_expect_nat(const struct nlattr *attr, 3485 struct nf_conntrack_expect *exp, 3486 u_int8_t u3) 3487{ 3488#if IS_ENABLED(CONFIG_NF_NAT) 3489 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1]; 3490 struct nf_conntrack_tuple nat_tuple = {}; 3491 int err; 3492 3493 err = nla_parse_nested_deprecated(tb, CTA_EXPECT_NAT_MAX, attr, 3494 exp_nat_nla_policy, NULL); 3495 if (err < 0) 3496 return err; 3497 3498 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE]) 3499 return -EINVAL; 3500 3501 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb, 3502 &nat_tuple, CTA_EXPECT_NAT_TUPLE, 3503 u3, NULL); 3504 if (err < 0) 3505 return err; 3506 3507 exp->saved_addr = nat_tuple.src.u3; 3508 exp->saved_proto = nat_tuple.src.u; 3509 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR])); 3510 3511 return 0; 3512#else 3513 return -EOPNOTSUPP; 3514#endif 3515} 3516 3517static struct nf_conntrack_expect * 3518ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, 3519 struct nf_conntrack_tuple *tuple, 3520 struct nf_conntrack_tuple *mask) 3521{ 3522 struct net *net = read_pnet(&ct->ct_net); 3523 struct nf_conntrack_helper *helper; 3524 struct nf_conntrack_expect *exp; 3525 struct nf_conn_help *help; 3526 u32 class = 0; 3527 int err; 3528 3529 help = nfct_help(ct); 3530 if (!help) 3531 return ERR_PTR(-EOPNOTSUPP); 3532 3533 helper = rcu_dereference(help->helper); 3534 if (!helper) 3535 return ERR_PTR(-EOPNOTSUPP); 3536 3537 if (cda[CTA_EXPECT_CLASS]) { 3538 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS])); 3539 if (class > helper->expect_class_max) 3540 return ERR_PTR(-EINVAL); 3541 } 3542 exp = nf_ct_expect_alloc(ct); 3543 if (!exp) 3544 return ERR_PTR(-ENOMEM); 3545 3546 if (cda[CTA_EXPECT_FLAGS]) { 3547 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS])); 3548 exp->flags &= ~NF_CT_EXPECT_USERSPACE; 3549 } else { 3550 exp->flags = 0; 3551 } 3552 if (cda[CTA_EXPECT_FN]) { 3553 const char *name = nla_data(cda[CTA_EXPECT_FN]); 3554 struct nf_ct_helper_expectfn *expfn; 3555 3556 expfn = nf_ct_helper_expectfn_find_by_name(name); 3557 if (expfn == NULL) { 3558 err = -EINVAL; 3559 goto err_out; 3560 } 3561 exp->expectfn = expfn->expectfn; 3562 } else 3563 exp->expectfn = NULL; 3564 3565 exp->class = class; 3566 exp->master = ct; 3567 write_pnet(&exp->net, net); 3568#ifdef CONFIG_NF_CONNTRACK_ZONES 3569 exp->zone = ct->zone; 3570#endif 3571 rcu_assign_pointer(exp->helper, helper); 3572 exp->tuple = *tuple; 3573 exp->mask.src.u3 = mask->src.u3; 3574 exp->mask.src.u.all = mask->src.u.all; 3575 3576 if (cda[CTA_EXPECT_NAT]) { 3577 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT], 3578 exp, nf_ct_l3num(ct)); 3579 if (err < 0) 3580 goto err_out; 3581#if IS_ENABLED(CONFIG_NF_NAT) 3582 } else { 3583 memset(&exp->saved_addr, 0, sizeof(exp->saved_addr)); 3584 memset(&exp->saved_proto, 0, sizeof(exp->saved_proto)); 3585 exp->dir = 0; 3586#endif 3587 } 3588 return exp; 3589err_out: 3590 nf_ct_expect_put(exp); 3591 return ERR_PTR(err); 3592} 3593 3594static int 3595ctnetlink_create_expect(struct net *net, 3596 const struct nf_conntrack_zone *zone, 3597 const struct nlattr * const cda[], 3598 u_int8_t u3, u32 portid, int report) 3599{ 3600 struct nf_conntrack_tuple tuple, mask, master_tuple; 3601 struct nf_conntrack_tuple_hash *h = NULL; 3602 struct nf_conntrack_expect *exp; 3603 struct nf_conn *ct; 3604 int err; 3605 3606 /* caller guarantees that those three CTA_EXPECT_* exist */ 3607 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, 3608 u3, NULL); 3609 if (err < 0) 3610 return err; 3611 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, 3612 u3, NULL); 3613 if (err < 0) 3614 return err; 3615 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, 3616 u3, NULL); 3617 if (err < 0) 3618 return err; 3619 3620 /* Look for master conntrack of this expectation */ 3621 h = nf_conntrack_find_get(net, zone, &master_tuple); 3622 if (!h) 3623 return -ENOENT; 3624 ct = nf_ct_tuplehash_to_ctrack(h); 3625 3626 rcu_read_lock(); 3627 exp = ctnetlink_alloc_expect(cda, ct, &tuple, &mask); 3628 if (IS_ERR(exp)) { 3629 err = PTR_ERR(exp); 3630 goto err_rcu; 3631 } 3632 3633 err = nf_ct_expect_related_report(exp, portid, report, 0); 3634 nf_ct_expect_put(exp); 3635err_rcu: 3636 rcu_read_unlock(); 3637 nf_ct_put(ct); 3638 3639 return err; 3640} 3641 3642static int ctnetlink_new_expect(struct sk_buff *skb, 3643 const struct nfnl_info *info, 3644 const struct nlattr * const cda[]) 3645{ 3646 u_int8_t u3 = info->nfmsg->nfgen_family; 3647 struct nf_conntrack_tuple tuple; 3648 struct nf_conntrack_expect *exp; 3649 struct nf_conntrack_zone zone; 3650 int err; 3651 3652 if (!cda[CTA_EXPECT_TUPLE] 3653 || !cda[CTA_EXPECT_MASK] 3654 || !cda[CTA_EXPECT_MASTER]) 3655 return -EINVAL; 3656 3657 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone); 3658 if (err < 0) 3659 return err; 3660 3661 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, 3662 u3, NULL); 3663 if (err < 0) 3664 return err; 3665 3666 spin_lock_bh(&nf_conntrack_expect_lock); 3667 exp = __nf_ct_expect_find(info->net, &zone, &tuple); 3668 if (!exp) { 3669 spin_unlock_bh(&nf_conntrack_expect_lock); 3670 err = -ENOENT; 3671 if (info->nlh->nlmsg_flags & NLM_F_CREATE) { 3672 err = ctnetlink_create_expect(info->net, &zone, cda, u3, 3673 NETLINK_CB(skb).portid, 3674 nlmsg_report(info->nlh)); 3675 } 3676 return err; 3677 } 3678 3679 err = -EEXIST; 3680 if (!(info->nlh->nlmsg_flags & NLM_F_EXCL)) 3681 err = ctnetlink_change_expect(exp, cda); 3682 spin_unlock_bh(&nf_conntrack_expect_lock); 3683 3684 return err; 3685} 3686 3687static int 3688ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu, 3689 const struct ip_conntrack_stat *st) 3690{ 3691 struct nlmsghdr *nlh; 3692 unsigned int flags = portid ? NLM_F_MULTI : 0, event; 3693 3694 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, 3695 IPCTNL_MSG_EXP_GET_STATS_CPU); 3696 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC, 3697 NFNETLINK_V0, htons(cpu)); 3698 if (!nlh) 3699 goto nlmsg_failure; 3700 3701 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) || 3702 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) || 3703 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete))) 3704 goto nla_put_failure; 3705 3706 nlmsg_end(skb, nlh); 3707 return skb->len; 3708 3709nla_put_failure: 3710nlmsg_failure: 3711 nlmsg_cancel(skb, nlh); 3712 return -1; 3713} 3714 3715static int 3716ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb) 3717{ 3718 int cpu; 3719 struct net *net = sock_net(skb->sk); 3720 3721 if (cb->args[0] == nr_cpu_ids) 3722 return 0; 3723 3724 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) { 3725 const struct ip_conntrack_stat *st; 3726 3727 if (!cpu_possible(cpu)) 3728 continue; 3729 3730 st = per_cpu_ptr(net->ct.stat, cpu); 3731 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid, 3732 cb->nlh->nlmsg_seq, 3733 cpu, st) < 0) 3734 break; 3735 } 3736 cb->args[0] = cpu; 3737 3738 return skb->len; 3739} 3740 3741static int ctnetlink_stat_exp_cpu(struct sk_buff *skb, 3742 const struct nfnl_info *info, 3743 const struct nlattr * const cda[]) 3744{ 3745 if (info->nlh->nlmsg_flags & NLM_F_DUMP) { 3746 struct netlink_dump_control c = { 3747 .dump = ctnetlink_exp_stat_cpu_dump, 3748 }; 3749 return netlink_dump_start(info->sk, skb, info->nlh, &c); 3750 } 3751 3752 return 0; 3753} 3754 3755#ifdef CONFIG_NF_CONNTRACK_EVENTS 3756static struct nf_ct_event_notifier ctnl_notifier = { 3757 .ct_event = ctnetlink_conntrack_event, 3758 .exp_event = ctnetlink_expect_event, 3759}; 3760#endif 3761 3762static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { 3763 [IPCTNL_MSG_CT_NEW] = { 3764 .call = ctnetlink_new_conntrack, 3765 .type = NFNL_CB_MUTEX, 3766 .attr_count = CTA_MAX, 3767 .policy = ct_nla_policy 3768 }, 3769 [IPCTNL_MSG_CT_GET] = { 3770 .call = ctnetlink_get_conntrack, 3771 .type = NFNL_CB_MUTEX, 3772 .attr_count = CTA_MAX, 3773 .policy = ct_nla_policy 3774 }, 3775 [IPCTNL_MSG_CT_DELETE] = { 3776 .call = ctnetlink_del_conntrack, 3777 .type = NFNL_CB_MUTEX, 3778 .attr_count = CTA_MAX, 3779 .policy = ct_nla_policy 3780 }, 3781 [IPCTNL_MSG_CT_GET_CTRZERO] = { 3782 .call = ctnetlink_get_conntrack, 3783 .type = NFNL_CB_MUTEX, 3784 .attr_count = CTA_MAX, 3785 .policy = ct_nla_policy 3786 }, 3787 [IPCTNL_MSG_CT_GET_STATS_CPU] = { 3788 .call = ctnetlink_stat_ct_cpu, 3789 .type = NFNL_CB_MUTEX, 3790 }, 3791 [IPCTNL_MSG_CT_GET_STATS] = { 3792 .call = ctnetlink_stat_ct, 3793 .type = NFNL_CB_MUTEX, 3794 }, 3795 [IPCTNL_MSG_CT_GET_DYING] = { 3796 .call = ctnetlink_get_ct_dying, 3797 .type = NFNL_CB_MUTEX, 3798 }, 3799 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { 3800 .call = ctnetlink_get_ct_unconfirmed, 3801 .type = NFNL_CB_MUTEX, 3802 }, 3803}; 3804 3805static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { 3806 [IPCTNL_MSG_EXP_GET] = { 3807 .call = ctnetlink_get_expect, 3808 .type = NFNL_CB_MUTEX, 3809 .attr_count = CTA_EXPECT_MAX, 3810 .policy = exp_nla_policy 3811 }, 3812 [IPCTNL_MSG_EXP_NEW] = { 3813 .call = ctnetlink_new_expect, 3814 .type = NFNL_CB_MUTEX, 3815 .attr_count = CTA_EXPECT_MAX, 3816 .policy = exp_nla_policy 3817 }, 3818 [IPCTNL_MSG_EXP_DELETE] = { 3819 .call = ctnetlink_del_expect, 3820 .type = NFNL_CB_MUTEX, 3821 .attr_count = CTA_EXPECT_MAX, 3822 .policy = exp_nla_policy 3823 }, 3824 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { 3825 .call = ctnetlink_stat_exp_cpu, 3826 .type = NFNL_CB_MUTEX, 3827 }, 3828}; 3829 3830static const struct nfnetlink_subsystem ctnl_subsys = { 3831 .name = "conntrack", 3832 .subsys_id = NFNL_SUBSYS_CTNETLINK, 3833 .cb_count = IPCTNL_MSG_MAX, 3834 .cb = ctnl_cb, 3835}; 3836 3837static const struct nfnetlink_subsystem ctnl_exp_subsys = { 3838 .name = "conntrack_expect", 3839 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, 3840 .cb_count = IPCTNL_MSG_EXP_MAX, 3841 .cb = ctnl_exp_cb, 3842}; 3843 3844MODULE_ALIAS("ip_conntrack_netlink"); 3845MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); 3846MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); 3847 3848static int __net_init ctnetlink_net_init(struct net *net) 3849{ 3850#ifdef CONFIG_NF_CONNTRACK_EVENTS 3851 nf_conntrack_register_notifier(net, &ctnl_notifier); 3852#endif 3853 return 0; 3854} 3855 3856static void ctnetlink_net_pre_exit(struct net *net) 3857{ 3858#ifdef CONFIG_NF_CONNTRACK_EVENTS 3859 nf_conntrack_unregister_notifier(net); 3860#endif 3861} 3862 3863static struct pernet_operations ctnetlink_net_ops = { 3864 .init = ctnetlink_net_init, 3865 .pre_exit = ctnetlink_net_pre_exit, 3866}; 3867 3868static int __init ctnetlink_init(void) 3869{ 3870 int ret; 3871 3872 NL_ASSERT_CTX_FITS(struct ctnetlink_list_dump_ctx); 3873 3874 ret = nfnetlink_subsys_register(&ctnl_subsys); 3875 if (ret < 0) { 3876 pr_err("ctnetlink_init: cannot register with nfnetlink.\n"); 3877 goto err_out; 3878 } 3879 3880 ret = nfnetlink_subsys_register(&ctnl_exp_subsys); 3881 if (ret < 0) { 3882 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n"); 3883 goto err_unreg_subsys; 3884 } 3885 3886 ret = register_pernet_subsys(&ctnetlink_net_ops); 3887 if (ret < 0) { 3888 pr_err("ctnetlink_init: cannot register pernet operations\n"); 3889 goto err_unreg_exp_subsys; 3890 } 3891#ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT 3892 /* setup interaction between nf_queue and nf_conntrack_netlink. */ 3893 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook); 3894#endif 3895 return 0; 3896 3897err_unreg_exp_subsys: 3898 nfnetlink_subsys_unregister(&ctnl_exp_subsys); 3899err_unreg_subsys: 3900 nfnetlink_subsys_unregister(&ctnl_subsys); 3901err_out: 3902 return ret; 3903} 3904 3905static void __exit ctnetlink_exit(void) 3906{ 3907 unregister_pernet_subsys(&ctnetlink_net_ops); 3908 nfnetlink_subsys_unregister(&ctnl_exp_subsys); 3909 nfnetlink_subsys_unregister(&ctnl_subsys); 3910#ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT 3911 RCU_INIT_POINTER(nfnl_ct_hook, NULL); 3912#endif 3913 synchronize_rcu(); 3914} 3915 3916module_init(ctnetlink_init); 3917module_exit(ctnetlink_exit);
~/
Read-only file preview.