netfilter: ctnetlink: dump entries from the dying and unconfirmed lists
[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 };
903
904 static inline int
905 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
906                      struct nlattr **helpinfo)
907 {
908         struct nlattr *tb[CTA_HELP_MAX+1];
909
910         nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
911
912         if (!tb[CTA_HELP_NAME])
913                 return -EINVAL;
914
915         *helper_name = nla_data(tb[CTA_HELP_NAME]);
916
917         if (tb[CTA_HELP_INFO])
918                 *helpinfo = tb[CTA_HELP_INFO];
919
920         return 0;
921 }
922
923 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
924         [CTA_TUPLE_ORIG]        = { .type = NLA_NESTED },
925         [CTA_TUPLE_REPLY]       = { .type = NLA_NESTED },
926         [CTA_STATUS]            = { .type = NLA_U32 },
927         [CTA_PROTOINFO]         = { .type = NLA_NESTED },
928         [CTA_HELP]              = { .type = NLA_NESTED },
929         [CTA_NAT_SRC]           = { .type = NLA_NESTED },
930         [CTA_TIMEOUT]           = { .type = NLA_U32 },
931         [CTA_MARK]              = { .type = NLA_U32 },
932         [CTA_ID]                = { .type = NLA_U32 },
933         [CTA_NAT_DST]           = { .type = NLA_NESTED },
934         [CTA_TUPLE_MASTER]      = { .type = NLA_NESTED },
935         [CTA_ZONE]              = { .type = NLA_U16 },
936         [CTA_MARK_MASK]         = { .type = NLA_U32 },
937 };
938
939 static int
940 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
941                         const struct nlmsghdr *nlh,
942                         const struct nlattr * const cda[])
943 {
944         struct net *net = sock_net(ctnl);
945         struct nf_conntrack_tuple_hash *h;
946         struct nf_conntrack_tuple tuple;
947         struct nf_conn *ct;
948         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
949         u_int8_t u3 = nfmsg->nfgen_family;
950         u16 zone;
951         int err;
952
953         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
954         if (err < 0)
955                 return err;
956
957         if (cda[CTA_TUPLE_ORIG])
958                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
959         else if (cda[CTA_TUPLE_REPLY])
960                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
961         else {
962                 /* Flush the whole table */
963                 nf_conntrack_flush_report(net,
964                                          NETLINK_CB(skb).portid,
965                                          nlmsg_report(nlh));
966                 return 0;
967         }
968
969         if (err < 0)
970                 return err;
971
972         h = nf_conntrack_find_get(net, zone, &tuple);
973         if (!h)
974                 return -ENOENT;
975
976         ct = nf_ct_tuplehash_to_ctrack(h);
977
978         if (cda[CTA_ID]) {
979                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
980                 if (id != (u32)(unsigned long)ct) {
981                         nf_ct_put(ct);
982                         return -ENOENT;
983                 }
984         }
985
986         if (del_timer(&ct->timeout)) {
987                 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
988                                               NETLINK_CB(skb).portid,
989                                               nlmsg_report(nlh)) < 0) {
990                         nf_ct_delete_from_lists(ct);
991                         /* we failed to report the event, try later */
992                         nf_ct_dying_timeout(ct);
993                         nf_ct_put(ct);
994                         return 0;
995                 }
996                 /* death_by_timeout would report the event again */
997                 set_bit(IPS_DYING_BIT, &ct->status);
998                 nf_ct_delete_from_lists(ct);
999                 nf_ct_put(ct);
1000         }
1001         nf_ct_put(ct);
1002
1003         return 0;
1004 }
1005
1006 static int
1007 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1008                         const struct nlmsghdr *nlh,
1009                         const struct nlattr * const cda[])
1010 {
1011         struct net *net = sock_net(ctnl);
1012         struct nf_conntrack_tuple_hash *h;
1013         struct nf_conntrack_tuple tuple;
1014         struct nf_conn *ct;
1015         struct sk_buff *skb2 = NULL;
1016         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1017         u_int8_t u3 = nfmsg->nfgen_family;
1018         u16 zone;
1019         int err;
1020
1021         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1022                 struct netlink_dump_control c = {
1023                         .dump = ctnetlink_dump_table,
1024                         .done = ctnetlink_done,
1025                 };
1026 #ifdef CONFIG_NF_CONNTRACK_MARK
1027                 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1028                         struct ctnetlink_dump_filter *filter;
1029
1030                         filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1031                                          GFP_ATOMIC);
1032                         if (filter == NULL)
1033                                 return -ENOMEM;
1034
1035                         filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1036                         filter->mark.mask =
1037                                 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1038                         c.data = filter;
1039                 }
1040 #endif
1041                 return netlink_dump_start(ctnl, skb, nlh, &c);
1042         }
1043
1044         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1045         if (err < 0)
1046                 return err;
1047
1048         if (cda[CTA_TUPLE_ORIG])
1049                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1050         else if (cda[CTA_TUPLE_REPLY])
1051                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1052         else
1053                 return -EINVAL;
1054
1055         if (err < 0)
1056                 return err;
1057
1058         h = nf_conntrack_find_get(net, zone, &tuple);
1059         if (!h)
1060                 return -ENOENT;
1061
1062         ct = nf_ct_tuplehash_to_ctrack(h);
1063
1064         err = -ENOMEM;
1065         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1066         if (skb2 == NULL) {
1067                 nf_ct_put(ct);
1068                 return -ENOMEM;
1069         }
1070
1071         rcu_read_lock();
1072         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1073                                   NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1074         rcu_read_unlock();
1075         nf_ct_put(ct);
1076         if (err <= 0)
1077                 goto free;
1078
1079         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1080         if (err < 0)
1081                 goto out;
1082
1083         return 0;
1084
1085 free:
1086         kfree_skb(skb2);
1087 out:
1088         /* this avoids a loop in nfnetlink. */
1089         return err == -EAGAIN ? -ENOBUFS : err;
1090 }
1091
1092 static int ctnetlink_done_list(struct netlink_callback *cb)
1093 {
1094         if (cb->args[1])
1095                 nf_ct_put((struct nf_conn *)cb->args[1]);
1096         return 0;
1097 }
1098
1099 static int
1100 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1101                     struct hlist_nulls_head *list)
1102 {
1103         struct nf_conn *ct, *last;
1104         struct nf_conntrack_tuple_hash *h;
1105         struct hlist_nulls_node *n;
1106         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1107         u_int8_t l3proto = nfmsg->nfgen_family;
1108         int res;
1109
1110         if (cb->args[2])
1111                 return 0;
1112
1113         spin_lock_bh(&nf_conntrack_lock);
1114         last = (struct nf_conn *)cb->args[1];
1115 restart:
1116         hlist_nulls_for_each_entry(h, n, list, hnnode) {
1117                 ct = nf_ct_tuplehash_to_ctrack(h);
1118                 if (l3proto && nf_ct_l3num(ct) != l3proto)
1119                         continue;
1120                 if (cb->args[1]) {
1121                         if (ct != last)
1122                                 continue;
1123                         cb->args[1] = 0;
1124                 }
1125                 rcu_read_lock();
1126                 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1127                                           cb->nlh->nlmsg_seq,
1128                                           NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1129                                           ct);
1130                 rcu_read_unlock();
1131                 if (res < 0) {
1132                         nf_conntrack_get(&ct->ct_general);
1133                         cb->args[1] = (unsigned long)ct;
1134                         goto out;
1135                 }
1136         }
1137         if (cb->args[1]) {
1138                 cb->args[1] = 0;
1139                 goto restart;
1140         } else
1141                 cb->args[2] = 1;
1142 out:
1143         spin_unlock_bh(&nf_conntrack_lock);
1144         if (last)
1145                 nf_ct_put(last);
1146
1147         return skb->len;
1148 }
1149
1150 static int
1151 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1152 {
1153         struct net *net = sock_net(skb->sk);
1154
1155         return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1156 }
1157
1158 static int
1159 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1160                        const struct nlmsghdr *nlh,
1161                        const struct nlattr * const cda[])
1162 {
1163         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1164                 struct netlink_dump_control c = {
1165                         .dump = ctnetlink_dump_dying,
1166                         .done = ctnetlink_done_list,
1167                 };
1168                 return netlink_dump_start(ctnl, skb, nlh, &c);
1169         }
1170
1171         return -EOPNOTSUPP;
1172 }
1173
1174 static int
1175 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1176 {
1177         struct net *net = sock_net(skb->sk);
1178
1179         return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1180 }
1181
1182 static int
1183 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1184                              const struct nlmsghdr *nlh,
1185                              const struct nlattr * const cda[])
1186 {
1187         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1188                 struct netlink_dump_control c = {
1189                         .dump = ctnetlink_dump_unconfirmed,
1190                         .done = ctnetlink_done_list,
1191                 };
1192                 return netlink_dump_start(ctnl, skb, nlh, &c);
1193         }
1194
1195         return -EOPNOTSUPP;
1196 }
1197
1198 #ifdef CONFIG_NF_NAT_NEEDED
1199 static int
1200 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1201                           enum nf_nat_manip_type manip,
1202                           const struct nlattr *attr)
1203 {
1204         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1205         int err;
1206
1207         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1208         if (!parse_nat_setup) {
1209 #ifdef CONFIG_MODULES
1210                 rcu_read_unlock();
1211                 nfnl_unlock();
1212                 if (request_module("nf-nat") < 0) {
1213                         nfnl_lock();
1214                         rcu_read_lock();
1215                         return -EOPNOTSUPP;
1216                 }
1217                 nfnl_lock();
1218                 rcu_read_lock();
1219                 if (nfnetlink_parse_nat_setup_hook)
1220                         return -EAGAIN;
1221 #endif
1222                 return -EOPNOTSUPP;
1223         }
1224
1225         err = parse_nat_setup(ct, manip, attr);
1226         if (err == -EAGAIN) {
1227 #ifdef CONFIG_MODULES
1228                 rcu_read_unlock();
1229                 nfnl_unlock();
1230                 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1231                         nfnl_lock();
1232                         rcu_read_lock();
1233                         return -EOPNOTSUPP;
1234                 }
1235                 nfnl_lock();
1236                 rcu_read_lock();
1237 #else
1238                 err = -EOPNOTSUPP;
1239 #endif
1240         }
1241         return err;
1242 }
1243 #endif
1244
1245 static int
1246 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1247 {
1248         unsigned long d;
1249         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1250         d = ct->status ^ status;
1251
1252         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1253                 /* unchangeable */
1254                 return -EBUSY;
1255
1256         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1257                 /* SEEN_REPLY bit can only be set */
1258                 return -EBUSY;
1259
1260         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1261                 /* ASSURED bit can only be set */
1262                 return -EBUSY;
1263
1264         /* Be careful here, modifying NAT bits can screw up things,
1265          * so don't let users modify them directly if they don't pass
1266          * nf_nat_range. */
1267         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1268         return 0;
1269 }
1270
1271 static int
1272 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1273 {
1274 #ifdef CONFIG_NF_NAT_NEEDED
1275         int ret;
1276
1277         if (cda[CTA_NAT_DST]) {
1278                 ret = ctnetlink_parse_nat_setup(ct,
1279                                                 NF_NAT_MANIP_DST,
1280                                                 cda[CTA_NAT_DST]);
1281                 if (ret < 0)
1282                         return ret;
1283         }
1284         if (cda[CTA_NAT_SRC]) {
1285                 ret = ctnetlink_parse_nat_setup(ct,
1286                                                 NF_NAT_MANIP_SRC,
1287                                                 cda[CTA_NAT_SRC]);
1288                 if (ret < 0)
1289                         return ret;
1290         }
1291         return 0;
1292 #else
1293         return -EOPNOTSUPP;
1294 #endif
1295 }
1296
1297 static inline int
1298 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1299 {
1300         struct nf_conntrack_helper *helper;
1301         struct nf_conn_help *help = nfct_help(ct);
1302         char *helpname = NULL;
1303         struct nlattr *helpinfo = NULL;
1304         int err;
1305
1306         /* don't change helper of sibling connections */
1307         if (ct->master)
1308                 return -EBUSY;
1309
1310         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1311         if (err < 0)
1312                 return err;
1313
1314         if (!strcmp(helpname, "")) {
1315                 if (help && help->helper) {
1316                         /* we had a helper before ... */
1317                         nf_ct_remove_expectations(ct);
1318                         RCU_INIT_POINTER(help->helper, NULL);
1319                 }
1320
1321                 return 0;
1322         }
1323
1324         helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1325                                             nf_ct_protonum(ct));
1326         if (helper == NULL) {
1327 #ifdef CONFIG_MODULES
1328                 spin_unlock_bh(&nf_conntrack_lock);
1329
1330                 if (request_module("nfct-helper-%s", helpname) < 0) {
1331                         spin_lock_bh(&nf_conntrack_lock);
1332                         return -EOPNOTSUPP;
1333                 }
1334
1335                 spin_lock_bh(&nf_conntrack_lock);
1336                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1337                                                     nf_ct_protonum(ct));
1338                 if (helper)
1339                         return -EAGAIN;
1340 #endif
1341                 return -EOPNOTSUPP;
1342         }
1343
1344         if (help) {
1345                 if (help->helper == helper) {
1346                         /* update private helper data if allowed. */
1347                         if (helper->from_nlattr)
1348                                 helper->from_nlattr(helpinfo, ct);
1349                         return 0;
1350                 } else
1351                         return -EBUSY;
1352         }
1353
1354         /* we cannot set a helper for an existing conntrack */
1355         return -EOPNOTSUPP;
1356 }
1357
1358 static inline int
1359 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1360 {
1361         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1362
1363         if (!del_timer(&ct->timeout))
1364                 return -ETIME;
1365
1366         ct->timeout.expires = jiffies + timeout * HZ;
1367         add_timer(&ct->timeout);
1368
1369         return 0;
1370 }
1371
1372 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1373         [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
1374         [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
1375         [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
1376 };
1377
1378 static inline int
1379 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1380 {
1381         const struct nlattr *attr = cda[CTA_PROTOINFO];
1382         struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1383         struct nf_conntrack_l4proto *l4proto;
1384         int err = 0;
1385
1386         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1387
1388         rcu_read_lock();
1389         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1390         if (l4proto->from_nlattr)
1391                 err = l4proto->from_nlattr(tb, ct);
1392         rcu_read_unlock();
1393
1394         return err;
1395 }
1396
1397 #ifdef CONFIG_NF_NAT_NEEDED
1398 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1399         [CTA_NAT_SEQ_CORRECTION_POS]    = { .type = NLA_U32 },
1400         [CTA_NAT_SEQ_OFFSET_BEFORE]     = { .type = NLA_U32 },
1401         [CTA_NAT_SEQ_OFFSET_AFTER]      = { .type = NLA_U32 },
1402 };
1403
1404 static inline int
1405 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1406 {
1407         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1408
1409         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1410
1411         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1412                 return -EINVAL;
1413
1414         natseq->correction_pos =
1415                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1416
1417         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1418                 return -EINVAL;
1419
1420         natseq->offset_before =
1421                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1422
1423         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1424                 return -EINVAL;
1425
1426         natseq->offset_after =
1427                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1428
1429         return 0;
1430 }
1431
1432 static int
1433 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1434                              const struct nlattr * const cda[])
1435 {
1436         int ret = 0;
1437         struct nf_conn_nat *nat = nfct_nat(ct);
1438
1439         if (!nat)
1440                 return 0;
1441
1442         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1443                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1444                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1445                 if (ret < 0)
1446                         return ret;
1447
1448                 ct->status |= IPS_SEQ_ADJUST;
1449         }
1450
1451         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1452                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1453                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1454                 if (ret < 0)
1455                         return ret;
1456
1457                 ct->status |= IPS_SEQ_ADJUST;
1458         }
1459
1460         return 0;
1461 }
1462 #endif
1463
1464 static int
1465 ctnetlink_change_conntrack(struct nf_conn *ct,
1466                            const struct nlattr * const cda[])
1467 {
1468         int err;
1469
1470         /* only allow NAT changes and master assignation for new conntracks */
1471         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1472                 return -EOPNOTSUPP;
1473
1474         if (cda[CTA_HELP]) {
1475                 err = ctnetlink_change_helper(ct, cda);
1476                 if (err < 0)
1477                         return err;
1478         }
1479
1480         if (cda[CTA_TIMEOUT]) {
1481                 err = ctnetlink_change_timeout(ct, cda);
1482                 if (err < 0)
1483                         return err;
1484         }
1485
1486         if (cda[CTA_STATUS]) {
1487                 err = ctnetlink_change_status(ct, cda);
1488                 if (err < 0)
1489                         return err;
1490         }
1491
1492         if (cda[CTA_PROTOINFO]) {
1493                 err = ctnetlink_change_protoinfo(ct, cda);
1494                 if (err < 0)
1495                         return err;
1496         }
1497
1498 #if defined(CONFIG_NF_CONNTRACK_MARK)
1499         if (cda[CTA_MARK])
1500                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1501 #endif
1502
1503 #ifdef CONFIG_NF_NAT_NEEDED
1504         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1505                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1506                 if (err < 0)
1507                         return err;
1508         }
1509 #endif
1510
1511         return 0;
1512 }
1513
1514 static struct nf_conn *
1515 ctnetlink_create_conntrack(struct net *net, u16 zone,
1516                            const struct nlattr * const cda[],
1517                            struct nf_conntrack_tuple *otuple,
1518                            struct nf_conntrack_tuple *rtuple,
1519                            u8 u3)
1520 {
1521         struct nf_conn *ct;
1522         int err = -EINVAL;
1523         struct nf_conntrack_helper *helper;
1524         struct nf_conn_tstamp *tstamp;
1525
1526         ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1527         if (IS_ERR(ct))
1528                 return ERR_PTR(-ENOMEM);
1529
1530         if (!cda[CTA_TIMEOUT])
1531                 goto err1;
1532         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1533
1534         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1535
1536         rcu_read_lock();
1537         if (cda[CTA_HELP]) {
1538                 char *helpname = NULL;
1539                 struct nlattr *helpinfo = NULL;
1540
1541                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1542                 if (err < 0)
1543                         goto err2;
1544
1545                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1546                                                     nf_ct_protonum(ct));
1547                 if (helper == NULL) {
1548                         rcu_read_unlock();
1549 #ifdef CONFIG_MODULES
1550                         if (request_module("nfct-helper-%s", helpname) < 0) {
1551                                 err = -EOPNOTSUPP;
1552                                 goto err1;
1553                         }
1554
1555                         rcu_read_lock();
1556                         helper = __nf_conntrack_helper_find(helpname,
1557                                                             nf_ct_l3num(ct),
1558                                                             nf_ct_protonum(ct));
1559                         if (helper) {
1560                                 err = -EAGAIN;
1561                                 goto err2;
1562                         }
1563                         rcu_read_unlock();
1564 #endif
1565                         err = -EOPNOTSUPP;
1566                         goto err1;
1567                 } else {
1568                         struct nf_conn_help *help;
1569
1570                         help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1571                         if (help == NULL) {
1572                                 err = -ENOMEM;
1573                                 goto err2;
1574                         }
1575                         /* set private helper data if allowed. */
1576                         if (helper->from_nlattr)
1577                                 helper->from_nlattr(helpinfo, ct);
1578
1579                         /* not in hash table yet so not strictly necessary */
1580                         RCU_INIT_POINTER(help->helper, helper);
1581                 }
1582         } else {
1583                 /* try an implicit helper assignation */
1584                 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1585                 if (err < 0)
1586                         goto err2;
1587         }
1588
1589         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1590                 err = ctnetlink_change_nat(ct, cda);
1591                 if (err < 0)
1592                         goto err2;
1593         }
1594
1595         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1596         nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1597         nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1598         /* we must add conntrack extensions before confirmation. */
1599         ct->status |= IPS_CONFIRMED;
1600
1601         if (cda[CTA_STATUS]) {
1602                 err = ctnetlink_change_status(ct, cda);
1603                 if (err < 0)
1604                         goto err2;
1605         }
1606
1607 #ifdef CONFIG_NF_NAT_NEEDED
1608         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1609                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1610                 if (err < 0)
1611                         goto err2;
1612         }
1613 #endif
1614
1615         memset(&ct->proto, 0, sizeof(ct->proto));
1616         if (cda[CTA_PROTOINFO]) {
1617                 err = ctnetlink_change_protoinfo(ct, cda);
1618                 if (err < 0)
1619                         goto err2;
1620         }
1621
1622 #if defined(CONFIG_NF_CONNTRACK_MARK)
1623         if (cda[CTA_MARK])
1624                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1625 #endif
1626
1627         /* setup master conntrack: this is a confirmed expectation */
1628         if (cda[CTA_TUPLE_MASTER]) {
1629                 struct nf_conntrack_tuple master;
1630                 struct nf_conntrack_tuple_hash *master_h;
1631                 struct nf_conn *master_ct;
1632
1633                 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1634                 if (err < 0)
1635                         goto err2;
1636
1637                 master_h = nf_conntrack_find_get(net, zone, &master);
1638                 if (master_h == NULL) {
1639                         err = -ENOENT;
1640                         goto err2;
1641                 }
1642                 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1643                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1644                 ct->master = master_ct;
1645         }
1646         tstamp = nf_conn_tstamp_find(ct);
1647         if (tstamp)
1648                 tstamp->start = ktime_to_ns(ktime_get_real());
1649
1650         err = nf_conntrack_hash_check_insert(ct);
1651         if (err < 0)
1652                 goto err2;
1653
1654         rcu_read_unlock();
1655
1656         return ct;
1657
1658 err2:
1659         rcu_read_unlock();
1660 err1:
1661         nf_conntrack_free(ct);
1662         return ERR_PTR(err);
1663 }
1664
1665 static int
1666 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1667                         const struct nlmsghdr *nlh,
1668                         const struct nlattr * const cda[])
1669 {
1670         struct net *net = sock_net(ctnl);
1671         struct nf_conntrack_tuple otuple, rtuple;
1672         struct nf_conntrack_tuple_hash *h = NULL;
1673         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1674         struct nf_conn *ct;
1675         u_int8_t u3 = nfmsg->nfgen_family;
1676         u16 zone;
1677         int err;
1678
1679         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1680         if (err < 0)
1681                 return err;
1682
1683         if (cda[CTA_TUPLE_ORIG]) {
1684                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1685                 if (err < 0)
1686                         return err;
1687         }
1688
1689         if (cda[CTA_TUPLE_REPLY]) {
1690                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1691                 if (err < 0)
1692                         return err;
1693         }
1694
1695         if (cda[CTA_TUPLE_ORIG])
1696                 h = nf_conntrack_find_get(net, zone, &otuple);
1697         else if (cda[CTA_TUPLE_REPLY])
1698                 h = nf_conntrack_find_get(net, zone, &rtuple);
1699
1700         if (h == NULL) {
1701                 err = -ENOENT;
1702                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1703                         enum ip_conntrack_events events;
1704
1705                         ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1706                                                         &rtuple, u3);
1707                         if (IS_ERR(ct))
1708                                 return PTR_ERR(ct);
1709
1710                         err = 0;
1711                         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1712                                 events = IPCT_RELATED;
1713                         else
1714                                 events = IPCT_NEW;
1715
1716                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1717                                                       (1 << IPCT_ASSURED) |
1718                                                       (1 << IPCT_HELPER) |
1719                                                       (1 << IPCT_PROTOINFO) |
1720                                                       (1 << IPCT_NATSEQADJ) |
1721                                                       (1 << IPCT_MARK) | events,
1722                                                       ct, NETLINK_CB(skb).portid,
1723                                                       nlmsg_report(nlh));
1724                         nf_ct_put(ct);
1725                 }
1726
1727                 return err;
1728         }
1729         /* implicit 'else' */
1730
1731         err = -EEXIST;
1732         ct = nf_ct_tuplehash_to_ctrack(h);
1733         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1734                 spin_lock_bh(&nf_conntrack_lock);
1735                 err = ctnetlink_change_conntrack(ct, cda);
1736                 spin_unlock_bh(&nf_conntrack_lock);
1737                 if (err == 0) {
1738                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1739                                                       (1 << IPCT_ASSURED) |
1740                                                       (1 << IPCT_HELPER) |
1741                                                       (1 << IPCT_PROTOINFO) |
1742                                                       (1 << IPCT_NATSEQADJ) |
1743                                                       (1 << IPCT_MARK),
1744                                                       ct, NETLINK_CB(skb).portid,
1745                                                       nlmsg_report(nlh));
1746                 }
1747         }
1748
1749         nf_ct_put(ct);
1750         return err;
1751 }
1752
1753 static int
1754 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1755                                 __u16 cpu, const struct ip_conntrack_stat *st)
1756 {
1757         struct nlmsghdr *nlh;
1758         struct nfgenmsg *nfmsg;
1759         unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1760
1761         event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1762         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1763         if (nlh == NULL)
1764                 goto nlmsg_failure;
1765
1766         nfmsg = nlmsg_data(nlh);
1767         nfmsg->nfgen_family = AF_UNSPEC;
1768         nfmsg->version      = NFNETLINK_V0;
1769         nfmsg->res_id       = htons(cpu);
1770
1771         if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1772             nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1773             nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1774             nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1775             nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1776             nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1777             nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1778             nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1779             nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1780                                 htonl(st->insert_failed)) ||
1781             nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1782             nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1783             nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1784             nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1785                                 htonl(st->search_restart)))
1786                 goto nla_put_failure;
1787
1788         nlmsg_end(skb, nlh);
1789         return skb->len;
1790
1791 nla_put_failure:
1792 nlmsg_failure:
1793         nlmsg_cancel(skb, nlh);
1794         return -1;
1795 }
1796
1797 static int
1798 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1799 {
1800         int cpu;
1801         struct net *net = sock_net(skb->sk);
1802
1803         if (cb->args[0] == nr_cpu_ids)
1804                 return 0;
1805
1806         for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1807                 const struct ip_conntrack_stat *st;
1808
1809                 if (!cpu_possible(cpu))
1810                         continue;
1811
1812                 st = per_cpu_ptr(net->ct.stat, cpu);
1813                 if (ctnetlink_ct_stat_cpu_fill_info(skb,
1814                                                     NETLINK_CB(cb->skb).portid,
1815                                                     cb->nlh->nlmsg_seq,
1816                                                     cpu, st) < 0)
1817                                 break;
1818         }
1819         cb->args[0] = cpu;
1820
1821         return skb->len;
1822 }
1823
1824 static int
1825 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1826                       const struct nlmsghdr *nlh,
1827                       const struct nlattr * const cda[])
1828 {
1829         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1830                 struct netlink_dump_control c = {
1831                         .dump = ctnetlink_ct_stat_cpu_dump,
1832                 };
1833                 return netlink_dump_start(ctnl, skb, nlh, &c);
1834         }
1835
1836         return 0;
1837 }
1838
1839 static int
1840 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1841                             struct net *net)
1842 {
1843         struct nlmsghdr *nlh;
1844         struct nfgenmsg *nfmsg;
1845         unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1846         unsigned int nr_conntracks = atomic_read(&net->ct.count);
1847
1848         event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1849         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1850         if (nlh == NULL)
1851                 goto nlmsg_failure;
1852
1853         nfmsg = nlmsg_data(nlh);
1854         nfmsg->nfgen_family = AF_UNSPEC;
1855         nfmsg->version      = NFNETLINK_V0;
1856         nfmsg->res_id       = 0;
1857
1858         if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1859                 goto nla_put_failure;
1860
1861         nlmsg_end(skb, nlh);
1862         return skb->len;
1863
1864 nla_put_failure:
1865 nlmsg_failure:
1866         nlmsg_cancel(skb, nlh);
1867         return -1;
1868 }
1869
1870 static int
1871 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1872                   const struct nlmsghdr *nlh,
1873                   const struct nlattr * const cda[])
1874 {
1875         struct sk_buff *skb2;
1876         int err;
1877
1878         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1879         if (skb2 == NULL)
1880                 return -ENOMEM;
1881
1882         err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1883                                           nlh->nlmsg_seq,
1884                                           NFNL_MSG_TYPE(nlh->nlmsg_type),
1885                                           sock_net(skb->sk));
1886         if (err <= 0)
1887                 goto free;
1888
1889         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1890         if (err < 0)
1891                 goto out;
1892
1893         return 0;
1894
1895 free:
1896         kfree_skb(skb2);
1897 out:
1898         /* this avoids a loop in nfnetlink. */
1899         return err == -EAGAIN ? -ENOBUFS : err;
1900 }
1901
1902 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
1903 static size_t
1904 ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1905 {
1906         return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1907                + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1908                + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1909                + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1910                + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1911                + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
1912                + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
1913                + nla_total_size(0) /* CTA_PROTOINFO */
1914                + nla_total_size(0) /* CTA_HELP */
1915                + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
1916                + ctnetlink_secctx_size(ct)
1917 #ifdef CONFIG_NF_NAT_NEEDED
1918                + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1919                + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
1920 #endif
1921 #ifdef CONFIG_NF_CONNTRACK_MARK
1922                + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
1923 #endif
1924                + ctnetlink_proto_size(ct)
1925                ;
1926 }
1927
1928 static int
1929 ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
1930 {
1931         struct nlattr *nest_parms;
1932
1933         rcu_read_lock();
1934         nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
1935         if (!nest_parms)
1936                 goto nla_put_failure;
1937         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
1938                 goto nla_put_failure;
1939         nla_nest_end(skb, nest_parms);
1940
1941         nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
1942         if (!nest_parms)
1943                 goto nla_put_failure;
1944         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
1945                 goto nla_put_failure;
1946         nla_nest_end(skb, nest_parms);
1947
1948         if (nf_ct_zone(ct)) {
1949                 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
1950                         goto nla_put_failure;
1951         }
1952
1953         if (ctnetlink_dump_id(skb, ct) < 0)
1954                 goto nla_put_failure;
1955
1956         if (ctnetlink_dump_status(skb, ct) < 0)
1957                 goto nla_put_failure;
1958
1959         if (ctnetlink_dump_timeout(skb, ct) < 0)
1960                 goto nla_put_failure;
1961
1962         if (ctnetlink_dump_protoinfo(skb, ct) < 0)
1963                 goto nla_put_failure;
1964
1965         if (ctnetlink_dump_helpinfo(skb, ct) < 0)
1966                 goto nla_put_failure;
1967
1968 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1969         if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
1970                 goto nla_put_failure;
1971 #endif
1972         if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
1973                 goto nla_put_failure;
1974
1975         if ((ct->status & IPS_SEQ_ADJUST) &&
1976             ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
1977                 goto nla_put_failure;
1978
1979 #ifdef CONFIG_NF_CONNTRACK_MARK
1980         if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
1981                 goto nla_put_failure;
1982 #endif
1983         rcu_read_unlock();
1984         return 0;
1985
1986 nla_put_failure:
1987         rcu_read_unlock();
1988         return -ENOSPC;
1989 }
1990
1991 static int
1992 ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
1993 {
1994         int err;
1995
1996         if (cda[CTA_TIMEOUT]) {
1997                 err = ctnetlink_change_timeout(ct, cda);
1998                 if (err < 0)
1999                         return err;
2000         }
2001         if (cda[CTA_STATUS]) {
2002                 err = ctnetlink_change_status(ct, cda);
2003                 if (err < 0)
2004                         return err;
2005         }
2006         if (cda[CTA_HELP]) {
2007                 err = ctnetlink_change_helper(ct, cda);
2008                 if (err < 0)
2009                         return err;
2010         }
2011 #if defined(CONFIG_NF_CONNTRACK_MARK)
2012         if (cda[CTA_MARK])
2013                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2014 #endif
2015         return 0;
2016 }
2017
2018 static int
2019 ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2020 {
2021         struct nlattr *cda[CTA_MAX+1];
2022         int ret;
2023
2024         nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2025
2026         spin_lock_bh(&nf_conntrack_lock);
2027         ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2028         spin_unlock_bh(&nf_conntrack_lock);
2029
2030         return ret;
2031 }
2032
2033 static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2034         .build_size     = ctnetlink_nfqueue_build_size,
2035         .build          = ctnetlink_nfqueue_build,
2036         .parse          = ctnetlink_nfqueue_parse,
2037 };
2038 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2039
2040 /***********************************************************************
2041  * EXPECT
2042  ***********************************************************************/
2043
2044 static inline int
2045 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2046                          const struct nf_conntrack_tuple *tuple,
2047                          enum ctattr_expect type)
2048 {
2049         struct nlattr *nest_parms;
2050
2051         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2052         if (!nest_parms)
2053                 goto nla_put_failure;
2054         if (ctnetlink_dump_tuples(skb, tuple) < 0)
2055                 goto nla_put_failure;
2056         nla_nest_end(skb, nest_parms);
2057
2058         return 0;
2059
2060 nla_put_failure:
2061         return -1;
2062 }
2063
2064 static inline int
2065 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2066                         const struct nf_conntrack_tuple *tuple,
2067                         const struct nf_conntrack_tuple_mask *mask)
2068 {
2069         int ret;
2070         struct nf_conntrack_l3proto *l3proto;
2071         struct nf_conntrack_l4proto *l4proto;
2072         struct nf_conntrack_tuple m;
2073         struct nlattr *nest_parms;
2074
2075         memset(&m, 0xFF, sizeof(m));
2076         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2077         m.src.u.all = mask->src.u.all;
2078         m.dst.protonum = tuple->dst.protonum;
2079
2080         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2081         if (!nest_parms)
2082                 goto nla_put_failure;
2083
2084         rcu_read_lock();
2085         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2086         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2087         if (ret >= 0) {
2088                 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2089                                                tuple->dst.protonum);
2090         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2091         }
2092         rcu_read_unlock();
2093
2094         if (unlikely(ret < 0))
2095                 goto nla_put_failure;
2096
2097         nla_nest_end(skb, nest_parms);
2098
2099         return 0;
2100
2101 nla_put_failure:
2102         return -1;
2103 }
2104
2105 static const union nf_inet_addr any_addr;
2106
2107 static int
2108 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2109                           const struct nf_conntrack_expect *exp)
2110 {
2111         struct nf_conn *master = exp->master;
2112         long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2113         struct nf_conn_help *help;
2114 #ifdef CONFIG_NF_NAT_NEEDED
2115         struct nlattr *nest_parms;
2116         struct nf_conntrack_tuple nat_tuple = {};
2117 #endif
2118         struct nf_ct_helper_expectfn *expfn;
2119
2120         if (timeout < 0)
2121                 timeout = 0;
2122
2123         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2124                 goto nla_put_failure;
2125         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2126                 goto nla_put_failure;
2127         if (ctnetlink_exp_dump_tuple(skb,
2128                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2129                                  CTA_EXPECT_MASTER) < 0)
2130                 goto nla_put_failure;
2131
2132 #ifdef CONFIG_NF_NAT_NEEDED
2133         if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2134             exp->saved_proto.all) {
2135                 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2136                 if (!nest_parms)
2137                         goto nla_put_failure;
2138
2139                 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2140                         goto nla_put_failure;
2141
2142                 nat_tuple.src.l3num = nf_ct_l3num(master);
2143                 nat_tuple.src.u3 = exp->saved_addr;
2144                 nat_tuple.dst.protonum = nf_ct_protonum(master);
2145                 nat_tuple.src.u = exp->saved_proto;
2146
2147                 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2148                                                 CTA_EXPECT_NAT_TUPLE) < 0)
2149                         goto nla_put_failure;
2150                 nla_nest_end(skb, nest_parms);
2151         }
2152 #endif
2153         if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2154             nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2155             nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2156             nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2157                 goto nla_put_failure;
2158         help = nfct_help(master);
2159         if (help) {
2160                 struct nf_conntrack_helper *helper;
2161
2162                 helper = rcu_dereference(help->helper);
2163                 if (helper &&
2164                     nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2165                         goto nla_put_failure;
2166         }
2167         expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2168         if (expfn != NULL &&
2169             nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2170                 goto nla_put_failure;
2171
2172         return 0;
2173
2174 nla_put_failure:
2175         return -1;
2176 }
2177
2178 static int
2179 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2180                         int event, const struct nf_conntrack_expect *exp)
2181 {
2182         struct nlmsghdr *nlh;
2183         struct nfgenmsg *nfmsg;
2184         unsigned int flags = portid ? NLM_F_MULTI : 0;
2185
2186         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2187         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2188         if (nlh == NULL)
2189                 goto nlmsg_failure;
2190
2191         nfmsg = nlmsg_data(nlh);
2192         nfmsg->nfgen_family = exp->tuple.src.l3num;
2193         nfmsg->version      = NFNETLINK_V0;
2194         nfmsg->res_id       = 0;
2195
2196         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2197                 goto nla_put_failure;
2198
2199         nlmsg_end(skb, nlh);
2200         return skb->len;
2201
2202 nlmsg_failure:
2203 nla_put_failure:
2204         nlmsg_cancel(skb, nlh);
2205         return -1;
2206 }
2207
2208 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2209 static int
2210 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2211 {
2212         struct nf_conntrack_expect *exp = item->exp;
2213         struct net *net = nf_ct_exp_net(exp);
2214         struct nlmsghdr *nlh;
2215         struct nfgenmsg *nfmsg;
2216         struct sk_buff *skb;
2217         unsigned int type, group;
2218         int flags = 0;
2219
2220         if (events & (1 << IPEXP_DESTROY)) {
2221                 type = IPCTNL_MSG_EXP_DELETE;
2222                 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2223         } else if (events & (1 << IPEXP_NEW)) {
2224                 type = IPCTNL_MSG_EXP_NEW;
2225                 flags = NLM_F_CREATE|NLM_F_EXCL;
2226                 group = NFNLGRP_CONNTRACK_EXP_NEW;
2227         } else
2228                 return 0;
2229
2230         if (!item->report && !nfnetlink_has_listeners(net, group))
2231                 return 0;
2232
2233         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2234         if (skb == NULL)
2235                 goto errout;
2236
2237         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2238         nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2239         if (nlh == NULL)
2240                 goto nlmsg_failure;
2241
2242         nfmsg = nlmsg_data(nlh);
2243         nfmsg->nfgen_family = exp->tuple.src.l3num;
2244         nfmsg->version      = NFNETLINK_V0;
2245         nfmsg->res_id       = 0;
2246
2247         rcu_read_lock();
2248         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2249                 goto nla_put_failure;
2250         rcu_read_unlock();
2251
2252         nlmsg_end(skb, nlh);
2253         nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2254         return 0;
2255
2256 nla_put_failure:
2257         rcu_read_unlock();
2258         nlmsg_cancel(skb, nlh);
2259 nlmsg_failure:
2260         kfree_skb(skb);
2261 errout:
2262         nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2263         return 0;
2264 }
2265 #endif
2266 static int ctnetlink_exp_done(struct netlink_callback *cb)
2267 {
2268         if (cb->args[1])
2269                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2270         return 0;
2271 }
2272
2273 static int
2274 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2275 {
2276         struct net *net = sock_net(skb->sk);
2277         struct nf_conntrack_expect *exp, *last;
2278         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2279         struct hlist_node *n;
2280         u_int8_t l3proto = nfmsg->nfgen_family;
2281
2282         rcu_read_lock();
2283         last = (struct nf_conntrack_expect *)cb->args[1];
2284         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2285 restart:
2286                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
2287                                      hnode) {
2288                         if (l3proto && exp->tuple.src.l3num != l3proto)
2289                                 continue;
2290                         if (cb->args[1]) {
2291                                 if (exp != last)
2292                                         continue;
2293                                 cb->args[1] = 0;
2294                         }
2295                         if (ctnetlink_exp_fill_info(skb,
2296                                                     NETLINK_CB(cb->skb).portid,
2297                                                     cb->nlh->nlmsg_seq,
2298                                                     IPCTNL_MSG_EXP_NEW,
2299                                                     exp) < 0) {
2300                                 if (!atomic_inc_not_zero(&exp->use))
2301                                         continue;
2302                                 cb->args[1] = (unsigned long)exp;
2303                                 goto out;
2304                         }
2305                 }
2306                 if (cb->args[1]) {
2307                         cb->args[1] = 0;
2308                         goto restart;
2309                 }
2310         }
2311 out:
2312         rcu_read_unlock();
2313         if (last)
2314                 nf_ct_expect_put(last);
2315
2316         return skb->len;
2317 }
2318
2319 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2320         [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
2321         [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
2322         [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
2323         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
2324         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
2325         [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING },
2326         [CTA_EXPECT_ZONE]       = { .type = NLA_U16 },
2327         [CTA_EXPECT_FLAGS]      = { .type = NLA_U32 },
2328         [CTA_EXPECT_CLASS]      = { .type = NLA_U32 },
2329         [CTA_EXPECT_NAT]        = { .type = NLA_NESTED },
2330         [CTA_EXPECT_FN]         = { .type = NLA_NUL_STRING },
2331 };
2332
2333 static int
2334 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2335                      const struct nlmsghdr *nlh,
2336                      const struct nlattr * const cda[])
2337 {
2338         struct net *net = sock_net(ctnl);
2339         struct nf_conntrack_tuple tuple;
2340         struct nf_conntrack_expect *exp;
2341         struct sk_buff *skb2;
2342         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2343         u_int8_t u3 = nfmsg->nfgen_family;
2344         u16 zone;
2345         int err;
2346
2347         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2348                 struct netlink_dump_control c = {
2349                         .dump = ctnetlink_exp_dump_table,
2350                         .done = ctnetlink_exp_done,
2351                 };
2352                 return netlink_dump_start(ctnl, skb, nlh, &c);
2353         }
2354
2355         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2356         if (err < 0)
2357                 return err;
2358
2359         if (cda[CTA_EXPECT_TUPLE])
2360                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2361         else if (cda[CTA_EXPECT_MASTER])
2362                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2363         else
2364                 return -EINVAL;
2365
2366         if (err < 0)
2367                 return err;
2368
2369         exp = nf_ct_expect_find_get(net, zone, &tuple);
2370         if (!exp)
2371                 return -ENOENT;
2372
2373         if (cda[CTA_EXPECT_ID]) {
2374                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2375                 if (ntohl(id) != (u32)(unsigned long)exp) {
2376                         nf_ct_expect_put(exp);
2377                         return -ENOENT;
2378                 }
2379         }
2380
2381         err = -ENOMEM;
2382         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2383         if (skb2 == NULL) {
2384                 nf_ct_expect_put(exp);
2385                 goto out;
2386         }
2387
2388         rcu_read_lock();
2389         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2390                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2391         rcu_read_unlock();
2392         nf_ct_expect_put(exp);
2393         if (err <= 0)
2394                 goto free;
2395
2396         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2397         if (err < 0)
2398                 goto out;
2399
2400         return 0;
2401
2402 free:
2403         kfree_skb(skb2);
2404 out:
2405         /* this avoids a loop in nfnetlink. */
2406         return err == -EAGAIN ? -ENOBUFS : err;
2407 }
2408
2409 static int
2410 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2411                      const struct nlmsghdr *nlh,
2412                      const struct nlattr * const cda[])
2413 {
2414         struct net *net = sock_net(ctnl);
2415         struct nf_conntrack_expect *exp;
2416         struct nf_conntrack_tuple tuple;
2417         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2418         struct hlist_node *n, *next;
2419         u_int8_t u3 = nfmsg->nfgen_family;
2420         unsigned int i;
2421         u16 zone;
2422         int err;
2423
2424         if (cda[CTA_EXPECT_TUPLE]) {
2425                 /* delete a single expect by tuple */
2426                 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2427                 if (err < 0)
2428                         return err;
2429
2430                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2431                 if (err < 0)
2432                         return err;
2433
2434                 /* bump usage count to 2 */
2435                 exp = nf_ct_expect_find_get(net, zone, &tuple);
2436                 if (!exp)
2437                         return -ENOENT;
2438
2439                 if (cda[CTA_EXPECT_ID]) {
2440                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2441                         if (ntohl(id) != (u32)(unsigned long)exp) {
2442                                 nf_ct_expect_put(exp);
2443                                 return -ENOENT;
2444                         }
2445                 }
2446
2447                 /* after list removal, usage count == 1 */
2448                 spin_lock_bh(&nf_conntrack_lock);
2449                 if (del_timer(&exp->timeout)) {
2450                         nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2451                                                    nlmsg_report(nlh));
2452                         nf_ct_expect_put(exp);
2453                 }
2454                 spin_unlock_bh(&nf_conntrack_lock);
2455                 /* have to put what we 'get' above.
2456                  * after this line usage count == 0 */
2457                 nf_ct_expect_put(exp);
2458         } else if (cda[CTA_EXPECT_HELP_NAME]) {
2459                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2460                 struct nf_conn_help *m_help;
2461
2462                 /* delete all expectations for this helper */
2463                 spin_lock_bh(&nf_conntrack_lock);
2464                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2465                         hlist_for_each_entry_safe(exp, n, next,
2466                                                   &net->ct.expect_hash[i],
2467                                                   hnode) {
2468                                 m_help = nfct_help(exp->master);
2469                                 if (!strcmp(m_help->helper->name, name) &&
2470                                     del_timer(&exp->timeout)) {
2471                                         nf_ct_unlink_expect_report(exp,
2472                                                         NETLINK_CB(skb).portid,
2473                                                         nlmsg_report(nlh));
2474                                         nf_ct_expect_put(exp);
2475                                 }
2476                         }
2477                 }
2478                 spin_unlock_bh(&nf_conntrack_lock);
2479         } else {
2480                 /* This basically means we have to flush everything*/
2481                 spin_lock_bh(&nf_conntrack_lock);
2482                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2483                         hlist_for_each_entry_safe(exp, n, next,
2484                                                   &net->ct.expect_hash[i],
2485                                                   hnode) {
2486                                 if (del_timer(&exp->timeout)) {
2487                                         nf_ct_unlink_expect_report(exp,
2488                                                         NETLINK_CB(skb).portid,
2489                                                         nlmsg_report(nlh));
2490                                         nf_ct_expect_put(exp);
2491                                 }
2492                         }
2493                 }
2494                 spin_unlock_bh(&nf_conntrack_lock);
2495         }
2496
2497         return 0;
2498 }
2499 static int
2500 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2501                         const struct nlattr * const cda[])
2502 {
2503         if (cda[CTA_EXPECT_TIMEOUT]) {
2504                 if (!del_timer(&x->timeout))
2505                         return -ETIME;
2506
2507                 x->timeout.expires = jiffies +
2508                         ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2509                 add_timer(&x->timeout);
2510         }
2511         return 0;
2512 }
2513
2514 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2515         [CTA_EXPECT_NAT_DIR]    = { .type = NLA_U32 },
2516         [CTA_EXPECT_NAT_TUPLE]  = { .type = NLA_NESTED },
2517 };
2518
2519 static int
2520 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2521                            struct nf_conntrack_expect *exp,
2522                            u_int8_t u3)
2523 {
2524 #ifdef CONFIG_NF_NAT_NEEDED
2525         struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2526         struct nf_conntrack_tuple nat_tuple = {};
2527         int err;
2528
2529         nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2530
2531         if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2532                 return -EINVAL;
2533
2534         err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2535                                         &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2536         if (err < 0)
2537                 return err;
2538
2539         exp->saved_addr = nat_tuple.src.u3;
2540         exp->saved_proto = nat_tuple.src.u;
2541         exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2542
2543         return 0;
2544 #else
2545         return -EOPNOTSUPP;
2546 #endif
2547 }
2548
2549 static int
2550 ctnetlink_create_expect(struct net *net, u16 zone,
2551                         const struct nlattr * const cda[],
2552                         u_int8_t u3,
2553                         u32 portid, int report)
2554 {
2555         struct nf_conntrack_tuple tuple, mask, master_tuple;
2556         struct nf_conntrack_tuple_hash *h = NULL;
2557         struct nf_conntrack_expect *exp;
2558         struct nf_conn *ct;
2559         struct nf_conn_help *help;
2560         struct nf_conntrack_helper *helper = NULL;
2561         u_int32_t class = 0;
2562         int err = 0;
2563
2564         /* caller guarantees that those three CTA_EXPECT_* exist */
2565         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2566         if (err < 0)
2567                 return err;
2568         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2569         if (err < 0)
2570                 return err;
2571         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2572         if (err < 0)
2573                 return err;
2574
2575         /* Look for master conntrack of this expectation */
2576         h = nf_conntrack_find_get(net, zone, &master_tuple);
2577         if (!h)
2578                 return -ENOENT;
2579         ct = nf_ct_tuplehash_to_ctrack(h);
2580
2581         /* Look for helper of this expectation */
2582         if (cda[CTA_EXPECT_HELP_NAME]) {
2583                 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2584
2585                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2586                                                     nf_ct_protonum(ct));
2587                 if (helper == NULL) {
2588 #ifdef CONFIG_MODULES
2589                         if (request_module("nfct-helper-%s", helpname) < 0) {
2590                                 err = -EOPNOTSUPP;
2591                                 goto out;
2592                         }
2593
2594                         helper = __nf_conntrack_helper_find(helpname,
2595                                                             nf_ct_l3num(ct),
2596                                                             nf_ct_protonum(ct));
2597                         if (helper) {
2598                                 err = -EAGAIN;
2599                                 goto out;
2600                         }
2601 #endif
2602                         err = -EOPNOTSUPP;
2603                         goto out;
2604                 }
2605         }
2606
2607         if (cda[CTA_EXPECT_CLASS] && helper) {
2608                 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2609                 if (class > helper->expect_class_max) {
2610                         err = -EINVAL;
2611                         goto out;
2612                 }
2613         }
2614         exp = nf_ct_expect_alloc(ct);
2615         if (!exp) {
2616                 err = -ENOMEM;
2617                 goto out;
2618         }
2619         help = nfct_help(ct);
2620         if (!help) {
2621                 if (!cda[CTA_EXPECT_TIMEOUT]) {
2622                         err = -EINVAL;
2623                         goto out;
2624                 }
2625                 exp->timeout.expires =
2626                   jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2627
2628                 exp->flags = NF_CT_EXPECT_USERSPACE;
2629                 if (cda[CTA_EXPECT_FLAGS]) {
2630                         exp->flags |=
2631                                 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2632                 }
2633         } else {
2634                 if (cda[CTA_EXPECT_FLAGS]) {
2635                         exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2636                         exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2637                 } else
2638                         exp->flags = 0;
2639         }
2640         if (cda[CTA_EXPECT_FN]) {
2641                 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2642                 struct nf_ct_helper_expectfn *expfn;
2643
2644                 expfn = nf_ct_helper_expectfn_find_by_name(name);
2645                 if (expfn == NULL) {
2646                         err = -EINVAL;
2647                         goto err_out;
2648                 }
2649                 exp->expectfn = expfn->expectfn;
2650         } else
2651                 exp->expectfn = NULL;
2652
2653         exp->class = class;
2654         exp->master = ct;
2655         exp->helper = helper;
2656         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2657         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2658         exp->mask.src.u.all = mask.src.u.all;
2659
2660         if (cda[CTA_EXPECT_NAT]) {
2661                 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2662                                                  exp, u3);
2663                 if (err < 0)
2664                         goto err_out;
2665         }
2666         err = nf_ct_expect_related_report(exp, portid, report);
2667 err_out:
2668         nf_ct_expect_put(exp);
2669 out:
2670         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2671         return err;
2672 }
2673
2674 static int
2675 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2676                      const struct nlmsghdr *nlh,
2677                      const struct nlattr * const cda[])
2678 {
2679         struct net *net = sock_net(ctnl);
2680         struct nf_conntrack_tuple tuple;
2681         struct nf_conntrack_expect *exp;
2682         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2683         u_int8_t u3 = nfmsg->nfgen_family;
2684         u16 zone;
2685         int err;
2686
2687         if (!cda[CTA_EXPECT_TUPLE]
2688             || !cda[CTA_EXPECT_MASK]
2689             || !cda[CTA_EXPECT_MASTER])
2690                 return -EINVAL;
2691
2692         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2693         if (err < 0)
2694                 return err;
2695
2696         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2697         if (err < 0)
2698                 return err;
2699
2700         spin_lock_bh(&nf_conntrack_lock);
2701         exp = __nf_ct_expect_find(net, zone, &tuple);
2702
2703         if (!exp) {
2704                 spin_unlock_bh(&nf_conntrack_lock);
2705                 err = -ENOENT;
2706                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2707                         err = ctnetlink_create_expect(net, zone, cda,
2708                                                       u3,
2709                                                       NETLINK_CB(skb).portid,
2710                                                       nlmsg_report(nlh));
2711                 }
2712                 return err;
2713         }
2714
2715         err = -EEXIST;
2716         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2717                 err = ctnetlink_change_expect(exp, cda);
2718         spin_unlock_bh(&nf_conntrack_lock);
2719
2720         return err;
2721 }
2722
2723 static int
2724 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2725                              const struct ip_conntrack_stat *st)
2726 {
2727         struct nlmsghdr *nlh;
2728         struct nfgenmsg *nfmsg;
2729         unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2730
2731         event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2732         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2733         if (nlh == NULL)
2734                 goto nlmsg_failure;
2735
2736         nfmsg = nlmsg_data(nlh);
2737         nfmsg->nfgen_family = AF_UNSPEC;
2738         nfmsg->version      = NFNETLINK_V0;
2739         nfmsg->res_id       = htons(cpu);
2740
2741         if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2742             nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2743             nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2744                 goto nla_put_failure;
2745
2746         nlmsg_end(skb, nlh);
2747         return skb->len;
2748
2749 nla_put_failure:
2750 nlmsg_failure:
2751         nlmsg_cancel(skb, nlh);
2752         return -1;
2753 }
2754
2755 static int
2756 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2757 {
2758         int cpu;
2759         struct net *net = sock_net(skb->sk);
2760
2761         if (cb->args[0] == nr_cpu_ids)
2762                 return 0;
2763
2764         for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2765                 const struct ip_conntrack_stat *st;
2766
2767                 if (!cpu_possible(cpu))
2768                         continue;
2769
2770                 st = per_cpu_ptr(net->ct.stat, cpu);
2771                 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
2772                                                  cb->nlh->nlmsg_seq,
2773                                                  cpu, st) < 0)
2774                         break;
2775         }
2776         cb->args[0] = cpu;
2777
2778         return skb->len;
2779 }
2780
2781 static int
2782 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2783                        const struct nlmsghdr *nlh,
2784                        const struct nlattr * const cda[])
2785 {
2786         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2787                 struct netlink_dump_control c = {
2788                         .dump = ctnetlink_exp_stat_cpu_dump,
2789                 };
2790                 return netlink_dump_start(ctnl, skb, nlh, &c);
2791         }
2792
2793         return 0;
2794 }
2795
2796 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2797 static struct nf_ct_event_notifier ctnl_notifier = {
2798         .fcn = ctnetlink_conntrack_event,
2799 };
2800
2801 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2802         .fcn = ctnetlink_expect_event,
2803 };
2804 #endif
2805
2806 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2807         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
2808                                             .attr_count = CTA_MAX,
2809                                             .policy = ct_nla_policy },
2810         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
2811                                             .attr_count = CTA_MAX,
2812                                             .policy = ct_nla_policy },
2813         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
2814                                             .attr_count = CTA_MAX,
2815                                             .policy = ct_nla_policy },
2816         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
2817                                             .attr_count = CTA_MAX,
2818                                             .policy = ct_nla_policy },
2819         [IPCTNL_MSG_CT_GET_STATS_CPU]   = { .call = ctnetlink_stat_ct_cpu },
2820         [IPCTNL_MSG_CT_GET_STATS]       = { .call = ctnetlink_stat_ct },
2821         [IPCTNL_MSG_CT_GET_DYING]       = { .call = ctnetlink_get_ct_dying },
2822         [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
2823 };
2824
2825 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2826         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
2827                                             .attr_count = CTA_EXPECT_MAX,
2828                                             .policy = exp_nla_policy },
2829         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
2830                                             .attr_count = CTA_EXPECT_MAX,
2831                                             .policy = exp_nla_policy },
2832         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
2833                                             .attr_count = CTA_EXPECT_MAX,
2834                                             .policy = exp_nla_policy },
2835         [IPCTNL_MSG_EXP_GET_STATS_CPU]  = { .call = ctnetlink_stat_exp_cpu },
2836 };
2837
2838 static const struct nfnetlink_subsystem ctnl_subsys = {
2839         .name                           = "conntrack",
2840         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
2841         .cb_count                       = IPCTNL_MSG_MAX,
2842         .cb                             = ctnl_cb,
2843 };
2844
2845 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2846         .name                           = "conntrack_expect",
2847         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
2848         .cb_count                       = IPCTNL_MSG_EXP_MAX,
2849         .cb                             = ctnl_exp_cb,
2850 };
2851
2852 MODULE_ALIAS("ip_conntrack_netlink");
2853 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2854 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2855
2856 static int __net_init ctnetlink_net_init(struct net *net)
2857 {
2858 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2859         int ret;
2860
2861         ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2862         if (ret < 0) {
2863                 pr_err("ctnetlink_init: cannot register notifier.\n");
2864                 goto err_out;
2865         }
2866
2867         ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2868         if (ret < 0) {
2869                 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2870                 goto err_unreg_notifier;
2871         }
2872 #endif
2873         return 0;
2874
2875 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2876 err_unreg_notifier:
2877         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2878 err_out:
2879         return ret;
2880 #endif
2881 }
2882
2883 static void ctnetlink_net_exit(struct net *net)
2884 {
2885 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2886         nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2887         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2888 #endif
2889 }
2890
2891 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2892 {
2893         struct net *net;
2894
2895         list_for_each_entry(net, net_exit_list, exit_list)
2896                 ctnetlink_net_exit(net);
2897 }
2898
2899 static struct pernet_operations ctnetlink_net_ops = {
2900         .init           = ctnetlink_net_init,
2901         .exit_batch     = ctnetlink_net_exit_batch,
2902 };
2903
2904 static int __init ctnetlink_init(void)
2905 {
2906         int ret;
2907
2908         pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2909         ret = nfnetlink_subsys_register(&ctnl_subsys);
2910         if (ret < 0) {
2911                 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2912                 goto err_out;
2913         }
2914
2915         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2916         if (ret < 0) {
2917                 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2918                 goto err_unreg_subsys;
2919         }
2920
2921         ret = register_pernet_subsys(&ctnetlink_net_ops);
2922         if (ret < 0) {
2923                 pr_err("ctnetlink_init: cannot register pernet operations\n");
2924                 goto err_unreg_exp_subsys;
2925         }
2926 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2927         /* setup interaction between nf_queue and nf_conntrack_netlink. */
2928         RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
2929 #endif
2930         return 0;
2931
2932 err_unreg_exp_subsys:
2933         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2934 err_unreg_subsys:
2935         nfnetlink_subsys_unregister(&ctnl_subsys);
2936 err_out:
2937         return ret;
2938 }
2939
2940 static void __exit ctnetlink_exit(void)
2941 {
2942         pr_info("ctnetlink: unregistering from nfnetlink.\n");
2943
2944         unregister_pernet_subsys(&ctnetlink_net_ops);
2945         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2946         nfnetlink_subsys_unregister(&ctnl_subsys);
2947 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2948         RCU_INIT_POINTER(nfq_ct_hook, NULL);
2949 #endif
2950 }
2951
2952 module_init(ctnetlink_init);
2953 module_exit(ctnetlink_exit);