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