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