GNU Linux-libre 4.14.332-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 #if IS_ENABLED(CONFIG_NF_NAT)
2535 static const union nf_inet_addr any_addr;
2536 #endif
2537
2538 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2539 {
2540         static __read_mostly siphash_key_t exp_id_seed;
2541         unsigned long a, b, c, d;
2542
2543         net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2544
2545         a = (unsigned long)exp;
2546         b = (unsigned long)exp->helper;
2547         c = (unsigned long)exp->master;
2548         d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2549
2550 #ifdef CONFIG_64BIT
2551         return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2552 #else
2553         return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2554 #endif
2555 }
2556
2557 static int
2558 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2559                           const struct nf_conntrack_expect *exp)
2560 {
2561         struct nf_conn *master = exp->master;
2562         long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2563         struct nf_conn_help *help;
2564 #ifdef CONFIG_NF_NAT_NEEDED
2565         struct nlattr *nest_parms;
2566         struct nf_conntrack_tuple nat_tuple = {};
2567 #endif
2568         struct nf_ct_helper_expectfn *expfn;
2569
2570         if (timeout < 0)
2571                 timeout = 0;
2572
2573         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2574                 goto nla_put_failure;
2575         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2576                 goto nla_put_failure;
2577         if (ctnetlink_exp_dump_tuple(skb,
2578                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2579                                  CTA_EXPECT_MASTER) < 0)
2580                 goto nla_put_failure;
2581
2582 #ifdef CONFIG_NF_NAT_NEEDED
2583         if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2584             exp->saved_proto.all) {
2585                 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2586                 if (!nest_parms)
2587                         goto nla_put_failure;
2588
2589                 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2590                         goto nla_put_failure;
2591
2592                 nat_tuple.src.l3num = nf_ct_l3num(master);
2593                 nat_tuple.src.u3 = exp->saved_addr;
2594                 nat_tuple.dst.protonum = nf_ct_protonum(master);
2595                 nat_tuple.src.u = exp->saved_proto;
2596
2597                 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2598                                                 CTA_EXPECT_NAT_TUPLE) < 0)
2599                         goto nla_put_failure;
2600                 nla_nest_end(skb, nest_parms);
2601         }
2602 #endif
2603         if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2604             nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2605             nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2606             nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2607                 goto nla_put_failure;
2608         help = nfct_help(master);
2609         if (help) {
2610                 struct nf_conntrack_helper *helper;
2611
2612                 helper = rcu_dereference(help->helper);
2613                 if (helper &&
2614                     nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2615                         goto nla_put_failure;
2616         }
2617         expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2618         if (expfn != NULL &&
2619             nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2620                 goto nla_put_failure;
2621
2622         return 0;
2623
2624 nla_put_failure:
2625         return -1;
2626 }
2627
2628 static int
2629 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2630                         int event, const struct nf_conntrack_expect *exp)
2631 {
2632         struct nlmsghdr *nlh;
2633         struct nfgenmsg *nfmsg;
2634         unsigned int flags = portid ? NLM_F_MULTI : 0;
2635
2636         event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2637         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2638         if (nlh == NULL)
2639                 goto nlmsg_failure;
2640
2641         nfmsg = nlmsg_data(nlh);
2642         nfmsg->nfgen_family = exp->tuple.src.l3num;
2643         nfmsg->version      = NFNETLINK_V0;
2644         nfmsg->res_id       = 0;
2645
2646         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2647                 goto nla_put_failure;
2648
2649         nlmsg_end(skb, nlh);
2650         return skb->len;
2651
2652 nlmsg_failure:
2653 nla_put_failure:
2654         nlmsg_cancel(skb, nlh);
2655         return -1;
2656 }
2657
2658 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2659 static int
2660 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2661 {
2662         struct nf_conntrack_expect *exp = item->exp;
2663         struct net *net = nf_ct_exp_net(exp);
2664         struct nlmsghdr *nlh;
2665         struct nfgenmsg *nfmsg;
2666         struct sk_buff *skb;
2667         unsigned int type, group;
2668         int flags = 0;
2669
2670         if (events & (1 << IPEXP_DESTROY)) {
2671                 type = IPCTNL_MSG_EXP_DELETE;
2672                 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2673         } else if (events & (1 << IPEXP_NEW)) {
2674                 type = IPCTNL_MSG_EXP_NEW;
2675                 flags = NLM_F_CREATE|NLM_F_EXCL;
2676                 group = NFNLGRP_CONNTRACK_EXP_NEW;
2677         } else
2678                 return 0;
2679
2680         if (!item->report && !nfnetlink_has_listeners(net, group))
2681                 return 0;
2682
2683         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2684         if (skb == NULL)
2685                 goto errout;
2686
2687         type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2688         nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2689         if (nlh == NULL)
2690                 goto nlmsg_failure;
2691
2692         nfmsg = nlmsg_data(nlh);
2693         nfmsg->nfgen_family = exp->tuple.src.l3num;
2694         nfmsg->version      = NFNETLINK_V0;
2695         nfmsg->res_id       = 0;
2696
2697         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2698                 goto nla_put_failure;
2699
2700         nlmsg_end(skb, nlh);
2701         nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2702         return 0;
2703
2704 nla_put_failure:
2705         nlmsg_cancel(skb, nlh);
2706 nlmsg_failure:
2707         kfree_skb(skb);
2708 errout:
2709         nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2710         return 0;
2711 }
2712 #endif
2713 static int ctnetlink_exp_done(struct netlink_callback *cb)
2714 {
2715         if (cb->args[1])
2716                 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2717         return 0;
2718 }
2719
2720 static int
2721 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2722 {
2723         struct net *net = sock_net(skb->sk);
2724         struct nf_conntrack_expect *exp, *last;
2725         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2726         u_int8_t l3proto = nfmsg->nfgen_family;
2727
2728         rcu_read_lock();
2729         last = (struct nf_conntrack_expect *)cb->args[1];
2730         for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2731 restart:
2732                 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2733                                          hnode) {
2734                         if (l3proto && exp->tuple.src.l3num != l3proto)
2735                                 continue;
2736
2737                         if (!net_eq(nf_ct_net(exp->master), net))
2738                                 continue;
2739
2740                         if (cb->args[1]) {
2741                                 if (exp != last)
2742                                         continue;
2743                                 cb->args[1] = 0;
2744                         }
2745                         if (ctnetlink_exp_fill_info(skb,
2746                                                     NETLINK_CB(cb->skb).portid,
2747                                                     cb->nlh->nlmsg_seq,
2748                                                     IPCTNL_MSG_EXP_NEW,
2749                                                     exp) < 0) {
2750                                 if (!refcount_inc_not_zero(&exp->use))
2751                                         continue;
2752                                 cb->args[1] = (unsigned long)exp;
2753                                 goto out;
2754                         }
2755                 }
2756                 if (cb->args[1]) {
2757                         cb->args[1] = 0;
2758                         goto restart;
2759                 }
2760         }
2761 out:
2762         rcu_read_unlock();
2763         if (last)
2764                 nf_ct_expect_put(last);
2765
2766         return skb->len;
2767 }
2768
2769 static int
2770 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2771 {
2772         struct nf_conntrack_expect *exp, *last;
2773         struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2774         struct nf_conn *ct = cb->data;
2775         struct nf_conn_help *help = nfct_help(ct);
2776         u_int8_t l3proto = nfmsg->nfgen_family;
2777
2778         if (cb->args[0])
2779                 return 0;
2780
2781         rcu_read_lock();
2782         last = (struct nf_conntrack_expect *)cb->args[1];
2783 restart:
2784         hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2785                 if (l3proto && exp->tuple.src.l3num != l3proto)
2786                         continue;
2787                 if (cb->args[1]) {
2788                         if (exp != last)
2789                                 continue;
2790                         cb->args[1] = 0;
2791                 }
2792                 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2793                                             cb->nlh->nlmsg_seq,
2794                                             IPCTNL_MSG_EXP_NEW,
2795                                             exp) < 0) {
2796                         if (!refcount_inc_not_zero(&exp->use))
2797                                 continue;
2798                         cb->args[1] = (unsigned long)exp;
2799                         goto out;
2800                 }
2801         }
2802         if (cb->args[1]) {
2803                 cb->args[1] = 0;
2804                 goto restart;
2805         }
2806         cb->args[0] = 1;
2807 out:
2808         rcu_read_unlock();
2809         if (last)
2810                 nf_ct_expect_put(last);
2811
2812         return skb->len;
2813 }
2814
2815 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2816                                  struct sk_buff *skb,
2817                                  const struct nlmsghdr *nlh,
2818                                  const struct nlattr * const cda[],
2819                                  struct netlink_ext_ack *extack)
2820 {
2821         int err;
2822         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2823         u_int8_t u3 = nfmsg->nfgen_family;
2824         struct nf_conntrack_tuple tuple;
2825         struct nf_conntrack_tuple_hash *h;
2826         struct nf_conn *ct;
2827         struct nf_conntrack_zone zone;
2828         struct netlink_dump_control c = {
2829                 .dump = ctnetlink_exp_ct_dump_table,
2830                 .done = ctnetlink_exp_done,
2831         };
2832
2833         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2834                                     u3, NULL);
2835         if (err < 0)
2836                 return err;
2837
2838         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2839         if (err < 0)
2840                 return err;
2841
2842         h = nf_conntrack_find_get(net, &zone, &tuple);
2843         if (!h)
2844                 return -ENOENT;
2845
2846         ct = nf_ct_tuplehash_to_ctrack(h);
2847         /* No expectation linked to this connection tracking. */
2848         if (!nfct_help(ct)) {
2849                 nf_ct_put(ct);
2850                 return 0;
2851         }
2852
2853         c.data = ct;
2854
2855         err = netlink_dump_start(ctnl, skb, nlh, &c);
2856         nf_ct_put(ct);
2857
2858         return err;
2859 }
2860
2861 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2862                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
2863                                 const struct nlattr * const cda[],
2864                                 struct netlink_ext_ack *extack)
2865 {
2866         struct nf_conntrack_tuple tuple;
2867         struct nf_conntrack_expect *exp;
2868         struct sk_buff *skb2;
2869         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2870         u_int8_t u3 = nfmsg->nfgen_family;
2871         struct nf_conntrack_zone zone;
2872         int err;
2873
2874         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2875                 if (cda[CTA_EXPECT_MASTER])
2876                         return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2877                                                      extack);
2878                 else {
2879                         struct netlink_dump_control c = {
2880                                 .dump = ctnetlink_exp_dump_table,
2881                                 .done = ctnetlink_exp_done,
2882                         };
2883                         return netlink_dump_start(ctnl, skb, nlh, &c);
2884                 }
2885         }
2886
2887         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2888         if (err < 0)
2889                 return err;
2890
2891         if (cda[CTA_EXPECT_TUPLE])
2892                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2893                                             u3, NULL);
2894         else if (cda[CTA_EXPECT_MASTER])
2895                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2896                                             u3, NULL);
2897         else
2898                 return -EINVAL;
2899
2900         if (err < 0)
2901                 return err;
2902
2903         exp = nf_ct_expect_find_get(net, &zone, &tuple);
2904         if (!exp)
2905                 return -ENOENT;
2906
2907         if (cda[CTA_EXPECT_ID]) {
2908                 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2909
2910                 if (id != nf_expect_get_id(exp)) {
2911                         nf_ct_expect_put(exp);
2912                         return -ENOENT;
2913                 }
2914         }
2915
2916         err = -ENOMEM;
2917         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2918         if (skb2 == NULL) {
2919                 nf_ct_expect_put(exp);
2920                 goto out;
2921         }
2922
2923         rcu_read_lock();
2924         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2925                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2926         rcu_read_unlock();
2927         nf_ct_expect_put(exp);
2928         if (err <= 0)
2929                 goto free;
2930
2931         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2932         if (err < 0)
2933                 goto out;
2934
2935         return 0;
2936
2937 free:
2938         kfree_skb(skb2);
2939 out:
2940         /* this avoids a loop in nfnetlink. */
2941         return err == -EAGAIN ? -ENOBUFS : err;
2942 }
2943
2944 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2945 {
2946         const struct nf_conn_help *m_help;
2947         const char *name = data;
2948
2949         m_help = nfct_help(exp->master);
2950
2951         return strcmp(m_help->helper->name, name) == 0;
2952 }
2953
2954 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2955 {
2956         return true;
2957 }
2958
2959 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2960                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
2961                                 const struct nlattr * const cda[],
2962                                 struct netlink_ext_ack *extack)
2963 {
2964         struct nf_conntrack_expect *exp;
2965         struct nf_conntrack_tuple tuple;
2966         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2967         u_int8_t u3 = nfmsg->nfgen_family;
2968         struct nf_conntrack_zone zone;
2969         int err;
2970
2971         if (cda[CTA_EXPECT_TUPLE]) {
2972                 /* delete a single expect by tuple */
2973                 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2974                 if (err < 0)
2975                         return err;
2976
2977                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2978                                             u3, NULL);
2979                 if (err < 0)
2980                         return err;
2981
2982                 /* bump usage count to 2 */
2983                 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2984                 if (!exp)
2985                         return -ENOENT;
2986
2987                 if (cda[CTA_EXPECT_ID]) {
2988                         __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2989                         if (ntohl(id) != (u32)(unsigned long)exp) {
2990                                 nf_ct_expect_put(exp);
2991                                 return -ENOENT;
2992                         }
2993                 }
2994
2995                 /* after list removal, usage count == 1 */
2996                 spin_lock_bh(&nf_conntrack_expect_lock);
2997                 if (del_timer(&exp->timeout)) {
2998                         nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2999                                                    nlmsg_report(nlh));
3000                         nf_ct_expect_put(exp);
3001                 }
3002                 spin_unlock_bh(&nf_conntrack_expect_lock);
3003                 /* have to put what we 'get' above.
3004                  * after this line usage count == 0 */
3005                 nf_ct_expect_put(exp);
3006         } else if (cda[CTA_EXPECT_HELP_NAME]) {
3007                 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3008
3009                 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3010                                          NETLINK_CB(skb).portid,
3011                                          nlmsg_report(nlh));
3012         } else {
3013                 /* This basically means we have to flush everything*/
3014                 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3015                                          NETLINK_CB(skb).portid,
3016                                          nlmsg_report(nlh));
3017         }
3018
3019         return 0;
3020 }
3021 static int
3022 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3023                         const struct nlattr * const cda[])
3024 {
3025         if (cda[CTA_EXPECT_TIMEOUT]) {
3026                 if (!del_timer(&x->timeout))
3027                         return -ETIME;
3028
3029                 x->timeout.expires = jiffies +
3030                         ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3031                 add_timer(&x->timeout);
3032         }
3033         return 0;
3034 }
3035
3036 #if IS_ENABLED(CONFIG_NF_NAT)
3037 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3038         [CTA_EXPECT_NAT_DIR]    = { .type = NLA_U32 },
3039         [CTA_EXPECT_NAT_TUPLE]  = { .type = NLA_NESTED },
3040 };
3041 #endif
3042
3043 static int
3044 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3045                            struct nf_conntrack_expect *exp,
3046                            u_int8_t u3)
3047 {
3048 #ifdef CONFIG_NF_NAT_NEEDED
3049         struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3050         struct nf_conntrack_tuple nat_tuple = {};
3051         int err;
3052
3053         err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3054                                exp_nat_nla_policy, NULL);
3055         if (err < 0)
3056                 return err;
3057
3058         if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3059                 return -EINVAL;
3060
3061         err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3062                                     &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3063                                     u3, NULL);
3064         if (err < 0)
3065                 return err;
3066
3067         exp->saved_addr = nat_tuple.src.u3;
3068         exp->saved_proto = nat_tuple.src.u;
3069         exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3070
3071         return 0;
3072 #else
3073         return -EOPNOTSUPP;
3074 #endif
3075 }
3076
3077 static struct nf_conntrack_expect *
3078 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3079                        struct nf_conntrack_helper *helper,
3080                        struct nf_conntrack_tuple *tuple,
3081                        struct nf_conntrack_tuple *mask)
3082 {
3083         u_int32_t class = 0;
3084         struct nf_conntrack_expect *exp;
3085         struct nf_conn_help *help;
3086         int err;
3087
3088         help = nfct_help(ct);
3089         if (!help)
3090                 return ERR_PTR(-EOPNOTSUPP);
3091
3092         if (cda[CTA_EXPECT_CLASS] && helper) {
3093                 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3094                 if (class > helper->expect_class_max)
3095                         return ERR_PTR(-EINVAL);
3096         }
3097         exp = nf_ct_expect_alloc(ct);
3098         if (!exp)
3099                 return ERR_PTR(-ENOMEM);
3100
3101         if (cda[CTA_EXPECT_FLAGS]) {
3102                 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3103                 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3104         } else {
3105                 exp->flags = 0;
3106         }
3107         if (cda[CTA_EXPECT_FN]) {
3108                 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3109                 struct nf_ct_helper_expectfn *expfn;
3110
3111                 expfn = nf_ct_helper_expectfn_find_by_name(name);
3112                 if (expfn == NULL) {
3113                         err = -EINVAL;
3114                         goto err_out;
3115                 }
3116                 exp->expectfn = expfn->expectfn;
3117         } else
3118                 exp->expectfn = NULL;
3119
3120         exp->class = class;
3121         exp->master = ct;
3122         exp->helper = helper;
3123         exp->tuple = *tuple;
3124         exp->mask.src.u3 = mask->src.u3;
3125         exp->mask.src.u.all = mask->src.u.all;
3126
3127         if (cda[CTA_EXPECT_NAT]) {
3128                 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3129                                                  exp, nf_ct_l3num(ct));
3130                 if (err < 0)
3131                         goto err_out;
3132         }
3133         return exp;
3134 err_out:
3135         nf_ct_expect_put(exp);
3136         return ERR_PTR(err);
3137 }
3138
3139 static int
3140 ctnetlink_create_expect(struct net *net,
3141                         const struct nf_conntrack_zone *zone,
3142                         const struct nlattr * const cda[],
3143                         u_int8_t u3, u32 portid, int report)
3144 {
3145         struct nf_conntrack_tuple tuple, mask, master_tuple;
3146         struct nf_conntrack_tuple_hash *h = NULL;
3147         struct nf_conntrack_helper *helper = NULL;
3148         struct nf_conntrack_expect *exp;
3149         struct nf_conn *ct;
3150         int err;
3151
3152         /* caller guarantees that those three CTA_EXPECT_* exist */
3153         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3154                                     u3, NULL);
3155         if (err < 0)
3156                 return err;
3157         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3158                                     u3, NULL);
3159         if (err < 0)
3160                 return err;
3161         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3162                                     u3, NULL);
3163         if (err < 0)
3164                 return err;
3165
3166         /* Look for master conntrack of this expectation */
3167         h = nf_conntrack_find_get(net, zone, &master_tuple);
3168         if (!h)
3169                 return -ENOENT;
3170         ct = nf_ct_tuplehash_to_ctrack(h);
3171
3172         rcu_read_lock();
3173         if (cda[CTA_EXPECT_HELP_NAME]) {
3174                 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3175
3176                 helper = __nf_conntrack_helper_find(helpname, u3,
3177                                                     nf_ct_protonum(ct));
3178                 if (helper == NULL) {
3179                         rcu_read_unlock();
3180 #ifdef CONFIG_MODULES
3181                         if (request_module("nfct-helper-%s", helpname) < 0) {
3182                                 err = -EOPNOTSUPP;
3183                                 goto err_ct;
3184                         }
3185                         rcu_read_lock();
3186                         helper = __nf_conntrack_helper_find(helpname, u3,
3187                                                             nf_ct_protonum(ct));
3188                         if (helper) {
3189                                 err = -EAGAIN;
3190                                 goto err_rcu;
3191                         }
3192                         rcu_read_unlock();
3193 #endif
3194                         err = -EOPNOTSUPP;
3195                         goto err_ct;
3196                 }
3197         }
3198
3199         exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3200         if (IS_ERR(exp)) {
3201                 err = PTR_ERR(exp);
3202                 goto err_rcu;
3203         }
3204
3205         err = nf_ct_expect_related_report(exp, portid, report);
3206         nf_ct_expect_put(exp);
3207 err_rcu:
3208         rcu_read_unlock();
3209 err_ct:
3210         nf_ct_put(ct);
3211         return err;
3212 }
3213
3214 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3215                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
3216                                 const struct nlattr * const cda[],
3217                                 struct netlink_ext_ack *extack)
3218 {
3219         struct nf_conntrack_tuple tuple;
3220         struct nf_conntrack_expect *exp;
3221         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3222         u_int8_t u3 = nfmsg->nfgen_family;
3223         struct nf_conntrack_zone zone;
3224         int err;
3225
3226         if (!cda[CTA_EXPECT_TUPLE]
3227             || !cda[CTA_EXPECT_MASK]
3228             || !cda[CTA_EXPECT_MASTER])
3229                 return -EINVAL;
3230
3231         err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3232         if (err < 0)
3233                 return err;
3234
3235         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3236                                     u3, NULL);
3237         if (err < 0)
3238                 return err;
3239
3240         spin_lock_bh(&nf_conntrack_expect_lock);
3241         exp = __nf_ct_expect_find(net, &zone, &tuple);
3242         if (!exp) {
3243                 spin_unlock_bh(&nf_conntrack_expect_lock);
3244                 err = -ENOENT;
3245                 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3246                         err = ctnetlink_create_expect(net, &zone, cda, u3,
3247                                                       NETLINK_CB(skb).portid,
3248                                                       nlmsg_report(nlh));
3249                 }
3250                 return err;
3251         }
3252
3253         err = -EEXIST;
3254         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3255                 err = ctnetlink_change_expect(exp, cda);
3256         spin_unlock_bh(&nf_conntrack_expect_lock);
3257
3258         return err;
3259 }
3260
3261 static int
3262 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3263                              const struct ip_conntrack_stat *st)
3264 {
3265         struct nlmsghdr *nlh;
3266         struct nfgenmsg *nfmsg;
3267         unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3268
3269         event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3270                               IPCTNL_MSG_EXP_GET_STATS_CPU);
3271         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3272         if (nlh == NULL)
3273                 goto nlmsg_failure;
3274
3275         nfmsg = nlmsg_data(nlh);
3276         nfmsg->nfgen_family = AF_UNSPEC;
3277         nfmsg->version      = NFNETLINK_V0;
3278         nfmsg->res_id       = htons(cpu);
3279
3280         if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3281             nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3282             nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3283                 goto nla_put_failure;
3284
3285         nlmsg_end(skb, nlh);
3286         return skb->len;
3287
3288 nla_put_failure:
3289 nlmsg_failure:
3290         nlmsg_cancel(skb, nlh);
3291         return -1;
3292 }
3293
3294 static int
3295 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3296 {
3297         int cpu;
3298         struct net *net = sock_net(skb->sk);
3299
3300         if (cb->args[0] == nr_cpu_ids)
3301                 return 0;
3302
3303         for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3304                 const struct ip_conntrack_stat *st;
3305
3306                 if (!cpu_possible(cpu))
3307                         continue;
3308
3309                 st = per_cpu_ptr(net->ct.stat, cpu);
3310                 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3311                                                  cb->nlh->nlmsg_seq,
3312                                                  cpu, st) < 0)
3313                         break;
3314         }
3315         cb->args[0] = cpu;
3316
3317         return skb->len;
3318 }
3319
3320 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3321                                   struct sk_buff *skb,
3322                                   const struct nlmsghdr *nlh,
3323                                   const struct nlattr * const cda[],
3324                                   struct netlink_ext_ack *extack)
3325 {
3326         if (nlh->nlmsg_flags & NLM_F_DUMP) {
3327                 struct netlink_dump_control c = {
3328                         .dump = ctnetlink_exp_stat_cpu_dump,
3329                 };
3330                 return netlink_dump_start(ctnl, skb, nlh, &c);
3331         }
3332
3333         return 0;
3334 }
3335
3336 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3337 static struct nf_ct_event_notifier ctnl_notifier = {
3338         .fcn = ctnetlink_conntrack_event,
3339 };
3340
3341 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3342         .fcn = ctnetlink_expect_event,
3343 };
3344 #endif
3345
3346 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3347         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
3348                                             .attr_count = CTA_MAX,
3349                                             .policy = ct_nla_policy },
3350         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
3351                                             .attr_count = CTA_MAX,
3352                                             .policy = ct_nla_policy },
3353         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
3354                                             .attr_count = CTA_MAX,
3355                                             .policy = ct_nla_policy },
3356         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
3357                                             .attr_count = CTA_MAX,
3358                                             .policy = ct_nla_policy },
3359         [IPCTNL_MSG_CT_GET_STATS_CPU]   = { .call = ctnetlink_stat_ct_cpu },
3360         [IPCTNL_MSG_CT_GET_STATS]       = { .call = ctnetlink_stat_ct },
3361         [IPCTNL_MSG_CT_GET_DYING]       = { .call = ctnetlink_get_ct_dying },
3362         [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3363 };
3364
3365 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3366         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
3367                                             .attr_count = CTA_EXPECT_MAX,
3368                                             .policy = exp_nla_policy },
3369         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
3370                                             .attr_count = CTA_EXPECT_MAX,
3371                                             .policy = exp_nla_policy },
3372         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
3373                                             .attr_count = CTA_EXPECT_MAX,
3374                                             .policy = exp_nla_policy },
3375         [IPCTNL_MSG_EXP_GET_STATS_CPU]  = { .call = ctnetlink_stat_exp_cpu },
3376 };
3377
3378 static const struct nfnetlink_subsystem ctnl_subsys = {
3379         .name                           = "conntrack",
3380         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
3381         .cb_count                       = IPCTNL_MSG_MAX,
3382         .cb                             = ctnl_cb,
3383 };
3384
3385 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3386         .name                           = "conntrack_expect",
3387         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
3388         .cb_count                       = IPCTNL_MSG_EXP_MAX,
3389         .cb                             = ctnl_exp_cb,
3390 };
3391
3392 MODULE_ALIAS("ip_conntrack_netlink");
3393 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3394 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3395
3396 static int __net_init ctnetlink_net_init(struct net *net)
3397 {
3398 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3399         int ret;
3400
3401         ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3402         if (ret < 0) {
3403                 pr_err("ctnetlink_init: cannot register notifier.\n");
3404                 goto err_out;
3405         }
3406
3407         ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3408         if (ret < 0) {
3409                 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3410                 goto err_unreg_notifier;
3411         }
3412 #endif
3413         return 0;
3414
3415 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3416 err_unreg_notifier:
3417         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3418 err_out:
3419         return ret;
3420 #endif
3421 }
3422
3423 static void ctnetlink_net_exit(struct net *net)
3424 {
3425 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3426         nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3427         nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3428 #endif
3429 }
3430
3431 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3432 {
3433         struct net *net;
3434
3435         list_for_each_entry(net, net_exit_list, exit_list)
3436                 ctnetlink_net_exit(net);
3437
3438         /* wait for other cpus until they are done with ctnl_notifiers */
3439         synchronize_rcu();
3440 }
3441
3442 static struct pernet_operations ctnetlink_net_ops = {
3443         .init           = ctnetlink_net_init,
3444         .exit_batch     = ctnetlink_net_exit_batch,
3445 };
3446
3447 static int __init ctnetlink_init(void)
3448 {
3449         int ret;
3450
3451         pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3452         ret = nfnetlink_subsys_register(&ctnl_subsys);
3453         if (ret < 0) {
3454                 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3455                 goto err_out;
3456         }
3457
3458         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3459         if (ret < 0) {
3460                 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3461                 goto err_unreg_subsys;
3462         }
3463
3464         ret = register_pernet_subsys(&ctnetlink_net_ops);
3465         if (ret < 0) {
3466                 pr_err("ctnetlink_init: cannot register pernet operations\n");
3467                 goto err_unreg_exp_subsys;
3468         }
3469 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3470         /* setup interaction between nf_queue and nf_conntrack_netlink. */
3471         RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3472 #endif
3473         return 0;
3474
3475 err_unreg_exp_subsys:
3476         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3477 err_unreg_subsys:
3478         nfnetlink_subsys_unregister(&ctnl_subsys);
3479 err_out:
3480         return ret;
3481 }
3482
3483 static void __exit ctnetlink_exit(void)
3484 {
3485         pr_info("ctnetlink: unregistering from nfnetlink.\n");
3486
3487         unregister_pernet_subsys(&ctnetlink_net_ops);
3488         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3489         nfnetlink_subsys_unregister(&ctnl_subsys);
3490 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3491         RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3492 #endif
3493         synchronize_rcu();
3494 }
3495
3496 module_init(ctnetlink_init);
3497 module_exit(ctnetlink_exit);