1 /* $KAME: rtadvd.h,v 1.26 2003/08/05 12:34:23 itojun Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (C) 1998 WIDE Project. 7 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org> 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the project nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <stdbool.h> 36 37 #define ELM_MALLOC(p,error_action) \ 38 do { \ 39 p = malloc(sizeof(*p)); \ 40 if (p == NULL) { \ 41 syslog(LOG_ERR, "<%s> malloc failed: %s", \ 42 __func__, strerror(errno)); \ 43 error_action; \ 44 } \ 45 memset(p, 0, sizeof(*p)); \ 46 } while(0) 47 48 #define IN6ADDR_LINKLOCAL_ALLNODES_INIT \ 49 {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}} 51 52 #define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \ 53 {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}} 55 56 #define IN6ADDR_SITELOCAL_ALLROUTERS_INIT \ 57 {{{ 0xff, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}} 59 60 extern struct sockaddr_in6 sin6_linklocal_allnodes; 61 extern struct sockaddr_in6 sin6_linklocal_allrouters; 62 extern struct sockaddr_in6 sin6_sitelocal_allrouters; 63 64 /* 65 * RFC 3542 API deprecates IPV6_PKTINFO in favor of 66 * IPV6_RECVPKTINFO 67 */ 68 #ifndef IPV6_RECVPKTINFO 69 #ifdef IPV6_PKTINFO 70 #define IPV6_RECVPKTINFO IPV6_PKTINFO 71 #endif 72 #endif 73 74 /* 75 * RFC 3542 API deprecates IPV6_HOPLIMIT in favor of 76 * IPV6_RECVHOPLIMIT 77 */ 78 #ifndef IPV6_RECVHOPLIMIT 79 #ifdef IPV6_HOPLIMIT 80 #define IPV6_RECVHOPLIMIT IPV6_HOPLIMIT 81 #endif 82 #endif 83 84 /* protocol constants and default values */ 85 #define DEF_MAXRTRADVINTERVAL 600 86 #define DEF_ADVLINKMTU 0 87 #define DEF_ADVREACHABLETIME 0 88 #define DEF_ADVRETRANSTIMER 0 89 #define DEF_ADVCURHOPLIMIT 64 90 #define DEF_ADVVALIDLIFETIME 2592000 91 #define DEF_ADVPREFERREDLIFETIME 604800 92 93 #define MAXROUTERLIFETIME 9000 94 #define MIN_MAXINTERVAL 4 95 #define MAX_MAXINTERVAL 1800 96 #define MIN_MININTERVAL 3 97 #define MAXREACHABLETIME 3600000 98 99 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 100 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 101 #define MAX_FINAL_RTR_ADVERTISEMENTS 3 102 #define MIN_DELAY_BETWEEN_RAS 3 103 #define MAX_RA_DELAY_TIME 500000 /* usec */ 104 105 #define PREFIX_FROM_KERNEL 1 106 #define PREFIX_FROM_CONFIG 2 107 #define PREFIX_FROM_DYNAMIC 3 108 109 struct prefix { 110 TAILQ_ENTRY(prefix) pfx_next; 111 112 struct rainfo *pfx_rainfo; /* back pointer to the interface */ 113 /* 114 * Expiration timer. This is used when a prefix derived from 115 * the kernel is deleted. 116 */ 117 struct rtadvd_timer *pfx_timer; 118 119 uint32_t pfx_validlifetime; /* AdvValidLifetime */ 120 uint32_t pfx_vltimeexpire; /* Expiration of vltime */ 121 uint32_t pfx_preflifetime; /* AdvPreferredLifetime */ 122 uint32_t pfx_pltimeexpire; /* Expiration of pltime */ 123 int pfx_onlinkflg; /* bool: AdvOnLinkFlag */ 124 int pfx_autoconfflg; /* bool: AdvAutonomousFlag */ 125 int pfx_prefixlen; 126 int pfx_origin; /* From kernel or config */ 127 128 struct in6_addr pfx_prefix; 129 }; 130 131 struct rtinfo { 132 TAILQ_ENTRY(rtinfo) rti_next; 133 134 uint32_t rti_ltime; /* route lifetime */ 135 int rti_rtpref; /* route preference */ 136 int rti_prefixlen; 137 struct in6_addr rti_prefix; 138 }; 139 140 struct rdnss_addr { 141 TAILQ_ENTRY(rdnss_addr) ra_next; 142 143 struct in6_addr ra_dns; /* DNS server entry */ 144 }; 145 146 struct rdnss { 147 TAILQ_ENTRY(rdnss) rd_next; 148 149 TAILQ_HEAD(, rdnss_addr) rd_list; /* list of DNS servers */ 150 uint32_t rd_ltime; /* number of seconds valid */ 151 }; 152 153 struct pref64 { 154 TAILQ_ENTRY(pref64) p64_next; 155 uint16_t p64_plc; /* prefix length code */ 156 uint16_t p64_sl; /* scaled lifetime */ 157 struct in6_addr p64_prefix; 158 }; 159 160 /* 161 * The maximum length of a domain name in a DNS search list is calculated 162 * by a domain name + length fields per 63 octets + a zero octet at 163 * the tail and adding 8 octet boundary padding. 164 */ 165 #define _DNAME_LABELENC_MAXLEN \ 166 (NI_MAXHOST + (NI_MAXHOST / 64 + 1) + 1) 167 168 #define DNAME_LABELENC_MAXLEN \ 169 (_DNAME_LABELENC_MAXLEN + 8 - _DNAME_LABELENC_MAXLEN % 8) 170 171 struct dnssl_addr { 172 TAILQ_ENTRY(dnssl_addr) da_next; 173 174 int da_len; /* length of entry */ 175 char da_dom[DNAME_LABELENC_MAXLEN]; /* search domain name entry */ 176 }; 177 178 struct dnssl { 179 TAILQ_ENTRY(dnssl) dn_next; 180 181 TAILQ_HEAD(, dnssl_addr) dn_list; /* list of search domains */ 182 uint32_t dn_ltime; /* number of seconds valid */ 183 }; 184 185 struct soliciter { 186 TAILQ_ENTRY(soliciter) sol_next; 187 188 struct sockaddr_in6 sol_addr; 189 }; 190 191 struct rainfo { 192 /* pointer for list */ 193 TAILQ_ENTRY(rainfo) rai_next; 194 195 /* interface information */ 196 struct ifinfo *rai_ifinfo; 197 198 int rai_advlinkopt; /* bool: whether include link-layer addr opt */ 199 int rai_advifprefix; /* bool: gather IF prefixes? */ 200 201 /* Router configuration variables */ 202 uint16_t rai_lifetime; /* AdvDefaultLifetime */ 203 uint16_t rai_maxinterval; /* MaxRtrAdvInterval */ 204 uint16_t rai_mininterval; /* MinRtrAdvInterval */ 205 int rai_managedflg; /* AdvManagedFlag */ 206 int rai_otherflg; /* AdvOtherConfigFlag */ 207 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG 208 int rai_ipv6onlyflg; /* AdvIPv6OnlyFlag */ 209 #endif 210 211 int rai_rtpref; /* router preference */ 212 uint32_t rai_linkmtu; /* AdvLinkMTU */ 213 uint32_t rai_reachabletime; /* AdvReachableTime */ 214 uint32_t rai_retranstimer; /* AdvRetransTimer */ 215 uint8_t rai_hoplimit; /* AdvCurHopLimit */ 216 217 TAILQ_HEAD(, prefix) rai_prefix;/* AdvPrefixList(link head) */ 218 int rai_pfxs; /* number of prefixes */ 219 220 uint16_t rai_clockskew; /* used for consisitency check of lifetimes */ 221 222 TAILQ_HEAD(, rdnss) rai_rdnss; /* DNS server list */ 223 TAILQ_HEAD(, dnssl) rai_dnssl; /* search domain list */ 224 TAILQ_HEAD(, rtinfo) rai_route; /* route information option (link head) */ 225 int rai_routes; /* number of route information options */ 226 /* actual RA packet data and its length */ 227 size_t rai_ra_datalen; 228 char *rai_ra_data; 229 TAILQ_HEAD(, pref64) rai_pref64; /* PREF64 option */ 230 231 /* info about soliciter */ 232 TAILQ_HEAD(, soliciter) rai_soliciter; /* recent solication source */ 233 }; 234 235 /* RA information list */ 236 extern TAILQ_HEAD(railist_head_t, rainfo) railist; 237 238 /* 239 * ifi_state: 240 * 241 * (INIT) 242 * | 243 * | update_ifinfo() 244 * | update_persist_ifinfo() 245 * v 246 * UNCONFIGURED 247 * | ^ 248 * loadconfig()| |rm_ifinfo(), ra_output() 249 * (MC join)| |(MC leave) 250 * | | 251 * | | 252 * v | 253 * TRANSITIVE 254 * | ^ 255 * ra_output()| |getconfig() 256 * | | 257 * | | 258 * | | 259 * v | 260 * CONFIGURED 261 * 262 * 263 */ 264 #define IFI_STATE_UNCONFIGURED 0 265 #define IFI_STATE_CONFIGURED 1 266 #define IFI_STATE_TRANSITIVE 2 267 268 struct ifinfo { 269 TAILQ_ENTRY(ifinfo) ifi_next; 270 271 uint16_t ifi_state; 272 uint16_t ifi_persist; 273 uint16_t ifi_ifindex; 274 char ifi_ifname[IFNAMSIZ]; 275 uint8_t ifi_type; 276 uint16_t ifi_flags; 277 uint32_t ifi_nd_flags; 278 uint32_t ifi_phymtu; 279 struct sockaddr_dl ifi_sdl; 280 281 struct rainfo *ifi_rainfo; 282 struct rainfo *ifi_rainfo_trans; 283 uint16_t ifi_burstcount; 284 uint32_t ifi_burstinterval; 285 struct rtadvd_timer *ifi_ra_timer; 286 /* timestamp when the latest RA was sent */ 287 struct timespec ifi_ra_lastsent; 288 uint16_t ifi_rs_waitcount; 289 290 /* statistics */ 291 uint64_t ifi_raoutput; /* # of RAs sent */ 292 uint64_t ifi_rainput; /* # of RAs received */ 293 uint64_t ifi_rainconsistent; /* # of inconsistent recv'd RAs */ 294 uint64_t ifi_rsinput; /* # of RSs received */ 295 }; 296 297 /* Interface list */ 298 extern TAILQ_HEAD(ifilist_head_t, ifinfo) ifilist; 299 300 extern char *mcastif; 301 302 struct rtadvd_timer *ra_timeout(void *); 303 void ra_timer_update(void *, struct timespec *); 304 void ra_output(struct ifinfo *); 305 306 int prefix_match(struct in6_addr *, int, 307 struct in6_addr *, int); 308 struct ifinfo *if_indextoifinfo(int); 309 struct prefix *find_prefix(struct rainfo *, 310 struct in6_addr *, int); 311 void rtadvd_set_reload(int); 312 void rtadvd_set_shutdown(int); 313