This allows to redirect bridged packets to local machine: ether type ip ether daddr set aa:53:08:12:34:56 meta pkttype set unicast Without 'set unicast', ip stack discards PACKET_OTHERHOST skbs. It is also useful to add support for a '-m cluster like' nft rule (where switch floods packets to several nodes, and each cluster node node processes a subset of packets for load distribution). Mangling is restricted to HOST/OTHER/BROAD/MULTICAST, i.e. you cannot set skb->pkt_type to PACKET_KERNEL or change PACKET_LOOPBACK to PACKET_HOST.
Unified · Split
| @@ -26,6 +26,8 @@ · 25 unmodified lines | ||
| 26 | 26 | #include <net/netfilter/nf_tables_core.h> |
| 27 | 27 | #include <net/netfilter/nft_meta.h> |
| 28 | 28 | |
| 29 | +#include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */ | |
| 30 | + | |
| 29 | 31 | void nft_meta_get_eval(const struct nft_expr *expr, |
| 30 | 32 | struct nft_regs *regs, |
| 31 | 33 | const struct nft_pktinfo *pkt) |
| @@ -190,6 +192,13 @@ err: · 158 unmodified lines | ||
| 190 | 192 | } |
| 191 | 193 | EXPORT_SYMBOL_GPL(nft_meta_get_eval); |
| 192 | 194 | |
| 195 | +/* don't change or set _LOOPBACK, _USER, etc. */ | |
| 196 | +static bool pkt_type_ok(u32 p) | |
| 197 | +{ | |
| 198 | + return p == PACKET_HOST || p == PACKET_BROADCAST || | |
| 199 | + p == PACKET_MULTICAST || p == PACKET_OTHERHOST; | |
| 200 | +} | |
| 201 | + | |
| 193 | 202 | void nft_meta_set_eval(const struct nft_expr *expr, |
| 194 | 203 | struct nft_regs *regs, |
| 195 | 204 | const struct nft_pktinfo *pkt) |
| @@ -205,6 +214,11 @@ void nft_meta_set_eval(const struct nft_expr *expr, · 9 unmodified lines | ||
| 205 | 214 | case NFT_META_PRIORITY: |
| 206 | 215 | skb->priority = value; |
| 207 | 216 | break; |
| 217 | + case NFT_META_PKTTYPE: | |
| 218 | + if (skb->pkt_type != value && | |
| 219 | + pkt_type_ok(value) && pkt_type_ok(skb->pkt_type)) | |
| 220 | + skb->pkt_type = value; | |
| 221 | + break; | |
| 208 | 222 | case NFT_META_NFTRACE: |
| 209 | 223 | skb->nf_trace = 1; |
| 210 | 224 | break; |
| @@ -273,6 +287,24 @@ int nft_meta_get_init(const struct nft_ctx *ctx, · 62 unmodified lines | ||
| 273 | 287 | } |
| 274 | 288 | EXPORT_SYMBOL_GPL(nft_meta_get_init); |
| 275 | 289 | |
| 290 | +static int nft_meta_set_init_pkttype(const struct nft_ctx *ctx) | |
| 291 | +{ | |
| 292 | + unsigned int hooks; | |
| 293 | + | |
| 294 | + switch (ctx->afi->family) { | |
| 295 | + case NFPROTO_BRIDGE: | |
| 296 | + hooks = 1 << NF_BR_PRE_ROUTING; | |
| 297 | + break; | |
| 298 | + case NFPROTO_NETDEV: | |
| 299 | + hooks = 1 << NF_NETDEV_INGRESS; | |
| 300 | + break; | |
| 301 | + default: | |
| 302 | + return -EOPNOTSUPP; | |
| 303 | + } | |
| 304 | + | |
| 305 | + return nft_chain_validate_hooks(ctx->chain, hooks); | |
| 306 | +} | |
| 307 | + | |
| 276 | 308 | int nft_meta_set_init(const struct nft_ctx *ctx, |
| 277 | 309 | const struct nft_expr *expr, |
| 278 | 310 | const struct nlattr * const tb[]) |
| @@ -290,6 +322,12 @@ int nft_meta_set_init(const struct nft_ctx *ctx, · 11 unmodified lines | ||
| 290 | 322 | case NFT_META_NFTRACE: |
| 291 | 323 | len = sizeof(u8); |
| 292 | 324 | break; |
| 325 | + case NFT_META_PKTTYPE: | |
| 326 | + err = nft_meta_set_init_pkttype(ctx); | |
| 327 | + if (err) | |
| 328 | + return err; | |
| 329 | + len = sizeof(u8); | |
| 330 | + break; | |
| 293 | 331 | default: |
| 294 | 332 | return -EOPNOTSUPP; |
| 295 | 333 | } |