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