1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match the bridge port in and
3 * out device for IP packets coming into contact with a bridge. */
5 /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/netfilter_bridge.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <uapi/linux/netfilter/xt_physdev.h>
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
18 MODULE_DESCRIPTION("Xtables: Bridge physical device match");
19 MODULE_ALIAS("ipt_physdev");
20 MODULE_ALIAS("ip6t_physdev");
24 physdev_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
26 const struct xt_physdev_info
*info
= par
->matchinfo
;
27 const struct net_device
*physdev
;
29 const char *indev
, *outdev
;
31 /* Not a bridged IP packet or no info available yet:
32 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
33 * the destination device will be a bridge. */
34 if (!nf_bridge_info_exists(skb
)) {
35 /* Return MATCH if the invert flags of the used options are on */
36 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
37 !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
))
39 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
) &&
40 !(info
->invert
& XT_PHYSDEV_OP_ISIN
))
42 if ((info
->bitmask
& XT_PHYSDEV_OP_ISOUT
) &&
43 !(info
->invert
& XT_PHYSDEV_OP_ISOUT
))
45 if ((info
->bitmask
& XT_PHYSDEV_OP_IN
) &&
46 !(info
->invert
& XT_PHYSDEV_OP_IN
))
48 if ((info
->bitmask
& XT_PHYSDEV_OP_OUT
) &&
49 !(info
->invert
& XT_PHYSDEV_OP_OUT
))
54 physdev
= nf_bridge_get_physoutdev(skb
);
55 outdev
= physdev
? physdev
->name
: NULL
;
57 /* This only makes sense in the FORWARD and POSTROUTING chains */
58 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
59 (!!outdev
^ !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
)))
62 physdev
= nf_bridge_get_physindev(skb
);
63 indev
= physdev
? physdev
->name
: NULL
;
65 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
&&
66 (!indev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISIN
))) ||
67 (info
->bitmask
& XT_PHYSDEV_OP_ISOUT
&&
68 (!outdev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISOUT
))))
71 if (!(info
->bitmask
& XT_PHYSDEV_OP_IN
))
75 ret
= ifname_compare_aligned(indev
, info
->physindev
,
78 if (!ret
^ !(info
->invert
& XT_PHYSDEV_OP_IN
))
83 if (!(info
->bitmask
& XT_PHYSDEV_OP_OUT
))
89 ret
= ifname_compare_aligned(outdev
, info
->physoutdev
, info
->out_mask
);
91 return (!!ret
^ !(info
->invert
& XT_PHYSDEV_OP_OUT
));
94 static int physdev_mt_check(const struct xt_mtchk_param
*par
)
96 const struct xt_physdev_info
*info
= par
->matchinfo
;
97 static bool brnf_probed __read_mostly
;
99 if (!(info
->bitmask
& XT_PHYSDEV_OP_MASK
) ||
100 info
->bitmask
& ~XT_PHYSDEV_OP_MASK
)
102 if (info
->bitmask
& (XT_PHYSDEV_OP_OUT
| XT_PHYSDEV_OP_ISOUT
) &&
103 (!(info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) ||
104 info
->invert
& XT_PHYSDEV_OP_BRIDGED
) &&
105 par
->hook_mask
& (1 << NF_INET_LOCAL_OUT
)) {
106 pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
112 request_module("br_netfilter");
118 static struct xt_match physdev_mt_reg __read_mostly
= {
121 .family
= NFPROTO_UNSPEC
,
122 .checkentry
= physdev_mt_check
,
124 .matchsize
= sizeof(struct xt_physdev_info
),
128 static int __init
physdev_mt_init(void)
130 return xt_register_match(&physdev_mt_reg
);
133 static void __exit
physdev_mt_exit(void)
135 xt_unregister_match(&physdev_mt_reg
);
138 module_init(physdev_mt_init
);
139 module_exit(physdev_mt_exit
);