netfilter: ctnetlink: helper modules load-on-demand support
[safe/jmp/linux-2.6] / 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/types.h>
23 #include <linux/timer.h>
24 #include <linux/skbuff.h>
25 #include <linux/errno.h>
26 #include <linux/netlink.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/notifier.h>
30
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
44 #endif
45
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
48
49 MODULE_LICENSE("GPL");
50
51 static char __initdata version[] = "0.93";
52
53 static inline int
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55                             const struct nf_conntrack_tuple *tuple,
56                             struct nf_conntrack_l4proto *l4proto)
57 {
58         int ret = 0;
59         struct nlattr *nest_parms;
60
61         nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
62         if (!nest_parms)
63                 goto nla_put_failure;
64         NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
65
66         if (likely(l4proto->tuple_to_nlattr))
67                 ret = l4proto->tuple_to_nlattr(skb, tuple);
68
69         nla_nest_end(skb, nest_parms);
70
71         return ret;
72
73 nla_put_failure:
74         return -1;
75 }
76
77 static inline int
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79                          const struct nf_conntrack_tuple *tuple,
80                          struct nf_conntrack_l3proto *l3proto)
81 {
82         int ret = 0;
83         struct nlattr *nest_parms;
84
85         nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
86         if (!nest_parms)
87                 goto nla_put_failure;
88
89         if (likely(l3proto->tuple_to_nlattr))
90                 ret = l3proto->tuple_to_nlattr(skb, tuple);
91
92         nla_nest_end(skb, nest_parms);
93
94         return ret;
95
96 nla_put_failure:
97         return -1;
98 }
99
100 static int
101 ctnetlink_dump_tuples(struct sk_buff *skb,
102                       const struct nf_conntrack_tuple *tuple)
103 {
104         int ret;
105         struct nf_conntrack_l3proto *l3proto;
106         struct nf_conntrack_l4proto *l4proto;
107
108         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
109         ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
110
111         if (unlikely(ret < 0))
112                 return ret;
113
114         l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
115         ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
116
117         return ret;
118 }
119
120 static inline int
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
122 {
123         NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
124         return 0;
125
126 nla_put_failure:
127         return -1;
128 }
129
130 static inline int
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
132 {
133         long timeout = (ct->timeout.expires - jiffies) / HZ;
134
135         if (timeout < 0)
136                 timeout = 0;
137
138         NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
139         return 0;
140
141 nla_put_failure:
142         return -1;
143 }
144
145 static inline int
146 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
147 {
148         struct nf_conntrack_l4proto *l4proto;
149         struct nlattr *nest_proto;
150         int ret;
151
152         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153         if (!l4proto->to_nlattr)
154                 return 0;
155
156         nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
157         if (!nest_proto)
158                 goto nla_put_failure;
159
160         ret = l4proto->to_nlattr(skb, nest_proto, ct);
161
162         nla_nest_end(skb, nest_proto);
163
164         return ret;
165
166 nla_put_failure:
167         return -1;
168 }
169
170 static inline int
171 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
172 {
173         struct nlattr *nest_helper;
174         const struct nf_conn_help *help = nfct_help(ct);
175         struct nf_conntrack_helper *helper;
176
177         if (!help)
178                 return 0;
179
180         helper = rcu_dereference(help->helper);
181         if (!helper)
182                 goto out;
183
184         nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
185         if (!nest_helper)
186                 goto nla_put_failure;
187         NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
188
189         if (helper->to_nlattr)
190                 helper->to_nlattr(skb, ct);
191
192         nla_nest_end(skb, nest_helper);
193 out:
194         return 0;
195
196 nla_put_failure:
197         return -1;
198 }
199
200 static int
201 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202                         enum ip_conntrack_dir dir)
203 {
204         enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
205         struct nlattr *nest_count;
206         const struct nf_conn_counter *acct;
207
208         acct = nf_conn_acct_find(ct);
209         if (!acct)
210                 return 0;
211
212         nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
213         if (!nest_count)
214                 goto nla_put_failure;
215
216         NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217                      cpu_to_be64(acct[dir].packets));
218         NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219                      cpu_to_be64(acct[dir].bytes));
220
221         nla_nest_end(skb, nest_count);
222
223         return 0;
224
225 nla_put_failure:
226         return -1;
227 }
228
229 #ifdef CONFIG_NF_CONNTRACK_MARK
230 static inline int
231 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
232 {
233         NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
234         return 0;
235
236 nla_put_failure:
237         return -1;
238 }
239 #else
240 #define ctnetlink_dump_mark(a, b) (0)
241 #endif
242
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
244 static inline int
245 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
246 {
247         NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
248         return 0;
249
250 nla_put_failure:
251         return -1;
252 }
253 #else
254 #define ctnetlink_dump_secmark(a, b) (0)
255 #endif
256
257 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
258
259 static inline int
260 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
261 {
262         struct nlattr *nest_parms;
263
264         if (!(ct->status & IPS_EXPECTED))
265                 return 0;
266
267         nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
268         if (!nest_parms)
269                 goto nla_put_failure;
270         if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271                 goto nla_put_failure;
272         nla_nest_end(skb, nest_parms);
273
274         return 0;
275
276 nla_put_failure:
277         return -1;
278 }
279
280 #ifdef CONFIG_NF_NAT_NEEDED
281 static int
282 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
283 {
284         struct nlattr *nest_parms;
285
286         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
287         if (!nest_parms)
288                 goto nla_put_failure;
289
290         NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291                      htonl(natseq->correction_pos));
292         NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293                      htonl(natseq->offset_before));
294         NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295                      htonl(natseq->offset_after));
296
297         nla_nest_end(skb, nest_parms);
298
299         return 0;
300
301 nla_put_failure:
302         return -1;
303 }
304
305 static inline int
306 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
307 {
308         struct nf_nat_seq *natseq;
309         struct nf_conn_nat *nat = nfct_nat(ct);
310
311         if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
312                 return 0;
313
314         natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315         if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
316                 return -1;
317
318         natseq = &nat->seq[IP_CT_DIR_REPLY];
319         if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
320                 return -1;
321
322         return 0;
323 }
324 #else
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
326 #endif
327
328 static inline int
329 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
330 {
331         NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
332         return 0;
333
334 nla_put_failure:
335         return -1;
336 }
337
338 static inline int
339 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
340 {
341         NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
342         return 0;
343
344 nla_put_failure:
345         return -1;
346 }
347
348 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
349
350 static int
351 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
352                     int event, int nowait,
353                     const struct nf_conn *ct)
354 {
355         struct nlmsghdr *nlh;
356         struct nfgenmsg *nfmsg;
357         struct nlattr *nest_parms;
358         unsigned char *b = skb_tail_pointer(skb);
359
360         event |= NFNL_SUBSYS_CTNETLINK << 8;
361         nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
362         nfmsg  = NLMSG_DATA(nlh);
363
364         nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
365         nfmsg->nfgen_family = nf_ct_l3num(ct);
366         nfmsg->version      = NFNETLINK_V0;
367         nfmsg->res_id       = 0;
368
369         nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
370         if (!nest_parms)
371                 goto nla_put_failure;
372         if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
373                 goto nla_put_failure;
374         nla_nest_end(skb, nest_parms);
375
376         nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
377         if (!nest_parms)
378                 goto nla_put_failure;
379         if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
380                 goto nla_put_failure;
381         nla_nest_end(skb, nest_parms);
382
383         if (ctnetlink_dump_status(skb, ct) < 0 ||
384             ctnetlink_dump_timeout(skb, ct) < 0 ||
385             ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
386             ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
387             ctnetlink_dump_protoinfo(skb, ct) < 0 ||
388             ctnetlink_dump_helpinfo(skb, ct) < 0 ||
389             ctnetlink_dump_mark(skb, ct) < 0 ||
390             ctnetlink_dump_secmark(skb, ct) < 0 ||
391             ctnetlink_dump_id(skb, ct) < 0 ||
392             ctnetlink_dump_use(skb, ct) < 0 ||
393             ctnetlink_dump_master(skb, ct) < 0 ||
394             ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
395                 goto nla_put_failure;
396
397         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
398         return skb->len;
399
400 nlmsg_failure:
401 nla_put_failure:
402         nlmsg_trim(skb, b);
403         return -1;
404 }
405
406 #ifdef CONFIG_NF_CONNTRACK_EVENTS
407 static int ctnetlink_conntrack_event(struct notifier_block *this,
408                                      unsigned long events, void *ptr)
409 {
410         struct nlmsghdr *nlh;
411         struct nfgenmsg *nfmsg;
412         struct nlattr *nest_parms;
413         struct nf_conn *ct = (struct nf_conn *)ptr;
414         struct sk_buff *skb;
415         unsigned int type;
416         sk_buff_data_t b;
417         unsigned int flags = 0, group;
418
419         /* ignore our fake conntrack entry */
420         if (ct == &nf_conntrack_untracked)
421                 return NOTIFY_DONE;
422
423         if (events & IPCT_DESTROY) {
424                 type = IPCTNL_MSG_CT_DELETE;
425                 group = NFNLGRP_CONNTRACK_DESTROY;
426         } else  if (events & (IPCT_NEW | IPCT_RELATED)) {
427                 type = IPCTNL_MSG_CT_NEW;
428                 flags = NLM_F_CREATE|NLM_F_EXCL;
429                 group = NFNLGRP_CONNTRACK_NEW;
430         } else  if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
431                 type = IPCTNL_MSG_CT_NEW;
432                 group = NFNLGRP_CONNTRACK_UPDATE;
433         } else
434                 return NOTIFY_DONE;
435
436         if (!nfnetlink_has_listeners(group))
437                 return NOTIFY_DONE;
438
439         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
440         if (!skb)
441                 return NOTIFY_DONE;
442
443         b = skb->tail;
444
445         type |= NFNL_SUBSYS_CTNETLINK << 8;
446         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
447         nfmsg = NLMSG_DATA(nlh);
448
449         nlh->nlmsg_flags    = flags;
450         nfmsg->nfgen_family = nf_ct_l3num(ct);
451         nfmsg->version  = NFNETLINK_V0;
452         nfmsg->res_id   = 0;
453
454         rcu_read_lock();
455         nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
456         if (!nest_parms)
457                 goto nla_put_failure;
458         if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
459                 goto nla_put_failure;
460         nla_nest_end(skb, nest_parms);
461
462         nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
463         if (!nest_parms)
464                 goto nla_put_failure;
465         if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
466                 goto nla_put_failure;
467         nla_nest_end(skb, nest_parms);
468
469         if (ctnetlink_dump_id(skb, ct) < 0)
470                 goto nla_put_failure;
471
472         if (ctnetlink_dump_status(skb, ct) < 0)
473                 goto nla_put_failure;
474
475         if (events & IPCT_DESTROY) {
476                 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
477                     ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
478                         goto nla_put_failure;
479         } else {
480                 if (ctnetlink_dump_timeout(skb, ct) < 0)
481                         goto nla_put_failure;
482
483                 if (events & IPCT_PROTOINFO
484                     && ctnetlink_dump_protoinfo(skb, ct) < 0)
485                         goto nla_put_failure;
486
487                 if ((events & IPCT_HELPER || nfct_help(ct))
488                     && ctnetlink_dump_helpinfo(skb, ct) < 0)
489                         goto nla_put_failure;
490
491 #ifdef CONFIG_NF_CONNTRACK_SECMARK
492                 if ((events & IPCT_SECMARK || ct->secmark)
493                     && ctnetlink_dump_secmark(skb, ct) < 0)
494                         goto nla_put_failure;
495 #endif
496
497                 if (events & IPCT_RELATED &&
498                     ctnetlink_dump_master(skb, ct) < 0)
499                         goto nla_put_failure;
500
501                 if (events & IPCT_NATSEQADJ &&
502                     ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
503                         goto nla_put_failure;
504         }
505
506 #ifdef CONFIG_NF_CONNTRACK_MARK
507         if ((events & IPCT_MARK || ct->mark)
508             && ctnetlink_dump_mark(skb, ct) < 0)
509                 goto nla_put_failure;
510 #endif
511         rcu_read_unlock();
512
513         nlh->nlmsg_len = skb->tail - b;
514         nfnetlink_send(skb, 0, group, 0);
515         return NOTIFY_DONE;
516
517 nla_put_failure:
518         rcu_read_unlock();
519 nlmsg_failure:
520         kfree_skb(skb);
521         return NOTIFY_DONE;
522 }
523 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
524
525 static int ctnetlink_done(struct netlink_callback *cb)
526 {
527         if (cb->args[1])
528                 nf_ct_put((struct nf_conn *)cb->args[1]);
529         return 0;
530 }
531
532 static int
533 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
534 {
535         struct nf_conn *ct, *last;
536         struct nf_conntrack_tuple_hash *h;
537         struct hlist_node *n;
538         struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
539         u_int8_t l3proto = nfmsg->nfgen_family;
540
541         rcu_read_lock();
542         last = (struct nf_conn *)cb->args[1];
543         for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
544 restart:
545                 hlist_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
546                                          hnode) {
547                         if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
548                                 continue;
549                         ct = nf_ct_tuplehash_to_ctrack(h);
550                         /* Dump entries of a given L3 protocol number.
551                          * If it is not specified, ie. l3proto == 0,
552                          * then dump everything. */
553                         if (l3proto && nf_ct_l3num(ct) != l3proto)
554                                 continue;
555                         if (cb->args[1]) {
556                                 if (ct != last)
557                                         continue;
558                                 cb->args[1] = 0;
559                         }
560                         if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
561                                                 cb->nlh->nlmsg_seq,
562                                                 IPCTNL_MSG_CT_NEW,
563                                                 1, ct) < 0) {
564                                 if (!atomic_inc_not_zero(&ct->ct_general.use))
565                                         continue;
566                                 cb->args[1] = (unsigned long)ct;
567                                 goto out;
568                         }
569
570                         if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
571                                                 IPCTNL_MSG_CT_GET_CTRZERO) {
572                                 struct nf_conn_counter *acct;
573
574                                 acct = nf_conn_acct_find(ct);
575                                 if (acct)
576                                         memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
577                         }
578                 }
579                 if (cb->args[1]) {
580                         cb->args[1] = 0;
581                         goto restart;
582                 }
583         }
584 out:
585         rcu_read_unlock();
586         if (last)
587                 nf_ct_put(last);
588
589         return skb->len;
590 }
591
592 static inline int
593 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
594 {
595         struct nlattr *tb[CTA_IP_MAX+1];
596         struct nf_conntrack_l3proto *l3proto;
597         int ret = 0;
598
599         nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
600
601         l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
602
603         if (likely(l3proto->nlattr_to_tuple)) {
604                 ret = nla_validate_nested(attr, CTA_IP_MAX,
605                                           l3proto->nla_policy);
606                 if (ret == 0)
607                         ret = l3proto->nlattr_to_tuple(tb, tuple);
608         }
609
610         nf_ct_l3proto_put(l3proto);
611
612         return ret;
613 }
614
615 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
616         [CTA_PROTO_NUM] = { .type = NLA_U8 },
617 };
618
619 static inline int
620 ctnetlink_parse_tuple_proto(struct nlattr *attr,
621                             struct nf_conntrack_tuple *tuple)
622 {
623         struct nlattr *tb[CTA_PROTO_MAX+1];
624         struct nf_conntrack_l4proto *l4proto;
625         int ret = 0;
626
627         ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
628         if (ret < 0)
629                 return ret;
630
631         if (!tb[CTA_PROTO_NUM])
632                 return -EINVAL;
633         tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
634
635         l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
636
637         if (likely(l4proto->nlattr_to_tuple)) {
638                 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
639                                           l4proto->nla_policy);
640                 if (ret == 0)
641                         ret = l4proto->nlattr_to_tuple(tb, tuple);
642         }
643
644         nf_ct_l4proto_put(l4proto);
645
646         return ret;
647 }
648
649 static int
650 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
651                       enum ctattr_tuple type, u_int8_t l3num)
652 {
653         struct nlattr *tb[CTA_TUPLE_MAX+1];
654         int err;
655
656         memset(tuple, 0, sizeof(*tuple));
657
658         nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
659
660         if (!tb[CTA_TUPLE_IP])
661                 return -EINVAL;
662
663         tuple->src.l3num = l3num;
664
665         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
666         if (err < 0)
667                 return err;
668
669         if (!tb[CTA_TUPLE_PROTO])
670                 return -EINVAL;
671
672         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
673         if (err < 0)
674                 return err;
675
676         /* orig and expect tuples get DIR_ORIGINAL */
677         if (type == CTA_TUPLE_REPLY)
678                 tuple->dst.dir = IP_CT_DIR_REPLY;
679         else
680                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
681
682         return 0;
683 }
684
685 static inline int
686 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
687 {
688         struct nlattr *tb[CTA_HELP_MAX+1];
689
690         nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
691
692         if (!tb[CTA_HELP_NAME])
693                 return -EINVAL;
694
695         *helper_name = nla_data(tb[CTA_HELP_NAME]);
696
697         return 0;
698 }
699
700 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
701         [CTA_STATUS]            = { .type = NLA_U32 },
702         [CTA_TIMEOUT]           = { .type = NLA_U32 },
703         [CTA_MARK]              = { .type = NLA_U32 },
704         [CTA_USE]               = { .type = NLA_U32 },
705         [CTA_ID]                = { .type = NLA_U32 },
706 };
707
708 static int
709 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
710                         struct nlmsghdr *nlh, struct nlattr *cda[])
711 {
712         struct nf_conntrack_tuple_hash *h;
713         struct nf_conntrack_tuple tuple;
714         struct nf_conn *ct;
715         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
716         u_int8_t u3 = nfmsg->nfgen_family;
717         int err = 0;
718
719         if (cda[CTA_TUPLE_ORIG])
720                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
721         else if (cda[CTA_TUPLE_REPLY])
722                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
723         else {
724                 /* Flush the whole table */
725                 nf_conntrack_flush(&init_net);
726                 return 0;
727         }
728
729         if (err < 0)
730                 return err;
731
732         h = nf_conntrack_find_get(&init_net, &tuple);
733         if (!h)
734                 return -ENOENT;
735
736         ct = nf_ct_tuplehash_to_ctrack(h);
737
738         if (cda[CTA_ID]) {
739                 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
740                 if (id != (u32)(unsigned long)ct) {
741                         nf_ct_put(ct);
742                         return -ENOENT;
743                 }
744         }
745
746         nf_ct_kill(ct);
747         nf_ct_put(ct);
748
749         return 0;
750 }
751
752 static int
753 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
754                         struct nlmsghdr *nlh, struct nlattr *cda[])
755 {
756         struct nf_conntrack_tuple_hash *h;
757         struct nf_conntrack_tuple tuple;
758         struct nf_conn *ct;
759         struct sk_buff *skb2 = NULL;
760         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
761         u_int8_t u3 = nfmsg->nfgen_family;
762         int err = 0;
763
764         if (nlh->nlmsg_flags & NLM_F_DUMP)
765                 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
766                                           ctnetlink_done);
767
768         if (cda[CTA_TUPLE_ORIG])
769                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
770         else if (cda[CTA_TUPLE_REPLY])
771                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
772         else
773                 return -EINVAL;
774
775         if (err < 0)
776                 return err;
777
778         h = nf_conntrack_find_get(&init_net, &tuple);
779         if (!h)
780                 return -ENOENT;
781
782         ct = nf_ct_tuplehash_to_ctrack(h);
783
784         err = -ENOMEM;
785         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
786         if (!skb2) {
787                 nf_ct_put(ct);
788                 return -ENOMEM;
789         }
790
791         rcu_read_lock();
792         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
793                                   IPCTNL_MSG_CT_NEW, 1, ct);
794         rcu_read_unlock();
795         nf_ct_put(ct);
796         if (err <= 0)
797                 goto free;
798
799         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
800         if (err < 0)
801                 goto out;
802
803         return 0;
804
805 free:
806         kfree_skb(skb2);
807 out:
808         return err;
809 }
810
811 #ifdef CONFIG_NF_NAT_NEEDED
812 static int
813 ctnetlink_parse_nat_setup(struct nf_conn *ct,
814                           enum nf_nat_manip_type manip,
815                           struct nlattr *attr)
816 {
817         typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
818
819         parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
820         if (!parse_nat_setup) {
821 #ifdef CONFIG_MODULES
822                 rcu_read_unlock();
823                 nfnl_unlock();
824                 if (request_module("nf-nat-ipv4") < 0) {
825                         nfnl_lock();
826                         rcu_read_lock();
827                         return -EOPNOTSUPP;
828                 }
829                 nfnl_lock();
830                 rcu_read_lock();
831                 if (nfnetlink_parse_nat_setup_hook)
832                         return -EAGAIN;
833 #endif
834                 return -EOPNOTSUPP;
835         }
836
837         return parse_nat_setup(ct, manip, attr);
838 }
839 #endif
840
841 static int
842 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
843 {
844         unsigned long d;
845         unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
846         d = ct->status ^ status;
847
848         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
849                 /* unchangeable */
850                 return -EBUSY;
851
852         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
853                 /* SEEN_REPLY bit can only be set */
854                 return -EBUSY;
855
856         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
857                 /* ASSURED bit can only be set */
858                 return -EBUSY;
859
860         /* Be careful here, modifying NAT bits can screw up things,
861          * so don't let users modify them directly if they don't pass
862          * nf_nat_range. */
863         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
864         return 0;
865 }
866
867 static int
868 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
869 {
870 #ifdef CONFIG_NF_NAT_NEEDED
871         int ret;
872
873         if (cda[CTA_NAT_DST]) {
874                 ret = ctnetlink_parse_nat_setup(ct,
875                                                 IP_NAT_MANIP_DST,
876                                                 cda[CTA_NAT_DST]);
877                 if (ret < 0)
878                         return ret;
879         }
880         if (cda[CTA_NAT_SRC]) {
881                 ret = ctnetlink_parse_nat_setup(ct,
882                                                 IP_NAT_MANIP_SRC,
883                                                 cda[CTA_NAT_SRC]);
884                 if (ret < 0)
885                         return ret;
886         }
887         return 0;
888 #else
889         return -EOPNOTSUPP;
890 #endif
891 }
892
893 static inline int
894 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
895 {
896         struct nf_conntrack_helper *helper;
897         struct nf_conn_help *help = nfct_help(ct);
898         char *helpname;
899         int err;
900
901         /* don't change helper of sibling connections */
902         if (ct->master)
903                 return -EBUSY;
904
905         err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
906         if (err < 0)
907                 return err;
908
909         if (!strcmp(helpname, "")) {
910                 if (help && help->helper) {
911                         /* we had a helper before ... */
912                         nf_ct_remove_expectations(ct);
913                         rcu_assign_pointer(help->helper, NULL);
914                 }
915
916                 return 0;
917         }
918
919         helper = __nf_conntrack_helper_find_byname(helpname);
920         if (helper == NULL) {
921 #ifdef CONFIG_MODULES
922                 spin_unlock_bh(&nf_conntrack_lock);
923
924                 if (request_module("nfct-helper-%s", helpname) < 0) {
925                         spin_lock_bh(&nf_conntrack_lock);
926                         return -EOPNOTSUPP;
927                 }
928
929                 spin_lock_bh(&nf_conntrack_lock);
930                 helper = __nf_conntrack_helper_find_byname(helpname);
931                 if (helper)
932                         return -EAGAIN;
933 #endif
934                 return -EOPNOTSUPP;
935         }
936
937         if (help) {
938                 if (help->helper == helper)
939                         return 0;
940                 if (help->helper)
941                         return -EBUSY;
942                 /* need to zero data of old helper */
943                 memset(&help->help, 0, sizeof(help->help));
944         } else {
945                 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
946                 if (help == NULL)
947                         return -ENOMEM;
948         }
949
950         rcu_assign_pointer(help->helper, helper);
951
952         return 0;
953 }
954
955 static inline int
956 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
957 {
958         u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
959
960         if (!del_timer(&ct->timeout))
961                 return -ETIME;
962
963         ct->timeout.expires = jiffies + timeout * HZ;
964         add_timer(&ct->timeout);
965
966         return 0;
967 }
968
969 static inline int
970 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
971 {
972         struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
973         struct nf_conntrack_l4proto *l4proto;
974         int err = 0;
975
976         nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
977
978         l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
979         if (l4proto->from_nlattr)
980                 err = l4proto->from_nlattr(tb, ct);
981         nf_ct_l4proto_put(l4proto);
982
983         return err;
984 }
985
986 #ifdef CONFIG_NF_NAT_NEEDED
987 static inline int
988 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
989 {
990         struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
991
992         nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
993
994         if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
995                 return -EINVAL;
996
997         natseq->correction_pos =
998                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
999
1000         if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1001                 return -EINVAL;
1002
1003         natseq->offset_before =
1004                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1005
1006         if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1007                 return -EINVAL;
1008
1009         natseq->offset_after =
1010                 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1011
1012         return 0;
1013 }
1014
1015 static int
1016 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1017 {
1018         int ret = 0;
1019         struct nf_conn_nat *nat = nfct_nat(ct);
1020
1021         if (!nat)
1022                 return 0;
1023
1024         if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1025                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1026                                          cda[CTA_NAT_SEQ_ADJ_ORIG]);
1027                 if (ret < 0)
1028                         return ret;
1029
1030                 ct->status |= IPS_SEQ_ADJUST;
1031         }
1032
1033         if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1034                 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1035                                          cda[CTA_NAT_SEQ_ADJ_REPLY]);
1036                 if (ret < 0)
1037                         return ret;
1038
1039                 ct->status |= IPS_SEQ_ADJUST;
1040         }
1041
1042         return 0;
1043 }
1044 #endif
1045
1046 static int
1047 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1048 {
1049         int err;
1050
1051         if (cda[CTA_HELP]) {
1052                 err = ctnetlink_change_helper(ct, cda);
1053                 if (err < 0)
1054                         return err;
1055         }
1056
1057         if (cda[CTA_TIMEOUT]) {
1058                 err = ctnetlink_change_timeout(ct, cda);
1059                 if (err < 0)
1060                         return err;
1061         }
1062
1063         if (cda[CTA_STATUS]) {
1064                 err = ctnetlink_change_status(ct, cda);
1065                 if (err < 0)
1066                         return err;
1067         }
1068
1069         if (cda[CTA_PROTOINFO]) {
1070                 err = ctnetlink_change_protoinfo(ct, cda);
1071                 if (err < 0)
1072                         return err;
1073         }
1074
1075 #if defined(CONFIG_NF_CONNTRACK_MARK)
1076         if (cda[CTA_MARK])
1077                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1078 #endif
1079
1080 #ifdef CONFIG_NF_NAT_NEEDED
1081         if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1082                 err = ctnetlink_change_nat_seq_adj(ct, cda);
1083                 if (err < 0)
1084                         return err;
1085         }
1086 #endif
1087
1088         return 0;
1089 }
1090
1091 static int
1092 ctnetlink_create_conntrack(struct nlattr *cda[],
1093                            struct nf_conntrack_tuple *otuple,
1094                            struct nf_conntrack_tuple *rtuple,
1095                            struct nf_conn *master_ct)
1096 {
1097         struct nf_conn *ct;
1098         int err = -EINVAL;
1099         struct nf_conntrack_helper *helper;
1100
1101         ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_KERNEL);
1102         if (ct == NULL || IS_ERR(ct))
1103                 return -ENOMEM;
1104
1105         if (!cda[CTA_TIMEOUT])
1106                 goto err;
1107         ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1108
1109         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1110         ct->status |= IPS_CONFIRMED;
1111
1112         rcu_read_lock();
1113         if (cda[CTA_HELP]) {
1114                 char *helpname;
1115  
1116                 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1117                 if (err < 0) {
1118                         rcu_read_unlock();
1119                         goto err;
1120                 }
1121
1122                 helper = __nf_conntrack_helper_find_byname(helpname);
1123                 if (helper == NULL) {
1124                         rcu_read_unlock();
1125 #ifdef CONFIG_MODULES
1126                         if (request_module("nfct-helper-%s", helpname) < 0) {
1127                                 err = -EOPNOTSUPP;
1128                                 goto err;
1129                         }
1130
1131                         rcu_read_lock();
1132                         helper = __nf_conntrack_helper_find_byname(helpname);
1133                         if (helper) {
1134                                 rcu_read_unlock();
1135                                 err = -EAGAIN;
1136                                 goto err;
1137                         }
1138                         rcu_read_unlock();
1139 #endif
1140                         err = -EOPNOTSUPP;
1141                         goto err;
1142                 } else {
1143                         struct nf_conn_help *help;
1144
1145                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1146                         if (help == NULL) {
1147                                 rcu_read_unlock();
1148                                 err = -ENOMEM;
1149                                 goto err;
1150                         }
1151
1152                         /* not in hash table yet so not strictly necessary */
1153                         rcu_assign_pointer(help->helper, helper);
1154                 }
1155         } else {
1156                 /* try an implicit helper assignation */
1157                 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1158                 if (err < 0) {
1159                         rcu_read_unlock();
1160                         goto err;
1161                 }
1162         }
1163
1164         if (cda[CTA_STATUS]) {
1165                 err = ctnetlink_change_status(ct, cda);
1166                 if (err < 0) {
1167                         rcu_read_unlock();
1168                         goto err;
1169                 }
1170         }
1171
1172         if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1173                 err = ctnetlink_change_nat(ct, cda);
1174                 if (err < 0) {
1175                         rcu_read_unlock();
1176                         goto err;
1177                 }
1178         }
1179
1180         if (cda[CTA_PROTOINFO]) {
1181                 err = ctnetlink_change_protoinfo(ct, cda);
1182                 if (err < 0) {
1183                         rcu_read_unlock();
1184                         goto err;
1185                 }
1186         }
1187
1188         nf_ct_acct_ext_add(ct, GFP_KERNEL);
1189
1190 #if defined(CONFIG_NF_CONNTRACK_MARK)
1191         if (cda[CTA_MARK])
1192                 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1193 #endif
1194
1195         /* setup master conntrack: this is a confirmed expectation */
1196         if (master_ct) {
1197                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1198                 ct->master = master_ct;
1199         }
1200
1201         add_timer(&ct->timeout);
1202         nf_conntrack_hash_insert(ct);
1203         rcu_read_unlock();
1204
1205         return 0;
1206
1207 err:
1208         nf_conntrack_free(ct);
1209         return err;
1210 }
1211
1212 static int
1213 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1214                         struct nlmsghdr *nlh, struct nlattr *cda[])
1215 {
1216         struct nf_conntrack_tuple otuple, rtuple;
1217         struct nf_conntrack_tuple_hash *h = NULL;
1218         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1219         u_int8_t u3 = nfmsg->nfgen_family;
1220         int err = 0;
1221
1222         if (cda[CTA_TUPLE_ORIG]) {
1223                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1224                 if (err < 0)
1225                         return err;
1226         }
1227
1228         if (cda[CTA_TUPLE_REPLY]) {
1229                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1230                 if (err < 0)
1231                         return err;
1232         }
1233
1234         spin_lock_bh(&nf_conntrack_lock);
1235         if (cda[CTA_TUPLE_ORIG])
1236                 h = __nf_conntrack_find(&init_net, &otuple);
1237         else if (cda[CTA_TUPLE_REPLY])
1238                 h = __nf_conntrack_find(&init_net, &rtuple);
1239
1240         if (h == NULL) {
1241                 struct nf_conntrack_tuple master;
1242                 struct nf_conntrack_tuple_hash *master_h = NULL;
1243                 struct nf_conn *master_ct = NULL;
1244
1245                 if (cda[CTA_TUPLE_MASTER]) {
1246                         err = ctnetlink_parse_tuple(cda,
1247                                                     &master,
1248                                                     CTA_TUPLE_MASTER,
1249                                                     u3);
1250                         if (err < 0)
1251                                 goto out_unlock;
1252
1253                         master_h = __nf_conntrack_find(&init_net, &master);
1254                         if (master_h == NULL) {
1255                                 err = -ENOENT;
1256                                 goto out_unlock;
1257                         }
1258                         master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1259                         nf_conntrack_get(&master_ct->ct_general);
1260                 }
1261
1262                 spin_unlock_bh(&nf_conntrack_lock);
1263                 err = -ENOENT;
1264                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1265                         err = ctnetlink_create_conntrack(cda,
1266                                                          &otuple,
1267                                                          &rtuple,
1268                                                          master_ct);
1269                 if (err < 0 && master_ct)
1270                         nf_ct_put(master_ct);
1271
1272                 return err;
1273         }
1274         /* implicit 'else' */
1275
1276         /* We manipulate the conntrack inside the global conntrack table lock,
1277          * so there's no need to increase the refcount */
1278         err = -EEXIST;
1279         if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1280                 /* we only allow nat config for new conntracks */
1281                 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1282                         err = -EOPNOTSUPP;
1283                         goto out_unlock;
1284                 }
1285                 /* can't link an existing conntrack to a master */
1286                 if (cda[CTA_TUPLE_MASTER]) {
1287                         err = -EOPNOTSUPP;
1288                         goto out_unlock;
1289                 }
1290                 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1291                                                  cda);
1292         }
1293
1294 out_unlock:
1295         spin_unlock_bh(&nf_conntrack_lock);
1296         return err;
1297 }
1298
1299 /***********************************************************************
1300  * EXPECT
1301  ***********************************************************************/
1302
1303 static inline int
1304 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1305                          const struct nf_conntrack_tuple *tuple,
1306                          enum ctattr_expect type)
1307 {
1308         struct nlattr *nest_parms;
1309
1310         nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1311         if (!nest_parms)
1312                 goto nla_put_failure;
1313         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1314                 goto nla_put_failure;
1315         nla_nest_end(skb, nest_parms);
1316
1317         return 0;
1318
1319 nla_put_failure:
1320         return -1;
1321 }
1322
1323 static inline int
1324 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1325                         const struct nf_conntrack_tuple *tuple,
1326                         const struct nf_conntrack_tuple_mask *mask)
1327 {
1328         int ret;
1329         struct nf_conntrack_l3proto *l3proto;
1330         struct nf_conntrack_l4proto *l4proto;
1331         struct nf_conntrack_tuple m;
1332         struct nlattr *nest_parms;
1333
1334         memset(&m, 0xFF, sizeof(m));
1335         m.src.u.all = mask->src.u.all;
1336         memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1337
1338         nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1339         if (!nest_parms)
1340                 goto nla_put_failure;
1341
1342         l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1343         ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1344
1345         if (unlikely(ret < 0))
1346                 goto nla_put_failure;
1347
1348         l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1349         ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1350         if (unlikely(ret < 0))
1351                 goto nla_put_failure;
1352
1353         nla_nest_end(skb, nest_parms);
1354
1355         return 0;
1356
1357 nla_put_failure:
1358         return -1;
1359 }
1360
1361 static int
1362 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1363                           const struct nf_conntrack_expect *exp)
1364 {
1365         struct nf_conn *master = exp->master;
1366         long timeout = (exp->timeout.expires - jiffies) / HZ;
1367
1368         if (timeout < 0)
1369                 timeout = 0;
1370
1371         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1372                 goto nla_put_failure;
1373         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1374                 goto nla_put_failure;
1375         if (ctnetlink_exp_dump_tuple(skb,
1376                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1377                                  CTA_EXPECT_MASTER) < 0)
1378                 goto nla_put_failure;
1379
1380         NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1381         NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1382
1383         return 0;
1384
1385 nla_put_failure:
1386         return -1;
1387 }
1388
1389 static int
1390 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1391                     int event,
1392                     int nowait,
1393                     const struct nf_conntrack_expect *exp)
1394 {
1395         struct nlmsghdr *nlh;
1396         struct nfgenmsg *nfmsg;
1397         unsigned char *b = skb_tail_pointer(skb);
1398
1399         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1400         nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1401         nfmsg  = NLMSG_DATA(nlh);
1402
1403         nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
1404         nfmsg->nfgen_family = exp->tuple.src.l3num;
1405         nfmsg->version      = NFNETLINK_V0;
1406         nfmsg->res_id       = 0;
1407
1408         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1409                 goto nla_put_failure;
1410
1411         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1412         return skb->len;
1413
1414 nlmsg_failure:
1415 nla_put_failure:
1416         nlmsg_trim(skb, b);
1417         return -1;
1418 }
1419
1420 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1421 static int ctnetlink_expect_event(struct notifier_block *this,
1422                                   unsigned long events, void *ptr)
1423 {
1424         struct nlmsghdr *nlh;
1425         struct nfgenmsg *nfmsg;
1426         struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1427         struct sk_buff *skb;
1428         unsigned int type;
1429         sk_buff_data_t b;
1430         int flags = 0;
1431
1432         if (events & IPEXP_NEW) {
1433                 type = IPCTNL_MSG_EXP_NEW;
1434                 flags = NLM_F_CREATE|NLM_F_EXCL;
1435         } else
1436                 return NOTIFY_DONE;
1437
1438         if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1439                 return NOTIFY_DONE;
1440
1441         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1442         if (!skb)
1443                 return NOTIFY_DONE;
1444
1445         b = skb->tail;
1446
1447         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1448         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1449         nfmsg = NLMSG_DATA(nlh);
1450
1451         nlh->nlmsg_flags    = flags;
1452         nfmsg->nfgen_family = exp->tuple.src.l3num;
1453         nfmsg->version      = NFNETLINK_V0;
1454         nfmsg->res_id       = 0;
1455
1456         rcu_read_lock();
1457         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1458                 goto nla_put_failure;
1459         rcu_read_unlock();
1460
1461         nlh->nlmsg_len = skb->tail - b;
1462         nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1463         return NOTIFY_DONE;
1464
1465 nla_put_failure:
1466         rcu_read_unlock();
1467 nlmsg_failure:
1468         kfree_skb(skb);
1469         return NOTIFY_DONE;
1470 }
1471 #endif
1472 static int ctnetlink_exp_done(struct netlink_callback *cb)
1473 {
1474         if (cb->args[1])
1475                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1476         return 0;
1477 }
1478
1479 static int
1480 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1481 {
1482         struct net *net = &init_net;
1483         struct nf_conntrack_expect *exp, *last;
1484         struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1485         struct hlist_node *n;
1486         u_int8_t l3proto = nfmsg->nfgen_family;
1487
1488         rcu_read_lock();
1489         last = (struct nf_conntrack_expect *)cb->args[1];
1490         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1491 restart:
1492                 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1493                                      hnode) {
1494                         if (l3proto && exp->tuple.src.l3num != l3proto)
1495                                 continue;
1496                         if (cb->args[1]) {
1497                                 if (exp != last)
1498                                         continue;
1499                                 cb->args[1] = 0;
1500                         }
1501                         if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1502                                                     cb->nlh->nlmsg_seq,
1503                                                     IPCTNL_MSG_EXP_NEW,
1504                                                     1, exp) < 0) {
1505                                 if (!atomic_inc_not_zero(&exp->use))
1506                                         continue;
1507                                 cb->args[1] = (unsigned long)exp;
1508                                 goto out;
1509                         }
1510                 }
1511                 if (cb->args[1]) {
1512                         cb->args[1] = 0;
1513                         goto restart;
1514                 }
1515         }
1516 out:
1517         rcu_read_unlock();
1518         if (last)
1519                 nf_ct_expect_put(last);
1520
1521         return skb->len;
1522 }
1523
1524 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1525         [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
1526         [CTA_EXPECT_ID]         = { .type = NLA_U32 },
1527 };
1528
1529 static int
1530 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1531                      struct nlmsghdr *nlh, struct nlattr *cda[])
1532 {
1533         struct nf_conntrack_tuple tuple;
1534         struct nf_conntrack_expect *exp;
1535         struct sk_buff *skb2;
1536         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1537         u_int8_t u3 = nfmsg->nfgen_family;
1538         int err = 0;
1539
1540         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1541                 return netlink_dump_start(ctnl, skb, nlh,
1542                                           ctnetlink_exp_dump_table,
1543                                           ctnetlink_exp_done);
1544         }
1545
1546         if (cda[CTA_EXPECT_MASTER])
1547                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1548         else
1549                 return -EINVAL;
1550
1551         if (err < 0)
1552                 return err;
1553
1554         exp = nf_ct_expect_find_get(&init_net, &tuple);
1555         if (!exp)
1556                 return -ENOENT;
1557
1558         if (cda[CTA_EXPECT_ID]) {
1559                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1560                 if (ntohl(id) != (u32)(unsigned long)exp) {
1561                         nf_ct_expect_put(exp);
1562                         return -ENOENT;
1563                 }
1564         }
1565
1566         err = -ENOMEM;
1567         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1568         if (!skb2)
1569                 goto out;
1570
1571         rcu_read_lock();
1572         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1573                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1574                                       1, exp);
1575         rcu_read_unlock();
1576         if (err <= 0)
1577                 goto free;
1578
1579         nf_ct_expect_put(exp);
1580
1581         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1582
1583 free:
1584         kfree_skb(skb2);
1585 out:
1586         nf_ct_expect_put(exp);
1587         return err;
1588 }
1589
1590 static int
1591 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1592                      struct nlmsghdr *nlh, struct nlattr *cda[])
1593 {
1594         struct nf_conntrack_expect *exp;
1595         struct nf_conntrack_tuple tuple;
1596         struct nf_conntrack_helper *h;
1597         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1598         struct hlist_node *n, *next;
1599         u_int8_t u3 = nfmsg->nfgen_family;
1600         unsigned int i;
1601         int err;
1602
1603         if (cda[CTA_EXPECT_TUPLE]) {
1604                 /* delete a single expect by tuple */
1605                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1606                 if (err < 0)
1607                         return err;
1608
1609                 /* bump usage count to 2 */
1610                 exp = nf_ct_expect_find_get(&init_net, &tuple);
1611                 if (!exp)
1612                         return -ENOENT;
1613
1614                 if (cda[CTA_EXPECT_ID]) {
1615                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1616                         if (ntohl(id) != (u32)(unsigned long)exp) {
1617                                 nf_ct_expect_put(exp);
1618                                 return -ENOENT;
1619                         }
1620                 }
1621
1622                 /* after list removal, usage count == 1 */
1623                 nf_ct_unexpect_related(exp);
1624                 /* have to put what we 'get' above.
1625                  * after this line usage count == 0 */
1626                 nf_ct_expect_put(exp);
1627         } else if (cda[CTA_EXPECT_HELP_NAME]) {
1628                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1629                 struct nf_conn_help *m_help;
1630
1631                 /* delete all expectations for this helper */
1632                 spin_lock_bh(&nf_conntrack_lock);
1633                 h = __nf_conntrack_helper_find_byname(name);
1634                 if (!h) {
1635                         spin_unlock_bh(&nf_conntrack_lock);
1636                         return -EOPNOTSUPP;
1637                 }
1638                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1639                         hlist_for_each_entry_safe(exp, n, next,
1640                                                   &init_net.ct.expect_hash[i],
1641                                                   hnode) {
1642                                 m_help = nfct_help(exp->master);
1643                                 if (m_help->helper == h
1644                                     && del_timer(&exp->timeout)) {
1645                                         nf_ct_unlink_expect(exp);
1646                                         nf_ct_expect_put(exp);
1647                                 }
1648                         }
1649                 }
1650                 spin_unlock_bh(&nf_conntrack_lock);
1651         } else {
1652                 /* This basically means we have to flush everything*/
1653                 spin_lock_bh(&nf_conntrack_lock);
1654                 for (i = 0; i < nf_ct_expect_hsize; i++) {
1655                         hlist_for_each_entry_safe(exp, n, next,
1656                                                   &init_net.ct.expect_hash[i],
1657                                                   hnode) {
1658                                 if (del_timer(&exp->timeout)) {
1659                                         nf_ct_unlink_expect(exp);
1660                                         nf_ct_expect_put(exp);
1661                                 }
1662                         }
1663                 }
1664                 spin_unlock_bh(&nf_conntrack_lock);
1665         }
1666
1667         return 0;
1668 }
1669 static int
1670 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1671 {
1672         return -EOPNOTSUPP;
1673 }
1674
1675 static int
1676 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1677 {
1678         struct nf_conntrack_tuple tuple, mask, master_tuple;
1679         struct nf_conntrack_tuple_hash *h = NULL;
1680         struct nf_conntrack_expect *exp;
1681         struct nf_conn *ct;
1682         struct nf_conn_help *help;
1683         int err = 0;
1684
1685         /* caller guarantees that those three CTA_EXPECT_* exist */
1686         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1687         if (err < 0)
1688                 return err;
1689         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1690         if (err < 0)
1691                 return err;
1692         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1693         if (err < 0)
1694                 return err;
1695
1696         /* Look for master conntrack of this expectation */
1697         h = nf_conntrack_find_get(&init_net, &master_tuple);
1698         if (!h)
1699                 return -ENOENT;
1700         ct = nf_ct_tuplehash_to_ctrack(h);
1701         help = nfct_help(ct);
1702
1703         if (!help || !help->helper) {
1704                 /* such conntrack hasn't got any helper, abort */
1705                 err = -EOPNOTSUPP;
1706                 goto out;
1707         }
1708
1709         exp = nf_ct_expect_alloc(ct);
1710         if (!exp) {
1711                 err = -ENOMEM;
1712                 goto out;
1713         }
1714
1715         exp->expectfn = NULL;
1716         exp->flags = 0;
1717         exp->master = ct;
1718         exp->helper = NULL;
1719         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1720         memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1721         exp->mask.src.u.all = mask.src.u.all;
1722
1723         err = nf_ct_expect_related(exp);
1724         nf_ct_expect_put(exp);
1725
1726 out:
1727         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1728         return err;
1729 }
1730
1731 static int
1732 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1733                      struct nlmsghdr *nlh, struct nlattr *cda[])
1734 {
1735         struct nf_conntrack_tuple tuple;
1736         struct nf_conntrack_expect *exp;
1737         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1738         u_int8_t u3 = nfmsg->nfgen_family;
1739         int err = 0;
1740
1741         if (!cda[CTA_EXPECT_TUPLE]
1742             || !cda[CTA_EXPECT_MASK]
1743             || !cda[CTA_EXPECT_MASTER])
1744                 return -EINVAL;
1745
1746         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1747         if (err < 0)
1748                 return err;
1749
1750         spin_lock_bh(&nf_conntrack_lock);
1751         exp = __nf_ct_expect_find(&init_net, &tuple);
1752
1753         if (!exp) {
1754                 spin_unlock_bh(&nf_conntrack_lock);
1755                 err = -ENOENT;
1756                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1757                         err = ctnetlink_create_expect(cda, u3);
1758                 return err;
1759         }
1760
1761         err = -EEXIST;
1762         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1763                 err = ctnetlink_change_expect(exp, cda);
1764         spin_unlock_bh(&nf_conntrack_lock);
1765
1766         return err;
1767 }
1768
1769 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1770 static struct notifier_block ctnl_notifier = {
1771         .notifier_call  = ctnetlink_conntrack_event,
1772 };
1773
1774 static struct notifier_block ctnl_notifier_exp = {
1775         .notifier_call  = ctnetlink_expect_event,
1776 };
1777 #endif
1778
1779 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1780         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1781                                             .attr_count = CTA_MAX,
1782                                             .policy = ct_nla_policy },
1783         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1784                                             .attr_count = CTA_MAX,
1785                                             .policy = ct_nla_policy },
1786         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1787                                             .attr_count = CTA_MAX,
1788                                             .policy = ct_nla_policy },
1789         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1790                                             .attr_count = CTA_MAX,
1791                                             .policy = ct_nla_policy },
1792 };
1793
1794 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1795         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1796                                             .attr_count = CTA_EXPECT_MAX,
1797                                             .policy = exp_nla_policy },
1798         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1799                                             .attr_count = CTA_EXPECT_MAX,
1800                                             .policy = exp_nla_policy },
1801         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1802                                             .attr_count = CTA_EXPECT_MAX,
1803                                             .policy = exp_nla_policy },
1804 };
1805
1806 static const struct nfnetlink_subsystem ctnl_subsys = {
1807         .name                           = "conntrack",
1808         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1809         .cb_count                       = IPCTNL_MSG_MAX,
1810         .cb                             = ctnl_cb,
1811 };
1812
1813 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1814         .name                           = "conntrack_expect",
1815         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1816         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1817         .cb                             = ctnl_exp_cb,
1818 };
1819
1820 MODULE_ALIAS("ip_conntrack_netlink");
1821 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1822 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1823
1824 static int __init ctnetlink_init(void)
1825 {
1826         int ret;
1827
1828         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1829         ret = nfnetlink_subsys_register(&ctnl_subsys);
1830         if (ret < 0) {
1831                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1832                 goto err_out;
1833         }
1834
1835         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1836         if (ret < 0) {
1837                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1838                 goto err_unreg_subsys;
1839         }
1840
1841 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1842         ret = nf_conntrack_register_notifier(&ctnl_notifier);
1843         if (ret < 0) {
1844                 printk("ctnetlink_init: cannot register notifier.\n");
1845                 goto err_unreg_exp_subsys;
1846         }
1847
1848         ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1849         if (ret < 0) {
1850                 printk("ctnetlink_init: cannot expect register notifier.\n");
1851                 goto err_unreg_notifier;
1852         }
1853 #endif
1854
1855         return 0;
1856
1857 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1858 err_unreg_notifier:
1859         nf_conntrack_unregister_notifier(&ctnl_notifier);
1860 err_unreg_exp_subsys:
1861         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1862 #endif
1863 err_unreg_subsys:
1864         nfnetlink_subsys_unregister(&ctnl_subsys);
1865 err_out:
1866         return ret;
1867 }
1868
1869 static void __exit ctnetlink_exit(void)
1870 {
1871         printk("ctnetlink: unregistering from nfnetlink.\n");
1872
1873 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1874         nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1875         nf_conntrack_unregister_notifier(&ctnl_notifier);
1876 #endif
1877
1878         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1879         nfnetlink_subsys_unregister(&ctnl_subsys);
1880         return;
1881 }
1882
1883 module_init(ctnetlink_init);
1884 module_exit(ctnetlink_exit);