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