12 #include <linux/kernel.h>
13 #include <linux/list.h>
15 #include <linux/in6.h>
16 #include <linux/slab.h>
19 #include <linux/netlink.h>
20 #include <linux/rtnetlink.h>
23 #define ADDRLABEL(x...) printk(x)
25 #define ADDRLABEL(x...) do { ; } while(0)
46 static struct ip6addrlbl_table
80 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
87 } ip6addrlbl_init_table[] = {
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 }}},
112 .prefix = &(
struct in6_addr){{{ 0x20, 0x01, 0x00, 0x10 }}},
116 .prefix = &(
struct in6_addr){{{ [10] = 0xff, [11] = 0xff }}},
134 release_net(p->lbl_net);
139 static void ip6addrlbl_free_rcu(
struct rcu_head *
h)
156 static bool __ip6addrlbl_match(
struct net *
net,
159 int addrtype,
int ifindex)
161 if (!net_eq(ip6addrlbl_net(p), net))
174 int type,
int ifindex)
178 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head,
list) {
179 if (__ip6addrlbl_match(net, p, addr, type, ifindex))
186 const struct in6_addr *addr,
int type,
int ifindex)
194 p = __ipv6_addr_label(net, addr, type, ifindex);
199 __func__, addr, type, ifindex, label);
214 __func__, prefix, prefixlen, ifindex, (
unsigned int)label);
230 if (prefixlen != 128)
239 ipv6_addr_prefix(&newp->
prefix, prefix, prefixlen);
244 INIT_HLIST_NODE(&newp->
list);
246 newp->lbl_net = hold_net(net);
261 if (hlist_empty(&ip6addrlbl_table.head)) {
262 hlist_add_head_rcu(&newp->
list, &ip6addrlbl_table.head);
267 &ip6addrlbl_table.head,
list) {
269 net_eq(ip6addrlbl_net(p), ip6addrlbl_net(newp)) &&
276 hlist_replace_rcu(&p->
list, &newp->
list);
281 hlist_add_before_rcu(&newp->
list, &p->
list);
285 hlist_add_after_rcu(&p->
list, &newp->
list);
289 ip6addrlbl_table.seq++;
294 static int ip6addrlbl_add(
struct net *net,
295 const struct in6_addr *prefix,
int prefixlen,
296 int ifindex,
u32 label,
int replace)
302 __func__, prefix, prefixlen, ifindex, (
unsigned int)label,
305 newp = ip6addrlbl_alloc(net, prefix, prefixlen, ifindex, label);
307 return PTR_ERR(newp);
308 spin_lock(&ip6addrlbl_table.lock);
309 ret = __ip6addrlbl_add(newp, replace);
310 spin_unlock(&ip6addrlbl_table.lock);
312 ip6addrlbl_free(newp);
317 static int __ip6addrlbl_del(
struct net *net,
318 const struct in6_addr *prefix,
int prefixlen,
326 __func__, prefix, prefixlen, ifindex);
330 net_eq(ip6addrlbl_net(p), net) &&
332 ipv6_addr_equal(&p->
prefix, prefix)) {
333 hlist_del_rcu(&p->
list);
342 static int ip6addrlbl_del(
struct net *net,
343 const struct in6_addr *prefix,
int prefixlen,
350 __func__, prefix, prefixlen, ifindex);
352 ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
353 spin_lock(&ip6addrlbl_table.lock);
354 ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
355 spin_unlock(&ip6addrlbl_table.lock);
360 static int __net_init ip6addrlbl_net_init(
struct net *net)
367 for (i = 0; i <
ARRAY_SIZE(ip6addrlbl_init_table); i++) {
368 int ret = ip6addrlbl_add(net,
369 ip6addrlbl_init_table[i].prefix,
370 ip6addrlbl_init_table[i].prefixlen,
372 ip6addrlbl_init_table[i].label, 0);
374 if (ret && (!err || err != -
ENOMEM))
380 static void __net_exit ip6addrlbl_net_exit(
struct net *net)
386 spin_lock(&ip6addrlbl_table.lock);
388 if (net_eq(ip6addrlbl_net(p), net)) {
389 hlist_del_rcu(&p->
list);
393 spin_unlock(&ip6addrlbl_table.lock);
397 .init = ip6addrlbl_net_init,
398 .exit = ip6addrlbl_net_exit,
421 struct net *net = sock_net(skb->
sk);
428 err = nlmsg_parse(nlh,
sizeof(*ifal), tb,
IFAL_MAX, ifal_policy);
432 ifal = nlmsg_data(nlh);
441 pfx = nla_data(tb[IFAL_ADDRESS]);
447 label = nla_get_u32(tb[IFAL_LABEL]);
471 static void ip6addrlbl_putmsg(
struct nlmsghdr *nlh,
472 int prefixlen,
int ifindex,
u32 lseq)
482 static int ip6addrlbl_fill(
struct sk_buff *skb,
488 struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
496 nla_put_u32(skb, IFAL_LABEL, p->
label) < 0) {
497 nlmsg_cancel(skb, nlh);
501 return nlmsg_end(skb, nlh);
506 struct net *net = sock_net(skb->
sk);
509 int idx = 0, s_idx = cb->
args[0];
513 hlist_for_each_entry_rcu(p, pos, &ip6addrlbl_table.head,
list) {
515 net_eq(ip6addrlbl_net(p), net)) {
516 if ((err = ip6addrlbl_fill(skb, p,
517 ip6addrlbl_table.seq,
531 static inline int ip6addrlbl_msgsize(
void)
538 static int ip6addrlbl_get(
struct sk_buff *in_skb,
struct nlmsghdr* nlh,
541 struct net *net = sock_net(in_skb->
sk);
550 err = nlmsg_parse(nlh,
sizeof(*ifal), tb,
IFAL_MAX, ifal_policy);
554 ifal = nlmsg_data(nlh);
564 if (!tb[IFAL_ADDRESS])
567 addr = nla_data(tb[IFAL_ADDRESS]);
572 p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->
ifal_index);
573 if (p && ip6addrlbl_hold(p))
575 lseq = ip6addrlbl_table.seq;
583 if (!(skb = nlmsg_new(ip6addrlbl_msgsize(),
GFP_KERNEL))) {
588 err = ip6addrlbl_fill(skb, p, lseq,
612 ip6addrlbl_dump,
NULL);