netfilter: ctnetlink: don't permit ct creation with random tuple
[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-2012 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_l4proto.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 portid, 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 = portid ? NLM_F_MULTI : 0, event;
428
429 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
430 nlh = nlmsg_put(skb, portid, 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 static inline size_t
482 ctnetlink_proto_size(const struct nf_conn *ct)
483 {
484 struct nf_conntrack_l3proto *l3proto;
485 struct nf_conntrack_l4proto *l4proto;
486 size_t len = 0;
487
488 rcu_read_lock();
489 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
490 len += l3proto->nla_size;
491
492 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
493 len += l4proto->nla_size;
494 rcu_read_unlock();
495
496 return len;
497 }
498
499 static inline size_t
500 ctnetlink_counters_size(const struct nf_conn *ct)
501 {
502 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
503 return 0;
504 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
505 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
506 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
507 ;
508 }
509
510 static inline int
511 ctnetlink_secctx_size(const struct nf_conn *ct)
512 {
513 #ifdef CONFIG_NF_CONNTRACK_SECMARK
514 int len, ret;
515
516 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
517 if (ret)
518 return 0;
519
520 return nla_total_size(0) /* CTA_SECCTX */
521 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
522 #else
523 return 0;
524 #endif
525 }
526
527 static inline size_t
528 ctnetlink_timestamp_size(const struct nf_conn *ct)
529 {
530 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
531 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
532 return 0;
533 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
534 #else
535 return 0;
536 #endif
537 }
538
539 static inline size_t
540 ctnetlink_nlmsg_size(const struct nf_conn *ct)
541 {
542 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
543 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
544 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
545 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
546 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
547 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
548 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
549 + ctnetlink_counters_size(ct)
550 + ctnetlink_timestamp_size(ct)
551 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
552 + nla_total_size(0) /* CTA_PROTOINFO */
553 + nla_total_size(0) /* CTA_HELP */
554 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
555 + ctnetlink_secctx_size(ct)
556 #ifdef CONFIG_NF_NAT_NEEDED
557 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
558 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
559 #endif
560 #ifdef CONFIG_NF_CONNTRACK_MARK
561 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
562 #endif
563 + ctnetlink_proto_size(ct)
564 ;
565 }
566
567 #ifdef CONFIG_NF_CONNTRACK_EVENTS
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->portid, 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->portid, 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).portid,
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 .len = NF_CT_HELPER_NAME_LEN - 1 },
903 };
904
905 static inline int
906 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
907 struct nlattr **helpinfo)
908 {
909 struct nlattr *tb[CTA_HELP_MAX+1];
910
911 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
912
913 if (!tb[CTA_HELP_NAME])
914 return -EINVAL;
915
916 *helper_name = nla_data(tb[CTA_HELP_NAME]);
917
918 if (tb[CTA_HELP_INFO])
919 *helpinfo = tb[CTA_HELP_INFO];
920
921 return 0;
922 }
923
924 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
925 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
926 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
927 [CTA_STATUS] = { .type = NLA_U32 },
928 [CTA_PROTOINFO] = { .type = NLA_NESTED },
929 [CTA_HELP] = { .type = NLA_NESTED },
930 [CTA_NAT_SRC] = { .type = NLA_NESTED },
931 [CTA_TIMEOUT] = { .type = NLA_U32 },
932 [CTA_MARK] = { .type = NLA_U32 },
933 [CTA_ID] = { .type = NLA_U32 },
934 [CTA_NAT_DST] = { .type = NLA_NESTED },
935 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
936 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
937 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
938 [CTA_ZONE] = { .type = NLA_U16 },
939 [CTA_MARK_MASK] = { .type = NLA_U32 },
940 };
941
942 static int
943 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
944 const struct nlmsghdr *nlh,
945 const struct nlattr * const cda[])
946 {
947 struct net *net = sock_net(ctnl);
948 struct nf_conntrack_tuple_hash *h;
949 struct nf_conntrack_tuple tuple;
950 struct nf_conn *ct;
951 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
952 u_int8_t u3 = nfmsg->nfgen_family;
953 u16 zone;
954 int err;
955
956 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
957 if (err < 0)
958 return err;
959
960 if (cda[CTA_TUPLE_ORIG])
961 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
962 else if (cda[CTA_TUPLE_REPLY])
963 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
964 else {
965 /* Flush the whole table */
966 nf_conntrack_flush_report(net,
967 NETLINK_CB(skb).portid,
968 nlmsg_report(nlh));
969 return 0;
970 }
971
972 if (err < 0)
973 return err;
974
975 h = nf_conntrack_find_get(net, zone, &tuple);
976 if (!h)
977 return -ENOENT;
978
979 ct = nf_ct_tuplehash_to_ctrack(h);
980
981 if (cda[CTA_ID]) {
982 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
983 if (id != (u32)(unsigned long)ct) {
984 nf_ct_put(ct);
985 return -ENOENT;
986 }
987 }
988
989 if (del_timer(&ct->timeout)) {
990 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
991 NETLINK_CB(skb).portid,
992 nlmsg_report(nlh)) < 0) {
993 nf_ct_delete_from_lists(ct);
994 /* we failed to report the event, try later */
995 nf_ct_dying_timeout(ct);
996 nf_ct_put(ct);
997 return 0;
998 }
999 /* death_by_timeout would report the event again */
1000 set_bit(IPS_DYING_BIT, &ct->status);
1001 nf_ct_delete_from_lists(ct);
1002 nf_ct_put(ct);
1003 }
1004 nf_ct_put(ct);
1005
1006 return 0;
1007 }
1008
1009 static int
1010 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1011 const struct nlmsghdr *nlh,
1012 const struct nlattr * const cda[])
1013 {
1014 struct net *net = sock_net(ctnl);
1015 struct nf_conntrack_tuple_hash *h;
1016 struct nf_conntrack_tuple tuple;
1017 struct nf_conn *ct;
1018 struct sk_buff *skb2 = NULL;
1019 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1020 u_int8_t u3 = nfmsg->nfgen_family;
1021 u16 zone;
1022 int err;
1023
1024 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1025 struct netlink_dump_control c = {
1026 .dump = ctnetlink_dump_table,
1027 .done = ctnetlink_done,
1028 };
1029 #ifdef CONFIG_NF_CONNTRACK_MARK
1030 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1031 struct ctnetlink_dump_filter *filter;
1032
1033 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1034 GFP_ATOMIC);
1035 if (filter == NULL)
1036 return -ENOMEM;
1037
1038 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1039 filter->mark.mask =
1040 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1041 c.data = filter;
1042 }
1043 #endif
1044 return netlink_dump_start(ctnl, skb, nlh, &c);
1045 }
1046
1047 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1048 if (err < 0)
1049 return err;
1050
1051 if (cda[CTA_TUPLE_ORIG])
1052 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1053 else if (cda[CTA_TUPLE_REPLY])
1054 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1055 else
1056 return -EINVAL;
1057
1058 if (err < 0)
1059 return err;
1060
1061 h = nf_conntrack_find_get(net, zone, &tuple);
1062 if (!h)
1063 return -ENOENT;
1064
1065 ct = nf_ct_tuplehash_to_ctrack(h);
1066
1067 err = -ENOMEM;
1068 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1069 if (skb2 == NULL) {
1070 nf_ct_put(ct);
1071 return -ENOMEM;
1072 }
1073
1074 rcu_read_lock();
1075 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1076 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1077 rcu_read_unlock();
1078 nf_ct_put(ct);
1079 if (err <= 0)
1080 goto free;
1081
1082 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1083 if (err < 0)
1084 goto out;
1085
1086 return 0;
1087
1088 free:
1089 kfree_skb(skb2);
1090 out:
1091 /* this avoids a loop in nfnetlink. */
1092 return err == -EAGAIN ? -ENOBUFS : err;
1093 }
1094
1095 static int ctnetlink_done_list(struct netlink_callback *cb)
1096 {
1097 if (cb->args[1])
1098 nf_ct_put((struct nf_conn *)cb->args[1]);
1099 return 0;
1100 }
1101
1102 static int
1103 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1104 struct hlist_nulls_head *list)
1105 {
1106 struct nf_conn *ct, *last;
1107 struct nf_conntrack_tuple_hash *h;
1108 struct hlist_nulls_node *n;
1109 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1110 u_int8_t l3proto = nfmsg->nfgen_family;
1111 int res;
1112
1113 if (cb->args[2])
1114 return 0;
1115
1116 spin_lock_bh(&nf_conntrack_lock);
1117 last = (struct nf_conn *)cb->args[1];
1118 restart:
1119 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1120 ct = nf_ct_tuplehash_to_ctrack(h);
1121 if (l3proto && nf_ct_l3num(ct) != l3proto)
1122 continue;
1123 if (cb->args[1]) {
1124 if (ct != last)
1125 continue;
1126 cb->args[1] = 0;
1127 }
1128 rcu_read_lock();
1129 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1130 cb->nlh->nlmsg_seq,
1131 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1132 ct);
1133 rcu_read_unlock();
1134 if (res < 0) {
1135 nf_conntrack_get(&ct->ct_general);
1136 cb->args[1] = (unsigned long)ct;
1137 goto out;
1138 }
1139 }
1140 if (cb->args[1]) {
1141 cb->args[1] = 0;
1142 goto restart;
1143 } else
1144 cb->args[2] = 1;
1145 out:
1146 spin_unlock_bh(&nf_conntrack_lock);
1147 if (last)
1148 nf_ct_put(last);
1149
1150 return skb->len;
1151 }
1152
1153 static int
1154 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1155 {
1156 struct net *net = sock_net(skb->sk);
1157
1158 return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1159 }
1160
1161 static int
1162 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1163 const struct nlmsghdr *nlh,
1164 const struct nlattr * const cda[])
1165 {
1166 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1167 struct netlink_dump_control c = {
1168 .dump = ctnetlink_dump_dying,
1169 .done = ctnetlink_done_list,
1170 };
1171 return netlink_dump_start(ctnl, skb, nlh, &c);
1172 }
1173
1174 return -EOPNOTSUPP;
1175 }
1176
1177 static int
1178 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1179 {
1180 struct net *net = sock_net(skb->sk);
1181
1182 return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1183 }
1184
1185 static int
1186 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1187 const struct nlmsghdr *nlh,
1188 const struct nlattr * const cda[])
1189 {
1190 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1191 struct netlink_dump_control c = {
1192 .dump = ctnetlink_dump_unconfirmed,
1193 .done = ctnetlink_done_list,
1194 };
1195 return netlink_dump_start(ctnl, skb, nlh, &c);
1196 }
1197
1198 return -EOPNOTSUPP;
1199 }
1200
1201 #ifdef CONFIG_NF_NAT_NEEDED
1202 static int
1203 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1204 enum nf_nat_manip_type manip,
1205 const struct nlattr *attr)
1206 {
1207 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1208 int err;
1209
1210 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1211 if (!parse_nat_setup) {
1212 #ifdef CONFIG_MODULES
1213 rcu_read_unlock();
1214 nfnl_unlock();
1215 if (request_module("nf-nat") < 0) {
1216 nfnl_lock();
1217 rcu_read_lock();
1218 return -EOPNOTSUPP;
1219 }
1220 nfnl_lock();
1221 rcu_read_lock();
1222 if (nfnetlink_parse_nat_setup_hook)
1223 return -EAGAIN;
1224 #endif
1225 return -EOPNOTSUPP;
1226 }
1227
1228 err = parse_nat_setup(ct, manip, attr);
1229 if (err == -EAGAIN) {
1230 #ifdef CONFIG_MODULES
1231 rcu_read_unlock();
1232 nfnl_unlock();
1233 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1234 nfnl_lock();
1235 rcu_read_lock();
1236 return -EOPNOTSUPP;
1237 }
1238 nfnl_lock();
1239 rcu_read_lock();
1240 #else
1241 err = -EOPNOTSUPP;
1242 #endif
1243 }
1244 return err;
1245 }
1246 #endif
1247
1248 static int
1249 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1250 {
1251 unsigned long d;
1252 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1253 d = ct->status ^ status;
1254
1255 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1256 /* unchangeable */
1257 return -EBUSY;
1258
1259 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1260 /* SEEN_REPLY bit can only be set */
1261 return -EBUSY;
1262
1263 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1264 /* ASSURED bit can only be set */
1265 return -EBUSY;
1266
1267 /* Be careful here, modifying NAT bits can screw up things,
1268 * so don't let users modify them directly if they don't pass
1269 * nf_nat_range. */
1270 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1271 return 0;
1272 }
1273
1274 static int
1275 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1276 {
1277 #ifdef CONFIG_NF_NAT_NEEDED
1278 int ret;
1279
1280 if (cda[CTA_NAT_DST]) {
1281 ret = ctnetlink_parse_nat_setup(ct,
1282 NF_NAT_MANIP_DST,
1283 cda[CTA_NAT_DST]);
1284 if (ret < 0)
1285 return ret;
1286 }
1287 if (cda[CTA_NAT_SRC]) {
1288 ret = ctnetlink_parse_nat_setup(ct,
1289 NF_NAT_MANIP_SRC,
1290 cda[CTA_NAT_SRC]);
1291 if (ret < 0)
1292 return ret;
1293 }
1294 return 0;
1295 #else
1296 return -EOPNOTSUPP;
1297 #endif
1298 }
1299
1300 static inline int
1301 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1302 {
1303 struct nf_conntrack_helper *helper;
1304 struct nf_conn_help *help = nfct_help(ct);
1305 char *helpname = NULL;
1306 struct nlattr *helpinfo = NULL;
1307 int err;
1308
1309 /* don't change helper of sibling connections */
1310 if (ct->master)
1311 return -EBUSY;
1312
1313 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1314 if (err < 0)
1315 return err;
1316
1317 if (!strcmp(helpname, "")) {
1318 if (help && help->helper) {
1319 /* we had a helper before ... */
1320 nf_ct_remove_expectations(ct);
1321 RCU_INIT_POINTER(help->helper, NULL);
1322 }
1323
1324 return 0;
1325 }
1326
1327 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1328 nf_ct_protonum(ct));
1329 if (helper == NULL) {
1330 #ifdef CONFIG_MODULES
1331 spin_unlock_bh(&nf_conntrack_lock);
1332
1333 if (request_module("nfct-helper-%s", helpname) < 0) {
1334 spin_lock_bh(&nf_conntrack_lock);
1335 return -EOPNOTSUPP;
1336 }
1337
1338 spin_lock_bh(&nf_conntrack_lock);
1339 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1340 nf_ct_protonum(ct));
1341 if (helper)
1342 return -EAGAIN;
1343 #endif
1344 return -EOPNOTSUPP;
1345 }
1346
1347 if (help) {
1348 if (help->helper == helper) {
1349 /* update private helper data if allowed. */
1350 if (helper->from_nlattr)
1351 helper->from_nlattr(helpinfo, ct);
1352 return 0;
1353 } else
1354 return -EBUSY;
1355 }
1356
1357 /* we cannot set a helper for an existing conntrack */
1358 return -EOPNOTSUPP;
1359 }
1360
1361 static inline int
1362 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1363 {
1364 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1365
1366 if (!del_timer(&ct->timeout))
1367 return -ETIME;
1368
1369 ct->timeout.expires = jiffies + timeout * HZ;
1370 add_timer(&ct->timeout);
1371
1372 return 0;
1373 }
1374
1375 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1376 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1377 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1378 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1379 };
1380
1381 static inline int
1382 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1383 {
1384 const struct nlattr *attr = cda[CTA_PROTOINFO];
1385 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1386 struct nf_conntrack_l4proto *l4proto;
1387 int err = 0;
1388
1389 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1390
1391 rcu_read_lock();
1392 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1393 if (l4proto->from_nlattr)
1394 err = l4proto->from_nlattr(tb, ct);
1395 rcu_read_unlock();
1396
1397 return err;
1398 }
1399
1400 #ifdef CONFIG_NF_NAT_NEEDED
1401 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1402 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1403 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1404 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1405 };
1406
1407 static inline int
1408 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1409 {
1410 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1411
1412 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1413
1414 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1415 return -EINVAL;
1416
1417 natseq->correction_pos =
1418 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1419
1420 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1421 return -EINVAL;
1422
1423 natseq->offset_before =
1424 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1425
1426 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1427 return -EINVAL;
1428
1429 natseq->offset_after =
1430 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1431
1432 return 0;
1433 }
1434
1435 static int
1436 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1437 const struct nlattr * const cda[])
1438 {
1439 int ret = 0;
1440 struct nf_conn_nat *nat = nfct_nat(ct);
1441
1442 if (!nat)
1443 return 0;
1444
1445 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1446 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1447 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1448 if (ret < 0)
1449 return ret;
1450
1451 ct->status |= IPS_SEQ_ADJUST;
1452 }
1453
1454 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1455 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1456 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1457 if (ret < 0)
1458 return ret;
1459
1460 ct->status |= IPS_SEQ_ADJUST;
1461 }
1462
1463 return 0;
1464 }
1465 #endif
1466
1467 static int
1468 ctnetlink_change_conntrack(struct nf_conn *ct,
1469 const struct nlattr * const cda[])
1470 {
1471 int err;
1472
1473 /* only allow NAT changes and master assignation for new conntracks */
1474 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1475 return -EOPNOTSUPP;
1476
1477 if (cda[CTA_HELP]) {
1478 err = ctnetlink_change_helper(ct, cda);
1479 if (err < 0)
1480 return err;
1481 }
1482
1483 if (cda[CTA_TIMEOUT]) {
1484 err = ctnetlink_change_timeout(ct, cda);
1485 if (err < 0)
1486 return err;
1487 }
1488
1489 if (cda[CTA_STATUS]) {
1490 err = ctnetlink_change_status(ct, cda);
1491 if (err < 0)
1492 return err;
1493 }
1494
1495 if (cda[CTA_PROTOINFO]) {
1496 err = ctnetlink_change_protoinfo(ct, cda);
1497 if (err < 0)
1498 return err;
1499 }
1500
1501 #if defined(CONFIG_NF_CONNTRACK_MARK)
1502 if (cda[CTA_MARK])
1503 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1504 #endif
1505
1506 #ifdef CONFIG_NF_NAT_NEEDED
1507 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1508 err = ctnetlink_change_nat_seq_adj(ct, cda);
1509 if (err < 0)
1510 return err;
1511 }
1512 #endif
1513
1514 return 0;
1515 }
1516
1517 static struct nf_conn *
1518 ctnetlink_create_conntrack(struct net *net, u16 zone,
1519 const struct nlattr * const cda[],
1520 struct nf_conntrack_tuple *otuple,
1521 struct nf_conntrack_tuple *rtuple,
1522 u8 u3)
1523 {
1524 struct nf_conn *ct;
1525 int err = -EINVAL;
1526 struct nf_conntrack_helper *helper;
1527 struct nf_conn_tstamp *tstamp;
1528
1529 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1530 if (IS_ERR(ct))
1531 return ERR_PTR(-ENOMEM);
1532
1533 if (!cda[CTA_TIMEOUT])
1534 goto err1;
1535 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1536
1537 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1538
1539 rcu_read_lock();
1540 if (cda[CTA_HELP]) {
1541 char *helpname = NULL;
1542 struct nlattr *helpinfo = NULL;
1543
1544 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1545 if (err < 0)
1546 goto err2;
1547
1548 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1549 nf_ct_protonum(ct));
1550 if (helper == NULL) {
1551 rcu_read_unlock();
1552 #ifdef CONFIG_MODULES
1553 if (request_module("nfct-helper-%s", helpname) < 0) {
1554 err = -EOPNOTSUPP;
1555 goto err1;
1556 }
1557
1558 rcu_read_lock();
1559 helper = __nf_conntrack_helper_find(helpname,
1560 nf_ct_l3num(ct),
1561 nf_ct_protonum(ct));
1562 if (helper) {
1563 err = -EAGAIN;
1564 goto err2;
1565 }
1566 rcu_read_unlock();
1567 #endif
1568 err = -EOPNOTSUPP;
1569 goto err1;
1570 } else {
1571 struct nf_conn_help *help;
1572
1573 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1574 if (help == NULL) {
1575 err = -ENOMEM;
1576 goto err2;
1577 }
1578 /* set private helper data if allowed. */
1579 if (helper->from_nlattr)
1580 helper->from_nlattr(helpinfo, ct);
1581
1582 /* not in hash table yet so not strictly necessary */
1583 RCU_INIT_POINTER(help->helper, helper);
1584 }
1585 } else {
1586 /* try an implicit helper assignation */
1587 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1588 if (err < 0)
1589 goto err2;
1590 }
1591
1592 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1593 err = ctnetlink_change_nat(ct, cda);
1594 if (err < 0)
1595 goto err2;
1596 }
1597
1598 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1599 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1600 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1601 /* we must add conntrack extensions before confirmation. */
1602 ct->status |= IPS_CONFIRMED;
1603
1604 if (cda[CTA_STATUS]) {
1605 err = ctnetlink_change_status(ct, cda);
1606 if (err < 0)
1607 goto err2;
1608 }
1609
1610 #ifdef CONFIG_NF_NAT_NEEDED
1611 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1612 err = ctnetlink_change_nat_seq_adj(ct, cda);
1613 if (err < 0)
1614 goto err2;
1615 }
1616 #endif
1617
1618 memset(&ct->proto, 0, sizeof(ct->proto));
1619 if (cda[CTA_PROTOINFO]) {
1620 err = ctnetlink_change_protoinfo(ct, cda);
1621 if (err < 0)
1622 goto err2;
1623 }
1624
1625 #if defined(CONFIG_NF_CONNTRACK_MARK)
1626 if (cda[CTA_MARK])
1627 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1628 #endif
1629
1630 /* setup master conntrack: this is a confirmed expectation */
1631 if (cda[CTA_TUPLE_MASTER]) {
1632 struct nf_conntrack_tuple master;
1633 struct nf_conntrack_tuple_hash *master_h;
1634 struct nf_conn *master_ct;
1635
1636 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1637 if (err < 0)
1638 goto err2;
1639
1640 master_h = nf_conntrack_find_get(net, zone, &master);
1641 if (master_h == NULL) {
1642 err = -ENOENT;
1643 goto err2;
1644 }
1645 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1646 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1647 ct->master = master_ct;
1648 }
1649 tstamp = nf_conn_tstamp_find(ct);
1650 if (tstamp)
1651 tstamp->start = ktime_to_ns(ktime_get_real());
1652
1653 err = nf_conntrack_hash_check_insert(ct);
1654 if (err < 0)
1655 goto err2;
1656
1657 rcu_read_unlock();
1658
1659 return ct;
1660
1661 err2:
1662 rcu_read_unlock();
1663 err1:
1664 nf_conntrack_free(ct);
1665 return ERR_PTR(err);
1666 }
1667
1668 static int
1669 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1670 const struct nlmsghdr *nlh,
1671 const struct nlattr * const cda[])
1672 {
1673 struct net *net = sock_net(ctnl);
1674 struct nf_conntrack_tuple otuple, rtuple;
1675 struct nf_conntrack_tuple_hash *h = NULL;
1676 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1677 struct nf_conn *ct;
1678 u_int8_t u3 = nfmsg->nfgen_family;
1679 u16 zone;
1680 int err;
1681
1682 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1683 if (err < 0)
1684 return err;
1685
1686 if (cda[CTA_TUPLE_ORIG]) {
1687 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1688 if (err < 0)
1689 return err;
1690 }
1691
1692 if (cda[CTA_TUPLE_REPLY]) {
1693 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1694 if (err < 0)
1695 return err;
1696 }
1697
1698 if (cda[CTA_TUPLE_ORIG])
1699 h = nf_conntrack_find_get(net, zone, &otuple);
1700 else if (cda[CTA_TUPLE_REPLY])
1701 h = nf_conntrack_find_get(net, zone, &rtuple);
1702
1703 if (h == NULL) {
1704 err = -ENOENT;
1705 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1706 enum ip_conntrack_events events;
1707
1708 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1709 return -EINVAL;
1710
1711 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1712 &rtuple, u3);
1713 if (IS_ERR(ct))
1714 return PTR_ERR(ct);
1715
1716 err = 0;
1717 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1718 events = IPCT_RELATED;
1719 else
1720 events = IPCT_NEW;
1721
1722 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1723 (1 << IPCT_ASSURED) |
1724 (1 << IPCT_HELPER) |
1725 (1 << IPCT_PROTOINFO) |
1726 (1 << IPCT_NATSEQADJ) |
1727 (1 << IPCT_MARK) | events,
1728 ct, NETLINK_CB(skb).portid,
1729 nlmsg_report(nlh));
1730 nf_ct_put(ct);
1731 }
1732
1733 return err;
1734 }
1735 /* implicit 'else' */
1736
1737 err = -EEXIST;
1738 ct = nf_ct_tuplehash_to_ctrack(h);
1739 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1740 spin_lock_bh(&nf_conntrack_lock);
1741 err = ctnetlink_change_conntrack(ct, cda);
1742 spin_unlock_bh(&nf_conntrack_lock);
1743 if (err == 0) {
1744 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1745 (1 << IPCT_ASSURED) |
1746 (1 << IPCT_HELPER) |
1747 (1 << IPCT_PROTOINFO) |
1748 (1 << IPCT_NATSEQADJ) |
1749 (1 << IPCT_MARK),
1750 ct, NETLINK_CB(skb).portid,
1751 nlmsg_report(nlh));
1752 }
1753 }
1754
1755 nf_ct_put(ct);
1756 return err;
1757 }
1758
1759 static int
1760 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1761 __u16 cpu, const struct ip_conntrack_stat *st)
1762 {
1763 struct nlmsghdr *nlh;
1764 struct nfgenmsg *nfmsg;
1765 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1766
1767 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1768 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1769 if (nlh == NULL)
1770 goto nlmsg_failure;
1771
1772 nfmsg = nlmsg_data(nlh);
1773 nfmsg->nfgen_family = AF_UNSPEC;
1774 nfmsg->version = NFNETLINK_V0;
1775 nfmsg->res_id = htons(cpu);
1776
1777 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1778 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1779 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1780 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1781 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1782 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1783 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1784 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1785 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1786 htonl(st->insert_failed)) ||
1787 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1788 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1789 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1790 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1791 htonl(st->search_restart)))
1792 goto nla_put_failure;
1793
1794 nlmsg_end(skb, nlh);
1795 return skb->len;
1796
1797 nla_put_failure:
1798 nlmsg_failure:
1799 nlmsg_cancel(skb, nlh);
1800 return -1;
1801 }
1802
1803 static int
1804 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1805 {
1806 int cpu;
1807 struct net *net = sock_net(skb->sk);
1808
1809 if (cb->args[0] == nr_cpu_ids)
1810 return 0;
1811
1812 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1813 const struct ip_conntrack_stat *st;
1814
1815 if (!cpu_possible(cpu))
1816 continue;
1817
1818 st = per_cpu_ptr(net->ct.stat, cpu);
1819 if (ctnetlink_ct_stat_cpu_fill_info(skb,
1820 NETLINK_CB(cb->skb).portid,
1821 cb->nlh->nlmsg_seq,
1822 cpu, st) < 0)
1823 break;
1824 }
1825 cb->args[0] = cpu;
1826
1827 return skb->len;
1828 }
1829
1830 static int
1831 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1832 const struct nlmsghdr *nlh,
1833 const struct nlattr * const cda[])
1834 {
1835 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1836 struct netlink_dump_control c = {
1837 .dump = ctnetlink_ct_stat_cpu_dump,
1838 };
1839 return netlink_dump_start(ctnl, skb, nlh, &c);
1840 }
1841
1842 return 0;
1843 }
1844
1845 static int
1846 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1847 struct net *net)
1848 {
1849 struct nlmsghdr *nlh;
1850 struct nfgenmsg *nfmsg;
1851 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1852 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1853
1854 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1855 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1856 if (nlh == NULL)
1857 goto nlmsg_failure;
1858
1859 nfmsg = nlmsg_data(nlh);
1860 nfmsg->nfgen_family = AF_UNSPEC;
1861 nfmsg->version = NFNETLINK_V0;
1862 nfmsg->res_id = 0;
1863
1864 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1865 goto nla_put_failure;
1866
1867 nlmsg_end(skb, nlh);
1868 return skb->len;
1869
1870 nla_put_failure:
1871 nlmsg_failure:
1872 nlmsg_cancel(skb, nlh);
1873 return -1;
1874 }
1875
1876 static int
1877 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1878 const struct nlmsghdr *nlh,
1879 const struct nlattr * const cda[])
1880 {
1881 struct sk_buff *skb2;
1882 int err;
1883
1884 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1885 if (skb2 == NULL)
1886 return -ENOMEM;
1887
1888 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1889 nlh->nlmsg_seq,
1890 NFNL_MSG_TYPE(nlh->nlmsg_type),
1891 sock_net(skb->sk));
1892 if (err <= 0)
1893 goto free;
1894
1895 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1896 if (err < 0)
1897 goto out;
1898
1899 return 0;
1900
1901 free:
1902 kfree_skb(skb2);
1903 out:
1904 /* this avoids a loop in nfnetlink. */
1905 return err == -EAGAIN ? -ENOBUFS : err;
1906 }
1907
1908 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
1909 static size_t
1910 ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1911 {
1912 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1913 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1914 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1915 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1916 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1917 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
1918 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
1919 + nla_total_size(0) /* CTA_PROTOINFO */
1920 + nla_total_size(0) /* CTA_HELP */
1921 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
1922 + ctnetlink_secctx_size(ct)
1923 #ifdef CONFIG_NF_NAT_NEEDED
1924 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1925 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
1926 #endif
1927 #ifdef CONFIG_NF_CONNTRACK_MARK
1928 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
1929 #endif
1930 + ctnetlink_proto_size(ct)
1931 ;
1932 }
1933
1934 static int
1935 ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
1936 {
1937 struct nlattr *nest_parms;
1938
1939 rcu_read_lock();
1940 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
1941 if (!nest_parms)
1942 goto nla_put_failure;
1943 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
1944 goto nla_put_failure;
1945 nla_nest_end(skb, nest_parms);
1946
1947 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
1948 if (!nest_parms)
1949 goto nla_put_failure;
1950 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
1951 goto nla_put_failure;
1952 nla_nest_end(skb, nest_parms);
1953
1954 if (nf_ct_zone(ct)) {
1955 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
1956 goto nla_put_failure;
1957 }
1958
1959 if (ctnetlink_dump_id(skb, ct) < 0)
1960 goto nla_put_failure;
1961
1962 if (ctnetlink_dump_status(skb, ct) < 0)
1963 goto nla_put_failure;
1964
1965 if (ctnetlink_dump_timeout(skb, ct) < 0)
1966 goto nla_put_failure;
1967
1968 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
1969 goto nla_put_failure;
1970
1971 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
1972 goto nla_put_failure;
1973
1974 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1975 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
1976 goto nla_put_failure;
1977 #endif
1978 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
1979 goto nla_put_failure;
1980
1981 if ((ct->status & IPS_SEQ_ADJUST) &&
1982 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
1983 goto nla_put_failure;
1984
1985 #ifdef CONFIG_NF_CONNTRACK_MARK
1986 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
1987 goto nla_put_failure;
1988 #endif
1989 rcu_read_unlock();
1990 return 0;
1991
1992 nla_put_failure:
1993 rcu_read_unlock();
1994 return -ENOSPC;
1995 }
1996
1997 static int
1998 ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
1999 {
2000 int err;
2001
2002 if (cda[CTA_TIMEOUT]) {
2003 err = ctnetlink_change_timeout(ct, cda);
2004 if (err < 0)
2005 return err;
2006 }
2007 if (cda[CTA_STATUS]) {
2008 err = ctnetlink_change_status(ct, cda);
2009 if (err < 0)
2010 return err;
2011 }
2012 if (cda[CTA_HELP]) {
2013 err = ctnetlink_change_helper(ct, cda);
2014 if (err < 0)
2015 return err;
2016 }
2017 #if defined(CONFIG_NF_CONNTRACK_MARK)
2018 if (cda[CTA_MARK])
2019 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2020 #endif
2021 return 0;
2022 }
2023
2024 static int
2025 ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2026 {
2027 struct nlattr *cda[CTA_MAX+1];
2028 int ret;
2029
2030 nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2031
2032 spin_lock_bh(&nf_conntrack_lock);
2033 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2034 spin_unlock_bh(&nf_conntrack_lock);
2035
2036 return ret;
2037 }
2038
2039 static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2040 .build_size = ctnetlink_nfqueue_build_size,
2041 .build = ctnetlink_nfqueue_build,
2042 .parse = ctnetlink_nfqueue_parse,
2043 };
2044 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2045
2046 /***********************************************************************
2047 * EXPECT
2048 ***********************************************************************/
2049
2050 static inline int
2051 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2052 const struct nf_conntrack_tuple *tuple,
2053 enum ctattr_expect type)
2054 {
2055 struct nlattr *nest_parms;
2056
2057 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2058 if (!nest_parms)
2059 goto nla_put_failure;
2060 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2061 goto nla_put_failure;
2062 nla_nest_end(skb, nest_parms);
2063
2064 return 0;
2065
2066 nla_put_failure:
2067 return -1;
2068 }
2069
2070 static inline int
2071 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2072 const struct nf_conntrack_tuple *tuple,
2073 const struct nf_conntrack_tuple_mask *mask)
2074 {
2075 int ret;
2076 struct nf_conntrack_l3proto *l3proto;
2077 struct nf_conntrack_l4proto *l4proto;
2078 struct nf_conntrack_tuple m;
2079 struct nlattr *nest_parms;
2080
2081 memset(&m, 0xFF, sizeof(m));
2082 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2083 m.src.u.all = mask->src.u.all;
2084 m.dst.protonum = tuple->dst.protonum;
2085
2086 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2087 if (!nest_parms)
2088 goto nla_put_failure;
2089
2090 rcu_read_lock();
2091 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2092 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2093 if (ret >= 0) {
2094 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2095 tuple->dst.protonum);
2096 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2097 }
2098 rcu_read_unlock();
2099
2100 if (unlikely(ret < 0))
2101 goto nla_put_failure;
2102
2103 nla_nest_end(skb, nest_parms);
2104
2105 return 0;
2106
2107 nla_put_failure:
2108 return -1;
2109 }
2110
2111 static const union nf_inet_addr any_addr;
2112
2113 static int
2114 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2115 const struct nf_conntrack_expect *exp)
2116 {
2117 struct nf_conn *master = exp->master;
2118 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2119 struct nf_conn_help *help;
2120 #ifdef CONFIG_NF_NAT_NEEDED
2121 struct nlattr *nest_parms;
2122 struct nf_conntrack_tuple nat_tuple = {};
2123 #endif
2124 struct nf_ct_helper_expectfn *expfn;
2125
2126 if (timeout < 0)
2127 timeout = 0;
2128
2129 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2130 goto nla_put_failure;
2131 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2132 goto nla_put_failure;
2133 if (ctnetlink_exp_dump_tuple(skb,
2134 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2135 CTA_EXPECT_MASTER) < 0)
2136 goto nla_put_failure;
2137
2138 #ifdef CONFIG_NF_NAT_NEEDED
2139 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2140 exp->saved_proto.all) {
2141 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2142 if (!nest_parms)
2143 goto nla_put_failure;
2144
2145 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2146 goto nla_put_failure;
2147
2148 nat_tuple.src.l3num = nf_ct_l3num(master);
2149 nat_tuple.src.u3 = exp->saved_addr;
2150 nat_tuple.dst.protonum = nf_ct_protonum(master);
2151 nat_tuple.src.u = exp->saved_proto;
2152
2153 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2154 CTA_EXPECT_NAT_TUPLE) < 0)
2155 goto nla_put_failure;
2156 nla_nest_end(skb, nest_parms);
2157 }
2158 #endif
2159 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2160 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2161 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2162 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2163 goto nla_put_failure;
2164 help = nfct_help(master);
2165 if (help) {
2166 struct nf_conntrack_helper *helper;
2167
2168 helper = rcu_dereference(help->helper);
2169 if (helper &&
2170 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2171 goto nla_put_failure;
2172 }
2173 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2174 if (expfn != NULL &&
2175 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2176 goto nla_put_failure;
2177
2178 return 0;
2179
2180 nla_put_failure:
2181 return -1;
2182 }
2183
2184 static int
2185 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2186 int event, const struct nf_conntrack_expect *exp)
2187 {
2188 struct nlmsghdr *nlh;
2189 struct nfgenmsg *nfmsg;
2190 unsigned int flags = portid ? NLM_F_MULTI : 0;
2191
2192 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2193 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2194 if (nlh == NULL)
2195 goto nlmsg_failure;
2196
2197 nfmsg = nlmsg_data(nlh);
2198 nfmsg->nfgen_family = exp->tuple.src.l3num;
2199 nfmsg->version = NFNETLINK_V0;
2200 nfmsg->res_id = 0;
2201
2202 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2203 goto nla_put_failure;
2204
2205 nlmsg_end(skb, nlh);
2206 return skb->len;
2207
2208 nlmsg_failure:
2209 nla_put_failure:
2210 nlmsg_cancel(skb, nlh);
2211 return -1;
2212 }
2213
2214 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2215 static int
2216 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2217 {
2218 struct nf_conntrack_expect *exp = item->exp;
2219 struct net *net = nf_ct_exp_net(exp);
2220 struct nlmsghdr *nlh;
2221 struct nfgenmsg *nfmsg;
2222 struct sk_buff *skb;
2223 unsigned int type, group;
2224 int flags = 0;
2225
2226 if (events & (1 << IPEXP_DESTROY)) {
2227 type = IPCTNL_MSG_EXP_DELETE;
2228 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2229 } else if (events & (1 << IPEXP_NEW)) {
2230 type = IPCTNL_MSG_EXP_NEW;
2231 flags = NLM_F_CREATE|NLM_F_EXCL;
2232 group = NFNLGRP_CONNTRACK_EXP_NEW;
2233 } else
2234 return 0;
2235
2236 if (!item->report && !nfnetlink_has_listeners(net, group))
2237 return 0;
2238
2239 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2240 if (skb == NULL)
2241 goto errout;
2242
2243 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2244 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2245 if (nlh == NULL)
2246 goto nlmsg_failure;
2247
2248 nfmsg = nlmsg_data(nlh);
2249 nfmsg->nfgen_family = exp->tuple.src.l3num;
2250 nfmsg->version = NFNETLINK_V0;
2251 nfmsg->res_id = 0;
2252
2253 rcu_read_lock();
2254 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2255 goto nla_put_failure;
2256 rcu_read_unlock();
2257
2258 nlmsg_end(skb, nlh);
2259 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2260 return 0;
2261
2262 nla_put_failure:
2263 rcu_read_unlock();
2264 nlmsg_cancel(skb, nlh);
2265 nlmsg_failure:
2266 kfree_skb(skb);
2267 errout:
2268 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2269 return 0;
2270 }
2271 #endif
2272 static int ctnetlink_exp_done(struct netlink_callback *cb)
2273 {
2274 if (cb->args[1])
2275 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2276 return 0;
2277 }
2278
2279 static int
2280 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2281 {
2282 struct net *net = sock_net(skb->sk);
2283 struct nf_conntrack_expect *exp, *last;
2284 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2285 struct hlist_node *n;
2286 u_int8_t l3proto = nfmsg->nfgen_family;
2287
2288 rcu_read_lock();
2289 last = (struct nf_conntrack_expect *)cb->args[1];
2290 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2291 restart:
2292 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
2293 hnode) {
2294 if (l3proto && exp->tuple.src.l3num != l3proto)
2295 continue;
2296 if (cb->args[1]) {
2297 if (exp != last)
2298 continue;
2299 cb->args[1] = 0;
2300 }
2301 if (ctnetlink_exp_fill_info(skb,
2302 NETLINK_CB(cb->skb).portid,
2303 cb->nlh->nlmsg_seq,
2304 IPCTNL_MSG_EXP_NEW,
2305 exp) < 0) {
2306 if (!atomic_inc_not_zero(&exp->use))
2307 continue;
2308 cb->args[1] = (unsigned long)exp;
2309 goto out;
2310 }
2311 }
2312 if (cb->args[1]) {
2313 cb->args[1] = 0;
2314 goto restart;
2315 }
2316 }
2317 out:
2318 rcu_read_unlock();
2319 if (last)
2320 nf_ct_expect_put(last);
2321
2322 return skb->len;
2323 }
2324
2325 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2326 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2327 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2328 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2329 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2330 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2331 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2332 .len = NF_CT_HELPER_NAME_LEN - 1 },
2333 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2334 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2335 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2336 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2337 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2338 };
2339
2340 static int
2341 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2342 const struct nlmsghdr *nlh,
2343 const struct nlattr * const cda[])
2344 {
2345 struct net *net = sock_net(ctnl);
2346 struct nf_conntrack_tuple tuple;
2347 struct nf_conntrack_expect *exp;
2348 struct sk_buff *skb2;
2349 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2350 u_int8_t u3 = nfmsg->nfgen_family;
2351 u16 zone;
2352 int err;
2353
2354 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2355 struct netlink_dump_control c = {
2356 .dump = ctnetlink_exp_dump_table,
2357 .done = ctnetlink_exp_done,
2358 };
2359 return netlink_dump_start(ctnl, skb, nlh, &c);
2360 }
2361
2362 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2363 if (err < 0)
2364 return err;
2365
2366 if (cda[CTA_EXPECT_TUPLE])
2367 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2368 else if (cda[CTA_EXPECT_MASTER])
2369 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2370 else
2371 return -EINVAL;
2372
2373 if (err < 0)
2374 return err;
2375
2376 exp = nf_ct_expect_find_get(net, zone, &tuple);
2377 if (!exp)
2378 return -ENOENT;
2379
2380 if (cda[CTA_EXPECT_ID]) {
2381 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2382 if (ntohl(id) != (u32)(unsigned long)exp) {
2383 nf_ct_expect_put(exp);
2384 return -ENOENT;
2385 }
2386 }
2387
2388 err = -ENOMEM;
2389 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2390 if (skb2 == NULL) {
2391 nf_ct_expect_put(exp);
2392 goto out;
2393 }
2394
2395 rcu_read_lock();
2396 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2397 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2398 rcu_read_unlock();
2399 nf_ct_expect_put(exp);
2400 if (err <= 0)
2401 goto free;
2402
2403 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2404 if (err < 0)
2405 goto out;
2406
2407 return 0;
2408
2409 free:
2410 kfree_skb(skb2);
2411 out:
2412 /* this avoids a loop in nfnetlink. */
2413 return err == -EAGAIN ? -ENOBUFS : err;
2414 }
2415
2416 static int
2417 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2418 const struct nlmsghdr *nlh,
2419 const struct nlattr * const cda[])
2420 {
2421 struct net *net = sock_net(ctnl);
2422 struct nf_conntrack_expect *exp;
2423 struct nf_conntrack_tuple tuple;
2424 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2425 struct hlist_node *n, *next;
2426 u_int8_t u3 = nfmsg->nfgen_family;
2427 unsigned int i;
2428 u16 zone;
2429 int err;
2430
2431 if (cda[CTA_EXPECT_TUPLE]) {
2432 /* delete a single expect by tuple */
2433 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2434 if (err < 0)
2435 return err;
2436
2437 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2438 if (err < 0)
2439 return err;
2440
2441 /* bump usage count to 2 */
2442 exp = nf_ct_expect_find_get(net, zone, &tuple);
2443 if (!exp)
2444 return -ENOENT;
2445
2446 if (cda[CTA_EXPECT_ID]) {
2447 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2448 if (ntohl(id) != (u32)(unsigned long)exp) {
2449 nf_ct_expect_put(exp);
2450 return -ENOENT;
2451 }
2452 }
2453
2454 /* after list removal, usage count == 1 */
2455 spin_lock_bh(&nf_conntrack_lock);
2456 if (del_timer(&exp->timeout)) {
2457 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2458 nlmsg_report(nlh));
2459 nf_ct_expect_put(exp);
2460 }
2461 spin_unlock_bh(&nf_conntrack_lock);
2462 /* have to put what we 'get' above.
2463 * after this line usage count == 0 */
2464 nf_ct_expect_put(exp);
2465 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2466 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2467 struct nf_conn_help *m_help;
2468
2469 /* delete all expectations for this helper */
2470 spin_lock_bh(&nf_conntrack_lock);
2471 for (i = 0; i < nf_ct_expect_hsize; i++) {
2472 hlist_for_each_entry_safe(exp, n, next,
2473 &net->ct.expect_hash[i],
2474 hnode) {
2475 m_help = nfct_help(exp->master);
2476 if (!strcmp(m_help->helper->name, name) &&
2477 del_timer(&exp->timeout)) {
2478 nf_ct_unlink_expect_report(exp,
2479 NETLINK_CB(skb).portid,
2480 nlmsg_report(nlh));
2481 nf_ct_expect_put(exp);
2482 }
2483 }
2484 }
2485 spin_unlock_bh(&nf_conntrack_lock);
2486 } else {
2487 /* This basically means we have to flush everything*/
2488 spin_lock_bh(&nf_conntrack_lock);
2489 for (i = 0; i < nf_ct_expect_hsize; i++) {
2490 hlist_for_each_entry_safe(exp, n, next,
2491 &net->ct.expect_hash[i],
2492 hnode) {
2493 if (del_timer(&exp->timeout)) {
2494 nf_ct_unlink_expect_report(exp,
2495 NETLINK_CB(skb).portid,
2496 nlmsg_report(nlh));
2497 nf_ct_expect_put(exp);
2498 }
2499 }
2500 }
2501 spin_unlock_bh(&nf_conntrack_lock);
2502 }
2503
2504 return 0;
2505 }
2506 static int
2507 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2508 const struct nlattr * const cda[])
2509 {
2510 if (cda[CTA_EXPECT_TIMEOUT]) {
2511 if (!del_timer(&x->timeout))
2512 return -ETIME;
2513
2514 x->timeout.expires = jiffies +
2515 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2516 add_timer(&x->timeout);
2517 }
2518 return 0;
2519 }
2520
2521 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2522 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2523 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2524 };
2525
2526 static int
2527 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2528 struct nf_conntrack_expect *exp,
2529 u_int8_t u3)
2530 {
2531 #ifdef CONFIG_NF_NAT_NEEDED
2532 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2533 struct nf_conntrack_tuple nat_tuple = {};
2534 int err;
2535
2536 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2537
2538 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2539 return -EINVAL;
2540
2541 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2542 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2543 if (err < 0)
2544 return err;
2545
2546 exp->saved_addr = nat_tuple.src.u3;
2547 exp->saved_proto = nat_tuple.src.u;
2548 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2549
2550 return 0;
2551 #else
2552 return -EOPNOTSUPP;
2553 #endif
2554 }
2555
2556 static int
2557 ctnetlink_create_expect(struct net *net, u16 zone,
2558 const struct nlattr * const cda[],
2559 u_int8_t u3,
2560 u32 portid, int report)
2561 {
2562 struct nf_conntrack_tuple tuple, mask, master_tuple;
2563 struct nf_conntrack_tuple_hash *h = NULL;
2564 struct nf_conntrack_expect *exp;
2565 struct nf_conn *ct;
2566 struct nf_conn_help *help;
2567 struct nf_conntrack_helper *helper = NULL;
2568 u_int32_t class = 0;
2569 int err = 0;
2570
2571 /* caller guarantees that those three CTA_EXPECT_* exist */
2572 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2573 if (err < 0)
2574 return err;
2575 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2576 if (err < 0)
2577 return err;
2578 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2579 if (err < 0)
2580 return err;
2581
2582 /* Look for master conntrack of this expectation */
2583 h = nf_conntrack_find_get(net, zone, &master_tuple);
2584 if (!h)
2585 return -ENOENT;
2586 ct = nf_ct_tuplehash_to_ctrack(h);
2587
2588 /* Look for helper of this expectation */
2589 if (cda[CTA_EXPECT_HELP_NAME]) {
2590 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2591
2592 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2593 nf_ct_protonum(ct));
2594 if (helper == NULL) {
2595 #ifdef CONFIG_MODULES
2596 if (request_module("nfct-helper-%s", helpname) < 0) {
2597 err = -EOPNOTSUPP;
2598 goto out;
2599 }
2600
2601 helper = __nf_conntrack_helper_find(helpname,
2602 nf_ct_l3num(ct),
2603 nf_ct_protonum(ct));
2604 if (helper) {
2605 err = -EAGAIN;
2606 goto out;
2607 }
2608 #endif
2609 err = -EOPNOTSUPP;
2610 goto out;
2611 }
2612 }
2613
2614 if (cda[CTA_EXPECT_CLASS] && helper) {
2615 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2616 if (class > helper->expect_class_max) {
2617 err = -EINVAL;
2618 goto out;
2619 }
2620 }
2621 exp = nf_ct_expect_alloc(ct);
2622 if (!exp) {
2623 err = -ENOMEM;
2624 goto out;
2625 }
2626 help = nfct_help(ct);
2627 if (!help) {
2628 if (!cda[CTA_EXPECT_TIMEOUT]) {
2629 err = -EINVAL;
2630 goto err_out;
2631 }
2632 exp->timeout.expires =
2633 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2634
2635 exp->flags = NF_CT_EXPECT_USERSPACE;
2636 if (cda[CTA_EXPECT_FLAGS]) {
2637 exp->flags |=
2638 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2639 }
2640 } else {
2641 if (cda[CTA_EXPECT_FLAGS]) {
2642 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2643 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2644 } else
2645 exp->flags = 0;
2646 }
2647 if (cda[CTA_EXPECT_FN]) {
2648 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2649 struct nf_ct_helper_expectfn *expfn;
2650
2651 expfn = nf_ct_helper_expectfn_find_by_name(name);
2652 if (expfn == NULL) {
2653 err = -EINVAL;
2654 goto err_out;
2655 }
2656 exp->expectfn = expfn->expectfn;
2657 } else
2658 exp->expectfn = NULL;
2659
2660 exp->class = class;
2661 exp->master = ct;
2662 exp->helper = helper;
2663 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2664 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2665 exp->mask.src.u.all = mask.src.u.all;
2666
2667 if (cda[CTA_EXPECT_NAT]) {
2668 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2669 exp, u3);
2670 if (err < 0)
2671 goto err_out;
2672 }
2673 err = nf_ct_expect_related_report(exp, portid, report);
2674 err_out:
2675 nf_ct_expect_put(exp);
2676 out:
2677 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2678 return err;
2679 }
2680
2681 static int
2682 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2683 const struct nlmsghdr *nlh,
2684 const struct nlattr * const cda[])
2685 {
2686 struct net *net = sock_net(ctnl);
2687 struct nf_conntrack_tuple tuple;
2688 struct nf_conntrack_expect *exp;
2689 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2690 u_int8_t u3 = nfmsg->nfgen_family;
2691 u16 zone;
2692 int err;
2693
2694 if (!cda[CTA_EXPECT_TUPLE]
2695 || !cda[CTA_EXPECT_MASK]
2696 || !cda[CTA_EXPECT_MASTER])
2697 return -EINVAL;
2698
2699 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2700 if (err < 0)
2701 return err;
2702
2703 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2704 if (err < 0)
2705 return err;
2706
2707 spin_lock_bh(&nf_conntrack_lock);
2708 exp = __nf_ct_expect_find(net, zone, &tuple);
2709
2710 if (!exp) {
2711 spin_unlock_bh(&nf_conntrack_lock);
2712 err = -ENOENT;
2713 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2714 err = ctnetlink_create_expect(net, zone, cda,
2715 u3,
2716 NETLINK_CB(skb).portid,
2717 nlmsg_report(nlh));
2718 }
2719 return err;
2720 }
2721
2722 err = -EEXIST;
2723 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2724 err = ctnetlink_change_expect(exp, cda);
2725 spin_unlock_bh(&nf_conntrack_lock);
2726
2727 return err;
2728 }
2729
2730 static int
2731 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2732 const struct ip_conntrack_stat *st)
2733 {
2734 struct nlmsghdr *nlh;
2735 struct nfgenmsg *nfmsg;
2736 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2737
2738 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2739 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2740 if (nlh == NULL)
2741 goto nlmsg_failure;
2742
2743 nfmsg = nlmsg_data(nlh);
2744 nfmsg->nfgen_family = AF_UNSPEC;
2745 nfmsg->version = NFNETLINK_V0;
2746 nfmsg->res_id = htons(cpu);
2747
2748 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2749 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2750 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2751 goto nla_put_failure;
2752
2753 nlmsg_end(skb, nlh);
2754 return skb->len;
2755
2756 nla_put_failure:
2757 nlmsg_failure:
2758 nlmsg_cancel(skb, nlh);
2759 return -1;
2760 }
2761
2762 static int
2763 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2764 {
2765 int cpu;
2766 struct net *net = sock_net(skb->sk);
2767
2768 if (cb->args[0] == nr_cpu_ids)
2769 return 0;
2770
2771 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2772 const struct ip_conntrack_stat *st;
2773
2774 if (!cpu_possible(cpu))
2775 continue;
2776
2777 st = per_cpu_ptr(net->ct.stat, cpu);
2778 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
2779 cb->nlh->nlmsg_seq,
2780 cpu, st) < 0)
2781 break;
2782 }
2783 cb->args[0] = cpu;
2784
2785 return skb->len;
2786 }
2787
2788 static int
2789 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2790 const struct nlmsghdr *nlh,
2791 const struct nlattr * const cda[])
2792 {
2793 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2794 struct netlink_dump_control c = {
2795 .dump = ctnetlink_exp_stat_cpu_dump,
2796 };
2797 return netlink_dump_start(ctnl, skb, nlh, &c);
2798 }
2799
2800 return 0;
2801 }
2802
2803 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2804 static struct nf_ct_event_notifier ctnl_notifier = {
2805 .fcn = ctnetlink_conntrack_event,
2806 };
2807
2808 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2809 .fcn = ctnetlink_expect_event,
2810 };
2811 #endif
2812
2813 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2814 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2815 .attr_count = CTA_MAX,
2816 .policy = ct_nla_policy },
2817 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2818 .attr_count = CTA_MAX,
2819 .policy = ct_nla_policy },
2820 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2821 .attr_count = CTA_MAX,
2822 .policy = ct_nla_policy },
2823 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2824 .attr_count = CTA_MAX,
2825 .policy = ct_nla_policy },
2826 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
2827 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
2828 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
2829 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
2830 };
2831
2832 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2833 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2834 .attr_count = CTA_EXPECT_MAX,
2835 .policy = exp_nla_policy },
2836 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2837 .attr_count = CTA_EXPECT_MAX,
2838 .policy = exp_nla_policy },
2839 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2840 .attr_count = CTA_EXPECT_MAX,
2841 .policy = exp_nla_policy },
2842 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
2843 };
2844
2845 static const struct nfnetlink_subsystem ctnl_subsys = {
2846 .name = "conntrack",
2847 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2848 .cb_count = IPCTNL_MSG_MAX,
2849 .cb = ctnl_cb,
2850 };
2851
2852 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2853 .name = "conntrack_expect",
2854 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2855 .cb_count = IPCTNL_MSG_EXP_MAX,
2856 .cb = ctnl_exp_cb,
2857 };
2858
2859 MODULE_ALIAS("ip_conntrack_netlink");
2860 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2861 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2862
2863 static int __net_init ctnetlink_net_init(struct net *net)
2864 {
2865 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2866 int ret;
2867
2868 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2869 if (ret < 0) {
2870 pr_err("ctnetlink_init: cannot register notifier.\n");
2871 goto err_out;
2872 }
2873
2874 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2875 if (ret < 0) {
2876 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2877 goto err_unreg_notifier;
2878 }
2879 #endif
2880 return 0;
2881
2882 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2883 err_unreg_notifier:
2884 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2885 err_out:
2886 return ret;
2887 #endif
2888 }
2889
2890 static void ctnetlink_net_exit(struct net *net)
2891 {
2892 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2893 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2894 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2895 #endif
2896 }
2897
2898 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2899 {
2900 struct net *net;
2901
2902 list_for_each_entry(net, net_exit_list, exit_list)
2903 ctnetlink_net_exit(net);
2904 }
2905
2906 static struct pernet_operations ctnetlink_net_ops = {
2907 .init = ctnetlink_net_init,
2908 .exit_batch = ctnetlink_net_exit_batch,
2909 };
2910
2911 static int __init ctnetlink_init(void)
2912 {
2913 int ret;
2914
2915 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2916 ret = nfnetlink_subsys_register(&ctnl_subsys);
2917 if (ret < 0) {
2918 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2919 goto err_out;
2920 }
2921
2922 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2923 if (ret < 0) {
2924 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2925 goto err_unreg_subsys;
2926 }
2927
2928 ret = register_pernet_subsys(&ctnetlink_net_ops);
2929 if (ret < 0) {
2930 pr_err("ctnetlink_init: cannot register pernet operations\n");
2931 goto err_unreg_exp_subsys;
2932 }
2933 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2934 /* setup interaction between nf_queue and nf_conntrack_netlink. */
2935 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
2936 #endif
2937 return 0;
2938
2939 err_unreg_exp_subsys:
2940 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2941 err_unreg_subsys:
2942 nfnetlink_subsys_unregister(&ctnl_subsys);
2943 err_out:
2944 return ret;
2945 }
2946
2947 static void __exit ctnetlink_exit(void)
2948 {
2949 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2950
2951 unregister_pernet_subsys(&ctnetlink_net_ops);
2952 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2953 nfnetlink_subsys_unregister(&ctnl_subsys);
2954 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2955 RCU_INIT_POINTER(nfq_ct_hook, NULL);
2956 #endif
2957 }
2958
2959 module_init(ctnetlink_init);
2960 module_exit(ctnetlink_exit);
This page took 0.140397 seconds and 5 git commands to generate.