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