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