treewide: Add SPDX license identifier for more missed files
[linux-block.git] / net / bridge / netfilter / ebt_stp.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * ebt_stp
4 *
5 * Authors:
6 * Bart De Schuymer <bdschuym@pandora.be>
7 * Stephen Hemminger <shemminger@osdl.org>
8 *
9 * July, 2003
10 */
82bf7e97 11#include <linux/etherdevice.h>
1da177e4 12#include <linux/module.h>
18219d3f
JE
13#include <linux/netfilter/x_tables.h>
14#include <linux/netfilter_bridge/ebtables.h>
15#include <linux/netfilter_bridge/ebt_stp.h>
1da177e4
LT
16
17#define BPDU_TYPE_CONFIG 0
18#define BPDU_TYPE_TCN 0x80
19
20struct stp_header {
402f9030
TH
21 u8 dsap;
22 u8 ssap;
23 u8 ctrl;
24 u8 pid;
25 u8 vers;
26 u8 type;
1da177e4
LT
27};
28
29struct stp_config_pdu {
402f9030
TH
30 u8 flags;
31 u8 root[8];
32 u8 root_cost[4];
33 u8 sender[8];
34 u8 port[2];
35 u8 msg_age[2];
36 u8 max_age[2];
37 u8 hello_time[2];
38 u8 forward_delay[2];
1da177e4
LT
39};
40
41#define NR16(p) (p[0] << 8 | p[1])
42#define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
43
8cc784ee 44static bool ebt_filter_config(const struct ebt_stp_info *info,
052a4bc4 45 const struct stp_config_pdu *stpc)
1da177e4 46{
abfdf1c4 47 const struct ebt_stp_config_info *c;
402f9030
TH
48 u16 v16;
49 u32 v32;
1da177e4
LT
50
51 c = &info->config;
52 if ((info->bitmask & EBT_STP_FLAGS) &&
c37a2dfa 53 NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags))
8cc784ee 54 return false;
1da177e4
LT
55 if (info->bitmask & EBT_STP_ROOTPRIO) {
56 v16 = NR16(stpc->root);
c37a2dfa
JP
57 if (NF_INVF(info, EBT_STP_ROOTPRIO,
58 v16 < c->root_priol || v16 > c->root_priou))
8cc784ee 59 return false;
1da177e4
LT
60 }
61 if (info->bitmask & EBT_STP_ROOTADDR) {
c37a2dfa
JP
62 if (NF_INVF(info, EBT_STP_ROOTADDR,
63 !ether_addr_equal_masked(&stpc->root[2],
64 c->root_addr,
65 c->root_addrmsk)))
8cc784ee 66 return false;
1da177e4
LT
67 }
68 if (info->bitmask & EBT_STP_ROOTCOST) {
69 v32 = NR32(stpc->root_cost);
c37a2dfa
JP
70 if (NF_INVF(info, EBT_STP_ROOTCOST,
71 v32 < c->root_costl || v32 > c->root_costu))
8cc784ee 72 return false;
1da177e4
LT
73 }
74 if (info->bitmask & EBT_STP_SENDERPRIO) {
75 v16 = NR16(stpc->sender);
c37a2dfa
JP
76 if (NF_INVF(info, EBT_STP_SENDERPRIO,
77 v16 < c->sender_priol || v16 > c->sender_priou))
8cc784ee 78 return false;
1da177e4
LT
79 }
80 if (info->bitmask & EBT_STP_SENDERADDR) {
c37a2dfa
JP
81 if (NF_INVF(info, EBT_STP_SENDERADDR,
82 !ether_addr_equal_masked(&stpc->sender[2],
83 c->sender_addr,
84 c->sender_addrmsk)))
8cc784ee 85 return false;
1da177e4
LT
86 }
87 if (info->bitmask & EBT_STP_PORT) {
88 v16 = NR16(stpc->port);
c37a2dfa
JP
89 if (NF_INVF(info, EBT_STP_PORT,
90 v16 < c->portl || v16 > c->portu))
8cc784ee 91 return false;
1da177e4
LT
92 }
93 if (info->bitmask & EBT_STP_MSGAGE) {
94 v16 = NR16(stpc->msg_age);
c37a2dfa
JP
95 if (NF_INVF(info, EBT_STP_MSGAGE,
96 v16 < c->msg_agel || v16 > c->msg_ageu))
8cc784ee 97 return false;
1da177e4
LT
98 }
99 if (info->bitmask & EBT_STP_MAXAGE) {
100 v16 = NR16(stpc->max_age);
c37a2dfa
JP
101 if (NF_INVF(info, EBT_STP_MAXAGE,
102 v16 < c->max_agel || v16 > c->max_ageu))
8cc784ee 103 return false;
1da177e4
LT
104 }
105 if (info->bitmask & EBT_STP_HELLOTIME) {
106 v16 = NR16(stpc->hello_time);
c37a2dfa
JP
107 if (NF_INVF(info, EBT_STP_HELLOTIME,
108 v16 < c->hello_timel || v16 > c->hello_timeu))
8cc784ee 109 return false;
1da177e4
LT
110 }
111 if (info->bitmask & EBT_STP_FWDD) {
112 v16 = NR16(stpc->forward_delay);
c37a2dfa
JP
113 if (NF_INVF(info, EBT_STP_FWDD,
114 v16 < c->forward_delayl || v16 > c->forward_delayu))
8cc784ee 115 return false;
1da177e4 116 }
8cc784ee 117 return true;
1da177e4
LT
118}
119
2d06d4a5 120static bool
62fc8051 121ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 122{
f7108a20 123 const struct ebt_stp_info *info = par->matchinfo;
abfdf1c4
JE
124 const struct stp_header *sp;
125 struct stp_header _stph;
402f9030 126 const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
1da177e4
LT
127
128 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
129 if (sp == NULL)
8cc784ee 130 return false;
1da177e4
LT
131
132 /* The stp code only considers these */
133 if (memcmp(sp, header, sizeof(header)))
8cc784ee 134 return false;
1da177e4 135
c37a2dfa
JP
136 if ((info->bitmask & EBT_STP_TYPE) &&
137 NF_INVF(info, EBT_STP_TYPE, info->type != sp->type))
8cc784ee 138 return false;
1da177e4
LT
139
140 if (sp->type == BPDU_TYPE_CONFIG &&
141 info->bitmask & EBT_STP_CONFIG_MASK) {
abfdf1c4
JE
142 const struct stp_config_pdu *st;
143 struct stp_config_pdu _stpc;
1da177e4
LT
144
145 st = skb_header_pointer(skb, sizeof(_stph),
146 sizeof(_stpc), &_stpc);
147 if (st == NULL)
8cc784ee 148 return false;
1da177e4
LT
149 return ebt_filter_config(info, st);
150 }
8cc784ee 151 return true;
1da177e4
LT
152}
153
b0f38452 154static int ebt_stp_mt_check(const struct xt_mtchk_param *par)
1da177e4 155{
9b4fce7a 156 const struct ebt_stp_info *info = par->matchinfo;
9b4fce7a 157 const struct ebt_entry *e = par->entryinfo;
1da177e4
LT
158
159 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
160 !(info->bitmask & EBT_STP_MASK))
bd414ee6 161 return -EINVAL;
1da177e4 162 /* Make sure the match only receives stp frames */
55917a21 163 if (!par->nft_compat &&
9124a20d 164 (!ether_addr_equal(e->destmac, eth_stp_addr) ||
a4995684
SH
165 !(e->bitmask & EBT_DESTMAC) ||
166 !is_broadcast_ether_addr(e->destmsk)))
bd414ee6 167 return -EINVAL;
1da177e4 168
bd414ee6 169 return 0;
1da177e4
LT
170}
171
043ef46c
JE
172static struct xt_match ebt_stp_mt_reg __read_mostly = {
173 .name = "stp",
001a18d3
JE
174 .revision = 0,
175 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
176 .match = ebt_stp_mt,
177 .checkentry = ebt_stp_mt_check,
fc0e3df4 178 .matchsize = sizeof(struct ebt_stp_info),
1da177e4
LT
179 .me = THIS_MODULE,
180};
181
65b4b4e8 182static int __init ebt_stp_init(void)
1da177e4 183{
043ef46c 184 return xt_register_match(&ebt_stp_mt_reg);
1da177e4
LT
185}
186
65b4b4e8 187static void __exit ebt_stp_fini(void)
1da177e4 188{
043ef46c 189 xt_unregister_match(&ebt_stp_mt_reg);
1da177e4
LT
190}
191
65b4b4e8
AM
192module_init(ebt_stp_init);
193module_exit(ebt_stp_fini);
f776c4cd 194MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
1da177e4 195MODULE_LICENSE("GPL");