David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Generic nexthop implementation |
| 3 | * |
| 4 | * Copyright (c) 2017-19 Cumulus Networks |
| 5 | * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/nexthop.h> |
| 9 | #include <linux/rtnetlink.h> |
| 10 | #include <linux/slab.h> |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 11 | #include <net/arp.h> |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 12 | #include <net/ipv6_stubs.h> |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 13 | #include <net/lwtunnel.h> |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 14 | #include <net/ndisc.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 15 | #include <net/nexthop.h> |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 16 | #include <net/route.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 19 | static void remove_nexthop(struct net *net, struct nexthop *nh, |
| 20 | struct nl_info *nlinfo); |
| 21 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 22 | #define NH_DEV_HASHBITS 8 |
| 23 | #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS) |
| 24 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 25 | static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = { |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 26 | [NHA_ID] = { .type = NLA_U32 }, |
| 27 | [NHA_GROUP] = { .type = NLA_BINARY }, |
| 28 | [NHA_GROUP_TYPE] = { .type = NLA_U16 }, |
| 29 | [NHA_BLACKHOLE] = { .type = NLA_FLAG }, |
| 30 | [NHA_OIF] = { .type = NLA_U32 }, |
| 31 | [NHA_GATEWAY] = { .type = NLA_BINARY }, |
| 32 | [NHA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 33 | [NHA_ENCAP] = { .type = NLA_NESTED }, |
| 34 | [NHA_GROUPS] = { .type = NLA_FLAG }, |
| 35 | [NHA_MASTER] = { .type = NLA_U32 }, |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 36 | [NHA_FDB] = { .type = NLA_FLAG }, |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 39 | static int call_nexthop_notifiers(struct net *net, |
Nathan Chancellor | d8e79f1 | 2020-05-27 01:00:20 -0700 | [diff] [blame] | 40 | enum nexthop_event_type event_type, |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 41 | struct nexthop *nh) |
| 42 | { |
| 43 | int err; |
| 44 | |
| 45 | err = atomic_notifier_call_chain(&net->nexthop.notifier_chain, |
| 46 | event_type, nh); |
| 47 | return notifier_to_errno(err); |
| 48 | } |
| 49 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 50 | static unsigned int nh_dev_hashfn(unsigned int val) |
| 51 | { |
| 52 | unsigned int mask = NH_DEV_HASHSIZE - 1; |
| 53 | |
| 54 | return (val ^ |
| 55 | (val >> NH_DEV_HASHBITS) ^ |
| 56 | (val >> (NH_DEV_HASHBITS * 2))) & mask; |
| 57 | } |
| 58 | |
| 59 | static void nexthop_devhash_add(struct net *net, struct nh_info *nhi) |
| 60 | { |
| 61 | struct net_device *dev = nhi->fib_nhc.nhc_dev; |
| 62 | struct hlist_head *head; |
| 63 | unsigned int hash; |
| 64 | |
| 65 | WARN_ON(!dev); |
| 66 | |
| 67 | hash = nh_dev_hashfn(dev->ifindex); |
| 68 | head = &net->nexthop.devhash[hash]; |
| 69 | hlist_add_head(&nhi->dev_hash, head); |
| 70 | } |
| 71 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 72 | static void nexthop_free_mpath(struct nexthop *nh) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 73 | { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 74 | struct nh_group *nhg; |
| 75 | int i; |
| 76 | |
| 77 | nhg = rcu_dereference_raw(nh->nh_grp); |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 78 | for (i = 0; i < nhg->num_nh; ++i) { |
| 79 | struct nh_grp_entry *nhge = &nhg->nh_entries[i]; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 80 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 81 | WARN_ON(!list_empty(&nhge->nh_list)); |
| 82 | nexthop_put(nhge->nh); |
| 83 | } |
| 84 | |
| 85 | WARN_ON(nhg->spare == nhg); |
| 86 | |
| 87 | kfree(nhg->spare); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 88 | kfree(nhg); |
| 89 | } |
| 90 | |
| 91 | static void nexthop_free_single(struct nexthop *nh) |
| 92 | { |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 93 | struct nh_info *nhi; |
| 94 | |
| 95 | nhi = rcu_dereference_raw(nh->nh_info); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 96 | switch (nhi->family) { |
| 97 | case AF_INET: |
| 98 | fib_nh_release(nh->net, &nhi->fib_nh); |
| 99 | break; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 100 | case AF_INET6: |
| 101 | ipv6_stub->fib6_nh_release(&nhi->fib6_nh); |
| 102 | break; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 103 | } |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 104 | kfree(nhi); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void nexthop_free_rcu(struct rcu_head *head) |
| 108 | { |
| 109 | struct nexthop *nh = container_of(head, struct nexthop, rcu); |
| 110 | |
| 111 | if (nh->is_group) |
| 112 | nexthop_free_mpath(nh); |
| 113 | else |
| 114 | nexthop_free_single(nh); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 115 | |
| 116 | kfree(nh); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(nexthop_free_rcu); |
| 119 | |
| 120 | static struct nexthop *nexthop_alloc(void) |
| 121 | { |
| 122 | struct nexthop *nh; |
| 123 | |
| 124 | nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 125 | if (nh) { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 126 | INIT_LIST_HEAD(&nh->fi_list); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 127 | INIT_LIST_HEAD(&nh->f6i_list); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 128 | INIT_LIST_HEAD(&nh->grp_list); |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 129 | INIT_LIST_HEAD(&nh->fdb_list); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 130 | } |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 131 | return nh; |
| 132 | } |
| 133 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 134 | static struct nh_group *nexthop_grp_alloc(u16 num_nh) |
| 135 | { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 136 | struct nh_group *nhg; |
| 137 | |
Ido Schimmel | d7d49dc | 2020-08-26 19:48:51 +0300 | [diff] [blame^] | 138 | nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 139 | if (nhg) |
| 140 | nhg->num_nh = num_nh; |
| 141 | |
| 142 | return nhg; |
| 143 | } |
| 144 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 145 | static void nh_base_seq_inc(struct net *net) |
| 146 | { |
| 147 | while (++net->nexthop.seq == 0) |
| 148 | ; |
| 149 | } |
| 150 | |
| 151 | /* no reference taken; rcu lock or rtnl must be held */ |
| 152 | struct nexthop *nexthop_find_by_id(struct net *net, u32 id) |
| 153 | { |
| 154 | struct rb_node **pp, *parent = NULL, *next; |
| 155 | |
| 156 | pp = &net->nexthop.rb_root.rb_node; |
| 157 | while (1) { |
| 158 | struct nexthop *nh; |
| 159 | |
| 160 | next = rcu_dereference_raw(*pp); |
| 161 | if (!next) |
| 162 | break; |
| 163 | parent = next; |
| 164 | |
| 165 | nh = rb_entry(parent, struct nexthop, rb_node); |
| 166 | if (id < nh->id) |
| 167 | pp = &next->rb_left; |
| 168 | else if (id > nh->id) |
| 169 | pp = &next->rb_right; |
| 170 | else |
| 171 | return nh; |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(nexthop_find_by_id); |
| 176 | |
| 177 | /* used for auto id allocation; called with rtnl held */ |
| 178 | static u32 nh_find_unused_id(struct net *net) |
| 179 | { |
| 180 | u32 id_start = net->nexthop.last_id_allocated; |
| 181 | |
| 182 | while (1) { |
| 183 | net->nexthop.last_id_allocated++; |
| 184 | if (net->nexthop.last_id_allocated == id_start) |
| 185 | break; |
| 186 | |
| 187 | if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated)) |
| 188 | return net->nexthop.last_id_allocated; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 193 | static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg) |
| 194 | { |
| 195 | struct nexthop_grp *p; |
| 196 | size_t len = nhg->num_nh * sizeof(*p); |
| 197 | struct nlattr *nla; |
| 198 | u16 group_type = 0; |
| 199 | int i; |
| 200 | |
| 201 | if (nhg->mpath) |
| 202 | group_type = NEXTHOP_GRP_TYPE_MPATH; |
| 203 | |
| 204 | if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type)) |
| 205 | goto nla_put_failure; |
| 206 | |
| 207 | nla = nla_reserve(skb, NHA_GROUP, len); |
| 208 | if (!nla) |
| 209 | goto nla_put_failure; |
| 210 | |
| 211 | p = nla_data(nla); |
| 212 | for (i = 0; i < nhg->num_nh; ++i) { |
| 213 | p->id = nhg->nh_entries[i].nh->id; |
| 214 | p->weight = nhg->nh_entries[i].weight - 1; |
| 215 | p += 1; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | |
| 220 | nla_put_failure: |
| 221 | return -EMSGSIZE; |
| 222 | } |
| 223 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 224 | static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh, |
| 225 | int event, u32 portid, u32 seq, unsigned int nlflags) |
| 226 | { |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 227 | struct fib6_nh *fib6_nh; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 228 | struct fib_nh *fib_nh; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 229 | struct nlmsghdr *nlh; |
| 230 | struct nh_info *nhi; |
| 231 | struct nhmsg *nhm; |
| 232 | |
| 233 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); |
| 234 | if (!nlh) |
| 235 | return -EMSGSIZE; |
| 236 | |
| 237 | nhm = nlmsg_data(nlh); |
| 238 | nhm->nh_family = AF_UNSPEC; |
| 239 | nhm->nh_flags = nh->nh_flags; |
| 240 | nhm->nh_protocol = nh->protocol; |
| 241 | nhm->nh_scope = 0; |
| 242 | nhm->resvd = 0; |
| 243 | |
| 244 | if (nla_put_u32(skb, NHA_ID, nh->id)) |
| 245 | goto nla_put_failure; |
| 246 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 247 | if (nh->is_group) { |
| 248 | struct nh_group *nhg = rtnl_dereference(nh->nh_grp); |
| 249 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 250 | if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB)) |
| 251 | goto nla_put_failure; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 252 | if (nla_put_nh_group(skb, nhg)) |
| 253 | goto nla_put_failure; |
| 254 | goto out; |
| 255 | } |
| 256 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 257 | nhi = rtnl_dereference(nh->nh_info); |
| 258 | nhm->nh_family = nhi->family; |
| 259 | if (nhi->reject_nh) { |
| 260 | if (nla_put_flag(skb, NHA_BLACKHOLE)) |
| 261 | goto nla_put_failure; |
| 262 | goto out; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 263 | } else if (nhi->fdb_nh) { |
| 264 | if (nla_put_flag(skb, NHA_FDB)) |
| 265 | goto nla_put_failure; |
| 266 | } else { |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 267 | const struct net_device *dev; |
| 268 | |
| 269 | dev = nhi->fib_nhc.nhc_dev; |
| 270 | if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex)) |
| 271 | goto nla_put_failure; |
| 272 | } |
| 273 | |
| 274 | nhm->nh_scope = nhi->fib_nhc.nhc_scope; |
| 275 | switch (nhi->family) { |
| 276 | case AF_INET: |
| 277 | fib_nh = &nhi->fib_nh; |
| 278 | if (fib_nh->fib_nh_gw_family && |
| 279 | nla_put_u32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4)) |
| 280 | goto nla_put_failure; |
| 281 | break; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 282 | |
| 283 | case AF_INET6: |
| 284 | fib6_nh = &nhi->fib6_nh; |
| 285 | if (fib6_nh->fib_nh_gw_family && |
| 286 | nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6)) |
| 287 | goto nla_put_failure; |
| 288 | break; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 289 | } |
| 290 | |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 291 | if (nhi->fib_nhc.nhc_lwtstate && |
| 292 | lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate, |
| 293 | NHA_ENCAP, NHA_ENCAP_TYPE) < 0) |
| 294 | goto nla_put_failure; |
| 295 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 296 | out: |
| 297 | nlmsg_end(skb, nlh); |
| 298 | return 0; |
| 299 | |
| 300 | nla_put_failure: |
Stephen Worley | d69100b | 2020-05-19 21:57:12 -0400 | [diff] [blame] | 301 | nlmsg_cancel(skb, nlh); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 302 | return -EMSGSIZE; |
| 303 | } |
| 304 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 305 | static size_t nh_nlmsg_size_grp(struct nexthop *nh) |
| 306 | { |
| 307 | struct nh_group *nhg = rtnl_dereference(nh->nh_grp); |
| 308 | size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh; |
| 309 | |
| 310 | return nla_total_size(sz) + |
| 311 | nla_total_size(2); /* NHA_GROUP_TYPE */ |
| 312 | } |
| 313 | |
| 314 | static size_t nh_nlmsg_size_single(struct nexthop *nh) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 315 | { |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 316 | struct nh_info *nhi = rtnl_dereference(nh->nh_info); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 317 | size_t sz; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 318 | |
| 319 | /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE |
| 320 | * are mutually exclusive |
| 321 | */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 322 | sz = nla_total_size(4); /* NHA_OIF */ |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 323 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 324 | switch (nhi->family) { |
| 325 | case AF_INET: |
| 326 | if (nhi->fib_nh.fib_nh_gw_family) |
| 327 | sz += nla_total_size(4); /* NHA_GATEWAY */ |
| 328 | break; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 329 | |
| 330 | case AF_INET6: |
| 331 | /* NHA_GATEWAY */ |
| 332 | if (nhi->fib6_nh.fib_nh_gw_family) |
| 333 | sz += nla_total_size(sizeof(const struct in6_addr)); |
| 334 | break; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 335 | } |
| 336 | |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 337 | if (nhi->fib_nhc.nhc_lwtstate) { |
| 338 | sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate); |
| 339 | sz += nla_total_size(2); /* NHA_ENCAP_TYPE */ |
| 340 | } |
| 341 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 342 | return sz; |
| 343 | } |
| 344 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 345 | static size_t nh_nlmsg_size(struct nexthop *nh) |
| 346 | { |
Stephen Worley | f9e9555 | 2020-01-24 16:53:27 -0500 | [diff] [blame] | 347 | size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg)); |
| 348 | |
| 349 | sz += nla_total_size(4); /* NHA_ID */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 350 | |
| 351 | if (nh->is_group) |
| 352 | sz += nh_nlmsg_size_grp(nh); |
| 353 | else |
| 354 | sz += nh_nlmsg_size_single(nh); |
| 355 | |
| 356 | return sz; |
| 357 | } |
| 358 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 359 | static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info) |
| 360 | { |
| 361 | unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0; |
| 362 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 363 | struct sk_buff *skb; |
| 364 | int err = -ENOBUFS; |
| 365 | |
| 366 | skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any()); |
| 367 | if (!skb) |
| 368 | goto errout; |
| 369 | |
| 370 | err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags); |
| 371 | if (err < 0) { |
| 372 | /* -EMSGSIZE implies BUG in nh_nlmsg_size() */ |
| 373 | WARN_ON(err == -EMSGSIZE); |
| 374 | kfree_skb(skb); |
| 375 | goto errout; |
| 376 | } |
| 377 | |
| 378 | rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP, |
| 379 | info->nlh, gfp_any()); |
| 380 | return; |
| 381 | errout: |
| 382 | if (err < 0) |
| 383 | rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err); |
| 384 | } |
| 385 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 386 | static bool valid_group_nh(struct nexthop *nh, unsigned int npaths, |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 387 | bool *is_fdb, struct netlink_ext_ack *extack) |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 388 | { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 389 | if (nh->is_group) { |
| 390 | struct nh_group *nhg = rtnl_dereference(nh->nh_grp); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 391 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 392 | /* nested multipath (group within a group) is not |
| 393 | * supported |
| 394 | */ |
| 395 | if (nhg->mpath) { |
| 396 | NL_SET_ERR_MSG(extack, |
| 397 | "Multipath group can not be a nexthop within a group"); |
| 398 | return false; |
| 399 | } |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 400 | *is_fdb = nhg->fdb_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 401 | } else { |
| 402 | struct nh_info *nhi = rtnl_dereference(nh->nh_info); |
| 403 | |
| 404 | if (nhi->reject_nh && npaths > 1) { |
| 405 | NL_SET_ERR_MSG(extack, |
| 406 | "Blackhole nexthop can not be used in a group with more than 1 path"); |
| 407 | return false; |
| 408 | } |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 409 | *is_fdb = nhi->fdb_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 415 | static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family, |
| 416 | struct netlink_ext_ack *extack) |
| 417 | { |
| 418 | struct nh_info *nhi; |
| 419 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 420 | nhi = rtnl_dereference(nh->nh_info); |
| 421 | |
| 422 | if (!nhi->fdb_nh) { |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 423 | NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops"); |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 427 | if (*nh_family == AF_UNSPEC) { |
| 428 | *nh_family = nhi->family; |
| 429 | } else if (*nh_family != nhi->family) { |
| 430 | NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops"); |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 437 | static int nh_check_attr_group(struct net *net, struct nlattr *tb[], |
| 438 | struct netlink_ext_ack *extack) |
| 439 | { |
| 440 | unsigned int len = nla_len(tb[NHA_GROUP]); |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 441 | u8 nh_family = AF_UNSPEC; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 442 | struct nexthop_grp *nhg; |
| 443 | unsigned int i, j; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 444 | u8 nhg_fdb = 0; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 445 | |
Nikolay Aleksandrov | eeaac36 | 2020-08-22 15:06:36 +0300 | [diff] [blame] | 446 | if (!len || len & (sizeof(struct nexthop_grp) - 1)) { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 447 | NL_SET_ERR_MSG(extack, |
| 448 | "Invalid length for nexthop group attribute"); |
| 449 | return -EINVAL; |
| 450 | } |
| 451 | |
| 452 | /* convert len to number of nexthop ids */ |
| 453 | len /= sizeof(*nhg); |
| 454 | |
| 455 | nhg = nla_data(tb[NHA_GROUP]); |
| 456 | for (i = 0; i < len; ++i) { |
| 457 | if (nhg[i].resvd1 || nhg[i].resvd2) { |
| 458 | NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0"); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | if (nhg[i].weight > 254) { |
| 462 | NL_SET_ERR_MSG(extack, "Invalid value for weight"); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | for (j = i + 1; j < len; ++j) { |
| 466 | if (nhg[i].id == nhg[j].id) { |
| 467 | NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group"); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 473 | if (tb[NHA_FDB]) |
| 474 | nhg_fdb = 1; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 475 | nhg = nla_data(tb[NHA_GROUP]); |
| 476 | for (i = 0; i < len; ++i) { |
| 477 | struct nexthop *nh; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 478 | bool is_fdb_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 479 | |
| 480 | nh = nexthop_find_by_id(net, nhg[i].id); |
| 481 | if (!nh) { |
| 482 | NL_SET_ERR_MSG(extack, "Invalid nexthop id"); |
| 483 | return -EINVAL; |
| 484 | } |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 485 | if (!valid_group_nh(nh, len, &is_fdb_nh, extack)) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 486 | return -EINVAL; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 487 | |
| 488 | if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack)) |
| 489 | return -EINVAL; |
| 490 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 491 | if (!nhg_fdb && is_fdb_nh) { |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 492 | NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops"); |
| 493 | return -EINVAL; |
| 494 | } |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 495 | } |
David Ahern | 84be69b | 2020-05-17 11:26:32 -0600 | [diff] [blame] | 496 | for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 497 | if (!tb[i]) |
| 498 | continue; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 499 | if (tb[NHA_FDB]) |
| 500 | continue; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 501 | NL_SET_ERR_MSG(extack, |
| 502 | "No other attributes can be set in nexthop groups"); |
| 503 | return -EINVAL; |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static bool ipv6_good_nh(const struct fib6_nh *nh) |
| 510 | { |
| 511 | int state = NUD_REACHABLE; |
| 512 | struct neighbour *n; |
| 513 | |
| 514 | rcu_read_lock_bh(); |
| 515 | |
| 516 | n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6); |
| 517 | if (n) |
| 518 | state = n->nud_state; |
| 519 | |
| 520 | rcu_read_unlock_bh(); |
| 521 | |
| 522 | return !!(state & NUD_VALID); |
| 523 | } |
| 524 | |
| 525 | static bool ipv4_good_nh(const struct fib_nh *nh) |
| 526 | { |
| 527 | int state = NUD_REACHABLE; |
| 528 | struct neighbour *n; |
| 529 | |
| 530 | rcu_read_lock_bh(); |
| 531 | |
| 532 | n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, |
| 533 | (__force u32)nh->fib_nh_gw4); |
| 534 | if (n) |
| 535 | state = n->nud_state; |
| 536 | |
| 537 | rcu_read_unlock_bh(); |
| 538 | |
| 539 | return !!(state & NUD_VALID); |
| 540 | } |
| 541 | |
| 542 | struct nexthop *nexthop_select_path(struct nexthop *nh, int hash) |
| 543 | { |
| 544 | struct nexthop *rc = NULL; |
| 545 | struct nh_group *nhg; |
| 546 | int i; |
| 547 | |
| 548 | if (!nh->is_group) |
| 549 | return nh; |
| 550 | |
| 551 | nhg = rcu_dereference(nh->nh_grp); |
| 552 | for (i = 0; i < nhg->num_nh; ++i) { |
| 553 | struct nh_grp_entry *nhge = &nhg->nh_entries[i]; |
| 554 | struct nh_info *nhi; |
| 555 | |
| 556 | if (hash > atomic_read(&nhge->upper_bound)) |
| 557 | continue; |
| 558 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 559 | nhi = rcu_dereference(nhge->nh->nh_info); |
| 560 | if (nhi->fdb_nh) |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 561 | return nhge->nh; |
| 562 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 563 | /* nexthops always check if it is good and does |
| 564 | * not rely on a sysctl for this behavior |
| 565 | */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 566 | switch (nhi->family) { |
| 567 | case AF_INET: |
| 568 | if (ipv4_good_nh(&nhi->fib_nh)) |
| 569 | return nhge->nh; |
| 570 | break; |
| 571 | case AF_INET6: |
| 572 | if (ipv6_good_nh(&nhi->fib6_nh)) |
| 573 | return nhge->nh; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | if (!rc) |
| 578 | rc = nhge->nh; |
| 579 | } |
| 580 | |
| 581 | return rc; |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(nexthop_select_path); |
| 584 | |
David Ahern | f88c9aa | 2019-06-08 14:53:22 -0700 | [diff] [blame] | 585 | int nexthop_for_each_fib6_nh(struct nexthop *nh, |
| 586 | int (*cb)(struct fib6_nh *nh, void *arg), |
| 587 | void *arg) |
| 588 | { |
| 589 | struct nh_info *nhi; |
| 590 | int err; |
| 591 | |
| 592 | if (nh->is_group) { |
| 593 | struct nh_group *nhg; |
| 594 | int i; |
| 595 | |
| 596 | nhg = rcu_dereference_rtnl(nh->nh_grp); |
| 597 | for (i = 0; i < nhg->num_nh; i++) { |
| 598 | struct nh_grp_entry *nhge = &nhg->nh_entries[i]; |
| 599 | |
| 600 | nhi = rcu_dereference_rtnl(nhge->nh->nh_info); |
| 601 | err = cb(&nhi->fib6_nh, arg); |
| 602 | if (err) |
| 603 | return err; |
| 604 | } |
| 605 | } else { |
| 606 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 607 | err = cb(&nhi->fib6_nh, arg); |
| 608 | if (err) |
| 609 | return err; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh); |
| 615 | |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 616 | static int check_src_addr(const struct in6_addr *saddr, |
| 617 | struct netlink_ext_ack *extack) |
| 618 | { |
| 619 | if (!ipv6_addr_any(saddr)) { |
| 620 | NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects"); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | return 0; |
| 624 | } |
| 625 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 626 | int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, |
| 627 | struct netlink_ext_ack *extack) |
| 628 | { |
| 629 | struct nh_info *nhi; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 630 | bool is_fdb_nh; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 631 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 632 | /* fib6_src is unique to a fib6_info and limits the ability to cache |
| 633 | * routes in fib6_nh within a nexthop that is potentially shared |
| 634 | * across multiple fib entries. If the config wants to use source |
| 635 | * routing it can not use nexthop objects. mlxsw also does not allow |
| 636 | * fib6_src on routes. |
| 637 | */ |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 638 | if (cfg && check_src_addr(&cfg->fc_src, extack) < 0) |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 639 | return -EINVAL; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 640 | |
| 641 | if (nh->is_group) { |
| 642 | struct nh_group *nhg; |
| 643 | |
| 644 | nhg = rtnl_dereference(nh->nh_grp); |
| 645 | if (nhg->has_v4) |
| 646 | goto no_v4_nh; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 647 | is_fdb_nh = nhg->fdb_nh; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 648 | } else { |
| 649 | nhi = rtnl_dereference(nh->nh_info); |
| 650 | if (nhi->family == AF_INET) |
| 651 | goto no_v4_nh; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 652 | is_fdb_nh = nhi->fdb_nh; |
| 653 | } |
| 654 | |
| 655 | if (is_fdb_nh) { |
| 656 | NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); |
| 657 | return -EINVAL; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return 0; |
| 661 | no_v4_nh: |
| 662 | NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop"); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | EXPORT_SYMBOL_GPL(fib6_check_nexthop); |
| 666 | |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 667 | /* if existing nexthop has ipv6 routes linked to it, need |
| 668 | * to verify this new spec works with ipv6 |
| 669 | */ |
| 670 | static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new, |
| 671 | struct netlink_ext_ack *extack) |
| 672 | { |
| 673 | struct fib6_info *f6i; |
| 674 | |
| 675 | if (list_empty(&old->f6i_list)) |
| 676 | return 0; |
| 677 | |
| 678 | list_for_each_entry(f6i, &old->f6i_list, nh_list) { |
| 679 | if (check_src_addr(&f6i->fib6_src.addr, extack) < 0) |
| 680 | return -EINVAL; |
| 681 | } |
| 682 | |
| 683 | return fib6_check_nexthop(new, NULL, extack); |
| 684 | } |
| 685 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 686 | static int nexthop_check_scope(struct nh_info *nhi, u8 scope, |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 687 | struct netlink_ext_ack *extack) |
| 688 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 689 | if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) { |
| 690 | NL_SET_ERR_MSG(extack, |
| 691 | "Route with host scope can not have a gateway"); |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) { |
| 696 | NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop"); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* Invoked by fib add code to verify nexthop by id is ok with |
| 704 | * config for prefix; parts of fib_check_nh not done when nexthop |
| 705 | * object is used. |
| 706 | */ |
| 707 | int fib_check_nexthop(struct nexthop *nh, u8 scope, |
| 708 | struct netlink_ext_ack *extack) |
| 709 | { |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 710 | struct nh_info *nhi; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 711 | int err = 0; |
| 712 | |
| 713 | if (nh->is_group) { |
| 714 | struct nh_group *nhg; |
| 715 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 716 | nhg = rtnl_dereference(nh->nh_grp); |
| 717 | if (nhg->fdb_nh) { |
| 718 | NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); |
| 719 | err = -EINVAL; |
| 720 | goto out; |
| 721 | } |
| 722 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 723 | if (scope == RT_SCOPE_HOST) { |
| 724 | NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops"); |
| 725 | err = -EINVAL; |
| 726 | goto out; |
| 727 | } |
| 728 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 729 | /* all nexthops in a group have the same scope */ |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 730 | nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info); |
| 731 | err = nexthop_check_scope(nhi, scope, extack); |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 732 | } else { |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 733 | nhi = rtnl_dereference(nh->nh_info); |
| 734 | if (nhi->fdb_nh) { |
| 735 | NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); |
| 736 | err = -EINVAL; |
| 737 | goto out; |
| 738 | } |
| 739 | err = nexthop_check_scope(nhi, scope, extack); |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 740 | } |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 741 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 742 | out: |
| 743 | return err; |
| 744 | } |
| 745 | |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 746 | static int fib_check_nh_list(struct nexthop *old, struct nexthop *new, |
| 747 | struct netlink_ext_ack *extack) |
| 748 | { |
| 749 | struct fib_info *fi; |
| 750 | |
| 751 | list_for_each_entry(fi, &old->fi_list, nh_list) { |
| 752 | int err; |
| 753 | |
| 754 | err = fib_check_nexthop(new, fi->fib_scope, extack); |
| 755 | if (err) |
| 756 | return err; |
| 757 | } |
| 758 | return 0; |
| 759 | } |
| 760 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 761 | static void nh_group_rebalance(struct nh_group *nhg) |
| 762 | { |
| 763 | int total = 0; |
| 764 | int w = 0; |
| 765 | int i; |
| 766 | |
| 767 | for (i = 0; i < nhg->num_nh; ++i) |
| 768 | total += nhg->nh_entries[i].weight; |
| 769 | |
| 770 | for (i = 0; i < nhg->num_nh; ++i) { |
| 771 | struct nh_grp_entry *nhge = &nhg->nh_entries[i]; |
| 772 | int upper_bound; |
| 773 | |
| 774 | w += nhge->weight; |
| 775 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1; |
| 776 | atomic_set(&nhge->upper_bound, upper_bound); |
| 777 | } |
| 778 | } |
| 779 | |
David Ahern | ac21753 | 2020-05-26 12:56:14 -0600 | [diff] [blame] | 780 | static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 781 | struct nl_info *nlinfo) |
| 782 | { |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 783 | struct nh_grp_entry *nhges, *new_nhges; |
David Ahern | ac21753 | 2020-05-26 12:56:14 -0600 | [diff] [blame] | 784 | struct nexthop *nhp = nhge->nh_parent; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 785 | struct nexthop *nh = nhge->nh; |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 786 | struct nh_group *nhg, *newg; |
| 787 | int i, j; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 788 | |
| 789 | WARN_ON(!nh); |
| 790 | |
David Ahern | ac21753 | 2020-05-26 12:56:14 -0600 | [diff] [blame] | 791 | nhg = rtnl_dereference(nhp->nh_grp); |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 792 | newg = nhg->spare; |
| 793 | |
| 794 | /* last entry, keep it visible and remove the parent */ |
| 795 | if (nhg->num_nh == 1) { |
| 796 | remove_nexthop(net, nhp, nlinfo); |
| 797 | return; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 800 | newg->has_v4 = nhg->has_v4; |
| 801 | newg->mpath = nhg->mpath; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 802 | newg->fdb_nh = nhg->fdb_nh; |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 803 | newg->num_nh = nhg->num_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 804 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 805 | /* copy old entries to new except the one getting removed */ |
| 806 | nhges = nhg->nh_entries; |
| 807 | new_nhges = newg->nh_entries; |
| 808 | for (i = 0, j = 0; i < nhg->num_nh; ++i) { |
| 809 | /* current nexthop getting removed */ |
| 810 | if (nhg->nh_entries[i].nh == nh) { |
| 811 | newg->num_nh--; |
| 812 | continue; |
| 813 | } |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 814 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 815 | list_del(&nhges[i].nh_list); |
| 816 | new_nhges[j].nh_parent = nhges[i].nh_parent; |
| 817 | new_nhges[j].nh = nhges[i].nh; |
| 818 | new_nhges[j].weight = nhges[i].weight; |
| 819 | list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); |
| 820 | j++; |
| 821 | } |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 822 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 823 | nh_group_rebalance(newg); |
| 824 | rcu_assign_pointer(nhp->nh_grp, newg); |
| 825 | |
| 826 | list_del(&nhge->nh_list); |
| 827 | nexthop_put(nhge->nh); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 828 | |
| 829 | if (nlinfo) |
David Ahern | ac21753 | 2020-05-26 12:56:14 -0600 | [diff] [blame] | 830 | nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh, |
| 834 | struct nl_info *nlinfo) |
| 835 | { |
| 836 | struct nh_grp_entry *nhge, *tmp; |
| 837 | |
David Ahern | ac21753 | 2020-05-26 12:56:14 -0600 | [diff] [blame] | 838 | list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) |
| 839 | remove_nh_grp_entry(net, nhge, nlinfo); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 840 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 841 | /* make sure all see the newly published array before releasing rtnl */ |
| 842 | synchronize_rcu(); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) |
| 846 | { |
| 847 | struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); |
| 848 | int i, num_nh = nhg->num_nh; |
| 849 | |
| 850 | for (i = 0; i < num_nh; ++i) { |
| 851 | struct nh_grp_entry *nhge = &nhg->nh_entries[i]; |
| 852 | |
| 853 | if (WARN_ON(!nhge->nh)) |
| 854 | continue; |
| 855 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 856 | list_del_init(&nhge->nh_list); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 860 | /* not called for nexthop replace */ |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 861 | static void __remove_nexthop_fib(struct net *net, struct nexthop *nh) |
| 862 | { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 863 | struct fib6_info *f6i, *tmp; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 864 | bool do_flush = false; |
| 865 | struct fib_info *fi; |
| 866 | |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 867 | call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh); |
| 868 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 869 | list_for_each_entry(fi, &nh->fi_list, nh_list) { |
| 870 | fi->fib_flags |= RTNH_F_DEAD; |
| 871 | do_flush = true; |
| 872 | } |
| 873 | if (do_flush) |
| 874 | fib_flush(net); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 875 | |
| 876 | /* ip6_del_rt removes the entry from this list hence the _safe */ |
| 877 | list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) { |
| 878 | /* __ip6_del_rt does a release, so do a hold here */ |
| 879 | fib6_info_hold(f6i); |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 880 | ipv6_stub->ip6_del_rt(net, f6i, |
| 881 | !net->ipv4.sysctl_nexthop_compat_mode); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 882 | } |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 883 | } |
| 884 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 885 | static void __remove_nexthop(struct net *net, struct nexthop *nh, |
| 886 | struct nl_info *nlinfo) |
| 887 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 888 | __remove_nexthop_fib(net, nh); |
| 889 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 890 | if (nh->is_group) { |
| 891 | remove_nexthop_group(nh, nlinfo); |
| 892 | } else { |
| 893 | struct nh_info *nhi; |
| 894 | |
| 895 | nhi = rtnl_dereference(nh->nh_info); |
| 896 | if (nhi->fib_nhc.nhc_dev) |
| 897 | hlist_del(&nhi->dev_hash); |
| 898 | |
| 899 | remove_nexthop_from_groups(net, nh, nlinfo); |
| 900 | } |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 901 | } |
| 902 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 903 | static void remove_nexthop(struct net *net, struct nexthop *nh, |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 904 | struct nl_info *nlinfo) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 905 | { |
| 906 | /* remove from the tree */ |
| 907 | rb_erase(&nh->rb_node, &net->nexthop.rb_root); |
| 908 | |
| 909 | if (nlinfo) |
| 910 | nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); |
| 911 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 912 | __remove_nexthop(net, nh, nlinfo); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 913 | nh_base_seq_inc(net); |
| 914 | |
| 915 | nexthop_put(nh); |
| 916 | } |
| 917 | |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 918 | /* if any FIB entries reference this nexthop, any dst entries |
| 919 | * need to be regenerated |
| 920 | */ |
| 921 | static void nh_rt_cache_flush(struct net *net, struct nexthop *nh) |
| 922 | { |
| 923 | struct fib6_info *f6i; |
| 924 | |
| 925 | if (!list_empty(&nh->fi_list)) |
| 926 | rt_cache_flush(net); |
| 927 | |
| 928 | list_for_each_entry(f6i, &nh->f6i_list, nh_list) |
| 929 | ipv6_stub->fib6_update_sernum(net, f6i); |
| 930 | } |
| 931 | |
| 932 | static int replace_nexthop_grp(struct net *net, struct nexthop *old, |
| 933 | struct nexthop *new, |
| 934 | struct netlink_ext_ack *extack) |
| 935 | { |
| 936 | struct nh_group *oldg, *newg; |
| 937 | int i; |
| 938 | |
| 939 | if (!new->is_group) { |
| 940 | NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | |
| 944 | oldg = rtnl_dereference(old->nh_grp); |
| 945 | newg = rtnl_dereference(new->nh_grp); |
| 946 | |
| 947 | /* update parents - used by nexthop code for cleanup */ |
| 948 | for (i = 0; i < newg->num_nh; i++) |
| 949 | newg->nh_entries[i].nh_parent = old; |
| 950 | |
| 951 | rcu_assign_pointer(old->nh_grp, newg); |
| 952 | |
| 953 | for (i = 0; i < oldg->num_nh; i++) |
| 954 | oldg->nh_entries[i].nh_parent = new; |
| 955 | |
| 956 | rcu_assign_pointer(new->nh_grp, oldg); |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static int replace_nexthop_single(struct net *net, struct nexthop *old, |
| 962 | struct nexthop *new, |
| 963 | struct netlink_ext_ack *extack) |
| 964 | { |
| 965 | struct nh_info *oldi, *newi; |
| 966 | |
| 967 | if (new->is_group) { |
| 968 | NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); |
| 969 | return -EINVAL; |
| 970 | } |
| 971 | |
| 972 | oldi = rtnl_dereference(old->nh_info); |
| 973 | newi = rtnl_dereference(new->nh_info); |
| 974 | |
| 975 | newi->nh_parent = old; |
| 976 | oldi->nh_parent = new; |
| 977 | |
| 978 | old->protocol = new->protocol; |
| 979 | old->nh_flags = new->nh_flags; |
| 980 | |
| 981 | rcu_assign_pointer(old->nh_info, newi); |
| 982 | rcu_assign_pointer(new->nh_info, oldi); |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static void __nexthop_replace_notify(struct net *net, struct nexthop *nh, |
| 988 | struct nl_info *info) |
| 989 | { |
| 990 | struct fib6_info *f6i; |
| 991 | |
| 992 | if (!list_empty(&nh->fi_list)) { |
| 993 | struct fib_info *fi; |
| 994 | |
| 995 | /* expectation is a few fib_info per nexthop and then |
| 996 | * a lot of routes per fib_info. So mark the fib_info |
| 997 | * and then walk the fib tables once |
| 998 | */ |
| 999 | list_for_each_entry(fi, &nh->fi_list, nh_list) |
| 1000 | fi->nh_updated = true; |
| 1001 | |
| 1002 | fib_info_notify_update(net, info); |
| 1003 | |
| 1004 | list_for_each_entry(fi, &nh->fi_list, nh_list) |
| 1005 | fi->nh_updated = false; |
| 1006 | } |
| 1007 | |
| 1008 | list_for_each_entry(f6i, &nh->f6i_list, nh_list) |
| 1009 | ipv6_stub->fib6_rt_update(net, f6i, info); |
| 1010 | } |
| 1011 | |
| 1012 | /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries |
| 1013 | * linked to this nexthop and for all groups that the nexthop |
| 1014 | * is a member of |
| 1015 | */ |
| 1016 | static void nexthop_replace_notify(struct net *net, struct nexthop *nh, |
| 1017 | struct nl_info *info) |
| 1018 | { |
| 1019 | struct nh_grp_entry *nhge; |
| 1020 | |
| 1021 | __nexthop_replace_notify(net, nh, info); |
| 1022 | |
| 1023 | list_for_each_entry(nhge, &nh->grp_list, nh_list) |
| 1024 | __nexthop_replace_notify(net, nhge->nh_parent, info); |
| 1025 | } |
| 1026 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1027 | static int replace_nexthop(struct net *net, struct nexthop *old, |
| 1028 | struct nexthop *new, struct netlink_ext_ack *extack) |
| 1029 | { |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 1030 | bool new_is_reject = false; |
| 1031 | struct nh_grp_entry *nhge; |
| 1032 | int err; |
| 1033 | |
| 1034 | /* check that existing FIB entries are ok with the |
| 1035 | * new nexthop definition |
| 1036 | */ |
| 1037 | err = fib_check_nh_list(old, new, extack); |
| 1038 | if (err) |
| 1039 | return err; |
| 1040 | |
| 1041 | err = fib6_check_nh_list(old, new, extack); |
| 1042 | if (err) |
| 1043 | return err; |
| 1044 | |
| 1045 | if (!new->is_group) { |
| 1046 | struct nh_info *nhi = rtnl_dereference(new->nh_info); |
| 1047 | |
| 1048 | new_is_reject = nhi->reject_nh; |
| 1049 | } |
| 1050 | |
| 1051 | list_for_each_entry(nhge, &old->grp_list, nh_list) { |
| 1052 | /* if new nexthop is a blackhole, any groups using this |
| 1053 | * nexthop cannot have more than 1 path |
| 1054 | */ |
| 1055 | if (new_is_reject && |
| 1056 | nexthop_num_path(nhge->nh_parent) > 1) { |
| 1057 | NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); |
| 1058 | return -EINVAL; |
| 1059 | } |
| 1060 | |
| 1061 | err = fib_check_nh_list(nhge->nh_parent, new, extack); |
| 1062 | if (err) |
| 1063 | return err; |
| 1064 | |
| 1065 | err = fib6_check_nh_list(nhge->nh_parent, new, extack); |
| 1066 | if (err) |
| 1067 | return err; |
| 1068 | } |
| 1069 | |
| 1070 | if (old->is_group) |
| 1071 | err = replace_nexthop_grp(net, old, new, extack); |
| 1072 | else |
| 1073 | err = replace_nexthop_single(net, old, new, extack); |
| 1074 | |
| 1075 | if (!err) { |
| 1076 | nh_rt_cache_flush(net, old); |
| 1077 | |
| 1078 | __remove_nexthop(net, new, NULL); |
| 1079 | nexthop_put(new); |
| 1080 | } |
| 1081 | |
| 1082 | return err; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* called with rtnl_lock held */ |
| 1086 | static int insert_nexthop(struct net *net, struct nexthop *new_nh, |
| 1087 | struct nh_config *cfg, struct netlink_ext_ack *extack) |
| 1088 | { |
| 1089 | struct rb_node **pp, *parent = NULL, *next; |
| 1090 | struct rb_root *root = &net->nexthop.rb_root; |
| 1091 | bool replace = !!(cfg->nlflags & NLM_F_REPLACE); |
| 1092 | bool create = !!(cfg->nlflags & NLM_F_CREATE); |
| 1093 | u32 new_id = new_nh->id; |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 1094 | int replace_notify = 0; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1095 | int rc = -EEXIST; |
| 1096 | |
| 1097 | pp = &root->rb_node; |
| 1098 | while (1) { |
| 1099 | struct nexthop *nh; |
| 1100 | |
| 1101 | next = rtnl_dereference(*pp); |
| 1102 | if (!next) |
| 1103 | break; |
| 1104 | |
| 1105 | parent = next; |
| 1106 | |
| 1107 | nh = rb_entry(parent, struct nexthop, rb_node); |
| 1108 | if (new_id < nh->id) { |
| 1109 | pp = &next->rb_left; |
| 1110 | } else if (new_id > nh->id) { |
| 1111 | pp = &next->rb_right; |
| 1112 | } else if (replace) { |
| 1113 | rc = replace_nexthop(net, nh, new_nh, extack); |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 1114 | if (!rc) { |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1115 | new_nh = nh; /* send notification with old nh */ |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 1116 | replace_notify = 1; |
| 1117 | } |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1118 | goto out; |
| 1119 | } else { |
| 1120 | /* id already exists and not a replace */ |
| 1121 | goto out; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | if (replace && !create) { |
| 1126 | NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); |
| 1127 | rc = -ENOENT; |
| 1128 | goto out; |
| 1129 | } |
| 1130 | |
| 1131 | rb_link_node_rcu(&new_nh->rb_node, parent, pp); |
| 1132 | rb_insert_color(&new_nh->rb_node, root); |
| 1133 | rc = 0; |
| 1134 | out: |
| 1135 | if (!rc) { |
| 1136 | nh_base_seq_inc(net); |
| 1137 | nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 1138 | if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode) |
David Ahern | 7bf4796 | 2019-06-08 14:53:35 -0700 | [diff] [blame] | 1139 | nexthop_replace_notify(net, new_nh, &cfg->nlinfo); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | return rc; |
| 1143 | } |
| 1144 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1145 | /* rtnl */ |
| 1146 | /* remove all nexthops tied to a device being deleted */ |
| 1147 | static void nexthop_flush_dev(struct net_device *dev) |
| 1148 | { |
| 1149 | unsigned int hash = nh_dev_hashfn(dev->ifindex); |
| 1150 | struct net *net = dev_net(dev); |
| 1151 | struct hlist_head *head = &net->nexthop.devhash[hash]; |
| 1152 | struct hlist_node *n; |
| 1153 | struct nh_info *nhi; |
| 1154 | |
| 1155 | hlist_for_each_entry_safe(nhi, n, head, dev_hash) { |
| 1156 | if (nhi->fib_nhc.nhc_dev != dev) |
| 1157 | continue; |
| 1158 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1159 | remove_nexthop(net, nhi->nh_parent, NULL); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1163 | /* rtnl; called when net namespace is deleted */ |
| 1164 | static void flush_all_nexthops(struct net *net) |
| 1165 | { |
| 1166 | struct rb_root *root = &net->nexthop.rb_root; |
| 1167 | struct rb_node *node; |
| 1168 | struct nexthop *nh; |
| 1169 | |
| 1170 | while ((node = rb_first(root))) { |
| 1171 | nh = rb_entry(node, struct nexthop, rb_node); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1172 | remove_nexthop(net, nh, NULL); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1173 | cond_resched(); |
| 1174 | } |
| 1175 | } |
| 1176 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1177 | static struct nexthop *nexthop_create_group(struct net *net, |
| 1178 | struct nh_config *cfg) |
| 1179 | { |
| 1180 | struct nlattr *grps_attr = cfg->nh_grp; |
| 1181 | struct nexthop_grp *entry = nla_data(grps_attr); |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1182 | u16 num_nh = nla_len(grps_attr) / sizeof(*entry); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1183 | struct nh_group *nhg; |
| 1184 | struct nexthop *nh; |
| 1185 | int i; |
| 1186 | |
Nikolay Aleksandrov | eeaac36 | 2020-08-22 15:06:36 +0300 | [diff] [blame] | 1187 | if (WARN_ON(!num_nh)) |
| 1188 | return ERR_PTR(-EINVAL); |
| 1189 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1190 | nh = nexthop_alloc(); |
| 1191 | if (!nh) |
| 1192 | return ERR_PTR(-ENOMEM); |
| 1193 | |
| 1194 | nh->is_group = 1; |
| 1195 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1196 | nhg = nexthop_grp_alloc(num_nh); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1197 | if (!nhg) { |
| 1198 | kfree(nh); |
| 1199 | return ERR_PTR(-ENOMEM); |
| 1200 | } |
| 1201 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1202 | /* spare group used for removals */ |
| 1203 | nhg->spare = nexthop_grp_alloc(num_nh); |
Patrick Eigensatz | dafe207 | 2020-06-01 13:12:01 +0200 | [diff] [blame] | 1204 | if (!nhg->spare) { |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1205 | kfree(nhg); |
| 1206 | kfree(nh); |
Patrick Eigensatz | dafe207 | 2020-06-01 13:12:01 +0200 | [diff] [blame] | 1207 | return ERR_PTR(-ENOMEM); |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1208 | } |
| 1209 | nhg->spare->spare = nhg; |
| 1210 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1211 | for (i = 0; i < nhg->num_nh; ++i) { |
| 1212 | struct nexthop *nhe; |
| 1213 | struct nh_info *nhi; |
| 1214 | |
| 1215 | nhe = nexthop_find_by_id(net, entry[i].id); |
| 1216 | if (!nexthop_get(nhe)) |
| 1217 | goto out_no_nh; |
| 1218 | |
| 1219 | nhi = rtnl_dereference(nhe->nh_info); |
| 1220 | if (nhi->family == AF_INET) |
| 1221 | nhg->has_v4 = true; |
| 1222 | |
| 1223 | nhg->nh_entries[i].nh = nhe; |
| 1224 | nhg->nh_entries[i].weight = entry[i].weight + 1; |
| 1225 | list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); |
| 1226 | nhg->nh_entries[i].nh_parent = nh; |
| 1227 | } |
| 1228 | |
| 1229 | if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { |
| 1230 | nhg->mpath = 1; |
| 1231 | nh_group_rebalance(nhg); |
| 1232 | } |
| 1233 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1234 | if (cfg->nh_fdb) |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 1235 | nhg->fdb_nh = 1; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1236 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1237 | rcu_assign_pointer(nh->nh_grp, nhg); |
| 1238 | |
| 1239 | return nh; |
| 1240 | |
| 1241 | out_no_nh: |
| 1242 | for (; i >= 0; --i) |
| 1243 | nexthop_put(nhg->nh_entries[i].nh); |
| 1244 | |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 1245 | kfree(nhg->spare); |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1246 | kfree(nhg); |
| 1247 | kfree(nh); |
| 1248 | |
| 1249 | return ERR_PTR(-ENOENT); |
| 1250 | } |
| 1251 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1252 | static int nh_create_ipv4(struct net *net, struct nexthop *nh, |
| 1253 | struct nh_info *nhi, struct nh_config *cfg, |
| 1254 | struct netlink_ext_ack *extack) |
| 1255 | { |
| 1256 | struct fib_nh *fib_nh = &nhi->fib_nh; |
| 1257 | struct fib_config fib_cfg = { |
| 1258 | .fc_oif = cfg->nh_ifindex, |
| 1259 | .fc_gw4 = cfg->gw.ipv4, |
| 1260 | .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, |
| 1261 | .fc_flags = cfg->nh_flags, |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 1262 | .fc_encap = cfg->nh_encap, |
| 1263 | .fc_encap_type = cfg->nh_encap_type, |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1264 | }; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1265 | u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); |
Colin Ian King | c76c992 | 2019-08-22 13:53:40 +0100 | [diff] [blame] | 1266 | int err; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1267 | |
| 1268 | err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); |
| 1269 | if (err) { |
| 1270 | fib_nh_release(net, fib_nh); |
| 1271 | goto out; |
| 1272 | } |
| 1273 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 1274 | if (nhi->fdb_nh) |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1275 | goto out; |
| 1276 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1277 | /* sets nh_dev if successful */ |
| 1278 | err = fib_check_nh(net, fib_nh, tb_id, 0, extack); |
| 1279 | if (!err) { |
| 1280 | nh->nh_flags = fib_nh->fib_nh_flags; |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1281 | fib_info_update_nhc_saddr(net, &fib_nh->nh_common, |
| 1282 | fib_nh->fib_nh_scope); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1283 | } else { |
| 1284 | fib_nh_release(net, fib_nh); |
| 1285 | } |
| 1286 | out: |
| 1287 | return err; |
| 1288 | } |
| 1289 | |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1290 | static int nh_create_ipv6(struct net *net, struct nexthop *nh, |
| 1291 | struct nh_info *nhi, struct nh_config *cfg, |
| 1292 | struct netlink_ext_ack *extack) |
| 1293 | { |
| 1294 | struct fib6_nh *fib6_nh = &nhi->fib6_nh; |
| 1295 | struct fib6_config fib6_cfg = { |
| 1296 | .fc_table = l3mdev_fib_table(cfg->dev), |
| 1297 | .fc_ifindex = cfg->nh_ifindex, |
| 1298 | .fc_gateway = cfg->gw.ipv6, |
| 1299 | .fc_flags = cfg->nh_flags, |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 1300 | .fc_encap = cfg->nh_encap, |
| 1301 | .fc_encap_type = cfg->nh_encap_type, |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1302 | .fc_is_fdb = cfg->nh_fdb, |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1303 | }; |
Colin Ian King | 6f43e52 | 2019-05-30 16:57:54 +0100 | [diff] [blame] | 1304 | int err; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1305 | |
| 1306 | if (!ipv6_addr_any(&cfg->gw.ipv6)) |
| 1307 | fib6_cfg.fc_flags |= RTF_GATEWAY; |
| 1308 | |
| 1309 | /* sets nh_dev if successful */ |
| 1310 | err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, |
| 1311 | extack); |
| 1312 | if (err) |
| 1313 | ipv6_stub->fib6_nh_release(fib6_nh); |
| 1314 | else |
| 1315 | nh->nh_flags = fib6_nh->fib_nh_flags; |
| 1316 | |
| 1317 | return err; |
| 1318 | } |
| 1319 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1320 | static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, |
| 1321 | struct netlink_ext_ack *extack) |
| 1322 | { |
| 1323 | struct nh_info *nhi; |
| 1324 | struct nexthop *nh; |
| 1325 | int err = 0; |
| 1326 | |
| 1327 | nh = nexthop_alloc(); |
| 1328 | if (!nh) |
| 1329 | return ERR_PTR(-ENOMEM); |
| 1330 | |
| 1331 | nhi = kzalloc(sizeof(*nhi), GFP_KERNEL); |
| 1332 | if (!nhi) { |
| 1333 | kfree(nh); |
| 1334 | return ERR_PTR(-ENOMEM); |
| 1335 | } |
| 1336 | |
| 1337 | nh->nh_flags = cfg->nh_flags; |
| 1338 | nh->net = net; |
| 1339 | |
| 1340 | nhi->nh_parent = nh; |
| 1341 | nhi->family = cfg->nh_family; |
| 1342 | nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; |
| 1343 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1344 | if (cfg->nh_fdb) |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 1345 | nhi->fdb_nh = 1; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1346 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1347 | if (cfg->nh_blackhole) { |
| 1348 | nhi->reject_nh = 1; |
| 1349 | cfg->nh_ifindex = net->loopback_dev->ifindex; |
| 1350 | } |
| 1351 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1352 | switch (cfg->nh_family) { |
| 1353 | case AF_INET: |
| 1354 | err = nh_create_ipv4(net, nh, nhi, cfg, extack); |
| 1355 | break; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1356 | case AF_INET6: |
| 1357 | err = nh_create_ipv6(net, nh, nhi, cfg, extack); |
| 1358 | break; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1361 | if (err) { |
| 1362 | kfree(nhi); |
| 1363 | kfree(nh); |
| 1364 | return ERR_PTR(err); |
| 1365 | } |
| 1366 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1367 | /* add the entry to the device based hash */ |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 1368 | if (!nhi->fdb_nh) |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1369 | nexthop_devhash_add(net, nhi); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1370 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1371 | rcu_assign_pointer(nh->nh_info, nhi); |
| 1372 | |
| 1373 | return nh; |
| 1374 | } |
| 1375 | |
| 1376 | /* called with rtnl lock held */ |
| 1377 | static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, |
| 1378 | struct netlink_ext_ack *extack) |
| 1379 | { |
| 1380 | struct nexthop *nh; |
| 1381 | int err; |
| 1382 | |
| 1383 | if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) { |
| 1384 | NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); |
| 1385 | return ERR_PTR(-EINVAL); |
| 1386 | } |
| 1387 | |
| 1388 | if (!cfg->nh_id) { |
| 1389 | cfg->nh_id = nh_find_unused_id(net); |
| 1390 | if (!cfg->nh_id) { |
| 1391 | NL_SET_ERR_MSG(extack, "No unused id"); |
| 1392 | return ERR_PTR(-EINVAL); |
| 1393 | } |
| 1394 | } |
| 1395 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1396 | if (cfg->nh_grp) |
| 1397 | nh = nexthop_create_group(net, cfg); |
| 1398 | else |
| 1399 | nh = nexthop_create(net, cfg, extack); |
| 1400 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1401 | if (IS_ERR(nh)) |
| 1402 | return nh; |
| 1403 | |
| 1404 | refcount_set(&nh->refcnt, 1); |
| 1405 | nh->id = cfg->nh_id; |
| 1406 | nh->protocol = cfg->nh_protocol; |
| 1407 | nh->net = net; |
| 1408 | |
| 1409 | err = insert_nexthop(net, nh, cfg, extack); |
| 1410 | if (err) { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1411 | __remove_nexthop(net, nh, NULL); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1412 | nexthop_put(nh); |
| 1413 | nh = ERR_PTR(err); |
| 1414 | } |
| 1415 | |
| 1416 | return nh; |
| 1417 | } |
| 1418 | |
| 1419 | static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, |
| 1420 | struct nlmsghdr *nlh, struct nh_config *cfg, |
| 1421 | struct netlink_ext_ack *extack) |
| 1422 | { |
| 1423 | struct nhmsg *nhm = nlmsg_data(nlh); |
| 1424 | struct nlattr *tb[NHA_MAX + 1]; |
| 1425 | int err; |
| 1426 | |
| 1427 | err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, |
| 1428 | extack); |
| 1429 | if (err < 0) |
| 1430 | return err; |
| 1431 | |
| 1432 | err = -EINVAL; |
| 1433 | if (nhm->resvd || nhm->nh_scope) { |
| 1434 | NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); |
| 1435 | goto out; |
| 1436 | } |
| 1437 | if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { |
| 1438 | NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); |
| 1439 | goto out; |
| 1440 | } |
| 1441 | |
| 1442 | switch (nhm->nh_family) { |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1443 | case AF_INET: |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1444 | case AF_INET6: |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1445 | break; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1446 | case AF_UNSPEC: |
| 1447 | if (tb[NHA_GROUP]) |
| 1448 | break; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 1449 | fallthrough; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1450 | default: |
| 1451 | NL_SET_ERR_MSG(extack, "Invalid address family"); |
| 1452 | goto out; |
| 1453 | } |
| 1454 | |
| 1455 | if (tb[NHA_GROUPS] || tb[NHA_MASTER]) { |
| 1456 | NL_SET_ERR_MSG(extack, "Invalid attributes in request"); |
| 1457 | goto out; |
| 1458 | } |
| 1459 | |
| 1460 | memset(cfg, 0, sizeof(*cfg)); |
| 1461 | cfg->nlflags = nlh->nlmsg_flags; |
| 1462 | cfg->nlinfo.portid = NETLINK_CB(skb).portid; |
| 1463 | cfg->nlinfo.nlh = nlh; |
| 1464 | cfg->nlinfo.nl_net = net; |
| 1465 | |
| 1466 | cfg->nh_family = nhm->nh_family; |
| 1467 | cfg->nh_protocol = nhm->nh_protocol; |
| 1468 | cfg->nh_flags = nhm->nh_flags; |
| 1469 | |
| 1470 | if (tb[NHA_ID]) |
| 1471 | cfg->nh_id = nla_get_u32(tb[NHA_ID]); |
| 1472 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1473 | if (tb[NHA_FDB]) { |
| 1474 | if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || |
| 1475 | tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { |
| 1476 | NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); |
| 1477 | goto out; |
| 1478 | } |
| 1479 | if (nhm->nh_flags) { |
| 1480 | NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); |
| 1481 | goto out; |
| 1482 | } |
| 1483 | cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); |
| 1484 | } |
| 1485 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1486 | if (tb[NHA_GROUP]) { |
| 1487 | if (nhm->nh_family != AF_UNSPEC) { |
| 1488 | NL_SET_ERR_MSG(extack, "Invalid family for group"); |
| 1489 | goto out; |
| 1490 | } |
| 1491 | cfg->nh_grp = tb[NHA_GROUP]; |
| 1492 | |
| 1493 | cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; |
| 1494 | if (tb[NHA_GROUP_TYPE]) |
| 1495 | cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); |
| 1496 | |
| 1497 | if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { |
| 1498 | NL_SET_ERR_MSG(extack, "Invalid group type"); |
| 1499 | goto out; |
| 1500 | } |
| 1501 | err = nh_check_attr_group(net, tb, extack); |
| 1502 | |
| 1503 | /* no other attributes should be set */ |
| 1504 | goto out; |
| 1505 | } |
| 1506 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1507 | if (tb[NHA_BLACKHOLE]) { |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 1508 | if (tb[NHA_GATEWAY] || tb[NHA_OIF] || |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1509 | tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { |
| 1510 | NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1511 | goto out; |
| 1512 | } |
| 1513 | |
| 1514 | cfg->nh_blackhole = 1; |
| 1515 | err = 0; |
| 1516 | goto out; |
| 1517 | } |
| 1518 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1519 | if (!cfg->nh_fdb && !tb[NHA_OIF]) { |
| 1520 | NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1521 | goto out; |
| 1522 | } |
| 1523 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1524 | if (!cfg->nh_fdb && tb[NHA_OIF]) { |
| 1525 | cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); |
| 1526 | if (cfg->nh_ifindex) |
| 1527 | cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1528 | |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1529 | if (!cfg->dev) { |
| 1530 | NL_SET_ERR_MSG(extack, "Invalid device index"); |
| 1531 | goto out; |
| 1532 | } else if (!(cfg->dev->flags & IFF_UP)) { |
| 1533 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 1534 | err = -ENETDOWN; |
| 1535 | goto out; |
| 1536 | } else if (!netif_carrier_ok(cfg->dev)) { |
| 1537 | NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); |
| 1538 | err = -ENETDOWN; |
| 1539 | goto out; |
| 1540 | } |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1543 | err = -EINVAL; |
| 1544 | if (tb[NHA_GATEWAY]) { |
| 1545 | struct nlattr *gwa = tb[NHA_GATEWAY]; |
| 1546 | |
| 1547 | switch (cfg->nh_family) { |
| 1548 | case AF_INET: |
| 1549 | if (nla_len(gwa) != sizeof(u32)) { |
| 1550 | NL_SET_ERR_MSG(extack, "Invalid gateway"); |
| 1551 | goto out; |
| 1552 | } |
| 1553 | cfg->gw.ipv4 = nla_get_be32(gwa); |
| 1554 | break; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 1555 | case AF_INET6: |
| 1556 | if (nla_len(gwa) != sizeof(struct in6_addr)) { |
| 1557 | NL_SET_ERR_MSG(extack, "Invalid gateway"); |
| 1558 | goto out; |
| 1559 | } |
| 1560 | cfg->gw.ipv6 = nla_get_in6_addr(gwa); |
| 1561 | break; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1562 | default: |
| 1563 | NL_SET_ERR_MSG(extack, |
| 1564 | "Unknown address family for gateway"); |
| 1565 | goto out; |
| 1566 | } |
| 1567 | } else { |
| 1568 | /* device only nexthop (no gateway) */ |
| 1569 | if (cfg->nh_flags & RTNH_F_ONLINK) { |
| 1570 | NL_SET_ERR_MSG(extack, |
| 1571 | "ONLINK flag can not be set for nexthop without a gateway"); |
| 1572 | goto out; |
| 1573 | } |
| 1574 | } |
| 1575 | |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 1576 | if (tb[NHA_ENCAP]) { |
| 1577 | cfg->nh_encap = tb[NHA_ENCAP]; |
| 1578 | |
| 1579 | if (!tb[NHA_ENCAP_TYPE]) { |
| 1580 | NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); |
| 1581 | goto out; |
| 1582 | } |
| 1583 | |
| 1584 | cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); |
| 1585 | err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); |
| 1586 | if (err < 0) |
| 1587 | goto out; |
| 1588 | |
| 1589 | } else if (tb[NHA_ENCAP_TYPE]) { |
| 1590 | NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); |
| 1591 | goto out; |
| 1592 | } |
| 1593 | |
| 1594 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1595 | err = 0; |
| 1596 | out: |
| 1597 | return err; |
| 1598 | } |
| 1599 | |
| 1600 | /* rtnl */ |
| 1601 | static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1602 | struct netlink_ext_ack *extack) |
| 1603 | { |
| 1604 | struct net *net = sock_net(skb->sk); |
| 1605 | struct nh_config cfg; |
| 1606 | struct nexthop *nh; |
| 1607 | int err; |
| 1608 | |
| 1609 | err = rtm_to_nh_config(net, skb, nlh, &cfg, extack); |
| 1610 | if (!err) { |
| 1611 | nh = nexthop_add(net, &cfg, extack); |
| 1612 | if (IS_ERR(nh)) |
| 1613 | err = PTR_ERR(nh); |
| 1614 | } |
| 1615 | |
| 1616 | return err; |
| 1617 | } |
| 1618 | |
| 1619 | static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id, |
| 1620 | struct netlink_ext_ack *extack) |
| 1621 | { |
| 1622 | struct nhmsg *nhm = nlmsg_data(nlh); |
| 1623 | struct nlattr *tb[NHA_MAX + 1]; |
| 1624 | int err, i; |
| 1625 | |
| 1626 | err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, |
| 1627 | extack); |
| 1628 | if (err < 0) |
| 1629 | return err; |
| 1630 | |
| 1631 | err = -EINVAL; |
| 1632 | for (i = 0; i < __NHA_MAX; ++i) { |
| 1633 | if (!tb[i]) |
| 1634 | continue; |
| 1635 | |
| 1636 | switch (i) { |
| 1637 | case NHA_ID: |
| 1638 | break; |
| 1639 | default: |
| 1640 | NL_SET_ERR_MSG_ATTR(extack, tb[i], |
| 1641 | "Unexpected attribute in request"); |
| 1642 | goto out; |
| 1643 | } |
| 1644 | } |
| 1645 | if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { |
| 1646 | NL_SET_ERR_MSG(extack, "Invalid values in header"); |
| 1647 | goto out; |
| 1648 | } |
| 1649 | |
| 1650 | if (!tb[NHA_ID]) { |
| 1651 | NL_SET_ERR_MSG(extack, "Nexthop id is missing"); |
| 1652 | goto out; |
| 1653 | } |
| 1654 | |
| 1655 | *id = nla_get_u32(tb[NHA_ID]); |
| 1656 | if (!(*id)) |
| 1657 | NL_SET_ERR_MSG(extack, "Invalid nexthop id"); |
| 1658 | else |
| 1659 | err = 0; |
| 1660 | out: |
| 1661 | return err; |
| 1662 | } |
| 1663 | |
| 1664 | /* rtnl */ |
| 1665 | static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1666 | struct netlink_ext_ack *extack) |
| 1667 | { |
| 1668 | struct net *net = sock_net(skb->sk); |
| 1669 | struct nl_info nlinfo = { |
| 1670 | .nlh = nlh, |
| 1671 | .nl_net = net, |
| 1672 | .portid = NETLINK_CB(skb).portid, |
| 1673 | }; |
| 1674 | struct nexthop *nh; |
| 1675 | int err; |
| 1676 | u32 id; |
| 1677 | |
| 1678 | err = nh_valid_get_del_req(nlh, &id, extack); |
| 1679 | if (err) |
| 1680 | return err; |
| 1681 | |
| 1682 | nh = nexthop_find_by_id(net, id); |
| 1683 | if (!nh) |
| 1684 | return -ENOENT; |
| 1685 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1686 | remove_nexthop(net, nh, &nlinfo); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* rtnl */ |
| 1692 | static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 1693 | struct netlink_ext_ack *extack) |
| 1694 | { |
| 1695 | struct net *net = sock_net(in_skb->sk); |
| 1696 | struct sk_buff *skb = NULL; |
| 1697 | struct nexthop *nh; |
| 1698 | int err; |
| 1699 | u32 id; |
| 1700 | |
| 1701 | err = nh_valid_get_del_req(nlh, &id, extack); |
| 1702 | if (err) |
| 1703 | return err; |
| 1704 | |
| 1705 | err = -ENOBUFS; |
| 1706 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1707 | if (!skb) |
| 1708 | goto out; |
| 1709 | |
| 1710 | err = -ENOENT; |
| 1711 | nh = nexthop_find_by_id(net, id); |
| 1712 | if (!nh) |
| 1713 | goto errout_free; |
| 1714 | |
| 1715 | err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, |
| 1716 | nlh->nlmsg_seq, 0); |
| 1717 | if (err < 0) { |
| 1718 | WARN_ON(err == -EMSGSIZE); |
| 1719 | goto errout_free; |
| 1720 | } |
| 1721 | |
| 1722 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 1723 | out: |
| 1724 | return err; |
| 1725 | errout_free: |
| 1726 | kfree_skb(skb); |
| 1727 | goto out; |
| 1728 | } |
| 1729 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1730 | static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx, |
| 1731 | bool group_filter, u8 family) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1732 | { |
| 1733 | const struct net_device *dev; |
| 1734 | const struct nh_info *nhi; |
| 1735 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1736 | if (group_filter && !nh->is_group) |
| 1737 | return true; |
| 1738 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1739 | if (!dev_idx && !master_idx && !family) |
| 1740 | return false; |
| 1741 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1742 | if (nh->is_group) |
| 1743 | return true; |
| 1744 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1745 | nhi = rtnl_dereference(nh->nh_info); |
| 1746 | if (family && nhi->family != family) |
| 1747 | return true; |
| 1748 | |
| 1749 | dev = nhi->fib_nhc.nhc_dev; |
| 1750 | if (dev_idx && (!dev || dev->ifindex != dev_idx)) |
| 1751 | return true; |
| 1752 | |
| 1753 | if (master_idx) { |
| 1754 | struct net_device *master; |
| 1755 | |
| 1756 | if (!dev) |
| 1757 | return true; |
| 1758 | |
| 1759 | master = netdev_master_upper_dev_get((struct net_device *)dev); |
| 1760 | if (!master || master->ifindex != master_idx) |
| 1761 | return true; |
| 1762 | } |
| 1763 | |
| 1764 | return false; |
| 1765 | } |
| 1766 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1767 | static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx, |
| 1768 | int *master_idx, bool *group_filter, |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1769 | bool *fdb_filter, struct netlink_callback *cb) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1770 | { |
| 1771 | struct netlink_ext_ack *extack = cb->extack; |
| 1772 | struct nlattr *tb[NHA_MAX + 1]; |
| 1773 | struct nhmsg *nhm; |
| 1774 | int err, i; |
| 1775 | u32 idx; |
| 1776 | |
| 1777 | err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, |
| 1778 | NULL); |
| 1779 | if (err < 0) |
| 1780 | return err; |
| 1781 | |
| 1782 | for (i = 0; i <= NHA_MAX; ++i) { |
| 1783 | if (!tb[i]) |
| 1784 | continue; |
| 1785 | |
| 1786 | switch (i) { |
| 1787 | case NHA_OIF: |
| 1788 | idx = nla_get_u32(tb[i]); |
| 1789 | if (idx > INT_MAX) { |
| 1790 | NL_SET_ERR_MSG(extack, "Invalid device index"); |
| 1791 | return -EINVAL; |
| 1792 | } |
| 1793 | *dev_idx = idx; |
| 1794 | break; |
| 1795 | case NHA_MASTER: |
| 1796 | idx = nla_get_u32(tb[i]); |
| 1797 | if (idx > INT_MAX) { |
| 1798 | NL_SET_ERR_MSG(extack, "Invalid master device index"); |
| 1799 | return -EINVAL; |
| 1800 | } |
| 1801 | *master_idx = idx; |
| 1802 | break; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1803 | case NHA_GROUPS: |
| 1804 | *group_filter = true; |
| 1805 | break; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1806 | case NHA_FDB: |
| 1807 | *fdb_filter = true; |
| 1808 | break; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1809 | default: |
| 1810 | NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); |
| 1811 | return -EINVAL; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | nhm = nlmsg_data(nlh); |
| 1816 | if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { |
| 1817 | NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); |
| 1818 | return -EINVAL; |
| 1819 | } |
| 1820 | |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | /* rtnl */ |
| 1825 | static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) |
| 1826 | { |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1827 | bool group_filter = false, fdb_filter = false; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1828 | struct nhmsg *nhm = nlmsg_data(cb->nlh); |
| 1829 | int dev_filter_idx = 0, master_idx = 0; |
| 1830 | struct net *net = sock_net(skb->sk); |
| 1831 | struct rb_root *root = &net->nexthop.rb_root; |
| 1832 | struct rb_node *node; |
| 1833 | int idx = 0, s_idx; |
| 1834 | int err; |
| 1835 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1836 | err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx, |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 1837 | &group_filter, &fdb_filter, cb); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1838 | if (err < 0) |
| 1839 | return err; |
| 1840 | |
| 1841 | s_idx = cb->args[0]; |
| 1842 | for (node = rb_first(root); node; node = rb_next(node)) { |
| 1843 | struct nexthop *nh; |
| 1844 | |
| 1845 | if (idx < s_idx) |
| 1846 | goto cont; |
| 1847 | |
| 1848 | nh = rb_entry(node, struct nexthop, rb_node); |
| 1849 | if (nh_dump_filtered(nh, dev_filter_idx, master_idx, |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 1850 | group_filter, nhm->nh_family)) |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1851 | goto cont; |
| 1852 | |
| 1853 | err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, |
| 1854 | NETLINK_CB(cb->skb).portid, |
| 1855 | cb->nlh->nlmsg_seq, NLM_F_MULTI); |
| 1856 | if (err < 0) { |
| 1857 | if (likely(skb->len)) |
| 1858 | goto out; |
| 1859 | |
| 1860 | goto out_err; |
| 1861 | } |
| 1862 | cont: |
| 1863 | idx++; |
| 1864 | } |
| 1865 | |
| 1866 | out: |
| 1867 | err = skb->len; |
| 1868 | out_err: |
| 1869 | cb->args[0] = idx; |
| 1870 | cb->seq = net->nexthop.seq; |
| 1871 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| 1872 | |
| 1873 | return err; |
| 1874 | } |
| 1875 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1876 | static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) |
| 1877 | { |
| 1878 | unsigned int hash = nh_dev_hashfn(dev->ifindex); |
| 1879 | struct net *net = dev_net(dev); |
| 1880 | struct hlist_head *head = &net->nexthop.devhash[hash]; |
| 1881 | struct hlist_node *n; |
| 1882 | struct nh_info *nhi; |
| 1883 | |
| 1884 | hlist_for_each_entry_safe(nhi, n, head, dev_hash) { |
| 1885 | if (nhi->fib_nhc.nhc_dev == dev) { |
| 1886 | if (nhi->family == AF_INET) |
| 1887 | fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, |
| 1888 | orig_mtu); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* rtnl */ |
| 1894 | static int nh_netdev_event(struct notifier_block *this, |
| 1895 | unsigned long event, void *ptr) |
| 1896 | { |
| 1897 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1898 | struct netdev_notifier_info_ext *info_ext; |
| 1899 | |
| 1900 | switch (event) { |
| 1901 | case NETDEV_DOWN: |
| 1902 | case NETDEV_UNREGISTER: |
| 1903 | nexthop_flush_dev(dev); |
| 1904 | break; |
| 1905 | case NETDEV_CHANGE: |
| 1906 | if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) |
| 1907 | nexthop_flush_dev(dev); |
| 1908 | break; |
| 1909 | case NETDEV_CHANGEMTU: |
| 1910 | info_ext = ptr; |
| 1911 | nexthop_sync_mtu(dev, info_ext->ext.mtu); |
| 1912 | rt_cache_flush(dev_net(dev)); |
| 1913 | break; |
| 1914 | } |
| 1915 | return NOTIFY_DONE; |
| 1916 | } |
| 1917 | |
| 1918 | static struct notifier_block nh_netdev_notifier = { |
| 1919 | .notifier_call = nh_netdev_event, |
| 1920 | }; |
| 1921 | |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 1922 | int register_nexthop_notifier(struct net *net, struct notifier_block *nb) |
| 1923 | { |
| 1924 | return atomic_notifier_chain_register(&net->nexthop.notifier_chain, nb); |
| 1925 | } |
| 1926 | EXPORT_SYMBOL(register_nexthop_notifier); |
| 1927 | |
| 1928 | int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) |
| 1929 | { |
| 1930 | return atomic_notifier_chain_unregister(&net->nexthop.notifier_chain, |
| 1931 | nb); |
| 1932 | } |
| 1933 | EXPORT_SYMBOL(unregister_nexthop_notifier); |
| 1934 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1935 | static void __net_exit nexthop_net_exit(struct net *net) |
| 1936 | { |
| 1937 | rtnl_lock(); |
| 1938 | flush_all_nexthops(net); |
| 1939 | rtnl_unlock(); |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1940 | kfree(net->nexthop.devhash); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | static int __net_init nexthop_net_init(struct net *net) |
| 1944 | { |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1945 | size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; |
| 1946 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1947 | net->nexthop.rb_root = RB_ROOT; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1948 | net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); |
| 1949 | if (!net->nexthop.devhash) |
| 1950 | return -ENOMEM; |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 1951 | ATOMIC_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1952 | |
| 1953 | return 0; |
| 1954 | } |
| 1955 | |
| 1956 | static struct pernet_operations nexthop_net_ops = { |
| 1957 | .init = nexthop_net_init, |
| 1958 | .exit = nexthop_net_exit, |
| 1959 | }; |
| 1960 | |
| 1961 | static int __init nexthop_init(void) |
| 1962 | { |
| 1963 | register_pernet_subsys(&nexthop_net_ops); |
| 1964 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 1965 | register_netdevice_notifier(&nh_netdev_notifier); |
| 1966 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1967 | rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); |
| 1968 | rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0); |
| 1969 | rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop, |
| 1970 | rtm_dump_nexthop, 0); |
| 1971 | |
| 1972 | rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); |
| 1973 | rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); |
| 1974 | |
| 1975 | rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); |
| 1976 | rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | subsys_initcall(nexthop_init); |