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>
16 enum nft_tunnel_keys key:8;
17 enum nft_registers dreg:8;
18 enum nft_tunnel_mode mode:8;
21 static void nft_tunnel_get_eval(const struct nft_expr *expr,
22 struct nft_regs *regs,
23 const struct nft_pktinfo *pkt)
25 const struct nft_tunnel *priv = nft_expr_priv(expr);
26 u32 *dest = ®s->data[priv->dreg];
27 struct ip_tunnel_info *tun_info;
29 tun_info = skb_tunnel_info(pkt->skb);
34 nft_reg_store8(dest, false);
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);
44 nft_reg_store8(dest, false);
48 regs->verdict.code = NFT_BREAK;
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));
58 regs->verdict.code = NFT_BREAK;
62 regs->verdict.code = NFT_BREAK;
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 },
72 static int nft_tunnel_get_init(const struct nft_ctx *ctx,
73 const struct nft_expr *expr,
74 const struct nlattr * const tb[])
76 struct nft_tunnel *priv = nft_expr_priv(expr);
79 if (!tb[NFTA_TUNNEL_KEY] ||
80 !tb[NFTA_TUNNEL_DREG])
83 priv->key = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY]));
95 priv->dreg = nft_parse_register(tb[NFTA_TUNNEL_DREG]);
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)
102 priv->mode = NFT_TUNNEL_MODE_NONE;
105 return nft_validate_register_store(ctx, priv->dreg, NULL,
106 NFT_DATA_VALUE, len);
109 static int nft_tunnel_get_dump(struct sk_buff *skb,
110 const struct nft_expr *expr)
112 const struct nft_tunnel *priv = nft_expr_priv(expr);
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;
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,
135 static struct nft_expr_type nft_tunnel_type __read_mostly = {
137 .ops = &nft_tunnel_get_ops,
138 .policy = nft_tunnel_policy,
139 .maxattr = NFTA_TUNNEL_MAX,
140 .owner = THIS_MODULE,
143 struct nft_tunnel_opts {
145 struct vxlan_metadata vxlan;
146 struct erspan_metadata erspan;
152 struct nft_tunnel_obj {
153 struct metadata_dst *md;
154 struct nft_tunnel_opts opts;
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 },
162 static int nft_tunnel_obj_ip_init(const struct nft_ctx *ctx,
163 const struct nlattr *attr,
164 struct ip_tunnel_info *info)
166 struct nlattr *tb[NFTA_TUNNEL_KEY_IP_MAX + 1];
169 err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_IP_MAX, attr,
170 nft_tunnel_ip_policy, NULL);
174 if (!tb[NFTA_TUNNEL_KEY_IP_DST])
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]);
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, }
191 static int nft_tunnel_obj_ip6_init(const struct nft_ctx *ctx,
192 const struct nlattr *attr,
193 struct ip_tunnel_info *info)
195 struct nlattr *tb[NFTA_TUNNEL_KEY_IP6_MAX + 1];
198 err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_IP6_MAX, attr,
199 nft_tunnel_ip6_policy, NULL);
203 if (!tb[NFTA_TUNNEL_KEY_IP6_DST])
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));
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));
216 if (tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL])
217 info->key.label = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]);
219 info->mode |= IP_TUNNEL_INFO_IPV6;
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 },
228 static int nft_tunnel_obj_vxlan_init(const struct nlattr *attr,
229 struct nft_tunnel_opts *opts)
231 struct nlattr *tb[NFTA_TUNNEL_KEY_VXLAN_MAX + 1];
234 err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_VXLAN_MAX, attr,
235 nft_tunnel_opts_vxlan_policy, NULL);
239 if (!tb[NFTA_TUNNEL_KEY_VXLAN_GBP])
242 opts->u.vxlan.gbp = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_VXLAN_GBP]));
244 opts->len = sizeof(struct vxlan_metadata);
245 opts->flags = TUNNEL_VXLAN_OPT;
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 },
257 static int nft_tunnel_obj_erspan_init(const struct nlattr *attr,
258 struct nft_tunnel_opts *opts)
260 struct nlattr *tb[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1];
264 err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_ERSPAN_MAX,
265 attr, nft_tunnel_opts_erspan_policy,
270 if (!tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION])
273 version = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]));
276 if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX])
279 opts->u.erspan.u.index =
280 nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]);
282 case ERSPAN_VERSION2:
283 if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] ||
284 !tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID])
287 hwid = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]);
288 dir = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]);
290 set_hwid(&opts->u.erspan.u.md2, hwid);
291 opts->u.erspan.u.md2.dir = dir;
296 opts->u.erspan.version = version;
298 opts->len = sizeof(struct erspan_metadata);
299 opts->flags = TUNNEL_ERSPAN_OPT;
304 static const struct nla_policy nft_tunnel_opts_policy[NFTA_TUNNEL_KEY_OPTS_MAX + 1] = {
305 [NFTA_TUNNEL_KEY_OPTS_VXLAN] = { .type = NLA_NESTED, },
306 [NFTA_TUNNEL_KEY_OPTS_ERSPAN] = { .type = NLA_NESTED, },
309 static int nft_tunnel_obj_opts_init(const struct nft_ctx *ctx,
310 const struct nlattr *attr,
311 struct ip_tunnel_info *info,
312 struct nft_tunnel_opts *opts)
314 struct nlattr *tb[NFTA_TUNNEL_KEY_OPTS_MAX + 1];
317 err = nla_parse_nested_deprecated(tb, NFTA_TUNNEL_KEY_OPTS_MAX, attr,
318 nft_tunnel_opts_policy, NULL);
322 if (tb[NFTA_TUNNEL_KEY_OPTS_VXLAN]) {
323 err = nft_tunnel_obj_vxlan_init(tb[NFTA_TUNNEL_KEY_OPTS_VXLAN],
325 } else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
326 err = nft_tunnel_obj_erspan_init(tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
335 static const struct nla_policy nft_tunnel_key_policy[NFTA_TUNNEL_KEY_MAX + 1] = {
336 [NFTA_TUNNEL_KEY_IP] = { .type = NLA_NESTED, },
337 [NFTA_TUNNEL_KEY_IP6] = { .type = NLA_NESTED, },
338 [NFTA_TUNNEL_KEY_ID] = { .type = NLA_U32, },
339 [NFTA_TUNNEL_KEY_FLAGS] = { .type = NLA_U32, },
340 [NFTA_TUNNEL_KEY_TOS] = { .type = NLA_U8, },
341 [NFTA_TUNNEL_KEY_TTL] = { .type = NLA_U8, },
342 [NFTA_TUNNEL_KEY_SPORT] = { .type = NLA_U16, },
343 [NFTA_TUNNEL_KEY_DPORT] = { .type = NLA_U16, },
344 [NFTA_TUNNEL_KEY_OPTS] = { .type = NLA_NESTED, },
347 static int nft_tunnel_obj_init(const struct nft_ctx *ctx,
348 const struct nlattr * const tb[],
349 struct nft_object *obj)
351 struct nft_tunnel_obj *priv = nft_obj_data(obj);
352 struct ip_tunnel_info info;
353 struct metadata_dst *md;
356 if (!tb[NFTA_TUNNEL_KEY_ID])
359 memset(&info, 0, sizeof(info));
360 info.mode = IP_TUNNEL_INFO_TX;
361 info.key.tun_id = key32_to_tunnel_id(nla_get_be32(tb[NFTA_TUNNEL_KEY_ID]));
362 info.key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
364 if (tb[NFTA_TUNNEL_KEY_IP]) {
365 err = nft_tunnel_obj_ip_init(ctx, tb[NFTA_TUNNEL_KEY_IP], &info);
368 } else if (tb[NFTA_TUNNEL_KEY_IP6]) {
369 err = nft_tunnel_obj_ip6_init(ctx, tb[NFTA_TUNNEL_KEY_IP6], &info);
376 if (tb[NFTA_TUNNEL_KEY_SPORT]) {
377 info.key.tp_src = nla_get_be16(tb[NFTA_TUNNEL_KEY_SPORT]);
379 if (tb[NFTA_TUNNEL_KEY_DPORT]) {
380 info.key.tp_dst = nla_get_be16(tb[NFTA_TUNNEL_KEY_DPORT]);
383 if (tb[NFTA_TUNNEL_KEY_FLAGS]) {
386 tun_flags = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_FLAGS]));
387 if (tun_flags & ~NFT_TUNNEL_F_MASK)
390 if (tun_flags & NFT_TUNNEL_F_ZERO_CSUM_TX)
391 info.key.tun_flags &= ~TUNNEL_CSUM;
392 if (tun_flags & NFT_TUNNEL_F_DONT_FRAGMENT)
393 info.key.tun_flags |= TUNNEL_DONT_FRAGMENT;
394 if (tun_flags & NFT_TUNNEL_F_SEQ_NUMBER)
395 info.key.tun_flags |= TUNNEL_SEQ;
397 if (tb[NFTA_TUNNEL_KEY_TOS])
398 info.key.tos = nla_get_u8(tb[NFTA_TUNNEL_KEY_TOS]);
399 if (tb[NFTA_TUNNEL_KEY_TTL])
400 info.key.ttl = nla_get_u8(tb[NFTA_TUNNEL_KEY_TTL]);
402 info.key.ttl = U8_MAX;
404 if (tb[NFTA_TUNNEL_KEY_OPTS]) {
405 err = nft_tunnel_obj_opts_init(ctx, tb[NFTA_TUNNEL_KEY_OPTS],
411 md = metadata_dst_alloc(priv->opts.len, METADATA_IP_TUNNEL, GFP_KERNEL);
415 memcpy(&md->u.tun_info, &info, sizeof(info));
416 #ifdef CONFIG_DST_CACHE
417 err = dst_cache_init(&md->u.tun_info.dst_cache, GFP_KERNEL);
419 metadata_dst_free(md);
423 ip_tunnel_info_opts_set(&md->u.tun_info, &priv->opts.u, priv->opts.len,
430 static inline void nft_tunnel_obj_eval(struct nft_object *obj,
431 struct nft_regs *regs,
432 const struct nft_pktinfo *pkt)
434 struct nft_tunnel_obj *priv = nft_obj_data(obj);
435 struct sk_buff *skb = pkt->skb;
438 dst_hold((struct dst_entry *) priv->md);
439 skb_dst_set(skb, (struct dst_entry *) priv->md);
442 static int nft_tunnel_ip_dump(struct sk_buff *skb, struct ip_tunnel_info *info)
446 if (info->mode & IP_TUNNEL_INFO_IPV6) {
447 nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_IP6);
451 if (nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_SRC,
452 &info->key.u.ipv6.src) < 0 ||
453 nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_DST,
454 &info->key.u.ipv6.dst) < 0 ||
455 nla_put_be32(skb, NFTA_TUNNEL_KEY_IP6_FLOWLABEL,
457 nla_nest_cancel(skb, nest);
461 nla_nest_end(skb, nest);
463 nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_IP);
467 if (nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_SRC,
468 info->key.u.ipv4.src) < 0 ||
469 nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_DST,
470 info->key.u.ipv4.dst) < 0) {
471 nla_nest_cancel(skb, nest);
475 nla_nest_end(skb, nest);
481 static int nft_tunnel_opts_dump(struct sk_buff *skb,
482 struct nft_tunnel_obj *priv)
484 struct nft_tunnel_opts *opts = &priv->opts;
485 struct nlattr *nest, *inner;
487 nest = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS);
491 if (opts->flags & TUNNEL_VXLAN_OPT) {
492 inner = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS_VXLAN);
495 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_VXLAN_GBP,
496 htonl(opts->u.vxlan.gbp)))
498 nla_nest_end(skb, inner);
499 } else if (opts->flags & TUNNEL_ERSPAN_OPT) {
500 inner = nla_nest_start_noflag(skb, NFTA_TUNNEL_KEY_OPTS_ERSPAN);
503 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ERSPAN_VERSION,
504 htonl(opts->u.erspan.version)))
506 switch (opts->u.erspan.version) {
508 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX,
509 opts->u.erspan.u.index))
512 case ERSPAN_VERSION2:
513 if (nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID,
514 get_hwid(&opts->u.erspan.u.md2)) ||
515 nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR,
516 opts->u.erspan.u.md2.dir))
520 nla_nest_end(skb, inner);
522 nla_nest_end(skb, nest);
526 nla_nest_cancel(skb, inner);
528 nla_nest_cancel(skb, nest);
532 static int nft_tunnel_ports_dump(struct sk_buff *skb,
533 struct ip_tunnel_info *info)
535 if (nla_put_be16(skb, NFTA_TUNNEL_KEY_SPORT, info->key.tp_src) < 0 ||
536 nla_put_be16(skb, NFTA_TUNNEL_KEY_DPORT, info->key.tp_dst) < 0)
542 static int nft_tunnel_flags_dump(struct sk_buff *skb,
543 struct ip_tunnel_info *info)
547 if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
548 flags |= NFT_TUNNEL_F_DONT_FRAGMENT;
549 if (!(info->key.tun_flags & TUNNEL_CSUM))
550 flags |= NFT_TUNNEL_F_ZERO_CSUM_TX;
551 if (info->key.tun_flags & TUNNEL_SEQ)
552 flags |= NFT_TUNNEL_F_SEQ_NUMBER;
554 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_FLAGS, htonl(flags)) < 0)
560 static int nft_tunnel_obj_dump(struct sk_buff *skb,
561 struct nft_object *obj, bool reset)
563 struct nft_tunnel_obj *priv = nft_obj_data(obj);
564 struct ip_tunnel_info *info = &priv->md->u.tun_info;
566 if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ID,
567 tunnel_id_to_key32(info->key.tun_id)) ||
568 nft_tunnel_ip_dump(skb, info) < 0 ||
569 nft_tunnel_ports_dump(skb, info) < 0 ||
570 nft_tunnel_flags_dump(skb, info) < 0 ||
571 nla_put_u8(skb, NFTA_TUNNEL_KEY_TOS, info->key.tos) ||
572 nla_put_u8(skb, NFTA_TUNNEL_KEY_TTL, info->key.ttl) ||
573 nft_tunnel_opts_dump(skb, priv) < 0)
574 goto nla_put_failure;
582 static void nft_tunnel_obj_destroy(const struct nft_ctx *ctx,
583 struct nft_object *obj)
585 struct nft_tunnel_obj *priv = nft_obj_data(obj);
587 metadata_dst_free(priv->md);
590 static struct nft_object_type nft_tunnel_obj_type;
591 static const struct nft_object_ops nft_tunnel_obj_ops = {
592 .type = &nft_tunnel_obj_type,
593 .size = sizeof(struct nft_tunnel_obj),
594 .eval = nft_tunnel_obj_eval,
595 .init = nft_tunnel_obj_init,
596 .destroy = nft_tunnel_obj_destroy,
597 .dump = nft_tunnel_obj_dump,
600 static struct nft_object_type nft_tunnel_obj_type __read_mostly = {
601 .type = NFT_OBJECT_TUNNEL,
602 .ops = &nft_tunnel_obj_ops,
603 .maxattr = NFTA_TUNNEL_KEY_MAX,
604 .policy = nft_tunnel_key_policy,
605 .owner = THIS_MODULE,
608 static int __init nft_tunnel_module_init(void)
612 err = nft_register_expr(&nft_tunnel_type);
616 err = nft_register_obj(&nft_tunnel_obj_type);
618 nft_unregister_expr(&nft_tunnel_type);
623 static void __exit nft_tunnel_module_exit(void)
625 nft_unregister_obj(&nft_tunnel_obj_type);
626 nft_unregister_expr(&nft_tunnel_type);
629 module_init(nft_tunnel_module_init);
630 module_exit(nft_tunnel_module_exit);
632 MODULE_LICENSE("GPL");
633 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
634 MODULE_ALIAS_NFT_EXPR("tunnel");
635 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_TUNNEL);