blob: f664871feca666fb6256990c2fc36fc15940a541 [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>
Reshetova, Elena87078f22017-07-04 09:35:00 +030022#include <linux/refcount.h>
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090023
24#if 0
25#define ADDRLABEL(x...) printk(x)
26#else
Wang Yufen1c8669e2014-02-17 16:53:34 +080027#define ADDRLABEL(x...) do { ; } while (0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090028#endif
29
30/*
31 * Policy Table
32 */
Wang Yufen22b285d2014-02-17 16:53:33 +080033struct ip6addrlbl_entry {
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -050034 possible_net_t lbl_net;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090035 struct in6_addr prefix;
36 int prefixlen;
37 int ifindex;
38 int addrtype;
39 u32 label;
40 struct hlist_node list;
Reshetova, Elena87078f22017-07-04 09:35:00 +030041 refcount_t refcnt;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090042 struct rcu_head rcu;
43};
44
45static struct ip6addrlbl_table
46{
47 struct hlist_head head;
48 spinlock_t lock;
49 u32 seq;
50} ip6addrlbl_table;
51
Benjamin Thery3de23252008-05-28 14:51:24 +020052static inline
53struct net *ip6addrlbl_net(const struct ip6addrlbl_entry *lbl)
54{
Eric Dumazetc2d9ba92010-06-01 06:51:19 +000055 return read_pnet(&lbl->lbl_net);
Benjamin Thery3de23252008-05-28 14:51:24 +020056}
57
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090058/*
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000059 * Default policy table (RFC6724 + extensions)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090060 *
61 * prefix addr_type label
62 * -------------------------------------------------------------------------
63 * ::1/128 LOOPBACK 0
64 * ::/0 N/A 1
65 * 2002::/16 N/A 2
66 * ::/96 COMPATv4 3
67 * ::ffff:0:0/96 V4MAPPED 4
68 * fc00::/7 N/A 5 ULA (RFC 4193)
69 * 2001::/32 N/A 6 Teredo (RFC 4380)
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -080070 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000071 * fec0::/10 N/A 11 Site-local
72 * (deprecated by RFC3879)
73 * 3ffe::/16 N/A 12 6bone
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090074 *
75 * Note: 0xffffffff is used if we do not have any policies.
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000076 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090077 */
78
79#define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
80
Andi Kleen04a6f822012-10-04 17:12:11 -070081static const __net_initconst struct ip6addrlbl_init_table
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090082{
83 const struct in6_addr *prefix;
84 int prefixlen;
85 u32 label;
86} ip6addrlbl_init_table[] = {
87 { /* ::/0 */
88 .prefix = &in6addr_any,
89 .label = 1,
Wang Yufen1c8669e2014-02-17 16:53:34 +080090 }, { /* fc00::/7 */
91 .prefix = &(struct in6_addr){ { { 0xfc } } } ,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090092 .prefixlen = 7,
93 .label = 5,
Wang Yufen1c8669e2014-02-17 16:53:34 +080094 }, { /* fec0::/10 */
95 .prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } },
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +000096 .prefixlen = 10,
97 .label = 11,
Wang Yufen1c8669e2014-02-17 16:53:34 +080098 }, { /* 2002::/16 */
99 .prefix = &(struct in6_addr){ { { 0x20, 0x02 } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900100 .prefixlen = 16,
101 .label = 2,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800102 }, { /* 3ffe::/16 */
103 .prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } },
YOSHIFUJI Hideaki / 吉藤英明417962a2012-09-10 18:16:49 +0000104 .prefixlen = 16,
105 .label = 12,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800106 }, { /* 2001::/32 */
107 .prefix = &(struct in6_addr){ { { 0x20, 0x01 } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900108 .prefixlen = 32,
109 .label = 6,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800110 }, { /* 2001:10::/28 */
111 .prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } },
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -0800112 .prefixlen = 28,
113 .label = 7,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800114 }, { /* ::ffff:0:0 */
115 .prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } },
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900116 .prefixlen = 96,
117 .label = 4,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800118 }, { /* ::/96 */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900119 .prefix = &in6addr_any,
120 .prefixlen = 96,
121 .label = 3,
Wang Yufen1c8669e2014-02-17 16:53:34 +0800122 }, { /* ::1/128 */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900123 .prefix = &in6addr_loopback,
124 .prefixlen = 128,
125 .label = 0,
126 }
127};
128
129/* Object management */
130static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
131{
132 kfree(p);
133}
134
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800135static void ip6addrlbl_free_rcu(struct rcu_head *h)
136{
137 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
138}
139
Eric Dumazeta50feda2012-05-18 18:57:34 +0000140static bool ip6addrlbl_hold(struct ip6addrlbl_entry *p)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900141{
Reshetova, Elena87078f22017-07-04 09:35:00 +0300142 return refcount_inc_not_zero(&p->refcnt);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900143}
144
145static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
146{
Reshetova, Elena87078f22017-07-04 09:35:00 +0300147 if (refcount_dec_and_test(&p->refcnt))
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800148 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900149}
150
151/* Find label */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000152static bool __ip6addrlbl_match(struct net *net,
153 const struct ip6addrlbl_entry *p,
154 const struct in6_addr *addr,
155 int addrtype, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900156{
Benjamin Thery3de23252008-05-28 14:51:24 +0200157 if (!net_eq(ip6addrlbl_net(p), net))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000158 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900159 if (p->ifindex && p->ifindex != ifindex)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000160 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900161 if (p->addrtype && p->addrtype != addrtype)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000162 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900163 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000164 return false;
165 return true;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900166}
167
Benjamin Thery3de23252008-05-28 14:51:24 +0200168static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
169 const struct in6_addr *addr,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900170 int type, int ifindex)
171{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900172 struct ip6addrlbl_entry *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800173 hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200174 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900175 return p;
176 }
177 return NULL;
178}
179
Benjamin Thery3de23252008-05-28 14:51:24 +0200180u32 ipv6_addr_label(struct net *net,
181 const struct in6_addr *addr, int type, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900182{
183 u32 label;
184 struct ip6addrlbl_entry *p;
185
186 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
187
188 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200189 p = __ipv6_addr_label(net, addr, type, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900190 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
191 rcu_read_unlock();
192
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700193 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700194 __func__, addr, type, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900195
196 return label;
197}
198
199/* allocate one entry */
Benjamin Thery3de23252008-05-28 14:51:24 +0200200static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
201 const struct in6_addr *prefix,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900202 int prefixlen, int ifindex,
203 u32 label)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900204{
205 struct ip6addrlbl_entry *newp;
206 int addrtype;
207
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700208 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700209 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900210
211 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
212
213 switch (addrtype) {
214 case IPV6_ADDR_MAPPED:
215 if (prefixlen > 96)
216 return ERR_PTR(-EINVAL);
217 if (prefixlen < 96)
218 addrtype = 0;
219 break;
220 case IPV6_ADDR_COMPATv4:
221 if (prefixlen != 96)
222 addrtype = 0;
223 break;
224 case IPV6_ADDR_LOOPBACK:
225 if (prefixlen != 128)
226 addrtype = 0;
227 break;
228 }
229
230 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
231 if (!newp)
232 return ERR_PTR(-ENOMEM);
233
234 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
235 newp->prefixlen = prefixlen;
236 newp->ifindex = ifindex;
237 newp->addrtype = addrtype;
238 newp->label = label;
239 INIT_HLIST_NODE(&newp->list);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -0500240 write_pnet(&newp->lbl_net, net);
Reshetova, Elena87078f22017-07-04 09:35:00 +0300241 refcount_set(&newp->refcnt, 1);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900242 return newp;
243}
244
245/* add a label */
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900246static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900247{
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200248 struct hlist_node *n;
249 struct ip6addrlbl_entry *last = NULL, *p = NULL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900250 int ret = 0;
251
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200252 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp,
253 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900254
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200255 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
256 if (p->prefixlen == newp->prefixlen &&
257 net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
258 p->ifindex == newp->ifindex &&
259 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
260 if (!replace) {
261 ret = -EEXIST;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900262 goto out;
263 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200264 hlist_replace_rcu(&p->list, &newp->list);
265 ip6addrlbl_put(p);
266 goto out;
267 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
268 (p->prefixlen < newp->prefixlen)) {
269 hlist_add_before_rcu(&newp->list, &p->list);
270 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900271 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200272 last = p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900273 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200274 if (last)
Ken Helias1d023282014-08-06 16:09:16 -0700275 hlist_add_behind_rcu(&newp->list, &last->list);
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200276 else
277 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900278out:
279 if (!ret)
280 ip6addrlbl_table.seq++;
281 return ret;
282}
283
284/* add a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200285static int ip6addrlbl_add(struct net *net,
286 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900287 int ifindex, u32 label, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900288{
289 struct ip6addrlbl_entry *newp;
290 int ret = 0;
291
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700292 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700293 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
294 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900295
Benjamin Thery3de23252008-05-28 14:51:24 +0200296 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900297 if (IS_ERR(newp))
298 return PTR_ERR(newp);
299 spin_lock(&ip6addrlbl_table.lock);
300 ret = __ip6addrlbl_add(newp, replace);
301 spin_unlock(&ip6addrlbl_table.lock);
302 if (ret)
303 ip6addrlbl_free(newp);
304 return ret;
305}
306
307/* remove a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200308static int __ip6addrlbl_del(struct net *net,
309 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900310 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900311{
312 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800313 struct hlist_node *n;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900314 int ret = -ESRCH;
315
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700316 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700317 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900318
Sasha Levinb67bfe02013-02-27 17:06:00 -0800319 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900320 if (p->prefixlen == prefixlen &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200321 net_eq(ip6addrlbl_net(p), net) &&
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900322 p->ifindex == ifindex &&
323 ipv6_addr_equal(&p->prefix, prefix)) {
324 hlist_del_rcu(&p->list);
325 ip6addrlbl_put(p);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900326 ret = 0;
327 break;
328 }
329 }
330 return ret;
331}
332
Benjamin Thery3de23252008-05-28 14:51:24 +0200333static int ip6addrlbl_del(struct net *net,
334 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900335 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900336{
337 struct in6_addr prefix_buf;
338 int ret;
339
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700340 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700341 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900342
343 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
344 spin_lock(&ip6addrlbl_table.lock);
Benjamin Thery3de23252008-05-28 14:51:24 +0200345 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900346 spin_unlock(&ip6addrlbl_table.lock);
347 return ret;
348}
349
350/* add default label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200351static int __net_init ip6addrlbl_net_init(struct net *net)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900352{
353 int err = 0;
354 int i;
355
Joe Perchesf3213832012-05-15 14:11:53 +0000356 ADDRLABEL(KERN_DEBUG "%s\n", __func__);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900357
358 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200359 int ret = ip6addrlbl_add(net,
360 ip6addrlbl_init_table[i].prefix,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900361 ip6addrlbl_init_table[i].prefixlen,
362 0,
363 ip6addrlbl_init_table[i].label, 0);
364 /* XXX: should we free all rules when we catch an error? */
365 if (ret && (!err || err != -ENOMEM))
366 err = ret;
367 }
368 return err;
369}
370
Benjamin Thery3de23252008-05-28 14:51:24 +0200371static void __net_exit ip6addrlbl_net_exit(struct net *net)
372{
373 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800374 struct hlist_node *n;
Benjamin Thery3de23252008-05-28 14:51:24 +0200375
376 /* Remove all labels belonging to the exiting net */
377 spin_lock(&ip6addrlbl_table.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800378 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200379 if (net_eq(ip6addrlbl_net(p), net)) {
380 hlist_del_rcu(&p->list);
381 ip6addrlbl_put(p);
382 }
383 }
384 spin_unlock(&ip6addrlbl_table.lock);
385}
386
387static struct pernet_operations ipv6_addr_label_ops = {
388 .init = ip6addrlbl_net_init,
389 .exit = ip6addrlbl_net_exit,
390};
391
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900392int __init ipv6_addr_label_init(void)
393{
394 spin_lock_init(&ip6addrlbl_table.lock);
395
Benjamin Thery3de23252008-05-28 14:51:24 +0200396 return register_pernet_subsys(&ipv6_addr_label_ops);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900397}
398
Neil Horman2cc6d2b2010-09-24 09:55:52 +0000399void ipv6_addr_label_cleanup(void)
400{
401 unregister_pernet_subsys(&ipv6_addr_label_ops);
402}
403
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900404static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
405 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
406 [IFAL_LABEL] = { .len = sizeof(u32), },
407};
408
Florian Westphala6f57022017-08-29 13:29:42 +0200409static bool addrlbl_ifindex_exists(struct net *net, int ifindex)
410{
411
412 struct net_device *dev;
413
414 rcu_read_lock();
415 dev = dev_get_by_index_rcu(net, ifindex);
416 rcu_read_unlock();
417
418 return dev != NULL;
419}
420
David Ahernc21ef3e2017-04-16 09:48:24 -0700421static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
422 struct netlink_ext_ack *extack)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900423{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900424 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900425 struct ifaddrlblmsg *ifal;
426 struct nlattr *tb[IFAL_MAX+1];
427 struct in6_addr *pfx;
428 u32 label;
429 int err = 0;
430
David Ahernc21ef3e2017-04-16 09:48:24 -0700431 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy,
432 extack);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900433 if (err < 0)
434 return err;
435
436 ifal = nlmsg_data(nlh);
437
438 if (ifal->ifal_family != AF_INET6 ||
439 ifal->ifal_prefixlen > 128)
440 return -EINVAL;
441
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900442 if (!tb[IFAL_ADDRESS])
443 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900444 pfx = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900445
446 if (!tb[IFAL_LABEL])
447 return -EINVAL;
448 label = nla_get_u32(tb[IFAL_LABEL]);
449 if (label == IPV6_ADDR_LABEL_DEFAULT)
450 return -EINVAL;
451
Wang Yufen1c8669e2014-02-17 16:53:34 +0800452 switch (nlh->nlmsg_type) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900453 case RTM_NEWADDRLABEL:
Florian Westphal07712752010-05-07 11:31:33 +0000454 if (ifal->ifal_index &&
Florian Westphala6f57022017-08-29 13:29:42 +0200455 !addrlbl_ifindex_exists(net, ifal->ifal_index))
Florian Westphal07712752010-05-07 11:31:33 +0000456 return -EINVAL;
457
Benjamin Thery3de23252008-05-28 14:51:24 +0200458 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900459 ifal->ifal_index, label,
460 nlh->nlmsg_flags & NLM_F_REPLACE);
461 break;
462 case RTM_DELADDRLABEL:
Benjamin Thery3de23252008-05-28 14:51:24 +0200463 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900464 ifal->ifal_index);
465 break;
466 default:
467 err = -EOPNOTSUPP;
468 }
469 return err;
470}
471
Eric Dumazeta50feda2012-05-18 18:57:34 +0000472static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
473 int prefixlen, int ifindex, u32 lseq)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900474{
475 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
476 ifal->ifal_family = AF_INET6;
477 ifal->ifal_prefixlen = prefixlen;
478 ifal->ifal_flags = 0;
479 ifal->ifal_index = ifindex;
480 ifal->ifal_seq = lseq;
481};
482
483static int ip6addrlbl_fill(struct sk_buff *skb,
484 struct ip6addrlbl_entry *p,
485 u32 lseq,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000486 u32 portid, u32 seq, int event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900487 unsigned int flags)
488{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000489 struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900490 sizeof(struct ifaddrlblmsg), flags);
491 if (!nlh)
492 return -EMSGSIZE;
493
494 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
495
Jiri Benc930345e2015-03-29 16:59:25 +0200496 if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 ||
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900497 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
498 nlmsg_cancel(skb, nlh);
499 return -EMSGSIZE;
500 }
501
Johannes Berg053c0952015-01-16 22:09:00 +0100502 nlmsg_end(skb, nlh);
503 return 0;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900504}
505
506static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
507{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900508 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900509 struct ip6addrlbl_entry *p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900510 int idx = 0, s_idx = cb->args[0];
511 int err;
512
513 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800514 hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200515 if (idx >= s_idx &&
516 net_eq(ip6addrlbl_net(p), net)) {
Wang Yufencb6e9262014-02-17 16:53:35 +0800517 err = ip6addrlbl_fill(skb, p,
518 ip6addrlbl_table.seq,
519 NETLINK_CB(cb->skb).portid,
520 cb->nlh->nlmsg_seq,
521 RTM_NEWADDRLABEL,
522 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +0100523 if (err < 0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900524 break;
525 }
526 idx++;
527 }
528 rcu_read_unlock();
529 cb->args[0] = idx;
530 return skb->len;
531}
532
533static inline int ip6addrlbl_msgsize(void)
534{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000535 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900536 + nla_total_size(16) /* IFAL_ADDRESS */
Eric Dumazeta02cec22010-09-22 20:43:57 +0000537 + nla_total_size(4); /* IFAL_LABEL */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900538}
539
David Ahernc21ef3e2017-04-16 09:48:24 -0700540static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
541 struct netlink_ext_ack *extack)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900542{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900543 struct net *net = sock_net(in_skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900544 struct ifaddrlblmsg *ifal;
545 struct nlattr *tb[IFAL_MAX+1];
546 struct in6_addr *addr;
547 u32 lseq;
548 int err = 0;
549 struct ip6addrlbl_entry *p;
550 struct sk_buff *skb;
551
David Ahernc21ef3e2017-04-16 09:48:24 -0700552 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy,
553 extack);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900554 if (err < 0)
555 return err;
556
557 ifal = nlmsg_data(nlh);
558
559 if (ifal->ifal_family != AF_INET6 ||
560 ifal->ifal_prefixlen != 128)
561 return -EINVAL;
562
563 if (ifal->ifal_index &&
Florian Westphala6f57022017-08-29 13:29:42 +0200564 !addrlbl_ifindex_exists(net, ifal->ifal_index))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900565 return -EINVAL;
566
567 if (!tb[IFAL_ADDRESS])
568 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900569 addr = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900570
571 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200572 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
Andrey Ryabinine459dfe2015-12-21 12:54:45 +0300573 if (p && !ip6addrlbl_hold(p))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900574 p = NULL;
575 lseq = ip6addrlbl_table.seq;
576 rcu_read_unlock();
577
578 if (!p) {
579 err = -ESRCH;
580 goto out;
581 }
582
Wang Yufencb6e9262014-02-17 16:53:35 +0800583 skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL);
584 if (!skb) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900585 ip6addrlbl_put(p);
586 return -ENOBUFS;
587 }
588
589 err = ip6addrlbl_fill(skb, p, lseq,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000590 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900591 RTM_NEWADDRLABEL, 0);
592
593 ip6addrlbl_put(p);
594
595 if (err < 0) {
596 WARN_ON(err == -EMSGSIZE);
597 kfree_skb(skb);
598 goto out;
599 }
600
Eric W. Biederman15e47302012-09-07 20:12:54 +0000601 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900602out:
603 return err;
604}
605
606void __init ipv6_addr_label_rtnl_register(void)
607{
Greg Rosec7ac8672011-06-10 01:27:09 +0000608 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel,
Florian Westphala6f57022017-08-29 13:29:42 +0200609 NULL, RTNL_FLAG_DOIT_UNLOCKED);
Greg Rosec7ac8672011-06-10 01:27:09 +0000610 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel,
Florian Westphala6f57022017-08-29 13:29:42 +0200611 NULL, RTNL_FLAG_DOIT_UNLOCKED);
Greg Rosec7ac8672011-06-10 01:27:09 +0000612 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get,
Florian Westphala6f57022017-08-29 13:29:42 +0200613 ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900614}
615