if_ether.c revision 1817
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
34 * $Id$
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 *	add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/socket.h>
48#include <sys/time.h>
49#include <sys/kernel.h>
50#include <sys/errno.h>
51#include <sys/ioctl.h>
52#include <sys/syslog.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/in_var.h>
61#include <netinet/ip.h>
62#include <netinet/if_ether.h>
63
64#define SIN(s) ((struct sockaddr_in *)s)
65#define SDL(s) ((struct sockaddr_dl *)s)
66#define SRP(s) ((struct sockaddr_inarp *)s)
67
68/*
69 * ARP trailer negotiation.  Trailer protocol is not IP specific,
70 * but ARP request/response use IP addresses.
71 */
72#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
73
74
75/* timer values */
76int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
77int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
78int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
79#define	rt_expire rt_rmx.rmx_expire
80
81static	void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
82static	void arptfree __P((struct llinfo_arp *));
83static	void arptimer __P((void *));
84static	struct llinfo_arp *arplookup __P((u_long, int, int));
85static	void in_arpinput __P((struct mbuf *));
86
87extern	struct ifnet loif;
88extern	struct timeval time;
89struct	llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp};
90struct	ifqueue arpintrq = {0, 0, 0, 50};
91int	arp_inuse, arp_allocated, arp_intimer;
92int	arp_maxtries = 5;
93int	useloopback = 1;	/* use loopback interface for local traffic */
94int	arpinit_done = 0;
95
96/*
97 * Timeout routine.  Age arp_tab entries periodically.
98 */
99/* ARGSUSED */
100static void
101arptimer(ignored_arg)
102	void *ignored_arg;
103{
104	int s = splnet();
105	register struct llinfo_arp *la = llinfo_arp.la_next;
106
107	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
108	while (la != &llinfo_arp) {
109		register struct rtentry *rt = la->la_rt;
110		la = la->la_next;
111		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
112			arptfree(la->la_prev); /* timer has expired, clear */
113	}
114	splx(s);
115}
116
117/*
118 * Parallel to llc_rtrequest.
119 */
120void
121arp_rtrequest(req, rt, sa)
122	int req;
123	register struct rtentry *rt;
124	struct sockaddr *sa;
125{
126	register struct sockaddr *gate = rt->rt_gateway;
127	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
128	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
129
130	if (!arpinit_done) {
131		arpinit_done = 1;
132		timeout(arptimer, (caddr_t)0, hz);
133	}
134	if (rt->rt_flags & RTF_GATEWAY)
135		return;
136	switch (req) {
137
138	case RTM_ADD:
139		/*
140		 * XXX: If this is a manually added route to interface
141		 * such as older version of routed or gated might provide,
142		 * restore cloning bit.
143		 */
144		if ((rt->rt_flags & RTF_HOST) == 0 &&
145		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
146			rt->rt_flags |= RTF_CLONING;
147		if (rt->rt_flags & RTF_CLONING) {
148			/*
149			 * Case 1: This route should come from a route to iface.
150			 */
151			rt_setgate(rt, rt_key(rt),
152					(struct sockaddr *)&null_sdl);
153			gate = rt->rt_gateway;
154			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
155			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
156			rt->rt_expire = time.tv_sec;
157			break;
158		}
159		/* Announce a new entry if requested. */
160		if (rt->rt_flags & RTF_ANNOUNCE)
161			arprequest((struct arpcom *)rt->rt_ifp,
162			    &SIN(rt_key(rt))->sin_addr.s_addr,
163			    &SIN(rt_key(rt))->sin_addr.s_addr,
164			    (u_char *)LLADDR(SDL(gate)));
165		/*FALLTHROUGH*/
166	case RTM_RESOLVE:
167		if (gate->sa_family != AF_LINK ||
168		    gate->sa_len < sizeof(null_sdl)) {
169			log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
170			break;
171		}
172		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
173		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
174		if (la != 0)
175			break; /* This happens on a route change */
176		/*
177		 * Case 2:  This route may come from cloning, or a manual route
178		 * add with a LL address.
179		 */
180		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
181		rt->rt_llinfo = (caddr_t)la;
182		if (la == 0) {
183			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
184			break;
185		}
186		arp_inuse++, arp_allocated++;
187		Bzero(la, sizeof(*la));
188		la->la_rt = rt;
189		rt->rt_flags |= RTF_LLINFO;
190		insque(la, &llinfo_arp);
191		if (SIN(rt_key(rt))->sin_addr.s_addr ==
192		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
193		    /*
194		     * This test used to be
195		     *	if (loif.if_flags & IFF_UP)
196		     * It allowed local traffic to be forced
197		     * through the hardware by configuring the loopback down.
198		     * However, it causes problems during network configuration
199		     * for boards that can't receive packets they send.
200		     * It is now necessary to clear "useloopback" and remove
201		     * the route to force traffic out to the hardware.
202		     */
203			rt->rt_expire = 0;
204			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
205				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
206			if (useloopback)
207				rt->rt_ifp = &loif;
208
209		}
210		break;
211
212	case RTM_DELETE:
213		if (la == 0)
214			break;
215		arp_inuse--;
216		remque(la);
217		rt->rt_llinfo = 0;
218		rt->rt_flags &= ~RTF_LLINFO;
219		if (la->la_hold)
220			m_freem(la->la_hold);
221		Free((caddr_t)la);
222	}
223}
224
225/*
226 * Broadcast an ARP packet, asking who has addr on interface ac.
227 */
228void
229arpwhohas(ac, addr)
230	register struct arpcom *ac;
231	register struct in_addr *addr;
232{
233	arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
234}
235
236/*
237 * Broadcast an ARP request. Caller specifies:
238 *	- arp header source ip address
239 *	- arp header target ip address
240 *	- arp header source ethernet address
241 */
242static void
243arprequest(ac, sip, tip, enaddr)
244	register struct arpcom *ac;
245	register u_long *sip, *tip;
246	register u_char *enaddr;
247{
248	register struct mbuf *m;
249	register struct ether_header *eh;
250	register struct ether_arp *ea;
251	struct sockaddr sa;
252
253	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
254		return;
255	m->m_len = sizeof(*ea);
256	m->m_pkthdr.len = sizeof(*ea);
257	MH_ALIGN(m, sizeof(*ea));
258	ea = mtod(m, struct ether_arp *);
259	eh = (struct ether_header *)sa.sa_data;
260	bzero((caddr_t)ea, sizeof (*ea));
261	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
262	    sizeof(eh->ether_dhost));
263	eh->ether_type = ETHERTYPE_ARP;		/* if_output will swap */
264	ea->arp_hrd = htons(ARPHRD_ETHER);
265	ea->arp_pro = htons(ETHERTYPE_IP);
266	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
267	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
268	ea->arp_op = htons(ARPOP_REQUEST);
269	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
270	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
271	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
272	sa.sa_family = AF_UNSPEC;
273	sa.sa_len = sizeof(sa);
274	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
275}
276
277/*
278 * Resolve an IP address into an ethernet address.  If success,
279 * desten is filled in.  If there is no entry in arptab,
280 * set one up and broadcast a request for the IP address.
281 * Hold onto this mbuf and resend it once the address
282 * is finally resolved.  A return value of 1 indicates
283 * that desten has been filled in and the packet should be sent
284 * normally; a 0 return indicates that the packet has been
285 * taken over here, either now or for later transmission.
286 */
287int
288arpresolve(ac, rt, m, dst, desten)
289	register struct arpcom *ac;
290	register struct rtentry *rt;
291	struct mbuf *m;
292	register struct sockaddr *dst;
293	register u_char *desten;
294{
295	register struct llinfo_arp *la;
296	struct sockaddr_dl *sdl;
297
298	if (m->m_flags & M_BCAST) {	/* broadcast */
299		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
300		    sizeof(etherbroadcastaddr));
301		return (1);
302	}
303	if (m->m_flags & M_MCAST) {	/* multicast */
304		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
305		return(1);
306	}
307	if (rt)
308		la = (struct llinfo_arp *)rt->rt_llinfo;
309	else {
310		if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0))
311			rt = la->la_rt;
312	}
313	if (la == 0 || rt == 0) {
314		log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
315		m_freem(m);
316		return (0);
317	}
318	sdl = SDL(rt->rt_gateway);
319	/*
320	 * Check the address family and length is valid, the address
321	 * is resolved; otherwise, try to resolve.
322	 */
323	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
324	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
325		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
326		return 1;
327	}
328	/*
329	 * There is an arptab entry, but no ethernet address
330	 * response yet.  Replace the held mbuf with this
331	 * latest one.
332	 */
333	if (la->la_hold)
334		m_freem(la->la_hold);
335	la->la_hold = m;
336	if (rt->rt_expire) {
337		rt->rt_flags &= ~RTF_REJECT;
338		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
339			rt->rt_expire = time.tv_sec;
340			if (la->la_asked++ < arp_maxtries)
341				arpwhohas(ac, &(SIN(dst)->sin_addr));
342			else {
343				rt->rt_flags |= RTF_REJECT;
344				rt->rt_expire += arpt_down;
345				la->la_asked = 0;
346			}
347
348		}
349	}
350	return (0);
351}
352
353/*
354 * Common length and type checks are done here,
355 * then the protocol-specific routine is called.
356 */
357void
358arpintr()
359{
360	register struct mbuf *m;
361	register struct arphdr *ar;
362	int s;
363
364	while (arpintrq.ifq_head) {
365		s = splimp();
366		IF_DEQUEUE(&arpintrq, m);
367		splx(s);
368		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
369			panic("arpintr");
370		if (m->m_len >= sizeof(struct arphdr) &&
371		    (ar = mtod(m, struct arphdr *)) &&
372		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
373		    m->m_len >=
374		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
375
376			    switch (ntohs(ar->ar_pro)) {
377
378			    case ETHERTYPE_IP:
379			    case ETHERTYPE_IPTRAILERS:
380				    in_arpinput(m);
381				    continue;
382			    }
383		m_freem(m);
384	}
385}
386
387/*
388 * ARP for Internet protocols on 10 Mb/s Ethernet.
389 * Algorithm is that given in RFC 826.
390 * In addition, a sanity check is performed on the sender
391 * protocol address, to catch impersonators.
392 * We no longer handle negotiations for use of trailer protocol:
393 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
394 * along with IP replies if we wanted trailers sent to us,
395 * and also sent them in response to IP replies.
396 * This allowed either end to announce the desire to receive
397 * trailer packets.
398 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
399 * but formerly didn't normally send requests.
400 */
401static void
402in_arpinput(m)
403	struct mbuf *m;
404{
405	register struct ether_arp *ea;
406	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
407	struct ether_header *eh;
408	register struct llinfo_arp *la = 0;
409	register struct rtentry *rt;
410	struct in_ifaddr *ia, *maybe_ia = 0;
411	struct sockaddr_dl *sdl;
412	struct sockaddr sa;
413	struct in_addr isaddr, itaddr, myaddr;
414	int op;
415
416	ea = mtod(m, struct ether_arp *);
417	op = ntohs(ea->arp_op);
418	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
419	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
420	for (ia = in_ifaddr; ia; ia = ia->ia_next)
421		if (ia->ia_ifp == &ac->ac_if) {
422			maybe_ia = ia;
423			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
424			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
425				break;
426		}
427	if (maybe_ia == 0)
428		goto out;
429	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
430	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
431	    sizeof (ea->arp_sha)))
432		goto out;	/* it's from me, ignore it. */
433	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
434	    sizeof (ea->arp_sha))) {
435		log(LOG_ERR,
436		    "arp: ether address is broadcast for IP address %x!\n",
437		    ntohl(isaddr.s_addr));
438		goto out;
439	}
440	if (isaddr.s_addr == myaddr.s_addr) {
441		log(LOG_ERR,
442		   "duplicate IP address %x!! sent from ethernet address: %s\n",
443		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
444		itaddr = myaddr;
445		goto reply;
446	}
447	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
448	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
449		if (sdl->sdl_alen &&
450		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
451			log(LOG_INFO, "arp info overwritten for %x by %s\n",
452			    isaddr.s_addr, ether_sprintf(ea->arp_sha));
453		bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
454			    sdl->sdl_alen = sizeof(ea->arp_sha));
455		if (rt->rt_expire)
456			rt->rt_expire = time.tv_sec + arpt_keep;
457		rt->rt_flags &= ~RTF_REJECT;
458		la->la_asked = 0;
459		if (la->la_hold) {
460			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
461				rt_key(rt), rt);
462			la->la_hold = 0;
463		}
464	}
465reply:
466	if (op != ARPOP_REQUEST) {
467	out:
468		m_freem(m);
469		return;
470	}
471	if (itaddr.s_addr == myaddr.s_addr) {
472		/* I am the target */
473		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
474		    sizeof(ea->arp_sha));
475		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
476		    sizeof(ea->arp_sha));
477	} else {
478		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
479		if (la == NULL)
480			goto out;
481		rt = la->la_rt;
482		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
483		    sizeof(ea->arp_sha));
484		sdl = SDL(rt->rt_gateway);
485		bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
486	}
487
488	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa));
489	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
490	ea->arp_op = htons(ARPOP_REPLY);
491	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
492	eh = (struct ether_header *)sa.sa_data;
493	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
494	    sizeof(eh->ether_dhost));
495	eh->ether_type = ETHERTYPE_ARP;
496	sa.sa_family = AF_UNSPEC;
497	sa.sa_len = sizeof(sa);
498	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
499	return;
500}
501
502/*
503 * Free an arp entry.
504 */
505static void
506arptfree(la)
507	register struct llinfo_arp *la;
508{
509	register struct rtentry *rt = la->la_rt;
510	register struct sockaddr_dl *sdl;
511	if (rt == 0)
512		panic("arptfree");
513	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
514	    sdl->sdl_family == AF_LINK) {
515		sdl->sdl_alen = 0;
516		la->la_asked = 0;
517		rt->rt_flags &= ~RTF_REJECT;
518		return;
519	}
520	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
521			0, (struct rtentry **)0);
522}
523/*
524 * Lookup or enter a new address in arptab.
525 */
526static struct llinfo_arp *
527arplookup(addr, create, proxy)
528	u_long addr;
529	int create, proxy;
530{
531	register struct rtentry *rt;
532	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
533
534	sin.sin_addr.s_addr = addr;
535	sin.sin_other = proxy ? SIN_PROXY : 0;
536	rt = rtalloc1((struct sockaddr *)&sin, create);
537	if (rt == 0)
538		return (0);
539	rt->rt_refcnt--;
540	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
541	    rt->rt_gateway->sa_family != AF_LINK) {
542		if (create)
543			log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr));
544		return (0);
545	}
546	return ((struct llinfo_arp *)rt->rt_llinfo);
547}
548
549int
550arpioctl(cmd, data)
551	int cmd;
552	caddr_t data;
553{
554	return (EOPNOTSUPP);
555}
556