| 1 | |
| 2 | |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX
|
| 4 | * operating system. INET is implemented using the BSD Socket
|
| 5 | * interface as the means of communication with the user level.
|
| 6 | *
|
| 7 | * IPv4 Forwarding Information Base: semantics.
|
| 8 | *
|
| 9 | * Authors: Alexey Kuznetsov, <[email protected]>
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/uaccess.h>
|
| 13 | #include <linux/bitops.h>
|
| 14 | #include <linux/types.h>
|
| 15 | #include <linux/kernel.h>
|
| 16 | #include <linux/jiffies.h>
|
| 17 | #include <linux/mm.h>
|
| 18 | #include <linux/string.h>
|
| 19 | #include <linux/socket.h>
|
| 20 | #include <linux/sockios.h>
|
| 21 | #include <linux/errno.h>
|
| 22 | #include <linux/in.h>
|
| 23 | #include <linux/inet.h>
|
| 24 | #include <linux/inetdevice.h>
|
| 25 | #include <linux/netdevice.h>
|
| 26 | #include <linux/if_arp.h>
|
| 27 | #include <linux/proc_fs.h>
|
| 28 | #include <linux/skbuff.h>
|
| 29 | #include <linux/init.h>
|
| 30 | #include <linux/slab.h>
|
| 31 | #include <linux/netlink.h>
|
| 32 | #include <linux/hash.h>
|
| 33 | #include <linux/nospec.h>
|
| 34 |
|
| 35 | #include <net/arp.h>
|
| 36 | #include <net/inet_dscp.h>
|
| 37 | #include <net/ip.h>
|
| 38 | #include <net/protocol.h>
|
| 39 | #include <net/route.h>
|
| 40 | #include <net/tcp.h>
|
| 41 | #include <net/sock.h>
|
| 42 | #include <net/ip_fib.h>
|
| 43 | #include <net/ip6_fib.h>
|
| 44 | #include <net/nexthop.h>
|
| 45 | #include <net/netlink.h>
|
| 46 | #include <net/rtnh.h>
|
| 47 | #include <net/lwtunnel.h>
|
| 48 | #include <net/fib_notifier.h>
|
| 49 | #include <net/addrconf.h>
|
| 50 |
|
| 51 | #include "fib_lookup.h"
|
| 52 |
|
| 53 | |
| 54 | * is not set in a fib_info. The logic within can reference fib_nh.
|
| 55 |
|
| 56 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 57 |
|
| 58 | #define for_nexthops(fi) { \
|
| 59 | int nhsel; const struct fib_nh *nh; \
|
| 60 | for (nhsel = 0, nh = (fi)->fib_nh; \
|
| 61 | nhsel < fib_info_num_path((fi)); \
|
| 62 | nh++, nhsel++)
|
| 63 |
|
| 64 | #define change_nexthops(fi) { \
|
| 65 | int nhsel; struct fib_nh *nexthop_nh; \
|
| 66 | for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
|
| 67 | nhsel < fib_info_num_path((fi)); \
|
| 68 | nexthop_nh++, nhsel++)
|
| 69 |
|
| 70 | #else
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 | #define for_nexthops(fi) { \
|
| 75 | int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
|
| 76 | for (nhsel = 0; nhsel < 1; nhsel++)
|
| 77 |
|
| 78 | #define change_nexthops(fi) { \
|
| 79 | int nhsel; \
|
| 80 | struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
|
| 81 | for (nhsel = 0; nhsel < 1; nhsel++)
|
| 82 |
|
| 83 | #endif
|
| 84 |
|
| 85 | #define endfor_nexthops(fi) }
|
| 86 |
|
| 87 |
|
| 88 | const struct fib_prop fib_props[RTN_MAX + 1] = {
|
| 89 | [RTN_UNSPEC] = {
|
| 90 | .error = 0,
|
| 91 | .scope = RT_SCOPE_NOWHERE,
|
| 92 | },
|
| 93 | [RTN_UNICAST] = {
|
| 94 | .error = 0,
|
| 95 | .scope = RT_SCOPE_UNIVERSE,
|
| 96 | },
|
| 97 | [RTN_LOCAL] = {
|
| 98 | .error = 0,
|
| 99 | .scope = RT_SCOPE_HOST,
|
| 100 | },
|
| 101 | [RTN_BROADCAST] = {
|
| 102 | .error = 0,
|
| 103 | .scope = RT_SCOPE_LINK,
|
| 104 | },
|
| 105 | [RTN_ANYCAST] = {
|
| 106 | .error = 0,
|
| 107 | .scope = RT_SCOPE_LINK,
|
| 108 | },
|
| 109 | [RTN_MULTICAST] = {
|
| 110 | .error = 0,
|
| 111 | .scope = RT_SCOPE_UNIVERSE,
|
| 112 | },
|
| 113 | [RTN_BLACKHOLE] = {
|
| 114 | .error = -EINVAL,
|
| 115 | .scope = RT_SCOPE_UNIVERSE,
|
| 116 | },
|
| 117 | [RTN_UNREACHABLE] = {
|
| 118 | .error = -EHOSTUNREACH,
|
| 119 | .scope = RT_SCOPE_UNIVERSE,
|
| 120 | },
|
| 121 | [RTN_PROHIBIT] = {
|
| 122 | .error = -EACCES,
|
| 123 | .scope = RT_SCOPE_UNIVERSE,
|
| 124 | },
|
| 125 | [RTN_THROW] = {
|
| 126 | .error = -EAGAIN,
|
| 127 | .scope = RT_SCOPE_UNIVERSE,
|
| 128 | },
|
| 129 | [RTN_NAT] = {
|
| 130 | .error = -EINVAL,
|
| 131 | .scope = RT_SCOPE_NOWHERE,
|
| 132 | },
|
| 133 | [RTN_XRESOLVE] = {
|
| 134 | .error = -EINVAL,
|
| 135 | .scope = RT_SCOPE_NOWHERE,
|
| 136 | },
|
| 137 | };
|
| 138 |
|
| 139 | static void rt_fibinfo_free(struct rtable __rcu **rtp)
|
| 140 | {
|
| 141 | struct rtable *rt = rcu_dereference_protected(*rtp, 1);
|
| 142 |
|
| 143 | if (!rt)
|
| 144 | return;
|
| 145 |
|
| 146 | |
| 147 | * because we waited an RCU grace period before calling
|
| 148 | * free_fib_info_rcu()
|
| 149 |
|
| 150 |
|
| 151 | dst_dev_put(&rt->dst);
|
| 152 | dst_release_immediate(&rt->dst);
|
| 153 | }
|
| 154 |
|
| 155 | static void free_nh_exceptions(struct fib_nh_common *nhc)
|
| 156 | {
|
| 157 | struct fnhe_hash_bucket *hash;
|
| 158 | int i;
|
| 159 |
|
| 160 | hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
|
| 161 | if (!hash)
|
| 162 | return;
|
| 163 | for (i = 0; i < FNHE_HASH_SIZE; i++) {
|
| 164 | struct fib_nh_exception *fnhe;
|
| 165 |
|
| 166 | fnhe = rcu_dereference_protected(hash[i].chain, 1);
|
| 167 | while (fnhe) {
|
| 168 | struct fib_nh_exception *next;
|
| 169 |
|
| 170 | next = rcu_dereference_protected(fnhe->fnhe_next, 1);
|
| 171 |
|
| 172 | rt_fibinfo_free(&fnhe->fnhe_rth_input);
|
| 173 | rt_fibinfo_free(&fnhe->fnhe_rth_output);
|
| 174 |
|
| 175 | kfree(fnhe);
|
| 176 |
|
| 177 | fnhe = next;
|
| 178 | }
|
| 179 | }
|
| 180 | kfree(hash);
|
| 181 | }
|
| 182 |
|
| 183 | static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
|
| 184 | {
|
| 185 | int cpu;
|
| 186 |
|
| 187 | if (!rtp)
|
| 188 | return;
|
| 189 |
|
| 190 | for_each_possible_cpu(cpu) {
|
| 191 | struct rtable *rt;
|
| 192 |
|
| 193 | rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
|
| 194 | if (rt) {
|
| 195 | dst_dev_put(&rt->dst);
|
| 196 | dst_release_immediate(&rt->dst);
|
| 197 | }
|
| 198 | }
|
| 199 | free_percpu(rtp);
|
| 200 | }
|
| 201 |
|
| 202 | void fib_nh_common_release(struct fib_nh_common *nhc)
|
| 203 | {
|
| 204 | netdev_put(nhc->nhc_dev, &nhc->nhc_dev_tracker);
|
| 205 | lwtstate_put(nhc->nhc_lwtstate);
|
| 206 | rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
|
| 207 | rt_fibinfo_free(&nhc->nhc_rth_input);
|
| 208 | free_nh_exceptions(nhc);
|
| 209 | }
|
| 210 |
|
| 211 | void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
|
| 212 | {
|
| 213 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 214 | if (fib_nh->nh_tclassid)
|
| 215 | atomic_dec(&net->ipv4.fib_num_tclassid_users);
|
| 216 | #endif
|
| 217 | fib_nh_common_release(&fib_nh->nh_common);
|
| 218 | }
|
| 219 |
|
| 220 |
|
| 221 | static void free_fib_info_rcu(struct rcu_head *head)
|
| 222 | {
|
| 223 | struct fib_info *fi = container_of(head, struct fib_info, rcu);
|
| 224 |
|
| 225 | if (fi->nh) {
|
| 226 | nexthop_put(fi->nh);
|
| 227 | } else {
|
| 228 | change_nexthops(fi) {
|
| 229 | fib_nh_release(fi->fib_net, nexthop_nh);
|
| 230 | } endfor_nexthops(fi);
|
| 231 | }
|
| 232 |
|
| 233 | ip_fib_metrics_put(fi->fib_metrics);
|
| 234 |
|
| 235 | kfree(fi);
|
| 236 | }
|
| 237 |
|
| 238 | void free_fib_info(struct fib_info *fi)
|
| 239 | {
|
| 240 | if (fi->fib_dead == 0) {
|
| 241 | pr_warn("Freeing alive fib_info %p\n", fi);
|
| 242 | return;
|
| 243 | }
|
| 244 |
|
| 245 | call_rcu_hurry(&fi->rcu, free_fib_info_rcu);
|
| 246 | }
|
| 247 | EXPORT_SYMBOL_GPL(free_fib_info);
|
| 248 |
|
| 249 | void fib_release_info(struct fib_info *fi)
|
| 250 | {
|
| 251 | ASSERT_RTNL();
|
| 252 | if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
|
| 253 | hlist_del(&fi->fib_hash);
|
| 254 | fi->fib_net->ipv4.fib_info_cnt--;
|
| 255 |
|
| 256 | if (fi->fib_prefsrc)
|
| 257 | hlist_del(&fi->fib_lhash);
|
| 258 | if (fi->nh) {
|
| 259 | list_del(&fi->nh_list);
|
| 260 | } else {
|
| 261 | change_nexthops(fi) {
|
| 262 | if (!nexthop_nh->fib_nh_dev)
|
| 263 | continue;
|
| 264 | hlist_del_rcu(&nexthop_nh->nh_hash);
|
| 265 | } endfor_nexthops(fi)
|
| 266 | }
|
| 267 |
|
| 268 | WRITE_ONCE(fi->fib_dead, 1);
|
| 269 | fib_info_put(fi);
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
|
| 274 | {
|
| 275 | const struct fib_nh *onh;
|
| 276 |
|
| 277 | if (fi->nh || ofi->nh)
|
| 278 | return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
|
| 279 |
|
| 280 | if (ofi->fib_nhs == 0)
|
| 281 | return 0;
|
| 282 |
|
| 283 | for_nexthops(fi) {
|
| 284 | onh = fib_info_nh(ofi, nhsel);
|
| 285 |
|
| 286 | if (nh->fib_nh_oif != onh->fib_nh_oif ||
|
| 287 | nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
|
| 288 | nh->fib_nh_scope != onh->fib_nh_scope ||
|
| 289 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 290 | nh->fib_nh_weight != onh->fib_nh_weight ||
|
| 291 | #endif
|
| 292 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 293 | nh->nh_tclassid != onh->nh_tclassid ||
|
| 294 | #endif
|
| 295 | lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
|
| 296 | ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
|
| 297 | return -1;
|
| 298 |
|
| 299 | if (nh->fib_nh_gw_family == AF_INET &&
|
| 300 | nh->fib_nh_gw4 != onh->fib_nh_gw4)
|
| 301 | return -1;
|
| 302 |
|
| 303 | if (nh->fib_nh_gw_family == AF_INET6 &&
|
| 304 | ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
|
| 305 | return -1;
|
| 306 | } endfor_nexthops(fi);
|
| 307 | return 0;
|
| 308 | }
|
| 309 |
|
| 310 | static struct hlist_head *fib_nh_head(struct net_device *dev)
|
| 311 | {
|
| 312 | return &dev->fib_nh_head;
|
| 313 | }
|
| 314 |
|
| 315 | static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
|
| 316 | u32 prefsrc, u32 priority)
|
| 317 | {
|
| 318 | unsigned int val = init_val;
|
| 319 |
|
| 320 | val ^= (protocol << 8) | scope;
|
| 321 | val ^= prefsrc;
|
| 322 | val ^= priority;
|
| 323 |
|
| 324 | return val;
|
| 325 | }
|
| 326 |
|
| 327 | static unsigned int fib_info_hashfn_result(const struct net *net,
|
| 328 | unsigned int val)
|
| 329 | {
|
| 330 | return hash_32(val ^ net_hash_mix(net), net->ipv4.fib_info_hash_bits);
|
| 331 | }
|
| 332 |
|
| 333 | static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi)
|
| 334 | {
|
| 335 | struct net *net = fi->fib_net;
|
| 336 | unsigned int val;
|
| 337 |
|
| 338 | val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
|
| 339 | fi->fib_scope, (__force u32)fi->fib_prefsrc,
|
| 340 | fi->fib_priority);
|
| 341 |
|
| 342 | if (fi->nh) {
|
| 343 | val ^= fi->nh->id;
|
| 344 | } else {
|
| 345 | for_nexthops(fi) {
|
| 346 | val ^= nh->fib_nh_oif;
|
| 347 | } endfor_nexthops(fi)
|
| 348 | }
|
| 349 |
|
| 350 | return &net->ipv4.fib_info_hash[fib_info_hashfn_result(net, val)];
|
| 351 | }
|
| 352 |
|
| 353 | static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
|
| 354 | __be32 val)
|
| 355 | {
|
| 356 | unsigned int hash_bits = net->ipv4.fib_info_hash_bits;
|
| 357 | u32 slot;
|
| 358 |
|
| 359 | slot = hash_32(net_hash_mix(net) ^ (__force u32)val, hash_bits);
|
| 360 |
|
| 361 | return &net->ipv4.fib_info_hash[(1 << hash_bits) + slot];
|
| 362 | }
|
| 363 |
|
| 364 | static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
|
| 365 | {
|
| 366 |
|
| 367 | return kvzalloc_objs(struct hlist_head, (1 << hash_bits) * 2);
|
| 368 | }
|
| 369 |
|
| 370 | static void fib_info_hash_free(struct hlist_head *head)
|
| 371 | {
|
| 372 | kvfree(head);
|
| 373 | }
|
| 374 |
|
| 375 | static void fib_info_hash_grow(struct net *net)
|
| 376 | {
|
| 377 | unsigned int old_size = 1 << net->ipv4.fib_info_hash_bits;
|
| 378 | struct hlist_head *new_info_hash, *old_info_hash;
|
| 379 | unsigned int i;
|
| 380 |
|
| 381 | if (net->ipv4.fib_info_cnt < old_size)
|
| 382 | return;
|
| 383 |
|
| 384 | new_info_hash = fib_info_hash_alloc(net->ipv4.fib_info_hash_bits + 1);
|
| 385 | if (!new_info_hash)
|
| 386 | return;
|
| 387 |
|
| 388 | old_info_hash = net->ipv4.fib_info_hash;
|
| 389 | net->ipv4.fib_info_hash = new_info_hash;
|
| 390 | net->ipv4.fib_info_hash_bits += 1;
|
| 391 |
|
| 392 | for (i = 0; i < old_size; i++) {
|
| 393 | struct hlist_head *head = &old_info_hash[i];
|
| 394 | struct hlist_node *n;
|
| 395 | struct fib_info *fi;
|
| 396 |
|
| 397 | hlist_for_each_entry_safe(fi, n, head, fib_hash)
|
| 398 | hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi));
|
| 399 | }
|
| 400 |
|
| 401 | for (i = 0; i < old_size; i++) {
|
| 402 | struct hlist_head *lhead = &old_info_hash[old_size + i];
|
| 403 | struct hlist_node *n;
|
| 404 | struct fib_info *fi;
|
| 405 |
|
| 406 | hlist_for_each_entry_safe(fi, n, lhead, fib_lhash)
|
| 407 | hlist_add_head(&fi->fib_lhash,
|
| 408 | fib_info_laddrhash_bucket(fi->fib_net,
|
| 409 | fi->fib_prefsrc));
|
| 410 | }
|
| 411 |
|
| 412 | fib_info_hash_free(old_info_hash);
|
| 413 | }
|
| 414 |
|
| 415 |
|
| 416 | static struct fib_info *fib_find_info_nh(struct net *net,
|
| 417 | const struct fib_config *cfg)
|
| 418 | {
|
| 419 | struct hlist_head *head;
|
| 420 | struct fib_info *fi;
|
| 421 | unsigned int hash;
|
| 422 |
|
| 423 | hash = fib_info_hashfn_1(cfg->fc_nh_id,
|
| 424 | cfg->fc_protocol, cfg->fc_scope,
|
| 425 | (__force u32)cfg->fc_prefsrc,
|
| 426 | cfg->fc_priority);
|
| 427 | hash = fib_info_hashfn_result(net, hash);
|
| 428 | head = &net->ipv4.fib_info_hash[hash];
|
| 429 |
|
| 430 | hlist_for_each_entry(fi, head, fib_hash) {
|
| 431 | if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
|
| 432 | continue;
|
| 433 |
|
| 434 | if (cfg->fc_protocol == fi->fib_protocol &&
|
| 435 | cfg->fc_scope == fi->fib_scope &&
|
| 436 | cfg->fc_prefsrc == fi->fib_prefsrc &&
|
| 437 | cfg->fc_priority == fi->fib_priority &&
|
| 438 | cfg->fc_type == fi->fib_type &&
|
| 439 | cfg->fc_table == fi->fib_tb_id &&
|
| 440 | !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
|
| 441 | return fi;
|
| 442 | }
|
| 443 |
|
| 444 | return NULL;
|
| 445 | }
|
| 446 |
|
| 447 | static struct fib_info *fib_find_info(struct fib_info *nfi)
|
| 448 | {
|
| 449 | struct hlist_head *head = fib_info_hash_bucket(nfi);
|
| 450 | struct fib_info *fi;
|
| 451 |
|
| 452 | hlist_for_each_entry(fi, head, fib_hash) {
|
| 453 | if (fi->fib_nhs != nfi->fib_nhs)
|
| 454 | continue;
|
| 455 |
|
| 456 | if (nfi->fib_protocol == fi->fib_protocol &&
|
| 457 | nfi->fib_scope == fi->fib_scope &&
|
| 458 | nfi->fib_prefsrc == fi->fib_prefsrc &&
|
| 459 | nfi->fib_priority == fi->fib_priority &&
|
| 460 | nfi->fib_type == fi->fib_type &&
|
| 461 | nfi->fib_tb_id == fi->fib_tb_id &&
|
| 462 | memcmp(nfi->fib_metrics, fi->fib_metrics,
|
| 463 | sizeof(u32) * RTAX_MAX) == 0 &&
|
| 464 | !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
|
| 465 | nh_comp(fi, nfi) == 0)
|
| 466 | return fi;
|
| 467 | }
|
| 468 |
|
| 469 | return NULL;
|
| 470 | }
|
| 471 |
|
| 472 | |
| 473 | * Used only by redirect accept routine, under rcu_read_lock();
|
| 474 |
|
| 475 | int ip_fib_check_default(__be32 gw, struct net_device *dev)
|
| 476 | {
|
| 477 | struct hlist_head *head;
|
| 478 | struct fib_nh *nh;
|
| 479 |
|
| 480 | head = fib_nh_head(dev);
|
| 481 |
|
| 482 | hlist_for_each_entry_rcu(nh, head, nh_hash) {
|
| 483 | DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
|
| 484 | if (nh->fib_nh_gw4 == gw &&
|
| 485 | !(nh->fib_nh_flags & RTNH_F_DEAD)) {
|
| 486 | return 0;
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | return -1;
|
| 491 | }
|
| 492 |
|
| 493 | static size_t fib_nexthop_nlmsg_size(const struct fib_nh_common *nhc,
|
| 494 | bool skip_oif)
|
| 495 | {
|
| 496 | size_t nhsize = 0;
|
| 497 |
|
| 498 | switch (nhc->nhc_gw_family) {
|
| 499 | case AF_INET:
|
| 500 | nhsize += nla_total_size(4);
|
| 501 | break;
|
| 502 | case AF_INET6:
|
| 503 | nhsize += nla_total_size(sizeof(struct rtvia) +
|
| 504 | sizeof(struct in6_addr));
|
| 505 | break;
|
| 506 | }
|
| 507 |
|
| 508 | if (!skip_oif && nhc->nhc_dev)
|
| 509 | nhsize += nla_total_size(4);
|
| 510 |
|
| 511 | if (nhc->nhc_lwtstate) {
|
| 512 |
|
| 513 | nhsize += lwtunnel_get_encap_size(nhc->nhc_lwtstate);
|
| 514 |
|
| 515 | nhsize += nla_total_size(2);
|
| 516 | }
|
| 517 |
|
| 518 | return nhsize;
|
| 519 | }
|
| 520 |
|
| 521 | size_t fib_nlmsg_size(struct fib_info *fi)
|
| 522 | {
|
| 523 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
|
| 524 | + nla_total_size(4)
|
| 525 | + nla_total_size(4)
|
| 526 | + nla_total_size(4)
|
| 527 | + nla_total_size(4)
|
| 528 | + nla_total_size(TCP_CA_NAME_MAX);
|
| 529 | unsigned int nhs = fib_info_num_path(fi);
|
| 530 |
|
| 531 |
|
| 532 | payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
|
| 533 |
|
| 534 | if (fi->nh)
|
| 535 | payload += nla_total_size(4);
|
| 536 |
|
| 537 | if (nhs) {
|
| 538 | size_t mpsize = 0;
|
| 539 | unsigned int i;
|
| 540 |
|
| 541 | for (i = 0; i < fib_info_num_path(fi); i++) {
|
| 542 | struct fib_nh_common *nhc = fib_info_nhc(fi, i);
|
| 543 | size_t nhsize;
|
| 544 |
|
| 545 | if (!nhc)
|
| 546 | break;
|
| 547 |
|
| 548 | nhsize = fib_nexthop_nlmsg_size(nhc, nhs != 1);
|
| 549 |
|
| 550 | if (nhs != 1)
|
| 551 | nhsize += NLA_ALIGN(sizeof(struct rtnexthop));
|
| 552 |
|
| 553 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 554 | if (nhc->nhc_family == AF_INET) {
|
| 555 | struct fib_nh *nh;
|
| 556 |
|
| 557 | nh = container_of(nhc, struct fib_nh, nh_common);
|
| 558 | if (nh->nh_tclassid)
|
| 559 | nhsize += nla_total_size(4);
|
| 560 | }
|
| 561 | #endif
|
| 562 | if (nhs == 1)
|
| 563 | payload += nhsize;
|
| 564 | else
|
| 565 | mpsize += nhsize;
|
| 566 | }
|
| 567 |
|
| 568 | if (nhs != 1)
|
| 569 | payload += nla_total_size(mpsize);
|
| 570 | }
|
| 571 |
|
| 572 | return payload;
|
| 573 | }
|
| 574 |
|
| 575 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
|
| 576 | int dst_len, u32 tb_id, const struct nl_info *info,
|
| 577 | unsigned int nlm_flags)
|
| 578 | {
|
| 579 | struct fib_rt_info fri;
|
| 580 | struct sk_buff *skb;
|
| 581 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
|
| 582 | int err = -ENOBUFS;
|
| 583 |
|
| 584 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
|
| 585 | if (!skb)
|
| 586 | goto errout;
|
| 587 |
|
| 588 | fri.fi = fa->fa_info;
|
| 589 | fri.tb_id = tb_id;
|
| 590 | fri.dst = key;
|
| 591 | fri.dst_len = dst_len;
|
| 592 | fri.dscp = fa->fa_dscp;
|
| 593 | fri.type = fa->fa_type;
|
| 594 | fri.offload = READ_ONCE(fa->offload);
|
| 595 | fri.trap = READ_ONCE(fa->trap);
|
| 596 | fri.offload_failed = READ_ONCE(fa->offload_failed);
|
| 597 | err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
|
| 598 | if (err < 0) {
|
| 599 |
|
| 600 | WARN_ON(err == -EMSGSIZE);
|
| 601 | kfree_skb(skb);
|
| 602 | goto errout;
|
| 603 | }
|
| 604 | rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
|
| 605 | info->nlh, GFP_KERNEL);
|
| 606 | return;
|
| 607 | errout:
|
| 608 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
|
| 609 | }
|
| 610 |
|
| 611 | static int fib_detect_death(struct fib_info *fi, int order,
|
| 612 | struct fib_info **last_resort, int *last_idx,
|
| 613 | int dflt)
|
| 614 | {
|
| 615 | const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
|
| 616 | struct neighbour *n;
|
| 617 | int state = NUD_NONE;
|
| 618 |
|
| 619 | if (likely(nhc->nhc_gw_family == AF_INET))
|
| 620 | n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
|
| 621 | else if (IS_ENABLED(CONFIG_IPV6) && nhc->nhc_gw_family == AF_INET6)
|
| 622 | n = neigh_lookup(&nd_tbl, &nhc->nhc_gw.ipv6, nhc->nhc_dev);
|
| 623 | else
|
| 624 | n = NULL;
|
| 625 |
|
| 626 | if (n) {
|
| 627 | state = READ_ONCE(n->nud_state);
|
| 628 | neigh_release(n);
|
| 629 | } else {
|
| 630 | return 0;
|
| 631 | }
|
| 632 | if (state == NUD_REACHABLE)
|
| 633 | return 0;
|
| 634 | if ((state & NUD_VALID) && order != dflt)
|
| 635 | return 0;
|
| 636 | if ((state & NUD_VALID) ||
|
| 637 | (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
|
| 638 | *last_resort = fi;
|
| 639 | *last_idx = order;
|
| 640 | }
|
| 641 | return 1;
|
| 642 | }
|
| 643 |
|
| 644 | int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
|
| 645 | struct nlattr *encap, u16 encap_type,
|
| 646 | void *cfg, gfp_t gfp_flags,
|
| 647 | struct netlink_ext_ack *extack)
|
| 648 | {
|
| 649 | int err;
|
| 650 |
|
| 651 | nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
|
| 652 | gfp_flags);
|
| 653 | if (!nhc->nhc_pcpu_rth_output)
|
| 654 | return -ENOMEM;
|
| 655 |
|
| 656 | if (encap) {
|
| 657 | struct lwtunnel_state *lwtstate;
|
| 658 |
|
| 659 | err = lwtunnel_build_state(net, encap_type, encap,
|
| 660 | nhc->nhc_family, cfg, &lwtstate,
|
| 661 | extack);
|
| 662 | if (err)
|
| 663 | goto lwt_failure;
|
| 664 |
|
| 665 | nhc->nhc_lwtstate = lwtstate_get(lwtstate);
|
| 666 | }
|
| 667 |
|
| 668 | return 0;
|
| 669 |
|
| 670 | lwt_failure:
|
| 671 | rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
|
| 672 | nhc->nhc_pcpu_rth_output = NULL;
|
| 673 | return err;
|
| 674 | }
|
| 675 |
|
| 676 | int fib_nh_init(struct net *net, struct fib_nh *nh,
|
| 677 | struct fib_config *cfg, int nh_weight,
|
| 678 | struct netlink_ext_ack *extack)
|
| 679 | {
|
| 680 | int err;
|
| 681 |
|
| 682 | nh->fib_nh_family = AF_INET;
|
| 683 |
|
| 684 | err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
|
| 685 | cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
|
| 686 | if (err)
|
| 687 | return err;
|
| 688 |
|
| 689 | nh->fib_nh_oif = cfg->fc_oif;
|
| 690 | nh->fib_nh_gw_family = cfg->fc_gw_family;
|
| 691 | if (cfg->fc_gw_family == AF_INET)
|
| 692 | nh->fib_nh_gw4 = cfg->fc_gw4;
|
| 693 | else if (cfg->fc_gw_family == AF_INET6)
|
| 694 | nh->fib_nh_gw6 = cfg->fc_gw6;
|
| 695 |
|
| 696 | nh->fib_nh_flags = cfg->fc_flags;
|
| 697 |
|
| 698 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 699 | nh->nh_tclassid = cfg->fc_flow;
|
| 700 | if (nh->nh_tclassid)
|
| 701 | atomic_inc(&net->ipv4.fib_num_tclassid_users);
|
| 702 | #endif
|
| 703 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 704 | nh->fib_nh_weight = nh_weight;
|
| 705 | #endif
|
| 706 | return 0;
|
| 707 | }
|
| 708 |
|
| 709 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 710 |
|
| 711 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
|
| 712 | struct netlink_ext_ack *extack)
|
| 713 | {
|
| 714 | int nhs = 0;
|
| 715 |
|
| 716 | while (rtnh_ok(rtnh, remaining)) {
|
| 717 | nhs++;
|
| 718 | rtnh = rtnh_next(rtnh, &remaining);
|
| 719 | }
|
| 720 |
|
| 721 |
|
| 722 | if (remaining > 0) {
|
| 723 | NL_SET_ERR_MSG(extack,
|
| 724 | "Invalid nexthop configuration - extra data after nexthops");
|
| 725 | nhs = 0;
|
| 726 | }
|
| 727 |
|
| 728 | return nhs;
|
| 729 | }
|
| 730 |
|
| 731 | static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
|
| 732 | struct netlink_ext_ack *extack)
|
| 733 | {
|
| 734 | if (nla_len(nla) < sizeof(*gw)) {
|
| 735 | NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
|
| 736 | return -EINVAL;
|
| 737 | }
|
| 738 |
|
| 739 | *gw = nla_get_in_addr(nla);
|
| 740 |
|
| 741 | return 0;
|
| 742 | }
|
| 743 |
|
| 744 |
|
| 745 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
|
| 746 | int remaining, struct fib_config *cfg,
|
| 747 | struct netlink_ext_ack *extack)
|
| 748 | {
|
| 749 | struct net *net = fi->fib_net;
|
| 750 | struct fib_config fib_cfg;
|
| 751 | struct fib_nh *nh;
|
| 752 | int ret;
|
| 753 |
|
| 754 | change_nexthops(fi) {
|
| 755 | int attrlen;
|
| 756 |
|
| 757 | memset(&fib_cfg, 0, sizeof(fib_cfg));
|
| 758 |
|
| 759 | if (!rtnh_ok(rtnh, remaining)) {
|
| 760 | NL_SET_ERR_MSG(extack,
|
| 761 | "Invalid nexthop configuration - extra data after nexthop");
|
| 762 | return -EINVAL;
|
| 763 | }
|
| 764 |
|
| 765 | if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
|
| 766 | NL_SET_ERR_MSG(extack,
|
| 767 | "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
|
| 768 | return -EINVAL;
|
| 769 | }
|
| 770 |
|
| 771 | fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
|
| 772 | fib_cfg.fc_oif = rtnh->rtnh_ifindex;
|
| 773 |
|
| 774 | attrlen = rtnh_attrlen(rtnh);
|
| 775 | if (attrlen > 0) {
|
| 776 | struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
|
| 777 |
|
| 778 | nla = nla_find(attrs, attrlen, RTA_GATEWAY);
|
| 779 | nlav = nla_find(attrs, attrlen, RTA_VIA);
|
| 780 | if (nla && nlav) {
|
| 781 | NL_SET_ERR_MSG(extack,
|
| 782 | "Nexthop configuration can not contain both GATEWAY and VIA");
|
| 783 | return -EINVAL;
|
| 784 | }
|
| 785 | if (nla) {
|
| 786 | ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
|
| 787 | extack);
|
| 788 | if (ret)
|
| 789 | goto errout;
|
| 790 |
|
| 791 | if (fib_cfg.fc_gw4)
|
| 792 | fib_cfg.fc_gw_family = AF_INET;
|
| 793 | } else if (nlav) {
|
| 794 | ret = fib_gw_from_via(&fib_cfg, nlav, extack);
|
| 795 | if (ret)
|
| 796 | goto errout;
|
| 797 | }
|
| 798 |
|
| 799 | nla = nla_find(attrs, attrlen, RTA_FLOW);
|
| 800 | if (nla) {
|
| 801 | if (nla_len(nla) < sizeof(u32)) {
|
| 802 | NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
|
| 803 | return -EINVAL;
|
| 804 | }
|
| 805 | fib_cfg.fc_flow = nla_get_u32(nla);
|
| 806 | }
|
| 807 |
|
| 808 | fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
|
| 809 | |
| 810 | * lwtunnel_valid_encap_type_attr
|
| 811 |
|
| 812 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
|
| 813 | if (nla)
|
| 814 | fib_cfg.fc_encap_type = nla_get_u16(nla);
|
| 815 | }
|
| 816 |
|
| 817 | ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
|
| 818 | rtnh->rtnh_hops + 1, extack);
|
| 819 | if (ret)
|
| 820 | goto errout;
|
| 821 |
|
| 822 | rtnh = rtnh_next(rtnh, &remaining);
|
| 823 | } endfor_nexthops(fi);
|
| 824 |
|
| 825 | ret = -EINVAL;
|
| 826 | nh = fib_info_nh(fi, 0);
|
| 827 | if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
|
| 828 | NL_SET_ERR_MSG(extack,
|
| 829 | "Nexthop device index does not match RTA_OIF");
|
| 830 | goto errout;
|
| 831 | }
|
| 832 | if (cfg->fc_gw_family) {
|
| 833 | if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
|
| 834 | (cfg->fc_gw_family == AF_INET &&
|
| 835 | nh->fib_nh_gw4 != cfg->fc_gw4) ||
|
| 836 | (cfg->fc_gw_family == AF_INET6 &&
|
| 837 | ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
|
| 838 | NL_SET_ERR_MSG(extack,
|
| 839 | "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
|
| 840 | goto errout;
|
| 841 | }
|
| 842 | }
|
| 843 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 844 | if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
|
| 845 | NL_SET_ERR_MSG(extack,
|
| 846 | "Nexthop class id does not match RTA_FLOW");
|
| 847 | goto errout;
|
| 848 | }
|
| 849 | #endif
|
| 850 | ret = 0;
|
| 851 | errout:
|
| 852 | return ret;
|
| 853 | }
|
| 854 |
|
| 855 |
|
| 856 | static void fib_rebalance(struct fib_info *fi)
|
| 857 | {
|
| 858 | int total;
|
| 859 | int w;
|
| 860 |
|
| 861 | if (fib_info_num_path(fi) < 2)
|
| 862 | return;
|
| 863 |
|
| 864 | total = 0;
|
| 865 | for_nexthops(fi) {
|
| 866 | if (nh->fib_nh_flags & RTNH_F_DEAD)
|
| 867 | continue;
|
| 868 |
|
| 869 | if (ip_ignore_linkdown(nh->fib_nh_dev) &&
|
| 870 | nh->fib_nh_flags & RTNH_F_LINKDOWN)
|
| 871 | continue;
|
| 872 |
|
| 873 | total += nh->fib_nh_weight;
|
| 874 | } endfor_nexthops(fi);
|
| 875 |
|
| 876 | w = 0;
|
| 877 | change_nexthops(fi) {
|
| 878 | int upper_bound;
|
| 879 |
|
| 880 | if (!total || nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
|
| 881 | upper_bound = -1;
|
| 882 | } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
|
| 883 | nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
|
| 884 | upper_bound = -1;
|
| 885 | } else {
|
| 886 | w += nexthop_nh->fib_nh_weight;
|
| 887 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
|
| 888 | total) - 1;
|
| 889 | }
|
| 890 |
|
| 891 | atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
|
| 892 | } endfor_nexthops(fi);
|
| 893 | }
|
| 894 | #else
|
| 895 |
|
| 896 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
|
| 897 | int remaining, struct fib_config *cfg,
|
| 898 | struct netlink_ext_ack *extack)
|
| 899 | {
|
| 900 | NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
|
| 901 |
|
| 902 | return -EINVAL;
|
| 903 | }
|
| 904 |
|
| 905 | #define fib_rebalance(fi) do { } while (0)
|
| 906 |
|
| 907 | #endif
|
| 908 |
|
| 909 | static int fib_encap_match(struct net *net, u16 encap_type,
|
| 910 | struct nlattr *encap,
|
| 911 | const struct fib_nh *nh,
|
| 912 | const struct fib_config *cfg,
|
| 913 | struct netlink_ext_ack *extack)
|
| 914 | {
|
| 915 | struct lwtunnel_state *lwtstate;
|
| 916 | int ret, result = 0;
|
| 917 |
|
| 918 | if (encap_type == LWTUNNEL_ENCAP_NONE)
|
| 919 | return 0;
|
| 920 |
|
| 921 | ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
|
| 922 | cfg, &lwtstate, extack);
|
| 923 | if (!ret) {
|
| 924 | result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
|
| 925 | lwtstate_free(lwtstate);
|
| 926 | }
|
| 927 |
|
| 928 | return result;
|
| 929 | }
|
| 930 |
|
| 931 | int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
|
| 932 | struct netlink_ext_ack *extack)
|
| 933 | {
|
| 934 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 935 | struct rtnexthop *rtnh;
|
| 936 | int remaining;
|
| 937 | #endif
|
| 938 |
|
| 939 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
|
| 940 | return 1;
|
| 941 |
|
| 942 | if (cfg->fc_nh_id) {
|
| 943 | if (fi->nh && cfg->fc_nh_id == fi->nh->id)
|
| 944 | return 0;
|
| 945 | return 1;
|
| 946 | }
|
| 947 |
|
| 948 | if (fi->nh) {
|
| 949 | if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
|
| 950 | return 1;
|
| 951 | return 0;
|
| 952 | }
|
| 953 |
|
| 954 | if (cfg->fc_oif || cfg->fc_gw_family) {
|
| 955 | struct fib_nh *nh;
|
| 956 |
|
| 957 | nh = fib_info_nh(fi, 0);
|
| 958 | if (cfg->fc_encap) {
|
| 959 | if (fib_encap_match(net, cfg->fc_encap_type,
|
| 960 | cfg->fc_encap, nh, cfg, extack))
|
| 961 | return 1;
|
| 962 | }
|
| 963 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 964 | if (cfg->fc_flow &&
|
| 965 | cfg->fc_flow != nh->nh_tclassid)
|
| 966 | return 1;
|
| 967 | #endif
|
| 968 | if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
|
| 969 | (cfg->fc_gw_family &&
|
| 970 | cfg->fc_gw_family != nh->fib_nh_gw_family))
|
| 971 | return 1;
|
| 972 |
|
| 973 | if (cfg->fc_gw_family == AF_INET &&
|
| 974 | cfg->fc_gw4 != nh->fib_nh_gw4)
|
| 975 | return 1;
|
| 976 |
|
| 977 | if (cfg->fc_gw_family == AF_INET6 &&
|
| 978 | ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
|
| 979 | return 1;
|
| 980 |
|
| 981 | return 0;
|
| 982 | }
|
| 983 |
|
| 984 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 985 | if (!cfg->fc_mp)
|
| 986 | return 0;
|
| 987 |
|
| 988 | rtnh = cfg->fc_mp;
|
| 989 | remaining = cfg->fc_mp_len;
|
| 990 |
|
| 991 | for_nexthops(fi) {
|
| 992 | int attrlen;
|
| 993 |
|
| 994 | if (!rtnh_ok(rtnh, remaining))
|
| 995 | return -EINVAL;
|
| 996 |
|
| 997 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
|
| 998 | return 1;
|
| 999 |
|
| 1000 | attrlen = rtnh_attrlen(rtnh);
|
| 1001 | if (attrlen > 0) {
|
| 1002 | struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
|
| 1003 | int err;
|
| 1004 |
|
| 1005 | nla = nla_find(attrs, attrlen, RTA_GATEWAY);
|
| 1006 | nlav = nla_find(attrs, attrlen, RTA_VIA);
|
| 1007 | if (nla && nlav) {
|
| 1008 | NL_SET_ERR_MSG(extack,
|
| 1009 | "Nexthop configuration can not contain both GATEWAY and VIA");
|
| 1010 | return -EINVAL;
|
| 1011 | }
|
| 1012 |
|
| 1013 | if (nla) {
|
| 1014 | __be32 gw;
|
| 1015 |
|
| 1016 | err = fib_gw_from_attr(&gw, nla, extack);
|
| 1017 | if (err)
|
| 1018 | return err;
|
| 1019 |
|
| 1020 | if (nh->fib_nh_gw_family != AF_INET ||
|
| 1021 | gw != nh->fib_nh_gw4)
|
| 1022 | return 1;
|
| 1023 | } else if (nlav) {
|
| 1024 | struct fib_config cfg2;
|
| 1025 |
|
| 1026 | err = fib_gw_from_via(&cfg2, nlav, extack);
|
| 1027 | if (err)
|
| 1028 | return err;
|
| 1029 |
|
| 1030 | switch (nh->fib_nh_gw_family) {
|
| 1031 | case AF_INET:
|
| 1032 | if (cfg2.fc_gw_family != AF_INET ||
|
| 1033 | cfg2.fc_gw4 != nh->fib_nh_gw4)
|
| 1034 | return 1;
|
| 1035 | break;
|
| 1036 | case AF_INET6:
|
| 1037 | if (cfg2.fc_gw_family != AF_INET6 ||
|
| 1038 | ipv6_addr_cmp(&cfg2.fc_gw6,
|
| 1039 | &nh->fib_nh_gw6))
|
| 1040 | return 1;
|
| 1041 | break;
|
| 1042 | }
|
| 1043 | }
|
| 1044 |
|
| 1045 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 1046 | nla = nla_find(attrs, attrlen, RTA_FLOW);
|
| 1047 | if (nla) {
|
| 1048 | if (nla_len(nla) < sizeof(u32)) {
|
| 1049 | NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
|
| 1050 | return -EINVAL;
|
| 1051 | }
|
| 1052 | if (nla_get_u32(nla) != nh->nh_tclassid)
|
| 1053 | return 1;
|
| 1054 | }
|
| 1055 | #endif
|
| 1056 | }
|
| 1057 |
|
| 1058 | rtnh = rtnh_next(rtnh, &remaining);
|
| 1059 | } endfor_nexthops(fi);
|
| 1060 | #endif
|
| 1061 | return 0;
|
| 1062 | }
|
| 1063 |
|
| 1064 | bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
|
| 1065 | {
|
| 1066 | struct nlattr *nla;
|
| 1067 | int remaining;
|
| 1068 |
|
| 1069 | if (!cfg->fc_mx)
|
| 1070 | return true;
|
| 1071 |
|
| 1072 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
|
| 1073 | int type = nla_type(nla);
|
| 1074 | u32 fi_val, val;
|
| 1075 |
|
| 1076 | if (!type)
|
| 1077 | continue;
|
| 1078 | if (type > RTAX_MAX)
|
| 1079 | return false;
|
| 1080 |
|
| 1081 | type = array_index_nospec(type, RTAX_MAX + 1);
|
| 1082 | if (type == RTAX_CC_ALGO) {
|
| 1083 | char tmp[TCP_CA_NAME_MAX];
|
| 1084 | bool ecn_ca = false;
|
| 1085 |
|
| 1086 | nla_strscpy(tmp, nla, sizeof(tmp));
|
| 1087 | val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
|
| 1088 | } else {
|
| 1089 | if (nla_len(nla) != sizeof(u32))
|
| 1090 | return false;
|
| 1091 | val = nla_get_u32(nla);
|
| 1092 | }
|
| 1093 |
|
| 1094 | fi_val = fi->fib_metrics->metrics[type - 1];
|
| 1095 | if (type == RTAX_FEATURES)
|
| 1096 | fi_val &= ~DST_FEATURE_ECN_CA;
|
| 1097 |
|
| 1098 | if (fi_val != val)
|
| 1099 | return false;
|
| 1100 | }
|
| 1101 |
|
| 1102 | return true;
|
| 1103 | }
|
| 1104 |
|
| 1105 | static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
|
| 1106 | u32 table, struct netlink_ext_ack *extack)
|
| 1107 | {
|
| 1108 | struct fib6_config cfg = {
|
| 1109 | .fc_table = table,
|
| 1110 | .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
|
| 1111 | .fc_ifindex = nh->fib_nh_oif,
|
| 1112 | .fc_gateway = nh->fib_nh_gw6,
|
| 1113 | };
|
| 1114 | struct fib6_nh fib6_nh = {};
|
| 1115 | int err;
|
| 1116 |
|
| 1117 | err = fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
|
| 1118 | if (!err) {
|
| 1119 | nh->fib_nh_dev = fib6_nh.fib_nh_dev;
|
| 1120 | netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
|
| 1121 | GFP_KERNEL);
|
| 1122 | nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
|
| 1123 | nh->fib_nh_scope = RT_SCOPE_LINK;
|
| 1124 |
|
| 1125 | fib6_nh_release(&fib6_nh);
|
| 1126 | }
|
| 1127 |
|
| 1128 | return err;
|
| 1129 | }
|
| 1130 |
|
| 1131 | |
| 1132 | * Picture
|
| 1133 | * -------
|
| 1134 | *
|
| 1135 | * Semantics of nexthop is very messy by historical reasons.
|
| 1136 | * We have to take into account, that:
|
| 1137 | * a) gateway can be actually local interface address,
|
| 1138 | * so that gatewayed route is direct.
|
| 1139 | * b) gateway must be on-link address, possibly
|
| 1140 | * described not by an ifaddr, but also by a direct route.
|
| 1141 | * c) If both gateway and interface are specified, they should not
|
| 1142 | * contradict.
|
| 1143 | * d) If we use tunnel routes, gateway could be not on-link.
|
| 1144 | *
|
| 1145 | * Attempt to reconcile all of these (alas, self-contradictory) conditions
|
| 1146 | * results in pretty ugly and hairy code with obscure logic.
|
| 1147 | *
|
| 1148 | * I chose to generalized it instead, so that the size
|
| 1149 | * of code does not increase practically, but it becomes
|
| 1150 | * much more general.
|
| 1151 | * Every prefix is assigned a "scope" value: "host" is local address,
|
| 1152 | * "link" is direct route,
|
| 1153 | * [ ... "site" ... "interior" ... ]
|
| 1154 | * and "universe" is true gateway route with global meaning.
|
| 1155 | *
|
| 1156 | * Every prefix refers to a set of "nexthop"s (gw, oif),
|
| 1157 | * where gw must have narrower scope. This recursion stops
|
| 1158 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
|
| 1159 | * which means that gw is forced to be on link.
|
| 1160 | *
|
| 1161 | * Code is still hairy, but now it is apparently logically
|
| 1162 | * consistent and very flexible. F.e. as by-product it allows
|
| 1163 | * to co-exists in peace independent exterior and interior
|
| 1164 | * routing processes.
|
| 1165 | *
|
| 1166 | * Normally it looks as following.
|
| 1167 | *
|
| 1168 | * {universe prefix} -> (gw, oif) [scope link]
|
| 1169 | * |
|
| 1170 | * |-> {link prefix} -> (gw, oif) [scope local]
|
| 1171 | * |
|
| 1172 | * |-> {local prefix} (terminal node)
|
| 1173 |
|
| 1174 | static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
|
| 1175 | u8 scope, struct netlink_ext_ack *extack)
|
| 1176 | {
|
| 1177 | struct net_device *dev;
|
| 1178 | struct fib_result res;
|
| 1179 | int err = 0;
|
| 1180 |
|
| 1181 | if (nh->fib_nh_flags & RTNH_F_ONLINK) {
|
| 1182 | unsigned int addr_type;
|
| 1183 |
|
| 1184 | if (scope >= RT_SCOPE_LINK) {
|
| 1185 | NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
|
| 1186 | return -EINVAL;
|
| 1187 | }
|
| 1188 | dev = __dev_get_by_index(net, nh->fib_nh_oif);
|
| 1189 | if (!dev) {
|
| 1190 | NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
|
| 1191 | return -ENODEV;
|
| 1192 | }
|
| 1193 | if (!(dev->flags & IFF_UP)) {
|
| 1194 | NL_SET_ERR_MSG(extack, "Nexthop device is not up");
|
| 1195 | return -ENETDOWN;
|
| 1196 | }
|
| 1197 | addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
|
| 1198 | if (addr_type != RTN_UNICAST) {
|
| 1199 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
|
| 1200 | return -EINVAL;
|
| 1201 | }
|
| 1202 | if (!netif_carrier_ok(dev))
|
| 1203 | nh->fib_nh_flags |= RTNH_F_LINKDOWN;
|
| 1204 | nh->fib_nh_dev = dev;
|
| 1205 | netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
|
| 1206 | nh->fib_nh_scope = RT_SCOPE_LINK;
|
| 1207 | return 0;
|
| 1208 | }
|
| 1209 | rcu_read_lock();
|
| 1210 | {
|
| 1211 | struct fib_table *tbl = NULL;
|
| 1212 | struct flowi4 fl4 = {
|
| 1213 | .daddr = nh->fib_nh_gw4,
|
| 1214 | .flowi4_scope = scope + 1,
|
| 1215 | .flowi4_oif = nh->fib_nh_oif,
|
| 1216 | .flowi4_iif = LOOPBACK_IFINDEX,
|
| 1217 | };
|
| 1218 |
|
| 1219 |
|
| 1220 | if (fl4.flowi4_scope < RT_SCOPE_LINK)
|
| 1221 | fl4.flowi4_scope = RT_SCOPE_LINK;
|
| 1222 |
|
| 1223 | if (table && table != RT_TABLE_MAIN)
|
| 1224 | tbl = fib_get_table(net, table);
|
| 1225 |
|
| 1226 | if (tbl)
|
| 1227 | err = fib_table_lookup(tbl, &fl4, &res,
|
| 1228 | FIB_LOOKUP_IGNORE_LINKSTATE |
|
| 1229 | FIB_LOOKUP_NOREF);
|
| 1230 |
|
| 1231 | |
| 1232 | * is needed for example when nexthops are in the local
|
| 1233 | * table rather than the given table
|
| 1234 |
|
| 1235 | if (!tbl || err) {
|
| 1236 | err = fib_lookup(net, &fl4, &res,
|
| 1237 | FIB_LOOKUP_IGNORE_LINKSTATE);
|
| 1238 | }
|
| 1239 |
|
| 1240 | if (err) {
|
| 1241 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
|
| 1242 | goto out;
|
| 1243 | }
|
| 1244 | }
|
| 1245 |
|
| 1246 | err = -EINVAL;
|
| 1247 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
|
| 1248 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
|
| 1249 | goto out;
|
| 1250 | }
|
| 1251 | nh->fib_nh_scope = res.scope;
|
| 1252 | nh->fib_nh_oif = FIB_RES_OIF(res);
|
| 1253 | nh->fib_nh_dev = dev = FIB_RES_DEV(res);
|
| 1254 | if (!dev) {
|
| 1255 | NL_SET_ERR_MSG(extack,
|
| 1256 | "No egress device for nexthop gateway");
|
| 1257 | goto out;
|
| 1258 | }
|
| 1259 | netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
|
| 1260 | if (!netif_carrier_ok(dev))
|
| 1261 | nh->fib_nh_flags |= RTNH_F_LINKDOWN;
|
| 1262 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
|
| 1263 | out:
|
| 1264 | rcu_read_unlock();
|
| 1265 | return err;
|
| 1266 | }
|
| 1267 |
|
| 1268 | static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
|
| 1269 | struct netlink_ext_ack *extack)
|
| 1270 | {
|
| 1271 | struct in_device *in_dev;
|
| 1272 | int err;
|
| 1273 |
|
| 1274 | if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
|
| 1275 | NL_SET_ERR_MSG(extack,
|
| 1276 | "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
|
| 1277 | return -EINVAL;
|
| 1278 | }
|
| 1279 |
|
| 1280 | rcu_read_lock();
|
| 1281 |
|
| 1282 | err = -ENODEV;
|
| 1283 | in_dev = inetdev_by_index(net, nh->fib_nh_oif);
|
| 1284 | if (!in_dev)
|
| 1285 | goto out;
|
| 1286 | err = -ENETDOWN;
|
| 1287 | if (!(in_dev->dev->flags & IFF_UP)) {
|
| 1288 | NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
|
| 1289 | goto out;
|
| 1290 | }
|
| 1291 |
|
| 1292 | nh->fib_nh_dev = in_dev->dev;
|
| 1293 | netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
|
| 1294 | nh->fib_nh_scope = RT_SCOPE_HOST;
|
| 1295 | if (!netif_carrier_ok(nh->fib_nh_dev))
|
| 1296 | nh->fib_nh_flags |= RTNH_F_LINKDOWN;
|
| 1297 | err = 0;
|
| 1298 | out:
|
| 1299 | rcu_read_unlock();
|
| 1300 | return err;
|
| 1301 | }
|
| 1302 |
|
| 1303 | int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
|
| 1304 | struct netlink_ext_ack *extack)
|
| 1305 | {
|
| 1306 | int err;
|
| 1307 |
|
| 1308 | if (nh->fib_nh_gw_family == AF_INET)
|
| 1309 | err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
|
| 1310 | else if (nh->fib_nh_gw_family == AF_INET6)
|
| 1311 | err = fib_check_nh_v6_gw(net, nh, table, extack);
|
| 1312 | else
|
| 1313 | err = fib_check_nh_nongw(net, nh, extack);
|
| 1314 |
|
| 1315 | return err;
|
| 1316 | }
|
| 1317 |
|
| 1318 | __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
|
| 1319 | unsigned char scope)
|
| 1320 | {
|
| 1321 | struct fib_nh *nh;
|
| 1322 | __be32 saddr;
|
| 1323 |
|
| 1324 | if (nhc->nhc_family != AF_INET)
|
| 1325 | return inet_select_addr(nhc->nhc_dev, 0, scope);
|
| 1326 |
|
| 1327 | nh = container_of(nhc, struct fib_nh, nh_common);
|
| 1328 | saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
|
| 1329 |
|
| 1330 | WRITE_ONCE(nh->nh_saddr, saddr);
|
| 1331 | WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
|
| 1332 |
|
| 1333 | return saddr;
|
| 1334 | }
|
| 1335 |
|
| 1336 | __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
|
| 1337 | {
|
| 1338 | struct fib_nh_common *nhc = res->nhc;
|
| 1339 |
|
| 1340 | if (res->fi->fib_prefsrc)
|
| 1341 | return res->fi->fib_prefsrc;
|
| 1342 |
|
| 1343 | if (nhc->nhc_family == AF_INET) {
|
| 1344 | struct fib_nh *nh;
|
| 1345 |
|
| 1346 | nh = container_of(nhc, struct fib_nh, nh_common);
|
| 1347 | if (READ_ONCE(nh->nh_saddr_genid) ==
|
| 1348 | atomic_read(&net->ipv4.dev_addr_genid))
|
| 1349 | return READ_ONCE(nh->nh_saddr);
|
| 1350 | }
|
| 1351 |
|
| 1352 | return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
|
| 1353 | }
|
| 1354 |
|
| 1355 | static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
|
| 1356 | {
|
| 1357 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
|
| 1358 | fib_prefsrc != cfg->fc_dst) {
|
| 1359 | u32 tb_id = cfg->fc_table;
|
| 1360 | int rc;
|
| 1361 |
|
| 1362 | if (tb_id == RT_TABLE_MAIN)
|
| 1363 | tb_id = RT_TABLE_LOCAL;
|
| 1364 |
|
| 1365 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
|
| 1366 | fib_prefsrc, tb_id);
|
| 1367 |
|
| 1368 | if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
|
| 1369 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
|
| 1370 | fib_prefsrc, RT_TABLE_LOCAL);
|
| 1371 | }
|
| 1372 |
|
| 1373 | if (rc != RTN_LOCAL)
|
| 1374 | return false;
|
| 1375 | }
|
| 1376 | return true;
|
| 1377 | }
|
| 1378 |
|
| 1379 | struct fib_info *fib_create_info(struct fib_config *cfg,
|
| 1380 | struct netlink_ext_ack *extack)
|
| 1381 | {
|
| 1382 | int err;
|
| 1383 | struct fib_info *fi = NULL;
|
| 1384 | struct nexthop *nh = NULL;
|
| 1385 | struct fib_info *ofi;
|
| 1386 | int nhs = 1;
|
| 1387 | struct net *net = cfg->fc_nlinfo.nl_net;
|
| 1388 |
|
| 1389 | ASSERT_RTNL();
|
| 1390 | if (cfg->fc_type > RTN_MAX)
|
| 1391 | goto err_inval;
|
| 1392 |
|
| 1393 |
|
| 1394 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
|
| 1395 | NL_SET_ERR_MSG(extack, "Invalid scope");
|
| 1396 | goto err_inval;
|
| 1397 | }
|
| 1398 |
|
| 1399 | if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
|
| 1400 | NL_SET_ERR_MSG(extack,
|
| 1401 | "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
|
| 1402 | goto err_inval;
|
| 1403 | }
|
| 1404 |
|
| 1405 | if (cfg->fc_nh_id) {
|
| 1406 | if (!cfg->fc_mx) {
|
| 1407 | fi = fib_find_info_nh(net, cfg);
|
| 1408 | if (fi) {
|
| 1409 | refcount_inc(&fi->fib_treeref);
|
| 1410 | return fi;
|
| 1411 | }
|
| 1412 | }
|
| 1413 |
|
| 1414 | nh = nexthop_find_by_id(net, cfg->fc_nh_id);
|
| 1415 | if (!nh) {
|
| 1416 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
|
| 1417 | goto err_inval;
|
| 1418 | }
|
| 1419 | nhs = 0;
|
| 1420 | }
|
| 1421 |
|
| 1422 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 1423 | if (cfg->fc_mp) {
|
| 1424 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
|
| 1425 | if (nhs == 0)
|
| 1426 | goto err_inval;
|
| 1427 | }
|
| 1428 | #endif
|
| 1429 |
|
| 1430 | fib_info_hash_grow(net);
|
| 1431 |
|
| 1432 | fi = kzalloc_flex(*fi, fib_nh, nhs);
|
| 1433 | if (!fi) {
|
| 1434 | err = -ENOBUFS;
|
| 1435 | goto failure;
|
| 1436 | }
|
| 1437 |
|
| 1438 | fi->fib_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, extack);
|
| 1439 | if (IS_ERR(fi->fib_metrics)) {
|
| 1440 | err = PTR_ERR(fi->fib_metrics);
|
| 1441 | kfree(fi);
|
| 1442 | return ERR_PTR(err);
|
| 1443 | }
|
| 1444 |
|
| 1445 | fi->fib_net = net;
|
| 1446 | fi->fib_protocol = cfg->fc_protocol;
|
| 1447 | fi->fib_scope = cfg->fc_scope;
|
| 1448 | fi->fib_flags = cfg->fc_flags;
|
| 1449 | fi->fib_priority = cfg->fc_priority;
|
| 1450 | fi->fib_prefsrc = cfg->fc_prefsrc;
|
| 1451 | fi->fib_type = cfg->fc_type;
|
| 1452 | fi->fib_tb_id = cfg->fc_table;
|
| 1453 |
|
| 1454 | fi->fib_nhs = nhs;
|
| 1455 | if (nh) {
|
| 1456 | if (!nexthop_get(nh)) {
|
| 1457 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
|
| 1458 | err = -EINVAL;
|
| 1459 | } else {
|
| 1460 | err = 0;
|
| 1461 | fi->nh = nh;
|
| 1462 | }
|
| 1463 | } else {
|
| 1464 | change_nexthops(fi) {
|
| 1465 | nexthop_nh->nh_parent = fi;
|
| 1466 | } endfor_nexthops(fi)
|
| 1467 |
|
| 1468 | if (cfg->fc_mp)
|
| 1469 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
|
| 1470 | extack);
|
| 1471 | else
|
| 1472 | err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
|
| 1473 | }
|
| 1474 |
|
| 1475 | if (err != 0)
|
| 1476 | goto failure;
|
| 1477 |
|
| 1478 | if (fib_props[cfg->fc_type].error) {
|
| 1479 | if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
|
| 1480 | NL_SET_ERR_MSG(extack,
|
| 1481 | "Gateway, device and multipath can not be specified for this route type");
|
| 1482 | goto err_inval;
|
| 1483 | }
|
| 1484 | goto link_it;
|
| 1485 | } else {
|
| 1486 | switch (cfg->fc_type) {
|
| 1487 | case RTN_UNICAST:
|
| 1488 | case RTN_LOCAL:
|
| 1489 | case RTN_BROADCAST:
|
| 1490 | case RTN_ANYCAST:
|
| 1491 | case RTN_MULTICAST:
|
| 1492 | break;
|
| 1493 | default:
|
| 1494 | NL_SET_ERR_MSG(extack, "Invalid route type");
|
| 1495 | goto err_inval;
|
| 1496 | }
|
| 1497 | }
|
| 1498 |
|
| 1499 | if (cfg->fc_scope > RT_SCOPE_HOST) {
|
| 1500 | NL_SET_ERR_MSG(extack, "Invalid scope");
|
| 1501 | goto err_inval;
|
| 1502 | }
|
| 1503 |
|
| 1504 | if (fi->nh) {
|
| 1505 | err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
|
| 1506 | if (err)
|
| 1507 | goto failure;
|
| 1508 | } else if (cfg->fc_scope == RT_SCOPE_HOST) {
|
| 1509 | struct fib_nh *nh = fi->fib_nh;
|
| 1510 |
|
| 1511 |
|
| 1512 | if (nhs != 1) {
|
| 1513 | NL_SET_ERR_MSG(extack,
|
| 1514 | "Route with host scope can not have multiple nexthops");
|
| 1515 | goto err_inval;
|
| 1516 | }
|
| 1517 | if (nh->fib_nh_gw_family) {
|
| 1518 | NL_SET_ERR_MSG(extack,
|
| 1519 | "Route with host scope can not have a gateway");
|
| 1520 | goto err_inval;
|
| 1521 | }
|
| 1522 | nh->fib_nh_scope = RT_SCOPE_NOWHERE;
|
| 1523 | nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
|
| 1524 | err = -ENODEV;
|
| 1525 | if (!nh->fib_nh_dev)
|
| 1526 | goto failure;
|
| 1527 | netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
|
| 1528 | GFP_KERNEL);
|
| 1529 | } else {
|
| 1530 | int linkdown = 0;
|
| 1531 |
|
| 1532 | change_nexthops(fi) {
|
| 1533 | err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
|
| 1534 | cfg->fc_table, cfg->fc_scope,
|
| 1535 | extack);
|
| 1536 | if (err != 0)
|
| 1537 | goto failure;
|
| 1538 | if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
|
| 1539 | linkdown++;
|
| 1540 | } endfor_nexthops(fi)
|
| 1541 | if (linkdown == fi->fib_nhs)
|
| 1542 | fi->fib_flags |= RTNH_F_LINKDOWN;
|
| 1543 | }
|
| 1544 |
|
| 1545 | if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
|
| 1546 | NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
|
| 1547 | goto err_inval;
|
| 1548 | }
|
| 1549 |
|
| 1550 | if (!fi->nh) {
|
| 1551 | change_nexthops(fi) {
|
| 1552 | fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
|
| 1553 | fi->fib_scope);
|
| 1554 | if (nexthop_nh->fib_nh_gw_family == AF_INET6)
|
| 1555 | fi->fib_nh_is_v6 = true;
|
| 1556 | } endfor_nexthops(fi)
|
| 1557 |
|
| 1558 | fib_rebalance(fi);
|
| 1559 | }
|
| 1560 |
|
| 1561 | link_it:
|
| 1562 | ofi = fib_find_info(fi);
|
| 1563 | if (ofi) {
|
| 1564 |
|
| 1565 | fi->fib_dead = 1;
|
| 1566 | free_fib_info(fi);
|
| 1567 | refcount_inc(&ofi->fib_treeref);
|
| 1568 | return ofi;
|
| 1569 | }
|
| 1570 |
|
| 1571 | refcount_set(&fi->fib_treeref, 1);
|
| 1572 | refcount_set(&fi->fib_clntref, 1);
|
| 1573 |
|
| 1574 | net->ipv4.fib_info_cnt++;
|
| 1575 | hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi));
|
| 1576 |
|
| 1577 | if (fi->fib_prefsrc) {
|
| 1578 | struct hlist_head *head;
|
| 1579 |
|
| 1580 | head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
|
| 1581 | hlist_add_head(&fi->fib_lhash, head);
|
| 1582 | }
|
| 1583 | if (fi->nh) {
|
| 1584 | list_add(&fi->nh_list, &nh->fi_list);
|
| 1585 | } else {
|
| 1586 | change_nexthops(fi) {
|
| 1587 | struct hlist_head *head;
|
| 1588 |
|
| 1589 | if (!nexthop_nh->fib_nh_dev)
|
| 1590 | continue;
|
| 1591 | head = fib_nh_head(nexthop_nh->fib_nh_dev);
|
| 1592 | hlist_add_head_rcu(&nexthop_nh->nh_hash, head);
|
| 1593 | } endfor_nexthops(fi)
|
| 1594 | }
|
| 1595 | return fi;
|
| 1596 |
|
| 1597 | err_inval:
|
| 1598 | err = -EINVAL;
|
| 1599 |
|
| 1600 | failure:
|
| 1601 | if (fi) {
|
| 1602 |
|
| 1603 | fi->fib_dead = 1;
|
| 1604 | free_fib_info(fi);
|
| 1605 | }
|
| 1606 |
|
| 1607 | return ERR_PTR(err);
|
| 1608 | }
|
| 1609 |
|
| 1610 | int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
|
| 1611 | u8 rt_family, unsigned char *flags, bool skip_oif)
|
| 1612 | {
|
| 1613 | if (nhc->nhc_flags & RTNH_F_DEAD)
|
| 1614 | *flags |= RTNH_F_DEAD;
|
| 1615 |
|
| 1616 | if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
|
| 1617 | *flags |= RTNH_F_LINKDOWN;
|
| 1618 |
|
| 1619 | rcu_read_lock();
|
| 1620 | switch (nhc->nhc_family) {
|
| 1621 | case AF_INET:
|
| 1622 | if (ip_ignore_linkdown(nhc->nhc_dev))
|
| 1623 | *flags |= RTNH_F_DEAD;
|
| 1624 | break;
|
| 1625 | case AF_INET6:
|
| 1626 | if (ip6_ignore_linkdown(nhc->nhc_dev))
|
| 1627 | *flags |= RTNH_F_DEAD;
|
| 1628 | break;
|
| 1629 | }
|
| 1630 | rcu_read_unlock();
|
| 1631 | }
|
| 1632 |
|
| 1633 | switch (nhc->nhc_gw_family) {
|
| 1634 | case AF_INET:
|
| 1635 | if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
|
| 1636 | goto nla_put_failure;
|
| 1637 | break;
|
| 1638 | case AF_INET6:
|
| 1639 | |
| 1640 | * gateway is encoded as RTA_VIA
|
| 1641 |
|
| 1642 | if (rt_family != nhc->nhc_gw_family) {
|
| 1643 | int alen = sizeof(struct in6_addr);
|
| 1644 | struct nlattr *nla;
|
| 1645 | struct rtvia *via;
|
| 1646 |
|
| 1647 | nla = nla_reserve(skb, RTA_VIA, alen + 2);
|
| 1648 | if (!nla)
|
| 1649 | goto nla_put_failure;
|
| 1650 |
|
| 1651 | via = nla_data(nla);
|
| 1652 | via->rtvia_family = AF_INET6;
|
| 1653 | memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
|
| 1654 | } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
|
| 1655 | &nhc->nhc_gw.ipv6) < 0) {
|
| 1656 | goto nla_put_failure;
|
| 1657 | }
|
| 1658 | break;
|
| 1659 | }
|
| 1660 |
|
| 1661 | *flags |= (nhc->nhc_flags &
|
| 1662 | (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
|
| 1663 |
|
| 1664 | if (!skip_oif && nhc->nhc_dev &&
|
| 1665 | nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
|
| 1666 | goto nla_put_failure;
|
| 1667 |
|
| 1668 | if (lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
|
| 1669 | RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
|
| 1670 | goto nla_put_failure;
|
| 1671 |
|
| 1672 | return 0;
|
| 1673 |
|
| 1674 | nla_put_failure:
|
| 1675 | return -EMSGSIZE;
|
| 1676 | }
|
| 1677 |
|
| 1678 | #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
|
| 1679 | int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
|
| 1680 | int nh_weight, u8 rt_family, u32 nh_tclassid)
|
| 1681 | {
|
| 1682 | const struct net_device *dev = nhc->nhc_dev;
|
| 1683 | struct rtnexthop *rtnh;
|
| 1684 | unsigned char flags = 0;
|
| 1685 |
|
| 1686 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
|
| 1687 | if (!rtnh)
|
| 1688 | goto nla_put_failure;
|
| 1689 |
|
| 1690 | rtnh->rtnh_hops = nh_weight - 1;
|
| 1691 | rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
|
| 1692 |
|
| 1693 | if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
|
| 1694 | goto nla_put_failure;
|
| 1695 |
|
| 1696 | rtnh->rtnh_flags = flags;
|
| 1697 |
|
| 1698 | if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
|
| 1699 | goto nla_put_failure;
|
| 1700 |
|
| 1701 |
|
| 1702 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
|
| 1703 |
|
| 1704 | return 0;
|
| 1705 |
|
| 1706 | nla_put_failure:
|
| 1707 | return -EMSGSIZE;
|
| 1708 | }
|
| 1709 | #endif
|
| 1710 |
|
| 1711 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 1712 | static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
|
| 1713 | {
|
| 1714 | struct nlattr *mp;
|
| 1715 |
|
| 1716 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
|
| 1717 | if (!mp)
|
| 1718 | goto nla_put_failure;
|
| 1719 |
|
| 1720 | if (unlikely(fi->nh)) {
|
| 1721 | if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
|
| 1722 | goto nla_put_failure;
|
| 1723 | goto mp_end;
|
| 1724 | }
|
| 1725 |
|
| 1726 | for_nexthops(fi) {
|
| 1727 | u32 nh_tclassid = 0;
|
| 1728 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 1729 | nh_tclassid = nh->nh_tclassid;
|
| 1730 | #endif
|
| 1731 | if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
|
| 1732 | AF_INET, nh_tclassid) < 0)
|
| 1733 | goto nla_put_failure;
|
| 1734 | } endfor_nexthops(fi);
|
| 1735 |
|
| 1736 | mp_end:
|
| 1737 | nla_nest_end(skb, mp);
|
| 1738 |
|
| 1739 | return 0;
|
| 1740 |
|
| 1741 | nla_put_failure:
|
| 1742 | return -EMSGSIZE;
|
| 1743 | }
|
| 1744 | #else
|
| 1745 | static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
|
| 1746 | {
|
| 1747 | return 0;
|
| 1748 | }
|
| 1749 | #endif
|
| 1750 |
|
| 1751 | int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
|
| 1752 | const struct fib_rt_info *fri, unsigned int flags)
|
| 1753 | {
|
| 1754 | unsigned int nhs = fib_info_num_path(fri->fi);
|
| 1755 | struct fib_info *fi = fri->fi;
|
| 1756 | u32 tb_id = fri->tb_id;
|
| 1757 | struct nlmsghdr *nlh;
|
| 1758 | struct rtmsg *rtm;
|
| 1759 |
|
| 1760 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
|
| 1761 | if (!nlh)
|
| 1762 | return -EMSGSIZE;
|
| 1763 |
|
| 1764 | rtm = nlmsg_data(nlh);
|
| 1765 | rtm->rtm_family = AF_INET;
|
| 1766 | rtm->rtm_dst_len = fri->dst_len;
|
| 1767 | rtm->rtm_src_len = 0;
|
| 1768 | rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
|
| 1769 | if (tb_id < 256)
|
| 1770 | rtm->rtm_table = tb_id;
|
| 1771 | else
|
| 1772 | rtm->rtm_table = RT_TABLE_COMPAT;
|
| 1773 | if (nla_put_u32(skb, RTA_TABLE, tb_id))
|
| 1774 | goto nla_put_failure;
|
| 1775 | rtm->rtm_type = fri->type;
|
| 1776 | rtm->rtm_flags = fi->fib_flags;
|
| 1777 | rtm->rtm_scope = fi->fib_scope;
|
| 1778 | rtm->rtm_protocol = fi->fib_protocol;
|
| 1779 |
|
| 1780 | if (rtm->rtm_dst_len &&
|
| 1781 | nla_put_in_addr(skb, RTA_DST, fri->dst))
|
| 1782 | goto nla_put_failure;
|
| 1783 | if (fi->fib_priority &&
|
| 1784 | nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
|
| 1785 | goto nla_put_failure;
|
| 1786 | if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
|
| 1787 | goto nla_put_failure;
|
| 1788 |
|
| 1789 | if (fi->fib_prefsrc &&
|
| 1790 | nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
|
| 1791 | goto nla_put_failure;
|
| 1792 |
|
| 1793 | if (fi->nh) {
|
| 1794 | if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
|
| 1795 | goto nla_put_failure;
|
| 1796 | if (nexthop_is_blackhole(fi->nh))
|
| 1797 | rtm->rtm_type = RTN_BLACKHOLE;
|
| 1798 | if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
|
| 1799 | goto offload;
|
| 1800 | }
|
| 1801 |
|
| 1802 | if (nhs == 1) {
|
| 1803 | const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
|
| 1804 | unsigned char flags = 0;
|
| 1805 |
|
| 1806 | if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
|
| 1807 | goto nla_put_failure;
|
| 1808 |
|
| 1809 | rtm->rtm_flags = flags;
|
| 1810 | #ifdef CONFIG_IP_ROUTE_CLASSID
|
| 1811 | if (nhc->nhc_family == AF_INET) {
|
| 1812 | struct fib_nh *nh;
|
| 1813 |
|
| 1814 | nh = container_of(nhc, struct fib_nh, nh_common);
|
| 1815 | if (nh->nh_tclassid &&
|
| 1816 | nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
|
| 1817 | goto nla_put_failure;
|
| 1818 | }
|
| 1819 | #endif
|
| 1820 | } else {
|
| 1821 | if (fib_add_multipath(skb, fi) < 0)
|
| 1822 | goto nla_put_failure;
|
| 1823 | }
|
| 1824 |
|
| 1825 | offload:
|
| 1826 | if (fri->offload)
|
| 1827 | rtm->rtm_flags |= RTM_F_OFFLOAD;
|
| 1828 | if (fri->trap)
|
| 1829 | rtm->rtm_flags |= RTM_F_TRAP;
|
| 1830 | if (fri->offload_failed)
|
| 1831 | rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
|
| 1832 |
|
| 1833 | nlmsg_end(skb, nlh);
|
| 1834 | return 0;
|
| 1835 |
|
| 1836 | nla_put_failure:
|
| 1837 | nlmsg_cancel(skb, nlh);
|
| 1838 | return -EMSGSIZE;
|
| 1839 | }
|
| 1840 |
|
| 1841 | |
| 1842 | * Update FIB if:
|
| 1843 | * - local address disappeared -> we must delete all the entries
|
| 1844 | * referring to it.
|
| 1845 | * - device went down -> we must shutdown all nexthops going via it.
|
| 1846 |
|
| 1847 | int fib_sync_down_addr(struct net_device *dev, __be32 local)
|
| 1848 | {
|
| 1849 | int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
|
| 1850 | struct net *net = dev_net(dev);
|
| 1851 | struct hlist_head *head;
|
| 1852 | struct fib_info *fi;
|
| 1853 | int ret = 0;
|
| 1854 |
|
| 1855 | if (!local)
|
| 1856 | return 0;
|
| 1857 |
|
| 1858 | head = fib_info_laddrhash_bucket(net, local);
|
| 1859 | hlist_for_each_entry(fi, head, fib_lhash) {
|
| 1860 | if (!net_eq(fi->fib_net, net) ||
|
| 1861 | fi->fib_tb_id != tb_id)
|
| 1862 | continue;
|
| 1863 | if (fi->fib_prefsrc == local) {
|
| 1864 | fi->fib_flags |= RTNH_F_DEAD;
|
| 1865 | fi->pfsrc_removed = true;
|
| 1866 | ret++;
|
| 1867 | }
|
| 1868 | }
|
| 1869 | return ret;
|
| 1870 | }
|
| 1871 |
|
| 1872 | static int call_fib_nh_notifiers(struct fib_nh *nh,
|
| 1873 | enum fib_event_type event_type)
|
| 1874 | {
|
| 1875 | bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
|
| 1876 | struct fib_nh_notifier_info info = {
|
| 1877 | .fib_nh = nh,
|
| 1878 | };
|
| 1879 |
|
| 1880 | switch (event_type) {
|
| 1881 | case FIB_EVENT_NH_ADD:
|
| 1882 | if (nh->fib_nh_flags & RTNH_F_DEAD)
|
| 1883 | break;
|
| 1884 | if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
|
| 1885 | break;
|
| 1886 | return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
|
| 1887 | &info.info);
|
| 1888 | case FIB_EVENT_NH_DEL:
|
| 1889 | if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
|
| 1890 | (nh->fib_nh_flags & RTNH_F_DEAD))
|
| 1891 | return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
|
| 1892 | event_type, &info.info);
|
| 1893 | break;
|
| 1894 | default:
|
| 1895 | break;
|
| 1896 | }
|
| 1897 |
|
| 1898 | return NOTIFY_DONE;
|
| 1899 | }
|
| 1900 |
|
| 1901 | |
| 1902 | * chain is RCU protected here, while fnhe_update_pmtu() takes fnhe_lock
|
| 1903 | * for the update of each entry.
|
| 1904 |
|
| 1905 | void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
|
| 1906 | {
|
| 1907 | struct fnhe_hash_bucket *bucket;
|
| 1908 | int i;
|
| 1909 |
|
| 1910 | rcu_read_lock();
|
| 1911 | bucket = rcu_dereference(nhc->nhc_exceptions);
|
| 1912 | if (!bucket)
|
| 1913 | goto out;
|
| 1914 |
|
| 1915 | for (i = 0; i < FNHE_HASH_SIZE; i++) {
|
| 1916 | struct fib_nh_exception *fnhe;
|
| 1917 |
|
| 1918 | for (fnhe = rcu_dereference(bucket[i].chain);
|
| 1919 | fnhe;
|
| 1920 | fnhe = rcu_dereference(fnhe->fnhe_next))
|
| 1921 | fnhe_update_pmtu(fnhe, new, orig);
|
| 1922 | }
|
| 1923 | out:
|
| 1924 | rcu_read_unlock();
|
| 1925 | }
|
| 1926 |
|
| 1927 | void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
|
| 1928 | {
|
| 1929 | struct hlist_head *head = fib_nh_head(dev);
|
| 1930 | struct fib_nh *nh;
|
| 1931 |
|
| 1932 | hlist_for_each_entry(nh, head, nh_hash) {
|
| 1933 | DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
|
| 1934 | fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
|
| 1935 | }
|
| 1936 | }
|
| 1937 |
|
| 1938 | |
| 1939 | * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
|
| 1940 | * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
|
| 1941 | * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
|
| 1942 | * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
|
| 1943 | *
|
| 1944 | * only used when fib_nh is built into fib_info
|
| 1945 |
|
| 1946 | int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
|
| 1947 | {
|
| 1948 | struct hlist_head *head = fib_nh_head(dev);
|
| 1949 | struct fib_info *prev_fi = NULL;
|
| 1950 | int scope = RT_SCOPE_NOWHERE;
|
| 1951 | struct fib_nh *nh;
|
| 1952 | int ret = 0;
|
| 1953 |
|
| 1954 | if (force)
|
| 1955 | scope = -1;
|
| 1956 |
|
| 1957 | hlist_for_each_entry(nh, head, nh_hash) {
|
| 1958 | struct fib_info *fi = nh->nh_parent;
|
| 1959 | int dead;
|
| 1960 |
|
| 1961 | BUG_ON(!fi->fib_nhs);
|
| 1962 | DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
|
| 1963 | if (fi == prev_fi)
|
| 1964 | continue;
|
| 1965 | prev_fi = fi;
|
| 1966 | dead = 0;
|
| 1967 | change_nexthops(fi) {
|
| 1968 | if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
|
| 1969 | dead++;
|
| 1970 | else if (nexthop_nh->fib_nh_dev == dev &&
|
| 1971 | nexthop_nh->fib_nh_scope != scope) {
|
| 1972 | switch (event) {
|
| 1973 | case NETDEV_DOWN:
|
| 1974 | case NETDEV_UNREGISTER:
|
| 1975 | nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
|
| 1976 | fallthrough;
|
| 1977 | case NETDEV_CHANGE:
|
| 1978 | nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
|
| 1979 | break;
|
| 1980 | }
|
| 1981 | call_fib_nh_notifiers(nexthop_nh,
|
| 1982 | FIB_EVENT_NH_DEL);
|
| 1983 | dead++;
|
| 1984 | }
|
| 1985 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 1986 | if (event == NETDEV_UNREGISTER &&
|
| 1987 | nexthop_nh->fib_nh_dev == dev) {
|
| 1988 | dead = fi->fib_nhs;
|
| 1989 | break;
|
| 1990 | }
|
| 1991 | #endif
|
| 1992 | } endfor_nexthops(fi)
|
| 1993 | if (dead == fi->fib_nhs) {
|
| 1994 | switch (event) {
|
| 1995 | case NETDEV_DOWN:
|
| 1996 | case NETDEV_UNREGISTER:
|
| 1997 | fi->fib_flags |= RTNH_F_DEAD;
|
| 1998 | fallthrough;
|
| 1999 | case NETDEV_CHANGE:
|
| 2000 | fi->fib_flags |= RTNH_F_LINKDOWN;
|
| 2001 | break;
|
| 2002 | }
|
| 2003 | ret++;
|
| 2004 | }
|
| 2005 |
|
| 2006 | fib_rebalance(fi);
|
| 2007 | }
|
| 2008 |
|
| 2009 | return ret;
|
| 2010 | }
|
| 2011 |
|
| 2012 |
|
| 2013 | static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
|
| 2014 | {
|
| 2015 | struct fib_info *fi = NULL, *last_resort = NULL;
|
| 2016 | struct hlist_head *fa_head = res->fa_head;
|
| 2017 | struct fib_table *tb = res->table;
|
| 2018 | u8 slen = 32 - res->prefixlen;
|
| 2019 | int order = -1, last_idx = -1;
|
| 2020 | struct fib_alias *fa, *fa1 = NULL;
|
| 2021 | u32 last_prio = res->fi->fib_priority;
|
| 2022 | dscp_t last_dscp = 0;
|
| 2023 |
|
| 2024 | hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
|
| 2025 | struct fib_info *next_fi = fa->fa_info;
|
| 2026 | struct fib_nh_common *nhc;
|
| 2027 |
|
| 2028 | if (fa->fa_slen != slen)
|
| 2029 | continue;
|
| 2030 | if (fa->fa_dscp && !fib_dscp_masked_match(fa->fa_dscp, flp))
|
| 2031 | continue;
|
| 2032 | if (fa->tb_id != tb->tb_id)
|
| 2033 | continue;
|
| 2034 | if (next_fi->fib_priority > last_prio &&
|
| 2035 | fa->fa_dscp == last_dscp) {
|
| 2036 | if (last_dscp)
|
| 2037 | continue;
|
| 2038 | break;
|
| 2039 | }
|
| 2040 | if (next_fi->fib_flags & RTNH_F_DEAD)
|
| 2041 | continue;
|
| 2042 | last_dscp = fa->fa_dscp;
|
| 2043 | last_prio = next_fi->fib_priority;
|
| 2044 |
|
| 2045 | if (next_fi->fib_scope != res->scope ||
|
| 2046 | fa->fa_type != RTN_UNICAST)
|
| 2047 | continue;
|
| 2048 |
|
| 2049 | nhc = fib_info_nhc(next_fi, 0);
|
| 2050 | if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
|
| 2051 | continue;
|
| 2052 |
|
| 2053 | fib_alias_accessed(fa);
|
| 2054 |
|
| 2055 | if (!fi) {
|
| 2056 | if (next_fi != res->fi)
|
| 2057 | break;
|
| 2058 | fa1 = fa;
|
| 2059 | } else if (!fib_detect_death(fi, order, &last_resort,
|
| 2060 | &last_idx, fa1->fa_default)) {
|
| 2061 | fib_result_assign(res, fi);
|
| 2062 | fa1->fa_default = order;
|
| 2063 | goto out;
|
| 2064 | }
|
| 2065 | fi = next_fi;
|
| 2066 | order++;
|
| 2067 | }
|
| 2068 |
|
| 2069 | if (order <= 0 || !fi) {
|
| 2070 | if (fa1)
|
| 2071 | fa1->fa_default = -1;
|
| 2072 | goto out;
|
| 2073 | }
|
| 2074 |
|
| 2075 | if (!fib_detect_death(fi, order, &last_resort, &last_idx,
|
| 2076 | fa1->fa_default)) {
|
| 2077 | fib_result_assign(res, fi);
|
| 2078 | fa1->fa_default = order;
|
| 2079 | goto out;
|
| 2080 | }
|
| 2081 |
|
| 2082 | if (last_idx >= 0)
|
| 2083 | fib_result_assign(res, last_resort);
|
| 2084 | fa1->fa_default = last_idx;
|
| 2085 | out:
|
| 2086 | return;
|
| 2087 | }
|
| 2088 |
|
| 2089 | |
| 2090 | * Dead device goes up. We wake up dead nexthops.
|
| 2091 | * It takes sense only on multipath routes.
|
| 2092 | *
|
| 2093 | * only used when fib_nh is built into fib_info
|
| 2094 |
|
| 2095 | int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
|
| 2096 | {
|
| 2097 | struct fib_info *prev_fi;
|
| 2098 | struct hlist_head *head;
|
| 2099 | struct fib_nh *nh;
|
| 2100 | int ret;
|
| 2101 |
|
| 2102 | if (!(dev->flags & IFF_UP))
|
| 2103 | return 0;
|
| 2104 |
|
| 2105 | if (nh_flags & RTNH_F_DEAD) {
|
| 2106 | unsigned int flags = netif_get_flags(dev);
|
| 2107 |
|
| 2108 | if (flags & (IFF_RUNNING | IFF_LOWER_UP))
|
| 2109 | nh_flags |= RTNH_F_LINKDOWN;
|
| 2110 | }
|
| 2111 |
|
| 2112 | prev_fi = NULL;
|
| 2113 | head = fib_nh_head(dev);
|
| 2114 | ret = 0;
|
| 2115 |
|
| 2116 | hlist_for_each_entry(nh, head, nh_hash) {
|
| 2117 | struct fib_info *fi = nh->nh_parent;
|
| 2118 | int alive;
|
| 2119 |
|
| 2120 | BUG_ON(!fi->fib_nhs);
|
| 2121 | DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
|
| 2122 | if (fi == prev_fi)
|
| 2123 | continue;
|
| 2124 |
|
| 2125 | prev_fi = fi;
|
| 2126 | alive = 0;
|
| 2127 | change_nexthops(fi) {
|
| 2128 | if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
|
| 2129 | alive++;
|
| 2130 | continue;
|
| 2131 | }
|
| 2132 | if (!nexthop_nh->fib_nh_dev ||
|
| 2133 | !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
|
| 2134 | continue;
|
| 2135 | if (nexthop_nh->fib_nh_dev != dev ||
|
| 2136 | !__in_dev_get_rtnl(dev))
|
| 2137 | continue;
|
| 2138 | alive++;
|
| 2139 | nexthop_nh->fib_nh_flags &= ~nh_flags;
|
| 2140 | call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
|
| 2141 | } endfor_nexthops(fi)
|
| 2142 |
|
| 2143 | if (alive > 0) {
|
| 2144 | fi->fib_flags &= ~nh_flags;
|
| 2145 | ret++;
|
| 2146 | }
|
| 2147 |
|
| 2148 | fib_rebalance(fi);
|
| 2149 | }
|
| 2150 |
|
| 2151 | return ret;
|
| 2152 | }
|
| 2153 |
|
| 2154 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 2155 | static bool fib_good_nh(const struct fib_nh *nh)
|
| 2156 | {
|
| 2157 | int state = NUD_REACHABLE;
|
| 2158 |
|
| 2159 | if (nh->fib_nh_scope == RT_SCOPE_LINK) {
|
| 2160 | struct neighbour *n;
|
| 2161 |
|
| 2162 | rcu_read_lock();
|
| 2163 |
|
| 2164 | if (likely(nh->fib_nh_gw_family == AF_INET))
|
| 2165 | n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
|
| 2166 | (__force u32)nh->fib_nh_gw4);
|
| 2167 | else if (IS_ENABLED(CONFIG_IPV6) &&
|
| 2168 | nh->fib_nh_gw_family == AF_INET6)
|
| 2169 | n = __ipv6_neigh_lookup_noref(nh->fib_nh_dev,
|
| 2170 | &nh->fib_nh_gw6);
|
| 2171 | else
|
| 2172 | n = NULL;
|
| 2173 | if (n)
|
| 2174 | state = READ_ONCE(n->nud_state);
|
| 2175 |
|
| 2176 | rcu_read_unlock();
|
| 2177 | }
|
| 2178 |
|
| 2179 | return !!(state & NUD_VALID);
|
| 2180 | }
|
| 2181 |
|
| 2182 | static __be32 fib_nh_saddr(struct net *net, const struct fib_info *fi,
|
| 2183 | struct fib_nh *nh, int genid)
|
| 2184 | {
|
| 2185 | if (READ_ONCE(nh->nh_saddr_genid) == genid)
|
| 2186 | return READ_ONCE(nh->nh_saddr);
|
| 2187 |
|
| 2188 | return fib_info_update_nhc_saddr(net, &nh->nh_common, fi->fib_scope);
|
| 2189 | }
|
| 2190 |
|
| 2191 | void fib_select_multipath(struct fib_result *res, int hash,
|
| 2192 | const struct flowi4 *fl4)
|
| 2193 | {
|
| 2194 | struct fib_info *fi = res->fi;
|
| 2195 | struct net *net = fi->fib_net;
|
| 2196 | bool use_neigh;
|
| 2197 | int score = -1;
|
| 2198 | __be32 saddr;
|
| 2199 | int genid;
|
| 2200 |
|
| 2201 | if (unlikely(res->fi->nh)) {
|
| 2202 | nexthop_path_fib_result(res, hash);
|
| 2203 | return;
|
| 2204 | }
|
| 2205 |
|
| 2206 | use_neigh = READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh);
|
| 2207 | saddr = fl4 ? fl4->saddr : 0;
|
| 2208 | genid = saddr ? atomic_read(&net->ipv4.dev_addr_genid) : 0;
|
| 2209 |
|
| 2210 | change_nexthops(fi) {
|
| 2211 | int nh_upper_bound, nh_score = 0;
|
| 2212 |
|
| 2213 | |
| 2214 | * minus one when "ignore_routes_with_linkdown" is set.
|
| 2215 |
|
| 2216 | nh_upper_bound = atomic_read(&nexthop_nh->fib_nh_upper_bound);
|
| 2217 | if (nh_upper_bound == -1 ||
|
| 2218 | (use_neigh && !fib_good_nh(nexthop_nh)))
|
| 2219 | continue;
|
| 2220 |
|
| 2221 | if (saddr && fib_nh_saddr(net, fi, nexthop_nh, genid) == saddr)
|
| 2222 | nh_score += 2;
|
| 2223 | if (hash <= nh_upper_bound)
|
| 2224 | nh_score++;
|
| 2225 | if (score < nh_score) {
|
| 2226 | res->nh_sel = nhsel;
|
| 2227 | res->nhc = &nexthop_nh->nh_common;
|
| 2228 | if (nh_score == 3 || (!saddr && nh_score == 1))
|
| 2229 | return;
|
| 2230 | score = nh_score;
|
| 2231 | }
|
| 2232 |
|
| 2233 | } endfor_nexthops(fi);
|
| 2234 | }
|
| 2235 | #endif
|
| 2236 |
|
| 2237 | void fib_select_path(struct net *net, struct fib_result *res,
|
| 2238 | struct flowi4 *fl4, const struct sk_buff *skb)
|
| 2239 | {
|
| 2240 | if (fl4->flowi4_oif)
|
| 2241 | goto check_saddr;
|
| 2242 |
|
| 2243 | #ifdef CONFIG_IP_ROUTE_MULTIPATH
|
| 2244 | if (fib_info_num_path(res->fi) > 1) {
|
| 2245 | int h = fib_multipath_hash(net, fl4, skb, NULL);
|
| 2246 |
|
| 2247 | fib_select_multipath(res, h, fl4);
|
| 2248 | }
|
| 2249 | else
|
| 2250 | #endif
|
| 2251 | if (!res->prefixlen &&
|
| 2252 | res->table->tb_num_default > 1 &&
|
| 2253 | res->type == RTN_UNICAST)
|
| 2254 | fib_select_default(fl4, res);
|
| 2255 |
|
| 2256 | check_saddr:
|
| 2257 | if (!fl4->saddr) {
|
| 2258 | struct net_device *l3mdev;
|
| 2259 |
|
| 2260 | l3mdev = dev_get_by_index_rcu(net, fl4->flowi4_l3mdev);
|
| 2261 |
|
| 2262 | if (!l3mdev ||
|
| 2263 | l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) == l3mdev)
|
| 2264 | fl4->saddr = fib_result_prefsrc(net, res);
|
| 2265 | else
|
| 2266 | fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK);
|
| 2267 | }
|
| 2268 | }
|
| 2269 |
|
| 2270 | int __net_init fib4_semantics_init(struct net *net)
|
| 2271 | {
|
| 2272 | unsigned int hash_bits = 4;
|
| 2273 |
|
| 2274 | net->ipv4.fib_info_hash = fib_info_hash_alloc(hash_bits);
|
| 2275 | if (!net->ipv4.fib_info_hash)
|
| 2276 | return -ENOMEM;
|
| 2277 |
|
| 2278 | net->ipv4.fib_info_hash_bits = hash_bits;
|
| 2279 | net->ipv4.fib_info_cnt = 0;
|
| 2280 |
|
| 2281 | return 0;
|
| 2282 | }
|
| 2283 |
|
| 2284 | void __net_exit fib4_semantics_exit(struct net *net)
|
| 2285 | {
|
| 2286 | fib_info_hash_free(net->ipv4.fib_info_hash);
|
| 2287 | }
|