]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/netinet/if_ether.c
Initial revision
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / netinet / if_ether.c
1 //==========================================================================
2 //
3 //      src/sys/netinet/if_ether.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 1982, 1986, 1988, 1993
24  *      The Regents of the University of California.  All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *      This product includes software developed by the University of
37  *      California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
55  * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.11 2001/07/25 17:27:56 jlemon Exp $
56  */
57
58 /*
59  * Ethernet address resolution protocol.
60  * TODO:
61  *      add "inuse/lock" bit (or ref. count) along with valid bit
62  */
63
64 #include <sys/param.h>
65 #include <sys/queue.h>
66 #include <sys/sysctl.h>
67 #include <sys/mbuf.h>
68 #include <sys/malloc.h>
69 #include <sys/socket.h>
70
71 #include <net/if.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
75 #include <net/netisr.h>
76 #include <net/if_llc.h>
77 #ifdef BRIDGE
78 #include <net/ethernet.h>
79 #include <net/bridge.h>
80 #endif
81
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
85
86 #include <net/iso88025.h>
87
88 #define SIN(s) ((struct sockaddr_in *)s)
89 #define SDL(s) ((struct sockaddr_dl *)s)
90
91 SYSCTL_DECL(_net_link_ether);
92 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
93
94 /* timer values */
95 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
96 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
97 static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
98
99 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
100            &arpt_prune, 0, "");
101 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 
102            &arpt_keep, 0, "");
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
104            &arpt_down, 0, "");
105
106 #define rt_expire rt_rmx.rmx_expire
107
108 struct llinfo_arp {
109         LIST_ENTRY(llinfo_arp) la_le;
110         struct  rtentry *la_rt;
111         struct  mbuf *la_hold;          /* last packet until resolved/timeout */
112         long    la_asked;               /* last time we QUERIED for this addr */
113 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
114 };
115
116 static  LIST_HEAD(, llinfo_arp) llinfo_arp;
117
118 struct  ifqueue arpintrq = {0, 0, 0, 50};
119 static int      arp_inuse, arp_allocated;
120
121 static int      arp_maxtries = 5;
122 static int      useloopback = 1; /* use loopback interface for local traffic */
123 static int      arp_proxyall = 0;
124
125 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
126            &arp_maxtries, 0, "");
127 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
128            &useloopback, 0, "");
129 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
130            &arp_proxyall, 0, "");
131
132 static void     arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
133 static void     arprequest __P((struct arpcom *,
134                         struct in_addr *, struct in_addr *, u_char *));
135 static void     arpintr __P((void));
136 static void     arptfree __P((struct llinfo_arp *));
137 static void     arptimer __P((void *));
138 static struct llinfo_arp
139                 *arplookup __P((u_long, int, int));
140 #ifdef INET
141 static void     in_arpinput __P((struct mbuf *));
142 #endif
143
144 /*
145  * Timeout routine.  Age arp_tab entries periodically.
146  */
147 /* ARGSUSED */
148 static void
149 arptimer(ignored_arg)
150         void *ignored_arg;
151 {
152         int s = splnet();
153         register struct llinfo_arp *la = llinfo_arp.lh_first;
154         struct llinfo_arp *ola;
155
156         timeout(arptimer, (caddr_t)0, arpt_prune * hz);
157         while ((ola = la) != 0) {
158                 register struct rtentry *rt = la->la_rt;
159                 la = la->la_le.le_next;
160                 if (rt->rt_expire && rt->rt_expire <= time_second)
161                         arptfree(ola); /* timer has expired, clear */
162         }
163         splx(s);
164 }
165
166 /*
167  * Parallel to llc_rtrequest.
168  */
169 static void
170 arp_rtrequest(req, rt, sa)
171         int req;
172         register struct rtentry *rt;
173         struct sockaddr *sa;
174 {
175         register struct sockaddr *gate = rt->rt_gateway;
176         register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
177         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
178         static int arpinit_done;
179
180         if (!arpinit_done) {
181                 arpinit_done = 1;
182                 LIST_INIT(&llinfo_arp);
183                 timeout(arptimer, (caddr_t)0, hz);
184                 register_netisr(NETISR_ARP, arpintr);
185         }
186         if (rt->rt_flags & RTF_GATEWAY)
187                 return;
188         switch (req) {
189
190         case RTM_ADD:
191                 /*
192                  * XXX: If this is a manually added route to interface
193                  * such as older version of routed or gated might provide,
194                  * restore cloning bit.
195                  */
196                 if ((rt->rt_flags & RTF_HOST) == 0 &&
197                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
198                         rt->rt_flags |= RTF_CLONING;
199                 if (rt->rt_flags & RTF_CLONING) {
200                         /*
201                          * Case 1: This route should come from a route to iface.
202                          */
203                         rt_setgate(rt, rt_key(rt),
204                                         (struct sockaddr *)&null_sdl);
205                         gate = rt->rt_gateway;
206                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
207                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
208                         rt->rt_expire = time_second;
209                         break;
210                 }
211                 /* Announce a new entry if requested. */
212                 if (rt->rt_flags & RTF_ANNOUNCE)
213                         arprequest((struct arpcom *)rt->rt_ifp,
214                             &SIN(rt_key(rt))->sin_addr,
215                             &SIN(rt_key(rt))->sin_addr,
216                             (u_char *)LLADDR(SDL(gate)));
217                 /*FALLTHROUGH*/
218         case RTM_RESOLVE:
219                 if (gate->sa_family != AF_LINK ||
220                     gate->sa_len < sizeof(null_sdl)) {
221                         log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
222                         break;
223                 }
224                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
225                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
226                 if (la != 0)
227                         break; /* This happens on a route change */
228                 /*
229                  * Case 2:  This route may come from cloning, or a manual route
230                  * add with a LL address.
231                  */
232                 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
233                 rt->rt_llinfo = (caddr_t)la;
234                 if (la == 0) {
235                         log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
236                         break;
237                 }
238                 arp_inuse++, arp_allocated++;
239                 Bzero(la, sizeof(*la));
240                 la->la_rt = rt;
241                 rt->rt_flags |= RTF_LLINFO;
242                 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
243
244 #ifdef INET
245                 /*
246                  * This keeps the multicast addresses from showing up
247                  * in `arp -a' listings as unresolved.  It's not actually
248                  * functional.  Then the same for broadcast.
249                  */
250                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
251                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
252                                                LLADDR(SDL(gate)));
253                         SDL(gate)->sdl_alen = 6;
254                         rt->rt_expire = 0;
255                 }
256                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
257                         memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
258                         SDL(gate)->sdl_alen = 6;
259                         rt->rt_expire = 0;
260                 }
261 #endif
262
263                 if (SIN(rt_key(rt))->sin_addr.s_addr ==
264                     (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
265                     /*
266                      * This test used to be
267                      *  if (loif.if_flags & IFF_UP)
268                      * It allowed local traffic to be forced
269                      * through the hardware by configuring the loopback down.
270                      * However, it causes problems during network configuration
271                      * for boards that can't receive packets they send.
272                      * It is now necessary to clear "useloopback" and remove
273                      * the route to force traffic out to the hardware.
274                      */
275                         rt->rt_expire = 0;
276                         Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
277                                 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
278                         if (useloopback)
279                                 rt->rt_ifp = loif;
280
281                 }
282                 break;
283
284         case RTM_DELETE:
285                 if (la == 0)
286                         break;
287                 arp_inuse--;
288                 LIST_REMOVE(la, la_le);
289                 rt->rt_llinfo = 0;
290                 rt->rt_flags &= ~RTF_LLINFO;
291                 if (la->la_hold)
292                         m_freem(la->la_hold);
293                 Free((caddr_t)la);
294         }
295 }
296
297 /*
298  * Broadcast an ARP request. Caller specifies:
299  *      - arp header source ip address
300  *      - arp header target ip address
301  *      - arp header source ethernet address
302  */
303 static void
304 arprequest(ac, sip, tip, enaddr)
305         register struct arpcom *ac;
306         register struct in_addr *sip, *tip;
307         register u_char *enaddr;
308 {
309         register struct mbuf *m;
310         register struct ether_header *eh;
311         register struct ether_arp *ea;
312         struct sockaddr sa;
313         static u_char   llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
314                                    LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
315
316         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
317                 return;
318         m->m_pkthdr.rcvif = (struct ifnet *)0;
319         switch (ac->ac_if.if_type) {
320         case IFT_ISO88025:
321                 m->m_len = sizeof(*ea) + sizeof(llcx);
322                 m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx);
323                 MH_ALIGN(m, sizeof(*ea) + sizeof(llcx));
324                 (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
325                 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
326                 (void)memcpy(sa.sa_data + 6, enaddr, 6);
327                 sa.sa_data[6] |= TR_RII;
328                 sa.sa_data[12] = TR_AC;
329                 sa.sa_data[13] = TR_LLC_FRAME;
330                 ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx));
331                 bzero((caddr_t)ea, sizeof (*ea));
332                 ea->arp_hrd = htons(ARPHRD_IEEE802);
333                 break;
334         case IFT_FDDI:
335         case IFT_ETHER:
336                 /*
337                  * This may not be correct for types not explicitly
338                  * listed, but this is our best guess
339                  */
340         default:
341                 m->m_len = sizeof(*ea);
342                 m->m_pkthdr.len = sizeof(*ea);
343                 MH_ALIGN(m, sizeof(*ea));
344                 ea = mtod(m, struct ether_arp *);
345                 eh = (struct ether_header *)sa.sa_data;
346                 bzero((caddr_t)ea, sizeof (*ea));
347                 /* if_output will not swap */
348                 eh->ether_type = htons(ETHERTYPE_ARP);
349                 (void)memcpy(eh->ether_dhost, etherbroadcastaddr,
350                     sizeof(eh->ether_dhost));
351                 ea->arp_hrd = htons(ARPHRD_ETHER);
352                 break;
353         }
354         ea->arp_pro = htons(ETHERTYPE_IP);
355         ea->arp_hln = sizeof(ea->arp_sha);      /* hardware address length */
356         ea->arp_pln = sizeof(ea->arp_spa);      /* protocol address length */
357         ea->arp_op = htons(ARPOP_REQUEST);
358         (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
359         (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
360         (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
361         sa.sa_family = AF_UNSPEC;
362         sa.sa_len = sizeof(sa);
363         (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
364 }
365
366 /*
367  * Resolve an IP address into an ethernet address.  If success,
368  * desten is filled in.  If there is no entry in arptab,
369  * set one up and broadcast a request for the IP address.
370  * Hold onto this mbuf and resend it once the address
371  * is finally resolved.  A return value of 1 indicates
372  * that desten has been filled in and the packet should be sent
373  * normally; a 0 return indicates that the packet has been
374  * taken over here, either now or for later transmission.
375  */
376 int
377 arpresolve(ac, rt, m, dst, desten, rt0)
378         register struct arpcom *ac;
379         register struct rtentry *rt;
380         struct mbuf *m;
381         register struct sockaddr *dst;
382         register u_char *desten;
383         struct rtentry *rt0;
384 {
385         struct llinfo_arp *la = 0;
386         struct sockaddr_dl *sdl;
387
388         if (m->m_flags & M_BCAST) {     /* broadcast */
389                 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
390                 return (1);
391         }
392         if (m->m_flags & M_MCAST) {     /* multicast */
393                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
394                 return(1);
395         }
396         if (rt)
397                 la = (struct llinfo_arp *)rt->rt_llinfo;
398         if (la == 0) {
399                 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
400                 if (la)
401                         rt = la->la_rt;
402         }
403         if (la == 0 || rt == 0) {
404                 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
405                         inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
406                                 rt ? "rt" : "");
407                 m_freem(m);
408                 return (0);
409         }
410         sdl = SDL(rt->rt_gateway);
411         /*
412          * Check the address family and length is valid, the address
413          * is resolved; otherwise, try to resolve.
414          */
415         if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
416             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
417                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
418                 return 1;
419         }
420         /*
421          * If ARP is disabled on this interface, stop.
422          * XXX
423          * Probably should not allocate empty llinfo struct if we are
424          * not going to be sending out an arp request.
425          */
426         if (ac->ac_if.if_flags & IFF_NOARP)
427                 return (0);
428         /*
429          * There is an arptab entry, but no ethernet address
430          * response yet.  Replace the held mbuf with this
431          * latest one.
432          */
433         if (la->la_hold)
434                 m_freem(la->la_hold);
435         la->la_hold = m;
436         if (rt->rt_expire) {
437                 rt->rt_flags &= ~RTF_REJECT;
438                 if (la->la_asked == 0 || rt->rt_expire != time_second) {
439                         rt->rt_expire = time_second;
440                         if (la->la_asked++ < arp_maxtries)
441                             arprequest(ac,
442                                 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
443                                 &SIN(dst)->sin_addr, ac->ac_enaddr);
444                         else {
445                                 rt->rt_flags |= RTF_REJECT;
446                                 rt->rt_expire += arpt_down;
447                                 la->la_asked = 0;
448                         }
449
450                 }
451         }
452         return (0);
453 }
454
455 /*
456  * Common length and type checks are done here,
457  * then the protocol-specific routine is called.
458  */
459 static void
460 arpintr()
461 {
462         register struct mbuf *m;
463         register struct arphdr *ar;
464         int s;
465
466         while (arpintrq.ifq_head) {
467                 s = splimp();
468                 IF_DEQUEUE(&arpintrq, m);
469                 splx(s);
470                 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
471                         panic("arpintr");
472         
473                 if (m->m_len < sizeof(struct arphdr) &&
474                     ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
475                         log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
476                         continue;
477                 }
478                 ar = mtod(m, struct arphdr *);
479
480                 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER
481                     && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) {
482                         log(LOG_ERR,
483                             "arp: unknown hardware address format (0x%d)\n",
484                             ntohs(ar->ar_hrd));
485                         m_freem(m);
486                         continue;
487                 }
488
489                 if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln
490                     + 2 * ar->ar_pln) {
491                         log(LOG_ERR, "arp: runt packet\n");
492                         m_freem(m);
493                         continue;
494                 }
495
496                 switch (ntohs(ar->ar_pro)) {
497 #ifdef INET
498                         case ETHERTYPE_IP:
499                                 in_arpinput(m);
500                                 continue;
501 #endif
502                 }
503                 m_freem(m);
504         }
505 }
506
507 #ifdef INET
508 /*
509  * ARP for Internet protocols on 10 Mb/s Ethernet.
510  * Algorithm is that given in RFC 826.
511  * In addition, a sanity check is performed on the sender
512  * protocol address, to catch impersonators.
513  * We no longer handle negotiations for use of trailer protocol:
514  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
515  * along with IP replies if we wanted trailers sent to us,
516  * and also sent them in response to IP replies.
517  * This allowed either end to announce the desire to receive
518  * trailer packets.
519  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
520  * but formerly didn't normally send requests.
521  */
522 static int log_arp_wrong_iface = 1;
523
524 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
525         &log_arp_wrong_iface, 0,
526         "log arp packets arriving on the wrong interface");
527
528 static void
529 in_arpinput(m)
530         struct mbuf *m;
531 {
532         register struct ether_arp *ea;
533         register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
534         struct ether_header *eh;
535         struct iso88025_header *th = (struct iso88025_header *)0;
536         register struct llinfo_arp *la = 0;
537         register struct rtentry *rt;
538         struct in_ifaddr *ia, *maybe_ia = 0;
539         struct sockaddr_dl *sdl;
540         struct sockaddr sa;
541         struct in_addr isaddr, itaddr, myaddr;
542         int op, rif_len;
543
544         if (m->m_len < sizeof(struct ether_arp) &&
545             (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) {
546                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
547                 return;
548         }
549
550         ea = mtod(m, struct ether_arp *);
551         op = ntohs(ea->arp_op);
552         (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
553         (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
554         for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
555                 /*
556                  * For a bridge, we want to check the address irrespective
557                  * of the receive interface. (This will change slightly
558                  * when we have clusters of interfaces).
559                  */
560 #ifdef BRIDGE
561 #define BRIDGE_TEST (do_bridge)
562 #else
563 #define BRIDGE_TEST (0) /* cc will optimise the test away */
564 #endif
565                 if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) {
566                         maybe_ia = ia;
567                         if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
568                              (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
569                                 break;
570                         }
571                 }
572         }
573         if (maybe_ia == 0) {
574                 m_freem(m);
575                 return;
576         }
577         myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
578         if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
579             sizeof (ea->arp_sha))) {
580                 m_freem(m);     /* it's from me, ignore it. */
581                 return;
582         }
583         if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
584             sizeof (ea->arp_sha))) {
585                 log(LOG_ERR,
586                     "arp: ether address is broadcast for IP address %s!\n",
587                     inet_ntoa(isaddr));
588                 m_freem(m);
589                 return;
590         }
591         if (isaddr.s_addr == myaddr.s_addr) {
592                 log(LOG_ERR,
593                    "arp: %6p is using my IP address %s!\n",
594                    ea->arp_sha, inet_ntoa(isaddr));
595                 itaddr = myaddr;
596                 goto reply;
597         }
598         la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
599         if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
600                 /* the following is not an error when doing bridging */
601                 if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) {
602                     if (log_arp_wrong_iface)
603                         log(LOG_ERR, "arp: %s is on %s%d but got reply from %6p on %s%d\n",
604                             inet_ntoa(isaddr),
605                             rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
606                             ea->arp_sha, 
607                             ac->ac_if.if_name, ac->ac_if.if_unit);
608                     goto reply;
609                 }
610                 if (sdl->sdl_alen &&
611                     bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
612                         if (rt->rt_expire)
613                             log(LOG_INFO, "arp: %s moved from %6p to %6p on %s%d\n",
614                                 inet_ntoa(isaddr), (u_char *)LLADDR(sdl),
615                                 ea->arp_sha,
616                                 ac->ac_if.if_name, ac->ac_if.if_unit);
617                         else {
618                             log(LOG_ERR,
619                                 "arp: %6p attempts to modify permanent entry for %s on %s%d\n",
620                                 ea->arp_sha, inet_ntoa(isaddr),
621                                 ac->ac_if.if_name, ac->ac_if.if_unit);
622                             goto reply;
623                         }
624                 }
625                 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
626                 sdl->sdl_alen = sizeof(ea->arp_sha);
627                 sdl->sdl_rcf = (u_short)0;
628                 /*
629                  * If we receive an arp from a token-ring station over
630                  * a token-ring nic then try to save the source
631                  * routing info.
632                  */
633                 if (ac->ac_if.if_type == IFT_ISO88025) {
634                         th = (struct iso88025_header *)m->m_pkthdr.header;
635                         rif_len = TR_RCF_RIFLEN(th->rcf);
636                         if ((th->iso88025_shost[0] & TR_RII) &&
637                             (rif_len > 2)) {
638                                 sdl->sdl_rcf = th->rcf;
639                                 sdl->sdl_rcf ^= htons(TR_RCF_DIR);
640                                 memcpy(sdl->sdl_route, th->rd, rif_len - 2);
641                                 sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK);
642                                 /*
643                                  * Set up source routing information for
644                                  * reply packet (XXX)
645                                  */
646                                 m->m_data -= rif_len;
647                                 m->m_len  += rif_len;
648                                 m->m_pkthdr.len += rif_len;
649                         } else {
650                                 th->iso88025_shost[0] &= ~TR_RII;
651                         }
652                         m->m_data -= 8;
653                         m->m_len  += 8;
654                         m->m_pkthdr.len += 8;
655                         th->rcf = sdl->sdl_rcf;
656                 } else {
657                         sdl->sdl_rcf = (u_short)0;
658                 }
659                 if (rt->rt_expire)
660                         rt->rt_expire = time_second + arpt_keep;
661                 rt->rt_flags &= ~RTF_REJECT;
662                 la->la_asked = 0;
663                 if (la->la_hold) {
664                         (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
665                                 rt_key(rt), rt);
666                         la->la_hold = 0;
667                 }
668         }
669 reply:
670         if (op != ARPOP_REQUEST) {
671                 m_freem(m);
672                 return;
673         }
674         if (itaddr.s_addr == myaddr.s_addr) {
675                 /* I am the target */
676                 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
677                 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
678         } else {
679                 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
680                 if (la == NULL) {
681                         struct sockaddr_in sin;
682
683                         if (!arp_proxyall) {
684                                 m_freem(m);
685                                 return;
686                         }
687
688                         bzero(&sin, sizeof sin);
689                         sin.sin_family = AF_INET;
690                         sin.sin_len = sizeof sin;
691                         sin.sin_addr = itaddr;
692
693                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
694                         if (!rt) {
695                                 m_freem(m);
696                                 return;
697                         }
698                         /*
699                          * Don't send proxies for nodes on the same interface
700                          * as this one came out of, or we'll get into a fight
701                          * over who claims what Ether address.
702                          */
703                         if (rt->rt_ifp == &ac->ac_if) {
704                                 rtfree(rt);
705                                 m_freem(m);
706                                 return;
707                         }
708                         (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
709                         (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
710                         rtfree(rt);
711 #ifdef DEBUG_PROXY
712                         printf("arp: proxying for %s\n",
713                                inet_ntoa(itaddr));
714 #endif
715                 } else {
716                         rt = la->la_rt;
717                         (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
718                         sdl = SDL(rt->rt_gateway);
719                         (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
720                 }
721         }
722
723         (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
724         (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
725         ea->arp_op = htons(ARPOP_REPLY);
726         ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
727         switch (ac->ac_if.if_type) {
728         case IFT_ISO88025:
729                 /* Re-arrange the source/dest address */
730                 memcpy(th->iso88025_dhost, th->iso88025_shost,
731                     sizeof(th->iso88025_dhost));
732                 memcpy(th->iso88025_shost, ac->ac_enaddr,
733                     sizeof(th->iso88025_shost));
734                 /* Set the source routing bit if neccesary */
735                 if (th->iso88025_dhost[0] & TR_RII) {
736                         th->iso88025_dhost[0] &= ~TR_RII;
737                         if (TR_RCF_RIFLEN(th->rcf) > 2)
738                                 th->iso88025_shost[0] |= TR_RII;
739                 }
740                 /* Copy the addresses, ac and fc into sa_data */
741                 memcpy(sa.sa_data, th->iso88025_dhost,
742                     sizeof(th->iso88025_dhost) * 2);
743                 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
744                 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
745                 break;
746         case IFT_ETHER:
747         case IFT_FDDI:
748         /*
749          * May not be correct for types not explictly
750          * listed, but it is our best guess.
751          */
752         default:
753                 eh = (struct ether_header *)sa.sa_data;
754                 (void)memcpy(eh->ether_dhost, ea->arp_tha,
755                     sizeof(eh->ether_dhost));
756                 eh->ether_type = htons(ETHERTYPE_ARP);
757                 break;
758         }
759         sa.sa_family = AF_UNSPEC;
760         sa.sa_len = sizeof(sa);
761         (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
762         return;
763 }
764 #endif
765
766 /*
767  * Free an arp entry.
768  */
769 static void
770 arptfree(la)
771         register struct llinfo_arp *la;
772 {
773         register struct rtentry *rt = la->la_rt;
774         register struct sockaddr_dl *sdl;
775         if (rt == 0)
776                 panic("arptfree");
777         if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
778             sdl->sdl_family == AF_LINK) {
779                 sdl->sdl_alen = 0;
780                 la->la_asked = 0;
781                 rt->rt_flags &= ~RTF_REJECT;
782                 return;
783         }
784         rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
785                         0, (struct rtentry **)0);
786 }
787 /*
788  * Lookup or enter a new address in arptab.
789  */
790 static struct llinfo_arp *
791 arplookup(addr, create, proxy)
792         u_long addr;
793         int create, proxy;
794 {
795         register struct rtentry *rt;
796         static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
797         const char *why = 0;
798
799         sin.sin_addr.s_addr = addr;
800         sin.sin_other = proxy ? SIN_PROXY : 0;
801         rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
802         if (rt == 0)
803                 return (0);
804         rt->rt_refcnt--;
805
806         if (rt->rt_flags & RTF_GATEWAY)
807                 why = "host is not on local network";
808         else if ((rt->rt_flags & RTF_LLINFO) == 0)
809                 why = "could not allocate llinfo";
810         else if (rt->rt_gateway->sa_family != AF_LINK)
811                 why = "gateway route is not ours";
812
813         if (why && create) {
814                 log(LOG_DEBUG, "arplookup %s failed: %s\n",
815                     inet_ntoa(sin.sin_addr), why);
816                 return 0;
817         } else if (why) {
818                 return 0;
819         }
820         return ((struct llinfo_arp *)rt->rt_llinfo);
821 }
822
823 void
824 arp_ifinit(ac, ifa)
825         struct arpcom *ac;
826         struct ifaddr *ifa;
827 {
828         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
829                 arprequest(ac, &IA_SIN(ifa)->sin_addr,
830                                &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
831         ifa->ifa_rtrequest = arp_rtrequest;
832         ifa->ifa_flags |= RTF_CLONING;
833 }