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