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