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