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