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