Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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> |
Andrew Morton | deb47c6 | 2006-08-15 00:04:56 -0700 | [diff] [blame] | 13 | #include <linux/netfilter_bridge.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 14 | #include <linux/netfilter/xt_physdev.h> |
| 15 | #include <linux/netfilter/x_tables.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | |
| 17 | MODULE_LICENSE("GPL"); |
| 18 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 19 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 20 | MODULE_ALIAS("ipt_physdev"); |
| 21 | MODULE_ALIAS("ip6t_physdev"); |
| 22 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 23 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 24 | physdev_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | { |
| 26 | int i; |
| 27 | static const char nulldevname[IFNAMSIZ]; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 28 | const struct xt_physdev_info *info = par->matchinfo; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 29 | bool ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 30 | const char *indev, *outdev; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 31 | const struct nf_bridge_info *nf_bridge; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | |
| 33 | /* Not a bridged IP packet or no info available yet: |
| 34 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if |
| 35 | * the destination device will be a bridge. */ |
| 36 | if (!(nf_bridge = skb->nf_bridge)) { |
| 37 | /* Return MATCH if the invert flags of the used options are on */ |
| 38 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
| 39 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 40 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
| 42 | !(info->invert & XT_PHYSDEV_OP_ISIN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 43 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 44 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
| 45 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 46 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
| 48 | !(info->invert & XT_PHYSDEV_OP_IN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 49 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 50 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
| 51 | !(info->invert & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 52 | return false; |
| 53 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* This only makes sense in the FORWARD and POSTROUTING chains */ |
| 57 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
| 58 | (!!(nf_bridge->mask & BRNF_BRIDGED) ^ |
| 59 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 60 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 61 | |
| 62 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
| 63 | (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || |
| 64 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && |
| 65 | (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 66 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | |
| 68 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
| 69 | goto match_outdev; |
| 70 | indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 71 | for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | ret |= (((const unsigned int *)indev)[i] |
| 73 | ^ ((const unsigned int *)info->physindev)[i]) |
| 74 | & ((const unsigned int *)info->in_mask)[i]; |
| 75 | } |
| 76 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 77 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) |
| 78 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | |
| 80 | match_outdev: |
| 81 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 82 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 83 | outdev = nf_bridge->physoutdev ? |
| 84 | nf_bridge->physoutdev->name : nulldevname; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 85 | for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 86 | ret |= (((const unsigned int *)outdev)[i] |
| 87 | ^ ((const unsigned int *)info->physoutdev)[i]) |
| 88 | & ((const unsigned int *)info->out_mask)[i]; |
| 89 | } |
| 90 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 91 | return ret ^ !(info->invert & XT_PHYSDEV_OP_OUT); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 94 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 95 | physdev_mt_check(const char *tablename, const void *ip, |
| 96 | const struct xt_match *match, void *matchinfo, |
| 97 | unsigned int hook_mask) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 98 | { |
| 99 | const struct xt_physdev_info *info = matchinfo; |
| 100 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 101 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
| 102 | info->bitmask & ~XT_PHYSDEV_OP_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 103 | return false; |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame] | 104 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 105 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
| 106 | info->invert & XT_PHYSDEV_OP_BRIDGED) && |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 107 | hook_mask & ((1 << NF_INET_LOCAL_OUT) | (1 << NF_INET_FORWARD) | |
| 108 | (1 << NF_INET_POST_ROUTING))) { |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 109 | printk(KERN_WARNING "physdev match: using --physdev-out in the " |
| 110 | "OUTPUT, FORWARD and POSTROUTING chains for non-bridged " |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame] | 111 | "traffic is not supported anymore.\n"); |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 112 | if (hook_mask & (1 << NF_INET_LOCAL_OUT)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 113 | return false; |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 114 | } |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 115 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 118 | static struct xt_match physdev_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 119 | { |
| 120 | .name = "physdev", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 121 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 122 | .checkentry = physdev_mt_check, |
| 123 | .match = physdev_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 124 | .matchsize = sizeof(struct xt_physdev_info), |
| 125 | .me = THIS_MODULE, |
| 126 | }, |
| 127 | { |
| 128 | .name = "physdev", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 129 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 130 | .checkentry = physdev_mt_check, |
| 131 | .match = physdev_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 132 | .matchsize = sizeof(struct xt_physdev_info), |
| 133 | .me = THIS_MODULE, |
| 134 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 137 | static int __init physdev_mt_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 138 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 139 | return xt_register_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 142 | static void __exit physdev_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 143 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 144 | xt_unregister_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 147 | module_init(physdev_mt_init); |
| 148 | module_exit(physdev_mt_exit); |