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