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: if_ether.c,v 1.57 1999/04/15 17:58:24 eivind Exp $
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 "opt_inet.h"
44#include "opt_bdg.h"
45
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/queue.h>
49#include <sys/sysctl.h>
50#include <sys/systm.h>
51#include <sys/mbuf.h>
52#include <sys/malloc.h>
53#include <sys/socket.h>
54#include <sys/syslog.h>
55
56#include <net/if.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/route.h>
60#include <net/netisr.h>
61
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#include <netinet/if_ether.h>
65
66#include <net/iso88025.h>
67
68#define SIN(s) ((struct sockaddr_in *)s)
69#define SDL(s) ((struct sockaddr_dl *)s)
70
71SYSCTL_DECL(_net_link_ether);
72SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
73
74/* timer values */
75static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
76static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
77static int arpt_down = 20; /* once declared down, don't send for 20 sec */
78
79SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
80 &arpt_prune, 0, "");
81SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
82 &arpt_keep, 0, "");
83SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
84 &arpt_down, 0, "");
85
86#define rt_expire rt_rmx.rmx_expire
87
88struct llinfo_arp {
89 LIST_ENTRY(llinfo_arp) la_le;
90 struct rtentry *la_rt;
91 struct mbuf *la_hold; /* last packet until resolved/timeout */
92 long la_asked; /* last time we QUERIED for this addr */
93#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
94};
95
96static LIST_HEAD(, llinfo_arp) llinfo_arp;
97
98struct ifqueue arpintrq = {0, 0, 0, 50};
99static int arp_inuse, arp_allocated;
100
101static int arp_maxtries = 5;
102static int useloopback = 1; /* use loopback interface for local traffic */
103static int arp_proxyall = 0;
104
105SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
106 &arp_maxtries, 0, "");
107SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
108 &useloopback, 0, "");
109SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
110 &arp_proxyall, 0, "");
111
112static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
113static void arprequest __P((struct arpcom *,
114 struct in_addr *, struct in_addr *, u_char *));
115static void arpintr __P((void));
116static void arptfree __P((struct llinfo_arp *));
117static void arptimer __P((void *));
118static struct llinfo_arp
119 *arplookup __P((u_long, int, int));
120#ifdef INET
121static void in_arpinput __P((struct mbuf *));
122#endif
123
124/*
125 * Timeout routine. Age arp_tab entries periodically.
126 */
127/* ARGSUSED */
128static void
129arptimer(ignored_arg)
130 void *ignored_arg;
131{
132 int s = splnet();
133 register struct llinfo_arp *la = llinfo_arp.lh_first;
134 struct llinfo_arp *ola;
135
136 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
137 while ((ola = la) != 0) {
138 register struct rtentry *rt = la->la_rt;
139 la = la->la_le.le_next;
140 if (rt->rt_expire && rt->rt_expire <= time_second)
141 arptfree(ola); /* timer has expired, clear */
142 }
143 splx(s);
144}
145
146/*
147 * Parallel to llc_rtrequest.
148 */
149static void
150arp_rtrequest(req, rt, sa)
151 int req;
152 register struct rtentry *rt;
153 struct sockaddr *sa;
154{
155 register struct sockaddr *gate = rt->rt_gateway;
156 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
157 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
158 static int arpinit_done;
159
160 if (!arpinit_done) {
161 arpinit_done = 1;
162 LIST_INIT(&llinfo_arp);
163 timeout(arptimer, (caddr_t)0, hz);
164 }
165 if (rt->rt_flags & RTF_GATEWAY)
166 return;
167 switch (req) {
168
169 case RTM_ADD:
170 /*
171 * XXX: If this is a manually added route to interface
172 * such as older version of routed or gated might provide,
173 * restore cloning bit.
174 */
175 if ((rt->rt_flags & RTF_HOST) == 0 &&
176 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
177 rt->rt_flags |= RTF_CLONING;
178 if (rt->rt_flags & RTF_CLONING) {
179 /*
180 * Case 1: This route should come from a route to iface.
181 */
182 rt_setgate(rt, rt_key(rt),
183 (struct sockaddr *)&null_sdl);
184 gate = rt->rt_gateway;
185 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
186 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
187 rt->rt_expire = time_second;
188 break;
189 }
190 /* Announce a new entry if requested. */
191 if (rt->rt_flags & RTF_ANNOUNCE)
192 arprequest((struct arpcom *)rt->rt_ifp,
193 &SIN(rt_key(rt))->sin_addr,
194 &SIN(rt_key(rt))->sin_addr,
195 (u_char *)LLADDR(SDL(gate)));
196 /*FALLTHROUGH*/
197 case RTM_RESOLVE:
198 if (gate->sa_family != AF_LINK ||
199 gate->sa_len < sizeof(null_sdl)) {
200 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
201 break;
202 }
203 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
204 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
205 if (la != 0)
206 break; /* This happens on a route change */
207 /*
208 * Case 2: This route may come from cloning, or a manual route
209 * add with a LL address.
210 */
211 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
212 rt->rt_llinfo = (caddr_t)la;
213 if (la == 0) {
214 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
215 break;
216 }
217 arp_inuse++, arp_allocated++;
218 Bzero(la, sizeof(*la));
219 la->la_rt = rt;
220 rt->rt_flags |= RTF_LLINFO;
221 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
222
223#ifdef INET
224 /*
225 * This keeps the multicast addresses from showing up
226 * in `arp -a' listings as unresolved. It's not actually
227 * functional. Then the same for broadcast.
228 */
229 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
230 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
231 LLADDR(SDL(gate)));
232 SDL(gate)->sdl_alen = 6;
233 rt->rt_expire = 0;
234 }
235 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
236 memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
237 SDL(gate)->sdl_alen = 6;
238 rt->rt_expire = 0;
239 }
240#endif
241
242 if (SIN(rt_key(rt))->sin_addr.s_addr ==
243 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
244 /*
245 * This test used to be
246 * if (loif.if_flags & IFF_UP)
247 * It allowed local traffic to be forced
248 * through the hardware by configuring the loopback down.
249 * However, it causes problems during network configuration
250 * for boards that can't receive packets they send.
251 * It is now necessary to clear "useloopback" and remove
252 * the route to force traffic out to the hardware.
253 */
254 rt->rt_expire = 0;
255 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
256 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
257 if (useloopback)
258 rt->rt_ifp = loif;
259
260 }
261 break;
262
263 case RTM_DELETE:
264 if (la == 0)
265 break;
266 arp_inuse--;
267 LIST_REMOVE(la, la_le);
268 rt->rt_llinfo = 0;
269 rt->rt_flags &= ~RTF_LLINFO;
270 if (la->la_hold)
271 m_freem(la->la_hold);
272 Free((caddr_t)la);
273 }
274}
275
276/*
277 * Broadcast an ARP request. Caller specifies:
278 * - arp header source ip address
279 * - arp header target ip address
280 * - arp header source ethernet address
281 */
282static void
283arprequest(ac, sip, tip, enaddr)
284 register struct arpcom *ac;
285 register struct in_addr *sip, *tip;
286 register u_char *enaddr;
287{
288 register struct mbuf *m;
289 register struct ether_header *eh;
290 register struct ether_arp *ea;
291 struct sockaddr sa;
292
293 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
294 return;
295 m->m_pkthdr.rcvif = (struct ifnet *)0;
296 switch (ac->ac_if.if_type) {
297 case IFT_ETHER:
298 m->m_len = sizeof(*ea);
299 m->m_pkthdr.len = sizeof(*ea);
300 MH_ALIGN(m, sizeof(*ea));
301 ea = mtod(m, struct ether_arp *);
302 eh = (struct ether_header *)sa.sa_data;
303 bzero((caddr_t)ea, sizeof (*ea));
304 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */
305 (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
306 ea->arp_hrd = htons(ARPHRD_ETHER);
307 break;
308 case IFT_ISO88025:
309 m->m_len = sizeof(*ea) + 10;
310 m->m_pkthdr.len = sizeof(*ea) + 10;
311 MH_ALIGN(m, sizeof(*ea) + 10);
312 (void)memcpy(mtod(m, caddr_t), "\x82\x40\xaa\xaa\x03\x00\x00\x00\x08\x06", 10);
313 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
314 (void)memcpy(sa.sa_data + 6, enaddr, 6);
315 sa.sa_data[6] |= 0x80;
316 sa.sa_data[12] = 0x10;
317 sa.sa_data[13] = 0x40;
318 ea = (struct ether_arp *)(mtod(m, char *) + 10);
319 bzero((caddr_t)ea, sizeof (*ea));
320 ea->arp_hrd = htons(ARPHRD_IEEE802);
321 break;
322 default:
323 m_freem(m);
324 return;
325 }
326 ea->arp_pro = htons(ETHERTYPE_IP);
327 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
328 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
329 ea->arp_op = htons(ARPOP_REQUEST);
330 (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
331 (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
332 (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
333 sa.sa_family = AF_UNSPEC;
334 sa.sa_len = sizeof(sa);
335 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
336}
337
338/*
339 * Resolve an IP address into an ethernet address. If success,
340 * desten is filled in. If there is no entry in arptab,
341 * set one up and broadcast a request for the IP address.
342 * Hold onto this mbuf and resend it once the address
343 * is finally resolved. A return value of 1 indicates
344 * that desten has been filled in and the packet should be sent
345 * normally; a 0 return indicates that the packet has been
346 * taken over here, either now or for later transmission.
347 */
348int
349arpresolve(ac, rt, m, dst, desten, rt0)
350 register struct arpcom *ac;
351 register struct rtentry *rt;
352 struct mbuf *m;
353 register struct sockaddr *dst;
354 register u_char *desten;
355 struct rtentry *rt0;
356{
357 register struct llinfo_arp *la = 0;
358 struct sockaddr_dl *sdl;
359
360 if (m->m_flags & M_BCAST) { /* broadcast */
361 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
362 return (1);
363 }
364 if (m->m_flags & M_MCAST) { /* multicast */
365 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
366 return(1);
367 }
368 if (rt)
369 la = (struct llinfo_arp *)rt->rt_llinfo;
370 if (la == 0) {
371 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
372 if (la)
373 rt = la->la_rt;
374 }
375 if (la == 0 || rt == 0) {
376 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
377 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
378 rt ? "rt" : "");
379 m_freem(m);
380 return (0);
381 }
382 sdl = SDL(rt->rt_gateway);
383 /*
384 * Check the address family and length is valid, the address
385 * is resolved; otherwise, try to resolve.
386 */
387 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
388 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
389 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
390 return 1;
391 }
392 /*
393 * There is an arptab entry, but no ethernet address
394 * response yet. Replace the held mbuf with this
395 * latest one.
396 */
397 if (la->la_hold)
398 m_freem(la->la_hold);
399 la->la_hold = m;
400 if (rt->rt_expire) {
401 rt->rt_flags &= ~RTF_REJECT;
402 if (la->la_asked == 0 || rt->rt_expire != time_second) {
403 rt->rt_expire = time_second;
404 if (la->la_asked++ < arp_maxtries)
405 arprequest(ac,
406 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
407 &SIN(dst)->sin_addr, ac->ac_enaddr);
408 else {
409 rt->rt_flags |= RTF_REJECT;
410 rt->rt_expire += arpt_down;
411 la->la_asked = 0;
412 }
413
414 }
415 }
416 return (0);
417}
418
419/*
420 * Common length and type checks are done here,
421 * then the protocol-specific routine is called.
422 */
423static void
424arpintr()
425{
426 register struct mbuf *m;
427 register struct arphdr *ar;
428 int s;
429
430 while (arpintrq.ifq_head) {
431 s = splimp();
432 IF_DEQUEUE(&arpintrq, m);
433 splx(s);
434 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
435 panic("arpintr");
436 if (m->m_len >= sizeof(struct arphdr) &&
437 (ar = mtod(m, struct arphdr *)) &&
438 (ntohs(ar->ar_hrd) == ARPHRD_ETHER ||
439 ntohs(ar->ar_hrd) == ARPHRD_IEEE802) &&
440 m->m_len >=
441 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
442
443 switch (ntohs(ar->ar_pro)) {
444
445#ifdef INET
446 case ETHERTYPE_IP:
447 in_arpinput(m);
448 continue;
449#endif
450 }
451 m_freem(m);
452 }
453}
454
455NETISR_SET(NETISR_ARP, arpintr);
456
457
458#ifdef INET
459/*
460 * ARP for Internet protocols on 10 Mb/s Ethernet.
461 * Algorithm is that given in RFC 826.
462 * In addition, a sanity check is performed on the sender
463 * protocol address, to catch impersonators.
464 * We no longer handle negotiations for use of trailer protocol:
465 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
466 * along with IP replies if we wanted trailers sent to us,
467 * and also sent them in response to IP replies.
468 * This allowed either end to announce the desire to receive
469 * trailer packets.
470 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
471 * but formerly didn't normally send requests.
472 */
473static void
474in_arpinput(m)
475 struct mbuf *m;
476{
477 register struct ether_arp *ea;
478 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
479 struct ether_header *eh;
480 struct iso88025_header *th = (struct iso88025_header *)0;
481 register struct llinfo_arp *la = 0;
482 register struct rtentry *rt;
483 struct in_ifaddr *ia, *maybe_ia = 0;
484 struct sockaddr_dl *sdl;
485 struct sockaddr sa;
486 struct in_addr isaddr, itaddr, myaddr;
487 int op;
488
489 ea = mtod(m, struct ether_arp *);
490 op = ntohs(ea->arp_op);
491 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
492 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
493 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
494#ifdef BRIDGE
495 /*
496 * For a bridge, we want to check the address irrespective
497 * of the receive interface. (This will change slightly
498 * when we have clusters of interfaces).
499 */
500 {
501#else
502 if (ia->ia_ifp == &ac->ac_if) {
503#endif
504 maybe_ia = ia;
505 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
506 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
507 break;
508 }
509 if (maybe_ia == 0) {
510 m_freem(m);
511 return;
512 }
513 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
514 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
515 sizeof (ea->arp_sha))) {
516 m_freem(m); /* it's from me, ignore it. */
517 return;
518 }
519 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
520 sizeof (ea->arp_sha))) {
521 log(LOG_ERR,
522 "arp: ether address is broadcast for IP address %s!\n",
523 inet_ntoa(isaddr));
524 m_freem(m);
525 return;
526 }
527 if (isaddr.s_addr == myaddr.s_addr) {
528 log(LOG_ERR,
529 "arp: %6D is using my IP address %s!\n",
530 ea->arp_sha, ":", inet_ntoa(isaddr));
531 itaddr = myaddr;
532 goto reply;
533 }
534 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
535 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
536#ifndef BRIDGE /* the following is not an error when doing bridging */
537 if (rt->rt_ifp != &ac->ac_if) {
538 log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
539 inet_ntoa(isaddr),
540 rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
541 ea->arp_sha, ":",
542 ac->ac_if.if_name, ac->ac_if.if_unit);
543 goto reply;
544 }
545#endif
546 if (sdl->sdl_alen &&
547 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
548 if (rt->rt_expire)
549 log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
550 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
551 ea->arp_sha, ":",
552 ac->ac_if.if_name, ac->ac_if.if_unit);
553 else {
554 log(LOG_ERR,
555 "arp: %6D attempts to modify permanent entry for %s on %s%d",
556 ea->arp_sha, ":", inet_ntoa(isaddr),
557 ac->ac_if.if_name, ac->ac_if.if_unit);
558 goto reply;
559 }
560 }
561 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
562 sdl->sdl_alen = sizeof(ea->arp_sha);
563 sdl->sdl_rcf = NULL;
564 /*
565 * If we receive an arp from a token-ring station over
566 * a token-ring nic then try to save the source
567 * routing info.
568 */
569 if (ac->ac_if.if_type == IFT_ISO88025) {
570 th = (struct iso88025_header *)m->m_pkthdr.header;
571 if ((th->iso88025_shost[0] & 0x80) &&
572 (((ntohs(th->rcf) & 0x1f00) >> 8) > 2)) {
573 sdl->sdl_rcf = ntohs(th->rcf) & 0x0080 ?
574 htons(ntohs(th->rcf) & 0xff7f) :
575 htons(ntohs(th->rcf) | 0x0080);
576 memcpy(sdl->sdl_route, th->rseg, ((ntohs(th->rcf) & 0x1f00) >> 8) - 2);
577 sdl->sdl_rcf = htons(ntohs(sdl->sdl_rcf) & 0x1fff);
578 /* Set up source routing information for reply packet (XXX)*/
579 m->m_data -= (((ntohs(th->rcf) & 0x1f00) >> 8) + 8);
580 m->m_len += (((ntohs(th->rcf) & 0x1f00) >> 8) + 8);
581 } else {
582 th->iso88025_shost[0] &= 0x7f;
583 m->m_data -= 8;
584 m->m_len += 8;
585 }
586 th->rcf = sdl->sdl_rcf;
587
588 } else {
589 sdl->sdl_rcf = NULL;
590 }
591 if (rt->rt_expire)
592 rt->rt_expire = time_second + arpt_keep;
593 rt->rt_flags &= ~RTF_REJECT;
594 la->la_asked = 0;
595 if (la->la_hold) {
596 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
597 rt_key(rt), rt);
598 la->la_hold = 0;
599 }
600 }
601reply:
602 if (op != ARPOP_REQUEST) {
603 m_freem(m);
604 return;
605 }
606 if (itaddr.s_addr == myaddr.s_addr) {
607 /* I am the target */
608 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
609 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
610 } else {
611 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
612 if (la == NULL) {
613 struct sockaddr_in sin;
614
615 if (!arp_proxyall) {
616 m_freem(m);
617 return;
618 }
619
620 bzero(&sin, sizeof sin);
621 sin.sin_family = AF_INET;
622 sin.sin_len = sizeof sin;
623 sin.sin_addr = itaddr;
624
625 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
626 if (!rt) {
627 m_freem(m);
628 return;
629 }
630 /*
631 * Don't send proxies for nodes on the same interface
632 * as this one came out of, or we'll get into a fight
633 * over who claims what Ether address.
634 */
635 if (rt->rt_ifp == &ac->ac_if) {
636 rtfree(rt);
637 m_freem(m);
638 return;
639 }
640 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
641 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
642 rtfree(rt);
643#ifdef DEBUG_PROXY
644 printf("arp: proxying for %s\n",
645 inet_ntoa(itaddr));
646#endif
647 } else {
648 rt = la->la_rt;
649 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
650 sdl = SDL(rt->rt_gateway);
651 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
652 }
653 }
654
655 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
656 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
657 ea->arp_op = htons(ARPOP_REPLY);
658 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
659 switch (ac->ac_if.if_type) {
660 case IFT_ISO88025:
661 /* Re-arrange the source/dest address */
662 memcpy(th->iso88025_dhost, th->iso88025_shost, sizeof(th->iso88025_dhost));
663 memcpy(th->iso88025_shost, ac->ac_enaddr, sizeof(th->iso88025_shost));
664 /* Set the source routing bit if neccesary */
665 if (th->iso88025_dhost[0] & 0x80) {
666 th->iso88025_dhost[0] &= 0x7f;
667 if (((ntohs(th->rcf) & 0x1f00) >> 8) - 2)
668 th->iso88025_shost[0] |= 0x80;
669 }
670 /* Copy the addresses, ac and fc into sa_data */
671 memcpy(sa.sa_data, th->iso88025_dhost, sizeof(th->iso88025_dhost) * 2);
672 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = 0x10;
673 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = 0x40;
674 break;
675 case IFT_ETHER:
676 eh = (struct ether_header *)sa.sa_data;
677 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
678 eh->ether_type = htons(ETHERTYPE_ARP);
679 break;
680 default:
681 m_free(m);
682 return;
683 }
684 sa.sa_family = AF_UNSPEC;
685 sa.sa_len = sizeof(sa);
686 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
687 return;
688}
689#endif
690
691/*
692 * Free an arp entry.
693 */
694static void
695arptfree(la)
696 register struct llinfo_arp *la;
697{
698 register struct rtentry *rt = la->la_rt;
699 register struct sockaddr_dl *sdl;
700 if (rt == 0)
701 panic("arptfree");
702 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
703 sdl->sdl_family == AF_LINK) {
704 sdl->sdl_alen = 0;
705 la->la_asked = 0;
706 rt->rt_flags &= ~RTF_REJECT;
707 return;
708 }
709 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
710 0, (struct rtentry **)0);
711}
712/*
713 * Lookup or enter a new address in arptab.
714 */
715static struct llinfo_arp *
716arplookup(addr, create, proxy)
717 u_long addr;
718 int create, proxy;
719{
720 register struct rtentry *rt;
721 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
722 const char *why = 0;
723
724 sin.sin_addr.s_addr = addr;
725 sin.sin_other = proxy ? SIN_PROXY : 0;
726 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
727 if (rt == 0)
728 return (0);
729 rt->rt_refcnt--;
730
731 if (rt->rt_flags & RTF_GATEWAY)
732 why = "host is not on local network";
733 else if ((rt->rt_flags & RTF_LLINFO) == 0)
734 why = "could not allocate llinfo";
735 else if (rt->rt_gateway->sa_family != AF_LINK)
736 why = "gateway route is not ours";
737
738 if (why && create) {
739 log(LOG_DEBUG, "arplookup %s failed: %s\n",
740 inet_ntoa(sin.sin_addr), why);
741 return 0;
742 } else if (why) {
743 return 0;
744 }
745 return ((struct llinfo_arp *)rt->rt_llinfo);
746}
747
748void
749arp_ifinit(ac, ifa)
750 struct arpcom *ac;
751 struct ifaddr *ifa;
752{
753 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
754 arprequest(ac, &IA_SIN(ifa)->sin_addr,
755 &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
756 ifa->ifa_rtrequest = arp_rtrequest;
757 ifa->ifa_flags |= RTF_CLONING;
758}
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: if_ether.c,v 1.57 1999/04/15 17:58:24 eivind Exp $
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 "opt_inet.h"
44#include "opt_bdg.h"
45
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/queue.h>
49#include <sys/sysctl.h>
50#include <sys/systm.h>
51#include <sys/mbuf.h>
52#include <sys/malloc.h>
53#include <sys/socket.h>
54#include <sys/syslog.h>
55
56#include <net/if.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/route.h>
60#include <net/netisr.h>
61
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#include <netinet/if_ether.h>
65
66#include <net/iso88025.h>
67
68#define SIN(s) ((struct sockaddr_in *)s)
69#define SDL(s) ((struct sockaddr_dl *)s)
70
71SYSCTL_DECL(_net_link_ether);
72SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
73
74/* timer values */
75static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
76static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
77static int arpt_down = 20; /* once declared down, don't send for 20 sec */
78
79SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
80 &arpt_prune, 0, "");
81SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
82 &arpt_keep, 0, "");
83SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
84 &arpt_down, 0, "");
85
86#define rt_expire rt_rmx.rmx_expire
87
88struct llinfo_arp {
89 LIST_ENTRY(llinfo_arp) la_le;
90 struct rtentry *la_rt;
91 struct mbuf *la_hold; /* last packet until resolved/timeout */
92 long la_asked; /* last time we QUERIED for this addr */
93#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
94};
95
96static LIST_HEAD(, llinfo_arp) llinfo_arp;
97
98struct ifqueue arpintrq = {0, 0, 0, 50};
99static int arp_inuse, arp_allocated;
100
101static int arp_maxtries = 5;
102static int useloopback = 1; /* use loopback interface for local traffic */
103static int arp_proxyall = 0;
104
105SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
106 &arp_maxtries, 0, "");
107SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
108 &useloopback, 0, "");
109SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
110 &arp_proxyall, 0, "");
111
112static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
113static void arprequest __P((struct arpcom *,
114 struct in_addr *, struct in_addr *, u_char *));
115static void arpintr __P((void));
116static void arptfree __P((struct llinfo_arp *));
117static void arptimer __P((void *));
118static struct llinfo_arp
119 *arplookup __P((u_long, int, int));
120#ifdef INET
121static void in_arpinput __P((struct mbuf *));
122#endif
123
124/*
125 * Timeout routine. Age arp_tab entries periodically.
126 */
127/* ARGSUSED */
128static void
129arptimer(ignored_arg)
130 void *ignored_arg;
131{
132 int s = splnet();
133 register struct llinfo_arp *la = llinfo_arp.lh_first;
134 struct llinfo_arp *ola;
135
136 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
137 while ((ola = la) != 0) {
138 register struct rtentry *rt = la->la_rt;
139 la = la->la_le.le_next;
140 if (rt->rt_expire && rt->rt_expire <= time_second)
141 arptfree(ola); /* timer has expired, clear */
142 }
143 splx(s);
144}
145
146/*
147 * Parallel to llc_rtrequest.
148 */
149static void
150arp_rtrequest(req, rt, sa)
151 int req;
152 register struct rtentry *rt;
153 struct sockaddr *sa;
154{
155 register struct sockaddr *gate = rt->rt_gateway;
156 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
157 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
158 static int arpinit_done;
159
160 if (!arpinit_done) {
161 arpinit_done = 1;
162 LIST_INIT(&llinfo_arp);
163 timeout(arptimer, (caddr_t)0, hz);
164 }
165 if (rt->rt_flags & RTF_GATEWAY)
166 return;
167 switch (req) {
168
169 case RTM_ADD:
170 /*
171 * XXX: If this is a manually added route to interface
172 * such as older version of routed or gated might provide,
173 * restore cloning bit.
174 */
175 if ((rt->rt_flags & RTF_HOST) == 0 &&
176 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
177 rt->rt_flags |= RTF_CLONING;
178 if (rt->rt_flags & RTF_CLONING) {
179 /*
180 * Case 1: This route should come from a route to iface.
181 */
182 rt_setgate(rt, rt_key(rt),
183 (struct sockaddr *)&null_sdl);
184 gate = rt->rt_gateway;
185 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
186 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
187 rt->rt_expire = time_second;
188 break;
189 }
190 /* Announce a new entry if requested. */
191 if (rt->rt_flags & RTF_ANNOUNCE)
192 arprequest((struct arpcom *)rt->rt_ifp,
193 &SIN(rt_key(rt))->sin_addr,
194 &SIN(rt_key(rt))->sin_addr,
195 (u_char *)LLADDR(SDL(gate)));
196 /*FALLTHROUGH*/
197 case RTM_RESOLVE:
198 if (gate->sa_family != AF_LINK ||
199 gate->sa_len < sizeof(null_sdl)) {
200 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
201 break;
202 }
203 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
204 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
205 if (la != 0)
206 break; /* This happens on a route change */
207 /*
208 * Case 2: This route may come from cloning, or a manual route
209 * add with a LL address.
210 */
211 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
212 rt->rt_llinfo = (caddr_t)la;
213 if (la == 0) {
214 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
215 break;
216 }
217 arp_inuse++, arp_allocated++;
218 Bzero(la, sizeof(*la));
219 la->la_rt = rt;
220 rt->rt_flags |= RTF_LLINFO;
221 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
222
223#ifdef INET
224 /*
225 * This keeps the multicast addresses from showing up
226 * in `arp -a' listings as unresolved. It's not actually
227 * functional. Then the same for broadcast.
228 */
229 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
230 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
231 LLADDR(SDL(gate)));
232 SDL(gate)->sdl_alen = 6;
233 rt->rt_expire = 0;
234 }
235 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
236 memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
237 SDL(gate)->sdl_alen = 6;
238 rt->rt_expire = 0;
239 }
240#endif
241
242 if (SIN(rt_key(rt))->sin_addr.s_addr ==
243 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
244 /*
245 * This test used to be
246 * if (loif.if_flags & IFF_UP)
247 * It allowed local traffic to be forced
248 * through the hardware by configuring the loopback down.
249 * However, it causes problems during network configuration
250 * for boards that can't receive packets they send.
251 * It is now necessary to clear "useloopback" and remove
252 * the route to force traffic out to the hardware.
253 */
254 rt->rt_expire = 0;
255 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
256 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
257 if (useloopback)
258 rt->rt_ifp = loif;
259
260 }
261 break;
262
263 case RTM_DELETE:
264 if (la == 0)
265 break;
266 arp_inuse--;
267 LIST_REMOVE(la, la_le);
268 rt->rt_llinfo = 0;
269 rt->rt_flags &= ~RTF_LLINFO;
270 if (la->la_hold)
271 m_freem(la->la_hold);
272 Free((caddr_t)la);
273 }
274}
275
276/*
277 * Broadcast an ARP request. Caller specifies:
278 * - arp header source ip address
279 * - arp header target ip address
280 * - arp header source ethernet address
281 */
282static void
283arprequest(ac, sip, tip, enaddr)
284 register struct arpcom *ac;
285 register struct in_addr *sip, *tip;
286 register u_char *enaddr;
287{
288 register struct mbuf *m;
289 register struct ether_header *eh;
290 register struct ether_arp *ea;
291 struct sockaddr sa;
292
293 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
294 return;
295 m->m_pkthdr.rcvif = (struct ifnet *)0;
296 switch (ac->ac_if.if_type) {
297 case IFT_ETHER:
298 m->m_len = sizeof(*ea);
299 m->m_pkthdr.len = sizeof(*ea);
300 MH_ALIGN(m, sizeof(*ea));
301 ea = mtod(m, struct ether_arp *);
302 eh = (struct ether_header *)sa.sa_data;
303 bzero((caddr_t)ea, sizeof (*ea));
304 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */
305 (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
306 ea->arp_hrd = htons(ARPHRD_ETHER);
307 break;
308 case IFT_ISO88025:
309 m->m_len = sizeof(*ea) + 10;
310 m->m_pkthdr.len = sizeof(*ea) + 10;
311 MH_ALIGN(m, sizeof(*ea) + 10);
312 (void)memcpy(mtod(m, caddr_t), "\x82\x40\xaa\xaa\x03\x00\x00\x00\x08\x06", 10);
313 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
314 (void)memcpy(sa.sa_data + 6, enaddr, 6);
315 sa.sa_data[6] |= 0x80;
316 sa.sa_data[12] = 0x10;
317 sa.sa_data[13] = 0x40;
318 ea = (struct ether_arp *)(mtod(m, char *) + 10);
319 bzero((caddr_t)ea, sizeof (*ea));
320 ea->arp_hrd = htons(ARPHRD_IEEE802);
321 break;
322 default:
323 m_freem(m);
324 return;
325 }
326 ea->arp_pro = htons(ETHERTYPE_IP);
327 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
328 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
329 ea->arp_op = htons(ARPOP_REQUEST);
330 (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
331 (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
332 (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
333 sa.sa_family = AF_UNSPEC;
334 sa.sa_len = sizeof(sa);
335 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
336}
337
338/*
339 * Resolve an IP address into an ethernet address. If success,
340 * desten is filled in. If there is no entry in arptab,
341 * set one up and broadcast a request for the IP address.
342 * Hold onto this mbuf and resend it once the address
343 * is finally resolved. A return value of 1 indicates
344 * that desten has been filled in and the packet should be sent
345 * normally; a 0 return indicates that the packet has been
346 * taken over here, either now or for later transmission.
347 */
348int
349arpresolve(ac, rt, m, dst, desten, rt0)
350 register struct arpcom *ac;
351 register struct rtentry *rt;
352 struct mbuf *m;
353 register struct sockaddr *dst;
354 register u_char *desten;
355 struct rtentry *rt0;
356{
357 register struct llinfo_arp *la = 0;
358 struct sockaddr_dl *sdl;
359
360 if (m->m_flags & M_BCAST) { /* broadcast */
361 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
362 return (1);
363 }
364 if (m->m_flags & M_MCAST) { /* multicast */
365 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
366 return(1);
367 }
368 if (rt)
369 la = (struct llinfo_arp *)rt->rt_llinfo;
370 if (la == 0) {
371 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
372 if (la)
373 rt = la->la_rt;
374 }
375 if (la == 0 || rt == 0) {
376 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
377 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
378 rt ? "rt" : "");
379 m_freem(m);
380 return (0);
381 }
382 sdl = SDL(rt->rt_gateway);
383 /*
384 * Check the address family and length is valid, the address
385 * is resolved; otherwise, try to resolve.
386 */
387 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
388 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
389 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
390 return 1;
391 }
392 /*
393 * There is an arptab entry, but no ethernet address
394 * response yet. Replace the held mbuf with this
395 * latest one.
396 */
397 if (la->la_hold)
398 m_freem(la->la_hold);
399 la->la_hold = m;
400 if (rt->rt_expire) {
401 rt->rt_flags &= ~RTF_REJECT;
402 if (la->la_asked == 0 || rt->rt_expire != time_second) {
403 rt->rt_expire = time_second;
404 if (la->la_asked++ < arp_maxtries)
405 arprequest(ac,
406 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
407 &SIN(dst)->sin_addr, ac->ac_enaddr);
408 else {
409 rt->rt_flags |= RTF_REJECT;
410 rt->rt_expire += arpt_down;
411 la->la_asked = 0;
412 }
413
414 }
415 }
416 return (0);
417}
418
419/*
420 * Common length and type checks are done here,
421 * then the protocol-specific routine is called.
422 */
423static void
424arpintr()
425{
426 register struct mbuf *m;
427 register struct arphdr *ar;
428 int s;
429
430 while (arpintrq.ifq_head) {
431 s = splimp();
432 IF_DEQUEUE(&arpintrq, m);
433 splx(s);
434 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
435 panic("arpintr");
436 if (m->m_len >= sizeof(struct arphdr) &&
437 (ar = mtod(m, struct arphdr *)) &&
438 (ntohs(ar->ar_hrd) == ARPHRD_ETHER ||
439 ntohs(ar->ar_hrd) == ARPHRD_IEEE802) &&
440 m->m_len >=
441 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
442
443 switch (ntohs(ar->ar_pro)) {
444
445#ifdef INET
446 case ETHERTYPE_IP:
447 in_arpinput(m);
448 continue;
449#endif
450 }
451 m_freem(m);
452 }
453}
454
455NETISR_SET(NETISR_ARP, arpintr);
456
457
458#ifdef INET
459/*
460 * ARP for Internet protocols on 10 Mb/s Ethernet.
461 * Algorithm is that given in RFC 826.
462 * In addition, a sanity check is performed on the sender
463 * protocol address, to catch impersonators.
464 * We no longer handle negotiations for use of trailer protocol:
465 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
466 * along with IP replies if we wanted trailers sent to us,
467 * and also sent them in response to IP replies.
468 * This allowed either end to announce the desire to receive
469 * trailer packets.
470 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
471 * but formerly didn't normally send requests.
472 */
473static void
474in_arpinput(m)
475 struct mbuf *m;
476{
477 register struct ether_arp *ea;
478 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
479 struct ether_header *eh;
480 struct iso88025_header *th = (struct iso88025_header *)0;
481 register struct llinfo_arp *la = 0;
482 register struct rtentry *rt;
483 struct in_ifaddr *ia, *maybe_ia = 0;
484 struct sockaddr_dl *sdl;
485 struct sockaddr sa;
486 struct in_addr isaddr, itaddr, myaddr;
487 int op;
488
489 ea = mtod(m, struct ether_arp *);
490 op = ntohs(ea->arp_op);
491 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
492 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
493 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
494#ifdef BRIDGE
495 /*
496 * For a bridge, we want to check the address irrespective
497 * of the receive interface. (This will change slightly
498 * when we have clusters of interfaces).
499 */
500 {
501#else
502 if (ia->ia_ifp == &ac->ac_if) {
503#endif
504 maybe_ia = ia;
505 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
506 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
507 break;
508 }
509 if (maybe_ia == 0) {
510 m_freem(m);
511 return;
512 }
513 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
514 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
515 sizeof (ea->arp_sha))) {
516 m_freem(m); /* it's from me, ignore it. */
517 return;
518 }
519 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
520 sizeof (ea->arp_sha))) {
521 log(LOG_ERR,
522 "arp: ether address is broadcast for IP address %s!\n",
523 inet_ntoa(isaddr));
524 m_freem(m);
525 return;
526 }
527 if (isaddr.s_addr == myaddr.s_addr) {
528 log(LOG_ERR,
529 "arp: %6D is using my IP address %s!\n",
530 ea->arp_sha, ":", inet_ntoa(isaddr));
531 itaddr = myaddr;
532 goto reply;
533 }
534 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
535 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
536#ifndef BRIDGE /* the following is not an error when doing bridging */
537 if (rt->rt_ifp != &ac->ac_if) {
538 log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
539 inet_ntoa(isaddr),
540 rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
541 ea->arp_sha, ":",
542 ac->ac_if.if_name, ac->ac_if.if_unit);
543 goto reply;
544 }
545#endif
546 if (sdl->sdl_alen &&
547 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
548 if (rt->rt_expire)
549 log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
550 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
551 ea->arp_sha, ":",
552 ac->ac_if.if_name, ac->ac_if.if_unit);
553 else {
554 log(LOG_ERR,
555 "arp: %6D attempts to modify permanent entry for %s on %s%d",
556 ea->arp_sha, ":", inet_ntoa(isaddr),
557 ac->ac_if.if_name, ac->ac_if.if_unit);
558 goto reply;
559 }
560 }
561 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
562 sdl->sdl_alen = sizeof(ea->arp_sha);
563 sdl->sdl_rcf = NULL;
564 /*
565 * If we receive an arp from a token-ring station over
566 * a token-ring nic then try to save the source
567 * routing info.
568 */
569 if (ac->ac_if.if_type == IFT_ISO88025) {
570 th = (struct iso88025_header *)m->m_pkthdr.header;
571 if ((th->iso88025_shost[0] & 0x80) &&
572 (((ntohs(th->rcf) & 0x1f00) >> 8) > 2)) {
573 sdl->sdl_rcf = ntohs(th->rcf) & 0x0080 ?
574 htons(ntohs(th->rcf) & 0xff7f) :
575 htons(ntohs(th->rcf) | 0x0080);
576 memcpy(sdl->sdl_route, th->rseg, ((ntohs(th->rcf) & 0x1f00) >> 8) - 2);
577 sdl->sdl_rcf = htons(ntohs(sdl->sdl_rcf) & 0x1fff);
578 /* Set up source routing information for reply packet (XXX)*/
579 m->m_data -= (((ntohs(th->rcf) & 0x1f00) >> 8) + 8);
580 m->m_len += (((ntohs(th->rcf) & 0x1f00) >> 8) + 8);
581 } else {
582 th->iso88025_shost[0] &= 0x7f;
583 m->m_data -= 8;
584 m->m_len += 8;
585 }
586 th->rcf = sdl->sdl_rcf;
587
588 } else {
589 sdl->sdl_rcf = NULL;
590 }
591 if (rt->rt_expire)
592 rt->rt_expire = time_second + arpt_keep;
593 rt->rt_flags &= ~RTF_REJECT;
594 la->la_asked = 0;
595 if (la->la_hold) {
596 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
597 rt_key(rt), rt);
598 la->la_hold = 0;
599 }
600 }
601reply:
602 if (op != ARPOP_REQUEST) {
603 m_freem(m);
604 return;
605 }
606 if (itaddr.s_addr == myaddr.s_addr) {
607 /* I am the target */
608 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
609 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
610 } else {
611 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
612 if (la == NULL) {
613 struct sockaddr_in sin;
614
615 if (!arp_proxyall) {
616 m_freem(m);
617 return;
618 }
619
620 bzero(&sin, sizeof sin);
621 sin.sin_family = AF_INET;
622 sin.sin_len = sizeof sin;
623 sin.sin_addr = itaddr;
624
625 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
626 if (!rt) {
627 m_freem(m);
628 return;
629 }
630 /*
631 * Don't send proxies for nodes on the same interface
632 * as this one came out of, or we'll get into a fight
633 * over who claims what Ether address.
634 */
635 if (rt->rt_ifp == &ac->ac_if) {
636 rtfree(rt);
637 m_freem(m);
638 return;
639 }
640 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
641 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
642 rtfree(rt);
643#ifdef DEBUG_PROXY
644 printf("arp: proxying for %s\n",
645 inet_ntoa(itaddr));
646#endif
647 } else {
648 rt = la->la_rt;
649 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
650 sdl = SDL(rt->rt_gateway);
651 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
652 }
653 }
654
655 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
656 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
657 ea->arp_op = htons(ARPOP_REPLY);
658 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
659 switch (ac->ac_if.if_type) {
660 case IFT_ISO88025:
661 /* Re-arrange the source/dest address */
662 memcpy(th->iso88025_dhost, th->iso88025_shost, sizeof(th->iso88025_dhost));
663 memcpy(th->iso88025_shost, ac->ac_enaddr, sizeof(th->iso88025_shost));
664 /* Set the source routing bit if neccesary */
665 if (th->iso88025_dhost[0] & 0x80) {
666 th->iso88025_dhost[0] &= 0x7f;
667 if (((ntohs(th->rcf) & 0x1f00) >> 8) - 2)
668 th->iso88025_shost[0] |= 0x80;
669 }
670 /* Copy the addresses, ac and fc into sa_data */
671 memcpy(sa.sa_data, th->iso88025_dhost, sizeof(th->iso88025_dhost) * 2);
672 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = 0x10;
673 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = 0x40;
674 break;
675 case IFT_ETHER:
676 eh = (struct ether_header *)sa.sa_data;
677 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
678 eh->ether_type = htons(ETHERTYPE_ARP);
679 break;
680 default:
681 m_free(m);
682 return;
683 }
684 sa.sa_family = AF_UNSPEC;
685 sa.sa_len = sizeof(sa);
686 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
687 return;
688}
689#endif
690
691/*
692 * Free an arp entry.
693 */
694static void
695arptfree(la)
696 register struct llinfo_arp *la;
697{
698 register struct rtentry *rt = la->la_rt;
699 register struct sockaddr_dl *sdl;
700 if (rt == 0)
701 panic("arptfree");
702 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
703 sdl->sdl_family == AF_LINK) {
704 sdl->sdl_alen = 0;
705 la->la_asked = 0;
706 rt->rt_flags &= ~RTF_REJECT;
707 return;
708 }
709 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
710 0, (struct rtentry **)0);
711}
712/*
713 * Lookup or enter a new address in arptab.
714 */
715static struct llinfo_arp *
716arplookup(addr, create, proxy)
717 u_long addr;
718 int create, proxy;
719{
720 register struct rtentry *rt;
721 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
722 const char *why = 0;
723
724 sin.sin_addr.s_addr = addr;
725 sin.sin_other = proxy ? SIN_PROXY : 0;
726 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
727 if (rt == 0)
728 return (0);
729 rt->rt_refcnt--;
730
731 if (rt->rt_flags & RTF_GATEWAY)
732 why = "host is not on local network";
733 else if ((rt->rt_flags & RTF_LLINFO) == 0)
734 why = "could not allocate llinfo";
735 else if (rt->rt_gateway->sa_family != AF_LINK)
736 why = "gateway route is not ours";
737
738 if (why && create) {
739 log(LOG_DEBUG, "arplookup %s failed: %s\n",
740 inet_ntoa(sin.sin_addr), why);
741 return 0;
742 } else if (why) {
743 return 0;
744 }
745 return ((struct llinfo_arp *)rt->rt_llinfo);
746}
747
748void
749arp_ifinit(ac, ifa)
750 struct arpcom *ac;
751 struct ifaddr *ifa;
752{
753 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
754 arprequest(ac, &IA_SIN(ifa)->sin_addr,
755 &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
756 ifa->ifa_rtrequest = arp_rtrequest;
757 ifa->ifa_flags |= RTF_CLONING;
758}