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