Permanently fix the 'allocating low physmem in early boot' problem which
[dragonfly.git] / sys / netinet / if_ether.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
42a7fc75 35 * $DragonFly: src/sys/netinet/if_ether.c,v 1.5 2003/08/23 11:18:00 rob Exp $
984263bc
MD
36 */
37
38/*
39 * Ethernet address resolution protocol.
40 * TODO:
41 * add "inuse/lock" bit (or ref. count) along with valid bit
42 */
43
44#include "opt_inet.h"
45#include "opt_bdg.h"
46
47#include <sys/param.h>
48#include <sys/kernel.h>
49#include <sys/queue.h>
50#include <sys/sysctl.h>
51#include <sys/systm.h>
52#include <sys/mbuf.h>
53#include <sys/malloc.h>
54#include <sys/socket.h>
55#include <sys/syslog.h>
56
57#include <net/if.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/route.h>
61#include <net/netisr.h>
62#include <net/if_llc.h>
63#ifdef BRIDGE
64#include <net/ethernet.h>
d2438d69 65#include <net/bridge/bridge.h>
984263bc
MD
66#endif
67
68#include <netinet/in.h>
69#include <netinet/in_var.h>
70#include <netinet/if_ether.h>
71
72#include <net/if_arc.h>
73#include <net/iso88025.h>
74
75#define SIN(s) ((struct sockaddr_in *)s)
76#define SDL(s) ((struct sockaddr_dl *)s)
77
78SYSCTL_DECL(_net_link_ether);
79SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80
81/* timer values */
82static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
83static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84static int arpt_down = 20; /* once declared down, don't send for 20 sec */
85
86SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
87 &arpt_prune, 0, "");
88SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
89 &arpt_keep, 0, "");
90SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
91 &arpt_down, 0, "");
92
93#define rt_expire rt_rmx.rmx_expire
94
95struct llinfo_arp {
96 LIST_ENTRY(llinfo_arp) la_le;
97 struct rtentry *la_rt;
98 struct mbuf *la_hold; /* last packet until resolved/timeout */
99 u_short la_preempt; /* #times we QUERIED before entry expiration */
100 u_short la_asked; /* #times we QUERIED following expiration */
101#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
102};
103
104static LIST_HEAD(, llinfo_arp) llinfo_arp;
105
106struct ifqueue arpintrq = {0, 0, 0, 50};
107static int arp_inuse, arp_allocated, arpinit_done;
108
109static int arp_maxtries = 5;
110static int useloopback = 1; /* use loopback interface for local traffic */
111static int arp_proxyall = 0;
112
113SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
114 &arp_maxtries, 0, "");
115SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
116 &useloopback, 0, "");
117SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
118 &arp_proxyall, 0, "");
119
42a7fc75
RG
120static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
121static void arprequest (struct ifnet *,
122 struct in_addr *, struct in_addr *, u_char *);
123static void arpintr (void);
124static void arptfree (struct llinfo_arp *);
125static void arptimer (void *);
984263bc 126static struct llinfo_arp
42a7fc75 127 *arplookup (u_long, int, int);
984263bc 128#ifdef INET
42a7fc75 129static void in_arpinput (struct mbuf *);
984263bc
MD
130#endif
131
132u_char arcbroadcastaddr = 0;
133
134#define IF_BCASTADDR(ifp) \
135 ((ifp)->if_type == IFT_ARCNET ? \
136 &arcbroadcastaddr : etherbroadcastaddr)
137
138/*
139 * Timeout routine. Age arp_tab entries periodically.
140 */
141/* ARGSUSED */
142static void
143arptimer(ignored_arg)
144 void *ignored_arg;
145{
146 int s = splnet();
2256ba69 147 struct llinfo_arp *la = LIST_FIRST(&llinfo_arp);
984263bc
MD
148 struct llinfo_arp *ola;
149
150 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
151 while ((ola = la) != 0) {
2256ba69 152 struct rtentry *rt = la->la_rt;
984263bc
MD
153 la = LIST_NEXT(la, la_le);
154 if (rt->rt_expire && rt->rt_expire <= time_second)
155 arptfree(ola); /* timer has expired, clear */
156 }
157 splx(s);
158}
159
160/*
161 * Parallel to llc_rtrequest.
162 */
163static void
164arp_rtrequest(req, rt, info)
165 int req;
2256ba69 166 struct rtentry *rt;
984263bc
MD
167 struct rt_addrinfo *info;
168{
2256ba69
RG
169 struct sockaddr *gate = rt->rt_gateway;
170 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
984263bc
MD
171 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
172
173 if (!arpinit_done) {
174 arpinit_done = 1;
175 timeout(arptimer, (caddr_t)0, hz);
176 }
177 if (rt->rt_flags & RTF_GATEWAY)
178 return;
179 switch (req) {
180
181 case RTM_ADD:
182 /*
183 * XXX: If this is a manually added route to interface
184 * such as older version of routed or gated might provide,
185 * restore cloning bit.
186 */
187 if ((rt->rt_flags & RTF_HOST) == 0 &&
188 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
189 rt->rt_flags |= RTF_CLONING;
190 if (rt->rt_flags & RTF_CLONING) {
191 /*
192 * Case 1: This route should come from a route to iface.
193 */
194 rt_setgate(rt, rt_key(rt),
195 (struct sockaddr *)&null_sdl);
196 gate = rt->rt_gateway;
197 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
198 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
199 rt->rt_expire = time_second;
200 break;
201 }
202 /* Announce a new entry if requested. */
203 if (rt->rt_flags & RTF_ANNOUNCE)
204 arprequest(rt->rt_ifp,
205 &SIN(rt_key(rt))->sin_addr,
206 &SIN(rt_key(rt))->sin_addr,
207 (u_char *)LLADDR(SDL(gate)));
208 /*FALLTHROUGH*/
209 case RTM_RESOLVE:
210 if (gate->sa_family != AF_LINK ||
211 gate->sa_len < sizeof(null_sdl)) {
212 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
213 break;
214 }
215 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
216 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
217 if (la != 0)
218 break; /* This happens on a route change */
219 /*
220 * Case 2: This route may come from cloning, or a manual route
221 * add with a LL address.
222 */
223 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
224 rt->rt_llinfo = (caddr_t)la;
225 if (la == 0) {
226 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
227 break;
228 }
229 arp_inuse++, arp_allocated++;
230 Bzero(la, sizeof(*la));
231 la->la_rt = rt;
232 rt->rt_flags |= RTF_LLINFO;
233 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
234
235#ifdef INET
236 /*
237 * This keeps the multicast addresses from showing up
238 * in `arp -a' listings as unresolved. It's not actually
239 * functional. Then the same for broadcast.
240 */
241 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))
242 && rt->rt_ifp->if_type != IFT_ARCNET) {
243 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
244 LLADDR(SDL(gate)));
245 SDL(gate)->sdl_alen = 6;
246 rt->rt_expire = 0;
247 }
248 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
249 memcpy(LLADDR(SDL(gate)), IF_BCASTADDR(rt->rt_ifp),
250 rt->rt_ifp->if_addrlen);
251 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
252 rt->rt_expire = 0;
253 }
254#endif
255
256 if (SIN(rt_key(rt))->sin_addr.s_addr ==
257 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
258 /*
259 * This test used to be
260 * if (loif.if_flags & IFF_UP)
261 * It allowed local traffic to be forced
262 * through the hardware by configuring the loopback down.
263 * However, it causes problems during network configuration
264 * for boards that can't receive packets they send.
265 * It is now necessary to clear "useloopback" and remove
266 * the route to force traffic out to the hardware.
267 */
268 rt->rt_expire = 0;
269 Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
270 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
271 if (useloopback)
272 rt->rt_ifp = loif;
273
274 }
275 break;
276
277 case RTM_DELETE:
278 if (la == 0)
279 break;
280 arp_inuse--;
281 LIST_REMOVE(la, la_le);
282 rt->rt_llinfo = 0;
283 rt->rt_flags &= ~RTF_LLINFO;
284 if (la->la_hold)
285 m_freem(la->la_hold);
286 Free((caddr_t)la);
287 }
288}
289
290/*
291 * Broadcast an ARP request. Caller specifies:
292 * - arp header source ip address
293 * - arp header target ip address
294 * - arp header source ethernet address
295 */
296static void
297arprequest(ifp, sip, tip, enaddr)
2256ba69
RG
298 struct ifnet *ifp;
299 struct in_addr *sip, *tip;
300 u_char *enaddr;
984263bc 301{
2256ba69
RG
302 struct mbuf *m;
303 struct ether_header *eh;
304 struct arc_header *arh;
305 struct arphdr *ah;
984263bc
MD
306 struct sockaddr sa;
307 static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
308 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
309 u_short ar_hrd;
310
311 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
312 return;
313 m->m_pkthdr.rcvif = (struct ifnet *)0;
314 switch (ifp->if_type) {
315 case IFT_ARCNET:
316 ar_hrd = htons(ARPHRD_ARCNET);
317
318 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
319 m->m_pkthdr.len = m->m_len;
320 MH_ALIGN(m, m->m_len);
321
322 arh = (struct arc_header *)sa.sa_data;
323 arh->arc_dhost = arcbroadcastaddr;
324 arh->arc_type = ARCTYPE_ARP;
325
326 ah = mtod(m, struct arphdr *);
327 break;
328
329 case IFT_ISO88025:
330 ar_hrd = htons(ARPHRD_IEEE802);
331
332 m->m_len = sizeof(llcx) +
333 arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
334 m->m_pkthdr.len = m->m_len;
335 MH_ALIGN(m, m->m_len);
336
337 (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
338 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
339 (void)memcpy(sa.sa_data + 6, enaddr, 6);
340 sa.sa_data[6] |= TR_RII;
341 sa.sa_data[12] = TR_AC;
342 sa.sa_data[13] = TR_LLC_FRAME;
343
344 ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
345 break;
346 case IFT_FDDI:
347 case IFT_ETHER:
348 /*
349 * This may not be correct for types not explicitly
350 * listed, but this is our best guess
351 */
352 default:
353 ar_hrd = htons(ARPHRD_ETHER);
354
355 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
356 m->m_pkthdr.len = m->m_len;
357 MH_ALIGN(m, m->m_len);
358
359 eh = (struct ether_header *)sa.sa_data;
360 /* if_output will not swap */
361 eh->ether_type = htons(ETHERTYPE_ARP);
362 (void)memcpy(eh->ether_dhost, etherbroadcastaddr,
363 sizeof(eh->ether_dhost));
364
365 ah = mtod(m, struct arphdr *);
366 break;
367 }
368
369 ah->ar_hrd = ar_hrd;
370 ah->ar_pro = htons(ETHERTYPE_IP);
371 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
372 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
373 ah->ar_op = htons(ARPOP_REQUEST);
374 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
375 memset(ar_tha(ah), 0, ah->ar_hln);
376 (void)memcpy(ar_spa(ah), sip, ah->ar_pln);
377 (void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
378
379 sa.sa_family = AF_UNSPEC;
380 sa.sa_len = sizeof(sa);
381 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
382}
383
384/*
385 * Resolve an IP address into an ethernet address. If success,
386 * desten is filled in. If there is no entry in arptab,
387 * set one up and broadcast a request for the IP address.
388 * Hold onto this mbuf and resend it once the address
389 * is finally resolved. A return value of 1 indicates
390 * that desten has been filled in and the packet should be sent
391 * normally; a 0 return indicates that the packet has been
392 * taken over here, either now or for later transmission.
393 */
394int
395arpresolve(ifp, rt, m, dst, desten, rt0)
2256ba69
RG
396 struct ifnet *ifp;
397 struct rtentry *rt;
984263bc 398 struct mbuf *m;
2256ba69
RG
399 struct sockaddr *dst;
400 u_char *desten;
984263bc
MD
401 struct rtentry *rt0;
402{
403 struct llinfo_arp *la = 0;
404 struct sockaddr_dl *sdl;
405
406 if (m->m_flags & M_BCAST) { /* broadcast */
407 (void)memcpy(desten, IF_BCASTADDR(ifp), ifp->if_addrlen);
408 return (1);
409 }
410 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
411 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
412 return(1);
413 }
414 if (rt)
415 la = (struct llinfo_arp *)rt->rt_llinfo;
416 if (la == 0) {
417 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
418 if (la)
419 rt = la->la_rt;
420 }
421 if (la == 0 || rt == 0) {
422 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
423 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
424 rt ? "rt" : "");
425 m_freem(m);
426 return (0);
427 }
428 sdl = SDL(rt->rt_gateway);
429 /*
430 * Check the address family and length is valid, the address
431 * is resolved; otherwise, try to resolve.
432 */
433 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
434 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
435 /*
436 * If entry has an expiry time and it is approaching,
437 * see if we need to send an ARP request within this
438 * arpt_down interval.
439 */
440 if ((rt->rt_expire != 0) &&
441 (time_second + (arp_maxtries - la->la_preempt) * arpt_down
442 > rt->rt_expire)) {
443 arprequest(ifp,
444 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
445 &SIN(dst)->sin_addr,
446 IF_LLADDR(ifp));
447 la->la_preempt++;
448 }
449
450 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
451 return 1;
452 }
453 /*
454 * If ARP is disabled on this interface, stop.
455 * XXX
456 * Probably should not allocate empty llinfo struct if we are
457 * not going to be sending out an arp request.
458 */
459 if (ifp->if_flags & IFF_NOARP) {
460 m_freem(m);
461 return (0);
462 }
463 /*
464 * There is an arptab entry, but no ethernet address
465 * response yet. Replace the held mbuf with this
466 * latest one.
467 */
468 if (la->la_hold)
469 m_freem(la->la_hold);
470 la->la_hold = m;
471 if (rt->rt_expire) {
472 rt->rt_flags &= ~RTF_REJECT;
473 if (la->la_asked == 0 || rt->rt_expire != time_second) {
474 rt->rt_expire = time_second;
475 if (la->la_asked++ < arp_maxtries) {
476 arprequest(ifp,
477 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
478 &SIN(dst)->sin_addr,
479 IF_LLADDR(ifp));
480 } else {
481 rt->rt_flags |= RTF_REJECT;
482 rt->rt_expire += arpt_down;
483 la->la_preempt = la->la_asked = 0;
484 }
485
486 }
487 }
488 return (0);
489}
490
491/*
492 * Common length and type checks are done here,
493 * then the protocol-specific routine is called.
494 */
495static void
496arpintr()
497{
2256ba69
RG
498 struct mbuf *m;
499 struct arphdr *ar;
984263bc
MD
500 int s;
501
502 while (arpintrq.ifq_head) {
503 s = splimp();
504 IF_DEQUEUE(&arpintrq, m);
505 splx(s);
506 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
507 panic("arpintr");
508
509 if (m->m_len < sizeof(struct arphdr) &&
510 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
511 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
512 continue;
513 }
514 ar = mtod(m, struct arphdr *);
515
516 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER
517 && ntohs(ar->ar_hrd) != ARPHRD_IEEE802
518 && ntohs(ar->ar_hrd) != ARPHRD_ARCNET) {
519 log(LOG_ERR,
520 "arp: unknown hardware address format (0x%2D)\n",
521 (unsigned char *)&ar->ar_hrd, "");
522 m_freem(m);
523 continue;
524 }
525
526 if (m->m_pkthdr.len < arphdr_len(ar) &&
527 (m = m_pullup(m, arphdr_len(ar))) == NULL) {
528 log(LOG_ERR, "arp: runt packet\n");
529 m_freem(m);
530 continue;
531 }
532
533 switch (ntohs(ar->ar_pro)) {
534#ifdef INET
535 case ETHERTYPE_IP:
536 in_arpinput(m);
537 continue;
538#endif
539 }
540 m_freem(m);
541 }
542}
543
544#ifdef INET
545/*
546 * ARP for Internet protocols on 10 Mb/s Ethernet.
547 * Algorithm is that given in RFC 826.
548 * In addition, a sanity check is performed on the sender
549 * protocol address, to catch impersonators.
550 * We no longer handle negotiations for use of trailer protocol:
551 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
552 * along with IP replies if we wanted trailers sent to us,
553 * and also sent them in response to IP replies.
554 * This allowed either end to announce the desire to receive
555 * trailer packets.
556 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
557 * but formerly didn't normally send requests.
558 */
559static int log_arp_wrong_iface = 1;
560
561SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
562 &log_arp_wrong_iface, 0,
563 "log arp packets arriving on the wrong interface");
564
565static void
566in_arpinput(m)
567 struct mbuf *m;
568{
2256ba69
RG
569 struct arphdr *ah;
570 struct ifnet *ifp = m->m_pkthdr.rcvif;
984263bc
MD
571 struct ether_header *eh;
572 struct arc_header *arh;
573 struct iso88025_header *th = (struct iso88025_header *)0;
574 struct iso88025_sockaddr_dl_data *trld;
2256ba69
RG
575 struct llinfo_arp *la = 0;
576 struct rtentry *rt;
984263bc
MD
577 struct ifaddr *ifa;
578 struct in_ifaddr *ia;
579 struct sockaddr_dl *sdl;
580 struct sockaddr sa;
581 struct in_addr isaddr, itaddr, myaddr;
582 int op, rif_len;
583 int req_len;
584
585 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
586 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
587 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
588 return;
589 }
590
591 ah = mtod(m, struct arphdr *);
592 op = ntohs(ah->ar_op);
593 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
594 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
595#ifdef BRIDGE
596#define BRIDGE_TEST (do_bridge)
597#else
598#define BRIDGE_TEST (0) /* cc will optimise the test away */
599#endif
600 /*
601 * For a bridge, we want to check the address irrespective
602 * of the receive interface. (This will change slightly
603 * when we have clusters of interfaces).
604 */
605 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
606 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
607 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
608 goto match;
609 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
610 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
611 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
612 goto match;
613 /*
614 * No match, use the first inet address on the receive interface
615 * as a dummy address for the rest of the function.
616 */
617 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
618 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
619 ia = ifatoia(ifa);
620 goto match;
621 }
622 /*
623 * If bridging, fall back to using any inet address.
624 * This is probably incorrect, the right way being try to match
625 * addresses for interfaces in the same cluster, so if we
626 * get here we should always drop the packet.
627 */
628 if (!BRIDGE_TEST ||
629 (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
630 m_freem(m);
631 return;
632 }
633match:
634 myaddr = ia->ia_addr.sin_addr;
635 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
636 m_freem(m); /* it's from me, ignore it. */
637 return;
638 }
639 if (!bcmp(ar_sha(ah), IF_BCASTADDR(ifp), ifp->if_addrlen)) {
640 log(LOG_ERR,
641 "arp: link address is broadcast for IP address %s!\n",
642 inet_ntoa(isaddr));
643 m_freem(m);
644 return;
645 }
646 if (isaddr.s_addr == myaddr.s_addr) {
647 log(LOG_ERR,
648 "arp: %*D is using my IP address %s!\n",
649 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
650 inet_ntoa(isaddr));
651 itaddr = myaddr;
652 goto reply;
653 }
654 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
655 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
656 /* the following is not an error when doing bridging */
657 if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
658 if (log_arp_wrong_iface)
659 log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n",
660 inet_ntoa(isaddr),
661 rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
662 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
663 ifp->if_name, ifp->if_unit);
664 goto reply;
665 }
666 if (sdl->sdl_alen &&
667 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
668 if (rt->rt_expire)
669 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n",
670 inet_ntoa(isaddr),
671 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
672 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
673 ifp->if_name, ifp->if_unit);
674 else {
675 log(LOG_ERR,
676 "arp: %*D attempts to modify permanent entry for %s on %s%d\n",
677 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
678 inet_ntoa(isaddr), ifp->if_name, ifp->if_unit);
679 goto reply;
680 }
681 }
682 /*
683 * sanity check for the address length.
684 * XXX this does not work for protocols with variable address
685 * length. -is
686 */
687 if (sdl->sdl_alen &&
688 sdl->sdl_alen != ah->ar_hln) {
689 log(LOG_WARNING,
690 "arp from %*D: new addr len %d, was %d",
691 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
692 ah->ar_hln, sdl->sdl_alen);
693 }
694 if (ifp->if_addrlen != ah->ar_hln) {
695 log(LOG_WARNING,
696 "arp from %*D: addr len: new %d, i/f %d (ignored)",
697 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
698 ah->ar_hln, ifp->if_addrlen);
699 goto reply;
700 }
701 (void)memcpy(LLADDR(sdl), ar_sha(ah),
702 sdl->sdl_alen = ah->ar_hln);
703 /*
704 * If we receive an arp from a token-ring station over
705 * a token-ring nic then try to save the source
706 * routing info.
707 */
708 if (ifp->if_type == IFT_ISO88025) {
709 th = (struct iso88025_header *)m->m_pkthdr.header;
710 trld = SDL_ISO88025(sdl);
711 rif_len = TR_RCF_RIFLEN(th->rcf);
712 if ((th->iso88025_shost[0] & TR_RII) &&
713 (rif_len > 2)) {
714 trld->trld_rcf = th->rcf;
715 trld->trld_rcf ^= htons(TR_RCF_DIR);
716 memcpy(trld->trld_route, th->rd, rif_len - 2);
717 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
718 /*
719 * Set up source routing information for
720 * reply packet (XXX)
721 */
722 m->m_data -= rif_len;
723 m->m_len += rif_len;
724 m->m_pkthdr.len += rif_len;
725 } else {
726 th->iso88025_shost[0] &= ~TR_RII;
727 trld->trld_rcf = 0;
728 }
729 m->m_data -= 8;
730 m->m_len += 8;
731 m->m_pkthdr.len += 8;
732 th->rcf = trld->trld_rcf;
733 }
734 if (rt->rt_expire)
735 rt->rt_expire = time_second + arpt_keep;
736 rt->rt_flags &= ~RTF_REJECT;
737 la->la_preempt = la->la_asked = 0;
738 if (la->la_hold) {
739 (*ifp->if_output)(ifp, la->la_hold,
740 rt_key(rt), rt);
741 la->la_hold = 0;
742 }
743 }
744reply:
745 if (op != ARPOP_REQUEST) {
746 m_freem(m);
747 return;
748 }
749 if (itaddr.s_addr == myaddr.s_addr) {
750 /* I am the target */
751 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
752 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
753 } else {
754 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
755 if (la == NULL) {
756 struct sockaddr_in sin;
757
758 if (!arp_proxyall) {
759 m_freem(m);
760 return;
761 }
762
763 bzero(&sin, sizeof sin);
764 sin.sin_family = AF_INET;
765 sin.sin_len = sizeof sin;
766 sin.sin_addr = itaddr;
767
768 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
769 if (!rt) {
770 m_freem(m);
771 return;
772 }
773 /*
774 * Don't send proxies for nodes on the same interface
775 * as this one came out of, or we'll get into a fight
776 * over who claims what Ether address.
777 */
778 if (rt->rt_ifp == ifp) {
779 rtfree(rt);
780 m_freem(m);
781 return;
782 }
783 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
784 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
785 rtfree(rt);
786#ifdef DEBUG_PROXY
787 printf("arp: proxying for %s\n",
788 inet_ntoa(itaddr));
789#endif
790 } else {
791 rt = la->la_rt;
792 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
793 sdl = SDL(rt->rt_gateway);
794 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
795 }
796 }
797
798 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
799 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
800 ah->ar_op = htons(ARPOP_REPLY);
801 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
802 switch (ifp->if_type) {
803 case IFT_ARCNET:
804 arh = (struct arc_header *)sa.sa_data;
805 arh->arc_dhost = *ar_tha(ah);
806 arh->arc_type = ARCTYPE_ARP;
807 break;
808
809 case IFT_ISO88025:
810 /* Re-arrange the source/dest address */
811 memcpy(th->iso88025_dhost, th->iso88025_shost,
812 sizeof(th->iso88025_dhost));
813 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
814 sizeof(th->iso88025_shost));
815 /* Set the source routing bit if neccesary */
816 if (th->iso88025_dhost[0] & TR_RII) {
817 th->iso88025_dhost[0] &= ~TR_RII;
818 if (TR_RCF_RIFLEN(th->rcf) > 2)
819 th->iso88025_shost[0] |= TR_RII;
820 }
821 /* Copy the addresses, ac and fc into sa_data */
822 memcpy(sa.sa_data, th->iso88025_dhost,
823 sizeof(th->iso88025_dhost) * 2);
824 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
825 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
826 break;
827 case IFT_ETHER:
828 case IFT_FDDI:
829 /*
830 * May not be correct for types not explictly
831 * listed, but it is our best guess.
832 */
833 default:
834 eh = (struct ether_header *)sa.sa_data;
835 (void)memcpy(eh->ether_dhost, ar_tha(ah),
836 sizeof(eh->ether_dhost));
837 eh->ether_type = htons(ETHERTYPE_ARP);
838 break;
839 }
840 sa.sa_family = AF_UNSPEC;
841 sa.sa_len = sizeof(sa);
842 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
843 return;
844}
845#endif
846
847/*
848 * Free an arp entry.
849 */
850static void
851arptfree(la)
2256ba69 852 struct llinfo_arp *la;
984263bc 853{
2256ba69
RG
854 struct rtentry *rt = la->la_rt;
855 struct sockaddr_dl *sdl;
984263bc
MD
856 if (rt == 0)
857 panic("arptfree");
858 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
859 sdl->sdl_family == AF_LINK) {
860 sdl->sdl_alen = 0;
861 la->la_preempt = la->la_asked = 0;
862 rt->rt_flags &= ~RTF_REJECT;
863 return;
864 }
865 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
866 0, (struct rtentry **)0);
867}
868/*
869 * Lookup or enter a new address in arptab.
870 */
871static struct llinfo_arp *
872arplookup(addr, create, proxy)
873 u_long addr;
874 int create, proxy;
875{
2256ba69 876 struct rtentry *rt;
984263bc
MD
877 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
878 const char *why = 0;
879
880 sin.sin_addr.s_addr = addr;
881 sin.sin_other = proxy ? SIN_PROXY : 0;
882 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
883 if (rt == 0)
884 return (0);
885 rt->rt_refcnt--;
886
887 if (rt->rt_flags & RTF_GATEWAY)
888 why = "host is not on local network";
889 else if ((rt->rt_flags & RTF_LLINFO) == 0)
890 why = "could not allocate llinfo";
891 else if (rt->rt_gateway->sa_family != AF_LINK)
892 why = "gateway route is not ours";
893
894 if (why && create) {
895 log(LOG_DEBUG, "arplookup %s failed: %s\n",
896 inet_ntoa(sin.sin_addr), why);
897 return 0;
898 } else if (why) {
899 return 0;
900 }
901 return ((struct llinfo_arp *)rt->rt_llinfo);
902}
903
904void
905arp_ifinit(ifp, ifa)
906 struct ifnet *ifp;
907 struct ifaddr *ifa;
908{
909 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
910 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
911 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
912 ifa->ifa_rtrequest = arp_rtrequest;
913 ifa->ifa_flags |= RTF_CLONING;
914}
915
916static void
917arp_init(void)
918{
919 LIST_INIT(&llinfo_arp);
920 register_netisr(NETISR_ARP, arpintr);
921}
922
923SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);