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