]>
Commit | Line | Data |
---|---|---|
07b5b17e PM |
1 | /* |
2 | * VLAN netlink control interface | |
3 | * | |
4 | * Copyright (c) 2007 Patrick McHardy <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * version 2 as published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/netdevice.h> | |
13 | #include <linux/if_vlan.h> | |
14 | #include <net/netlink.h> | |
15 | #include <net/rtnetlink.h> | |
16 | #include "vlan.h" | |
17 | ||
18 | ||
19 | static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = { | |
20 | [IFLA_VLAN_ID] = { .type = NLA_U16 }, | |
21 | [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) }, | |
22 | [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED }, | |
23 | [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED }, | |
24 | }; | |
25 | ||
26 | static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = { | |
27 | [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) }, | |
28 | }; | |
29 | ||
30 | ||
31 | static inline int vlan_validate_qos_map(struct nlattr *attr) | |
32 | { | |
33 | if (!attr) | |
34 | return 0; | |
35 | return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy); | |
36 | } | |
37 | ||
38 | static int vlan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
39 | { | |
40 | struct ifla_vlan_flags *flags; | |
41 | u16 id; | |
42 | int err; | |
43 | ||
0e06877c PM |
44 | if (tb[IFLA_ADDRESS]) { |
45 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
46 | return -EINVAL; | |
47 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
48 | return -EADDRNOTAVAIL; | |
49 | } | |
50 | ||
07b5b17e PM |
51 | if (!data) |
52 | return -EINVAL; | |
53 | ||
54 | if (data[IFLA_VLAN_ID]) { | |
55 | id = nla_get_u16(data[IFLA_VLAN_ID]); | |
56 | if (id >= VLAN_VID_MASK) | |
57 | return -ERANGE; | |
58 | } | |
59 | if (data[IFLA_VLAN_FLAGS]) { | |
60 | flags = nla_data(data[IFLA_VLAN_FLAGS]); | |
61 | if ((flags->flags & flags->mask) & ~VLAN_FLAG_REORDER_HDR) | |
62 | return -EINVAL; | |
63 | } | |
64 | ||
65 | err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]); | |
66 | if (err < 0) | |
67 | return err; | |
68 | err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]); | |
69 | if (err < 0) | |
70 | return err; | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static int vlan_changelink(struct net_device *dev, | |
75 | struct nlattr *tb[], struct nlattr *data[]) | |
76 | { | |
77 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); | |
78 | struct ifla_vlan_flags *flags; | |
79 | struct ifla_vlan_qos_mapping *m; | |
80 | struct nlattr *attr; | |
81 | int rem; | |
82 | ||
83 | if (data[IFLA_VLAN_FLAGS]) { | |
84 | flags = nla_data(data[IFLA_VLAN_FLAGS]); | |
85 | vlan->flags = (vlan->flags & ~flags->mask) | | |
86 | (flags->flags & flags->mask); | |
87 | } | |
88 | if (data[IFLA_VLAN_INGRESS_QOS]) { | |
89 | nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) { | |
90 | m = nla_data(attr); | |
91 | vlan_dev_set_ingress_priority(dev, m->to, m->from); | |
92 | } | |
93 | } | |
94 | if (data[IFLA_VLAN_EGRESS_QOS]) { | |
95 | nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) { | |
96 | m = nla_data(attr); | |
97 | vlan_dev_set_egress_priority(dev, m->from, m->to); | |
98 | } | |
99 | } | |
100 | return 0; | |
101 | } | |
102 | ||
103 | static int vlan_newlink(struct net_device *dev, | |
104 | struct nlattr *tb[], struct nlattr *data[]) | |
105 | { | |
106 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); | |
107 | struct net_device *real_dev; | |
108 | int err; | |
109 | ||
110 | if (!data[IFLA_VLAN_ID]) | |
111 | return -EINVAL; | |
112 | ||
113 | if (!tb[IFLA_LINK]) | |
114 | return -EINVAL; | |
115 | real_dev = __dev_get_by_index(nla_get_u32(tb[IFLA_LINK])); | |
116 | if (!real_dev) | |
117 | return -ENODEV; | |
118 | ||
119 | vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]); | |
120 | vlan->real_dev = real_dev; | |
121 | vlan->flags = VLAN_FLAG_REORDER_HDR; | |
122 | ||
123 | err = vlan_check_real_dev(real_dev, vlan->vlan_id); | |
124 | if (err < 0) | |
125 | return err; | |
126 | ||
127 | if (!tb[IFLA_MTU]) | |
128 | dev->mtu = real_dev->mtu; | |
129 | else if (dev->mtu > real_dev->mtu) | |
130 | return -EINVAL; | |
131 | ||
132 | err = vlan_changelink(dev, tb, data); | |
133 | if (err < 0) | |
134 | return err; | |
135 | ||
136 | return register_vlan_dev(dev); | |
137 | } | |
138 | ||
139 | static void vlan_dellink(struct net_device *dev) | |
140 | { | |
141 | unregister_vlan_device(dev); | |
142 | } | |
143 | ||
144 | static inline size_t vlan_qos_map_size(unsigned int n) | |
145 | { | |
146 | if (n == 0) | |
147 | return 0; | |
148 | /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */ | |
149 | return nla_total_size(sizeof(struct nlattr)) + | |
150 | nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n; | |
151 | } | |
152 | ||
153 | static size_t vlan_get_size(const struct net_device *dev) | |
154 | { | |
155 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); | |
156 | ||
157 | return nla_total_size(2) + /* IFLA_VLAN_ID */ | |
158 | vlan_qos_map_size(vlan->nr_ingress_mappings) + | |
159 | vlan_qos_map_size(vlan->nr_egress_mappings); | |
160 | } | |
161 | ||
162 | static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
163 | { | |
164 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); | |
165 | struct vlan_priority_tci_mapping *pm; | |
166 | struct ifla_vlan_flags f; | |
167 | struct ifla_vlan_qos_mapping m; | |
168 | struct nlattr *nest; | |
169 | unsigned int i; | |
170 | ||
171 | NLA_PUT_U16(skb, IFLA_VLAN_ID, VLAN_DEV_INFO(dev)->vlan_id); | |
172 | if (vlan->flags) { | |
173 | f.flags = vlan->flags; | |
174 | f.mask = ~0; | |
175 | NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f); | |
176 | } | |
177 | if (vlan->nr_ingress_mappings) { | |
178 | nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS); | |
179 | if (nest == NULL) | |
180 | goto nla_put_failure; | |
181 | ||
182 | for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) { | |
183 | if (!vlan->ingress_priority_map[i]) | |
184 | continue; | |
185 | ||
186 | m.from = i; | |
187 | m.to = vlan->ingress_priority_map[i]; | |
188 | NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING, | |
189 | sizeof(m), &m); | |
190 | } | |
191 | nla_nest_end(skb, nest); | |
192 | } | |
193 | ||
194 | if (vlan->nr_egress_mappings) { | |
195 | nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS); | |
196 | if (nest == NULL) | |
197 | goto nla_put_failure; | |
198 | ||
199 | for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { | |
200 | for (pm = vlan->egress_priority_map[i]; pm; | |
201 | pm = pm->next) { | |
202 | if (!pm->vlan_qos) | |
203 | continue; | |
204 | ||
205 | m.from = pm->priority; | |
206 | m.to = (pm->vlan_qos >> 13) & 0x7; | |
207 | NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING, | |
208 | sizeof(m), &m); | |
209 | } | |
210 | } | |
211 | nla_nest_end(skb, nest); | |
212 | } | |
213 | return 0; | |
214 | ||
215 | nla_put_failure: | |
216 | return -EMSGSIZE; | |
217 | } | |
218 | ||
219 | struct rtnl_link_ops vlan_link_ops __read_mostly = { | |
220 | .kind = "vlan", | |
221 | .maxtype = IFLA_VLAN_MAX, | |
222 | .policy = vlan_policy, | |
223 | .priv_size = sizeof(struct vlan_dev_info), | |
224 | .setup = vlan_setup, | |
225 | .validate = vlan_validate, | |
226 | .newlink = vlan_newlink, | |
227 | .changelink = vlan_changelink, | |
228 | .dellink = vlan_dellink, | |
229 | .get_size = vlan_get_size, | |
230 | .fill_info = vlan_fill_info, | |
231 | }; | |
232 | ||
233 | int __init vlan_netlink_init(void) | |
234 | { | |
235 | return rtnl_link_register(&vlan_link_ops); | |
236 | } | |
237 | ||
238 | void __exit vlan_netlink_fini(void) | |
239 | { | |
240 | rtnl_link_unregister(&vlan_link_ops); | |
241 | } | |
242 | ||
243 | MODULE_ALIAS_RTNL_LINK("vlan"); |