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