IB/hfi1: Fix TID caching actions
[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(labels->words * sizeof(long));
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 len, i;
357
358 if (!labels)
359 return 0;
360
361 len = labels->words * sizeof(long);
362 i = 0;
363 do {
364 if (labels->bits[i] != 0)
365 return nla_put(skb, CTA_LABELS, len, labels->bits);
366 i++;
367 } while (i < labels->words);
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
1898 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1899 &rtuple, u3);
1900 if (IS_ERR(ct))
1901 return PTR_ERR(ct);
1902
1903 err = 0;
1904 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1905 events = IPCT_RELATED;
1906 else
1907 events = IPCT_NEW;
1908
1909 if (cda[CTA_LABELS] &&
1910 ctnetlink_attach_labels(ct, cda) == 0)
1911 events |= (1 << IPCT_LABEL);
1912
1913 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1914 (1 << IPCT_ASSURED) |
1915 (1 << IPCT_HELPER) |
1916 (1 << IPCT_PROTOINFO) |
1917 (1 << IPCT_SEQADJ) |
1918 (1 << IPCT_MARK) | events,
1919 ct, NETLINK_CB(skb).portid,
1920 nlmsg_report(nlh));
1921 nf_ct_put(ct);
1922 }
1923
1924 return err;
1925 }
1926 /* implicit 'else' */
1927
1928 err = -EEXIST;
1929 ct = nf_ct_tuplehash_to_ctrack(h);
1930 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1931 spin_lock_bh(&nf_conntrack_expect_lock);
1932 err = ctnetlink_change_conntrack(ct, cda);
1933 spin_unlock_bh(&nf_conntrack_expect_lock);
1934 if (err == 0) {
1935 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1936 (1 << IPCT_ASSURED) |
1937 (1 << IPCT_HELPER) |
1938 (1 << IPCT_LABEL) |
1939 (1 << IPCT_PROTOINFO) |
1940 (1 << IPCT_SEQADJ) |
1941 (1 << IPCT_MARK),
1942 ct, NETLINK_CB(skb).portid,
1943 nlmsg_report(nlh));
1944 }
1945 }
1946
1947 nf_ct_put(ct);
1948 return err;
1949 }
1950
1951 static int
1952 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1953 __u16 cpu, const struct ip_conntrack_stat *st)
1954 {
1955 struct nlmsghdr *nlh;
1956 struct nfgenmsg *nfmsg;
1957 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1958
1959 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1960 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1961 if (nlh == NULL)
1962 goto nlmsg_failure;
1963
1964 nfmsg = nlmsg_data(nlh);
1965 nfmsg->nfgen_family = AF_UNSPEC;
1966 nfmsg->version = NFNETLINK_V0;
1967 nfmsg->res_id = htons(cpu);
1968
1969 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1970 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1971 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1972 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1973 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1974 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1975 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1976 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1977 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1978 htonl(st->insert_failed)) ||
1979 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1980 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1981 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1982 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1983 htonl(st->search_restart)))
1984 goto nla_put_failure;
1985
1986 nlmsg_end(skb, nlh);
1987 return skb->len;
1988
1989 nla_put_failure:
1990 nlmsg_failure:
1991 nlmsg_cancel(skb, nlh);
1992 return -1;
1993 }
1994
1995 static int
1996 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1997 {
1998 int cpu;
1999 struct net *net = sock_net(skb->sk);
2000
2001 if (cb->args[0] == nr_cpu_ids)
2002 return 0;
2003
2004 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2005 const struct ip_conntrack_stat *st;
2006
2007 if (!cpu_possible(cpu))
2008 continue;
2009
2010 st = per_cpu_ptr(net->ct.stat, cpu);
2011 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2012 NETLINK_CB(cb->skb).portid,
2013 cb->nlh->nlmsg_seq,
2014 cpu, st) < 0)
2015 break;
2016 }
2017 cb->args[0] = cpu;
2018
2019 return skb->len;
2020 }
2021
2022 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2023 struct sk_buff *skb,
2024 const struct nlmsghdr *nlh,
2025 const struct nlattr * const cda[])
2026 {
2027 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2028 struct netlink_dump_control c = {
2029 .dump = ctnetlink_ct_stat_cpu_dump,
2030 };
2031 return netlink_dump_start(ctnl, skb, nlh, &c);
2032 }
2033
2034 return 0;
2035 }
2036
2037 static int
2038 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2039 struct net *net)
2040 {
2041 struct nlmsghdr *nlh;
2042 struct nfgenmsg *nfmsg;
2043 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2044 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2045
2046 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
2047 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2048 if (nlh == NULL)
2049 goto nlmsg_failure;
2050
2051 nfmsg = nlmsg_data(nlh);
2052 nfmsg->nfgen_family = AF_UNSPEC;
2053 nfmsg->version = NFNETLINK_V0;
2054 nfmsg->res_id = 0;
2055
2056 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2057 goto nla_put_failure;
2058
2059 nlmsg_end(skb, nlh);
2060 return skb->len;
2061
2062 nla_put_failure:
2063 nlmsg_failure:
2064 nlmsg_cancel(skb, nlh);
2065 return -1;
2066 }
2067
2068 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2069 struct sk_buff *skb, const struct nlmsghdr *nlh,
2070 const struct nlattr * const cda[])
2071 {
2072 struct sk_buff *skb2;
2073 int err;
2074
2075 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2076 if (skb2 == NULL)
2077 return -ENOMEM;
2078
2079 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2080 nlh->nlmsg_seq,
2081 NFNL_MSG_TYPE(nlh->nlmsg_type),
2082 sock_net(skb->sk));
2083 if (err <= 0)
2084 goto free;
2085
2086 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2087 if (err < 0)
2088 goto out;
2089
2090 return 0;
2091
2092 free:
2093 kfree_skb(skb2);
2094 out:
2095 /* this avoids a loop in nfnetlink. */
2096 return err == -EAGAIN ? -ENOBUFS : err;
2097 }
2098
2099 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2100 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2101 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2102 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2103 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2104 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2105 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2106 .len = NF_CT_HELPER_NAME_LEN - 1 },
2107 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2108 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2109 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2110 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2111 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2112 };
2113
2114 static struct nf_conntrack_expect *
2115 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2116 struct nf_conntrack_helper *helper,
2117 struct nf_conntrack_tuple *tuple,
2118 struct nf_conntrack_tuple *mask);
2119
2120 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2121 static size_t
2122 ctnetlink_glue_build_size(const struct nf_conn *ct)
2123 {
2124 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2125 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2126 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2127 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2128 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2129 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2130 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2131 + nla_total_size(0) /* CTA_PROTOINFO */
2132 + nla_total_size(0) /* CTA_HELP */
2133 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2134 + ctnetlink_secctx_size(ct)
2135 #ifdef CONFIG_NF_NAT_NEEDED
2136 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2137 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2138 #endif
2139 #ifdef CONFIG_NF_CONNTRACK_MARK
2140 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2141 #endif
2142 #ifdef CONFIG_NF_CONNTRACK_ZONES
2143 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2144 #endif
2145 + ctnetlink_proto_size(ct)
2146 ;
2147 }
2148
2149 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2150 enum ip_conntrack_info *ctinfo)
2151 {
2152 struct nf_conn *ct;
2153
2154 ct = nf_ct_get(skb, ctinfo);
2155 if (ct && nf_ct_is_untracked(ct))
2156 ct = NULL;
2157
2158 return ct;
2159 }
2160
2161 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2162 {
2163 const struct nf_conntrack_zone *zone;
2164 struct nlattr *nest_parms;
2165
2166 rcu_read_lock();
2167 zone = nf_ct_zone(ct);
2168
2169 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2170 if (!nest_parms)
2171 goto nla_put_failure;
2172 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2173 goto nla_put_failure;
2174 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2175 NF_CT_ZONE_DIR_ORIG) < 0)
2176 goto nla_put_failure;
2177 nla_nest_end(skb, nest_parms);
2178
2179 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2180 if (!nest_parms)
2181 goto nla_put_failure;
2182 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2183 goto nla_put_failure;
2184 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2185 NF_CT_ZONE_DIR_REPL) < 0)
2186 goto nla_put_failure;
2187 nla_nest_end(skb, nest_parms);
2188
2189 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2190 NF_CT_DEFAULT_ZONE_DIR) < 0)
2191 goto nla_put_failure;
2192
2193 if (ctnetlink_dump_id(skb, ct) < 0)
2194 goto nla_put_failure;
2195
2196 if (ctnetlink_dump_status(skb, ct) < 0)
2197 goto nla_put_failure;
2198
2199 if (ctnetlink_dump_timeout(skb, ct) < 0)
2200 goto nla_put_failure;
2201
2202 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2203 goto nla_put_failure;
2204
2205 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2206 goto nla_put_failure;
2207
2208 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2209 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2210 goto nla_put_failure;
2211 #endif
2212 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2213 goto nla_put_failure;
2214
2215 if ((ct->status & IPS_SEQ_ADJUST) &&
2216 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2217 goto nla_put_failure;
2218
2219 #ifdef CONFIG_NF_CONNTRACK_MARK
2220 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2221 goto nla_put_failure;
2222 #endif
2223 if (ctnetlink_dump_labels(skb, ct) < 0)
2224 goto nla_put_failure;
2225 rcu_read_unlock();
2226 return 0;
2227
2228 nla_put_failure:
2229 rcu_read_unlock();
2230 return -ENOSPC;
2231 }
2232
2233 static int
2234 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2235 enum ip_conntrack_info ctinfo,
2236 u_int16_t ct_attr, u_int16_t ct_info_attr)
2237 {
2238 struct nlattr *nest_parms;
2239
2240 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2241 if (!nest_parms)
2242 goto nla_put_failure;
2243
2244 if (__ctnetlink_glue_build(skb, ct) < 0)
2245 goto nla_put_failure;
2246
2247 nla_nest_end(skb, nest_parms);
2248
2249 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2250 goto nla_put_failure;
2251
2252 return 0;
2253
2254 nla_put_failure:
2255 return -ENOSPC;
2256 }
2257
2258 static int
2259 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2260 {
2261 int err;
2262
2263 if (cda[CTA_TIMEOUT]) {
2264 err = ctnetlink_change_timeout(ct, cda);
2265 if (err < 0)
2266 return err;
2267 }
2268 if (cda[CTA_STATUS]) {
2269 err = ctnetlink_change_status(ct, cda);
2270 if (err < 0)
2271 return err;
2272 }
2273 if (cda[CTA_HELP]) {
2274 err = ctnetlink_change_helper(ct, cda);
2275 if (err < 0)
2276 return err;
2277 }
2278 if (cda[CTA_LABELS]) {
2279 err = ctnetlink_attach_labels(ct, cda);
2280 if (err < 0)
2281 return err;
2282 }
2283 #if defined(CONFIG_NF_CONNTRACK_MARK)
2284 if (cda[CTA_MARK]) {
2285 u32 mask = 0, mark, newmark;
2286 if (cda[CTA_MARK_MASK])
2287 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2288
2289 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2290 newmark = (ct->mark & mask) ^ mark;
2291 if (newmark != ct->mark)
2292 ct->mark = newmark;
2293 }
2294 #endif
2295 return 0;
2296 }
2297
2298 static int
2299 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2300 {
2301 struct nlattr *cda[CTA_MAX+1];
2302 int ret;
2303
2304 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2305 if (ret < 0)
2306 return ret;
2307
2308 spin_lock_bh(&nf_conntrack_expect_lock);
2309 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2310 spin_unlock_bh(&nf_conntrack_expect_lock);
2311
2312 return ret;
2313 }
2314
2315 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2316 const struct nf_conn *ct,
2317 struct nf_conntrack_tuple *tuple,
2318 struct nf_conntrack_tuple *mask)
2319 {
2320 int err;
2321
2322 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2323 nf_ct_l3num(ct), NULL);
2324 if (err < 0)
2325 return err;
2326
2327 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2328 nf_ct_l3num(ct), NULL);
2329 }
2330
2331 static int
2332 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2333 u32 portid, u32 report)
2334 {
2335 struct nlattr *cda[CTA_EXPECT_MAX+1];
2336 struct nf_conntrack_tuple tuple, mask;
2337 struct nf_conntrack_helper *helper = NULL;
2338 struct nf_conntrack_expect *exp;
2339 int err;
2340
2341 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2342 if (err < 0)
2343 return err;
2344
2345 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2346 ct, &tuple, &mask);
2347 if (err < 0)
2348 return err;
2349
2350 if (cda[CTA_EXPECT_HELP_NAME]) {
2351 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2352
2353 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2354 nf_ct_protonum(ct));
2355 if (helper == NULL)
2356 return -EOPNOTSUPP;
2357 }
2358
2359 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2360 helper, &tuple, &mask);
2361 if (IS_ERR(exp))
2362 return PTR_ERR(exp);
2363
2364 err = nf_ct_expect_related_report(exp, portid, report);
2365 if (err < 0) {
2366 nf_ct_expect_put(exp);
2367 return err;
2368 }
2369
2370 return 0;
2371 }
2372
2373 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2374 enum ip_conntrack_info ctinfo, int diff)
2375 {
2376 if (!(ct->status & IPS_NAT_MASK))
2377 return;
2378
2379 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2380 }
2381
2382 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2383 .get_ct = ctnetlink_glue_get_ct,
2384 .build_size = ctnetlink_glue_build_size,
2385 .build = ctnetlink_glue_build,
2386 .parse = ctnetlink_glue_parse,
2387 .attach_expect = ctnetlink_glue_attach_expect,
2388 .seq_adjust = ctnetlink_glue_seqadj,
2389 };
2390 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2391
2392 /***********************************************************************
2393 * EXPECT
2394 ***********************************************************************/
2395
2396 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2397 const struct nf_conntrack_tuple *tuple,
2398 enum ctattr_expect type)
2399 {
2400 struct nlattr *nest_parms;
2401
2402 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2403 if (!nest_parms)
2404 goto nla_put_failure;
2405 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2406 goto nla_put_failure;
2407 nla_nest_end(skb, nest_parms);
2408
2409 return 0;
2410
2411 nla_put_failure:
2412 return -1;
2413 }
2414
2415 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2416 const struct nf_conntrack_tuple *tuple,
2417 const struct nf_conntrack_tuple_mask *mask)
2418 {
2419 int ret;
2420 struct nf_conntrack_l3proto *l3proto;
2421 struct nf_conntrack_l4proto *l4proto;
2422 struct nf_conntrack_tuple m;
2423 struct nlattr *nest_parms;
2424
2425 memset(&m, 0xFF, sizeof(m));
2426 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2427 m.src.u.all = mask->src.u.all;
2428 m.dst.protonum = tuple->dst.protonum;
2429
2430 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2431 if (!nest_parms)
2432 goto nla_put_failure;
2433
2434 rcu_read_lock();
2435 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2436 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2437 if (ret >= 0) {
2438 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2439 tuple->dst.protonum);
2440 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2441 }
2442 rcu_read_unlock();
2443
2444 if (unlikely(ret < 0))
2445 goto nla_put_failure;
2446
2447 nla_nest_end(skb, nest_parms);
2448
2449 return 0;
2450
2451 nla_put_failure:
2452 return -1;
2453 }
2454
2455 static const union nf_inet_addr any_addr;
2456
2457 static int
2458 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2459 const struct nf_conntrack_expect *exp)
2460 {
2461 struct nf_conn *master = exp->master;
2462 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2463 struct nf_conn_help *help;
2464 #ifdef CONFIG_NF_NAT_NEEDED
2465 struct nlattr *nest_parms;
2466 struct nf_conntrack_tuple nat_tuple = {};
2467 #endif
2468 struct nf_ct_helper_expectfn *expfn;
2469
2470 if (timeout < 0)
2471 timeout = 0;
2472
2473 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2474 goto nla_put_failure;
2475 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2476 goto nla_put_failure;
2477 if (ctnetlink_exp_dump_tuple(skb,
2478 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2479 CTA_EXPECT_MASTER) < 0)
2480 goto nla_put_failure;
2481
2482 #ifdef CONFIG_NF_NAT_NEEDED
2483 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2484 exp->saved_proto.all) {
2485 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2486 if (!nest_parms)
2487 goto nla_put_failure;
2488
2489 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2490 goto nla_put_failure;
2491
2492 nat_tuple.src.l3num = nf_ct_l3num(master);
2493 nat_tuple.src.u3 = exp->saved_addr;
2494 nat_tuple.dst.protonum = nf_ct_protonum(master);
2495 nat_tuple.src.u = exp->saved_proto;
2496
2497 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2498 CTA_EXPECT_NAT_TUPLE) < 0)
2499 goto nla_put_failure;
2500 nla_nest_end(skb, nest_parms);
2501 }
2502 #endif
2503 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2504 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2505 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2506 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2507 goto nla_put_failure;
2508 help = nfct_help(master);
2509 if (help) {
2510 struct nf_conntrack_helper *helper;
2511
2512 helper = rcu_dereference(help->helper);
2513 if (helper &&
2514 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2515 goto nla_put_failure;
2516 }
2517 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2518 if (expfn != NULL &&
2519 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2520 goto nla_put_failure;
2521
2522 return 0;
2523
2524 nla_put_failure:
2525 return -1;
2526 }
2527
2528 static int
2529 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2530 int event, const struct nf_conntrack_expect *exp)
2531 {
2532 struct nlmsghdr *nlh;
2533 struct nfgenmsg *nfmsg;
2534 unsigned int flags = portid ? NLM_F_MULTI : 0;
2535
2536 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2537 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2538 if (nlh == NULL)
2539 goto nlmsg_failure;
2540
2541 nfmsg = nlmsg_data(nlh);
2542 nfmsg->nfgen_family = exp->tuple.src.l3num;
2543 nfmsg->version = NFNETLINK_V0;
2544 nfmsg->res_id = 0;
2545
2546 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2547 goto nla_put_failure;
2548
2549 nlmsg_end(skb, nlh);
2550 return skb->len;
2551
2552 nlmsg_failure:
2553 nla_put_failure:
2554 nlmsg_cancel(skb, nlh);
2555 return -1;
2556 }
2557
2558 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2559 static int
2560 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2561 {
2562 struct nf_conntrack_expect *exp = item->exp;
2563 struct net *net = nf_ct_exp_net(exp);
2564 struct nlmsghdr *nlh;
2565 struct nfgenmsg *nfmsg;
2566 struct sk_buff *skb;
2567 unsigned int type, group;
2568 int flags = 0;
2569
2570 if (events & (1 << IPEXP_DESTROY)) {
2571 type = IPCTNL_MSG_EXP_DELETE;
2572 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2573 } else if (events & (1 << IPEXP_NEW)) {
2574 type = IPCTNL_MSG_EXP_NEW;
2575 flags = NLM_F_CREATE|NLM_F_EXCL;
2576 group = NFNLGRP_CONNTRACK_EXP_NEW;
2577 } else
2578 return 0;
2579
2580 if (!item->report && !nfnetlink_has_listeners(net, group))
2581 return 0;
2582
2583 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2584 if (skb == NULL)
2585 goto errout;
2586
2587 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2588 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2589 if (nlh == NULL)
2590 goto nlmsg_failure;
2591
2592 nfmsg = nlmsg_data(nlh);
2593 nfmsg->nfgen_family = exp->tuple.src.l3num;
2594 nfmsg->version = NFNETLINK_V0;
2595 nfmsg->res_id = 0;
2596
2597 rcu_read_lock();
2598 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2599 goto nla_put_failure;
2600 rcu_read_unlock();
2601
2602 nlmsg_end(skb, nlh);
2603 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2604 return 0;
2605
2606 nla_put_failure:
2607 rcu_read_unlock();
2608 nlmsg_cancel(skb, nlh);
2609 nlmsg_failure:
2610 kfree_skb(skb);
2611 errout:
2612 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2613 return 0;
2614 }
2615 #endif
2616 static int ctnetlink_exp_done(struct netlink_callback *cb)
2617 {
2618 if (cb->args[1])
2619 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2620 return 0;
2621 }
2622
2623 static int
2624 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2625 {
2626 struct net *net = sock_net(skb->sk);
2627 struct nf_conntrack_expect *exp, *last;
2628 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2629 u_int8_t l3proto = nfmsg->nfgen_family;
2630
2631 rcu_read_lock();
2632 last = (struct nf_conntrack_expect *)cb->args[1];
2633 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2634 restart:
2635 hlist_for_each_entry(exp, &nf_ct_expect_hash[cb->args[0]],
2636 hnode) {
2637 if (l3proto && exp->tuple.src.l3num != l3proto)
2638 continue;
2639
2640 if (!net_eq(nf_ct_net(exp->master), net))
2641 continue;
2642
2643 if (cb->args[1]) {
2644 if (exp != last)
2645 continue;
2646 cb->args[1] = 0;
2647 }
2648 if (ctnetlink_exp_fill_info(skb,
2649 NETLINK_CB(cb->skb).portid,
2650 cb->nlh->nlmsg_seq,
2651 IPCTNL_MSG_EXP_NEW,
2652 exp) < 0) {
2653 if (!atomic_inc_not_zero(&exp->use))
2654 continue;
2655 cb->args[1] = (unsigned long)exp;
2656 goto out;
2657 }
2658 }
2659 if (cb->args[1]) {
2660 cb->args[1] = 0;
2661 goto restart;
2662 }
2663 }
2664 out:
2665 rcu_read_unlock();
2666 if (last)
2667 nf_ct_expect_put(last);
2668
2669 return skb->len;
2670 }
2671
2672 static int
2673 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2674 {
2675 struct nf_conntrack_expect *exp, *last;
2676 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2677 struct nf_conn *ct = cb->data;
2678 struct nf_conn_help *help = nfct_help(ct);
2679 u_int8_t l3proto = nfmsg->nfgen_family;
2680
2681 if (cb->args[0])
2682 return 0;
2683
2684 rcu_read_lock();
2685 last = (struct nf_conntrack_expect *)cb->args[1];
2686 restart:
2687 hlist_for_each_entry(exp, &help->expectations, lnode) {
2688 if (l3proto && exp->tuple.src.l3num != l3proto)
2689 continue;
2690 if (cb->args[1]) {
2691 if (exp != last)
2692 continue;
2693 cb->args[1] = 0;
2694 }
2695 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2696 cb->nlh->nlmsg_seq,
2697 IPCTNL_MSG_EXP_NEW,
2698 exp) < 0) {
2699 if (!atomic_inc_not_zero(&exp->use))
2700 continue;
2701 cb->args[1] = (unsigned long)exp;
2702 goto out;
2703 }
2704 }
2705 if (cb->args[1]) {
2706 cb->args[1] = 0;
2707 goto restart;
2708 }
2709 cb->args[0] = 1;
2710 out:
2711 rcu_read_unlock();
2712 if (last)
2713 nf_ct_expect_put(last);
2714
2715 return skb->len;
2716 }
2717
2718 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2719 struct sk_buff *skb,
2720 const struct nlmsghdr *nlh,
2721 const struct nlattr * const cda[])
2722 {
2723 int err;
2724 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2725 u_int8_t u3 = nfmsg->nfgen_family;
2726 struct nf_conntrack_tuple tuple;
2727 struct nf_conntrack_tuple_hash *h;
2728 struct nf_conn *ct;
2729 struct nf_conntrack_zone zone;
2730 struct netlink_dump_control c = {
2731 .dump = ctnetlink_exp_ct_dump_table,
2732 .done = ctnetlink_exp_done,
2733 };
2734
2735 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2736 u3, NULL);
2737 if (err < 0)
2738 return err;
2739
2740 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2741 if (err < 0)
2742 return err;
2743
2744 h = nf_conntrack_find_get(net, &zone, &tuple);
2745 if (!h)
2746 return -ENOENT;
2747
2748 ct = nf_ct_tuplehash_to_ctrack(h);
2749 c.data = ct;
2750
2751 err = netlink_dump_start(ctnl, skb, nlh, &c);
2752 nf_ct_put(ct);
2753
2754 return err;
2755 }
2756
2757 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2758 struct sk_buff *skb, const struct nlmsghdr *nlh,
2759 const struct nlattr * const cda[])
2760 {
2761 struct nf_conntrack_tuple tuple;
2762 struct nf_conntrack_expect *exp;
2763 struct sk_buff *skb2;
2764 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2765 u_int8_t u3 = nfmsg->nfgen_family;
2766 struct nf_conntrack_zone zone;
2767 int err;
2768
2769 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2770 if (cda[CTA_EXPECT_MASTER])
2771 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2772 else {
2773 struct netlink_dump_control c = {
2774 .dump = ctnetlink_exp_dump_table,
2775 .done = ctnetlink_exp_done,
2776 };
2777 return netlink_dump_start(ctnl, skb, nlh, &c);
2778 }
2779 }
2780
2781 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2782 if (err < 0)
2783 return err;
2784
2785 if (cda[CTA_EXPECT_TUPLE])
2786 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2787 u3, NULL);
2788 else if (cda[CTA_EXPECT_MASTER])
2789 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2790 u3, NULL);
2791 else
2792 return -EINVAL;
2793
2794 if (err < 0)
2795 return err;
2796
2797 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2798 if (!exp)
2799 return -ENOENT;
2800
2801 if (cda[CTA_EXPECT_ID]) {
2802 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2803 if (ntohl(id) != (u32)(unsigned long)exp) {
2804 nf_ct_expect_put(exp);
2805 return -ENOENT;
2806 }
2807 }
2808
2809 err = -ENOMEM;
2810 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2811 if (skb2 == NULL) {
2812 nf_ct_expect_put(exp);
2813 goto out;
2814 }
2815
2816 rcu_read_lock();
2817 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2818 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2819 rcu_read_unlock();
2820 nf_ct_expect_put(exp);
2821 if (err <= 0)
2822 goto free;
2823
2824 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2825 if (err < 0)
2826 goto out;
2827
2828 return 0;
2829
2830 free:
2831 kfree_skb(skb2);
2832 out:
2833 /* this avoids a loop in nfnetlink. */
2834 return err == -EAGAIN ? -ENOBUFS : err;
2835 }
2836
2837 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2838 struct sk_buff *skb, const struct nlmsghdr *nlh,
2839 const struct nlattr * const cda[])
2840 {
2841 struct nf_conntrack_expect *exp;
2842 struct nf_conntrack_tuple tuple;
2843 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2844 struct hlist_node *next;
2845 u_int8_t u3 = nfmsg->nfgen_family;
2846 struct nf_conntrack_zone zone;
2847 unsigned int i;
2848 int err;
2849
2850 if (cda[CTA_EXPECT_TUPLE]) {
2851 /* delete a single expect by tuple */
2852 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2853 if (err < 0)
2854 return err;
2855
2856 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2857 u3, NULL);
2858 if (err < 0)
2859 return err;
2860
2861 /* bump usage count to 2 */
2862 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2863 if (!exp)
2864 return -ENOENT;
2865
2866 if (cda[CTA_EXPECT_ID]) {
2867 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2868 if (ntohl(id) != (u32)(unsigned long)exp) {
2869 nf_ct_expect_put(exp);
2870 return -ENOENT;
2871 }
2872 }
2873
2874 /* after list removal, usage count == 1 */
2875 spin_lock_bh(&nf_conntrack_expect_lock);
2876 if (del_timer(&exp->timeout)) {
2877 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2878 nlmsg_report(nlh));
2879 nf_ct_expect_put(exp);
2880 }
2881 spin_unlock_bh(&nf_conntrack_expect_lock);
2882 /* have to put what we 'get' above.
2883 * after this line usage count == 0 */
2884 nf_ct_expect_put(exp);
2885 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2886 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2887 struct nf_conn_help *m_help;
2888
2889 /* delete all expectations for this helper */
2890 spin_lock_bh(&nf_conntrack_expect_lock);
2891 for (i = 0; i < nf_ct_expect_hsize; i++) {
2892 hlist_for_each_entry_safe(exp, next,
2893 &nf_ct_expect_hash[i],
2894 hnode) {
2895
2896 if (!net_eq(nf_ct_exp_net(exp), net))
2897 continue;
2898
2899 m_help = nfct_help(exp->master);
2900 if (!strcmp(m_help->helper->name, name) &&
2901 del_timer(&exp->timeout)) {
2902 nf_ct_unlink_expect_report(exp,
2903 NETLINK_CB(skb).portid,
2904 nlmsg_report(nlh));
2905 nf_ct_expect_put(exp);
2906 }
2907 }
2908 }
2909 spin_unlock_bh(&nf_conntrack_expect_lock);
2910 } else {
2911 /* This basically means we have to flush everything*/
2912 spin_lock_bh(&nf_conntrack_expect_lock);
2913 for (i = 0; i < nf_ct_expect_hsize; i++) {
2914 hlist_for_each_entry_safe(exp, next,
2915 &nf_ct_expect_hash[i],
2916 hnode) {
2917
2918 if (!net_eq(nf_ct_exp_net(exp), net))
2919 continue;
2920
2921 if (del_timer(&exp->timeout)) {
2922 nf_ct_unlink_expect_report(exp,
2923 NETLINK_CB(skb).portid,
2924 nlmsg_report(nlh));
2925 nf_ct_expect_put(exp);
2926 }
2927 }
2928 }
2929 spin_unlock_bh(&nf_conntrack_expect_lock);
2930 }
2931
2932 return 0;
2933 }
2934 static int
2935 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2936 const struct nlattr * const cda[])
2937 {
2938 if (cda[CTA_EXPECT_TIMEOUT]) {
2939 if (!del_timer(&x->timeout))
2940 return -ETIME;
2941
2942 x->timeout.expires = jiffies +
2943 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2944 add_timer(&x->timeout);
2945 }
2946 return 0;
2947 }
2948
2949 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2950 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2951 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2952 };
2953
2954 static int
2955 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2956 struct nf_conntrack_expect *exp,
2957 u_int8_t u3)
2958 {
2959 #ifdef CONFIG_NF_NAT_NEEDED
2960 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2961 struct nf_conntrack_tuple nat_tuple = {};
2962 int err;
2963
2964 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2965 if (err < 0)
2966 return err;
2967
2968 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2969 return -EINVAL;
2970
2971 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2972 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
2973 u3, NULL);
2974 if (err < 0)
2975 return err;
2976
2977 exp->saved_addr = nat_tuple.src.u3;
2978 exp->saved_proto = nat_tuple.src.u;
2979 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2980
2981 return 0;
2982 #else
2983 return -EOPNOTSUPP;
2984 #endif
2985 }
2986
2987 static struct nf_conntrack_expect *
2988 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
2989 struct nf_conntrack_helper *helper,
2990 struct nf_conntrack_tuple *tuple,
2991 struct nf_conntrack_tuple *mask)
2992 {
2993 u_int32_t class = 0;
2994 struct nf_conntrack_expect *exp;
2995 struct nf_conn_help *help;
2996 int err;
2997
2998 if (cda[CTA_EXPECT_CLASS] && helper) {
2999 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3000 if (class > helper->expect_class_max)
3001 return ERR_PTR(-EINVAL);
3002 }
3003 exp = nf_ct_expect_alloc(ct);
3004 if (!exp)
3005 return ERR_PTR(-ENOMEM);
3006
3007 help = nfct_help(ct);
3008 if (!help) {
3009 if (!cda[CTA_EXPECT_TIMEOUT]) {
3010 err = -EINVAL;
3011 goto err_out;
3012 }
3013 exp->timeout.expires =
3014 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3015
3016 exp->flags = NF_CT_EXPECT_USERSPACE;
3017 if (cda[CTA_EXPECT_FLAGS]) {
3018 exp->flags |=
3019 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3020 }
3021 } else {
3022 if (cda[CTA_EXPECT_FLAGS]) {
3023 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3024 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3025 } else
3026 exp->flags = 0;
3027 }
3028 if (cda[CTA_EXPECT_FN]) {
3029 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3030 struct nf_ct_helper_expectfn *expfn;
3031
3032 expfn = nf_ct_helper_expectfn_find_by_name(name);
3033 if (expfn == NULL) {
3034 err = -EINVAL;
3035 goto err_out;
3036 }
3037 exp->expectfn = expfn->expectfn;
3038 } else
3039 exp->expectfn = NULL;
3040
3041 exp->class = class;
3042 exp->master = ct;
3043 exp->helper = helper;
3044 exp->tuple = *tuple;
3045 exp->mask.src.u3 = mask->src.u3;
3046 exp->mask.src.u.all = mask->src.u.all;
3047
3048 if (cda[CTA_EXPECT_NAT]) {
3049 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3050 exp, nf_ct_l3num(ct));
3051 if (err < 0)
3052 goto err_out;
3053 }
3054 return exp;
3055 err_out:
3056 nf_ct_expect_put(exp);
3057 return ERR_PTR(err);
3058 }
3059
3060 static int
3061 ctnetlink_create_expect(struct net *net,
3062 const struct nf_conntrack_zone *zone,
3063 const struct nlattr * const cda[],
3064 u_int8_t u3, u32 portid, int report)
3065 {
3066 struct nf_conntrack_tuple tuple, mask, master_tuple;
3067 struct nf_conntrack_tuple_hash *h = NULL;
3068 struct nf_conntrack_helper *helper = NULL;
3069 struct nf_conntrack_expect *exp;
3070 struct nf_conn *ct;
3071 int err;
3072
3073 /* caller guarantees that those three CTA_EXPECT_* exist */
3074 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3075 u3, NULL);
3076 if (err < 0)
3077 return err;
3078 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3079 u3, NULL);
3080 if (err < 0)
3081 return err;
3082 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3083 u3, NULL);
3084 if (err < 0)
3085 return err;
3086
3087 /* Look for master conntrack of this expectation */
3088 h = nf_conntrack_find_get(net, zone, &master_tuple);
3089 if (!h)
3090 return -ENOENT;
3091 ct = nf_ct_tuplehash_to_ctrack(h);
3092
3093 if (cda[CTA_EXPECT_HELP_NAME]) {
3094 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3095
3096 helper = __nf_conntrack_helper_find(helpname, u3,
3097 nf_ct_protonum(ct));
3098 if (helper == NULL) {
3099 #ifdef CONFIG_MODULES
3100 if (request_module("nfct-helper-%s", helpname) < 0) {
3101 err = -EOPNOTSUPP;
3102 goto err_ct;
3103 }
3104 helper = __nf_conntrack_helper_find(helpname, u3,
3105 nf_ct_protonum(ct));
3106 if (helper) {
3107 err = -EAGAIN;
3108 goto err_ct;
3109 }
3110 #endif
3111 err = -EOPNOTSUPP;
3112 goto err_ct;
3113 }
3114 }
3115
3116 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3117 if (IS_ERR(exp)) {
3118 err = PTR_ERR(exp);
3119 goto err_ct;
3120 }
3121
3122 err = nf_ct_expect_related_report(exp, portid, report);
3123 nf_ct_expect_put(exp);
3124 err_ct:
3125 nf_ct_put(ct);
3126 return err;
3127 }
3128
3129 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3130 struct sk_buff *skb, const struct nlmsghdr *nlh,
3131 const struct nlattr * const cda[])
3132 {
3133 struct nf_conntrack_tuple tuple;
3134 struct nf_conntrack_expect *exp;
3135 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3136 u_int8_t u3 = nfmsg->nfgen_family;
3137 struct nf_conntrack_zone zone;
3138 int err;
3139
3140 if (!cda[CTA_EXPECT_TUPLE]
3141 || !cda[CTA_EXPECT_MASK]
3142 || !cda[CTA_EXPECT_MASTER])
3143 return -EINVAL;
3144
3145 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3146 if (err < 0)
3147 return err;
3148
3149 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3150 u3, NULL);
3151 if (err < 0)
3152 return err;
3153
3154 spin_lock_bh(&nf_conntrack_expect_lock);
3155 exp = __nf_ct_expect_find(net, &zone, &tuple);
3156 if (!exp) {
3157 spin_unlock_bh(&nf_conntrack_expect_lock);
3158 err = -ENOENT;
3159 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3160 err = ctnetlink_create_expect(net, &zone, cda, u3,
3161 NETLINK_CB(skb).portid,
3162 nlmsg_report(nlh));
3163 }
3164 return err;
3165 }
3166
3167 err = -EEXIST;
3168 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3169 err = ctnetlink_change_expect(exp, cda);
3170 spin_unlock_bh(&nf_conntrack_expect_lock);
3171
3172 return err;
3173 }
3174
3175 static int
3176 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3177 const struct ip_conntrack_stat *st)
3178 {
3179 struct nlmsghdr *nlh;
3180 struct nfgenmsg *nfmsg;
3181 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3182
3183 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
3184 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3185 if (nlh == NULL)
3186 goto nlmsg_failure;
3187
3188 nfmsg = nlmsg_data(nlh);
3189 nfmsg->nfgen_family = AF_UNSPEC;
3190 nfmsg->version = NFNETLINK_V0;
3191 nfmsg->res_id = htons(cpu);
3192
3193 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3194 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3195 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3196 goto nla_put_failure;
3197
3198 nlmsg_end(skb, nlh);
3199 return skb->len;
3200
3201 nla_put_failure:
3202 nlmsg_failure:
3203 nlmsg_cancel(skb, nlh);
3204 return -1;
3205 }
3206
3207 static int
3208 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3209 {
3210 int cpu;
3211 struct net *net = sock_net(skb->sk);
3212
3213 if (cb->args[0] == nr_cpu_ids)
3214 return 0;
3215
3216 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3217 const struct ip_conntrack_stat *st;
3218
3219 if (!cpu_possible(cpu))
3220 continue;
3221
3222 st = per_cpu_ptr(net->ct.stat, cpu);
3223 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3224 cb->nlh->nlmsg_seq,
3225 cpu, st) < 0)
3226 break;
3227 }
3228 cb->args[0] = cpu;
3229
3230 return skb->len;
3231 }
3232
3233 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3234 struct sk_buff *skb,
3235 const struct nlmsghdr *nlh,
3236 const struct nlattr * const cda[])
3237 {
3238 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3239 struct netlink_dump_control c = {
3240 .dump = ctnetlink_exp_stat_cpu_dump,
3241 };
3242 return netlink_dump_start(ctnl, skb, nlh, &c);
3243 }
3244
3245 return 0;
3246 }
3247
3248 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3249 static struct nf_ct_event_notifier ctnl_notifier = {
3250 .fcn = ctnetlink_conntrack_event,
3251 };
3252
3253 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3254 .fcn = ctnetlink_expect_event,
3255 };
3256 #endif
3257
3258 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3259 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3260 .attr_count = CTA_MAX,
3261 .policy = ct_nla_policy },
3262 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3263 .attr_count = CTA_MAX,
3264 .policy = ct_nla_policy },
3265 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3266 .attr_count = CTA_MAX,
3267 .policy = ct_nla_policy },
3268 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3269 .attr_count = CTA_MAX,
3270 .policy = ct_nla_policy },
3271 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3272 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3273 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3274 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3275 };
3276
3277 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3278 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3279 .attr_count = CTA_EXPECT_MAX,
3280 .policy = exp_nla_policy },
3281 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3282 .attr_count = CTA_EXPECT_MAX,
3283 .policy = exp_nla_policy },
3284 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3285 .attr_count = CTA_EXPECT_MAX,
3286 .policy = exp_nla_policy },
3287 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3288 };
3289
3290 static const struct nfnetlink_subsystem ctnl_subsys = {
3291 .name = "conntrack",
3292 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3293 .cb_count = IPCTNL_MSG_MAX,
3294 .cb = ctnl_cb,
3295 };
3296
3297 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3298 .name = "conntrack_expect",
3299 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3300 .cb_count = IPCTNL_MSG_EXP_MAX,
3301 .cb = ctnl_exp_cb,
3302 };
3303
3304 MODULE_ALIAS("ip_conntrack_netlink");
3305 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3306 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3307
3308 static int __net_init ctnetlink_net_init(struct net *net)
3309 {
3310 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3311 int ret;
3312
3313 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3314 if (ret < 0) {
3315 pr_err("ctnetlink_init: cannot register notifier.\n");
3316 goto err_out;
3317 }
3318
3319 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3320 if (ret < 0) {
3321 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3322 goto err_unreg_notifier;
3323 }
3324 #endif
3325 return 0;
3326
3327 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3328 err_unreg_notifier:
3329 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3330 err_out:
3331 return ret;
3332 #endif
3333 }
3334
3335 static void ctnetlink_net_exit(struct net *net)
3336 {
3337 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3338 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3339 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3340 #endif
3341 }
3342
3343 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3344 {
3345 struct net *net;
3346
3347 list_for_each_entry(net, net_exit_list, exit_list)
3348 ctnetlink_net_exit(net);
3349 }
3350
3351 static struct pernet_operations ctnetlink_net_ops = {
3352 .init = ctnetlink_net_init,
3353 .exit_batch = ctnetlink_net_exit_batch,
3354 };
3355
3356 static int __init ctnetlink_init(void)
3357 {
3358 int ret;
3359
3360 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3361 ret = nfnetlink_subsys_register(&ctnl_subsys);
3362 if (ret < 0) {
3363 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3364 goto err_out;
3365 }
3366
3367 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3368 if (ret < 0) {
3369 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3370 goto err_unreg_subsys;
3371 }
3372
3373 ret = register_pernet_subsys(&ctnetlink_net_ops);
3374 if (ret < 0) {
3375 pr_err("ctnetlink_init: cannot register pernet operations\n");
3376 goto err_unreg_exp_subsys;
3377 }
3378 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3379 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3380 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3381 #endif
3382 return 0;
3383
3384 err_unreg_exp_subsys:
3385 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3386 err_unreg_subsys:
3387 nfnetlink_subsys_unregister(&ctnl_subsys);
3388 err_out:
3389 return ret;
3390 }
3391
3392 static void __exit ctnetlink_exit(void)
3393 {
3394 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3395
3396 unregister_pernet_subsys(&ctnetlink_net_ops);
3397 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3398 nfnetlink_subsys_unregister(&ctnl_subsys);
3399 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3400 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3401 #endif
3402 }
3403
3404 module_init(ctnetlink_init);
3405 module_exit(ctnetlink_exit);
This page took 0.10313 seconds and 5 git commands to generate.