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
32 struct in6_addr prefix;
37 struct hlist_node list;
42 static struct ip6addrlbl_table
44 struct hlist_head head;
50 * Default policy table (RFC3484 + extensions)
52 * prefix addr_type label
53 * -------------------------------------------------------------------------
58 * ::ffff:0:0/96 V4MAPPED 4
59 * fc00::/7 N/A 5 ULA (RFC 4193)
60 * 2001::/32 N/A 6 Teredo (RFC 4380)
62 * Note: 0xffffffff is used if we do not have any policies.
65 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
67 static const __initdata struct ip6addrlbl_init_table
69 const struct in6_addr *prefix;
72 } ip6addrlbl_init_table[] = {
74 .prefix = &in6addr_any,
77 .prefix = &(struct in6_addr){{{ 0xfc }}},
81 .prefix = &(struct in6_addr){{{ 0x20, 0x02 }}},
85 .prefix = &(struct in6_addr){{{ 0x20, 0x01 }}},
89 .prefix = &(struct in6_addr){{{ [10] = 0xff, [11] = 0xff }}},
93 .prefix = &in6addr_any,
97 .prefix = &in6addr_loopback,
103 /* Object management */
104 static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
109 static inline int ip6addrlbl_hold(struct ip6addrlbl_entry *p)
111 return atomic_inc_not_zero(&p->refcnt);
114 static inline void ip6addrlbl_put(struct ip6addrlbl_entry *p)
116 if (atomic_dec_and_test(&p->refcnt))
120 static void ip6addrlbl_free_rcu(struct rcu_head *h)
122 ip6addrlbl_free(container_of(h, struct ip6addrlbl_entry, rcu));
126 static int __ip6addrlbl_match(struct ip6addrlbl_entry *p,
127 const struct in6_addr *addr,
128 int addrtype, int ifindex)
130 if (p->ifindex && p->ifindex != ifindex)
132 if (p->addrtype && p->addrtype != addrtype)
134 if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
139 static struct ip6addrlbl_entry *__ipv6_addr_label(const struct in6_addr *addr,
140 int type, int ifindex)
142 struct hlist_node *pos;
143 struct ip6addrlbl_entry *p;
144 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
145 if (__ip6addrlbl_match(p, addr, type, ifindex))
151 u32 ipv6_addr_label(const struct in6_addr *addr, int type, int ifindex)
154 struct ip6addrlbl_entry *p;
156 type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;
159 p = __ipv6_addr_label(addr, type, ifindex);
160 label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
163 ADDRLABEL(KERN_DEBUG "%s(addr=" NIP6_FMT ", type=%d, ifindex=%d) => %08x\n",
165 NIP6(*addr), type, ifindex,
171 /* allocate one entry */
172 struct ip6addrlbl_entry *ip6addrlbl_alloc(const struct in6_addr *prefix,
173 int prefixlen, int ifindex,
176 struct ip6addrlbl_entry *newp;
179 ADDRLABEL(KERN_DEBUG "%s(prefix=" NIP6_FMT ", prefixlen=%d, ifindex=%d, label=%u)\n",
181 NIP6(*prefix), prefixlen,
183 (unsigned int)label);
185 addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);
188 case IPV6_ADDR_MAPPED:
190 return ERR_PTR(-EINVAL);
194 case IPV6_ADDR_COMPATv4:
198 case IPV6_ADDR_LOOPBACK:
199 if (prefixlen != 128)
204 newp = kmalloc(sizeof(*newp), GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
208 ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
209 newp->prefixlen = prefixlen;
210 newp->ifindex = ifindex;
211 newp->addrtype = addrtype;
213 INIT_HLIST_NODE(&newp->list);
214 atomic_set(&newp->refcnt, 1);
219 int __ip6addrlbl_add(struct ip6addrlbl_entry *newp, int replace)
223 ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n",
227 if (hlist_empty(&ip6addrlbl_table.head)) {
228 hlist_add_head_rcu(&newp->list, &ip6addrlbl_table.head);
230 struct hlist_node *pos, *n;
231 struct ip6addrlbl_entry *p = NULL;
232 hlist_for_each_entry_safe(p, pos, n,
233 &ip6addrlbl_table.head, list) {
234 if (p->prefixlen == newp->prefixlen &&
235 p->ifindex == newp->ifindex &&
236 ipv6_addr_equal(&p->prefix, &newp->prefix)) {
241 hlist_replace_rcu(&p->list, &newp->list);
243 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
245 } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
246 (p->prefixlen < newp->prefixlen)) {
247 hlist_add_before_rcu(&newp->list, &p->list);
251 hlist_add_after_rcu(&p->list, &newp->list);
255 ip6addrlbl_table.seq++;
260 int ip6addrlbl_add(const struct in6_addr *prefix, int prefixlen,
261 int ifindex, u32 label, int replace)
263 struct ip6addrlbl_entry *newp;
266 ADDRLABEL(KERN_DEBUG "%s(prefix=" NIP6_FMT ", prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
268 NIP6(*prefix), prefixlen,
273 newp = ip6addrlbl_alloc(prefix, prefixlen, ifindex, label);
275 return PTR_ERR(newp);
276 spin_lock(&ip6addrlbl_table.lock);
277 ret = __ip6addrlbl_add(newp, replace);
278 spin_unlock(&ip6addrlbl_table.lock);
280 ip6addrlbl_free(newp);
285 int __ip6addrlbl_del(const struct in6_addr *prefix, int prefixlen,
288 struct ip6addrlbl_entry *p = NULL;
289 struct hlist_node *pos, *n;
292 ADDRLABEL(KERN_DEBUG "%s(prefix=" NIP6_FMT ", prefixlen=%d, ifindex=%d)\n",
294 NIP6(*prefix), prefixlen,
297 hlist_for_each_entry_safe(p, pos, n, &ip6addrlbl_table.head, list) {
298 if (p->prefixlen == prefixlen &&
299 p->ifindex == ifindex &&
300 ipv6_addr_equal(&p->prefix, prefix)) {
301 hlist_del_rcu(&p->list);
303 call_rcu(&p->rcu, ip6addrlbl_free_rcu);
311 int ip6addrlbl_del(const struct in6_addr *prefix, int prefixlen,
314 struct in6_addr prefix_buf;
317 ADDRLABEL(KERN_DEBUG "%s(prefix=" NIP6_FMT ", prefixlen=%d, ifindex=%d)\n",
319 NIP6(*prefix), prefixlen,
322 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
323 spin_lock(&ip6addrlbl_table.lock);
324 ret = __ip6addrlbl_del(&prefix_buf, prefixlen, ifindex);
325 spin_unlock(&ip6addrlbl_table.lock);
329 /* add default label */
330 static __init int ip6addrlbl_init(void)
335 ADDRLABEL(KERN_DEBUG "%s()\n", __FUNCTION__);
337 for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
338 int ret = ip6addrlbl_add(ip6addrlbl_init_table[i].prefix,
339 ip6addrlbl_init_table[i].prefixlen,
341 ip6addrlbl_init_table[i].label, 0);
342 /* XXX: should we free all rules when we catch an error? */
343 if (ret && (!err || err != -ENOMEM))
349 int __init ipv6_addr_label_init(void)
351 spin_lock_init(&ip6addrlbl_table.lock);
353 return ip6addrlbl_init();
356 static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
357 [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), },
358 [IFAL_LABEL] = { .len = sizeof(u32), },
361 static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
364 struct net *net = skb->sk->sk_net;
365 struct ifaddrlblmsg *ifal;
366 struct nlattr *tb[IFAL_MAX+1];
367 struct in6_addr *pfx;
371 if (net != &init_net)
374 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
378 ifal = nlmsg_data(nlh);
380 if (ifal->ifal_family != AF_INET6 ||
381 ifal->ifal_prefixlen > 128)
384 if (ifal->ifal_index &&
385 !__dev_get_by_index(&init_net, ifal->ifal_index))
388 if (!tb[IFAL_ADDRESS])
391 pfx = nla_data(tb[IFAL_ADDRESS]);
397 label = nla_get_u32(tb[IFAL_LABEL]);
398 if (label == IPV6_ADDR_LABEL_DEFAULT)
401 switch(nlh->nlmsg_type) {
402 case RTM_NEWADDRLABEL:
403 err = ip6addrlbl_add(pfx, ifal->ifal_prefixlen,
404 ifal->ifal_index, label,
405 nlh->nlmsg_flags & NLM_F_REPLACE);
407 case RTM_DELADDRLABEL:
408 err = ip6addrlbl_del(pfx, ifal->ifal_prefixlen,
417 static inline void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
418 int prefixlen, int ifindex, u32 lseq)
420 struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
421 ifal->ifal_family = AF_INET6;
422 ifal->ifal_prefixlen = prefixlen;
423 ifal->ifal_flags = 0;
424 ifal->ifal_index = ifindex;
425 ifal->ifal_seq = lseq;
428 static int ip6addrlbl_fill(struct sk_buff *skb,
429 struct ip6addrlbl_entry *p,
431 u32 pid, u32 seq, int event,
434 struct nlmsghdr *nlh = nlmsg_put(skb, pid, seq, event,
435 sizeof(struct ifaddrlblmsg), flags);
439 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
441 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 ||
442 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
443 nlmsg_cancel(skb, nlh);
447 return nlmsg_end(skb, nlh);
450 static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
452 struct net *net = skb->sk->sk_net;
453 struct ip6addrlbl_entry *p;
454 struct hlist_node *pos;
455 int idx = 0, s_idx = cb->args[0];
458 if (net != &init_net)
462 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head, list) {
464 if ((err = ip6addrlbl_fill(skb, p,
465 ip6addrlbl_table.seq,
466 NETLINK_CB(cb->skb).pid,
479 static inline int ip6addrlbl_msgsize(void)
481 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
482 + nla_total_size(16) /* IFAL_ADDRESS */
483 + nla_total_size(4) /* IFAL_LABEL */
487 static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr* nlh,
490 struct net *net = in_skb->sk->sk_net;
491 struct ifaddrlblmsg *ifal;
492 struct nlattr *tb[IFAL_MAX+1];
493 struct in6_addr *addr;
496 struct ip6addrlbl_entry *p;
499 if (net != &init_net)
502 err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy);
506 ifal = nlmsg_data(nlh);
508 if (ifal->ifal_family != AF_INET6 ||
509 ifal->ifal_prefixlen != 128)
512 if (ifal->ifal_index &&
513 !__dev_get_by_index(&init_net, ifal->ifal_index))
516 if (!tb[IFAL_ADDRESS])
519 addr = nla_data(tb[IFAL_ADDRESS]);
524 p = __ipv6_addr_label(addr, ipv6_addr_type(addr), ifal->ifal_index);
525 if (p && ip6addrlbl_hold(p))
527 lseq = ip6addrlbl_table.seq;
535 if (!(skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL))) {
540 err = ip6addrlbl_fill(skb, p, lseq,
541 NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
542 RTM_NEWADDRLABEL, 0);
547 WARN_ON(err == -EMSGSIZE);
552 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
557 void __init ipv6_addr_label_rtnl_register(void)
559 __rtnl_register(PF_INET6, RTM_NEWADDRLABEL, ip6addrlbl_newdel, NULL);
560 __rtnl_register(PF_INET6, RTM_DELADDRLABEL, ip6addrlbl_newdel, NULL);
561 __rtnl_register(PF_INET6, RTM_GETADDRLABEL, ip6addrlbl_get, ip6addrlbl_dump);