blob: 731e1e1722d9b4322f3907e57e3a1fd1994230b5 [file] [log] [blame]
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09001/*
2 * IPv6 Address Label subsystem
3 * for the IPv6 "Default" Source Address Selection
4 *
5 * Copyright (C)2007 USAGI/WIDE Project
6 */
7/*
8 * Author:
Wang Yufen1c8669e2014-02-17 16:53:34 +08009 * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090010 */
11
12#include <linux/kernel.h>
13#include <linux/list.h>
14#include <linux/rcupdate.h>
15#include <linux/in6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090017#include <net/addrconf.h>
18#include <linux/if_addrlabel.h>
19#include <linux/netlink.h>
20#include <linux/rtnetlink.h>
21
22#if 0
23#define ADDRLABEL(x...) printk(x)
24#else
Wang Yufen1c8669e2014-02-17 16:53:34 +080025#define ADDRLABEL(x...) do { ; } while (0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090026#endif
27
28/*
29 * Policy Table
30 */
Wang Yufen22b285d2014-02-17 16:53:33 +080031struct ip6addrlbl_entry {
Benjamin Thery3de23252008-05-28 14:51:24 +020032#ifdef CONFIG_NET_NS
33 struct net *lbl_net;
34#endif
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;
41 atomic_t refcnt;
42 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{
Benjamin Thery3de23252008-05-28 14:51:24 +0200132#ifdef CONFIG_NET_NS
133 release_net(p->lbl_net);
134#endif
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900135 kfree(p);
136}
137
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800138static void ip6addrlbl_free_rcu(struct rcu_head *h)
139{
140 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
141}
142
Eric Dumazeta50feda2012-05-18 18:57:34 +0000143static bool ip6addrlbl_hold(struct ip6addrlbl_entry *p)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900144{
145 return atomic_inc_not_zero(&p->refcnt);
146}
147
148static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
149{
150 if (atomic_dec_and_test(&p->refcnt))
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800151 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900152}
153
154/* Find label */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000155static bool __ip6addrlbl_match(struct net *net,
156 const struct ip6addrlbl_entry *p,
157 const struct in6_addr *addr,
158 int addrtype, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900159{
Benjamin Thery3de23252008-05-28 14:51:24 +0200160 if (!net_eq(ip6addrlbl_net(p), net))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000161 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900162 if (p->ifindex && p->ifindex != ifindex)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000163 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900164 if (p->addrtype && p->addrtype != addrtype)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000165 return false;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900166 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000167 return false;
168 return true;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900169}
170
Benjamin Thery3de23252008-05-28 14:51:24 +0200171static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
172 const struct in6_addr *addr,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900173 int type, int ifindex)
174{
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900175 struct ip6addrlbl_entry *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800176 hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200177 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900178 return p;
179 }
180 return NULL;
181}
182
Benjamin Thery3de23252008-05-28 14:51:24 +0200183u32 ipv6_addr_label(struct net *net,
184 const struct in6_addr *addr, int type, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900185{
186 u32 label;
187 struct ip6addrlbl_entry *p;
188
189 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
190
191 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200192 p = __ipv6_addr_label(net, addr, type, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900193 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
194 rcu_read_unlock();
195
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700196 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700197 __func__, addr, type, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900198
199 return label;
200}
201
202/* allocate one entry */
Benjamin Thery3de23252008-05-28 14:51:24 +0200203static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
204 const struct in6_addr *prefix,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900205 int prefixlen, int ifindex,
206 u32 label)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900207{
208 struct ip6addrlbl_entry *newp;
209 int addrtype;
210
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700211 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700212 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900213
214 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
215
216 switch (addrtype) {
217 case IPV6_ADDR_MAPPED:
218 if (prefixlen > 96)
219 return ERR_PTR(-EINVAL);
220 if (prefixlen < 96)
221 addrtype = 0;
222 break;
223 case IPV6_ADDR_COMPATv4:
224 if (prefixlen != 96)
225 addrtype = 0;
226 break;
227 case IPV6_ADDR_LOOPBACK:
228 if (prefixlen != 128)
229 addrtype = 0;
230 break;
231 }
232
233 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
234 if (!newp)
235 return ERR_PTR(-ENOMEM);
236
237 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
238 newp->prefixlen = prefixlen;
239 newp->ifindex = ifindex;
240 newp->addrtype = addrtype;
241 newp->label = label;
242 INIT_HLIST_NODE(&newp->list);
Benjamin Thery3de23252008-05-28 14:51:24 +0200243#ifdef CONFIG_NET_NS
244 newp->lbl_net = hold_net(net);
245#endif
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900246 atomic_set(&newp->refcnt, 1);
247 return newp;
248}
249
250/* add a label */
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900251static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900252{
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200253 struct hlist_node *n;
254 struct ip6addrlbl_entry *last = NULL, *p = NULL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900255 int ret = 0;
256
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200257 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp,
258 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900259
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200260 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
261 if (p->prefixlen == newp->prefixlen &&
262 net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
263 p->ifindex == newp->ifindex &&
264 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
265 if (!replace) {
266 ret = -EEXIST;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900267 goto out;
268 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200269 hlist_replace_rcu(&p->list, &newp->list);
270 ip6addrlbl_put(p);
271 goto out;
272 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
273 (p->prefixlen < newp->prefixlen)) {
274 hlist_add_before_rcu(&newp->list, &p->list);
275 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900276 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200277 last = p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900278 }
Hannes Frederic Sowa639739b2013-09-03 02:13:31 +0200279 if (last)
280 hlist_add_after_rcu(&last->list, &newp->list);
281 else
282 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900283out:
284 if (!ret)
285 ip6addrlbl_table.seq++;
286 return ret;
287}
288
289/* add a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200290static int ip6addrlbl_add(struct net *net,
291 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900292 int ifindex, u32 label, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900293{
294 struct ip6addrlbl_entry *newp;
295 int ret = 0;
296
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700297 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700298 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
299 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900300
Benjamin Thery3de23252008-05-28 14:51:24 +0200301 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900302 if (IS_ERR(newp))
303 return PTR_ERR(newp);
304 spin_lock(&ip6addrlbl_table.lock);
305 ret = __ip6addrlbl_add(newp, replace);
306 spin_unlock(&ip6addrlbl_table.lock);
307 if (ret)
308 ip6addrlbl_free(newp);
309 return ret;
310}
311
312/* remove a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200313static int __ip6addrlbl_del(struct net *net,
314 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900315 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900316{
317 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800318 struct hlist_node *n;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900319 int ret = -ESRCH;
320
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700321 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700322 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900323
Sasha Levinb67bfe02013-02-27 17:06:00 -0800324 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900325 if (p->prefixlen == prefixlen &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200326 net_eq(ip6addrlbl_net(p), net) &&
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900327 p->ifindex == ifindex &&
328 ipv6_addr_equal(&p->prefix, prefix)) {
329 hlist_del_rcu(&p->list);
330 ip6addrlbl_put(p);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900331 ret = 0;
332 break;
333 }
334 }
335 return ret;
336}
337
Benjamin Thery3de23252008-05-28 14:51:24 +0200338static int ip6addrlbl_del(struct net *net,
339 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900340 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900341{
342 struct in6_addr prefix_buf;
343 int ret;
344
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700345 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700346 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900347
348 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
349 spin_lock(&ip6addrlbl_table.lock);
Benjamin Thery3de23252008-05-28 14:51:24 +0200350 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900351 spin_unlock(&ip6addrlbl_table.lock);
352 return ret;
353}
354
355/* add default label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200356static int __net_init ip6addrlbl_net_init(struct net *net)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900357{
358 int err = 0;
359 int i;
360
Joe Perchesf3213832012-05-15 14:11:53 +0000361 ADDRLABEL(KERN_DEBUG "%s\n", __func__);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900362
363 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200364 int ret = ip6addrlbl_add(net,
365 ip6addrlbl_init_table[i].prefix,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900366 ip6addrlbl_init_table[i].prefixlen,
367 0,
368 ip6addrlbl_init_table[i].label, 0);
369 /* XXX: should we free all rules when we catch an error? */
370 if (ret && (!err || err != -ENOMEM))
371 err = ret;
372 }
373 return err;
374}
375
Benjamin Thery3de23252008-05-28 14:51:24 +0200376static void __net_exit ip6addrlbl_net_exit(struct net *net)
377{
378 struct ip6addrlbl_entry *p = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800379 struct hlist_node *n;
Benjamin Thery3de23252008-05-28 14:51:24 +0200380
381 /* Remove all labels belonging to the exiting net */
382 spin_lock(&ip6addrlbl_table.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800383 hlist_for_each_entry_safe(p, n, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200384 if (net_eq(ip6addrlbl_net(p), net)) {
385 hlist_del_rcu(&p->list);
386 ip6addrlbl_put(p);
387 }
388 }
389 spin_unlock(&ip6addrlbl_table.lock);
390}
391
392static struct pernet_operations ipv6_addr_label_ops = {
393 .init = ip6addrlbl_net_init,
394 .exit = ip6addrlbl_net_exit,
395};
396
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900397int __init ipv6_addr_label_init(void)
398{
399 spin_lock_init(&ip6addrlbl_table.lock);
400
Benjamin Thery3de23252008-05-28 14:51:24 +0200401 return register_pernet_subsys(&ipv6_addr_label_ops);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900402}
403
Neil Horman2cc6d2b2010-09-24 09:55:52 +0000404void ipv6_addr_label_cleanup(void)
405{
406 unregister_pernet_subsys(&ipv6_addr_label_ops);
407}
408
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900409static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
410 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
411 [IFAL_LABEL] = { .len = sizeof(u32), },
412};
413
Thomas Graf661d2962013-03-21 07:45:29 +0000414static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900415{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900416 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900417 struct ifaddrlblmsg *ifal;
418 struct nlattr *tb[IFAL_MAX+1];
419 struct in6_addr *pfx;
420 u32 label;
421 int err = 0;
422
423 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
424 if (err < 0)
425 return err;
426
427 ifal = nlmsg_data(nlh);
428
429 if (ifal->ifal_family != AF_INET6 ||
430 ifal->ifal_prefixlen > 128)
431 return -EINVAL;
432
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900433 if (!tb[IFAL_ADDRESS])
434 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900435 pfx = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900436
437 if (!tb[IFAL_LABEL])
438 return -EINVAL;
439 label = nla_get_u32(tb[IFAL_LABEL]);
440 if (label == IPV6_ADDR_LABEL_DEFAULT)
441 return -EINVAL;
442
Wang Yufen1c8669e2014-02-17 16:53:34 +0800443 switch (nlh->nlmsg_type) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900444 case RTM_NEWADDRLABEL:
Florian Westphal07712752010-05-07 11:31:33 +0000445 if (ifal->ifal_index &&
446 !__dev_get_by_index(net, ifal->ifal_index))
447 return -EINVAL;
448
Benjamin Thery3de23252008-05-28 14:51:24 +0200449 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900450 ifal->ifal_index, label,
451 nlh->nlmsg_flags & NLM_F_REPLACE);
452 break;
453 case RTM_DELADDRLABEL:
Benjamin Thery3de23252008-05-28 14:51:24 +0200454 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900455 ifal->ifal_index);
456 break;
457 default:
458 err = -EOPNOTSUPP;
459 }
460 return err;
461}
462
Eric Dumazeta50feda2012-05-18 18:57:34 +0000463static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
464 int prefixlen, int ifindex, u32 lseq)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900465{
466 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
467 ifal->ifal_family = AF_INET6;
468 ifal->ifal_prefixlen = prefixlen;
469 ifal->ifal_flags = 0;
470 ifal->ifal_index = ifindex;
471 ifal->ifal_seq = lseq;
472};
473
474static int ip6addrlbl_fill(struct sk_buff *skb,
475 struct ip6addrlbl_entry *p,
476 u32 lseq,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000477 u32 portid, u32 seq, int event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900478 unsigned int flags)
479{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000480 struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900481 sizeof(struct ifaddrlblmsg), flags);
482 if (!nlh)
483 return -EMSGSIZE;
484
485 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
486
487 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 ||
488 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
489 nlmsg_cancel(skb, nlh);
490 return -EMSGSIZE;
491 }
492
493 return nlmsg_end(skb, nlh);
494}
495
496static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
497{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900498 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900499 struct ip6addrlbl_entry *p;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900500 int idx = 0, s_idx = cb->args[0];
501 int err;
502
503 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800504 hlist_for_each_entry_rcu(p, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200505 if (idx >= s_idx &&
506 net_eq(ip6addrlbl_net(p), net)) {
Wang Yufencb6e9262014-02-17 16:53:35 +0800507 err = ip6addrlbl_fill(skb, p,
508 ip6addrlbl_table.seq,
509 NETLINK_CB(cb->skb).portid,
510 cb->nlh->nlmsg_seq,
511 RTM_NEWADDRLABEL,
512 NLM_F_MULTI);
513 if (err <= 0)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900514 break;
515 }
516 idx++;
517 }
518 rcu_read_unlock();
519 cb->args[0] = idx;
520 return skb->len;
521}
522
523static inline int ip6addrlbl_msgsize(void)
524{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000525 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900526 + nla_total_size(16) /* IFAL_ADDRESS */
Eric Dumazeta02cec22010-09-22 20:43:57 +0000527 + nla_total_size(4); /* IFAL_LABEL */
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900528}
529
Wang Yufen22b285d2014-02-17 16:53:33 +0800530static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900531{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900532 struct net *net = sock_net(in_skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900533 struct ifaddrlblmsg *ifal;
534 struct nlattr *tb[IFAL_MAX+1];
535 struct in6_addr *addr;
536 u32 lseq;
537 int err = 0;
538 struct ip6addrlbl_entry *p;
539 struct sk_buff *skb;
540
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900541 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
542 if (err < 0)
543 return err;
544
545 ifal = nlmsg_data(nlh);
546
547 if (ifal->ifal_family != AF_INET6 ||
548 ifal->ifal_prefixlen != 128)
549 return -EINVAL;
550
551 if (ifal->ifal_index &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200552 !__dev_get_by_index(net, ifal->ifal_index))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900553 return -EINVAL;
554
555 if (!tb[IFAL_ADDRESS])
556 return -EINVAL;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900557 addr = nla_data(tb[IFAL_ADDRESS]);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900558
559 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200560 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900561 if (p && ip6addrlbl_hold(p))
562 p = NULL;
563 lseq = ip6addrlbl_table.seq;
564 rcu_read_unlock();
565
566 if (!p) {
567 err = -ESRCH;
568 goto out;
569 }
570
Wang Yufencb6e9262014-02-17 16:53:35 +0800571 skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL);
572 if (!skb) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900573 ip6addrlbl_put(p);
574 return -ENOBUFS;
575 }
576
577 err = ip6addrlbl_fill(skb, p, lseq,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000578 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900579 RTM_NEWADDRLABEL, 0);
580
581 ip6addrlbl_put(p);
582
583 if (err < 0) {
584 WARN_ON(err == -EMSGSIZE);
585 kfree_skb(skb);
586 goto out;
587 }
588
Eric W. Biederman15e47302012-09-07 20:12:54 +0000589 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900590out:
591 return err;
592}
593
594void __init ipv6_addr_label_rtnl_register(void)
595{
Greg Rosec7ac8672011-06-10 01:27:09 +0000596 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel,
597 NULL, NULL);
598 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel,
599 NULL, NULL);
600 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get,
601 ip6addrlbl_dump, NULL);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900602}
603