linux/net/netfilter/nf_conntrack_netlink.c
<<
>>
Prefs
   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
  33#include <linux/netfilter.h>
  34#include <net/netlink.h>
  35#include <net/sock.h>
  36#include <net/netfilter/nf_conntrack.h>
  37#include <net/netfilter/nf_conntrack_core.h>
  38#include <net/netfilter/nf_conntrack_expect.h>
  39#include <net/netfilter/nf_conntrack_helper.h>
  40#include <net/netfilter/nf_conntrack_seqadj.h>
  41#include <net/netfilter/nf_conntrack_l3proto.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#ifdef CONFIG_NF_NAT_NEEDED
  49#include <net/netfilter/nf_nat_core.h>
  50#include <net/netfilter/nf_nat_l4proto.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
  57MODULE_LICENSE("GPL");
  58
  59static char __initdata version[] = "0.93";
  60
  61static inline int
  62ctnetlink_dump_tuples_proto(struct sk_buff *skb,
  63                            const struct nf_conntrack_tuple *tuple,
  64                            struct nf_conntrack_l4proto *l4proto)
  65{
  66        int ret = 0;
  67        struct nlattr *nest_parms;
  68
  69        nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
  70        if (!nest_parms)
  71                goto nla_put_failure;
  72        if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
  73                goto nla_put_failure;
  74
  75        if (likely(l4proto->tuple_to_nlattr))
  76                ret = l4proto->tuple_to_nlattr(skb, tuple);
  77
  78        nla_nest_end(skb, nest_parms);
  79
  80        return ret;
  81
  82nla_put_failure:
  83        return -1;
  84}
  85
  86static inline int
  87ctnetlink_dump_tuples_ip(struct sk_buff *skb,
  88                         const struct nf_conntrack_tuple *tuple,
  89                         struct nf_conntrack_l3proto *l3proto)
  90{
  91        int ret = 0;
  92        struct nlattr *nest_parms;
  93
  94        nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
  95        if (!nest_parms)
  96                goto nla_put_failure;
  97
  98        if (likely(l3proto->tuple_to_nlattr))
  99                ret = l3proto->tuple_to_nlattr(skb, tuple);
 100
 101        nla_nest_end(skb, nest_parms);
 102
 103        return ret;
 104
 105nla_put_failure:
 106        return -1;
 107}
 108
 109static int
 110ctnetlink_dump_tuples(struct sk_buff *skb,
 111                      const struct nf_conntrack_tuple *tuple)
 112{
 113        int ret;
 114        struct nf_conntrack_l3proto *l3proto;
 115        struct nf_conntrack_l4proto *l4proto;
 116
 117        rcu_read_lock();
 118        l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
 119        ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
 120
 121        if (ret >= 0) {
 122                l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
 123                                               tuple->dst.protonum);
 124                ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
 125        }
 126        rcu_read_unlock();
 127        return ret;
 128}
 129
 130static inline int
 131ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
 132{
 133        if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
 134                goto nla_put_failure;
 135        return 0;
 136
 137nla_put_failure:
 138        return -1;
 139}
 140
 141static inline int
 142ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
 143{
 144        long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
 145
 146        if (timeout < 0)
 147                timeout = 0;
 148
 149        if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
 150                goto nla_put_failure;
 151        return 0;
 152
 153nla_put_failure:
 154        return -1;
 155}
 156
 157static inline int
 158ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
 159{
 160        struct nf_conntrack_l4proto *l4proto;
 161        struct nlattr *nest_proto;
 162        int ret;
 163
 164        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
 165        if (!l4proto->to_nlattr)
 166                return 0;
 167
 168        nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
 169        if (!nest_proto)
 170                goto nla_put_failure;
 171
 172        ret = l4proto->to_nlattr(skb, nest_proto, ct);
 173
 174        nla_nest_end(skb, nest_proto);
 175
 176        return ret;
 177
 178nla_put_failure:
 179        return -1;
 180}
 181
 182static inline int
 183ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
 184{
 185        struct nlattr *nest_helper;
 186        const struct nf_conn_help *help = nfct_help(ct);
 187        struct nf_conntrack_helper *helper;
 188
 189        if (!help)
 190                return 0;
 191
 192        helper = rcu_dereference(help->helper);
 193        if (!helper)
 194                goto out;
 195
 196        nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
 197        if (!nest_helper)
 198                goto nla_put_failure;
 199        if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
 200                goto nla_put_failure;
 201
 202        if (helper->to_nlattr)
 203                helper->to_nlattr(skb, ct);
 204
 205        nla_nest_end(skb, nest_helper);
 206out:
 207        return 0;
 208
 209nla_put_failure:
 210        return -1;
 211}
 212
 213static int
 214dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
 215              enum ip_conntrack_dir dir, int type)
 216{
 217        enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
 218        struct nf_conn_counter *counter = acct->counter;
 219        struct nlattr *nest_count;
 220        u64 pkts, bytes;
 221
 222        if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
 223                pkts = atomic64_xchg(&counter[dir].packets, 0);
 224                bytes = atomic64_xchg(&counter[dir].bytes, 0);
 225        } else {
 226                pkts = atomic64_read(&counter[dir].packets);
 227                bytes = atomic64_read(&counter[dir].bytes);
 228        }
 229
 230        nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
 231        if (!nest_count)
 232                goto nla_put_failure;
 233
 234        if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
 235            nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
 236                goto nla_put_failure;
 237
 238        nla_nest_end(skb, nest_count);
 239
 240        return 0;
 241
 242nla_put_failure:
 243        return -1;
 244}
 245
 246static int
 247ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
 248{
 249        struct nf_conn_acct *acct = nf_conn_acct_find(ct);
 250
 251        if (!acct)
 252                return 0;
 253
 254        if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
 255                return -1;
 256        if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
 257                return -1;
 258
 259        return 0;
 260}
 261
 262static int
 263ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
 264{
 265        struct nlattr *nest_count;
 266        const struct nf_conn_tstamp *tstamp;
 267
 268        tstamp = nf_conn_tstamp_find(ct);
 269        if (!tstamp)
 270                return 0;
 271
 272        nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
 273        if (!nest_count)
 274                goto nla_put_failure;
 275
 276        if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
 277            (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
 278                                               cpu_to_be64(tstamp->stop))))
 279                goto nla_put_failure;
 280        nla_nest_end(skb, nest_count);
 281
 282        return 0;
 283
 284nla_put_failure:
 285        return -1;
 286}
 287
 288#ifdef CONFIG_NF_CONNTRACK_MARK
 289static inline int
 290ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
 291{
 292        if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
 293                goto nla_put_failure;
 294        return 0;
 295
 296nla_put_failure:
 297        return -1;
 298}
 299#else
 300#define ctnetlink_dump_mark(a, b) (0)
 301#endif
 302
 303#ifdef CONFIG_NF_CONNTRACK_SECMARK
 304static inline int
 305ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
 306{
 307        struct nlattr *nest_secctx;
 308        int len, ret;
 309        char *secctx;
 310
 311        ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
 312        if (ret)
 313                return 0;
 314
 315        ret = -1;
 316        nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
 317        if (!nest_secctx)
 318                goto nla_put_failure;
 319
 320        if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
 321                goto nla_put_failure;
 322        nla_nest_end(skb, nest_secctx);
 323
 324        ret = 0;
 325nla_put_failure:
 326        security_release_secctx(secctx, len);
 327        return ret;
 328}
 329#else
 330#define ctnetlink_dump_secctx(a, b) (0)
 331#endif
 332
 333#ifdef CONFIG_NF_CONNTRACK_LABELS
 334static int ctnetlink_label_size(const struct nf_conn *ct)
 335{
 336        struct nf_conn_labels *labels = nf_ct_labels_find(ct);
 337
 338        if (!labels)
 339                return 0;
 340        return nla_total_size(labels->words * sizeof(long));
 341}
 342
 343static int
 344ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
 345{
 346        struct nf_conn_labels *labels = nf_ct_labels_find(ct);
 347        unsigned int len, i;
 348
 349        if (!labels)
 350                return 0;
 351
 352        len = labels->words * sizeof(long);
 353        i = 0;
 354        do {
 355                if (labels->bits[i] != 0)
 356                        return nla_put(skb, CTA_LABELS, len, labels->bits);
 357                i++;
 358        } while (i < labels->words);
 359
 360        return 0;
 361}
 362#else
 363#define ctnetlink_dump_labels(a, b) (0)
 364#define ctnetlink_label_size(a) (0)
 365#endif
 366
 367#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
 368
 369static inline int
 370ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
 371{
 372        struct nlattr *nest_parms;
 373
 374        if (!(ct->status & IPS_EXPECTED))
 375                return 0;
 376
 377        nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
 378        if (!nest_parms)
 379                goto nla_put_failure;
 380        if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
 381                goto nla_put_failure;
 382        nla_nest_end(skb, nest_parms);
 383
 384        return 0;
 385
 386nla_put_failure:
 387        return -1;
 388}
 389
 390static int
 391dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
 392{
 393        struct nlattr *nest_parms;
 394
 395        nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
 396        if (!nest_parms)
 397                goto nla_put_failure;
 398
 399        if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
 400                         htonl(seq->correction_pos)) ||
 401            nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
 402                         htonl(seq->offset_before)) ||
 403            nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
 404                         htonl(seq->offset_after)))
 405                goto nla_put_failure;
 406
 407        nla_nest_end(skb, nest_parms);
 408
 409        return 0;
 410
 411nla_put_failure:
 412        return -1;
 413}
 414
 415static inline int
 416ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
 417{
 418        struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
 419        struct nf_ct_seqadj *seq;
 420
 421        if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
 422                return 0;
 423
 424        seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
 425        if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
 426                return -1;
 427
 428        seq = &seqadj->seq[IP_CT_DIR_REPLY];
 429        if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
 430                return -1;
 431
 432        return 0;
 433}
 434
 435static inline int
 436ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
 437{
 438        if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
 439                goto nla_put_failure;
 440        return 0;
 441
 442nla_put_failure:
 443        return -1;
 444}
 445
 446static inline int
 447ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
 448{
 449        if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
 450                goto nla_put_failure;
 451        return 0;
 452
 453nla_put_failure:
 454        return -1;
 455}
 456
 457static int
 458ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
 459                    struct nf_conn *ct)
 460{
 461        struct nlmsghdr *nlh;
 462        struct nfgenmsg *nfmsg;
 463        struct nlattr *nest_parms;
 464        unsigned int flags = portid ? NLM_F_MULTI : 0, event;
 465
 466        event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
 467        nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
 468        if (nlh == NULL)
 469                goto nlmsg_failure;
 470
 471        nfmsg = nlmsg_data(nlh);
 472        nfmsg->nfgen_family = nf_ct_l3num(ct);
 473        nfmsg->version      = NFNETLINK_V0;
 474        nfmsg->res_id       = 0;
 475
 476        nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
 477        if (!nest_parms)
 478                goto nla_put_failure;
 479        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
 480                goto nla_put_failure;
 481        nla_nest_end(skb, nest_parms);
 482
 483        nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
 484        if (!nest_parms)
 485                goto nla_put_failure;
 486        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
 487                goto nla_put_failure;
 488        nla_nest_end(skb, nest_parms);
 489
 490        if (nf_ct_zone(ct) &&
 491            nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
 492                goto nla_put_failure;
 493
 494        if (ctnetlink_dump_status(skb, ct) < 0 ||
 495            ctnetlink_dump_timeout(skb, ct) < 0 ||
 496            ctnetlink_dump_acct(skb, ct, type) < 0 ||
 497            ctnetlink_dump_timestamp(skb, ct) < 0 ||
 498            ctnetlink_dump_protoinfo(skb, ct) < 0 ||
 499            ctnetlink_dump_helpinfo(skb, ct) < 0 ||
 500            ctnetlink_dump_mark(skb, ct) < 0 ||
 501            ctnetlink_dump_secctx(skb, ct) < 0 ||
 502            ctnetlink_dump_labels(skb, ct) < 0 ||
 503            ctnetlink_dump_id(skb, ct) < 0 ||
 504            ctnetlink_dump_use(skb, ct) < 0 ||
 505            ctnetlink_dump_master(skb, ct) < 0 ||
 506            ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
 507                goto nla_put_failure;
 508
 509        nlmsg_end(skb, nlh);
 510        return skb->len;
 511
 512nlmsg_failure:
 513nla_put_failure:
 514        nlmsg_cancel(skb, nlh);
 515        return -1;
 516}
 517
 518static inline size_t
 519ctnetlink_proto_size(const struct nf_conn *ct)
 520{
 521        struct nf_conntrack_l3proto *l3proto;
 522        struct nf_conntrack_l4proto *l4proto;
 523        size_t len = 0;
 524
 525        rcu_read_lock();
 526        l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
 527        len += l3proto->nla_size;
 528
 529        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
 530        len += l4proto->nla_size;
 531        rcu_read_unlock();
 532
 533        return len;
 534}
 535
 536static inline size_t
 537ctnetlink_acct_size(const struct nf_conn *ct)
 538{
 539        if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
 540                return 0;
 541        return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
 542               + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
 543               + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
 544               ;
 545}
 546
 547static inline int
 548ctnetlink_secctx_size(const struct nf_conn *ct)
 549{
 550#ifdef CONFIG_NF_CONNTRACK_SECMARK
 551        int len, ret;
 552
 553        ret = security_secid_to_secctx(ct->secmark, NULL, &len);
 554        if (ret)
 555                return 0;
 556
 557        return nla_total_size(0) /* CTA_SECCTX */
 558               + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
 559#else
 560        return 0;
 561#endif
 562}
 563
 564static inline size_t
 565ctnetlink_timestamp_size(const struct nf_conn *ct)
 566{
 567#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
 568        if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
 569                return 0;
 570        return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
 571#else
 572        return 0;
 573#endif
 574}
 575
 576static inline size_t
 577ctnetlink_nlmsg_size(const struct nf_conn *ct)
 578{
 579        return NLMSG_ALIGN(sizeof(struct nfgenmsg))
 580               + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
 581               + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
 582               + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
 583               + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
 584               + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
 585               + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
 586               + ctnetlink_acct_size(ct)
 587               + ctnetlink_timestamp_size(ct)
 588               + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
 589               + nla_total_size(0) /* CTA_PROTOINFO */
 590               + nla_total_size(0) /* CTA_HELP */
 591               + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
 592               + ctnetlink_secctx_size(ct)
 593#ifdef CONFIG_NF_NAT_NEEDED
 594               + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
 595               + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
 596#endif
 597#ifdef CONFIG_NF_CONNTRACK_MARK
 598               + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
 599#endif
 600               + ctnetlink_proto_size(ct)
 601               + ctnetlink_label_size(ct)
 602               ;
 603}
 604
 605#ifdef CONFIG_NF_CONNTRACK_EVENTS
 606static int
 607ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
 608{
 609        struct net *net;
 610        struct nlmsghdr *nlh;
 611        struct nfgenmsg *nfmsg;
 612        struct nlattr *nest_parms;
 613        struct nf_conn *ct = item->ct;
 614        struct sk_buff *skb;
 615        unsigned int type;
 616        unsigned int flags = 0, group;
 617        int err;
 618
 619        /* ignore our fake conntrack entry */
 620        if (nf_ct_is_untracked(ct))
 621                return 0;
 622
 623        if (events & (1 << IPCT_DESTROY)) {
 624                type = IPCTNL_MSG_CT_DELETE;
 625                group = NFNLGRP_CONNTRACK_DESTROY;
 626        } else  if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
 627                type = IPCTNL_MSG_CT_NEW;
 628                flags = NLM_F_CREATE|NLM_F_EXCL;
 629                group = NFNLGRP_CONNTRACK_NEW;
 630        } else  if (events) {
 631                type = IPCTNL_MSG_CT_NEW;
 632                group = NFNLGRP_CONNTRACK_UPDATE;
 633        } else
 634                return 0;
 635
 636        net = nf_ct_net(ct);
 637        if (!item->report && !nfnetlink_has_listeners(net, group))
 638                return 0;
 639
 640        skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
 641        if (skb == NULL)
 642                goto errout;
 643
 644        type |= NFNL_SUBSYS_CTNETLINK << 8;
 645        nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
 646        if (nlh == NULL)
 647                goto nlmsg_failure;
 648
 649        nfmsg = nlmsg_data(nlh);
 650        nfmsg->nfgen_family = nf_ct_l3num(ct);
 651        nfmsg->version  = NFNETLINK_V0;
 652        nfmsg->res_id   = 0;
 653
 654        rcu_read_lock();
 655        nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
 656        if (!nest_parms)
 657                goto nla_put_failure;
 658        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
 659                goto nla_put_failure;
 660        nla_nest_end(skb, nest_parms);
 661
 662        nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
 663        if (!nest_parms)
 664                goto nla_put_failure;
 665        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
 666                goto nla_put_failure;
 667        nla_nest_end(skb, nest_parms);
 668
 669        if (nf_ct_zone(ct) &&
 670            nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
 671                goto nla_put_failure;
 672
 673        if (ctnetlink_dump_id(skb, ct) < 0)
 674                goto nla_put_failure;
 675
 676        if (ctnetlink_dump_status(skb, ct) < 0)
 677                goto nla_put_failure;
 678
 679        if (events & (1 << IPCT_DESTROY)) {
 680                if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
 681                    ctnetlink_dump_timestamp(skb, ct) < 0)
 682                        goto nla_put_failure;
 683        } else {
 684                if (ctnetlink_dump_timeout(skb, ct) < 0)
 685                        goto nla_put_failure;
 686
 687                if (events & (1 << IPCT_PROTOINFO)
 688                    && ctnetlink_dump_protoinfo(skb, ct) < 0)
 689                        goto nla_put_failure;
 690
 691                if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
 692                    && ctnetlink_dump_helpinfo(skb, ct) < 0)
 693                        goto nla_put_failure;
 694
 695#ifdef CONFIG_NF_CONNTRACK_SECMARK
 696                if ((events & (1 << IPCT_SECMARK) || ct->secmark)
 697                    && ctnetlink_dump_secctx(skb, ct) < 0)
 698                        goto nla_put_failure;
 699#endif
 700                if (events & (1 << IPCT_LABEL) &&
 701                     ctnetlink_dump_labels(skb, ct) < 0)
 702                        goto nla_put_failure;
 703
 704                if (events & (1 << IPCT_RELATED) &&
 705                    ctnetlink_dump_master(skb, ct) < 0)
 706                        goto nla_put_failure;
 707
 708                if (events & (1 << IPCT_SEQADJ) &&
 709                    ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
 710                        goto nla_put_failure;
 711        }
 712
 713#ifdef CONFIG_NF_CONNTRACK_MARK
 714        if ((events & (1 << IPCT_MARK) || ct->mark)
 715            && ctnetlink_dump_mark(skb, ct) < 0)
 716                goto nla_put_failure;
 717#endif
 718        rcu_read_unlock();
 719
 720        nlmsg_end(skb, nlh);
 721        err = nfnetlink_send(skb, net, item->portid, group, item->report,
 722                             GFP_ATOMIC);
 723        if (err == -ENOBUFS || err == -EAGAIN)
 724                return -ENOBUFS;
 725
 726        return 0;
 727
 728nla_put_failure:
 729        rcu_read_unlock();
 730        nlmsg_cancel(skb, nlh);
 731nlmsg_failure:
 732        kfree_skb(skb);
 733errout:
 734        if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
 735                return -ENOBUFS;
 736
 737        return 0;
 738}
 739#endif /* CONFIG_NF_CONNTRACK_EVENTS */
 740
 741static int ctnetlink_done(struct netlink_callback *cb)
 742{
 743        if (cb->args[1])
 744                nf_ct_put((struct nf_conn *)cb->args[1]);
 745        if (cb->data)
 746                kfree(cb->data);
 747        return 0;
 748}
 749
 750struct ctnetlink_dump_filter {
 751        struct {
 752                u_int32_t val;
 753                u_int32_t mask;
 754        } mark;
 755};
 756
 757static int
 758ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 759{
 760        struct net *net = sock_net(skb->sk);
 761        struct nf_conn *ct, *last;
 762        struct nf_conntrack_tuple_hash *h;
 763        struct hlist_nulls_node *n;
 764        struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
 765        u_int8_t l3proto = nfmsg->nfgen_family;
 766        int res;
 767#ifdef CONFIG_NF_CONNTRACK_MARK
 768        const struct ctnetlink_dump_filter *filter = cb->data;
 769#endif
 770
 771        spin_lock_bh(&nf_conntrack_lock);
 772        last = (struct nf_conn *)cb->args[1];
 773        for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
 774restart:
 775                hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
 776                                         hnnode) {
 777                        if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
 778                                continue;
 779                        ct = nf_ct_tuplehash_to_ctrack(h);
 780                        /* Dump entries of a given L3 protocol number.
 781                         * If it is not specified, ie. l3proto == 0,
 782                         * then dump everything. */
 783                        if (l3proto && nf_ct_l3num(ct) != l3proto)
 784                                continue;
 785                        if (cb->args[1]) {
 786                                if (ct != last)
 787                                        continue;
 788                                cb->args[1] = 0;
 789                        }
 790#ifdef CONFIG_NF_CONNTRACK_MARK
 791                        if (filter && !((ct->mark & filter->mark.mask) ==
 792                                        filter->mark.val)) {
 793                                continue;
 794                        }
 795#endif
 796                        rcu_read_lock();
 797                        res =
 798                        ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
 799                                            cb->nlh->nlmsg_seq,
 800                                            NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
 801                                            ct);
 802                        rcu_read_unlock();
 803                        if (res < 0) {
 804                                nf_conntrack_get(&ct->ct_general);
 805                                cb->args[1] = (unsigned long)ct;
 806                                goto out;
 807                        }
 808                }
 809                if (cb->args[1]) {
 810                        cb->args[1] = 0;
 811                        goto restart;
 812                }
 813        }
 814out:
 815        spin_unlock_bh(&nf_conntrack_lock);
 816        if (last)
 817                nf_ct_put(last);
 818
 819        return skb->len;
 820}
 821
 822static inline int
 823ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
 824{
 825        struct nlattr *tb[CTA_IP_MAX+1];
 826        struct nf_conntrack_l3proto *l3proto;
 827        int ret = 0;
 828
 829        ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
 830        if (ret < 0)
 831                return ret;
 832
 833        rcu_read_lock();
 834        l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
 835
 836        if (likely(l3proto->nlattr_to_tuple)) {
 837                ret = nla_validate_nested(attr, CTA_IP_MAX,
 838                                          l3proto->nla_policy);
 839                if (ret == 0)
 840                        ret = l3proto->nlattr_to_tuple(tb, tuple);
 841        }
 842
 843        rcu_read_unlock();
 844
 845        return ret;
 846}
 847
 848static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
 849        [CTA_PROTO_NUM] = { .type = NLA_U8 },
 850};
 851
 852static inline int
 853ctnetlink_parse_tuple_proto(struct nlattr *attr,
 854                            struct nf_conntrack_tuple *tuple)
 855{
 856        struct nlattr *tb[CTA_PROTO_MAX+1];
 857        struct nf_conntrack_l4proto *l4proto;
 858        int ret = 0;
 859
 860        ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
 861        if (ret < 0)
 862                return ret;
 863
 864        if (!tb[CTA_PROTO_NUM])
 865                return -EINVAL;
 866        tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
 867
 868        rcu_read_lock();
 869        l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
 870
 871        if (likely(l4proto->nlattr_to_tuple)) {
 872                ret = nla_validate_nested(attr, CTA_PROTO_MAX,
 873                                          l4proto->nla_policy);
 874                if (ret == 0)
 875                        ret = l4proto->nlattr_to_tuple(tb, tuple);
 876        }
 877
 878        rcu_read_unlock();
 879
 880        return ret;
 881}
 882
 883static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
 884        [CTA_TUPLE_IP]          = { .type = NLA_NESTED },
 885        [CTA_TUPLE_PROTO]       = { .type = NLA_NESTED },
 886};
 887
 888static int
 889ctnetlink_parse_tuple(const struct nlattr * const cda[],
 890                      struct nf_conntrack_tuple *tuple,
 891                      enum ctattr_type type, u_int8_t l3num)
 892{
 893        struct nlattr *tb[CTA_TUPLE_MAX+1];
 894        int err;
 895
 896        memset(tuple, 0, sizeof(*tuple));
 897
 898        err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
 899        if (err < 0)
 900                return err;
 901
 902        if (!tb[CTA_TUPLE_IP])
 903                return -EINVAL;
 904
 905        tuple->src.l3num = l3num;
 906
 907        err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
 908        if (err < 0)
 909                return err;
 910
 911        if (!tb[CTA_TUPLE_PROTO])
 912                return -EINVAL;
 913
 914        err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
 915        if (err < 0)
 916                return err;
 917
 918        /* orig and expect tuples get DIR_ORIGINAL */
 919        if (type == CTA_TUPLE_REPLY)
 920                tuple->dst.dir = IP_CT_DIR_REPLY;
 921        else
 922                tuple->dst.dir = IP_CT_DIR_ORIGINAL;
 923
 924        return 0;
 925}
 926
 927static int
 928ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
 929{
 930        if (attr)
 931#ifdef CONFIG_NF_CONNTRACK_ZONES
 932                *zone = ntohs(nla_get_be16(attr));
 933#else
 934                return -EOPNOTSUPP;
 935#endif
 936        else
 937                *zone = 0;
 938
 939        return 0;
 940}
 941
 942static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
 943        [CTA_HELP_NAME]         = { .type = NLA_NUL_STRING,
 944                                    .len = NF_CT_HELPER_NAME_LEN - 1 },
 945};
 946
 947static inline int
 948ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
 949                     struct nlattr **helpinfo)
 950{
 951        int err;
 952        struct nlattr *tb[CTA_HELP_MAX+1];
 953
 954        err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
 955        if (err < 0)
 956                return err;
 957
 958        if (!tb[CTA_HELP_NAME])
 959                return -EINVAL;
 960
 961        *helper_name = nla_data(tb[CTA_HELP_NAME]);
 962
 963        if (tb[CTA_HELP_INFO])
 964                *helpinfo = tb[CTA_HELP_INFO];
 965
 966        return 0;
 967}
 968
 969#define __CTA_LABELS_MAX_LENGTH ((XT_CONNLABEL_MAXBIT + 1) / BITS_PER_BYTE)
 970static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
 971        [CTA_TUPLE_ORIG]        = { .type = NLA_NESTED },
 972        [CTA_TUPLE_REPLY]       = { .type = NLA_NESTED },
 973        [CTA_STATUS]            = { .type = NLA_U32 },
 974        [CTA_PROTOINFO]         = { .type = NLA_NESTED },
 975        [CTA_HELP]              = { .type = NLA_NESTED },
 976        [CTA_NAT_SRC]           = { .type = NLA_NESTED },
 977        [CTA_TIMEOUT]           = { .type = NLA_U32 },
 978        [CTA_MARK]              = { .type = NLA_U32 },
 979        [CTA_ID]                = { .type = NLA_U32 },
 980        [CTA_NAT_DST]           = { .type = NLA_NESTED },
 981        [CTA_TUPLE_MASTER]      = { .type = NLA_NESTED },
 982        [CTA_NAT_SEQ_ADJ_ORIG]  = { .type = NLA_NESTED },
 983        [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
 984        [CTA_ZONE]              = { .type = NLA_U16 },
 985        [CTA_MARK_MASK]         = { .type = NLA_U32 },
 986        [CTA_LABELS]            = { .type = NLA_BINARY,
 987                                    .len = __CTA_LABELS_MAX_LENGTH },
 988        [CTA_LABELS_MASK]       = { .type = NLA_BINARY,
 989                                    .len = __CTA_LABELS_MAX_LENGTH },
 990};
 991
 992static int
 993ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
 994                        const struct nlmsghdr *nlh,
 995                        const struct nlattr * const cda[])
 996{
 997        struct net *net = sock_net(ctnl);
 998        struct nf_conntrack_tuple_hash *h;
 999        struct nf_conntrack_tuple tuple;
1000        struct nf_conn *ct;
1001        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1002        u_int8_t u3 = nfmsg->nfgen_family;
1003        u16 zone;
1004        int err;
1005
1006        err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1007        if (err < 0)
1008                return err;
1009
1010        if (cda[CTA_TUPLE_ORIG])
1011                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1012        else if (cda[CTA_TUPLE_REPLY])
1013                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1014        else {
1015                /* Flush the whole table */
1016                nf_conntrack_flush_report(net,
1017                                         NETLINK_CB(skb).portid,
1018                                         nlmsg_report(nlh));
1019                return 0;
1020        }
1021
1022        if (err < 0)
1023                return err;
1024
1025        h = nf_conntrack_find_get(net, zone, &tuple);
1026        if (!h)
1027                return -ENOENT;
1028
1029        ct = nf_ct_tuplehash_to_ctrack(h);
1030
1031        if (cda[CTA_ID]) {
1032                u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1033                if (id != (u32)(unsigned long)ct) {
1034                        nf_ct_put(ct);
1035                        return -ENOENT;
1036                }
1037        }
1038
1039        if (del_timer(&ct->timeout))
1040                nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1041
1042        nf_ct_put(ct);
1043
1044        return 0;
1045}
1046
1047static int
1048ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1049                        const struct nlmsghdr *nlh,
1050                        const struct nlattr * const cda[])
1051{
1052        struct net *net = sock_net(ctnl);
1053        struct nf_conntrack_tuple_hash *h;
1054        struct nf_conntrack_tuple tuple;
1055        struct nf_conn *ct;
1056        struct sk_buff *skb2 = NULL;
1057        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1058        u_int8_t u3 = nfmsg->nfgen_family;
1059        u16 zone;
1060        int err;
1061
1062        if (nlh->nlmsg_flags & NLM_F_DUMP) {
1063                struct netlink_dump_control c = {
1064                        .dump = ctnetlink_dump_table,
1065                        .done = ctnetlink_done,
1066                };
1067#ifdef CONFIG_NF_CONNTRACK_MARK
1068                if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1069                        struct ctnetlink_dump_filter *filter;
1070
1071                        filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1072                                         GFP_ATOMIC);
1073                        if (filter == NULL)
1074                                return -ENOMEM;
1075
1076                        filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1077                        filter->mark.mask =
1078                                ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1079                        c.data = filter;
1080                }
1081#endif
1082                return netlink_dump_start(ctnl, skb, nlh, &c);
1083        }
1084
1085        err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1086        if (err < 0)
1087                return err;
1088
1089        if (cda[CTA_TUPLE_ORIG])
1090                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1091        else if (cda[CTA_TUPLE_REPLY])
1092                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1093        else
1094                return -EINVAL;
1095
1096        if (err < 0)
1097                return err;
1098
1099        h = nf_conntrack_find_get(net, zone, &tuple);
1100        if (!h)
1101                return -ENOENT;
1102
1103        ct = nf_ct_tuplehash_to_ctrack(h);
1104
1105        err = -ENOMEM;
1106        skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1107        if (skb2 == NULL) {
1108                nf_ct_put(ct);
1109                return -ENOMEM;
1110        }
1111
1112        rcu_read_lock();
1113        err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1114                                  NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1115        rcu_read_unlock();
1116        nf_ct_put(ct);
1117        if (err <= 0)
1118                goto free;
1119
1120        err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1121        if (err < 0)
1122                goto out;
1123
1124        return 0;
1125
1126free:
1127        kfree_skb(skb2);
1128out:
1129        /* this avoids a loop in nfnetlink. */
1130        return err == -EAGAIN ? -ENOBUFS : err;
1131}
1132
1133static int ctnetlink_done_list(struct netlink_callback *cb)
1134{
1135        if (cb->args[1])
1136                nf_ct_put((struct nf_conn *)cb->args[1]);
1137        return 0;
1138}
1139
1140static int
1141ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1142                    struct hlist_nulls_head *list)
1143{
1144        struct nf_conn *ct, *last;
1145        struct nf_conntrack_tuple_hash *h;
1146        struct hlist_nulls_node *n;
1147        struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1148        u_int8_t l3proto = nfmsg->nfgen_family;
1149        int res;
1150
1151        if (cb->args[2])
1152                return 0;
1153
1154        spin_lock_bh(&nf_conntrack_lock);
1155        last = (struct nf_conn *)cb->args[1];
1156restart:
1157        hlist_nulls_for_each_entry(h, n, list, hnnode) {
1158                ct = nf_ct_tuplehash_to_ctrack(h);
1159                if (l3proto && nf_ct_l3num(ct) != l3proto)
1160                        continue;
1161                if (cb->args[1]) {
1162                        if (ct != last)
1163                                continue;
1164                        cb->args[1] = 0;
1165                }
1166                rcu_read_lock();
1167                res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1168                                          cb->nlh->nlmsg_seq,
1169                                          NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1170                                          ct);
1171                rcu_read_unlock();
1172                if (res < 0) {
1173                        nf_conntrack_get(&ct->ct_general);
1174                        cb->args[1] = (unsigned long)ct;
1175                        goto out;
1176                }
1177        }
1178        if (cb->args[1]) {
1179                cb->args[1] = 0;
1180                goto restart;
1181        } else
1182                cb->args[2] = 1;
1183out:
1184        spin_unlock_bh(&nf_conntrack_lock);
1185        if (last)
1186                nf_ct_put(last);
1187
1188        return skb->len;
1189}
1190
1191static int
1192ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1193{
1194        struct net *net = sock_net(skb->sk);
1195
1196        return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1197}
1198
1199static int
1200ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1201                       const struct nlmsghdr *nlh,
1202                       const struct nlattr * const cda[])
1203{
1204        if (nlh->nlmsg_flags & NLM_F_DUMP) {
1205                struct netlink_dump_control c = {
1206                        .dump = ctnetlink_dump_dying,
1207                        .done = ctnetlink_done_list,
1208                };
1209                return netlink_dump_start(ctnl, skb, nlh, &c);
1210        }
1211
1212        return -EOPNOTSUPP;
1213}
1214
1215static int
1216ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1217{
1218        struct net *net = sock_net(skb->sk);
1219
1220        return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1221}
1222
1223static int
1224ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1225                             const struct nlmsghdr *nlh,
1226                             const struct nlattr * const cda[])
1227{
1228        if (nlh->nlmsg_flags & NLM_F_DUMP) {
1229                struct netlink_dump_control c = {
1230                        .dump = ctnetlink_dump_unconfirmed,
1231                        .done = ctnetlink_done_list,
1232                };
1233                return netlink_dump_start(ctnl, skb, nlh, &c);
1234        }
1235
1236        return -EOPNOTSUPP;
1237}
1238
1239#ifdef CONFIG_NF_NAT_NEEDED
1240static int
1241ctnetlink_parse_nat_setup(struct nf_conn *ct,
1242                          enum nf_nat_manip_type manip,
1243                          const struct nlattr *attr)
1244{
1245        typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1246        int err;
1247
1248        parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1249        if (!parse_nat_setup) {
1250#ifdef CONFIG_MODULES
1251                rcu_read_unlock();
1252                nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1253                if (request_module("nf-nat") < 0) {
1254                        nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1255                        rcu_read_lock();
1256                        return -EOPNOTSUPP;
1257                }
1258                nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1259                rcu_read_lock();
1260                if (nfnetlink_parse_nat_setup_hook)
1261                        return -EAGAIN;
1262#endif
1263                return -EOPNOTSUPP;
1264        }
1265
1266        err = parse_nat_setup(ct, manip, attr);
1267        if (err == -EAGAIN) {
1268#ifdef CONFIG_MODULES
1269                rcu_read_unlock();
1270                nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1271                if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1272                        nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1273                        rcu_read_lock();
1274                        return -EOPNOTSUPP;
1275                }
1276                nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1277                rcu_read_lock();
1278#else
1279                err = -EOPNOTSUPP;
1280#endif
1281        }
1282        return err;
1283}
1284#endif
1285
1286static int
1287ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1288{
1289        unsigned long d;
1290        unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1291        d = ct->status ^ status;
1292
1293        if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1294                /* unchangeable */
1295                return -EBUSY;
1296
1297        if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1298                /* SEEN_REPLY bit can only be set */
1299                return -EBUSY;
1300
1301        if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1302                /* ASSURED bit can only be set */
1303                return -EBUSY;
1304
1305        /* Be careful here, modifying NAT bits can screw up things,
1306         * so don't let users modify them directly if they don't pass
1307         * nf_nat_range. */
1308        ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1309        return 0;
1310}
1311
1312static int
1313ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1314{
1315#ifdef CONFIG_NF_NAT_NEEDED
1316        int ret;
1317
1318        if (cda[CTA_NAT_DST]) {
1319                ret = ctnetlink_parse_nat_setup(ct,
1320                                                NF_NAT_MANIP_DST,
1321                                                cda[CTA_NAT_DST]);
1322                if (ret < 0)
1323                        return ret;
1324        }
1325        if (cda[CTA_NAT_SRC]) {
1326                ret = ctnetlink_parse_nat_setup(ct,
1327                                                NF_NAT_MANIP_SRC,
1328                                                cda[CTA_NAT_SRC]);
1329                if (ret < 0)
1330                        return ret;
1331        }
1332        return 0;
1333#else
1334        return -EOPNOTSUPP;
1335#endif
1336}
1337
1338static inline int
1339ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1340{
1341        struct nf_conntrack_helper *helper;
1342        struct nf_conn_help *help = nfct_help(ct);
1343        char *helpname = NULL;
1344        struct nlattr *helpinfo = NULL;
1345        int err;
1346
1347        /* don't change helper of sibling connections */
1348        if (ct->master)
1349                return -EBUSY;
1350
1351        err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1352        if (err < 0)
1353                return err;
1354
1355        if (!strcmp(helpname, "")) {
1356                if (help && help->helper) {
1357                        /* we had a helper before ... */
1358                        nf_ct_remove_expectations(ct);
1359                        RCU_INIT_POINTER(help->helper, NULL);
1360                }
1361
1362                return 0;
1363        }
1364
1365        helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1366                                            nf_ct_protonum(ct));
1367        if (helper == NULL) {
1368#ifdef CONFIG_MODULES
1369                spin_unlock_bh(&nf_conntrack_lock);
1370
1371                if (request_module("nfct-helper-%s", helpname) < 0) {
1372                        spin_lock_bh(&nf_conntrack_lock);
1373                        return -EOPNOTSUPP;
1374                }
1375
1376                spin_lock_bh(&nf_conntrack_lock);
1377                helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1378                                                    nf_ct_protonum(ct));
1379                if (helper)
1380                        return -EAGAIN;
1381#endif
1382                return -EOPNOTSUPP;
1383        }
1384
1385        if (help) {
1386                if (help->helper == helper) {
1387                        /* update private helper data if allowed. */
1388                        if (helper->from_nlattr)
1389                                helper->from_nlattr(helpinfo, ct);
1390                        return 0;
1391                } else
1392                        return -EBUSY;
1393        }
1394
1395        /* we cannot set a helper for an existing conntrack */
1396        return -EOPNOTSUPP;
1397}
1398
1399static inline int
1400ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1401{
1402        u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1403
1404        if (!del_timer(&ct->timeout))
1405                return -ETIME;
1406
1407        ct->timeout.expires = jiffies + timeout * HZ;
1408        add_timer(&ct->timeout);
1409
1410        return 0;
1411}
1412
1413static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1414        [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
1415        [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
1416        [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
1417};
1418
1419static inline int
1420ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1421{
1422        const struct nlattr *attr = cda[CTA_PROTOINFO];
1423        struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1424        struct nf_conntrack_l4proto *l4proto;
1425        int err = 0;
1426
1427        err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1428        if (err < 0)
1429                return err;
1430
1431        rcu_read_lock();
1432        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1433        if (l4proto->from_nlattr)
1434                err = l4proto->from_nlattr(tb, ct);
1435        rcu_read_unlock();
1436
1437        return err;
1438}
1439
1440static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1441        [CTA_SEQADJ_CORRECTION_POS]     = { .type = NLA_U32 },
1442        [CTA_SEQADJ_OFFSET_BEFORE]      = { .type = NLA_U32 },
1443        [CTA_SEQADJ_OFFSET_AFTER]       = { .type = NLA_U32 },
1444};
1445
1446static inline int
1447change_seq_adj(struct nf_ct_seqadj *seq, const struct nlattr * const attr)
1448{
1449        int err;
1450        struct nlattr *cda[CTA_SEQADJ_MAX+1];
1451
1452        err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1453        if (err < 0)
1454                return err;
1455
1456        if (!cda[CTA_SEQADJ_CORRECTION_POS])
1457                return -EINVAL;
1458
1459        seq->correction_pos =
1460                ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1461
1462        if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1463                return -EINVAL;
1464
1465        seq->offset_before =
1466                ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1467
1468        if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1469                return -EINVAL;
1470
1471        seq->offset_after =
1472                ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1473
1474        return 0;
1475}
1476
1477static int
1478ctnetlink_change_seq_adj(struct nf_conn *ct,
1479                         const struct nlattr * const cda[])
1480{
1481        struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1482        int ret = 0;
1483
1484        if (!seqadj)
1485                return 0;
1486
1487        if (cda[CTA_SEQ_ADJ_ORIG]) {
1488                ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1489                                     cda[CTA_SEQ_ADJ_ORIG]);
1490                if (ret < 0)
1491                        return ret;
1492
1493                ct->status |= IPS_SEQ_ADJUST;
1494        }
1495
1496        if (cda[CTA_SEQ_ADJ_REPLY]) {
1497                ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1498                                     cda[CTA_SEQ_ADJ_REPLY]);
1499                if (ret < 0)
1500                        return ret;
1501
1502                ct->status |= IPS_SEQ_ADJUST;
1503        }
1504
1505        return 0;
1506}
1507
1508static int
1509ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1510{
1511#ifdef CONFIG_NF_CONNTRACK_LABELS
1512        size_t len = nla_len(cda[CTA_LABELS]);
1513        const void *mask = cda[CTA_LABELS_MASK];
1514
1515        if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1516                return -EINVAL;
1517
1518        if (mask) {
1519                if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1520                    nla_len(cda[CTA_LABELS_MASK]) != len)
1521                        return -EINVAL;
1522                mask = nla_data(cda[CTA_LABELS_MASK]);
1523        }
1524
1525        len /= sizeof(u32);
1526
1527        return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1528#else
1529        return -EOPNOTSUPP;
1530#endif
1531}
1532
1533static int
1534ctnetlink_change_conntrack(struct nf_conn *ct,
1535                           const struct nlattr * const cda[])
1536{
1537        int err;
1538
1539        /* only allow NAT changes and master assignation for new conntracks */
1540        if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1541                return -EOPNOTSUPP;
1542
1543        if (cda[CTA_HELP]) {
1544                err = ctnetlink_change_helper(ct, cda);
1545                if (err < 0)
1546                        return err;
1547        }
1548
1549        if (cda[CTA_TIMEOUT]) {
1550                err = ctnetlink_change_timeout(ct, cda);
1551                if (err < 0)
1552                        return err;
1553        }
1554
1555        if (cda[CTA_STATUS]) {
1556                err = ctnetlink_change_status(ct, cda);
1557                if (err < 0)
1558                        return err;
1559        }
1560
1561        if (cda[CTA_PROTOINFO]) {
1562                err = ctnetlink_change_protoinfo(ct, cda);
1563                if (err < 0)
1564                        return err;
1565        }
1566
1567#if defined(CONFIG_NF_CONNTRACK_MARK)
1568        if (cda[CTA_MARK])
1569                ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1570#endif
1571
1572        if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1573                err = ctnetlink_change_seq_adj(ct, cda);
1574                if (err < 0)
1575                        return err;
1576        }
1577
1578        if (cda[CTA_LABELS]) {
1579                err = ctnetlink_attach_labels(ct, cda);
1580                if (err < 0)
1581                        return err;
1582        }
1583
1584        return 0;
1585}
1586
1587static struct nf_conn *
1588ctnetlink_create_conntrack(struct net *net, u16 zone,
1589                           const struct nlattr * const cda[],
1590                           struct nf_conntrack_tuple *otuple,
1591                           struct nf_conntrack_tuple *rtuple,
1592                           u8 u3)
1593{
1594        struct nf_conn *ct;
1595        int err = -EINVAL;
1596        struct nf_conntrack_helper *helper;
1597        struct nf_conn_tstamp *tstamp;
1598
1599        ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1600        if (IS_ERR(ct))
1601                return ERR_PTR(-ENOMEM);
1602
1603        if (!cda[CTA_TIMEOUT])
1604                goto err1;
1605        ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1606
1607        ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1608
1609        rcu_read_lock();
1610        if (cda[CTA_HELP]) {
1611                char *helpname = NULL;
1612                struct nlattr *helpinfo = NULL;
1613
1614                err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1615                if (err < 0)
1616                        goto err2;
1617
1618                helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1619                                                    nf_ct_protonum(ct));
1620                if (helper == NULL) {
1621                        rcu_read_unlock();
1622#ifdef CONFIG_MODULES
1623                        if (request_module("nfct-helper-%s", helpname) < 0) {
1624                                err = -EOPNOTSUPP;
1625                                goto err1;
1626                        }
1627
1628                        rcu_read_lock();
1629                        helper = __nf_conntrack_helper_find(helpname,
1630                                                            nf_ct_l3num(ct),
1631                                                            nf_ct_protonum(ct));
1632                        if (helper) {
1633                                err = -EAGAIN;
1634                                goto err2;
1635                        }
1636                        rcu_read_unlock();
1637#endif
1638                        err = -EOPNOTSUPP;
1639                        goto err1;
1640                } else {
1641                        struct nf_conn_help *help;
1642
1643                        help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1644                        if (help == NULL) {
1645                                err = -ENOMEM;
1646                                goto err2;
1647                        }
1648                        /* set private helper data if allowed. */
1649                        if (helper->from_nlattr)
1650                                helper->from_nlattr(helpinfo, ct);
1651
1652                        /* not in hash table yet so not strictly necessary */
1653                        RCU_INIT_POINTER(help->helper, helper);
1654                }
1655        } else {
1656                /* try an implicit helper assignation */
1657                err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1658                if (err < 0)
1659                        goto err2;
1660        }
1661
1662        if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1663                err = ctnetlink_change_nat(ct, cda);
1664                if (err < 0)
1665                        goto err2;
1666        }
1667
1668        nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1669        nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1670        nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1671        nf_ct_labels_ext_add(ct);
1672
1673        /* we must add conntrack extensions before confirmation. */
1674        ct->status |= IPS_CONFIRMED;
1675
1676        if (cda[CTA_STATUS]) {
1677                err = ctnetlink_change_status(ct, cda);
1678                if (err < 0)
1679                        goto err2;
1680        }
1681
1682        if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1683                err = ctnetlink_change_seq_adj(ct, cda);
1684                if (err < 0)
1685                        goto err2;
1686        }
1687
1688        memset(&ct->proto, 0, sizeof(ct->proto));
1689        if (cda[CTA_PROTOINFO]) {
1690                err = ctnetlink_change_protoinfo(ct, cda);
1691                if (err < 0)
1692                        goto err2;
1693        }
1694
1695#if defined(CONFIG_NF_CONNTRACK_MARK)
1696        if (cda[CTA_MARK])
1697                ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1698#endif
1699
1700        /* setup master conntrack: this is a confirmed expectation */
1701        if (cda[CTA_TUPLE_MASTER]) {
1702                struct nf_conntrack_tuple master;
1703                struct nf_conntrack_tuple_hash *master_h;
1704                struct nf_conn *master_ct;
1705
1706                err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1707                if (err < 0)
1708                        goto err2;
1709
1710                master_h = nf_conntrack_find_get(net, zone, &master);
1711                if (master_h == NULL) {
1712                        err = -ENOENT;
1713                        goto err2;
1714                }
1715                master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1716                __set_bit(IPS_EXPECTED_BIT, &ct->status);
1717                ct->master = master_ct;
1718        }
1719        tstamp = nf_conn_tstamp_find(ct);
1720        if (tstamp)
1721                tstamp->start = ktime_to_ns(ktime_get_real());
1722
1723        err = nf_conntrack_hash_check_insert(ct);
1724        if (err < 0)
1725                goto err2;
1726
1727        rcu_read_unlock();
1728
1729        return ct;
1730
1731err2:
1732        rcu_read_unlock();
1733err1:
1734        nf_conntrack_free(ct);
1735        return ERR_PTR(err);
1736}
1737
1738static int
1739ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1740                        const struct nlmsghdr *nlh,
1741                        const struct nlattr * const cda[])
1742{
1743        struct net *net = sock_net(ctnl);
1744        struct nf_conntrack_tuple otuple, rtuple;
1745        struct nf_conntrack_tuple_hash *h = NULL;
1746        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1747        struct nf_conn *ct;
1748        u_int8_t u3 = nfmsg->nfgen_family;
1749        u16 zone;
1750        int err;
1751
1752        err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1753        if (err < 0)
1754                return err;
1755
1756        if (cda[CTA_TUPLE_ORIG]) {
1757                err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1758                if (err < 0)
1759                        return err;
1760        }
1761
1762        if (cda[CTA_TUPLE_REPLY]) {
1763                err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1764                if (err < 0)
1765                        return err;
1766        }
1767
1768        if (cda[CTA_TUPLE_ORIG])
1769                h = nf_conntrack_find_get(net, zone, &otuple);
1770        else if (cda[CTA_TUPLE_REPLY])
1771                h = nf_conntrack_find_get(net, zone, &rtuple);
1772
1773        if (h == NULL) {
1774                err = -ENOENT;
1775                if (nlh->nlmsg_flags & NLM_F_CREATE) {
1776                        enum ip_conntrack_events events;
1777
1778                        if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1779                                return -EINVAL;
1780
1781                        ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1782                                                        &rtuple, u3);
1783                        if (IS_ERR(ct))
1784                                return PTR_ERR(ct);
1785
1786                        err = 0;
1787                        if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1788                                events = IPCT_RELATED;
1789                        else
1790                                events = IPCT_NEW;
1791
1792                        if (cda[CTA_LABELS] &&
1793                            ctnetlink_attach_labels(ct, cda) == 0)
1794                                events |= (1 << IPCT_LABEL);
1795
1796                        nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1797                                                      (1 << IPCT_ASSURED) |
1798                                                      (1 << IPCT_HELPER) |
1799                                                      (1 << IPCT_PROTOINFO) |
1800                                                      (1 << IPCT_SEQADJ) |
1801                                                      (1 << IPCT_MARK) | events,
1802                                                      ct, NETLINK_CB(skb).portid,
1803                                                      nlmsg_report(nlh));
1804                        nf_ct_put(ct);
1805                }
1806
1807                return err;
1808        }
1809        /* implicit 'else' */
1810
1811        err = -EEXIST;
1812        ct = nf_ct_tuplehash_to_ctrack(h);
1813        if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1814                spin_lock_bh(&nf_conntrack_lock);
1815                err = ctnetlink_change_conntrack(ct, cda);
1816                spin_unlock_bh(&nf_conntrack_lock);
1817                if (err == 0) {
1818                        nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1819                                                      (1 << IPCT_ASSURED) |
1820                                                      (1 << IPCT_HELPER) |
1821                                                      (1 << IPCT_LABEL) |
1822                                                      (1 << IPCT_PROTOINFO) |
1823                                                      (1 << IPCT_SEQADJ) |
1824                                                      (1 << IPCT_MARK),
1825                                                      ct, NETLINK_CB(skb).portid,
1826                                                      nlmsg_report(nlh));
1827                }
1828        }
1829
1830        nf_ct_put(ct);
1831        return err;
1832}
1833
1834static int
1835ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1836                                __u16 cpu, const struct ip_conntrack_stat *st)
1837{
1838        struct nlmsghdr *nlh;
1839        struct nfgenmsg *nfmsg;
1840        unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1841
1842        event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1843        nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1844        if (nlh == NULL)
1845                goto nlmsg_failure;
1846
1847        nfmsg = nlmsg_data(nlh);
1848        nfmsg->nfgen_family = AF_UNSPEC;
1849        nfmsg->version      = NFNETLINK_V0;
1850        nfmsg->res_id       = htons(cpu);
1851
1852        if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1853            nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1854            nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1855            nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1856            nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1857            nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1858            nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1859            nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1860            nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1861                                htonl(st->insert_failed)) ||
1862            nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1863            nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1864            nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1865            nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1866                                htonl(st->search_restart)))
1867                goto nla_put_failure;
1868
1869        nlmsg_end(skb, nlh);
1870        return skb->len;
1871
1872nla_put_failure:
1873nlmsg_failure:
1874        nlmsg_cancel(skb, nlh);
1875        return -1;
1876}
1877
1878static int
1879ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1880{
1881        int cpu;
1882        struct net *net = sock_net(skb->sk);
1883
1884        if (cb->args[0] == nr_cpu_ids)
1885                return 0;
1886
1887        for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1888                const struct ip_conntrack_stat *st;
1889
1890                if (!cpu_possible(cpu))
1891                        continue;
1892
1893                st = per_cpu_ptr(net->ct.stat, cpu);
1894                if (ctnetlink_ct_stat_cpu_fill_info(skb,
1895                                                    NETLINK_CB(cb->skb).portid,
1896                                                    cb->nlh->nlmsg_seq,
1897                                                    cpu, st) < 0)
1898                                break;
1899        }
1900        cb->args[0] = cpu;
1901
1902        return skb->len;
1903}
1904
1905static int
1906ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1907                      const struct nlmsghdr *nlh,
1908                      const struct nlattr * const cda[])
1909{
1910        if (nlh->nlmsg_flags & NLM_F_DUMP) {
1911                struct netlink_dump_control c = {
1912                        .dump = ctnetlink_ct_stat_cpu_dump,
1913                };
1914                return netlink_dump_start(ctnl, skb, nlh, &c);
1915        }
1916
1917        return 0;
1918}
1919
1920static int
1921ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1922                            struct net *net)
1923{
1924        struct nlmsghdr *nlh;
1925        struct nfgenmsg *nfmsg;
1926        unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1927        unsigned int nr_conntracks = atomic_read(&net->ct.count);
1928
1929        event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1930        nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1931        if (nlh == NULL)
1932                goto nlmsg_failure;
1933
1934        nfmsg = nlmsg_data(nlh);
1935        nfmsg->nfgen_family = AF_UNSPEC;
1936        nfmsg->version      = NFNETLINK_V0;
1937        nfmsg->res_id       = 0;
1938
1939        if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1940                goto nla_put_failure;
1941
1942        nlmsg_end(skb, nlh);
1943        return skb->len;
1944
1945nla_put_failure:
1946nlmsg_failure:
1947        nlmsg_cancel(skb, nlh);
1948        return -1;
1949}
1950
1951static int
1952ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1953                  const struct nlmsghdr *nlh,
1954                  const struct nlattr * const cda[])
1955{
1956        struct sk_buff *skb2;
1957        int err;
1958
1959        skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1960        if (skb2 == NULL)
1961                return -ENOMEM;
1962
1963        err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1964                                          nlh->nlmsg_seq,
1965                                          NFNL_MSG_TYPE(nlh->nlmsg_type),
1966                                          sock_net(skb->sk));
1967        if (err <= 0)
1968                goto free;
1969
1970        err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1971        if (err < 0)
1972                goto out;
1973
1974        return 0;
1975
1976free:
1977        kfree_skb(skb2);
1978out:
1979        /* this avoids a loop in nfnetlink. */
1980        return err == -EAGAIN ? -ENOBUFS : err;
1981}
1982
1983static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1984        [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
1985        [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
1986        [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
1987        [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1988        [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1989        [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING,
1990                                    .len = NF_CT_HELPER_NAME_LEN - 1 },
1991        [CTA_EXPECT_ZONE]       = { .type = NLA_U16 },
1992        [CTA_EXPECT_FLAGS]      = { .type = NLA_U32 },
1993        [CTA_EXPECT_CLASS]      = { .type = NLA_U32 },
1994        [CTA_EXPECT_NAT]        = { .type = NLA_NESTED },
1995        [CTA_EXPECT_FN]         = { .type = NLA_NUL_STRING },
1996};
1997
1998static struct nf_conntrack_expect *
1999ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2000                       struct nf_conntrack_helper *helper,
2001                       struct nf_conntrack_tuple *tuple,
2002                       struct nf_conntrack_tuple *mask);
2003
2004#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2005static size_t
2006ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
2007{
2008        return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2009               + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2010               + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2011               + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2012               + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2013               + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2014               + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2015               + nla_total_size(0) /* CTA_PROTOINFO */
2016               + nla_total_size(0) /* CTA_HELP */
2017               + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2018               + ctnetlink_secctx_size(ct)
2019#ifdef CONFIG_NF_NAT_NEEDED
2020               + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2021               + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2022#endif
2023#ifdef CONFIG_NF_CONNTRACK_MARK
2024               + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2025#endif
2026               + ctnetlink_proto_size(ct)
2027               ;
2028}
2029
2030static int
2031ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
2032{
2033        struct nlattr *nest_parms;
2034
2035        rcu_read_lock();
2036        nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2037        if (!nest_parms)
2038                goto nla_put_failure;
2039        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2040                goto nla_put_failure;
2041        nla_nest_end(skb, nest_parms);
2042
2043        nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2044        if (!nest_parms)
2045                goto nla_put_failure;
2046        if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2047                goto nla_put_failure;
2048        nla_nest_end(skb, nest_parms);
2049
2050        if (nf_ct_zone(ct)) {
2051                if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
2052                        goto nla_put_failure;
2053        }
2054
2055        if (ctnetlink_dump_id(skb, ct) < 0)
2056                goto nla_put_failure;
2057
2058        if (ctnetlink_dump_status(skb, ct) < 0)
2059                goto nla_put_failure;
2060
2061        if (ctnetlink_dump_timeout(skb, ct) < 0)
2062                goto nla_put_failure;
2063
2064        if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2065                goto nla_put_failure;
2066
2067        if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2068                goto nla_put_failure;
2069
2070#ifdef CONFIG_NF_CONNTRACK_SECMARK
2071        if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2072                goto nla_put_failure;
2073#endif
2074        if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2075                goto nla_put_failure;
2076
2077        if ((ct->status & IPS_SEQ_ADJUST) &&
2078            ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2079                goto nla_put_failure;
2080
2081#ifdef CONFIG_NF_CONNTRACK_MARK
2082        if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2083                goto nla_put_failure;
2084#endif
2085        if (ctnetlink_dump_labels(skb, ct) < 0)
2086                goto nla_put_failure;
2087        rcu_read_unlock();
2088        return 0;
2089
2090nla_put_failure:
2091        rcu_read_unlock();
2092        return -ENOSPC;
2093}
2094
2095static int
2096ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2097{
2098        int err;
2099
2100        if (cda[CTA_TIMEOUT]) {
2101                err = ctnetlink_change_timeout(ct, cda);
2102                if (err < 0)
2103                        return err;
2104        }
2105        if (cda[CTA_STATUS]) {
2106                err = ctnetlink_change_status(ct, cda);
2107                if (err < 0)
2108                        return err;
2109        }
2110        if (cda[CTA_HELP]) {
2111                err = ctnetlink_change_helper(ct, cda);
2112                if (err < 0)
2113                        return err;
2114        }
2115        if (cda[CTA_LABELS]) {
2116                err = ctnetlink_attach_labels(ct, cda);
2117                if (err < 0)
2118                        return err;
2119        }
2120#if defined(CONFIG_NF_CONNTRACK_MARK)
2121        if (cda[CTA_MARK])
2122                ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2123#endif
2124        return 0;
2125}
2126
2127static int
2128ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2129{
2130        struct nlattr *cda[CTA_MAX+1];
2131        int ret;
2132
2133        ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2134        if (ret < 0)
2135                return ret;
2136
2137        spin_lock_bh(&nf_conntrack_lock);
2138        ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2139        spin_unlock_bh(&nf_conntrack_lock);
2140
2141        return ret;
2142}
2143
2144static int ctnetlink_nfqueue_exp_parse(const struct nlattr * const *cda,
2145                                       const struct nf_conn *ct,
2146                                       struct nf_conntrack_tuple *tuple,
2147                                       struct nf_conntrack_tuple *mask)
2148{
2149        int err;
2150
2151        err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2152                                    nf_ct_l3num(ct));
2153        if (err < 0)
2154                return err;
2155
2156        return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2157                                     nf_ct_l3num(ct));
2158}
2159
2160static int
2161ctnetlink_nfqueue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2162                                u32 portid, u32 report)
2163{
2164        struct nlattr *cda[CTA_EXPECT_MAX+1];
2165        struct nf_conntrack_tuple tuple, mask;
2166        struct nf_conntrack_helper *helper = NULL;
2167        struct nf_conntrack_expect *exp;
2168        int err;
2169
2170        err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2171        if (err < 0)
2172                return err;
2173
2174        err = ctnetlink_nfqueue_exp_parse((const struct nlattr * const *)cda,
2175                                          ct, &tuple, &mask);
2176        if (err < 0)
2177                return err;
2178
2179        if (cda[CTA_EXPECT_HELP_NAME]) {
2180                const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2181
2182                helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2183                                                    nf_ct_protonum(ct));
2184                if (helper == NULL)
2185                        return -EOPNOTSUPP;
2186        }
2187
2188        exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2189                                     helper, &tuple, &mask);
2190        if (IS_ERR(exp))
2191                return PTR_ERR(exp);
2192
2193        err = nf_ct_expect_related_report(exp, portid, report);
2194        if (err < 0) {
2195                nf_ct_expect_put(exp);
2196                return err;
2197        }
2198
2199        return 0;
2200}
2201
2202static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2203        .build_size     = ctnetlink_nfqueue_build_size,
2204        .build          = ctnetlink_nfqueue_build,
2205        .parse          = ctnetlink_nfqueue_parse,
2206        .attach_expect  = ctnetlink_nfqueue_attach_expect,
2207        .seq_adjust     = nf_ct_tcp_seqadj_set,
2208};
2209#endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2210
2211/***********************************************************************
2212 * EXPECT
2213 ***********************************************************************/
2214
2215static inline int
2216ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2217                         const struct nf_conntrack_tuple *tuple,
2218                         enum ctattr_expect type)
2219{
2220        struct nlattr *nest_parms;
2221
2222        nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2223        if (!nest_parms)
2224                goto nla_put_failure;
2225        if (ctnetlink_dump_tuples(skb, tuple) < 0)
2226                goto nla_put_failure;
2227        nla_nest_end(skb, nest_parms);
2228
2229        return 0;
2230
2231nla_put_failure:
2232        return -1;
2233}
2234
2235static inline int
2236ctnetlink_exp_dump_mask(struct sk_buff *skb,
2237                        const struct nf_conntrack_tuple *tuple,
2238                        const struct nf_conntrack_tuple_mask *mask)
2239{
2240        int ret;
2241        struct nf_conntrack_l3proto *l3proto;
2242        struct nf_conntrack_l4proto *l4proto;
2243        struct nf_conntrack_tuple m;
2244        struct nlattr *nest_parms;
2245
2246        memset(&m, 0xFF, sizeof(m));
2247        memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2248        m.src.u.all = mask->src.u.all;
2249        m.dst.protonum = tuple->dst.protonum;
2250
2251        nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2252        if (!nest_parms)
2253                goto nla_put_failure;
2254
2255        rcu_read_lock();
2256        l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2257        ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2258        if (ret >= 0) {
2259                l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2260                                               tuple->dst.protonum);
2261        ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2262        }
2263        rcu_read_unlock();
2264
2265        if (unlikely(ret < 0))
2266                goto nla_put_failure;
2267
2268        nla_nest_end(skb, nest_parms);
2269
2270        return 0;
2271
2272nla_put_failure:
2273        return -1;
2274}
2275
2276static const union nf_inet_addr any_addr;
2277
2278static int
2279ctnetlink_exp_dump_expect(struct sk_buff *skb,
2280                          const struct nf_conntrack_expect *exp)
2281{
2282        struct nf_conn *master = exp->master;
2283        long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2284        struct nf_conn_help *help;
2285#ifdef CONFIG_NF_NAT_NEEDED
2286        struct nlattr *nest_parms;
2287        struct nf_conntrack_tuple nat_tuple = {};
2288#endif
2289        struct nf_ct_helper_expectfn *expfn;
2290
2291        if (timeout < 0)
2292                timeout = 0;
2293
2294        if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2295                goto nla_put_failure;
2296        if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2297                goto nla_put_failure;
2298        if (ctnetlink_exp_dump_tuple(skb,
2299                                 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2300                                 CTA_EXPECT_MASTER) < 0)
2301                goto nla_put_failure;
2302
2303#ifdef CONFIG_NF_NAT_NEEDED
2304        if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2305            exp->saved_proto.all) {
2306                nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2307                if (!nest_parms)
2308                        goto nla_put_failure;
2309
2310                if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2311                        goto nla_put_failure;
2312
2313                nat_tuple.src.l3num = nf_ct_l3num(master);
2314                nat_tuple.src.u3 = exp->saved_addr;
2315                nat_tuple.dst.protonum = nf_ct_protonum(master);
2316                nat_tuple.src.u = exp->saved_proto;
2317
2318                if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2319                                                CTA_EXPECT_NAT_TUPLE) < 0)
2320                        goto nla_put_failure;
2321                nla_nest_end(skb, nest_parms);
2322        }
2323#endif
2324        if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2325            nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2326            nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2327            nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2328                goto nla_put_failure;
2329        help = nfct_help(master);
2330        if (help) {
2331                struct nf_conntrack_helper *helper;
2332
2333                helper = rcu_dereference(help->helper);
2334                if (helper &&
2335                    nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2336                        goto nla_put_failure;
2337        }
2338        expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2339        if (expfn != NULL &&
2340            nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2341                goto nla_put_failure;
2342
2343        return 0;
2344
2345nla_put_failure:
2346        return -1;
2347}
2348
2349static int
2350ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2351                        int event, const struct nf_conntrack_expect *exp)
2352{
2353        struct nlmsghdr *nlh;
2354        struct nfgenmsg *nfmsg;
2355        unsigned int flags = portid ? NLM_F_MULTI : 0;
2356
2357        event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2358        nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2359        if (nlh == NULL)
2360                goto nlmsg_failure;
2361
2362        nfmsg = nlmsg_data(nlh);
2363        nfmsg->nfgen_family = exp->tuple.src.l3num;
2364        nfmsg->version      = NFNETLINK_V0;
2365        nfmsg->res_id       = 0;
2366
2367        if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2368                goto nla_put_failure;
2369
2370        nlmsg_end(skb, nlh);
2371        return skb->len;
2372
2373nlmsg_failure:
2374nla_put_failure:
2375        nlmsg_cancel(skb, nlh);
2376        return -1;
2377}
2378
2379#ifdef CONFIG_NF_CONNTRACK_EVENTS
2380static int
2381ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2382{
2383        struct nf_conntrack_expect *exp = item->exp;
2384        struct net *net = nf_ct_exp_net(exp);
2385        struct nlmsghdr *nlh;
2386        struct nfgenmsg *nfmsg;
2387        struct sk_buff *skb;
2388        unsigned int type, group;
2389        int flags = 0;
2390
2391        if (events & (1 << IPEXP_DESTROY)) {
2392                type = IPCTNL_MSG_EXP_DELETE;
2393                group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2394        } else if (events & (1 << IPEXP_NEW)) {
2395                type = IPCTNL_MSG_EXP_NEW;
2396                flags = NLM_F_CREATE|NLM_F_EXCL;
2397                group = NFNLGRP_CONNTRACK_EXP_NEW;
2398        } else
2399                return 0;
2400
2401        if (!item->report && !nfnetlink_has_listeners(net, group))
2402                return 0;
2403
2404        skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2405        if (skb == NULL)
2406                goto errout;
2407
2408        type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2409        nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2410        if (nlh == NULL)
2411                goto nlmsg_failure;
2412
2413        nfmsg = nlmsg_data(nlh);
2414        nfmsg->nfgen_family = exp->tuple.src.l3num;
2415        nfmsg->version      = NFNETLINK_V0;
2416        nfmsg->res_id       = 0;
2417
2418        rcu_read_lock();
2419        if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2420                goto nla_put_failure;
2421        rcu_read_unlock();
2422
2423        nlmsg_end(skb, nlh);
2424        nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2425        return 0;
2426
2427nla_put_failure:
2428        rcu_read_unlock();
2429        nlmsg_cancel(skb, nlh);
2430nlmsg_failure:
2431        kfree_skb(skb);
2432errout:
2433        nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2434        return 0;
2435}
2436#endif
2437static int ctnetlink_exp_done(struct netlink_callback *cb)
2438{
2439        if (cb->args[1])
2440                nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2441        return 0;
2442}
2443
2444static int
2445ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2446{
2447        struct net *net = sock_net(skb->sk);
2448        struct nf_conntrack_expect *exp, *last;
2449        struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2450        u_int8_t l3proto = nfmsg->nfgen_family;
2451
2452        rcu_read_lock();
2453        last = (struct nf_conntrack_expect *)cb->args[1];
2454        for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2455restart:
2456                hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
2457                                     hnode) {
2458                        if (l3proto && exp->tuple.src.l3num != l3proto)
2459                                continue;
2460                        if (cb->args[1]) {
2461                                if (exp != last)
2462                                        continue;
2463                                cb->args[1] = 0;
2464                        }
2465                        if (ctnetlink_exp_fill_info(skb,
2466                                                    NETLINK_CB(cb->skb).portid,
2467                                                    cb->nlh->nlmsg_seq,
2468                                                    IPCTNL_MSG_EXP_NEW,
2469                                                    exp) < 0) {
2470                                if (!atomic_inc_not_zero(&exp->use))
2471                                        continue;
2472                                cb->args[1] = (unsigned long)exp;
2473                                goto out;
2474                        }
2475                }
2476                if (cb->args[1]) {
2477                        cb->args[1] = 0;
2478                        goto restart;
2479                }
2480        }
2481out:
2482        rcu_read_unlock();
2483        if (last)
2484                nf_ct_expect_put(last);
2485
2486        return skb->len;
2487}
2488
2489static int
2490ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2491{
2492        struct nf_conntrack_expect *exp, *last;
2493        struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2494        struct nf_conn *ct = cb->data;
2495        struct nf_conn_help *help = nfct_help(ct);
2496        u_int8_t l3proto = nfmsg->nfgen_family;
2497
2498        if (cb->args[0])
2499                return 0;
2500
2501        rcu_read_lock();
2502        last = (struct nf_conntrack_expect *)cb->args[1];
2503restart:
2504        hlist_for_each_entry(exp, &help->expectations, lnode) {
2505                if (l3proto && exp->tuple.src.l3num != l3proto)
2506                        continue;
2507                if (cb->args[1]) {
2508                        if (exp != last)
2509                                continue;
2510                        cb->args[1] = 0;
2511                }
2512                if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2513                                            cb->nlh->nlmsg_seq,
2514                                            IPCTNL_MSG_EXP_NEW,
2515                                            exp) < 0) {
2516                        if (!atomic_inc_not_zero(&exp->use))
2517                                continue;
2518                        cb->args[1] = (unsigned long)exp;
2519                        goto out;
2520                }
2521        }
2522        if (cb->args[1]) {
2523                cb->args[1] = 0;
2524                goto restart;
2525        }
2526        cb->args[0] = 1;
2527out:
2528        rcu_read_unlock();
2529        if (last)
2530                nf_ct_expect_put(last);
2531
2532        return skb->len;
2533}
2534
2535static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb,
2536                                 const struct nlmsghdr *nlh,
2537                                 const struct nlattr * const cda[])
2538{
2539        int err;
2540        struct net *net = sock_net(ctnl);
2541        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2542        u_int8_t u3 = nfmsg->nfgen_family;
2543        struct nf_conntrack_tuple tuple;
2544        struct nf_conntrack_tuple_hash *h;
2545        struct nf_conn *ct;
2546        u16 zone = 0;
2547        struct netlink_dump_control c = {
2548                .dump = ctnetlink_exp_ct_dump_table,
2549                .done = ctnetlink_exp_done,
2550        };
2551
2552        err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2553        if (err < 0)
2554                return err;
2555
2556        if (cda[CTA_EXPECT_ZONE]) {
2557                err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2558                if (err < 0)
2559                        return err;
2560        }
2561
2562        h = nf_conntrack_find_get(net, zone, &tuple);
2563        if (!h)
2564                return -ENOENT;
2565
2566        ct = nf_ct_tuplehash_to_ctrack(h);
2567        c.data = ct;
2568
2569        err = netlink_dump_start(ctnl, skb, nlh, &c);
2570        nf_ct_put(ct);
2571
2572        return err;
2573}
2574
2575static int
2576ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2577                     const struct nlmsghdr *nlh,
2578                     const struct nlattr * const cda[])
2579{
2580        struct net *net = sock_net(ctnl);
2581        struct nf_conntrack_tuple tuple;
2582        struct nf_conntrack_expect *exp;
2583        struct sk_buff *skb2;
2584        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2585        u_int8_t u3 = nfmsg->nfgen_family;
2586        u16 zone;
2587        int err;
2588
2589        if (nlh->nlmsg_flags & NLM_F_DUMP) {
2590                if (cda[CTA_EXPECT_MASTER])
2591                        return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda);
2592                else {
2593                        struct netlink_dump_control c = {
2594                                .dump = ctnetlink_exp_dump_table,
2595                                .done = ctnetlink_exp_done,
2596                        };
2597                        return netlink_dump_start(ctnl, skb, nlh, &c);
2598                }
2599        }
2600
2601        err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2602        if (err < 0)
2603                return err;
2604
2605        if (cda[CTA_EXPECT_TUPLE])
2606                err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2607        else if (cda[CTA_EXPECT_MASTER])
2608                err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2609        else
2610                return -EINVAL;
2611
2612        if (err < 0)
2613                return err;
2614
2615        exp = nf_ct_expect_find_get(net, zone, &tuple);
2616        if (!exp)
2617                return -ENOENT;
2618
2619        if (cda[CTA_EXPECT_ID]) {
2620                __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2621                if (ntohl(id) != (u32)(unsigned long)exp) {
2622                        nf_ct_expect_put(exp);
2623                        return -ENOENT;
2624                }
2625        }
2626
2627        err = -ENOMEM;
2628        skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2629        if (skb2 == NULL) {
2630                nf_ct_expect_put(exp);
2631                goto out;
2632        }
2633
2634        rcu_read_lock();
2635        err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2636                                      nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2637        rcu_read_unlock();
2638        nf_ct_expect_put(exp);
2639        if (err <= 0)
2640                goto free;
2641
2642        err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2643        if (err < 0)
2644                goto out;
2645
2646        return 0;
2647
2648free:
2649        kfree_skb(skb2);
2650out:
2651        /* this avoids a loop in nfnetlink. */
2652        return err == -EAGAIN ? -ENOBUFS : err;
2653}
2654
2655static int
2656ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2657                     const struct nlmsghdr *nlh,
2658                     const struct nlattr * const cda[])
2659{
2660        struct net *net = sock_net(ctnl);
2661        struct nf_conntrack_expect *exp;
2662        struct nf_conntrack_tuple tuple;
2663        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2664        struct hlist_node *next;
2665        u_int8_t u3 = nfmsg->nfgen_family;
2666        unsigned int i;
2667        u16 zone;
2668        int err;
2669
2670        if (cda[CTA_EXPECT_TUPLE]) {
2671                /* delete a single expect by tuple */
2672                err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2673                if (err < 0)
2674                        return err;
2675
2676                err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2677                if (err < 0)
2678                        return err;
2679
2680                /* bump usage count to 2 */
2681                exp = nf_ct_expect_find_get(net, zone, &tuple);
2682                if (!exp)
2683                        return -ENOENT;
2684
2685                if (cda[CTA_EXPECT_ID]) {
2686                        __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2687                        if (ntohl(id) != (u32)(unsigned long)exp) {
2688                                nf_ct_expect_put(exp);
2689                                return -ENOENT;
2690                        }
2691                }
2692
2693                /* after list removal, usage count == 1 */
2694                spin_lock_bh(&nf_conntrack_lock);
2695                if (del_timer(&exp->timeout)) {
2696                        nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2697                                                   nlmsg_report(nlh));
2698                        nf_ct_expect_put(exp);
2699                }
2700                spin_unlock_bh(&nf_conntrack_lock);
2701                /* have to put what we 'get' above.
2702                 * after this line usage count == 0 */
2703                nf_ct_expect_put(exp);
2704        } else if (cda[CTA_EXPECT_HELP_NAME]) {
2705                char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2706                struct nf_conn_help *m_help;
2707
2708                /* delete all expectations for this helper */
2709                spin_lock_bh(&nf_conntrack_lock);
2710                for (i = 0; i < nf_ct_expect_hsize; i++) {
2711                        hlist_for_each_entry_safe(exp, next,
2712                                                  &net->ct.expect_hash[i],
2713                                                  hnode) {
2714                                m_help = nfct_help(exp->master);
2715                                if (!strcmp(m_help->helper->name, name) &&
2716                                    del_timer(&exp->timeout)) {
2717                                        nf_ct_unlink_expect_report(exp,
2718                                                        NETLINK_CB(skb).portid,
2719                                                        nlmsg_report(nlh));
2720                                        nf_ct_expect_put(exp);
2721                                }
2722                        }
2723                }
2724                spin_unlock_bh(&nf_conntrack_lock);
2725        } else {
2726                /* This basically means we have to flush everything*/
2727                spin_lock_bh(&nf_conntrack_lock);
2728                for (i = 0; i < nf_ct_expect_hsize; i++) {
2729                        hlist_for_each_entry_safe(exp, next,
2730                                                  &net->ct.expect_hash[i],
2731                                                  hnode) {
2732                                if (del_timer(&exp->timeout)) {
2733                                        nf_ct_unlink_expect_report(exp,
2734                                                        NETLINK_CB(skb).portid,
2735                                                        nlmsg_report(nlh));
2736                                        nf_ct_expect_put(exp);
2737                                }
2738                        }
2739                }
2740                spin_unlock_bh(&nf_conntrack_lock);
2741        }
2742
2743        return 0;
2744}
2745static int
2746ctnetlink_change_expect(struct nf_conntrack_expect *x,
2747                        const struct nlattr * const cda[])
2748{
2749        if (cda[CTA_EXPECT_TIMEOUT]) {
2750                if (!del_timer(&x->timeout))
2751                        return -ETIME;
2752
2753                x->timeout.expires = jiffies +
2754                        ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2755                add_timer(&x->timeout);
2756        }
2757        return 0;
2758}
2759
2760static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2761        [CTA_EXPECT_NAT_DIR]    = { .type = NLA_U32 },
2762        [CTA_EXPECT_NAT_TUPLE]  = { .type = NLA_NESTED },
2763};
2764
2765static int
2766ctnetlink_parse_expect_nat(const struct nlattr *attr,
2767                           struct nf_conntrack_expect *exp,
2768                           u_int8_t u3)
2769{
2770#ifdef CONFIG_NF_NAT_NEEDED
2771        struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2772        struct nf_conntrack_tuple nat_tuple = {};
2773        int err;
2774
2775        err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2776        if (err < 0)
2777                return err;
2778
2779        if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2780                return -EINVAL;
2781
2782        err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2783                                        &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2784        if (err < 0)
2785                return err;
2786
2787        exp->saved_addr = nat_tuple.src.u3;
2788        exp->saved_proto = nat_tuple.src.u;
2789        exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2790
2791        return 0;
2792#else
2793        return -EOPNOTSUPP;
2794#endif
2795}
2796
2797static struct nf_conntrack_expect *
2798ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
2799                       struct nf_conntrack_helper *helper,
2800                       struct nf_conntrack_tuple *tuple,
2801                       struct nf_conntrack_tuple *mask)
2802{
2803        u_int32_t class = 0;
2804        struct nf_conntrack_expect *exp;
2805        struct nf_conn_help *help;
2806        int err;
2807
2808        if (cda[CTA_EXPECT_CLASS] && helper) {
2809                class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2810                if (class > helper->expect_class_max)
2811                        return ERR_PTR(-EINVAL);
2812        }
2813        exp = nf_ct_expect_alloc(ct);
2814        if (!exp)
2815                return ERR_PTR(-ENOMEM);
2816
2817        help = nfct_help(ct);
2818        if (!help) {
2819                if (!cda[CTA_EXPECT_TIMEOUT]) {
2820                        err = -EINVAL;
2821                        goto err_out;
2822                }
2823                exp->timeout.expires =
2824                  jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2825
2826                exp->flags = NF_CT_EXPECT_USERSPACE;
2827                if (cda[CTA_EXPECT_FLAGS]) {
2828                        exp->flags |=
2829                                ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2830                }
2831        } else {
2832                if (cda[CTA_EXPECT_FLAGS]) {
2833                        exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2834                        exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2835                } else
2836                        exp->flags = 0;
2837        }
2838        if (cda[CTA_EXPECT_FN]) {
2839                const char *name = nla_data(cda[CTA_EXPECT_FN]);
2840                struct nf_ct_helper_expectfn *expfn;
2841
2842                expfn = nf_ct_helper_expectfn_find_by_name(name);
2843                if (expfn == NULL) {
2844                        err = -EINVAL;
2845                        goto err_out;
2846                }
2847                exp->expectfn = expfn->expectfn;
2848        } else
2849                exp->expectfn = NULL;
2850
2851        exp->class = class;
2852        exp->master = ct;
2853        exp->helper = helper;
2854        exp->tuple = *tuple;
2855        exp->mask.src.u3 = mask->src.u3;
2856        exp->mask.src.u.all = mask->src.u.all;
2857
2858        if (cda[CTA_EXPECT_NAT]) {
2859                err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2860                                                 exp, nf_ct_l3num(ct));
2861                if (err < 0)
2862                        goto err_out;
2863        }
2864        return exp;
2865err_out:
2866        nf_ct_expect_put(exp);
2867        return ERR_PTR(err);
2868}
2869
2870static int
2871ctnetlink_create_expect(struct net *net, u16 zone,
2872                        const struct nlattr * const cda[],
2873                        u_int8_t u3, u32 portid, int report)
2874{
2875        struct nf_conntrack_tuple tuple, mask, master_tuple;
2876        struct nf_conntrack_tuple_hash *h = NULL;
2877        struct nf_conntrack_helper *helper = NULL;
2878        struct nf_conntrack_expect *exp;
2879        struct nf_conn *ct;
2880        int err;
2881
2882        /* caller guarantees that those three CTA_EXPECT_* exist */
2883        err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2884        if (err < 0)
2885                return err;
2886        err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2887        if (err < 0)
2888                return err;
2889        err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2890        if (err < 0)
2891                return err;
2892
2893        /* Look for master conntrack of this expectation */
2894        h = nf_conntrack_find_get(net, zone, &master_tuple);
2895        if (!h)
2896                return -ENOENT;
2897        ct = nf_ct_tuplehash_to_ctrack(h);
2898
2899        if (cda[CTA_EXPECT_HELP_NAME]) {
2900                const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2901
2902                helper = __nf_conntrack_helper_find(helpname, u3,
2903                                                    nf_ct_protonum(ct));
2904                if (helper == NULL) {
2905#ifdef CONFIG_MODULES
2906                        if (request_module("nfct-helper-%s", helpname) < 0) {
2907                                err = -EOPNOTSUPP;
2908                                goto err_ct;
2909                        }
2910                        helper = __nf_conntrack_helper_find(helpname, u3,
2911                                                            nf_ct_protonum(ct));
2912                        if (helper) {
2913                                err = -EAGAIN;
2914                                goto err_ct;
2915                        }
2916#endif
2917                        err = -EOPNOTSUPP;
2918                        goto err_ct;
2919                }
2920        }
2921
2922        exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
2923        if (IS_ERR(exp)) {
2924                err = PTR_ERR(exp);
2925                goto err_ct;
2926        }
2927
2928        err = nf_ct_expect_related_report(exp, portid, report);
2929        if (err < 0)
2930                goto err_exp;
2931
2932        return 0;
2933err_exp:
2934        nf_ct_expect_put(exp);
2935err_ct:
2936        nf_ct_put(ct);
2937        return err;
2938}
2939
2940static int
2941ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2942                     const struct nlmsghdr *nlh,
2943                     const struct nlattr * const cda[])
2944{
2945        struct net *net = sock_net(ctnl);
2946        struct nf_conntrack_tuple tuple;
2947        struct nf_conntrack_expect *exp;
2948        struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2949        u_int8_t u3 = nfmsg->nfgen_family;
2950        u16 zone;
2951        int err;
2952
2953        if (!cda[CTA_EXPECT_TUPLE]
2954            || !cda[CTA_EXPECT_MASK]
2955            || !cda[CTA_EXPECT_MASTER])
2956                return -EINVAL;
2957
2958        err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2959        if (err < 0)
2960                return err;
2961
2962        err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2963        if (err < 0)
2964                return err;
2965
2966        spin_lock_bh(&nf_conntrack_lock);
2967        exp = __nf_ct_expect_find(net, zone, &tuple);
2968
2969        if (!exp) {
2970                spin_unlock_bh(&nf_conntrack_lock);
2971                err = -ENOENT;
2972                if (nlh->nlmsg_flags & NLM_F_CREATE) {
2973                        err = ctnetlink_create_expect(net, zone, cda,
2974                                                      u3,
2975                                                      NETLINK_CB(skb).portid,
2976                                                      nlmsg_report(nlh));
2977                }
2978                return err;
2979        }
2980
2981        err = -EEXIST;
2982        if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2983                err = ctnetlink_change_expect(exp, cda);
2984        spin_unlock_bh(&nf_conntrack_lock);
2985
2986        return err;
2987}
2988
2989static int
2990ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2991                             const struct ip_conntrack_stat *st)
2992{
2993        struct nlmsghdr *nlh;
2994        struct nfgenmsg *nfmsg;
2995        unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2996
2997        event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2998        nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2999        if (nlh == NULL)
3000                goto nlmsg_failure;
3001
3002        nfmsg = nlmsg_data(nlh);
3003        nfmsg->nfgen_family = AF_UNSPEC;
3004        nfmsg->version      = NFNETLINK_V0;
3005        nfmsg->res_id       = htons(cpu);
3006
3007        if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3008            nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3009            nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3010                goto nla_put_failure;
3011
3012        nlmsg_end(skb, nlh);
3013        return skb->len;
3014
3015nla_put_failure:
3016nlmsg_failure:
3017        nlmsg_cancel(skb, nlh);
3018        return -1;
3019}
3020
3021static int
3022ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3023{
3024        int cpu;
3025        struct net *net = sock_net(skb->sk);
3026
3027        if (cb->args[0] == nr_cpu_ids)
3028                return 0;
3029
3030        for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3031                const struct ip_conntrack_stat *st;
3032
3033                if (!cpu_possible(cpu))
3034                        continue;
3035
3036                st = per_cpu_ptr(net->ct.stat, cpu);
3037                if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3038                                                 cb->nlh->nlmsg_seq,
3039                                                 cpu, st) < 0)
3040                        break;
3041        }
3042        cb->args[0] = cpu;
3043
3044        return skb->len;
3045}
3046
3047static int
3048ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
3049                       const struct nlmsghdr *nlh,
3050                       const struct nlattr * const cda[])
3051{
3052        if (nlh->nlmsg_flags & NLM_F_DUMP) {
3053                struct netlink_dump_control c = {
3054                        .dump = ctnetlink_exp_stat_cpu_dump,
3055                };
3056                return netlink_dump_start(ctnl, skb, nlh, &c);
3057        }
3058
3059        return 0;
3060}
3061
3062#ifdef CONFIG_NF_CONNTRACK_EVENTS
3063static struct nf_ct_event_notifier ctnl_notifier = {
3064        .fcn = ctnetlink_conntrack_event,
3065};
3066
3067static struct nf_exp_event_notifier ctnl_notifier_exp = {
3068        .fcn = ctnetlink_expect_event,
3069};
3070#endif
3071
3072static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3073        [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
3074                                            .attr_count = CTA_MAX,
3075                                            .policy = ct_nla_policy },
3076        [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
3077                                            .attr_count = CTA_MAX,
3078                                            .policy = ct_nla_policy },
3079        [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
3080                                            .attr_count = CTA_MAX,
3081                                            .policy = ct_nla_policy },
3082        [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
3083                                            .attr_count = CTA_MAX,
3084                                            .policy = ct_nla_policy },
3085        [IPCTNL_MSG_CT_GET_STATS_CPU]   = { .call = ctnetlink_stat_ct_cpu },
3086        [IPCTNL_MSG_CT_GET_STATS]       = { .call = ctnetlink_stat_ct },
3087        [IPCTNL_MSG_CT_GET_DYING]       = { .call = ctnetlink_get_ct_dying },
3088        [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3089};
3090
3091static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3092        [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
3093                                            .attr_count = CTA_EXPECT_MAX,
3094                                            .policy = exp_nla_policy },
3095        [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
3096                                            .attr_count = CTA_EXPECT_MAX,
3097                                            .policy = exp_nla_policy },
3098        [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
3099                                            .attr_count = CTA_EXPECT_MAX,
3100                                            .policy = exp_nla_policy },
3101        [IPCTNL_MSG_EXP_GET_STATS_CPU]  = { .call = ctnetlink_stat_exp_cpu },
3102};
3103
3104static const struct nfnetlink_subsystem ctnl_subsys = {
3105        .name                           = "conntrack",
3106        .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
3107        .cb_count                       = IPCTNL_MSG_MAX,
3108        .cb                             = ctnl_cb,
3109};
3110
3111static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3112        .name                           = "conntrack_expect",
3113        .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
3114        .cb_count                       = IPCTNL_MSG_EXP_MAX,
3115        .cb                             = ctnl_exp_cb,
3116};
3117
3118MODULE_ALIAS("ip_conntrack_netlink");
3119MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3120MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3121
3122static int __net_init ctnetlink_net_init(struct net *net)
3123{
3124#ifdef CONFIG_NF_CONNTRACK_EVENTS
3125        int ret;
3126
3127        ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3128        if (ret < 0) {
3129                pr_err("ctnetlink_init: cannot register notifier.\n");
3130                goto err_out;
3131        }
3132
3133        ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3134        if (ret < 0) {
3135                pr_err("ctnetlink_init: cannot expect register notifier.\n");
3136                goto err_unreg_notifier;
3137        }
3138#endif
3139        return 0;
3140
3141#ifdef CONFIG_NF_CONNTRACK_EVENTS
3142err_unreg_notifier:
3143        nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3144err_out:
3145        return ret;
3146#endif
3147}
3148
3149static void ctnetlink_net_exit(struct net *net)
3150{
3151#ifdef CONFIG_NF_CONNTRACK_EVENTS
3152        nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3153        nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3154#endif
3155}
3156
3157static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3158{
3159        struct net *net;
3160
3161        list_for_each_entry(net, net_exit_list, exit_list)
3162                ctnetlink_net_exit(net);
3163}
3164
3165static struct pernet_operations ctnetlink_net_ops = {
3166        .init           = ctnetlink_net_init,
3167        .exit_batch     = ctnetlink_net_exit_batch,
3168};
3169
3170static int __init ctnetlink_init(void)
3171{
3172        int ret;
3173
3174        pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3175        ret = nfnetlink_subsys_register(&ctnl_subsys);
3176        if (ret < 0) {
3177                pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3178                goto err_out;
3179        }
3180
3181        ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3182        if (ret < 0) {
3183                pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3184                goto err_unreg_subsys;
3185        }
3186
3187        ret = register_pernet_subsys(&ctnetlink_net_ops);
3188        if (ret < 0) {
3189                pr_err("ctnetlink_init: cannot register pernet operations\n");
3190                goto err_unreg_exp_subsys;
3191        }
3192#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3193        /* setup interaction between nf_queue and nf_conntrack_netlink. */
3194        RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
3195#endif
3196        return 0;
3197
3198err_unreg_exp_subsys:
3199        nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3200err_unreg_subsys:
3201        nfnetlink_subsys_unregister(&ctnl_subsys);
3202err_out:
3203        return ret;
3204}
3205
3206static void __exit ctnetlink_exit(void)
3207{
3208        pr_info("ctnetlink: unregistering from nfnetlink.\n");
3209
3210        unregister_pernet_subsys(&ctnetlink_net_ops);
3211        nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3212        nfnetlink_subsys_unregister(&ctnl_subsys);
3213#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3214        RCU_INIT_POINTER(nfq_ct_hook, NULL);
3215#endif
3216}
3217
3218module_init(ctnetlink_init);
3219module_exit(ctnetlink_exit);
3220