Skip to content

Commit b4aae75

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: meta: add support for setting skb->pkttype
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. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 9c55d3b commit b4aae75

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎net/netfilter/nft_meta.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <net/netfilter/nf_tables_core.h>
2727
#include <net/netfilter/nft_meta.h>
2828

29+
#include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
30+
2931
void nft_meta_get_eval(const struct nft_expr *expr,
3032
struct nft_regs *regs,
3133
const struct nft_pktinfo *pkt)
@@ -190,6 +192,13 @@ void nft_meta_get_eval(const struct nft_expr *expr,
190192
}
191193
EXPORT_SYMBOL_GPL(nft_meta_get_eval);
192194

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+
193202
void nft_meta_set_eval(const struct nft_expr *expr,
194203
struct nft_regs *regs,
195204
const struct nft_pktinfo *pkt)
@@ -205,6 +214,11 @@ void nft_meta_set_eval(const struct nft_expr *expr,
205214
case NFT_META_PRIORITY:
206215
skb->priority = value;
207216
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;
208222
case NFT_META_NFTRACE:
209223
skb->nf_trace = 1;
210224
break;
@@ -273,6 +287,24 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
273287
}
274288
EXPORT_SYMBOL_GPL(nft_meta_get_init);
275289

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+
276308
int nft_meta_set_init(const struct nft_ctx *ctx,
277309
const struct nft_expr *expr,
278310
const struct nlattr * const tb[])
@@ -290,6 +322,12 @@ int nft_meta_set_init(const struct nft_ctx *ctx,
290322
case NFT_META_NFTRACE:
291323
len = sizeof(u8);
292324
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;
293331
default:
294332
return -EOPNOTSUPP;
295333
}

0 commit comments

Comments
 (0)