1 // SPDX-License-Identifier: GPL-2.0
3 * IPv6 Address Label subsystem
4 * for the IPv6 "Default" Source Address Selection
6 * Copyright (C)2007 USAGI/WIDE Project
10 * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/rcupdate.h>
16 #include <linux/in6.h>
17 #include <linux/slab.h>
18 #include <net/addrconf.h>
19 #include <linux/if_addrlabel.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
24 #define ADDRLABEL(x...) printk(x)
26 #define ADDRLABEL(x...) do { ; } while (0)
32 struct ip6addrlbl_entry
{
33 struct in6_addr prefix
;
38 struct hlist_node list
;
43 * Default policy table (RFC6724 + extensions)
45 * prefix addr_type label
46 * -------------------------------------------------------------------------
51 * ::ffff:0:0/96 V4MAPPED 4
52 * fc00::/7 N/A 5 ULA (RFC 4193)
53 * 2001::/32 N/A 6 Teredo (RFC 4380)
54 * 2001:10::/28 N/A 7 ORCHID (RFC 4843)
55 * fec0::/10 N/A 11 Site-local
56 * (deprecated by RFC3879)
57 * 3ffe::/16 N/A 12 6bone
59 * Note: 0xffffffff is used if we do not have any policies.
60 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
63 #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL
65 static const __net_initconst
struct ip6addrlbl_init_table
67 const struct in6_addr
*prefix
;
70 } ip6addrlbl_init_table
[] = {
72 .prefix
= &in6addr_any
,
75 .prefix
= &(struct in6_addr
){ { { 0xfc } } } ,
79 .prefix
= &(struct in6_addr
){ { { 0xfe, 0xc0 } } },
83 .prefix
= &(struct in6_addr
){ { { 0x20, 0x02 } } },
87 .prefix
= &(struct in6_addr
){ { { 0x3f, 0xfe } } },
91 .prefix
= &(struct in6_addr
){ { { 0x20, 0x01 } } },
94 }, { /* 2001:10::/28 */
95 .prefix
= &(struct in6_addr
){ { { 0x20, 0x01, 0x00, 0x10 } } },
99 .prefix
= &(struct in6_addr
){ { { [10] = 0xff, [11] = 0xff } } },
103 .prefix
= &in6addr_any
,
107 .prefix
= &in6addr_loopback
,
114 static bool __ip6addrlbl_match(const struct ip6addrlbl_entry
*p
,
115 const struct in6_addr
*addr
,
116 int addrtype
, int ifindex
)
118 if (p
->ifindex
&& p
->ifindex
!= ifindex
)
120 if (p
->addrtype
&& p
->addrtype
!= addrtype
)
122 if (!ipv6_prefix_equal(addr
, &p
->prefix
, p
->prefixlen
))
127 static struct ip6addrlbl_entry
*__ipv6_addr_label(struct net
*net
,
128 const struct in6_addr
*addr
,
129 int type
, int ifindex
)
131 struct ip6addrlbl_entry
*p
;
133 hlist_for_each_entry_rcu(p
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
134 if (__ip6addrlbl_match(p
, addr
, type
, ifindex
))
140 u32
ipv6_addr_label(struct net
*net
,
141 const struct in6_addr
*addr
, int type
, int ifindex
)
144 struct ip6addrlbl_entry
*p
;
146 type
&= IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
;
149 p
= __ipv6_addr_label(net
, addr
, type
, ifindex
);
150 label
= p
? p
->label
: IPV6_ADDR_LABEL_DEFAULT
;
153 ADDRLABEL(KERN_DEBUG
"%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
154 __func__
, addr
, type
, ifindex
, label
);
159 /* allocate one entry */
160 static struct ip6addrlbl_entry
*ip6addrlbl_alloc(const struct in6_addr
*prefix
,
161 int prefixlen
, int ifindex
,
164 struct ip6addrlbl_entry
*newp
;
167 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
168 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
);
170 addrtype
= ipv6_addr_type(prefix
) & (IPV6_ADDR_MAPPED
| IPV6_ADDR_COMPATv4
| IPV6_ADDR_LOOPBACK
);
173 case IPV6_ADDR_MAPPED
:
175 return ERR_PTR(-EINVAL
);
179 case IPV6_ADDR_COMPATv4
:
183 case IPV6_ADDR_LOOPBACK
:
184 if (prefixlen
!= 128)
189 newp
= kmalloc(sizeof(*newp
), GFP_KERNEL
);
191 return ERR_PTR(-ENOMEM
);
193 ipv6_addr_prefix(&newp
->prefix
, prefix
, prefixlen
);
194 newp
->prefixlen
= prefixlen
;
195 newp
->ifindex
= ifindex
;
196 newp
->addrtype
= addrtype
;
198 INIT_HLIST_NODE(&newp
->list
);
203 static int __ip6addrlbl_add(struct net
*net
, struct ip6addrlbl_entry
*newp
,
206 struct ip6addrlbl_entry
*last
= NULL
, *p
= NULL
;
207 struct hlist_node
*n
;
210 ADDRLABEL(KERN_DEBUG
"%s(newp=%p, replace=%d)\n", __func__
, newp
,
213 hlist_for_each_entry_safe(p
, n
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
214 if (p
->prefixlen
== newp
->prefixlen
&&
215 p
->ifindex
== newp
->ifindex
&&
216 ipv6_addr_equal(&p
->prefix
, &newp
->prefix
)) {
221 hlist_replace_rcu(&p
->list
, &newp
->list
);
224 } else if ((p
->prefixlen
== newp
->prefixlen
&& !p
->ifindex
) ||
225 (p
->prefixlen
< newp
->prefixlen
)) {
226 hlist_add_before_rcu(&newp
->list
, &p
->list
);
232 hlist_add_behind_rcu(&newp
->list
, &last
->list
);
234 hlist_add_head_rcu(&newp
->list
, &net
->ipv6
.ip6addrlbl_table
.head
);
237 WRITE_ONCE(net
->ipv6
.ip6addrlbl_table
.seq
,
238 net
->ipv6
.ip6addrlbl_table
.seq
+ 1);
243 static int ip6addrlbl_add(struct net
*net
,
244 const struct in6_addr
*prefix
, int prefixlen
,
245 int ifindex
, u32 label
, int replace
)
247 struct ip6addrlbl_entry
*newp
;
250 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
251 __func__
, prefix
, prefixlen
, ifindex
, (unsigned int)label
,
254 newp
= ip6addrlbl_alloc(prefix
, prefixlen
, ifindex
, label
);
256 return PTR_ERR(newp
);
257 spin_lock(&net
->ipv6
.ip6addrlbl_table
.lock
);
258 ret
= __ip6addrlbl_add(net
, newp
, replace
);
259 spin_unlock(&net
->ipv6
.ip6addrlbl_table
.lock
);
266 static int __ip6addrlbl_del(struct net
*net
,
267 const struct in6_addr
*prefix
, int prefixlen
,
270 struct ip6addrlbl_entry
*p
= NULL
;
271 struct hlist_node
*n
;
274 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
275 __func__
, prefix
, prefixlen
, ifindex
);
277 hlist_for_each_entry_safe(p
, n
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
278 if (p
->prefixlen
== prefixlen
&&
279 p
->ifindex
== ifindex
&&
280 ipv6_addr_equal(&p
->prefix
, prefix
)) {
281 hlist_del_rcu(&p
->list
);
290 static int ip6addrlbl_del(struct net
*net
,
291 const struct in6_addr
*prefix
, int prefixlen
,
294 struct in6_addr prefix_buf
;
297 ADDRLABEL(KERN_DEBUG
"%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
298 __func__
, prefix
, prefixlen
, ifindex
);
300 ipv6_addr_prefix(&prefix_buf
, prefix
, prefixlen
);
301 spin_lock(&net
->ipv6
.ip6addrlbl_table
.lock
);
302 ret
= __ip6addrlbl_del(net
, &prefix_buf
, prefixlen
, ifindex
);
303 spin_unlock(&net
->ipv6
.ip6addrlbl_table
.lock
);
307 /* add default label */
308 static int __net_init
ip6addrlbl_net_init(struct net
*net
)
310 struct ip6addrlbl_entry
*p
= NULL
;
311 struct hlist_node
*n
;
315 ADDRLABEL(KERN_DEBUG
"%s\n", __func__
);
317 spin_lock_init(&net
->ipv6
.ip6addrlbl_table
.lock
);
318 INIT_HLIST_HEAD(&net
->ipv6
.ip6addrlbl_table
.head
);
320 for (i
= 0; i
< ARRAY_SIZE(ip6addrlbl_init_table
); i
++) {
321 err
= ip6addrlbl_add(net
,
322 ip6addrlbl_init_table
[i
].prefix
,
323 ip6addrlbl_init_table
[i
].prefixlen
,
325 ip6addrlbl_init_table
[i
].label
, 0);
327 goto err_ip6addrlbl_add
;
332 hlist_for_each_entry_safe(p
, n
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
333 hlist_del_rcu(&p
->list
);
339 static void __net_exit
ip6addrlbl_net_exit(struct net
*net
)
341 struct ip6addrlbl_entry
*p
= NULL
;
342 struct hlist_node
*n
;
344 /* Remove all labels belonging to the exiting net */
345 spin_lock(&net
->ipv6
.ip6addrlbl_table
.lock
);
346 hlist_for_each_entry_safe(p
, n
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
347 hlist_del_rcu(&p
->list
);
350 spin_unlock(&net
->ipv6
.ip6addrlbl_table
.lock
);
353 static struct pernet_operations ipv6_addr_label_ops
= {
354 .init
= ip6addrlbl_net_init
,
355 .exit
= ip6addrlbl_net_exit
,
358 int __init
ipv6_addr_label_init(void)
360 return register_pernet_subsys(&ipv6_addr_label_ops
);
363 void ipv6_addr_label_cleanup(void)
365 unregister_pernet_subsys(&ipv6_addr_label_ops
);
368 static const struct nla_policy ifal_policy
[IFAL_MAX
+1] = {
369 [IFAL_ADDRESS
] = { .len
= sizeof(struct in6_addr
), },
370 [IFAL_LABEL
] = { .len
= sizeof(u32
), },
373 static bool addrlbl_ifindex_exists(struct net
*net
, int ifindex
)
376 struct net_device
*dev
;
379 dev
= dev_get_by_index_rcu(net
, ifindex
);
385 static int ip6addrlbl_newdel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
386 struct netlink_ext_ack
*extack
)
388 struct net
*net
= sock_net(skb
->sk
);
389 struct ifaddrlblmsg
*ifal
;
390 struct nlattr
*tb
[IFAL_MAX
+1];
391 struct in6_addr
*pfx
;
395 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
,
396 ifal_policy
, extack
);
400 ifal
= nlmsg_data(nlh
);
402 if (ifal
->ifal_family
!= AF_INET6
||
403 ifal
->ifal_prefixlen
> 128)
406 if (!tb
[IFAL_ADDRESS
])
408 pfx
= nla_data(tb
[IFAL_ADDRESS
]);
412 label
= nla_get_u32(tb
[IFAL_LABEL
]);
413 if (label
== IPV6_ADDR_LABEL_DEFAULT
)
416 switch (nlh
->nlmsg_type
) {
417 case RTM_NEWADDRLABEL
:
418 if (ifal
->ifal_index
&&
419 !addrlbl_ifindex_exists(net
, ifal
->ifal_index
))
422 err
= ip6addrlbl_add(net
, pfx
, ifal
->ifal_prefixlen
,
423 ifal
->ifal_index
, label
,
424 nlh
->nlmsg_flags
& NLM_F_REPLACE
);
426 case RTM_DELADDRLABEL
:
427 err
= ip6addrlbl_del(net
, pfx
, ifal
->ifal_prefixlen
,
436 static void ip6addrlbl_putmsg(struct nlmsghdr
*nlh
,
437 int prefixlen
, int ifindex
, u32 lseq
)
439 struct ifaddrlblmsg
*ifal
= nlmsg_data(nlh
);
440 ifal
->ifal_family
= AF_INET6
;
441 ifal
->__ifal_reserved
= 0;
442 ifal
->ifal_prefixlen
= prefixlen
;
443 ifal
->ifal_flags
= 0;
444 ifal
->ifal_index
= ifindex
;
445 ifal
->ifal_seq
= lseq
;
448 static int ip6addrlbl_fill(struct sk_buff
*skb
,
449 const struct ip6addrlbl_entry
*p
,
451 u32 portid
, u32 seq
, int event
,
454 struct nlmsghdr
*nlh
= nlmsg_put(skb
, portid
, seq
, event
,
455 sizeof(struct ifaddrlblmsg
), flags
);
459 ip6addrlbl_putmsg(nlh
, p
->prefixlen
, p
->ifindex
, lseq
);
461 if (nla_put_in6_addr(skb
, IFAL_ADDRESS
, &p
->prefix
) < 0 ||
462 nla_put_u32(skb
, IFAL_LABEL
, p
->label
) < 0) {
463 nlmsg_cancel(skb
, nlh
);
471 static int ip6addrlbl_valid_dump_req(const struct nlmsghdr
*nlh
,
472 struct netlink_ext_ack
*extack
)
474 struct ifaddrlblmsg
*ifal
;
476 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifal
))) {
477 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for address label dump request");
481 ifal
= nlmsg_data(nlh
);
482 if (ifal
->__ifal_reserved
|| ifal
->ifal_prefixlen
||
483 ifal
->ifal_flags
|| ifal
->ifal_index
|| ifal
->ifal_seq
) {
484 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for address label dump request");
488 if (nlmsg_attrlen(nlh
, sizeof(*ifal
))) {
489 NL_SET_ERR_MSG_MOD(extack
, "Invalid data after header for address label dump request");
496 static int ip6addrlbl_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
498 const struct nlmsghdr
*nlh
= cb
->nlh
;
499 struct net
*net
= sock_net(skb
->sk
);
500 struct ip6addrlbl_entry
*p
;
501 int idx
= 0, s_idx
= cb
->args
[0];
505 if (cb
->strict_check
) {
506 err
= ip6addrlbl_valid_dump_req(nlh
, cb
->extack
);
512 lseq
= READ_ONCE(net
->ipv6
.ip6addrlbl_table
.seq
);
513 hlist_for_each_entry_rcu(p
, &net
->ipv6
.ip6addrlbl_table
.head
, list
) {
515 err
= ip6addrlbl_fill(skb
, p
,
517 NETLINK_CB(cb
->skb
).portid
,
531 static inline int ip6addrlbl_msgsize(void)
533 return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg
))
534 + nla_total_size(16) /* IFAL_ADDRESS */
535 + nla_total_size(4); /* IFAL_LABEL */
538 static int ip6addrlbl_valid_get_req(struct sk_buff
*skb
,
539 const struct nlmsghdr
*nlh
,
541 struct netlink_ext_ack
*extack
)
543 struct ifaddrlblmsg
*ifal
;
546 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifal
))) {
547 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for addrlabel get request");
551 if (!netlink_strict_get_check(skb
))
552 return nlmsg_parse_deprecated(nlh
, sizeof(*ifal
), tb
,
553 IFAL_MAX
, ifal_policy
, extack
);
555 ifal
= nlmsg_data(nlh
);
556 if (ifal
->__ifal_reserved
|| ifal
->ifal_flags
|| ifal
->ifal_seq
) {
557 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for addrlabel get request");
561 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifal
), tb
, IFAL_MAX
,
562 ifal_policy
, extack
);
566 for (i
= 0; i
<= IFAL_MAX
; i
++) {
574 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in addrlabel get request");
582 static int ip6addrlbl_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
583 struct netlink_ext_ack
*extack
)
585 struct net
*net
= sock_net(in_skb
->sk
);
586 struct ifaddrlblmsg
*ifal
;
587 struct nlattr
*tb
[IFAL_MAX
+1];
588 struct in6_addr
*addr
;
591 struct ip6addrlbl_entry
*p
;
594 err
= ip6addrlbl_valid_get_req(in_skb
, nlh
, tb
, extack
);
598 ifal
= nlmsg_data(nlh
);
600 if (ifal
->ifal_family
!= AF_INET6
||
601 ifal
->ifal_prefixlen
!= 128)
604 if (ifal
->ifal_index
&&
605 !addrlbl_ifindex_exists(net
, ifal
->ifal_index
))
608 if (!tb
[IFAL_ADDRESS
])
610 addr
= nla_data(tb
[IFAL_ADDRESS
]);
612 skb
= nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL
);
619 p
= __ipv6_addr_label(net
, addr
, ipv6_addr_type(addr
), ifal
->ifal_index
);
620 lseq
= READ_ONCE(net
->ipv6
.ip6addrlbl_table
.seq
);
622 err
= ip6addrlbl_fill(skb
, p
, lseq
,
623 NETLINK_CB(in_skb
).portid
,
625 RTM_NEWADDRLABEL
, 0);
629 WARN_ON(err
== -EMSGSIZE
);
632 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
637 static const struct rtnl_msg_handler ipv6_adddr_label_rtnl_msg_handlers
[] __initconst_or_module
= {
638 {.owner
= THIS_MODULE
, .protocol
= PF_INET6
, .msgtype
= RTM_NEWADDRLABEL
,
639 .doit
= ip6addrlbl_newdel
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
640 {.owner
= THIS_MODULE
, .protocol
= PF_INET6
, .msgtype
= RTM_DELADDRLABEL
,
641 .doit
= ip6addrlbl_newdel
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
642 {.owner
= THIS_MODULE
, .protocol
= PF_INET6
, .msgtype
= RTM_GETADDRLABEL
,
643 .doit
= ip6addrlbl_get
, .dumpit
= ip6addrlbl_dump
,
644 .flags
= RTNL_FLAG_DOIT_UNLOCKED
| RTNL_FLAG_DUMP_UNLOCKED
},
647 int __init
ipv6_addr_label_rtnl_register(void)
649 return rtnl_register_many(ipv6_adddr_label_rtnl_msg_handlers
);