]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/if_ether.c
Move "options MAC" from opt_mac.h to opt_global.h, as it's now in GENERIC
[FreeBSD/FreeBSD.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
30  */
31
32 /*
33  * Ethernet address resolution protocol.
34  * TODO:
35  *      add "inuse/lock" bit (or ref. count) along with valid bit
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_inet.h"
42 #include "opt_route.h"
43 #include "opt_carp.h"
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.h>
55 #include <sys/vimage.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
61 #include <net/netisr.h>
62 #include <net/if_llc.h>
63 #include <net/ethernet.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <net/if_llatbl.h>
69 #include <netinet/if_ether.h>
70 #include <netinet/vinet.h>
71
72 #include <net/if_arc.h>
73 #include <net/iso88025.h>
74
75 #ifdef DEV_CARP
76 #include <netinet/ip_carp.h>
77 #endif
78
79 #include <security/mac/mac_framework.h>
80
81 #define SIN(s) ((struct sockaddr_in *)s)
82 #define SDL(s) ((struct sockaddr_dl *)s)
83
84 SYSCTL_DECL(_net_link_ether);
85 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
86
87 /* timer values */
88 #ifdef VIMAGE_GLOBALS
89 static int      arpt_keep; /* once resolved, good for 20 more minutes */
90 static int      arp_maxtries;
91 int     useloopback; /* use loopback interface for local traffic */
92 static int      arp_proxyall;
93 #endif
94
95 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, max_age,
96     CTLFLAG_RW, arpt_keep, 0, "ARP entry lifetime in seconds");
97
98 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, maxtries,
99         CTLFLAG_RW, arp_maxtries, 0,
100         "ARP resolution attempts before returning error");
101 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, useloopback,
102         CTLFLAG_RW, useloopback, 0,
103         "Use the loopback interface for local traffic");
104 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, proxyall,
105         CTLFLAG_RW, arp_proxyall, 0,
106         "Enable proxy ARP for all suitable requests");
107
108 static void     arp_init(void);
109 static int      arp_iattach(const void *);
110 void            arprequest(struct ifnet *,
111                         struct in_addr *, struct in_addr *, u_char *);
112 static void     arpintr(struct mbuf *);
113 static void     arptimer(void *);
114 #ifdef INET
115 static void     in_arpinput(struct mbuf *);
116 #endif
117
118 static const struct netisr_handler arp_nh = {
119         .nh_name = "arp",
120         .nh_handler = arpintr,
121         .nh_proto = NETISR_ARP,
122         .nh_policy = NETISR_POLICY_SOURCE,
123 };
124
125 #ifndef VIMAGE_GLOBALS
126 static const vnet_modinfo_t vnet_arp_modinfo = {
127         .vmi_id         = VNET_MOD_ARP,
128         .vmi_name       = "arp",
129         .vmi_dependson  = VNET_MOD_INET,
130         .vmi_iattach    = arp_iattach
131 };
132 #endif /* !VIMAGE_GLOBALS */
133
134 #ifdef AF_INET
135 void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
136
137 /*
138  * called by in_ifscrub to remove entry from the table when
139  * the interface goes away
140  */
141 void
142 arp_ifscrub(struct ifnet *ifp, uint32_t addr)
143 {
144         struct sockaddr_in addr4;
145
146         bzero((void *)&addr4, sizeof(addr4));
147         addr4.sin_len    = sizeof(addr4);
148         addr4.sin_family = AF_INET;
149         addr4.sin_addr.s_addr = addr;
150         CURVNET_SET(ifp->if_vnet);
151         IF_AFDATA_LOCK(ifp);
152         lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR),
153             (struct sockaddr *)&addr4);
154         IF_AFDATA_UNLOCK(ifp);
155         CURVNET_RESTORE();
156 }
157 #endif
158
159 /*
160  * Timeout routine.  Age arp_tab entries periodically.
161  */
162 static void
163 arptimer(void *arg)
164 {
165         struct ifnet *ifp;
166         struct llentry   *lle = (struct llentry *)arg;
167
168         if (lle == NULL) {
169                 panic("%s: NULL entry!\n", __func__);
170                 return;
171         }
172         ifp = lle->lle_tbl->llt_ifp;
173         IF_AFDATA_LOCK(ifp);
174         LLE_WLOCK(lle);
175         if (((lle->la_flags & LLE_DELETED)
176                 || (time_second >= lle->la_expire))
177             && (!callout_pending(&lle->la_timer) &&
178                 callout_active(&lle->la_timer)))
179                 (void) llentry_free(lle);
180         else {
181                 /*
182                  * Still valid, just drop our reference
183                  */
184                 LLE_FREE_LOCKED(lle);
185         }
186         IF_AFDATA_UNLOCK(ifp);
187 }
188
189 /*
190  * Broadcast an ARP request. Caller specifies:
191  *      - arp header source ip address
192  *      - arp header target ip address
193  *      - arp header source ethernet address
194  */
195 void
196 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr  *tip,
197     u_char *enaddr)
198 {
199         struct mbuf *m;
200         struct arphdr *ah;
201         struct sockaddr sa;
202
203         if (sip == NULL) {
204                 /* XXX don't believe this can happen (or explain why) */
205                 /*
206                  * The caller did not supply a source address, try to find
207                  * a compatible one among those assigned to this interface.
208                  */
209                 struct ifaddr *ifa;
210
211                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
212                         if (!ifa->ifa_addr ||
213                             ifa->ifa_addr->sa_family != AF_INET)
214                                 continue;
215                         sip = &SIN(ifa->ifa_addr)->sin_addr;
216                         if (0 == ((sip->s_addr ^ tip->s_addr) &
217                             SIN(ifa->ifa_netmask)->sin_addr.s_addr) )
218                                 break;  /* found it. */
219                 }
220                 if (sip == NULL) {  
221                         printf("%s: cannot find matching address\n", __func__);
222                         return;
223                 }
224         }
225
226         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
227                 return;
228         m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
229                 2*ifp->if_data.ifi_addrlen;
230         m->m_pkthdr.len = m->m_len;
231         MH_ALIGN(m, m->m_len);
232         ah = mtod(m, struct arphdr *);
233         bzero((caddr_t)ah, m->m_len);
234 #ifdef MAC
235         mac_netinet_arp_send(ifp, m);
236 #endif
237         ah->ar_pro = htons(ETHERTYPE_IP);
238         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
239         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
240         ah->ar_op = htons(ARPOP_REQUEST);
241         bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
242         bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
243         bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
244         sa.sa_family = AF_ARP;
245         sa.sa_len = 2;
246         m->m_flags |= M_BCAST;
247         (*ifp->if_output)(ifp, m, &sa, NULL);
248 }
249
250 /*
251  * Resolve an IP address into an ethernet address.
252  * On input:
253  *    ifp is the interface we use
254  *    rt0 is the route to the final destination (possibly useless)
255  *    m is the mbuf. May be NULL if we don't have a packet.
256  *    dst is the next hop,
257  *    desten is where we want the address.
258  *
259  * On success, desten is filled in and the function returns 0;
260  * If the packet must be held pending resolution, we return EWOULDBLOCK
261  * On other errors, we return the corresponding error code.
262  * Note that m_freem() handles NULL.
263  */
264 int
265 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
266         struct sockaddr *dst, u_char *desten, struct llentry **lle)
267 {
268         INIT_VNET_INET(ifp->if_vnet);
269         struct llentry *la = 0;
270         u_int flags = 0;
271         int error, renew;
272
273         *lle = NULL;
274         if (m != NULL) {
275                 if (m->m_flags & M_BCAST) {
276                         /* broadcast */
277                         (void)memcpy(desten,
278                             ifp->if_broadcastaddr, ifp->if_addrlen);
279                         return (0);
280                 }
281                 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
282                         /* multicast */
283                         ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
284                         return (0);
285                 }
286         }
287         /* XXXXX
288          */
289 retry:
290         IF_AFDATA_RLOCK(ifp);   
291         la = lla_lookup(LLTABLE(ifp), flags, dst);
292         IF_AFDATA_RUNLOCK(ifp); 
293         if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0)
294             && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) {          
295                 flags |= (LLE_CREATE | LLE_EXCLUSIVE);
296                 IF_AFDATA_WLOCK(ifp);   
297                 la = lla_lookup(LLTABLE(ifp), flags, dst);
298                 IF_AFDATA_WUNLOCK(ifp); 
299         }
300         if (la == NULL) {
301                 if (flags & LLE_CREATE)
302                         log(LOG_DEBUG,
303                             "arpresolve: can't allocate llinfo for %s\n",
304                             inet_ntoa(SIN(dst)->sin_addr));
305                 m_freem(m);
306                 return (EINVAL);
307         } 
308
309         if ((la->la_flags & LLE_VALID) &&
310             ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
311                 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
312                 /*
313                  * If entry has an expiry time and it is approaching,
314                  * see if we need to send an ARP request within this
315                  * arpt_down interval.
316                  */
317                 if (!(la->la_flags & LLE_STATIC) &&
318                     time_uptime + la->la_preempt > la->la_expire) {
319                         arprequest(ifp, NULL,
320                             &SIN(dst)->sin_addr, IF_LLADDR(ifp));
321
322                         la->la_preempt--;
323                 }
324                 
325                 *lle = la;
326                 error = 0;
327                 goto done;
328         } 
329                             
330         if (la->la_flags & LLE_STATIC) {   /* should not happen! */
331                 log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n",
332                     inet_ntoa(SIN(dst)->sin_addr));
333                 m_freem(m);
334                 error = EINVAL;
335                 goto done;
336         }
337
338         renew = (la->la_asked == 0 || la->la_expire != time_uptime);
339         if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) {
340                 flags |= LLE_EXCLUSIVE;
341                 LLE_RUNLOCK(la);
342                 goto retry;
343         }
344         /*
345          * There is an arptab entry, but no ethernet address
346          * response yet.  Replace the held mbuf with this
347          * latest one.
348          */
349         if (m != NULL) {
350                 if (la->la_hold != NULL)
351                         m_freem(la->la_hold);
352                 la->la_hold = m;
353                 if (renew == 0 && (flags & LLE_EXCLUSIVE)) {
354                         flags &= ~LLE_EXCLUSIVE;
355                         LLE_DOWNGRADE(la);
356                 }
357                 
358         }
359         /*
360          * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
361          * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
362          * if we have already sent arp_maxtries ARP requests. Retransmit the
363          * ARP request, but not faster than one request per second.
364          */
365         if (la->la_asked < V_arp_maxtries)
366                 error = EWOULDBLOCK;    /* First request. */
367         else
368                 error =
369                     (rt0->rt_flags & RTF_GATEWAY) ? EHOSTDOWN : EHOSTUNREACH;
370
371         if (renew) {
372                 LLE_ADDREF(la);
373                 la->la_expire = time_uptime;
374                 callout_reset(&la->la_timer, hz, arptimer, la);
375                 la->la_asked++;
376                 LLE_WUNLOCK(la);
377                 arprequest(ifp, NULL, &SIN(dst)->sin_addr,
378                     IF_LLADDR(ifp));
379                 return (error);
380         }
381 done:
382         if (flags & LLE_EXCLUSIVE)
383                 LLE_WUNLOCK(la);
384         else
385                 LLE_RUNLOCK(la);
386         return (error);
387 }
388
389 /*
390  * Common length and type checks are done here,
391  * then the protocol-specific routine is called.
392  */
393 static void
394 arpintr(struct mbuf *m)
395 {
396         struct arphdr *ar;
397
398         if (m->m_len < sizeof(struct arphdr) &&
399             ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
400                 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
401                 return;
402         }
403         ar = mtod(m, struct arphdr *);
404
405         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
406             ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
407             ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
408             ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
409                 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
410                     (unsigned char *)&ar->ar_hrd, "");
411                 m_freem(m);
412                 return;
413         }
414
415         if (m->m_len < arphdr_len(ar)) {
416                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
417                         log(LOG_ERR, "arp: runt packet\n");
418                         m_freem(m);
419                         return;
420                 }
421                 ar = mtod(m, struct arphdr *);
422         }
423
424         switch (ntohs(ar->ar_pro)) {
425 #ifdef INET
426         case ETHERTYPE_IP:
427                 in_arpinput(m);
428                 return;
429 #endif
430         }
431         m_freem(m);
432 }
433
434 #ifdef INET
435 /*
436  * ARP for Internet protocols on 10 Mb/s Ethernet.
437  * Algorithm is that given in RFC 826.
438  * In addition, a sanity check is performed on the sender
439  * protocol address, to catch impersonators.
440  * We no longer handle negotiations for use of trailer protocol:
441  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
442  * along with IP replies if we wanted trailers sent to us,
443  * and also sent them in response to IP replies.
444  * This allowed either end to announce the desire to receive
445  * trailer packets.
446  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
447  * but formerly didn't normally send requests.
448  */
449 static int log_arp_wrong_iface = 1;
450 static int log_arp_movements = 1;
451 static int log_arp_permanent_modify = 1;
452
453 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
454         &log_arp_wrong_iface, 0,
455         "log arp packets arriving on the wrong interface");
456 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
457         &log_arp_movements, 0,
458         "log arp replies from MACs different than the one in the cache");
459 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
460         &log_arp_permanent_modify, 0,
461         "log arp replies from MACs different than the one in the permanent arp entry");
462
463
464 static void
465 in_arpinput(struct mbuf *m)
466 {
467         struct arphdr *ah;
468         struct ifnet *ifp = m->m_pkthdr.rcvif;
469         struct llentry *la = NULL;
470         struct rtentry *rt;
471         struct ifaddr *ifa;
472         struct in_ifaddr *ia;
473         struct sockaddr sa;
474         struct in_addr isaddr, itaddr, myaddr;
475         u_int8_t *enaddr = NULL;
476         int op, flags;
477         struct mbuf *m0;
478         int req_len;
479         int bridged = 0, is_bridge = 0;
480 #ifdef DEV_CARP
481         int carp_match = 0;
482 #endif
483         struct sockaddr_in sin;
484         sin.sin_len = sizeof(struct sockaddr_in);
485         sin.sin_family = AF_INET;
486         sin.sin_addr.s_addr = 0;
487         INIT_VNET_INET(ifp->if_vnet);
488
489         if (ifp->if_bridge)
490                 bridged = 1;
491         if (ifp->if_type == IFT_BRIDGE)
492                 is_bridge = 1;
493
494         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
495         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
496                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
497                 return;
498         }
499
500         ah = mtod(m, struct arphdr *);
501         op = ntohs(ah->ar_op);
502         (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
503         (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
504
505         /*
506          * For a bridge, we want to check the address irrespective
507          * of the receive interface. (This will change slightly
508          * when we have clusters of interfaces).
509          * If the interface does not match, but the recieving interface
510          * is part of carp, we call carp_iamatch to see if this is a
511          * request for the virtual host ip.
512          * XXX: This is really ugly!
513          */
514         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
515                 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
516                     ia->ia_ifp == ifp) &&
517                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
518                         goto match;
519 #ifdef DEV_CARP
520                 if (ifp->if_carp != NULL &&
521                     carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
522                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
523                         carp_match = 1;
524                         goto match;
525                 }
526 #endif
527         }
528         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
529                 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
530                     ia->ia_ifp == ifp) &&
531                     isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
532                         goto match;
533
534 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)                           \
535   (ia->ia_ifp->if_bridge == ifp->if_softc &&                            \
536   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&      \
537   addr == ia->ia_addr.sin_addr.s_addr)
538         /*
539          * Check the case when bridge shares its MAC address with
540          * some of its children, so packets are claimed by bridge
541          * itself (bridge_input() does it first), but they are really
542          * meant to be destined to the bridge member.
543          */
544         if (is_bridge) {
545                 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
546                         if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
547                                 ifp = ia->ia_ifp;
548                                 goto match;
549                         }
550                 }
551         }
552 #undef BDG_MEMBER_MATCHES_ARP
553
554         /*
555          * No match, use the first inet address on the receive interface
556          * as a dummy address for the rest of the function.
557          */
558         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
559                 if (ifa->ifa_addr->sa_family == AF_INET) {
560                         ia = ifatoia(ifa);
561                         goto match;
562                 }
563         /*
564          * If bridging, fall back to using any inet address.
565          */
566         if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL)
567                 goto drop;
568 match:
569         if (!enaddr)
570                 enaddr = (u_int8_t *)IF_LLADDR(ifp);
571         myaddr = ia->ia_addr.sin_addr;
572         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
573                 goto drop;      /* it's from me, ignore it. */
574         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
575                 log(LOG_ERR,
576                     "arp: link address is broadcast for IP address %s!\n",
577                     inet_ntoa(isaddr));
578                 goto drop;
579         }
580         /*
581          * Warn if another host is using the same IP address, but only if the
582          * IP address isn't 0.0.0.0, which is used for DHCP only, in which
583          * case we suppress the warning to avoid false positive complaints of
584          * potential misconfiguration.
585          */
586         if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
587                 log(LOG_ERR,
588                    "arp: %*D is using my IP address %s on %s!\n",
589                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
590                    inet_ntoa(isaddr), ifp->if_xname);
591                 itaddr = myaddr;
592                 goto reply;
593         }
594         if (ifp->if_flags & IFF_STATICARP)
595                 goto reply;
596
597         bzero(&sin, sizeof(sin));
598         sin.sin_len = sizeof(struct sockaddr_in);
599         sin.sin_family = AF_INET;
600         sin.sin_addr = isaddr;
601         flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0;
602         flags |= LLE_EXCLUSIVE;
603         IF_AFDATA_LOCK(ifp); 
604         la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin);
605         IF_AFDATA_UNLOCK(ifp);
606         if (la != NULL) {
607                 /* the following is not an error when doing bridging */
608                 if (!bridged && la->lle_tbl->llt_ifp != ifp
609 #ifdef DEV_CARP
610                     && (ifp->if_type != IFT_CARP || !carp_match)
611 #endif
612                         ) {
613                         if (log_arp_wrong_iface)
614                                 log(LOG_ERR, "arp: %s is on %s "
615                                     "but got reply from %*D on %s\n",
616                                     inet_ntoa(isaddr),
617                                     la->lle_tbl->llt_ifp->if_xname,
618                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
619                                     ifp->if_xname);
620                         goto reply;
621                 }
622                 if ((la->la_flags & LLE_VALID) &&
623                     bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
624                         if (la->la_flags & LLE_STATIC) {
625                                 log(LOG_ERR,
626                                     "arp: %*D attempts to modify permanent "
627                                     "entry for %s on %s\n",
628                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
629                                     inet_ntoa(isaddr), ifp->if_xname);
630                                 goto reply;
631                         }
632                         if (log_arp_movements) {
633                                 log(LOG_INFO, "arp: %s moved from %*D "
634                                     "to %*D on %s\n",
635                                     inet_ntoa(isaddr),
636                                     ifp->if_addrlen,
637                                     (u_char *)&la->ll_addr, ":",
638                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
639                                     ifp->if_xname);
640                         }
641                 }
642                     
643                 if (ifp->if_addrlen != ah->ar_hln) {
644                         log(LOG_WARNING,
645                             "arp from %*D: addr len: new %d, i/f %d (ignored)",
646                             ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
647                             ah->ar_hln, ifp->if_addrlen);
648                         goto reply;
649                 }
650                 (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
651                 la->la_flags |= LLE_VALID;
652
653                 if (!(la->la_flags & LLE_STATIC)) {
654                         la->la_expire = time_uptime + V_arpt_keep;
655                         callout_reset(&la->la_timer, hz * V_arpt_keep,
656                             arptimer, la);
657                 }
658                 la->la_asked = 0;
659                 la->la_preempt = V_arp_maxtries;
660                 if (la->la_hold != NULL) {
661                         m0 = la->la_hold;
662                         la->la_hold = 0;
663                         memcpy(&sa, L3_ADDR(la), sizeof(sa));
664                         LLE_WUNLOCK(la);
665                         
666                         (*ifp->if_output)(ifp, m0, &sa, NULL);
667                         return;
668                 }
669         }
670 reply:
671         if (op != ARPOP_REQUEST)
672                 goto drop;
673
674         if (itaddr.s_addr == myaddr.s_addr) {
675                 /* Shortcut.. the receiving interface is the target. */
676                 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
677                 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
678         } else {
679                 struct llentry *lle = NULL;
680
681                 if (!V_arp_proxyall)
682                         goto drop;
683
684                 sin.sin_addr = itaddr;
685                 /* XXX MRT use table 0 for arp reply  */
686                 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
687                 if (!rt)
688                         goto drop;
689
690                 /*
691                  * Don't send proxies for nodes on the same interface
692                  * as this one came out of, or we'll get into a fight
693                  * over who claims what Ether address.
694                  */
695                 if (!rt->rt_ifp || rt->rt_ifp == ifp) {
696                         RTFREE_LOCKED(rt);
697                         goto drop;
698                 }
699                 IF_AFDATA_LOCK(rt->rt_ifp); 
700                 lle = lla_lookup(LLTABLE(rt->rt_ifp), 0, (struct sockaddr *)&sin);
701                 IF_AFDATA_UNLOCK(rt->rt_ifp);
702                 RTFREE_LOCKED(rt);
703
704                 if (lle != NULL) {
705                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
706                         (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
707                         LLE_RUNLOCK(lle);
708                 } else
709                         goto drop;
710
711                 /*
712                  * Also check that the node which sent the ARP packet
713                  * is on the the interface we expect it to be on. This
714                  * avoids ARP chaos if an interface is connected to the
715                  * wrong network.
716                  */
717                 sin.sin_addr = isaddr;
718
719                 /* XXX MRT use table 0 for arp checks */
720                 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
721                 if (!rt)
722                         goto drop;
723                 if (rt->rt_ifp != ifp) {
724                         log(LOG_INFO, "arp_proxy: ignoring request"
725                             " from %s via %s, expecting %s\n",
726                             inet_ntoa(isaddr), ifp->if_xname,
727                             rt->rt_ifp->if_xname);
728                         RTFREE_LOCKED(rt);
729                         goto drop;
730                 }
731                 RTFREE_LOCKED(rt);
732
733 #ifdef DEBUG_PROXY
734                 printf("arp: proxying for %s\n",
735                        inet_ntoa(itaddr));
736 #endif
737         }
738
739         if (la != NULL)
740                 LLE_WUNLOCK(la);
741         if (itaddr.s_addr == myaddr.s_addr &&
742             IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
743                 /* RFC 3927 link-local IPv4; always reply by broadcast. */
744 #ifdef DEBUG_LINKLOCAL
745                 printf("arp: sending reply for link-local addr %s\n",
746                     inet_ntoa(itaddr));
747 #endif
748                 m->m_flags |= M_BCAST;
749                 m->m_flags &= ~M_MCAST;
750         } else {
751                 /* default behaviour; never reply by broadcast. */
752                 m->m_flags &= ~(M_BCAST|M_MCAST);
753         }
754         (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
755         (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
756         ah->ar_op = htons(ARPOP_REPLY);
757         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
758         m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);   
759         m->m_pkthdr.len = m->m_len;   
760         sa.sa_family = AF_ARP;
761         sa.sa_len = 2;
762         (*ifp->if_output)(ifp, m, &sa, NULL);
763         return;
764
765 drop:
766         if (la != NULL)
767                 LLE_WUNLOCK(la);
768         m_freem(m);
769 }
770 #endif
771
772 void
773 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
774 {
775         struct llentry *lle;
776
777         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) {
778                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
779                                 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
780                 /* 
781                  * interface address is considered static entry
782                  * because the output of the arp utility shows
783                  * that L2 entry as permanent
784                  */
785                 IF_AFDATA_LOCK(ifp);
786                 lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC),
787                                  (struct sockaddr *)IA_SIN(ifa));
788                 IF_AFDATA_UNLOCK(ifp);
789                 if (lle == NULL)
790                         log(LOG_INFO, "arp_ifinit: cannot create arp "
791                             "entry for interface address\n");
792                 else
793                         LLE_RUNLOCK(lle);
794         }
795         ifa->ifa_rtrequest = NULL;
796 }
797
798 void
799 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
800 {
801         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
802                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
803                                 &IA_SIN(ifa)->sin_addr, enaddr);
804         ifa->ifa_rtrequest = NULL;
805 }
806
807 static int
808 arp_iattach(const void *unused __unused)
809 {
810         INIT_VNET_INET(curvnet);
811
812         V_arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
813         V_arp_maxtries = 5;
814         V_useloopback = 1; /* use loopback interface for local traffic */
815         V_arp_proxyall = 0;
816
817         return (0);
818 }
819
820 static void
821 arp_init(void)
822 {
823
824 #ifndef VIMAGE_GLOBALS
825         vnet_mod_register(&vnet_arp_modinfo);
826 #else
827         arp_iattach(NULL);
828 #endif
829
830         netisr_register(&arp_nh);
831 }
832 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);