1 /* $OpenBSD: nd6.h,v 1.63 2016/07/13 01:51:22 dlg Exp $ */ 2 /* $KAME: nd6.h,v 1.95 2002/06/08 11:31:06 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef _NETINET6_ND6_H_ 34 #define _NETINET6_ND6_H_ 35 36 #include <sys/task.h> 37 38 #define ND6_LLINFO_PURGE -3 39 #define ND6_LLINFO_NOSTATE -2 40 #define ND6_LLINFO_INCOMPLETE 0 41 #define ND6_LLINFO_REACHABLE 1 42 #define ND6_LLINFO_STALE 2 43 #define ND6_LLINFO_DELAY 3 44 #define ND6_LLINFO_PROBE 4 45 46 struct nd_ifinfo { 47 u_int32_t basereachable; /* BaseReachableTime */ 48 u_int32_t reachable; /* Reachable Time */ 49 u_int32_t retrans; /* Retrans Timer */ 50 u_int32_t flags; /* Flags */ 51 int recalctm; /* BaseReacable re-calculation timer */ 52 u_int8_t initialized; /* Flag to see the entry is initialized */ 53 /* the following 3 members are for privacy extension for addrconf */ 54 u_int8_t randomseed0[8]; /* upper 64 bits of MD5 digest */ 55 u_int8_t randomseed1[8]; /* lower 64 bits (usually the EUI64 IFID) */ 56 u_int8_t randomid[8]; /* current random ID */ 57 }; 58 59 #define ND6_IFF_PERFORMNUD 0x1 60 #define ND6_IFF_ACCEPT_RTADV 0x2 61 62 struct in6_nbrinfo { 63 char ifname[IFNAMSIZ]; /* if name, e.g. "en0" */ 64 struct in6_addr addr; /* IPv6 address of the neighbor */ 65 time_t expire; /* lifetime for NDP state transition */ 66 long asked; /* number of queries already sent for addr */ 67 int isrouter; /* if it acts as a router */ 68 int state; /* reachability state */ 69 }; 70 71 struct in6_defrouter { 72 struct sockaddr_in6 rtaddr; 73 time_t expire; 74 u_short rtlifetime; 75 u_short if_index; 76 u_char flags; 77 }; 78 79 struct prf_ra { 80 u_int onlink : 1; 81 u_int autonomous : 1; 82 u_int router : 1; 83 u_int reserved : 5; 84 }; 85 86 struct in6_prefix { 87 struct sockaddr_in6 prefix; 88 struct prf_ra raflags; 89 time_t expire; 90 u_int32_t vltime; 91 u_int32_t pltime; 92 u_int32_t flags; 93 int refcnt; 94 u_short if_index; 95 u_short advrtrs; /* number of advertisement routers */ 96 u_char prefixlen; 97 u_char origin; 98 #define PR_ORIG_RA 0 99 #define PR_ORIG_RR 1 100 #define PR_ORIG_STATIC 2 101 #define PR_ORIG_KERNEL 3 102 /* struct sockaddr_in6 advrtr[] */ 103 }; 104 105 struct in6_ndireq { 106 char ifname[IFNAMSIZ]; 107 struct nd_ifinfo ndi; 108 }; 109 110 struct in6_ndifreq { 111 char ifname[IFNAMSIZ]; 112 u_long ifindex; 113 }; 114 115 /* Prefix status */ 116 #define NDPRF_ONLINK 0x1 117 #define NDPRF_DETACHED 0x2 118 #define NDPRF_HOME 0x4 119 120 /* protocol constants */ 121 #define MAX_RTR_SOLICITATION_DELAY 1 /*1sec*/ 122 #define RTR_SOLICITATION_INTERVAL 4 /*4sec*/ 123 #define MAX_RTR_SOLICITATIONS 3 124 125 #define ND6_INFINITE_LIFETIME 0xffffffff 126 127 /* constants for RFC 4941 autoconf privacy extension */ 128 #define ND6_PRIV_MAX_DESYNC_FACTOR 512 /* largest pow2 < 10 minutes */ 129 #define ND6_PRIV_VALID_LIFETIME 604800 /* 1 week */ 130 #define ND6_PRIV_PREFERRED_LIFETIME 86400 /* 1 day */ 131 132 #ifdef _KERNEL 133 134 #include <sys/queue.h> 135 #include <sys/timeout.h> 136 137 #define ND_IFINFO(ifp) \ 138 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->nd_ifinfo) 139 140 #define RS_LHCOOKIE(ifp) \ 141 ((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->rs_lhcookie 142 143 struct llinfo_nd6 { 144 TAILQ_ENTRY(llinfo_nd6) ln_list; 145 struct rtentry *ln_rt; 146 struct mbuf *ln_hold; /* last packet until resolved/timeout */ 147 long ln_asked; /* number of queries already sent for addr */ 148 int ln_byhint; /* # of times we made it reachable by UL hint */ 149 short ln_state; /* reachability state */ 150 short ln_router; /* 2^0: ND6 router bit */ 151 152 struct timeout ln_timer_ch; 153 }; 154 155 #define ND6_IS_LLINFO_PROBREACH(n) ((n)->ln_state > ND6_LLINFO_INCOMPLETE) 156 #define ND6_LLINFO_PERMANENT(n) ((n)->ln_rt->rt_expire == 0) 157 158 /* node constants */ 159 #define MAX_REACHABLE_TIME 3600000 /* msec */ 160 #define REACHABLE_TIME 30000 /* msec */ 161 #define RETRANS_TIMER 1000 /* msec */ 162 #define MIN_RANDOM_FACTOR 512 /* 1024 * 0.5 */ 163 #define MAX_RANDOM_FACTOR 1536 /* 1024 * 1.5 */ 164 #define ND_COMPUTE_RTIME(x) \ 165 (((MIN_RANDOM_FACTOR * (x >> 10)) + (arc4random() & \ 166 ((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000) 167 168 TAILQ_HEAD(nd_drhead, nd_defrouter); 169 struct nd_defrouter { 170 TAILQ_ENTRY(nd_defrouter) dr_entry; 171 struct in6_addr rtaddr; 172 struct ifnet *ifp; 173 time_t expire; 174 int installed; /* is installed into kernel routing table */ 175 u_short rtlifetime; 176 u_char flags; /* flags on RA message */ 177 }; 178 179 struct nd_prefix { 180 struct ifnet *ndpr_ifp; 181 LIST_ENTRY(nd_prefix) ndpr_entry; 182 struct sockaddr_in6 ndpr_prefix; /* prefix */ 183 struct in6_addr ndpr_mask; /* netmask derived from the prefix */ 184 185 struct task ndpr_task; 186 187 time_t ndpr_expire; /* expiration time of the prefix */ 188 time_t ndpr_preferred; /* preferred time of the prefix */ 189 time_t ndpr_lastupdate; /* reception time of last advertisement */ 190 191 u_int32_t ndpr_vltime; /* advertised valid lifetime */ 192 u_int32_t ndpr_pltime; /* advertised preferred lifetime */ 193 194 struct prf_ra ndpr_flags; 195 u_int32_t ndpr_stateflags; /* actual state flags */ 196 /* list of routers that advertise the prefix: */ 197 LIST_HEAD(pr_rtrhead, nd_pfxrouter) ndpr_advrtrs; 198 u_char ndpr_plen; 199 int ndpr_refcnt; /* reference counter from addresses */ 200 }; 201 202 #define ndpr_raf ndpr_flags 203 #define ndpr_raf_onlink ndpr_flags.onlink 204 #define ndpr_raf_auto ndpr_flags.autonomous 205 #define ndpr_raf_router ndpr_flags.router 206 207 struct nd_pfxrouter { 208 LIST_ENTRY(nd_pfxrouter) pfr_entry; 209 struct nd_defrouter *router; 210 }; 211 212 LIST_HEAD(nd_prhead, nd_prefix); 213 214 extern int nd6_prune; 215 extern int nd6_delay; 216 extern int nd6_umaxtries; 217 extern int nd6_mmaxtries; 218 extern int nd6_maxnudhint; 219 extern int nd6_gctimer; 220 extern struct nd_drhead nd_defrouter; 221 extern struct nd_prhead nd_prefix; 222 extern int nd6_debug; 223 224 #define nd6log(x) do { if (nd6_debug) log x; } while (0) 225 226 extern struct timeout nd6_timer_ch; 227 228 union nd_opts { 229 struct nd_opt_hdr *nd_opt_array[9]; 230 struct { 231 struct nd_opt_hdr *zero; 232 struct nd_opt_hdr *src_lladdr; 233 struct nd_opt_hdr *tgt_lladdr; 234 struct nd_opt_prefix_info *pi_beg; /* multiple opts, start */ 235 struct nd_opt_rd_hdr *rh; 236 struct nd_opt_mtu *mtu; 237 struct nd_opt_hdr *search; /* multiple opts */ 238 struct nd_opt_hdr *last; /* multiple opts */ 239 int done; 240 struct nd_opt_prefix_info *pi_end;/* multiple opts, end */ 241 } nd_opt_each; 242 }; 243 #define nd_opts_src_lladdr nd_opt_each.src_lladdr 244 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr 245 #define nd_opts_pi nd_opt_each.pi_beg 246 #define nd_opts_pi_end nd_opt_each.pi_end 247 #define nd_opts_rh nd_opt_each.rh 248 #define nd_opts_mtu nd_opt_each.mtu 249 #define nd_opts_search nd_opt_each.search 250 #define nd_opts_last nd_opt_each.last 251 #define nd_opts_done nd_opt_each.done 252 253 void nd6_init(void); 254 struct nd_ifinfo *nd6_ifattach(struct ifnet *); 255 void nd6_ifdetach(struct nd_ifinfo *); 256 int nd6_is_addr_neighbor(struct sockaddr_in6 *, struct ifnet *); 257 void nd6_option_init(void *, int, union nd_opts *); 258 struct nd_opt_hdr *nd6_option(union nd_opts *); 259 int nd6_options(union nd_opts *); 260 struct rtentry *nd6_lookup(struct in6_addr *, int, struct ifnet *, u_int); 261 void nd6_setmtu(struct ifnet *); 262 void nd6_llinfo_settimer(struct llinfo_nd6 *, int); 263 void nd6_timer(void *); 264 void nd6_purge(struct ifnet *); 265 void nd6_nud_hint(struct rtentry *); 266 void nd6_rtrequest(struct ifnet *, int, struct rtentry *); 267 int nd6_ioctl(u_long, caddr_t, struct ifnet *); 268 void nd6_cache_lladdr(struct ifnet *, struct in6_addr *, char *, int, int, int); 269 int nd6_resolve(struct ifnet *, struct rtentry *, struct mbuf *, 270 struct sockaddr *, u_char *); 271 int nd6_sysctl(int, void *, size_t *, void *, size_t); 272 int nd6_need_cache(struct ifnet *); 273 274 void nd6_na_input(struct mbuf *, int, int); 275 void nd6_na_output(struct ifnet *, struct in6_addr *, 276 struct in6_addr *, u_long, int, struct sockaddr *); 277 void nd6_ns_input(struct mbuf *, int, int); 278 void nd6_ns_output(struct ifnet *, struct in6_addr *, 279 struct in6_addr *, struct llinfo_nd6 *, int); 280 caddr_t nd6_ifptomac(struct ifnet *); 281 void nd6_dad_start(struct ifaddr *); 282 void nd6_dad_stop(struct ifaddr *); 283 void nd6_ra_input(struct mbuf *, int, int); 284 285 void nd6_rs_init(void); 286 void nd6_rs_attach(struct ifnet *); 287 void nd6_rs_detach(struct ifnet *); 288 void nd6_rs_input(struct mbuf *, int, int); 289 290 void prelist_del(struct nd_prefix *); 291 void defrouter_addreq(struct nd_defrouter *); 292 void defrouter_reset(void); 293 void defrouter_select(void); 294 void defrtrlist_del(struct nd_defrouter *); 295 void prelist_remove(struct nd_prefix *); 296 int prelist_update(struct nd_prefix *, struct nd_defrouter *, struct mbuf *); 297 int nd6_prelist_add(struct nd_prefix *, struct nd_defrouter *, 298 struct nd_prefix **); 299 void pfxlist_onlink_check(void); 300 struct nd_defrouter *defrouter_lookup(struct in6_addr *, unsigned int); 301 302 struct nd_prefix *nd6_prefix_lookup(struct nd_prefix *); 303 int in6_ifdel(struct ifnet *, struct in6_addr *); 304 int in6_init_prefix_ltimes(struct nd_prefix *ndpr); 305 void rt6_flush(struct in6_addr *, struct ifnet *); 306 307 #endif /* _KERNEL */ 308 309 #endif /* _NETINET6_ND6_H_ */ 310