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