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