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