]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_netlink.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nf_conntrack_netlink.c
1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
8 *
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 *
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 *
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
16 */
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
35 #include <net/sock.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_seqadj.h>
49 #include <net/netfilter/nf_conntrack_synproxy.h>
50 #ifdef CONFIG_NF_NAT_NEEDED
51 #include <net/netfilter/nf_nat_core.h>
52 #include <net/netfilter/nf_nat_l4proto.h>
53 #include <net/netfilter/nf_nat_helper.h>
54 #endif
55
56 #include <linux/netfilter/nfnetlink.h>
57 #include <linux/netfilter/nfnetlink_conntrack.h>
58
59 MODULE_LICENSE("GPL");
60
61 static char __initdata version[] = "0.93";
62
63 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
64 const struct nf_conntrack_tuple *tuple,
65 struct nf_conntrack_l4proto *l4proto)
66 {
67 int ret = 0;
68 struct nlattr *nest_parms;
69
70 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
71 if (!nest_parms)
72 goto nla_put_failure;
73 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
74 goto nla_put_failure;
75
76 if (likely(l4proto->tuple_to_nlattr))
77 ret = l4proto->tuple_to_nlattr(skb, tuple);
78
79 nla_nest_end(skb, nest_parms);
80
81 return ret;
82
83 nla_put_failure:
84 return -1;
85 }
86
87 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
88 const struct nf_conntrack_tuple *tuple,
89 struct nf_conntrack_l3proto *l3proto)
90 {
91 int ret = 0;
92 struct nlattr *nest_parms;
93
94 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
95 if (!nest_parms)
96 goto nla_put_failure;
97
98 if (likely(l3proto->tuple_to_nlattr))
99 ret = l3proto->tuple_to_nlattr(skb, tuple);
100
101 nla_nest_end(skb, nest_parms);
102
103 return ret;
104
105 nla_put_failure:
106 return -1;
107 }
108
109 static int ctnetlink_dump_tuples(struct sk_buff *skb,
110 const struct nf_conntrack_tuple *tuple)
111 {
112 int ret;
113 struct nf_conntrack_l3proto *l3proto;
114 struct nf_conntrack_l4proto *l4proto;
115
116 rcu_read_lock();
117 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
118 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
119
120 if (ret >= 0) {
121 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
122 tuple->dst.protonum);
123 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
124 }
125 rcu_read_unlock();
126 return ret;
127 }
128
129 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
130 const struct nf_conntrack_zone *zone, int dir)
131 {
132 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
133 return 0;
134 if (nla_put_be16(skb, attrtype, htons(zone->id)))
135 goto nla_put_failure;
136 return 0;
137
138 nla_put_failure:
139 return -1;
140 }
141
142 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
143 {
144 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
145 goto nla_put_failure;
146 return 0;
147
148 nla_put_failure:
149 return -1;
150 }
151
152 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
153 {
154 long timeout = nf_ct_expires(ct) / HZ;
155
156 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
157 goto nla_put_failure;
158 return 0;
159
160 nla_put_failure:
161 return -1;
162 }
163
164 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
165 {
166 struct nf_conntrack_l4proto *l4proto;
167 struct nlattr *nest_proto;
168 int ret;
169
170 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
171 if (!l4proto->to_nlattr)
172 return 0;
173
174 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
175 if (!nest_proto)
176 goto nla_put_failure;
177
178 ret = l4proto->to_nlattr(skb, nest_proto, ct);
179
180 nla_nest_end(skb, nest_proto);
181
182 return ret;
183
184 nla_put_failure:
185 return -1;
186 }
187
188 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
189 const struct nf_conn *ct)
190 {
191 struct nlattr *nest_helper;
192 const struct nf_conn_help *help = nfct_help(ct);
193 struct nf_conntrack_helper *helper;
194
195 if (!help)
196 return 0;
197
198 helper = rcu_dereference(help->helper);
199 if (!helper)
200 goto out;
201
202 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
203 if (!nest_helper)
204 goto nla_put_failure;
205 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
206 goto nla_put_failure;
207
208 if (helper->to_nlattr)
209 helper->to_nlattr(skb, ct);
210
211 nla_nest_end(skb, nest_helper);
212 out:
213 return 0;
214
215 nla_put_failure:
216 return -1;
217 }
218
219 static int
220 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
221 enum ip_conntrack_dir dir, int type)
222 {
223 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
224 struct nf_conn_counter *counter = acct->counter;
225 struct nlattr *nest_count;
226 u64 pkts, bytes;
227
228 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
229 pkts = atomic64_xchg(&counter[dir].packets, 0);
230 bytes = atomic64_xchg(&counter[dir].bytes, 0);
231 } else {
232 pkts = atomic64_read(&counter[dir].packets);
233 bytes = atomic64_read(&counter[dir].bytes);
234 }
235
236 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
237 if (!nest_count)
238 goto nla_put_failure;
239
240 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
241 CTA_COUNTERS_PAD) ||
242 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
243 CTA_COUNTERS_PAD))
244 goto nla_put_failure;
245
246 nla_nest_end(skb, nest_count);
247
248 return 0;
249
250 nla_put_failure:
251 return -1;
252 }
253
254 static int
255 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
256 {
257 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
258
259 if (!acct)
260 return 0;
261
262 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
263 return -1;
264 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
265 return -1;
266
267 return 0;
268 }
269
270 static int
271 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
272 {
273 struct nlattr *nest_count;
274 const struct nf_conn_tstamp *tstamp;
275
276 tstamp = nf_conn_tstamp_find(ct);
277 if (!tstamp)
278 return 0;
279
280 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
281 if (!nest_count)
282 goto nla_put_failure;
283
284 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
285 CTA_TIMESTAMP_PAD) ||
286 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
287 cpu_to_be64(tstamp->stop),
288 CTA_TIMESTAMP_PAD)))
289 goto nla_put_failure;
290 nla_nest_end(skb, nest_count);
291
292 return 0;
293
294 nla_put_failure:
295 return -1;
296 }
297
298 #ifdef CONFIG_NF_CONNTRACK_MARK
299 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
300 {
301 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
302 goto nla_put_failure;
303 return 0;
304
305 nla_put_failure:
306 return -1;
307 }
308 #else
309 #define ctnetlink_dump_mark(a, b) (0)
310 #endif
311
312 #ifdef CONFIG_NF_CONNTRACK_SECMARK
313 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
314 {
315 struct nlattr *nest_secctx;
316 int len, ret;
317 char *secctx;
318
319 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
320 if (ret)
321 return 0;
322
323 ret = -1;
324 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
325 if (!nest_secctx)
326 goto nla_put_failure;
327
328 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
329 goto nla_put_failure;
330 nla_nest_end(skb, nest_secctx);
331
332 ret = 0;
333 nla_put_failure:
334 security_release_secctx(secctx, len);
335 return ret;
336 }
337 #else
338 #define ctnetlink_dump_secctx(a, b) (0)
339 #endif
340
341 #ifdef CONFIG_NF_CONNTRACK_LABELS
342 static inline int ctnetlink_label_size(const struct nf_conn *ct)
343 {
344 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
345
346 if (!labels)
347 return 0;
348 return nla_total_size(sizeof(labels->bits));
349 }
350
351 static int
352 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
353 {
354 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
355 unsigned int i;
356
357 if (!labels)
358 return 0;
359
360 i = 0;
361 do {
362 if (labels->bits[i] != 0)
363 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
364 labels->bits);
365 i++;
366 } while (i < ARRAY_SIZE(labels->bits));
367
368 return 0;
369 }
370 #else
371 #define ctnetlink_dump_labels(a, b) (0)
372 #define ctnetlink_label_size(a) (0)
373 #endif
374
375 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
376
377 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
378 {
379 struct nlattr *nest_parms;
380
381 if (!(ct->status & IPS_EXPECTED))
382 return 0;
383
384 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
385 if (!nest_parms)
386 goto nla_put_failure;
387 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
388 goto nla_put_failure;
389 nla_nest_end(skb, nest_parms);
390
391 return 0;
392
393 nla_put_failure:
394 return -1;
395 }
396
397 static int
398 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
399 {
400 struct nlattr *nest_parms;
401
402 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
403 if (!nest_parms)
404 goto nla_put_failure;
405
406 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
407 htonl(seq->correction_pos)) ||
408 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
409 htonl(seq->offset_before)) ||
410 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
411 htonl(seq->offset_after)))
412 goto nla_put_failure;
413
414 nla_nest_end(skb, nest_parms);
415
416 return 0;
417
418 nla_put_failure:
419 return -1;
420 }
421
422 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
423 {
424 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
425 struct nf_ct_seqadj *seq;
426
427 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
428 return 0;
429
430 spin_lock_bh(&ct->lock);
431 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
432 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
433 goto err;
434
435 seq = &seqadj->seq[IP_CT_DIR_REPLY];
436 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
437 goto err;
438
439 spin_unlock_bh(&ct->lock);
440 return 0;
441 err:
442 spin_unlock_bh(&ct->lock);
443 return -1;
444 }
445
446 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
447 {
448 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
449 goto nla_put_failure;
450 return 0;
451
452 nla_put_failure:
453 return -1;
454 }
455
456 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
457 {
458 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
459 goto nla_put_failure;
460 return 0;
461
462 nla_put_failure:
463 return -1;
464 }
465
466 static int
467 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
468 struct nf_conn *ct)
469 {
470 const struct nf_conntrack_zone *zone;
471 struct nlmsghdr *nlh;
472 struct nfgenmsg *nfmsg;
473 struct nlattr *nest_parms;
474 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
475
476 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
477 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
478 if (nlh == NULL)
479 goto nlmsg_failure;
480
481 nfmsg = nlmsg_data(nlh);
482 nfmsg->nfgen_family = nf_ct_l3num(ct);
483 nfmsg->version = NFNETLINK_V0;
484 nfmsg->res_id = 0;
485
486 zone = nf_ct_zone(ct);
487
488 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
489 if (!nest_parms)
490 goto nla_put_failure;
491 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
492 goto nla_put_failure;
493 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
494 NF_CT_ZONE_DIR_ORIG) < 0)
495 goto nla_put_failure;
496 nla_nest_end(skb, nest_parms);
497
498 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
499 if (!nest_parms)
500 goto nla_put_failure;
501 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
502 goto nla_put_failure;
503 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
504 NF_CT_ZONE_DIR_REPL) < 0)
505 goto nla_put_failure;
506 nla_nest_end(skb, nest_parms);
507
508 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
509 NF_CT_DEFAULT_ZONE_DIR) < 0)
510 goto nla_put_failure;
511
512 if (ctnetlink_dump_status(skb, ct) < 0 ||
513 ctnetlink_dump_timeout(skb, ct) < 0 ||
514 ctnetlink_dump_acct(skb, ct, type) < 0 ||
515 ctnetlink_dump_timestamp(skb, ct) < 0 ||
516 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
517 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
518 ctnetlink_dump_mark(skb, ct) < 0 ||
519 ctnetlink_dump_secctx(skb, ct) < 0 ||
520 ctnetlink_dump_labels(skb, ct) < 0 ||
521 ctnetlink_dump_id(skb, ct) < 0 ||
522 ctnetlink_dump_use(skb, ct) < 0 ||
523 ctnetlink_dump_master(skb, ct) < 0 ||
524 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
525 goto nla_put_failure;
526
527 nlmsg_end(skb, nlh);
528 return skb->len;
529
530 nlmsg_failure:
531 nla_put_failure:
532 nlmsg_cancel(skb, nlh);
533 return -1;
534 }
535
536 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
537 {
538 struct nf_conntrack_l3proto *l3proto;
539 struct nf_conntrack_l4proto *l4proto;
540 size_t len = 0;
541
542 rcu_read_lock();
543 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
544 len += l3proto->nla_size;
545
546 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
547 len += l4proto->nla_size;
548 rcu_read_unlock();
549
550 return len;
551 }
552
553 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
554 {
555 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
556 return 0;
557 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
558 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
559 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
560 ;
561 }
562
563 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
564 {
565 #ifdef CONFIG_NF_CONNTRACK_SECMARK
566 int len, ret;
567
568 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
569 if (ret)
570 return 0;
571
572 return nla_total_size(0) /* CTA_SECCTX */
573 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
574 #else
575 return 0;
576 #endif
577 }
578
579 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
580 {
581 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
582 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
583 return 0;
584 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
585 #else
586 return 0;
587 #endif
588 }
589
590 #ifdef CONFIG_NF_CONNTRACK_EVENTS
591 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
592 {
593 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
594 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
595 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
596 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
597 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
598 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
599 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
600 + ctnetlink_acct_size(ct)
601 + ctnetlink_timestamp_size(ct)
602 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
603 + nla_total_size(0) /* CTA_PROTOINFO */
604 + nla_total_size(0) /* CTA_HELP */
605 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
606 + ctnetlink_secctx_size(ct)
607 #ifdef CONFIG_NF_NAT_NEEDED
608 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
609 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
610 #endif
611 #ifdef CONFIG_NF_CONNTRACK_MARK
612 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
613 #endif
614 #ifdef CONFIG_NF_CONNTRACK_ZONES
615 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
616 #endif
617 + ctnetlink_proto_size(ct)
618 + ctnetlink_label_size(ct)
619 ;
620 }
621
622 static int
623 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
624 {
625 const struct nf_conntrack_zone *zone;
626 struct net *net;
627 struct nlmsghdr *nlh;
628 struct nfgenmsg *nfmsg;
629 struct nlattr *nest_parms;
630 struct nf_conn *ct = item->ct;
631 struct sk_buff *skb;
632 unsigned int type;
633 unsigned int flags = 0, group;
634 int err;
635
636 if (events & (1 << IPCT_DESTROY)) {
637 type = IPCTNL_MSG_CT_DELETE;
638 group = NFNLGRP_CONNTRACK_DESTROY;
639 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
640 type = IPCTNL_MSG_CT_NEW;
641 flags = NLM_F_CREATE|NLM_F_EXCL;
642 group = NFNLGRP_CONNTRACK_NEW;
643 } else if (events) {
644 type = IPCTNL_MSG_CT_NEW;
645 group = NFNLGRP_CONNTRACK_UPDATE;
646 } else
647 return 0;
648
649 net = nf_ct_net(ct);
650 if (!item->report && !nfnetlink_has_listeners(net, group))
651 return 0;
652
653 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
654 if (skb == NULL)
655 goto errout;
656
657 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
658 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
659 if (nlh == NULL)
660 goto nlmsg_failure;
661
662 nfmsg = nlmsg_data(nlh);
663 nfmsg->nfgen_family = nf_ct_l3num(ct);
664 nfmsg->version = NFNETLINK_V0;
665 nfmsg->res_id = 0;
666
667 rcu_read_lock();
668 zone = nf_ct_zone(ct);
669
670 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
671 if (!nest_parms)
672 goto nla_put_failure;
673 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
674 goto nla_put_failure;
675 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
676 NF_CT_ZONE_DIR_ORIG) < 0)
677 goto nla_put_failure;
678 nla_nest_end(skb, nest_parms);
679
680 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
681 if (!nest_parms)
682 goto nla_put_failure;
683 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
684 goto nla_put_failure;
685 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
686 NF_CT_ZONE_DIR_REPL) < 0)
687 goto nla_put_failure;
688 nla_nest_end(skb, nest_parms);
689
690 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
691 NF_CT_DEFAULT_ZONE_DIR) < 0)
692 goto nla_put_failure;
693
694 if (ctnetlink_dump_id(skb, ct) < 0)
695 goto nla_put_failure;
696
697 if (ctnetlink_dump_status(skb, ct) < 0)
698 goto nla_put_failure;
699
700 if (events & (1 << IPCT_DESTROY)) {
701 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
702 ctnetlink_dump_timestamp(skb, ct) < 0)
703 goto nla_put_failure;
704 } else {
705 if (ctnetlink_dump_timeout(skb, ct) < 0)
706 goto nla_put_failure;
707
708 if (events & (1 << IPCT_PROTOINFO)
709 && ctnetlink_dump_protoinfo(skb, ct) < 0)
710 goto nla_put_failure;
711
712 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
713 && ctnetlink_dump_helpinfo(skb, ct) < 0)
714 goto nla_put_failure;
715
716 #ifdef CONFIG_NF_CONNTRACK_SECMARK
717 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
718 && ctnetlink_dump_secctx(skb, ct) < 0)
719 goto nla_put_failure;
720 #endif
721 if (events & (1 << IPCT_LABEL) &&
722 ctnetlink_dump_labels(skb, ct) < 0)
723 goto nla_put_failure;
724
725 if (events & (1 << IPCT_RELATED) &&
726 ctnetlink_dump_master(skb, ct) < 0)
727 goto nla_put_failure;
728
729 if (events & (1 << IPCT_SEQADJ) &&
730 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
731 goto nla_put_failure;
732 }
733
734 #ifdef CONFIG_NF_CONNTRACK_MARK
735 if ((events & (1 << IPCT_MARK) || ct->mark)
736 && ctnetlink_dump_mark(skb, ct) < 0)
737 goto nla_put_failure;
738 #endif
739 rcu_read_unlock();
740
741 nlmsg_end(skb, nlh);
742 err = nfnetlink_send(skb, net, item->portid, group, item->report,
743 GFP_ATOMIC);
744 if (err == -ENOBUFS || err == -EAGAIN)
745 return -ENOBUFS;
746
747 return 0;
748
749 nla_put_failure:
750 rcu_read_unlock();
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 struct nlattr *tb[CTA_PROTO_MAX+1];
945 struct nf_conntrack_l4proto *l4proto;
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 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1569
1570 ct->timeout = nfct_time_stamp + timeout * HZ;
1571
1572 if (test_bit(IPS_DYING_BIT, &ct->status))
1573 return -ETIME;
1574
1575 return 0;
1576 }
1577
1578 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1579 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1580 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1581 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1582 };
1583
1584 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1585 const struct nlattr * const cda[])
1586 {
1587 const struct nlattr *attr = cda[CTA_PROTOINFO];
1588 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1589 struct nf_conntrack_l4proto *l4proto;
1590 int err = 0;
1591
1592 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1593 NULL);
1594 if (err < 0)
1595 return err;
1596
1597 rcu_read_lock();
1598 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1599 if (l4proto->from_nlattr)
1600 err = l4proto->from_nlattr(tb, ct);
1601 rcu_read_unlock();
1602
1603 return err;
1604 }
1605
1606 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1607 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1608 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1609 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1610 };
1611
1612 static int change_seq_adj(struct nf_ct_seqadj *seq,
1613 const struct nlattr * const attr)
1614 {
1615 int err;
1616 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1617
1618 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1619 if (err < 0)
1620 return err;
1621
1622 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1623 return -EINVAL;
1624
1625 seq->correction_pos =
1626 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1627
1628 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1629 return -EINVAL;
1630
1631 seq->offset_before =
1632 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1633
1634 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1635 return -EINVAL;
1636
1637 seq->offset_after =
1638 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1639
1640 return 0;
1641 }
1642
1643 static int
1644 ctnetlink_change_seq_adj(struct nf_conn *ct,
1645 const struct nlattr * const cda[])
1646 {
1647 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1648 int ret = 0;
1649
1650 if (!seqadj)
1651 return 0;
1652
1653 spin_lock_bh(&ct->lock);
1654 if (cda[CTA_SEQ_ADJ_ORIG]) {
1655 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1656 cda[CTA_SEQ_ADJ_ORIG]);
1657 if (ret < 0)
1658 goto err;
1659
1660 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1661 }
1662
1663 if (cda[CTA_SEQ_ADJ_REPLY]) {
1664 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1665 cda[CTA_SEQ_ADJ_REPLY]);
1666 if (ret < 0)
1667 goto err;
1668
1669 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1670 }
1671
1672 spin_unlock_bh(&ct->lock);
1673 return 0;
1674 err:
1675 spin_unlock_bh(&ct->lock);
1676 return ret;
1677 }
1678
1679 static int
1680 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1681 {
1682 #ifdef CONFIG_NF_CONNTRACK_LABELS
1683 size_t len = nla_len(cda[CTA_LABELS]);
1684 const void *mask = cda[CTA_LABELS_MASK];
1685
1686 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1687 return -EINVAL;
1688
1689 if (mask) {
1690 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1691 nla_len(cda[CTA_LABELS_MASK]) != len)
1692 return -EINVAL;
1693 mask = nla_data(cda[CTA_LABELS_MASK]);
1694 }
1695
1696 len /= sizeof(u32);
1697
1698 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1699 #else
1700 return -EOPNOTSUPP;
1701 #endif
1702 }
1703
1704 static int
1705 ctnetlink_change_conntrack(struct nf_conn *ct,
1706 const struct nlattr * const cda[])
1707 {
1708 int err;
1709
1710 /* only allow NAT changes and master assignation for new conntracks */
1711 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1712 return -EOPNOTSUPP;
1713
1714 if (cda[CTA_HELP]) {
1715 err = ctnetlink_change_helper(ct, cda);
1716 if (err < 0)
1717 return err;
1718 }
1719
1720 if (cda[CTA_TIMEOUT]) {
1721 err = ctnetlink_change_timeout(ct, cda);
1722 if (err < 0)
1723 return err;
1724 }
1725
1726 if (cda[CTA_STATUS]) {
1727 err = ctnetlink_change_status(ct, cda);
1728 if (err < 0)
1729 return err;
1730 }
1731
1732 if (cda[CTA_PROTOINFO]) {
1733 err = ctnetlink_change_protoinfo(ct, cda);
1734 if (err < 0)
1735 return err;
1736 }
1737
1738 #if defined(CONFIG_NF_CONNTRACK_MARK)
1739 if (cda[CTA_MARK])
1740 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1741 #endif
1742
1743 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1744 err = ctnetlink_change_seq_adj(ct, cda);
1745 if (err < 0)
1746 return err;
1747 }
1748
1749 if (cda[CTA_LABELS]) {
1750 err = ctnetlink_attach_labels(ct, cda);
1751 if (err < 0)
1752 return err;
1753 }
1754
1755 return 0;
1756 }
1757
1758 static struct nf_conn *
1759 ctnetlink_create_conntrack(struct net *net,
1760 const struct nf_conntrack_zone *zone,
1761 const struct nlattr * const cda[],
1762 struct nf_conntrack_tuple *otuple,
1763 struct nf_conntrack_tuple *rtuple,
1764 u8 u3)
1765 {
1766 struct nf_conn *ct;
1767 int err = -EINVAL;
1768 struct nf_conntrack_helper *helper;
1769 struct nf_conn_tstamp *tstamp;
1770
1771 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1772 if (IS_ERR(ct))
1773 return ERR_PTR(-ENOMEM);
1774
1775 if (!cda[CTA_TIMEOUT])
1776 goto err1;
1777
1778 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1779
1780 rcu_read_lock();
1781 if (cda[CTA_HELP]) {
1782 char *helpname = NULL;
1783 struct nlattr *helpinfo = NULL;
1784
1785 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1786 if (err < 0)
1787 goto err2;
1788
1789 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1790 nf_ct_protonum(ct));
1791 if (helper == NULL) {
1792 rcu_read_unlock();
1793 #ifdef CONFIG_MODULES
1794 if (request_module("nfct-helper-%s", helpname) < 0) {
1795 err = -EOPNOTSUPP;
1796 goto err1;
1797 }
1798
1799 rcu_read_lock();
1800 helper = __nf_conntrack_helper_find(helpname,
1801 nf_ct_l3num(ct),
1802 nf_ct_protonum(ct));
1803 if (helper) {
1804 err = -EAGAIN;
1805 goto err2;
1806 }
1807 rcu_read_unlock();
1808 #endif
1809 err = -EOPNOTSUPP;
1810 goto err1;
1811 } else {
1812 struct nf_conn_help *help;
1813
1814 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1815 if (help == NULL) {
1816 err = -ENOMEM;
1817 goto err2;
1818 }
1819 /* set private helper data if allowed. */
1820 if (helper->from_nlattr)
1821 helper->from_nlattr(helpinfo, ct);
1822
1823 /* not in hash table yet so not strictly necessary */
1824 RCU_INIT_POINTER(help->helper, helper);
1825 }
1826 } else {
1827 /* try an implicit helper assignation */
1828 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1829 if (err < 0)
1830 goto err2;
1831 }
1832
1833 err = ctnetlink_setup_nat(ct, cda);
1834 if (err < 0)
1835 goto err2;
1836
1837 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1838 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1839 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1840 nf_ct_labels_ext_add(ct);
1841 nfct_seqadj_ext_add(ct);
1842 nfct_synproxy_ext_add(ct);
1843
1844 /* we must add conntrack extensions before confirmation. */
1845 ct->status |= IPS_CONFIRMED;
1846
1847 if (cda[CTA_STATUS]) {
1848 err = ctnetlink_change_status(ct, cda);
1849 if (err < 0)
1850 goto err2;
1851 }
1852
1853 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1854 err = ctnetlink_change_seq_adj(ct, cda);
1855 if (err < 0)
1856 goto err2;
1857 }
1858
1859 memset(&ct->proto, 0, sizeof(ct->proto));
1860 if (cda[CTA_PROTOINFO]) {
1861 err = ctnetlink_change_protoinfo(ct, cda);
1862 if (err < 0)
1863 goto err2;
1864 }
1865
1866 #if defined(CONFIG_NF_CONNTRACK_MARK)
1867 if (cda[CTA_MARK])
1868 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1869 #endif
1870
1871 /* setup master conntrack: this is a confirmed expectation */
1872 if (cda[CTA_TUPLE_MASTER]) {
1873 struct nf_conntrack_tuple master;
1874 struct nf_conntrack_tuple_hash *master_h;
1875 struct nf_conn *master_ct;
1876
1877 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1878 u3, NULL);
1879 if (err < 0)
1880 goto err2;
1881
1882 master_h = nf_conntrack_find_get(net, zone, &master);
1883 if (master_h == NULL) {
1884 err = -ENOENT;
1885 goto err2;
1886 }
1887 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1888 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1889 ct->master = master_ct;
1890 }
1891 tstamp = nf_conn_tstamp_find(ct);
1892 if (tstamp)
1893 tstamp->start = ktime_get_real_ns();
1894
1895 err = nf_conntrack_hash_check_insert(ct);
1896 if (err < 0)
1897 goto err2;
1898
1899 rcu_read_unlock();
1900
1901 return ct;
1902
1903 err2:
1904 rcu_read_unlock();
1905 err1:
1906 nf_conntrack_free(ct);
1907 return ERR_PTR(err);
1908 }
1909
1910 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1911 struct sk_buff *skb,
1912 const struct nlmsghdr *nlh,
1913 const struct nlattr * const cda[],
1914 struct netlink_ext_ack *extack)
1915 {
1916 struct nf_conntrack_tuple otuple, rtuple;
1917 struct nf_conntrack_tuple_hash *h = NULL;
1918 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1919 struct nf_conn *ct;
1920 u_int8_t u3 = nfmsg->nfgen_family;
1921 struct nf_conntrack_zone zone;
1922 int err;
1923
1924 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1925 if (err < 0)
1926 return err;
1927
1928 if (cda[CTA_TUPLE_ORIG]) {
1929 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1930 u3, &zone);
1931 if (err < 0)
1932 return err;
1933 }
1934
1935 if (cda[CTA_TUPLE_REPLY]) {
1936 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1937 u3, &zone);
1938 if (err < 0)
1939 return err;
1940 }
1941
1942 if (cda[CTA_TUPLE_ORIG])
1943 h = nf_conntrack_find_get(net, &zone, &otuple);
1944 else if (cda[CTA_TUPLE_REPLY])
1945 h = nf_conntrack_find_get(net, &zone, &rtuple);
1946
1947 if (h == NULL) {
1948 err = -ENOENT;
1949 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1950 enum ip_conntrack_events events;
1951
1952 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1953 return -EINVAL;
1954 if (otuple.dst.protonum != rtuple.dst.protonum)
1955 return -EINVAL;
1956
1957 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1958 &rtuple, u3);
1959 if (IS_ERR(ct))
1960 return PTR_ERR(ct);
1961
1962 err = 0;
1963 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1964 events = 1 << IPCT_RELATED;
1965 else
1966 events = 1 << IPCT_NEW;
1967
1968 if (cda[CTA_LABELS] &&
1969 ctnetlink_attach_labels(ct, cda) == 0)
1970 events |= (1 << IPCT_LABEL);
1971
1972 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1973 (1 << IPCT_ASSURED) |
1974 (1 << IPCT_HELPER) |
1975 (1 << IPCT_PROTOINFO) |
1976 (1 << IPCT_SEQADJ) |
1977 (1 << IPCT_MARK) | events,
1978 ct, NETLINK_CB(skb).portid,
1979 nlmsg_report(nlh));
1980 nf_ct_put(ct);
1981 }
1982
1983 return err;
1984 }
1985 /* implicit 'else' */
1986
1987 err = -EEXIST;
1988 ct = nf_ct_tuplehash_to_ctrack(h);
1989 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1990 err = ctnetlink_change_conntrack(ct, cda);
1991 if (err == 0) {
1992 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1993 (1 << IPCT_ASSURED) |
1994 (1 << IPCT_HELPER) |
1995 (1 << IPCT_LABEL) |
1996 (1 << IPCT_PROTOINFO) |
1997 (1 << IPCT_SEQADJ) |
1998 (1 << IPCT_MARK),
1999 ct, NETLINK_CB(skb).portid,
2000 nlmsg_report(nlh));
2001 }
2002 }
2003
2004 nf_ct_put(ct);
2005 return err;
2006 }
2007
2008 static int
2009 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2010 __u16 cpu, const struct ip_conntrack_stat *st)
2011 {
2012 struct nlmsghdr *nlh;
2013 struct nfgenmsg *nfmsg;
2014 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2015
2016 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2017 IPCTNL_MSG_CT_GET_STATS_CPU);
2018 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2019 if (nlh == NULL)
2020 goto nlmsg_failure;
2021
2022 nfmsg = nlmsg_data(nlh);
2023 nfmsg->nfgen_family = AF_UNSPEC;
2024 nfmsg->version = NFNETLINK_V0;
2025 nfmsg->res_id = htons(cpu);
2026
2027 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2028 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2029 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2030 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2031 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2032 htonl(st->insert_failed)) ||
2033 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2034 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2035 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2036 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2037 htonl(st->search_restart)))
2038 goto nla_put_failure;
2039
2040 nlmsg_end(skb, nlh);
2041 return skb->len;
2042
2043 nla_put_failure:
2044 nlmsg_failure:
2045 nlmsg_cancel(skb, nlh);
2046 return -1;
2047 }
2048
2049 static int
2050 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2051 {
2052 int cpu;
2053 struct net *net = sock_net(skb->sk);
2054
2055 if (cb->args[0] == nr_cpu_ids)
2056 return 0;
2057
2058 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2059 const struct ip_conntrack_stat *st;
2060
2061 if (!cpu_possible(cpu))
2062 continue;
2063
2064 st = per_cpu_ptr(net->ct.stat, cpu);
2065 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2066 NETLINK_CB(cb->skb).portid,
2067 cb->nlh->nlmsg_seq,
2068 cpu, st) < 0)
2069 break;
2070 }
2071 cb->args[0] = cpu;
2072
2073 return skb->len;
2074 }
2075
2076 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2077 struct sk_buff *skb,
2078 const struct nlmsghdr *nlh,
2079 const struct nlattr * const cda[],
2080 struct netlink_ext_ack *extack)
2081 {
2082 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2083 struct netlink_dump_control c = {
2084 .dump = ctnetlink_ct_stat_cpu_dump,
2085 };
2086 return netlink_dump_start(ctnl, skb, nlh, &c);
2087 }
2088
2089 return 0;
2090 }
2091
2092 static int
2093 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2094 struct net *net)
2095 {
2096 struct nlmsghdr *nlh;
2097 struct nfgenmsg *nfmsg;
2098 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2099 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2100
2101 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2102 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2103 if (nlh == NULL)
2104 goto nlmsg_failure;
2105
2106 nfmsg = nlmsg_data(nlh);
2107 nfmsg->nfgen_family = AF_UNSPEC;
2108 nfmsg->version = NFNETLINK_V0;
2109 nfmsg->res_id = 0;
2110
2111 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2112 goto nla_put_failure;
2113
2114 nlmsg_end(skb, nlh);
2115 return skb->len;
2116
2117 nla_put_failure:
2118 nlmsg_failure:
2119 nlmsg_cancel(skb, nlh);
2120 return -1;
2121 }
2122
2123 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2124 struct sk_buff *skb, const struct nlmsghdr *nlh,
2125 const struct nlattr * const cda[],
2126 struct netlink_ext_ack *extack)
2127 {
2128 struct sk_buff *skb2;
2129 int err;
2130
2131 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2132 if (skb2 == NULL)
2133 return -ENOMEM;
2134
2135 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2136 nlh->nlmsg_seq,
2137 NFNL_MSG_TYPE(nlh->nlmsg_type),
2138 sock_net(skb->sk));
2139 if (err <= 0)
2140 goto free;
2141
2142 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2143 if (err < 0)
2144 goto out;
2145
2146 return 0;
2147
2148 free:
2149 kfree_skb(skb2);
2150 out:
2151 /* this avoids a loop in nfnetlink. */
2152 return err == -EAGAIN ? -ENOBUFS : err;
2153 }
2154
2155 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2156 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2157 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2158 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2159 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2160 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2161 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2162 .len = NF_CT_HELPER_NAME_LEN - 1 },
2163 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2164 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2165 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2166 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2167 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2168 };
2169
2170 static struct nf_conntrack_expect *
2171 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2172 struct nf_conntrack_helper *helper,
2173 struct nf_conntrack_tuple *tuple,
2174 struct nf_conntrack_tuple *mask);
2175
2176 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2177 static size_t
2178 ctnetlink_glue_build_size(const struct nf_conn *ct)
2179 {
2180 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2181 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2182 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2183 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2184 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2185 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2186 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2187 + nla_total_size(0) /* CTA_PROTOINFO */
2188 + nla_total_size(0) /* CTA_HELP */
2189 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2190 + ctnetlink_secctx_size(ct)
2191 #ifdef CONFIG_NF_NAT_NEEDED
2192 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2193 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2194 #endif
2195 #ifdef CONFIG_NF_CONNTRACK_MARK
2196 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2197 #endif
2198 #ifdef CONFIG_NF_CONNTRACK_ZONES
2199 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2200 #endif
2201 + ctnetlink_proto_size(ct)
2202 ;
2203 }
2204
2205 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2206 enum ip_conntrack_info *ctinfo)
2207 {
2208 return nf_ct_get(skb, ctinfo);
2209 }
2210
2211 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2212 {
2213 const struct nf_conntrack_zone *zone;
2214 struct nlattr *nest_parms;
2215
2216 rcu_read_lock();
2217 zone = nf_ct_zone(ct);
2218
2219 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2220 if (!nest_parms)
2221 goto nla_put_failure;
2222 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2223 goto nla_put_failure;
2224 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2225 NF_CT_ZONE_DIR_ORIG) < 0)
2226 goto nla_put_failure;
2227 nla_nest_end(skb, nest_parms);
2228
2229 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2230 if (!nest_parms)
2231 goto nla_put_failure;
2232 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2233 goto nla_put_failure;
2234 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2235 NF_CT_ZONE_DIR_REPL) < 0)
2236 goto nla_put_failure;
2237 nla_nest_end(skb, nest_parms);
2238
2239 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2240 NF_CT_DEFAULT_ZONE_DIR) < 0)
2241 goto nla_put_failure;
2242
2243 if (ctnetlink_dump_id(skb, ct) < 0)
2244 goto nla_put_failure;
2245
2246 if (ctnetlink_dump_status(skb, ct) < 0)
2247 goto nla_put_failure;
2248
2249 if (ctnetlink_dump_timeout(skb, ct) < 0)
2250 goto nla_put_failure;
2251
2252 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2253 goto nla_put_failure;
2254
2255 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2256 goto nla_put_failure;
2257
2258 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2259 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2260 goto nla_put_failure;
2261 #endif
2262 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2263 goto nla_put_failure;
2264
2265 if ((ct->status & IPS_SEQ_ADJUST) &&
2266 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2267 goto nla_put_failure;
2268
2269 #ifdef CONFIG_NF_CONNTRACK_MARK
2270 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2271 goto nla_put_failure;
2272 #endif
2273 if (ctnetlink_dump_labels(skb, ct) < 0)
2274 goto nla_put_failure;
2275 rcu_read_unlock();
2276 return 0;
2277
2278 nla_put_failure:
2279 rcu_read_unlock();
2280 return -ENOSPC;
2281 }
2282
2283 static int
2284 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2285 enum ip_conntrack_info ctinfo,
2286 u_int16_t ct_attr, u_int16_t ct_info_attr)
2287 {
2288 struct nlattr *nest_parms;
2289
2290 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2291 if (!nest_parms)
2292 goto nla_put_failure;
2293
2294 if (__ctnetlink_glue_build(skb, ct) < 0)
2295 goto nla_put_failure;
2296
2297 nla_nest_end(skb, nest_parms);
2298
2299 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2300 goto nla_put_failure;
2301
2302 return 0;
2303
2304 nla_put_failure:
2305 return -ENOSPC;
2306 }
2307
2308 static int
2309 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2310 {
2311 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2312 unsigned long d = ct->status ^ status;
2313
2314 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2315 /* SEEN_REPLY bit can only be set */
2316 return -EBUSY;
2317
2318 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2319 /* ASSURED bit can only be set */
2320 return -EBUSY;
2321
2322 /* This check is less strict than ctnetlink_change_status()
2323 * because callers often flip IPS_EXPECTED bits when sending
2324 * an NFQA_CT attribute to the kernel. So ignore the
2325 * unchangeable bits but do not error out. Also user programs
2326 * are allowed to clear the bits that they are allowed to change.
2327 */
2328 __ctnetlink_change_status(ct, status, ~status);
2329 return 0;
2330 }
2331
2332 static int
2333 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2334 {
2335 int err;
2336
2337 if (cda[CTA_TIMEOUT]) {
2338 err = ctnetlink_change_timeout(ct, cda);
2339 if (err < 0)
2340 return err;
2341 }
2342 if (cda[CTA_STATUS]) {
2343 err = ctnetlink_update_status(ct, cda);
2344 if (err < 0)
2345 return err;
2346 }
2347 if (cda[CTA_HELP]) {
2348 err = ctnetlink_change_helper(ct, cda);
2349 if (err < 0)
2350 return err;
2351 }
2352 if (cda[CTA_LABELS]) {
2353 err = ctnetlink_attach_labels(ct, cda);
2354 if (err < 0)
2355 return err;
2356 }
2357 #if defined(CONFIG_NF_CONNTRACK_MARK)
2358 if (cda[CTA_MARK]) {
2359 u32 mask = 0, mark, newmark;
2360 if (cda[CTA_MARK_MASK])
2361 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2362
2363 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2364 newmark = (ct->mark & mask) ^ mark;
2365 if (newmark != ct->mark)
2366 ct->mark = newmark;
2367 }
2368 #endif
2369 return 0;
2370 }
2371
2372 static int
2373 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2374 {
2375 struct nlattr *cda[CTA_MAX+1];
2376 int ret;
2377
2378 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2379 if (ret < 0)
2380 return ret;
2381
2382 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2383 }
2384
2385 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2386 const struct nf_conn *ct,
2387 struct nf_conntrack_tuple *tuple,
2388 struct nf_conntrack_tuple *mask)
2389 {
2390 int err;
2391
2392 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2393 nf_ct_l3num(ct), NULL);
2394 if (err < 0)
2395 return err;
2396
2397 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2398 nf_ct_l3num(ct), NULL);
2399 }
2400
2401 static int
2402 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2403 u32 portid, u32 report)
2404 {
2405 struct nlattr *cda[CTA_EXPECT_MAX+1];
2406 struct nf_conntrack_tuple tuple, mask;
2407 struct nf_conntrack_helper *helper = NULL;
2408 struct nf_conntrack_expect *exp;
2409 int err;
2410
2411 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2412 NULL);
2413 if (err < 0)
2414 return err;
2415
2416 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2417 ct, &tuple, &mask);
2418 if (err < 0)
2419 return err;
2420
2421 if (cda[CTA_EXPECT_HELP_NAME]) {
2422 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2423
2424 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2425 nf_ct_protonum(ct));
2426 if (helper == NULL)
2427 return -EOPNOTSUPP;
2428 }
2429
2430 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2431 helper, &tuple, &mask);
2432 if (IS_ERR(exp))
2433 return PTR_ERR(exp);
2434
2435 err = nf_ct_expect_related_report(exp, portid, report);
2436 nf_ct_expect_put(exp);
2437 return err;
2438 }
2439
2440 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2441 enum ip_conntrack_info ctinfo, int diff)
2442 {
2443 if (!(ct->status & IPS_NAT_MASK))
2444 return;
2445
2446 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2447 }
2448
2449 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2450 .get_ct = ctnetlink_glue_get_ct,
2451 .build_size = ctnetlink_glue_build_size,
2452 .build = ctnetlink_glue_build,
2453 .parse = ctnetlink_glue_parse,
2454 .attach_expect = ctnetlink_glue_attach_expect,
2455 .seq_adjust = ctnetlink_glue_seqadj,
2456 };
2457 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2458
2459 /***********************************************************************
2460 * EXPECT
2461 ***********************************************************************/
2462
2463 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2464 const struct nf_conntrack_tuple *tuple,
2465 u32 type)
2466 {
2467 struct nlattr *nest_parms;
2468
2469 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2470 if (!nest_parms)
2471 goto nla_put_failure;
2472 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2473 goto nla_put_failure;
2474 nla_nest_end(skb, nest_parms);
2475
2476 return 0;
2477
2478 nla_put_failure:
2479 return -1;
2480 }
2481
2482 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2483 const struct nf_conntrack_tuple *tuple,
2484 const struct nf_conntrack_tuple_mask *mask)
2485 {
2486 int ret;
2487 struct nf_conntrack_l3proto *l3proto;
2488 struct nf_conntrack_l4proto *l4proto;
2489 struct nf_conntrack_tuple m;
2490 struct nlattr *nest_parms;
2491
2492 memset(&m, 0xFF, sizeof(m));
2493 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2494 m.src.u.all = mask->src.u.all;
2495 m.dst.protonum = tuple->dst.protonum;
2496
2497 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2498 if (!nest_parms)
2499 goto nla_put_failure;
2500
2501 rcu_read_lock();
2502 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2503 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2504 if (ret >= 0) {
2505 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2506 tuple->dst.protonum);
2507 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2508 }
2509 rcu_read_unlock();
2510
2511 if (unlikely(ret < 0))
2512 goto nla_put_failure;
2513
2514 nla_nest_end(skb, nest_parms);
2515
2516 return 0;
2517
2518 nla_put_failure:
2519 return -1;
2520 }
2521
2522 static const union nf_inet_addr any_addr;
2523
2524 static int
2525 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2526 const struct nf_conntrack_expect *exp)
2527 {
2528 struct nf_conn *master = exp->master;
2529 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2530 struct nf_conn_help *help;
2531 #ifdef CONFIG_NF_NAT_NEEDED
2532 struct nlattr *nest_parms;
2533 struct nf_conntrack_tuple nat_tuple = {};
2534 #endif
2535 struct nf_ct_helper_expectfn *expfn;
2536
2537 if (timeout < 0)
2538 timeout = 0;
2539
2540 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2541 goto nla_put_failure;
2542 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2543 goto nla_put_failure;
2544 if (ctnetlink_exp_dump_tuple(skb,
2545 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2546 CTA_EXPECT_MASTER) < 0)
2547 goto nla_put_failure;
2548
2549 #ifdef CONFIG_NF_NAT_NEEDED
2550 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2551 exp->saved_proto.all) {
2552 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2553 if (!nest_parms)
2554 goto nla_put_failure;
2555
2556 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2557 goto nla_put_failure;
2558
2559 nat_tuple.src.l3num = nf_ct_l3num(master);
2560 nat_tuple.src.u3 = exp->saved_addr;
2561 nat_tuple.dst.protonum = nf_ct_protonum(master);
2562 nat_tuple.src.u = exp->saved_proto;
2563
2564 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2565 CTA_EXPECT_NAT_TUPLE) < 0)
2566 goto nla_put_failure;
2567 nla_nest_end(skb, nest_parms);
2568 }
2569 #endif
2570 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2571 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2572 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2573 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2574 goto nla_put_failure;
2575 help = nfct_help(master);
2576 if (help) {
2577 struct nf_conntrack_helper *helper;
2578
2579 helper = rcu_dereference(help->helper);
2580 if (helper &&
2581 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2582 goto nla_put_failure;
2583 }
2584 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2585 if (expfn != NULL &&
2586 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2587 goto nla_put_failure;
2588
2589 return 0;
2590
2591 nla_put_failure:
2592 return -1;
2593 }
2594
2595 static int
2596 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2597 int event, const struct nf_conntrack_expect *exp)
2598 {
2599 struct nlmsghdr *nlh;
2600 struct nfgenmsg *nfmsg;
2601 unsigned int flags = portid ? NLM_F_MULTI : 0;
2602
2603 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2604 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2605 if (nlh == NULL)
2606 goto nlmsg_failure;
2607
2608 nfmsg = nlmsg_data(nlh);
2609 nfmsg->nfgen_family = exp->tuple.src.l3num;
2610 nfmsg->version = NFNETLINK_V0;
2611 nfmsg->res_id = 0;
2612
2613 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2614 goto nla_put_failure;
2615
2616 nlmsg_end(skb, nlh);
2617 return skb->len;
2618
2619 nlmsg_failure:
2620 nla_put_failure:
2621 nlmsg_cancel(skb, nlh);
2622 return -1;
2623 }
2624
2625 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2626 static int
2627 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2628 {
2629 struct nf_conntrack_expect *exp = item->exp;
2630 struct net *net = nf_ct_exp_net(exp);
2631 struct nlmsghdr *nlh;
2632 struct nfgenmsg *nfmsg;
2633 struct sk_buff *skb;
2634 unsigned int type, group;
2635 int flags = 0;
2636
2637 if (events & (1 << IPEXP_DESTROY)) {
2638 type = IPCTNL_MSG_EXP_DELETE;
2639 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2640 } else if (events & (1 << IPEXP_NEW)) {
2641 type = IPCTNL_MSG_EXP_NEW;
2642 flags = NLM_F_CREATE|NLM_F_EXCL;
2643 group = NFNLGRP_CONNTRACK_EXP_NEW;
2644 } else
2645 return 0;
2646
2647 if (!item->report && !nfnetlink_has_listeners(net, group))
2648 return 0;
2649
2650 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2651 if (skb == NULL)
2652 goto errout;
2653
2654 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2655 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2656 if (nlh == NULL)
2657 goto nlmsg_failure;
2658
2659 nfmsg = nlmsg_data(nlh);
2660 nfmsg->nfgen_family = exp->tuple.src.l3num;
2661 nfmsg->version = NFNETLINK_V0;
2662 nfmsg->res_id = 0;
2663
2664 rcu_read_lock();
2665 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2666 goto nla_put_failure;
2667 rcu_read_unlock();
2668
2669 nlmsg_end(skb, nlh);
2670 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2671 return 0;
2672
2673 nla_put_failure:
2674 rcu_read_unlock();
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 int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2914 struct sk_buff *skb, const struct nlmsghdr *nlh,
2915 const struct nlattr * const cda[],
2916 struct netlink_ext_ack *extack)
2917 {
2918 struct nf_conntrack_expect *exp;
2919 struct nf_conntrack_tuple tuple;
2920 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2921 struct hlist_node *next;
2922 u_int8_t u3 = nfmsg->nfgen_family;
2923 struct nf_conntrack_zone zone;
2924 unsigned int i;
2925 int err;
2926
2927 if (cda[CTA_EXPECT_TUPLE]) {
2928 /* delete a single expect by tuple */
2929 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2930 if (err < 0)
2931 return err;
2932
2933 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2934 u3, NULL);
2935 if (err < 0)
2936 return err;
2937
2938 /* bump usage count to 2 */
2939 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2940 if (!exp)
2941 return -ENOENT;
2942
2943 if (cda[CTA_EXPECT_ID]) {
2944 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2945 if (ntohl(id) != (u32)(unsigned long)exp) {
2946 nf_ct_expect_put(exp);
2947 return -ENOENT;
2948 }
2949 }
2950
2951 /* after list removal, usage count == 1 */
2952 spin_lock_bh(&nf_conntrack_expect_lock);
2953 if (del_timer(&exp->timeout)) {
2954 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2955 nlmsg_report(nlh));
2956 nf_ct_expect_put(exp);
2957 }
2958 spin_unlock_bh(&nf_conntrack_expect_lock);
2959 /* have to put what we 'get' above.
2960 * after this line usage count == 0 */
2961 nf_ct_expect_put(exp);
2962 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2963 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2964 struct nf_conn_help *m_help;
2965
2966 /* delete all expectations for this helper */
2967 spin_lock_bh(&nf_conntrack_expect_lock);
2968 for (i = 0; i < nf_ct_expect_hsize; i++) {
2969 hlist_for_each_entry_safe(exp, next,
2970 &nf_ct_expect_hash[i],
2971 hnode) {
2972
2973 if (!net_eq(nf_ct_exp_net(exp), net))
2974 continue;
2975
2976 m_help = nfct_help(exp->master);
2977 if (!strcmp(m_help->helper->name, name) &&
2978 del_timer(&exp->timeout)) {
2979 nf_ct_unlink_expect_report(exp,
2980 NETLINK_CB(skb).portid,
2981 nlmsg_report(nlh));
2982 nf_ct_expect_put(exp);
2983 }
2984 }
2985 }
2986 spin_unlock_bh(&nf_conntrack_expect_lock);
2987 } else {
2988 /* This basically means we have to flush everything*/
2989 spin_lock_bh(&nf_conntrack_expect_lock);
2990 for (i = 0; i < nf_ct_expect_hsize; i++) {
2991 hlist_for_each_entry_safe(exp, next,
2992 &nf_ct_expect_hash[i],
2993 hnode) {
2994
2995 if (!net_eq(nf_ct_exp_net(exp), net))
2996 continue;
2997
2998 if (del_timer(&exp->timeout)) {
2999 nf_ct_unlink_expect_report(exp,
3000 NETLINK_CB(skb).portid,
3001 nlmsg_report(nlh));
3002 nf_ct_expect_put(exp);
3003 }
3004 }
3005 }
3006 spin_unlock_bh(&nf_conntrack_expect_lock);
3007 }
3008
3009 return 0;
3010 }
3011 static int
3012 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3013 const struct nlattr * const cda[])
3014 {
3015 if (cda[CTA_EXPECT_TIMEOUT]) {
3016 if (!del_timer(&x->timeout))
3017 return -ETIME;
3018
3019 x->timeout.expires = jiffies +
3020 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3021 add_timer(&x->timeout);
3022 }
3023 return 0;
3024 }
3025
3026 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3027 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3028 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3029 };
3030
3031 static int
3032 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3033 struct nf_conntrack_expect *exp,
3034 u_int8_t u3)
3035 {
3036 #ifdef CONFIG_NF_NAT_NEEDED
3037 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3038 struct nf_conntrack_tuple nat_tuple = {};
3039 int err;
3040
3041 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3042 exp_nat_nla_policy, NULL);
3043 if (err < 0)
3044 return err;
3045
3046 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3047 return -EINVAL;
3048
3049 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3050 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3051 u3, NULL);
3052 if (err < 0)
3053 return err;
3054
3055 exp->saved_addr = nat_tuple.src.u3;
3056 exp->saved_proto = nat_tuple.src.u;
3057 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3058
3059 return 0;
3060 #else
3061 return -EOPNOTSUPP;
3062 #endif
3063 }
3064
3065 static struct nf_conntrack_expect *
3066 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3067 struct nf_conntrack_helper *helper,
3068 struct nf_conntrack_tuple *tuple,
3069 struct nf_conntrack_tuple *mask)
3070 {
3071 u_int32_t class = 0;
3072 struct nf_conntrack_expect *exp;
3073 struct nf_conn_help *help;
3074 int err;
3075
3076 help = nfct_help(ct);
3077 if (!help)
3078 return ERR_PTR(-EOPNOTSUPP);
3079
3080 if (cda[CTA_EXPECT_CLASS] && helper) {
3081 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3082 if (class > helper->expect_class_max)
3083 return ERR_PTR(-EINVAL);
3084 }
3085 exp = nf_ct_expect_alloc(ct);
3086 if (!exp)
3087 return ERR_PTR(-ENOMEM);
3088
3089 if (cda[CTA_EXPECT_FLAGS]) {
3090 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3091 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3092 } else {
3093 exp->flags = 0;
3094 }
3095 if (cda[CTA_EXPECT_FN]) {
3096 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3097 struct nf_ct_helper_expectfn *expfn;
3098
3099 expfn = nf_ct_helper_expectfn_find_by_name(name);
3100 if (expfn == NULL) {
3101 err = -EINVAL;
3102 goto err_out;
3103 }
3104 exp->expectfn = expfn->expectfn;
3105 } else
3106 exp->expectfn = NULL;
3107
3108 exp->class = class;
3109 exp->master = ct;
3110 exp->helper = helper;
3111 exp->tuple = *tuple;
3112 exp->mask.src.u3 = mask->src.u3;
3113 exp->mask.src.u.all = mask->src.u.all;
3114
3115 if (cda[CTA_EXPECT_NAT]) {
3116 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3117 exp, nf_ct_l3num(ct));
3118 if (err < 0)
3119 goto err_out;
3120 }
3121 return exp;
3122 err_out:
3123 nf_ct_expect_put(exp);
3124 return ERR_PTR(err);
3125 }
3126
3127 static int
3128 ctnetlink_create_expect(struct net *net,
3129 const struct nf_conntrack_zone *zone,
3130 const struct nlattr * const cda[],
3131 u_int8_t u3, u32 portid, int report)
3132 {
3133 struct nf_conntrack_tuple tuple, mask, master_tuple;
3134 struct nf_conntrack_tuple_hash *h = NULL;
3135 struct nf_conntrack_helper *helper = NULL;
3136 struct nf_conntrack_expect *exp;
3137 struct nf_conn *ct;
3138 int err;
3139
3140 /* caller guarantees that those three CTA_EXPECT_* exist */
3141 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3142 u3, NULL);
3143 if (err < 0)
3144 return err;
3145 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3146 u3, NULL);
3147 if (err < 0)
3148 return err;
3149 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3150 u3, NULL);
3151 if (err < 0)
3152 return err;
3153
3154 /* Look for master conntrack of this expectation */
3155 h = nf_conntrack_find_get(net, zone, &master_tuple);
3156 if (!h)
3157 return -ENOENT;
3158 ct = nf_ct_tuplehash_to_ctrack(h);
3159
3160 rcu_read_lock();
3161 if (cda[CTA_EXPECT_HELP_NAME]) {
3162 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3163
3164 helper = __nf_conntrack_helper_find(helpname, u3,
3165 nf_ct_protonum(ct));
3166 if (helper == NULL) {
3167 rcu_read_unlock();
3168 #ifdef CONFIG_MODULES
3169 if (request_module("nfct-helper-%s", helpname) < 0) {
3170 err = -EOPNOTSUPP;
3171 goto err_ct;
3172 }
3173 rcu_read_lock();
3174 helper = __nf_conntrack_helper_find(helpname, u3,
3175 nf_ct_protonum(ct));
3176 if (helper) {
3177 err = -EAGAIN;
3178 goto err_rcu;
3179 }
3180 rcu_read_unlock();
3181 #endif
3182 err = -EOPNOTSUPP;
3183 goto err_ct;
3184 }
3185 }
3186
3187 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3188 if (IS_ERR(exp)) {
3189 err = PTR_ERR(exp);
3190 goto err_rcu;
3191 }
3192
3193 err = nf_ct_expect_related_report(exp, portid, report);
3194 nf_ct_expect_put(exp);
3195 err_rcu:
3196 rcu_read_unlock();
3197 err_ct:
3198 nf_ct_put(ct);
3199 return err;
3200 }
3201
3202 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3203 struct sk_buff *skb, const struct nlmsghdr *nlh,
3204 const struct nlattr * const cda[],
3205 struct netlink_ext_ack *extack)
3206 {
3207 struct nf_conntrack_tuple tuple;
3208 struct nf_conntrack_expect *exp;
3209 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3210 u_int8_t u3 = nfmsg->nfgen_family;
3211 struct nf_conntrack_zone zone;
3212 int err;
3213
3214 if (!cda[CTA_EXPECT_TUPLE]
3215 || !cda[CTA_EXPECT_MASK]
3216 || !cda[CTA_EXPECT_MASTER])
3217 return -EINVAL;
3218
3219 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3220 if (err < 0)
3221 return err;
3222
3223 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3224 u3, NULL);
3225 if (err < 0)
3226 return err;
3227
3228 spin_lock_bh(&nf_conntrack_expect_lock);
3229 exp = __nf_ct_expect_find(net, &zone, &tuple);
3230 if (!exp) {
3231 spin_unlock_bh(&nf_conntrack_expect_lock);
3232 err = -ENOENT;
3233 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3234 err = ctnetlink_create_expect(net, &zone, cda, u3,
3235 NETLINK_CB(skb).portid,
3236 nlmsg_report(nlh));
3237 }
3238 return err;
3239 }
3240
3241 err = -EEXIST;
3242 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3243 err = ctnetlink_change_expect(exp, cda);
3244 spin_unlock_bh(&nf_conntrack_expect_lock);
3245
3246 return err;
3247 }
3248
3249 static int
3250 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3251 const struct ip_conntrack_stat *st)
3252 {
3253 struct nlmsghdr *nlh;
3254 struct nfgenmsg *nfmsg;
3255 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3256
3257 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3258 IPCTNL_MSG_EXP_GET_STATS_CPU);
3259 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3260 if (nlh == NULL)
3261 goto nlmsg_failure;
3262
3263 nfmsg = nlmsg_data(nlh);
3264 nfmsg->nfgen_family = AF_UNSPEC;
3265 nfmsg->version = NFNETLINK_V0;
3266 nfmsg->res_id = htons(cpu);
3267
3268 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3269 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3270 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3271 goto nla_put_failure;
3272
3273 nlmsg_end(skb, nlh);
3274 return skb->len;
3275
3276 nla_put_failure:
3277 nlmsg_failure:
3278 nlmsg_cancel(skb, nlh);
3279 return -1;
3280 }
3281
3282 static int
3283 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3284 {
3285 int cpu;
3286 struct net *net = sock_net(skb->sk);
3287
3288 if (cb->args[0] == nr_cpu_ids)
3289 return 0;
3290
3291 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3292 const struct ip_conntrack_stat *st;
3293
3294 if (!cpu_possible(cpu))
3295 continue;
3296
3297 st = per_cpu_ptr(net->ct.stat, cpu);
3298 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3299 cb->nlh->nlmsg_seq,
3300 cpu, st) < 0)
3301 break;
3302 }
3303 cb->args[0] = cpu;
3304
3305 return skb->len;
3306 }
3307
3308 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3309 struct sk_buff *skb,
3310 const struct nlmsghdr *nlh,
3311 const struct nlattr * const cda[],
3312 struct netlink_ext_ack *extack)
3313 {
3314 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3315 struct netlink_dump_control c = {
3316 .dump = ctnetlink_exp_stat_cpu_dump,
3317 };
3318 return netlink_dump_start(ctnl, skb, nlh, &c);
3319 }
3320
3321 return 0;
3322 }
3323
3324 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3325 static struct nf_ct_event_notifier ctnl_notifier = {
3326 .fcn = ctnetlink_conntrack_event,
3327 };
3328
3329 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3330 .fcn = ctnetlink_expect_event,
3331 };
3332 #endif
3333
3334 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3335 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3336 .attr_count = CTA_MAX,
3337 .policy = ct_nla_policy },
3338 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3339 .attr_count = CTA_MAX,
3340 .policy = ct_nla_policy },
3341 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3342 .attr_count = CTA_MAX,
3343 .policy = ct_nla_policy },
3344 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3345 .attr_count = CTA_MAX,
3346 .policy = ct_nla_policy },
3347 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3348 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3349 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3350 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3351 };
3352
3353 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3354 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3355 .attr_count = CTA_EXPECT_MAX,
3356 .policy = exp_nla_policy },
3357 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3358 .attr_count = CTA_EXPECT_MAX,
3359 .policy = exp_nla_policy },
3360 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3361 .attr_count = CTA_EXPECT_MAX,
3362 .policy = exp_nla_policy },
3363 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3364 };
3365
3366 static const struct nfnetlink_subsystem ctnl_subsys = {
3367 .name = "conntrack",
3368 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3369 .cb_count = IPCTNL_MSG_MAX,
3370 .cb = ctnl_cb,
3371 };
3372
3373 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3374 .name = "conntrack_expect",
3375 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3376 .cb_count = IPCTNL_MSG_EXP_MAX,
3377 .cb = ctnl_exp_cb,
3378 };
3379
3380 MODULE_ALIAS("ip_conntrack_netlink");
3381 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3382 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3383
3384 static int __net_init ctnetlink_net_init(struct net *net)
3385 {
3386 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3387 int ret;
3388
3389 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3390 if (ret < 0) {
3391 pr_err("ctnetlink_init: cannot register notifier.\n");
3392 goto err_out;
3393 }
3394
3395 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3396 if (ret < 0) {
3397 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3398 goto err_unreg_notifier;
3399 }
3400 #endif
3401 return 0;
3402
3403 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3404 err_unreg_notifier:
3405 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3406 err_out:
3407 return ret;
3408 #endif
3409 }
3410
3411 static void ctnetlink_net_exit(struct net *net)
3412 {
3413 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3414 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3415 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3416 #endif
3417 }
3418
3419 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3420 {
3421 struct net *net;
3422
3423 list_for_each_entry(net, net_exit_list, exit_list)
3424 ctnetlink_net_exit(net);
3425 }
3426
3427 static struct pernet_operations ctnetlink_net_ops = {
3428 .init = ctnetlink_net_init,
3429 .exit_batch = ctnetlink_net_exit_batch,
3430 };
3431
3432 static int __init ctnetlink_init(void)
3433 {
3434 int ret;
3435
3436 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3437 ret = nfnetlink_subsys_register(&ctnl_subsys);
3438 if (ret < 0) {
3439 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3440 goto err_out;
3441 }
3442
3443 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3444 if (ret < 0) {
3445 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3446 goto err_unreg_subsys;
3447 }
3448
3449 ret = register_pernet_subsys(&ctnetlink_net_ops);
3450 if (ret < 0) {
3451 pr_err("ctnetlink_init: cannot register pernet operations\n");
3452 goto err_unreg_exp_subsys;
3453 }
3454 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3455 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3456 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3457 #endif
3458 return 0;
3459
3460 err_unreg_exp_subsys:
3461 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3462 err_unreg_subsys:
3463 nfnetlink_subsys_unregister(&ctnl_subsys);
3464 err_out:
3465 return ret;
3466 }
3467
3468 static void __exit ctnetlink_exit(void)
3469 {
3470 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3471
3472 unregister_pernet_subsys(&ctnetlink_net_ops);
3473 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3474 nfnetlink_subsys_unregister(&ctnl_subsys);
3475 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3476 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3477 #endif
3478 synchronize_rcu();
3479 }
3480
3481 module_init(ctnetlink_init);
3482 module_exit(ctnetlink_exit);