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

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

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

~ [ 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