blob: 642fc6ac13d229230a51d8513b60fc31c00b7bc4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09002/*
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:
Wang Yufen1c8669e2014-02-17 16:53:34 +080010 * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090011 */
12
13#include <linux/kernel.h>
14#include <linux/list.h>
15#include <linux/rcupdate.h>
16#include <linux/in6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090018#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
Wang Yufen1c8669e2014-02-17 16:53:34 +080026#define ADDRLABEL(x...) do { ; } while (0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090027#endif
28
29/*
30 * Policy Table
31 */
Wang Yufen22b285d2014-02-17 16:53:33 +080032struct ip6addrlbl_entry {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090033 struct in6_addr prefix;
34 int prefixlen;
35 int ifindex;
36 int addrtype;
37 u32 label;
38 struct hlist_node list;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090039 struct rcu_head rcu;
40};
41
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090042/*
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000043 * Default policy table (RFC6724 + extensions)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090044 *
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)
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -080054 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000055 * fec0::/10 N/A 11 Site-local
56 * (deprecated by RFC3879)
57 * 3ffe::/16 N/A 12 6bone
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090058 *
59 * Note: 0xffffffff is used if we do not have any policies.
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000060 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090061 */
62
63#define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
64
Andi Kleen04a6f822012-10-04 17:12:11 -070065static const __net_initconst struct ip6addrlbl_init_table
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090066{
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,
Wang Yufen1c8669e2014-02-17 16:53:34 +080074 }, { /* fc00::/7 */
75 .prefix = &(struct in6_addr){ { { 0xfc } } } ,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090076 .prefixlen = 7,
77 .label = 5,
Wang Yufen1c8669e2014-02-17 16:53:34 +080078 }, { /* fec0::/10 */
79 .prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } },
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000080 .prefixlen = 10,
81 .label = 11,
Wang Yufen1c8669e2014-02-17 16:53:34 +080082 }, { /* 2002::/16 */
83 .prefix = &(struct in6_addr){ { { 0x20, 0x02 } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090084 .prefixlen = 16,
85 .label = 2,
Wang Yufen1c8669e2014-02-17 16:53:34 +080086 }, { /* 3ffe::/16 */
87 .prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } },
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000088 .prefixlen = 16,
89 .label = 12,
Wang Yufen1c8669e2014-02-17 16:53:34 +080090 }, { /* 2001::/32 */
91 .prefix = &(struct in6_addr){ { { 0x20, 0x01 } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090092 .prefixlen = 32,
93 .label = 6,
Wang Yufen1c8669e2014-02-17 16:53:34 +080094 }, { /* 2001:10::/28 */
95 .prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } },
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -080096 .prefixlen = 28,
97 .label = 7,
Wang Yufen1c8669e2014-02-17 16:53:34 +080098 }, { /* ::ffff:0:0 */
99 .prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900100 .prefixlen = 96,
101 .label = 4,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800102 }, { /* ::/96 */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900103 .prefix = &in6addr_any,
104 .prefixlen = 96,
105 .label = 3,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800106 }, { /* ::1/128 */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900107 .prefix = &in6addr_loopback,
108 .prefixlen = 128,
109 .label = 0,
110 }
111};
112
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900113/* Find label */
Eric Dumazeta90c9342017-09-19 16:27:06 -0700114static bool __ip6addrlbl_match(const struct ip6addrlbl_entry *p,
Eric Dumazeta50feda2012-05-18 18:57:34 +0000115 const struct in6_addr *addr,
116 int addrtype, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900117{
118 if (p->ifindex && p->ifindex != ifindex)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000119 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900120 if (p->addrtype && p->addrtype != addrtype)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000121 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900122 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000123 return false;
124 return true;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900125}
126
Benjamin Thery3de23252008-05-28 14:51:24 +0200127static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
128 const struct in6_addr *addr,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900129 int type, int ifindex)
130{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900131 struct ip6addrlbl_entry *p;
Eric Dumazeta90c9342017-09-19 16:27:06 -0700132
133 hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
134 if (__ip6addrlbl_match(p, addr, type, ifindex))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900135 return p;
136 }
137 return NULL;
138}
139
Benjamin Thery3de23252008-05-28 14:51:24 +0200140u32 ipv6_addr_label(struct net *net,
141 const struct in6_addr *addr, int type, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900142{
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();
Benjamin Thery3de23252008-05-28 14:51:24 +0200149 p = __ipv6_addr_label(net, addr, type, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900150 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
151 rcu_read_unlock();
152
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700153 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700154 __func__, addr, type, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900155
156 return label;
157}
158
159/* allocate one entry */
Eric Dumazeta90c9342017-09-19 16:27:06 -0700160static struct ip6addrlbl_entry *ip6addrlbl_alloc(const struct in6_addr *prefix,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900161 int prefixlen, int ifindex,
162 u32 label)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900163{
164 struct ip6addrlbl_entry *newp;
165 int addrtype;
166
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700167 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700168 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900169
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);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900199 return newp;
200}
201
202/* add a label */
Eric Dumazeta90c9342017-09-19 16:27:06 -0700203static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
204 int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900205{
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200206 struct ip6addrlbl_entry *last = NULL, *p = NULL;
Eric Dumazeta90c9342017-09-19 16:27:06 -0700207 struct hlist_node *n;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900208 int ret = 0;
209
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200210 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp,
211 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900212
Eric Dumazeta90c9342017-09-19 16:27:06 -0700213 hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) {
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200214 if (p->prefixlen == newp->prefixlen &&
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200215 p->ifindex == newp->ifindex &&
216 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
217 if (!replace) {
218 ret = -EEXIST;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900219 goto out;
220 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200221 hlist_replace_rcu(&p->list, &newp->list);
Eric Dumazet2809c092017-10-09 09:52:25 -0700222 kfree_rcu(p, rcu);
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200223 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;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900228 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200229 last = p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900230 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200231 if (last)
Ken Helias1d023282014-08-06 16:09:16 -0700232 hlist_add_behind_rcu(&newp->list, &last->list);
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200233 else
Eric Dumazeta90c9342017-09-19 16:27:06 -0700234 hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900235out:
236 if (!ret)
Eric Dumazeta90c9342017-09-19 16:27:06 -0700237 net->ipv6.ip6addrlbl_table.seq++;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900238 return ret;
239}
240
241/* add a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200242static int ip6addrlbl_add(struct net *net,
243 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900244 int ifindex, u32 label, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900245{
246 struct ip6addrlbl_entry *newp;
247 int ret = 0;
248
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700249 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700250 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
251 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900252
Eric Dumazeta90c9342017-09-19 16:27:06 -0700253 newp = ip6addrlbl_alloc(prefix, prefixlen, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900254 if (IS_ERR(newp))
255 return PTR_ERR(newp);
Eric Dumazeta90c9342017-09-19 16:27:06 -0700256 spin_lock(&net->ipv6.ip6addrlbl_table.lock);
257 ret = __ip6addrlbl_add(net, newp, replace);
258 spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900259 if (ret)
Eric Dumazet2809c092017-10-09 09:52:25 -0700260 kfree(newp);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900261 return ret;
262}
263
264/* remove a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200265static int __ip6addrlbl_del(struct net *net,
266 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900267 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900268{
269 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800270 struct hlist_node *n;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900271 int ret = -ESRCH;
272
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700273 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700274 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900275
Eric Dumazeta90c9342017-09-19 16:27:06 -0700276 hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900277 if (p->prefixlen == prefixlen &&
278 p->ifindex == ifindex &&
279 ipv6_addr_equal(&p->prefix, prefix)) {
280 hlist_del_rcu(&p->list);
Eric Dumazet2809c092017-10-09 09:52:25 -0700281 kfree_rcu(p, rcu);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900282 ret = 0;
283 break;
284 }
285 }
286 return ret;
287}
288
Benjamin Thery3de23252008-05-28 14:51:24 +0200289static int ip6addrlbl_del(struct net *net,
290 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900291 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900292{
293 struct in6_addr prefix_buf;
294 int ret;
295
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700296 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700297 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900298
299 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
Eric Dumazeta90c9342017-09-19 16:27:06 -0700300 spin_lock(&net->ipv6.ip6addrlbl_table.lock);
Benjamin Thery3de23252008-05-28 14:51:24 +0200301 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
Eric Dumazeta90c9342017-09-19 16:27:06 -0700302 spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900303 return ret;
304}
305
306/* add default label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200307static int __net_init ip6addrlbl_net_init(struct net *net)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900308{
309 int err = 0;
310 int i;
311
Joe Perchesf3213832012-05-15 14:11:53 +0000312 ADDRLABEL(KERN_DEBUG "%s\n", __func__);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900313
Eric Dumazeta90c9342017-09-19 16:27:06 -0700314 spin_lock_init(&net->ipv6.ip6addrlbl_table.lock);
315 INIT_HLIST_HEAD(&net->ipv6.ip6addrlbl_table.head);
316
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900317 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200318 int ret = ip6addrlbl_add(net,
319 ip6addrlbl_init_table[i].prefix,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900320 ip6addrlbl_init_table[i].prefixlen,
321 0,
322 ip6addrlbl_init_table[i].label, 0);
323 /* XXX: should we free all rules when we catch an error? */
324 if (ret && (!err || err != -ENOMEM))
325 err = ret;
326 }
327 return err;
328}
329
Benjamin Thery3de23252008-05-28 14:51:24 +0200330static void __net_exit ip6addrlbl_net_exit(struct net *net)
331{
332 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800333 struct hlist_node *n;
Benjamin Thery3de23252008-05-28 14:51:24 +0200334
335 /* Remove all labels belonging to the exiting net */
Eric Dumazeta90c9342017-09-19 16:27:06 -0700336 spin_lock(&net->ipv6.ip6addrlbl_table.lock);
337 hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) {
338 hlist_del_rcu(&p->list);
Eric Dumazet2809c092017-10-09 09:52:25 -0700339 kfree_rcu(p, rcu);
Benjamin Thery3de23252008-05-28 14:51:24 +0200340 }
Eric Dumazeta90c9342017-09-19 16:27:06 -0700341 spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
Benjamin Thery3de23252008-05-28 14:51:24 +0200342}
343
344static struct pernet_operations ipv6_addr_label_ops = {
345 .init = ip6addrlbl_net_init,
346 .exit = ip6addrlbl_net_exit,
347};
348
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900349int __init ipv6_addr_label_init(void)
350{
Benjamin Thery3de23252008-05-28 14:51:24 +0200351 return register_pernet_subsys(&ipv6_addr_label_ops);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900352}
353
Neil Horman2cc6d2b2010-09-24 09:55:52 +0000354void ipv6_addr_label_cleanup(void)
355{
356 unregister_pernet_subsys(&ipv6_addr_label_ops);
357}
358
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900359static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
360 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
361 [IFAL_LABEL] = { .len = sizeof(u32), },
362};
363
Florian Westphala6f57022017-08-29 13:29:42 +0200364static bool addrlbl_ifindex_exists(struct net *net, int ifindex)
365{
366
367 struct net_device *dev;
368
369 rcu_read_lock();
370 dev = dev_get_by_index_rcu(net, ifindex);
371 rcu_read_unlock();
372
373 return dev != NULL;
374}
375
David Ahernc21ef3e2017-04-16 09:48:24 -0700376static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
377 struct netlink_ext_ack *extack)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900378{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900379 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900380 struct ifaddrlblmsg *ifal;
381 struct nlattr *tb[IFAL_MAX+1];
382 struct in6_addr *pfx;
383 u32 label;
384 int err = 0;
385
Johannes Berg8cb08172019-04-26 14:07:28 +0200386 err = nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb, IFAL_MAX,
387 ifal_policy, extack);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900388 if (err < 0)
389 return err;
390
391 ifal = nlmsg_data(nlh);
392
393 if (ifal->ifal_family != AF_INET6 ||
394 ifal->ifal_prefixlen > 128)
395 return -EINVAL;
396
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900397 if (!tb[IFAL_ADDRESS])
398 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900399 pfx = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900400
401 if (!tb[IFAL_LABEL])
402 return -EINVAL;
403 label = nla_get_u32(tb[IFAL_LABEL]);
404 if (label == IPV6_ADDR_LABEL_DEFAULT)
405 return -EINVAL;
406
Wang Yufen1c8669e2014-02-17 16:53:34 +0800407 switch (nlh->nlmsg_type) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900408 case RTM_NEWADDRLABEL:
Florian Westphal07712752010-05-07 11:31:33 +0000409 if (ifal->ifal_index &&
Florian Westphala6f57022017-08-29 13:29:42 +0200410 !addrlbl_ifindex_exists(net, ifal->ifal_index))
Florian Westphal07712752010-05-07 11:31:33 +0000411 return -EINVAL;
412
Benjamin Thery3de23252008-05-28 14:51:24 +0200413 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900414 ifal->ifal_index, label,
415 nlh->nlmsg_flags & NLM_F_REPLACE);
416 break;
417 case RTM_DELADDRLABEL:
Benjamin Thery3de23252008-05-28 14:51:24 +0200418 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900419 ifal->ifal_index);
420 break;
421 default:
422 err = -EOPNOTSUPP;
423 }
424 return err;
425}
426
Eric Dumazeta50feda2012-05-18 18:57:34 +0000427static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
428 int prefixlen, int ifindex, u32 lseq)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900429{
430 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
431 ifal->ifal_family = AF_INET6;
432 ifal->ifal_prefixlen = prefixlen;
433 ifal->ifal_flags = 0;
434 ifal->ifal_index = ifindex;
435 ifal->ifal_seq = lseq;
436};
437
438static int ip6addrlbl_fill(struct sk_buff *skb,
439 struct ip6addrlbl_entry *p,
440 u32 lseq,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000441 u32 portid, u32 seq, int event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900442 unsigned int flags)
443{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000444 struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900445 sizeof(struct ifaddrlblmsg), flags);
446 if (!nlh)
447 return -EMSGSIZE;
448
449 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
450
Jiri Benc930345e2015-03-29 16:59:25 +0200451 if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 ||
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900452 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
453 nlmsg_cancel(skb, nlh);
454 return -EMSGSIZE;
455 }
456
Johannes Berg053c0952015-01-16 22:09:00 +0100457 nlmsg_end(skb, nlh);
458 return 0;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900459}
460
David Ahernf2ae64b2018-10-07 20:16:40 -0700461static int ip6addrlbl_valid_dump_req(const struct nlmsghdr *nlh,
462 struct netlink_ext_ack *extack)
463{
464 struct ifaddrlblmsg *ifal;
465
466 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) {
467 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address label dump request");
468 return -EINVAL;
469 }
470
471 ifal = nlmsg_data(nlh);
472 if (ifal->__ifal_reserved || ifal->ifal_prefixlen ||
473 ifal->ifal_flags || ifal->ifal_index || ifal->ifal_seq) {
474 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address label dump request");
475 return -EINVAL;
476 }
477
478 if (nlmsg_attrlen(nlh, sizeof(*ifal))) {
Colin Ian Kingd5f6db32019-04-18 11:39:18 +0100479 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header for address label dump request");
David Ahernf2ae64b2018-10-07 20:16:40 -0700480 return -EINVAL;
481 }
482
483 return 0;
484}
485
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900486static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
487{
David Ahernf2ae64b2018-10-07 20:16:40 -0700488 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900489 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900490 struct ip6addrlbl_entry *p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900491 int idx = 0, s_idx = cb->args[0];
492 int err;
493
David Ahernf2ae64b2018-10-07 20:16:40 -0700494 if (cb->strict_check) {
495 err = ip6addrlbl_valid_dump_req(nlh, cb->extack);
496 if (err < 0)
497 return err;
498 }
499
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900500 rcu_read_lock();
Eric Dumazeta90c9342017-09-19 16:27:06 -0700501 hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
502 if (idx >= s_idx) {
Wang Yufencb6e9262014-02-17 16:53:35 +0800503 err = ip6addrlbl_fill(skb, p,
Eric Dumazeta90c9342017-09-19 16:27:06 -0700504 net->ipv6.ip6addrlbl_table.seq,
Wang Yufencb6e9262014-02-17 16:53:35 +0800505 NETLINK_CB(cb->skb).portid,
David Ahernf2ae64b2018-10-07 20:16:40 -0700506 nlh->nlmsg_seq,
Wang Yufencb6e9262014-02-17 16:53:35 +0800507 RTM_NEWADDRLABEL,
508 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +0100509 if (err < 0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900510 break;
511 }
512 idx++;
513 }
514 rcu_read_unlock();
515 cb->args[0] = idx;
516 return skb->len;
517}
518
519static inline int ip6addrlbl_msgsize(void)
520{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000521 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900522 + nla_total_size(16) /* IFAL_ADDRESS */
Eric Dumazeta02cec22010-09-22 20:43:57 +0000523 + nla_total_size(4); /* IFAL_LABEL */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900524}
525
Jakub Kicinski5912a772019-01-18 10:46:23 -0800526static int ip6addrlbl_valid_get_req(struct sk_buff *skb,
527 const struct nlmsghdr *nlh,
528 struct nlattr **tb,
529 struct netlink_ext_ack *extack)
530{
531 struct ifaddrlblmsg *ifal;
532 int i, err;
533
534 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) {
535 NL_SET_ERR_MSG_MOD(extack, "Invalid header for addrlabel get request");
536 return -EINVAL;
537 }
538
539 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +0200540 return nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb,
541 IFAL_MAX, ifal_policy, extack);
Jakub Kicinski5912a772019-01-18 10:46:23 -0800542
543 ifal = nlmsg_data(nlh);
544 if (ifal->__ifal_reserved || ifal->ifal_flags || ifal->ifal_seq) {
545 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for addrlabel get request");
546 return -EINVAL;
547 }
548
Johannes Berg8cb08172019-04-26 14:07:28 +0200549 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifal), tb, IFAL_MAX,
550 ifal_policy, extack);
Jakub Kicinski5912a772019-01-18 10:46:23 -0800551 if (err)
552 return err;
553
554 for (i = 0; i <= IFAL_MAX; i++) {
555 if (!tb[i])
556 continue;
557
558 switch (i) {
559 case IFAL_ADDRESS:
560 break;
561 default:
562 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in addrlabel get request");
563 return -EINVAL;
564 }
565 }
566
567 return 0;
568}
569
David Ahernc21ef3e2017-04-16 09:48:24 -0700570static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
571 struct netlink_ext_ack *extack)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900572{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900573 struct net *net = sock_net(in_skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900574 struct ifaddrlblmsg *ifal;
575 struct nlattr *tb[IFAL_MAX+1];
576 struct in6_addr *addr;
577 u32 lseq;
578 int err = 0;
579 struct ip6addrlbl_entry *p;
580 struct sk_buff *skb;
581
Jakub Kicinski5912a772019-01-18 10:46:23 -0800582 err = ip6addrlbl_valid_get_req(in_skb, nlh, tb, extack);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900583 if (err < 0)
584 return err;
585
586 ifal = nlmsg_data(nlh);
587
588 if (ifal->ifal_family != AF_INET6 ||
589 ifal->ifal_prefixlen != 128)
590 return -EINVAL;
591
592 if (ifal->ifal_index &&
Florian Westphala6f57022017-08-29 13:29:42 +0200593 !addrlbl_ifindex_exists(net, ifal->ifal_index))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900594 return -EINVAL;
595
596 if (!tb[IFAL_ADDRESS])
597 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900598 addr = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900599
Eric Dumazet66c77ff2017-10-09 09:52:24 -0700600 skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL);
601 if (!skb)
602 return -ENOBUFS;
603
604 err = -ESRCH;
605
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900606 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200607 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
Eric Dumazeta90c9342017-09-19 16:27:06 -0700608 lseq = net->ipv6.ip6addrlbl_table.seq;
Eric Dumazet66c77ff2017-10-09 09:52:24 -0700609 if (p)
610 err = ip6addrlbl_fill(skb, p, lseq,
611 NETLINK_CB(in_skb).portid,
612 nlh->nlmsg_seq,
613 RTM_NEWADDRLABEL, 0);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900614 rcu_read_unlock();
615
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900616 if (err < 0) {
617 WARN_ON(err == -EMSGSIZE);
618 kfree_skb(skb);
Eric Dumazet66c77ff2017-10-09 09:52:24 -0700619 } else {
620 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900621 }
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900622 return err;
623}
624
Florian Westphala3fde2a2017-12-04 19:19:18 +0100625int __init ipv6_addr_label_rtnl_register(void)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900626{
Florian Westphala3fde2a2017-12-04 19:19:18 +0100627 int ret;
628
629 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDRLABEL,
630 ip6addrlbl_newdel,
631 NULL, RTNL_FLAG_DOIT_UNLOCKED);
632 if (ret < 0)
633 return ret;
634 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDRLABEL,
635 ip6addrlbl_newdel,
636 NULL, RTNL_FLAG_DOIT_UNLOCKED);
637 if (ret < 0)
638 return ret;
639 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDRLABEL,
640 ip6addrlbl_get,
641 ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED);
642 return ret;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900643}