blob: b4add9ea8964a9a83ac50d73ff4d13fac2638508 [file] [log] [blame]
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02001// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2020, Nikolay Aleksandrov <nikolay@cumulusnetworks.com>
3#include <linux/kernel.h>
4#include <linux/netdevice.h>
5#include <linux/rtnetlink.h>
6#include <linux/slab.h>
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +02007#include <net/ip_tunnels.h>
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +02008
9#include "br_private.h"
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020010#include "br_private_tunnel.h"
11
12static bool __vlan_tun_put(struct sk_buff *skb, const struct net_bridge_vlan *v)
13{
14 __be32 tid = tunnel_id_to_key32(v->tinfo.tunnel_id);
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +020015 struct nlattr *nest;
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020016
17 if (!v->tinfo.tunnel_dst)
18 return true;
19
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +020020 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_TUNNEL_INFO);
21 if (!nest)
22 return false;
23 if (nla_put_u32(skb, BRIDGE_VLANDB_TINFO_ID, be32_to_cpu(tid))) {
24 nla_nest_cancel(skb, nest);
25 return false;
26 }
27 nla_nest_end(skb, nest);
28
29 return true;
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020030}
31
32static bool __vlan_tun_can_enter_range(const struct net_bridge_vlan *v_curr,
33 const struct net_bridge_vlan *range_end)
34{
35 return (!v_curr->tinfo.tunnel_dst && !range_end->tinfo.tunnel_dst) ||
36 vlan_tunid_inrange(v_curr, range_end);
37}
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +020038
Nikolay Aleksandrov99f7c5e2020-03-17 14:08:33 +020039/* check if the options' state of v_curr allow it to enter the range */
40bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
41 const struct net_bridge_vlan *range_end)
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +020042{
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020043 return v_curr->state == range_end->state &&
44 __vlan_tun_can_enter_range(v_curr, range_end);
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +020045}
46
47bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v)
48{
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020049 return !nla_put_u8(skb, BRIDGE_VLANDB_ENTRY_STATE,
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020050 br_vlan_get_state(v)) &&
51 __vlan_tun_put(skb, v);
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +020052}
53
54size_t br_vlan_opts_nl_size(void)
55{
Nikolay Aleksandrov188c67d2020-03-17 14:08:35 +020056 return nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_ENTRY_STATE */
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +020057 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY_TUNNEL_INFO */
58 + nla_total_size(sizeof(u32)); /* BRIDGE_VLANDB_TINFO_ID */
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020059}
60
61static int br_vlan_modify_state(struct net_bridge_vlan_group *vg,
62 struct net_bridge_vlan *v,
63 u8 state,
64 bool *changed,
65 struct netlink_ext_ack *extack)
66{
67 struct net_bridge *br;
68
69 ASSERT_RTNL();
70
71 if (state > BR_STATE_BLOCKING) {
72 NL_SET_ERR_MSG_MOD(extack, "Invalid vlan state");
73 return -EINVAL;
74 }
75
76 if (br_vlan_is_brentry(v))
77 br = v->br;
78 else
79 br = v->port->br;
80
81 if (br->stp_enabled == BR_KERNEL_STP) {
82 NL_SET_ERR_MSG_MOD(extack, "Can't modify vlan state when using kernel STP");
83 return -EBUSY;
84 }
85
86 if (v->state == state)
87 return 0;
88
89 if (v->vid == br_get_pvid(vg))
90 br_vlan_set_pvid_state(vg, state);
91
92 br_vlan_set_state(v, state);
93 *changed = true;
94
Nikolay Aleksandrov7a53e712020-01-24 13:40:20 +020095 return 0;
96}
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +020097
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +020098static const struct nla_policy br_vlandb_tinfo_pol[BRIDGE_VLANDB_TINFO_MAX + 1] = {
99 [BRIDGE_VLANDB_TINFO_ID] = { .type = NLA_U32 },
Nikolay Aleksandrovc4437582020-03-20 13:23:03 +0200100 [BRIDGE_VLANDB_TINFO_CMD] = { .type = NLA_U32 },
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +0200101};
102
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200103static int br_vlan_modify_tunnel(const struct net_bridge_port *p,
104 struct net_bridge_vlan *v,
105 struct nlattr **tb,
106 bool *changed,
107 struct netlink_ext_ack *extack)
108{
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +0200109 struct nlattr *tun_tb[BRIDGE_VLANDB_TINFO_MAX + 1], *attr;
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200110 struct bridge_vlan_info *vinfo;
Nikolay Aleksandrovc4437582020-03-20 13:23:03 +0200111 u32 tun_id = 0;
112 int cmd, err;
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200113
114 if (!p) {
115 NL_SET_ERR_MSG_MOD(extack, "Can't modify tunnel mapping of non-port vlans");
116 return -EINVAL;
117 }
118 if (!(p->flags & BR_VLAN_TUNNEL)) {
119 NL_SET_ERR_MSG_MOD(extack, "Port doesn't have tunnel flag set");
120 return -EINVAL;
121 }
122
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +0200123 attr = tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO];
124 err = nla_parse_nested(tun_tb, BRIDGE_VLANDB_TINFO_MAX, attr,
125 br_vlandb_tinfo_pol, extack);
126 if (err)
127 return err;
128
Nikolay Aleksandrovc4437582020-03-20 13:23:03 +0200129 if (!tun_tb[BRIDGE_VLANDB_TINFO_CMD]) {
130 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel command attribute");
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +0200131 return -ENOENT;
132 }
Nikolay Aleksandrovc4437582020-03-20 13:23:03 +0200133 cmd = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_CMD]);
134 switch (cmd) {
135 case RTM_SETLINK:
136 if (!tun_tb[BRIDGE_VLANDB_TINFO_ID]) {
137 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel id attribute");
138 return -ENOENT;
139 }
140 /* when working on vlan ranges this is the starting tunnel id */
141 tun_id = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_ID]);
142 /* vlan info attr is guaranteed by br_vlan_rtm_process_one */
143 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
144 /* tunnel ids are mapped to each vlan in increasing order,
145 * the starting vlan is in BRIDGE_VLANDB_ENTRY_INFO and v is the
146 * current vlan, so we compute: tun_id + v - vinfo->vid
147 */
148 tun_id += v->vid - vinfo->vid;
149 break;
150 case RTM_DELLINK:
151 break;
152 default:
153 NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel command");
154 return -EINVAL;
155 }
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200156
Nikolay Aleksandrovc4437582020-03-20 13:23:03 +0200157 return br_vlan_tunnel_info(p, cmd, v->vid, tun_id, changed);
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200158}
159
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +0200160static int br_vlan_process_one_opts(const struct net_bridge *br,
161 const struct net_bridge_port *p,
162 struct net_bridge_vlan_group *vg,
163 struct net_bridge_vlan *v,
164 struct nlattr **tb,
165 bool *changed,
166 struct netlink_ext_ack *extack)
167{
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200168 int err;
169
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +0200170 *changed = false;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200171 if (tb[BRIDGE_VLANDB_ENTRY_STATE]) {
172 u8 state = nla_get_u8(tb[BRIDGE_VLANDB_ENTRY_STATE]);
173
174 err = br_vlan_modify_state(vg, v, state, changed, extack);
175 if (err)
176 return err;
177 }
Nikolay Aleksandrovfa388f22020-03-20 13:23:02 +0200178 if (tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO]) {
Nikolay Aleksandrov569da082020-03-17 14:08:36 +0200179 err = br_vlan_modify_tunnel(p, v, tb, changed, extack);
180 if (err)
181 return err;
182 }
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200183
Nikolay Aleksandrova5d29ae2020-01-24 13:40:21 +0200184 return 0;
185}
186
187int br_vlan_process_options(const struct net_bridge *br,
188 const struct net_bridge_port *p,
189 struct net_bridge_vlan *range_start,
190 struct net_bridge_vlan *range_end,
191 struct nlattr **tb,
192 struct netlink_ext_ack *extack)
193{
194 struct net_bridge_vlan *v, *curr_start = NULL, *curr_end = NULL;
195 struct net_bridge_vlan_group *vg;
196 int vid, err = 0;
197 u16 pvid;
198
199 if (p)
200 vg = nbp_vlan_group(p);
201 else
202 vg = br_vlan_group(br);
203
204 if (!range_start || !br_vlan_should_use(range_start)) {
205 NL_SET_ERR_MSG_MOD(extack, "Vlan range start doesn't exist, can't process options");
206 return -ENOENT;
207 }
208 if (!range_end || !br_vlan_should_use(range_end)) {
209 NL_SET_ERR_MSG_MOD(extack, "Vlan range end doesn't exist, can't process options");
210 return -ENOENT;
211 }
212
213 pvid = br_get_pvid(vg);
214 for (vid = range_start->vid; vid <= range_end->vid; vid++) {
215 bool changed = false;
216
217 v = br_vlan_find(vg, vid);
218 if (!v || !br_vlan_should_use(v)) {
219 NL_SET_ERR_MSG_MOD(extack, "Vlan in range doesn't exist, can't process options");
220 err = -ENOENT;
221 break;
222 }
223
224 err = br_vlan_process_one_opts(br, p, vg, v, tb, &changed,
225 extack);
226 if (err)
227 break;
228
229 if (changed) {
230 /* vlan options changed, check for range */
231 if (!curr_start) {
232 curr_start = v;
233 curr_end = v;
234 continue;
235 }
236
237 if (v->vid == pvid ||
238 !br_vlan_can_enter_range(v, curr_end)) {
239 br_vlan_notify(br, p, curr_start->vid,
240 curr_end->vid, RTM_NEWVLAN);
241 curr_start = v;
242 }
243 curr_end = v;
244 } else {
245 /* nothing changed and nothing to notify yet */
246 if (!curr_start)
247 continue;
248
249 br_vlan_notify(br, p, curr_start->vid, curr_end->vid,
250 RTM_NEWVLAN);
251 curr_start = NULL;
252 curr_end = NULL;
253 }
254 }
255 if (curr_start)
256 br_vlan_notify(br, p, curr_start->vid, curr_end->vid,
257 RTM_NEWVLAN);
258
259 return err;
260}