blob: 8c4348cb19505446e13471d09369610e41851146 [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:
9 * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
10 */
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
25#define ADDRLABEL(x...) do { ; } while(0)
26#endif
27
28/*
29 * Policy Table
30 */
31struct ip6addrlbl_entry
32{
Benjamin Thery3de23252008-05-28 14:51:24 +020033#ifdef CONFIG_NET_NS
34 struct net *lbl_net;
35#endif
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090036 struct in6_addr prefix;
37 int prefixlen;
38 int ifindex;
39 int addrtype;
40 u32 label;
41 struct hlist_node list;
42 atomic_t refcnt;
43 struct rcu_head rcu;
44};
45
46static struct ip6addrlbl_table
47{
48 struct hlist_head head;
49 spinlock_t lock;
50 u32 seq;
51} ip6addrlbl_table;
52
Benjamin Thery3de23252008-05-28 14:51:24 +020053static inline
54struct net *ip6addrlbl_net(const struct ip6addrlbl_entry *lbl)
55{
56#ifdef CONFIG_NET_NS
57 return lbl->lbl_net;
58#else
59 return &init_net;
60#endif
61}
62
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090063/*
64 * Default policy table (RFC3484 + extensions)
65 *
66 * prefix addr_type label
67 * -------------------------------------------------------------------------
68 * ::1/128 LOOPBACK 0
69 * ::/0 N/A 1
70 * 2002::/16 N/A 2
71 * ::/96 COMPATv4 3
72 * ::ffff:0:0/96 V4MAPPED 4
73 * fc00::/7 N/A 5 ULA (RFC 4193)
74 * 2001::/32 N/A 6 Teredo (RFC 4380)
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -080075 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090076 *
77 * Note: 0xffffffff is used if we do not have any policies.
78 */
79
80#define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
81
Benjamin Thery3de23252008-05-28 14:51:24 +020082static const __net_initdata struct ip6addrlbl_init_table
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +090083{
84 const struct in6_addr *prefix;
85 int prefixlen;
86 u32 label;
87} ip6addrlbl_init_table[] = {
88 { /* ::/0 */
89 .prefix = &in6addr_any,
90 .label = 1,
91 },{ /* fc00::/7 */
92 .prefix = &(struct in6_addr){{{ 0xfc }}},
93 .prefixlen = 7,
94 .label = 5,
95 },{ /* 2002::/16 */
96 .prefix = &(struct in6_addr){{{ 0x20, 0x02 }}},
97 .prefixlen = 16,
98 .label = 2,
99 },{ /* 2001::/32 */
100 .prefix = &(struct in6_addr){{{ 0x20, 0x01 }}},
101 .prefixlen = 32,
102 .label = 6,
Juha-Matti Tapio5fe47b82008-02-28 20:55:02 -0800103 },{ /* 2001:10::/28 */
104 .prefix = &(struct in6_addr){{{ 0x20, 0x01, 0x00, 0x10 }}},
105 .prefixlen = 28,
106 .label = 7,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900107 },{ /* ::ffff:0:0 */
108 .prefix = &(struct in6_addr){{{ [10] = 0xff, [11] = 0xff }}},
109 .prefixlen = 96,
110 .label = 4,
111 },{ /* ::/96 */
112 .prefix = &in6addr_any,
113 .prefixlen = 96,
114 .label = 3,
115 },{ /* ::1/128 */
116 .prefix = &in6addr_loopback,
117 .prefixlen = 128,
118 .label = 0,
119 }
120};
121
122/* Object management */
123static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
124{
Benjamin Thery3de23252008-05-28 14:51:24 +0200125#ifdef CONFIG_NET_NS
126 release_net(p->lbl_net);
127#endif
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900128 kfree(p);
129}
130
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800131static void ip6addrlbl_free_rcu(struct rcu_head *h)
132{
133 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
134}
135
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900136static inline int ip6addrlbl_hold(struct ip6addrlbl_entry *p)
137{
138 return atomic_inc_not_zero(&p->refcnt);
139}
140
141static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
142{
143 if (atomic_dec_and_test(&p->refcnt))
YOSHIFUJI Hideaki85040bc2008-01-28 15:46:02 -0800144 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900145}
146
147/* Find label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200148static int __ip6addrlbl_match(struct net *net,
149 struct ip6addrlbl_entry *p,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900150 const struct in6_addr *addr,
151 int addrtype, int ifindex)
152{
Benjamin Thery3de23252008-05-28 14:51:24 +0200153 if (!net_eq(ip6addrlbl_net(p), net))
154 return 0;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900155 if (p->ifindex && p->ifindex != ifindex)
156 return 0;
157 if (p->addrtype && p->addrtype != addrtype)
158 return 0;
159 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
160 return 0;
161 return 1;
162}
163
Benjamin Thery3de23252008-05-28 14:51:24 +0200164static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
165 const struct in6_addr *addr,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900166 int type, int ifindex)
167{
168 struct hlist_node *pos;
169 struct ip6addrlbl_entry *p;
170 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200171 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900172 return p;
173 }
174 return NULL;
175}
176
Benjamin Thery3de23252008-05-28 14:51:24 +0200177u32 ipv6_addr_label(struct net *net,
178 const struct in6_addr *addr, int type, int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900179{
180 u32 label;
181 struct ip6addrlbl_entry *p;
182
183 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
184
185 rcu_read_lock();
Benjamin Thery3de23252008-05-28 14:51:24 +0200186 p = __ipv6_addr_label(net, addr, type, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900187 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
188 rcu_read_unlock();
189
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700190 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700191 __func__, addr, type, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900192
193 return label;
194}
195
196/* allocate one entry */
Benjamin Thery3de23252008-05-28 14:51:24 +0200197static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
198 const struct in6_addr *prefix,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900199 int prefixlen, int ifindex,
200 u32 label)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900201{
202 struct ip6addrlbl_entry *newp;
203 int addrtype;
204
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700205 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700206 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900207
208 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
209
210 switch (addrtype) {
211 case IPV6_ADDR_MAPPED:
212 if (prefixlen > 96)
213 return ERR_PTR(-EINVAL);
214 if (prefixlen < 96)
215 addrtype = 0;
216 break;
217 case IPV6_ADDR_COMPATv4:
218 if (prefixlen != 96)
219 addrtype = 0;
220 break;
221 case IPV6_ADDR_LOOPBACK:
222 if (prefixlen != 128)
223 addrtype = 0;
224 break;
225 }
226
227 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
228 if (!newp)
229 return ERR_PTR(-ENOMEM);
230
231 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
232 newp->prefixlen = prefixlen;
233 newp->ifindex = ifindex;
234 newp->addrtype = addrtype;
235 newp->label = label;
236 INIT_HLIST_NODE(&newp->list);
Benjamin Thery3de23252008-05-28 14:51:24 +0200237#ifdef CONFIG_NET_NS
238 newp->lbl_net = hold_net(net);
239#endif
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900240 atomic_set(&newp->refcnt, 1);
241 return newp;
242}
243
244/* add a label */
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900245static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900246{
247 int ret = 0;
248
249 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800250 __func__,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900251 newp, replace);
252
253 if (hlist_empty(&ip6addrlbl_table.head)) {
254 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
255 } else {
256 struct hlist_node *pos, *n;
257 struct ip6addrlbl_entry *p = NULL;
258 hlist_for_each_entry_safe(p, pos, n,
259 &ip6addrlbl_table.head, list) {
260 if (p->prefixlen == newp->prefixlen &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200261 net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900262 p->ifindex == newp->ifindex &&
263 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
264 if (!replace) {
265 ret = -EEXIST;
266 goto out;
267 }
268 hlist_replace_rcu(&p->list, &newp->list);
269 ip6addrlbl_put(p);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900270 goto out;
271 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
272 (p->prefixlen < newp->prefixlen)) {
273 hlist_add_before_rcu(&newp->list, &p->list);
274 goto out;
275 }
276 }
277 hlist_add_after_rcu(&p->list, &newp->list);
278 }
279out:
280 if (!ret)
281 ip6addrlbl_table.seq++;
282 return ret;
283}
284
285/* add a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200286static int ip6addrlbl_add(struct net *net,
287 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900288 int ifindex, u32 label, int replace)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900289{
290 struct ip6addrlbl_entry *newp;
291 int ret = 0;
292
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700293 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700294 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
295 replace);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900296
Benjamin Thery3de23252008-05-28 14:51:24 +0200297 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900298 if (IS_ERR(newp))
299 return PTR_ERR(newp);
300 spin_lock(&ip6addrlbl_table.lock);
301 ret = __ip6addrlbl_add(newp, replace);
302 spin_unlock(&ip6addrlbl_table.lock);
303 if (ret)
304 ip6addrlbl_free(newp);
305 return ret;
306}
307
308/* remove a label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200309static int __ip6addrlbl_del(struct net *net,
310 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900311 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900312{
313 struct ip6addrlbl_entry *p = NULL;
314 struct hlist_node *pos, *n;
315 int ret = -ESRCH;
316
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700317 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700318 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900319
320 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
321 if (p->prefixlen == prefixlen &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200322 net_eq(ip6addrlbl_net(p), net) &&
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900323 p->ifindex == ifindex &&
324 ipv6_addr_equal(&p->prefix, prefix)) {
325 hlist_del_rcu(&p->list);
326 ip6addrlbl_put(p);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900327 ret = 0;
328 break;
329 }
330 }
331 return ret;
332}
333
Benjamin Thery3de23252008-05-28 14:51:24 +0200334static int ip6addrlbl_del(struct net *net,
335 const struct in6_addr *prefix, int prefixlen,
YOSHIFUJI Hideaki40fee362008-01-22 17:12:50 +0900336 int ifindex)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900337{
338 struct in6_addr prefix_buf;
339 int ret;
340
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700341 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700342 __func__, prefix, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900343
344 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
345 spin_lock(&ip6addrlbl_table.lock);
Benjamin Thery3de23252008-05-28 14:51:24 +0200346 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900347 spin_unlock(&ip6addrlbl_table.lock);
348 return ret;
349}
350
351/* add default label */
Benjamin Thery3de23252008-05-28 14:51:24 +0200352static int __net_init ip6addrlbl_net_init(struct net *net)
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900353{
354 int err = 0;
355 int i;
356
Harvey Harrison0dc47872008-03-05 20:47:47 -0800357 ADDRLABEL(KERN_DEBUG "%s()\n", __func__);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900358
359 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200360 int ret = ip6addrlbl_add(net,
361 ip6addrlbl_init_table[i].prefix,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900362 ip6addrlbl_init_table[i].prefixlen,
363 0,
364 ip6addrlbl_init_table[i].label, 0);
365 /* XXX: should we free all rules when we catch an error? */
366 if (ret && (!err || err != -ENOMEM))
367 err = ret;
368 }
369 return err;
370}
371
Benjamin Thery3de23252008-05-28 14:51:24 +0200372static void __net_exit ip6addrlbl_net_exit(struct net *net)
373{
374 struct ip6addrlbl_entry *p = NULL;
375 struct hlist_node *pos, *n;
376
377 /* Remove all labels belonging to the exiting net */
378 spin_lock(&ip6addrlbl_table.lock);
379 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
380 if (net_eq(ip6addrlbl_net(p), net)) {
381 hlist_del_rcu(&p->list);
382 ip6addrlbl_put(p);
383 }
384 }
385 spin_unlock(&ip6addrlbl_table.lock);
386}
387
388static struct pernet_operations ipv6_addr_label_ops = {
389 .init = ip6addrlbl_net_init,
390 .exit = ip6addrlbl_net_exit,
391};
392
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900393int __init ipv6_addr_label_init(void)
394{
395 spin_lock_init(&ip6addrlbl_table.lock);
396
Benjamin Thery3de23252008-05-28 14:51:24 +0200397 return register_pernet_subsys(&ipv6_addr_label_ops);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900398}
399
400static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
401 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
402 [IFAL_LABEL] = { .len = sizeof(u32), },
403};
404
405static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
406 void *arg)
407{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900408 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900409 struct ifaddrlblmsg *ifal;
410 struct nlattr *tb[IFAL_MAX+1];
411 struct in6_addr *pfx;
412 u32 label;
413 int err = 0;
414
415 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
416 if (err < 0)
417 return err;
418
419 ifal = nlmsg_data(nlh);
420
421 if (ifal->ifal_family != AF_INET6 ||
422 ifal->ifal_prefixlen > 128)
423 return -EINVAL;
424
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900425 if (!tb[IFAL_ADDRESS])
426 return -EINVAL;
427
428 pfx = nla_data(tb[IFAL_ADDRESS]);
429 if (!pfx)
430 return -EINVAL;
431
432 if (!tb[IFAL_LABEL])
433 return -EINVAL;
434 label = nla_get_u32(tb[IFAL_LABEL]);
435 if (label == IPV6_ADDR_LABEL_DEFAULT)
436 return -EINVAL;
437
438 switch(nlh->nlmsg_type) {
439 case RTM_NEWADDRLABEL:
Florian Westphal07712752010-05-07 11:31:33 +0000440 if (ifal->ifal_index &&
441 !__dev_get_by_index(net, ifal->ifal_index))
442 return -EINVAL;
443
Benjamin Thery3de23252008-05-28 14:51:24 +0200444 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900445 ifal->ifal_index, label,
446 nlh->nlmsg_flags & NLM_F_REPLACE);
447 break;
448 case RTM_DELADDRLABEL:
Benjamin Thery3de23252008-05-28 14:51:24 +0200449 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900450 ifal->ifal_index);
451 break;
452 default:
453 err = -EOPNOTSUPP;
454 }
455 return err;
456}
457
458static inline void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
459 int prefixlen, int ifindex, u32 lseq)
460{
461 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
462 ifal->ifal_family = AF_INET6;
463 ifal->ifal_prefixlen = prefixlen;
464 ifal->ifal_flags = 0;
465 ifal->ifal_index = ifindex;
466 ifal->ifal_seq = lseq;
467};
468
469static int ip6addrlbl_fill(struct sk_buff *skb,
470 struct ip6addrlbl_entry *p,
471 u32 lseq,
472 u32 pid, u32 seq, int event,
473 unsigned int flags)
474{
475 struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, event,
476 sizeof(struct ifaddrlblmsg), flags);
477 if (!nlh)
478 return -EMSGSIZE;
479
480 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
481
482 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 ||
483 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
484 nlmsg_cancel(skb, nlh);
485 return -EMSGSIZE;
486 }
487
488 return nlmsg_end(skb, nlh);
489}
490
491static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
492{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900493 struct net *net = sock_net(skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900494 struct ip6addrlbl_entry *p;
495 struct hlist_node *pos;
496 int idx = 0, s_idx = cb->args[0];
497 int err;
498
499 rcu_read_lock();
500 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
Benjamin Thery3de23252008-05-28 14:51:24 +0200501 if (idx >= s_idx &&
502 net_eq(ip6addrlbl_net(p), net)) {
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900503 if ((err = ip6addrlbl_fill(skb, p,
504 ip6addrlbl_table.seq,
505 NETLINK_CB(cb->skb).pid,
506 cb->nlh->nlmsg_seq,
507 RTM_NEWADDRLABEL,
508 NLM_F_MULTI)) <= 0)
509 break;
510 }
511 idx++;
512 }
513 rcu_read_unlock();
514 cb->args[0] = idx;
515 return skb->len;
516}
517
518static inline int ip6addrlbl_msgsize(void)
519{
520 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
521 + nla_total_size(16) /* IFAL_ADDRESS */
522 + nla_total_size(4) /* IFAL_LABEL */
523 );
524}
525
526static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr* nlh,
527 void *arg)
528{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900529 struct net *net = sock_net(in_skb->sk);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900530 struct ifaddrlblmsg *ifal;
531 struct nlattr *tb[IFAL_MAX+1];
532 struct in6_addr *addr;
533 u32 lseq;
534 int err = 0;
535 struct ip6addrlbl_entry *p;
536 struct sk_buff *skb;
537
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900538 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
539 if (err < 0)
540 return err;
541
542 ifal = nlmsg_data(nlh);
543
544 if (ifal->ifal_family != AF_INET6 ||
545 ifal->ifal_prefixlen != 128)
546 return -EINVAL;
547
548 if (ifal->ifal_index &&
Benjamin Thery3de23252008-05-28 14:51:24 +0200549 !__dev_get_by_index(net, ifal->ifal_index))
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900550 return -EINVAL;
551
552 if (!tb[IFAL_ADDRESS])
553 return -EINVAL;
554
555 addr = nla_data(tb[IFAL_ADDRESS]);
556 if (!addr)
557 return -EINVAL;
558
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
571 if (!(skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL))) {
572 ip6addrlbl_put(p);
573 return -ENOBUFS;
574 }
575
576 err = ip6addrlbl_fill(skb, p, lseq,
577 NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
578 RTM_NEWADDRLABEL, 0);
579
580 ip6addrlbl_put(p);
581
582 if (err < 0) {
583 WARN_ON(err == -EMSGSIZE);
584 kfree_skb(skb);
585 goto out;
586 }
587
Benjamin Thery3de23252008-05-28 14:51:24 +0200588 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +0900589out:
590 return err;
591}
592
593void __init ipv6_addr_label_rtnl_register(void)
594{
595 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel, NULL);
596 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel, NULL);
597 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get, ip6addrlbl_dump);
598}
599