Home | History | Annotate | Line # | Download | only in net
nd.c revision 1.2
      1 /* $NetBSD: */
      2 
      3 /*
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Roy Marples.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: nd.c,v 1.2 2020/09/14 15:09:57 roy Exp $");
     32 
     33 #include <sys/callout.h>
     34 #include <sys/mbuf.h>
     35 #include <sys/socketvar.h> /* for softnet_lock */
     36 
     37 #include <net/if_llatbl.h>
     38 #include <net/nd.h>
     39 #include <net/route.h>
     40 
     41 #include <netinet/in.h>
     42 #include <netinet/ip6.h>
     43 
     44 static struct nd_domain *nd_domains[AF_MAX];
     45 
     46 static int nd_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
     47 
     48 static void nd_set_timertick(struct llentry *, time_t);
     49 static struct nd_domain *nd_find_domain(int);
     50 
     51 static void
     52 nd_timer(void *arg)
     53 {
     54 	struct llentry *ln = arg;
     55 	struct nd_domain *nd;
     56 	struct ifnet *ifp = NULL;
     57 	struct psref psref;
     58 	struct mbuf *m = NULL;
     59 	bool send_ns = false, missed = false;
     60 	union l3addr taddr, *daddrp = NULL;
     61 
     62 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
     63 	LLE_WLOCK(ln);
     64 
     65 	if (!(ln->la_flags & LLE_LINKED))
     66 		goto out;
     67 	if (ln->ln_ntick > 0) {
     68 		nd_set_timer(ln, ND_TIMER_TICK);
     69 		goto out;
     70 	}
     71 
     72 	nd = nd_find_domain(ln->lle_tbl->llt_af);
     73 	ifp = ln->lle_tbl->llt_ifp;
     74 	KASSERT(ifp != NULL);
     75 	if_acquire(ifp, &psref);
     76 
     77 	memcpy(&taddr, &ln->r_l3addr, sizeof(taddr));
     78 
     79 	switch (ln->ln_state) {
     80 	case ND_LLINFO_WAITDELETE:
     81 		LLE_REMREF(ln);
     82 		nd->nd_free(ln, 0);
     83 		ln = NULL;
     84 		break;
     85 
     86 	case ND_LLINFO_INCOMPLETE:
     87 		if (ln->ln_asked++ < nd->nd_mmaxtries) {
     88 			send_ns = true;
     89 			break;
     90 		}
     91 
     92 		if (ln->ln_hold) {
     93 			struct mbuf *m0, *mnxt;
     94 
     95 			/*
     96 			 * Assuming every packet in ln_hold
     97 			 * has the same IP header.
     98 			 */
     99 			m = ln->ln_hold;
    100 			for (m0 = m->m_nextpkt; m0 != NULL; m0 = mnxt) {
    101 				mnxt = m0->m_nextpkt;
    102 				m0->m_nextpkt = NULL;
    103 				m_freem(m0);
    104 			}
    105 
    106 			m->m_nextpkt = NULL;
    107 			ln->ln_hold = NULL;
    108 		}
    109 
    110 		missed = true;
    111 		ln->ln_state = ND_LLINFO_WAITDELETE;
    112 		if (ln->ln_asked == nd->nd_mmaxtries)
    113 			nd_set_timer(ln, ND_TIMER_RETRANS);
    114 		else
    115 			send_ns = true;
    116 		break;
    117 
    118 	case ND_LLINFO_REACHABLE:
    119 		if (!ND_IS_LLINFO_PERMANENT(ln)) {
    120 			ln->ln_state = ND_LLINFO_STALE;
    121 			nd_set_timer(ln, ND_TIMER_GC);
    122 		}
    123 		break;
    124 
    125 	case ND_LLINFO_PURGE: /* FALLTHROUGH */
    126 	case ND_LLINFO_STALE:
    127 		if (!ND_IS_LLINFO_PERMANENT(ln)) {
    128 			LLE_REMREF(ln);
    129 			nd->nd_free(ln, 1);
    130 			ln = NULL;
    131 		}
    132 		break;
    133 
    134 	case ND_LLINFO_DELAY:
    135 		if (nd->nd_nud_enabled(ifp)) {
    136 			ln->ln_asked = 1;
    137 			ln->ln_state = ND_LLINFO_PROBE;
    138 			send_ns = true;
    139 			daddrp = &taddr;
    140 		} else {
    141 			ln->ln_state = ND_LLINFO_STALE;
    142 			nd_set_timer(ln, ND_TIMER_GC);
    143 		}
    144 		break;
    145 
    146 	case ND_LLINFO_PROBE:
    147 		if (ln->ln_asked < nd->nd_umaxtries) {
    148 			ln->ln_asked++;
    149 			send_ns = true;
    150 			daddrp = &taddr;
    151 		} else {
    152 			LLE_REMREF(ln);
    153 			nd->nd_free(ln, 0);
    154 			ln = NULL;
    155 		}
    156 		break;
    157 	}
    158 
    159 	if (send_ns) {
    160 		uint8_t lladdr[255], *lladdrp;
    161 		union l3addr src, *psrc;
    162 
    163 		nd_set_timer(ln, ND_TIMER_RETRANS);
    164 		if (ln->ln_state > ND_LLINFO_INCOMPLETE &&
    165 		    ln->la_flags & LLE_VALID)
    166 		{
    167 			KASSERT(sizeof(lladdr) >= ifp->if_addrlen);
    168 			memcpy(lladdr, &ln->ll_addr, ifp->if_addrlen);
    169 			lladdrp = lladdr;
    170 		} else
    171 			lladdrp = NULL;
    172 		psrc = nd->nd_holdsrc(ln, &src);
    173 		LLE_FREE_LOCKED(ln);
    174 		ln = NULL;
    175 		nd->nd_output(ifp, daddrp, &taddr, lladdrp, psrc);
    176 	}
    177 
    178 out:
    179 	if (ln != NULL)
    180 		LLE_FREE_LOCKED(ln);
    181 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    182 
    183 	if (missed)
    184 		nd->nd_missed(ifp, &taddr, m);
    185 	if (ifp != NULL)
    186 		if_release(ifp, &psref);
    187 }
    188 
    189 static void
    190 nd_set_timertick(struct llentry *ln, time_t xtick)
    191 {
    192 
    193 	CTASSERT(sizeof(time_t) > sizeof(int));
    194 	KASSERT(xtick >= 0);
    195 
    196 	/*
    197 	 * We have to take care of a reference leak which occurs if
    198 	 * callout_reset overwrites a pending callout schedule.  Unfortunately
    199 	 * we don't have a mean to know the overwrite, so we need to know it
    200 	 * using callout_stop.  We need to call callout_pending first to exclude
    201 	 * the case that the callout has never been scheduled.
    202 	 */
    203 	if (callout_pending(&ln->la_timer)) {
    204 		bool expired;
    205 
    206 		expired = callout_stop(&ln->la_timer);
    207 		if (!expired)
    208 			LLE_REMREF(ln);
    209 	}
    210 
    211 	ln->ln_expire = time_uptime + xtick / hz;
    212 	LLE_ADDREF(ln);
    213 	if (xtick > INT_MAX) {
    214 		ln->ln_ntick = xtick - INT_MAX;
    215 		xtick = INT_MAX;
    216 	} else {
    217 		ln->ln_ntick = 0;
    218 	}
    219 	callout_reset(&ln->ln_timer_ch, xtick, nd_timer, ln);
    220 }
    221 
    222 void
    223 nd_set_timer(struct llentry *ln, int type)
    224 {
    225 	time_t xtick;
    226 	struct ifnet *ifp;
    227 	struct nd_domain *nd;
    228 
    229 	LLE_WLOCK_ASSERT(ln);
    230 
    231 	ifp = ln->lle_tbl->llt_ifp;
    232 	nd = nd_find_domain(ln->lle_tbl->llt_af);
    233 
    234 	switch (type) {
    235 	case ND_TIMER_IMMEDIATE:
    236 		xtick = 0;
    237 		break;
    238 	case ND_TIMER_TICK:
    239 		xtick = ln->ln_ntick;
    240 		break;
    241 	case ND_TIMER_RETRANS:
    242 		xtick = nd->nd_retrans(ifp) * hz / 1000;
    243 		break;
    244 	case ND_TIMER_REACHABLE:
    245 		xtick = nd->nd_reachable(ifp) * hz / 1000;
    246 		break;
    247 	case ND_TIMER_EXPIRE:
    248 		if (ln->ln_expire > time_uptime)
    249 			xtick = (ln->ln_expire - time_uptime) * hz;
    250 		else
    251 			xtick = nd_gctimer * hz;
    252 		break;
    253 	case ND_TIMER_DELAY:
    254 		xtick = nd->nd_delay * hz;
    255 		break;
    256 	case ND_TIMER_GC:
    257 		xtick = nd_gctimer * hz;
    258 		break;
    259 	default:
    260 		panic("%s: invalid timer type\n", __func__);
    261 	}
    262 
    263 	nd_set_timertick(ln, xtick);
    264 }
    265 
    266 int
    267 nd_resolve(struct llentry *ln, const struct rtentry *rt, struct mbuf *m,
    268     uint8_t *lldst, size_t dstsize)
    269 {
    270 	struct ifnet *ifp;
    271 	struct nd_domain *nd;
    272 	int error;
    273 
    274 	LLE_WLOCK_ASSERT(ln);
    275 
    276 	ifp = ln->lle_tbl->llt_ifp;
    277 	nd = nd_find_domain(ln->lle_tbl->llt_af);
    278 
    279 	/* We don't have to do link-layer address resolution on a p2p link. */
    280 	if (ifp->if_flags & IFF_POINTOPOINT &&
    281 	    ln->ln_state < ND_LLINFO_REACHABLE)
    282 	{
    283 		ln->ln_state = ND_LLINFO_STALE;
    284 		nd_set_timer(ln, ND_TIMER_GC);
    285 	}
    286 
    287 	/*
    288 	 * The first time we send a packet to a neighbor whose entry is
    289 	 * STALE, we have to change the state to DELAY and a sets a timer to
    290 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
    291 	 * neighbor unreachability detection on expiration.
    292 	 * (RFC 2461 7.3.3)
    293 	 */
    294 	if (ln->ln_state == ND_LLINFO_STALE) {
    295 		ln->ln_asked = 0;
    296 		ln->ln_state = ND_LLINFO_DELAY;
    297 		nd_set_timer(ln, ND_TIMER_DELAY);
    298 	}
    299 
    300 	/*
    301 	 * If the neighbor cache entry has a state other than INCOMPLETE
    302 	 * (i.e. its link-layer address is already resolved), just
    303 	 * send the packet.
    304 	 */
    305 	if (ln->ln_state > ND_LLINFO_INCOMPLETE) {
    306 		KASSERT((ln->la_flags & LLE_VALID) != 0);
    307 		memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
    308 		LLE_WUNLOCK(ln);
    309 		return 0;
    310 	}
    311 
    312 	/*
    313 	 * There is a neighbor cache entry, but no ethernet address
    314 	 * response yet.  Append this latest packet to the end of the
    315 	 * packet queue in the mbuf, unless the number of the packet
    316 	 * does not exceed maxqueuelen.  When it exceeds maxqueuelen,
    317 	 * the oldest packet in the queue will be removed.
    318 	 */
    319 	if (ln->ln_state == ND_LLINFO_NOSTATE ||
    320 	    ln->ln_state == ND_LLINFO_WAITDELETE)
    321 		ln->ln_state = ND_LLINFO_INCOMPLETE;
    322 
    323 	if (ln->ln_hold != NULL) {
    324 		struct mbuf *m_hold;
    325 		int i;
    326 
    327 		i = 0;
    328 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
    329 			i++;
    330 			if (m_hold->m_nextpkt == NULL) {
    331 				m_hold->m_nextpkt = m;
    332 				break;
    333 			}
    334 		}
    335 		while (i >= nd->nd_maxqueuelen) {
    336 			m_hold = ln->ln_hold;
    337 			ln->ln_hold = ln->ln_hold->m_nextpkt;
    338 			m_freem(m_hold);
    339 			i--;
    340 		}
    341 	} else
    342 		ln->ln_hold = m;
    343 
    344 	if (ln->ln_asked >= nd->nd_mmaxtries)
    345 		error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
    346 		    EHOSTUNREACH : EHOSTDOWN;
    347 	else
    348 		error = EWOULDBLOCK;
    349 
    350 	/*
    351 	 * If there has been no NS for the neighbor after entering the
    352 	 * INCOMPLETE state, send the first solicitation.
    353 	 */
    354 	if (!ND_IS_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
    355 		struct psref psref;
    356 		union l3addr dst, src, *psrc;
    357 
    358 		ln->ln_asked++;
    359 		nd_set_timer(ln, ND_TIMER_RETRANS);
    360 		memcpy(&dst, &ln->r_l3addr, sizeof(dst));
    361 		psrc = nd->nd_holdsrc(ln, &src);
    362 		if_acquire(ifp, &psref);
    363 		LLE_WUNLOCK(ln);
    364 
    365 		nd->nd_output(ifp, NULL, &dst, NULL, psrc);
    366 		if_release(ifp, &psref);
    367 	} else
    368 		LLE_WUNLOCK(ln);
    369 
    370 	return error;
    371 }
    372 
    373 void
    374 nd_nud_hint(struct llentry *ln)
    375 {
    376 	struct nd_domain *nd;
    377 
    378 	if (ln == NULL)
    379 		return;
    380 
    381 	LLE_WLOCK_ASSERT(ln);
    382 
    383 	if (ln->ln_state < ND_LLINFO_REACHABLE)
    384 		goto done;
    385 
    386 	nd = nd_find_domain(ln->lle_tbl->llt_af);
    387 
    388 	/*
    389 	 * if we get upper-layer reachability confirmation many times,
    390 	 * it is possible we have false information.
    391 	 */
    392 	ln->ln_byhint++;
    393 	if (ln->ln_byhint > nd->nd_maxnudhint)
    394 		goto done;
    395 
    396 	ln->ln_state = ND_LLINFO_REACHABLE;
    397 	if (!ND_IS_LLINFO_PERMANENT(ln))
    398 		nd_set_timer(ln, ND_TIMER_REACHABLE);
    399 
    400 done:
    401 	LLE_WUNLOCK(ln);
    402 
    403 	return;
    404 }
    405 
    406 static struct nd_domain *
    407 nd_find_domain(int af)
    408 {
    409 
    410 	KASSERT(af < __arraycount(nd_domains) && nd_domains[af] != NULL);
    411 	return nd_domains[af];
    412 }
    413 
    414 void
    415 nd_attach_domain(struct nd_domain *nd)
    416 {
    417 
    418 	KASSERT(nd->nd_family < __arraycount(nd_domains));
    419 	nd_domains[nd->nd_family] = nd;
    420 }
    421