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