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