1 /* Shared library add-on to iptables to add bridge port matching support. */
8 #include <linux/netfilter_ipv4/ipt_physdev.h>
9 #if defined(__GLIBC__) && __GLIBC__ == 2
10 #include <net/ethernet.h>
12 #include <linux/if_ether.h>
19 "physdev v%s options:\n"
20 " --physdev-in [!] input name[+] bridge port name ([+] for wildcard)\n"
21 " --physdev-out [!] output name[+] bridge port name ([+] for wildcard)\n"
22 " [!] --physdev-is-in arrived on a bridge device\n"
23 " [!] --physdev-is-out will leave on a bridge device\n"
24 " [!] --physdev-is-bridged it's a bridged packet\n"
25 "\n", IPTABLES_VERSION);
28 static struct option opts[] = {
29 { "physdev-in", 1, 0, '1' },
30 { "physdev-out", 1, 0, '2' },
31 { "physdev-is-in", 0, 0, '3' },
32 { "physdev-is-out", 0, 0, '4' },
33 { "physdev-is-bridged", 0, 0, '5' },
37 /* copied from iptables.c */
39 parse_interface(const char *arg, char *vianame, unsigned char *mask)
41 int vialen = strlen(arg);
44 memset(mask, 0, IFNAMSIZ);
45 memset(vianame, 0, IFNAMSIZ);
47 if (vialen + 1 > IFNAMSIZ)
48 exit_error(PARAMETER_PROBLEM,
49 "interface name `%s' must be shorter than IFNAMSIZ"
50 " (%i)", arg, IFNAMSIZ-1);
54 memset(mask, 0, IFNAMSIZ);
55 else if (vianame[vialen - 1] == '+') {
56 memset(mask, 0xFF, vialen - 1);
57 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
58 /* Don't remove `+' here! -HW */
60 /* Include nul-terminator in match */
61 memset(mask, 0xFF, vialen + 1);
62 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
63 for (i = 0; vianame[i]; i++) {
64 if (!isalnum(vianame[i])
66 && vianame[i] != '.') {
67 printf("Warning: wierd character in interface"
68 " `%s' (No aliases, :, ! or *).\n",
77 init(struct ipt_entry_match *m, unsigned int *nfcache)
82 parse(int c, char **argv, int invert, unsigned int *flags,
83 const struct ipt_entry *entry,
84 unsigned int *nfcache,
85 struct ipt_entry_match **match)
87 struct ipt_physdev_info *info =
88 (struct ipt_physdev_info*)(*match)->data;
92 if (*flags & IPT_PHYSDEV_OP_IN)
94 check_inverse(optarg, &invert, &optind, 0);
95 parse_interface(argv[optind-1], info->physindev, info->in_mask);
97 info->invert |= IPT_PHYSDEV_OP_IN;
98 info->bitmask |= IPT_PHYSDEV_OP_IN;
99 *flags |= IPT_PHYSDEV_OP_IN;
103 if (*flags & IPT_PHYSDEV_OP_OUT)
105 check_inverse(optarg, &invert, &optind, 0);
106 parse_interface(argv[optind-1], info->physoutdev,
109 info->invert |= IPT_PHYSDEV_OP_OUT;
110 info->bitmask |= IPT_PHYSDEV_OP_OUT;
111 *flags |= IPT_PHYSDEV_OP_OUT;
115 if (*flags & IPT_PHYSDEV_OP_ISIN)
117 check_inverse(optarg, &invert, &optind, 0);
118 info->bitmask |= IPT_PHYSDEV_OP_ISIN;
120 info->invert |= IPT_PHYSDEV_OP_ISIN;
121 *flags |= IPT_PHYSDEV_OP_ISIN;
125 if (*flags & IPT_PHYSDEV_OP_ISOUT)
127 check_inverse(optarg, &invert, &optind, 0);
128 info->bitmask |= IPT_PHYSDEV_OP_ISOUT;
130 info->invert |= IPT_PHYSDEV_OP_ISOUT;
131 *flags |= IPT_PHYSDEV_OP_ISOUT;
135 if (*flags & IPT_PHYSDEV_OP_BRIDGED)
137 check_inverse(optarg, &invert, &optind, 0);
139 info->invert |= IPT_PHYSDEV_OP_BRIDGED;
140 *flags |= IPT_PHYSDEV_OP_BRIDGED;
141 info->bitmask |= IPT_PHYSDEV_OP_BRIDGED;
150 exit_error(PARAMETER_PROBLEM,
151 "multiple use of the same physdev option is not allowed");
155 static void final_check(unsigned int flags)
158 exit_error(PARAMETER_PROBLEM, "PHYSDEV: no physdev option specified");
162 print(const struct ipt_ip *ip,
163 const struct ipt_entry_match *match,
166 struct ipt_physdev_info *info =
167 (struct ipt_physdev_info*)match->data;
169 printf("PHYSDEV match");
170 if (info->bitmask & IPT_PHYSDEV_OP_ISIN)
171 printf("%s --physdev-is-in",
172 info->invert & IPT_PHYSDEV_OP_ISIN ? " !":"");
173 if (info->bitmask & IPT_PHYSDEV_OP_IN)
174 printf("%s --physdev-in %s",
175 (info->invert & IPT_PHYSDEV_OP_IN) ? " !":"", info->physindev);
177 if (info->bitmask & IPT_PHYSDEV_OP_ISOUT)
178 printf("%s --physdev-is-out",
179 info->invert & IPT_PHYSDEV_OP_ISOUT ? " !":"");
180 if (info->bitmask & IPT_PHYSDEV_OP_OUT)
181 printf("%s --physdev-out %s",
182 (info->invert & IPT_PHYSDEV_OP_OUT) ? " !":"", info->physoutdev);
183 if (info->bitmask & IPT_PHYSDEV_OP_BRIDGED)
184 printf("%s --physdev-is-bridged",
185 info->invert & IPT_PHYSDEV_OP_BRIDGED ? " !":"");
189 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
191 struct ipt_physdev_info *info =
192 (struct ipt_physdev_info*)match->data;
194 if (info->bitmask & IPT_PHYSDEV_OP_ISIN)
195 printf("%s --physdev-is-in",
196 info->invert & IPT_PHYSDEV_OP_ISIN ? " !":"");
197 if (info->bitmask & IPT_PHYSDEV_OP_IN)
198 printf("%s --physdev-in %s",
199 (info->invert & IPT_PHYSDEV_OP_IN) ? " !":"", info->physindev);
201 if (info->bitmask & IPT_PHYSDEV_OP_ISOUT)
202 printf("%s --physdev-is-out",
203 info->invert & IPT_PHYSDEV_OP_ISOUT ? " !":"");
204 if (info->bitmask & IPT_PHYSDEV_OP_OUT)
205 printf("%s --physdev-out %s",
206 (info->invert & IPT_PHYSDEV_OP_OUT) ? " !":"", info->physoutdev);
207 if (info->bitmask & IPT_PHYSDEV_OP_BRIDGED)
208 printf("%s --physdev-is-bridged",
209 info->invert & IPT_PHYSDEV_OP_BRIDGED ? " !":"");
214 struct iptables_match physdev
218 IPT_ALIGN(sizeof(struct ipt_physdev_info)),
219 IPT_ALIGN(sizeof(struct ipt_physdev_info)),
231 register_match(&physdev);