]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_netlink.c
netfilter: conntrack: remove prealloc support
[mirror_ubuntu-artful-kernel.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_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #ifdef CONFIG_NF_NAT_NEEDED
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_l4proto.h>
51 #include <net/netfilter/nf_nat_helper.h>
52 #endif
53
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
56
57 MODULE_LICENSE("GPL");
58
59 static char __initdata version[] = "0.93";
60
61 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
62 const struct nf_conntrack_tuple *tuple,
63 struct nf_conntrack_l4proto *l4proto)
64 {
65 int ret = 0;
66 struct nlattr *nest_parms;
67
68 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
69 if (!nest_parms)
70 goto nla_put_failure;
71 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
72 goto nla_put_failure;
73
74 if (likely(l4proto->tuple_to_nlattr))
75 ret = l4proto->tuple_to_nlattr(skb, tuple);
76
77 nla_nest_end(skb, nest_parms);
78
79 return ret;
80
81 nla_put_failure:
82 return -1;
83 }
84
85 static int 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 ctnetlink_dump_tuples(struct sk_buff *skb,
108 const struct nf_conntrack_tuple *tuple)
109 {
110 int ret;
111 struct nf_conntrack_l3proto *l3proto;
112 struct nf_conntrack_l4proto *l4proto;
113
114 rcu_read_lock();
115 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
116 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
117
118 if (ret >= 0) {
119 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
120 tuple->dst.protonum);
121 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
122 }
123 rcu_read_unlock();
124 return ret;
125 }
126
127 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
128 const struct nf_conntrack_zone *zone, int dir)
129 {
130 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
131 return 0;
132 if (nla_put_be16(skb, attrtype, htons(zone->id)))
133 goto nla_put_failure;
134 return 0;
135
136 nla_put_failure:
137 return -1;
138 }
139
140 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
141 {
142 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
143 goto nla_put_failure;
144 return 0;
145
146 nla_put_failure:
147 return -1;
148 }
149
150 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
151 {
152 long timeout = nf_ct_expires(ct) / HZ;
153
154 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
155 goto nla_put_failure;
156 return 0;
157
158 nla_put_failure:
159 return -1;
160 }
161
162 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
163 {
164 struct nf_conntrack_l4proto *l4proto;
165 struct nlattr *nest_proto;
166 int ret;
167
168 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
169 if (!l4proto->to_nlattr)
170 return 0;
171
172 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
173 if (!nest_proto)
174 goto nla_put_failure;
175
176 ret = l4proto->to_nlattr(skb, nest_proto, ct);
177
178 nla_nest_end(skb, nest_proto);
179
180 return ret;
181
182 nla_put_failure:
183 return -1;
184 }
185
186 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
187 const struct nf_conn *ct)
188 {
189 struct nlattr *nest_helper;
190 const struct nf_conn_help *help = nfct_help(ct);
191 struct nf_conntrack_helper *helper;
192
193 if (!help)
194 return 0;
195
196 helper = rcu_dereference(help->helper);
197 if (!helper)
198 goto out;
199
200 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
201 if (!nest_helper)
202 goto nla_put_failure;
203 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
204 goto nla_put_failure;
205
206 if (helper->to_nlattr)
207 helper->to_nlattr(skb, ct);
208
209 nla_nest_end(skb, nest_helper);
210 out:
211 return 0;
212
213 nla_put_failure:
214 return -1;
215 }
216
217 static int
218 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
219 enum ip_conntrack_dir dir, int type)
220 {
221 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
222 struct nf_conn_counter *counter = acct->counter;
223 struct nlattr *nest_count;
224 u64 pkts, bytes;
225
226 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
227 pkts = atomic64_xchg(&counter[dir].packets, 0);
228 bytes = atomic64_xchg(&counter[dir].bytes, 0);
229 } else {
230 pkts = atomic64_read(&counter[dir].packets);
231 bytes = atomic64_read(&counter[dir].bytes);
232 }
233
234 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
235 if (!nest_count)
236 goto nla_put_failure;
237
238 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
239 CTA_COUNTERS_PAD) ||
240 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
241 CTA_COUNTERS_PAD))
242 goto nla_put_failure;
243
244 nla_nest_end(skb, nest_count);
245
246 return 0;
247
248 nla_put_failure:
249 return -1;
250 }
251
252 static int
253 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
254 {
255 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
256
257 if (!acct)
258 return 0;
259
260 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
261 return -1;
262 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
263 return -1;
264
265 return 0;
266 }
267
268 static int
269 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
270 {
271 struct nlattr *nest_count;
272 const struct nf_conn_tstamp *tstamp;
273
274 tstamp = nf_conn_tstamp_find(ct);
275 if (!tstamp)
276 return 0;
277
278 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
279 if (!nest_count)
280 goto nla_put_failure;
281
282 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
283 CTA_TIMESTAMP_PAD) ||
284 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
285 cpu_to_be64(tstamp->stop),
286 CTA_TIMESTAMP_PAD)))
287 goto nla_put_failure;
288 nla_nest_end(skb, nest_count);
289
290 return 0;
291
292 nla_put_failure:
293 return -1;
294 }
295
296 #ifdef CONFIG_NF_CONNTRACK_MARK
297 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
298 {
299 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
300 goto nla_put_failure;
301 return 0;
302
303 nla_put_failure:
304 return -1;
305 }
306 #else
307 #define ctnetlink_dump_mark(a, b) (0)
308 #endif
309
310 #ifdef CONFIG_NF_CONNTRACK_SECMARK
311 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
312 {
313 struct nlattr *nest_secctx;
314 int len, ret;
315 char *secctx;
316
317 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
318 if (ret)
319 return 0;
320
321 ret = -1;
322 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
323 if (!nest_secctx)
324 goto nla_put_failure;
325
326 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
327 goto nla_put_failure;
328 nla_nest_end(skb, nest_secctx);
329
330 ret = 0;
331 nla_put_failure:
332 security_release_secctx(secctx, len);
333 return ret;
334 }
335 #else
336 #define ctnetlink_dump_secctx(a, b) (0)
337 #endif
338
339 #ifdef CONFIG_NF_CONNTRACK_LABELS
340 static inline int ctnetlink_label_size(const struct nf_conn *ct)
341 {
342 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
343
344 if (!labels)
345 return 0;
346 return nla_total_size(sizeof(labels->bits));
347 }
348
349 static int
350 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
351 {
352 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
353 unsigned int i;
354
355 if (!labels)
356 return 0;
357
358 i = 0;
359 do {
360 if (labels->bits[i] != 0)
361 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
362 labels->bits);
363 i++;
364 } while (i < ARRAY_SIZE(labels->bits));
365
366 return 0;
367 }
368 #else
369 #define ctnetlink_dump_labels(a, b) (0)
370 #define ctnetlink_label_size(a) (0)
371 #endif
372
373 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
374
375 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
376 {
377 struct nlattr *nest_parms;
378
379 if (!(ct->status & IPS_EXPECTED))
380 return 0;
381
382 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
383 if (!nest_parms)
384 goto nla_put_failure;
385 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
386 goto nla_put_failure;
387 nla_nest_end(skb, nest_parms);
388
389 return 0;
390
391 nla_put_failure:
392 return -1;
393 }
394
395 static int
396 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
397 {
398 struct nlattr *nest_parms;
399
400 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
401 if (!nest_parms)
402 goto nla_put_failure;
403
404 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
405 htonl(seq->correction_pos)) ||
406 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
407 htonl(seq->offset_before)) ||
408 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
409 htonl(seq->offset_after)))
410 goto nla_put_failure;
411
412 nla_nest_end(skb, nest_parms);
413
414 return 0;
415
416 nla_put_failure:
417 return -1;
418 }
419
420 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb,
421 const struct nf_conn *ct)
422 {
423 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
424 struct nf_ct_seqadj *seq;
425
426 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
427 return 0;
428
429 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
430 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
431 return -1;
432
433 seq = &seqadj->seq[IP_CT_DIR_REPLY];
434 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
435 return -1;
436
437 return 0;
438 }
439
440 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
441 {
442 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
443 goto nla_put_failure;
444 return 0;
445
446 nla_put_failure:
447 return -1;
448 }
449
450 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
451 {
452 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
453 goto nla_put_failure;
454 return 0;
455
456 nla_put_failure:
457 return -1;
458 }
459
460 static int
461 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
462 struct nf_conn *ct)
463 {
464 const struct nf_conntrack_zone *zone;
465 struct nlmsghdr *nlh;
466 struct nfgenmsg *nfmsg;
467 struct nlattr *nest_parms;
468 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
469
470 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
471 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
472 if (nlh == NULL)
473 goto nlmsg_failure;
474
475 nfmsg = nlmsg_data(nlh);
476 nfmsg->nfgen_family = nf_ct_l3num(ct);
477 nfmsg->version = NFNETLINK_V0;
478 nfmsg->res_id = 0;
479
480 zone = nf_ct_zone(ct);
481
482 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
483 if (!nest_parms)
484 goto nla_put_failure;
485 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
486 goto nla_put_failure;
487 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
488 NF_CT_ZONE_DIR_ORIG) < 0)
489 goto nla_put_failure;
490 nla_nest_end(skb, nest_parms);
491
492 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
493 if (!nest_parms)
494 goto nla_put_failure;
495 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
496 goto nla_put_failure;
497 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
498 NF_CT_ZONE_DIR_REPL) < 0)
499 goto nla_put_failure;
500 nla_nest_end(skb, nest_parms);
501
502 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
503 NF_CT_DEFAULT_ZONE_DIR) < 0)
504 goto nla_put_failure;
505
506 if (ctnetlink_dump_status(skb, ct) < 0 ||
507 ctnetlink_dump_timeout(skb, ct) < 0 ||
508 ctnetlink_dump_acct(skb, ct, type) < 0 ||
509 ctnetlink_dump_timestamp(skb, ct) < 0 ||
510 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
511 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
512 ctnetlink_dump_mark(skb, ct) < 0 ||
513 ctnetlink_dump_secctx(skb, ct) < 0 ||
514 ctnetlink_dump_labels(skb, ct) < 0 ||
515 ctnetlink_dump_id(skb, ct) < 0 ||
516 ctnetlink_dump_use(skb, ct) < 0 ||
517 ctnetlink_dump_master(skb, ct) < 0 ||
518 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
519 goto nla_put_failure;
520
521 nlmsg_end(skb, nlh);
522 return skb->len;
523
524 nlmsg_failure:
525 nla_put_failure:
526 nlmsg_cancel(skb, nlh);
527 return -1;
528 }
529
530 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
531 {
532 struct nf_conntrack_l3proto *l3proto;
533 struct nf_conntrack_l4proto *l4proto;
534 size_t len = 0;
535
536 rcu_read_lock();
537 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
538 len += l3proto->nla_size;
539
540 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
541 len += l4proto->nla_size;
542 rcu_read_unlock();
543
544 return len;
545 }
546
547 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
548 {
549 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
550 return 0;
551 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
552 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
553 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
554 ;
555 }
556
557 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
558 {
559 #ifdef CONFIG_NF_CONNTRACK_SECMARK
560 int len, ret;
561
562 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
563 if (ret)
564 return 0;
565
566 return nla_total_size(0) /* CTA_SECCTX */
567 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
568 #else
569 return 0;
570 #endif
571 }
572
573 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
574 {
575 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
576 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
577 return 0;
578 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
579 #else
580 return 0;
581 #endif
582 }
583
584 #ifdef CONFIG_NF_CONNTRACK_EVENTS
585 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
586 {
587 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
588 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
589 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
590 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
591 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
592 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
593 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
594 + ctnetlink_acct_size(ct)
595 + ctnetlink_timestamp_size(ct)
596 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
597 + nla_total_size(0) /* CTA_PROTOINFO */
598 + nla_total_size(0) /* CTA_HELP */
599 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
600 + ctnetlink_secctx_size(ct)
601 #ifdef CONFIG_NF_NAT_NEEDED
602 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
603 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
604 #endif
605 #ifdef CONFIG_NF_CONNTRACK_MARK
606 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
607 #endif
608 #ifdef CONFIG_NF_CONNTRACK_ZONES
609 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
610 #endif
611 + ctnetlink_proto_size(ct)
612 + ctnetlink_label_size(ct)
613 ;
614 }
615
616 static int
617 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
618 {
619 const struct nf_conntrack_zone *zone;
620 struct net *net;
621 struct nlmsghdr *nlh;
622 struct nfgenmsg *nfmsg;
623 struct nlattr *nest_parms;
624 struct nf_conn *ct = item->ct;
625 struct sk_buff *skb;
626 unsigned int type;
627 unsigned int flags = 0, group;
628 int err;
629
630 if (events & (1 << IPCT_DESTROY)) {
631 type = IPCTNL_MSG_CT_DELETE;
632 group = NFNLGRP_CONNTRACK_DESTROY;
633 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
634 type = IPCTNL_MSG_CT_NEW;
635 flags = NLM_F_CREATE|NLM_F_EXCL;
636 group = NFNLGRP_CONNTRACK_NEW;
637 } else if (events) {
638 type = IPCTNL_MSG_CT_NEW;
639 group = NFNLGRP_CONNTRACK_UPDATE;
640 } else
641 return 0;
642
643 net = nf_ct_net(ct);
644 if (!item->report && !nfnetlink_has_listeners(net, group))
645 return 0;
646
647 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
648 if (skb == NULL)
649 goto errout;
650
651 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
652 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
653 if (nlh == NULL)
654 goto nlmsg_failure;
655
656 nfmsg = nlmsg_data(nlh);
657 nfmsg->nfgen_family = nf_ct_l3num(ct);
658 nfmsg->version = NFNETLINK_V0;
659 nfmsg->res_id = 0;
660
661 rcu_read_lock();
662 zone = nf_ct_zone(ct);
663
664 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
665 if (!nest_parms)
666 goto nla_put_failure;
667 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
668 goto nla_put_failure;
669 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
670 NF_CT_ZONE_DIR_ORIG) < 0)
671 goto nla_put_failure;
672 nla_nest_end(skb, nest_parms);
673
674 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
675 if (!nest_parms)
676 goto nla_put_failure;
677 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
678 goto nla_put_failure;
679 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
680 NF_CT_ZONE_DIR_REPL) < 0)
681 goto nla_put_failure;
682 nla_nest_end(skb, nest_parms);
683
684 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
685 NF_CT_DEFAULT_ZONE_DIR) < 0)
686 goto nla_put_failure;
687
688 if (ctnetlink_dump_id(skb, ct) < 0)
689 goto nla_put_failure;
690
691 if (ctnetlink_dump_status(skb, ct) < 0)
692 goto nla_put_failure;
693
694 if (events & (1 << IPCT_DESTROY)) {
695 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
696 ctnetlink_dump_timestamp(skb, ct) < 0)
697 goto nla_put_failure;
698 } else {
699 if (ctnetlink_dump_timeout(skb, ct) < 0)
700 goto nla_put_failure;
701
702 if (events & (1 << IPCT_PROTOINFO)
703 && ctnetlink_dump_protoinfo(skb, ct) < 0)
704 goto nla_put_failure;
705
706 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
707 && ctnetlink_dump_helpinfo(skb, ct) < 0)
708 goto nla_put_failure;
709
710 #ifdef CONFIG_NF_CONNTRACK_SECMARK
711 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
712 && ctnetlink_dump_secctx(skb, ct) < 0)
713 goto nla_put_failure;
714 #endif
715 if (events & (1 << IPCT_LABEL) &&
716 ctnetlink_dump_labels(skb, ct) < 0)
717 goto nla_put_failure;
718
719 if (events & (1 << IPCT_RELATED) &&
720 ctnetlink_dump_master(skb, ct) < 0)
721 goto nla_put_failure;
722
723 if (events & (1 << IPCT_SEQADJ) &&
724 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
725 goto nla_put_failure;
726 }
727
728 #ifdef CONFIG_NF_CONNTRACK_MARK
729 if ((events & (1 << IPCT_MARK) || ct->mark)
730 && ctnetlink_dump_mark(skb, ct) < 0)
731 goto nla_put_failure;
732 #endif
733 rcu_read_unlock();
734
735 nlmsg_end(skb, nlh);
736 err = nfnetlink_send(skb, net, item->portid, group, item->report,
737 GFP_ATOMIC);
738 if (err == -ENOBUFS || err == -EAGAIN)
739 return -ENOBUFS;
740
741 return 0;
742
743 nla_put_failure:
744 rcu_read_unlock();
745 nlmsg_cancel(skb, nlh);
746 nlmsg_failure:
747 kfree_skb(skb);
748 errout:
749 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
750 return -ENOBUFS;
751
752 return 0;
753 }
754 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
755
756 static int ctnetlink_done(struct netlink_callback *cb)
757 {
758 if (cb->args[1])
759 nf_ct_put((struct nf_conn *)cb->args[1]);
760 kfree(cb->data);
761 return 0;
762 }
763
764 struct ctnetlink_filter {
765 struct {
766 u_int32_t val;
767 u_int32_t mask;
768 } mark;
769 };
770
771 static struct ctnetlink_filter *
772 ctnetlink_alloc_filter(const struct nlattr * const cda[])
773 {
774 #ifdef CONFIG_NF_CONNTRACK_MARK
775 struct ctnetlink_filter *filter;
776
777 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
778 if (filter == NULL)
779 return ERR_PTR(-ENOMEM);
780
781 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
782 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
783
784 return filter;
785 #else
786 return ERR_PTR(-EOPNOTSUPP);
787 #endif
788 }
789
790 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
791 {
792 struct ctnetlink_filter *filter = data;
793
794 if (filter == NULL)
795 return 1;
796
797 #ifdef CONFIG_NF_CONNTRACK_MARK
798 if ((ct->mark & filter->mark.mask) == filter->mark.val)
799 return 1;
800 #endif
801
802 return 0;
803 }
804
805 static int
806 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
807 {
808 struct net *net = sock_net(skb->sk);
809 struct nf_conn *ct, *last;
810 struct nf_conntrack_tuple_hash *h;
811 struct hlist_nulls_node *n;
812 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
813 u_int8_t l3proto = nfmsg->nfgen_family;
814 struct nf_conn *nf_ct_evict[8];
815 int res, i;
816 spinlock_t *lockp;
817
818 last = (struct nf_conn *)cb->args[1];
819 i = 0;
820
821 local_bh_disable();
822 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
823 restart:
824 while (i) {
825 i--;
826 if (nf_ct_should_gc(nf_ct_evict[i]))
827 nf_ct_kill(nf_ct_evict[i]);
828 nf_ct_put(nf_ct_evict[i]);
829 }
830
831 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
832 nf_conntrack_lock(lockp);
833 if (cb->args[0] >= nf_conntrack_htable_size) {
834 spin_unlock(lockp);
835 goto out;
836 }
837 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
838 hnnode) {
839 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
840 continue;
841 ct = nf_ct_tuplehash_to_ctrack(h);
842 if (nf_ct_is_expired(ct)) {
843 if (i < ARRAY_SIZE(nf_ct_evict) &&
844 atomic_inc_not_zero(&ct->ct_general.use))
845 nf_ct_evict[i++] = ct;
846 continue;
847 }
848
849 if (!net_eq(net, nf_ct_net(ct)))
850 continue;
851
852 /* Dump entries of a given L3 protocol number.
853 * If it is not specified, ie. l3proto == 0,
854 * then dump everything. */
855 if (l3proto && nf_ct_l3num(ct) != l3proto)
856 continue;
857 if (cb->args[1]) {
858 if (ct != last)
859 continue;
860 cb->args[1] = 0;
861 }
862 if (!ctnetlink_filter_match(ct, cb->data))
863 continue;
864
865 rcu_read_lock();
866 res =
867 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
868 cb->nlh->nlmsg_seq,
869 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
870 ct);
871 rcu_read_unlock();
872 if (res < 0) {
873 nf_conntrack_get(&ct->ct_general);
874 cb->args[1] = (unsigned long)ct;
875 spin_unlock(lockp);
876 goto out;
877 }
878 }
879 spin_unlock(lockp);
880 if (cb->args[1]) {
881 cb->args[1] = 0;
882 goto restart;
883 }
884 }
885 out:
886 local_bh_enable();
887 if (last)
888 nf_ct_put(last);
889
890 while (i) {
891 i--;
892 if (nf_ct_should_gc(nf_ct_evict[i]))
893 nf_ct_kill(nf_ct_evict[i]);
894 nf_ct_put(nf_ct_evict[i]);
895 }
896
897 return skb->len;
898 }
899
900 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
901 struct nf_conntrack_tuple *tuple)
902 {
903 struct nlattr *tb[CTA_IP_MAX+1];
904 struct nf_conntrack_l3proto *l3proto;
905 int ret = 0;
906
907 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
908 if (ret < 0)
909 return ret;
910
911 rcu_read_lock();
912 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
913
914 if (likely(l3proto->nlattr_to_tuple)) {
915 ret = nla_validate_nested(attr, CTA_IP_MAX,
916 l3proto->nla_policy);
917 if (ret == 0)
918 ret = l3proto->nlattr_to_tuple(tb, tuple);
919 }
920
921 rcu_read_unlock();
922
923 return ret;
924 }
925
926 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
927 [CTA_PROTO_NUM] = { .type = NLA_U8 },
928 };
929
930 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
931 struct nf_conntrack_tuple *tuple)
932 {
933 struct nlattr *tb[CTA_PROTO_MAX+1];
934 struct nf_conntrack_l4proto *l4proto;
935 int ret = 0;
936
937 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
938 if (ret < 0)
939 return ret;
940
941 if (!tb[CTA_PROTO_NUM])
942 return -EINVAL;
943 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
944
945 rcu_read_lock();
946 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
947
948 if (likely(l4proto->nlattr_to_tuple)) {
949 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
950 l4proto->nla_policy);
951 if (ret == 0)
952 ret = l4proto->nlattr_to_tuple(tb, tuple);
953 }
954
955 rcu_read_unlock();
956
957 return ret;
958 }
959
960 static int
961 ctnetlink_parse_zone(const struct nlattr *attr,
962 struct nf_conntrack_zone *zone)
963 {
964 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
965 NF_CT_DEFAULT_ZONE_DIR, 0);
966 #ifdef CONFIG_NF_CONNTRACK_ZONES
967 if (attr)
968 zone->id = ntohs(nla_get_be16(attr));
969 #else
970 if (attr)
971 return -EOPNOTSUPP;
972 #endif
973 return 0;
974 }
975
976 static int
977 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
978 struct nf_conntrack_zone *zone)
979 {
980 int ret;
981
982 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
983 return -EINVAL;
984
985 ret = ctnetlink_parse_zone(attr, zone);
986 if (ret < 0)
987 return ret;
988
989 if (type == CTA_TUPLE_REPLY)
990 zone->dir = NF_CT_ZONE_DIR_REPL;
991 else
992 zone->dir = NF_CT_ZONE_DIR_ORIG;
993
994 return 0;
995 }
996
997 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
998 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
999 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1000 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1001 };
1002
1003 static int
1004 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1005 struct nf_conntrack_tuple *tuple,
1006 enum ctattr_type type, u_int8_t l3num,
1007 struct nf_conntrack_zone *zone)
1008 {
1009 struct nlattr *tb[CTA_TUPLE_MAX+1];
1010 int err;
1011
1012 memset(tuple, 0, sizeof(*tuple));
1013
1014 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
1015 if (err < 0)
1016 return err;
1017
1018 if (!tb[CTA_TUPLE_IP])
1019 return -EINVAL;
1020
1021 tuple->src.l3num = l3num;
1022
1023 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1024 if (err < 0)
1025 return err;
1026
1027 if (!tb[CTA_TUPLE_PROTO])
1028 return -EINVAL;
1029
1030 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1031 if (err < 0)
1032 return err;
1033
1034 if (tb[CTA_TUPLE_ZONE]) {
1035 if (!zone)
1036 return -EINVAL;
1037
1038 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1039 type, zone);
1040 if (err < 0)
1041 return err;
1042 }
1043
1044 /* orig and expect tuples get DIR_ORIGINAL */
1045 if (type == CTA_TUPLE_REPLY)
1046 tuple->dst.dir = IP_CT_DIR_REPLY;
1047 else
1048 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1049
1050 return 0;
1051 }
1052
1053 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1054 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1055 .len = NF_CT_HELPER_NAME_LEN - 1 },
1056 };
1057
1058 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1059 struct nlattr **helpinfo)
1060 {
1061 int err;
1062 struct nlattr *tb[CTA_HELP_MAX+1];
1063
1064 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
1065 if (err < 0)
1066 return err;
1067
1068 if (!tb[CTA_HELP_NAME])
1069 return -EINVAL;
1070
1071 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1072
1073 if (tb[CTA_HELP_INFO])
1074 *helpinfo = tb[CTA_HELP_INFO];
1075
1076 return 0;
1077 }
1078
1079 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1080 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1081 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1082 [CTA_STATUS] = { .type = NLA_U32 },
1083 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1084 [CTA_HELP] = { .type = NLA_NESTED },
1085 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1086 [CTA_TIMEOUT] = { .type = NLA_U32 },
1087 [CTA_MARK] = { .type = NLA_U32 },
1088 [CTA_ID] = { .type = NLA_U32 },
1089 [CTA_NAT_DST] = { .type = NLA_NESTED },
1090 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1091 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1092 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1093 [CTA_ZONE] = { .type = NLA_U16 },
1094 [CTA_MARK_MASK] = { .type = NLA_U32 },
1095 [CTA_LABELS] = { .type = NLA_BINARY,
1096 .len = NF_CT_LABELS_MAX_SIZE },
1097 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1098 .len = NF_CT_LABELS_MAX_SIZE },
1099 };
1100
1101 static int ctnetlink_flush_conntrack(struct net *net,
1102 const struct nlattr * const cda[],
1103 u32 portid, int report)
1104 {
1105 struct ctnetlink_filter *filter = NULL;
1106
1107 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1108 filter = ctnetlink_alloc_filter(cda);
1109 if (IS_ERR(filter))
1110 return PTR_ERR(filter);
1111 }
1112
1113 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1114 portid, report);
1115 kfree(filter);
1116
1117 return 0;
1118 }
1119
1120 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1121 struct sk_buff *skb,
1122 const struct nlmsghdr *nlh,
1123 const struct nlattr * const cda[])
1124 {
1125 struct nf_conntrack_tuple_hash *h;
1126 struct nf_conntrack_tuple tuple;
1127 struct nf_conn *ct;
1128 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1129 u_int8_t u3 = nfmsg->nfgen_family;
1130 struct nf_conntrack_zone zone;
1131 int err;
1132
1133 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1134 if (err < 0)
1135 return err;
1136
1137 if (cda[CTA_TUPLE_ORIG])
1138 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1139 u3, &zone);
1140 else if (cda[CTA_TUPLE_REPLY])
1141 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1142 u3, &zone);
1143 else {
1144 return ctnetlink_flush_conntrack(net, cda,
1145 NETLINK_CB(skb).portid,
1146 nlmsg_report(nlh));
1147 }
1148
1149 if (err < 0)
1150 return err;
1151
1152 h = nf_conntrack_find_get(net, &zone, &tuple);
1153 if (!h)
1154 return -ENOENT;
1155
1156 ct = nf_ct_tuplehash_to_ctrack(h);
1157
1158 if (cda[CTA_ID]) {
1159 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1160 if (id != (u32)(unsigned long)ct) {
1161 nf_ct_put(ct);
1162 return -ENOENT;
1163 }
1164 }
1165
1166 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1167 nf_ct_put(ct);
1168
1169 return 0;
1170 }
1171
1172 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1173 struct sk_buff *skb,
1174 const struct nlmsghdr *nlh,
1175 const struct nlattr * const cda[])
1176 {
1177 struct nf_conntrack_tuple_hash *h;
1178 struct nf_conntrack_tuple tuple;
1179 struct nf_conn *ct;
1180 struct sk_buff *skb2 = NULL;
1181 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1182 u_int8_t u3 = nfmsg->nfgen_family;
1183 struct nf_conntrack_zone zone;
1184 int err;
1185
1186 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1187 struct netlink_dump_control c = {
1188 .dump = ctnetlink_dump_table,
1189 .done = ctnetlink_done,
1190 };
1191
1192 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1193 struct ctnetlink_filter *filter;
1194
1195 filter = ctnetlink_alloc_filter(cda);
1196 if (IS_ERR(filter))
1197 return PTR_ERR(filter);
1198
1199 c.data = filter;
1200 }
1201 return netlink_dump_start(ctnl, skb, nlh, &c);
1202 }
1203
1204 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1205 if (err < 0)
1206 return err;
1207
1208 if (cda[CTA_TUPLE_ORIG])
1209 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1210 u3, &zone);
1211 else if (cda[CTA_TUPLE_REPLY])
1212 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1213 u3, &zone);
1214 else
1215 return -EINVAL;
1216
1217 if (err < 0)
1218 return err;
1219
1220 h = nf_conntrack_find_get(net, &zone, &tuple);
1221 if (!h)
1222 return -ENOENT;
1223
1224 ct = nf_ct_tuplehash_to_ctrack(h);
1225
1226 err = -ENOMEM;
1227 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1228 if (skb2 == NULL) {
1229 nf_ct_put(ct);
1230 return -ENOMEM;
1231 }
1232
1233 rcu_read_lock();
1234 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1235 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1236 rcu_read_unlock();
1237 nf_ct_put(ct);
1238 if (err <= 0)
1239 goto free;
1240
1241 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1242 if (err < 0)
1243 goto out;
1244
1245 return 0;
1246
1247 free:
1248 kfree_skb(skb2);
1249 out:
1250 /* this avoids a loop in nfnetlink. */
1251 return err == -EAGAIN ? -ENOBUFS : err;
1252 }
1253
1254 static int ctnetlink_done_list(struct netlink_callback *cb)
1255 {
1256 if (cb->args[1])
1257 nf_ct_put((struct nf_conn *)cb->args[1]);
1258 return 0;
1259 }
1260
1261 static int
1262 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1263 {
1264 struct nf_conn *ct, *last;
1265 struct nf_conntrack_tuple_hash *h;
1266 struct hlist_nulls_node *n;
1267 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1268 u_int8_t l3proto = nfmsg->nfgen_family;
1269 int res;
1270 int cpu;
1271 struct hlist_nulls_head *list;
1272 struct net *net = sock_net(skb->sk);
1273
1274 if (cb->args[2])
1275 return 0;
1276
1277 last = (struct nf_conn *)cb->args[1];
1278
1279 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1280 struct ct_pcpu *pcpu;
1281
1282 if (!cpu_possible(cpu))
1283 continue;
1284
1285 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1286 spin_lock_bh(&pcpu->lock);
1287 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1288 restart:
1289 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1290 ct = nf_ct_tuplehash_to_ctrack(h);
1291 if (l3proto && nf_ct_l3num(ct) != l3proto)
1292 continue;
1293 if (cb->args[1]) {
1294 if (ct != last)
1295 continue;
1296 cb->args[1] = 0;
1297 }
1298 rcu_read_lock();
1299 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1300 cb->nlh->nlmsg_seq,
1301 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1302 ct);
1303 rcu_read_unlock();
1304 if (res < 0) {
1305 if (!atomic_inc_not_zero(&ct->ct_general.use))
1306 continue;
1307 cb->args[0] = cpu;
1308 cb->args[1] = (unsigned long)ct;
1309 spin_unlock_bh(&pcpu->lock);
1310 goto out;
1311 }
1312 }
1313 if (cb->args[1]) {
1314 cb->args[1] = 0;
1315 goto restart;
1316 }
1317 spin_unlock_bh(&pcpu->lock);
1318 }
1319 cb->args[2] = 1;
1320 out:
1321 if (last)
1322 nf_ct_put(last);
1323
1324 return skb->len;
1325 }
1326
1327 static int
1328 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1329 {
1330 return ctnetlink_dump_list(skb, cb, true);
1331 }
1332
1333 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1334 struct sk_buff *skb,
1335 const struct nlmsghdr *nlh,
1336 const struct nlattr * const cda[])
1337 {
1338 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1339 struct netlink_dump_control c = {
1340 .dump = ctnetlink_dump_dying,
1341 .done = ctnetlink_done_list,
1342 };
1343 return netlink_dump_start(ctnl, skb, nlh, &c);
1344 }
1345
1346 return -EOPNOTSUPP;
1347 }
1348
1349 static int
1350 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1351 {
1352 return ctnetlink_dump_list(skb, cb, false);
1353 }
1354
1355 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1356 struct sk_buff *skb,
1357 const struct nlmsghdr *nlh,
1358 const struct nlattr * const cda[])
1359 {
1360 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1361 struct netlink_dump_control c = {
1362 .dump = ctnetlink_dump_unconfirmed,
1363 .done = ctnetlink_done_list,
1364 };
1365 return netlink_dump_start(ctnl, skb, nlh, &c);
1366 }
1367
1368 return -EOPNOTSUPP;
1369 }
1370
1371 #ifdef CONFIG_NF_NAT_NEEDED
1372 static int
1373 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1374 enum nf_nat_manip_type manip,
1375 const struct nlattr *attr)
1376 {
1377 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1378 int err;
1379
1380 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1381 if (!parse_nat_setup) {
1382 #ifdef CONFIG_MODULES
1383 rcu_read_unlock();
1384 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1385 if (request_module("nf-nat") < 0) {
1386 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1387 rcu_read_lock();
1388 return -EOPNOTSUPP;
1389 }
1390 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1391 rcu_read_lock();
1392 if (nfnetlink_parse_nat_setup_hook)
1393 return -EAGAIN;
1394 #endif
1395 return -EOPNOTSUPP;
1396 }
1397
1398 err = parse_nat_setup(ct, manip, attr);
1399 if (err == -EAGAIN) {
1400 #ifdef CONFIG_MODULES
1401 rcu_read_unlock();
1402 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1403 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1404 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1405 rcu_read_lock();
1406 return -EOPNOTSUPP;
1407 }
1408 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1409 rcu_read_lock();
1410 #else
1411 err = -EOPNOTSUPP;
1412 #endif
1413 }
1414 return err;
1415 }
1416 #endif
1417
1418 static int
1419 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1420 {
1421 unsigned long d;
1422 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1423 d = ct->status ^ status;
1424
1425 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1426 /* unchangeable */
1427 return -EBUSY;
1428
1429 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1430 /* SEEN_REPLY bit can only be set */
1431 return -EBUSY;
1432
1433 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1434 /* ASSURED bit can only be set */
1435 return -EBUSY;
1436
1437 /* Be careful here, modifying NAT bits can screw up things,
1438 * so don't let users modify them directly if they don't pass
1439 * nf_nat_range. */
1440 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1441 return 0;
1442 }
1443
1444 static int
1445 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1446 {
1447 #ifdef CONFIG_NF_NAT_NEEDED
1448 int ret;
1449
1450 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1451 return 0;
1452
1453 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1454 cda[CTA_NAT_DST]);
1455 if (ret < 0)
1456 return ret;
1457
1458 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1459 cda[CTA_NAT_SRC]);
1460 return ret;
1461 #else
1462 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1463 return 0;
1464 return -EOPNOTSUPP;
1465 #endif
1466 }
1467
1468 static int ctnetlink_change_helper(struct nf_conn *ct,
1469 const struct nlattr * const cda[])
1470 {
1471 struct nf_conntrack_helper *helper;
1472 struct nf_conn_help *help = nfct_help(ct);
1473 char *helpname = NULL;
1474 struct nlattr *helpinfo = NULL;
1475 int err;
1476
1477 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1478 if (err < 0)
1479 return err;
1480
1481 /* don't change helper of sibling connections */
1482 if (ct->master) {
1483 /* If we try to change the helper to the same thing twice,
1484 * treat the second attempt as a no-op instead of returning
1485 * an error.
1486 */
1487 if (help && help->helper &&
1488 !strcmp(help->helper->name, helpname))
1489 return 0;
1490 else
1491 return -EBUSY;
1492 }
1493
1494 if (!strcmp(helpname, "")) {
1495 if (help && help->helper) {
1496 /* we had a helper before ... */
1497 nf_ct_remove_expectations(ct);
1498 RCU_INIT_POINTER(help->helper, NULL);
1499 }
1500
1501 return 0;
1502 }
1503
1504 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1505 nf_ct_protonum(ct));
1506 if (helper == NULL) {
1507 #ifdef CONFIG_MODULES
1508 spin_unlock_bh(&nf_conntrack_expect_lock);
1509
1510 if (request_module("nfct-helper-%s", helpname) < 0) {
1511 spin_lock_bh(&nf_conntrack_expect_lock);
1512 return -EOPNOTSUPP;
1513 }
1514
1515 spin_lock_bh(&nf_conntrack_expect_lock);
1516 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1517 nf_ct_protonum(ct));
1518 if (helper)
1519 return -EAGAIN;
1520 #endif
1521 return -EOPNOTSUPP;
1522 }
1523
1524 if (help) {
1525 if (help->helper == helper) {
1526 /* update private helper data if allowed. */
1527 if (helper->from_nlattr)
1528 helper->from_nlattr(helpinfo, ct);
1529 return 0;
1530 } else
1531 return -EBUSY;
1532 }
1533
1534 /* we cannot set a helper for an existing conntrack */
1535 return -EOPNOTSUPP;
1536 }
1537
1538 static int ctnetlink_change_timeout(struct nf_conn *ct,
1539 const struct nlattr * const cda[])
1540 {
1541 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1542
1543 ct->timeout = nfct_time_stamp + timeout * HZ;
1544
1545 if (test_bit(IPS_DYING_BIT, &ct->status))
1546 return -ETIME;
1547
1548 return 0;
1549 }
1550
1551 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1552 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1553 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1554 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1555 };
1556
1557 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1558 const struct nlattr * const cda[])
1559 {
1560 const struct nlattr *attr = cda[CTA_PROTOINFO];
1561 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1562 struct nf_conntrack_l4proto *l4proto;
1563 int err = 0;
1564
1565 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1566 if (err < 0)
1567 return err;
1568
1569 rcu_read_lock();
1570 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1571 if (l4proto->from_nlattr)
1572 err = l4proto->from_nlattr(tb, ct);
1573 rcu_read_unlock();
1574
1575 return err;
1576 }
1577
1578 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1579 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1580 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1581 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1582 };
1583
1584 static int change_seq_adj(struct nf_ct_seqadj *seq,
1585 const struct nlattr * const attr)
1586 {
1587 int err;
1588 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1589
1590 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1591 if (err < 0)
1592 return err;
1593
1594 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1595 return -EINVAL;
1596
1597 seq->correction_pos =
1598 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1599
1600 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1601 return -EINVAL;
1602
1603 seq->offset_before =
1604 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1605
1606 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1607 return -EINVAL;
1608
1609 seq->offset_after =
1610 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1611
1612 return 0;
1613 }
1614
1615 static int
1616 ctnetlink_change_seq_adj(struct nf_conn *ct,
1617 const struct nlattr * const cda[])
1618 {
1619 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1620 int ret = 0;
1621
1622 if (!seqadj)
1623 return 0;
1624
1625 if (cda[CTA_SEQ_ADJ_ORIG]) {
1626 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1627 cda[CTA_SEQ_ADJ_ORIG]);
1628 if (ret < 0)
1629 return ret;
1630
1631 ct->status |= IPS_SEQ_ADJUST;
1632 }
1633
1634 if (cda[CTA_SEQ_ADJ_REPLY]) {
1635 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1636 cda[CTA_SEQ_ADJ_REPLY]);
1637 if (ret < 0)
1638 return ret;
1639
1640 ct->status |= IPS_SEQ_ADJUST;
1641 }
1642
1643 return 0;
1644 }
1645
1646 static int
1647 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1648 {
1649 #ifdef CONFIG_NF_CONNTRACK_LABELS
1650 size_t len = nla_len(cda[CTA_LABELS]);
1651 const void *mask = cda[CTA_LABELS_MASK];
1652
1653 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1654 return -EINVAL;
1655
1656 if (mask) {
1657 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1658 nla_len(cda[CTA_LABELS_MASK]) != len)
1659 return -EINVAL;
1660 mask = nla_data(cda[CTA_LABELS_MASK]);
1661 }
1662
1663 len /= sizeof(u32);
1664
1665 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1666 #else
1667 return -EOPNOTSUPP;
1668 #endif
1669 }
1670
1671 static int
1672 ctnetlink_change_conntrack(struct nf_conn *ct,
1673 const struct nlattr * const cda[])
1674 {
1675 int err;
1676
1677 /* only allow NAT changes and master assignation for new conntracks */
1678 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1679 return -EOPNOTSUPP;
1680
1681 if (cda[CTA_HELP]) {
1682 err = ctnetlink_change_helper(ct, cda);
1683 if (err < 0)
1684 return err;
1685 }
1686
1687 if (cda[CTA_TIMEOUT]) {
1688 err = ctnetlink_change_timeout(ct, cda);
1689 if (err < 0)
1690 return err;
1691 }
1692
1693 if (cda[CTA_STATUS]) {
1694 err = ctnetlink_change_status(ct, cda);
1695 if (err < 0)
1696 return err;
1697 }
1698
1699 if (cda[CTA_PROTOINFO]) {
1700 err = ctnetlink_change_protoinfo(ct, cda);
1701 if (err < 0)
1702 return err;
1703 }
1704
1705 #if defined(CONFIG_NF_CONNTRACK_MARK)
1706 if (cda[CTA_MARK])
1707 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1708 #endif
1709
1710 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1711 err = ctnetlink_change_seq_adj(ct, cda);
1712 if (err < 0)
1713 return err;
1714 }
1715
1716 if (cda[CTA_LABELS]) {
1717 err = ctnetlink_attach_labels(ct, cda);
1718 if (err < 0)
1719 return err;
1720 }
1721
1722 return 0;
1723 }
1724
1725 static struct nf_conn *
1726 ctnetlink_create_conntrack(struct net *net,
1727 const struct nf_conntrack_zone *zone,
1728 const struct nlattr * const cda[],
1729 struct nf_conntrack_tuple *otuple,
1730 struct nf_conntrack_tuple *rtuple,
1731 u8 u3)
1732 {
1733 struct nf_conn *ct;
1734 int err = -EINVAL;
1735 struct nf_conntrack_helper *helper;
1736 struct nf_conn_tstamp *tstamp;
1737
1738 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1739 if (IS_ERR(ct))
1740 return ERR_PTR(-ENOMEM);
1741
1742 if (!cda[CTA_TIMEOUT])
1743 goto err1;
1744
1745 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1746
1747 rcu_read_lock();
1748 if (cda[CTA_HELP]) {
1749 char *helpname = NULL;
1750 struct nlattr *helpinfo = NULL;
1751
1752 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1753 if (err < 0)
1754 goto err2;
1755
1756 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1757 nf_ct_protonum(ct));
1758 if (helper == NULL) {
1759 rcu_read_unlock();
1760 #ifdef CONFIG_MODULES
1761 if (request_module("nfct-helper-%s", helpname) < 0) {
1762 err = -EOPNOTSUPP;
1763 goto err1;
1764 }
1765
1766 rcu_read_lock();
1767 helper = __nf_conntrack_helper_find(helpname,
1768 nf_ct_l3num(ct),
1769 nf_ct_protonum(ct));
1770 if (helper) {
1771 err = -EAGAIN;
1772 goto err2;
1773 }
1774 rcu_read_unlock();
1775 #endif
1776 err = -EOPNOTSUPP;
1777 goto err1;
1778 } else {
1779 struct nf_conn_help *help;
1780
1781 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1782 if (help == NULL) {
1783 err = -ENOMEM;
1784 goto err2;
1785 }
1786 /* set private helper data if allowed. */
1787 if (helper->from_nlattr)
1788 helper->from_nlattr(helpinfo, ct);
1789
1790 /* not in hash table yet so not strictly necessary */
1791 RCU_INIT_POINTER(help->helper, helper);
1792 }
1793 } else {
1794 /* try an implicit helper assignation */
1795 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1796 if (err < 0)
1797 goto err2;
1798 }
1799
1800 err = ctnetlink_setup_nat(ct, cda);
1801 if (err < 0)
1802 goto err2;
1803
1804 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1805 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1806 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1807 nf_ct_labels_ext_add(ct);
1808
1809 /* we must add conntrack extensions before confirmation. */
1810 ct->status |= IPS_CONFIRMED;
1811
1812 if (cda[CTA_STATUS]) {
1813 err = ctnetlink_change_status(ct, cda);
1814 if (err < 0)
1815 goto err2;
1816 }
1817
1818 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1819 err = ctnetlink_change_seq_adj(ct, cda);
1820 if (err < 0)
1821 goto err2;
1822 }
1823
1824 memset(&ct->proto, 0, sizeof(ct->proto));
1825 if (cda[CTA_PROTOINFO]) {
1826 err = ctnetlink_change_protoinfo(ct, cda);
1827 if (err < 0)
1828 goto err2;
1829 }
1830
1831 #if defined(CONFIG_NF_CONNTRACK_MARK)
1832 if (cda[CTA_MARK])
1833 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1834 #endif
1835
1836 /* setup master conntrack: this is a confirmed expectation */
1837 if (cda[CTA_TUPLE_MASTER]) {
1838 struct nf_conntrack_tuple master;
1839 struct nf_conntrack_tuple_hash *master_h;
1840 struct nf_conn *master_ct;
1841
1842 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1843 u3, NULL);
1844 if (err < 0)
1845 goto err2;
1846
1847 master_h = nf_conntrack_find_get(net, zone, &master);
1848 if (master_h == NULL) {
1849 err = -ENOENT;
1850 goto err2;
1851 }
1852 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1853 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1854 ct->master = master_ct;
1855 }
1856 tstamp = nf_conn_tstamp_find(ct);
1857 if (tstamp)
1858 tstamp->start = ktime_get_real_ns();
1859
1860 err = nf_conntrack_hash_check_insert(ct);
1861 if (err < 0)
1862 goto err2;
1863
1864 rcu_read_unlock();
1865
1866 return ct;
1867
1868 err2:
1869 rcu_read_unlock();
1870 err1:
1871 nf_conntrack_free(ct);
1872 return ERR_PTR(err);
1873 }
1874
1875 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1876 struct sk_buff *skb,
1877 const struct nlmsghdr *nlh,
1878 const struct nlattr * const cda[])
1879 {
1880 struct nf_conntrack_tuple otuple, rtuple;
1881 struct nf_conntrack_tuple_hash *h = NULL;
1882 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1883 struct nf_conn *ct;
1884 u_int8_t u3 = nfmsg->nfgen_family;
1885 struct nf_conntrack_zone zone;
1886 int err;
1887
1888 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1889 if (err < 0)
1890 return err;
1891
1892 if (cda[CTA_TUPLE_ORIG]) {
1893 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1894 u3, &zone);
1895 if (err < 0)
1896 return err;
1897 }
1898
1899 if (cda[CTA_TUPLE_REPLY]) {
1900 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1901 u3, &zone);
1902 if (err < 0)
1903 return err;
1904 }
1905
1906 if (cda[CTA_TUPLE_ORIG])
1907 h = nf_conntrack_find_get(net, &zone, &otuple);
1908 else if (cda[CTA_TUPLE_REPLY])
1909 h = nf_conntrack_find_get(net, &zone, &rtuple);
1910
1911 if (h == NULL) {
1912 err = -ENOENT;
1913 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1914 enum ip_conntrack_events events;
1915
1916 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1917 return -EINVAL;
1918 if (otuple.dst.protonum != rtuple.dst.protonum)
1919 return -EINVAL;
1920
1921 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1922 &rtuple, u3);
1923 if (IS_ERR(ct))
1924 return PTR_ERR(ct);
1925
1926 err = 0;
1927 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1928 events = IPCT_RELATED;
1929 else
1930 events = IPCT_NEW;
1931
1932 if (cda[CTA_LABELS] &&
1933 ctnetlink_attach_labels(ct, cda) == 0)
1934 events |= (1 << IPCT_LABEL);
1935
1936 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1937 (1 << IPCT_ASSURED) |
1938 (1 << IPCT_HELPER) |
1939 (1 << IPCT_PROTOINFO) |
1940 (1 << IPCT_SEQADJ) |
1941 (1 << IPCT_MARK) | events,
1942 ct, NETLINK_CB(skb).portid,
1943 nlmsg_report(nlh));
1944 nf_ct_put(ct);
1945 }
1946
1947 return err;
1948 }
1949 /* implicit 'else' */
1950
1951 err = -EEXIST;
1952 ct = nf_ct_tuplehash_to_ctrack(h);
1953 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1954 spin_lock_bh(&nf_conntrack_expect_lock);
1955 err = ctnetlink_change_conntrack(ct, cda);
1956 spin_unlock_bh(&nf_conntrack_expect_lock);
1957 if (err == 0) {
1958 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1959 (1 << IPCT_ASSURED) |
1960 (1 << IPCT_HELPER) |
1961 (1 << IPCT_LABEL) |
1962 (1 << IPCT_PROTOINFO) |
1963 (1 << IPCT_SEQADJ) |
1964 (1 << IPCT_MARK),
1965 ct, NETLINK_CB(skb).portid,
1966 nlmsg_report(nlh));
1967 }
1968 }
1969
1970 nf_ct_put(ct);
1971 return err;
1972 }
1973
1974 static int
1975 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1976 __u16 cpu, const struct ip_conntrack_stat *st)
1977 {
1978 struct nlmsghdr *nlh;
1979 struct nfgenmsg *nfmsg;
1980 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1981
1982 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
1983 IPCTNL_MSG_CT_GET_STATS_CPU);
1984 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1985 if (nlh == NULL)
1986 goto nlmsg_failure;
1987
1988 nfmsg = nlmsg_data(nlh);
1989 nfmsg->nfgen_family = AF_UNSPEC;
1990 nfmsg->version = NFNETLINK_V0;
1991 nfmsg->res_id = htons(cpu);
1992
1993 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1994 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1995 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1996 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1997 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1998 htonl(st->insert_failed)) ||
1999 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2000 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2001 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2002 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2003 htonl(st->search_restart)))
2004 goto nla_put_failure;
2005
2006 nlmsg_end(skb, nlh);
2007 return skb->len;
2008
2009 nla_put_failure:
2010 nlmsg_failure:
2011 nlmsg_cancel(skb, nlh);
2012 return -1;
2013 }
2014
2015 static int
2016 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2017 {
2018 int cpu;
2019 struct net *net = sock_net(skb->sk);
2020
2021 if (cb->args[0] == nr_cpu_ids)
2022 return 0;
2023
2024 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2025 const struct ip_conntrack_stat *st;
2026
2027 if (!cpu_possible(cpu))
2028 continue;
2029
2030 st = per_cpu_ptr(net->ct.stat, cpu);
2031 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2032 NETLINK_CB(cb->skb).portid,
2033 cb->nlh->nlmsg_seq,
2034 cpu, st) < 0)
2035 break;
2036 }
2037 cb->args[0] = cpu;
2038
2039 return skb->len;
2040 }
2041
2042 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2043 struct sk_buff *skb,
2044 const struct nlmsghdr *nlh,
2045 const struct nlattr * const cda[])
2046 {
2047 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2048 struct netlink_dump_control c = {
2049 .dump = ctnetlink_ct_stat_cpu_dump,
2050 };
2051 return netlink_dump_start(ctnl, skb, nlh, &c);
2052 }
2053
2054 return 0;
2055 }
2056
2057 static int
2058 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2059 struct net *net)
2060 {
2061 struct nlmsghdr *nlh;
2062 struct nfgenmsg *nfmsg;
2063 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2064 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2065
2066 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2067 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2068 if (nlh == NULL)
2069 goto nlmsg_failure;
2070
2071 nfmsg = nlmsg_data(nlh);
2072 nfmsg->nfgen_family = AF_UNSPEC;
2073 nfmsg->version = NFNETLINK_V0;
2074 nfmsg->res_id = 0;
2075
2076 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2077 goto nla_put_failure;
2078
2079 nlmsg_end(skb, nlh);
2080 return skb->len;
2081
2082 nla_put_failure:
2083 nlmsg_failure:
2084 nlmsg_cancel(skb, nlh);
2085 return -1;
2086 }
2087
2088 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2089 struct sk_buff *skb, const struct nlmsghdr *nlh,
2090 const struct nlattr * const cda[])
2091 {
2092 struct sk_buff *skb2;
2093 int err;
2094
2095 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2096 if (skb2 == NULL)
2097 return -ENOMEM;
2098
2099 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2100 nlh->nlmsg_seq,
2101 NFNL_MSG_TYPE(nlh->nlmsg_type),
2102 sock_net(skb->sk));
2103 if (err <= 0)
2104 goto free;
2105
2106 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2107 if (err < 0)
2108 goto out;
2109
2110 return 0;
2111
2112 free:
2113 kfree_skb(skb2);
2114 out:
2115 /* this avoids a loop in nfnetlink. */
2116 return err == -EAGAIN ? -ENOBUFS : err;
2117 }
2118
2119 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2120 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2121 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2122 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2123 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2124 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2125 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2126 .len = NF_CT_HELPER_NAME_LEN - 1 },
2127 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2128 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2129 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2130 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2131 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2132 };
2133
2134 static struct nf_conntrack_expect *
2135 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2136 struct nf_conntrack_helper *helper,
2137 struct nf_conntrack_tuple *tuple,
2138 struct nf_conntrack_tuple *mask);
2139
2140 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2141 static size_t
2142 ctnetlink_glue_build_size(const struct nf_conn *ct)
2143 {
2144 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2145 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2146 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2147 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2148 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2149 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2150 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2151 + nla_total_size(0) /* CTA_PROTOINFO */
2152 + nla_total_size(0) /* CTA_HELP */
2153 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2154 + ctnetlink_secctx_size(ct)
2155 #ifdef CONFIG_NF_NAT_NEEDED
2156 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2157 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2158 #endif
2159 #ifdef CONFIG_NF_CONNTRACK_MARK
2160 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2161 #endif
2162 #ifdef CONFIG_NF_CONNTRACK_ZONES
2163 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2164 #endif
2165 + ctnetlink_proto_size(ct)
2166 ;
2167 }
2168
2169 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2170 enum ip_conntrack_info *ctinfo)
2171 {
2172 return nf_ct_get(skb, ctinfo);
2173 }
2174
2175 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2176 {
2177 const struct nf_conntrack_zone *zone;
2178 struct nlattr *nest_parms;
2179
2180 rcu_read_lock();
2181 zone = nf_ct_zone(ct);
2182
2183 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2184 if (!nest_parms)
2185 goto nla_put_failure;
2186 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2187 goto nla_put_failure;
2188 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2189 NF_CT_ZONE_DIR_ORIG) < 0)
2190 goto nla_put_failure;
2191 nla_nest_end(skb, nest_parms);
2192
2193 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2194 if (!nest_parms)
2195 goto nla_put_failure;
2196 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2197 goto nla_put_failure;
2198 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2199 NF_CT_ZONE_DIR_REPL) < 0)
2200 goto nla_put_failure;
2201 nla_nest_end(skb, nest_parms);
2202
2203 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2204 NF_CT_DEFAULT_ZONE_DIR) < 0)
2205 goto nla_put_failure;
2206
2207 if (ctnetlink_dump_id(skb, ct) < 0)
2208 goto nla_put_failure;
2209
2210 if (ctnetlink_dump_status(skb, ct) < 0)
2211 goto nla_put_failure;
2212
2213 if (ctnetlink_dump_timeout(skb, ct) < 0)
2214 goto nla_put_failure;
2215
2216 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2217 goto nla_put_failure;
2218
2219 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2220 goto nla_put_failure;
2221
2222 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2223 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2224 goto nla_put_failure;
2225 #endif
2226 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2227 goto nla_put_failure;
2228
2229 if ((ct->status & IPS_SEQ_ADJUST) &&
2230 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2231 goto nla_put_failure;
2232
2233 #ifdef CONFIG_NF_CONNTRACK_MARK
2234 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2235 goto nla_put_failure;
2236 #endif
2237 if (ctnetlink_dump_labels(skb, ct) < 0)
2238 goto nla_put_failure;
2239 rcu_read_unlock();
2240 return 0;
2241
2242 nla_put_failure:
2243 rcu_read_unlock();
2244 return -ENOSPC;
2245 }
2246
2247 static int
2248 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2249 enum ip_conntrack_info ctinfo,
2250 u_int16_t ct_attr, u_int16_t ct_info_attr)
2251 {
2252 struct nlattr *nest_parms;
2253
2254 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2255 if (!nest_parms)
2256 goto nla_put_failure;
2257
2258 if (__ctnetlink_glue_build(skb, ct) < 0)
2259 goto nla_put_failure;
2260
2261 nla_nest_end(skb, nest_parms);
2262
2263 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2264 goto nla_put_failure;
2265
2266 return 0;
2267
2268 nla_put_failure:
2269 return -ENOSPC;
2270 }
2271
2272 static int
2273 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2274 {
2275 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2276 unsigned long d = ct->status ^ status;
2277
2278 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2279 /* SEEN_REPLY bit can only be set */
2280 return -EBUSY;
2281
2282 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2283 /* ASSURED bit can only be set */
2284 return -EBUSY;
2285
2286 /* This check is less strict than ctnetlink_change_status()
2287 * because callers often flip IPS_EXPECTED bits when sending
2288 * an NFQA_CT attribute to the kernel. So ignore the
2289 * unchangeable bits but do not error out.
2290 */
2291 ct->status = (status & ~IPS_UNCHANGEABLE_MASK) |
2292 (ct->status & IPS_UNCHANGEABLE_MASK);
2293 return 0;
2294 }
2295
2296 static int
2297 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2298 {
2299 int err;
2300
2301 if (cda[CTA_TIMEOUT]) {
2302 err = ctnetlink_change_timeout(ct, cda);
2303 if (err < 0)
2304 return err;
2305 }
2306 if (cda[CTA_STATUS]) {
2307 err = ctnetlink_update_status(ct, cda);
2308 if (err < 0)
2309 return err;
2310 }
2311 if (cda[CTA_HELP]) {
2312 err = ctnetlink_change_helper(ct, cda);
2313 if (err < 0)
2314 return err;
2315 }
2316 if (cda[CTA_LABELS]) {
2317 err = ctnetlink_attach_labels(ct, cda);
2318 if (err < 0)
2319 return err;
2320 }
2321 #if defined(CONFIG_NF_CONNTRACK_MARK)
2322 if (cda[CTA_MARK]) {
2323 u32 mask = 0, mark, newmark;
2324 if (cda[CTA_MARK_MASK])
2325 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2326
2327 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2328 newmark = (ct->mark & mask) ^ mark;
2329 if (newmark != ct->mark)
2330 ct->mark = newmark;
2331 }
2332 #endif
2333 return 0;
2334 }
2335
2336 static int
2337 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2338 {
2339 struct nlattr *cda[CTA_MAX+1];
2340 int ret;
2341
2342 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2343 if (ret < 0)
2344 return ret;
2345
2346 spin_lock_bh(&nf_conntrack_expect_lock);
2347 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2348 spin_unlock_bh(&nf_conntrack_expect_lock);
2349
2350 return ret;
2351 }
2352
2353 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2354 const struct nf_conn *ct,
2355 struct nf_conntrack_tuple *tuple,
2356 struct nf_conntrack_tuple *mask)
2357 {
2358 int err;
2359
2360 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2361 nf_ct_l3num(ct), NULL);
2362 if (err < 0)
2363 return err;
2364
2365 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2366 nf_ct_l3num(ct), NULL);
2367 }
2368
2369 static int
2370 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2371 u32 portid, u32 report)
2372 {
2373 struct nlattr *cda[CTA_EXPECT_MAX+1];
2374 struct nf_conntrack_tuple tuple, mask;
2375 struct nf_conntrack_helper *helper = NULL;
2376 struct nf_conntrack_expect *exp;
2377 int err;
2378
2379 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2380 if (err < 0)
2381 return err;
2382
2383 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2384 ct, &tuple, &mask);
2385 if (err < 0)
2386 return err;
2387
2388 if (cda[CTA_EXPECT_HELP_NAME]) {
2389 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2390
2391 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2392 nf_ct_protonum(ct));
2393 if (helper == NULL)
2394 return -EOPNOTSUPP;
2395 }
2396
2397 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2398 helper, &tuple, &mask);
2399 if (IS_ERR(exp))
2400 return PTR_ERR(exp);
2401
2402 err = nf_ct_expect_related_report(exp, portid, report);
2403 nf_ct_expect_put(exp);
2404 return err;
2405 }
2406
2407 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2408 enum ip_conntrack_info ctinfo, int diff)
2409 {
2410 if (!(ct->status & IPS_NAT_MASK))
2411 return;
2412
2413 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2414 }
2415
2416 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2417 .get_ct = ctnetlink_glue_get_ct,
2418 .build_size = ctnetlink_glue_build_size,
2419 .build = ctnetlink_glue_build,
2420 .parse = ctnetlink_glue_parse,
2421 .attach_expect = ctnetlink_glue_attach_expect,
2422 .seq_adjust = ctnetlink_glue_seqadj,
2423 };
2424 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2425
2426 /***********************************************************************
2427 * EXPECT
2428 ***********************************************************************/
2429
2430 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2431 const struct nf_conntrack_tuple *tuple,
2432 enum ctattr_expect type)
2433 {
2434 struct nlattr *nest_parms;
2435
2436 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2437 if (!nest_parms)
2438 goto nla_put_failure;
2439 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2440 goto nla_put_failure;
2441 nla_nest_end(skb, nest_parms);
2442
2443 return 0;
2444
2445 nla_put_failure:
2446 return -1;
2447 }
2448
2449 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2450 const struct nf_conntrack_tuple *tuple,
2451 const struct nf_conntrack_tuple_mask *mask)
2452 {
2453 int ret;
2454 struct nf_conntrack_l3proto *l3proto;
2455 struct nf_conntrack_l4proto *l4proto;
2456 struct nf_conntrack_tuple m;
2457 struct nlattr *nest_parms;
2458
2459 memset(&m, 0xFF, sizeof(m));
2460 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2461 m.src.u.all = mask->src.u.all;
2462 m.dst.protonum = tuple->dst.protonum;
2463
2464 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2465 if (!nest_parms)
2466 goto nla_put_failure;
2467
2468 rcu_read_lock();
2469 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2470 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2471 if (ret >= 0) {
2472 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2473 tuple->dst.protonum);
2474 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2475 }
2476 rcu_read_unlock();
2477
2478 if (unlikely(ret < 0))
2479 goto nla_put_failure;
2480
2481 nla_nest_end(skb, nest_parms);
2482
2483 return 0;
2484
2485 nla_put_failure:
2486 return -1;
2487 }
2488
2489 static const union nf_inet_addr any_addr;
2490
2491 static int
2492 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2493 const struct nf_conntrack_expect *exp)
2494 {
2495 struct nf_conn *master = exp->master;
2496 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2497 struct nf_conn_help *help;
2498 #ifdef CONFIG_NF_NAT_NEEDED
2499 struct nlattr *nest_parms;
2500 struct nf_conntrack_tuple nat_tuple = {};
2501 #endif
2502 struct nf_ct_helper_expectfn *expfn;
2503
2504 if (timeout < 0)
2505 timeout = 0;
2506
2507 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2508 goto nla_put_failure;
2509 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2510 goto nla_put_failure;
2511 if (ctnetlink_exp_dump_tuple(skb,
2512 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2513 CTA_EXPECT_MASTER) < 0)
2514 goto nla_put_failure;
2515
2516 #ifdef CONFIG_NF_NAT_NEEDED
2517 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2518 exp->saved_proto.all) {
2519 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2520 if (!nest_parms)
2521 goto nla_put_failure;
2522
2523 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2524 goto nla_put_failure;
2525
2526 nat_tuple.src.l3num = nf_ct_l3num(master);
2527 nat_tuple.src.u3 = exp->saved_addr;
2528 nat_tuple.dst.protonum = nf_ct_protonum(master);
2529 nat_tuple.src.u = exp->saved_proto;
2530
2531 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2532 CTA_EXPECT_NAT_TUPLE) < 0)
2533 goto nla_put_failure;
2534 nla_nest_end(skb, nest_parms);
2535 }
2536 #endif
2537 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2538 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2539 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2540 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2541 goto nla_put_failure;
2542 help = nfct_help(master);
2543 if (help) {
2544 struct nf_conntrack_helper *helper;
2545
2546 helper = rcu_dereference(help->helper);
2547 if (helper &&
2548 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2549 goto nla_put_failure;
2550 }
2551 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2552 if (expfn != NULL &&
2553 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2554 goto nla_put_failure;
2555
2556 return 0;
2557
2558 nla_put_failure:
2559 return -1;
2560 }
2561
2562 static int
2563 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2564 int event, const struct nf_conntrack_expect *exp)
2565 {
2566 struct nlmsghdr *nlh;
2567 struct nfgenmsg *nfmsg;
2568 unsigned int flags = portid ? NLM_F_MULTI : 0;
2569
2570 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2571 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2572 if (nlh == NULL)
2573 goto nlmsg_failure;
2574
2575 nfmsg = nlmsg_data(nlh);
2576 nfmsg->nfgen_family = exp->tuple.src.l3num;
2577 nfmsg->version = NFNETLINK_V0;
2578 nfmsg->res_id = 0;
2579
2580 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2581 goto nla_put_failure;
2582
2583 nlmsg_end(skb, nlh);
2584 return skb->len;
2585
2586 nlmsg_failure:
2587 nla_put_failure:
2588 nlmsg_cancel(skb, nlh);
2589 return -1;
2590 }
2591
2592 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2593 static int
2594 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2595 {
2596 struct nf_conntrack_expect *exp = item->exp;
2597 struct net *net = nf_ct_exp_net(exp);
2598 struct nlmsghdr *nlh;
2599 struct nfgenmsg *nfmsg;
2600 struct sk_buff *skb;
2601 unsigned int type, group;
2602 int flags = 0;
2603
2604 if (events & (1 << IPEXP_DESTROY)) {
2605 type = IPCTNL_MSG_EXP_DELETE;
2606 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2607 } else if (events & (1 << IPEXP_NEW)) {
2608 type = IPCTNL_MSG_EXP_NEW;
2609 flags = NLM_F_CREATE|NLM_F_EXCL;
2610 group = NFNLGRP_CONNTRACK_EXP_NEW;
2611 } else
2612 return 0;
2613
2614 if (!item->report && !nfnetlink_has_listeners(net, group))
2615 return 0;
2616
2617 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2618 if (skb == NULL)
2619 goto errout;
2620
2621 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2622 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2623 if (nlh == NULL)
2624 goto nlmsg_failure;
2625
2626 nfmsg = nlmsg_data(nlh);
2627 nfmsg->nfgen_family = exp->tuple.src.l3num;
2628 nfmsg->version = NFNETLINK_V0;
2629 nfmsg->res_id = 0;
2630
2631 rcu_read_lock();
2632 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2633 goto nla_put_failure;
2634 rcu_read_unlock();
2635
2636 nlmsg_end(skb, nlh);
2637 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2638 return 0;
2639
2640 nla_put_failure:
2641 rcu_read_unlock();
2642 nlmsg_cancel(skb, nlh);
2643 nlmsg_failure:
2644 kfree_skb(skb);
2645 errout:
2646 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2647 return 0;
2648 }
2649 #endif
2650 static int ctnetlink_exp_done(struct netlink_callback *cb)
2651 {
2652 if (cb->args[1])
2653 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2654 return 0;
2655 }
2656
2657 static int
2658 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2659 {
2660 struct net *net = sock_net(skb->sk);
2661 struct nf_conntrack_expect *exp, *last;
2662 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2663 u_int8_t l3proto = nfmsg->nfgen_family;
2664
2665 rcu_read_lock();
2666 last = (struct nf_conntrack_expect *)cb->args[1];
2667 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2668 restart:
2669 hlist_for_each_entry(exp, &nf_ct_expect_hash[cb->args[0]],
2670 hnode) {
2671 if (l3proto && exp->tuple.src.l3num != l3proto)
2672 continue;
2673
2674 if (!net_eq(nf_ct_net(exp->master), net))
2675 continue;
2676
2677 if (cb->args[1]) {
2678 if (exp != last)
2679 continue;
2680 cb->args[1] = 0;
2681 }
2682 if (ctnetlink_exp_fill_info(skb,
2683 NETLINK_CB(cb->skb).portid,
2684 cb->nlh->nlmsg_seq,
2685 IPCTNL_MSG_EXP_NEW,
2686 exp) < 0) {
2687 if (!refcount_inc_not_zero(&exp->use))
2688 continue;
2689 cb->args[1] = (unsigned long)exp;
2690 goto out;
2691 }
2692 }
2693 if (cb->args[1]) {
2694 cb->args[1] = 0;
2695 goto restart;
2696 }
2697 }
2698 out:
2699 rcu_read_unlock();
2700 if (last)
2701 nf_ct_expect_put(last);
2702
2703 return skb->len;
2704 }
2705
2706 static int
2707 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2708 {
2709 struct nf_conntrack_expect *exp, *last;
2710 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2711 struct nf_conn *ct = cb->data;
2712 struct nf_conn_help *help = nfct_help(ct);
2713 u_int8_t l3proto = nfmsg->nfgen_family;
2714
2715 if (cb->args[0])
2716 return 0;
2717
2718 rcu_read_lock();
2719 last = (struct nf_conntrack_expect *)cb->args[1];
2720 restart:
2721 hlist_for_each_entry(exp, &help->expectations, lnode) {
2722 if (l3proto && exp->tuple.src.l3num != l3proto)
2723 continue;
2724 if (cb->args[1]) {
2725 if (exp != last)
2726 continue;
2727 cb->args[1] = 0;
2728 }
2729 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2730 cb->nlh->nlmsg_seq,
2731 IPCTNL_MSG_EXP_NEW,
2732 exp) < 0) {
2733 if (!refcount_inc_not_zero(&exp->use))
2734 continue;
2735 cb->args[1] = (unsigned long)exp;
2736 goto out;
2737 }
2738 }
2739 if (cb->args[1]) {
2740 cb->args[1] = 0;
2741 goto restart;
2742 }
2743 cb->args[0] = 1;
2744 out:
2745 rcu_read_unlock();
2746 if (last)
2747 nf_ct_expect_put(last);
2748
2749 return skb->len;
2750 }
2751
2752 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2753 struct sk_buff *skb,
2754 const struct nlmsghdr *nlh,
2755 const struct nlattr * const cda[])
2756 {
2757 int err;
2758 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2759 u_int8_t u3 = nfmsg->nfgen_family;
2760 struct nf_conntrack_tuple tuple;
2761 struct nf_conntrack_tuple_hash *h;
2762 struct nf_conn *ct;
2763 struct nf_conntrack_zone zone;
2764 struct netlink_dump_control c = {
2765 .dump = ctnetlink_exp_ct_dump_table,
2766 .done = ctnetlink_exp_done,
2767 };
2768
2769 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2770 u3, NULL);
2771 if (err < 0)
2772 return err;
2773
2774 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2775 if (err < 0)
2776 return err;
2777
2778 h = nf_conntrack_find_get(net, &zone, &tuple);
2779 if (!h)
2780 return -ENOENT;
2781
2782 ct = nf_ct_tuplehash_to_ctrack(h);
2783 c.data = ct;
2784
2785 err = netlink_dump_start(ctnl, skb, nlh, &c);
2786 nf_ct_put(ct);
2787
2788 return err;
2789 }
2790
2791 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2792 struct sk_buff *skb, const struct nlmsghdr *nlh,
2793 const struct nlattr * const cda[])
2794 {
2795 struct nf_conntrack_tuple tuple;
2796 struct nf_conntrack_expect *exp;
2797 struct sk_buff *skb2;
2798 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2799 u_int8_t u3 = nfmsg->nfgen_family;
2800 struct nf_conntrack_zone zone;
2801 int err;
2802
2803 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2804 if (cda[CTA_EXPECT_MASTER])
2805 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2806 else {
2807 struct netlink_dump_control c = {
2808 .dump = ctnetlink_exp_dump_table,
2809 .done = ctnetlink_exp_done,
2810 };
2811 return netlink_dump_start(ctnl, skb, nlh, &c);
2812 }
2813 }
2814
2815 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2816 if (err < 0)
2817 return err;
2818
2819 if (cda[CTA_EXPECT_TUPLE])
2820 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2821 u3, NULL);
2822 else if (cda[CTA_EXPECT_MASTER])
2823 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2824 u3, NULL);
2825 else
2826 return -EINVAL;
2827
2828 if (err < 0)
2829 return err;
2830
2831 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2832 if (!exp)
2833 return -ENOENT;
2834
2835 if (cda[CTA_EXPECT_ID]) {
2836 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2837 if (ntohl(id) != (u32)(unsigned long)exp) {
2838 nf_ct_expect_put(exp);
2839 return -ENOENT;
2840 }
2841 }
2842
2843 err = -ENOMEM;
2844 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2845 if (skb2 == NULL) {
2846 nf_ct_expect_put(exp);
2847 goto out;
2848 }
2849
2850 rcu_read_lock();
2851 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2852 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2853 rcu_read_unlock();
2854 nf_ct_expect_put(exp);
2855 if (err <= 0)
2856 goto free;
2857
2858 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2859 if (err < 0)
2860 goto out;
2861
2862 return 0;
2863
2864 free:
2865 kfree_skb(skb2);
2866 out:
2867 /* this avoids a loop in nfnetlink. */
2868 return err == -EAGAIN ? -ENOBUFS : err;
2869 }
2870
2871 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2872 struct sk_buff *skb, const struct nlmsghdr *nlh,
2873 const struct nlattr * const cda[])
2874 {
2875 struct nf_conntrack_expect *exp;
2876 struct nf_conntrack_tuple tuple;
2877 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2878 struct hlist_node *next;
2879 u_int8_t u3 = nfmsg->nfgen_family;
2880 struct nf_conntrack_zone zone;
2881 unsigned int i;
2882 int err;
2883
2884 if (cda[CTA_EXPECT_TUPLE]) {
2885 /* delete a single expect by tuple */
2886 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2887 if (err < 0)
2888 return err;
2889
2890 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2891 u3, NULL);
2892 if (err < 0)
2893 return err;
2894
2895 /* bump usage count to 2 */
2896 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2897 if (!exp)
2898 return -ENOENT;
2899
2900 if (cda[CTA_EXPECT_ID]) {
2901 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2902 if (ntohl(id) != (u32)(unsigned long)exp) {
2903 nf_ct_expect_put(exp);
2904 return -ENOENT;
2905 }
2906 }
2907
2908 /* after list removal, usage count == 1 */
2909 spin_lock_bh(&nf_conntrack_expect_lock);
2910 if (del_timer(&exp->timeout)) {
2911 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2912 nlmsg_report(nlh));
2913 nf_ct_expect_put(exp);
2914 }
2915 spin_unlock_bh(&nf_conntrack_expect_lock);
2916 /* have to put what we 'get' above.
2917 * after this line usage count == 0 */
2918 nf_ct_expect_put(exp);
2919 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2920 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2921 struct nf_conn_help *m_help;
2922
2923 /* delete all expectations for this helper */
2924 spin_lock_bh(&nf_conntrack_expect_lock);
2925 for (i = 0; i < nf_ct_expect_hsize; i++) {
2926 hlist_for_each_entry_safe(exp, next,
2927 &nf_ct_expect_hash[i],
2928 hnode) {
2929
2930 if (!net_eq(nf_ct_exp_net(exp), net))
2931 continue;
2932
2933 m_help = nfct_help(exp->master);
2934 if (!strcmp(m_help->helper->name, name) &&
2935 del_timer(&exp->timeout)) {
2936 nf_ct_unlink_expect_report(exp,
2937 NETLINK_CB(skb).portid,
2938 nlmsg_report(nlh));
2939 nf_ct_expect_put(exp);
2940 }
2941 }
2942 }
2943 spin_unlock_bh(&nf_conntrack_expect_lock);
2944 } else {
2945 /* This basically means we have to flush everything*/
2946 spin_lock_bh(&nf_conntrack_expect_lock);
2947 for (i = 0; i < nf_ct_expect_hsize; i++) {
2948 hlist_for_each_entry_safe(exp, next,
2949 &nf_ct_expect_hash[i],
2950 hnode) {
2951
2952 if (!net_eq(nf_ct_exp_net(exp), net))
2953 continue;
2954
2955 if (del_timer(&exp->timeout)) {
2956 nf_ct_unlink_expect_report(exp,
2957 NETLINK_CB(skb).portid,
2958 nlmsg_report(nlh));
2959 nf_ct_expect_put(exp);
2960 }
2961 }
2962 }
2963 spin_unlock_bh(&nf_conntrack_expect_lock);
2964 }
2965
2966 return 0;
2967 }
2968 static int
2969 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2970 const struct nlattr * const cda[])
2971 {
2972 if (cda[CTA_EXPECT_TIMEOUT]) {
2973 if (!del_timer(&x->timeout))
2974 return -ETIME;
2975
2976 x->timeout.expires = jiffies +
2977 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2978 add_timer(&x->timeout);
2979 }
2980 return 0;
2981 }
2982
2983 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2984 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2985 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2986 };
2987
2988 static int
2989 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2990 struct nf_conntrack_expect *exp,
2991 u_int8_t u3)
2992 {
2993 #ifdef CONFIG_NF_NAT_NEEDED
2994 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2995 struct nf_conntrack_tuple nat_tuple = {};
2996 int err;
2997
2998 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2999 if (err < 0)
3000 return err;
3001
3002 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3003 return -EINVAL;
3004
3005 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3006 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3007 u3, NULL);
3008 if (err < 0)
3009 return err;
3010
3011 exp->saved_addr = nat_tuple.src.u3;
3012 exp->saved_proto = nat_tuple.src.u;
3013 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3014
3015 return 0;
3016 #else
3017 return -EOPNOTSUPP;
3018 #endif
3019 }
3020
3021 static struct nf_conntrack_expect *
3022 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3023 struct nf_conntrack_helper *helper,
3024 struct nf_conntrack_tuple *tuple,
3025 struct nf_conntrack_tuple *mask)
3026 {
3027 u_int32_t class = 0;
3028 struct nf_conntrack_expect *exp;
3029 struct nf_conn_help *help;
3030 int err;
3031
3032 help = nfct_help(ct);
3033 if (!help)
3034 return ERR_PTR(-EOPNOTSUPP);
3035
3036 if (cda[CTA_EXPECT_CLASS] && helper) {
3037 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3038 if (class > helper->expect_class_max)
3039 return ERR_PTR(-EINVAL);
3040 }
3041 exp = nf_ct_expect_alloc(ct);
3042 if (!exp)
3043 return ERR_PTR(-ENOMEM);
3044
3045 if (cda[CTA_EXPECT_FLAGS]) {
3046 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3047 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3048 } else {
3049 exp->flags = 0;
3050 }
3051 if (cda[CTA_EXPECT_FN]) {
3052 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3053 struct nf_ct_helper_expectfn *expfn;
3054
3055 expfn = nf_ct_helper_expectfn_find_by_name(name);
3056 if (expfn == NULL) {
3057 err = -EINVAL;
3058 goto err_out;
3059 }
3060 exp->expectfn = expfn->expectfn;
3061 } else
3062 exp->expectfn = NULL;
3063
3064 exp->class = class;
3065 exp->master = ct;
3066 exp->helper = helper;
3067 exp->tuple = *tuple;
3068 exp->mask.src.u3 = mask->src.u3;
3069 exp->mask.src.u.all = mask->src.u.all;
3070
3071 if (cda[CTA_EXPECT_NAT]) {
3072 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3073 exp, nf_ct_l3num(ct));
3074 if (err < 0)
3075 goto err_out;
3076 }
3077 return exp;
3078 err_out:
3079 nf_ct_expect_put(exp);
3080 return ERR_PTR(err);
3081 }
3082
3083 static int
3084 ctnetlink_create_expect(struct net *net,
3085 const struct nf_conntrack_zone *zone,
3086 const struct nlattr * const cda[],
3087 u_int8_t u3, u32 portid, int report)
3088 {
3089 struct nf_conntrack_tuple tuple, mask, master_tuple;
3090 struct nf_conntrack_tuple_hash *h = NULL;
3091 struct nf_conntrack_helper *helper = NULL;
3092 struct nf_conntrack_expect *exp;
3093 struct nf_conn *ct;
3094 int err;
3095
3096 /* caller guarantees that those three CTA_EXPECT_* exist */
3097 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3098 u3, NULL);
3099 if (err < 0)
3100 return err;
3101 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3102 u3, NULL);
3103 if (err < 0)
3104 return err;
3105 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3106 u3, NULL);
3107 if (err < 0)
3108 return err;
3109
3110 /* Look for master conntrack of this expectation */
3111 h = nf_conntrack_find_get(net, zone, &master_tuple);
3112 if (!h)
3113 return -ENOENT;
3114 ct = nf_ct_tuplehash_to_ctrack(h);
3115
3116 if (cda[CTA_EXPECT_HELP_NAME]) {
3117 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3118
3119 helper = __nf_conntrack_helper_find(helpname, u3,
3120 nf_ct_protonum(ct));
3121 if (helper == NULL) {
3122 #ifdef CONFIG_MODULES
3123 if (request_module("nfct-helper-%s", helpname) < 0) {
3124 err = -EOPNOTSUPP;
3125 goto err_ct;
3126 }
3127 helper = __nf_conntrack_helper_find(helpname, u3,
3128 nf_ct_protonum(ct));
3129 if (helper) {
3130 err = -EAGAIN;
3131 goto err_ct;
3132 }
3133 #endif
3134 err = -EOPNOTSUPP;
3135 goto err_ct;
3136 }
3137 }
3138
3139 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3140 if (IS_ERR(exp)) {
3141 err = PTR_ERR(exp);
3142 goto err_ct;
3143 }
3144
3145 err = nf_ct_expect_related_report(exp, portid, report);
3146 nf_ct_expect_put(exp);
3147 err_ct:
3148 nf_ct_put(ct);
3149 return err;
3150 }
3151
3152 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3153 struct sk_buff *skb, const struct nlmsghdr *nlh,
3154 const struct nlattr * const cda[])
3155 {
3156 struct nf_conntrack_tuple tuple;
3157 struct nf_conntrack_expect *exp;
3158 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3159 u_int8_t u3 = nfmsg->nfgen_family;
3160 struct nf_conntrack_zone zone;
3161 int err;
3162
3163 if (!cda[CTA_EXPECT_TUPLE]
3164 || !cda[CTA_EXPECT_MASK]
3165 || !cda[CTA_EXPECT_MASTER])
3166 return -EINVAL;
3167
3168 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3169 if (err < 0)
3170 return err;
3171
3172 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3173 u3, NULL);
3174 if (err < 0)
3175 return err;
3176
3177 spin_lock_bh(&nf_conntrack_expect_lock);
3178 exp = __nf_ct_expect_find(net, &zone, &tuple);
3179 if (!exp) {
3180 spin_unlock_bh(&nf_conntrack_expect_lock);
3181 err = -ENOENT;
3182 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3183 err = ctnetlink_create_expect(net, &zone, cda, u3,
3184 NETLINK_CB(skb).portid,
3185 nlmsg_report(nlh));
3186 }
3187 return err;
3188 }
3189
3190 err = -EEXIST;
3191 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3192 err = ctnetlink_change_expect(exp, cda);
3193 spin_unlock_bh(&nf_conntrack_expect_lock);
3194
3195 return err;
3196 }
3197
3198 static int
3199 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3200 const struct ip_conntrack_stat *st)
3201 {
3202 struct nlmsghdr *nlh;
3203 struct nfgenmsg *nfmsg;
3204 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3205
3206 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3207 IPCTNL_MSG_EXP_GET_STATS_CPU);
3208 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3209 if (nlh == NULL)
3210 goto nlmsg_failure;
3211
3212 nfmsg = nlmsg_data(nlh);
3213 nfmsg->nfgen_family = AF_UNSPEC;
3214 nfmsg->version = NFNETLINK_V0;
3215 nfmsg->res_id = htons(cpu);
3216
3217 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3218 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3219 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3220 goto nla_put_failure;
3221
3222 nlmsg_end(skb, nlh);
3223 return skb->len;
3224
3225 nla_put_failure:
3226 nlmsg_failure:
3227 nlmsg_cancel(skb, nlh);
3228 return -1;
3229 }
3230
3231 static int
3232 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3233 {
3234 int cpu;
3235 struct net *net = sock_net(skb->sk);
3236
3237 if (cb->args[0] == nr_cpu_ids)
3238 return 0;
3239
3240 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3241 const struct ip_conntrack_stat *st;
3242
3243 if (!cpu_possible(cpu))
3244 continue;
3245
3246 st = per_cpu_ptr(net->ct.stat, cpu);
3247 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3248 cb->nlh->nlmsg_seq,
3249 cpu, st) < 0)
3250 break;
3251 }
3252 cb->args[0] = cpu;
3253
3254 return skb->len;
3255 }
3256
3257 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3258 struct sk_buff *skb,
3259 const struct nlmsghdr *nlh,
3260 const struct nlattr * const cda[])
3261 {
3262 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3263 struct netlink_dump_control c = {
3264 .dump = ctnetlink_exp_stat_cpu_dump,
3265 };
3266 return netlink_dump_start(ctnl, skb, nlh, &c);
3267 }
3268
3269 return 0;
3270 }
3271
3272 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3273 static struct nf_ct_event_notifier ctnl_notifier = {
3274 .fcn = ctnetlink_conntrack_event,
3275 };
3276
3277 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3278 .fcn = ctnetlink_expect_event,
3279 };
3280 #endif
3281
3282 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3283 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3284 .attr_count = CTA_MAX,
3285 .policy = ct_nla_policy },
3286 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3287 .attr_count = CTA_MAX,
3288 .policy = ct_nla_policy },
3289 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3290 .attr_count = CTA_MAX,
3291 .policy = ct_nla_policy },
3292 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3293 .attr_count = CTA_MAX,
3294 .policy = ct_nla_policy },
3295 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3296 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3297 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3298 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3299 };
3300
3301 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3302 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3303 .attr_count = CTA_EXPECT_MAX,
3304 .policy = exp_nla_policy },
3305 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3306 .attr_count = CTA_EXPECT_MAX,
3307 .policy = exp_nla_policy },
3308 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3309 .attr_count = CTA_EXPECT_MAX,
3310 .policy = exp_nla_policy },
3311 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3312 };
3313
3314 static const struct nfnetlink_subsystem ctnl_subsys = {
3315 .name = "conntrack",
3316 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3317 .cb_count = IPCTNL_MSG_MAX,
3318 .cb = ctnl_cb,
3319 };
3320
3321 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3322 .name = "conntrack_expect",
3323 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3324 .cb_count = IPCTNL_MSG_EXP_MAX,
3325 .cb = ctnl_exp_cb,
3326 };
3327
3328 MODULE_ALIAS("ip_conntrack_netlink");
3329 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3330 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3331
3332 static int __net_init ctnetlink_net_init(struct net *net)
3333 {
3334 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3335 int ret;
3336
3337 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3338 if (ret < 0) {
3339 pr_err("ctnetlink_init: cannot register notifier.\n");
3340 goto err_out;
3341 }
3342
3343 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3344 if (ret < 0) {
3345 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3346 goto err_unreg_notifier;
3347 }
3348 #endif
3349 return 0;
3350
3351 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3352 err_unreg_notifier:
3353 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3354 err_out:
3355 return ret;
3356 #endif
3357 }
3358
3359 static void ctnetlink_net_exit(struct net *net)
3360 {
3361 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3362 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3363 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3364 #endif
3365 }
3366
3367 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3368 {
3369 struct net *net;
3370
3371 list_for_each_entry(net, net_exit_list, exit_list)
3372 ctnetlink_net_exit(net);
3373 }
3374
3375 static struct pernet_operations ctnetlink_net_ops = {
3376 .init = ctnetlink_net_init,
3377 .exit_batch = ctnetlink_net_exit_batch,
3378 };
3379
3380 static int __init ctnetlink_init(void)
3381 {
3382 int ret;
3383
3384 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3385 ret = nfnetlink_subsys_register(&ctnl_subsys);
3386 if (ret < 0) {
3387 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3388 goto err_out;
3389 }
3390
3391 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3392 if (ret < 0) {
3393 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3394 goto err_unreg_subsys;
3395 }
3396
3397 ret = register_pernet_subsys(&ctnetlink_net_ops);
3398 if (ret < 0) {
3399 pr_err("ctnetlink_init: cannot register pernet operations\n");
3400 goto err_unreg_exp_subsys;
3401 }
3402 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3403 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3404 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3405 #endif
3406 return 0;
3407
3408 err_unreg_exp_subsys:
3409 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3410 err_unreg_subsys:
3411 nfnetlink_subsys_unregister(&ctnl_subsys);
3412 err_out:
3413 return ret;
3414 }
3415
3416 static void __exit ctnetlink_exit(void)
3417 {
3418 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3419
3420 unregister_pernet_subsys(&ctnetlink_net_ops);
3421 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3422 nfnetlink_subsys_unregister(&ctnl_subsys);
3423 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3424 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3425 #endif
3426 synchronize_rcu();
3427 }
3428
3429 module_init(ctnetlink_init);
3430 module_exit(ctnetlink_exit);