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