David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Generic nexthop implementation |
| 4 | * |
| 5 | * Copyright (c) 2017-19 Cumulus Networks |
| 6 | * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> |
| 7 | */ |
| 8 | |
| 9 | #ifndef __LINUX_NEXTHOP_H |
| 10 | #define __LINUX_NEXTHOP_H |
| 11 | |
| 12 | #include <linux/netdevice.h> |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 13 | #include <linux/route.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <net/ip_fib.h> |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 16 | #include <net/ip6_fib.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 17 | #include <net/netlink.h> |
| 18 | |
| 19 | #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK |
| 20 | |
| 21 | struct nexthop; |
| 22 | |
| 23 | struct nh_config { |
| 24 | u32 nh_id; |
| 25 | |
| 26 | u8 nh_family; |
| 27 | u8 nh_protocol; |
| 28 | u8 nh_blackhole; |
| 29 | u32 nh_flags; |
| 30 | |
| 31 | int nh_ifindex; |
| 32 | struct net_device *dev; |
| 33 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 34 | union { |
| 35 | __be32 ipv4; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 36 | struct in6_addr ipv6; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 37 | } gw; |
| 38 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 39 | struct nlattr *nh_grp; |
| 40 | u16 nh_grp_type; |
| 41 | |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 42 | struct nlattr *nh_encap; |
| 43 | u16 nh_encap_type; |
| 44 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 45 | u32 nlflags; |
| 46 | struct nl_info nlinfo; |
| 47 | }; |
| 48 | |
| 49 | struct nh_info { |
| 50 | struct hlist_node dev_hash; /* entry on netns devhash */ |
| 51 | struct nexthop *nh_parent; |
| 52 | |
| 53 | u8 family; |
| 54 | bool reject_nh; |
| 55 | |
| 56 | union { |
| 57 | struct fib_nh_common fib_nhc; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 58 | struct fib_nh fib_nh; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 59 | struct fib6_nh fib6_nh; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 60 | }; |
| 61 | }; |
| 62 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 63 | struct nh_grp_entry { |
| 64 | struct nexthop *nh; |
| 65 | u8 weight; |
| 66 | atomic_t upper_bound; |
| 67 | |
| 68 | struct list_head nh_list; |
| 69 | struct nexthop *nh_parent; /* nexthop of group with this entry */ |
| 70 | }; |
| 71 | |
| 72 | struct nh_group { |
| 73 | u16 num_nh; |
| 74 | bool mpath; |
| 75 | bool has_v4; |
Gustavo A. R. Silva | 97a888c | 2020-02-28 18:14:11 -0600 | [diff] [blame] | 76 | struct nh_grp_entry nh_entries[]; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 79 | struct nexthop { |
| 80 | struct rb_node rb_node; /* entry on netns rbtree */ |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 81 | struct list_head fi_list; /* v4 entries using nh */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 82 | struct list_head f6i_list; /* v6 entries using nh */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 83 | struct list_head grp_list; /* nh group entries using this nh */ |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 84 | struct net *net; |
| 85 | |
| 86 | u32 id; |
| 87 | |
| 88 | u8 protocol; /* app managing this nh */ |
| 89 | u8 nh_flags; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 90 | bool is_group; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 91 | |
| 92 | refcount_t refcnt; |
| 93 | struct rcu_head rcu; |
| 94 | |
| 95 | union { |
| 96 | struct nh_info __rcu *nh_info; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 97 | struct nh_group __rcu *nh_grp; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 98 | }; |
| 99 | }; |
| 100 | |
| 101 | /* caller is holding rcu or rtnl; no reference taken to nexthop */ |
| 102 | struct nexthop *nexthop_find_by_id(struct net *net, u32 id); |
| 103 | void nexthop_free_rcu(struct rcu_head *head); |
| 104 | |
| 105 | static inline bool nexthop_get(struct nexthop *nh) |
| 106 | { |
| 107 | return refcount_inc_not_zero(&nh->refcnt); |
| 108 | } |
| 109 | |
| 110 | static inline void nexthop_put(struct nexthop *nh) |
| 111 | { |
| 112 | if (refcount_dec_and_test(&nh->refcnt)) |
| 113 | call_rcu(&nh->rcu, nexthop_free_rcu); |
| 114 | } |
| 115 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 116 | static inline bool nexthop_cmp(const struct nexthop *nh1, |
| 117 | const struct nexthop *nh2) |
| 118 | { |
| 119 | return nh1 == nh2; |
| 120 | } |
| 121 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 122 | static inline bool nexthop_is_multipath(const struct nexthop *nh) |
| 123 | { |
| 124 | if (nh->is_group) { |
| 125 | struct nh_group *nh_grp; |
| 126 | |
| 127 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 128 | return nh_grp->mpath; |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | struct nexthop *nexthop_select_path(struct nexthop *nh, int hash); |
| 134 | |
| 135 | static inline unsigned int nexthop_num_path(const struct nexthop *nh) |
| 136 | { |
| 137 | unsigned int rc = 1; |
| 138 | |
| 139 | if (nexthop_is_multipath(nh)) { |
| 140 | struct nh_group *nh_grp; |
| 141 | |
| 142 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 143 | rc = nh_grp->num_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | static inline |
| 150 | struct nexthop *nexthop_mpath_select(const struct nexthop *nh, int nhsel) |
| 151 | { |
| 152 | const struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); |
| 153 | |
| 154 | /* for_nexthops macros in fib_semantics.c grabs a pointer to |
| 155 | * the nexthop before checking nhsel |
| 156 | */ |
Dan Carpenter | 5270041 | 2019-06-07 18:31:07 +0300 | [diff] [blame] | 157 | if (nhsel >= nhg->num_nh) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 158 | return NULL; |
| 159 | |
| 160 | return nhg->nh_entries[nhsel].nh; |
| 161 | } |
| 162 | |
| 163 | static inline |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 164 | int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh, |
| 165 | u8 rt_family) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 166 | { |
| 167 | struct nh_group *nhg = rtnl_dereference(nh->nh_grp); |
| 168 | int i; |
| 169 | |
| 170 | for (i = 0; i < nhg->num_nh; i++) { |
| 171 | struct nexthop *nhe = nhg->nh_entries[i].nh; |
| 172 | struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info); |
| 173 | struct fib_nh_common *nhc = &nhi->fib_nhc; |
| 174 | int weight = nhg->nh_entries[i].weight; |
| 175 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 176 | if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 177 | return -EMSGSIZE; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 183 | /* called with rcu lock */ |
| 184 | static inline bool nexthop_is_blackhole(const struct nexthop *nh) |
| 185 | { |
| 186 | const struct nh_info *nhi; |
| 187 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 188 | if (nexthop_is_multipath(nh)) { |
| 189 | if (nexthop_num_path(nh) > 1) |
| 190 | return false; |
| 191 | nh = nexthop_mpath_select(nh, 0); |
| 192 | if (!nh) |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | nhi = rcu_dereference_rtnl(nh->nh_info); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 197 | return nhi->reject_nh; |
| 198 | } |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 199 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 200 | static inline void nexthop_path_fib_result(struct fib_result *res, int hash) |
| 201 | { |
| 202 | struct nh_info *nhi; |
| 203 | struct nexthop *nh; |
| 204 | |
| 205 | nh = nexthop_select_path(res->fi->nh, hash); |
| 206 | nhi = rcu_dereference(nh->nh_info); |
| 207 | res->nhc = &nhi->fib_nhc; |
| 208 | } |
| 209 | |
| 210 | /* called with rcu read lock or rtnl held */ |
| 211 | static inline |
| 212 | struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel) |
| 213 | { |
| 214 | struct nh_info *nhi; |
| 215 | |
| 216 | BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0); |
| 217 | BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0); |
| 218 | |
| 219 | if (nexthop_is_multipath(nh)) { |
| 220 | nh = nexthop_mpath_select(nh, nhsel); |
| 221 | if (!nh) |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 226 | return &nhi->fib_nhc; |
| 227 | } |
| 228 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 229 | static inline unsigned int fib_info_num_path(const struct fib_info *fi) |
| 230 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 231 | if (unlikely(fi->nh)) |
| 232 | return nexthop_num_path(fi->nh); |
| 233 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 234 | return fi->fib_nhs; |
| 235 | } |
| 236 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 237 | int fib_check_nexthop(struct nexthop *nh, u8 scope, |
| 238 | struct netlink_ext_ack *extack); |
| 239 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 240 | static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel) |
| 241 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 242 | if (unlikely(fi->nh)) |
| 243 | return nexthop_fib_nhc(fi->nh, nhsel); |
| 244 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 245 | return &fi->fib_nh[nhsel].nh_common; |
| 246 | } |
| 247 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 248 | /* only used when fib_nh is built into fib_info */ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 249 | static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel) |
| 250 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 251 | WARN_ON(fi->nh); |
| 252 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 253 | return &fi->fib_nh[nhsel]; |
| 254 | } |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * IPv6 variants |
| 258 | */ |
| 259 | int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, |
| 260 | struct netlink_ext_ack *extack); |
| 261 | |
| 262 | static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh) |
| 263 | { |
| 264 | struct nh_info *nhi; |
| 265 | |
| 266 | if (nexthop_is_multipath(nh)) { |
| 267 | nh = nexthop_mpath_select(nh, 0); |
| 268 | if (!nh) |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 273 | if (nhi->family == AF_INET6) |
| 274 | return &nhi->fib6_nh; |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i) |
| 280 | { |
| 281 | struct fib6_nh *fib6_nh; |
| 282 | |
| 283 | fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh; |
| 284 | return fib6_nh->fib_nh_dev; |
| 285 | } |
| 286 | |
| 287 | static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash) |
| 288 | { |
| 289 | struct nexthop *nh = res->f6i->nh; |
| 290 | struct nh_info *nhi; |
| 291 | |
| 292 | nh = nexthop_select_path(nh, hash); |
| 293 | |
| 294 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 295 | if (nhi->reject_nh) { |
| 296 | res->fib6_type = RTN_BLACKHOLE; |
| 297 | res->fib6_flags |= RTF_REJECT; |
| 298 | res->nh = nexthop_fib6_nh(nh); |
| 299 | } else { |
| 300 | res->nh = &nhi->fib6_nh; |
| 301 | } |
| 302 | } |
David Ahern | f88c9aa | 2019-06-08 14:53:22 -0700 | [diff] [blame] | 303 | |
| 304 | int nexthop_for_each_fib6_nh(struct nexthop *nh, |
| 305 | int (*cb)(struct fib6_nh *nh, void *arg), |
| 306 | void *arg); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 307 | #endif |