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=" NIP6_FMT
", type=%d, ifindex=%d) => %08x\n",
191 NIP6(*addr
), type
, ifindex
,
197 /* allocate one entry */
198 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(struct net
*net
,
199 const struct in6_addr
*prefix
,
200 int prefixlen
, int ifindex
,
203 struct ip6addrlbl_entry
*newp
;
206 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d, label=%u)\n",
208 NIP6(*prefix
), prefixlen
,
210 (unsigned int)label
);
212 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
215 case IPV6_ADDR_MAPPED
:
217 return ERR_PTR(-EINVAL
);
221 case IPV6_ADDR_COMPATv4
:
225 case IPV6_ADDR_LOOPBACK
:
226 if (prefixlen
!= 128)
231 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
233 return ERR_PTR(-ENOMEM
);
235 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
236 newp
->prefixlen
= prefixlen
;
237 newp
->ifindex
= ifindex
;
238 newp
->addrtype
= addrtype
;
240 INIT_HLIST_NODE(&newp
->list
);
242 newp
->lbl_net
= hold_net(net
);
244 atomic_set(&newp
->refcnt
, 1);
249 static int __ip6addrlbl_add(struct ip6addrlbl_entry
*newp
, int replace
)
253 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n",
257 if (hlist_empty(&ip6addrlbl_table
.head
)) {
258 hlist_add_head_rcu(&newp
->list
, &ip6addrlbl_table
.head
);
260 struct hlist_node
*pos
, *n
;
261 struct ip6addrlbl_entry
*p
= NULL
;
262 hlist_for_each_entry_safe(p
, pos
, n
,
263 &ip6addrlbl_table
.head
, list
) {
264 if (p
->prefixlen
== newp
->prefixlen
&&
265 net_eq(ip6addrlbl_net(p
), ip6addrlbl_net(newp
)) &&
266 p
->ifindex
== newp
->ifindex
&&
267 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
272 hlist_replace_rcu(&p
->list
, &newp
->list
);
275 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
276 (p
->prefixlen
< newp
->prefixlen
)) {
277 hlist_add_before_rcu(&newp
->list
, &p
->list
);
281 hlist_add_after_rcu(&p
->list
, &newp
->list
);
285 ip6addrlbl_table
.seq
++;
290 static int ip6addrlbl_add(struct net
*net
,
291 const struct in6_addr
*prefix
, int prefixlen
,
292 int ifindex
, u32 label
, int replace
)
294 struct ip6addrlbl_entry
*newp
;
297 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
299 NIP6(*prefix
), prefixlen
,
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
*pos
, *n
;
324 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d)\n",
326 NIP6(*prefix
), prefixlen
,
329 hlist_for_each_entry_safe(p
, pos
, n
, &ip6addrlbl_table
.head
, list
) {
330 if (p
->prefixlen
== prefixlen
&&
331 net_eq(ip6addrlbl_net(p
), net
) &&
332 p
->ifindex
== ifindex
&&
333 ipv6_addr_equal(&p
->prefix
, prefix
)) {
334 hlist_del_rcu(&p
->list
);
343 static int ip6addrlbl_del(struct net
*net
,
344 const struct in6_addr
*prefix
, int prefixlen
,
347 struct in6_addr prefix_buf
;
350 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d)\n",
352 NIP6(*prefix
), prefixlen
,
355 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
356 spin_lock(&ip6addrlbl_table
.lock
);
357 ret
= __ip6addrlbl_del(net
, &prefix_buf
, prefixlen
, ifindex
);
358 spin_unlock(&ip6addrlbl_table
.lock
);
362 /* add default label */
363 static int __net_init
ip6addrlbl_net_init(struct net
*net
)
368 ADDRLABEL(KERN_DEBUG
"%s()\n", __func__
);
370 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
371 int ret
= ip6addrlbl_add(net
,
372 ip6addrlbl_init_table
[i
].prefix
,
373 ip6addrlbl_init_table
[i
].prefixlen
,
375 ip6addrlbl_init_table
[i
].label
, 0);
376 /* XXX: should we free all rules when we catch an error? */
377 if (ret
&& (!err
|| err
!= -ENOMEM
))
383 static void __net_exit
ip6addrlbl_net_exit(struct net
*net
)
385 struct ip6addrlbl_entry
*p
= NULL
;
386 struct hlist_node
*pos
, *n
;
388 /* Remove all labels belonging to the exiting net */
389 spin_lock(&ip6addrlbl_table
.lock
);
390 hlist_for_each_entry_safe(p
, pos
, n
, &ip6addrlbl_table
.head
, list
) {
391 if (net_eq(ip6addrlbl_net(p
), net
)) {
392 hlist_del_rcu(&p
->list
);
396 spin_unlock(&ip6addrlbl_table
.lock
);
399 static struct pernet_operations ipv6_addr_label_ops
= {
400 .init
= ip6addrlbl_net_init
,
401 .exit
= ip6addrlbl_net_exit
,
404 int __init
ipv6_addr_label_init(void)
406 spin_lock_init(&ip6addrlbl_table
.lock
);
408 return register_pernet_subsys(&ipv6_addr_label_ops
);
411 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
412 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
413 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
416 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 (ifal
->ifal_index
&&
437 !__dev_get_by_index(net
, ifal
->ifal_index
))
440 if (!tb
[IFAL_ADDRESS
])
443 pfx
= nla_data(tb
[IFAL_ADDRESS
]);
449 label
= nla_get_u32(tb
[IFAL_LABEL
]);
450 if (label
== IPV6_ADDR_LABEL_DEFAULT
)
453 switch(nlh
->nlmsg_type
) {
454 case RTM_NEWADDRLABEL
:
455 err
= ip6addrlbl_add(net
, pfx
, ifal
->ifal_prefixlen
,
456 ifal
->ifal_index
, label
,
457 nlh
->nlmsg_flags
& NLM_F_REPLACE
);
459 case RTM_DELADDRLABEL
:
460 err
= ip6addrlbl_del(net
, pfx
, ifal
->ifal_prefixlen
,
469 static inline void ip6addrlbl_putmsg(struct nlmsghdr
*nlh
,
470 int prefixlen
, int ifindex
, u32 lseq
)
472 struct ifaddrlblmsg
*ifal
= nlmsg_data(nlh
);
473 ifal
->ifal_family
= AF_INET6
;
474 ifal
->ifal_prefixlen
= prefixlen
;
475 ifal
->ifal_flags
= 0;
476 ifal
->ifal_index
= ifindex
;
477 ifal
->ifal_seq
= lseq
;
480 static int ip6addrlbl_fill(struct sk_buff
*skb
,
481 struct ip6addrlbl_entry
*p
,
483 u32 pid
, u32 seq
, int event
,
486 struct nlmsghdr
*nlh
= nlmsg_put(skb
, pid
, seq
, event
,
487 sizeof(struct ifaddrlblmsg
), flags
);
491 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
493 if (nla_put(skb
, IFAL_ADDRESS
, 16, &p
->prefix
) < 0 ||
494 nla_put_u32(skb
, IFAL_LABEL
, p
->label
) < 0) {
495 nlmsg_cancel(skb
, nlh
);
499 return nlmsg_end(skb
, nlh
);
502 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
504 struct net
*net
= sock_net(skb
->sk
);
505 struct ip6addrlbl_entry
*p
;
506 struct hlist_node
*pos
;
507 int idx
= 0, s_idx
= cb
->args
[0];
511 hlist_for_each_entry_rcu(p
, pos
, &ip6addrlbl_table
.head
, list
) {
513 net_eq(ip6addrlbl_net(p
), net
)) {
514 if ((err
= ip6addrlbl_fill(skb
, p
,
515 ip6addrlbl_table
.seq
,
516 NETLINK_CB(cb
->skb
).pid
,
529 static inline int ip6addrlbl_msgsize(void)
531 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
532 + nla_total_size(16) /* IFAL_ADDRESS */
533 + nla_total_size(4) /* IFAL_LABEL */
537 static int ip6addrlbl_get(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
,
540 struct net
*net
= sock_net(in_skb
->sk
);
541 struct ifaddrlblmsg
*ifal
;
542 struct nlattr
*tb
[IFAL_MAX
+1];
543 struct in6_addr
*addr
;
546 struct ip6addrlbl_entry
*p
;
549 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
553 ifal
= nlmsg_data(nlh
);
555 if (ifal
->ifal_family
!= AF_INET6
||
556 ifal
->ifal_prefixlen
!= 128)
559 if (ifal
->ifal_index
&&
560 !__dev_get_by_index(net
, ifal
->ifal_index
))
563 if (!tb
[IFAL_ADDRESS
])
566 addr
= nla_data(tb
[IFAL_ADDRESS
]);
571 p
= __ipv6_addr_label(net
, addr
, ipv6_addr_type(addr
), ifal
->ifal_index
);
572 if (p
&& ip6addrlbl_hold(p
))
574 lseq
= ip6addrlbl_table
.seq
;
582 if (!(skb
= nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL
))) {
587 err
= ip6addrlbl_fill(skb
, p
, lseq
,
588 NETLINK_CB(in_skb
).pid
, nlh
->nlmsg_seq
,
589 RTM_NEWADDRLABEL
, 0);
594 WARN_ON(err
== -EMSGSIZE
);
599 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).pid
);
604 void __init
ipv6_addr_label_rtnl_register(void)
606 __rtnl_register(PF_INET6
, RTM_NEWADDRLABEL
, ip6addrlbl_newdel
, NULL
);
607 __rtnl_register(PF_INET6
, RTM_DELADDRLABEL
, ip6addrlbl_newdel
, NULL
);
608 __rtnl_register(PF_INET6
, RTM_GETADDRLABEL
, ip6addrlbl_get
, ip6addrlbl_dump
);