blob: 63a96546746575979e2200b87d9a557c774e6970 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* 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>
Andrew Mortondeb47c62006-08-15 00:04:56 -070013#include <linux/netfilter_bridge.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/netfilter/xt_physdev.h>
15#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter_bridge.h>
17#define MATCH 1
18#define NOMATCH 0
19
20MODULE_LICENSE("GPL");
21MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
22MODULE_DESCRIPTION("iptables bridge physical device match module");
23MODULE_ALIAS("ipt_physdev");
24MODULE_ALIAS("ip6t_physdev");
25
26static int
27match(const struct sk_buff *skb,
28 const struct net_device *in,
29 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080030 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -080031 const void *matchinfo,
32 int offset,
33 unsigned int protoff,
34 int *hotdrop)
35{
36 int i;
37 static const char nulldevname[IFNAMSIZ];
38 const struct xt_physdev_info *info = matchinfo;
39 unsigned int ret;
40 const char *indev, *outdev;
41 struct nf_bridge_info *nf_bridge;
42
43 /* Not a bridged IP packet or no info available yet:
44 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
45 * the destination device will be a bridge. */
46 if (!(nf_bridge = skb->nf_bridge)) {
47 /* Return MATCH if the invert flags of the used options are on */
48 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
49 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
50 return NOMATCH;
51 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
52 !(info->invert & XT_PHYSDEV_OP_ISIN))
53 return NOMATCH;
54 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
55 !(info->invert & XT_PHYSDEV_OP_ISOUT))
56 return NOMATCH;
57 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
58 !(info->invert & XT_PHYSDEV_OP_IN))
59 return NOMATCH;
60 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
61 !(info->invert & XT_PHYSDEV_OP_OUT))
62 return NOMATCH;
63 return MATCH;
64 }
65
66 /* This only makes sense in the FORWARD and POSTROUTING chains */
67 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
68 (!!(nf_bridge->mask & BRNF_BRIDGED) ^
69 !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
70 return NOMATCH;
71
72 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
73 (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
74 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
75 (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
76 return NOMATCH;
77
78 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
79 goto match_outdev;
80 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
81 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
82 ret |= (((const unsigned int *)indev)[i]
83 ^ ((const unsigned int *)info->physindev)[i])
84 & ((const unsigned int *)info->in_mask)[i];
85 }
86
87 if ((ret == 0) ^ !(info->invert & XT_PHYSDEV_OP_IN))
88 return NOMATCH;
89
90match_outdev:
91 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
92 return MATCH;
93 outdev = nf_bridge->physoutdev ?
94 nf_bridge->physoutdev->name : nulldevname;
95 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
96 ret |= (((const unsigned int *)outdev)[i]
97 ^ ((const unsigned int *)info->physoutdev)[i])
98 & ((const unsigned int *)info->out_mask)[i];
99 }
100
101 return (ret != 0) ^ !(info->invert & XT_PHYSDEV_OP_OUT);
102}
103
104static int
105checkentry(const char *tablename,
106 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -0800107 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108 void *matchinfo,
109 unsigned int matchsize,
110 unsigned int hook_mask)
111{
112 const struct xt_physdev_info *info = matchinfo;
113
Harald Welte2e4e6a12006-01-12 13:30:04 -0800114 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
115 info->bitmask & ~XT_PHYSDEV_OP_MASK)
116 return 0;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700117 if (brnf_deferred_hooks == 0 &&
118 info->bitmask & XT_PHYSDEV_OP_OUT &&
119 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
120 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
121 hook_mask & ((1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_FORWARD) |
122 (1 << NF_IP_POST_ROUTING))) {
123 printk(KERN_WARNING "physdev match: using --physdev-out in the "
124 "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
125 "traffic is deprecated and breaks other things, it will "
126 "be removed in January 2007. See Documentation/"
127 "feature-removal-schedule.txt for details. This doesn't "
128 "affect you in case you're using it for purely bridged "
129 "traffic.\n");
130 brnf_deferred_hooks = 1;
131 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132 return 1;
133}
134
135static struct xt_match physdev_match = {
136 .name = "physdev",
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800137 .match = match,
138 .matchsize = sizeof(struct xt_physdev_info),
139 .checkentry = checkentry,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800140 .family = AF_INET,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141 .me = THIS_MODULE,
142};
143
144static struct xt_match physdev6_match = {
145 .name = "physdev",
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800146 .match = match,
147 .matchsize = sizeof(struct xt_physdev_info),
148 .checkentry = checkentry,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800149 .family = AF_INET6,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800150 .me = THIS_MODULE,
151};
152
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800153static int __init xt_physdev_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800154{
155 int ret;
156
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800157 ret = xt_register_match(&physdev_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800158 if (ret < 0)
159 return ret;
160
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800161 ret = xt_register_match(&physdev6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800162 if (ret < 0)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800163 xt_unregister_match(&physdev_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800164
165 return ret;
166}
167
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800168static void __exit xt_physdev_fini(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800169{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800170 xt_unregister_match(&physdev_match);
171 xt_unregister_match(&physdev6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172}
173
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800174module_init(xt_physdev_init);
175module_exit(xt_physdev_fini);