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
)
64 * Default policy table (RFC3484 + extensions)
66 * prefix addr_type label
67 * -------------------------------------------------------------------------
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)
75 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
77 * Note: 0xffffffff is used if we do not have any policies.
80 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
82 static const __net_initdata
struct ip6addrlbl_init_table
84 const struct in6_addr
*prefix
;
87 } ip6addrlbl_init_table
[] = {
89 .prefix
= &in6addr_any
,
92 .prefix
= &(struct in6_addr
){{{ 0xfc }}},
96 .prefix
= &(struct in6_addr
){{{ 0x20, 0x02 }}},
100 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01 }}},
103 },{ /* 2001:10::/28 */
104 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01, 0x00, 0x10 }}},
108 .prefix
= &(struct in6_addr
){{{ [10] = 0xff, [11] = 0xff }}},
112 .prefix
= &in6addr_any
,
116 .prefix
= &in6addr_loopback
,
122 /* Object management */
123 static inline void ip6addrlbl_free(struct ip6addrlbl_entry
*p
)
126 release_net(p
->lbl_net
);
131 static void ip6addrlbl_free_rcu(struct rcu_head
*h
)
133 ip6addrlbl_free(container_of(h
, struct ip6addrlbl_entry
, rcu
));
136 static inline int ip6addrlbl_hold(struct ip6addrlbl_entry
*p
)
138 return atomic_inc_not_zero(&p
->refcnt
);
141 static inline void ip6addrlbl_put(struct ip6addrlbl_entry
*p
)
143 if (atomic_dec_and_test(&p
->refcnt
))
144 call_rcu(&p
->rcu
, ip6addrlbl_free_rcu
);
148 static int __ip6addrlbl_match(struct net
*net
,
149 struct ip6addrlbl_entry
*p
,
150 const struct in6_addr
*addr
,
151 int addrtype
, int ifindex
)
153 if (!net_eq(ip6addrlbl_net(p
), net
))
155 if (p
->ifindex
&& p
->ifindex
!= ifindex
)
157 if (p
->addrtype
&& p
->addrtype
!= addrtype
)
159 if (!ipv6_prefix_equal(addr
, &p
->prefix
, p
->prefixlen
))
164 static struct ip6addrlbl_entry
*__ipv6_addr_label(struct net
*net
,
165 const struct in6_addr
*addr
,
166 int type
, int ifindex
)
168 struct hlist_node
*pos
;
169 struct ip6addrlbl_entry
*p
;
170 hlist_for_each_entry_rcu(p
, pos
, &ip6addrlbl_table
.head
, list
) {
171 if (__ip6addrlbl_match(net
, p
, addr
, type
, ifindex
))
177 u32
ipv6_addr_label(struct net
*net
,
178 const struct in6_addr
*addr
, int type
, int ifindex
)
181 struct ip6addrlbl_entry
*p
;
183 type
&= IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
;
186 p
= __ipv6_addr_label(net
, addr
, type
, ifindex
);
187 label
= p
? p
->label
: IPV6_ADDR_LABEL_DEFAULT
;
190 ADDRLABEL(KERN_DEBUG
"%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
191 __func__
, addr
, type
, ifindex
, label
);
196 /* allocate one entry */
197 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(struct net
*net
,
198 const struct in6_addr
*prefix
,
199 int prefixlen
, int ifindex
,
202 struct ip6addrlbl_entry
*newp
;
205 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
206 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
);
208 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
211 case IPV6_ADDR_MAPPED
:
213 return ERR_PTR(-EINVAL
);
217 case IPV6_ADDR_COMPATv4
:
221 case IPV6_ADDR_LOOPBACK
:
222 if (prefixlen
!= 128)
227 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
229 return ERR_PTR(-ENOMEM
);
231 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
232 newp
->prefixlen
= prefixlen
;
233 newp
->ifindex
= ifindex
;
234 newp
->addrtype
= addrtype
;
236 INIT_HLIST_NODE(&newp
->list
);
238 newp
->lbl_net
= hold_net(net
);
240 atomic_set(&newp
->refcnt
, 1);
245 static int __ip6addrlbl_add(struct ip6addrlbl_entry
*newp
, int replace
)
249 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n",
253 if (hlist_empty(&ip6addrlbl_table
.head
)) {
254 hlist_add_head_rcu(&newp
->list
, &ip6addrlbl_table
.head
);
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
&&
261 net_eq(ip6addrlbl_net(p
), ip6addrlbl_net(newp
)) &&
262 p
->ifindex
== newp
->ifindex
&&
263 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
268 hlist_replace_rcu(&p
->list
, &newp
->list
);
271 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
272 (p
->prefixlen
< newp
->prefixlen
)) {
273 hlist_add_before_rcu(&newp
->list
, &p
->list
);
277 hlist_add_after_rcu(&p
->list
, &newp
->list
);
281 ip6addrlbl_table
.seq
++;
286 static int ip6addrlbl_add(struct net
*net
,
287 const struct in6_addr
*prefix
, int prefixlen
,
288 int ifindex
, u32 label
, int replace
)
290 struct ip6addrlbl_entry
*newp
;
293 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
294 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
,
297 newp
= ip6addrlbl_alloc(net
, prefix
, prefixlen
, ifindex
, label
);
299 return PTR_ERR(newp
);
300 spin_lock(&ip6addrlbl_table
.lock
);
301 ret
= __ip6addrlbl_add(newp
, replace
);
302 spin_unlock(&ip6addrlbl_table
.lock
);
304 ip6addrlbl_free(newp
);
309 static int __ip6addrlbl_del(struct net
*net
,
310 const struct in6_addr
*prefix
, int prefixlen
,
313 struct ip6addrlbl_entry
*p
= NULL
;
314 struct hlist_node
*pos
, *n
;
317 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
318 __func__
, prefix
, prefixlen
, ifindex
);
320 hlist_for_each_entry_safe(p
, pos
, n
, &ip6addrlbl_table
.head
, list
) {
321 if (p
->prefixlen
== prefixlen
&&
322 net_eq(ip6addrlbl_net(p
), net
) &&
323 p
->ifindex
== ifindex
&&
324 ipv6_addr_equal(&p
->prefix
, prefix
)) {
325 hlist_del_rcu(&p
->list
);
334 static int ip6addrlbl_del(struct net
*net
,
335 const struct in6_addr
*prefix
, int prefixlen
,
338 struct in6_addr prefix_buf
;
341 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
342 __func__
, prefix
, prefixlen
, ifindex
);
344 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
345 spin_lock(&ip6addrlbl_table
.lock
);
346 ret
= __ip6addrlbl_del(net
, &prefix_buf
, prefixlen
, ifindex
);
347 spin_unlock(&ip6addrlbl_table
.lock
);
351 /* add default label */
352 static int __net_init
ip6addrlbl_net_init(struct net
*net
)
357 ADDRLABEL(KERN_DEBUG
"%s()\n", __func__
);
359 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
360 int ret
= ip6addrlbl_add(net
,
361 ip6addrlbl_init_table
[i
].prefix
,
362 ip6addrlbl_init_table
[i
].prefixlen
,
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
))
372 static void __net_exit
ip6addrlbl_net_exit(struct net
*net
)
374 struct ip6addrlbl_entry
*p
= NULL
;
375 struct hlist_node
*pos
, *n
;
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
);
385 spin_unlock(&ip6addrlbl_table
.lock
);
388 static struct pernet_operations ipv6_addr_label_ops
= {
389 .init
= ip6addrlbl_net_init
,
390 .exit
= ip6addrlbl_net_exit
,
393 int __init
ipv6_addr_label_init(void)
395 spin_lock_init(&ip6addrlbl_table
.lock
);
397 return register_pernet_subsys(&ipv6_addr_label_ops
);
400 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
401 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
402 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
405 static int ip6addrlbl_newdel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
408 struct net
*net
= sock_net(skb
->sk
);
409 struct ifaddrlblmsg
*ifal
;
410 struct nlattr
*tb
[IFAL_MAX
+1];
411 struct in6_addr
*pfx
;
415 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
419 ifal
= nlmsg_data(nlh
);
421 if (ifal
->ifal_family
!= AF_INET6
||
422 ifal
->ifal_prefixlen
> 128)
425 if (ifal
->ifal_index
&&
426 !__dev_get_by_index(net
, ifal
->ifal_index
))
429 if (!tb
[IFAL_ADDRESS
])
432 pfx
= nla_data(tb
[IFAL_ADDRESS
]);
438 label
= nla_get_u32(tb
[IFAL_LABEL
]);
439 if (label
== IPV6_ADDR_LABEL_DEFAULT
)
442 switch(nlh
->nlmsg_type
) {
443 case RTM_NEWADDRLABEL
:
444 err
= ip6addrlbl_add(net
, pfx
, ifal
->ifal_prefixlen
,
445 ifal
->ifal_index
, label
,
446 nlh
->nlmsg_flags
& NLM_F_REPLACE
);
448 case RTM_DELADDRLABEL
:
449 err
= ip6addrlbl_del(net
, pfx
, ifal
->ifal_prefixlen
,
458 static inline void ip6addrlbl_putmsg(struct nlmsghdr
*nlh
,
459 int prefixlen
, int ifindex
, u32 lseq
)
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
;
469 static int ip6addrlbl_fill(struct sk_buff
*skb
,
470 struct ip6addrlbl_entry
*p
,
472 u32 pid
, u32 seq
, int event
,
475 struct nlmsghdr
*nlh
= nlmsg_put(skb
, pid
, seq
, event
,
476 sizeof(struct ifaddrlblmsg
), flags
);
480 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
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
);
488 return nlmsg_end(skb
, nlh
);
491 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
493 struct net
*net
= sock_net(skb
->sk
);
494 struct ip6addrlbl_entry
*p
;
495 struct hlist_node
*pos
;
496 int idx
= 0, s_idx
= cb
->args
[0];
500 hlist_for_each_entry_rcu(p
, pos
, &ip6addrlbl_table
.head
, list
) {
502 net_eq(ip6addrlbl_net(p
), net
)) {
503 if ((err
= ip6addrlbl_fill(skb
, p
,
504 ip6addrlbl_table
.seq
,
505 NETLINK_CB(cb
->skb
).pid
,
518 static inline int ip6addrlbl_msgsize(void)
520 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
521 + nla_total_size(16) /* IFAL_ADDRESS */
522 + 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
, NULL
);
596 __rtnl_register(PF_INET6
, RTM_DELADDRLABEL
, ip6addrlbl_newdel
, NULL
);
597 __rtnl_register(PF_INET6
, RTM_GETADDRLABEL
, ip6addrlbl_get
, ip6addrlbl_dump
);