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