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>
21 #include <linux/refcount.h>
24 #define ADDRLABEL(x...) printk(x)
26 #define ADDRLABEL(x...) do { ; } while (0)
32 struct ip6addrlbl_entry
{
33 possible_net_t lbl_net
;
34 struct in6_addr prefix
;
39 struct hlist_node list
;
44 static struct ip6addrlbl_table
46 struct hlist_head head
;
52 struct net
*ip6addrlbl_net(const struct ip6addrlbl_entry
*lbl
)
54 return read_pnet(&lbl
->lbl_net
);
58 * Default policy table (RFC6724 + extensions)
60 * prefix addr_type label
61 * -------------------------------------------------------------------------
66 * ::ffff:0:0/96 V4MAPPED 4
67 * fc00::/7 N/A 5 ULA (RFC 4193)
68 * 2001::/32 N/A 6 Teredo (RFC 4380)
69 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
70 * fec0::/10 N/A 11 Site-local
71 * (deprecated by RFC3879)
72 * 3ffe::/16 N/A 12 6bone
74 * Note: 0xffffffff is used if we do not have any policies.
75 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
78 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
80 static const __net_initconst
struct ip6addrlbl_init_table
82 const struct in6_addr
*prefix
;
85 } ip6addrlbl_init_table
[] = {
87 .prefix
= &in6addr_any
,
90 .prefix
= &(struct in6_addr
){ { { 0xfc } } } ,
94 .prefix
= &(struct in6_addr
){ { { 0xfe, 0xc0 } } },
98 .prefix
= &(struct in6_addr
){ { { 0x20, 0x02 } } },
102 .prefix
= &(struct in6_addr
){ { { 0x3f, 0xfe } } },
106 .prefix
= &(struct in6_addr
){ { { 0x20, 0x01 } } },
109 }, { /* 2001:10::/28 */
110 .prefix
= &(struct in6_addr
){ { { 0x20, 0x01, 0x00, 0x10 } } },
113 }, { /* ::ffff:0:0 */
114 .prefix
= &(struct in6_addr
){ { { [10] = 0xff, [11] = 0xff } } },
118 .prefix
= &in6addr_any
,
122 .prefix
= &in6addr_loopback
,
128 /* Object management */
129 static inline void ip6addrlbl_free(struct ip6addrlbl_entry
*p
)
134 static void ip6addrlbl_free_rcu(struct rcu_head
*h
)
136 ip6addrlbl_free(container_of(h
, struct ip6addrlbl_entry
, rcu
));
139 static bool ip6addrlbl_hold(struct ip6addrlbl_entry
*p
)
141 return refcount_inc_not_zero(&p
->refcnt
);
144 static inline void ip6addrlbl_put(struct ip6addrlbl_entry
*p
)
146 if (refcount_dec_and_test(&p
->refcnt
))
147 call_rcu(&p
->rcu
, ip6addrlbl_free_rcu
);
151 static bool __ip6addrlbl_match(struct net
*net
,
152 const struct ip6addrlbl_entry
*p
,
153 const struct in6_addr
*addr
,
154 int addrtype
, int ifindex
)
156 if (!net_eq(ip6addrlbl_net(p
), net
))
158 if (p
->ifindex
&& p
->ifindex
!= ifindex
)
160 if (p
->addrtype
&& p
->addrtype
!= addrtype
)
162 if (!ipv6_prefix_equal(addr
, &p
->prefix
, p
->prefixlen
))
167 static struct ip6addrlbl_entry
*__ipv6_addr_label(struct net
*net
,
168 const struct in6_addr
*addr
,
169 int type
, int ifindex
)
171 struct ip6addrlbl_entry
*p
;
172 hlist_for_each_entry_rcu(p
, &ip6addrlbl_table
.head
, list
) {
173 if (__ip6addrlbl_match(net
, p
, addr
, type
, ifindex
))
179 u32
ipv6_addr_label(struct net
*net
,
180 const struct in6_addr
*addr
, int type
, int ifindex
)
183 struct ip6addrlbl_entry
*p
;
185 type
&= IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
;
188 p
= __ipv6_addr_label(net
, addr
, type
, ifindex
);
189 label
= p
? p
->label
: IPV6_ADDR_LABEL_DEFAULT
;
192 ADDRLABEL(KERN_DEBUG
"%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
193 __func__
, addr
, type
, ifindex
, label
);
198 /* allocate one entry */
199 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(struct net
*net
,
200 const struct in6_addr
*prefix
,
201 int prefixlen
, int ifindex
,
204 struct ip6addrlbl_entry
*newp
;
207 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
208 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
);
210 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
213 case IPV6_ADDR_MAPPED
:
215 return ERR_PTR(-EINVAL
);
219 case IPV6_ADDR_COMPATv4
:
223 case IPV6_ADDR_LOOPBACK
:
224 if (prefixlen
!= 128)
229 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
231 return ERR_PTR(-ENOMEM
);
233 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
234 newp
->prefixlen
= prefixlen
;
235 newp
->ifindex
= ifindex
;
236 newp
->addrtype
= addrtype
;
238 INIT_HLIST_NODE(&newp
->list
);
239 write_pnet(&newp
->lbl_net
, net
);
240 refcount_set(&newp
->refcnt
, 1);
245 static int __ip6addrlbl_add(struct ip6addrlbl_entry
*newp
, int replace
)
247 struct hlist_node
*n
;
248 struct ip6addrlbl_entry
*last
= NULL
, *p
= NULL
;
251 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n", __func__
, newp
,
254 hlist_for_each_entry_safe(p
, n
, &ip6addrlbl_table
.head
, list
) {
255 if (p
->prefixlen
== newp
->prefixlen
&&
256 net_eq(ip6addrlbl_net(p
), ip6addrlbl_net(newp
)) &&
257 p
->ifindex
== newp
->ifindex
&&
258 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
263 hlist_replace_rcu(&p
->list
, &newp
->list
);
266 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
267 (p
->prefixlen
< newp
->prefixlen
)) {
268 hlist_add_before_rcu(&newp
->list
, &p
->list
);
274 hlist_add_behind_rcu(&newp
->list
, &last
->list
);
276 hlist_add_head_rcu(&newp
->list
, &ip6addrlbl_table
.head
);
279 ip6addrlbl_table
.seq
++;
284 static int ip6addrlbl_add(struct net
*net
,
285 const struct in6_addr
*prefix
, int prefixlen
,
286 int ifindex
, u32 label
, int replace
)
288 struct ip6addrlbl_entry
*newp
;
291 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
292 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
,
295 newp
= ip6addrlbl_alloc(net
, prefix
, prefixlen
, ifindex
, label
);
297 return PTR_ERR(newp
);
298 spin_lock(&ip6addrlbl_table
.lock
);
299 ret
= __ip6addrlbl_add(newp
, replace
);
300 spin_unlock(&ip6addrlbl_table
.lock
);
302 ip6addrlbl_free(newp
);
307 static int __ip6addrlbl_del(struct net
*net
,
308 const struct in6_addr
*prefix
, int prefixlen
,
311 struct ip6addrlbl_entry
*p
= NULL
;
312 struct hlist_node
*n
;
315 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
316 __func__
, prefix
, prefixlen
, ifindex
);
318 hlist_for_each_entry_safe(p
, n
, &ip6addrlbl_table
.head
, list
) {
319 if (p
->prefixlen
== prefixlen
&&
320 net_eq(ip6addrlbl_net(p
), net
) &&
321 p
->ifindex
== ifindex
&&
322 ipv6_addr_equal(&p
->prefix
, prefix
)) {
323 hlist_del_rcu(&p
->list
);
332 static int ip6addrlbl_del(struct net
*net
,
333 const struct in6_addr
*prefix
, int prefixlen
,
336 struct in6_addr prefix_buf
;
339 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
340 __func__
, prefix
, prefixlen
, ifindex
);
342 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
343 spin_lock(&ip6addrlbl_table
.lock
);
344 ret
= __ip6addrlbl_del(net
, &prefix_buf
, prefixlen
, ifindex
);
345 spin_unlock(&ip6addrlbl_table
.lock
);
349 /* add default label */
350 static int __net_init
ip6addrlbl_net_init(struct net
*net
)
355 ADDRLABEL(KERN_DEBUG
"%s\n", __func__
);
357 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
358 int ret
= ip6addrlbl_add(net
,
359 ip6addrlbl_init_table
[i
].prefix
,
360 ip6addrlbl_init_table
[i
].prefixlen
,
362 ip6addrlbl_init_table
[i
].label
, 0);
363 /* XXX: should we free all rules when we catch an error? */
364 if (ret
&& (!err
|| err
!= -ENOMEM
))
370 static void __net_exit
ip6addrlbl_net_exit(struct net
*net
)
372 struct ip6addrlbl_entry
*p
= NULL
;
373 struct hlist_node
*n
;
375 /* Remove all labels belonging to the exiting net */
376 spin_lock(&ip6addrlbl_table
.lock
);
377 hlist_for_each_entry_safe(p
, n
, &ip6addrlbl_table
.head
, list
) {
378 if (net_eq(ip6addrlbl_net(p
), net
)) {
379 hlist_del_rcu(&p
->list
);
383 spin_unlock(&ip6addrlbl_table
.lock
);
386 static struct pernet_operations ipv6_addr_label_ops
= {
387 .init
= ip6addrlbl_net_init
,
388 .exit
= ip6addrlbl_net_exit
,
391 int __init
ipv6_addr_label_init(void)
393 spin_lock_init(&ip6addrlbl_table
.lock
);
395 return register_pernet_subsys(&ipv6_addr_label_ops
);
398 void ipv6_addr_label_cleanup(void)
400 unregister_pernet_subsys(&ipv6_addr_label_ops
);
403 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
404 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
405 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
408 static int ip6addrlbl_newdel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
409 struct netlink_ext_ack
*extack
)
411 struct net
*net
= sock_net(skb
->sk
);
412 struct ifaddrlblmsg
*ifal
;
413 struct nlattr
*tb
[IFAL_MAX
+1];
414 struct in6_addr
*pfx
;
418 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
,
423 ifal
= nlmsg_data(nlh
);
425 if (ifal
->ifal_family
!= AF_INET6
||
426 ifal
->ifal_prefixlen
> 128)
429 if (!tb
[IFAL_ADDRESS
])
431 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 portid
, u32 seq
, int event
,
476 struct nlmsghdr
*nlh
= nlmsg_put(skb
, portid
, seq
, event
,
477 sizeof(struct ifaddrlblmsg
), flags
);
481 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
483 if (nla_put_in6_addr(skb
, IFAL_ADDRESS
, &p
->prefix
) < 0 ||
484 nla_put_u32(skb
, IFAL_LABEL
, p
->label
) < 0) {
485 nlmsg_cancel(skb
, nlh
);
493 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
495 struct net
*net
= sock_net(skb
->sk
);
496 struct ip6addrlbl_entry
*p
;
497 int idx
= 0, s_idx
= cb
->args
[0];
501 hlist_for_each_entry_rcu(p
, &ip6addrlbl_table
.head
, list
) {
503 net_eq(ip6addrlbl_net(p
), net
)) {
504 err
= ip6addrlbl_fill(skb
, p
,
505 ip6addrlbl_table
.seq
,
506 NETLINK_CB(cb
->skb
).portid
,
520 static inline int ip6addrlbl_msgsize(void)
522 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
523 + nla_total_size(16) /* IFAL_ADDRESS */
524 + nla_total_size(4); /* IFAL_LABEL */
527 static int ip6addrlbl_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
528 struct netlink_ext_ack
*extack
)
530 struct net
*net
= sock_net(in_skb
->sk
);
531 struct ifaddrlblmsg
*ifal
;
532 struct nlattr
*tb
[IFAL_MAX
+1];
533 struct in6_addr
*addr
;
536 struct ip6addrlbl_entry
*p
;
539 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
,
544 ifal
= nlmsg_data(nlh
);
546 if (ifal
->ifal_family
!= AF_INET6
||
547 ifal
->ifal_prefixlen
!= 128)
550 if (ifal
->ifal_index
&&
551 !__dev_get_by_index(net
, ifal
->ifal_index
))
554 if (!tb
[IFAL_ADDRESS
])
556 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 skb
= nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL
);
576 err
= ip6addrlbl_fill(skb
, p
, lseq
,
577 NETLINK_CB(in_skb
).portid
, nlh
->nlmsg_seq
,
578 RTM_NEWADDRLABEL
, 0);
583 WARN_ON(err
== -EMSGSIZE
);
588 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
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
);