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;
20 static void nft_tunnel_get_eval(const struct nft_expr
*expr
,
21 struct nft_regs
*regs
,
22 const struct nft_pktinfo
*pkt
)
24 const struct nft_tunnel
*priv
= nft_expr_priv(expr
);
25 u32
*dest
= ®s
->data
[priv
->dreg
];
26 struct ip_tunnel_info
*tun_info
;
28 tun_info
= skb_tunnel_info(pkt
->skb
);
32 nft_reg_store8(dest
, !!tun_info
);
36 regs
->verdict
.code
= NFT_BREAK
;
39 *dest
= ntohl(tunnel_id_to_key32(tun_info
->key
.tun_id
));
43 regs
->verdict
.code
= NFT_BREAK
;
47 static const struct nla_policy nft_tunnel_policy
[NFTA_TUNNEL_MAX
+ 1] = {
48 [NFTA_TUNNEL_KEY
] = { .type
= NLA_U32
},
49 [NFTA_TUNNEL_DREG
] = { .type
= NLA_U32
},
52 static int nft_tunnel_get_init(const struct nft_ctx
*ctx
,
53 const struct nft_expr
*expr
,
54 const struct nlattr
* const tb
[])
56 struct nft_tunnel
*priv
= nft_expr_priv(expr
);
59 if (!tb
[NFTA_TUNNEL_KEY
] ||
60 !tb
[NFTA_TUNNEL_DREG
])
63 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_TUNNEL_KEY
]));
75 priv
->dreg
= nft_parse_register(tb
[NFTA_TUNNEL_DREG
]);
77 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
81 static int nft_tunnel_get_dump(struct sk_buff
*skb
,
82 const struct nft_expr
*expr
)
84 const struct nft_tunnel
*priv
= nft_expr_priv(expr
);
86 if (nla_put_be32(skb
, NFTA_TUNNEL_KEY
, htonl(priv
->key
)))
88 if (nft_dump_register(skb
, NFTA_TUNNEL_DREG
, priv
->dreg
))
96 static struct nft_expr_type nft_tunnel_type
;
97 static const struct nft_expr_ops nft_tunnel_get_ops
= {
98 .type
= &nft_tunnel_type
,
99 .size
= NFT_EXPR_SIZE(sizeof(struct nft_tunnel
)),
100 .eval
= nft_tunnel_get_eval
,
101 .init
= nft_tunnel_get_init
,
102 .dump
= nft_tunnel_get_dump
,
105 static struct nft_expr_type nft_tunnel_type __read_mostly
= {
107 .ops
= &nft_tunnel_get_ops
,
108 .policy
= nft_tunnel_policy
,
109 .maxattr
= NFTA_TUNNEL_MAX
,
110 .owner
= THIS_MODULE
,
113 struct nft_tunnel_opts
{
115 struct vxlan_metadata vxlan
;
116 struct erspan_metadata erspan
;
122 struct nft_tunnel_obj
{
123 struct metadata_dst
*md
;
124 struct nft_tunnel_opts opts
;
127 static const struct nla_policy nft_tunnel_ip_policy
[NFTA_TUNNEL_KEY_IP_MAX
+ 1] = {
128 [NFTA_TUNNEL_KEY_IP_SRC
] = { .type
= NLA_U32
},
129 [NFTA_TUNNEL_KEY_IP_DST
] = { .type
= NLA_U32
},
132 static int nft_tunnel_obj_ip_init(const struct nft_ctx
*ctx
,
133 const struct nlattr
*attr
,
134 struct ip_tunnel_info
*info
)
136 struct nlattr
*tb
[NFTA_TUNNEL_KEY_IP_MAX
+ 1];
139 err
= nla_parse_nested(tb
, NFTA_TUNNEL_KEY_IP_MAX
, attr
,
140 nft_tunnel_ip_policy
, NULL
);
144 if (!tb
[NFTA_TUNNEL_KEY_IP_DST
])
147 if (tb
[NFTA_TUNNEL_KEY_IP_SRC
])
148 info
->key
.u
.ipv4
.src
= nla_get_be32(tb
[NFTA_TUNNEL_KEY_IP_SRC
]);
149 if (tb
[NFTA_TUNNEL_KEY_IP_DST
])
150 info
->key
.u
.ipv4
.dst
= nla_get_be32(tb
[NFTA_TUNNEL_KEY_IP_DST
]);
155 static const struct nla_policy nft_tunnel_ip6_policy
[NFTA_TUNNEL_KEY_IP6_MAX
+ 1] = {
156 [NFTA_TUNNEL_KEY_IP6_SRC
] = { .len
= sizeof(struct in6_addr
), },
157 [NFTA_TUNNEL_KEY_IP6_DST
] = { .len
= sizeof(struct in6_addr
), },
158 [NFTA_TUNNEL_KEY_IP6_FLOWLABEL
] = { .type
= NLA_U32
, }
161 static int nft_tunnel_obj_ip6_init(const struct nft_ctx
*ctx
,
162 const struct nlattr
*attr
,
163 struct ip_tunnel_info
*info
)
165 struct nlattr
*tb
[NFTA_TUNNEL_KEY_IP6_MAX
+ 1];
168 err
= nla_parse_nested(tb
, NFTA_TUNNEL_KEY_IP6_MAX
, attr
,
169 nft_tunnel_ip6_policy
, NULL
);
173 if (!tb
[NFTA_TUNNEL_KEY_IP6_DST
])
176 if (tb
[NFTA_TUNNEL_KEY_IP6_SRC
]) {
177 memcpy(&info
->key
.u
.ipv6
.src
,
178 nla_data(tb
[NFTA_TUNNEL_KEY_IP6_SRC
]),
179 sizeof(struct in6_addr
));
181 if (tb
[NFTA_TUNNEL_KEY_IP6_DST
]) {
182 memcpy(&info
->key
.u
.ipv6
.dst
,
183 nla_data(tb
[NFTA_TUNNEL_KEY_IP6_DST
]),
184 sizeof(struct in6_addr
));
186 if (tb
[NFTA_TUNNEL_KEY_IP6_FLOWLABEL
])
187 info
->key
.label
= nla_get_be32(tb
[NFTA_TUNNEL_KEY_IP6_FLOWLABEL
]);
189 info
->mode
|= IP_TUNNEL_INFO_IPV6
;
194 static const struct nla_policy nft_tunnel_opts_vxlan_policy
[NFTA_TUNNEL_KEY_VXLAN_MAX
+ 1] = {
195 [NFTA_TUNNEL_KEY_VXLAN_GBP
] = { .type
= NLA_U32
},
198 static int nft_tunnel_obj_vxlan_init(const struct nlattr
*attr
,
199 struct nft_tunnel_opts
*opts
)
201 struct nlattr
*tb
[NFTA_TUNNEL_KEY_VXLAN_MAX
+ 1];
204 err
= nla_parse_nested(tb
, NFTA_TUNNEL_KEY_VXLAN_MAX
, attr
,
205 nft_tunnel_opts_vxlan_policy
, NULL
);
209 if (!tb
[NFTA_TUNNEL_KEY_VXLAN_GBP
])
212 opts
->u
.vxlan
.gbp
= ntohl(nla_get_be32(tb
[NFTA_TUNNEL_KEY_VXLAN_GBP
]));
214 opts
->len
= sizeof(struct vxlan_metadata
);
215 opts
->flags
= TUNNEL_VXLAN_OPT
;
220 static const struct nla_policy nft_tunnel_opts_erspan_policy
[NFTA_TUNNEL_KEY_ERSPAN_MAX
+ 1] = {
221 [NFTA_TUNNEL_KEY_ERSPAN_VERSION
] = { .type
= NLA_U32
},
222 [NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX
] = { .type
= NLA_U32
},
223 [NFTA_TUNNEL_KEY_ERSPAN_V2_DIR
] = { .type
= NLA_U8
},
224 [NFTA_TUNNEL_KEY_ERSPAN_V2_HWID
] = { .type
= NLA_U8
},
227 static int nft_tunnel_obj_erspan_init(const struct nlattr
*attr
,
228 struct nft_tunnel_opts
*opts
)
230 struct nlattr
*tb
[NFTA_TUNNEL_KEY_ERSPAN_MAX
+ 1];
234 err
= nla_parse_nested(tb
, NFTA_TUNNEL_KEY_ERSPAN_MAX
, attr
,
235 nft_tunnel_opts_erspan_policy
, NULL
);
239 if (!tb
[NFTA_TUNNEL_KEY_ERSPAN_VERSION
])
242 version
= ntohl(nla_get_be32(tb
[NFTA_TUNNEL_KEY_ERSPAN_VERSION
]));
245 if (!tb
[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX
])
248 opts
->u
.erspan
.u
.index
=
249 nla_get_be32(tb
[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX
]);
251 case ERSPAN_VERSION2
:
252 if (!tb
[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR
] ||
253 !tb
[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID
])
256 hwid
= nla_get_u8(tb
[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID
]);
257 dir
= nla_get_u8(tb
[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR
]);
259 set_hwid(&opts
->u
.erspan
.u
.md2
, hwid
);
260 opts
->u
.erspan
.u
.md2
.dir
= dir
;
265 opts
->u
.erspan
.version
= version
;
267 opts
->len
= sizeof(struct erspan_metadata
);
268 opts
->flags
= TUNNEL_ERSPAN_OPT
;
273 static const struct nla_policy nft_tunnel_opts_policy
[NFTA_TUNNEL_KEY_OPTS_MAX
+ 1] = {
274 [NFTA_TUNNEL_KEY_OPTS_VXLAN
] = { .type
= NLA_NESTED
, },
275 [NFTA_TUNNEL_KEY_OPTS_ERSPAN
] = { .type
= NLA_NESTED
, },
278 static int nft_tunnel_obj_opts_init(const struct nft_ctx
*ctx
,
279 const struct nlattr
*attr
,
280 struct ip_tunnel_info
*info
,
281 struct nft_tunnel_opts
*opts
)
283 struct nlattr
*tb
[NFTA_TUNNEL_KEY_OPTS_MAX
+ 1];
286 err
= nla_parse_nested(tb
, NFTA_TUNNEL_KEY_OPTS_MAX
, attr
,
287 nft_tunnel_opts_policy
, NULL
);
291 if (tb
[NFTA_TUNNEL_KEY_OPTS_VXLAN
]) {
292 err
= nft_tunnel_obj_vxlan_init(tb
[NFTA_TUNNEL_KEY_OPTS_VXLAN
],
294 } else if (tb
[NFTA_TUNNEL_KEY_OPTS_ERSPAN
]) {
295 err
= nft_tunnel_obj_erspan_init(tb
[NFTA_TUNNEL_KEY_OPTS_ERSPAN
],
304 static const struct nla_policy nft_tunnel_key_policy
[NFTA_TUNNEL_KEY_MAX
+ 1] = {
305 [NFTA_TUNNEL_KEY_IP
] = { .type
= NLA_NESTED
, },
306 [NFTA_TUNNEL_KEY_IP6
] = { .type
= NLA_NESTED
, },
307 [NFTA_TUNNEL_KEY_ID
] = { .type
= NLA_U32
, },
308 [NFTA_TUNNEL_KEY_FLAGS
] = { .type
= NLA_U32
, },
309 [NFTA_TUNNEL_KEY_TOS
] = { .type
= NLA_U8
, },
310 [NFTA_TUNNEL_KEY_TTL
] = { .type
= NLA_U8
, },
311 [NFTA_TUNNEL_KEY_SPORT
] = { .type
= NLA_U16
, },
312 [NFTA_TUNNEL_KEY_DPORT
] = { .type
= NLA_U16
, },
313 [NFTA_TUNNEL_KEY_OPTS
] = { .type
= NLA_NESTED
, },
316 static int nft_tunnel_obj_init(const struct nft_ctx
*ctx
,
317 const struct nlattr
* const tb
[],
318 struct nft_object
*obj
)
320 struct nft_tunnel_obj
*priv
= nft_obj_data(obj
);
321 struct ip_tunnel_info info
;
322 struct metadata_dst
*md
;
325 if (!tb
[NFTA_TUNNEL_KEY_ID
])
328 memset(&info
, 0, sizeof(info
));
329 info
.mode
= IP_TUNNEL_INFO_TX
;
330 info
.key
.tun_id
= key32_to_tunnel_id(nla_get_be32(tb
[NFTA_TUNNEL_KEY_ID
]));
331 info
.key
.tun_flags
= TUNNEL_KEY
| TUNNEL_CSUM
| TUNNEL_NOCACHE
;
333 if (tb
[NFTA_TUNNEL_KEY_IP
]) {
334 err
= nft_tunnel_obj_ip_init(ctx
, tb
[NFTA_TUNNEL_KEY_IP
], &info
);
337 } else if (tb
[NFTA_TUNNEL_KEY_IP6
]) {
338 err
= nft_tunnel_obj_ip6_init(ctx
, tb
[NFTA_TUNNEL_KEY_IP6
], &info
);
345 if (tb
[NFTA_TUNNEL_KEY_SPORT
]) {
346 info
.key
.tp_src
= nla_get_be16(tb
[NFTA_TUNNEL_KEY_SPORT
]);
348 if (tb
[NFTA_TUNNEL_KEY_DPORT
]) {
349 info
.key
.tp_dst
= nla_get_be16(tb
[NFTA_TUNNEL_KEY_DPORT
]);
352 if (tb
[NFTA_TUNNEL_KEY_FLAGS
]) {
355 tun_flags
= ntohl(nla_get_be32(tb
[NFTA_TUNNEL_KEY_FLAGS
]));
356 if (tun_flags
& ~NFT_TUNNEL_F_MASK
)
359 if (tun_flags
& NFT_TUNNEL_F_ZERO_CSUM_TX
)
360 info
.key
.tun_flags
&= ~TUNNEL_CSUM
;
361 if (tun_flags
& NFT_TUNNEL_F_DONT_FRAGMENT
)
362 info
.key
.tun_flags
|= TUNNEL_DONT_FRAGMENT
;
363 if (tun_flags
& NFT_TUNNEL_F_SEQ_NUMBER
)
364 info
.key
.tun_flags
|= TUNNEL_SEQ
;
366 if (tb
[NFTA_TUNNEL_KEY_TOS
])
367 info
.key
.tos
= nla_get_u8(tb
[NFTA_TUNNEL_KEY_TOS
]);
368 if (tb
[NFTA_TUNNEL_KEY_TTL
])
369 info
.key
.ttl
= nla_get_u8(tb
[NFTA_TUNNEL_KEY_TTL
]);
371 info
.key
.ttl
= U8_MAX
;
373 if (tb
[NFTA_TUNNEL_KEY_OPTS
]) {
374 err
= nft_tunnel_obj_opts_init(ctx
, tb
[NFTA_TUNNEL_KEY_OPTS
],
380 md
= metadata_dst_alloc(priv
->opts
.len
, METADATA_IP_TUNNEL
, GFP_KERNEL
);
384 memcpy(&md
->u
.tun_info
, &info
, sizeof(info
));
385 ip_tunnel_info_opts_set(&md
->u
.tun_info
, &priv
->opts
.u
, priv
->opts
.len
,
392 static inline void nft_tunnel_obj_eval(struct nft_object
*obj
,
393 struct nft_regs
*regs
,
394 const struct nft_pktinfo
*pkt
)
396 struct nft_tunnel_obj
*priv
= nft_obj_data(obj
);
397 struct sk_buff
*skb
= pkt
->skb
;
400 dst_hold((struct dst_entry
*) priv
->md
);
401 skb_dst_set(skb
, (struct dst_entry
*) priv
->md
);
404 static int nft_tunnel_ip_dump(struct sk_buff
*skb
, struct ip_tunnel_info
*info
)
408 if (info
->mode
& IP_TUNNEL_INFO_IPV6
) {
409 nest
= nla_nest_start(skb
, NFTA_TUNNEL_KEY_IP6
);
413 if (nla_put_in6_addr(skb
, NFTA_TUNNEL_KEY_IP6_SRC
, &info
->key
.u
.ipv6
.src
) < 0 ||
414 nla_put_in6_addr(skb
, NFTA_TUNNEL_KEY_IP6_DST
, &info
->key
.u
.ipv6
.dst
) < 0 ||
415 nla_put_be32(skb
, NFTA_TUNNEL_KEY_IP6_FLOWLABEL
, info
->key
.label
))
418 nla_nest_end(skb
, nest
);
420 nest
= nla_nest_start(skb
, NFTA_TUNNEL_KEY_IP
);
424 if (nla_put_in_addr(skb
, NFTA_TUNNEL_KEY_IP_SRC
, info
->key
.u
.ipv4
.src
) < 0 ||
425 nla_put_in_addr(skb
, NFTA_TUNNEL_KEY_IP_DST
, info
->key
.u
.ipv4
.dst
) < 0)
428 nla_nest_end(skb
, nest
);
434 static int nft_tunnel_opts_dump(struct sk_buff
*skb
,
435 struct nft_tunnel_obj
*priv
)
437 struct nft_tunnel_opts
*opts
= &priv
->opts
;
440 nest
= nla_nest_start(skb
, NFTA_TUNNEL_KEY_OPTS
);
444 if (opts
->flags
& TUNNEL_VXLAN_OPT
) {
445 if (nla_put_be32(skb
, NFTA_TUNNEL_KEY_VXLAN_GBP
,
446 htonl(opts
->u
.vxlan
.gbp
)))
448 } else if (opts
->flags
& TUNNEL_ERSPAN_OPT
) {
449 switch (opts
->u
.erspan
.version
) {
451 if (nla_put_be32(skb
, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX
,
452 opts
->u
.erspan
.u
.index
))
455 case ERSPAN_VERSION2
:
456 if (nla_put_u8(skb
, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID
,
457 get_hwid(&opts
->u
.erspan
.u
.md2
)) ||
458 nla_put_u8(skb
, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR
,
459 opts
->u
.erspan
.u
.md2
.dir
))
464 nla_nest_end(skb
, nest
);
469 static int nft_tunnel_ports_dump(struct sk_buff
*skb
,
470 struct ip_tunnel_info
*info
)
472 if (nla_put_be16(skb
, NFTA_TUNNEL_KEY_SPORT
, info
->key
.tp_src
) < 0 ||
473 nla_put_be16(skb
, NFTA_TUNNEL_KEY_DPORT
, info
->key
.tp_dst
) < 0)
479 static int nft_tunnel_flags_dump(struct sk_buff
*skb
,
480 struct ip_tunnel_info
*info
)
484 if (info
->key
.tun_flags
& TUNNEL_DONT_FRAGMENT
)
485 flags
|= NFT_TUNNEL_F_DONT_FRAGMENT
;
486 if (!(info
->key
.tun_flags
& TUNNEL_CSUM
))
487 flags
|= NFT_TUNNEL_F_ZERO_CSUM_TX
;
488 if (info
->key
.tun_flags
& TUNNEL_SEQ
)
489 flags
|= NFT_TUNNEL_F_SEQ_NUMBER
;
491 if (nla_put_be32(skb
, NFTA_TUNNEL_KEY_FLAGS
, htonl(flags
)) < 0)
497 static int nft_tunnel_obj_dump(struct sk_buff
*skb
,
498 struct nft_object
*obj
, bool reset
)
500 struct nft_tunnel_obj
*priv
= nft_obj_data(obj
);
501 struct ip_tunnel_info
*info
= &priv
->md
->u
.tun_info
;
503 if (nla_put_be32(skb
, NFTA_TUNNEL_KEY_ID
,
504 tunnel_id_to_key32(info
->key
.tun_id
)) ||
505 nft_tunnel_ip_dump(skb
, info
) < 0 ||
506 nft_tunnel_ports_dump(skb
, info
) < 0 ||
507 nft_tunnel_flags_dump(skb
, info
) < 0 ||
508 nla_put_u8(skb
, NFTA_TUNNEL_KEY_TOS
, info
->key
.tos
) ||
509 nla_put_u8(skb
, NFTA_TUNNEL_KEY_TTL
, info
->key
.ttl
) ||
510 nft_tunnel_opts_dump(skb
, priv
) < 0)
511 goto nla_put_failure
;
519 static void nft_tunnel_obj_destroy(const struct nft_ctx
*ctx
,
520 struct nft_object
*obj
)
522 struct nft_tunnel_obj
*priv
= nft_obj_data(obj
);
524 metadata_dst_free(priv
->md
);
527 static struct nft_object_type nft_tunnel_obj_type
;
528 static const struct nft_object_ops nft_tunnel_obj_ops
= {
529 .type
= &nft_tunnel_obj_type
,
530 .size
= sizeof(struct nft_tunnel_obj
),
531 .eval
= nft_tunnel_obj_eval
,
532 .init
= nft_tunnel_obj_init
,
533 .destroy
= nft_tunnel_obj_destroy
,
534 .dump
= nft_tunnel_obj_dump
,
537 static struct nft_object_type nft_tunnel_obj_type __read_mostly
= {
538 .type
= NFT_OBJECT_TUNNEL
,
539 .ops
= &nft_tunnel_obj_ops
,
540 .maxattr
= NFTA_TUNNEL_KEY_MAX
,
541 .policy
= nft_tunnel_key_policy
,
542 .owner
= THIS_MODULE
,
545 static int __init
nft_tunnel_module_init(void)
549 err
= nft_register_expr(&nft_tunnel_type
);
553 err
= nft_register_obj(&nft_tunnel_obj_type
);
555 nft_unregister_expr(&nft_tunnel_type
);
560 static void __exit
nft_tunnel_module_exit(void)
562 nft_unregister_obj(&nft_tunnel_obj_type
);
563 nft_unregister_expr(&nft_tunnel_type
);
566 module_init(nft_tunnel_module_init
);
567 module_exit(nft_tunnel_module_exit
);
569 MODULE_LICENSE("GPL");
570 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
571 MODULE_ALIAS_NFT_EXPR("tunnel");
572 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_TUNNEL
);