/*
* Copyright (c) 2020 The NetBSD Foundation, Inc.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Roy Marples.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
case ND_LLINFO_INCOMPLETE:
send_ns = true;
if (ln->ln_asked++ < nd->nd_mmaxtries)
break;
if (ln->ln_hold) {
struct mbuf *m0, *mnxt;
/*
* Assuming every packet in ln_hold
* has the same IP header.
*/
m = ln->ln_hold;
for (m0 = m->m_nextpkt; m0 != NULL; m0 = mnxt) {
mnxt = m0->m_nextpkt;
m0->m_nextpkt = NULL;
m_freem(m0);
}
case ND_LLINFO_PROBE:
send_ns = true;
if (ln->ln_asked++ < nd->nd_umaxtries) {
daddrp = &taddr;
} else {
ln->ln_state = ND_LLINFO_UNREACHABLE;
ln->ln_asked = 1;
missed = ND_LLINFO_PROBE;
/* nd_missed() consumers can use missed to know if
* they need to send ICMP UNREACHABLE or not. */
}
break;
case ND_LLINFO_UNREACHABLE:
/*
* RFC 7048 Section 3 says in the UNREACHABLE state
* packets continue to be sent to the link-layer address and
* then backoff exponentially.
* We adjust this slightly and move to the INCOMPLETE state
* after nd_mmaxtries probes and then start backing off.
*
* This results in simpler code whilst providing a more robust
* model which doubles the time to failure over what we did
* before. We don't want to be back to the old ARP model where
* no unreachability errors are returned because very
* few applications would look at unreachability hints provided
* such as ND_LLINFO_UNREACHABLE or RTM_MISS.
*/
send_ns = true;
if (ln->ln_asked++ < nd->nd_mmaxtries)
break;
/*
* We have to take care of a reference leak which occurs if
* callout_reset overwrites a pending callout schedule. Unfortunately
* we don't have a mean to know the overwrite, so we need to know it
* using callout_stop. We need to call callout_pending first to exclude
* the case that the callout has never been scheduled.
*/
if (callout_pending(&ln->la_timer)) {
bool expired;
expired = callout_stop(&ln->la_timer);
if (!expired)
LLE_REMREF(ln);
}
/* We don't have to do link-layer address resolution on a p2p link. */
if (ifp->if_flags & IFF_POINTOPOINT &&
ln->ln_state < ND_LLINFO_REACHABLE)
{
ln->ln_state = ND_LLINFO_STALE;
nd_set_timer(ln, ND_TIMER_GC);
}
/*
* The first time we send a packet to a neighbor whose entry is
* STALE, we have to change the state to DELAY and a sets a timer to
* expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
* neighbor unreachability detection on expiration.
* (RFC 2461 7.3.3)
*/
if (ln->ln_state == ND_LLINFO_STALE) {
ln->ln_asked = 0;
ln->ln_state = ND_LLINFO_DELAY;
nd_set_timer(ln, ND_TIMER_DELAY);
}
/*
* If the neighbor cache entry has a state other than INCOMPLETE
* (i.e. its link-layer address is already resolved), just
* send the packet.
*/
if (ln->ln_state > ND_LLINFO_INCOMPLETE) {
KASSERT((ln->la_flags & LLE_VALID) != 0);
memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
LLE_WUNLOCK(ln);
return 0;
}
/*
* There is a neighbor cache entry, but no ethernet address
* response yet. Append this latest packet to the end of the
* packet queue in the mbuf, unless the number of the packet
* does not exceed maxqueuelen. When it exceeds maxqueuelen,
* the oldest packet in the queue will be removed.
*/
if (ln->ln_state == ND_LLINFO_NOSTATE ||
ln->ln_state == ND_LLINFO_WAITDELETE)
ln->ln_state = ND_LLINFO_INCOMPLETE;
#ifdef MBUFTRACE
m_claimm(m, ln->lle_tbl->llt_mowner);
#endif
if (ln->ln_hold != NULL) {
struct mbuf *m_hold;
int i;
i = 0;
for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
i++;
if (m_hold->m_nextpkt == NULL) {
m_hold->m_nextpkt = m;
break;
}
}
KASSERTMSG(ln->la_numheld == i, "la_numheld=%d i=%d",
ln->la_numheld, i);
while (i >= nd->nd_maxqueuelen) {
m_hold = ln->ln_hold;
ln->ln_hold = ln->ln_hold->m_nextpkt;
m_freem(m_hold);
i--;
ln->la_numheld--;
}
} else {
KASSERTMSG(ln->la_numheld == 0, "la_numheld=%d",
ln->la_numheld);
ln->ln_hold = m;
}
/*
* If there has been no NS for the neighbor after entering the
* INCOMPLETE state, send the first solicitation.
*/
if (!ND_IS_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
struct psref psref;
union l3addr dst, src, *psrc;
if (ln->ln_state < ND_LLINFO_REACHABLE)
goto done;
nd = nd_find_domain(ln->lle_tbl->llt_af);
/*
* if we get upper-layer reachability confirmation many times,
* it is possible we have false information.
*/
ln->ln_byhint++;
if (ln->ln_byhint > nd->nd_maxnudhint)
goto done;
ln->ln_state = ND_LLINFO_REACHABLE;
if (!ND_IS_LLINFO_PERMANENT(ln))
nd_set_timer(ln, ND_TIMER_REACHABLE);