Merge branch 'bind_unbind' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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, 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 {
1137 struct nf_conntrack_tuple_hash *h;
1138 struct nf_conntrack_tuple tuple;
1139 struct nf_conn *ct;
1140 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1141 u_int8_t u3 = nfmsg->nfgen_family;
1142 struct nf_conntrack_zone zone;
1143 int err;
1144
1145 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1146 if (err < 0)
1147 return err;
1148
1149 if (cda[CTA_TUPLE_ORIG])
1150 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1151 u3, &zone);
1152 else if (cda[CTA_TUPLE_REPLY])
1153 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1154 u3, &zone);
1155 else {
1156 return ctnetlink_flush_conntrack(net, cda,
1157 NETLINK_CB(skb).portid,
1158 nlmsg_report(nlh));
1159 }
1160
1161 if (err < 0)
1162 return err;
1163
1164 h = nf_conntrack_find_get(net, &zone, &tuple);
1165 if (!h)
1166 return -ENOENT;
1167
1168 ct = nf_ct_tuplehash_to_ctrack(h);
1169
1170 if (cda[CTA_ID]) {
1171 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1172 if (id != (u32)(unsigned long)ct) {
1173 nf_ct_put(ct);
1174 return -ENOENT;
1175 }
1176 }
1177
1178 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1179 nf_ct_put(ct);
1180
1181 return 0;
1182 }
1183
1184 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1185 struct sk_buff *skb,
1186 const struct nlmsghdr *nlh,
1187 const struct nlattr * const cda[])
1188 {
1189 struct nf_conntrack_tuple_hash *h;
1190 struct nf_conntrack_tuple tuple;
1191 struct nf_conn *ct;
1192 struct sk_buff *skb2 = NULL;
1193 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1194 u_int8_t u3 = nfmsg->nfgen_family;
1195 struct nf_conntrack_zone zone;
1196 int err;
1197
1198 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1199 struct netlink_dump_control c = {
1200 .dump = ctnetlink_dump_table,
1201 .done = ctnetlink_done,
1202 };
1203
1204 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1205 struct ctnetlink_filter *filter;
1206
1207 filter = ctnetlink_alloc_filter(cda);
1208 if (IS_ERR(filter))
1209 return PTR_ERR(filter);
1210
1211 c.data = filter;
1212 }
1213 return netlink_dump_start(ctnl, skb, nlh, &c);
1214 }
1215
1216 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1217 if (err < 0)
1218 return err;
1219
1220 if (cda[CTA_TUPLE_ORIG])
1221 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1222 u3, &zone);
1223 else if (cda[CTA_TUPLE_REPLY])
1224 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1225 u3, &zone);
1226 else
1227 return -EINVAL;
1228
1229 if (err < 0)
1230 return err;
1231
1232 h = nf_conntrack_find_get(net, &zone, &tuple);
1233 if (!h)
1234 return -ENOENT;
1235
1236 ct = nf_ct_tuplehash_to_ctrack(h);
1237
1238 err = -ENOMEM;
1239 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1240 if (skb2 == NULL) {
1241 nf_ct_put(ct);
1242 return -ENOMEM;
1243 }
1244
1245 rcu_read_lock();
1246 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1247 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1248 rcu_read_unlock();
1249 nf_ct_put(ct);
1250 if (err <= 0)
1251 goto free;
1252
1253 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1254 if (err < 0)
1255 goto out;
1256
1257 return 0;
1258
1259 free:
1260 kfree_skb(skb2);
1261 out:
1262 /* this avoids a loop in nfnetlink. */
1263 return err == -EAGAIN ? -ENOBUFS : err;
1264 }
1265
1266 static int ctnetlink_done_list(struct netlink_callback *cb)
1267 {
1268 if (cb->args[1])
1269 nf_ct_put((struct nf_conn *)cb->args[1]);
1270 return 0;
1271 }
1272
1273 static int
1274 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1275 {
1276 struct nf_conn *ct, *last;
1277 struct nf_conntrack_tuple_hash *h;
1278 struct hlist_nulls_node *n;
1279 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1280 u_int8_t l3proto = nfmsg->nfgen_family;
1281 int res;
1282 int cpu;
1283 struct hlist_nulls_head *list;
1284 struct net *net = sock_net(skb->sk);
1285
1286 if (cb->args[2])
1287 return 0;
1288
1289 last = (struct nf_conn *)cb->args[1];
1290
1291 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1292 struct ct_pcpu *pcpu;
1293
1294 if (!cpu_possible(cpu))
1295 continue;
1296
1297 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1298 spin_lock_bh(&pcpu->lock);
1299 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1300 restart:
1301 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1302 ct = nf_ct_tuplehash_to_ctrack(h);
1303 if (l3proto && nf_ct_l3num(ct) != l3proto)
1304 continue;
1305 if (cb->args[1]) {
1306 if (ct != last)
1307 continue;
1308 cb->args[1] = 0;
1309 }
1310 rcu_read_lock();
1311 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1312 cb->nlh->nlmsg_seq,
1313 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1314 ct);
1315 rcu_read_unlock();
1316 if (res < 0) {
1317 if (!atomic_inc_not_zero(&ct->ct_general.use))
1318 continue;
1319 cb->args[0] = cpu;
1320 cb->args[1] = (unsigned long)ct;
1321 spin_unlock_bh(&pcpu->lock);
1322 goto out;
1323 }
1324 }
1325 if (cb->args[1]) {
1326 cb->args[1] = 0;
1327 goto restart;
1328 }
1329 spin_unlock_bh(&pcpu->lock);
1330 }
1331 cb->args[2] = 1;
1332 out:
1333 if (last)
1334 nf_ct_put(last);
1335
1336 return skb->len;
1337 }
1338
1339 static int
1340 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1341 {
1342 return ctnetlink_dump_list(skb, cb, true);
1343 }
1344
1345 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1346 struct sk_buff *skb,
1347 const struct nlmsghdr *nlh,
1348 const struct nlattr * const cda[])
1349 {
1350 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1351 struct netlink_dump_control c = {
1352 .dump = ctnetlink_dump_dying,
1353 .done = ctnetlink_done_list,
1354 };
1355 return netlink_dump_start(ctnl, skb, nlh, &c);
1356 }
1357
1358 return -EOPNOTSUPP;
1359 }
1360
1361 static int
1362 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1363 {
1364 return ctnetlink_dump_list(skb, cb, false);
1365 }
1366
1367 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1368 struct sk_buff *skb,
1369 const struct nlmsghdr *nlh,
1370 const struct nlattr * const cda[])
1371 {
1372 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1373 struct netlink_dump_control c = {
1374 .dump = ctnetlink_dump_unconfirmed,
1375 .done = ctnetlink_done_list,
1376 };
1377 return netlink_dump_start(ctnl, skb, nlh, &c);
1378 }
1379
1380 return -EOPNOTSUPP;
1381 }
1382
1383 #ifdef CONFIG_NF_NAT_NEEDED
1384 static int
1385 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1386 enum nf_nat_manip_type manip,
1387 const struct nlattr *attr)
1388 {
1389 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1390 int err;
1391
1392 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1393 if (!parse_nat_setup) {
1394 #ifdef CONFIG_MODULES
1395 rcu_read_unlock();
1396 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1397 if (request_module("nf-nat") < 0) {
1398 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1399 rcu_read_lock();
1400 return -EOPNOTSUPP;
1401 }
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1403 rcu_read_lock();
1404 if (nfnetlink_parse_nat_setup_hook)
1405 return -EAGAIN;
1406 #endif
1407 return -EOPNOTSUPP;
1408 }
1409
1410 err = parse_nat_setup(ct, manip, attr);
1411 if (err == -EAGAIN) {
1412 #ifdef CONFIG_MODULES
1413 rcu_read_unlock();
1414 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1415 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1416 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1417 rcu_read_lock();
1418 return -EOPNOTSUPP;
1419 }
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1421 rcu_read_lock();
1422 #else
1423 err = -EOPNOTSUPP;
1424 #endif
1425 }
1426 return err;
1427 }
1428 #endif
1429
1430 static void
1431 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1432 unsigned long off)
1433 {
1434 unsigned int bit;
1435
1436 /* Ignore these unchangable bits */
1437 on &= ~IPS_UNCHANGEABLE_MASK;
1438 off &= ~IPS_UNCHANGEABLE_MASK;
1439
1440 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1441 if (on & (1 << bit))
1442 set_bit(bit, &ct->status);
1443 else if (off & (1 << bit))
1444 clear_bit(bit, &ct->status);
1445 }
1446 }
1447
1448 static int
1449 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1450 {
1451 unsigned long d;
1452 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1453 d = ct->status ^ status;
1454
1455 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1456 /* unchangeable */
1457 return -EBUSY;
1458
1459 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1460 /* SEEN_REPLY bit can only be set */
1461 return -EBUSY;
1462
1463 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1464 /* ASSURED bit can only be set */
1465 return -EBUSY;
1466
1467 __ctnetlink_change_status(ct, status, 0);
1468 return 0;
1469 }
1470
1471 static int
1472 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1473 {
1474 #ifdef CONFIG_NF_NAT_NEEDED
1475 int ret;
1476
1477 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1478 return 0;
1479
1480 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1481 cda[CTA_NAT_DST]);
1482 if (ret < 0)
1483 return ret;
1484
1485 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1486 cda[CTA_NAT_SRC]);
1487 return ret;
1488 #else
1489 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1490 return 0;
1491 return -EOPNOTSUPP;
1492 #endif
1493 }
1494
1495 static int ctnetlink_change_helper(struct nf_conn *ct,
1496 const struct nlattr * const cda[])
1497 {
1498 struct nf_conntrack_helper *helper;
1499 struct nf_conn_help *help = nfct_help(ct);
1500 char *helpname = NULL;
1501 struct nlattr *helpinfo = NULL;
1502 int err;
1503
1504 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1505 if (err < 0)
1506 return err;
1507
1508 /* don't change helper of sibling connections */
1509 if (ct->master) {
1510 /* If we try to change the helper to the same thing twice,
1511 * treat the second attempt as a no-op instead of returning
1512 * an error.
1513 */
1514 err = -EBUSY;
1515 if (help) {
1516 rcu_read_lock();
1517 helper = rcu_dereference(help->helper);
1518 if (helper && !strcmp(helper->name, helpname))
1519 err = 0;
1520 rcu_read_unlock();
1521 }
1522
1523 return err;
1524 }
1525
1526 if (!strcmp(helpname, "")) {
1527 if (help && help->helper) {
1528 /* we had a helper before ... */
1529 nf_ct_remove_expectations(ct);
1530 RCU_INIT_POINTER(help->helper, NULL);
1531 }
1532
1533 return 0;
1534 }
1535
1536 rcu_read_lock();
1537 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1538 nf_ct_protonum(ct));
1539 if (helper == NULL) {
1540 rcu_read_unlock();
1541 return -EOPNOTSUPP;
1542 }
1543
1544 if (help) {
1545 if (help->helper == helper) {
1546 /* update private helper data if allowed. */
1547 if (helper->from_nlattr)
1548 helper->from_nlattr(helpinfo, ct);
1549 err = 0;
1550 } else
1551 err = -EBUSY;
1552 } else {
1553 /* we cannot set a helper for an existing conntrack */
1554 err = -EOPNOTSUPP;
1555 }
1556
1557 rcu_read_unlock();
1558 return err;
1559 }
1560
1561 static int ctnetlink_change_timeout(struct nf_conn *ct,
1562 const struct nlattr * const cda[])
1563 {
1564 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1565
1566 ct->timeout = nfct_time_stamp + timeout * HZ;
1567
1568 if (test_bit(IPS_DYING_BIT, &ct->status))
1569 return -ETIME;
1570
1571 return 0;
1572 }
1573
1574 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1575 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1576 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1577 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1578 };
1579
1580 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1581 const struct nlattr * const cda[])
1582 {
1583 const struct nlattr *attr = cda[CTA_PROTOINFO];
1584 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1585 struct nf_conntrack_l4proto *l4proto;
1586 int err = 0;
1587
1588 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1589 NULL);
1590 if (err < 0)
1591 return err;
1592
1593 rcu_read_lock();
1594 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1595 if (l4proto->from_nlattr)
1596 err = l4proto->from_nlattr(tb, ct);
1597 rcu_read_unlock();
1598
1599 return err;
1600 }
1601
1602 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1603 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1604 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1605 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1606 };
1607
1608 static int change_seq_adj(struct nf_ct_seqadj *seq,
1609 const struct nlattr * const attr)
1610 {
1611 int err;
1612 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1613
1614 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1615 if (err < 0)
1616 return err;
1617
1618 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1619 return -EINVAL;
1620
1621 seq->correction_pos =
1622 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1623
1624 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1625 return -EINVAL;
1626
1627 seq->offset_before =
1628 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1629
1630 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1631 return -EINVAL;
1632
1633 seq->offset_after =
1634 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1635
1636 return 0;
1637 }
1638
1639 static int
1640 ctnetlink_change_seq_adj(struct nf_conn *ct,
1641 const struct nlattr * const cda[])
1642 {
1643 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1644 int ret = 0;
1645
1646 if (!seqadj)
1647 return 0;
1648
1649 spin_lock_bh(&ct->lock);
1650 if (cda[CTA_SEQ_ADJ_ORIG]) {
1651 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1652 cda[CTA_SEQ_ADJ_ORIG]);
1653 if (ret < 0)
1654 goto err;
1655
1656 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1657 }
1658
1659 if (cda[CTA_SEQ_ADJ_REPLY]) {
1660 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1661 cda[CTA_SEQ_ADJ_REPLY]);
1662 if (ret < 0)
1663 goto err;
1664
1665 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1666 }
1667
1668 spin_unlock_bh(&ct->lock);
1669 return 0;
1670 err:
1671 spin_unlock_bh(&ct->lock);
1672 return ret;
1673 }
1674
1675 static int
1676 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1677 {
1678 #ifdef CONFIG_NF_CONNTRACK_LABELS
1679 size_t len = nla_len(cda[CTA_LABELS]);
1680 const void *mask = cda[CTA_LABELS_MASK];
1681
1682 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1683 return -EINVAL;
1684
1685 if (mask) {
1686 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1687 nla_len(cda[CTA_LABELS_MASK]) != len)
1688 return -EINVAL;
1689 mask = nla_data(cda[CTA_LABELS_MASK]);
1690 }
1691
1692 len /= sizeof(u32);
1693
1694 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1695 #else
1696 return -EOPNOTSUPP;
1697 #endif
1698 }
1699
1700 static int
1701 ctnetlink_change_conntrack(struct nf_conn *ct,
1702 const struct nlattr * const cda[])
1703 {
1704 int err;
1705
1706 /* only allow NAT changes and master assignation for new conntracks */
1707 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1708 return -EOPNOTSUPP;
1709
1710 if (cda[CTA_HELP]) {
1711 err = ctnetlink_change_helper(ct, cda);
1712 if (err < 0)
1713 return err;
1714 }
1715
1716 if (cda[CTA_TIMEOUT]) {
1717 err = ctnetlink_change_timeout(ct, cda);
1718 if (err < 0)
1719 return err;
1720 }
1721
1722 if (cda[CTA_STATUS]) {
1723 err = ctnetlink_change_status(ct, cda);
1724 if (err < 0)
1725 return err;
1726 }
1727
1728 if (cda[CTA_PROTOINFO]) {
1729 err = ctnetlink_change_protoinfo(ct, cda);
1730 if (err < 0)
1731 return err;
1732 }
1733
1734 #if defined(CONFIG_NF_CONNTRACK_MARK)
1735 if (cda[CTA_MARK])
1736 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1737 #endif
1738
1739 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1740 err = ctnetlink_change_seq_adj(ct, cda);
1741 if (err < 0)
1742 return err;
1743 }
1744
1745 if (cda[CTA_LABELS]) {
1746 err = ctnetlink_attach_labels(ct, cda);
1747 if (err < 0)
1748 return err;
1749 }
1750
1751 return 0;
1752 }
1753
1754 static struct nf_conn *
1755 ctnetlink_create_conntrack(struct net *net,
1756 const struct nf_conntrack_zone *zone,
1757 const struct nlattr * const cda[],
1758 struct nf_conntrack_tuple *otuple,
1759 struct nf_conntrack_tuple *rtuple,
1760 u8 u3)
1761 {
1762 struct nf_conn *ct;
1763 int err = -EINVAL;
1764 struct nf_conntrack_helper *helper;
1765 struct nf_conn_tstamp *tstamp;
1766
1767 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1768 if (IS_ERR(ct))
1769 return ERR_PTR(-ENOMEM);
1770
1771 if (!cda[CTA_TIMEOUT])
1772 goto err1;
1773
1774 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1775
1776 rcu_read_lock();
1777 if (cda[CTA_HELP]) {
1778 char *helpname = NULL;
1779 struct nlattr *helpinfo = NULL;
1780
1781 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1782 if (err < 0)
1783 goto err2;
1784
1785 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1786 nf_ct_protonum(ct));
1787 if (helper == NULL) {
1788 rcu_read_unlock();
1789 #ifdef CONFIG_MODULES
1790 if (request_module("nfct-helper-%s", helpname) < 0) {
1791 err = -EOPNOTSUPP;
1792 goto err1;
1793 }
1794
1795 rcu_read_lock();
1796 helper = __nf_conntrack_helper_find(helpname,
1797 nf_ct_l3num(ct),
1798 nf_ct_protonum(ct));
1799 if (helper) {
1800 err = -EAGAIN;
1801 goto err2;
1802 }
1803 rcu_read_unlock();
1804 #endif
1805 err = -EOPNOTSUPP;
1806 goto err1;
1807 } else {
1808 struct nf_conn_help *help;
1809
1810 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1811 if (help == NULL) {
1812 err = -ENOMEM;
1813 goto err2;
1814 }
1815 /* set private helper data if allowed. */
1816 if (helper->from_nlattr)
1817 helper->from_nlattr(helpinfo, ct);
1818
1819 /* not in hash table yet so not strictly necessary */
1820 RCU_INIT_POINTER(help->helper, helper);
1821 }
1822 } else {
1823 /* try an implicit helper assignation */
1824 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1825 if (err < 0)
1826 goto err2;
1827 }
1828
1829 err = ctnetlink_setup_nat(ct, cda);
1830 if (err < 0)
1831 goto err2;
1832
1833 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1834 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1835 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1836 nf_ct_labels_ext_add(ct);
1837 nfct_seqadj_ext_add(ct);
1838 nfct_synproxy_ext_add(ct);
1839
1840 /* we must add conntrack extensions before confirmation. */
1841 ct->status |= IPS_CONFIRMED;
1842
1843 if (cda[CTA_STATUS]) {
1844 err = ctnetlink_change_status(ct, cda);
1845 if (err < 0)
1846 goto err2;
1847 }
1848
1849 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1850 err = ctnetlink_change_seq_adj(ct, cda);
1851 if (err < 0)
1852 goto err2;
1853 }
1854
1855 memset(&ct->proto, 0, sizeof(ct->proto));
1856 if (cda[CTA_PROTOINFO]) {
1857 err = ctnetlink_change_protoinfo(ct, cda);
1858 if (err < 0)
1859 goto err2;
1860 }
1861
1862 #if defined(CONFIG_NF_CONNTRACK_MARK)
1863 if (cda[CTA_MARK])
1864 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1865 #endif
1866
1867 /* setup master conntrack: this is a confirmed expectation */
1868 if (cda[CTA_TUPLE_MASTER]) {
1869 struct nf_conntrack_tuple master;
1870 struct nf_conntrack_tuple_hash *master_h;
1871 struct nf_conn *master_ct;
1872
1873 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1874 u3, NULL);
1875 if (err < 0)
1876 goto err2;
1877
1878 master_h = nf_conntrack_find_get(net, zone, &master);
1879 if (master_h == NULL) {
1880 err = -ENOENT;
1881 goto err2;
1882 }
1883 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1884 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1885 ct->master = master_ct;
1886 }
1887 tstamp = nf_conn_tstamp_find(ct);
1888 if (tstamp)
1889 tstamp->start = ktime_get_real_ns();
1890
1891 err = nf_conntrack_hash_check_insert(ct);
1892 if (err < 0)
1893 goto err2;
1894
1895 rcu_read_unlock();
1896
1897 return ct;
1898
1899 err2:
1900 rcu_read_unlock();
1901 err1:
1902 nf_conntrack_free(ct);
1903 return ERR_PTR(err);
1904 }
1905
1906 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1907 struct sk_buff *skb,
1908 const struct nlmsghdr *nlh,
1909 const struct nlattr * const cda[])
1910 {
1911 struct nf_conntrack_tuple otuple, rtuple;
1912 struct nf_conntrack_tuple_hash *h = NULL;
1913 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1914 struct nf_conn *ct;
1915 u_int8_t u3 = nfmsg->nfgen_family;
1916 struct nf_conntrack_zone zone;
1917 int err;
1918
1919 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1920 if (err < 0)
1921 return err;
1922
1923 if (cda[CTA_TUPLE_ORIG]) {
1924 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1925 u3, &zone);
1926 if (err < 0)
1927 return err;
1928 }
1929
1930 if (cda[CTA_TUPLE_REPLY]) {
1931 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1932 u3, &zone);
1933 if (err < 0)
1934 return err;
1935 }
1936
1937 if (cda[CTA_TUPLE_ORIG])
1938 h = nf_conntrack_find_get(net, &zone, &otuple);
1939 else if (cda[CTA_TUPLE_REPLY])
1940 h = nf_conntrack_find_get(net, &zone, &rtuple);
1941
1942 if (h == NULL) {
1943 err = -ENOENT;
1944 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1945 enum ip_conntrack_events events;
1946
1947 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1948 return -EINVAL;
1949 if (otuple.dst.protonum != rtuple.dst.protonum)
1950 return -EINVAL;
1951
1952 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1953 &rtuple, u3);
1954 if (IS_ERR(ct))
1955 return PTR_ERR(ct);
1956
1957 err = 0;
1958 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1959 events = 1 << IPCT_RELATED;
1960 else
1961 events = 1 << IPCT_NEW;
1962
1963 if (cda[CTA_LABELS] &&
1964 ctnetlink_attach_labels(ct, cda) == 0)
1965 events |= (1 << IPCT_LABEL);
1966
1967 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1968 (1 << IPCT_ASSURED) |
1969 (1 << IPCT_HELPER) |
1970 (1 << IPCT_PROTOINFO) |
1971 (1 << IPCT_SEQADJ) |
1972 (1 << IPCT_MARK) | events,
1973 ct, NETLINK_CB(skb).portid,
1974 nlmsg_report(nlh));
1975 nf_ct_put(ct);
1976 }
1977
1978 return err;
1979 }
1980 /* implicit 'else' */
1981
1982 err = -EEXIST;
1983 ct = nf_ct_tuplehash_to_ctrack(h);
1984 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1985 err = ctnetlink_change_conntrack(ct, cda);
1986 if (err == 0) {
1987 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1988 (1 << IPCT_ASSURED) |
1989 (1 << IPCT_HELPER) |
1990 (1 << IPCT_LABEL) |
1991 (1 << IPCT_PROTOINFO) |
1992 (1 << IPCT_SEQADJ) |
1993 (1 << IPCT_MARK),
1994 ct, NETLINK_CB(skb).portid,
1995 nlmsg_report(nlh));
1996 }
1997 }
1998
1999 nf_ct_put(ct);
2000 return err;
2001 }
2002
2003 static int
2004 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2005 __u16 cpu, const struct ip_conntrack_stat *st)
2006 {
2007 struct nlmsghdr *nlh;
2008 struct nfgenmsg *nfmsg;
2009 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2010
2011 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2012 IPCTNL_MSG_CT_GET_STATS_CPU);
2013 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2014 if (nlh == NULL)
2015 goto nlmsg_failure;
2016
2017 nfmsg = nlmsg_data(nlh);
2018 nfmsg->nfgen_family = AF_UNSPEC;
2019 nfmsg->version = NFNETLINK_V0;
2020 nfmsg->res_id = htons(cpu);
2021
2022 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2023 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2024 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2025 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2026 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2027 htonl(st->insert_failed)) ||
2028 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2029 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2030 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2031 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2032 htonl(st->search_restart)))
2033 goto nla_put_failure;
2034
2035 nlmsg_end(skb, nlh);
2036 return skb->len;
2037
2038 nla_put_failure:
2039 nlmsg_failure:
2040 nlmsg_cancel(skb, nlh);
2041 return -1;
2042 }
2043
2044 static int
2045 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2046 {
2047 int cpu;
2048 struct net *net = sock_net(skb->sk);
2049
2050 if (cb->args[0] == nr_cpu_ids)
2051 return 0;
2052
2053 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2054 const struct ip_conntrack_stat *st;
2055
2056 if (!cpu_possible(cpu))
2057 continue;
2058
2059 st = per_cpu_ptr(net->ct.stat, cpu);
2060 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2061 NETLINK_CB(cb->skb).portid,
2062 cb->nlh->nlmsg_seq,
2063 cpu, st) < 0)
2064 break;
2065 }
2066 cb->args[0] = cpu;
2067
2068 return skb->len;
2069 }
2070
2071 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2072 struct sk_buff *skb,
2073 const struct nlmsghdr *nlh,
2074 const struct nlattr * const cda[])
2075 {
2076 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2077 struct netlink_dump_control c = {
2078 .dump = ctnetlink_ct_stat_cpu_dump,
2079 };
2080 return netlink_dump_start(ctnl, skb, nlh, &c);
2081 }
2082
2083 return 0;
2084 }
2085
2086 static int
2087 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2088 struct net *net)
2089 {
2090 struct nlmsghdr *nlh;
2091 struct nfgenmsg *nfmsg;
2092 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2093 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2094
2095 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2096 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2097 if (nlh == NULL)
2098 goto nlmsg_failure;
2099
2100 nfmsg = nlmsg_data(nlh);
2101 nfmsg->nfgen_family = AF_UNSPEC;
2102 nfmsg->version = NFNETLINK_V0;
2103 nfmsg->res_id = 0;
2104
2105 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2106 goto nla_put_failure;
2107
2108 nlmsg_end(skb, nlh);
2109 return skb->len;
2110
2111 nla_put_failure:
2112 nlmsg_failure:
2113 nlmsg_cancel(skb, nlh);
2114 return -1;
2115 }
2116
2117 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2118 struct sk_buff *skb, const struct nlmsghdr *nlh,
2119 const struct nlattr * const cda[])
2120 {
2121 struct sk_buff *skb2;
2122 int err;
2123
2124 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2125 if (skb2 == NULL)
2126 return -ENOMEM;
2127
2128 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2129 nlh->nlmsg_seq,
2130 NFNL_MSG_TYPE(nlh->nlmsg_type),
2131 sock_net(skb->sk));
2132 if (err <= 0)
2133 goto free;
2134
2135 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2136 if (err < 0)
2137 goto out;
2138
2139 return 0;
2140
2141 free:
2142 kfree_skb(skb2);
2143 out:
2144 /* this avoids a loop in nfnetlink. */
2145 return err == -EAGAIN ? -ENOBUFS : err;
2146 }
2147
2148 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2149 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2150 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2151 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2152 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2153 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2154 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2155 .len = NF_CT_HELPER_NAME_LEN - 1 },
2156 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2157 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2158 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2159 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2160 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2161 };
2162
2163 static struct nf_conntrack_expect *
2164 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2165 struct nf_conntrack_helper *helper,
2166 struct nf_conntrack_tuple *tuple,
2167 struct nf_conntrack_tuple *mask);
2168
2169 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2170 static size_t
2171 ctnetlink_glue_build_size(const struct nf_conn *ct)
2172 {
2173 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2174 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2175 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2176 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2177 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2178 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2179 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2180 + nla_total_size(0) /* CTA_PROTOINFO */
2181 + nla_total_size(0) /* CTA_HELP */
2182 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2183 + ctnetlink_secctx_size(ct)
2184 #ifdef CONFIG_NF_NAT_NEEDED
2185 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2186 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2187 #endif
2188 #ifdef CONFIG_NF_CONNTRACK_MARK
2189 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2190 #endif
2191 #ifdef CONFIG_NF_CONNTRACK_ZONES
2192 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2193 #endif
2194 + ctnetlink_proto_size(ct)
2195 ;
2196 }
2197
2198 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2199 enum ip_conntrack_info *ctinfo)
2200 {
2201 return nf_ct_get(skb, ctinfo);
2202 }
2203
2204 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2205 {
2206 const struct nf_conntrack_zone *zone;
2207 struct nlattr *nest_parms;
2208
2209 rcu_read_lock();
2210 zone = nf_ct_zone(ct);
2211
2212 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2213 if (!nest_parms)
2214 goto nla_put_failure;
2215 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2216 goto nla_put_failure;
2217 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2218 NF_CT_ZONE_DIR_ORIG) < 0)
2219 goto nla_put_failure;
2220 nla_nest_end(skb, nest_parms);
2221
2222 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2223 if (!nest_parms)
2224 goto nla_put_failure;
2225 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2226 goto nla_put_failure;
2227 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2228 NF_CT_ZONE_DIR_REPL) < 0)
2229 goto nla_put_failure;
2230 nla_nest_end(skb, nest_parms);
2231
2232 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2233 NF_CT_DEFAULT_ZONE_DIR) < 0)
2234 goto nla_put_failure;
2235
2236 if (ctnetlink_dump_id(skb, ct) < 0)
2237 goto nla_put_failure;
2238
2239 if (ctnetlink_dump_status(skb, ct) < 0)
2240 goto nla_put_failure;
2241
2242 if (ctnetlink_dump_timeout(skb, ct) < 0)
2243 goto nla_put_failure;
2244
2245 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2246 goto nla_put_failure;
2247
2248 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2249 goto nla_put_failure;
2250
2251 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2252 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2253 goto nla_put_failure;
2254 #endif
2255 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2256 goto nla_put_failure;
2257
2258 if ((ct->status & IPS_SEQ_ADJUST) &&
2259 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2260 goto nla_put_failure;
2261
2262 #ifdef CONFIG_NF_CONNTRACK_MARK
2263 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2264 goto nla_put_failure;
2265 #endif
2266 if (ctnetlink_dump_labels(skb, ct) < 0)
2267 goto nla_put_failure;
2268 rcu_read_unlock();
2269 return 0;
2270
2271 nla_put_failure:
2272 rcu_read_unlock();
2273 return -ENOSPC;
2274 }
2275
2276 static int
2277 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2278 enum ip_conntrack_info ctinfo,
2279 u_int16_t ct_attr, u_int16_t ct_info_attr)
2280 {
2281 struct nlattr *nest_parms;
2282
2283 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2284 if (!nest_parms)
2285 goto nla_put_failure;
2286
2287 if (__ctnetlink_glue_build(skb, ct) < 0)
2288 goto nla_put_failure;
2289
2290 nla_nest_end(skb, nest_parms);
2291
2292 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2293 goto nla_put_failure;
2294
2295 return 0;
2296
2297 nla_put_failure:
2298 return -ENOSPC;
2299 }
2300
2301 static int
2302 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2303 {
2304 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2305 unsigned long d = ct->status ^ status;
2306
2307 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2308 /* SEEN_REPLY bit can only be set */
2309 return -EBUSY;
2310
2311 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2312 /* ASSURED bit can only be set */
2313 return -EBUSY;
2314
2315 /* This check is less strict than ctnetlink_change_status()
2316 * because callers often flip IPS_EXPECTED bits when sending
2317 * an NFQA_CT attribute to the kernel. So ignore the
2318 * unchangeable bits but do not error out. Also user programs
2319 * are allowed to clear the bits that they are allowed to change.
2320 */
2321 __ctnetlink_change_status(ct, status, ~status);
2322 return 0;
2323 }
2324
2325 static int
2326 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2327 {
2328 int err;
2329
2330 if (cda[CTA_TIMEOUT]) {
2331 err = ctnetlink_change_timeout(ct, cda);
2332 if (err < 0)
2333 return err;
2334 }
2335 if (cda[CTA_STATUS]) {
2336 err = ctnetlink_update_status(ct, cda);
2337 if (err < 0)
2338 return err;
2339 }
2340 if (cda[CTA_HELP]) {
2341 err = ctnetlink_change_helper(ct, cda);
2342 if (err < 0)
2343 return err;
2344 }
2345 if (cda[CTA_LABELS]) {
2346 err = ctnetlink_attach_labels(ct, cda);
2347 if (err < 0)
2348 return err;
2349 }
2350 #if defined(CONFIG_NF_CONNTRACK_MARK)
2351 if (cda[CTA_MARK]) {
2352 u32 mask = 0, mark, newmark;
2353 if (cda[CTA_MARK_MASK])
2354 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2355
2356 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2357 newmark = (ct->mark & mask) ^ mark;
2358 if (newmark != ct->mark)
2359 ct->mark = newmark;
2360 }
2361 #endif
2362 return 0;
2363 }
2364
2365 static int
2366 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2367 {
2368 struct nlattr *cda[CTA_MAX+1];
2369 int ret;
2370
2371 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2372 if (ret < 0)
2373 return ret;
2374
2375 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2376 }
2377
2378 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2379 const struct nf_conn *ct,
2380 struct nf_conntrack_tuple *tuple,
2381 struct nf_conntrack_tuple *mask)
2382 {
2383 int err;
2384
2385 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2386 nf_ct_l3num(ct), NULL);
2387 if (err < 0)
2388 return err;
2389
2390 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2391 nf_ct_l3num(ct), NULL);
2392 }
2393
2394 static int
2395 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2396 u32 portid, u32 report)
2397 {
2398 struct nlattr *cda[CTA_EXPECT_MAX+1];
2399 struct nf_conntrack_tuple tuple, mask;
2400 struct nf_conntrack_helper *helper = NULL;
2401 struct nf_conntrack_expect *exp;
2402 int err;
2403
2404 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2405 NULL);
2406 if (err < 0)
2407 return err;
2408
2409 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2410 ct, &tuple, &mask);
2411 if (err < 0)
2412 return err;
2413
2414 if (cda[CTA_EXPECT_HELP_NAME]) {
2415 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2416
2417 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2418 nf_ct_protonum(ct));
2419 if (helper == NULL)
2420 return -EOPNOTSUPP;
2421 }
2422
2423 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2424 helper, &tuple, &mask);
2425 if (IS_ERR(exp))
2426 return PTR_ERR(exp);
2427
2428 err = nf_ct_expect_related_report(exp, portid, report);
2429 nf_ct_expect_put(exp);
2430 return err;
2431 }
2432
2433 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2434 enum ip_conntrack_info ctinfo, int diff)
2435 {
2436 if (!(ct->status & IPS_NAT_MASK))
2437 return;
2438
2439 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2440 }
2441
2442 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2443 .get_ct = ctnetlink_glue_get_ct,
2444 .build_size = ctnetlink_glue_build_size,
2445 .build = ctnetlink_glue_build,
2446 .parse = ctnetlink_glue_parse,
2447 .attach_expect = ctnetlink_glue_attach_expect,
2448 .seq_adjust = ctnetlink_glue_seqadj,
2449 };
2450 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2451
2452 /***********************************************************************
2453 * EXPECT
2454 ***********************************************************************/
2455
2456 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2457 const struct nf_conntrack_tuple *tuple,
2458 u32 type)
2459 {
2460 struct nlattr *nest_parms;
2461
2462 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2463 if (!nest_parms)
2464 goto nla_put_failure;
2465 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2466 goto nla_put_failure;
2467 nla_nest_end(skb, nest_parms);
2468
2469 return 0;
2470
2471 nla_put_failure:
2472 return -1;
2473 }
2474
2475 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2476 const struct nf_conntrack_tuple *tuple,
2477 const struct nf_conntrack_tuple_mask *mask)
2478 {
2479 int ret;
2480 struct nf_conntrack_l3proto *l3proto;
2481 struct nf_conntrack_l4proto *l4proto;
2482 struct nf_conntrack_tuple m;
2483 struct nlattr *nest_parms;
2484
2485 memset(&m, 0xFF, sizeof(m));
2486 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2487 m.src.u.all = mask->src.u.all;
2488 m.dst.protonum = tuple->dst.protonum;
2489
2490 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2491 if (!nest_parms)
2492 goto nla_put_failure;
2493
2494 rcu_read_lock();
2495 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2496 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2497 if (ret >= 0) {
2498 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2499 tuple->dst.protonum);
2500 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2501 }
2502 rcu_read_unlock();
2503
2504 if (unlikely(ret < 0))
2505 goto nla_put_failure;
2506
2507 nla_nest_end(skb, nest_parms);
2508
2509 return 0;
2510
2511 nla_put_failure:
2512 return -1;
2513 }
2514
2515 static const union nf_inet_addr any_addr;
2516
2517 static int
2518 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2519 const struct nf_conntrack_expect *exp)
2520 {
2521 struct nf_conn *master = exp->master;
2522 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2523 struct nf_conn_help *help;
2524 #ifdef CONFIG_NF_NAT_NEEDED
2525 struct nlattr *nest_parms;
2526 struct nf_conntrack_tuple nat_tuple = {};
2527 #endif
2528 struct nf_ct_helper_expectfn *expfn;
2529
2530 if (timeout < 0)
2531 timeout = 0;
2532
2533 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2534 goto nla_put_failure;
2535 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2536 goto nla_put_failure;
2537 if (ctnetlink_exp_dump_tuple(skb,
2538 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2539 CTA_EXPECT_MASTER) < 0)
2540 goto nla_put_failure;
2541
2542 #ifdef CONFIG_NF_NAT_NEEDED
2543 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2544 exp->saved_proto.all) {
2545 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2546 if (!nest_parms)
2547 goto nla_put_failure;
2548
2549 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2550 goto nla_put_failure;
2551
2552 nat_tuple.src.l3num = nf_ct_l3num(master);
2553 nat_tuple.src.u3 = exp->saved_addr;
2554 nat_tuple.dst.protonum = nf_ct_protonum(master);
2555 nat_tuple.src.u = exp->saved_proto;
2556
2557 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2558 CTA_EXPECT_NAT_TUPLE) < 0)
2559 goto nla_put_failure;
2560 nla_nest_end(skb, nest_parms);
2561 }
2562 #endif
2563 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2564 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2565 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2566 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2567 goto nla_put_failure;
2568 help = nfct_help(master);
2569 if (help) {
2570 struct nf_conntrack_helper *helper;
2571
2572 helper = rcu_dereference(help->helper);
2573 if (helper &&
2574 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2575 goto nla_put_failure;
2576 }
2577 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2578 if (expfn != NULL &&
2579 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2580 goto nla_put_failure;
2581
2582 return 0;
2583
2584 nla_put_failure:
2585 return -1;
2586 }
2587
2588 static int
2589 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2590 int event, const struct nf_conntrack_expect *exp)
2591 {
2592 struct nlmsghdr *nlh;
2593 struct nfgenmsg *nfmsg;
2594 unsigned int flags = portid ? NLM_F_MULTI : 0;
2595
2596 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2597 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2598 if (nlh == NULL)
2599 goto nlmsg_failure;
2600
2601 nfmsg = nlmsg_data(nlh);
2602 nfmsg->nfgen_family = exp->tuple.src.l3num;
2603 nfmsg->version = NFNETLINK_V0;
2604 nfmsg->res_id = 0;
2605
2606 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2607 goto nla_put_failure;
2608
2609 nlmsg_end(skb, nlh);
2610 return skb->len;
2611
2612 nlmsg_failure:
2613 nla_put_failure:
2614 nlmsg_cancel(skb, nlh);
2615 return -1;
2616 }
2617
2618 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2619 static int
2620 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2621 {
2622 struct nf_conntrack_expect *exp = item->exp;
2623 struct net *net = nf_ct_exp_net(exp);
2624 struct nlmsghdr *nlh;
2625 struct nfgenmsg *nfmsg;
2626 struct sk_buff *skb;
2627 unsigned int type, group;
2628 int flags = 0;
2629
2630 if (events & (1 << IPEXP_DESTROY)) {
2631 type = IPCTNL_MSG_EXP_DELETE;
2632 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2633 } else if (events & (1 << IPEXP_NEW)) {
2634 type = IPCTNL_MSG_EXP_NEW;
2635 flags = NLM_F_CREATE|NLM_F_EXCL;
2636 group = NFNLGRP_CONNTRACK_EXP_NEW;
2637 } else
2638 return 0;
2639
2640 if (!item->report && !nfnetlink_has_listeners(net, group))
2641 return 0;
2642
2643 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2644 if (skb == NULL)
2645 goto errout;
2646
2647 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2648 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2649 if (nlh == NULL)
2650 goto nlmsg_failure;
2651
2652 nfmsg = nlmsg_data(nlh);
2653 nfmsg->nfgen_family = exp->tuple.src.l3num;
2654 nfmsg->version = NFNETLINK_V0;
2655 nfmsg->res_id = 0;
2656
2657 rcu_read_lock();
2658 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2659 goto nla_put_failure;
2660 rcu_read_unlock();
2661
2662 nlmsg_end(skb, nlh);
2663 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2664 return 0;
2665
2666 nla_put_failure:
2667 rcu_read_unlock();
2668 nlmsg_cancel(skb, nlh);
2669 nlmsg_failure:
2670 kfree_skb(skb);
2671 errout:
2672 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2673 return 0;
2674 }
2675 #endif
2676 static int ctnetlink_exp_done(struct netlink_callback *cb)
2677 {
2678 if (cb->args[1])
2679 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2680 return 0;
2681 }
2682
2683 static int
2684 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2685 {
2686 struct net *net = sock_net(skb->sk);
2687 struct nf_conntrack_expect *exp, *last;
2688 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2689 u_int8_t l3proto = nfmsg->nfgen_family;
2690
2691 rcu_read_lock();
2692 last = (struct nf_conntrack_expect *)cb->args[1];
2693 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2694 restart:
2695 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2696 hnode) {
2697 if (l3proto && exp->tuple.src.l3num != l3proto)
2698 continue;
2699
2700 if (!net_eq(nf_ct_net(exp->master), net))
2701 continue;
2702
2703 if (cb->args[1]) {
2704 if (exp != last)
2705 continue;
2706 cb->args[1] = 0;
2707 }
2708 if (ctnetlink_exp_fill_info(skb,
2709 NETLINK_CB(cb->skb).portid,
2710 cb->nlh->nlmsg_seq,
2711 IPCTNL_MSG_EXP_NEW,
2712 exp) < 0) {
2713 if (!refcount_inc_not_zero(&exp->use))
2714 continue;
2715 cb->args[1] = (unsigned long)exp;
2716 goto out;
2717 }
2718 }
2719 if (cb->args[1]) {
2720 cb->args[1] = 0;
2721 goto restart;
2722 }
2723 }
2724 out:
2725 rcu_read_unlock();
2726 if (last)
2727 nf_ct_expect_put(last);
2728
2729 return skb->len;
2730 }
2731
2732 static int
2733 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2734 {
2735 struct nf_conntrack_expect *exp, *last;
2736 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2737 struct nf_conn *ct = cb->data;
2738 struct nf_conn_help *help = nfct_help(ct);
2739 u_int8_t l3proto = nfmsg->nfgen_family;
2740
2741 if (cb->args[0])
2742 return 0;
2743
2744 rcu_read_lock();
2745 last = (struct nf_conntrack_expect *)cb->args[1];
2746 restart:
2747 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2748 if (l3proto && exp->tuple.src.l3num != l3proto)
2749 continue;
2750 if (cb->args[1]) {
2751 if (exp != last)
2752 continue;
2753 cb->args[1] = 0;
2754 }
2755 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2756 cb->nlh->nlmsg_seq,
2757 IPCTNL_MSG_EXP_NEW,
2758 exp) < 0) {
2759 if (!refcount_inc_not_zero(&exp->use))
2760 continue;
2761 cb->args[1] = (unsigned long)exp;
2762 goto out;
2763 }
2764 }
2765 if (cb->args[1]) {
2766 cb->args[1] = 0;
2767 goto restart;
2768 }
2769 cb->args[0] = 1;
2770 out:
2771 rcu_read_unlock();
2772 if (last)
2773 nf_ct_expect_put(last);
2774
2775 return skb->len;
2776 }
2777
2778 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2779 struct sk_buff *skb,
2780 const struct nlmsghdr *nlh,
2781 const struct nlattr * const cda[])
2782 {
2783 int err;
2784 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2785 u_int8_t u3 = nfmsg->nfgen_family;
2786 struct nf_conntrack_tuple tuple;
2787 struct nf_conntrack_tuple_hash *h;
2788 struct nf_conn *ct;
2789 struct nf_conntrack_zone zone;
2790 struct netlink_dump_control c = {
2791 .dump = ctnetlink_exp_ct_dump_table,
2792 .done = ctnetlink_exp_done,
2793 };
2794
2795 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2796 u3, NULL);
2797 if (err < 0)
2798 return err;
2799
2800 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2801 if (err < 0)
2802 return err;
2803
2804 h = nf_conntrack_find_get(net, &zone, &tuple);
2805 if (!h)
2806 return -ENOENT;
2807
2808 ct = nf_ct_tuplehash_to_ctrack(h);
2809 /* No expectation linked to this connection tracking. */
2810 if (!nfct_help(ct)) {
2811 nf_ct_put(ct);
2812 return 0;
2813 }
2814
2815 c.data = ct;
2816
2817 err = netlink_dump_start(ctnl, skb, nlh, &c);
2818 nf_ct_put(ct);
2819
2820 return err;
2821 }
2822
2823 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2824 struct sk_buff *skb, const struct nlmsghdr *nlh,
2825 const struct nlattr * const cda[])
2826 {
2827 struct nf_conntrack_tuple tuple;
2828 struct nf_conntrack_expect *exp;
2829 struct sk_buff *skb2;
2830 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2831 u_int8_t u3 = nfmsg->nfgen_family;
2832 struct nf_conntrack_zone zone;
2833 int err;
2834
2835 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2836 if (cda[CTA_EXPECT_MASTER])
2837 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2838 else {
2839 struct netlink_dump_control c = {
2840 .dump = ctnetlink_exp_dump_table,
2841 .done = ctnetlink_exp_done,
2842 };
2843 return netlink_dump_start(ctnl, skb, nlh, &c);
2844 }
2845 }
2846
2847 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2848 if (err < 0)
2849 return err;
2850
2851 if (cda[CTA_EXPECT_TUPLE])
2852 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2853 u3, NULL);
2854 else if (cda[CTA_EXPECT_MASTER])
2855 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2856 u3, NULL);
2857 else
2858 return -EINVAL;
2859
2860 if (err < 0)
2861 return err;
2862
2863 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2864 if (!exp)
2865 return -ENOENT;
2866
2867 if (cda[CTA_EXPECT_ID]) {
2868 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2869 if (ntohl(id) != (u32)(unsigned long)exp) {
2870 nf_ct_expect_put(exp);
2871 return -ENOENT;
2872 }
2873 }
2874
2875 err = -ENOMEM;
2876 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2877 if (skb2 == NULL) {
2878 nf_ct_expect_put(exp);
2879 goto out;
2880 }
2881
2882 rcu_read_lock();
2883 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2884 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2885 rcu_read_unlock();
2886 nf_ct_expect_put(exp);
2887 if (err <= 0)
2888 goto free;
2889
2890 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2891 if (err < 0)
2892 goto out;
2893
2894 return 0;
2895
2896 free:
2897 kfree_skb(skb2);
2898 out:
2899 /* this avoids a loop in nfnetlink. */
2900 return err == -EAGAIN ? -ENOBUFS : err;
2901 }
2902
2903 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2904 struct sk_buff *skb, const struct nlmsghdr *nlh,
2905 const struct nlattr * const cda[])
2906 {
2907 struct nf_conntrack_expect *exp;
2908 struct nf_conntrack_tuple tuple;
2909 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2910 struct hlist_node *next;
2911 u_int8_t u3 = nfmsg->nfgen_family;
2912 struct nf_conntrack_zone zone;
2913 unsigned int i;
2914 int err;
2915
2916 if (cda[CTA_EXPECT_TUPLE]) {
2917 /* delete a single expect by tuple */
2918 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2919 if (err < 0)
2920 return err;
2921
2922 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2923 u3, NULL);
2924 if (err < 0)
2925 return err;
2926
2927 /* bump usage count to 2 */
2928 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2929 if (!exp)
2930 return -ENOENT;
2931
2932 if (cda[CTA_EXPECT_ID]) {
2933 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2934 if (ntohl(id) != (u32)(unsigned long)exp) {
2935 nf_ct_expect_put(exp);
2936 return -ENOENT;
2937 }
2938 }
2939
2940 /* after list removal, usage count == 1 */
2941 spin_lock_bh(&nf_conntrack_expect_lock);
2942 if (del_timer(&exp->timeout)) {
2943 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2944 nlmsg_report(nlh));
2945 nf_ct_expect_put(exp);
2946 }
2947 spin_unlock_bh(&nf_conntrack_expect_lock);
2948 /* have to put what we 'get' above.
2949 * after this line usage count == 0 */
2950 nf_ct_expect_put(exp);
2951 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2952 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2953 struct nf_conn_help *m_help;
2954
2955 /* delete all expectations for this helper */
2956 spin_lock_bh(&nf_conntrack_expect_lock);
2957 for (i = 0; i < nf_ct_expect_hsize; i++) {
2958 hlist_for_each_entry_safe(exp, next,
2959 &nf_ct_expect_hash[i],
2960 hnode) {
2961
2962 if (!net_eq(nf_ct_exp_net(exp), net))
2963 continue;
2964
2965 m_help = nfct_help(exp->master);
2966 if (!strcmp(m_help->helper->name, name) &&
2967 del_timer(&exp->timeout)) {
2968 nf_ct_unlink_expect_report(exp,
2969 NETLINK_CB(skb).portid,
2970 nlmsg_report(nlh));
2971 nf_ct_expect_put(exp);
2972 }
2973 }
2974 }
2975 spin_unlock_bh(&nf_conntrack_expect_lock);
2976 } else {
2977 /* This basically means we have to flush everything*/
2978 spin_lock_bh(&nf_conntrack_expect_lock);
2979 for (i = 0; i < nf_ct_expect_hsize; i++) {
2980 hlist_for_each_entry_safe(exp, next,
2981 &nf_ct_expect_hash[i],
2982 hnode) {
2983
2984 if (!net_eq(nf_ct_exp_net(exp), net))
2985 continue;
2986
2987 if (del_timer(&exp->timeout)) {
2988 nf_ct_unlink_expect_report(exp,
2989 NETLINK_CB(skb).portid,
2990 nlmsg_report(nlh));
2991 nf_ct_expect_put(exp);
2992 }
2993 }
2994 }
2995 spin_unlock_bh(&nf_conntrack_expect_lock);
2996 }
2997
2998 return 0;
2999 }
3000 static int
3001 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3002 const struct nlattr * const cda[])
3003 {
3004 if (cda[CTA_EXPECT_TIMEOUT]) {
3005 if (!del_timer(&x->timeout))
3006 return -ETIME;
3007
3008 x->timeout.expires = jiffies +
3009 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3010 add_timer(&x->timeout);
3011 }
3012 return 0;
3013 }
3014
3015 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3016 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3017 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3018 };
3019
3020 static int
3021 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3022 struct nf_conntrack_expect *exp,
3023 u_int8_t u3)
3024 {
3025 #ifdef CONFIG_NF_NAT_NEEDED
3026 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3027 struct nf_conntrack_tuple nat_tuple = {};
3028 int err;
3029
3030 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3031 exp_nat_nla_policy, NULL);
3032 if (err < 0)
3033 return err;
3034
3035 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3036 return -EINVAL;
3037
3038 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3039 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3040 u3, NULL);
3041 if (err < 0)
3042 return err;
3043
3044 exp->saved_addr = nat_tuple.src.u3;
3045 exp->saved_proto = nat_tuple.src.u;
3046 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3047
3048 return 0;
3049 #else
3050 return -EOPNOTSUPP;
3051 #endif
3052 }
3053
3054 static struct nf_conntrack_expect *
3055 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3056 struct nf_conntrack_helper *helper,
3057 struct nf_conntrack_tuple *tuple,
3058 struct nf_conntrack_tuple *mask)
3059 {
3060 u_int32_t class = 0;
3061 struct nf_conntrack_expect *exp;
3062 struct nf_conn_help *help;
3063 int err;
3064
3065 help = nfct_help(ct);
3066 if (!help)
3067 return ERR_PTR(-EOPNOTSUPP);
3068
3069 if (cda[CTA_EXPECT_CLASS] && helper) {
3070 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3071 if (class > helper->expect_class_max)
3072 return ERR_PTR(-EINVAL);
3073 }
3074 exp = nf_ct_expect_alloc(ct);
3075 if (!exp)
3076 return ERR_PTR(-ENOMEM);
3077
3078 if (cda[CTA_EXPECT_FLAGS]) {
3079 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3080 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3081 } else {
3082 exp->flags = 0;
3083 }
3084 if (cda[CTA_EXPECT_FN]) {
3085 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3086 struct nf_ct_helper_expectfn *expfn;
3087
3088 expfn = nf_ct_helper_expectfn_find_by_name(name);
3089 if (expfn == NULL) {
3090 err = -EINVAL;
3091 goto err_out;
3092 }
3093 exp->expectfn = expfn->expectfn;
3094 } else
3095 exp->expectfn = NULL;
3096
3097 exp->class = class;
3098 exp->master = ct;
3099 exp->helper = helper;
3100 exp->tuple = *tuple;
3101 exp->mask.src.u3 = mask->src.u3;
3102 exp->mask.src.u.all = mask->src.u.all;
3103
3104 if (cda[CTA_EXPECT_NAT]) {
3105 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3106 exp, nf_ct_l3num(ct));
3107 if (err < 0)
3108 goto err_out;
3109 }
3110 return exp;
3111 err_out:
3112 nf_ct_expect_put(exp);
3113 return ERR_PTR(err);
3114 }
3115
3116 static int
3117 ctnetlink_create_expect(struct net *net,
3118 const struct nf_conntrack_zone *zone,
3119 const struct nlattr * const cda[],
3120 u_int8_t u3, u32 portid, int report)
3121 {
3122 struct nf_conntrack_tuple tuple, mask, master_tuple;
3123 struct nf_conntrack_tuple_hash *h = NULL;
3124 struct nf_conntrack_helper *helper = NULL;
3125 struct nf_conntrack_expect *exp;
3126 struct nf_conn *ct;
3127 int err;
3128
3129 /* caller guarantees that those three CTA_EXPECT_* exist */
3130 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3131 u3, NULL);
3132 if (err < 0)
3133 return err;
3134 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3135 u3, NULL);
3136 if (err < 0)
3137 return err;
3138 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3139 u3, NULL);
3140 if (err < 0)
3141 return err;
3142
3143 /* Look for master conntrack of this expectation */
3144 h = nf_conntrack_find_get(net, zone, &master_tuple);
3145 if (!h)
3146 return -ENOENT;
3147 ct = nf_ct_tuplehash_to_ctrack(h);
3148
3149 rcu_read_lock();
3150 if (cda[CTA_EXPECT_HELP_NAME]) {
3151 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3152
3153 helper = __nf_conntrack_helper_find(helpname, u3,
3154 nf_ct_protonum(ct));
3155 if (helper == NULL) {
3156 rcu_read_unlock();
3157 #ifdef CONFIG_MODULES
3158 if (request_module("nfct-helper-%s", helpname) < 0) {
3159 err = -EOPNOTSUPP;
3160 goto err_ct;
3161 }
3162 rcu_read_lock();
3163 helper = __nf_conntrack_helper_find(helpname, u3,
3164 nf_ct_protonum(ct));
3165 if (helper) {
3166 err = -EAGAIN;
3167 goto err_rcu;
3168 }
3169 rcu_read_unlock();
3170 #endif
3171 err = -EOPNOTSUPP;
3172 goto err_ct;
3173 }
3174 }
3175
3176 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3177 if (IS_ERR(exp)) {
3178 err = PTR_ERR(exp);
3179 goto err_rcu;
3180 }
3181
3182 err = nf_ct_expect_related_report(exp, portid, report);
3183 nf_ct_expect_put(exp);
3184 err_rcu:
3185 rcu_read_unlock();
3186 err_ct:
3187 nf_ct_put(ct);
3188 return err;
3189 }
3190
3191 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3192 struct sk_buff *skb, const struct nlmsghdr *nlh,
3193 const struct nlattr * const cda[])
3194 {
3195 struct nf_conntrack_tuple tuple;
3196 struct nf_conntrack_expect *exp;
3197 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3198 u_int8_t u3 = nfmsg->nfgen_family;
3199 struct nf_conntrack_zone zone;
3200 int err;
3201
3202 if (!cda[CTA_EXPECT_TUPLE]
3203 || !cda[CTA_EXPECT_MASK]
3204 || !cda[CTA_EXPECT_MASTER])
3205 return -EINVAL;
3206
3207 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3208 if (err < 0)
3209 return err;
3210
3211 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3212 u3, NULL);
3213 if (err < 0)
3214 return err;
3215
3216 spin_lock_bh(&nf_conntrack_expect_lock);
3217 exp = __nf_ct_expect_find(net, &zone, &tuple);
3218 if (!exp) {
3219 spin_unlock_bh(&nf_conntrack_expect_lock);
3220 err = -ENOENT;
3221 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3222 err = ctnetlink_create_expect(net, &zone, cda, u3,
3223 NETLINK_CB(skb).portid,
3224 nlmsg_report(nlh));
3225 }
3226 return err;
3227 }
3228
3229 err = -EEXIST;
3230 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3231 err = ctnetlink_change_expect(exp, cda);
3232 spin_unlock_bh(&nf_conntrack_expect_lock);
3233
3234 return err;
3235 }
3236
3237 static int
3238 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3239 const struct ip_conntrack_stat *st)
3240 {
3241 struct nlmsghdr *nlh;
3242 struct nfgenmsg *nfmsg;
3243 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3244
3245 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3246 IPCTNL_MSG_EXP_GET_STATS_CPU);
3247 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3248 if (nlh == NULL)
3249 goto nlmsg_failure;
3250
3251 nfmsg = nlmsg_data(nlh);
3252 nfmsg->nfgen_family = AF_UNSPEC;
3253 nfmsg->version = NFNETLINK_V0;
3254 nfmsg->res_id = htons(cpu);
3255
3256 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3257 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3258 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3259 goto nla_put_failure;
3260
3261 nlmsg_end(skb, nlh);
3262 return skb->len;
3263
3264 nla_put_failure:
3265 nlmsg_failure:
3266 nlmsg_cancel(skb, nlh);
3267 return -1;
3268 }
3269
3270 static int
3271 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3272 {
3273 int cpu;
3274 struct net *net = sock_net(skb->sk);
3275
3276 if (cb->args[0] == nr_cpu_ids)
3277 return 0;
3278
3279 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3280 const struct ip_conntrack_stat *st;
3281
3282 if (!cpu_possible(cpu))
3283 continue;
3284
3285 st = per_cpu_ptr(net->ct.stat, cpu);
3286 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3287 cb->nlh->nlmsg_seq,
3288 cpu, st) < 0)
3289 break;
3290 }
3291 cb->args[0] = cpu;
3292
3293 return skb->len;
3294 }
3295
3296 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3297 struct sk_buff *skb,
3298 const struct nlmsghdr *nlh,
3299 const struct nlattr * const cda[])
3300 {
3301 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3302 struct netlink_dump_control c = {
3303 .dump = ctnetlink_exp_stat_cpu_dump,
3304 };
3305 return netlink_dump_start(ctnl, skb, nlh, &c);
3306 }
3307
3308 return 0;
3309 }
3310
3311 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3312 static struct nf_ct_event_notifier ctnl_notifier = {
3313 .fcn = ctnetlink_conntrack_event,
3314 };
3315
3316 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3317 .fcn = ctnetlink_expect_event,
3318 };
3319 #endif
3320
3321 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3322 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3323 .attr_count = CTA_MAX,
3324 .policy = ct_nla_policy },
3325 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3326 .attr_count = CTA_MAX,
3327 .policy = ct_nla_policy },
3328 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3329 .attr_count = CTA_MAX,
3330 .policy = ct_nla_policy },
3331 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3332 .attr_count = CTA_MAX,
3333 .policy = ct_nla_policy },
3334 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3335 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3336 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3337 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3338 };
3339
3340 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3341 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3342 .attr_count = CTA_EXPECT_MAX,
3343 .policy = exp_nla_policy },
3344 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3345 .attr_count = CTA_EXPECT_MAX,
3346 .policy = exp_nla_policy },
3347 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3348 .attr_count = CTA_EXPECT_MAX,
3349 .policy = exp_nla_policy },
3350 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3351 };
3352
3353 static const struct nfnetlink_subsystem ctnl_subsys = {
3354 .name = "conntrack",
3355 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3356 .cb_count = IPCTNL_MSG_MAX,
3357 .cb = ctnl_cb,
3358 };
3359
3360 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3361 .name = "conntrack_expect",
3362 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3363 .cb_count = IPCTNL_MSG_EXP_MAX,
3364 .cb = ctnl_exp_cb,
3365 };
3366
3367 MODULE_ALIAS("ip_conntrack_netlink");
3368 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3369 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3370
3371 static int __net_init ctnetlink_net_init(struct net *net)
3372 {
3373 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3374 int ret;
3375
3376 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3377 if (ret < 0) {
3378 pr_err("ctnetlink_init: cannot register notifier.\n");
3379 goto err_out;
3380 }
3381
3382 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3383 if (ret < 0) {
3384 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3385 goto err_unreg_notifier;
3386 }
3387 #endif
3388 return 0;
3389
3390 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3391 err_unreg_notifier:
3392 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3393 err_out:
3394 return ret;
3395 #endif
3396 }
3397
3398 static void ctnetlink_net_exit(struct net *net)
3399 {
3400 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3401 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3402 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3403 #endif
3404 }
3405
3406 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3407 {
3408 struct net *net;
3409
3410 list_for_each_entry(net, net_exit_list, exit_list)
3411 ctnetlink_net_exit(net);
3412 }
3413
3414 static struct pernet_operations ctnetlink_net_ops = {
3415 .init = ctnetlink_net_init,
3416 .exit_batch = ctnetlink_net_exit_batch,
3417 };
3418
3419 static int __init ctnetlink_init(void)
3420 {
3421 int ret;
3422
3423 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3424 ret = nfnetlink_subsys_register(&ctnl_subsys);
3425 if (ret < 0) {
3426 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3427 goto err_out;
3428 }
3429
3430 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3431 if (ret < 0) {
3432 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3433 goto err_unreg_subsys;
3434 }
3435
3436 ret = register_pernet_subsys(&ctnetlink_net_ops);
3437 if (ret < 0) {
3438 pr_err("ctnetlink_init: cannot register pernet operations\n");
3439 goto err_unreg_exp_subsys;
3440 }
3441 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3442 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3443 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3444 #endif
3445 return 0;
3446
3447 err_unreg_exp_subsys:
3448 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3449 err_unreg_subsys:
3450 nfnetlink_subsys_unregister(&ctnl_subsys);
3451 err_out:
3452 return ret;
3453 }
3454
3455 static void __exit ctnetlink_exit(void)
3456 {
3457 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3458
3459 unregister_pernet_subsys(&ctnetlink_net_ops);
3460 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3461 nfnetlink_subsys_unregister(&ctnl_subsys);
3462 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3463 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3464 #endif
3465 synchronize_rcu();
3466 }
3467
3468 module_init(ctnetlink_init);
3469 module_exit(ctnetlink_exit);