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 (del_timer(&ct->timeout)) {
971                 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
972                                               NETLINK_CB(skb).pid,
973                                               nlmsg_report(nlh)) < 0) {
974                         nf_ct_delete_from_lists(ct);
975                         /* we failed to report the event, try later */
976                         nf_ct_insert_dying_list(ct);
977                         nf_ct_put(ct);
978                         return 0;
979                 }
980                 /* death_by_timeout would report the event again */
981                 set_bit(IPS_DYING_BIT, &ct->status);
982                 nf_ct_delete_from_lists(ct);
983                 nf_ct_put(ct);
984         }
985         nf_ct_put(ct);
986
987         return 0;
988 }
989
990 static int
991 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
992                         const struct nlmsghdr *nlh,
993                         const struct nlattr * const cda[])
994 {
995         struct net *net = sock_net(ctnl);
996         struct nf_conntrack_tuple_hash *h;
997         struct nf_conntrack_tuple tuple;
998         struct nf_conn *ct;
999         struct sk_buff *skb2 = NULL;
1000         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1001         u_int8_t u3 = nfmsg->nfgen_family;
1002         u16 zone;
1003         int err;
1004
1005         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1006                 struct netlink_dump_control c = {
1007                         .dump = ctnetlink_dump_table,
1008                         .done = ctnetlink_done,
1009                 };
1010 #ifdef CONFIG_NF_CONNTRACK_MARK
1011                 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1012                         struct ctnetlink_dump_filter *filter;
1013
1014                         filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1015                                          GFP_ATOMIC);
1016                         if (filter == NULL)
1017                                 return -ENOMEM;
1018
1019                         filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1020                         filter->mark.mask =
1021                                 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1022                         c.data = filter;
1023                 }
1024 #endif
1025                 return netlink_dump_start(ctnl, skb, nlh, &c);
1026         }
1027
1028         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1029         if (err < 0)
1030                 return err;
1031
1032         if (cda[CTA_TUPLE_ORIG])
1033                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1034         else if (cda[CTA_TUPLE_REPLY])
1035                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1036         else
1037                 return -EINVAL;
1038
1039         if (err < 0)
1040                 return err;
1041
1042         h = nf_conntrack_find_get(net, zone, &tuple);
1043         if (!h)
1044                 return -ENOENT;
1045
1046         ct = nf_ct_tuplehash_to_ctrack(h);
1047
1048         err = -ENOMEM;
1049         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1050         if (skb2 == NULL) {
1051                 nf_ct_put(ct);
1052                 return -ENOMEM;
1053         }
1054
1055         rcu_read_lock();
1056         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
1057                                   NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1058         rcu_read_unlock();
1059         nf_ct_put(ct);
1060         if (err <= 0)
1061                 goto free;
1062
1063         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1064         if (err < 0)
1065                 goto out;
1066
1067         return 0;
1068
1069 free:
1070         kfree_skb(skb2);
1071 out:
1072         /* this avoids a loop in nfnetlink. */
1073         return err == -EAGAIN ? -ENOBUFS : err;
1074 }
1075
1076 #ifdef CONFIG_NF_NAT_NEEDED
1077 static int
1078 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1079                           enum nf_nat_manip_type manip,
1080                           const struct nlattr *attr)
1081 {
1082         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1083
1084         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1085         if (!parse_nat_setup) {
1086 #ifdef CONFIG_MODULES
1087                 rcu_read_unlock();
1088                 nfnl_unlock();
1089                 if (request_module("nf-nat-ipv4") < 0) {
1090                         nfnl_lock();
1091                         rcu_read_lock();
1092                         return -EOPNOTSUPP;
1093                 }
1094                 nfnl_lock();
1095                 rcu_read_lock();
1096                 if (nfnetlink_parse_nat_setup_hook)
1097                         return -EAGAIN;
1098 #endif
1099                 return -EOPNOTSUPP;
1100         }
1101
1102         return parse_nat_setup(ct, manip, attr);
1103 }
1104 #endif
1105
1106 static int
1107 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1108 {
1109         unsigned long d;
1110         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1111         d = ct->status ^ status;
1112
1113         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1114                 /* unchangeable */
1115                 return -EBUSY;
1116
1117         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1118                 /* SEEN_REPLY bit can only be set */
1119                 return -EBUSY;
1120
1121         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1122                 /* ASSURED bit can only be set */
1123                 return -EBUSY;
1124
1125         /* Be careful here, modifying NAT bits can screw up things,
1126          * so don't let users modify them directly if they don't pass
1127          * nf_nat_range. */
1128         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1129         return 0;
1130 }
1131
1132 static int
1133 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1134 {
1135 #ifdef CONFIG_NF_NAT_NEEDED
1136         int ret;
1137
1138         if (cda[CTA_NAT_DST]) {
1139                 ret = ctnetlink_parse_nat_setup(ct,
1140                                                 NF_NAT_MANIP_DST,
1141                                                 cda[CTA_NAT_DST]);
1142                 if (ret < 0)
1143                         return ret;
1144         }
1145         if (cda[CTA_NAT_SRC]) {
1146                 ret = ctnetlink_parse_nat_setup(ct,
1147                                                 NF_NAT_MANIP_SRC,
1148                                                 cda[CTA_NAT_SRC]);
1149                 if (ret < 0)
1150                         return ret;
1151         }
1152         return 0;
1153 #else
1154         return -EOPNOTSUPP;
1155 #endif
1156 }
1157
1158 static inline int
1159 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1160 {
1161         struct nf_conntrack_helper *helper;
1162         struct nf_conn_help *help = nfct_help(ct);
1163         char *helpname = NULL;
1164         int err;
1165
1166         /* don't change helper of sibling connections */
1167         if (ct->master)
1168                 return -EBUSY;
1169
1170         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1171         if (err < 0)
1172                 return err;
1173
1174         if (!strcmp(helpname, "")) {
1175                 if (help && help->helper) {
1176                         /* we had a helper before ... */
1177                         nf_ct_remove_expectations(ct);
1178                         RCU_INIT_POINTER(help->helper, NULL);
1179                 }
1180
1181                 return 0;
1182         }
1183
1184         helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1185                                             nf_ct_protonum(ct));
1186         if (helper == NULL) {
1187 #ifdef CONFIG_MODULES
1188                 spin_unlock_bh(&nf_conntrack_lock);
1189
1190                 if (request_module("nfct-helper-%s", helpname) < 0) {
1191                         spin_lock_bh(&nf_conntrack_lock);
1192                         return -EOPNOTSUPP;
1193                 }
1194
1195                 spin_lock_bh(&nf_conntrack_lock);
1196                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1197                                                     nf_ct_protonum(ct));
1198                 if (helper)
1199                         return -EAGAIN;
1200 #endif
1201                 return -EOPNOTSUPP;
1202         }
1203
1204         if (help) {
1205                 if (help->helper == helper)
1206                         return 0;
1207                 if (help->helper)
1208                         return -EBUSY;
1209                 /* need to zero data of old helper */
1210                 memset(&help->help, 0, sizeof(help->help));
1211         } else {
1212                 /* we cannot set a helper for an existing conntrack */
1213                 return -EOPNOTSUPP;
1214         }
1215
1216         rcu_assign_pointer(help->helper, helper);
1217
1218         return 0;
1219 }
1220
1221 static inline int
1222 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1223 {
1224         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1225
1226         if (!del_timer(&ct->timeout))
1227                 return -ETIME;
1228
1229         ct->timeout.expires = jiffies + timeout * HZ;
1230         add_timer(&ct->timeout);
1231
1232         return 0;
1233 }
1234
1235 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1236         [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
1237         [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
1238         [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
1239 };
1240
1241 static inline int
1242 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1243 {
1244         const struct nlattr *attr = cda[CTA_PROTOINFO];
1245         struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1246         struct nf_conntrack_l4proto *l4proto;
1247         int err = 0;
1248
1249         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1250
1251         rcu_read_lock();
1252         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1253         if (l4proto->from_nlattr)
1254                 err = l4proto->from_nlattr(tb, ct);
1255         rcu_read_unlock();
1256
1257         return err;
1258 }
1259
1260 #ifdef CONFIG_NF_NAT_NEEDED
1261 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1262         [CTA_NAT_SEQ_CORRECTION_POS]    = { .type = NLA_U32 },
1263         [CTA_NAT_SEQ_OFFSET_BEFORE]     = { .type = NLA_U32 },
1264         [CTA_NAT_SEQ_OFFSET_AFTER]      = { .type = NLA_U32 },
1265 };
1266
1267 static inline int
1268 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1269 {
1270         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1271
1272         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1273
1274         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1275                 return -EINVAL;
1276
1277         natseq->correction_pos =
1278                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1279
1280         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1281                 return -EINVAL;
1282
1283         natseq->offset_before =
1284                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1285
1286         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1287                 return -EINVAL;
1288
1289         natseq->offset_after =
1290                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1291
1292         return 0;
1293 }
1294
1295 static int
1296 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1297                              const struct nlattr * const cda[])
1298 {
1299         int ret = 0;
1300         struct nf_conn_nat *nat = nfct_nat(ct);
1301
1302         if (!nat)
1303                 return 0;
1304
1305         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1306                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1307                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1308                 if (ret < 0)
1309                         return ret;
1310
1311                 ct->status |= IPS_SEQ_ADJUST;
1312         }
1313
1314         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1315                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1316                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1317                 if (ret < 0)
1318                         return ret;
1319
1320                 ct->status |= IPS_SEQ_ADJUST;
1321         }
1322
1323         return 0;
1324 }
1325 #endif
1326
1327 static int
1328 ctnetlink_change_conntrack(struct nf_conn *ct,
1329                            const struct nlattr * const cda[])
1330 {
1331         int err;
1332
1333         /* only allow NAT changes and master assignation for new conntracks */
1334         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1335                 return -EOPNOTSUPP;
1336
1337         if (cda[CTA_HELP]) {
1338                 err = ctnetlink_change_helper(ct, cda);
1339                 if (err < 0)
1340                         return err;
1341         }
1342
1343         if (cda[CTA_TIMEOUT]) {
1344                 err = ctnetlink_change_timeout(ct, cda);
1345                 if (err < 0)
1346                         return err;
1347         }
1348
1349         if (cda[CTA_STATUS]) {
1350                 err = ctnetlink_change_status(ct, cda);
1351                 if (err < 0)
1352                         return err;
1353         }
1354
1355         if (cda[CTA_PROTOINFO]) {
1356                 err = ctnetlink_change_protoinfo(ct, cda);
1357                 if (err < 0)
1358                         return err;
1359         }
1360
1361 #if defined(CONFIG_NF_CONNTRACK_MARK)
1362         if (cda[CTA_MARK])
1363                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1364 #endif
1365
1366 #ifdef CONFIG_NF_NAT_NEEDED
1367         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1368                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1369                 if (err < 0)
1370                         return err;
1371         }
1372 #endif
1373
1374         return 0;
1375 }
1376
1377 static struct nf_conn *
1378 ctnetlink_create_conntrack(struct net *net, u16 zone,
1379                            const struct nlattr * const cda[],
1380                            struct nf_conntrack_tuple *otuple,
1381                            struct nf_conntrack_tuple *rtuple,
1382                            u8 u3)
1383 {
1384         struct nf_conn *ct;
1385         int err = -EINVAL;
1386         struct nf_conntrack_helper *helper;
1387         struct nf_conn_tstamp *tstamp;
1388
1389         ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1390         if (IS_ERR(ct))
1391                 return ERR_PTR(-ENOMEM);
1392
1393         if (!cda[CTA_TIMEOUT])
1394                 goto err1;
1395         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1396
1397         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1398
1399         rcu_read_lock();
1400         if (cda[CTA_HELP]) {
1401                 char *helpname = NULL;
1402  
1403                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1404                 if (err < 0)
1405                         goto err2;
1406
1407                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1408                                                     nf_ct_protonum(ct));
1409                 if (helper == NULL) {
1410                         rcu_read_unlock();
1411 #ifdef CONFIG_MODULES
1412                         if (request_module("nfct-helper-%s", helpname) < 0) {
1413                                 err = -EOPNOTSUPP;
1414                                 goto err1;
1415                         }
1416
1417                         rcu_read_lock();
1418                         helper = __nf_conntrack_helper_find(helpname,
1419                                                             nf_ct_l3num(ct),
1420                                                             nf_ct_protonum(ct));
1421                         if (helper) {
1422                                 err = -EAGAIN;
1423                                 goto err2;
1424                         }
1425                         rcu_read_unlock();
1426 #endif
1427                         err = -EOPNOTSUPP;
1428                         goto err1;
1429                 } else {
1430                         struct nf_conn_help *help;
1431
1432                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1433                         if (help == NULL) {
1434                                 err = -ENOMEM;
1435                                 goto err2;
1436                         }
1437
1438                         /* not in hash table yet so not strictly necessary */
1439                         RCU_INIT_POINTER(help->helper, helper);
1440                 }
1441         } else {
1442                 /* try an implicit helper assignation */
1443                 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1444                 if (err < 0)
1445                         goto err2;
1446         }
1447
1448         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1449                 err = ctnetlink_change_nat(ct, cda);
1450                 if (err < 0)
1451                         goto err2;
1452         }
1453
1454         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1455         nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1456         nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1457         /* we must add conntrack extensions before confirmation. */
1458         ct->status |= IPS_CONFIRMED;
1459
1460         if (cda[CTA_STATUS]) {
1461                 err = ctnetlink_change_status(ct, cda);
1462                 if (err < 0)
1463                         goto err2;
1464         }
1465
1466 #ifdef CONFIG_NF_NAT_NEEDED
1467         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1468                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1469                 if (err < 0)
1470                         goto err2;
1471         }
1472 #endif
1473
1474         memset(&ct->proto, 0, sizeof(ct->proto));
1475         if (cda[CTA_PROTOINFO]) {
1476                 err = ctnetlink_change_protoinfo(ct, cda);
1477                 if (err < 0)
1478                         goto err2;
1479         }
1480
1481 #if defined(CONFIG_NF_CONNTRACK_MARK)
1482         if (cda[CTA_MARK])
1483                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1484 #endif
1485
1486         /* setup master conntrack: this is a confirmed expectation */
1487         if (cda[CTA_TUPLE_MASTER]) {
1488                 struct nf_conntrack_tuple master;
1489                 struct nf_conntrack_tuple_hash *master_h;
1490                 struct nf_conn *master_ct;
1491
1492                 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1493                 if (err < 0)
1494                         goto err2;
1495
1496                 master_h = nf_conntrack_find_get(net, zone, &master);
1497                 if (master_h == NULL) {
1498                         err = -ENOENT;
1499                         goto err2;
1500                 }
1501                 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1502                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1503                 ct->master = master_ct;
1504         }
1505         tstamp = nf_conn_tstamp_find(ct);
1506         if (tstamp)
1507                 tstamp->start = ktime_to_ns(ktime_get_real());
1508
1509         err = nf_conntrack_hash_check_insert(ct);
1510         if (err < 0)
1511                 goto err2;
1512
1513         rcu_read_unlock();
1514
1515         return ct;
1516
1517 err2:
1518         rcu_read_unlock();
1519 err1:
1520         nf_conntrack_free(ct);
1521         return ERR_PTR(err);
1522 }
1523
1524 static int
1525 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1526                         const struct nlmsghdr *nlh,
1527                         const struct nlattr * const cda[])
1528 {
1529         struct net *net = sock_net(ctnl);
1530         struct nf_conntrack_tuple otuple, rtuple;
1531         struct nf_conntrack_tuple_hash *h = NULL;
1532         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1533         struct nf_conn *ct;
1534         u_int8_t u3 = nfmsg->nfgen_family;
1535         u16 zone;
1536         int err;
1537
1538         err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1539         if (err < 0)
1540                 return err;
1541
1542         if (cda[CTA_TUPLE_ORIG]) {
1543                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1544                 if (err < 0)
1545                         return err;
1546         }
1547
1548         if (cda[CTA_TUPLE_REPLY]) {
1549                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1550                 if (err < 0)
1551                         return err;
1552         }
1553
1554         if (cda[CTA_TUPLE_ORIG])
1555                 h = nf_conntrack_find_get(net, zone, &otuple);
1556         else if (cda[CTA_TUPLE_REPLY])
1557                 h = nf_conntrack_find_get(net, zone, &rtuple);
1558
1559         if (h == NULL) {
1560                 err = -ENOENT;
1561                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1562                         enum ip_conntrack_events events;
1563
1564                         ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1565                                                         &rtuple, u3);
1566                         if (IS_ERR(ct))
1567                                 return PTR_ERR(ct);
1568
1569                         err = 0;
1570                         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1571                                 events = IPCT_RELATED;
1572                         else
1573                                 events = IPCT_NEW;
1574
1575                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1576                                                       (1 << IPCT_ASSURED) |
1577                                                       (1 << IPCT_HELPER) |
1578                                                       (1 << IPCT_PROTOINFO) |
1579                                                       (1 << IPCT_NATSEQADJ) |
1580                                                       (1 << IPCT_MARK) | events,
1581                                                       ct, NETLINK_CB(skb).pid,
1582                                                       nlmsg_report(nlh));
1583                         nf_ct_put(ct);
1584                 }
1585
1586                 return err;
1587         }
1588         /* implicit 'else' */
1589
1590         err = -EEXIST;
1591         ct = nf_ct_tuplehash_to_ctrack(h);
1592         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1593                 spin_lock_bh(&nf_conntrack_lock);
1594                 err = ctnetlink_change_conntrack(ct, cda);
1595                 spin_unlock_bh(&nf_conntrack_lock);
1596                 if (err == 0) {
1597                         nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1598                                                       (1 << IPCT_ASSURED) |
1599                                                       (1 << IPCT_HELPER) |
1600                                                       (1 << IPCT_PROTOINFO) |
1601                                                       (1 << IPCT_NATSEQADJ) |
1602                                                       (1 << IPCT_MARK),
1603                                                       ct, NETLINK_CB(skb).pid,
1604                                                       nlmsg_report(nlh));
1605                 }
1606         }
1607
1608         nf_ct_put(ct);
1609         return err;
1610 }
1611
1612 /***********************************************************************
1613  * EXPECT
1614  ***********************************************************************/
1615
1616 static inline int
1617 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1618                          const struct nf_conntrack_tuple *tuple,
1619                          enum ctattr_expect type)
1620 {
1621         struct nlattr *nest_parms;
1622
1623         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1624         if (!nest_parms)
1625                 goto nla_put_failure;
1626         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1627                 goto nla_put_failure;
1628         nla_nest_end(skb, nest_parms);
1629
1630         return 0;
1631
1632 nla_put_failure:
1633         return -1;
1634 }
1635
1636 static inline int
1637 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1638                         const struct nf_conntrack_tuple *tuple,
1639                         const struct nf_conntrack_tuple_mask *mask)
1640 {
1641         int ret;
1642         struct nf_conntrack_l3proto *l3proto;
1643         struct nf_conntrack_l4proto *l4proto;
1644         struct nf_conntrack_tuple m;
1645         struct nlattr *nest_parms;
1646
1647         memset(&m, 0xFF, sizeof(m));
1648         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1649         m.src.u.all = mask->src.u.all;
1650         m.dst.protonum = tuple->dst.protonum;
1651
1652         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1653         if (!nest_parms)
1654                 goto nla_put_failure;
1655
1656         rcu_read_lock();
1657         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1658         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1659         if (ret >= 0) {
1660                 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
1661                                                tuple->dst.protonum);
1662         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1663         }
1664         rcu_read_unlock();
1665
1666         if (unlikely(ret < 0))
1667                 goto nla_put_failure;
1668
1669         nla_nest_end(skb, nest_parms);
1670
1671         return 0;
1672
1673 nla_put_failure:
1674         return -1;
1675 }
1676
1677 static int
1678 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1679                           const struct nf_conntrack_expect *exp)
1680 {
1681         struct nf_conn *master = exp->master;
1682         long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
1683         struct nf_conn_help *help;
1684 #ifdef CONFIG_NF_NAT_NEEDED
1685         struct nlattr *nest_parms;
1686         struct nf_conntrack_tuple nat_tuple = {};
1687 #endif
1688         struct nf_ct_helper_expectfn *expfn;
1689
1690         if (timeout < 0)
1691                 timeout = 0;
1692
1693         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1694                 goto nla_put_failure;
1695         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1696                 goto nla_put_failure;
1697         if (ctnetlink_exp_dump_tuple(skb,
1698                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1699                                  CTA_EXPECT_MASTER) < 0)
1700                 goto nla_put_failure;
1701
1702 #ifdef CONFIG_NF_NAT_NEEDED
1703         if (exp->saved_ip || exp->saved_proto.all) {
1704                 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
1705                 if (!nest_parms)
1706                         goto nla_put_failure;
1707
1708                 NLA_PUT_BE32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir));
1709
1710                 nat_tuple.src.l3num = nf_ct_l3num(master);
1711                 nat_tuple.src.u3.ip = exp->saved_ip;
1712                 nat_tuple.dst.protonum = nf_ct_protonum(master);
1713                 nat_tuple.src.u = exp->saved_proto;
1714
1715                 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
1716                                                 CTA_EXPECT_NAT_TUPLE) < 0)
1717                         goto nla_put_failure;
1718                 nla_nest_end(skb, nest_parms);
1719         }
1720 #endif
1721         NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1722         NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1723         NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1724         NLA_PUT_BE32(skb, CTA_EXPECT_CLASS, htonl(exp->class));
1725         help = nfct_help(master);
1726         if (help) {
1727                 struct nf_conntrack_helper *helper;
1728
1729                 helper = rcu_dereference(help->helper);
1730                 if (helper)
1731                         NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1732         }
1733         expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
1734         if (expfn != NULL)
1735                 NLA_PUT_STRING(skb, CTA_EXPECT_FN, expfn->name);
1736
1737         return 0;
1738
1739 nla_put_failure:
1740         return -1;
1741 }
1742
1743 static int
1744 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1745                         int event, const struct nf_conntrack_expect *exp)
1746 {
1747         struct nlmsghdr *nlh;
1748         struct nfgenmsg *nfmsg;
1749         unsigned int flags = pid ? NLM_F_MULTI : 0;
1750
1751         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1752         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1753         if (nlh == NULL)
1754                 goto nlmsg_failure;
1755
1756         nfmsg = nlmsg_data(nlh);
1757         nfmsg->nfgen_family = exp->tuple.src.l3num;
1758         nfmsg->version      = NFNETLINK_V0;
1759         nfmsg->res_id       = 0;
1760
1761         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1762                 goto nla_put_failure;
1763
1764         nlmsg_end(skb, nlh);
1765         return skb->len;
1766
1767 nlmsg_failure:
1768 nla_put_failure:
1769         nlmsg_cancel(skb, nlh);
1770         return -1;
1771 }
1772
1773 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1774 static int
1775 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1776 {
1777         struct nf_conntrack_expect *exp = item->exp;
1778         struct net *net = nf_ct_exp_net(exp);
1779         struct nlmsghdr *nlh;
1780         struct nfgenmsg *nfmsg;
1781         struct sk_buff *skb;
1782         unsigned int type, group;
1783         int flags = 0;
1784
1785         if (events & (1 << IPEXP_DESTROY)) {
1786                 type = IPCTNL_MSG_EXP_DELETE;
1787                 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1788         } else if (events & (1 << IPEXP_NEW)) {
1789                 type = IPCTNL_MSG_EXP_NEW;
1790                 flags = NLM_F_CREATE|NLM_F_EXCL;
1791                 group = NFNLGRP_CONNTRACK_EXP_NEW;
1792         } else
1793                 return 0;
1794
1795         if (!item->report && !nfnetlink_has_listeners(net, group))
1796                 return 0;
1797
1798         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1799         if (skb == NULL)
1800                 goto errout;
1801
1802         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1803         nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1804         if (nlh == NULL)
1805                 goto nlmsg_failure;
1806
1807         nfmsg = nlmsg_data(nlh);
1808         nfmsg->nfgen_family = exp->tuple.src.l3num;
1809         nfmsg->version      = NFNETLINK_V0;
1810         nfmsg->res_id       = 0;
1811
1812         rcu_read_lock();
1813         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1814                 goto nla_put_failure;
1815         rcu_read_unlock();
1816
1817         nlmsg_end(skb, nlh);
1818         nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1819         return 0;
1820
1821 nla_put_failure:
1822         rcu_read_unlock();
1823         nlmsg_cancel(skb, nlh);
1824 nlmsg_failure:
1825         kfree_skb(skb);
1826 errout:
1827         nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1828         return 0;
1829 }
1830 #endif
1831 static int ctnetlink_exp_done(struct netlink_callback *cb)
1832 {
1833         if (cb->args[1])
1834                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1835         return 0;
1836 }
1837
1838 static int
1839 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1840 {
1841         struct net *net = sock_net(skb->sk);
1842         struct nf_conntrack_expect *exp, *last;
1843         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1844         struct hlist_node *n;
1845         u_int8_t l3proto = nfmsg->nfgen_family;
1846
1847         rcu_read_lock();
1848         last = (struct nf_conntrack_expect *)cb->args[1];
1849         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1850 restart:
1851                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1852                                      hnode) {
1853                         if (l3proto && exp->tuple.src.l3num != l3proto)
1854                                 continue;
1855                         if (cb->args[1]) {
1856                                 if (exp != last)
1857                                         continue;
1858                                 cb->args[1] = 0;
1859                         }
1860                         if (ctnetlink_exp_fill_info(skb,
1861                                                     NETLINK_CB(cb->skb).pid,
1862                                                     cb->nlh->nlmsg_seq,
1863                                                     IPCTNL_MSG_EXP_NEW,
1864                                                     exp) < 0) {
1865                                 if (!atomic_inc_not_zero(&exp->use))
1866                                         continue;
1867                                 cb->args[1] = (unsigned long)exp;
1868                                 goto out;
1869                         }
1870                 }
1871                 if (cb->args[1]) {
1872                         cb->args[1] = 0;
1873                         goto restart;
1874                 }
1875         }
1876 out:
1877         rcu_read_unlock();
1878         if (last)
1879                 nf_ct_expect_put(last);
1880
1881         return skb->len;
1882 }
1883
1884 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1885         [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
1886         [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
1887         [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
1888         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1889         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1890         [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING },
1891         [CTA_EXPECT_ZONE]       = { .type = NLA_U16 },
1892         [CTA_EXPECT_FLAGS]      = { .type = NLA_U32 },
1893         [CTA_EXPECT_CLASS]      = { .type = NLA_U32 },
1894         [CTA_EXPECT_NAT]        = { .type = NLA_NESTED },
1895         [CTA_EXPECT_FN]         = { .type = NLA_NUL_STRING },
1896 };
1897
1898 static int
1899 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1900                      const struct nlmsghdr *nlh,
1901                      const struct nlattr * const cda[])
1902 {
1903         struct net *net = sock_net(ctnl);
1904         struct nf_conntrack_tuple tuple;
1905         struct nf_conntrack_expect *exp;
1906         struct sk_buff *skb2;
1907         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1908         u_int8_t u3 = nfmsg->nfgen_family;
1909         u16 zone;
1910         int err;
1911
1912         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1913                 struct netlink_dump_control c = {
1914                         .dump = ctnetlink_exp_dump_table,
1915                         .done = ctnetlink_exp_done,
1916                 };
1917                 return netlink_dump_start(ctnl, skb, nlh, &c);
1918         }
1919
1920         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1921         if (err < 0)
1922                 return err;
1923
1924         if (cda[CTA_EXPECT_TUPLE])
1925                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1926         else if (cda[CTA_EXPECT_MASTER])
1927                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1928         else
1929                 return -EINVAL;
1930
1931         if (err < 0)
1932                 return err;
1933
1934         exp = nf_ct_expect_find_get(net, zone, &tuple);
1935         if (!exp)
1936                 return -ENOENT;
1937
1938         if (cda[CTA_EXPECT_ID]) {
1939                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1940                 if (ntohl(id) != (u32)(unsigned long)exp) {
1941                         nf_ct_expect_put(exp);
1942                         return -ENOENT;
1943                 }
1944         }
1945
1946         err = -ENOMEM;
1947         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1948         if (skb2 == NULL) {
1949                 nf_ct_expect_put(exp);
1950                 goto out;
1951         }
1952
1953         rcu_read_lock();
1954         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1955                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1956         rcu_read_unlock();
1957         nf_ct_expect_put(exp);
1958         if (err <= 0)
1959                 goto free;
1960
1961         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1962         if (err < 0)
1963                 goto out;
1964
1965         return 0;
1966
1967 free:
1968         kfree_skb(skb2);
1969 out:
1970         /* this avoids a loop in nfnetlink. */
1971         return err == -EAGAIN ? -ENOBUFS : err;
1972 }
1973
1974 static int
1975 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1976                      const struct nlmsghdr *nlh,
1977                      const struct nlattr * const cda[])
1978 {
1979         struct net *net = sock_net(ctnl);
1980         struct nf_conntrack_expect *exp;
1981         struct nf_conntrack_tuple tuple;
1982         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1983         struct hlist_node *n, *next;
1984         u_int8_t u3 = nfmsg->nfgen_family;
1985         unsigned int i;
1986         u16 zone;
1987         int err;
1988
1989         if (cda[CTA_EXPECT_TUPLE]) {
1990                 /* delete a single expect by tuple */
1991                 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1992                 if (err < 0)
1993                         return err;
1994
1995                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1996                 if (err < 0)
1997                         return err;
1998
1999                 /* bump usage count to 2 */
2000                 exp = nf_ct_expect_find_get(net, zone, &tuple);
2001                 if (!exp)
2002                         return -ENOENT;
2003
2004                 if (cda[CTA_EXPECT_ID]) {
2005                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2006                         if (ntohl(id) != (u32)(unsigned long)exp) {
2007                                 nf_ct_expect_put(exp);
2008                                 return -ENOENT;
2009                         }
2010                 }
2011
2012                 /* after list removal, usage count == 1 */
2013                 spin_lock_bh(&nf_conntrack_lock);
2014                 if (del_timer(&exp->timeout)) {
2015                         nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
2016                                                    nlmsg_report(nlh));
2017                         nf_ct_expect_put(exp);
2018                 }
2019                 spin_unlock_bh(&nf_conntrack_lock);
2020                 /* have to put what we 'get' above.
2021                  * after this line usage count == 0 */
2022                 nf_ct_expect_put(exp);
2023         } else if (cda[CTA_EXPECT_HELP_NAME]) {
2024                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2025                 struct nf_conn_help *m_help;
2026
2027                 /* delete all expectations for this helper */
2028                 spin_lock_bh(&nf_conntrack_lock);
2029                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2030                         hlist_for_each_entry_safe(exp, n, next,
2031                                                   &net->ct.expect_hash[i],
2032                                                   hnode) {
2033                                 m_help = nfct_help(exp->master);
2034                                 if (!strcmp(m_help->helper->name, name) &&
2035                                     del_timer(&exp->timeout)) {
2036                                         nf_ct_unlink_expect_report(exp,
2037                                                         NETLINK_CB(skb).pid,
2038                                                         nlmsg_report(nlh));
2039                                         nf_ct_expect_put(exp);
2040                                 }
2041                         }
2042                 }
2043                 spin_unlock_bh(&nf_conntrack_lock);
2044         } else {
2045                 /* This basically means we have to flush everything*/
2046                 spin_lock_bh(&nf_conntrack_lock);
2047                 for (i = 0; i < nf_ct_expect_hsize; i++) {
2048                         hlist_for_each_entry_safe(exp, n, next,
2049                                                   &net->ct.expect_hash[i],
2050                                                   hnode) {
2051                                 if (del_timer(&exp->timeout)) {
2052                                         nf_ct_unlink_expect_report(exp,
2053                                                         NETLINK_CB(skb).pid,
2054                                                         nlmsg_report(nlh));
2055                                         nf_ct_expect_put(exp);
2056                                 }
2057                         }
2058                 }
2059                 spin_unlock_bh(&nf_conntrack_lock);
2060         }
2061
2062         return 0;
2063 }
2064 static int
2065 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2066                         const struct nlattr * const cda[])
2067 {
2068         return -EOPNOTSUPP;
2069 }
2070
2071 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2072         [CTA_EXPECT_NAT_DIR]    = { .type = NLA_U32 },
2073         [CTA_EXPECT_NAT_TUPLE]  = { .type = NLA_NESTED },
2074 };
2075
2076 static int
2077 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2078                            struct nf_conntrack_expect *exp,
2079                            u_int8_t u3)
2080 {
2081 #ifdef CONFIG_NF_NAT_NEEDED
2082         struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2083         struct nf_conntrack_tuple nat_tuple = {};
2084         int err;
2085
2086         nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2087
2088         if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2089                 return -EINVAL;
2090
2091         err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2092                                         &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2093         if (err < 0)
2094                 return err;
2095
2096         exp->saved_ip = nat_tuple.src.u3.ip;
2097         exp->saved_proto = nat_tuple.src.u;
2098         exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2099
2100         return 0;
2101 #else
2102         return -EOPNOTSUPP;
2103 #endif
2104 }
2105
2106 static int
2107 ctnetlink_create_expect(struct net *net, u16 zone,
2108                         const struct nlattr * const cda[],
2109                         u_int8_t u3,
2110                         u32 pid, int report)
2111 {
2112         struct nf_conntrack_tuple tuple, mask, master_tuple;
2113         struct nf_conntrack_tuple_hash *h = NULL;
2114         struct nf_conntrack_expect *exp;
2115         struct nf_conn *ct;
2116         struct nf_conn_help *help;
2117         struct nf_conntrack_helper *helper = NULL;
2118         u_int32_t class = 0;
2119         int err = 0;
2120
2121         /* caller guarantees that those three CTA_EXPECT_* exist */
2122         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2123         if (err < 0)
2124                 return err;
2125         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2126         if (err < 0)
2127                 return err;
2128         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2129         if (err < 0)
2130                 return err;
2131
2132         /* Look for master conntrack of this expectation */
2133         h = nf_conntrack_find_get(net, zone, &master_tuple);
2134         if (!h)
2135                 return -ENOENT;
2136         ct = nf_ct_tuplehash_to_ctrack(h);
2137
2138         /* Look for helper of this expectation */
2139         if (cda[CTA_EXPECT_HELP_NAME]) {
2140                 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2141
2142                 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2143                                                     nf_ct_protonum(ct));
2144                 if (helper == NULL) {
2145 #ifdef CONFIG_MODULES
2146                         if (request_module("nfct-helper-%s", helpname) < 0) {
2147                                 err = -EOPNOTSUPP;
2148                                 goto out;
2149                         }
2150
2151                         helper = __nf_conntrack_helper_find(helpname,
2152                                                             nf_ct_l3num(ct),
2153                                                             nf_ct_protonum(ct));
2154                         if (helper) {
2155                                 err = -EAGAIN;
2156                                 goto out;
2157                         }
2158 #endif
2159                         err = -EOPNOTSUPP;
2160                         goto out;
2161                 }
2162         }
2163
2164         if (cda[CTA_EXPECT_CLASS] && helper) {
2165                 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2166                 if (class > helper->expect_class_max) {
2167                         err = -EINVAL;
2168                         goto out;
2169                 }
2170         }
2171         exp = nf_ct_expect_alloc(ct);
2172         if (!exp) {
2173                 err = -ENOMEM;
2174                 goto out;
2175         }
2176         help = nfct_help(ct);
2177         if (!help) {
2178                 if (!cda[CTA_EXPECT_TIMEOUT]) {
2179                         err = -EINVAL;
2180                         goto out;
2181                 }
2182                 exp->timeout.expires =
2183                   jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2184
2185                 exp->flags = NF_CT_EXPECT_USERSPACE;
2186                 if (cda[CTA_EXPECT_FLAGS]) {
2187                         exp->flags |=
2188                                 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2189                 }
2190         } else {
2191                 if (cda[CTA_EXPECT_FLAGS]) {
2192                         exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2193                         exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2194                 } else
2195                         exp->flags = 0;
2196         }
2197         if (cda[CTA_EXPECT_FN]) {
2198                 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2199                 struct nf_ct_helper_expectfn *expfn;
2200
2201                 expfn = nf_ct_helper_expectfn_find_by_name(name);
2202                 if (expfn == NULL) {
2203                         err = -EINVAL;
2204                         goto err_out;
2205                 }
2206                 exp->expectfn = expfn->expectfn;
2207         } else
2208                 exp->expectfn = NULL;
2209
2210         exp->class = class;
2211         exp->master = ct;
2212         exp->helper = helper;
2213         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2214         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2215         exp->mask.src.u.all = mask.src.u.all;
2216
2217         if (cda[CTA_EXPECT_NAT]) {
2218                 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2219                                                  exp, u3);
2220                 if (err < 0)
2221                         goto err_out;
2222         }
2223         err = nf_ct_expect_related_report(exp, pid, report);
2224 err_out:
2225         nf_ct_expect_put(exp);
2226 out:
2227         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2228         return err;
2229 }
2230
2231 static int
2232 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2233                      const struct nlmsghdr *nlh,
2234                      const struct nlattr * const cda[])
2235 {
2236         struct net *net = sock_net(ctnl);
2237         struct nf_conntrack_tuple tuple;
2238         struct nf_conntrack_expect *exp;
2239         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2240         u_int8_t u3 = nfmsg->nfgen_family;
2241         u16 zone;
2242         int err;
2243
2244         if (!cda[CTA_EXPECT_TUPLE]
2245             || !cda[CTA_EXPECT_MASK]
2246             || !cda[CTA_EXPECT_MASTER])
2247                 return -EINVAL;
2248
2249         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2250         if (err < 0)
2251                 return err;
2252
2253         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2254         if (err < 0)
2255                 return err;
2256
2257         spin_lock_bh(&nf_conntrack_lock);
2258         exp = __nf_ct_expect_find(net, zone, &tuple);
2259
2260         if (!exp) {
2261                 spin_unlock_bh(&nf_conntrack_lock);
2262                 err = -ENOENT;
2263                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2264                         err = ctnetlink_create_expect(net, zone, cda,
2265                                                       u3,
2266                                                       NETLINK_CB(skb).pid,
2267                                                       nlmsg_report(nlh));
2268                 }
2269                 return err;
2270         }
2271
2272         err = -EEXIST;
2273         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2274                 err = ctnetlink_change_expect(exp, cda);
2275         spin_unlock_bh(&nf_conntrack_lock);
2276
2277         return err;
2278 }
2279
2280 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2281 static struct nf_ct_event_notifier ctnl_notifier = {
2282         .fcn = ctnetlink_conntrack_event,
2283 };
2284
2285 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2286         .fcn = ctnetlink_expect_event,
2287 };
2288 #endif
2289
2290 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2291         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
2292                                             .attr_count = CTA_MAX,
2293                                             .policy = ct_nla_policy },
2294         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
2295                                             .attr_count = CTA_MAX,
2296                                             .policy = ct_nla_policy },
2297         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
2298                                             .attr_count = CTA_MAX,
2299                                             .policy = ct_nla_policy },
2300         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
2301                                             .attr_count = CTA_MAX,
2302                                             .policy = ct_nla_policy },
2303 };
2304
2305 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2306         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
2307                                             .attr_count = CTA_EXPECT_MAX,
2308                                             .policy = exp_nla_policy },
2309         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
2310                                             .attr_count = CTA_EXPECT_MAX,
2311                                             .policy = exp_nla_policy },
2312         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
2313                                             .attr_count = CTA_EXPECT_MAX,
2314                                             .policy = exp_nla_policy },
2315 };
2316
2317 static const struct nfnetlink_subsystem ctnl_subsys = {
2318         .name                           = "conntrack",
2319         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
2320         .cb_count                       = IPCTNL_MSG_MAX,
2321         .cb                             = ctnl_cb,
2322 };
2323
2324 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2325         .name                           = "conntrack_expect",
2326         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
2327         .cb_count                       = IPCTNL_MSG_EXP_MAX,
2328         .cb                             = ctnl_exp_cb,
2329 };
2330
2331 MODULE_ALIAS("ip_conntrack_netlink");
2332 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2333 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2334
2335 static int __net_init ctnetlink_net_init(struct net *net)
2336 {
2337 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2338         int ret;
2339
2340         ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2341         if (ret < 0) {
2342                 pr_err("ctnetlink_init: cannot register notifier.\n");
2343                 goto err_out;
2344         }
2345
2346         ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2347         if (ret < 0) {
2348                 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2349                 goto err_unreg_notifier;
2350         }
2351 #endif
2352         return 0;
2353
2354 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2355 err_unreg_notifier:
2356         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2357 err_out:
2358         return ret;
2359 #endif
2360 }
2361
2362 static void ctnetlink_net_exit(struct net *net)
2363 {
2364 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2365         nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2366         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2367 #endif
2368 }
2369
2370 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2371 {
2372         struct net *net;
2373
2374         list_for_each_entry(net, net_exit_list, exit_list)
2375                 ctnetlink_net_exit(net);
2376 }
2377
2378 static struct pernet_operations ctnetlink_net_ops = {
2379         .init           = ctnetlink_net_init,
2380         .exit_batch     = ctnetlink_net_exit_batch,
2381 };
2382
2383 static int __init ctnetlink_init(void)
2384 {
2385         int ret;
2386
2387         pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2388         ret = nfnetlink_subsys_register(&ctnl_subsys);
2389         if (ret < 0) {
2390                 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2391                 goto err_out;
2392         }
2393
2394         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2395         if (ret < 0) {
2396                 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2397                 goto err_unreg_subsys;
2398         }
2399
2400         if (register_pernet_subsys(&ctnetlink_net_ops)) {
2401                 pr_err("ctnetlink_init: cannot register pernet operations\n");
2402                 goto err_unreg_exp_subsys;
2403         }
2404
2405         return 0;
2406
2407 err_unreg_exp_subsys:
2408         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2409 err_unreg_subsys:
2410         nfnetlink_subsys_unregister(&ctnl_subsys);
2411 err_out:
2412         return ret;
2413 }
2414
2415 static void __exit ctnetlink_exit(void)
2416 {
2417         pr_info("ctnetlink: unregistering from nfnetlink.\n");
2418
2419         unregister_pernet_subsys(&ctnetlink_net_ops);
2420         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2421         nfnetlink_subsys_unregister(&ctnl_subsys);
2422 }
2423
2424 module_init(ctnetlink_init);
2425 module_exit(ctnetlink_exit);