]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2a8cc6c8 YH |
2 | /* |
3 | * IPv6 Address Label subsystem | |
4 | * for the IPv6 "Default" Source Address Selection | |
5 | * | |
6 | * Copyright (C)2007 USAGI/WIDE Project | |
7 | */ | |
8 | /* | |
9 | * Author: | |
1c8669e0 | 10 | * YOSHIFUJI Hideaki @ USAGI/WIDE Project <[email protected]> |
2a8cc6c8 YH |
11 | */ |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/list.h> | |
15 | #include <linux/rcupdate.h> | |
16 | #include <linux/in6.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
2a8cc6c8 YH |
18 | #include <net/addrconf.h> |
19 | #include <linux/if_addrlabel.h> | |
20 | #include <linux/netlink.h> | |
21 | #include <linux/rtnetlink.h> | |
22 | ||
23 | #if 0 | |
24 | #define ADDRLABEL(x...) printk(x) | |
25 | #else | |
1c8669e0 | 26 | #define ADDRLABEL(x...) do { ; } while (0) |
2a8cc6c8 YH |
27 | #endif |
28 | ||
29 | /* | |
30 | * Policy Table | |
31 | */ | |
22b285d6 | 32 | struct ip6addrlbl_entry { |
2a8cc6c8 YH |
33 | struct in6_addr prefix; |
34 | int prefixlen; | |
35 | int ifindex; | |
36 | int addrtype; | |
37 | u32 label; | |
38 | struct hlist_node list; | |
2a8cc6c8 YH |
39 | struct rcu_head rcu; |
40 | }; | |
41 | ||
2a8cc6c8 | 42 | /* |
417962a0 | 43 | * Default policy table (RFC6724 + extensions) |
2a8cc6c8 YH |
44 | * |
45 | * prefix addr_type label | |
46 | * ------------------------------------------------------------------------- | |
47 | * ::1/128 LOOPBACK 0 | |
48 | * ::/0 N/A 1 | |
49 | * 2002::/16 N/A 2 | |
50 | * ::/96 COMPATv4 3 | |
51 | * ::ffff:0:0/96 V4MAPPED 4 | |
52 | * fc00::/7 N/A 5 ULA (RFC 4193) | |
53 | * 2001::/32 N/A 6 Teredo (RFC 4380) | |
5fe47b8a | 54 | * 2001:10::/28 N/A 7 ORCHID (RFC 4843) |
417962a0 YH |
55 | * fec0::/10 N/A 11 Site-local |
56 | * (deprecated by RFC3879) | |
57 | * 3ffe::/16 N/A 12 6bone | |
2a8cc6c8 YH |
58 | * |
59 | * Note: 0xffffffff is used if we do not have any policies. | |
417962a0 | 60 | * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724. |
2a8cc6c8 YH |
61 | */ |
62 | ||
63 | #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL | |
64 | ||
04a6f82c | 65 | static const __net_initconst struct ip6addrlbl_init_table |
2a8cc6c8 YH |
66 | { |
67 | const struct in6_addr *prefix; | |
68 | int prefixlen; | |
69 | u32 label; | |
70 | } ip6addrlbl_init_table[] = { | |
71 | { /* ::/0 */ | |
72 | .prefix = &in6addr_any, | |
73 | .label = 1, | |
1c8669e0 WY |
74 | }, { /* fc00::/7 */ |
75 | .prefix = &(struct in6_addr){ { { 0xfc } } } , | |
2a8cc6c8 YH |
76 | .prefixlen = 7, |
77 | .label = 5, | |
1c8669e0 WY |
78 | }, { /* fec0::/10 */ |
79 | .prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } }, | |
417962a0 YH |
80 | .prefixlen = 10, |
81 | .label = 11, | |
1c8669e0 WY |
82 | }, { /* 2002::/16 */ |
83 | .prefix = &(struct in6_addr){ { { 0x20, 0x02 } } }, | |
2a8cc6c8 YH |
84 | .prefixlen = 16, |
85 | .label = 2, | |
1c8669e0 WY |
86 | }, { /* 3ffe::/16 */ |
87 | .prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } }, | |
417962a0 YH |
88 | .prefixlen = 16, |
89 | .label = 12, | |
1c8669e0 WY |
90 | }, { /* 2001::/32 */ |
91 | .prefix = &(struct in6_addr){ { { 0x20, 0x01 } } }, | |
2a8cc6c8 YH |
92 | .prefixlen = 32, |
93 | .label = 6, | |
1c8669e0 WY |
94 | }, { /* 2001:10::/28 */ |
95 | .prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } }, | |
5fe47b8a JMT |
96 | .prefixlen = 28, |
97 | .label = 7, | |
1c8669e0 WY |
98 | }, { /* ::ffff:0:0 */ |
99 | .prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } }, | |
2a8cc6c8 YH |
100 | .prefixlen = 96, |
101 | .label = 4, | |
1c8669e0 | 102 | }, { /* ::/96 */ |
2a8cc6c8 YH |
103 | .prefix = &in6addr_any, |
104 | .prefixlen = 96, | |
105 | .label = 3, | |
1c8669e0 | 106 | }, { /* ::1/128 */ |
2a8cc6c8 YH |
107 | .prefix = &in6addr_loopback, |
108 | .prefixlen = 128, | |
109 | .label = 0, | |
110 | } | |
111 | }; | |
112 | ||
2a8cc6c8 | 113 | /* Find label */ |
a90c9347 | 114 | static bool __ip6addrlbl_match(const struct ip6addrlbl_entry *p, |
a50feda5 ED |
115 | const struct in6_addr *addr, |
116 | int addrtype, int ifindex) | |
2a8cc6c8 YH |
117 | { |
118 | if (p->ifindex && p->ifindex != ifindex) | |
a50feda5 | 119 | return false; |
2a8cc6c8 | 120 | if (p->addrtype && p->addrtype != addrtype) |
a50feda5 | 121 | return false; |
2a8cc6c8 | 122 | if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen)) |
a50feda5 ED |
123 | return false; |
124 | return true; | |
2a8cc6c8 YH |
125 | } |
126 | ||
3de23255 BT |
127 | static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net, |
128 | const struct in6_addr *addr, | |
2a8cc6c8 YH |
129 | int type, int ifindex) |
130 | { | |
2a8cc6c8 | 131 | struct ip6addrlbl_entry *p; |
a90c9347 ED |
132 | |
133 | hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) { | |
134 | if (__ip6addrlbl_match(p, addr, type, ifindex)) | |
2a8cc6c8 YH |
135 | return p; |
136 | } | |
137 | return NULL; | |
138 | } | |
139 | ||
3de23255 BT |
140 | u32 ipv6_addr_label(struct net *net, |
141 | const struct in6_addr *addr, int type, int ifindex) | |
2a8cc6c8 YH |
142 | { |
143 | u32 label; | |
144 | struct ip6addrlbl_entry *p; | |
145 | ||
146 | type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK; | |
147 | ||
148 | rcu_read_lock(); | |
3de23255 | 149 | p = __ipv6_addr_label(net, addr, type, ifindex); |
2a8cc6c8 YH |
150 | label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT; |
151 | rcu_read_unlock(); | |
152 | ||
5b095d98 | 153 | ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n", |
0c6ce78a | 154 | __func__, addr, type, ifindex, label); |
2a8cc6c8 YH |
155 | |
156 | return label; | |
157 | } | |
158 | ||
159 | /* allocate one entry */ | |
a90c9347 | 160 | static struct ip6addrlbl_entry *ip6addrlbl_alloc(const struct in6_addr *prefix, |
40fee36e YH |
161 | int prefixlen, int ifindex, |
162 | u32 label) | |
2a8cc6c8 YH |
163 | { |
164 | struct ip6addrlbl_entry *newp; | |
165 | int addrtype; | |
166 | ||
5b095d98 | 167 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n", |
0c6ce78a | 168 | __func__, prefix, prefixlen, ifindex, (unsigned int)label); |
2a8cc6c8 YH |
169 | |
170 | addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK); | |
171 | ||
172 | switch (addrtype) { | |
173 | case IPV6_ADDR_MAPPED: | |
174 | if (prefixlen > 96) | |
175 | return ERR_PTR(-EINVAL); | |
176 | if (prefixlen < 96) | |
177 | addrtype = 0; | |
178 | break; | |
179 | case IPV6_ADDR_COMPATv4: | |
180 | if (prefixlen != 96) | |
181 | addrtype = 0; | |
182 | break; | |
183 | case IPV6_ADDR_LOOPBACK: | |
184 | if (prefixlen != 128) | |
185 | addrtype = 0; | |
186 | break; | |
187 | } | |
188 | ||
189 | newp = kmalloc(sizeof(*newp), GFP_KERNEL); | |
190 | if (!newp) | |
191 | return ERR_PTR(-ENOMEM); | |
192 | ||
193 | ipv6_addr_prefix(&newp->prefix, prefix, prefixlen); | |
194 | newp->prefixlen = prefixlen; | |
195 | newp->ifindex = ifindex; | |
196 | newp->addrtype = addrtype; | |
197 | newp->label = label; | |
198 | INIT_HLIST_NODE(&newp->list); | |
2a8cc6c8 YH |
199 | return newp; |
200 | } | |
201 | ||
202 | /* add a label */ | |
a90c9347 ED |
203 | static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp, |
204 | int replace) | |
2a8cc6c8 | 205 | { |
639739b5 | 206 | struct ip6addrlbl_entry *last = NULL, *p = NULL; |
a90c9347 | 207 | struct hlist_node *n; |
2a8cc6c8 YH |
208 | int ret = 0; |
209 | ||
639739b5 HFS |
210 | ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp, |
211 | replace); | |
2a8cc6c8 | 212 | |
a90c9347 | 213 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
639739b5 | 214 | if (p->prefixlen == newp->prefixlen && |
639739b5 HFS |
215 | p->ifindex == newp->ifindex && |
216 | ipv6_addr_equal(&p->prefix, &newp->prefix)) { | |
217 | if (!replace) { | |
218 | ret = -EEXIST; | |
2a8cc6c8 YH |
219 | goto out; |
220 | } | |
639739b5 | 221 | hlist_replace_rcu(&p->list, &newp->list); |
2809c095 | 222 | kfree_rcu(p, rcu); |
639739b5 HFS |
223 | goto out; |
224 | } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) || | |
225 | (p->prefixlen < newp->prefixlen)) { | |
226 | hlist_add_before_rcu(&newp->list, &p->list); | |
227 | goto out; | |
2a8cc6c8 | 228 | } |
639739b5 | 229 | last = p; |
2a8cc6c8 | 230 | } |
639739b5 | 231 | if (last) |
1d023284 | 232 | hlist_add_behind_rcu(&newp->list, &last->list); |
639739b5 | 233 | else |
a90c9347 | 234 | hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head); |
2a8cc6c8 YH |
235 | out: |
236 | if (!ret) | |
eec53cc3 ED |
237 | WRITE_ONCE(net->ipv6.ip6addrlbl_table.seq, |
238 | net->ipv6.ip6addrlbl_table.seq + 1); | |
2a8cc6c8 YH |
239 | return ret; |
240 | } | |
241 | ||
242 | /* add a label */ | |
3de23255 BT |
243 | static int ip6addrlbl_add(struct net *net, |
244 | const struct in6_addr *prefix, int prefixlen, | |
40fee36e | 245 | int ifindex, u32 label, int replace) |
2a8cc6c8 YH |
246 | { |
247 | struct ip6addrlbl_entry *newp; | |
248 | int ret = 0; | |
249 | ||
5b095d98 | 250 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n", |
0c6ce78a HH |
251 | __func__, prefix, prefixlen, ifindex, (unsigned int)label, |
252 | replace); | |
2a8cc6c8 | 253 | |
a90c9347 | 254 | newp = ip6addrlbl_alloc(prefix, prefixlen, ifindex, label); |
2a8cc6c8 YH |
255 | if (IS_ERR(newp)) |
256 | return PTR_ERR(newp); | |
a90c9347 ED |
257 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
258 | ret = __ip6addrlbl_add(net, newp, replace); | |
259 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); | |
2a8cc6c8 | 260 | if (ret) |
2809c095 | 261 | kfree(newp); |
2a8cc6c8 YH |
262 | return ret; |
263 | } | |
264 | ||
265 | /* remove a label */ | |
3de23255 BT |
266 | static int __ip6addrlbl_del(struct net *net, |
267 | const struct in6_addr *prefix, int prefixlen, | |
40fee36e | 268 | int ifindex) |
2a8cc6c8 YH |
269 | { |
270 | struct ip6addrlbl_entry *p = NULL; | |
b67bfe0d | 271 | struct hlist_node *n; |
2a8cc6c8 YH |
272 | int ret = -ESRCH; |
273 | ||
5b095d98 | 274 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n", |
0c6ce78a | 275 | __func__, prefix, prefixlen, ifindex); |
2a8cc6c8 | 276 | |
a90c9347 | 277 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
2a8cc6c8 YH |
278 | if (p->prefixlen == prefixlen && |
279 | p->ifindex == ifindex && | |
280 | ipv6_addr_equal(&p->prefix, prefix)) { | |
281 | hlist_del_rcu(&p->list); | |
2809c095 | 282 | kfree_rcu(p, rcu); |
2a8cc6c8 YH |
283 | ret = 0; |
284 | break; | |
285 | } | |
286 | } | |
287 | return ret; | |
288 | } | |
289 | ||
3de23255 BT |
290 | static int ip6addrlbl_del(struct net *net, |
291 | const struct in6_addr *prefix, int prefixlen, | |
40fee36e | 292 | int ifindex) |
2a8cc6c8 YH |
293 | { |
294 | struct in6_addr prefix_buf; | |
295 | int ret; | |
296 | ||
5b095d98 | 297 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n", |
0c6ce78a | 298 | __func__, prefix, prefixlen, ifindex); |
2a8cc6c8 YH |
299 | |
300 | ipv6_addr_prefix(&prefix_buf, prefix, prefixlen); | |
a90c9347 | 301 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
3de23255 | 302 | ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex); |
a90c9347 | 303 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); |
2a8cc6c8 YH |
304 | return ret; |
305 | } | |
306 | ||
307 | /* add default label */ | |
3de23255 | 308 | static int __net_init ip6addrlbl_net_init(struct net *net) |
2a8cc6c8 | 309 | { |
e255e11e WH |
310 | struct ip6addrlbl_entry *p = NULL; |
311 | struct hlist_node *n; | |
312 | int err; | |
2a8cc6c8 YH |
313 | int i; |
314 | ||
f3213831 | 315 | ADDRLABEL(KERN_DEBUG "%s\n", __func__); |
2a8cc6c8 | 316 | |
a90c9347 ED |
317 | spin_lock_init(&net->ipv6.ip6addrlbl_table.lock); |
318 | INIT_HLIST_HEAD(&net->ipv6.ip6addrlbl_table.head); | |
319 | ||
2a8cc6c8 | 320 | for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) { |
e255e11e WH |
321 | err = ip6addrlbl_add(net, |
322 | ip6addrlbl_init_table[i].prefix, | |
323 | ip6addrlbl_init_table[i].prefixlen, | |
324 | 0, | |
325 | ip6addrlbl_init_table[i].label, 0); | |
326 | if (err) | |
327 | goto err_ip6addrlbl_add; | |
328 | } | |
329 | return 0; | |
330 | ||
331 | err_ip6addrlbl_add: | |
332 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { | |
333 | hlist_del_rcu(&p->list); | |
334 | kfree_rcu(p, rcu); | |
2a8cc6c8 YH |
335 | } |
336 | return err; | |
337 | } | |
338 | ||
3de23255 BT |
339 | static void __net_exit ip6addrlbl_net_exit(struct net *net) |
340 | { | |
341 | struct ip6addrlbl_entry *p = NULL; | |
b67bfe0d | 342 | struct hlist_node *n; |
3de23255 BT |
343 | |
344 | /* Remove all labels belonging to the exiting net */ | |
a90c9347 ED |
345 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
346 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { | |
347 | hlist_del_rcu(&p->list); | |
2809c095 | 348 | kfree_rcu(p, rcu); |
3de23255 | 349 | } |
a90c9347 | 350 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); |
3de23255 BT |
351 | } |
352 | ||
353 | static struct pernet_operations ipv6_addr_label_ops = { | |
354 | .init = ip6addrlbl_net_init, | |
355 | .exit = ip6addrlbl_net_exit, | |
356 | }; | |
357 | ||
2a8cc6c8 YH |
358 | int __init ipv6_addr_label_init(void) |
359 | { | |
3de23255 | 360 | return register_pernet_subsys(&ipv6_addr_label_ops); |
2a8cc6c8 YH |
361 | } |
362 | ||
2cc6d2bf NH |
363 | void ipv6_addr_label_cleanup(void) |
364 | { | |
365 | unregister_pernet_subsys(&ipv6_addr_label_ops); | |
366 | } | |
367 | ||
2a8cc6c8 YH |
368 | static const struct nla_policy ifal_policy[IFAL_MAX+1] = { |
369 | [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), }, | |
370 | [IFAL_LABEL] = { .len = sizeof(u32), }, | |
371 | }; | |
372 | ||
a6f57028 FW |
373 | static bool addrlbl_ifindex_exists(struct net *net, int ifindex) |
374 | { | |
375 | ||
376 | struct net_device *dev; | |
377 | ||
378 | rcu_read_lock(); | |
379 | dev = dev_get_by_index_rcu(net, ifindex); | |
380 | rcu_read_unlock(); | |
381 | ||
382 | return dev != NULL; | |
383 | } | |
384 | ||
c21ef3e3 DA |
385 | static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh, |
386 | struct netlink_ext_ack *extack) | |
2a8cc6c8 | 387 | { |
3b1e0a65 | 388 | struct net *net = sock_net(skb->sk); |
2a8cc6c8 YH |
389 | struct ifaddrlblmsg *ifal; |
390 | struct nlattr *tb[IFAL_MAX+1]; | |
391 | struct in6_addr *pfx; | |
392 | u32 label; | |
393 | int err = 0; | |
394 | ||
8cb08174 JB |
395 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb, IFAL_MAX, |
396 | ifal_policy, extack); | |
2a8cc6c8 YH |
397 | if (err < 0) |
398 | return err; | |
399 | ||
400 | ifal = nlmsg_data(nlh); | |
401 | ||
402 | if (ifal->ifal_family != AF_INET6 || | |
403 | ifal->ifal_prefixlen > 128) | |
404 | return -EINVAL; | |
405 | ||
2a8cc6c8 YH |
406 | if (!tb[IFAL_ADDRESS]) |
407 | return -EINVAL; | |
2a8cc6c8 | 408 | pfx = nla_data(tb[IFAL_ADDRESS]); |
2a8cc6c8 YH |
409 | |
410 | if (!tb[IFAL_LABEL]) | |
411 | return -EINVAL; | |
412 | label = nla_get_u32(tb[IFAL_LABEL]); | |
413 | if (label == IPV6_ADDR_LABEL_DEFAULT) | |
414 | return -EINVAL; | |
415 | ||
1c8669e0 | 416 | switch (nlh->nlmsg_type) { |
2a8cc6c8 | 417 | case RTM_NEWADDRLABEL: |
0771275b | 418 | if (ifal->ifal_index && |
a6f57028 | 419 | !addrlbl_ifindex_exists(net, ifal->ifal_index)) |
0771275b FW |
420 | return -EINVAL; |
421 | ||
3de23255 | 422 | err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen, |
2a8cc6c8 YH |
423 | ifal->ifal_index, label, |
424 | nlh->nlmsg_flags & NLM_F_REPLACE); | |
425 | break; | |
426 | case RTM_DELADDRLABEL: | |
3de23255 | 427 | err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen, |
2a8cc6c8 YH |
428 | ifal->ifal_index); |
429 | break; | |
430 | default: | |
431 | err = -EOPNOTSUPP; | |
432 | } | |
433 | return err; | |
434 | } | |
435 | ||
a50feda5 ED |
436 | static void ip6addrlbl_putmsg(struct nlmsghdr *nlh, |
437 | int prefixlen, int ifindex, u32 lseq) | |
2a8cc6c8 YH |
438 | { |
439 | struct ifaddrlblmsg *ifal = nlmsg_data(nlh); | |
440 | ifal->ifal_family = AF_INET6; | |
c23fb2c8 | 441 | ifal->__ifal_reserved = 0; |
2a8cc6c8 YH |
442 | ifal->ifal_prefixlen = prefixlen; |
443 | ifal->ifal_flags = 0; | |
444 | ifal->ifal_index = ifindex; | |
445 | ifal->ifal_seq = lseq; | |
446 | }; | |
447 | ||
448 | static int ip6addrlbl_fill(struct sk_buff *skb, | |
eec53cc3 | 449 | const struct ip6addrlbl_entry *p, |
2a8cc6c8 | 450 | u32 lseq, |
15e47304 | 451 | u32 portid, u32 seq, int event, |
2a8cc6c8 YH |
452 | unsigned int flags) |
453 | { | |
15e47304 | 454 | struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event, |
2a8cc6c8 YH |
455 | sizeof(struct ifaddrlblmsg), flags); |
456 | if (!nlh) | |
457 | return -EMSGSIZE; | |
458 | ||
459 | ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq); | |
460 | ||
930345ea | 461 | if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 || |
2a8cc6c8 YH |
462 | nla_put_u32(skb, IFAL_LABEL, p->label) < 0) { |
463 | nlmsg_cancel(skb, nlh); | |
464 | return -EMSGSIZE; | |
465 | } | |
466 | ||
053c095a JB |
467 | nlmsg_end(skb, nlh); |
468 | return 0; | |
2a8cc6c8 YH |
469 | } |
470 | ||
f2ae64bb DA |
471 | static int ip6addrlbl_valid_dump_req(const struct nlmsghdr *nlh, |
472 | struct netlink_ext_ack *extack) | |
473 | { | |
474 | struct ifaddrlblmsg *ifal; | |
475 | ||
476 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) { | |
477 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for address label dump request"); | |
478 | return -EINVAL; | |
479 | } | |
480 | ||
481 | ifal = nlmsg_data(nlh); | |
482 | if (ifal->__ifal_reserved || ifal->ifal_prefixlen || | |
483 | ifal->ifal_flags || ifal->ifal_index || ifal->ifal_seq) { | |
484 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address label dump request"); | |
485 | return -EINVAL; | |
486 | } | |
487 | ||
488 | if (nlmsg_attrlen(nlh, sizeof(*ifal))) { | |
d5f6db35 | 489 | NL_SET_ERR_MSG_MOD(extack, "Invalid data after header for address label dump request"); |
f2ae64bb DA |
490 | return -EINVAL; |
491 | } | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
2a8cc6c8 YH |
496 | static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb) |
497 | { | |
f2ae64bb | 498 | const struct nlmsghdr *nlh = cb->nlh; |
3b1e0a65 | 499 | struct net *net = sock_net(skb->sk); |
2a8cc6c8 | 500 | struct ip6addrlbl_entry *p; |
2a8cc6c8 | 501 | int idx = 0, s_idx = cb->args[0]; |
eec53cc3 ED |
502 | int err = 0; |
503 | u32 lseq; | |
2a8cc6c8 | 504 | |
f2ae64bb DA |
505 | if (cb->strict_check) { |
506 | err = ip6addrlbl_valid_dump_req(nlh, cb->extack); | |
507 | if (err < 0) | |
508 | return err; | |
509 | } | |
510 | ||
2a8cc6c8 | 511 | rcu_read_lock(); |
eec53cc3 | 512 | lseq = READ_ONCE(net->ipv6.ip6addrlbl_table.seq); |
a90c9347 ED |
513 | hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) { |
514 | if (idx >= s_idx) { | |
cb6e926e | 515 | err = ip6addrlbl_fill(skb, p, |
eec53cc3 | 516 | lseq, |
cb6e926e | 517 | NETLINK_CB(cb->skb).portid, |
f2ae64bb | 518 | nlh->nlmsg_seq, |
cb6e926e WY |
519 | RTM_NEWADDRLABEL, |
520 | NLM_F_MULTI); | |
053c095a | 521 | if (err < 0) |
2a8cc6c8 YH |
522 | break; |
523 | } | |
524 | idx++; | |
525 | } | |
526 | rcu_read_unlock(); | |
527 | cb->args[0] = idx; | |
eec53cc3 | 528 | return err; |
2a8cc6c8 YH |
529 | } |
530 | ||
531 | static inline int ip6addrlbl_msgsize(void) | |
532 | { | |
a02cec21 | 533 | return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg)) |
2a8cc6c8 | 534 | + nla_total_size(16) /* IFAL_ADDRESS */ |
a02cec21 | 535 | + nla_total_size(4); /* IFAL_LABEL */ |
2a8cc6c8 YH |
536 | } |
537 | ||
5912a775 JK |
538 | static int ip6addrlbl_valid_get_req(struct sk_buff *skb, |
539 | const struct nlmsghdr *nlh, | |
540 | struct nlattr **tb, | |
541 | struct netlink_ext_ack *extack) | |
542 | { | |
543 | struct ifaddrlblmsg *ifal; | |
544 | int i, err; | |
545 | ||
546 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) { | |
547 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for addrlabel get request"); | |
548 | return -EINVAL; | |
549 | } | |
550 | ||
551 | if (!netlink_strict_get_check(skb)) | |
8cb08174 JB |
552 | return nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb, |
553 | IFAL_MAX, ifal_policy, extack); | |
5912a775 JK |
554 | |
555 | ifal = nlmsg_data(nlh); | |
556 | if (ifal->__ifal_reserved || ifal->ifal_flags || ifal->ifal_seq) { | |
557 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for addrlabel get request"); | |
558 | return -EINVAL; | |
559 | } | |
560 | ||
8cb08174 JB |
561 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifal), tb, IFAL_MAX, |
562 | ifal_policy, extack); | |
5912a775 JK |
563 | if (err) |
564 | return err; | |
565 | ||
566 | for (i = 0; i <= IFAL_MAX; i++) { | |
567 | if (!tb[i]) | |
568 | continue; | |
569 | ||
570 | switch (i) { | |
571 | case IFAL_ADDRESS: | |
572 | break; | |
573 | default: | |
574 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in addrlabel get request"); | |
575 | return -EINVAL; | |
576 | } | |
577 | } | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
c21ef3e3 DA |
582 | static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
583 | struct netlink_ext_ack *extack) | |
2a8cc6c8 | 584 | { |
3b1e0a65 | 585 | struct net *net = sock_net(in_skb->sk); |
2a8cc6c8 YH |
586 | struct ifaddrlblmsg *ifal; |
587 | struct nlattr *tb[IFAL_MAX+1]; | |
588 | struct in6_addr *addr; | |
589 | u32 lseq; | |
590 | int err = 0; | |
591 | struct ip6addrlbl_entry *p; | |
592 | struct sk_buff *skb; | |
593 | ||
5912a775 | 594 | err = ip6addrlbl_valid_get_req(in_skb, nlh, tb, extack); |
2a8cc6c8 YH |
595 | if (err < 0) |
596 | return err; | |
597 | ||
598 | ifal = nlmsg_data(nlh); | |
599 | ||
600 | if (ifal->ifal_family != AF_INET6 || | |
601 | ifal->ifal_prefixlen != 128) | |
602 | return -EINVAL; | |
603 | ||
604 | if (ifal->ifal_index && | |
a6f57028 | 605 | !addrlbl_ifindex_exists(net, ifal->ifal_index)) |
2a8cc6c8 YH |
606 | return -EINVAL; |
607 | ||
608 | if (!tb[IFAL_ADDRESS]) | |
609 | return -EINVAL; | |
2a8cc6c8 | 610 | addr = nla_data(tb[IFAL_ADDRESS]); |
2a8cc6c8 | 611 | |
cb6e926e | 612 | skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL); |
66c77ff3 | 613 | if (!skb) |
2a8cc6c8 | 614 | return -ENOBUFS; |
2a8cc6c8 | 615 | |
66c77ff3 | 616 | err = -ESRCH; |
2a8cc6c8 | 617 | |
66c77ff3 ED |
618 | rcu_read_lock(); |
619 | p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index); | |
eec53cc3 | 620 | lseq = READ_ONCE(net->ipv6.ip6addrlbl_table.seq); |
66c77ff3 ED |
621 | if (p) |
622 | err = ip6addrlbl_fill(skb, p, lseq, | |
623 | NETLINK_CB(in_skb).portid, | |
624 | nlh->nlmsg_seq, | |
625 | RTM_NEWADDRLABEL, 0); | |
626 | rcu_read_unlock(); | |
2a8cc6c8 YH |
627 | |
628 | if (err < 0) { | |
629 | WARN_ON(err == -EMSGSIZE); | |
630 | kfree_skb(skb); | |
66c77ff3 ED |
631 | } else { |
632 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); | |
2a8cc6c8 | 633 | } |
2a8cc6c8 YH |
634 | return err; |
635 | } | |
636 | ||
a37b0e4e KI |
637 | static const struct rtnl_msg_handler ipv6_adddr_label_rtnl_msg_handlers[] __initconst_or_module = { |
638 | {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWADDRLABEL, | |
639 | .doit = ip6addrlbl_newdel, .flags = RTNL_FLAG_DOIT_UNLOCKED}, | |
640 | {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELADDRLABEL, | |
641 | .doit = ip6addrlbl_newdel, .flags = RTNL_FLAG_DOIT_UNLOCKED}, | |
642 | {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETADDRLABEL, | |
643 | .doit = ip6addrlbl_get, .dumpit = ip6addrlbl_dump, | |
644 | .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, | |
645 | }; | |
646 | ||
a3fde2ad | 647 | int __init ipv6_addr_label_rtnl_register(void) |
2a8cc6c8 | 648 | { |
a37b0e4e | 649 | return rtnl_register_many(ipv6_adddr_label_rtnl_msg_handlers); |
2a8cc6c8 | 650 | } |