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