route.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
  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. /*
  28. * Route-related (RTM_<NEW|DEL|GET>ROUTE) message header and attributes.
  29. */
  30. #ifndef _NETLINK_ROUTE_ROUTE_H_
  31. #define _NETLINK_ROUTE_ROUTE_H_
  32. /* Base header for all of the relevant messages */
  33. struct rtmsg {
  34. unsigned char rtm_family; /* address family */
  35. unsigned char rtm_dst_len; /* Prefix length */
  36. unsigned char rtm_src_len; /* Source prefix length (not used) */
  37. unsigned char rtm_tos; /* Type of service (not used) */
  38. unsigned char rtm_table; /* rtable id */
  39. unsigned char rtm_protocol; /* Routing protocol id (RTPROT_) */
  40. unsigned char rtm_scope; /* Route distance (RT_SCOPE_) */
  41. unsigned char rtm_type; /* Route type (RTN_) */
  42. unsigned rtm_flags; /* Route flags (RTM_F_) */
  43. };
  44. /*
  45. * RFC 3549, 3.1.1, route type (rtm_type field).
  46. */
  47. enum {
  48. RTN_UNSPEC,
  49. RTN_UNICAST, /* Unicast route */
  50. RTN_LOCAL, /* Accept locally (not supported) */
  51. RTN_BROADCAST, /* Accept locally as broadcast, send as broadcast */
  52. RTN_ANYCAST, /* Accept locally as broadcast, but send as unicast */
  53. RTN_MULTICAST, /* Multicast route */
  54. RTN_BLACKHOLE, /* Drop traffic towards destination */
  55. RTN_UNREACHABLE, /* Destination is unreachable */
  56. RTN_PROHIBIT, /* Administratively prohibited */
  57. RTN_THROW, /* Not in this table (not supported) */
  58. RTN_NAT, /* Translate this address (not supported) */
  59. RTN_XRESOLVE, /* Use external resolver (not supported) */
  60. __RTN_MAX,
  61. };
  62. #define RTN_MAX (__RTN_MAX - 1)
  63. /*
  64. * RFC 3549, 3.1.1, protocol (Identifies what/who added the route).
  65. * Values larger than RTPROT_STATIC(4) are not interpreted by the
  66. * kernel, they are just for user information.
  67. */
  68. #define RTPROT_UNSPEC 0
  69. #define RTPROT_REDIRECT 1 /* Route installed by ICMP redirect */
  70. #define RTPROT_KERNEL 2 /* Route installed by kernel */
  71. #define RTPROT_BOOT 3 /* Route installed during boot */
  72. #define RTPROT_STATIC 4 /* Route installed by administrator */
  73. #define RTPROT_GATED 8
  74. #define RTPROT_RA 9
  75. #define RTPROT_MRT 1
  76. #define RTPROT_ZEBRA 11
  77. #define RTPROT_BIRD 12
  78. #define RTPROT_DNROUTED 13
  79. #define RTPROT_XORP 14
  80. #define RTPROT_NTK 15
  81. #define RTPROT_DHCP 16
  82. #define RTPROT_MROUTED 17
  83. #define RTPROT_KEEPALIVED 18
  84. #define RTPROT_BABEL 42
  85. #define RTPROT_OPENR 99
  86. #define RTPROT_BGP 186
  87. #define RTPROT_ISIS 187
  88. #define RTPROT_OSPF 188
  89. #define RTPROT_RIP 189
  90. #define RTPROT_EIGRP 192
  91. /*
  92. * RFC 3549 3.1.1 Route scope (valid distance to destination).
  93. *
  94. * The values between RT_SCOPE_UNIVERSE(0) and RT_SCOPE_SITE(200)
  95. * are available to the user.
  96. */
  97. enum rt_scope_t {
  98. RT_SCOPE_UNIVERSE = 0,
  99. /* User defined values */
  100. RT_SCOPE_SITE = 200,
  101. RT_SCOPE_LINK = 253,
  102. RT_SCOPE_HOST = 254,
  103. RT_SCOPE_NOWHERE = 255
  104. };
  105. /*
  106. * RFC 3549 3.1.1 Route flags (rtm_flags).
  107. * Is a composition of RTNH_F flags (0x1..0x40 range), RTM_F flags (below)
  108. * and per-protocol (IPv4/IPv6) flags.
  109. */
  110. #define RTM_F_NOTIFY 0x00000100 /* not supported */
  111. #define RTM_F_CLONED 0x00000200 /* not supported */
  112. #define RTM_F_EQUALIZE 0x00000400 /* not supported */
  113. #define RTM_F_PREFIX 0x00000800 /* not supported */
  114. #define RTM_F_LOOKUP_TABLE 0x00001000 /* not supported */
  115. #define RTM_F_FIB_MATCH 0x00002000 /* not supported */
  116. #define RTM_F_OFFLOAD 0x00004000 /* not supported */
  117. #define RTM_F_TRAP 0x00008000 /* not supported */
  118. #define RTM_F_OFFLOAD_FAILED 0x20000000 /* not supported */
  119. /* Compatibility handling helpers */
  120. #ifndef _KERNEL
  121. #define NL_RTM_HDRLEN ((int)sizeof(struct rtmsg))
  122. #define RTM_RTA(_rtm) ((struct rtattr *)((char *)(_rtm) + NL_RTM_HDRLEN))
  123. #define RTM_PAYLOAD(_hdr) NLMSG_PAYLOAD((_hdr), NL_RTM_HDRLEN)
  124. #endif
  125. /*
  126. * Routing table identifiers.
  127. * FreeBSD route table numbering starts from 0, where 0 is a valid default
  128. * routing table. Indicating "all tables" via netlink can be done by not
  129. * including RTA_TABLE attribute and keeping rtm_table=0 (compatibility) or
  130. * setting RTA_TABLE value to RT_TABLE_UNSPEC.
  131. */
  132. #define RT_TABLE_MAIN 0 /* RT_DEFAULT_FIB */
  133. #define RT_TABLE_UNSPEC 0xFFFFFFFF /* RT_ALL_FIBS */
  134. enum rtattr_type_t {
  135. NL_RTA_UNSPEC,
  136. NL_RTA_DST = 1, /* binary, IPv4/IPv6 destination */
  137. NL_RTA_SRC = 2, /* binary, preferred source address */
  138. NL_RTA_IIF = 3, /* not supported */
  139. NL_RTA_OIF = 4, /* u32, transmit ifindex */
  140. NL_RTA_GATEWAY = 5, /* binary: IPv4/IPv6 gateway */
  141. NL_RTA_PRIORITY = 6, /* not supported */
  142. NL_RTA_PREFSRC = 7, /* not supported */
  143. NL_RTA_METRICS = 8, /* nested, list of NL_RTAX* attrs */
  144. NL_RTA_MULTIPATH = 9, /* binary, array of struct rtnexthop */
  145. NL_RTA_PROTOINFO = 10, /* not supported / deprecated */
  146. NL_RTA_KNH_ID = 10, /* u32, FreeBSD specific, kernel nexthop index */
  147. NL_RTA_FLOW = 11, /* not supported */
  148. NL_RTA_CACHEINFO = 12, /* not supported */
  149. NL_RTA_SESSION = 13, /* not supported / deprecated */
  150. NL_RTA_WEIGHT = 13, /* u32, FreeBSD specific, path weight */
  151. NL_RTA_MP_ALGO = 14, /* not supported / deprecated */
  152. NL_RTA_RTFLAGS = 14, /* u32, FreeBSD specific, path flags (RTF_)*/
  153. NL_RTA_TABLE = 15, /* u32, fibnum */
  154. NL_RTA_MARK = 16, /* not supported */
  155. NL_RTA_MFC_STATS = 17, /* not supported */
  156. NL_RTA_VIA = 18, /* binary, struct rtvia */
  157. NL_RTA_NEWDST = 19, /* not supported */
  158. NL_RTA_PREF = 20, /* not supported */
  159. NL_RTA_ENCAP_TYPE = 21, /* not supported */
  160. NL_RTA_ENCAP = 22, /* not supported */
  161. NL_RTA_EXPIRES = 23, /* u32, seconds till expiration */
  162. NL_RTA_PAD = 24, /* not supported */
  163. NL_RTA_UID = 25, /* not supported */
  164. NL_RTA_TTL_PROPAGATE = 26, /* not supported */
  165. NL_RTA_IP_PROTO = 27, /* not supported */
  166. NL_RTA_SPORT = 28, /* not supported */
  167. NL_RTA_DPORT = 29, /* not supported */
  168. NL_RTA_NH_ID = 30, /* u32, nexthop/nexthop group index */
  169. __RTA_MAX
  170. };
  171. #define NL_RTA_MAX (__RTA_MAX - 1)
  172. /*
  173. * Attributes that can be used as filters:
  174. *
  175. */
  176. #ifndef _KERNEL
  177. /*
  178. * RTA_* space has clashes with rtsock namespace.
  179. * Use NL_RTA_ prefix in the kernel and map to
  180. * RTA_ for userland.
  181. */
  182. #define RTA_UNSPEC NL_RTA_UNSPEC
  183. #define RTA_DST NL_RTA_DST
  184. #define RTA_SRC NL_RTA_SRC
  185. #define RTA_IIF NL_RTA_IIF
  186. #define RTA_OIF NL_RTA_OIF
  187. #define RTA_GATEWAY NL_RTA_GATEWAY
  188. #define RTA_PRIORITY NL_RTA_PRIORITY
  189. #define RTA_PREFSRC NL_RTA_PREFSRC
  190. #define RTA_METRICS NL_RTA_METRICS
  191. #define RTA_MULTIPATH NL_RTA_MULTIPATH
  192. #define RTA_PROTOINFO NL_RTA_PROTOINFO
  193. #define RTA_KNH_ID NL_RTA_KNH_ID
  194. #define RTA_FLOW NL_RTA_FLOW
  195. #define RTA_CACHEINFO NL_RTA_CACHEINFO
  196. #define RTA_SESSION NL_RTA_SESSION
  197. #define RTA_MP_ALGO NL_RTA_MP_ALGO
  198. #define RTA_TABLE NL_RTA_TABLE
  199. #define RTA_MARK NL_RTA_MARK
  200. #define RTA_MFC_STATS NL_RTA_MFC_STATS
  201. #define RTA_VIA NL_RTA_VIA
  202. #define RTA_NEWDST NL_RTA_NEWDST
  203. #define RTA_PREF NL_RTA_PREF
  204. #define RTA_ENCAP_TYPE NL_RTA_ENCAP_TYPE
  205. #define RTA_ENCAP NL_RTA_ENCAP
  206. #define RTA_EXPIRES NL_RTA_EXPIRES
  207. #define RTA_PAD NL_RTA_PAD
  208. #define RTA_UID NL_RTA_UID
  209. #define RTA_TTL_PROPAGATE NL_RTA_TTL_PROPAGATE
  210. #define RTA_IP_PROTO NL_RTA_IP_PROTO
  211. #define RTA_SPORT NL_RTA_SPORT
  212. #define RTA_DPORT NL_RTA_DPORT
  213. #define RTA_NH_ID NL_RTA_NH_ID
  214. #define RTA_MAX NL_RTA_MAX
  215. #endif
  216. /* route attribute header */
  217. struct rtattr {
  218. unsigned short rta_len;
  219. unsigned short rta_type;
  220. };
  221. #define NL_RTA_ALIGN_SIZE NL_ITEM_ALIGN_SIZE
  222. #define NL_RTA_ALIGN NL_ITEM_ALIGN
  223. #define NL_RTA_HDRLEN ((int)sizeof(struct rtattr))
  224. #define NL_RTA_DATA_LEN(_rta) ((int)((_rta)->rta_len - NL_RTA_HDRLEN))
  225. #define NL_RTA_DATA(_rta) NL_ITEM_DATA(_rta, NL_RTA_HDRLEN)
  226. #define NL_RTA_DATA_CONST(_rta) NL_ITEM_DATA_CONST(_rta, NL_RTA_HDRLEN)
  227. /* Compatibility attribute handling helpers */
  228. #ifndef _KERNEL
  229. #define RTA_ALIGNTO NL_RTA_ALIGN_SIZE
  230. #define RTA_ALIGN(_len) NL_RTA_ALIGN(_len)
  231. #define _RTA_LEN(_rta) ((int)(_rta)->rta_len)
  232. #define _RTA_ALIGNED_LEN(_rta) RTA_ALIGN(_RTA_LEN(_rta))
  233. #define RTA_OK(_rta, _len) NL_ITEM_OK(_rta, _len, NL_RTA_HDRLEN, _RTA_LEN)
  234. #define RTA_NEXT(_rta, _len) NL_ITEM_ITER(_rta, _len, _RTA_ALIGNED_LEN)
  235. #define RTA_LENGTH(_len) (NL_RTA_HDRLEN + (_len))
  236. #define RTA_SPACE(_len) RTA_ALIGN(RTA_LENGTH(_len))
  237. #define RTA_DATA(_rta) NL_RTA_DATA(_rta)
  238. #define RTA_PAYLOAD(_rta) ((int)(_RTA_LEN(_rta) - NL_RTA_HDRLEN))
  239. #endif
  240. /* RTA attribute headers */
  241. /* RTA_VIA */
  242. struct rtvia {
  243. sa_family_t rtvia_family;
  244. uint8_t rtvia_addr[0];
  245. };
  246. /*
  247. * RTA_METRICS is a nested attribute, consisting of a list of
  248. * TLVs with types defined below.
  249. */
  250. enum {
  251. NL_RTAX_UNSPEC,
  252. NL_RTAX_LOCK = 1, /* not supported */
  253. NL_RTAX_MTU = 2, /* desired path MTU */
  254. NL_RTAX_WINDOW = 3, /* not supported */
  255. NL_RTAX_RTT = 4, /* not supported */
  256. NL_RTAX_RTTVAR = 5, /* not supported */
  257. NL_RTAX_SSTHRESH = 6, /* not supported */
  258. NL_RTAX_CWND = 7, /* not supported */
  259. NL_RTAX_ADVMSS = 8, /* not supported */
  260. NL_RTAX_REORDERING = 9, /* not supported */
  261. NL_RTAX_HOPLIMIT = 10, /* not supported */
  262. NL_RTAX_INITCWND = 11, /* not supporrted */
  263. NL_RTAX_FEATURES = 12, /* not supported */
  264. NL_RTAX_RTO_MIN = 13, /* not supported */
  265. NL_RTAX_INITRWND = 14, /* not supported */
  266. NL_RTAX_QUICKACK = 15, /* not supported */
  267. NL_RTAX_CC_ALGO = 16, /* not supported */
  268. NL_RTAX_FASTOPEN_NO_COOKIE = 17, /* not supported */
  269. __NL_RTAX_MAX
  270. };
  271. #define NL_RTAX_MAX (__NL_RTAX_MAX - 1)
  272. #define RTAX_FEATURE_ECN (1 << 0)
  273. #define RTAX_FEATURE_SACK (1 << 1)
  274. #define RTAX_FEATURE_TIMESTAMP (1 << 2)
  275. #define RTAX_FEATURE_ALLFRAG (1 << 3)
  276. #define RTAX_FEATURE_MASK \
  277. (RTAX_FEATURE_ECN | RTAX_FEATURE_SACK | RTAX_FEATURE_TIMESTAMP | \
  278. RTAX_FEATURE_ALLFRAG)
  279. #ifndef _KERNEL
  280. /*
  281. * RTAX_* space clashes with rtsock namespace.
  282. * Use NL_RTAX_ prefix in the kernel and map to
  283. * RTAX_ for userland.
  284. */
  285. #define RTAX_UNSPEC NL_RTAX_UNSPEC
  286. #define RTAX_LOCK NL_RTAX_LOCK
  287. #define RTAX_MTU NL_RTAX_MTU
  288. #define RTAX_WINDOW NL_RTAX_WINDOW
  289. #define RTAX_RTT NL_RTAX_RTT
  290. #define RTAX_RTTVAR NL_RTAX_RTTVAR
  291. #define RTAX_SSTHRESH NL_RTAX_SSTHRESH
  292. #define RTAX_CWND NL_RTAX_CWND
  293. #define RTAX_ADVMSS NL_RTAX_ADVMSS
  294. #define RTAX_REORDERING NL_RTAX_REORDERING
  295. #define RTAX_HOPLIMIT NL_RTAX_HOPLIMIT
  296. #define RTAX_INITCWND NL_RTAX_INITCWND
  297. #define RTAX_FEATURES NL_RTAX_FEATURES
  298. #define RTAX_RTO_MIN NL_RTAX_RTO_MIN
  299. #define RTAX_INITRWND NL_RTAX_INITRWND
  300. #define RTAX_QUICKACK NL_RTAX_QUICKACK
  301. #define RTAX_CC_ALGO NL_RTAX_CC_ALGO
  302. #define RTAX_FASTOPEN_NO_COOKIE NL_RTAX_FASTOPEN_NO_COOKIE
  303. #endif
  304. /*
  305. * RTA_MULTIPATH consists of an array of rtnexthop structures.
  306. * Each rtnexthop structure contains RTA_GATEWAY or RTA_VIA
  307. * attribute following the header.
  308. */
  309. struct rtnexthop {
  310. unsigned short rtnh_len;
  311. unsigned char rtnh_flags;
  312. unsigned char rtnh_hops; /* nexthop weight */
  313. int rtnh_ifindex;
  314. };
  315. /* rtnh_flags */
  316. #define RTNH_F_DEAD 0x01 /* not supported */
  317. #define RTNH_F_PERVASIVE 0x02 /* not supported */
  318. #define RTNH_F_ONLINK 0x04 /* not supported */
  319. #define RTNH_F_OFFLOAD 0x08 /* not supported */
  320. #define RTNH_F_LINKDOWN 0x10 /* not supported */
  321. #define RTNH_F_UNRESOLVED 0x20 /* not supported */
  322. #define RTNH_F_TRAP 0x40 /* not supported */
  323. #define RTNH_COMPARE_MASK (RTNH_F_DEAD | RTNH_F_LINKDOWN | \
  324. RTNH_F_OFFLOAD | RTNH_F_TRAP)
  325. /* Macros to handle hexthops */
  326. #define RTNH_ALIGNTO NL_ITEM_ALIGN_SIZE
  327. #define RTNH_ALIGN(_len) NL_ITEM_ALIGN(_len)
  328. #define RTNH_HDRLEN ((int)sizeof(struct rtnexthop))
  329. #define _RTNH_LEN(_nh) ((int)(_nh)->rtnh_len)
  330. #define _RTNH_ALIGNED_LEN(_nh) RTNH_ALIGN(_RTNH_LEN(_nh))
  331. #define RTNH_OK(_nh, _len) NL_ITEM_OK(_nh, _len, RTNH_HDRLEN, _RTNH_LEN)
  332. #define RTNH_NEXT(_nh) ((struct rtnexthop *)((char *)(_nh) + _RTNH_ALIGNED_LEN(_nh)))
  333. #define RTNH_LENGTH(_len) (RTNH_HDRLEN + (_len))
  334. #define RTNH_SPACE(_len) RTNH_ALIGN(RTNH_LENGTH(_len))
  335. #define RTNH_DATA(_nh) ((struct rtattr *)NL_ITEM_DATA(_nh, RTNH_HDRLEN))
  336. struct rtgenmsg {
  337. unsigned char rtgen_family;
  338. };
  339. #endif