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