History

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.

Florian Westphal committed 10 years ago

Commit b4aae751 parent 9c55d3b1 file · +38 −0Browse files

Unified · Split

net/netfilter/nft_meta.c+38−0
@@ -26,6 +26,8 @@ · 25 unmodified lines
26#include <net/netfilter/nf_tables_core.h>26#include <net/netfilter/nf_tables_core.h>
27#include <net/netfilter/nft_meta.h>27#include <net/netfilter/nft_meta.h>
28 28
29#include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
30
29void nft_meta_get_eval(const struct nft_expr *expr,31void nft_meta_get_eval(const struct nft_expr *expr,
30 struct nft_regs *regs,32 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)33 const struct nft_pktinfo *pkt)
@@ -190,6 +192,13 @@ err: · 158 unmodified lines
190}192}
191EXPORT_SYMBOL_GPL(nft_meta_get_eval);193EXPORT_SYMBOL_GPL(nft_meta_get_eval);
192 194
195/* don't change or set _LOOPBACK, _USER, etc. */
196static bool pkt_type_ok(u32 p)
197{
198 return p == PACKET_HOST || p == PACKET_BROADCAST ||
199 p == PACKET_MULTICAST || p == PACKET_OTHERHOST;
200}
201
193void nft_meta_set_eval(const struct nft_expr *expr,202void nft_meta_set_eval(const struct nft_expr *expr,
194 struct nft_regs *regs,203 struct nft_regs *regs,
195 const struct nft_pktinfo *pkt)204 const struct nft_pktinfo *pkt)
@@ -205,6 +214,11 @@ void nft_meta_set_eval(const struct nft_expr *expr, · 9 unmodified lines
205 case NFT_META_PRIORITY:214 case NFT_META_PRIORITY:
206 skb->priority = value;215 skb->priority = value;
207 break;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 case NFT_META_NFTRACE:222 case NFT_META_NFTRACE:
209 skb->nf_trace = 1;223 skb->nf_trace = 1;
210 break;224 break;
@@ -273,6 +287,24 @@ int nft_meta_get_init(const struct nft_ctx *ctx, · 62 unmodified lines
273}287}
274EXPORT_SYMBOL_GPL(nft_meta_get_init);288EXPORT_SYMBOL_GPL(nft_meta_get_init);
275 289
290static 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
276int nft_meta_set_init(const struct nft_ctx *ctx,308int nft_meta_set_init(const struct nft_ctx *ctx,
277 const struct nft_expr *expr,309 const struct nft_expr *expr,
278 const struct nlattr * const tb[])310 const struct nlattr * const tb[])
@@ -290,6 +322,12 @@ int nft_meta_set_init(const struct nft_ctx *ctx, · 11 unmodified lines
290 case NFT_META_NFTRACE:322 case NFT_META_NFTRACE:
291 len = sizeof(u8);323 len = sizeof(u8);
292 break;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 default:331 default:
294 return -EOPNOTSUPP;332 return -EOPNOTSUPP;
295 }333 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t