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