Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-flexiantxendom0-3.2.10.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-2011 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_protocol.h>
49 #endif
50
51 #include <linux/netfilter/nfnetlink.h>
52 #include <linux/netfilter/nfnetlink_conntrack.h>
53
54 MODULE_LICENSE("GPL");
55
56 static char __initdata version[] = "0.93";
57
58 static inline int
59 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
60                             const struct nf_conntrack_tuple *tuple,
61                             struct nf_conntrack_l4proto *l4proto)
62 {
63         int ret = 0;
64         struct nlattr *nest_parms;
65
66         nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
67         if (!nest_parms)
68                 goto nla_put_failure;
69         NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
70
71         if (likely(l4proto->tuple_to_nlattr))
72                 ret = l4proto->tuple_to_nlattr(skb, tuple);
73
74         nla_nest_end(skb, nest_parms);
75
76         return ret;
77
78 nla_put_failure:
79         return -1;
80 }
81
82 static inline int
83 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
84                          const struct nf_conntrack_tuple *tuple,
85                          struct nf_conntrack_l3proto *l3proto)
86 {
87         int ret = 0;
88         struct nlattr *nest_parms;
89
90         nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
91         if (!nest_parms)
92                 goto nla_put_failure;
93
94         if (likely(l3proto->tuple_to_nlattr))
95                 ret = l3proto->tuple_to_nlattr(skb, tuple);
96
97         nla_nest_end(skb, nest_parms);
98
99         return ret;
100
101 nla_put_failure:
102         return -1;
103 }
104
105 static int
106 ctnetlink_dump_tuples(struct sk_buff *skb,
107                       const struct nf_conntrack_tuple *tuple)
108 {
109         int ret;
110         struct nf_conntrack_l3proto *l3proto;
111         struct nf_conntrack_l4proto *l4proto;
112
113         rcu_read_lock();
114         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
115         ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
116
117         if (ret >= 0) {
118                 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
119                                                tuple->dst.protonum);
120                 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
121         }
122         rcu_read_unlock();
123         return ret;
124 }
125
126 static inline int
127 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
128 {
129         NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
130         return 0;
131
132 nla_put_failure:
133         return -1;
134 }
135
136 static inline int
137 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
138 {
139         long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
140
141         if (timeout < 0)
142                 timeout = 0;
143
144         NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
145         return 0;
146
147 nla_put_failure:
148         return -1;
149 }
150
151 static inline int
152 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
153 {
154         struct nf_conntrack_l4proto *l4proto;
155         struct nlattr *nest_proto;
156         int ret;
157
158         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
159         if (!l4proto->to_nlattr)
160                 return 0;
161
162         nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
163         if (!nest_proto)
164                 goto nla_put_failure;
165
166         ret = l4proto->to_nlattr(skb, nest_proto, ct);
167
168         nla_nest_end(skb, nest_proto);
169
170         return ret;
171
172 nla_put_failure:
173         return -1;
174 }
175
176 static inline int
177 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
178 {
179         struct nlattr *nest_helper;
180         const struct nf_conn_help *help = nfct_help(ct);
181         struct nf_conntrack_helper *helper;
182
183         if (!help)
184                 return 0;
185
186         helper = rcu_dereference(help->helper);
187         if (!helper)
188                 goto out;
189
190         nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
191         if (!nest_helper)
192                 goto nla_put_failure;
193         NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
194
195         if (helper->to_nlattr)
196                 helper->to_nlattr(skb, ct);
197
198         nla_nest_end(skb, nest_helper);
199 out:
200         return 0;
201
202 nla_put_failure:
203         return -1;
204 }
205
206 static int
207 dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
208               enum ip_conntrack_dir dir)
209 {
210         enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
211         struct nlattr *nest_count;
212
213         nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
214         if (!nest_count)
215                 goto nla_put_failure;
216
217         NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts));
218         NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes));
219
220         nla_nest_end(skb, nest_count);
221
222         return 0;
223
224 nla_put_failure:
225         return -1;
226 }
227
228 static int
229 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
230                         enum ip_conntrack_dir dir, int type)
231 {
232         struct nf_conn_counter *acct;
233         u64 pkts, bytes;
234
235         acct = nf_conn_acct_find(ct);
236         if (!acct)
237                 return 0;
238
239         if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
240                 pkts = atomic64_xchg(&acct[dir].packets, 0);
241                 bytes = atomic64_xchg(&acct[dir].bytes, 0);
242         } else {
243                 pkts = atomic64_read(&acct[dir].packets);
244                 bytes = atomic64_read(&acct[dir].bytes);
245         }
246         return dump_counters(skb, pkts, bytes, dir);
247 }
248
249 static int
250 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
251 {
252         struct nlattr *nest_count;
253         const struct nf_conn_tstamp *tstamp;
254
255         tstamp = nf_conn_tstamp_find(ct);
256         if (!tstamp)
257                 return 0;
258
259         nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
260         if (!nest_count)
261                 goto nla_put_failure;
262
263         NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start));
264         if (tstamp->stop != 0) {
265                 NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP,
266                              cpu_to_be64(tstamp->stop));
267         }
268         nla_nest_end(skb, nest_count);
269
270         return 0;
271
272 nla_put_failure:
273         return -1;
274 }
275
276 #ifdef CONFIG_NF_CONNTRACK_MARK
277 static inline int
278 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
279 {
280         NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
281         return 0;
282
283 nla_put_failure:
284         return -1;
285 }
286 #else
287 #define ctnetlink_dump_mark(a, b) (0)
288 #endif
289
290 #ifdef CONFIG_NF_CONNTRACK_SECMARK
291 static inline int
292 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
293 {
294         struct nlattr *nest_secctx;
295         int len, ret;
296         char *secctx;
297
298         ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
299         if (ret)
300                 return 0;
301
302         ret = -1;
303         nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
304         if (!nest_secctx)
305                 goto nla_put_failure;
306
307         NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
308         nla_nest_end(skb, nest_secctx);
309
310         ret = 0;
311 nla_put_failure:
312         security_release_secctx(secctx, len);
313         return ret;
314 }
315 #else
316 #define ctnetlink_dump_secctx(a, b) (0)
317 #endif
318
319 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
320
321 static inline int
322 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
323 {
324         struct nlattr *nest_parms;
325
326         if (!(ct->status & IPS_EXPECTED))
327                 return 0;
328
329         nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
330         if (!nest_parms)
331                 goto nla_put_failure;
332         if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
333                 goto nla_put_failure;
334         nla_nest_end(skb, nest_parms);
335
336         return 0;
337
338 nla_put_failure:
339         return -1;
340 }
341
342 #ifdef CONFIG_NF_NAT_NEEDED
343 static int
344 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
345 {
346         struct nlattr *nest_parms;
347
348         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
349         if (!nest_parms)
350                 goto nla_put_failure;
351
352         NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
353                      htonl(natseq->correction_pos));
354         NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
355                      htonl(natseq->offset_before));
356         NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
357                      htonl(natseq->offset_after));
358
359         nla_nest_end(skb, nest_parms);
360
361         return 0;
362
363 nla_put_failure:
364         return -1;
365 }
366
367 static inline int
368 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
369 {
370         struct nf_nat_seq *natseq;
371         struct nf_conn_nat *nat = nfct_nat(ct);
372
373         if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
374                 return 0;
375
376         natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
377         if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
378                 return -1;
379
380         natseq = &nat->seq[IP_CT_DIR_REPLY];
381         if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
382                 return -1;
383
384         return 0;
385 }
386 #else
387 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
388 #endif
389
390 static inline int
391 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
392 {
393         NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
394         return 0;
395
396 nla_put_failure:
397         return -1;
398 }
399
400 static inline int
401 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
402 {
403         NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
404         return 0;
405
406 nla_put_failure:
407         return -1;
408 }
409
410 static int
411 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type,
412                     struct nf_conn *ct)
413 {
414         struct nlmsghdr *nlh;
415         struct nfgenmsg *nfmsg;
416         struct nlattr *nest_parms;
417         unsigned int flags = pid ? NLM_F_MULTI : 0, event;
418
419         event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
420         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
421         if (nlh == NULL)
422                 goto nlmsg_failure;
423
424         nfmsg = nlmsg_data(nlh);
425         nfmsg->nfgen_family = nf_ct_l3num(ct);
426         nfmsg->version      = NFNETLINK_V0;
427         nfmsg->res_id       = 0;
428
429         nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
430         if (!nest_parms)
431                 goto nla_put_failure;
432         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
433                 goto nla_put_failure;
434         nla_nest_end(skb, nest_parms);
435
436         nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
437         if (!nest_parms)
438                 goto nla_put_failure;
439         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
440                 goto nla_put_failure;
441         nla_nest_end(skb, nest_parms);
442
443         if (nf_ct_zone(ct))
444                 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
445
446         if (ctnetlink_dump_status(skb, ct) < 0 ||
447             ctnetlink_dump_timeout(skb, ct) < 0 ||
448             ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
449             ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
450             ctnetlink_dump_timestamp(skb, ct) < 0 ||
451             ctnetlink_dump_protoinfo(skb, ct) < 0 ||
452             ctnetlink_dump_helpinfo(skb, ct) < 0 ||
453             ctnetlink_dump_mark(skb, ct) < 0 ||
454             ctnetlink_dump_secctx(skb, ct) < 0 ||
455             ctnetlink_dump_id(skb, ct) < 0 ||
456             ctnetlink_dump_use(skb, ct) < 0 ||
457             ctnetlink_dump_master(skb, ct) < 0 ||
458             ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
459                 goto nla_put_failure;
460
461         nlmsg_end(skb, nlh);
462         return skb->len;
463
464 nlmsg_failure:
465 nla_put_failure:
466         nlmsg_cancel(skb, nlh);
467         return -1;
468 }
469
470 #ifdef CONFIG_NF_CONNTRACK_EVENTS
471 static inline size_t
472 ctnetlink_proto_size(const struct nf_conn *ct)
473 {
474         struct nf_conntrack_l3proto *l3proto;
475         struct nf_conntrack_l4proto *l4proto;
476         size_t len = 0;
477
478         rcu_read_lock();
479         l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
480         len += l3proto->nla_size;
481
482         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
483         len += l4proto->nla_size;
484         rcu_read_unlock();
485
486         return len;
487 }
488
489 static inline size_t
490 ctnetlink_counters_size(const struct nf_conn *ct)
491 {
492         if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
493                 return 0;
494         return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
495                + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
496                + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
497                ;
498 }
499
500 static inline int
501 ctnetlink_secctx_size(const struct nf_conn *ct)
502 {
503 #ifdef CONFIG_NF_CONNTRACK_SECMARK
504         int len, ret;
505
506         ret = security_secid_to_secctx(ct->secmark, NULL, &len);
507         if (ret)
508                 return 0;
509
510         return nla_total_size(0) /* CTA_SECCTX */
511                + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
512 #else
513         return 0;
514 #endif
515 }
516
517 static inline size_t
518 ctnetlink_timestamp_size(const struct nf_conn *ct)
519 {
520 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
521         if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
522                 return 0;
523         return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
524 #else
525         return 0;
526 #endif
527 }
528
529 static inline size_t
530 ctnetlink_nlmsg_size(const struct nf_conn *ct)
531 {
532         return NLMSG_ALIGN(sizeof(struct nfgenmsg))
533                + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
534                + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
535                + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
536                + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
537                + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
538                + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
539                + ctnetlink_counters_size(ct)
540                + ctnetlink_timestamp_size(ct)
541                + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
542                + nla_total_size(0) /* CTA_PROTOINFO */
543                + nla_total_size(0) /* CTA_HELP */
544                + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
545                + ctnetlink_secctx_size(ct)
546 #ifdef CONFIG_NF_NAT_NEEDED
547                + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
548                + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
549 #endif
550 #ifdef CONFIG_NF_CONNTRACK_MARK
551                + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
552 #endif
553                + ctnetlink_proto_size(ct)
554                ;
555 }
556
557 static int
558 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
559 {
560         struct net *net;
561         struct nlmsghdr *nlh;
562         struct nfgenmsg *nfmsg;
563         struct nlattr *nest_parms;
564         struct nf_conn *ct = item->ct;
565         struct sk_buff *skb;
566         unsigned int type;
567         unsigned int flags = 0, group;
568         int err;
569
570         /* ignore our fake conntrack entry */
571         if (nf_ct_is_untracked(ct))
572                 return 0;
573
574         if (events & (1 << IPCT_DESTROY)) {
575                 type = IPCTNL_MSG_CT_DELETE;
576                 group = NFNLGRP_CONNTRACK_DESTROY;
577         } else  if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
578                 type = IPCTNL_MSG_CT_NEW;
579                 flags = NLM_F_CREATE|NLM_F_EXCL;
580                 group = NFNLGRP_CONNTRACK_NEW;
581         } else  if (events) {
582                 type = IPCTNL_MSG_CT_NEW;
583                 group = NFNLGRP_CONNTRACK_UPDATE;
584         } else
585                 return 0;
586
587         net = nf_ct_net(ct);
588         if (!item->report && !nfnetlink_has_listeners(net, group))
589                 return 0;
590
591         skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
592         if (skb == NULL)
593                 goto errout;
594
595         type |= NFNL_SUBSYS_CTNETLINK << 8;
596         nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
597         if (nlh == NULL)
598                 goto nlmsg_failure;
599
600         nfmsg = nlmsg_data(nlh);
601         nfmsg->nfgen_family = nf_ct_l3num(ct);
602         nfmsg->version  = NFNETLINK_V0;
603         nfmsg->res_id   = 0;
604
605         rcu_read_lock();
606         nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
607         if (!nest_parms)
608                 goto nla_put_failure;
609         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
610                 goto nla_put_failure;
611         nla_nest_end(skb, nest_parms);
612
613         nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
614         if (!nest_parms)
615                 goto nla_put_failure;
616         if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
617                 goto nla_put_failure;
618         nla_nest_end(skb, nest_parms);
619
620         if (nf_ct_zone(ct))
621                 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
622
623         if (ctnetlink_dump_id(skb, ct) < 0)
624                 goto nla_put_failure;
625
626         if (ctnetlink_dump_status(skb, ct) < 0)
627                 goto nla_put_failure;
628
629         if (events & (1 << IPCT_DESTROY)) {
630                 if (ctnetlink_dump_counters(skb, ct,
631                                             IP_CT_DIR_ORIGINAL, type) < 0 ||
632                     ctnetlink_dump_counters(skb, ct,
633                                             IP_CT_DIR_REPLY, type) < 0 ||
634                     ctnetlink_dump_timestamp(skb, ct) < 0)
635                         goto nla_put_failure;
636         } else {
637                 if (ctnetlink_dump_timeout(skb, ct) < 0)
638                         goto nla_put_failure;
639
640                 if (events & (1 << IPCT_PROTOINFO)
641                     && ctnetlink_dump_protoinfo(skb, ct) < 0)
642                         goto nla_put_failure;
643
644                 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
645                     && ctnetlink_dump_helpinfo(skb, ct) < 0)
646                         goto nla_put_failure;
647
648 #ifdef CONFIG_NF_CONNTRACK_SECMARK
649                 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
650                     && ctnetlink_dump_secctx(skb, ct) < 0)
651                         goto nla_put_failure;
652 #endif
653
654                 if (events & (1 << IPCT_RELATED) &&
655                     ctnetlink_dump_master(skb, ct) < 0)
656                         goto nla_put_failure;
657
658                 if (events & (1 << IPCT_NATSEQADJ) &&
659                     ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
660                         goto nla_put_failure;
661         }
662
663 #ifdef CONFIG_NF_CONNTRACK_MARK
664         if ((events & (1 << IPCT_MARK) || ct->mark)
665             && ctnetlink_dump_mark(skb, ct) < 0)
666                 goto nla_put_failure;
667 #endif
668         rcu_read_unlock();
669
670         nlmsg_end(skb, nlh);
671         err = nfnetlink_send(skb, net, item->pid, group, item->report,
672                              GFP_ATOMIC);
673         if (err == -ENOBUFS || err == -EAGAIN)
674                 return -ENOBUFS;
675
676         return 0;
677
678 nla_put_failure:
679         rcu_read_unlock();
680         nlmsg_cancel(skb, nlh);
681 nlmsg_failure:
682         kfree_skb(skb);
683 errout:
684         if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
685                 return -ENOBUFS;
686
687         return 0;
688 }
689 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
690
691 static int ctnetlink_done(struct netlink_callback *cb)
692 {
693         if (cb->args[1])
694                 nf_ct_put((struct nf_conn *)cb->args[1]);
695         if (cb->data)
696                 kfree(cb->data);
697         return 0;
698 }
699
700 struct ctnetlink_dump_filter {
701         struct {
702                 u_int32_t val;
703                 u_int32_t mask;
704         } mark;
705 };
706
707 static int
708 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
709 {
710         struct net *net = sock_net(skb->sk);
711         struct nf_conn *ct, *last;
712         struct nf_conntrack_tuple_hash *h;
713         struct hlist_nulls_node *n;
714         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
715         u_int8_t l3proto = nfmsg->nfgen_family;
716         int res;
717 #ifdef CONFIG_NF_CONNTRACK_MARK
718         const struct ctnetlink_dump_filter *filter = cb->data;
719 #endif
720
721         spin_lock_bh(&nf_conntrack_lock);
722         last = (struct nf_conn *)cb->args[1];
723         for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
724 restart:
725                 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
726                                          hnnode) {
727                         if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
728                                 continue;
729                         ct = nf_ct_tuplehash_to_ctrack(h);
730                         /* Dump entries of a given L3 protocol number.
731                          * If it is not specified, ie. l3proto == 0,
732                          * then dump everything. */
733                         if (l3proto && nf_ct_l3num(ct) != l3proto)
734                                 continue;
735                         if (cb->args[1]) {
736                                 if (ct != last)
737                                         continue;
738                                 cb->args[1] = 0;
739                         }
740 #ifdef CONFIG_NF_CONNTRACK_MARK
741                         if (filter && !((ct->mark & filter->mark.mask) ==
742                                         filter->mark.val)) {
743                                 continue;
744                         }
745 #endif
746                         rcu_read_lock();
747                         res =
748                         ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
749                                             cb->nlh->nlmsg_seq,
750                                             NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
751                                             ct);
752                         rcu_read_unlock();
753                         if (res < 0) {
754                                 nf_conntrack_get(&ct->ct_general);
755                                 cb->args[1] = (unsigned long)ct;
756                                 goto out;
757                         }
758                 }
759                 if (cb->args[1]) {
760                         cb->args[1] = 0;
761                         goto restart;
762                 }
763         }
764 out:
765         spin_unlock_bh(&nf_conntrack_lock);
766         if (last)
767                 nf_ct_put(last);
768
769         return skb->len;
770 }
771
772 static inline int
773 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
774 {
775         struct nlattr *tb[CTA_IP_MAX+1];
776         struct nf_conntrack_l3proto *l3proto;
777         int ret = 0;
778
779         nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
780
781         rcu_read_lock();
782         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
783
784         if (likely(l3proto->nlattr_to_tuple)) {
785                 ret = nla_validate_nested(attr, CTA_IP_MAX,
786                                           l3proto->nla_policy);
787                 if (ret == 0)
788                         ret = l3proto->nlattr_to_tuple(tb, tuple);
789         }
790
791         rcu_read_unlock();
792
793         return ret;
794 }
795
796 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
797         [CTA_PROTO_NUM] = { .type = NLA_U8 },
798 };
799
800 static inline int
801 ctnetlink_parse_tuple_proto(struct nlattr *attr,
802                             struct nf_conntrack_tuple *tuple)
803 {
804         struct nlattr *tb[CTA_PROTO_MAX+1];
805         struct nf_conntrack_l4proto *l4proto;
806         int ret = 0;
807
808         ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
809         if (ret < 0)
810                 return ret;
811
812         if (!tb[CTA_PROTO_NUM])
813                 return -EINVAL;
814         tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
815
816         rcu_read_lock();
817         l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
818
819         if (likely(l4proto->nlattr_to_tuple)) {
820                 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
821                                           l4proto->nla_policy);
822                 if (ret == 0)
823                         ret = l4proto->nlattr_to_tuple(tb, tuple);
824         }
825
826         rcu_read_unlock();
827
828         return ret;
829 }
830
831 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
832         [CTA_TUPLE_IP]          = { .type = NLA_NESTED },
833         [CTA_TUPLE_PROTO]       = { .type = NLA_NESTED },
834 };
835
836 static int
837 ctnetlink_parse_tuple(const struct nlattr * const cda[],
838                       struct nf_conntrack_tuple *tuple,
839                       enum ctattr_type type, u_int8_t l3num)
840 {
841         struct nlattr *tb[CTA_TUPLE_MAX+1];
842         int err;
843
844         memset(tuple, 0, sizeof(*tuple));
845
846         nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
847
848         if (!tb[CTA_TUPLE_IP])
849                 return -EINVAL;
850
851         tuple->src.l3num = l3num;
852
853         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
854         if (err < 0)
855                 return err;
856
857         if (!tb[CTA_TUPLE_PROTO])
858                 return -EINVAL;
859
860         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
861         if (err < 0)
862                 return err;
863
864         /* orig and expect tuples get DIR_ORIGINAL */
865         if (type == CTA_TUPLE_REPLY)
866                 tuple->dst.dir = IP_CT_DIR_REPLY;
867         else
868                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
869
870         return 0;
871 }
872
873 static int
874 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
875 {
876         if (attr)
877 #ifdef CONFIG_NF_CONNTRACK_ZONES
878                 *zone = ntohs(nla_get_be16(attr));
879 #else
880                 return -EOPNOTSUPP;
881 #endif
882         else
883                 *zone = 0;
884
885         return 0;
886 }
887
888 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
889         [CTA_HELP_NAME]         = { .type = NLA_NUL_STRING },
890 };
891
892 static inline int
893 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
894 {
895         struct nlattr *tb[CTA_HELP_MAX+1];
896
897         nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
898
899         if (!tb[CTA_HELP_NAME])
900                 return -EINVAL;
901
902         *helper_name = nla_data(tb[CTA_HELP_NAME]);
903
904         return 0;
905 }
906
907 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
908         [CTA_TUPLE_ORIG]        = { .type = NLA_NESTED },
909         [CTA_TUPLE_REPLY]       = { .type = NLA_NESTED },
910         [CTA_STATUS]            = { .type = NLA_U32 },
911         [CTA_PROTOINFO]         = { .type = NLA_NESTED },
912         [CTA_HELP]              = { .type = NLA_NESTED },
913         [CTA_NAT_SRC]           = { .type = NLA_NESTED },
914         [CTA_TIMEOUT]           = { .type = NLA_U32 },
915         [CTA_MARK]              = { .type = NLA_U32 },
916         [CTA_ID]                = { .type = NLA_U32 },
917         [CTA_NAT_DST]           = { .type = NLA_NESTED },
918         [CTA_TUPLE_MASTER]      = { .type = NLA_NESTED },
919         [CTA_ZONE]              = { .type = NLA_U16 },
920         [CTA_MARK_MASK]         = { .type = NLA_U32 },
921 };
922
923 static int
924 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
925                         const struct nlmsghdr *nlh,
926                         const struct nlattr * const cda[])
927 {
928         struct net *net = sock_net(ctnl);
929         struct nf_conntrack_tuple_hash *h;
930         struct nf_conntrack_tuple tuple;
931         struct nf_conn *ct;
932         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
933         u_int8_t u3 = nfmsg->nfgen_family;
934         u16 zone;
935         int err;
936
937         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
938         if (err < 0)
939                 return err;
940
941         if (cda[CTA_TUPLE_ORIG])
942                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
943         else if (cda[CTA_TUPLE_REPLY])
944                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
945         else {
946                 /* Flush the whole table */
947                 nf_conntrack_flush_report(net,
948                                          NETLINK_CB(skb).pid,
949                                          nlmsg_report(nlh));
950                 return 0;
951         }
952
953         if (err < 0)
954                 return err;
955
956         h = nf_conntrack_find_get(net, zone, &tuple);
957         if (!h)
958                 return -ENOENT;
959
960         ct = nf_ct_tuplehash_to_ctrack(h);
961
962         if (cda[CTA_ID]) {
963                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
964                 if (id != (u32)(unsigned long)ct) {
965                         nf_ct_put(ct);
966                         return -ENOENT;
967                 }
968         }
969
970         if (nf_conntrack_event_report(IPCT_DESTROY, ct,
971                                       NETLINK_CB(skb).pid,
972                                       nlmsg_report(nlh)) < 0) {
973                 nf_ct_delete_from_lists(ct);
974                 /* we failed to report the event, try later */
975                 nf_ct_insert_dying_list(ct);
976                 nf_ct_put(ct);
977                 return 0;
978         }
979
980         /* death_by_timeout would report the event again */
981         set_bit(IPS_DYING_BIT, &ct->status);
982
983         nf_ct_kill(ct);
984         nf_ct_put(ct);
985
986         return 0;
987 }
988
989 static int
990 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
991                         const struct nlmsghdr *nlh,
992                         const struct nlattr * const cda[])
993 {
994         struct net *net = sock_net(ctnl);
995         struct nf_conntrack_tuple_hash *h;
996         struct nf_conntrack_tuple tuple;
997         struct nf_conn *ct;
998         struct sk_buff *skb2 = NULL;
999         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1000         u_int8_t u3 = nfmsg->nfgen_family;
1001         u16 zone;
1002         int err;
1003
1004         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1005                 struct netlink_dump_control c = {
1006                         .dump = ctnetlink_dump_table,
1007                         .done = ctnetlink_done,
1008                 };
1009 #ifdef CONFIG_NF_CONNTRACK_MARK
1010                 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1011                         struct ctnetlink_dump_filter *filter;
1012
1013                         filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1014                                          GFP_ATOMIC);
1015                         if (filter == NULL)
1016                                 return -ENOMEM;
1017
1018                         filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1019                         filter->mark.mask =
1020                                 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1021                         c.data = filter;
1022                 }
1023 #endif
1024                 return netlink_dump_start(ctnl, skb, nlh, &c);
1025         }
1026
1027         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1028         if (err < 0)
1029                 return err;
1030
1031         if (cda[CTA_TUPLE_ORIG])
1032                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1033         else if (cda[CTA_TUPLE_REPLY])
1034                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1035         else
1036                 return -EINVAL;
1037
1038         if (err < 0)
1039                 return err;
1040
1041         h = nf_conntrack_find_get(net, zone, &tuple);
1042         if (!h)
1043                 return -ENOENT;
1044
1045         ct = nf_ct_tuplehash_to_ctrack(h);
1046
1047         err = -ENOMEM;
1048         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1049         if (skb2 == NULL) {
1050                 nf_ct_put(ct);
1051                 return -ENOMEM;
1052         }
1053
1054         rcu_read_lock();
1055         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
1056                                   NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1057         rcu_read_unlock();
1058         nf_ct_put(ct);
1059         if (err <= 0)
1060                 goto free;
1061
1062         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1063         if (err < 0)
1064                 goto out;
1065
1066         return 0;
1067
1068 free:
1069         kfree_skb(skb2);
1070 out:
1071         /* this avoids a loop in nfnetlink. */
1072         return err == -EAGAIN ? -ENOBUFS : err;
1073 }
1074
1075 #ifdef CONFIG_NF_NAT_NEEDED
1076 static int
1077 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1078                           enum nf_nat_manip_type manip,
1079                           const struct nlattr *attr)
1080 {
1081         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1082
1083         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1084         if (!parse_nat_setup) {
1085 #ifdef CONFIG_MODULES
1086                 rcu_read_unlock();
1087                 nfnl_unlock();
1088                 if (request_module("nf-nat-ipv4") < 0) {
1089                         nfnl_lock();
1090                         rcu_read_lock();
1091                         return -EOPNOTSUPP;
1092                 }
1093                 nfnl_lock();
1094                 rcu_read_lock();
1095                 if (nfnetlink_parse_nat_setup_hook)
1096                         return -EAGAIN;
1097 #endif
1098                 return -EOPNOTSUPP;
1099         }
1100
1101         return parse_nat_setup(ct, manip, attr);
1102 }
1103 #endif
1104
1105 static int
1106 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1107 {
1108         unsigned long d;
1109         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1110         d = ct->status ^ status;
1111
1112         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1113                 /* unchangeable */
1114                 return -EBUSY;
1115
1116         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1117                 /* SEEN_REPLY bit can only be set */
1118                 return -EBUSY;
1119
1120         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1121                 /* ASSURED bit can only be set */
1122                 return -EBUSY;
1123
1124         /* Be careful here, modifying NAT bits can screw up things,
1125          * so don't let users modify them directly if they don't pass
1126          * nf_nat_range. */
1127         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1128         return 0;
1129 }
1130
1131 static int
1132 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1133 {
1134 #ifdef CONFIG_NF_NAT_NEEDED
1135         int ret;
1136
1137         if (cda[CTA_NAT_DST]) {
1138                 ret = ctnetlink_parse_nat_setup(ct,
1139                                                 NF_NAT_MANIP_DST,
1140                                                 cda[CTA_NAT_DST]);
1141                 if (ret < 0)
1142                         return ret;
1143         }
1144         if (cda[CTA_NAT_SRC]) {
1145                 ret = ctnetlink_parse_nat_setup(ct,
1146                                                 NF_NAT_MANIP_SRC,
1147                                                 cda[CTA_NAT_SRC]);
1148                 if (ret < 0)
1149                         return ret;
1150         }
1151         return 0;
1152 #else
1153         return -EOPNOTSUPP;
1154 #endif
1155 }
1156
1157 static inline int
1158 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1159 {
1160         struct nf_conntrack_helper *helper;
1161         struct nf_conn_help *help = nfct_help(ct);
1162         char *helpname = NULL;
1163         int err;
1164
1165         /* don't change helper of sibling connections */
1166         if (ct->master)
1167                 return -EBUSY;
1168
1169         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1170         if (err < 0)
1171                 return err;
1172
1173         if (!strcmp(helpname, "")) {
1174                 if (help && help->helper) {
1175                         /* we had a helper before ... */
1176                         nf_ct_remove_expectations(ct);
1177                         RCU_INIT_POINTER(help->helper, NULL);
1178                 }
1179
1180                 return 0;
1181         }
1182
1183         helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1184                                             nf_ct_protonum(ct));
1185         if (helper == NULL) {
1186 #ifdef CONFIG_MODULES
1187                 spin_unlock_bh(&nf_conntrack_lock);
1188
1189                 if (request_module("nfct-helper-%s", helpname) < 0) {
1190                         spin_lock_bh(&nf_conntrack_lock);
1191                         return -EOPNOTSUPP;
1192                 }
1193
1194                 spin_lock_bh(&nf_conntrack_lock);
1195                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1196                                                     nf_ct_protonum(ct));
1197                 if (helper)
1198                         return -EAGAIN;
1199 #endif
1200                 return -EOPNOTSUPP;
1201         }
1202
1203         if (help) {
1204                 if (help->helper == helper)
1205                         return 0;
1206                 if (help->helper)
1207                         return -EBUSY;
1208                 /* need to zero data of old helper */
1209                 memset(&help->help, 0, sizeof(help->help));
1210         } else {
1211                 /* we cannot set a helper for an existing conntrack */
1212                 return -EOPNOTSUPP;
1213         }
1214
1215         rcu_assign_pointer(help->helper, helper);
1216
1217         return 0;
1218 }
1219
1220 static inline int
1221 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1222 {
1223         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1224
1225         if (!del_timer(&ct->timeout))
1226                 return -ETIME;
1227
1228         ct->timeout.expires = jiffies + timeout * HZ;
1229         add_timer(&ct->timeout);
1230
1231         return 0;
1232 }
1233
1234 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1235         [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
1236         [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
1237         [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
1238 };
1239
1240 static inline int
1241 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1242 {
1243         const struct nlattr *attr = cda[CTA_PROTOINFO];
1244         struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1245         struct nf_conntrack_l4proto *l4proto;
1246         int err = 0;
1247
1248         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1249
1250         rcu_read_lock();
1251         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1252         if (l4proto->from_nlattr)
1253                 err = l4proto->from_nlattr(tb, ct);
1254         rcu_read_unlock();
1255
1256         return err;
1257 }
1258
1259 #ifdef CONFIG_NF_NAT_NEEDED
1260 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1261         [CTA_NAT_SEQ_CORRECTION_POS]    = { .type = NLA_U32 },
1262         [CTA_NAT_SEQ_OFFSET_BEFORE]     = { .type = NLA_U32 },
1263         [CTA_NAT_SEQ_OFFSET_AFTER]      = { .type = NLA_U32 },
1264 };
1265
1266 static inline int
1267 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1268 {
1269         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1270
1271         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1272
1273         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1274                 return -EINVAL;
1275
1276         natseq->correction_pos =
1277                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1278
1279         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1280                 return -EINVAL;
1281
1282         natseq->offset_before =
1283                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1284
1285         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1286                 return -EINVAL;
1287
1288         natseq->offset_after =
1289                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1290
1291         return 0;
1292 }
1293
1294 static int
1295 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1296                              const struct nlattr * const cda[])
1297 {
1298         int ret = 0;
1299         struct nf_conn_nat *nat = nfct_nat(ct);
1300
1301         if (!nat)
1302                 return 0;
1303
1304         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1305                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1306                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1307                 if (ret < 0)
1308                         return ret;
1309
1310                 ct->status |= IPS_SEQ_ADJUST;
1311         }
1312
1313         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1314                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1315                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1316                 if (ret < 0)
1317                         return ret;
1318
1319                 ct->status |= IPS_SEQ_ADJUST;
1320         }
1321
1322         return 0;
1323 }
1324 #endif
1325
1326 static int
1327 ctnetlink_change_conntrack(struct nf_conn *ct,
1328                            const struct nlattr * const cda[])
1329 {
1330         int err;
1331
1332         /* only allow NAT changes and master assignation for new conntracks */
1333         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1334                 return -EOPNOTSUPP;
1335
1336         if (cda[CTA_HELP]) {
1337                 err = ctnetlink_change_helper(ct, cda);
1338                 if (err < 0)
1339                         return err;
1340         }
1341
1342         if (cda[CTA_TIMEOUT]) {
1343                 err = ctnetlink_change_timeout(ct, cda);
1344                 if (err < 0)
1345                         return err;
1346         }
1347
1348         if (cda[CTA_STATUS]) {
1349                 err = ctnetlink_change_status(ct, cda);
1350                 if (err < 0)
1351                         return err;
1352         }
1353
1354         if (cda[CTA_PROTOINFO]) {
1355                 err = ctnetlink_change_protoinfo(ct, cda);
1356                 if (err < 0)
1357                         return err;
1358         }
1359
1360 #if defined(CONFIG_NF_CONNTRACK_MARK)
1361         if (cda[CTA_MARK])
1362                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1363 #endif
1364
1365 #ifdef CONFIG_NF_NAT_NEEDED
1366         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1367                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1368                 if (err < 0)
1369                         return err;
1370         }
1371 #endif
1372
1373         return 0;
1374 }
1375
1376 static struct nf_conn *
1377 ctnetlink_create_conntrack(struct net *net, u16 zone,
1378                            const struct nlattr * const cda[],
1379                            struct nf_conntrack_tuple *otuple,
1380                            struct nf_conntrack_tuple *rtuple,
1381                            u8 u3)
1382 {
1383         struct nf_conn *ct;
1384         int err = -EINVAL;
1385         struct nf_conntrack_helper *helper;
1386         struct nf_conn_tstamp *tstamp;
1387
1388         ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1389         if (IS_ERR(ct))
1390                 return ERR_PTR(-ENOMEM);
1391
1392         if (!cda[CTA_TIMEOUT])
1393                 goto err1;
1394         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1395
1396         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1397
1398         rcu_read_lock();
1399         if (cda[CTA_HELP]) {
1400                 char *helpname = NULL;
1401  
1402                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1403                 if (err < 0)
1404                         goto err2;
1405
1406                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1407                                                     nf_ct_protonum(ct));
1408                 if (helper == NULL) {
1409                         rcu_read_unlock();
1410 #ifdef CONFIG_MODULES
1411                         if (request_module("nfct-helper-%s", helpname) < 0) {
1412                                 err = -EOPNOTSUPP;
1413                                 goto err1;
1414                         }
1415
1416                         rcu_read_lock();
1417                         helper = __nf_conntrack_helper_find(helpname,
1418                                                             nf_ct_l3num(ct),
1419                                                             nf_ct_protonum(ct));
1420                         if (helper) {
1421                                 err = -EAGAIN;
1422                                 goto err2;
1423                         }
1424                         rcu_read_unlock();
1425 #endif
1426                         err = -EOPNOTSUPP;
1427                         goto err1;
1428                 } else {
1429                         struct nf_conn_help *help;
1430
1431                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1432                         if (help == NULL) {
1433                                 err = -ENOMEM;
1434                                 goto err2;
1435                         }
1436
1437                         /* not in hash table yet so not strictly necessary */
1438                         RCU_INIT_POINTER(help->helper, helper);
1439                 }
1440         } else {
1441                 /* try an implicit helper assignation */
1442                 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1443                 if (err < 0)
1444                         goto err2;
1445         }
1446
1447         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1448                 err = ctnetlink_change_nat(ct, cda);
1449                 if (err < 0)
1450                         goto err2;
1451         }
1452
1453         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1454         nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1455         nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1456         /* we must add conntrack extensions before confirmation. */
1457         ct->status |= IPS_CONFIRMED;
1458
1459         if (cda[CTA_STATUS]) {
1460                 err = ctnetlink_change_status(ct, cda);
1461                 if (err < 0)
1462                         goto err2;
1463         }
1464
1465 #ifdef CONFIG_NF_NAT_NEEDED
1466         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1467                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1468                 if (err < 0)
1469                         goto err2;
1470         }
1471 #endif
1472
1473         memset(&ct->proto, 0, sizeof(ct->proto));
1474         if (cda[CTA_PROTOINFO]) {
1475                 err = ctnetlink_change_protoinfo(ct, cda);
1476                 if (err < 0)
1477                         goto err2;
1478         }
1479
1480 #if defined(CONFIG_NF_CONNTRACK_MARK)
1481         if (cda[CTA_MARK])
1482                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1483 #endif
1484
1485         /* setup master conntrack: this is a confirmed expectation */
1486         if (cda[CTA_TUPLE_MASTER]) {
1487                 struct nf_conntrack_tuple master;
1488                 struct nf_conntrack_tuple_hash *master_h;
1489                 struct nf_conn *master_ct;
1490
1491                 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1492                 if (err < 0)
1493                         goto err2;
1494
1495                 master_h = nf_conntrack_find_get(net, zone, &master);
1496                 if (master_h == NULL) {
1497                         err = -ENOENT;
1498                         goto err2;
1499                 }
1500                 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1501                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1502                 ct->master = master_ct;
1503         }
1504         tstamp = nf_conn_tstamp_find(ct);
1505         if (tstamp)
1506                 tstamp->start = ktime_to_ns(ktime_get_real());
1507
1508         err = nf_conntrack_hash_check_insert(ct);
1509         if (err < 0)
1510                 goto err2;
1511
1512         rcu_read_unlock();
1513
1514         return ct;
1515
1516 err2:
1517         rcu_read_unlock();
1518 err1:
1519         nf_conntrack_free(ct);
1520         return ERR_PTR(err);
1521 }
1522
1523 static int
1524 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1525                         const struct nlmsghdr *nlh,
1526                         const struct nlattr * const cda[])
1527 {
1528         struct net *net = sock_net(ctnl);
1529         struct nf_conntrack_tuple otuple, rtuple;
1530         struct nf_conntrack_tuple_hash *h = NULL;
1531         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1532         struct nf_conn *ct;
1533         u_int8_t u3 = nfmsg->nfgen_family;
1534         u16 zone;
1535         int err;
1536
1537         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1538         if (err < 0)
1539                 return err;
1540
1541         if (cda[CTA_TUPLE_ORIG]) {
1542                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1543                 if (err < 0)
1544                         return err;
1545         }
1546
1547         if (cda[CTA_TUPLE_REPLY]) {
1548                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1549                 if (err < 0)
1550                         return err;
1551         }
1552
1553         if (cda[CTA_TUPLE_ORIG])
1554                 h = nf_conntrack_find_get(net, zone, &otuple);
1555         else if (cda[CTA_TUPLE_REPLY])
1556                 h = nf_conntrack_find_get(net, zone, &rtuple);
1557
1558         if (h == NULL) {
1559                 err = -ENOENT;
1560                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1561                         enum ip_conntrack_events events;
1562
1563                         ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1564                                                         &rtuple, u3);
1565                         if (IS_ERR(ct))
1566                                 return PTR_ERR(ct);
1567
1568                         err = 0;
1569                         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1570                                 events = IPCT_RELATED;
1571                         else
1572                                 events = IPCT_NEW;
1573
1574                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1575                                                       (1 << IPCT_ASSURED) |
1576                                                       (1 << IPCT_HELPER) |
1577                                                       (1 << IPCT_PROTOINFO) |
1578                                                       (1 << IPCT_NATSEQADJ) |
1579                                                       (1 << IPCT_MARK) | events,
1580                                                       ct, NETLINK_CB(skb).pid,
1581                                                       nlmsg_report(nlh));
1582                         nf_ct_put(ct);
1583                 }
1584
1585                 return err;
1586         }
1587         /* implicit 'else' */
1588
1589         err = -EEXIST;
1590         ct = nf_ct_tuplehash_to_ctrack(h);
1591         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1592                 spin_lock_bh(&nf_conntrack_lock);
1593                 err = ctnetlink_change_conntrack(ct, cda);
1594                 spin_unlock_bh(&nf_conntrack_lock);
1595                 if (err == 0) {
1596                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1597                                                       (1 << IPCT_ASSURED) |
1598                                                       (1 << IPCT_HELPER) |
1599                                                       (1 << IPCT_PROTOINFO) |
1600                                                       (1 << IPCT_NATSEQADJ) |
1601                                                       (1 << IPCT_MARK),
1602                                                       ct, NETLINK_CB(skb).pid,
1603                                                       nlmsg_report(nlh));
1604                 }
1605         }
1606
1607         nf_ct_put(ct);
1608         return err;
1609 }
1610
1611 /***********************************************************************
1612  * EXPECT
1613  ***********************************************************************/
1614
1615 static inline int
1616 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1617                          const struct nf_conntrack_tuple *tuple,
1618                          enum ctattr_expect type)
1619 {
1620         struct nlattr *nest_parms;
1621
1622         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1623         if (!nest_parms)
1624                 goto nla_put_failure;
1625         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1626                 goto nla_put_failure;
1627         nla_nest_end(skb, nest_parms);
1628
1629         return 0;
1630
1631 nla_put_failure:
1632         return -1;
1633 }
1634
1635 static inline int
1636 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1637                         const struct nf_conntrack_tuple *tuple,
1638                         const struct nf_conntrack_tuple_mask *mask)
1639 {
1640         int ret;
1641         struct nf_conntrack_l3proto *l3proto;
1642         struct nf_conntrack_l4proto *l4proto;
1643         struct nf_conntrack_tuple m;
1644         struct nlattr *nest_parms;
1645
1646         memset(&m, 0xFF, sizeof(m));
1647         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1648         m.src.u.all = mask->src.u.all;
1649         m.dst.protonum = tuple->dst.protonum;
1650
1651         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1652         if (!nest_parms)
1653                 goto nla_put_failure;
1654
1655         rcu_read_lock();
1656         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1657         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1658         if (ret >= 0) {
1659                 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
1660                                                tuple->dst.protonum);
1661         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1662         }
1663         rcu_read_unlock();
1664
1665         if (unlikely(ret < 0))
1666                 goto nla_put_failure;
1667
1668         nla_nest_end(skb, nest_parms);
1669
1670         return 0;
1671
1672 nla_put_failure:
1673         return -1;
1674 }
1675
1676 static int
1677 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1678                           const struct nf_conntrack_expect *exp)
1679 {
1680         struct nf_conn *master = exp->master;
1681         long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
1682         struct nf_conn_help *help;
1683 #ifdef CONFIG_NF_NAT_NEEDED
1684         struct nlattr *nest_parms;
1685         struct nf_conntrack_tuple nat_tuple = {};
1686 #endif
1687         struct nf_ct_helper_expectfn *expfn;
1688
1689         if (timeout < 0)
1690                 timeout = 0;
1691
1692         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1693                 goto nla_put_failure;
1694         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1695                 goto nla_put_failure;
1696         if (ctnetlink_exp_dump_tuple(skb,
1697                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1698                                  CTA_EXPECT_MASTER) < 0)
1699                 goto nla_put_failure;
1700
1701 #ifdef CONFIG_NF_NAT_NEEDED
1702         if (exp->saved_ip || exp->saved_proto.all) {
1703                 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
1704                 if (!nest_parms)
1705                         goto nla_put_failure;
1706
1707                 NLA_PUT_BE32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir));
1708
1709                 nat_tuple.src.l3num = nf_ct_l3num(master);
1710                 nat_tuple.src.u3.ip = exp->saved_ip;
1711                 nat_tuple.dst.protonum = nf_ct_protonum(master);
1712                 nat_tuple.src.u = exp->saved_proto;
1713
1714                 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
1715                                                 CTA_EXPECT_NAT_TUPLE) < 0)
1716                         goto nla_put_failure;
1717                 nla_nest_end(skb, nest_parms);
1718         }
1719 #endif
1720         NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1721         NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1722         NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1723         NLA_PUT_BE32(skb, CTA_EXPECT_CLASS, htonl(exp->class));
1724         help = nfct_help(master);
1725         if (help) {
1726                 struct nf_conntrack_helper *helper;
1727
1728                 helper = rcu_dereference(help->helper);
1729                 if (helper)
1730                         NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1731         }
1732         expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
1733         if (expfn != NULL)
1734                 NLA_PUT_STRING(skb, CTA_EXPECT_FN, expfn->name);
1735
1736         return 0;
1737
1738 nla_put_failure:
1739         return -1;
1740 }
1741
1742 static int
1743 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1744                         int event, const struct nf_conntrack_expect *exp)
1745 {
1746         struct nlmsghdr *nlh;
1747         struct nfgenmsg *nfmsg;
1748         unsigned int flags = pid ? NLM_F_MULTI : 0;
1749
1750         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1751         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1752         if (nlh == NULL)
1753                 goto nlmsg_failure;
1754
1755         nfmsg = nlmsg_data(nlh);
1756         nfmsg->nfgen_family = exp->tuple.src.l3num;
1757         nfmsg->version      = NFNETLINK_V0;
1758         nfmsg->res_id       = 0;
1759
1760         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1761                 goto nla_put_failure;
1762
1763         nlmsg_end(skb, nlh);
1764         return skb->len;
1765
1766 nlmsg_failure:
1767 nla_put_failure:
1768         nlmsg_cancel(skb, nlh);
1769         return -1;
1770 }
1771
1772 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1773 static int
1774 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1775 {
1776         struct nf_conntrack_expect *exp = item->exp;
1777         struct net *net = nf_ct_exp_net(exp);
1778         struct nlmsghdr *nlh;
1779         struct nfgenmsg *nfmsg;
1780         struct sk_buff *skb;
1781         unsigned int type, group;
1782         int flags = 0;
1783
1784         if (events & (1 << IPEXP_DESTROY)) {
1785                 type = IPCTNL_MSG_EXP_DELETE;
1786                 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1787         } else if (events & (1 << IPEXP_NEW)) {
1788                 type = IPCTNL_MSG_EXP_NEW;
1789                 flags = NLM_F_CREATE|NLM_F_EXCL;
1790                 group = NFNLGRP_CONNTRACK_EXP_NEW;
1791         } else
1792                 return 0;
1793
1794         if (!item->report && !nfnetlink_has_listeners(net, group))
1795                 return 0;
1796
1797         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1798         if (skb == NULL)
1799                 goto errout;
1800
1801         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1802         nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1803         if (nlh == NULL)
1804                 goto nlmsg_failure;
1805
1806         nfmsg = nlmsg_data(nlh);
1807         nfmsg->nfgen_family = exp->tuple.src.l3num;
1808         nfmsg->version      = NFNETLINK_V0;
1809         nfmsg->res_id       = 0;
1810
1811         rcu_read_lock();
1812         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1813                 goto nla_put_failure;
1814         rcu_read_unlock();
1815
1816         nlmsg_end(skb, nlh);
1817         nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1818         return 0;
1819
1820 nla_put_failure:
1821         rcu_read_unlock();
1822         nlmsg_cancel(skb, nlh);
1823 nlmsg_failure:
1824         kfree_skb(skb);
1825 errout:
1826         nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1827         return 0;
1828 }
1829 #endif
1830 static int ctnetlink_exp_done(struct netlink_callback *cb)
1831 {
1832         if (cb->args[1])
1833                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1834         return 0;
1835 }
1836
1837 static int
1838 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1839 {
1840         struct net *net = sock_net(skb->sk);
1841         struct nf_conntrack_expect *exp, *last;
1842         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1843         struct hlist_node *n;
1844         u_int8_t l3proto = nfmsg->nfgen_family;
1845
1846         rcu_read_lock();
1847         last = (struct nf_conntrack_expect *)cb->args[1];
1848         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1849 restart:
1850                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1851                                      hnode) {
1852                         if (l3proto && exp->tuple.src.l3num != l3proto)
1853                                 continue;
1854                         if (cb->args[1]) {
1855                                 if (exp != last)
1856                                         continue;
1857                                 cb->args[1] = 0;
1858                         }
1859                         if (ctnetlink_exp_fill_info(skb,
1860                                                     NETLINK_CB(cb->skb).pid,
1861                                                     cb->nlh->nlmsg_seq,
1862                                                     IPCTNL_MSG_EXP_NEW,
1863                                                     exp) < 0) {
1864                                 if (!atomic_inc_not_zero(&exp->use))
1865                                         continue;
1866                                 cb->args[1] = (unsigned long)exp;
1867                                 goto out;
1868                         }
1869                 }
1870                 if (cb->args[1]) {
1871                         cb->args[1] = 0;
1872                         goto restart;
1873                 }
1874         }
1875 out:
1876         rcu_read_unlock();
1877         if (last)
1878                 nf_ct_expect_put(last);
1879
1880         return skb->len;
1881 }
1882
1883 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1884         [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
1885         [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
1886         [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
1887         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1888         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1889         [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING },
1890         [CTA_EXPECT_ZONE]       = { .type = NLA_U16 },
1891         [CTA_EXPECT_FLAGS]      = { .type = NLA_U32 },
1892         [CTA_EXPECT_CLASS]      = { .type = NLA_U32 },
1893         [CTA_EXPECT_NAT]        = { .type = NLA_NESTED },
1894         [CTA_EXPECT_FN]         = { .type = NLA_NUL_STRING },
1895 };
1896
1897 static int
1898 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1899                      const struct nlmsghdr *nlh,
1900                      const struct nlattr * const cda[])
1901 {
1902         struct net *net = sock_net(ctnl);
1903         struct nf_conntrack_tuple tuple;
1904         struct nf_conntrack_expect *exp;
1905         struct sk_buff *skb2;
1906         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1907         u_int8_t u3 = nfmsg->nfgen_family;
1908         u16 zone;
1909         int err;
1910
1911         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1912                 struct netlink_dump_control c = {
1913                         .dump = ctnetlink_exp_dump_table,
1914                         .done = ctnetlink_exp_done,
1915                 };
1916                 return netlink_dump_start(ctnl, skb, nlh, &c);
1917         }
1918
1919         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1920         if (err < 0)
1921                 return err;
1922
1923         if (cda[CTA_EXPECT_TUPLE])
1924                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1925         else if (cda[CTA_EXPECT_MASTER])
1926                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1927         else
1928                 return -EINVAL;
1929
1930         if (err < 0)
1931                 return err;
1932
1933         exp = nf_ct_expect_find_get(net, zone, &tuple);
1934         if (!exp)
1935                 return -ENOENT;
1936
1937         if (cda[CTA_EXPECT_ID]) {
1938                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1939                 if (ntohl(id) != (u32)(unsigned long)exp) {
1940                         nf_ct_expect_put(exp);
1941                         return -ENOENT;
1942                 }
1943         }
1944
1945         err = -ENOMEM;
1946         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1947         if (skb2 == NULL) {
1948                 nf_ct_expect_put(exp);
1949                 goto out;
1950         }
1951
1952         rcu_read_lock();
1953         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1954                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1955         rcu_read_unlock();
1956         nf_ct_expect_put(exp);
1957         if (err <= 0)
1958                 goto free;
1959
1960         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1961         if (err < 0)
1962                 goto out;
1963
1964         return 0;
1965
1966 free:
1967         kfree_skb(skb2);
1968 out:
1969         /* this avoids a loop in nfnetlink. */
1970         return err == -EAGAIN ? -ENOBUFS : err;
1971 }
1972
1973 static int
1974 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1975                      const struct nlmsghdr *nlh,
1976                      const struct nlattr * const cda[])
1977 {
1978         struct net *net = sock_net(ctnl);
1979         struct nf_conntrack_expect *exp;
1980         struct nf_conntrack_tuple tuple;
1981         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1982         struct hlist_node *n, *next;
1983         u_int8_t u3 = nfmsg->nfgen_family;
1984         unsigned int i;
1985         u16 zone;
1986         int err;
1987
1988         if (cda[CTA_EXPECT_TUPLE]) {
1989                 /* delete a single expect by tuple */
1990                 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1991                 if (err < 0)
1992                         return err;
1993
1994                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1995                 if (err < 0)
1996                         return err;
1997
1998                 /* bump usage count to 2 */
1999                 exp = nf_ct_expect_find_get(net, zone, &tuple);
2000                 if (!exp)
2001                         return -ENOENT;
2002
2003                 if (cda[CTA_EXPECT_ID]) {
2004                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2005                         if (ntohl(id) != (u32)(unsigned long)exp) {
2006                                 nf_ct_expect_put(exp);
2007                                 return -ENOENT;
2008                         }
2009                 }
2010
2011                 /* after list removal, usage count == 1 */
2012                 spin_lock_bh(&nf_conntrack_lock);
2013                 if (del_timer(&exp->timeout)) {
2014                         nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
2015                                                    nlmsg_report(nlh));
2016                         nf_ct_expect_put(exp);
2017                 }
2018                 spin_unlock_bh(&nf_conntrack_lock);
2019                 /* have to put what we 'get' above.
2020                  * after this line usage count == 0 */
2021                 nf_ct_expect_put(exp);
2022         } else if (cda[CTA_EXPECT_HELP_NAME]) {
2023                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2024                 struct nf_conn_help *m_help;
2025
2026                 /* delete all expectations for this helper */
2027                 spin_lock_bh(&nf_conntrack_lock);
2028                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2029                         hlist_for_each_entry_safe(exp, n, next,
2030                                                   &net->ct.expect_hash[i],
2031                                                   hnode) {
2032                                 m_help = nfct_help(exp->master);
2033                                 if (!strcmp(m_help->helper->name, name) &&
2034                                     del_timer(&exp->timeout)) {
2035                                         nf_ct_unlink_expect_report(exp,
2036                                                         NETLINK_CB(skb).pid,
2037                                                         nlmsg_report(nlh));
2038                                         nf_ct_expect_put(exp);
2039                                 }
2040                         }
2041                 }
2042                 spin_unlock_bh(&nf_conntrack_lock);
2043         } else {
2044                 /* This basically means we have to flush everything*/
2045                 spin_lock_bh(&nf_conntrack_lock);
2046                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2047                         hlist_for_each_entry_safe(exp, n, next,
2048                                                   &net->ct.expect_hash[i],
2049                                                   hnode) {
2050                                 if (del_timer(&exp->timeout)) {
2051                                         nf_ct_unlink_expect_report(exp,
2052                                                         NETLINK_CB(skb).pid,
2053                                                         nlmsg_report(nlh));
2054                                         nf_ct_expect_put(exp);
2055                                 }
2056                         }
2057                 }
2058                 spin_unlock_bh(&nf_conntrack_lock);
2059         }
2060
2061         return 0;
2062 }
2063 static int
2064 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2065                         const struct nlattr * const cda[])
2066 {
2067         return -EOPNOTSUPP;
2068 }
2069
2070 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2071         [CTA_EXPECT_NAT_DIR]    = { .type = NLA_U32 },
2072         [CTA_EXPECT_NAT_TUPLE]  = { .type = NLA_NESTED },
2073 };
2074
2075 static int
2076 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2077                            struct nf_conntrack_expect *exp,
2078                            u_int8_t u3)
2079 {
2080 #ifdef CONFIG_NF_NAT_NEEDED
2081         struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2082         struct nf_conntrack_tuple nat_tuple = {};
2083         int err;
2084
2085         nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2086
2087         if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2088                 return -EINVAL;
2089
2090         err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2091                                         &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2092         if (err < 0)
2093                 return err;
2094
2095         exp->saved_ip = nat_tuple.src.u3.ip;
2096         exp->saved_proto = nat_tuple.src.u;
2097         exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2098
2099         return 0;
2100 #else
2101         return -EOPNOTSUPP;
2102 #endif
2103 }
2104
2105 static int
2106 ctnetlink_create_expect(struct net *net, u16 zone,
2107                         const struct nlattr * const cda[],
2108                         u_int8_t u3,
2109                         u32 pid, int report)
2110 {
2111         struct nf_conntrack_tuple tuple, mask, master_tuple;
2112         struct nf_conntrack_tuple_hash *h = NULL;
2113         struct nf_conntrack_expect *exp;
2114         struct nf_conn *ct;
2115         struct nf_conn_help *help;
2116         struct nf_conntrack_helper *helper = NULL;
2117         u_int32_t class = 0;
2118         int err = 0;
2119
2120         /* caller guarantees that those three CTA_EXPECT_* exist */
2121         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2122         if (err < 0)
2123                 return err;
2124         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2125         if (err < 0)
2126                 return err;
2127         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2128         if (err < 0)
2129                 return err;
2130
2131         /* Look for master conntrack of this expectation */
2132         h = nf_conntrack_find_get(net, zone, &master_tuple);
2133         if (!h)
2134                 return -ENOENT;
2135         ct = nf_ct_tuplehash_to_ctrack(h);
2136
2137         /* Look for helper of this expectation */
2138         if (cda[CTA_EXPECT_HELP_NAME]) {
2139                 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2140
2141                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2142                                                     nf_ct_protonum(ct));
2143                 if (helper == NULL) {
2144 #ifdef CONFIG_MODULES
2145                         if (request_module("nfct-helper-%s", helpname) < 0) {
2146                                 err = -EOPNOTSUPP;
2147                                 goto out;
2148                         }
2149
2150                         helper = __nf_conntrack_helper_find(helpname,
2151                                                             nf_ct_l3num(ct),
2152                                                             nf_ct_protonum(ct));
2153                         if (helper) {
2154                                 err = -EAGAIN;
2155                                 goto out;
2156                         }
2157 #endif
2158                         err = -EOPNOTSUPP;
2159                         goto out;
2160                 }
2161         }
2162
2163         if (cda[CTA_EXPECT_CLASS] && helper) {
2164                 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2165                 if (class > helper->expect_class_max) {
2166                         err = -EINVAL;
2167                         goto out;
2168                 }
2169         }
2170         exp = nf_ct_expect_alloc(ct);
2171         if (!exp) {
2172                 err = -ENOMEM;
2173                 goto out;
2174         }
2175         help = nfct_help(ct);
2176         if (!help) {
2177                 if (!cda[CTA_EXPECT_TIMEOUT]) {
2178                         err = -EINVAL;
2179                         goto out;
2180                 }
2181                 exp->timeout.expires =
2182                   jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2183
2184                 exp->flags = NF_CT_EXPECT_USERSPACE;
2185                 if (cda[CTA_EXPECT_FLAGS]) {
2186                         exp->flags |=
2187                                 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2188                 }
2189         } else {
2190                 if (cda[CTA_EXPECT_FLAGS]) {
2191                         exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2192                         exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2193                 } else
2194                         exp->flags = 0;
2195         }
2196         if (cda[CTA_EXPECT_FN]) {
2197                 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2198                 struct nf_ct_helper_expectfn *expfn;
2199
2200                 expfn = nf_ct_helper_expectfn_find_by_name(name);
2201                 if (expfn == NULL) {
2202                         err = -EINVAL;
2203                         goto err_out;
2204                 }
2205                 exp->expectfn = expfn->expectfn;
2206         } else
2207                 exp->expectfn = NULL;
2208
2209         exp->class = class;
2210         exp->master = ct;
2211         exp->helper = helper;
2212         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2213         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2214         exp->mask.src.u.all = mask.src.u.all;
2215
2216         if (cda[CTA_EXPECT_NAT]) {
2217                 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2218                                                  exp, u3);
2219                 if (err < 0)
2220                         goto err_out;
2221         }
2222         err = nf_ct_expect_related_report(exp, pid, report);
2223 err_out:
2224         nf_ct_expect_put(exp);
2225 out:
2226         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2227         return err;
2228 }
2229
2230 static int
2231 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2232                      const struct nlmsghdr *nlh,
2233                      const struct nlattr * const cda[])
2234 {
2235         struct net *net = sock_net(ctnl);
2236         struct nf_conntrack_tuple tuple;
2237         struct nf_conntrack_expect *exp;
2238         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2239         u_int8_t u3 = nfmsg->nfgen_family;
2240         u16 zone;
2241         int err;
2242
2243         if (!cda[CTA_EXPECT_TUPLE]
2244             || !cda[CTA_EXPECT_MASK]
2245             || !cda[CTA_EXPECT_MASTER])
2246                 return -EINVAL;
2247
2248         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2249         if (err < 0)
2250                 return err;
2251
2252         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2253         if (err < 0)
2254                 return err;
2255
2256         spin_lock_bh(&nf_conntrack_lock);
2257         exp = __nf_ct_expect_find(net, zone, &tuple);
2258
2259         if (!exp) {
2260                 spin_unlock_bh(&nf_conntrack_lock);
2261                 err = -ENOENT;
2262                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2263                         err = ctnetlink_create_expect(net, zone, cda,
2264                                                       u3,
2265                                                       NETLINK_CB(skb).pid,
2266                                                       nlmsg_report(nlh));
2267                 }
2268                 return err;
2269         }
2270
2271         err = -EEXIST;
2272         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2273                 err = ctnetlink_change_expect(exp, cda);
2274         spin_unlock_bh(&nf_conntrack_lock);
2275
2276         return err;
2277 }
2278
2279 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2280 static struct nf_ct_event_notifier ctnl_notifier = {
2281         .fcn = ctnetlink_conntrack_event,
2282 };
2283
2284 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2285         .fcn = ctnetlink_expect_event,
2286 };
2287 #endif
2288
2289 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2290         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
2291                                             .attr_count = CTA_MAX,
2292                                             .policy = ct_nla_policy },
2293         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
2294                                             .attr_count = CTA_MAX,
2295                                             .policy = ct_nla_policy },
2296         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
2297                                             .attr_count = CTA_MAX,
2298                                             .policy = ct_nla_policy },
2299         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
2300                                             .attr_count = CTA_MAX,
2301                                             .policy = ct_nla_policy },
2302 };
2303
2304 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2305         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
2306                                             .attr_count = CTA_EXPECT_MAX,
2307                                             .policy = exp_nla_policy },
2308         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
2309                                             .attr_count = CTA_EXPECT_MAX,
2310                                             .policy = exp_nla_policy },
2311         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
2312                                             .attr_count = CTA_EXPECT_MAX,
2313                                             .policy = exp_nla_policy },
2314 };
2315
2316 static const struct nfnetlink_subsystem ctnl_subsys = {
2317         .name                           = "conntrack",
2318         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
2319         .cb_count                       = IPCTNL_MSG_MAX,
2320         .cb                             = ctnl_cb,
2321 };
2322
2323 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2324         .name                           = "conntrack_expect",
2325         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
2326         .cb_count                       = IPCTNL_MSG_EXP_MAX,
2327         .cb                             = ctnl_exp_cb,
2328 };
2329
2330 MODULE_ALIAS("ip_conntrack_netlink");
2331 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2332 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2333
2334 static int __net_init ctnetlink_net_init(struct net *net)
2335 {
2336 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2337         int ret;
2338
2339         ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2340         if (ret < 0) {
2341                 pr_err("ctnetlink_init: cannot register notifier.\n");
2342                 goto err_out;
2343         }
2344
2345         ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2346         if (ret < 0) {
2347                 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2348                 goto err_unreg_notifier;
2349         }
2350 #endif
2351         return 0;
2352
2353 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2354 err_unreg_notifier:
2355         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2356 err_out:
2357         return ret;
2358 #endif
2359 }
2360
2361 static void ctnetlink_net_exit(struct net *net)
2362 {
2363 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2364         nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2365         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2366 #endif
2367 }
2368
2369 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2370 {
2371         struct net *net;
2372
2373         list_for_each_entry(net, net_exit_list, exit_list)
2374                 ctnetlink_net_exit(net);
2375 }
2376
2377 static struct pernet_operations ctnetlink_net_ops = {
2378         .init           = ctnetlink_net_init,
2379         .exit_batch     = ctnetlink_net_exit_batch,
2380 };
2381
2382 static int __init ctnetlink_init(void)
2383 {
2384         int ret;
2385
2386         pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2387         ret = nfnetlink_subsys_register(&ctnl_subsys);
2388         if (ret < 0) {
2389                 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2390                 goto err_out;
2391         }
2392
2393         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2394         if (ret < 0) {
2395                 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2396                 goto err_unreg_subsys;
2397         }
2398
2399         if (register_pernet_subsys(&ctnetlink_net_ops)) {
2400                 pr_err("ctnetlink_init: cannot register pernet operations\n");
2401                 goto err_unreg_exp_subsys;
2402         }
2403
2404         return 0;
2405
2406 err_unreg_exp_subsys:
2407         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2408 err_unreg_subsys:
2409         nfnetlink_subsys_unregister(&ctnl_subsys);
2410 err_out:
2411         return ret;
2412 }
2413
2414 static void __exit ctnetlink_exit(void)
2415 {
2416         pr_info("ctnetlink: unregistering from nfnetlink.\n");
2417
2418         unregister_pernet_subsys(&ctnetlink_net_ops);
2419         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2420         nfnetlink_subsys_unregister(&ctnl_subsys);
2421 }
2422
2423 module_init(ctnetlink_init);
2424 module_exit(ctnetlink_exit);