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