2 * IPv6 Address Label subsystem
3 * for the IPv6 "Default" Source Address Selection
5 * Copyright (C)2007 USAGI/WIDE Project
9 * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/rcupdate.h>
15 #include <linux/in6.h>
16 #include <linux/slab.h>
17 #include <net/addrconf.h>
18 #include <linux/if_addrlabel.h>
19 #include <linux/netlink.h>
20 #include <linux/rtnetlink.h>
23 #define ADDRLABEL(x...) printk(x)
25 #define ADDRLABEL(x...) do { ; } while(0)
31 struct ip6addrlbl_entry
36 struct in6_addr prefix;
41 struct hlist_node list;
46 static struct ip6addrlbl_table
48 struct hlist_head head;
54 struct net *ip6addrlbl_net(const struct ip6addrlbl_entry *lbl)
56 return read_pnet(&lbl->lbl_net);
60 * Default policy table (RFC3484 + extensions)
62 * prefix addr_type label
63 * -------------------------------------------------------------------------
68 * ::ffff:0:0/96 V4MAPPED 4
69 * fc00::/7 N/A 5 ULA (RFC 4193)
70 * 2001::/32 N/A 6 Teredo (RFC 4380)
71 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
73 * Note: 0xffffffff is used if we do not have any policies.
76 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
78 static const __net_initdata struct ip6addrlbl_init_table
80 const struct in6_addr *prefix;
83 } ip6addrlbl_init_table[] = {
85 .prefix = &in6addr_any,
88 .prefix = &(struct in6_addr){{{ 0xfc }}},
92 .prefix = &(struct in6_addr){{{ 0x20, 0x02 }}},
96 .prefix = &(struct in6_addr){{{ 0x20, 0x01 }}},
99 },{ /* 2001:10::/28 */
100 .prefix = &(struct in6_addr){{{ 0x20, 0x01, 0x00, 0x10 }}},
104 .prefix = &(struct in6_addr){{{ [10] = 0xff, [11] = 0xff }}},
108 .prefix = &in6addr_any,
112 .prefix = &in6addr_loopback,
118 /* Object management */
119 static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
122 release_net(p->lbl_net);
127 static void ip6addrlbl_free_rcu(struct rcu_head *h)
129 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
132 static bool ip6addrlbl_hold(struct ip6addrlbl_entry *p)
134 return atomic_inc_not_zero(&p->refcnt);
137 static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
139 if (atomic_dec_and_test(&p->refcnt))
140 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
144 static bool __ip6addrlbl_match(struct net *net,
145 const struct ip6addrlbl_entry *p,
146 const struct in6_addr *addr,
147 int addrtype, int ifindex)
149 if (!net_eq(ip6addrlbl_net(p), net))
151 if (p->ifindex && p->ifindex != ifindex)
153 if (p->addrtype && p->addrtype != addrtype)
155 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
160 static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
161 const struct in6_addr *addr,
162 int type, int ifindex)
164 struct hlist_node *pos;
165 struct ip6addrlbl_entry *p;
166 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
167 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
173 u32 ipv6_addr_label(struct net *net,
174 const struct in6_addr *addr, int type, int ifindex)
177 struct ip6addrlbl_entry *p;
179 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
182 p = __ipv6_addr_label(net, addr, type, ifindex);
183 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
186 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
187 __func__, addr, type, ifindex, label);
192 /* allocate one entry */
193 static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
194 const struct in6_addr *prefix,
195 int prefixlen, int ifindex,
198 struct ip6addrlbl_entry *newp;
201 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
202 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
204 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
207 case IPV6_ADDR_MAPPED:
209 return ERR_PTR(-EINVAL);
213 case IPV6_ADDR_COMPATv4:
217 case IPV6_ADDR_LOOPBACK:
218 if (prefixlen != 128)
223 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
225 return ERR_PTR(-ENOMEM);
227 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
228 newp->prefixlen = prefixlen;
229 newp->ifindex = ifindex;
230 newp->addrtype = addrtype;
232 INIT_HLIST_NODE(&newp->list);
234 newp->lbl_net = hold_net(net);
236 atomic_set(&newp->refcnt, 1);
241 static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
245 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n",
249 if (hlist_empty(&ip6addrlbl_table.head)) {
250 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
252 struct hlist_node *pos, *n;
253 struct ip6addrlbl_entry *p = NULL;
254 hlist_for_each_entry_safe(p, pos, n,
255 &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)) {
264 hlist_replace_rcu(&p->list, &newp->list);
267 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
268 (p->prefixlen < newp->prefixlen)) {
269 hlist_add_before_rcu(&newp->list, &p->list);
273 hlist_add_after_rcu(&p->list, &newp->list);
277 ip6addrlbl_table.seq++;
282 static int ip6addrlbl_add(struct net *net,
283 const struct in6_addr *prefix, int prefixlen,
284 int ifindex, u32 label, int replace)
286 struct ip6addrlbl_entry *newp;
289 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
290 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
293 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
295 return PTR_ERR(newp);
296 spin_lock(&ip6addrlbl_table.lock);
297 ret = __ip6addrlbl_add(newp, replace);
298 spin_unlock(&ip6addrlbl_table.lock);
300 ip6addrlbl_free(newp);
305 static int __ip6addrlbl_del(struct net *net,
306 const struct in6_addr *prefix, int prefixlen,
309 struct ip6addrlbl_entry *p = NULL;
310 struct hlist_node *pos, *n;
313 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
314 __func__, prefix, prefixlen, ifindex);
316 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
317 if (p->prefixlen == prefixlen &&
318 net_eq(ip6addrlbl_net(p), net) &&
319 p->ifindex == ifindex &&
320 ipv6_addr_equal(&p->prefix, prefix)) {
321 hlist_del_rcu(&p->list);
330 static int ip6addrlbl_del(struct net *net,
331 const struct in6_addr *prefix, int prefixlen,
334 struct in6_addr prefix_buf;
337 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
338 __func__, prefix, prefixlen, ifindex);
340 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
341 spin_lock(&ip6addrlbl_table.lock);
342 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
343 spin_unlock(&ip6addrlbl_table.lock);
347 /* add default label */
348 static int __net_init ip6addrlbl_net_init(struct net *net)
353 ADDRLABEL(KERN_DEBUG "%s\n", __func__);
355 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
356 int ret = ip6addrlbl_add(net,
357 ip6addrlbl_init_table[i].prefix,
358 ip6addrlbl_init_table[i].prefixlen,
360 ip6addrlbl_init_table[i].label, 0);
361 /* XXX: should we free all rules when we catch an error? */
362 if (ret && (!err || err != -ENOMEM))
368 static void __net_exit ip6addrlbl_net_exit(struct net *net)
370 struct ip6addrlbl_entry *p = NULL;
371 struct hlist_node *pos, *n;
373 /* Remove all labels belonging to the exiting net */
374 spin_lock(&ip6addrlbl_table.lock);
375 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
376 if (net_eq(ip6addrlbl_net(p), net)) {
377 hlist_del_rcu(&p->list);
381 spin_unlock(&ip6addrlbl_table.lock);
384 static struct pernet_operations ipv6_addr_label_ops = {
385 .init = ip6addrlbl_net_init,
386 .exit = ip6addrlbl_net_exit,
389 int __init ipv6_addr_label_init(void)
391 spin_lock_init(&ip6addrlbl_table.lock);
393 return register_pernet_subsys(&ipv6_addr_label_ops);
396 void ipv6_addr_label_cleanup(void)
398 unregister_pernet_subsys(&ipv6_addr_label_ops);
401 static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
402 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
403 [IFAL_LABEL] = { .len = sizeof(u32), },
406 static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
409 struct net *net = sock_net(skb->sk);
410 struct ifaddrlblmsg *ifal;
411 struct nlattr *tb[IFAL_MAX+1];
412 struct in6_addr *pfx;
416 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
420 ifal = nlmsg_data(nlh);
422 if (ifal->ifal_family != AF_INET6 ||
423 ifal->ifal_prefixlen > 128)
426 if (!tb[IFAL_ADDRESS])
429 pfx = nla_data(tb[IFAL_ADDRESS]);
435 label = nla_get_u32(tb[IFAL_LABEL]);
436 if (label == IPV6_ADDR_LABEL_DEFAULT)
439 switch(nlh->nlmsg_type) {
440 case RTM_NEWADDRLABEL:
441 if (ifal->ifal_index &&
442 !__dev_get_by_index(net, ifal->ifal_index))
445 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
446 ifal->ifal_index, label,
447 nlh->nlmsg_flags & NLM_F_REPLACE);
449 case RTM_DELADDRLABEL:
450 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
459 static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
460 int prefixlen, int ifindex, u32 lseq)
462 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
463 ifal->ifal_family = AF_INET6;
464 ifal->ifal_prefixlen = prefixlen;
465 ifal->ifal_flags = 0;
466 ifal->ifal_index = ifindex;
467 ifal->ifal_seq = lseq;
470 static int ip6addrlbl_fill(struct sk_buff *skb,
471 struct ip6addrlbl_entry *p,
473 u32 pid, u32 seq, int event,
476 struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, event,
477 sizeof(struct ifaddrlblmsg), flags);
481 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
483 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 ||
484 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
485 nlmsg_cancel(skb, nlh);
489 return nlmsg_end(skb, nlh);
492 static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
494 struct net *net = sock_net(skb->sk);
495 struct ip6addrlbl_entry *p;
496 struct hlist_node *pos;
497 int idx = 0, s_idx = cb->args[0];
501 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
503 net_eq(ip6addrlbl_net(p), net)) {
504 if ((err = ip6addrlbl_fill(skb, p,
505 ip6addrlbl_table.seq,
506 NETLINK_CB(cb->skb).pid,
519 static inline int ip6addrlbl_msgsize(void)
521 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
522 + nla_total_size(16) /* IFAL_ADDRESS */
523 + nla_total_size(4); /* IFAL_LABEL */
526 static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr* nlh,
529 struct net *net = sock_net(in_skb->sk);
530 struct ifaddrlblmsg *ifal;
531 struct nlattr *tb[IFAL_MAX+1];
532 struct in6_addr *addr;
535 struct ip6addrlbl_entry *p;
538 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
542 ifal = nlmsg_data(nlh);
544 if (ifal->ifal_family != AF_INET6 ||
545 ifal->ifal_prefixlen != 128)
548 if (ifal->ifal_index &&
549 !__dev_get_by_index(net, ifal->ifal_index))
552 if (!tb[IFAL_ADDRESS])
555 addr = nla_data(tb[IFAL_ADDRESS]);
560 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
561 if (p && ip6addrlbl_hold(p))
563 lseq = ip6addrlbl_table.seq;
571 if (!(skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL))) {
576 err = ip6addrlbl_fill(skb, p, lseq,
577 NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
578 RTM_NEWADDRLABEL, 0);
583 WARN_ON(err == -EMSGSIZE);
588 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
593 void __init ipv6_addr_label_rtnl_register(void)
595 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel,
597 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel,
599 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get,
600 ip6addrlbl_dump, NULL);