Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match the bridge port in and |
2 | * out device for IP packets coming into contact with a bridge. */ | |
3 | ||
4 | /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
deb47c66 | 13 | #include <linux/netfilter_bridge.h> |
2e4e6a17 HW |
14 | #include <linux/netfilter/xt_physdev.h> |
15 | #include <linux/netfilter/x_tables.h> | |
1da177e4 LT |
16 | |
17 | MODULE_LICENSE("GPL"); | |
18 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); | |
19 | MODULE_DESCRIPTION("iptables bridge physical device match module"); | |
2e4e6a17 HW |
20 | MODULE_ALIAS("ipt_physdev"); |
21 | MODULE_ALIAS("ip6t_physdev"); | |
1da177e4 | 22 | |
1d93a9cb | 23 | static bool |
1da177e4 LT |
24 | match(const struct sk_buff *skb, |
25 | const struct net_device *in, | |
26 | const struct net_device *out, | |
c4986734 | 27 | const struct xt_match *match, |
1da177e4 LT |
28 | const void *matchinfo, |
29 | int offset, | |
30 | unsigned int protoff, | |
cff533ac | 31 | bool *hotdrop) |
1da177e4 LT |
32 | { |
33 | int i; | |
34 | static const char nulldevname[IFNAMSIZ]; | |
2e4e6a17 | 35 | const struct xt_physdev_info *info = matchinfo; |
1d93a9cb | 36 | bool ret; |
1da177e4 | 37 | const char *indev, *outdev; |
a47362a2 | 38 | const struct nf_bridge_info *nf_bridge; |
1da177e4 LT |
39 | |
40 | /* Not a bridged IP packet or no info available yet: | |
41 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if | |
42 | * the destination device will be a bridge. */ | |
43 | if (!(nf_bridge = skb->nf_bridge)) { | |
44 | /* Return MATCH if the invert flags of the used options are on */ | |
2e4e6a17 HW |
45 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
46 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) | |
1d93a9cb | 47 | return false; |
2e4e6a17 HW |
48 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
49 | !(info->invert & XT_PHYSDEV_OP_ISIN)) | |
1d93a9cb | 50 | return false; |
2e4e6a17 HW |
51 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
52 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) | |
1d93a9cb | 53 | return false; |
2e4e6a17 HW |
54 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
55 | !(info->invert & XT_PHYSDEV_OP_IN)) | |
1d93a9cb | 56 | return false; |
2e4e6a17 HW |
57 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
58 | !(info->invert & XT_PHYSDEV_OP_OUT)) | |
1d93a9cb JE |
59 | return false; |
60 | return true; | |
1da177e4 LT |
61 | } |
62 | ||
63 | /* This only makes sense in the FORWARD and POSTROUTING chains */ | |
2e4e6a17 | 64 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
1da177e4 | 65 | (!!(nf_bridge->mask & BRNF_BRIDGED) ^ |
2e4e6a17 | 66 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
1d93a9cb | 67 | return false; |
1da177e4 | 68 | |
2e4e6a17 HW |
69 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
70 | (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || | |
71 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && | |
72 | (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) | |
1d93a9cb | 73 | return false; |
1da177e4 | 74 | |
2e4e6a17 | 75 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
1da177e4 LT |
76 | goto match_outdev; |
77 | indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname; | |
1d93a9cb | 78 | for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) { |
1da177e4 LT |
79 | ret |= (((const unsigned int *)indev)[i] |
80 | ^ ((const unsigned int *)info->physindev)[i]) | |
81 | & ((const unsigned int *)info->in_mask)[i]; | |
82 | } | |
83 | ||
1d93a9cb JE |
84 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) |
85 | return false; | |
1da177e4 LT |
86 | |
87 | match_outdev: | |
2e4e6a17 | 88 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
1d93a9cb | 89 | return true; |
1da177e4 LT |
90 | outdev = nf_bridge->physoutdev ? |
91 | nf_bridge->physoutdev->name : nulldevname; | |
1d93a9cb | 92 | for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) { |
1da177e4 LT |
93 | ret |= (((const unsigned int *)outdev)[i] |
94 | ^ ((const unsigned int *)info->physoutdev)[i]) | |
95 | & ((const unsigned int *)info->out_mask)[i]; | |
96 | } | |
97 | ||
1d93a9cb | 98 | return ret ^ !(info->invert & XT_PHYSDEV_OP_OUT); |
1da177e4 LT |
99 | } |
100 | ||
ccb79bdc | 101 | static bool |
1da177e4 | 102 | checkentry(const char *tablename, |
2e4e6a17 | 103 | const void *ip, |
c4986734 | 104 | const struct xt_match *match, |
1da177e4 | 105 | void *matchinfo, |
1da177e4 LT |
106 | unsigned int hook_mask) |
107 | { | |
2e4e6a17 | 108 | const struct xt_physdev_info *info = matchinfo; |
1da177e4 | 109 | |
2e4e6a17 HW |
110 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
111 | info->bitmask & ~XT_PHYSDEV_OP_MASK) | |
ccb79bdc | 112 | return false; |
2bf540b7 | 113 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
10ea6ac8 PM |
114 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
115 | info->invert & XT_PHYSDEV_OP_BRIDGED) && | |
116 | hook_mask & ((1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_FORWARD) | | |
601e68e1 | 117 | (1 << NF_IP_POST_ROUTING))) { |
10ea6ac8 PM |
118 | printk(KERN_WARNING "physdev match: using --physdev-out in the " |
119 | "OUTPUT, FORWARD and POSTROUTING chains for non-bridged " | |
2bf540b7 PM |
120 | "traffic is not supported anymore.\n"); |
121 | if (hook_mask & (1 << NF_IP_LOCAL_OUT)) | |
ccb79bdc | 122 | return false; |
10ea6ac8 | 123 | } |
ccb79bdc | 124 | return true; |
1da177e4 LT |
125 | } |
126 | ||
9f15c530 | 127 | static struct xt_match xt_physdev_match[] __read_mostly = { |
4470bbc7 PM |
128 | { |
129 | .name = "physdev", | |
130 | .family = AF_INET, | |
131 | .checkentry = checkentry, | |
132 | .match = match, | |
133 | .matchsize = sizeof(struct xt_physdev_info), | |
134 | .me = THIS_MODULE, | |
135 | }, | |
136 | { | |
137 | .name = "physdev", | |
138 | .family = AF_INET6, | |
139 | .checkentry = checkentry, | |
140 | .match = match, | |
141 | .matchsize = sizeof(struct xt_physdev_info), | |
142 | .me = THIS_MODULE, | |
143 | }, | |
1da177e4 LT |
144 | }; |
145 | ||
65b4b4e8 | 146 | static int __init xt_physdev_init(void) |
1da177e4 | 147 | { |
4470bbc7 PM |
148 | return xt_register_matches(xt_physdev_match, |
149 | ARRAY_SIZE(xt_physdev_match)); | |
1da177e4 LT |
150 | } |
151 | ||
65b4b4e8 | 152 | static void __exit xt_physdev_fini(void) |
1da177e4 | 153 | { |
4470bbc7 | 154 | xt_unregister_matches(xt_physdev_match, ARRAY_SIZE(xt_physdev_match)); |
1da177e4 LT |
155 | } |
156 | ||
65b4b4e8 AM |
157 | module_init(xt_physdev_init); |
158 | module_exit(xt_physdev_fini); |