1ab84be7eSDavid Ahern /* SPDX-License-Identifier: GPL-2.0 */ 2ab84be7eSDavid Ahern /* 3ab84be7eSDavid Ahern * Generic nexthop implementation 4ab84be7eSDavid Ahern * 5ab84be7eSDavid Ahern * Copyright (c) 2017-19 Cumulus Networks 6ab84be7eSDavid Ahern * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> 7ab84be7eSDavid Ahern */ 8ab84be7eSDavid Ahern 9ab84be7eSDavid Ahern #ifndef __LINUX_NEXTHOP_H 10ab84be7eSDavid Ahern #define __LINUX_NEXTHOP_H 11ab84be7eSDavid Ahern 12ab84be7eSDavid Ahern #include <linux/netdevice.h> 13ab84be7eSDavid Ahern #include <linux/types.h> 14ab84be7eSDavid Ahern #include <net/ip_fib.h> 1553010f99SDavid Ahern #include <net/ip6_fib.h> 16ab84be7eSDavid Ahern #include <net/netlink.h> 17ab84be7eSDavid Ahern 18ab84be7eSDavid Ahern #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK 19ab84be7eSDavid Ahern 20ab84be7eSDavid Ahern struct nexthop; 21ab84be7eSDavid Ahern 22ab84be7eSDavid Ahern struct nh_config { 23ab84be7eSDavid Ahern u32 nh_id; 24ab84be7eSDavid Ahern 25ab84be7eSDavid Ahern u8 nh_family; 26ab84be7eSDavid Ahern u8 nh_protocol; 27ab84be7eSDavid Ahern u8 nh_blackhole; 28ab84be7eSDavid Ahern u32 nh_flags; 29ab84be7eSDavid Ahern 30ab84be7eSDavid Ahern int nh_ifindex; 31ab84be7eSDavid Ahern struct net_device *dev; 32ab84be7eSDavid Ahern 33597cfe4fSDavid Ahern union { 34597cfe4fSDavid Ahern __be32 ipv4; 3553010f99SDavid Ahern struct in6_addr ipv6; 36597cfe4fSDavid Ahern } gw; 37597cfe4fSDavid Ahern 38430a0491SDavid Ahern struct nlattr *nh_grp; 39430a0491SDavid Ahern u16 nh_grp_type; 40430a0491SDavid Ahern 41b513bd03SDavid Ahern struct nlattr *nh_encap; 42b513bd03SDavid Ahern u16 nh_encap_type; 43b513bd03SDavid Ahern 44ab84be7eSDavid Ahern u32 nlflags; 45ab84be7eSDavid Ahern struct nl_info nlinfo; 46ab84be7eSDavid Ahern }; 47ab84be7eSDavid Ahern 48ab84be7eSDavid Ahern struct nh_info { 49ab84be7eSDavid Ahern struct hlist_node dev_hash; /* entry on netns devhash */ 50ab84be7eSDavid Ahern struct nexthop *nh_parent; 51ab84be7eSDavid Ahern 52ab84be7eSDavid Ahern u8 family; 53ab84be7eSDavid Ahern bool reject_nh; 54ab84be7eSDavid Ahern 55ab84be7eSDavid Ahern union { 56ab84be7eSDavid Ahern struct fib_nh_common fib_nhc; 57597cfe4fSDavid Ahern struct fib_nh fib_nh; 5853010f99SDavid Ahern struct fib6_nh fib6_nh; 59ab84be7eSDavid Ahern }; 60ab84be7eSDavid Ahern }; 61ab84be7eSDavid Ahern 62430a0491SDavid Ahern struct nh_grp_entry { 63430a0491SDavid Ahern struct nexthop *nh; 64430a0491SDavid Ahern u8 weight; 65430a0491SDavid Ahern atomic_t upper_bound; 66430a0491SDavid Ahern 67430a0491SDavid Ahern struct list_head nh_list; 68430a0491SDavid Ahern struct nexthop *nh_parent; /* nexthop of group with this entry */ 69430a0491SDavid Ahern }; 70430a0491SDavid Ahern 71430a0491SDavid Ahern struct nh_group { 72430a0491SDavid Ahern u16 num_nh; 73430a0491SDavid Ahern bool mpath; 74430a0491SDavid Ahern bool has_v4; 75430a0491SDavid Ahern struct nh_grp_entry nh_entries[0]; 76430a0491SDavid Ahern }; 77430a0491SDavid Ahern 78ab84be7eSDavid Ahern struct nexthop { 79ab84be7eSDavid Ahern struct rb_node rb_node; /* entry on netns rbtree */ 80*4c7e8084SDavid Ahern struct list_head fi_list; /* v4 entries using nh */ 81430a0491SDavid Ahern struct list_head grp_list; /* nh group entries using this nh */ 82ab84be7eSDavid Ahern struct net *net; 83ab84be7eSDavid Ahern 84ab84be7eSDavid Ahern u32 id; 85ab84be7eSDavid Ahern 86ab84be7eSDavid Ahern u8 protocol; /* app managing this nh */ 87ab84be7eSDavid Ahern u8 nh_flags; 88430a0491SDavid Ahern bool is_group; 89ab84be7eSDavid Ahern 90ab84be7eSDavid Ahern refcount_t refcnt; 91ab84be7eSDavid Ahern struct rcu_head rcu; 92ab84be7eSDavid Ahern 93ab84be7eSDavid Ahern union { 94ab84be7eSDavid Ahern struct nh_info __rcu *nh_info; 95430a0491SDavid Ahern struct nh_group __rcu *nh_grp; 96ab84be7eSDavid Ahern }; 97ab84be7eSDavid Ahern }; 98ab84be7eSDavid Ahern 99ab84be7eSDavid Ahern /* caller is holding rcu or rtnl; no reference taken to nexthop */ 100ab84be7eSDavid Ahern struct nexthop *nexthop_find_by_id(struct net *net, u32 id); 101ab84be7eSDavid Ahern void nexthop_free_rcu(struct rcu_head *head); 102ab84be7eSDavid Ahern 103ab84be7eSDavid Ahern static inline bool nexthop_get(struct nexthop *nh) 104ab84be7eSDavid Ahern { 105ab84be7eSDavid Ahern return refcount_inc_not_zero(&nh->refcnt); 106ab84be7eSDavid Ahern } 107ab84be7eSDavid Ahern 108ab84be7eSDavid Ahern static inline void nexthop_put(struct nexthop *nh) 109ab84be7eSDavid Ahern { 110ab84be7eSDavid Ahern if (refcount_dec_and_test(&nh->refcnt)) 111ab84be7eSDavid Ahern call_rcu(&nh->rcu, nexthop_free_rcu); 112ab84be7eSDavid Ahern } 113ab84be7eSDavid Ahern 114*4c7e8084SDavid Ahern static inline bool nexthop_cmp(const struct nexthop *nh1, 115*4c7e8084SDavid Ahern const struct nexthop *nh2) 116*4c7e8084SDavid Ahern { 117*4c7e8084SDavid Ahern return nh1 == nh2; 118*4c7e8084SDavid Ahern } 119*4c7e8084SDavid Ahern 120430a0491SDavid Ahern static inline bool nexthop_is_multipath(const struct nexthop *nh) 121430a0491SDavid Ahern { 122430a0491SDavid Ahern if (nh->is_group) { 123430a0491SDavid Ahern struct nh_group *nh_grp; 124430a0491SDavid Ahern 125430a0491SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp); 126430a0491SDavid Ahern return nh_grp->mpath; 127430a0491SDavid Ahern } 128430a0491SDavid Ahern return false; 129430a0491SDavid Ahern } 130430a0491SDavid Ahern 131430a0491SDavid Ahern struct nexthop *nexthop_select_path(struct nexthop *nh, int hash); 132430a0491SDavid Ahern 133430a0491SDavid Ahern static inline unsigned int nexthop_num_path(const struct nexthop *nh) 134430a0491SDavid Ahern { 135430a0491SDavid Ahern unsigned int rc = 1; 136430a0491SDavid Ahern 137430a0491SDavid Ahern if (nexthop_is_multipath(nh)) { 138430a0491SDavid Ahern struct nh_group *nh_grp; 139430a0491SDavid Ahern 140430a0491SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp); 141430a0491SDavid Ahern rc = nh_grp->num_nh; 142430a0491SDavid Ahern } else { 143430a0491SDavid Ahern const struct nh_info *nhi; 144430a0491SDavid Ahern 145430a0491SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info); 146430a0491SDavid Ahern if (nhi->reject_nh) 147430a0491SDavid Ahern rc = 0; 148430a0491SDavid Ahern } 149430a0491SDavid Ahern 150430a0491SDavid Ahern return rc; 151430a0491SDavid Ahern } 152430a0491SDavid Ahern 153430a0491SDavid Ahern static inline 154430a0491SDavid Ahern struct nexthop *nexthop_mpath_select(const struct nexthop *nh, int nhsel) 155430a0491SDavid Ahern { 156430a0491SDavid Ahern const struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 157430a0491SDavid Ahern 158430a0491SDavid Ahern /* for_nexthops macros in fib_semantics.c grabs a pointer to 159430a0491SDavid Ahern * the nexthop before checking nhsel 160430a0491SDavid Ahern */ 161430a0491SDavid Ahern if (nhsel > nhg->num_nh) 162430a0491SDavid Ahern return NULL; 163430a0491SDavid Ahern 164430a0491SDavid Ahern return nhg->nh_entries[nhsel].nh; 165430a0491SDavid Ahern } 166430a0491SDavid Ahern 167430a0491SDavid Ahern static inline 168430a0491SDavid Ahern int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh) 169430a0491SDavid Ahern { 170430a0491SDavid Ahern struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 171430a0491SDavid Ahern int i; 172430a0491SDavid Ahern 173430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; i++) { 174430a0491SDavid Ahern struct nexthop *nhe = nhg->nh_entries[i].nh; 175430a0491SDavid Ahern struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info); 176430a0491SDavid Ahern struct fib_nh_common *nhc = &nhi->fib_nhc; 177430a0491SDavid Ahern int weight = nhg->nh_entries[i].weight; 178430a0491SDavid Ahern 179430a0491SDavid Ahern if (fib_add_nexthop(skb, nhc, weight) < 0) 180430a0491SDavid Ahern return -EMSGSIZE; 181430a0491SDavid Ahern } 182430a0491SDavid Ahern 183430a0491SDavid Ahern return 0; 184430a0491SDavid Ahern } 185430a0491SDavid Ahern 186ab84be7eSDavid Ahern /* called with rcu lock */ 187ab84be7eSDavid Ahern static inline bool nexthop_is_blackhole(const struct nexthop *nh) 188ab84be7eSDavid Ahern { 189ab84be7eSDavid Ahern const struct nh_info *nhi; 190ab84be7eSDavid Ahern 191430a0491SDavid Ahern if (nexthop_is_multipath(nh)) { 192430a0491SDavid Ahern if (nexthop_num_path(nh) > 1) 193430a0491SDavid Ahern return false; 194430a0491SDavid Ahern nh = nexthop_mpath_select(nh, 0); 195430a0491SDavid Ahern if (!nh) 196430a0491SDavid Ahern return false; 197430a0491SDavid Ahern } 198430a0491SDavid Ahern 199430a0491SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info); 200ab84be7eSDavid Ahern return nhi->reject_nh; 201ab84be7eSDavid Ahern } 2025481d73fSDavid Ahern 203*4c7e8084SDavid Ahern static inline void nexthop_path_fib_result(struct fib_result *res, int hash) 204*4c7e8084SDavid Ahern { 205*4c7e8084SDavid Ahern struct nh_info *nhi; 206*4c7e8084SDavid Ahern struct nexthop *nh; 207*4c7e8084SDavid Ahern 208*4c7e8084SDavid Ahern nh = nexthop_select_path(res->fi->nh, hash); 209*4c7e8084SDavid Ahern nhi = rcu_dereference(nh->nh_info); 210*4c7e8084SDavid Ahern res->nhc = &nhi->fib_nhc; 211*4c7e8084SDavid Ahern } 212*4c7e8084SDavid Ahern 213*4c7e8084SDavid Ahern /* called with rcu read lock or rtnl held */ 214*4c7e8084SDavid Ahern static inline 215*4c7e8084SDavid Ahern struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel) 216*4c7e8084SDavid Ahern { 217*4c7e8084SDavid Ahern struct nh_info *nhi; 218*4c7e8084SDavid Ahern 219*4c7e8084SDavid Ahern BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0); 220*4c7e8084SDavid Ahern BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0); 221*4c7e8084SDavid Ahern 222*4c7e8084SDavid Ahern if (nexthop_is_multipath(nh)) { 223*4c7e8084SDavid Ahern nh = nexthop_mpath_select(nh, nhsel); 224*4c7e8084SDavid Ahern if (!nh) 225*4c7e8084SDavid Ahern return NULL; 226*4c7e8084SDavid Ahern } 227*4c7e8084SDavid Ahern 228*4c7e8084SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info); 229*4c7e8084SDavid Ahern return &nhi->fib_nhc; 230*4c7e8084SDavid Ahern } 231*4c7e8084SDavid Ahern 2325481d73fSDavid Ahern static inline unsigned int fib_info_num_path(const struct fib_info *fi) 2335481d73fSDavid Ahern { 234*4c7e8084SDavid Ahern if (unlikely(fi->nh)) 235*4c7e8084SDavid Ahern return nexthop_num_path(fi->nh); 236*4c7e8084SDavid Ahern 2375481d73fSDavid Ahern return fi->fib_nhs; 2385481d73fSDavid Ahern } 2395481d73fSDavid Ahern 240*4c7e8084SDavid Ahern int fib_check_nexthop(struct nexthop *nh, u8 scope, 241*4c7e8084SDavid Ahern struct netlink_ext_ack *extack); 242*4c7e8084SDavid Ahern 2435481d73fSDavid Ahern static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel) 2445481d73fSDavid Ahern { 245*4c7e8084SDavid Ahern if (unlikely(fi->nh)) 246*4c7e8084SDavid Ahern return nexthop_fib_nhc(fi->nh, nhsel); 247*4c7e8084SDavid Ahern 2485481d73fSDavid Ahern return &fi->fib_nh[nhsel].nh_common; 2495481d73fSDavid Ahern } 2505481d73fSDavid Ahern 251*4c7e8084SDavid Ahern /* only used when fib_nh is built into fib_info */ 2525481d73fSDavid Ahern static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel) 2535481d73fSDavid Ahern { 254*4c7e8084SDavid Ahern WARN_ON(fi->nh); 255*4c7e8084SDavid Ahern 2565481d73fSDavid Ahern return &fi->fib_nh[nhsel]; 2575481d73fSDavid Ahern } 258ab84be7eSDavid Ahern #endif 259