Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / net / netfilter / xt_physdev.c
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. */
4
5 /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
6  */
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>
14
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");
20
21
22 static bool
23 physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
24 {
25         const struct xt_physdev_info *info = par->matchinfo;
26         const struct net_device *physdev;
27         unsigned long ret;
28         const char *indev, *outdev;
29
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))
37                         return false;
38                 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
39                     !(info->invert & XT_PHYSDEV_OP_ISIN))
40                         return false;
41                 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
42                     !(info->invert & XT_PHYSDEV_OP_ISOUT))
43                         return false;
44                 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
45                     !(info->invert & XT_PHYSDEV_OP_IN))
46                         return false;
47                 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
48                     !(info->invert & XT_PHYSDEV_OP_OUT))
49                         return false;
50                 return true;
51         }
52
53         physdev = nf_bridge_get_physoutdev(skb);
54         outdev = physdev ? physdev->name : NULL;
55
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)))
59                 return false;
60
61         physdev = nf_bridge_get_physindev(skb);
62         indev = physdev ? physdev->name : NULL;
63
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))))
68                 return false;
69
70         if (!(info->bitmask & XT_PHYSDEV_OP_IN))
71                 goto match_outdev;
72
73         if (indev) {
74                 ret = ifname_compare_aligned(indev, info->physindev,
75                                              info->in_mask);
76
77                 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
78                         return false;
79         }
80
81 match_outdev:
82         if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
83                 return true;
84
85         if (!outdev)
86                 return false;
87
88         ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
89
90         return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
91 }
92
93 static int physdev_mt_check(const struct xt_mtchk_param *par)
94 {
95         const struct xt_physdev_info *info = par->matchinfo;
96         static bool brnf_probed __read_mostly;
97
98         if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
99             info->bitmask & ~XT_PHYSDEV_OP_MASK)
100                 return -EINVAL;
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                 pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
106                 return -EINVAL;
107         }
108
109         if (!brnf_probed) {
110                 brnf_probed = true;
111                 request_module("br_netfilter");
112         }
113
114         return 0;
115 }
116
117 static struct xt_match physdev_mt_reg __read_mostly = {
118         .name       = "physdev",
119         .revision   = 0,
120         .family     = NFPROTO_UNSPEC,
121         .checkentry = physdev_mt_check,
122         .match      = physdev_mt,
123         .matchsize  = sizeof(struct xt_physdev_info),
124         .me         = THIS_MODULE,
125 };
126
127 static int __init physdev_mt_init(void)
128 {
129         return xt_register_match(&physdev_mt_reg);
130 }
131
132 static void __exit physdev_mt_exit(void)
133 {
134         xt_unregister_match(&physdev_mt_reg);
135 }
136
137 module_init(physdev_mt_init);
138 module_exit(physdev_mt_exit);