Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 2 | /* |
| 3 | * IPv6 Address Label subsystem |
| 4 | * for the IPv6 "Default" Source Address Selection |
| 5 | * |
| 6 | * Copyright (C)2007 USAGI/WIDE Project |
| 7 | */ |
| 8 | /* |
| 9 | * Author: |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 10 | * YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org> |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/rcupdate.h> |
| 16 | #include <linux/in6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 18 | #include <net/addrconf.h> |
| 19 | #include <linux/if_addrlabel.h> |
| 20 | #include <linux/netlink.h> |
| 21 | #include <linux/rtnetlink.h> |
| 22 | |
| 23 | #if 0 |
| 24 | #define ADDRLABEL(x...) printk(x) |
| 25 | #else |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 26 | #define ADDRLABEL(x...) do { ; } while (0) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * Policy Table |
| 31 | */ |
Wang Yufen | 22b285d | 2014-02-17 16:53:33 +0800 | [diff] [blame] | 32 | struct ip6addrlbl_entry { |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 33 | struct in6_addr prefix; |
| 34 | int prefixlen; |
| 35 | int ifindex; |
| 36 | int addrtype; |
| 37 | u32 label; |
| 38 | struct hlist_node list; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 39 | struct rcu_head rcu; |
| 40 | }; |
| 41 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 42 | /* |
YOSHIFUJI Hideaki / 吉藤英明 | 417962a | 2012-09-10 18:16:49 +0000 | [diff] [blame] | 43 | * Default policy table (RFC6724 + extensions) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 44 | * |
| 45 | * prefix addr_type label |
| 46 | * ------------------------------------------------------------------------- |
| 47 | * ::1/128 LOOPBACK 0 |
| 48 | * ::/0 N/A 1 |
| 49 | * 2002::/16 N/A 2 |
| 50 | * ::/96 COMPATv4 3 |
| 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) |
Juha-Matti Tapio | 5fe47b8 | 2008-02-28 20:55:02 -0800 | [diff] [blame] | 54 | * 2001:10::/28 N/A 7 ORCHID (RFC 4843) |
YOSHIFUJI Hideaki / 吉藤英明 | 417962a | 2012-09-10 18:16:49 +0000 | [diff] [blame] | 55 | * fec0::/10 N/A 11 Site-local |
| 56 | * (deprecated by RFC3879) |
| 57 | * 3ffe::/16 N/A 12 6bone |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 58 | * |
| 59 | * Note: 0xffffffff is used if we do not have any policies. |
YOSHIFUJI Hideaki / 吉藤英明 | 417962a | 2012-09-10 18:16:49 +0000 | [diff] [blame] | 60 | * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724. |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | #define IPV6_ADDR_LABEL_DEFAULT 0xffffffffUL |
| 64 | |
Andi Kleen | 04a6f82 | 2012-10-04 17:12:11 -0700 | [diff] [blame] | 65 | static const __net_initconst struct ip6addrlbl_init_table |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 66 | { |
| 67 | const struct in6_addr *prefix; |
| 68 | int prefixlen; |
| 69 | u32 label; |
| 70 | } ip6addrlbl_init_table[] = { |
| 71 | { /* ::/0 */ |
| 72 | .prefix = &in6addr_any, |
| 73 | .label = 1, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 74 | }, { /* fc00::/7 */ |
| 75 | .prefix = &(struct in6_addr){ { { 0xfc } } } , |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 76 | .prefixlen = 7, |
| 77 | .label = 5, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 78 | }, { /* fec0::/10 */ |
| 79 | .prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } }, |
YOSHIFUJI Hideaki / 吉藤英明 | 417962a | 2012-09-10 18:16:49 +0000 | [diff] [blame] | 80 | .prefixlen = 10, |
| 81 | .label = 11, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 82 | }, { /* 2002::/16 */ |
| 83 | .prefix = &(struct in6_addr){ { { 0x20, 0x02 } } }, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 84 | .prefixlen = 16, |
| 85 | .label = 2, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 86 | }, { /* 3ffe::/16 */ |
| 87 | .prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } }, |
YOSHIFUJI Hideaki / 吉藤英明 | 417962a | 2012-09-10 18:16:49 +0000 | [diff] [blame] | 88 | .prefixlen = 16, |
| 89 | .label = 12, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 90 | }, { /* 2001::/32 */ |
| 91 | .prefix = &(struct in6_addr){ { { 0x20, 0x01 } } }, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 92 | .prefixlen = 32, |
| 93 | .label = 6, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 94 | }, { /* 2001:10::/28 */ |
| 95 | .prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } }, |
Juha-Matti Tapio | 5fe47b8 | 2008-02-28 20:55:02 -0800 | [diff] [blame] | 96 | .prefixlen = 28, |
| 97 | .label = 7, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 98 | }, { /* ::ffff:0:0 */ |
| 99 | .prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } }, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 100 | .prefixlen = 96, |
| 101 | .label = 4, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 102 | }, { /* ::/96 */ |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 103 | .prefix = &in6addr_any, |
| 104 | .prefixlen = 96, |
| 105 | .label = 3, |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 106 | }, { /* ::1/128 */ |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 107 | .prefix = &in6addr_loopback, |
| 108 | .prefixlen = 128, |
| 109 | .label = 0, |
| 110 | } |
| 111 | }; |
| 112 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 113 | /* Find label */ |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 114 | static bool __ip6addrlbl_match(const struct ip6addrlbl_entry *p, |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 115 | const struct in6_addr *addr, |
| 116 | int addrtype, int ifindex) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 117 | { |
| 118 | if (p->ifindex && p->ifindex != ifindex) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 119 | return false; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 120 | if (p->addrtype && p->addrtype != addrtype) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 121 | return false; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 122 | if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 123 | return false; |
| 124 | return true; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 127 | static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net, |
| 128 | const struct in6_addr *addr, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 129 | int type, int ifindex) |
| 130 | { |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 131 | struct ip6addrlbl_entry *p; |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 132 | |
| 133 | hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) { |
| 134 | if (__ip6addrlbl_match(p, addr, type, ifindex)) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 135 | return p; |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 140 | u32 ipv6_addr_label(struct net *net, |
| 141 | const struct in6_addr *addr, int type, int ifindex) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 142 | { |
| 143 | u32 label; |
| 144 | struct ip6addrlbl_entry *p; |
| 145 | |
| 146 | type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK; |
| 147 | |
| 148 | rcu_read_lock(); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 149 | p = __ipv6_addr_label(net, addr, type, ifindex); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 150 | label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT; |
| 151 | rcu_read_unlock(); |
| 152 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 153 | ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 154 | __func__, addr, type, ifindex, label); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 155 | |
| 156 | return label; |
| 157 | } |
| 158 | |
| 159 | /* allocate one entry */ |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 160 | static struct ip6addrlbl_entry *ip6addrlbl_alloc(const struct in6_addr *prefix, |
YOSHIFUJI Hideaki | 40fee36 | 2008-01-22 17:12:50 +0900 | [diff] [blame] | 161 | int prefixlen, int ifindex, |
| 162 | u32 label) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 163 | { |
| 164 | struct ip6addrlbl_entry *newp; |
| 165 | int addrtype; |
| 166 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 167 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 168 | __func__, prefix, prefixlen, ifindex, (unsigned int)label); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 169 | |
| 170 | addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK); |
| 171 | |
| 172 | switch (addrtype) { |
| 173 | case IPV6_ADDR_MAPPED: |
| 174 | if (prefixlen > 96) |
| 175 | return ERR_PTR(-EINVAL); |
| 176 | if (prefixlen < 96) |
| 177 | addrtype = 0; |
| 178 | break; |
| 179 | case IPV6_ADDR_COMPATv4: |
| 180 | if (prefixlen != 96) |
| 181 | addrtype = 0; |
| 182 | break; |
| 183 | case IPV6_ADDR_LOOPBACK: |
| 184 | if (prefixlen != 128) |
| 185 | addrtype = 0; |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | newp = kmalloc(sizeof(*newp), GFP_KERNEL); |
| 190 | if (!newp) |
| 191 | return ERR_PTR(-ENOMEM); |
| 192 | |
| 193 | ipv6_addr_prefix(&newp->prefix, prefix, prefixlen); |
| 194 | newp->prefixlen = prefixlen; |
| 195 | newp->ifindex = ifindex; |
| 196 | newp->addrtype = addrtype; |
| 197 | newp->label = label; |
| 198 | INIT_HLIST_NODE(&newp->list); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 199 | return newp; |
| 200 | } |
| 201 | |
| 202 | /* add a label */ |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 203 | static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp, |
| 204 | int replace) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 205 | { |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 206 | struct ip6addrlbl_entry *last = NULL, *p = NULL; |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 207 | struct hlist_node *n; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 208 | int ret = 0; |
| 209 | |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 210 | ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp, |
| 211 | replace); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 212 | |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 213 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 214 | if (p->prefixlen == newp->prefixlen && |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 215 | p->ifindex == newp->ifindex && |
| 216 | ipv6_addr_equal(&p->prefix, &newp->prefix)) { |
| 217 | if (!replace) { |
| 218 | ret = -EEXIST; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 219 | goto out; |
| 220 | } |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 221 | hlist_replace_rcu(&p->list, &newp->list); |
Eric Dumazet | 2809c09 | 2017-10-09 09:52:25 -0700 | [diff] [blame] | 222 | kfree_rcu(p, rcu); |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 223 | goto out; |
| 224 | } else if ((p->prefixlen == newp->prefixlen && !p->ifindex) || |
| 225 | (p->prefixlen < newp->prefixlen)) { |
| 226 | hlist_add_before_rcu(&newp->list, &p->list); |
| 227 | goto out; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 228 | } |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 229 | last = p; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 230 | } |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 231 | if (last) |
Ken Helias | 1d02328 | 2014-08-06 16:09:16 -0700 | [diff] [blame] | 232 | hlist_add_behind_rcu(&newp->list, &last->list); |
Hannes Frederic Sowa | 639739b | 2013-09-03 02:13:31 +0200 | [diff] [blame] | 233 | else |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 234 | hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 235 | out: |
| 236 | if (!ret) |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 237 | net->ipv6.ip6addrlbl_table.seq++; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | /* add a label */ |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 242 | static int ip6addrlbl_add(struct net *net, |
| 243 | const struct in6_addr *prefix, int prefixlen, |
YOSHIFUJI Hideaki | 40fee36 | 2008-01-22 17:12:50 +0900 | [diff] [blame] | 244 | int ifindex, u32 label, int replace) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 245 | { |
| 246 | struct ip6addrlbl_entry *newp; |
| 247 | int ret = 0; |
| 248 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 249 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 250 | __func__, prefix, prefixlen, ifindex, (unsigned int)label, |
| 251 | replace); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 252 | |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 253 | newp = ip6addrlbl_alloc(prefix, prefixlen, ifindex, label); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 254 | if (IS_ERR(newp)) |
| 255 | return PTR_ERR(newp); |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 256 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
| 257 | ret = __ip6addrlbl_add(net, newp, replace); |
| 258 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 259 | if (ret) |
Eric Dumazet | 2809c09 | 2017-10-09 09:52:25 -0700 | [diff] [blame] | 260 | kfree(newp); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /* remove a label */ |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 265 | static int __ip6addrlbl_del(struct net *net, |
| 266 | const struct in6_addr *prefix, int prefixlen, |
YOSHIFUJI Hideaki | 40fee36 | 2008-01-22 17:12:50 +0900 | [diff] [blame] | 267 | int ifindex) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 268 | { |
| 269 | struct ip6addrlbl_entry *p = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 270 | struct hlist_node *n; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 271 | int ret = -ESRCH; |
| 272 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 273 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 274 | __func__, prefix, prefixlen, ifindex); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 275 | |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 276 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 277 | if (p->prefixlen == prefixlen && |
| 278 | p->ifindex == ifindex && |
| 279 | ipv6_addr_equal(&p->prefix, prefix)) { |
| 280 | hlist_del_rcu(&p->list); |
Eric Dumazet | 2809c09 | 2017-10-09 09:52:25 -0700 | [diff] [blame] | 281 | kfree_rcu(p, rcu); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 282 | ret = 0; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | return ret; |
| 287 | } |
| 288 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 289 | static int ip6addrlbl_del(struct net *net, |
| 290 | const struct in6_addr *prefix, int prefixlen, |
YOSHIFUJI Hideaki | 40fee36 | 2008-01-22 17:12:50 +0900 | [diff] [blame] | 291 | int ifindex) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 292 | { |
| 293 | struct in6_addr prefix_buf; |
| 294 | int ret; |
| 295 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 296 | ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 297 | __func__, prefix, prefixlen, ifindex); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 298 | |
| 299 | ipv6_addr_prefix(&prefix_buf, prefix, prefixlen); |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 300 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 301 | ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex); |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 302 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* add default label */ |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 307 | static int __net_init ip6addrlbl_net_init(struct net *net) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 308 | { |
Wang Hai | e255e11 | 2020-11-24 15:17:28 +0800 | [diff] [blame] | 309 | struct ip6addrlbl_entry *p = NULL; |
| 310 | struct hlist_node *n; |
| 311 | int err; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 312 | int i; |
| 313 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 314 | ADDRLABEL(KERN_DEBUG "%s\n", __func__); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 315 | |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 316 | spin_lock_init(&net->ipv6.ip6addrlbl_table.lock); |
| 317 | INIT_HLIST_HEAD(&net->ipv6.ip6addrlbl_table.head); |
| 318 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 319 | for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) { |
Wang Hai | e255e11 | 2020-11-24 15:17:28 +0800 | [diff] [blame] | 320 | err = ip6addrlbl_add(net, |
| 321 | ip6addrlbl_init_table[i].prefix, |
| 322 | ip6addrlbl_init_table[i].prefixlen, |
| 323 | 0, |
| 324 | ip6addrlbl_init_table[i].label, 0); |
| 325 | if (err) |
| 326 | goto err_ip6addrlbl_add; |
| 327 | } |
| 328 | return 0; |
| 329 | |
| 330 | err_ip6addrlbl_add: |
| 331 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
| 332 | hlist_del_rcu(&p->list); |
| 333 | kfree_rcu(p, rcu); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 334 | } |
| 335 | return err; |
| 336 | } |
| 337 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 338 | static void __net_exit ip6addrlbl_net_exit(struct net *net) |
| 339 | { |
| 340 | struct ip6addrlbl_entry *p = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 341 | struct hlist_node *n; |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 342 | |
| 343 | /* Remove all labels belonging to the exiting net */ |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 344 | spin_lock(&net->ipv6.ip6addrlbl_table.lock); |
| 345 | hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) { |
| 346 | hlist_del_rcu(&p->list); |
Eric Dumazet | 2809c09 | 2017-10-09 09:52:25 -0700 | [diff] [blame] | 347 | kfree_rcu(p, rcu); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 348 | } |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 349 | spin_unlock(&net->ipv6.ip6addrlbl_table.lock); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static struct pernet_operations ipv6_addr_label_ops = { |
| 353 | .init = ip6addrlbl_net_init, |
| 354 | .exit = ip6addrlbl_net_exit, |
| 355 | }; |
| 356 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 357 | int __init ipv6_addr_label_init(void) |
| 358 | { |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 359 | return register_pernet_subsys(&ipv6_addr_label_ops); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 360 | } |
| 361 | |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 362 | void ipv6_addr_label_cleanup(void) |
| 363 | { |
| 364 | unregister_pernet_subsys(&ipv6_addr_label_ops); |
| 365 | } |
| 366 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 367 | static const struct nla_policy ifal_policy[IFAL_MAX+1] = { |
| 368 | [IFAL_ADDRESS] = { .len = sizeof(struct in6_addr), }, |
| 369 | [IFAL_LABEL] = { .len = sizeof(u32), }, |
| 370 | }; |
| 371 | |
Florian Westphal | a6f5702 | 2017-08-29 13:29:42 +0200 | [diff] [blame] | 372 | static bool addrlbl_ifindex_exists(struct net *net, int ifindex) |
| 373 | { |
| 374 | |
| 375 | struct net_device *dev; |
| 376 | |
| 377 | rcu_read_lock(); |
| 378 | dev = dev_get_by_index_rcu(net, ifindex); |
| 379 | rcu_read_unlock(); |
| 380 | |
| 381 | return dev != NULL; |
| 382 | } |
| 383 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 384 | static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 385 | struct netlink_ext_ack *extack) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 386 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 387 | struct net *net = sock_net(skb->sk); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 388 | struct ifaddrlblmsg *ifal; |
| 389 | struct nlattr *tb[IFAL_MAX+1]; |
| 390 | struct in6_addr *pfx; |
| 391 | u32 label; |
| 392 | int err = 0; |
| 393 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 394 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb, IFAL_MAX, |
| 395 | ifal_policy, extack); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 396 | if (err < 0) |
| 397 | return err; |
| 398 | |
| 399 | ifal = nlmsg_data(nlh); |
| 400 | |
| 401 | if (ifal->ifal_family != AF_INET6 || |
| 402 | ifal->ifal_prefixlen > 128) |
| 403 | return -EINVAL; |
| 404 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 405 | if (!tb[IFAL_ADDRESS]) |
| 406 | return -EINVAL; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 407 | pfx = nla_data(tb[IFAL_ADDRESS]); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 408 | |
| 409 | if (!tb[IFAL_LABEL]) |
| 410 | return -EINVAL; |
| 411 | label = nla_get_u32(tb[IFAL_LABEL]); |
| 412 | if (label == IPV6_ADDR_LABEL_DEFAULT) |
| 413 | return -EINVAL; |
| 414 | |
Wang Yufen | 1c8669e | 2014-02-17 16:53:34 +0800 | [diff] [blame] | 415 | switch (nlh->nlmsg_type) { |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 416 | case RTM_NEWADDRLABEL: |
Florian Westphal | 0771275 | 2010-05-07 11:31:33 +0000 | [diff] [blame] | 417 | if (ifal->ifal_index && |
Florian Westphal | a6f5702 | 2017-08-29 13:29:42 +0200 | [diff] [blame] | 418 | !addrlbl_ifindex_exists(net, ifal->ifal_index)) |
Florian Westphal | 0771275 | 2010-05-07 11:31:33 +0000 | [diff] [blame] | 419 | return -EINVAL; |
| 420 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 421 | err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 422 | ifal->ifal_index, label, |
| 423 | nlh->nlmsg_flags & NLM_F_REPLACE); |
| 424 | break; |
| 425 | case RTM_DELADDRLABEL: |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 426 | err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 427 | ifal->ifal_index); |
| 428 | break; |
| 429 | default: |
| 430 | err = -EOPNOTSUPP; |
| 431 | } |
| 432 | return err; |
| 433 | } |
| 434 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 435 | static void ip6addrlbl_putmsg(struct nlmsghdr *nlh, |
| 436 | int prefixlen, int ifindex, u32 lseq) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 437 | { |
| 438 | struct ifaddrlblmsg *ifal = nlmsg_data(nlh); |
| 439 | ifal->ifal_family = AF_INET6; |
| 440 | ifal->ifal_prefixlen = prefixlen; |
| 441 | ifal->ifal_flags = 0; |
| 442 | ifal->ifal_index = ifindex; |
| 443 | ifal->ifal_seq = lseq; |
| 444 | }; |
| 445 | |
| 446 | static int ip6addrlbl_fill(struct sk_buff *skb, |
| 447 | struct ip6addrlbl_entry *p, |
| 448 | u32 lseq, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 449 | u32 portid, u32 seq, int event, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 450 | unsigned int flags) |
| 451 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 452 | struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event, |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 453 | sizeof(struct ifaddrlblmsg), flags); |
| 454 | if (!nlh) |
| 455 | return -EMSGSIZE; |
| 456 | |
| 457 | ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq); |
| 458 | |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 459 | if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 || |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 460 | nla_put_u32(skb, IFAL_LABEL, p->label) < 0) { |
| 461 | nlmsg_cancel(skb, nlh); |
| 462 | return -EMSGSIZE; |
| 463 | } |
| 464 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 465 | nlmsg_end(skb, nlh); |
| 466 | return 0; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 467 | } |
| 468 | |
David Ahern | f2ae64b | 2018-10-07 20:16:40 -0700 | [diff] [blame] | 469 | static int ip6addrlbl_valid_dump_req(const struct nlmsghdr *nlh, |
| 470 | struct netlink_ext_ack *extack) |
| 471 | { |
| 472 | struct ifaddrlblmsg *ifal; |
| 473 | |
| 474 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) { |
| 475 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for address label dump request"); |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
| 479 | ifal = nlmsg_data(nlh); |
| 480 | if (ifal->__ifal_reserved || ifal->ifal_prefixlen || |
| 481 | ifal->ifal_flags || ifal->ifal_index || ifal->ifal_seq) { |
| 482 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address label dump request"); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | if (nlmsg_attrlen(nlh, sizeof(*ifal))) { |
Colin Ian King | d5f6db3 | 2019-04-18 11:39:18 +0100 | [diff] [blame] | 487 | NL_SET_ERR_MSG_MOD(extack, "Invalid data after header for address label dump request"); |
David Ahern | f2ae64b | 2018-10-07 20:16:40 -0700 | [diff] [blame] | 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 494 | static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 495 | { |
David Ahern | f2ae64b | 2018-10-07 20:16:40 -0700 | [diff] [blame] | 496 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 497 | struct net *net = sock_net(skb->sk); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 498 | struct ip6addrlbl_entry *p; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 499 | int idx = 0, s_idx = cb->args[0]; |
| 500 | int err; |
| 501 | |
David Ahern | f2ae64b | 2018-10-07 20:16:40 -0700 | [diff] [blame] | 502 | if (cb->strict_check) { |
| 503 | err = ip6addrlbl_valid_dump_req(nlh, cb->extack); |
| 504 | if (err < 0) |
| 505 | return err; |
| 506 | } |
| 507 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 508 | rcu_read_lock(); |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 509 | hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) { |
| 510 | if (idx >= s_idx) { |
Wang Yufen | cb6e926 | 2014-02-17 16:53:35 +0800 | [diff] [blame] | 511 | err = ip6addrlbl_fill(skb, p, |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 512 | net->ipv6.ip6addrlbl_table.seq, |
Wang Yufen | cb6e926 | 2014-02-17 16:53:35 +0800 | [diff] [blame] | 513 | NETLINK_CB(cb->skb).portid, |
David Ahern | f2ae64b | 2018-10-07 20:16:40 -0700 | [diff] [blame] | 514 | nlh->nlmsg_seq, |
Wang Yufen | cb6e926 | 2014-02-17 16:53:35 +0800 | [diff] [blame] | 515 | RTM_NEWADDRLABEL, |
| 516 | NLM_F_MULTI); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 517 | if (err < 0) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | idx++; |
| 521 | } |
| 522 | rcu_read_unlock(); |
| 523 | cb->args[0] = idx; |
| 524 | return skb->len; |
| 525 | } |
| 526 | |
| 527 | static inline int ip6addrlbl_msgsize(void) |
| 528 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 529 | return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg)) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 530 | + nla_total_size(16) /* IFAL_ADDRESS */ |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 531 | + nla_total_size(4); /* IFAL_LABEL */ |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 532 | } |
| 533 | |
Jakub Kicinski | 5912a77 | 2019-01-18 10:46:23 -0800 | [diff] [blame] | 534 | static int ip6addrlbl_valid_get_req(struct sk_buff *skb, |
| 535 | const struct nlmsghdr *nlh, |
| 536 | struct nlattr **tb, |
| 537 | struct netlink_ext_ack *extack) |
| 538 | { |
| 539 | struct ifaddrlblmsg *ifal; |
| 540 | int i, err; |
| 541 | |
| 542 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) { |
| 543 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for addrlabel get request"); |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
| 547 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 548 | return nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb, |
| 549 | IFAL_MAX, ifal_policy, extack); |
Jakub Kicinski | 5912a77 | 2019-01-18 10:46:23 -0800 | [diff] [blame] | 550 | |
| 551 | ifal = nlmsg_data(nlh); |
| 552 | if (ifal->__ifal_reserved || ifal->ifal_flags || ifal->ifal_seq) { |
| 553 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for addrlabel get request"); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 557 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifal), tb, IFAL_MAX, |
| 558 | ifal_policy, extack); |
Jakub Kicinski | 5912a77 | 2019-01-18 10:46:23 -0800 | [diff] [blame] | 559 | if (err) |
| 560 | return err; |
| 561 | |
| 562 | for (i = 0; i <= IFAL_MAX; i++) { |
| 563 | if (!tb[i]) |
| 564 | continue; |
| 565 | |
| 566 | switch (i) { |
| 567 | case IFAL_ADDRESS: |
| 568 | break; |
| 569 | default: |
| 570 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in addrlabel get request"); |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 578 | static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 579 | struct netlink_ext_ack *extack) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 580 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 581 | struct net *net = sock_net(in_skb->sk); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 582 | struct ifaddrlblmsg *ifal; |
| 583 | struct nlattr *tb[IFAL_MAX+1]; |
| 584 | struct in6_addr *addr; |
| 585 | u32 lseq; |
| 586 | int err = 0; |
| 587 | struct ip6addrlbl_entry *p; |
| 588 | struct sk_buff *skb; |
| 589 | |
Jakub Kicinski | 5912a77 | 2019-01-18 10:46:23 -0800 | [diff] [blame] | 590 | err = ip6addrlbl_valid_get_req(in_skb, nlh, tb, extack); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 591 | if (err < 0) |
| 592 | return err; |
| 593 | |
| 594 | ifal = nlmsg_data(nlh); |
| 595 | |
| 596 | if (ifal->ifal_family != AF_INET6 || |
| 597 | ifal->ifal_prefixlen != 128) |
| 598 | return -EINVAL; |
| 599 | |
| 600 | if (ifal->ifal_index && |
Florian Westphal | a6f5702 | 2017-08-29 13:29:42 +0200 | [diff] [blame] | 601 | !addrlbl_ifindex_exists(net, ifal->ifal_index)) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 602 | return -EINVAL; |
| 603 | |
| 604 | if (!tb[IFAL_ADDRESS]) |
| 605 | return -EINVAL; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 606 | addr = nla_data(tb[IFAL_ADDRESS]); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 607 | |
Eric Dumazet | 66c77ff | 2017-10-09 09:52:24 -0700 | [diff] [blame] | 608 | skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL); |
| 609 | if (!skb) |
| 610 | return -ENOBUFS; |
| 611 | |
| 612 | err = -ESRCH; |
| 613 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 614 | rcu_read_lock(); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 615 | p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index); |
Eric Dumazet | a90c934 | 2017-09-19 16:27:06 -0700 | [diff] [blame] | 616 | lseq = net->ipv6.ip6addrlbl_table.seq; |
Eric Dumazet | 66c77ff | 2017-10-09 09:52:24 -0700 | [diff] [blame] | 617 | if (p) |
| 618 | err = ip6addrlbl_fill(skb, p, lseq, |
| 619 | NETLINK_CB(in_skb).portid, |
| 620 | nlh->nlmsg_seq, |
| 621 | RTM_NEWADDRLABEL, 0); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 622 | rcu_read_unlock(); |
| 623 | |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 624 | if (err < 0) { |
| 625 | WARN_ON(err == -EMSGSIZE); |
| 626 | kfree_skb(skb); |
Eric Dumazet | 66c77ff | 2017-10-09 09:52:24 -0700 | [diff] [blame] | 627 | } else { |
| 628 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 629 | } |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 630 | return err; |
| 631 | } |
| 632 | |
Florian Westphal | a3fde2a | 2017-12-04 19:19:18 +0100 | [diff] [blame] | 633 | int __init ipv6_addr_label_rtnl_register(void) |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 634 | { |
Florian Westphal | a3fde2a | 2017-12-04 19:19:18 +0100 | [diff] [blame] | 635 | int ret; |
| 636 | |
| 637 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDRLABEL, |
| 638 | ip6addrlbl_newdel, |
| 639 | NULL, RTNL_FLAG_DOIT_UNLOCKED); |
| 640 | if (ret < 0) |
| 641 | return ret; |
| 642 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDRLABEL, |
| 643 | ip6addrlbl_newdel, |
| 644 | NULL, RTNL_FLAG_DOIT_UNLOCKED); |
| 645 | if (ret < 0) |
| 646 | return ret; |
| 647 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDRLABEL, |
| 648 | ip6addrlbl_get, |
| 649 | ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED); |
| 650 | return ret; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 651 | } |