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