1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Alexander V. Chernikov 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * This header file contains public definitions for the nexthop routing subsystem. 32 */ 33 34 #ifndef _NET_ROUTE_NHOP_H_ 35 #define _NET_ROUTE_NHOP_H_ 36 37 #include <netinet/in.h> /* sockaddr_in && sockaddr_in6 */ 38 39 #include <sys/counter.h> 40 41 enum nhop_type { 42 NH_TYPE_IPV4_ETHER_RSLV = 1, /* IPv4 ethernet without GW */ 43 NH_TYPE_IPV4_ETHER_NHOP = 2, /* IPv4 with pre-calculated ethernet encap */ 44 NH_TYPE_IPV6_ETHER_RSLV = 3, /* IPv6 ethernet, without GW */ 45 NH_TYPE_IPV6_ETHER_NHOP = 4 /* IPv6 with pre-calculated ethernet encap*/ 46 }; 47 48 #ifdef _KERNEL 49 50 /* 51 * Define shorter version of AF_LINK sockaddr. 52 * 53 * Currently the only use case of AF_LINK gateway is storing 54 * interface index of the interface of the source IPv6 address. 55 * This is used by the IPv6 code for the connections over loopback 56 * interface. 57 * 58 * The structure below copies 'struct sockaddr_dl', reducing the 59 * size of sdl_data buffer, as it is not used. This change 60 * allows to store the AF_LINK gateways in the nhop gateway itself, 61 * simplifying control plane handling. 62 */ 63 struct sockaddr_dl_short { 64 u_char sdl_len; /* Total length of sockaddr */ 65 u_char sdl_family; /* AF_LINK */ 66 u_short sdl_index; /* if != 0, system given index for interface */ 67 u_char sdl_type; /* interface type */ 68 u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */ 69 u_char sdl_alen; /* link level address length */ 70 u_char sdl_slen; /* link layer selector length */ 71 char sdl_data[8]; /* unused */ 72 }; 73 74 #define NHOP_RELATED_FLAGS \ 75 (RTF_GATEWAY | RTF_HOST | RTF_REJECT | RTF_BLACKHOLE | \ 76 RTF_FIXEDMTU | RTF_LOCAL | RTF_BROADCAST | RTF_MULTICAST) 77 78 struct nh_control; 79 struct nhop_priv; 80 81 /* 82 * Struct 'nhop_object' field description: 83 * 84 * nh_flags: NHF_ flags used in the dataplane code. NHF_GATEWAY or NHF_BLACKHOLE 85 * can be examples of such flags. 86 * nh_mtu: ready-to-use nexthop mtu. Already accounts for the link-level header, 87 * interface MTU and protocol-specific limitations. 88 * nh_prepend_len: link-level prepend length. Currently unused. 89 * nh_ifp: logical transmit interface. The one from which if_transmit() will be 90 * called. Guaranteed to be non-NULL. 91 * nh_aifp: ifnet of the source address. Same as nh_ifp except IPv6 loopback 92 * routes. See the example below. 93 * nh_ifa: interface address to use. Guaranteed to be non-NULL. 94 * nh_pksent: counter(9) reflecting the number of packets transmitted. 95 * 96 * gw_: storage suitable to hold AF_INET, AF_INET6 or AF_LINK gateway. More 97 * details ara available in the examples below. 98 * 99 * Examples: 100 * 101 * Direct routes (routes w/o gateway): 102 * NHF_GATEWAY is NOT set. 103 * nh_ifp denotes the logical transmit interface (). 104 * nh_aifp is the same as nh_ifp 105 * gw_sa contains AF_LINK sa with nh_aifp ifindex (compat) 106 * Loopback routes: 107 * NHF_GATEWAY is NOT set. 108 * nh_ifp points to the loopback interface (lo0). 109 * nh_aifp points to the interface where the destination address belongs to. 110 * This is useful in IPv6 link-local-over-loopback communications. 111 * gw_sa contains AF_LINK sa with nh_aifp ifindex (compat) 112 * GW routes: 113 * NHF_GATEWAY is set. 114 * nh_ifp denotes the logical transmit interface. 115 * nh_aifp is the same as nh_ifp 116 * gw_sa contains L3 address (either AF_INET or AF_INET6). 117 * 118 * 119 * Note: struct nhop_object fields are ordered in a way that 120 * supports memcmp-based comparisons. 121 * 122 */ 123 #define NHOP_END_CMP (__offsetof(struct nhop_object, nh_pksent)) 124 125 struct nhop_object { 126 uint16_t nh_flags; /* nhop flags */ 127 uint16_t nh_mtu; /* nexthop mtu */ 128 union { 129 struct sockaddr_in gw4_sa; /* GW accessor as IPv4 */ 130 struct sockaddr_in6 gw6_sa; /* GW accessor as IPv6 */ 131 struct sockaddr gw_sa; 132 struct sockaddr_dl_short gwl_sa; /* AF_LINK gw (compat) */ 133 char gw_buf[28]; 134 }; 135 struct ifnet *nh_ifp; /* Logical egress interface. Always != NULL */ 136 struct ifaddr *nh_ifa; /* interface address to use. Always != NULL */ 137 struct ifnet *nh_aifp; /* ifnet of the source address. Always != NULL */ 138 counter_u64_t nh_pksent; /* packets sent using this nhop */ 139 /* 32 bytes + 4xPTR == 64(amd64) / 48(i386) */ 140 uint8_t nh_prepend_len; /* length of prepend data */ 141 uint8_t spare[3]; 142 uint32_t spare1; /* alignment */ 143 char nh_prepend[48]; /* L2 prepend */ 144 struct nhop_priv *nh_priv; /* control plane data */ 145 /* -- 128 bytes -- */ 146 }; 147 148 /* 149 * Nhop validness. 150 * 151 * Currently we verify whether link is up or not on every packet, which can be 152 * quite costy. 153 * TODO: subscribe for the interface notifications and update the nexthops 154 * with NHF_INVALID flag. 155 */ 156 157 #define NH_IS_VALID(_nh) RT_LINK_IS_UP((_nh)->nh_ifp) 158 #define NH_IS_NHGRP(_nh) ((_nh)->nh_flags & NHF_MULTIPATH) 159 160 #define NH_FREE(_nh) do { \ 161 nhop_free(_nh); \ 162 /* guard against invalid refs */ \ 163 _nh = NULL; \ 164 } while (0) 165 166 struct weightened_nhop { 167 struct nhop_object *nh; 168 uint32_t weight; 169 uint32_t storage; 170 }; 171 172 void nhop_free(struct nhop_object *nh); 173 174 struct sysctl_req; 175 struct sockaddr_dl; 176 struct rib_head; 177 178 /* flags that can be set using nhop_set_rtflags() */ 179 #define RT_SET_RTFLAGS_MASK (RTF_PROTO1 | RTF_PROTO2 | RTF_PROTO3 | RTF_STATIC) 180 #define RT_CHANGE_RTFLAGS_MASK RT_SET_RTFLAGS_MASK 181 182 struct nhop_object *nhop_alloc(uint32_t fibnum, int family); 183 void nhop_copy(struct nhop_object *nh, const struct nhop_object *nh_orig); 184 struct nhop_object *nhop_get_nhop(struct nhop_object *nh, int *perror); 185 186 void nhop_set_direct_gw(struct nhop_object *nh, struct ifnet *ifp); 187 bool nhop_set_gw(struct nhop_object *nh, const struct sockaddr *sa, bool is_gw); 188 189 190 void nhop_set_mtu(struct nhop_object *nh, uint32_t mtu, bool from_user); 191 void nhop_set_rtflags(struct nhop_object *nh, int rt_flags); 192 void nhop_set_pxtype_flag(struct nhop_object *nh, int nh_flag); 193 void nhop_set_broadcast(struct nhop_object *nh, bool is_broadcast); 194 void nhop_set_blackhole(struct nhop_object *nh, int blackhole_rt_flag); 195 void nhop_set_pinned(struct nhop_object *nh, bool is_pinned); 196 void nhop_set_redirect(struct nhop_object *nh, bool is_redirect); 197 void nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type); 198 void nhop_set_src(struct nhop_object *nh, struct ifaddr *ifa); 199 void nhop_set_transmit_ifp(struct nhop_object *nh, struct ifnet *ifp); 200 201 uint32_t nhop_get_idx(const struct nhop_object *nh); 202 enum nhop_type nhop_get_type(const struct nhop_object *nh); 203 int nhop_get_rtflags(const struct nhop_object *nh); 204 struct vnet *nhop_get_vnet(const struct nhop_object *nh); 205 struct nhop_object *nhop_select_func(struct nhop_object *nh, uint32_t flowid); 206 int nhop_get_upper_family(const struct nhop_object *nh); 207 int nhop_get_neigh_family(const struct nhop_object *nh); 208 uint32_t nhop_get_fibnum(const struct nhop_object *nh); 209 void nhop_set_fibnum(struct nhop_object *nh, uint32_t fibnum); 210 uint32_t nhop_get_expire(const struct nhop_object *nh); 211 void nhop_set_expire(struct nhop_object *nh, uint32_t expire); 212 213 #endif /* _KERNEL */ 214 215 /* Kernel <> userland structures */ 216 217 /* Structure usage and layout are described in dump_nhop_entry() */ 218 struct nhop_external { 219 uint32_t nh_len; /* length of the datastructure */ 220 uint32_t nh_idx; /* Nexthop index */ 221 uint32_t nh_fib; /* Fib nexhop is attached to */ 222 uint32_t ifindex; /* transmit interface ifindex */ 223 uint32_t aifindex; /* address ifindex */ 224 uint8_t prepend_len; /* length of the prepend */ 225 uint8_t nh_family; /* address family */ 226 uint16_t nh_type; /* nexthop type */ 227 uint16_t nh_mtu; /* nexthop mtu */ 228 229 uint16_t nh_flags; /* nhop flags */ 230 struct in_addr nh_addr; /* GW/DST IPv4 address */ 231 struct in_addr nh_src; /* default source IPv4 address */ 232 uint64_t nh_pksent; 233 /* control plane */ 234 /* lookup key: address, family, type */ 235 char nh_prepend[64]; /* L2 prepend */ 236 uint64_t nh_refcount; /* number of references */ 237 }; 238 239 struct nhop_addrs { 240 uint32_t na_len; /* length of the datastructure */ 241 uint16_t gw_sa_off; /* offset of gateway SA */ 242 uint16_t src_sa_off; /* offset of src address SA */ 243 }; 244 245 #define NHG_C_TYPE_CNHOPS 0x1 /* Control plane nhops list */ 246 #define NHG_C_TYPE_DNHOPS 0x2 /* Dataplane nhops list */ 247 struct nhgrp_container { 248 uint32_t nhgc_len; /* container length */ 249 uint16_t nhgc_count; /* number of items */ 250 uint8_t nhgc_type; /* container type */ 251 uint8_t nhgc_subtype; /* container subtype */ 252 }; 253 254 struct nhgrp_nhop_external { 255 uint32_t nh_idx; 256 uint32_t nh_weight; 257 }; 258 259 /* 260 * Layout: 261 * - nhgrp_external 262 * - nhgrp_container (control plane nhops list) 263 * - nhgrp_nhop_external 264 * - nhgrp_nhop_external 265 * .. 266 * - nhgrp_container (dataplane nhops list) 267 * - nhgrp_nhop_external 268 * - nhgrp_nhop_external 269 */ 270 struct nhgrp_external { 271 uint32_t nhg_idx; /* Nexthop group index */ 272 uint32_t nhg_refcount; /* number of references */ 273 }; 274 275 #endif 276