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 { |
Tobin C Harding | 402f903 | 2016-05-10 11:26:57 +1000 | [diff] [blame] | 20 | u8 dsap; |
| 21 | u8 ssap; |
| 22 | u8 ctrl; |
| 23 | u8 pid; |
| 24 | u8 vers; |
| 25 | u8 type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct stp_config_pdu { |
Tobin C Harding | 402f903 | 2016-05-10 11:26:57 +1000 | [diff] [blame] | 29 | u8 flags; |
| 30 | u8 root[8]; |
| 31 | u8 root_cost[4]; |
| 32 | u8 sender[8]; |
| 33 | u8 port[2]; |
| 34 | u8 msg_age[2]; |
| 35 | u8 max_age[2]; |
| 36 | u8 hello_time[2]; |
| 37 | u8 forward_delay[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Ian Morris | 052a4bc | 2015-10-26 09:10:40 +0000 | [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; |
Tobin C Harding | 402f903 | 2016-05-10 11:26:57 +1000 | [diff] [blame] | 47 | u16 v16; |
| 48 | u32 v32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | c = &info->config; |
| 51 | if ((info->bitmask & EBT_STP_FLAGS) && |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 52 | NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 53 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (info->bitmask & EBT_STP_ROOTPRIO) { |
| 55 | v16 = NR16(stpc->root); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 56 | if (NF_INVF(info, EBT_STP_ROOTPRIO, |
| 57 | v16 < c->root_priol || v16 > c->root_priou)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 58 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | if (info->bitmask & EBT_STP_ROOTADDR) { |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 61 | if (NF_INVF(info, EBT_STP_ROOTADDR, |
| 62 | !ether_addr_equal_masked(&stpc->root[2], |
| 63 | c->root_addr, |
| 64 | c->root_addrmsk))) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 65 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | if (info->bitmask & EBT_STP_ROOTCOST) { |
| 68 | v32 = NR32(stpc->root_cost); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 69 | if (NF_INVF(info, EBT_STP_ROOTCOST, |
| 70 | v32 < c->root_costl || v32 > c->root_costu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 71 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | if (info->bitmask & EBT_STP_SENDERPRIO) { |
| 74 | v16 = NR16(stpc->sender); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 75 | if (NF_INVF(info, EBT_STP_SENDERPRIO, |
| 76 | v16 < c->sender_priol || v16 > c->sender_priou)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 77 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | if (info->bitmask & EBT_STP_SENDERADDR) { |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 80 | if (NF_INVF(info, EBT_STP_SENDERADDR, |
| 81 | !ether_addr_equal_masked(&stpc->sender[2], |
| 82 | c->sender_addr, |
| 83 | c->sender_addrmsk))) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 84 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | if (info->bitmask & EBT_STP_PORT) { |
| 87 | v16 = NR16(stpc->port); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 88 | if (NF_INVF(info, EBT_STP_PORT, |
| 89 | v16 < c->portl || v16 > c->portu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 90 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | if (info->bitmask & EBT_STP_MSGAGE) { |
| 93 | v16 = NR16(stpc->msg_age); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 94 | if (NF_INVF(info, EBT_STP_MSGAGE, |
| 95 | v16 < c->msg_agel || v16 > c->msg_ageu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 96 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | if (info->bitmask & EBT_STP_MAXAGE) { |
| 99 | v16 = NR16(stpc->max_age); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 100 | if (NF_INVF(info, EBT_STP_MAXAGE, |
| 101 | v16 < c->max_agel || v16 > c->max_ageu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 102 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | if (info->bitmask & EBT_STP_HELLOTIME) { |
| 105 | v16 = NR16(stpc->hello_time); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 106 | if (NF_INVF(info, EBT_STP_HELLOTIME, |
| 107 | v16 < c->hello_timel || v16 > c->hello_timeu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 108 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | if (info->bitmask & EBT_STP_FWDD) { |
| 111 | v16 = NR16(stpc->forward_delay); |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 112 | if (NF_INVF(info, EBT_STP_FWDD, |
| 113 | v16 < c->forward_delayl || v16 > c->forward_delayu)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 114 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 116 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 119 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 120 | ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 122 | const struct ebt_stp_info *info = par->matchinfo; |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 123 | const struct stp_header *sp; |
| 124 | struct stp_header _stph; |
Tobin C Harding | 402f903 | 2016-05-10 11:26:57 +1000 | [diff] [blame] | 125 | const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph); |
| 128 | if (sp == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 129 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* The stp code only considers these */ |
| 132 | if (memcmp(sp, header, sizeof(header))) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 133 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Joe Perches | c37a2df | 2016-06-24 13:25:22 -0700 | [diff] [blame] | 135 | if ((info->bitmask & EBT_STP_TYPE) && |
| 136 | NF_INVF(info, EBT_STP_TYPE, info->type != sp->type)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 137 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | if (sp->type == BPDU_TYPE_CONFIG && |
| 140 | info->bitmask & EBT_STP_CONFIG_MASK) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 141 | const struct stp_config_pdu *st; |
| 142 | struct stp_config_pdu _stpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | st = skb_header_pointer(skb, sizeof(_stph), |
| 145 | sizeof(_stpc), &_stpc); |
| 146 | if (st == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 147 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return ebt_filter_config(info, st); |
| 149 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 150 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 153 | static int ebt_stp_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 155 | const struct ebt_stp_info *info = par->matchinfo; |
Tobin C Harding | 402f903 | 2016-05-10 11:26:57 +1000 | [diff] [blame] | 156 | const u8 bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00}; |
| 157 | const u8 msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 158 | const struct ebt_entry *e = par->entryinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK || |
| 161 | !(info->bitmask & EBT_STP_MASK)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 162 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* Make sure the match only receives stp frames */ |
Pablo Neira Ayuso | 55917a2 | 2015-05-14 14:57:23 +0200 | [diff] [blame] | 164 | if (!par->nft_compat && |
| 165 | (!ether_addr_equal(e->destmac, bridge_ula) || |
| 166 | !ether_addr_equal(e->destmsk, msk) || |
| 167 | !(e->bitmask & EBT_DESTMAC))) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 168 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 170 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 173 | static struct xt_match ebt_stp_mt_reg __read_mostly = { |
| 174 | .name = "stp", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 175 | .revision = 0, |
| 176 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 177 | .match = ebt_stp_mt, |
| 178 | .checkentry = ebt_stp_mt_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 179 | .matchsize = sizeof(struct ebt_stp_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | .me = THIS_MODULE, |
| 181 | }; |
| 182 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 183 | static int __init ebt_stp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 185 | return xt_register_match(&ebt_stp_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 188 | static void __exit ebt_stp_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 190 | xt_unregister_match(&ebt_stp_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 193 | module_init(ebt_stp_init); |
| 194 | module_exit(ebt_stp_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 195 | MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | MODULE_LICENSE("GPL"); |