Merge from vendor branch INTEL_ACPICA:
[dragonfly.git] / sys / netinet / if_ether.c
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Copyright (c) 2004, 2005 Jeffrey M. Hsu.  All rights reserved.
35  *
36  * License terms: all terms for the DragonFly license above plus the following:
37  *
38  * 4. All advertising materials mentioning features or use of this software
39  *    must display the following acknowledgement:
40  *
41  *      This product includes software developed by Jeffrey M. Hsu
42  *      for the DragonFly Project.
43  *
44  *    This requirement may be waived with permission from Jeffrey Hsu.
45  *    Permission will be granted to any DragonFly user for free.
46  *    This requirement will sunset and may be removed on Jan 31, 2006,
47  *    after which the standard DragonFly license (as shown above) will
48  *    apply.
49  */
50
51 /*
52  * Copyright (c) 1982, 1986, 1988, 1993
53  *      The Regents of the University of California.  All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  *    must display the following acknowledgement:
65  *      This product includes software developed by the University of
66  *      California, Berkeley and its contributors.
67  * 4. Neither the name of the University nor the names of its contributors
68  *    may be used to endorse or promote products derived from this software
69  *    without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81  * SUCH DAMAGE.
82  *
83  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
84  * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
85  * $DragonFly: src/sys/netinet/if_ether.c,v 1.35 2006/12/22 23:57:52 swildner Exp $
86  */
87
88 /*
89  * Ethernet address resolution protocol.
90  * TODO:
91  *      add "inuse/lock" bit (or ref. count) along with valid bit
92  */
93
94 #include "opt_inet.h"
95
96 #include <sys/param.h>
97 #include <sys/kernel.h>
98 #include <sys/queue.h>
99 #include <sys/sysctl.h>
100 #include <sys/systm.h>
101 #include <sys/mbuf.h>
102 #include <sys/malloc.h>
103 #include <sys/socket.h>
104 #include <sys/syslog.h>
105
106 #include <sys/thread2.h>
107 #include <sys/msgport2.h>
108
109 #include <net/if.h>
110 #include <net/if_dl.h>
111 #include <net/if_types.h>
112 #include <net/route.h>
113 #include <net/netisr.h>
114 #include <net/if_llc.h>
115
116 #include <netinet/in.h>
117 #include <netinet/in_var.h>
118 #include <netinet/if_ether.h>
119
120 #include <net/if_arc.h>
121 #include <net/iso88025.h>
122
123 #define SIN(s) ((struct sockaddr_in *)s)
124 #define SDL(s) ((struct sockaddr_dl *)s)
125
126 SYSCTL_DECL(_net_link_ether);
127 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
128
129 /* timer values */
130 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
131 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
132 static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
133
134 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
135            &arpt_prune, 0, "");
136 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
137            &arpt_keep, 0, "");
138 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
139            &arpt_down, 0, "");
140
141 #define rt_expire       rt_rmx.rmx_expire
142
143 struct llinfo_arp {
144         LIST_ENTRY(llinfo_arp) la_le;
145         struct  rtentry *la_rt;
146         struct  mbuf *la_hold;  /* last packet until resolved/timeout */
147         u_short la_preempt;     /* countdown for pre-expiry arps */
148         u_short la_asked;       /* #times we QUERIED following expiration */
149 };
150
151 static  LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
152
153 static int      arp_maxtries = 5;
154 static int      useloopback = 1; /* use loopback interface for local traffic */
155 static int      arp_proxyall = 0;
156
157 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
158            &arp_maxtries, 0, "");
159 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
160            &useloopback, 0, "");
161 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
162            &arp_proxyall, 0, "");
163
164 static void     arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
165 static void     arprequest (struct ifnet *,
166                         struct in_addr *, struct in_addr *, u_char *);
167 static int      arpintr(struct netmsg *);
168 static void     arptfree (struct llinfo_arp *);
169 static void     arptimer (void *);
170 static struct llinfo_arp
171                 *arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
172 #ifdef INET
173 static void     in_arpinput (struct mbuf *);
174 #endif
175
176 static struct callout   arptimer_ch[MAXCPU];
177
178 /*
179  * Timeout routine.  Age arp_tab entries periodically.
180  */
181 /* ARGSUSED */
182 static void
183 arptimer(void *ignored_arg)
184 {
185         struct llinfo_arp *la, *nla;
186
187         crit_enter();
188         LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
189                 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
190                         arptfree(la);   
191         }
192         callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
193         crit_exit();
194 }
195
196 /*
197  * Parallel to llc_rtrequest.
198  */
199 static void
200 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
201 {
202         struct sockaddr *gate = rt->rt_gateway;
203         struct llinfo_arp *la = rt->rt_llinfo;
204
205         struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
206         static boolean_t arpinit_done[MAXCPU];
207
208         if (!arpinit_done[mycpuid]) {
209                 arpinit_done[mycpuid] = TRUE;
210                 callout_init(&arptimer_ch[mycpuid]);
211                 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
212         }
213         if (rt->rt_flags & RTF_GATEWAY)
214                 return;
215
216         switch (req) {
217         case RTM_ADD:
218                 /*
219                  * XXX: If this is a manually added route to interface
220                  * such as older version of routed or gated might provide,
221                  * restore cloning bit.
222                  */
223                 if (!(rt->rt_flags & RTF_HOST) &&
224                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
225                         rt->rt_flags |= RTF_CLONING;
226                 if (rt->rt_flags & RTF_CLONING) {
227                         /*
228                          * Case 1: This route should come from a route to iface.
229                          */
230                         rt_setgate(rt, rt_key(rt),
231                                    (struct sockaddr *)&null_sdl);
232                         gate = rt->rt_gateway;
233                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
234                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
235                         rt->rt_expire = time_second;
236                         break;
237                 }
238                 /* Announce a new entry if requested. */
239                 if (rt->rt_flags & RTF_ANNOUNCE)
240                         arprequest(rt->rt_ifp,
241                             &SIN(rt_key(rt))->sin_addr,
242                             &SIN(rt_key(rt))->sin_addr,
243                             LLADDR(SDL(gate)));
244                 /*FALLTHROUGH*/
245         case RTM_RESOLVE:
246                 if (gate->sa_family != AF_LINK ||
247                     gate->sa_len < sizeof(struct sockaddr_dl)) {
248                         log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
249                         break;
250                 }
251                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
252                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
253                 if (la != NULL)
254                         break; /* This happens on a route change */
255                 /*
256                  * Case 2:  This route may come from cloning, or a manual route
257                  * add with a LL address.
258                  */
259                 R_Malloc(la, struct llinfo_arp *, sizeof *la);
260                 rt->rt_llinfo = la;
261                 if (la == NULL) {
262                         log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
263                         break;
264                 }
265                 bzero(la, sizeof *la);
266                 la->la_rt = rt;
267                 rt->rt_flags |= RTF_LLINFO;
268                 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
269
270 #ifdef INET
271                 /*
272                  * This keeps the multicast addresses from showing up
273                  * in `arp -a' listings as unresolved.  It's not actually
274                  * functional.  Then the same for broadcast.
275                  */
276                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
277                     rt->rt_ifp->if_type != IFT_ARCNET) {
278                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
279                                                LLADDR(SDL(gate)));
280                         SDL(gate)->sdl_alen = 6;
281                         rt->rt_expire = 0;
282                 }
283                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
284                         memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
285                                rt->rt_ifp->if_addrlen);
286                         SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
287                         rt->rt_expire = 0;
288                 }
289 #endif
290
291                 if (SIN(rt_key(rt))->sin_addr.s_addr ==
292                     (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
293                         /*
294                          * This test used to be
295                          *      if (loif.if_flags & IFF_UP)
296                          * It allowed local traffic to be forced
297                          * through the hardware by configuring the
298                          * loopback down.  However, it causes problems
299                          * during network configuration for boards
300                          * that can't receive packets they send.  It
301                          * is now necessary to clear "useloopback" and
302                          * remove the route to force traffic out to
303                          * the hardware.
304                          */
305                         rt->rt_expire = 0;
306                         bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
307                               SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
308                         if (useloopback)
309                                 rt->rt_ifp = loif;
310                 }
311                 break;
312
313         case RTM_DELETE:
314                 if (la == NULL)
315                         break;
316                 LIST_REMOVE(la, la_le);
317                 rt->rt_llinfo = NULL;
318                 rt->rt_flags &= ~RTF_LLINFO;
319                 if (la->la_hold != NULL)
320                         m_freem(la->la_hold);
321                 Free(la);
322         }
323 }
324
325 /*
326  * Broadcast an ARP request. Caller specifies:
327  *      - arp header source ip address
328  *      - arp header target ip address
329  *      - arp header source ethernet address
330  */
331 static void
332 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
333            u_char *enaddr)
334 {
335         struct mbuf *m;
336         struct ether_header *eh;
337         struct arc_header *arh;
338         struct arphdr *ah;
339         struct sockaddr sa;
340         static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
341                                  LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
342         u_short ar_hrd;
343
344         if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
345                 return;
346         m->m_pkthdr.rcvif = (struct ifnet *)NULL;
347
348         switch (ifp->if_type) {
349         case IFT_ARCNET:
350                 ar_hrd = htons(ARPHRD_ARCNET);
351
352                 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
353                 m->m_pkthdr.len = m->m_len;
354                 MH_ALIGN(m, m->m_len);
355
356                 arh = (struct arc_header *)sa.sa_data;
357                 arh->arc_dhost = ifp->if_broadcastaddr[0];
358                 arh->arc_type = ARCTYPE_ARP;
359
360                 ah = mtod(m, struct arphdr *);
361                 break;
362
363         case IFT_ISO88025:
364                 ar_hrd = htons(ARPHRD_IEEE802);
365
366                 m->m_len = (sizeof llcx) +
367                     arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
368                 m->m_pkthdr.len = m->m_len;
369                 MH_ALIGN(m, m->m_len);
370
371                 memcpy(mtod(m, caddr_t), llcx, sizeof llcx);
372                 memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
373                 memcpy(sa.sa_data + 6, enaddr, 6);
374                 sa.sa_data[6] |= TR_RII;
375                 sa.sa_data[12] = TR_AC;
376                 sa.sa_data[13] = TR_LLC_FRAME;
377
378                 ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx);
379                 break;
380         case IFT_FDDI:
381         case IFT_ETHER:
382                 /*
383                  * This may not be correct for types not explicitly
384                  * listed, but this is our best guess
385                  */
386         default:
387                 ar_hrd = htons(ARPHRD_ETHER);
388
389                 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
390                 m->m_pkthdr.len = m->m_len;
391                 MH_ALIGN(m, m->m_len);
392
393                 eh = (struct ether_header *)sa.sa_data;
394                 /* if_output() will not swap */
395                 eh->ether_type = htons(ETHERTYPE_ARP);
396                 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
397
398                 ah = mtod(m, struct arphdr *);
399                 break;
400         }
401
402         ah->ar_hrd = ar_hrd;
403         ah->ar_pro = htons(ETHERTYPE_IP);
404         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
405         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
406         ah->ar_op = htons(ARPOP_REQUEST);
407         memcpy(ar_sha(ah), enaddr, ah->ar_hln);
408         memset(ar_tha(ah), 0, ah->ar_hln);
409         memcpy(ar_spa(ah), sip, ah->ar_pln);
410         memcpy(ar_tpa(ah), tip, ah->ar_pln);
411
412         sa.sa_family = AF_UNSPEC;
413         sa.sa_len = sizeof sa;
414         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
415 }
416
417 /*
418  * Resolve an IP address into an ethernet address.  If success,
419  * desten is filled in.  If there is no entry in arptab,
420  * set one up and broadcast a request for the IP address.
421  * Hold onto this mbuf and resend it once the address
422  * is finally resolved.  A return value of 1 indicates
423  * that desten has been filled in and the packet should be sent
424  * normally; a 0 return indicates that the packet has been
425  * taken over here, either now or for later transmission.
426  */
427 int
428 arpresolve(
429         struct ifnet *ifp,
430         struct rtentry *rt0,
431         struct mbuf *m,
432         struct sockaddr *dst,
433         u_char *desten)
434 {
435         struct rtentry *rt;
436         struct llinfo_arp *la = NULL;
437         struct sockaddr_dl *sdl;
438
439         if (m->m_flags & M_BCAST) {     /* broadcast */
440                 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
441                 return (1);
442         }
443         if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
444                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
445                 return (1);
446         }
447         if (rt0 != NULL) {
448                 if (rt_llroute(dst, rt0, &rt) != 0) {
449                         m_freem(m);
450                         return 0;
451                 }
452                 la = rt->rt_llinfo;
453         }
454         if (la == NULL) {
455                 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
456                 if (la != NULL)
457                         rt = la->la_rt;
458         }
459         if (la == NULL || rt == NULL) {
460                 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
461                     inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
462                     rt ? "rt" : "");
463                 m_freem(m);
464                 return (0);
465         }
466         sdl = SDL(rt->rt_gateway);
467         /*
468          * Check the address family and length is valid, the address
469          * is resolved; otherwise, try to resolve.
470          */
471         if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
472             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
473                 /*
474                  * If entry has an expiry time and it is approaching,
475                  * see if we need to send an ARP request within this
476                  * arpt_down interval.
477                  */
478                 if ((rt->rt_expire != 0) &&
479                     (time_second + la->la_preempt > rt->rt_expire)) {
480                         arprequest(ifp,
481                                    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
482                                    &SIN(dst)->sin_addr,
483                                    IF_LLADDR(ifp));
484                         la->la_preempt--;
485                 }
486
487                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
488                 return 1;
489         }
490         /*
491          * If ARP is disabled on this interface, stop.
492          * XXX
493          * Probably should not allocate empty llinfo struct if we are
494          * not going to be sending out an arp request.
495          */
496         if (ifp->if_flags & IFF_NOARP) {
497                 m_freem(m);
498                 return (0);
499         }
500         /*
501          * There is an arptab entry, but no ethernet address
502          * response yet.  Replace the held mbuf with this
503          * latest one.
504          */
505         if (la->la_hold != NULL)
506                 m_freem(la->la_hold);
507         la->la_hold = m;
508         if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
509                 rt->rt_flags &= ~RTF_REJECT;
510                 if (la->la_asked == 0 || rt->rt_expire != time_second) {
511                         rt->rt_expire = time_second;
512                         if (la->la_asked++ < arp_maxtries) {
513                                 arprequest(ifp,
514                                            &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
515                                            &SIN(dst)->sin_addr,
516                                            IF_LLADDR(ifp));
517                         } else {
518                                 rt->rt_flags |= RTF_REJECT;
519                                 rt->rt_expire += arpt_down;
520                                 la->la_asked = 0;
521                                 la->la_preempt = arp_maxtries;
522                         }
523
524                 }
525         }
526         return (0);
527 }
528
529 /*
530  * Common length and type checks are done here,
531  * then the protocol-specific routine is called.
532  */
533 static int
534 arpintr(struct netmsg *msg)
535 {
536         struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
537         struct arphdr *ar;
538         u_short ar_hrd;
539
540         if (m->m_len < sizeof(struct arphdr) &&
541             ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
542                 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
543                 goto out2;
544         }
545         ar = mtod(m, struct arphdr *);
546
547         ar_hrd = ntohs(ar->ar_hrd);
548         if (ar_hrd != ARPHRD_ETHER &&
549             ar_hrd != ARPHRD_IEEE802 &&
550             ar_hrd != ARPHRD_ARCNET) {
551                 log(LOG_ERR,
552                     "arp: unknown hardware address format (0x%2D)\n",
553                     (unsigned char *)&ar->ar_hrd, "");
554                 goto out1;
555         }
556
557         if (m->m_pkthdr.len < arphdr_len(ar) &&
558             (m = m_pullup(m, arphdr_len(ar))) == NULL) {
559                 log(LOG_ERR, "arp: runt packet\n");
560                 goto out1;
561         }
562
563         switch (ntohs(ar->ar_pro)) {
564 #ifdef INET
565                 case ETHERTYPE_IP:
566                         in_arpinput(m);
567                         goto out2;
568 #endif
569         }
570 out1:
571         m_freem(m);
572 out2:
573         /* msg was embedded in the mbuf, do not reply! */
574         return(EASYNC);
575 }
576
577 #ifdef INET
578 /*
579  * ARP for Internet protocols on 10 Mb/s Ethernet.
580  * Algorithm is that given in RFC 826.
581  * In addition, a sanity check is performed on the sender
582  * protocol address, to catch impersonators.
583  * We no longer handle negotiations for use of trailer protocol:
584  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
585  * along with IP replies if we wanted trailers sent to us,
586  * and also sent them in response to IP replies.
587  * This allowed either end to announce the desire to receive
588  * trailer packets.
589  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
590  * but formerly didn't normally send requests.
591  */
592 static int log_arp_wrong_iface = 1;
593 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
594         &log_arp_wrong_iface, 0,
595         "log arp packets arriving on the wrong interface");
596
597 static void
598 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
599                  boolean_t dologging)
600 {
601         struct arphdr *ah = mtod(m, struct arphdr *);
602         struct ifnet *ifp = m->m_pkthdr.rcvif;
603         struct llinfo_arp *la;
604         struct sockaddr_dl *sdl;
605         struct rtentry *rt;
606         int cpu = mycpuid;
607
608         la = arplookup(saddr, create, FALSE);
609         if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
610                 struct in_addr isaddr = { saddr };
611
612                 /* the following is not an error when doing bridging */
613                 if (rt->rt_ifp != ifp) {
614                         if (dologging && log_arp_wrong_iface && cpu == 0) {
615                                 log(LOG_ERR,
616                                     "arp: %s is on %s "
617                                     "but got reply from %*D on %s\n",
618                                     inet_ntoa(isaddr),
619                                     rt->rt_ifp->if_xname,
620                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
621                                     ifp->if_xname);
622                         }
623                         return;
624                 }
625                 if (sdl->sdl_alen &&
626                     bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
627                         if (rt->rt_expire != 0) {
628                                 if (dologging && cpu == 0) {
629                                         log(LOG_INFO,
630                                         "arp: %s moved from %*D to %*D on %s\n",
631                                         inet_ntoa(isaddr),
632                                         ifp->if_addrlen, (u_char *)LLADDR(sdl),
633                                         ":", ifp->if_addrlen,
634                                         (u_char *)ar_sha(ah), ":",
635                                         ifp->if_xname);
636                                 }
637                         } else {
638                                 if (dologging && cpu == 0) {
639                                         log(LOG_ERR,
640                                         "arp: %*D attempts to modify permanent entry for %s on %s\n",
641                                         ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
642                                         inet_ntoa(isaddr), ifp->if_xname);
643                                 }
644                                 return;
645                         }
646                 }
647                 /*
648                  * sanity check for the address length.
649                  * XXX this does not work for protocols with variable address
650                  * length. -is
651                  */
652                 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
653                     cpu == 0)
654                 {
655                         log(LOG_WARNING,
656                             "arp from %*D: new addr len %d, was %d",
657                             ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
658                             ah->ar_hln, sdl->sdl_alen);
659                 }
660                 if (ifp->if_addrlen != ah->ar_hln) {
661                         if (dologging && cpu == 0) {
662                                 log(LOG_WARNING,
663                                 "arp from %*D: addr len: new %d, i/f %d (ignored)",
664                                 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
665                                 ah->ar_hln, ifp->if_addrlen);
666                         }
667                         return;
668                 }
669                 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
670                 /*
671                  * If we receive an arp from a token-ring station over
672                  * a token-ring nic then try to save the source
673                  * routing info.
674                  */
675                 if (ifp->if_type == IFT_ISO88025) {
676                         struct iso88025_header *th =
677                             (struct iso88025_header *)m->m_pkthdr.header;
678                         struct iso88025_sockaddr_dl_data *trld =
679                             SDL_ISO88025(sdl);
680                         int rif_len;
681
682                         rif_len = TR_RCF_RIFLEN(th->rcf);
683                         if ((th->iso88025_shost[0] & TR_RII) &&
684                             (rif_len > 2)) {
685                                 trld->trld_rcf = th->rcf;
686                                 trld->trld_rcf ^= htons(TR_RCF_DIR);
687                                 memcpy(trld->trld_route, th->rd, rif_len - 2);
688                                 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
689                                 /*
690                                  * Set up source routing information for
691                                  * reply packet (XXX)
692                                  */
693                                 m->m_data -= rif_len;
694                                 m->m_len  += rif_len;
695                                 m->m_pkthdr.len += rif_len;
696                         } else {
697                                 th->iso88025_shost[0] &= ~TR_RII;
698                                 trld->trld_rcf = 0;
699                         }
700                         m->m_data -= 8;
701                         m->m_len  += 8;
702                         m->m_pkthdr.len += 8;
703                         th->rcf = trld->trld_rcf;
704                 }
705                 if (rt->rt_expire != 0)
706                         rt->rt_expire = time_second + arpt_keep;
707                 rt->rt_flags &= ~RTF_REJECT;
708                 la->la_asked = 0;
709                 la->la_preempt = arp_maxtries;
710
711                 /*
712                  * This particular cpu might have been holding an mbuf
713                  * pending ARP resolution.  If so, transmit the mbuf now.
714                  */
715                 if (la->la_hold != NULL) {
716                         m_adj(la->la_hold, sizeof(struct ether_header));
717                         lwkt_serialize_enter(ifp->if_serializer);
718                         (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
719                         lwkt_serialize_exit(ifp->if_serializer);
720                         la->la_hold = NULL;
721                 }
722         }
723 }
724
725 #ifdef SMP
726
727 struct netmsg_arp_update {
728         struct lwkt_msg lmsg;
729         struct mbuf     *m;
730         in_addr_t       saddr;
731         boolean_t       create;
732 };
733
734 static int arp_update_msghandler(struct lwkt_msg *lmsg);
735
736 #endif
737
738 /*
739  * Called from arpintr() - this routine is run from a single cpu.
740  */
741 static void
742 in_arpinput(struct mbuf *m)
743 {
744         struct arphdr *ah;
745         struct ifnet *ifp = m->m_pkthdr.rcvif;
746         struct ether_header *eh;
747         struct arc_header *arh;
748         struct iso88025_header *th = (struct iso88025_header *)NULL;
749         struct rtentry *rt;
750         struct ifaddr *ifa;
751         struct in_ifaddr *ia;
752         struct sockaddr sa;
753         struct in_addr isaddr, itaddr, myaddr;
754 #ifdef SMP
755         struct netmsg_arp_update msg;
756 #endif
757         int op;
758         int req_len;
759
760         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
761         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
762                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
763                 return;
764         }
765
766         ah = mtod(m, struct arphdr *);
767         op = ntohs(ah->ar_op);
768         memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
769         memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
770         /*
771          * Check both target and sender IP addresses:
772          *
773          * If we receive the packet on the interface owning the address,
774          * then accept the address.
775          *
776          * For a bridge, we accept the address if the receive interface and
777          * the interface owning the address are on the same bridge.
778          * (This will change slightly when we have clusters of interfaces).
779          */
780         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
781                 /* Skip all ia's which don't match */
782                 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
783                         continue;
784
785                 if (ia->ia_ifp == ifp)
786                         goto match;
787
788                 if (ifp->if_bridge && ia->ia_ifp && 
789                     ifp->if_bridge == ia->ia_ifp->if_bridge)
790                         goto match;
791         }
792         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) {
793                 /* Skip all ia's which don't match */
794                 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
795                         continue;
796
797                 if (ia->ia_ifp == ifp)
798                         goto match;
799
800                 if (ifp->if_bridge && ia->ia_ifp &&
801                     ifp->if_bridge == ia->ia_ifp->if_bridge)
802                         goto match;
803         }
804         /*
805          * No match, use the first inet address on the receive interface
806          * as a dummy address for the rest of the function.
807          */
808         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
809                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
810                         ia = ifatoia(ifa);
811                         goto match;
812                 }
813         }
814         /*
815          * If we got here, we didn't find any suitable interface,
816          * so drop the packet.
817          */
818         m_freem(m);
819         return;
820
821 match:
822         myaddr = ia->ia_addr.sin_addr;
823         if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
824                 m_freem(m);     /* it's from me, ignore it. */
825                 return;
826         }
827         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
828                 log(LOG_ERR,
829                     "arp: link address is broadcast for IP address %s!\n",
830                     inet_ntoa(isaddr));
831                 m_freem(m);
832                 return;
833         }
834         if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
835                 log(LOG_ERR,
836                    "arp: %*D is using my IP address %s!\n",
837                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
838                    inet_ntoa(isaddr));
839                 itaddr = myaddr;
840                 goto reply;
841         }
842 #ifdef SMP
843         lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0, 
844                      lwkt_cmd_func(arp_update_msghandler), lwkt_cmd_op_none);
845         msg.m = m;
846         msg.saddr = isaddr.s_addr;
847         msg.create = (itaddr.s_addr == myaddr.s_addr);
848         lwkt_domsg(rtable_portfn(0), &msg.lmsg);
849 #endif
850         arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
851                          TRUE);
852 reply:
853         if (op != ARPOP_REQUEST) {
854                 m_freem(m);
855                 return;
856         }
857         if (itaddr.s_addr == myaddr.s_addr) {
858                 /* I am the target */
859                 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
860                 memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
861         } else {
862                 struct llinfo_arp *la;
863
864                 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
865                 if (la == NULL) {
866                         struct sockaddr_in sin;
867
868                         if (!arp_proxyall) {
869                                 m_freem(m);
870                                 return;
871                         }
872
873                         bzero(&sin, sizeof sin);
874                         sin.sin_family = AF_INET;
875                         sin.sin_len = sizeof sin;
876                         sin.sin_addr = itaddr;
877
878                         rt = rtpurelookup((struct sockaddr *)&sin);
879                         if (rt == NULL) {
880                                 m_freem(m);
881                                 return;
882                         }
883                         --rt->rt_refcnt;
884                         /*
885                          * Don't send proxies for nodes on the same interface
886                          * as this one came out of, or we'll get into a fight
887                          * over who claims what Ether address.
888                          */
889                         if (rt->rt_ifp == ifp) {
890                                 m_freem(m);
891                                 return;
892                         }
893                         memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
894                         memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
895 #ifdef DEBUG_PROXY
896                         kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
897 #endif
898                 } else {
899                         struct sockaddr_dl *sdl;
900
901                         rt = la->la_rt;
902                         memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
903                         sdl = SDL(rt->rt_gateway);
904                         memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
905                 }
906         }
907
908         memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
909         memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
910         ah->ar_op = htons(ARPOP_REPLY);
911         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
912         switch (ifp->if_type) {
913         case IFT_ARCNET:
914                 arh = (struct arc_header *)sa.sa_data;
915                 arh->arc_dhost = *ar_tha(ah);
916                 arh->arc_type = ARCTYPE_ARP;
917                 break;
918         case IFT_ISO88025:
919                 /* Re-arrange the source/dest address */
920                 memcpy(th->iso88025_dhost, th->iso88025_shost,
921                     sizeof th->iso88025_dhost);
922                 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
923                     sizeof th->iso88025_shost);
924                 /* Set the source routing bit if neccesary */
925                 if (th->iso88025_dhost[0] & TR_RII) {
926                         th->iso88025_dhost[0] &= ~TR_RII;
927                         if (TR_RCF_RIFLEN(th->rcf) > 2)
928                                 th->iso88025_shost[0] |= TR_RII;
929                 }
930                 /* Copy the addresses, ac and fc into sa_data */
931                 memcpy(sa.sa_data, th->iso88025_dhost,
932                     (sizeof th->iso88025_dhost) * 2);
933                 sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC;
934                 sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME;
935                 break;
936         case IFT_ETHER:
937         case IFT_FDDI:
938         /*
939          * May not be correct for types not explictly
940          * listed, but it is our best guess.
941          */
942         default:
943                 eh = (struct ether_header *)sa.sa_data;
944                 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
945                 eh->ether_type = htons(ETHERTYPE_ARP);
946                 break;
947         }
948         sa.sa_family = AF_UNSPEC;
949         sa.sa_len = sizeof sa;
950         lwkt_serialize_enter(ifp->if_serializer);
951         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
952         lwkt_serialize_exit(ifp->if_serializer);
953         return;
954 }
955
956 #ifdef SMP
957
958 static
959 int
960 arp_update_msghandler(struct lwkt_msg *lmsg)
961 {
962         struct netmsg_arp_update *msg = (struct netmsg_arp_update *)lmsg;
963         int nextcpu;
964
965         arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
966
967         nextcpu = mycpuid + 1;
968         if (nextcpu < ncpus) {
969                 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
970         } else {
971                 lwkt_replymsg(&msg->lmsg, 0);
972         }
973         return (0);
974 }
975
976 #endif
977
978 #endif
979
980 /*
981  * Free an arp entry.  If the arp entry is actively referenced or represents
982  * a static entry we only clear it back to an unresolved state, otherwise
983  * we destroy the entry entirely.
984  *
985  * Note that static entries are created when route add ... -interface is used
986  * to create an interface route to a (direct) destination.
987  */
988 static void
989 arptfree(struct llinfo_arp *la)
990 {
991         struct rtentry *rt = la->la_rt;
992         struct sockaddr_dl *sdl;
993
994         if (rt == NULL)
995                 panic("arptfree");
996         sdl = SDL(rt->rt_gateway);
997         if (sdl != NULL &&
998             ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
999              (rt->rt_flags & RTF_STATIC))) {
1000                 sdl->sdl_alen = 0;
1001                 la->la_preempt = la->la_asked = 0;
1002                 rt->rt_flags &= ~RTF_REJECT;
1003                 return;
1004         }
1005         rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1006 }
1007
1008 /*
1009  * Lookup or enter a new address in arptab.
1010  */
1011 static struct llinfo_arp *
1012 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
1013 {
1014         struct rtentry *rt;
1015         struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1016         const char *why = NULL;
1017
1018         sin.sin_addr.s_addr = addr;
1019         sin.sin_other = proxy ? SIN_PROXY : 0;
1020         if (create)
1021                 rt = rtlookup((struct sockaddr *)&sin);
1022         else
1023                 rt = rtpurelookup((struct sockaddr *)&sin);
1024         if (rt == NULL)
1025                 return (NULL);
1026         rt->rt_refcnt--;
1027
1028         if (rt->rt_flags & RTF_GATEWAY)
1029                 why = "host is not on local network";
1030         else if (!(rt->rt_flags & RTF_LLINFO))
1031                 why = "could not allocate llinfo";
1032         else if (rt->rt_gateway->sa_family != AF_LINK)
1033                 why = "gateway route is not ours";
1034
1035         if (why) {
1036                 if (create) {
1037                         log(LOG_DEBUG, "arplookup %s failed: %s\n",
1038                             inet_ntoa(sin.sin_addr), why);
1039                 }
1040                 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1041                         /* No references to this route.  Purge it. */
1042                         rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1043                                   rt_mask(rt), rt->rt_flags, NULL);
1044                 }
1045                 return (NULL);
1046         }
1047         return (rt->rt_llinfo);
1048 }
1049
1050 void
1051 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1052 {
1053         if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1054                 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1055                            IF_LLADDR(ifp));
1056         ifa->ifa_rtrequest = arp_rtrequest;
1057         ifa->ifa_flags |= RTF_CLONING;
1058 }
1059
1060 static void
1061 arp_init(void)
1062 {
1063         int cpu;
1064
1065         for (cpu = 0; cpu < ncpus2; cpu++)
1066                 LIST_INIT(&llinfo_arp_list[cpu]);
1067         netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
1068 }
1069
1070 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);