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
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <linux/netfilter_bridge.h>
11 #include <linux/netfilter/xt_physdev.h>
12 #include <linux/netfilter/x_tables.h>
13 #include <net/netfilter/br_netfilter.h>
15 MODULE_LICENSE("GPL");
16 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
17 MODULE_DESCRIPTION("Xtables: Bridge physical device match");
18 MODULE_ALIAS("ipt_physdev");
19 MODULE_ALIAS("ip6t_physdev");
23 physdev_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
25 const struct xt_physdev_info
*info
= par
->matchinfo
;
26 const struct net_device
*physdev
;
28 const char *indev
, *outdev
;
30 /* Not a bridged IP packet or no info available yet:
31 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
32 * the destination device will be a bridge. */
33 if (!nf_bridge_info_exists(skb
)) {
34 /* Return MATCH if the invert flags of the used options are on */
35 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
36 !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
))
38 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
) &&
39 !(info
->invert
& XT_PHYSDEV_OP_ISIN
))
41 if ((info
->bitmask
& XT_PHYSDEV_OP_ISOUT
) &&
42 !(info
->invert
& XT_PHYSDEV_OP_ISOUT
))
44 if ((info
->bitmask
& XT_PHYSDEV_OP_IN
) &&
45 !(info
->invert
& XT_PHYSDEV_OP_IN
))
47 if ((info
->bitmask
& XT_PHYSDEV_OP_OUT
) &&
48 !(info
->invert
& XT_PHYSDEV_OP_OUT
))
53 physdev
= nf_bridge_get_physoutdev(skb
);
54 outdev
= physdev
? physdev
->name
: NULL
;
56 /* This only makes sense in the FORWARD and POSTROUTING chains */
57 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
58 (!!outdev
^ !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
)))
61 physdev
= nf_bridge_get_physindev(skb
);
62 indev
= physdev
? physdev
->name
: NULL
;
64 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
&&
65 (!indev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISIN
))) ||
66 (info
->bitmask
& XT_PHYSDEV_OP_ISOUT
&&
67 (!outdev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISOUT
))))
70 if (!(info
->bitmask
& XT_PHYSDEV_OP_IN
))
74 ret
= ifname_compare_aligned(indev
, info
->physindev
,
77 if (!ret
^ !(info
->invert
& XT_PHYSDEV_OP_IN
))
82 if (!(info
->bitmask
& XT_PHYSDEV_OP_OUT
))
88 ret
= ifname_compare_aligned(outdev
, info
->physoutdev
, info
->out_mask
);
90 return (!!ret
^ !(info
->invert
& XT_PHYSDEV_OP_OUT
));
93 static int physdev_mt_check(const struct xt_mtchk_param
*par
)
95 const struct xt_physdev_info
*info
= par
->matchinfo
;
96 static bool brnf_probed __read_mostly
;
98 if (!(info
->bitmask
& XT_PHYSDEV_OP_MASK
) ||
99 info
->bitmask
& ~XT_PHYSDEV_OP_MASK
)
101 if (info
->bitmask
& (XT_PHYSDEV_OP_OUT
| XT_PHYSDEV_OP_ISOUT
) &&
102 (!(info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) ||
103 info
->invert
& XT_PHYSDEV_OP_BRIDGED
) &&
104 par
->hook_mask
& ((1 << NF_INET_LOCAL_OUT
) |
105 (1 << NF_INET_FORWARD
) | (1 << NF_INET_POST_ROUTING
))) {
106 pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
107 if (par
->hook_mask
& (1 << NF_INET_LOCAL_OUT
))
113 request_module("br_netfilter");
119 static struct xt_match physdev_mt_reg __read_mostly
= {
122 .family
= NFPROTO_UNSPEC
,
123 .checkentry
= physdev_mt_check
,
125 .matchsize
= sizeof(struct xt_physdev_info
),
129 static int __init
physdev_mt_init(void)
131 return xt_register_match(&physdev_mt_reg
);
134 static void __exit
physdev_mt_exit(void)
136 xt_unregister_match(&physdev_mt_reg
);
139 module_init(physdev_mt_init
);
140 module_exit(physdev_mt_exit
);