if_ether.c revision 39389
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
3439389Sfenner * $Id: if_ether.c,v 1.47 1998/06/12 03:48:14 julian Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Ethernet address resolution protocol.
391541Srgrimes * TODO:
401541Srgrimes *	add "inuse/lock" bit (or ref. count) along with valid bit
411541Srgrimes */
421541Srgrimes
4332350Seivind#include "opt_inet.h"
4432350Seivind
451541Srgrimes#include <sys/param.h>
4612693Sphk#include <sys/kernel.h>
4712693Sphk#include <sys/sysctl.h>
481541Srgrimes#include <sys/systm.h>
4912693Sphk#include <sys/mbuf.h>
501541Srgrimes#include <sys/malloc.h>
5118892Sbde#include <sys/socket.h>
521541Srgrimes#include <sys/syslog.h>
531541Srgrimes
541541Srgrimes#include <net/if.h>
551541Srgrimes#include <net/if_dl.h>
561541Srgrimes#include <net/route.h>
578426Swollman#include <net/netisr.h>
581541Srgrimes
591541Srgrimes#include <netinet/in.h>
601541Srgrimes#include <netinet/in_var.h>
611541Srgrimes#include <netinet/if_ether.h>
621541Srgrimes
631541Srgrimes#define SIN(s) ((struct sockaddr_in *)s)
641541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s)
651541Srgrimes
6612942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
671541Srgrimes
6812693Sphk/* timer values */
6912942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
7012942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
7112942Swollmanstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
721541Srgrimes
7312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
7412942Swollman	   &arpt_prune, 0, "");
7512942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
7612942Swollman	   &arpt_keep, 0, "");
7712942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
7812942Swollman	   &arpt_down, 0, "");
7912693Sphk
801541Srgrimes#define	rt_expire rt_rmx.rmx_expire
811541Srgrimes
8211225Swollmanstruct llinfo_arp {
8311225Swollman	LIST_ENTRY(llinfo_arp) la_le;
8411225Swollman	struct	rtentry *la_rt;
8511225Swollman	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
8611225Swollman	long	la_asked;		/* last time we QUERIED for this addr */
8711225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
8811225Swollman};
8912693Sphk
9011225Swollmanstatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
9111225Swollman
921541Srgrimesstruct	ifqueue arpintrq = {0, 0, 0, 50};
9312820Sphkstatic int	arp_inuse, arp_allocated;
941541Srgrimes
9512693Sphkstatic int	arp_maxtries = 5;
9612942Swollmanstatic int	useloopback = 1; /* use loopback interface for local traffic */
9712942Swollmanstatic int	arp_proxyall = 0;
983282Swollman
9912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
10012942Swollman	   &arp_maxtries, 0, "");
10112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
10212942Swollman	   &useloopback, 0, "");
10312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
10412942Swollman	   &arp_proxyall, 0, "");
10512693Sphk
10612877Sbdestatic void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
10736908Sjulianstatic void	arprequest __P((struct arpcom *,
10836908Sjulian			struct in_addr *, struct in_addr *, u_char *));
10912693Sphkstatic void	arpintr __P((void));
11012693Sphkstatic void	arptfree __P((struct llinfo_arp *));
11112693Sphkstatic void	arptimer __P((void *));
11212693Sphkstatic struct llinfo_arp
11312693Sphk		*arplookup __P((u_long, int, int));
11432350Seivind#ifdef INET
11512693Sphkstatic void	in_arpinput __P((struct mbuf *));
11632350Seivind#endif
11712693Sphk
1181541Srgrimes/*
1191541Srgrimes * Timeout routine.  Age arp_tab entries periodically.
1201541Srgrimes */
1211541Srgrimes/* ARGSUSED */
1221541Srgrimesstatic void
1231541Srgrimesarptimer(ignored_arg)
1241541Srgrimes	void *ignored_arg;
1251541Srgrimes{
1261541Srgrimes	int s = splnet();
12711225Swollman	register struct llinfo_arp *la = llinfo_arp.lh_first;
12811225Swollman	struct llinfo_arp *ola;
1291541Srgrimes
1301541Srgrimes	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
13111225Swollman	while ((ola = la) != 0) {
1321541Srgrimes		register struct rtentry *rt = la->la_rt;
13311225Swollman		la = la->la_le.le_next;
13434961Sphk		if (rt->rt_expire && rt->rt_expire <= time_second)
13511225Swollman			arptfree(ola); /* timer has expired, clear */
1361541Srgrimes	}
1371541Srgrimes	splx(s);
1381541Srgrimes}
1391541Srgrimes
1401541Srgrimes/*
1411541Srgrimes * Parallel to llc_rtrequest.
1421541Srgrimes */
1435196Swollmanstatic void
1441541Srgrimesarp_rtrequest(req, rt, sa)
1451541Srgrimes	int req;
1461541Srgrimes	register struct rtentry *rt;
1471541Srgrimes	struct sockaddr *sa;
1481541Srgrimes{
1491541Srgrimes	register struct sockaddr *gate = rt->rt_gateway;
1501541Srgrimes	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
1511541Srgrimes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
15212693Sphk	static int arpinit_done;
1531541Srgrimes
1541541Srgrimes	if (!arpinit_done) {
1551541Srgrimes		arpinit_done = 1;
15611225Swollman		LIST_INIT(&llinfo_arp);
1571541Srgrimes		timeout(arptimer, (caddr_t)0, hz);
1581541Srgrimes	}
1591541Srgrimes	if (rt->rt_flags & RTF_GATEWAY)
1601541Srgrimes		return;
1611541Srgrimes	switch (req) {
1621541Srgrimes
1631541Srgrimes	case RTM_ADD:
1641541Srgrimes		/*
1651541Srgrimes		 * XXX: If this is a manually added route to interface
1661541Srgrimes		 * such as older version of routed or gated might provide,
1671541Srgrimes		 * restore cloning bit.
1681541Srgrimes		 */
1691541Srgrimes		if ((rt->rt_flags & RTF_HOST) == 0 &&
1701541Srgrimes		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1711541Srgrimes			rt->rt_flags |= RTF_CLONING;
1721541Srgrimes		if (rt->rt_flags & RTF_CLONING) {
1731541Srgrimes			/*
1741541Srgrimes			 * Case 1: This route should come from a route to iface.
1751541Srgrimes			 */
1761541Srgrimes			rt_setgate(rt, rt_key(rt),
1771541Srgrimes					(struct sockaddr *)&null_sdl);
1781541Srgrimes			gate = rt->rt_gateway;
1791541Srgrimes			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
1801541Srgrimes			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
18134961Sphk			rt->rt_expire = time_second;
1821541Srgrimes			break;
1831541Srgrimes		}
1841541Srgrimes		/* Announce a new entry if requested. */
1851541Srgrimes		if (rt->rt_flags & RTF_ANNOUNCE)
1861541Srgrimes			arprequest((struct arpcom *)rt->rt_ifp,
18736908Sjulian			    &SIN(rt_key(rt))->sin_addr,
18836908Sjulian			    &SIN(rt_key(rt))->sin_addr,
1891541Srgrimes			    (u_char *)LLADDR(SDL(gate)));
1901541Srgrimes		/*FALLTHROUGH*/
1911541Srgrimes	case RTM_RESOLVE:
1921541Srgrimes		if (gate->sa_family != AF_LINK ||
1931541Srgrimes		    gate->sa_len < sizeof(null_sdl)) {
1946568Sdg			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
1951541Srgrimes			break;
1961541Srgrimes		}
1971541Srgrimes		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
1981541Srgrimes		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
1991541Srgrimes		if (la != 0)
2001541Srgrimes			break; /* This happens on a route change */
2011541Srgrimes		/*
2021541Srgrimes		 * Case 2:  This route may come from cloning, or a manual route
2031541Srgrimes		 * add with a LL address.
2041541Srgrimes		 */
2051541Srgrimes		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
2061541Srgrimes		rt->rt_llinfo = (caddr_t)la;
2071541Srgrimes		if (la == 0) {
2081541Srgrimes			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
2091541Srgrimes			break;
2101541Srgrimes		}
2111541Srgrimes		arp_inuse++, arp_allocated++;
2121541Srgrimes		Bzero(la, sizeof(*la));
2131541Srgrimes		la->la_rt = rt;
2141541Srgrimes		rt->rt_flags |= RTF_LLINFO;
21511225Swollman		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
21613926Swollman
21732350Seivind#ifdef INET
21813926Swollman		/*
21913926Swollman		 * This keeps the multicast addresses from showing up
22013926Swollman		 * in `arp -a' listings as unresolved.  It's not actually
22113926Swollman		 * functional.  Then the same for broadcast.
22213926Swollman		 */
22313926Swollman		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
22413926Swollman			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
22513926Swollman					       LLADDR(SDL(gate)));
22613926Swollman			SDL(gate)->sdl_alen = 6;
22716576Speter			rt->rt_expire = 0;
22813926Swollman		}
22913926Swollman		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
23013926Swollman			memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
23113926Swollman			SDL(gate)->sdl_alen = 6;
23216576Speter			rt->rt_expire = 0;
23313926Swollman		}
23432350Seivind#endif
23513926Swollman
2361541Srgrimes		if (SIN(rt_key(rt))->sin_addr.s_addr ==
2371541Srgrimes		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
2381541Srgrimes		    /*
2391541Srgrimes		     * This test used to be
2401541Srgrimes		     *	if (loif.if_flags & IFF_UP)
2411541Srgrimes		     * It allowed local traffic to be forced
2421541Srgrimes		     * through the hardware by configuring the loopback down.
2431541Srgrimes		     * However, it causes problems during network configuration
2441541Srgrimes		     * for boards that can't receive packets they send.
2451541Srgrimes		     * It is now necessary to clear "useloopback" and remove
2461541Srgrimes		     * the route to force traffic out to the hardware.
2471541Srgrimes		     */
2481541Srgrimes			rt->rt_expire = 0;
2491541Srgrimes			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
2501541Srgrimes				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
2511541Srgrimes			if (useloopback)
2528090Spst				rt->rt_ifp = loif;
2531541Srgrimes
2541541Srgrimes		}
2551541Srgrimes		break;
2561541Srgrimes
2571541Srgrimes	case RTM_DELETE:
2581541Srgrimes		if (la == 0)
2591541Srgrimes			break;
2601541Srgrimes		arp_inuse--;
26111225Swollman		LIST_REMOVE(la, la_le);
2621541Srgrimes		rt->rt_llinfo = 0;
2631541Srgrimes		rt->rt_flags &= ~RTF_LLINFO;
2641541Srgrimes		if (la->la_hold)
2651541Srgrimes			m_freem(la->la_hold);
2661541Srgrimes		Free((caddr_t)la);
2671541Srgrimes	}
2681541Srgrimes}
2691541Srgrimes
2701541Srgrimes/*
2711541Srgrimes * Broadcast an ARP request. Caller specifies:
2721541Srgrimes *	- arp header source ip address
2731541Srgrimes *	- arp header target ip address
2741541Srgrimes *	- arp header source ethernet address
2751541Srgrimes */
2761541Srgrimesstatic void
2771541Srgrimesarprequest(ac, sip, tip, enaddr)
2781541Srgrimes	register struct arpcom *ac;
27936908Sjulian	register struct in_addr *sip, *tip;
2801541Srgrimes	register u_char *enaddr;
2811541Srgrimes{
2821541Srgrimes	register struct mbuf *m;
2831541Srgrimes	register struct ether_header *eh;
2841541Srgrimes	register struct ether_arp *ea;
2851541Srgrimes	struct sockaddr sa;
2861541Srgrimes
2871541Srgrimes	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
2881541Srgrimes		return;
2891541Srgrimes	m->m_len = sizeof(*ea);
2901541Srgrimes	m->m_pkthdr.len = sizeof(*ea);
2911541Srgrimes	MH_ALIGN(m, sizeof(*ea));
2921541Srgrimes	ea = mtod(m, struct ether_arp *);
2931541Srgrimes	eh = (struct ether_header *)sa.sa_data;
2941541Srgrimes	bzero((caddr_t)ea, sizeof (*ea));
2958384Sdg	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
29616341Sdg	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
2971541Srgrimes	ea->arp_hrd = htons(ARPHRD_ETHER);
2981541Srgrimes	ea->arp_pro = htons(ETHERTYPE_IP);
2991541Srgrimes	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
3001541Srgrimes	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
3011541Srgrimes	ea->arp_op = htons(ARPOP_REQUEST);
3028384Sdg	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
3038384Sdg	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
3048384Sdg	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
3051541Srgrimes	sa.sa_family = AF_UNSPEC;
3061541Srgrimes	sa.sa_len = sizeof(sa);
3071541Srgrimes	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
3081541Srgrimes}
3091541Srgrimes
3101541Srgrimes/*
3111541Srgrimes * Resolve an IP address into an ethernet address.  If success,
3121541Srgrimes * desten is filled in.  If there is no entry in arptab,
3131541Srgrimes * set one up and broadcast a request for the IP address.
3141541Srgrimes * Hold onto this mbuf and resend it once the address
3151541Srgrimes * is finally resolved.  A return value of 1 indicates
3161541Srgrimes * that desten has been filled in and the packet should be sent
3171541Srgrimes * normally; a 0 return indicates that the packet has been
3181541Srgrimes * taken over here, either now or for later transmission.
3191541Srgrimes */
3201541Srgrimesint
3213514Swollmanarpresolve(ac, rt, m, dst, desten, rt0)
3221541Srgrimes	register struct arpcom *ac;
3231541Srgrimes	register struct rtentry *rt;
3241541Srgrimes	struct mbuf *m;
3251541Srgrimes	register struct sockaddr *dst;
3261541Srgrimes	register u_char *desten;
3273514Swollman	struct rtentry *rt0;
3281541Srgrimes{
3291541Srgrimes	register struct llinfo_arp *la;
3301541Srgrimes	struct sockaddr_dl *sdl;
3311541Srgrimes
3321541Srgrimes	if (m->m_flags & M_BCAST) {	/* broadcast */
3338384Sdg		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
3341541Srgrimes		return (1);
3351541Srgrimes	}
3361541Srgrimes	if (m->m_flags & M_MCAST) {	/* multicast */
3371541Srgrimes		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
3381541Srgrimes		return(1);
3391541Srgrimes	}
3401541Srgrimes	if (rt)
3411541Srgrimes		la = (struct llinfo_arp *)rt->rt_llinfo;
3421541Srgrimes	else {
3433311Sphk		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
3443311Sphk		if (la)
3451541Srgrimes			rt = la->la_rt;
3461541Srgrimes	}
3471541Srgrimes	if (la == 0 || rt == 0) {
34836308Sphk		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
34936308Sphk			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
35036308Sphk				rt ? "rt" : "");
3511541Srgrimes		m_freem(m);
3521541Srgrimes		return (0);
3531541Srgrimes	}
3541541Srgrimes	sdl = SDL(rt->rt_gateway);
3551541Srgrimes	/*
3561541Srgrimes	 * Check the address family and length is valid, the address
3571541Srgrimes	 * is resolved; otherwise, try to resolve.
3581541Srgrimes	 */
35934961Sphk	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
3601541Srgrimes	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
36116206Sbde		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
3621541Srgrimes		return 1;
3631541Srgrimes	}
3641541Srgrimes	/*
3651541Srgrimes	 * There is an arptab entry, but no ethernet address
3661541Srgrimes	 * response yet.  Replace the held mbuf with this
3671541Srgrimes	 * latest one.
3681541Srgrimes	 */
3691541Srgrimes	if (la->la_hold)
3701541Srgrimes		m_freem(la->la_hold);
3711541Srgrimes	la->la_hold = m;
3721541Srgrimes	if (rt->rt_expire) {
3731541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
37434961Sphk		if (la->la_asked == 0 || rt->rt_expire != time_second) {
37534961Sphk			rt->rt_expire = time_second;
3761541Srgrimes			if (la->la_asked++ < arp_maxtries)
37714761Sfenner			    arprequest(ac,
37836908Sjulian			        &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
37936908Sjulian				&SIN(dst)->sin_addr, ac->ac_enaddr);
3801541Srgrimes			else {
3811541Srgrimes				rt->rt_flags |= RTF_REJECT;
3821541Srgrimes				rt->rt_expire += arpt_down;
3831541Srgrimes				la->la_asked = 0;
3841541Srgrimes			}
3851541Srgrimes
3861541Srgrimes		}
3871541Srgrimes	}
3881541Srgrimes	return (0);
3891541Srgrimes}
3901541Srgrimes
3911541Srgrimes/*
3921541Srgrimes * Common length and type checks are done here,
3931541Srgrimes * then the protocol-specific routine is called.
3941541Srgrimes */
39512693Sphkstatic void
39631884Sbdearpintr()
3971541Srgrimes{
3981541Srgrimes	register struct mbuf *m;
3991541Srgrimes	register struct arphdr *ar;
4001541Srgrimes	int s;
4011541Srgrimes
4021541Srgrimes	while (arpintrq.ifq_head) {
4031541Srgrimes		s = splimp();
4041541Srgrimes		IF_DEQUEUE(&arpintrq, m);
4051541Srgrimes		splx(s);
4061541Srgrimes		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
4071541Srgrimes			panic("arpintr");
4081541Srgrimes		if (m->m_len >= sizeof(struct arphdr) &&
4091541Srgrimes		    (ar = mtod(m, struct arphdr *)) &&
4101541Srgrimes		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
4111541Srgrimes		    m->m_len >=
4121541Srgrimes		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
4131541Srgrimes
4141541Srgrimes			    switch (ntohs(ar->ar_pro)) {
4151541Srgrimes
41632350Seivind#ifdef INET
4171541Srgrimes			    case ETHERTYPE_IP:
4181541Srgrimes				    in_arpinput(m);
4191541Srgrimes				    continue;
42032350Seivind#endif
4211541Srgrimes			    }
4221541Srgrimes		m_freem(m);
4231541Srgrimes	}
4241541Srgrimes}
4251541Srgrimes
4268426SwollmanNETISR_SET(NETISR_ARP, arpintr);
4278426Swollman
42832350Seivind
42932350Seivind#ifdef INET
4301541Srgrimes/*
4311541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet.
4321541Srgrimes * Algorithm is that given in RFC 826.
4331541Srgrimes * In addition, a sanity check is performed on the sender
4341541Srgrimes * protocol address, to catch impersonators.
4351541Srgrimes * We no longer handle negotiations for use of trailer protocol:
4361541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
4371541Srgrimes * along with IP replies if we wanted trailers sent to us,
4381541Srgrimes * and also sent them in response to IP replies.
4391541Srgrimes * This allowed either end to announce the desire to receive
4401541Srgrimes * trailer packets.
4411541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
4421541Srgrimes * but formerly didn't normally send requests.
4431541Srgrimes */
4441541Srgrimesstatic void
4451541Srgrimesin_arpinput(m)
4461541Srgrimes	struct mbuf *m;
4471541Srgrimes{
4481541Srgrimes	register struct ether_arp *ea;
4491541Srgrimes	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
4501541Srgrimes	struct ether_header *eh;
4511541Srgrimes	register struct llinfo_arp *la = 0;
4521541Srgrimes	register struct rtentry *rt;
4531541Srgrimes	struct in_ifaddr *ia, *maybe_ia = 0;
4541541Srgrimes	struct sockaddr_dl *sdl;
4551541Srgrimes	struct sockaddr sa;
4561541Srgrimes	struct in_addr isaddr, itaddr, myaddr;
4571541Srgrimes	int op;
4581541Srgrimes
4591541Srgrimes	ea = mtod(m, struct ether_arp *);
4601541Srgrimes	op = ntohs(ea->arp_op);
4618384Sdg	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
4628384Sdg	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
46320525Sbde	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
4641541Srgrimes		if (ia->ia_ifp == &ac->ac_if) {
4651541Srgrimes			maybe_ia = ia;
4661541Srgrimes			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
4671541Srgrimes			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
4681541Srgrimes				break;
4691541Srgrimes		}
47012693Sphk	if (maybe_ia == 0) {
47112693Sphk		m_freem(m);
47212693Sphk		return;
47312693Sphk	}
4741541Srgrimes	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
4751541Srgrimes	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
47612693Sphk	    sizeof (ea->arp_sha))) {
47712693Sphk		m_freem(m);	/* it's from me, ignore it. */
47812693Sphk		return;
47912693Sphk	}
4801541Srgrimes	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
4811541Srgrimes	    sizeof (ea->arp_sha))) {
4821541Srgrimes		log(LOG_ERR,
4834069Swollman		    "arp: ether address is broadcast for IP address %s!\n",
4847088Swollman		    inet_ntoa(isaddr));
48512693Sphk		m_freem(m);
48612693Sphk		return;
4871541Srgrimes	}
4881541Srgrimes	if (isaddr.s_addr == myaddr.s_addr) {
4891541Srgrimes		log(LOG_ERR,
49019794Sfenner		   "arp: %6D is using my IP address %s!\n",
49119794Sfenner		   ea->arp_sha, ":", inet_ntoa(isaddr));
4921541Srgrimes		itaddr = myaddr;
4931541Srgrimes		goto reply;
4941541Srgrimes	}
4951541Srgrimes	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
4961541Srgrimes	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
49739389Sfenner		if (rt->rt_ifp != &ac->ac_if) {
49839389Sfenner			log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
49939389Sfenner			    inet_ntoa(isaddr),
50039389Sfenner			    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
50139389Sfenner			    ea->arp_sha, ":",
50239389Sfenner			    ac->ac_if.if_name, ac->ac_if.if_unit);
50339389Sfenner			goto reply;
50439389Sfenner		}
5051541Srgrimes		if (sdl->sdl_alen &&
5061541Srgrimes		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
50739389Sfenner			if (rt->rt_expire)
50839389Sfenner			    log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
50939389Sfenner				inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
51039389Sfenner				ea->arp_sha, ":",
51139389Sfenner				ac->ac_if.if_name, ac->ac_if.if_unit);
51239389Sfenner			else {
51339389Sfenner			    log(LOG_ERR,
51439389Sfenner				"arp: %6D attempts to modify permanent entry for %s on %s%d",
51539389Sfenner				ea->arp_sha, ":", inet_ntoa(isaddr),
51639389Sfenner				ac->ac_if.if_name, ac->ac_if.if_unit);
51739389Sfenner			    goto reply;
51839389Sfenner			}
5198384Sdg		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
5208384Sdg		sdl->sdl_alen = sizeof(ea->arp_sha);
5211541Srgrimes		if (rt->rt_expire)
52234961Sphk			rt->rt_expire = time_second + arpt_keep;
5231541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
5241541Srgrimes		la->la_asked = 0;
5251541Srgrimes		if (la->la_hold) {
5261541Srgrimes			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
5271541Srgrimes				rt_key(rt), rt);
5281541Srgrimes			la->la_hold = 0;
5291541Srgrimes		}
5301541Srgrimes	}
5311541Srgrimesreply:
5321541Srgrimes	if (op != ARPOP_REQUEST) {
5331541Srgrimes		m_freem(m);
5341541Srgrimes		return;
5351541Srgrimes	}
5361541Srgrimes	if (itaddr.s_addr == myaddr.s_addr) {
5371541Srgrimes		/* I am the target */
5388384Sdg		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
5398384Sdg		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
5401541Srgrimes	} else {
5411541Srgrimes		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
5423282Swollman		if (la == NULL) {
5433282Swollman			struct sockaddr_in sin;
5443282Swollman
54512693Sphk			if (!arp_proxyall) {
54612693Sphk				m_freem(m);
54712693Sphk				return;
54812693Sphk			}
5493282Swollman
5503282Swollman			bzero(&sin, sizeof sin);
5513282Swollman			sin.sin_family = AF_INET;
5523282Swollman			sin.sin_len = sizeof sin;
5533282Swollman			sin.sin_addr = itaddr;
5543282Swollman
5555101Swollman			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
55612693Sphk			if (!rt) {
55712693Sphk				m_freem(m);
55812693Sphk				return;
55912693Sphk			}
5603282Swollman			/*
5613282Swollman			 * Don't send proxies for nodes on the same interface
5623282Swollman			 * as this one came out of, or we'll get into a fight
5633282Swollman			 * over who claims what Ether address.
5643282Swollman			 */
56512693Sphk			if (rt->rt_ifp == &ac->ac_if) {
5663282Swollman				rtfree(rt);
56712693Sphk				m_freem(m);
56812693Sphk				return;
5693282Swollman			}
5708384Sdg			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
5718384Sdg			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
5723282Swollman			rtfree(rt);
5734069Swollman#ifdef DEBUG_PROXY
5748876Srgrimes			printf("arp: proxying for %s\n",
5757088Swollman			       inet_ntoa(itaddr));
5764069Swollman#endif
5773282Swollman		} else {
5783282Swollman			rt = la->la_rt;
5798384Sdg			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
5803282Swollman			sdl = SDL(rt->rt_gateway);
5818384Sdg			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
5823282Swollman		}
5831541Srgrimes	}
5841541Srgrimes
5858384Sdg	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
5868384Sdg	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
5871541Srgrimes	ea->arp_op = htons(ARPOP_REPLY);
5881541Srgrimes	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
5891541Srgrimes	eh = (struct ether_header *)sa.sa_data;
5908384Sdg	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
59116341Sdg	eh->ether_type = htons(ETHERTYPE_ARP);
5921541Srgrimes	sa.sa_family = AF_UNSPEC;
5931541Srgrimes	sa.sa_len = sizeof(sa);
5941541Srgrimes	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
5951541Srgrimes	return;
5961541Srgrimes}
59732350Seivind#endif
5981541Srgrimes
5991541Srgrimes/*
6001541Srgrimes * Free an arp entry.
6011541Srgrimes */
6021541Srgrimesstatic void
6031541Srgrimesarptfree(la)
6041541Srgrimes	register struct llinfo_arp *la;
6051541Srgrimes{
6061541Srgrimes	register struct rtentry *rt = la->la_rt;
6071541Srgrimes	register struct sockaddr_dl *sdl;
6081541Srgrimes	if (rt == 0)
6091541Srgrimes		panic("arptfree");
6101541Srgrimes	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
6111541Srgrimes	    sdl->sdl_family == AF_LINK) {
6121541Srgrimes		sdl->sdl_alen = 0;
6131541Srgrimes		la->la_asked = 0;
6141541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
6151541Srgrimes		return;
6161541Srgrimes	}
6171541Srgrimes	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
6181541Srgrimes			0, (struct rtentry **)0);
6191541Srgrimes}
6201541Srgrimes/*
6211541Srgrimes * Lookup or enter a new address in arptab.
6221541Srgrimes */
6231541Srgrimesstatic struct llinfo_arp *
6241541Srgrimesarplookup(addr, create, proxy)
6251541Srgrimes	u_long addr;
6261541Srgrimes	int create, proxy;
6271541Srgrimes{
6281541Srgrimes	register struct rtentry *rt;
6291541Srgrimes	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
6304069Swollman	const char *why = 0;
6311541Srgrimes
6321541Srgrimes	sin.sin_addr.s_addr = addr;
6331541Srgrimes	sin.sin_other = proxy ? SIN_PROXY : 0;
6345101Swollman	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
6351541Srgrimes	if (rt == 0)
6361541Srgrimes		return (0);
6371541Srgrimes	rt->rt_refcnt--;
6384069Swollman
63912693Sphk	if (rt->rt_flags & RTF_GATEWAY)
6404069Swollman		why = "host is not on local network";
64112693Sphk	else if ((rt->rt_flags & RTF_LLINFO) == 0)
6424069Swollman		why = "could not allocate llinfo";
64312693Sphk	else if (rt->rt_gateway->sa_family != AF_LINK)
6444069Swollman		why = "gateway route is not ours";
6454069Swollman
64612693Sphk	if (why && create) {
6474069Swollman		log(LOG_DEBUG, "arplookup %s failed: %s\n",
6487088Swollman		    inet_ntoa(sin.sin_addr), why);
6494069Swollman		return 0;
65012693Sphk	} else if (why) {
6514069Swollman		return 0;
6521541Srgrimes	}
6531541Srgrimes	return ((struct llinfo_arp *)rt->rt_llinfo);
6541541Srgrimes}
6551541Srgrimes
6565195Swollmanvoid
6575195Swollmanarp_ifinit(ac, ifa)
6585195Swollman	struct arpcom *ac;
6595195Swollman	struct ifaddr *ifa;
6605195Swollman{
66125822Stegge	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
66236908Sjulian		arprequest(ac, &IA_SIN(ifa)->sin_addr,
66336908Sjulian			       &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
6645195Swollman	ifa->ifa_rtrequest = arp_rtrequest;
6655195Swollman	ifa->ifa_flags |= RTF_CLONING;
6665195Swollman}
667