xref: /original-bsd/sys/netinet/if_ether.c (revision 8fbb78b3)
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.14 (Berkeley) 06/25/91
8  */
9 
10 /*
11  * Ethernet address resolution protocol.
12  * TODO:
13  *	run at splnet (add ARP protocol intr.)
14  *	link entries onto hash chains, keep free list
15  *	add "inuse/lock" bit (or ref. count) along with valid bit
16  */
17 
18 #include "param.h"
19 #include "systm.h"
20 #include "malloc.h"
21 #include "mbuf.h"
22 #include "socket.h"
23 #include "time.h"
24 #include "kernel.h"
25 #include "errno.h"
26 #include "ioctl.h"
27 #include "syslog.h"
28 
29 #include "../net/if.h"
30 #include "../net/if_dl.h"
31 #include "../net/route.h"
32 
33 #include "in.h"
34 #include "in_systm.h"
35 #include "in_var.h"
36 #include "ip.h"
37 #include "if_ether.h"
38 
39 #define SIN(s) ((struct sockaddr_in *)s)
40 #define SDL(s) ((struct sockaddr_dl *)s)
41 #define SRP(s) ((struct sockaddr_inarp *)s)
42 
43 /*
44  * ARP trailer negotiation.  Trailer protocol is not IP specific,
45  * but ARP request/response use IP addresses.
46  */
47 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
48 
49 
50 /* timer values */
51 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
52 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
53 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
54 #define RTF_USETRAILERS	RTF_PROTO1
55 #define rt_expire rt_rmx.rmx_expire
56 
57 extern struct ifnet loif;
58 extern struct timeval time;
59 struct llinfo_arp *arplookup(), llinfo_arp = {&llinfo_arp, &llinfo_arp};
60 struct ifqueue arpintrq = {0, 0, 0, 50};
61 int	arp_inuse, arp_allocated, arp_intimer;
62 int	arp_maxtries = 5;
63 int	useloopback = 1;	/* use loopback interface for local traffic */
64 int	arpinit_done = 0;
65 
66 /*
67  * Timeout routine.  Age arp_tab entries periodically.
68  */
69 arptimer()
70 {
71 	int s = splnet();
72 	register struct llinfo_arp *la = llinfo_arp.la_next;
73 
74 	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
75 	while (la != &llinfo_arp) {
76 		register struct rtentry *rt = la->la_rt;
77 		la = la->la_next;
78 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
79 			arptfree(la->la_prev); /* timer has expired, clear */
80 	}
81 	splx(s);
82 }
83 
84 /*
85  * Parallel to llc_rtrequest.
86  */
87 arp_rtrequest(req, rt, sa)
88 	int req;
89 	register struct rtentry *rt;
90 	struct sockaddr *sa;
91 {
92 	register struct sockaddr *gate = rt->rt_gateway;
93 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
94 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
95 
96 	if (!arpinit_done) {
97 		arpinit_done = 1;
98 		timeout(arptimer, (caddr_t)0, hz);
99 	}
100 	if (rt->rt_flags & RTF_GATEWAY)
101 		return;
102 	switch (req) {
103 	case RTM_ADD:
104 	case RTM_RESOLVE:
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 in_arpinput(m)
335 	struct mbuf *m;
336 {
337 	register struct ether_arp *ea;
338 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
339 	struct ether_header *eh;
340 	register struct llinfo_arp *la;
341 	register struct rtentry *rt;
342 	struct in_ifaddr *ia, *maybe_ia = 0;
343 	struct mbuf *mcopy = 0;
344 	struct sockaddr_dl *sdl;
345 	struct sockaddr sa;
346 	struct in_addr isaddr, itaddr, myaddr;
347 	int proto, op, completed = 0, sendtrailers;
348 
349 	if (ac->ac_if.if_flags & IFF_NOARP)
350 		goto out;
351 	ea = mtod(m, struct ether_arp *);
352 	proto = ntohs(ea->arp_pro);
353 	op = ntohs(ea->arp_op);
354 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
355 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
356 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
357 		if (ia->ia_ifp == &ac->ac_if) {
358 			maybe_ia = ia;
359 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
360 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
361 				break;
362 		}
363 	if (maybe_ia == 0)
364 		goto out;
365 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
366 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
367 	    sizeof (ea->arp_sha)))
368 		goto out;	/* it's from me, ignore it. */
369 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
370 	    sizeof (ea->arp_sha))) {
371 		log(LOG_ERR,
372 		    "arp: ether address is broadcast for IP address %x!\n",
373 		    ntohl(isaddr.s_addr));
374 		goto out;
375 	}
376 	if (isaddr.s_addr == myaddr.s_addr) {
377 		log(LOG_ERR,
378 		   "duplicate IP address %x!! sent from ethernet address: %s\n",
379 		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
380 		itaddr = myaddr;
381 		if (op == ARPOP_REQUEST)
382 			goto reply;
383 		goto out;
384 	}
385 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
386 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
387 		if (sdl->sdl_alen &&
388 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
389 			log(LOG_INFO, "arp info overwritten for %x by %s\n",
390 			    isaddr.s_addr, ether_sprintf(ea->arp_sha));
391 		completed = 1;
392 		bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
393 			    sdl->sdl_alen = sizeof(ea->arp_sha));
394 		if (rt->rt_expire)
395 			rt->rt_expire = time.tv_sec + arpt_keep;
396 		rt->rt_flags &= ~RTF_REJECT;
397 		la->la_asked = 0;
398 		if (la->la_hold) {
399 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
400 				rt_key(rt), rt);
401 			la->la_hold = 0;
402 		}
403 	}
404 reply:
405 	switch (proto) {
406 
407 	case ETHERTYPE_IPTRAILERS:
408 		/* partner says trailers are OK */
409 		if (la)
410 			la->la_rt->rt_flags |= RTF_USETRAILERS;
411 		/*
412 		 * Reply to request iff we want trailers.
413 		 */
414 		if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS)
415 			goto out;
416 		break;
417 
418 	case ETHERTYPE_IP:
419 		/*
420 		 * Reply if this is an IP request,
421 		 * or if we want to send a trailer response.
422 		 * Send the latter only to the IP response
423 		 * that completes the current ARP entry.
424 		 */
425 		if (op != ARPOP_REQUEST &&
426 		    (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS))
427 			goto out;
428 	}
429 	if (itaddr.s_addr == myaddr.s_addr) {
430 		/* I am the target */
431 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
432 		    sizeof(ea->arp_sha));
433 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
434 		    sizeof(ea->arp_sha));
435 		sendtrailers = !(ac->ac_if.if_flags & IFF_NOTRAILERS);
436 	} else {
437 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
438 		if (la == NULL)
439 			goto out;
440 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
441 		    sizeof(ea->arp_sha));
442 		sdl = SDL(la->la_rt->rt_gateway);
443 		bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
444 		sendtrailers = rt->rt_flags & RTF_USETRAILERS;
445 	}
446 
447 	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa,
448 	    sizeof(ea->arp_spa));
449 	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa,
450 	    sizeof(ea->arp_spa));
451 	ea->arp_op = htons(ARPOP_REPLY);
452 	/*
453 	 * If incoming packet was an IP reply,
454 	 * we are sending a reply for type IPTRAILERS.
455 	 * If we are sending a reply for type IP
456 	 * and we want to receive trailers,
457 	 * send a trailer reply as well.
458 	 */
459 	if (op == ARPOP_REPLY)
460 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
461 	else if (proto == ETHERTYPE_IP && sendtrailers)
462 		mcopy = m_copy(m, 0, (int)M_COPYALL);
463 	eh = (struct ether_header *)sa.sa_data;
464 	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
465 	    sizeof(eh->ether_dhost));
466 	eh->ether_type = ETHERTYPE_ARP;
467 	sa.sa_family = AF_UNSPEC;
468 	sa.sa_len = sizeof(sa);
469 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
470 	if (mcopy) {
471 		ea = mtod(mcopy, struct ether_arp *);
472 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
473 		(*ac->ac_if.if_output)(&ac->ac_if,
474 					mcopy, &sa, (struct rtentry *)0);
475 	}
476 	return;
477 out:
478 	m_freem(m);
479 	return;
480 }
481 
482 /*
483  * Free an arp entry.
484  */
485 arptfree(la)
486 	register struct llinfo_arp *la;
487 {
488 	register struct rtentry *rt = la->la_rt;
489 	register struct sockaddr_dl *sdl;
490 	if (rt == 0)
491 		panic("arptfree");
492 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
493 	    sdl->sdl_family == AF_LINK) {
494 		sdl->sdl_alen = 0;
495 		la->la_asked = 0;
496 		rt->rt_flags &= ~RTF_REJECT;
497 		return;
498 	}
499 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
500 			0, (struct rtentry **)0);
501 }
502 int arpdebug = 0;
503 /*
504  * Lookup or enter a new address in arptab.
505  */
506 struct llinfo_arp *
507 arplookup(addr, create, proxy)
508 	u_long addr;
509 {
510 	register struct rtentry *rt;
511 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
512 
513 	sin.sin_addr.s_addr = addr;
514 	sin.sin_other = proxy ? SIN_PROXY : 0;
515 	rt = rtalloc1((struct sockaddr *)&sin, create);
516 	if (rt == 0)
517 		return (0);
518 	rt->rt_refcnt--;
519 	if ((rt->rt_flags & RTF_GATEWAY) || !(rt->rt_flags & RTF_LLINFO) ||
520 		rt->rt_gateway->sa_family != AF_LINK) {
521 		arpcatchme();
522 		if (arpdebug)
523 			log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr));
524 		return (0);
525 	}
526 	return ((struct llinfo_arp *)rt->rt_llinfo);
527 }
528 
529 arpcatchme(){}
530 
531 arpioctl(cmd, data)
532 	int cmd;
533 	caddr_t data;
534 {
535 	return (EOPNOTSUPP);
536 }
537