Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_stp |
| 3 | * |
| 4 | * Authors: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
| 6 | * Stephen Hemminger <shemminger@osdl.org> |
| 7 | * |
| 8 | * July, 2003 |
| 9 | */ |
David S. Miller | 82bf7e9 | 2006-01-11 15:38:28 -0800 | [diff] [blame] | 10 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 12 | #include <linux/netfilter/x_tables.h> |
| 13 | #include <linux/netfilter_bridge/ebtables.h> |
| 14 | #include <linux/netfilter_bridge/ebt_stp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #define BPDU_TYPE_CONFIG 0 |
| 17 | #define BPDU_TYPE_TCN 0x80 |
| 18 | |
| 19 | struct stp_header { |
| 20 | uint8_t dsap; |
| 21 | uint8_t ssap; |
| 22 | uint8_t ctrl; |
| 23 | uint8_t pid; |
| 24 | uint8_t vers; |
| 25 | uint8_t type; |
| 26 | }; |
| 27 | |
| 28 | struct stp_config_pdu { |
| 29 | uint8_t flags; |
| 30 | uint8_t root[8]; |
| 31 | uint8_t root_cost[4]; |
| 32 | uint8_t sender[8]; |
| 33 | uint8_t port[2]; |
| 34 | uint8_t msg_age[2]; |
| 35 | uint8_t max_age[2]; |
| 36 | uint8_t hello_time[2]; |
| 37 | uint8_t forward_delay[2]; |
| 38 | }; |
| 39 | |
| 40 | #define NR16(p) (p[0] << 8 | p[1]) |
| 41 | #define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]) |
| 42 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 43 | static bool ebt_filter_config(const struct ebt_stp_info *info, |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 44 | const struct stp_config_pdu *stpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 46 | const struct ebt_stp_config_info *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | uint16_t v16; |
| 48 | uint32_t v32; |
| 49 | int verdict, i; |
| 50 | |
| 51 | c = &info->config; |
| 52 | if ((info->bitmask & EBT_STP_FLAGS) && |
| 53 | FWINV(c->flags != stpc->flags, EBT_STP_FLAGS)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 54 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (info->bitmask & EBT_STP_ROOTPRIO) { |
| 56 | v16 = NR16(stpc->root); |
| 57 | if (FWINV(v16 < c->root_priol || |
| 58 | v16 > c->root_priou, EBT_STP_ROOTPRIO)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 59 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | if (info->bitmask & EBT_STP_ROOTADDR) { |
| 62 | verdict = 0; |
| 63 | for (i = 0; i < 6; i++) |
| 64 | verdict |= (stpc->root[2+i] ^ c->root_addr[i]) & |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 65 | c->root_addrmsk[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (FWINV(verdict != 0, EBT_STP_ROOTADDR)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 67 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | if (info->bitmask & EBT_STP_ROOTCOST) { |
| 70 | v32 = NR32(stpc->root_cost); |
| 71 | if (FWINV(v32 < c->root_costl || |
| 72 | v32 > c->root_costu, EBT_STP_ROOTCOST)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 73 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | if (info->bitmask & EBT_STP_SENDERPRIO) { |
| 76 | v16 = NR16(stpc->sender); |
| 77 | if (FWINV(v16 < c->sender_priol || |
| 78 | v16 > c->sender_priou, EBT_STP_SENDERPRIO)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 79 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | if (info->bitmask & EBT_STP_SENDERADDR) { |
| 82 | verdict = 0; |
| 83 | for (i = 0; i < 6; i++) |
| 84 | verdict |= (stpc->sender[2+i] ^ c->sender_addr[i]) & |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 85 | c->sender_addrmsk[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (FWINV(verdict != 0, EBT_STP_SENDERADDR)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 87 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | if (info->bitmask & EBT_STP_PORT) { |
| 90 | v16 = NR16(stpc->port); |
| 91 | if (FWINV(v16 < c->portl || |
| 92 | v16 > c->portu, EBT_STP_PORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 93 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | if (info->bitmask & EBT_STP_MSGAGE) { |
| 96 | v16 = NR16(stpc->msg_age); |
| 97 | if (FWINV(v16 < c->msg_agel || |
| 98 | v16 > c->msg_ageu, EBT_STP_MSGAGE)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 99 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | if (info->bitmask & EBT_STP_MAXAGE) { |
| 102 | v16 = NR16(stpc->max_age); |
| 103 | if (FWINV(v16 < c->max_agel || |
| 104 | v16 > c->max_ageu, EBT_STP_MAXAGE)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 105 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | if (info->bitmask & EBT_STP_HELLOTIME) { |
| 108 | v16 = NR16(stpc->hello_time); |
| 109 | if (FWINV(v16 < c->hello_timel || |
| 110 | v16 > c->hello_timeu, EBT_STP_HELLOTIME)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 111 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | if (info->bitmask & EBT_STP_FWDD) { |
| 114 | v16 = NR16(stpc->forward_delay); |
| 115 | if (FWINV(v16 < c->forward_delayl || |
| 116 | v16 > c->forward_delayu, EBT_STP_FWDD)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 117 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 119 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 122 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 123 | ebt_stp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 125 | const struct ebt_stp_info *info = par->matchinfo; |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 126 | const struct stp_header *sp; |
| 127 | struct stp_header _stph; |
| 128 | const uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph); |
| 131 | if (sp == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 132 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* The stp code only considers these */ |
| 135 | if (memcmp(sp, header, sizeof(header))) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 136 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 138 | if (info->bitmask & EBT_STP_TYPE && |
| 139 | FWINV(info->type != sp->type, EBT_STP_TYPE)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 140 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (sp->type == BPDU_TYPE_CONFIG && |
| 143 | info->bitmask & EBT_STP_CONFIG_MASK) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 144 | const struct stp_config_pdu *st; |
| 145 | struct stp_config_pdu _stpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | st = skb_header_pointer(skb, sizeof(_stph), |
| 148 | sizeof(_stpc), &_stpc); |
| 149 | if (st == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 150 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return ebt_filter_config(info, st); |
| 152 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 153 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 156 | static bool ebt_stp_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 158 | const struct ebt_stp_info *info = par->matchinfo; |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 159 | const uint8_t bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00}; |
| 160 | const uint8_t msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 161 | const struct ebt_entry *e = par->entryinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK || |
| 164 | !(info->bitmask & EBT_STP_MASK)) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 165 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* Make sure the match only receives stp frames */ |
Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame] | 167 | if (compare_ether_addr(e->destmac, bridge_ula) || |
| 168 | compare_ether_addr(e->destmsk, msk) || !(e->bitmask & EBT_DESTMAC)) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 169 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 171 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 174 | static struct xt_match ebt_stp_mt_reg __read_mostly = { |
| 175 | .name = "stp", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 176 | .revision = 0, |
| 177 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 178 | .match = ebt_stp_mt, |
| 179 | .checkentry = ebt_stp_mt_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 180 | .matchsize = sizeof(struct ebt_stp_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | .me = THIS_MODULE, |
| 182 | }; |
| 183 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 184 | static int __init ebt_stp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 186 | return xt_register_match(&ebt_stp_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 189 | static void __exit ebt_stp_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 191 | xt_unregister_match(&ebt_stp_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 194 | module_init(ebt_stp_init); |
| 195 | module_exit(ebt_stp_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 196 | MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | MODULE_LICENSE("GPL"); |