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 <net/addrconf.h>
17 #include <linux/if_addrlabel.h>
18 #include <linux/netlink.h>
19 #include <linux/rtnetlink.h>
22 #define ADDRLABEL(x...) printk(x)
24 #define ADDRLABEL(x...) do { ; } while(0)
30 struct ip6addrlbl_entry
35 struct in6_addr prefix;
40 struct hlist_node list;
45 static struct ip6addrlbl_table
47 struct hlist_head head;
53 struct net *ip6addrlbl_net(const struct ip6addrlbl_entry *lbl)
63 * Default policy table (RFC3484 + extensions)
65 * prefix addr_type label
66 * -------------------------------------------------------------------------
71 * ::ffff:0:0/96 V4MAPPED 4
72 * fc00::/7 N/A 5 ULA (RFC 4193)
73 * 2001::/32 N/A 6 Teredo (RFC 4380)
74 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
76 * Note: 0xffffffff is used if we do not have any policies.
79 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
81 static const __net_initdata struct ip6addrlbl_init_table
83 const struct in6_addr *prefix;
86 } ip6addrlbl_init_table[] = {
88 .prefix = &in6addr_any,
91 .prefix = &(struct in6_addr){{{ 0xfc }}},
95 .prefix = &(struct in6_addr){{{ 0x20, 0x02 }}},
99 .prefix = &(struct in6_addr){{{ 0x20, 0x01 }}},
102 },{ /* 2001:10::/28 */
103 .prefix = &(struct in6_addr){{{ 0x20, 0x01, 0x00, 0x10 }}},
107 .prefix = &(struct in6_addr){{{ [10] = 0xff, [11] = 0xff }}},
111 .prefix = &in6addr_any,
115 .prefix = &in6addr_loopback,
121 /* Object management */
122 static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
125 release_net(p->lbl_net);
130 static void ip6addrlbl_free_rcu(struct rcu_head *h)
132 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
135 static inline int ip6addrlbl_hold(struct ip6addrlbl_entry *p)
137 return atomic_inc_not_zero(&p->refcnt);
140 static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
142 if (atomic_dec_and_test(&p->refcnt))
143 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
147 static int __ip6addrlbl_match(struct net *net,
148 struct ip6addrlbl_entry *p,
149 const struct in6_addr *addr,
150 int addrtype, int ifindex)
152 if (!net_eq(ip6addrlbl_net(p), net))
154 if (p->ifindex && p->ifindex != ifindex)
156 if (p->addrtype && p->addrtype != addrtype)
158 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
163 static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
164 const struct in6_addr *addr,
165 int type, int ifindex)
167 struct hlist_node *pos;
168 struct ip6addrlbl_entry *p;
169 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
170 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
176 u32 ipv6_addr_label(struct net *net,
177 const struct in6_addr *addr, int type, int ifindex)
180 struct ip6addrlbl_entry *p;
182 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
185 p = __ipv6_addr_label(net, addr, type, ifindex);
186 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
189 ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
190 __func__, addr, type, ifindex, label);
195 /* allocate one entry */
196 static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
197 const struct in6_addr *prefix,
198 int prefixlen, int ifindex,
201 struct ip6addrlbl_entry *newp;
204 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
205 __func__, prefix, prefixlen, ifindex, (unsigned int)label);
207 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
210 case IPV6_ADDR_MAPPED:
212 return ERR_PTR(-EINVAL);
216 case IPV6_ADDR_COMPATv4:
220 case IPV6_ADDR_LOOPBACK:
221 if (prefixlen != 128)
226 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
231 newp->prefixlen = prefixlen;
232 newp->ifindex = ifindex;
233 newp->addrtype = addrtype;
235 INIT_HLIST_NODE(&newp->list);
237 newp->lbl_net = hold_net(net);
239 atomic_set(&newp->refcnt, 1);
244 static int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
248 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n",
252 if (hlist_empty(&ip6addrlbl_table.head)) {
253 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
255 struct hlist_node *pos, *n;
256 struct ip6addrlbl_entry *p = NULL;
257 hlist_for_each_entry_safe(p, pos, n,
258 &ip6addrlbl_table.head, list) {
259 if (p->prefixlen == newp->prefixlen &&
260 net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
261 p->ifindex == newp->ifindex &&
262 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
267 hlist_replace_rcu(&p->list, &newp->list);
270 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
271 (p->prefixlen < newp->prefixlen)) {
272 hlist_add_before_rcu(&newp->list, &p->list);
276 hlist_add_after_rcu(&p->list, &newp->list);
280 ip6addrlbl_table.seq++;
285 static int ip6addrlbl_add(struct net *net,
286 const struct in6_addr *prefix, int prefixlen,
287 int ifindex, u32 label, int replace)
289 struct ip6addrlbl_entry *newp;
292 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
293 __func__, prefix, prefixlen, ifindex, (unsigned int)label,
296 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
298 return PTR_ERR(newp);
299 spin_lock(&ip6addrlbl_table.lock);
300 ret = __ip6addrlbl_add(newp, replace);
301 spin_unlock(&ip6addrlbl_table.lock);
303 ip6addrlbl_free(newp);
308 static int __ip6addrlbl_del(struct net *net,
309 const struct in6_addr *prefix, int prefixlen,
312 struct ip6addrlbl_entry *p = NULL;
313 struct hlist_node *pos, *n;
316 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
317 __func__, prefix, prefixlen, ifindex);
319 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
320 if (p->prefixlen == prefixlen &&
321 net_eq(ip6addrlbl_net(p), net) &&
322 p->ifindex == ifindex &&
323 ipv6_addr_equal(&p->prefix, prefix)) {
324 hlist_del_rcu(&p->list);
333 static int ip6addrlbl_del(struct net *net,
334 const struct in6_addr *prefix, int prefixlen,
337 struct in6_addr prefix_buf;
340 ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
341 __func__, prefix, prefixlen, ifindex);
343 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
344 spin_lock(&ip6addrlbl_table.lock);
345 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
346 spin_unlock(&ip6addrlbl_table.lock);
350 /* add default label */
351 static int __net_init ip6addrlbl_net_init(struct net *net)
356 ADDRLABEL(KERN_DEBUG "%s()\n", __func__);
358 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
359 int ret = ip6addrlbl_add(net,
360 ip6addrlbl_init_table[i].prefix,
361 ip6addrlbl_init_table[i].prefixlen,
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))
371 static void __net_exit ip6addrlbl_net_exit(struct net *net)
373 struct ip6addrlbl_entry *p = NULL;
374 struct hlist_node *pos, *n;
376 /* Remove all labels belonging to the exiting net */
377 spin_lock(&ip6addrlbl_table.lock);
378 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
379 if (net_eq(ip6addrlbl_net(p), net)) {
380 hlist_del_rcu(&p->list);
384 spin_unlock(&ip6addrlbl_table.lock);
387 static struct pernet_operations ipv6_addr_label_ops = {
388 .init = ip6addrlbl_net_init,
389 .exit = ip6addrlbl_net_exit,
392 int __init ipv6_addr_label_init(void)
394 spin_lock_init(&ip6addrlbl_table.lock);
396 return register_pernet_subsys(&ipv6_addr_label_ops);
399 static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
400 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
401 [IFAL_LABEL] = { .len = sizeof(u32), },
404 static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
407 struct net *net = sock_net(skb->sk);
408 struct ifaddrlblmsg *ifal;
409 struct nlattr *tb[IFAL_MAX+1];
410 struct in6_addr *pfx;
414 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
418 ifal = nlmsg_data(nlh);
420 if (ifal->ifal_family != AF_INET6 ||
421 ifal->ifal_prefixlen > 128)
424 if (ifal->ifal_index &&
425 !__dev_get_by_index(net, ifal->ifal_index))
428 if (!tb[IFAL_ADDRESS])
431 pfx = nla_data(tb[IFAL_ADDRESS]);
437 label = nla_get_u32(tb[IFAL_LABEL]);
438 if (label == IPV6_ADDR_LABEL_DEFAULT)
441 switch(nlh->nlmsg_type) {
442 case RTM_NEWADDRLABEL:
443 err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
444 ifal->ifal_index, label,
445 nlh->nlmsg_flags & NLM_F_REPLACE);
447 case RTM_DELADDRLABEL:
448 err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
457 static inline void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
458 int prefixlen, int ifindex, u32 lseq)
460 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
461 ifal->ifal_family = AF_INET6;
462 ifal->ifal_prefixlen = prefixlen;
463 ifal->ifal_flags = 0;
464 ifal->ifal_index = ifindex;
465 ifal->ifal_seq = lseq;
468 static int ip6addrlbl_fill(struct sk_buff *skb,
469 struct ip6addrlbl_entry *p,
471 u32 pid, u32 seq, int event,
474 struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, event,
475 sizeof(struct ifaddrlblmsg), flags);
479 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
481 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 ||
482 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
483 nlmsg_cancel(skb, nlh);
487 return nlmsg_end(skb, nlh);
490 static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
492 struct net *net = sock_net(skb->sk);
493 struct ip6addrlbl_entry *p;
494 struct hlist_node *pos;
495 int idx = 0, s_idx = cb->args[0];
499 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
501 net_eq(ip6addrlbl_net(p), net)) {
502 if ((err = ip6addrlbl_fill(skb, p,
503 ip6addrlbl_table.seq,
504 NETLINK_CB(cb->skb).pid,
517 static inline int ip6addrlbl_msgsize(void)
519 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
520 + nla_total_size(16) /* IFAL_ADDRESS */
521 + nla_total_size(4) /* IFAL_LABEL */
525 static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr* nlh,
528 struct net *net = sock_net(in_skb->sk);
529 struct ifaddrlblmsg *ifal;
530 struct nlattr *tb[IFAL_MAX+1];
531 struct in6_addr *addr;
534 struct ip6addrlbl_entry *p;
537 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
541 ifal = nlmsg_data(nlh);
543 if (ifal->ifal_family != AF_INET6 ||
544 ifal->ifal_prefixlen != 128)
547 if (ifal->ifal_index &&
548 !__dev_get_by_index(net, ifal->ifal_index))
551 if (!tb[IFAL_ADDRESS])
554 addr = nla_data(tb[IFAL_ADDRESS]);
559 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
560 if (p && ip6addrlbl_hold(p))
562 lseq = ip6addrlbl_table.seq;
570 if (!(skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL))) {
575 err = ip6addrlbl_fill(skb, p, lseq,
576 NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
577 RTM_NEWADDRLABEL, 0);
582 WARN_ON(err == -EMSGSIZE);
587 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
592 void __init ipv6_addr_label_rtnl_register(void)
594 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel, NULL);
595 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel, NULL);
596 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get, ip6addrlbl_dump);