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)
61 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
63 * Note: 0xffffffff is used if we do not have any policies.
66 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
68 static const __initdata
struct ip6addrlbl_init_table
70 const struct in6_addr
*prefix
;
73 } ip6addrlbl_init_table
[] = {
75 .prefix
= &in6addr_any
,
78 .prefix
= &(struct in6_addr
){{{ 0xfc }}},
82 .prefix
= &(struct in6_addr
){{{ 0x20, 0x02 }}},
86 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01 }}},
89 },{ /* 2001:10::/28 */
90 .prefix
= &(struct in6_addr
){{{ 0x20, 0x01, 0x00, 0x10 }}},
94 .prefix
= &(struct in6_addr
){{{ [10] = 0xff, [11] = 0xff }}},
98 .prefix
= &in6addr_any
,
102 .prefix
= &in6addr_loopback
,
108 /* Object management */
109 static inline void ip6addrlbl_free(struct ip6addrlbl_entry
*p
)
114 static void ip6addrlbl_free_rcu(struct rcu_head
*h
)
116 ip6addrlbl_free(container_of(h
, struct ip6addrlbl_entry
, rcu
));
119 static inline int ip6addrlbl_hold(struct ip6addrlbl_entry
*p
)
121 return atomic_inc_not_zero(&p
->refcnt
);
124 static inline void ip6addrlbl_put(struct ip6addrlbl_entry
*p
)
126 if (atomic_dec_and_test(&p
->refcnt
))
127 call_rcu(&p
->rcu
, ip6addrlbl_free_rcu
);
131 static int __ip6addrlbl_match(struct ip6addrlbl_entry
*p
,
132 const struct in6_addr
*addr
,
133 int addrtype
, int ifindex
)
135 if (p
->ifindex
&& p
->ifindex
!= ifindex
)
137 if (p
->addrtype
&& p
->addrtype
!= addrtype
)
139 if (!ipv6_prefix_equal(addr
, &p
->prefix
, p
->prefixlen
))
144 static struct ip6addrlbl_entry
*__ipv6_addr_label(const struct in6_addr
*addr
,
145 int type
, int ifindex
)
147 struct hlist_node
*pos
;
148 struct ip6addrlbl_entry
*p
;
149 hlist_for_each_entry_rcu(p
, pos
, &ip6addrlbl_table
.head
, list
) {
150 if (__ip6addrlbl_match(p
, addr
, type
, ifindex
))
156 u32
ipv6_addr_label(const struct in6_addr
*addr
, int type
, int ifindex
)
159 struct ip6addrlbl_entry
*p
;
161 type
&= IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
;
164 p
= __ipv6_addr_label(addr
, type
, ifindex
);
165 label
= p
? p
->label
: IPV6_ADDR_LABEL_DEFAULT
;
168 ADDRLABEL(KERN_DEBUG
"%s(addr=" NIP6_FMT
", type=%d, ifindex=%d) => %08x\n",
170 NIP6(*addr
), type
, ifindex
,
176 /* allocate one entry */
177 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(const struct in6_addr
*prefix
,
178 int prefixlen
, int ifindex
,
181 struct ip6addrlbl_entry
*newp
;
184 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d, label=%u)\n",
186 NIP6(*prefix
), prefixlen
,
188 (unsigned int)label
);
190 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
193 case IPV6_ADDR_MAPPED
:
195 return ERR_PTR(-EINVAL
);
199 case IPV6_ADDR_COMPATv4
:
203 case IPV6_ADDR_LOOPBACK
:
204 if (prefixlen
!= 128)
209 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
211 return ERR_PTR(-ENOMEM
);
213 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
214 newp
->prefixlen
= prefixlen
;
215 newp
->ifindex
= ifindex
;
216 newp
->addrtype
= addrtype
;
218 INIT_HLIST_NODE(&newp
->list
);
219 atomic_set(&newp
->refcnt
, 1);
224 static int __ip6addrlbl_add(struct ip6addrlbl_entry
*newp
, int replace
)
228 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n",
232 if (hlist_empty(&ip6addrlbl_table
.head
)) {
233 hlist_add_head_rcu(&newp
->list
, &ip6addrlbl_table
.head
);
235 struct hlist_node
*pos
, *n
;
236 struct ip6addrlbl_entry
*p
= NULL
;
237 hlist_for_each_entry_safe(p
, pos
, n
,
238 &ip6addrlbl_table
.head
, list
) {
239 if (p
->prefixlen
== newp
->prefixlen
&&
240 p
->ifindex
== newp
->ifindex
&&
241 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
246 hlist_replace_rcu(&p
->list
, &newp
->list
);
249 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
250 (p
->prefixlen
< newp
->prefixlen
)) {
251 hlist_add_before_rcu(&newp
->list
, &p
->list
);
255 hlist_add_after_rcu(&p
->list
, &newp
->list
);
259 ip6addrlbl_table
.seq
++;
264 static int ip6addrlbl_add(const struct in6_addr
*prefix
, int prefixlen
,
265 int ifindex
, u32 label
, int replace
)
267 struct ip6addrlbl_entry
*newp
;
270 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
272 NIP6(*prefix
), prefixlen
,
277 newp
= ip6addrlbl_alloc(prefix
, prefixlen
, ifindex
, label
);
279 return PTR_ERR(newp
);
280 spin_lock(&ip6addrlbl_table
.lock
);
281 ret
= __ip6addrlbl_add(newp
, replace
);
282 spin_unlock(&ip6addrlbl_table
.lock
);
284 ip6addrlbl_free(newp
);
289 static int __ip6addrlbl_del(const struct in6_addr
*prefix
, int prefixlen
,
292 struct ip6addrlbl_entry
*p
= NULL
;
293 struct hlist_node
*pos
, *n
;
296 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d)\n",
298 NIP6(*prefix
), prefixlen
,
301 hlist_for_each_entry_safe(p
, pos
, n
, &ip6addrlbl_table
.head
, list
) {
302 if (p
->prefixlen
== prefixlen
&&
303 p
->ifindex
== ifindex
&&
304 ipv6_addr_equal(&p
->prefix
, prefix
)) {
305 hlist_del_rcu(&p
->list
);
314 static int ip6addrlbl_del(const struct in6_addr
*prefix
, int prefixlen
,
317 struct in6_addr prefix_buf
;
320 ADDRLABEL(KERN_DEBUG
"%s(prefix=" NIP6_FMT
", prefixlen=%d, ifindex=%d)\n",
322 NIP6(*prefix
), prefixlen
,
325 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
326 spin_lock(&ip6addrlbl_table
.lock
);
327 ret
= __ip6addrlbl_del(&prefix_buf
, prefixlen
, ifindex
);
328 spin_unlock(&ip6addrlbl_table
.lock
);
332 /* add default label */
333 static __init
int ip6addrlbl_init(void)
338 ADDRLABEL(KERN_DEBUG
"%s()\n", __func__
);
340 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
341 int ret
= ip6addrlbl_add(ip6addrlbl_init_table
[i
].prefix
,
342 ip6addrlbl_init_table
[i
].prefixlen
,
344 ip6addrlbl_init_table
[i
].label
, 0);
345 /* XXX: should we free all rules when we catch an error? */
346 if (ret
&& (!err
|| err
!= -ENOMEM
))
352 int __init
ipv6_addr_label_init(void)
354 spin_lock_init(&ip6addrlbl_table
.lock
);
356 return ip6addrlbl_init();
359 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
360 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
361 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
364 static int ip6addrlbl_newdel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
367 struct net
*net
= skb
->sk
->sk_net
;
368 struct ifaddrlblmsg
*ifal
;
369 struct nlattr
*tb
[IFAL_MAX
+1];
370 struct in6_addr
*pfx
;
374 if (net
!= &init_net
)
377 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
381 ifal
= nlmsg_data(nlh
);
383 if (ifal
->ifal_family
!= AF_INET6
||
384 ifal
->ifal_prefixlen
> 128)
387 if (ifal
->ifal_index
&&
388 !__dev_get_by_index(&init_net
, ifal
->ifal_index
))
391 if (!tb
[IFAL_ADDRESS
])
394 pfx
= nla_data(tb
[IFAL_ADDRESS
]);
400 label
= nla_get_u32(tb
[IFAL_LABEL
]);
401 if (label
== IPV6_ADDR_LABEL_DEFAULT
)
404 switch(nlh
->nlmsg_type
) {
405 case RTM_NEWADDRLABEL
:
406 err
= ip6addrlbl_add(pfx
, ifal
->ifal_prefixlen
,
407 ifal
->ifal_index
, label
,
408 nlh
->nlmsg_flags
& NLM_F_REPLACE
);
410 case RTM_DELADDRLABEL
:
411 err
= ip6addrlbl_del(pfx
, ifal
->ifal_prefixlen
,
420 static inline void ip6addrlbl_putmsg(struct nlmsghdr
*nlh
,
421 int prefixlen
, int ifindex
, u32 lseq
)
423 struct ifaddrlblmsg
*ifal
= nlmsg_data(nlh
);
424 ifal
->ifal_family
= AF_INET6
;
425 ifal
->ifal_prefixlen
= prefixlen
;
426 ifal
->ifal_flags
= 0;
427 ifal
->ifal_index
= ifindex
;
428 ifal
->ifal_seq
= lseq
;
431 static int ip6addrlbl_fill(struct sk_buff
*skb
,
432 struct ip6addrlbl_entry
*p
,
434 u32 pid
, u32 seq
, int event
,
437 struct nlmsghdr
*nlh
= nlmsg_put(skb
, pid
, seq
, event
,
438 sizeof(struct ifaddrlblmsg
), flags
);
442 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
444 if (nla_put(skb
, IFAL_ADDRESS
, 16, &p
->prefix
) < 0 ||
445 nla_put_u32(skb
, IFAL_LABEL
, p
->label
) < 0) {
446 nlmsg_cancel(skb
, nlh
);
450 return nlmsg_end(skb
, nlh
);
453 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
455 struct net
*net
= skb
->sk
->sk_net
;
456 struct ip6addrlbl_entry
*p
;
457 struct hlist_node
*pos
;
458 int idx
= 0, s_idx
= cb
->args
[0];
461 if (net
!= &init_net
)
465 hlist_for_each_entry_rcu(p
, pos
, &ip6addrlbl_table
.head
, list
) {
467 if ((err
= ip6addrlbl_fill(skb
, p
,
468 ip6addrlbl_table
.seq
,
469 NETLINK_CB(cb
->skb
).pid
,
482 static inline int ip6addrlbl_msgsize(void)
484 return (NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
485 + nla_total_size(16) /* IFAL_ADDRESS */
486 + nla_total_size(4) /* IFAL_LABEL */
490 static int ip6addrlbl_get(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
,
493 struct net
*net
= in_skb
->sk
->sk_net
;
494 struct ifaddrlblmsg
*ifal
;
495 struct nlattr
*tb
[IFAL_MAX
+1];
496 struct in6_addr
*addr
;
499 struct ip6addrlbl_entry
*p
;
502 if (net
!= &init_net
)
505 err
= nlmsg_parse(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
, ifal_policy
);
509 ifal
= nlmsg_data(nlh
);
511 if (ifal
->ifal_family
!= AF_INET6
||
512 ifal
->ifal_prefixlen
!= 128)
515 if (ifal
->ifal_index
&&
516 !__dev_get_by_index(&init_net
, ifal
->ifal_index
))
519 if (!tb
[IFAL_ADDRESS
])
522 addr
= nla_data(tb
[IFAL_ADDRESS
]);
527 p
= __ipv6_addr_label(addr
, ipv6_addr_type(addr
), ifal
->ifal_index
);
528 if (p
&& ip6addrlbl_hold(p
))
530 lseq
= ip6addrlbl_table
.seq
;
538 if (!(skb
= nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL
))) {
543 err
= ip6addrlbl_fill(skb
, p
, lseq
,
544 NETLINK_CB(in_skb
).pid
, nlh
->nlmsg_seq
,
545 RTM_NEWADDRLABEL
, 0);
550 WARN_ON(err
== -EMSGSIZE
);
555 err
= rtnl_unicast(skb
, &init_net
, NETLINK_CB(in_skb
).pid
);
560 void __init
ipv6_addr_label_rtnl_register(void)
562 __rtnl_register(PF_INET6
, RTM_NEWADDRLABEL
, ip6addrlbl_newdel
, NULL
);
563 __rtnl_register(PF_INET6
, RTM_DELADDRLABEL
, ip6addrlbl_newdel
, NULL
);
564 __rtnl_register(PF_INET6
, RTM_GETADDRLABEL
, ip6addrlbl_get
, ip6addrlbl_dump
);