1 /* $OpenBSD: nd6.h,v 1.38 2014/07/11 15:03:17 blambert 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 /* see net/route.h, or net/if_inarp.h */ 39 #ifndef RTF_ANNOUNCE 40 #define RTF_ANNOUNCE RTF_PROTO2 41 #endif 42 43 #define ND6_LLINFO_PURGE -3 44 #define ND6_LLINFO_NOSTATE -2 45 #define ND6_LLINFO_INCOMPLETE 0 46 #define ND6_LLINFO_REACHABLE 1 47 #define ND6_LLINFO_STALE 2 48 #define ND6_LLINFO_DELAY 3 49 #define ND6_LLINFO_PROBE 4 50 51 struct nd_ifinfo { 52 u_int32_t linkmtu; /* LinkMTU */ 53 u_int32_t maxmtu; /* Upper bound of LinkMTU */ 54 u_int32_t basereachable; /* BaseReachableTime */ 55 u_int32_t reachable; /* Reachable Time */ 56 u_int32_t retrans; /* Retrans Timer */ 57 u_int32_t flags; /* Flags */ 58 int recalctm; /* BaseReacable re-calculation timer */ 59 u_int8_t chlim; /* CurHopLimit */ 60 u_int8_t initialized; /* Flag to see the entry is initialized */ 61 /* the following 3 members are for privacy extension for addrconf */ 62 u_int8_t randomseed0[8]; /* upper 64 bits of MD5 digest */ 63 u_int8_t randomseed1[8]; /* lower 64 bits (usually the EUI64 IFID) */ 64 u_int8_t randomid[8]; /* current random ID */ 65 }; 66 67 #define ND6_IFF_PERFORMNUD 0x1 68 #define ND6_IFF_ACCEPT_RTADV 0x2 69 70 struct in6_nbrinfo { 71 char ifname[IFNAMSIZ]; /* if name, e.g. "en0" */ 72 struct in6_addr addr; /* IPv6 address of the neighbor */ 73 time_t expire; /* lifetime for NDP state transition */ 74 long asked; /* number of queries already sent for addr */ 75 int isrouter; /* if it acts as a router */ 76 int state; /* reachability state */ 77 }; 78 79 struct in6_defrouter { 80 struct sockaddr_in6 rtaddr; 81 time_t expire; 82 u_short rtlifetime; 83 u_short if_index; 84 u_char flags; 85 }; 86 87 struct in6_prefix { 88 struct sockaddr_in6 prefix; 89 struct prf_ra raflags; 90 time_t expire; 91 u_int32_t vltime; 92 u_int32_t pltime; 93 u_int32_t flags; 94 int refcnt; 95 u_short if_index; 96 u_short advrtrs; /* number of advertisement routers */ 97 u_char prefixlen; 98 u_char origin; 99 /* struct sockaddr_in6 advrtr[] */ 100 }; 101 102 struct in6_ndireq { 103 char ifname[IFNAMSIZ]; 104 struct nd_ifinfo ndi; 105 }; 106 107 struct in6_ndifreq { 108 char ifname[IFNAMSIZ]; 109 u_long ifindex; 110 }; 111 112 /* Prefix status */ 113 #define NDPRF_ONLINK 0x1 114 #define NDPRF_DETACHED 0x2 115 #define NDPRF_HOME 0x4 116 117 /* protocol constants */ 118 #define MAX_RTR_SOLICITATION_DELAY 1 /*1sec*/ 119 #define RTR_SOLICITATION_INTERVAL 4 /*4sec*/ 120 #define MAX_RTR_SOLICITATIONS 3 121 122 #define ND6_INFINITE_LIFETIME 0xffffffff 123 124 /* constants for RFC 4941 autoconf privacy extension */ 125 #define ND6_PRIV_MAX_DESYNC_FACTOR 600 /* 10 minutes */ 126 #define ND6_PRIV_VALID_LIFETIME 604800 /* 1 week */ 127 #define ND6_PRIV_PREFERRED_LIFETIME 86400 /* 1 day */ 128 129 #ifdef _KERNEL 130 131 #include <sys/queue.h> 132 #include <sys/timeout.h> 133 134 #define ND_IFINFO(ifp) \ 135 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->nd_ifinfo) 136 137 #define IN6_LINKMTU(ifp) \ 138 ((ND_IFINFO(ifp)->linkmtu && ND_IFINFO(ifp)->linkmtu < (ifp)->if_mtu) \ 139 ? ND_IFINFO(ifp)->linkmtu \ 140 : ((ND_IFINFO(ifp)->maxmtu && ND_IFINFO(ifp)->maxmtu < (ifp)->if_mtu) \ 141 ? ND_IFINFO(ifp)->maxmtu : (ifp)->if_mtu)) 142 143 144 struct llinfo_nd6 { 145 struct llinfo_nd6 *ln_next; 146 struct llinfo_nd6 *ln_prev; 147 struct rtentry *ln_rt; 148 struct mbuf *ln_hold; /* last packet until resolved/timeout */ 149 time_t ln_expire; /* lifetime for NDP state transition */ 150 long ln_asked; /* number of queries already sent for addr */ 151 int ln_byhint; /* # of times we made it reachable by UL hint */ 152 short ln_state; /* reachability state */ 153 short ln_router; /* 2^0: ND6 router bit */ 154 155 long ln_ntick; 156 struct timeout ln_timer_ch; 157 }; 158 159 #define ND6_IS_LLINFO_PROBREACH(n) ((n)->ln_state > ND6_LLINFO_INCOMPLETE) 160 #define ND6_LLINFO_PERMANENT(n) ((n)->ln_expire == 0) 161 162 /* node constants */ 163 #define MAX_REACHABLE_TIME 3600000 /* msec */ 164 #define REACHABLE_TIME 30000 /* msec */ 165 #define RETRANS_TIMER 1000 /* msec */ 166 #define MIN_RANDOM_FACTOR 512 /* 1024 * 0.5 */ 167 #define MAX_RANDOM_FACTOR 1536 /* 1024 * 1.5 */ 168 #define ND_COMPUTE_RTIME(x) \ 169 (((MIN_RANDOM_FACTOR * (x >> 10)) + (arc4random() & \ 170 ((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000) 171 172 TAILQ_HEAD(nd_drhead, nd_defrouter); 173 struct nd_defrouter { 174 TAILQ_ENTRY(nd_defrouter) dr_entry; 175 struct in6_addr rtaddr; 176 struct ifnet *ifp; 177 time_t expire; 178 int installed; /* is installed into kernel routing table */ 179 u_short rtlifetime; 180 u_char flags; /* flags on RA message */ 181 }; 182 183 struct nd_prefix { 184 struct ifnet *ndpr_ifp; 185 LIST_ENTRY(nd_prefix) ndpr_entry; 186 struct sockaddr_in6 ndpr_prefix; /* prefix */ 187 struct in6_addr ndpr_mask; /* netmask derived from the prefix */ 188 189 struct task ndpr_task; 190 191 time_t ndpr_expire; /* expiration time of the prefix */ 192 time_t ndpr_preferred; /* preferred time of the prefix */ 193 time_t ndpr_lastupdate; /* reception time of last advertisement */ 194 195 u_int32_t ndpr_vltime; /* advertised valid lifetime */ 196 u_int32_t ndpr_pltime; /* advertised preferred lifetime */ 197 198 struct prf_ra ndpr_flags; 199 u_int32_t ndpr_stateflags; /* actual state flags */ 200 /* list of routers that advertise the prefix: */ 201 LIST_HEAD(pr_rtrhead, nd_pfxrouter) ndpr_advrtrs; 202 u_char ndpr_plen; 203 int ndpr_refcnt; /* reference counter from addresses */ 204 }; 205 206 #define ndpr_raf ndpr_flags 207 #define ndpr_raf_onlink ndpr_flags.onlink 208 #define ndpr_raf_auto ndpr_flags.autonomous 209 #define ndpr_raf_router ndpr_flags.router 210 211 struct nd_pfxrouter { 212 LIST_ENTRY(nd_pfxrouter) pfr_entry; 213 struct nd_defrouter *router; 214 }; 215 216 LIST_HEAD(nd_prhead, nd_prefix); 217 218 extern int nd6_prune; 219 extern int nd6_delay; 220 extern int nd6_umaxtries; 221 extern int nd6_mmaxtries; 222 extern int nd6_maxnudhint; 223 extern int nd6_gctimer; 224 extern struct llinfo_nd6 llinfo_nd6; 225 extern struct nd_drhead nd_defrouter; 226 extern struct nd_prhead nd_prefix; 227 extern int nd6_debug; 228 229 #define nd6log(x) do { if (nd6_debug) log x; } while (0) 230 231 extern struct timeout nd6_timer_ch; 232 233 union nd_opts { 234 struct nd_opt_hdr *nd_opt_array[9]; 235 struct { 236 struct nd_opt_hdr *zero; 237 struct nd_opt_hdr *src_lladdr; 238 struct nd_opt_hdr *tgt_lladdr; 239 struct nd_opt_prefix_info *pi_beg; /* multiple opts, start */ 240 struct nd_opt_rd_hdr *rh; 241 struct nd_opt_mtu *mtu; 242 struct nd_opt_hdr *search; /* multiple opts */ 243 struct nd_opt_hdr *last; /* multiple opts */ 244 int done; 245 struct nd_opt_prefix_info *pi_end;/* multiple opts, end */ 246 } nd_opt_each; 247 }; 248 #define nd_opts_src_lladdr nd_opt_each.src_lladdr 249 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr 250 #define nd_opts_pi nd_opt_each.pi_beg 251 #define nd_opts_pi_end nd_opt_each.pi_end 252 #define nd_opts_rh nd_opt_each.rh 253 #define nd_opts_mtu nd_opt_each.mtu 254 #define nd_opts_search nd_opt_each.search 255 #define nd_opts_last nd_opt_each.last 256 #define nd_opts_done nd_opt_each.done 257 258 void nd6_init(void); 259 struct nd_ifinfo *nd6_ifattach(struct ifnet *); 260 void nd6_ifdetach(struct nd_ifinfo *); 261 int nd6_is_addr_neighbor(struct sockaddr_in6 *, struct ifnet *); 262 void nd6_option_init(void *, int, union nd_opts *); 263 struct nd_opt_hdr *nd6_option(union nd_opts *); 264 int nd6_options(union nd_opts *); 265 struct rtentry *nd6_lookup(struct in6_addr *, int, struct ifnet *, u_int); 266 void nd6_setmtu(struct ifnet *); 267 void nd6_llinfo_settimer(struct llinfo_nd6 *, long); 268 void nd6_timer(void *); 269 void nd6_purge(struct ifnet *); 270 void nd6_nud_hint(struct rtentry *, struct in6_addr *, int, u_int); 271 int nd6_resolve(struct ifnet *, struct rtentry *, 272 struct mbuf *, struct sockaddr *, u_char *); 273 void nd6_rtrequest(int, struct rtentry *); 274 int nd6_ioctl(u_long, caddr_t, struct ifnet *); 275 struct rtentry *nd6_cache_lladdr(struct ifnet *, struct in6_addr *, 276 char *, int, int, int); 277 int nd6_output(struct ifnet *, struct ifnet *, struct mbuf *, 278 struct sockaddr_in6 *, struct rtentry *); 279 int nd6_storelladdr(struct ifnet *, struct rtentry *, struct mbuf *, 280 struct sockaddr *, u_char *); 281 int nd6_sysctl(int, void *, size_t *, void *, size_t); 282 int nd6_need_cache(struct ifnet *); 283 284 void nd6_na_input(struct mbuf *, int, int); 285 void nd6_na_output(struct ifnet *, struct in6_addr *, 286 struct in6_addr *, u_long, int, struct sockaddr *); 287 void nd6_ns_input(struct mbuf *, int, int); 288 void nd6_ns_output(struct ifnet *, struct in6_addr *, 289 struct in6_addr *, struct llinfo_nd6 *, int); 290 caddr_t nd6_ifptomac(struct ifnet *); 291 void nd6_dad_start(struct ifaddr *, int *); 292 void nd6_dad_stop(struct ifaddr *); 293 void nd6_dad_duplicated(struct ifaddr *); 294 295 void nd6_rs_input(struct mbuf *, int, int); 296 void nd6_ra_input(struct mbuf *, int, int); 297 void prelist_del(struct nd_prefix *); 298 void defrouter_addreq(struct nd_defrouter *); 299 void defrouter_reset(void); 300 void defrouter_select(void); 301 void defrtrlist_del(struct nd_defrouter *); 302 void prelist_remove(struct nd_prefix *); 303 int prelist_update(struct nd_prefix *, struct nd_defrouter *, struct mbuf *); 304 int nd6_prelist_add(struct nd_prefix *, struct nd_defrouter *, 305 struct nd_prefix **); 306 int nd6_prefix_onlink(struct nd_prefix *); 307 int nd6_prefix_offlink(struct nd_prefix *); 308 void pfxlist_onlink_check(void); 309 struct nd_defrouter *defrouter_lookup(struct in6_addr *, struct ifnet *); 310 struct nd_prefix *nd6_prefix_lookup(struct nd_prefix *); 311 int in6_ifdel(struct ifnet *, struct in6_addr *); 312 int in6_init_prefix_ltimes(struct nd_prefix *ndpr); 313 void rt6_flush(struct in6_addr *, struct ifnet *); 314 315 #endif /* _KERNEL */ 316 317 #endif /* _NETINET6_ND6_H_ */ 318