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