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 (RFC6724 + 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)
72 * fec0::/10 N/A 11 Site-local
73 * (deprecated by RFC3879)
74 * 3ffe::/16 N/A 12 6bone
76 * Note: 0xffffffff is used if we do not have any policies.
77 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
80 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
82 static const __net_initconst
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
){{{ 0xfe, 0xc0 }}},
100 .prefix
= &(struct in6_addr
){{{ 0x20, 0x02 }}},
104 .prefix
= &(struct in6_addr
){{{ 0x3f, 0xfe }}},
108 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01 }}},
111 },{ /* 2001:10::/28 */
112 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01, 0x00, 0x10 }}},
116 .prefix
= &(struct in6_addr
){{{ [10] = 0xff, [11] = 0xff }}},
120 .prefix
= &in6addr_any
,
124 .prefix
= &in6addr_loopback
,
130 /* Object management */
131 static inline void ip6addrlbl_free(struct ip6addrlbl_entry
*p
)
134 release_net(p
->lbl_net
);
139 static void ip6addrlbl_free_rcu(struct rcu_head
*h
)
141 ip6addrlbl_free(container_of(h
, struct ip6addrlbl_entry
, rcu
));
144 static bool ip6addrlbl_hold(struct ip6addrlbl_entry
*p
)
146 return atomic_inc_not_zero(&p
->refcnt
);
149 static inline void ip6addrlbl_put(struct ip6addrlbl_entry
*p
)
151 if (atomic_dec_and_test(&p
->refcnt
))
152 call_rcu(&p
->rcu
, ip6addrlbl_free_rcu
);
156 static bool __ip6addrlbl_match(struct net
*net
,
157 const struct ip6addrlbl_entry
*p
,
158 const struct in6_addr
*addr
,
159 int addrtype
, int ifindex
)
161 if (!net_eq(ip6addrlbl_net(p
), net
))
163 if (p
->ifindex
&& p
->ifindex
!= ifindex
)
165 if (p
->addrtype
&& p
->addrtype
!= addrtype
)
167 if (!ipv6_prefix_equal(addr
, &p
->prefix
, p
->prefixlen
))
172 static struct ip6addrlbl_entry
*__ipv6_addr_label(struct net
*net
,
173 const struct in6_addr
*addr
,
174 int type
, int ifindex
)
176 struct ip6addrlbl_entry
*p
;
177 hlist_for_each_entry_rcu(p
, &ip6addrlbl_table
.head
, list
) {
178 if (__ip6addrlbl_match(net
, p
, addr
, type
, ifindex
))
184 u32
ipv6_addr_label(struct net
*net
,
185 const struct in6_addr
*addr
, int type
, int ifindex
)
188 struct ip6addrlbl_entry
*p
;
190 type
&= IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
;
193 p
= __ipv6_addr_label(net
, addr
, type
, ifindex
);
194 label
= p
? p
->label
: IPV6_ADDR_LABEL_DEFAULT
;
197 ADDRLABEL(KERN_DEBUG
"%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
198 __func__
, addr
, type
, ifindex
, label
);
203 /* allocate one entry */
204 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(struct net
*net
,
205 const struct in6_addr
*prefix
,
206 int prefixlen
, int ifindex
,
209 struct ip6addrlbl_entry
*newp
;
212 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
213 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
);
215 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
218 case IPV6_ADDR_MAPPED
:
220 return ERR_PTR(-EINVAL
);
224 case IPV6_ADDR_COMPATv4
:
228 case IPV6_ADDR_LOOPBACK
:
229 if (prefixlen
!= 128)
234 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
236 return ERR_PTR(-ENOMEM
);
238 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
239 newp
->prefixlen
= prefixlen
;
240 newp
->ifindex
= ifindex
;
241 newp
->addrtype
= addrtype
;
243 INIT_HLIST_NODE(&newp
->list
);
245 newp
->lbl_net
= hold_net(net
);
247 atomic_set(&newp
->refcnt
, 1);
252 static int __ip6addrlbl_add(struct ip6addrlbl_entry
*newp
, int replace
)
256 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n",
260 if (hlist_empty(&ip6addrlbl_table
.head
)) {
261 hlist_add_head_rcu(&newp
->list
, &ip6addrlbl_table
.head
);
263 struct hlist_node
*n
;
264 struct ip6addrlbl_entry
*p
= NULL
;
265 hlist_for_each_entry_safe(p
, n
,
266 &ip6addrlbl_table
.head
, list
) {
267 if (p
->prefixlen
== newp
->prefixlen
&&
268 net_eq(ip6addrlbl_net(p
), ip6addrlbl_net(newp
)) &&
269 p
->ifindex
== newp
->ifindex
&&
270 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
275 hlist_replace_rcu(&p
->list
, &newp
->list
);
278 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
279 (p
->prefixlen
< newp
->prefixlen
)) {
280 hlist_add_before_rcu(&newp
->list
, &p
->list
);
284 hlist_add_after_rcu(&p
->list
, &newp
->list
);
288 ip6addrlbl_table
.seq
++;
293 static int ip6addrlbl_add(struct net
*net
,
294 const struct in6_addr
*prefix
, int prefixlen
,
295 int ifindex
, u32 label
, int replace
)
297 struct ip6addrlbl_entry
*newp
;
300 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
301 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
,
304 newp
= ip6addrlbl_alloc(net
, prefix
, prefixlen
, ifindex
, label
);
306 return PTR_ERR(newp
);
307 spin_lock(&ip6addrlbl_table
.lock
);
308 ret
= __ip6addrlbl_add(newp
, replace
);
309 spin_unlock(&ip6addrlbl_table
.lock
);
311 ip6addrlbl_free(newp
);
316 static int __ip6addrlbl_del(struct net
*net
,
317 const struct in6_addr
*prefix
, int prefixlen
,
320 struct ip6addrlbl_entry
*p
= NULL
;
321 struct hlist_node
*n
;
324 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
325 __func__
, prefix
, prefixlen
, ifindex
);
327 hlist_for_each_entry_safe(p
, n
, &ip6addrlbl_table
.head
, list
) {
328 if (p
->prefixlen
== prefixlen
&&
329 net_eq(ip6addrlbl_net(p
), net
) &&
330 p
->ifindex
== ifindex
&&
331 ipv6_addr_equal(&p
->prefix
, prefix
)) {
332 hlist_del_rcu(&p
->list
);
341 static int ip6addrlbl_del(struct net
*net
,
342 const struct in6_addr
*prefix
, int prefixlen
,
345 struct in6_addr prefix_buf
;
348 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
349 __func__
, prefix
, prefixlen
, ifindex
);
351 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
352 spin_lock(&ip6addrlbl_table
.lock
);
353 ret
= __ip6addrlbl_del(net
, &prefix_buf
, prefixlen
, ifindex
);
354 spin_unlock(&ip6addrlbl_table
.lock
);
358 /* add default label */
359 static int __net_init
ip6addrlbl_net_init(struct net
*net
)
364 ADDRLABEL(KERN_DEBUG
"%s\n", __func__
);
366 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
367 int ret
= ip6addrlbl_add(net
,
368 ip6addrlbl_init_table
[i
].prefix
,
369 ip6addrlbl_init_table
[i
].prefixlen
,
371 ip6addrlbl_init_table
[i
].label
, 0);
372 /* XXX: should we free all rules when we catch an error? */
373 if (ret
&& (!err
|| err
!= -ENOMEM
))
379 static void __net_exit
ip6addrlbl_net_exit(struct net
*net
)
381 struct ip6addrlbl_entry
*p
= NULL
;
382 struct hlist_node
*n
;
384 /* Remove all labels belonging to the exiting net */
385 spin_lock(&ip6addrlbl_table
.lock
);
386 hlist_for_each_entry_safe(p
, n
, &ip6addrlbl_table
.head
, list
) {
387 if (net_eq(ip6addrlbl_net(p
), net
)) {
388 hlist_del_rcu(&p
->list
);
392 spin_unlock(&ip6addrlbl_table
.lock
);
395 static struct pernet_operations ipv6_addr_label_ops
= {
396 .init
= ip6addrlbl_net_init
,
397 .exit
= ip6addrlbl_net_exit
,
400 int __init
ipv6_addr_label_init(void)
402 spin_lock_init(&ip6addrlbl_table
.lock
);
404 return register_pernet_subsys(&ipv6_addr_label_ops
);
407 void ipv6_addr_label_cleanup(void)
409 unregister_pernet_subsys(&ipv6_addr_label_ops
);
412 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
413 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
414 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
417 static int ip6addrlbl_newdel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
419 struct net
*net
= sock_net(skb
->sk
);
420 struct ifaddrlblmsg
*ifal
;
421 struct nlattr
*tb
[IFAL_MAX
+1];
422 struct in6_addr
*pfx
;
426 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
430 ifal
= nlmsg_data(nlh
);
432 if (ifal
->ifal_family
!= AF_INET6
||
433 ifal
->ifal_prefixlen
> 128)
436 if (!tb
[IFAL_ADDRESS
])
438 pfx
= nla_data(tb
[IFAL_ADDRESS
]);
442 label
= nla_get_u32(tb
[IFAL_LABEL
]);
443 if (label
== IPV6_ADDR_LABEL_DEFAULT
)
446 switch(nlh
->nlmsg_type
) {
447 case RTM_NEWADDRLABEL
:
448 if (ifal
->ifal_index
&&
449 !__dev_get_by_index(net
, ifal
->ifal_index
))
452 err
= ip6addrlbl_add(net
, pfx
, ifal
->ifal_prefixlen
,
453 ifal
->ifal_index
, label
,
454 nlh
->nlmsg_flags
& NLM_F_REPLACE
);
456 case RTM_DELADDRLABEL
:
457 err
= ip6addrlbl_del(net
, pfx
, ifal
->ifal_prefixlen
,
466 static void ip6addrlbl_putmsg(struct nlmsghdr
*nlh
,
467 int prefixlen
, int ifindex
, u32 lseq
)
469 struct ifaddrlblmsg
*ifal
= nlmsg_data(nlh
);
470 ifal
->ifal_family
= AF_INET6
;
471 ifal
->ifal_prefixlen
= prefixlen
;
472 ifal
->ifal_flags
= 0;
473 ifal
->ifal_index
= ifindex
;
474 ifal
->ifal_seq
= lseq
;
477 static int ip6addrlbl_fill(struct sk_buff
*skb
,
478 struct ip6addrlbl_entry
*p
,
480 u32 portid
, u32 seq
, int event
,
483 struct nlmsghdr
*nlh
= nlmsg_put(skb
, portid
, seq
, event
,
484 sizeof(struct ifaddrlblmsg
), flags
);
488 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
490 if (nla_put(skb
, IFAL_ADDRESS
, 16, &p
->prefix
) < 0 ||
491 nla_put_u32(skb
, IFAL_LABEL
, p
->label
) < 0) {
492 nlmsg_cancel(skb
, nlh
);
496 return nlmsg_end(skb
, nlh
);
499 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
501 struct net
*net
= sock_net(skb
->sk
);
502 struct ip6addrlbl_entry
*p
;
503 int idx
= 0, s_idx
= cb
->args
[0];
507 hlist_for_each_entry_rcu(p
, &ip6addrlbl_table
.head
, list
) {
509 net_eq(ip6addrlbl_net(p
), net
)) {
510 if ((err
= ip6addrlbl_fill(skb
, p
,
511 ip6addrlbl_table
.seq
,
512 NETLINK_CB(cb
->skb
).portid
,
525 static inline int ip6addrlbl_msgsize(void)
527 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
528 + nla_total_size(16) /* IFAL_ADDRESS */
529 + nla_total_size(4); /* IFAL_LABEL */
532 static int ip6addrlbl_get(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
)
534 struct net
*net
= sock_net(in_skb
->sk
);
535 struct ifaddrlblmsg
*ifal
;
536 struct nlattr
*tb
[IFAL_MAX
+1];
537 struct in6_addr
*addr
;
540 struct ip6addrlbl_entry
*p
;
543 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
547 ifal
= nlmsg_data(nlh
);
549 if (ifal
->ifal_family
!= AF_INET6
||
550 ifal
->ifal_prefixlen
!= 128)
553 if (ifal
->ifal_index
&&
554 !__dev_get_by_index(net
, ifal
->ifal_index
))
557 if (!tb
[IFAL_ADDRESS
])
559 addr
= nla_data(tb
[IFAL_ADDRESS
]);
562 p
= __ipv6_addr_label(net
, addr
, ipv6_addr_type(addr
), ifal
->ifal_index
);
563 if (p
&& ip6addrlbl_hold(p
))
565 lseq
= ip6addrlbl_table
.seq
;
573 if (!(skb
= nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL
))) {
578 err
= ip6addrlbl_fill(skb
, p
, lseq
,
579 NETLINK_CB(in_skb
).portid
, nlh
->nlmsg_seq
,
580 RTM_NEWADDRLABEL
, 0);
585 WARN_ON(err
== -EMSGSIZE
);
590 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
595 void __init
ipv6_addr_label_rtnl_register(void)
597 __rtnl_register(PF_INET6
, RTM_NEWADDRLABEL
, ip6addrlbl_newdel
,
599 __rtnl_register(PF_INET6
, RTM_DELADDRLABEL
, ip6addrlbl_newdel
,
601 __rtnl_register(PF_INET6
, RTM_GETADDRLABEL
, ip6addrlbl_get
,
602 ip6addrlbl_dump
, NULL
);