~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_conntrack_netlink.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp