]> asedeno.scripts.mit.edu Git - linux.git/blob - net/netfilter/nft_tunnel.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux.git] / net / netfilter / nft_tunnel.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/seqlock.h>
6 #include <linux/netlink.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nf_tables.h>
9 #include <net/netfilter/nf_tables.h>
10 #include <net/dst_metadata.h>
11 #include <net/ip_tunnels.h>
12 #include <net/vxlan.h>
13 #include <net/erspan.h>
14
15 struct nft_tunnel {
16         enum nft_tunnel_keys    key:8;
17         enum nft_registers      dreg:8;
18         enum nft_tunnel_mode    mode:8;
19 };
20
21 static void nft_tunnel_get_eval(const struct nft_expr *expr,
22                                 struct nft_regs *regs,
23                                 const struct nft_pktinfo *pkt)
24 {
25         const struct nft_tunnel *priv = nft_expr_priv(expr);
26         u32 *dest = &regs->data[priv->dreg];
27         struct ip_tunnel_info *tun_info;
28
29         tun_info = skb_tunnel_info(pkt->skb);
30
31         switch (priv->key) {
32         case NFT_TUNNEL_PATH:
33                 if (!tun_info) {
34                         nft_reg_store8(dest, false);
35                         return;
36                 }
37                 if (priv->mode == NFT_TUNNEL_MODE_NONE ||
38                     (priv->mode == NFT_TUNNEL_MODE_RX &&
39                      !(tun_info->mode & IP_TUNNEL_INFO_TX)) ||
40                     (priv->mode == NFT_TUNNEL_MODE_TX &&
41                      (tun_info->mode & IP_TUNNEL_INFO_TX)))
42                         nft_reg_store8(dest, true);
43                 else
44                         nft_reg_store8(dest, false);
45                 break;
46         case NFT_TUNNEL_ID:
47                 if (!tun_info) {
48                         regs->verdict.code = NFT_BREAK;
49                         return;
50                 }
51                 if (priv->mode == NFT_TUNNEL_MODE_NONE ||
52                     (priv->mode == NFT_TUNNEL_MODE_RX &&
53                      !(tun_info->mode & IP_TUNNEL_INFO_TX)) ||
54                     (priv->mode == NFT_TUNNEL_MODE_TX &&
55                      (tun_info->mode & IP_TUNNEL_INFO_TX)))
56                         *dest = ntohl(tunnel_id_to_key32(tun_info->key.tun_id));
57                 else
58                         regs->verdict.code = NFT_BREAK;
59                 break;
60         default:
61                 WARN_ON(1);
62                 regs->verdict.code = NFT_BREAK;
63         }
64 }
65
66 static const struct nla_policy nft_tunnel_policy[NFTA_TUNNEL_MAX + 1] = {
67         [NFTA_TUNNEL_KEY]       = { .type = NLA_U32 },
68         [NFTA_TUNNEL_DREG]      = { .type = NLA_U32 },
69         [NFTA_TUNNEL_MODE]      = { .type = NLA_U32 },
70 };
71
72 static int nft_tunnel_get_init(const struct nft_ctx *ctx,
73                                const struct nft_expr *expr,
74                                const struct nlattr * const tb[])
75 {
76         struct nft_tunnel *priv = nft_expr_priv(expr);
77         u32 len;
78
79         if (!tb[NFTA_TUNNEL_KEY] &&
80             !tb[NFTA_TUNNEL_DREG])
81                 return -EINVAL;
82
83         priv->key = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY]));
84         switch (priv->key) {
85         case NFT_TUNNEL_PATH:
86                 len = sizeof(u8);
87                 break;
88         case NFT_TUNNEL_ID:
89                 len = sizeof(u32);
90                 break;
91         default:
92                 return -EOPNOTSUPP;
93         }
94
95         priv->dreg = nft_parse_register(tb[NFTA_TUNNEL_DREG]);
96
97         if (tb[NFTA_TUNNEL_MODE]) {
98                 priv->mode = ntohl(nla_get_be32(tb[NFTA_TUNNEL_MODE]));
99                 if (priv->mode > NFT_TUNNEL_MODE_MAX)
100                         return -EOPNOTSUPP;
101         } else {
102                 priv->mode = NFT_TUNNEL_MODE_NONE;
103         }
104
105         return nft_validate_register_store(ctx, priv->dreg, NULL,
106                                            NFT_DATA_VALUE, len);
107 }
108
109 static int nft_tunnel_get_dump(struct sk_buff *skb,
110                                const struct nft_expr *expr)
111 {
112         const struct nft_tunnel *priv = nft_expr_priv(expr);
113
114         if (nla_put_be32(skb, NFTA_TUNNEL_KEY, htonl(priv->key)))
115                 goto nla_put_failure;
116         if (nft_dump_register(skb, NFTA_TUNNEL_DREG, priv->dreg))
117                 goto nla_put_failure;
118         if (nla_put_be32(skb, NFTA_TUNNEL_MODE, htonl(priv->mode)))
119                 goto nla_put_failure;
120         return 0;
121
122 nla_put_failure:
123         return -1;
124 }
125
126 static struct nft_expr_type nft_tunnel_type;
127 static const struct nft_expr_ops nft_tunnel_get_ops = {
128         .type           = &nft_tunnel_type,
129         .size           = NFT_EXPR_SIZE(sizeof(struct nft_tunnel)),
130         .eval           = nft_tunnel_get_eval,
131         .init           = nft_tunnel_get_init,
132         .dump           = nft_tunnel_get_dump,
133 };
134
135 static struct nft_expr_type nft_tunnel_type __read_mostly = {
136         .name           = "tunnel",
137         .ops            = &nft_tunnel_get_ops,
138         .policy         = nft_tunnel_policy,
139         .maxattr        = NFTA_TUNNEL_MAX,
140         .owner          = THIS_MODULE,
141 };
142
143 struct nft_tunnel_opts {
144         union {
145                 struct vxlan_metadata   vxlan;
146                 struct erspan_metadata  erspan;
147         } u;
148         u32     len;
149         __be16  flags;
150 };
151
152 struct nft_tunnel_obj {
153         struct metadata_dst     *md;
154         struct nft_tunnel_opts  opts;
155 };
156
157 static const struct nla_policy nft_tunnel_ip_policy[NFTA_TUNNEL_KEY_IP_MAX + 1] = {
158         [NFTA_TUNNEL_KEY_IP_SRC]        = { .type = NLA_U32 },
159         [NFTA_TUNNEL_KEY_IP_DST]        = { .type = NLA_U32 },
160 };
161
162 static int nft_tunnel_obj_ip_init(const struct nft_ctx *ctx,
163                                   const struct nlattr *attr,
164                                   struct ip_tunnel_info *info)
165 {
166         struct nlattr *tb[NFTA_TUNNEL_KEY_IP_MAX + 1];
167         int err;
168
169         err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_IP_MAX, attr,
170                                           nft_tunnel_ip_policy, NULL);
171         if (err < 0)
172                 return err;
173
174         if (!tb[NFTA_TUNNEL_KEY_IP_DST])
175                 return -EINVAL;
176
177         if (tb[NFTA_TUNNEL_KEY_IP_SRC])
178                 info->key.u.ipv4.src = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP_SRC]);
179         if (tb[NFTA_TUNNEL_KEY_IP_DST])
180                 info->key.u.ipv4.dst = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP_DST]);
181
182         return 0;
183 }
184
185 static const struct nla_policy nft_tunnel_ip6_policy[NFTA_TUNNEL_KEY_IP6_MAX + 1] = {
186         [NFTA_TUNNEL_KEY_IP6_SRC]       = { .len = sizeof(struct in6_addr), },
187         [NFTA_TUNNEL_KEY_IP6_DST]       = { .len = sizeof(struct in6_addr), },
188         [NFTA_TUNNEL_KEY_IP6_FLOWLABEL] = { .type = NLA_U32, }
189 };
190
191 static int nft_tunnel_obj_ip6_init(const struct nft_ctx *ctx,
192                                    const struct nlattr *attr,
193                                    struct ip_tunnel_info *info)
194 {
195         struct nlattr *tb[NFTA_TUNNEL_KEY_IP6_MAX + 1];
196         int err;
197
198         err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_IP6_MAX, attr,
199                                           nft_tunnel_ip6_policy, NULL);
200         if (err < 0)
201                 return err;
202
203         if (!tb[NFTA_TUNNEL_KEY_IP6_DST])
204                 return -EINVAL;
205
206         if (tb[NFTA_TUNNEL_KEY_IP6_SRC]) {
207                 memcpy(&info->key.u.ipv6.src,
208                        nla_data(tb[NFTA_TUNNEL_KEY_IP6_SRC]),
209                        sizeof(struct in6_addr));
210         }
211         if (tb[NFTA_TUNNEL_KEY_IP6_DST]) {
212                 memcpy(&info->key.u.ipv6.dst,
213                        nla_data(tb[NFTA_TUNNEL_KEY_IP6_DST]),
214                        sizeof(struct in6_addr));
215         }
216         if (tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL])
217                 info->key.label = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]);
218
219         info->mode |= IP_TUNNEL_INFO_IPV6;
220
221         return 0;
222 }
223
224 static const struct nla_policy nft_tunnel_opts_vxlan_policy[NFTA_TUNNEL_KEY_VXLAN_MAX + 1] = {
225         [NFTA_TUNNEL_KEY_VXLAN_GBP]     = { .type = NLA_U32 },
226 };
227
228 static int nft_tunnel_obj_vxlan_init(const struct nlattr *attr,
229                                      struct nft_tunnel_opts *opts)
230 {
231         struct nlattr *tb[NFTA_TUNNEL_KEY_VXLAN_MAX + 1];
232         int err;
233
234         err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_VXLAN_MAX, attr,
235                                           nft_tunnel_opts_vxlan_policy, NULL);
236         if (err < 0)
237                 return err;
238
239         if (!tb[NFTA_TUNNEL_KEY_VXLAN_GBP])
240                 return -EINVAL;
241
242         opts->u.vxlan.gbp = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_VXLAN_GBP]));
243
244         opts->len       = sizeof(struct vxlan_metadata);
245         opts->flags     = TUNNEL_VXLAN_OPT;
246
247         return 0;
248 }
249
250 static const struct nla_policy nft_tunnel_opts_erspan_policy[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1] = {
251         [NFTA_TUNNEL_KEY_ERSPAN_VERSION]        = { .type = NLA_U32 },
252         [NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]       = { .type = NLA_U32 },
253         [NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]         = { .type = NLA_U8 },
254         [NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]        = { .type = NLA_U8 },
255 };
256
257 static int nft_tunnel_obj_erspan_init(const struct nlattr *attr,
258                                       struct nft_tunnel_opts *opts)
259 {
260         struct nlattr *tb[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1];
261         uint8_t hwid, dir;
262         int err, version;
263
264         err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_ERSPAN_MAX,
265                                           attr, nft_tunnel_opts_erspan_policy,
266                                           NULL);
267         if (err < 0)
268                 return err;
269
270         version = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]));
271         switch (version) {
272         case ERSPAN_VERSION:
273                 if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX])
274                         return -EINVAL;
275
276                 opts->u.erspan.u.index =
277                         nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]);
278                 break;
279         case ERSPAN_VERSION2:
280                 if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] ||
281                     !tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID])
282                         return -EINVAL;
283
284                 hwid = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]);
285                 dir = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]);
286
287                 set_hwid(&opts->u.erspan.u.md2, hwid);
288                 opts->u.erspan.u.md2.dir = dir;
289                 break;
290         default:
291                 return -EOPNOTSUPP;
292         }
293         opts->u.erspan.version = version;
294
295         opts->len       = sizeof(struct erspan_metadata);
296         opts->flags     = TUNNEL_ERSPAN_OPT;
297
298         return 0;
299 }
300
301 static const struct nla_policy nft_tunnel_opts_policy[NFTA_TUNNEL_KEY_OPTS_MAX + 1] = {
302         [NFTA_TUNNEL_KEY_OPTS_VXLAN]    = { .type = NLA_NESTED, },
303         [NFTA_TUNNEL_KEY_OPTS_ERSPAN]   = { .type = NLA_NESTED, },
304 };
305
306 static int nft_tunnel_obj_opts_init(const struct nft_ctx *ctx,
307                                     const struct nlattr *attr,
308                                     struct ip_tunnel_info *info,
309                                     struct nft_tunnel_opts *opts)
310 {
311         struct nlattr *tb[NFTA_TUNNEL_KEY_OPTS_MAX + 1];
312         int err;
313
314         err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_OPTS_MAX, attr,
315                                           nft_tunnel_opts_policy, NULL);
316         if (err < 0)
317                 return err;
318
319         if (tb[NFTA_TUNNEL_KEY_OPTS_VXLAN]) {
320                 err = nft_tunnel_obj_vxlan_init(tb[NFTA_TUNNEL_KEY_OPTS_VXLAN],
321                                                 opts);
322         } else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
323                 err = nft_tunnel_obj_erspan_init(tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
324                                                  opts);
325         } else {
326                 return -EOPNOTSUPP;
327         }
328
329         return err;
330 }
331
332 static const struct nla_policy nft_tunnel_key_policy[NFTA_TUNNEL_KEY_MAX + 1] = {
333         [NFTA_TUNNEL_KEY_IP]    = { .type = NLA_NESTED, },
334         [NFTA_TUNNEL_KEY_IP6]   = { .type = NLA_NESTED, },
335         [NFTA_TUNNEL_KEY_ID]    = { .type = NLA_U32, },
336         [NFTA_TUNNEL_KEY_FLAGS] = { .type = NLA_U32, },
337         [NFTA_TUNNEL_KEY_TOS]   = { .type = NLA_U8, },
338         [NFTA_TUNNEL_KEY_TTL]   = { .type = NLA_U8, },
339         [NFTA_TUNNEL_KEY_OPTS]  = { .type = NLA_NESTED, },
340 };
341
342 static int nft_tunnel_obj_init(const struct nft_ctx *ctx,
343                                const struct nlattr * const tb[],
344                                struct nft_object *obj)
345 {
346         struct nft_tunnel_obj *priv = nft_obj_data(obj);
347         struct ip_tunnel_info info;
348         struct metadata_dst *md;
349         int err;
350
351         if (!tb[NFTA_TUNNEL_KEY_ID])
352                 return -EINVAL;
353
354         memset(&info, 0, sizeof(info));
355         info.mode               = IP_TUNNEL_INFO_TX;
356         info.key.tun_id         = key32_to_tunnel_id(nla_get_be32(tb[NFTA_TUNNEL_KEY_ID]));
357         info.key.tun_flags      = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
358
359         if (tb[NFTA_TUNNEL_KEY_IP]) {
360                 err = nft_tunnel_obj_ip_init(ctx, tb[NFTA_TUNNEL_KEY_IP], &info);
361                 if (err < 0)
362                         return err;
363         } else if (tb[NFTA_TUNNEL_KEY_IP6]) {
364                 err = nft_tunnel_obj_ip6_init(ctx, tb[NFTA_TUNNEL_KEY_IP6], &info);
365                 if (err < 0)
366                         return err;
367         } else {
368                 return -EINVAL;
369         }
370
371         if (tb[NFTA_TUNNEL_KEY_SPORT]) {
372                 info.key.tp_src = nla_get_be16(tb[NFTA_TUNNEL_KEY_SPORT]);
373         }
374         if (tb[NFTA_TUNNEL_KEY_DPORT]) {
375                 info.key.tp_dst = nla_get_be16(tb[NFTA_TUNNEL_KEY_DPORT]);
376         }
377
378         if (tb[NFTA_TUNNEL_KEY_FLAGS]) {
379                 u32 tun_flags;
380
381                 tun_flags = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_FLAGS]));
382                 if (tun_flags & ~NFT_TUNNEL_F_MASK)
383                         return -EOPNOTSUPP;
384
385                 if (tun_flags & NFT_TUNNEL_F_ZERO_CSUM_TX)
386                         info.key.tun_flags &= ~TUNNEL_CSUM;
387                 if (tun_flags & NFT_TUNNEL_F_DONT_FRAGMENT)
388                         info.key.tun_flags |= TUNNEL_DONT_FRAGMENT;
389                 if (tun_flags & NFT_TUNNEL_F_SEQ_NUMBER)
390                         info.key.tun_flags |= TUNNEL_SEQ;
391         }
392         if (tb[NFTA_TUNNEL_KEY_TOS])
393                 info.key.tos = nla_get_u8(tb[NFTA_TUNNEL_KEY_TOS]);
394         if (tb[NFTA_TUNNEL_KEY_TTL])
395                 info.key.ttl = nla_get_u8(tb[NFTA_TUNNEL_KEY_TTL]);
396         else
397                 info.key.ttl = U8_MAX;
398
399         if (tb[NFTA_TUNNEL_KEY_OPTS]) {
400                 err = nft_tunnel_obj_opts_init(ctx, tb[NFTA_TUNNEL_KEY_OPTS],
401                                                &info, &priv->opts);
402                 if (err < 0)
403                         return err;
404         }
405
406         md = metadata_dst_alloc(priv->opts.len, METADATA_IP_TUNNEL, GFP_KERNEL);
407         if (!md)
408                 return -ENOMEM;
409
410         memcpy(&md->u.tun_info, &info, sizeof(info));
411 #ifdef CONFIG_DST_CACHE
412         err = dst_cache_init(&md->u.tun_info.dst_cache, GFP_KERNEL);
413         if (err < 0) {
414                 metadata_dst_free(md);
415                 return err;
416         }
417 #endif
418         ip_tunnel_info_opts_set(&md->u.tun_info, &priv->opts.u, priv->opts.len,
419                                 priv->opts.flags);
420         priv->md = md;
421
422         return 0;
423 }
424
425 static inline void nft_tunnel_obj_eval(struct nft_object *obj,
426                                        struct nft_regs *regs,
427                                        const struct nft_pktinfo *pkt)
428 {
429         struct nft_tunnel_obj *priv = nft_obj_data(obj);
430         struct sk_buff *skb = pkt->skb;
431
432         skb_dst_drop(skb);
433         dst_hold((struct dst_entry *) priv->md);
434         skb_dst_set(skb, (struct dst_entry *) priv->md);
435 }
436
437 static int nft_tunnel_ip_dump(struct sk_buff *skb, struct ip_tunnel_info *info)
438 {
439         struct nlattr *nest;
440
441         if (info->mode & IP_TUNNEL_INFO_IPV6) {
442                 nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_IP6);
443                 if (!nest)
444                         return -1;
445
446                 if (nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_SRC,
447                                      &info->key.u.ipv6.src) < 0 ||
448                     nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_DST,
449                                      &info->key.u.ipv6.dst) < 0 ||
450                     nla_put_be32(skb, NFTA_TUNNEL_KEY_IP6_FLOWLABEL,
451                                  info->key.label)) {
452                         nla_nest_cancel(skb, nest);
453                         return -1;
454                 }
455
456                 nla_nest_end(skb, nest);
457         } else {
458                 nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_IP);
459                 if (!nest)
460                         return -1;
461
462                 if (nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_SRC,
463                                     info->key.u.ipv4.src) < 0 ||
464                     nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_DST,
465                                     info->key.u.ipv4.dst) < 0) {
466                         nla_nest_cancel(skb, nest);
467                         return -1;
468                 }
469
470                 nla_nest_end(skb, nest);
471         }
472
473         return 0;
474 }
475
476 static int nft_tunnel_opts_dump(struct sk_buff *skb,
477                                 struct nft_tunnel_obj *priv)
478 {
479         struct nft_tunnel_opts *opts = &priv->opts;
480         struct nlattr *nest, *inner;
481
482         nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS);
483         if (!nest)
484                 return -1;
485
486         if (opts->flags & TUNNEL_VXLAN_OPT) {
487                 inner = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS_VXLAN);
488                 if (!inner)
489                         goto failure;
490                 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_VXLAN_GBP,
491                                  htonl(opts->u.vxlan.gbp)))
492                         goto inner_failure;
493                 nla_nest_end(skb, inner);
494         } else if (opts->flags & TUNNEL_ERSPAN_OPT) {
495                 inner = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS_ERSPAN);
496                 if (!inner)
497                         goto failure;
498                 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ERSPAN_VERSION,
499                                  htonl(opts->u.erspan.version)))
500                         goto inner_failure;
501                 switch (opts->u.erspan.version) {
502                 case ERSPAN_VERSION:
503                         if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX,
504                                          opts->u.erspan.u.index))
505                                 goto inner_failure;
506                         break;
507                 case ERSPAN_VERSION2:
508                         if (nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID,
509                                        get_hwid(&opts->u.erspan.u.md2)) ||
510                             nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR,
511                                        opts->u.erspan.u.md2.dir))
512                                 goto inner_failure;
513                         break;
514                 }
515                 nla_nest_end(skb, inner);
516         }
517         nla_nest_end(skb, nest);
518         return 0;
519
520 inner_failure:
521         nla_nest_cancel(skb, inner);
522 failure:
523         nla_nest_cancel(skb, nest);
524         return -1;
525 }
526
527 static int nft_tunnel_ports_dump(struct sk_buff *skb,
528                                  struct ip_tunnel_info *info)
529 {
530         if (nla_put_be16(skb, NFTA_TUNNEL_KEY_SPORT, info->key.tp_src) < 0 ||
531             nla_put_be16(skb, NFTA_TUNNEL_KEY_DPORT, info->key.tp_dst) < 0)
532                 return -1;
533
534         return 0;
535 }
536
537 static int nft_tunnel_flags_dump(struct sk_buff *skb,
538                                  struct ip_tunnel_info *info)
539 {
540         u32 flags = 0;
541
542         if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
543                 flags |= NFT_TUNNEL_F_DONT_FRAGMENT;
544         if (!(info->key.tun_flags & TUNNEL_CSUM))
545                 flags |= NFT_TUNNEL_F_ZERO_CSUM_TX;
546         if (info->key.tun_flags & TUNNEL_SEQ)
547                 flags |= NFT_TUNNEL_F_SEQ_NUMBER;
548
549         if (nla_put_be32(skb, NFTA_TUNNEL_KEY_FLAGS, htonl(flags)) < 0)
550                 return -1;
551
552         return 0;
553 }
554
555 static int nft_tunnel_obj_dump(struct sk_buff *skb,
556                                struct nft_object *obj, bool reset)
557 {
558         struct nft_tunnel_obj *priv = nft_obj_data(obj);
559         struct ip_tunnel_info *info = &priv->md->u.tun_info;
560
561         if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ID,
562                          tunnel_id_to_key32(info->key.tun_id)) ||
563             nft_tunnel_ip_dump(skb, info) < 0 ||
564             nft_tunnel_ports_dump(skb, info) < 0 ||
565             nft_tunnel_flags_dump(skb, info) < 0 ||
566             nla_put_u8(skb, NFTA_TUNNEL_KEY_TOS, info->key.tos) ||
567             nla_put_u8(skb, NFTA_TUNNEL_KEY_TTL, info->key.ttl) ||
568             nft_tunnel_opts_dump(skb, priv) < 0)
569                 goto nla_put_failure;
570
571         return 0;
572
573 nla_put_failure:
574         return -1;
575 }
576
577 static void nft_tunnel_obj_destroy(const struct nft_ctx *ctx,
578                                    struct nft_object *obj)
579 {
580         struct nft_tunnel_obj *priv = nft_obj_data(obj);
581
582         metadata_dst_free(priv->md);
583 }
584
585 static struct nft_object_type nft_tunnel_obj_type;
586 static const struct nft_object_ops nft_tunnel_obj_ops = {
587         .type           = &nft_tunnel_obj_type,
588         .size           = sizeof(struct nft_tunnel_obj),
589         .eval           = nft_tunnel_obj_eval,
590         .init           = nft_tunnel_obj_init,
591         .destroy        = nft_tunnel_obj_destroy,
592         .dump           = nft_tunnel_obj_dump,
593 };
594
595 static struct nft_object_type nft_tunnel_obj_type __read_mostly = {
596         .type           = NFT_OBJECT_TUNNEL,
597         .ops            = &nft_tunnel_obj_ops,
598         .maxattr        = NFTA_TUNNEL_KEY_MAX,
599         .policy         = nft_tunnel_key_policy,
600         .owner          = THIS_MODULE,
601 };
602
603 static int __init nft_tunnel_module_init(void)
604 {
605         int err;
606
607         err = nft_register_expr(&nft_tunnel_type);
608         if (err < 0)
609                 return err;
610
611         err = nft_register_obj(&nft_tunnel_obj_type);
612         if (err < 0)
613                 nft_unregister_expr(&nft_tunnel_type);
614
615         return err;
616 }
617
618 static void __exit nft_tunnel_module_exit(void)
619 {
620         nft_unregister_obj(&nft_tunnel_obj_type);
621         nft_unregister_expr(&nft_tunnel_type);
622 }
623
624 module_init(nft_tunnel_module_init);
625 module_exit(nft_tunnel_module_exit);
626
627 MODULE_LICENSE("GPL");
628 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
629 MODULE_ALIAS_NFT_EXPR("tunnel");
630 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_TUNNEL);