]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/if_ether.c
- Add VRF id to sctp_ifa structure, needed mainly in panda but useful
[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  * $FreeBSD$
31  */
32
33 /*
34  * Ethernet address resolution protocol.
35  * TODO:
36  *      add "inuse/lock" bit (or ref. count) along with valid bit
37  */
38
39 #include "opt_inet.h"
40 #include "opt_mac.h"
41 #include "opt_carp.h"
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/netisr.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/if_ether.h>
64
65 #include <net/if_arc.h>
66 #include <net/iso88025.h>
67
68 #ifdef DEV_CARP
69 #include <netinet/ip_carp.h>
70 #endif
71
72 #include <security/mac/mac_framework.h>
73
74 #define SIN(s) ((struct sockaddr_in *)s)
75 #define SDL(s) ((struct sockaddr_dl *)s)
76
77 SYSCTL_DECL(_net_link_ether);
78 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
79
80 /* timer values */
81 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
82
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 
84            &arpt_keep, 0, "ARP entry lifetime in seconds");
85
86 #define rt_expire rt_rmx.rmx_expire
87
88 struct llinfo_arp {
89         struct  callout la_timer;
90         struct  rtentry *la_rt;
91         struct  mbuf *la_hold;  /* last packet until resolved/timeout */
92         u_short la_preempt;     /* countdown for pre-expiry arps */
93         u_short la_asked;       /* # requests sent */
94 };
95
96 static struct   ifqueue arpintrq;
97 static int      arp_allocated;
98
99 static int      arp_maxtries = 5;
100 static int      useloopback = 1; /* use loopback interface for local traffic */
101 static int      arp_proxyall = 0;
102
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
104            &arp_maxtries, 0, "ARP resolution attempts before returning error");
105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
106            &useloopback, 0, "Use the loopback interface for local traffic");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
108            &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
109
110 static void     arp_init(void);
111 static void     arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112 static 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 static struct rtentry
117                 *arplookup(u_long, int, int);
118 #ifdef INET
119 static void     in_arpinput(struct mbuf *);
120 #endif
121
122 /*
123  * Timeout routine.
124  */
125 static void
126 arptimer(void *arg)
127 {
128         struct rtentry *rt = (struct rtentry *)arg;
129
130         RT_LOCK_ASSERT(rt);
131         /*
132          * The lock is needed to close a theoretical race
133          * between spontaneous expiry and intentional removal.
134          * We still got an extra reference on rtentry, so can
135          * safely pass pointers to its contents.
136          */
137         RT_UNLOCK(rt);
138
139         rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
140 }
141
142 /*
143  * Parallel to llc_rtrequest.
144  */
145 static void
146 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
147 {
148         struct sockaddr *gate;
149         struct llinfo_arp *la;
150         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151         struct in_ifaddr *ia;
152         struct ifaddr *ifa;
153
154         RT_LOCK_ASSERT(rt);
155
156         if (rt->rt_flags & RTF_GATEWAY)
157                 return;
158         gate = rt->rt_gateway;
159         la = (struct llinfo_arp *)rt->rt_llinfo;
160         switch (req) {
161
162         case RTM_ADD:
163                 /*
164                  * XXX: If this is a manually added route to interface
165                  * such as older version of routed or gated might provide,
166                  * restore cloning bit.
167                  */
168                 if ((rt->rt_flags & RTF_HOST) == 0 &&
169                     rt_mask(rt) != NULL &&
170                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
171                         rt->rt_flags |= RTF_CLONING;
172                 if (rt->rt_flags & RTF_CLONING) {
173                         /*
174                          * Case 1: This route should come from a route to iface.
175                          */
176                         rt_setgate(rt, rt_key(rt),
177                                         (struct sockaddr *)&null_sdl);
178                         gate = rt->rt_gateway;
179                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
180                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
181                         rt->rt_expire = time_uptime;
182                         break;
183                 }
184                 /* Announce a new entry if requested. */
185                 if (rt->rt_flags & RTF_ANNOUNCE)
186                         arprequest(rt->rt_ifp,
187                             &SIN(rt_key(rt))->sin_addr,
188                             &SIN(rt_key(rt))->sin_addr,
189                             (u_char *)LLADDR(SDL(gate)));
190                 /*FALLTHROUGH*/
191         case RTM_RESOLVE:
192                 if (gate->sa_family != AF_LINK ||
193                     gate->sa_len < sizeof(null_sdl)) {
194                         log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
195                             inet_ntoa(SIN(rt_key(rt))->sin_addr),
196                             (gate->sa_family != AF_LINK) ?
197                             " (!AF_LINK)": "");
198                         break;
199                 }
200                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
201                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
202                 if (la != 0)
203                         break; /* This happens on a route change */
204                 /*
205                  * Case 2:  This route may come from cloning, or a manual route
206                  * add with a LL address.
207                  */
208                 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
209                 rt->rt_llinfo = (caddr_t)la;
210                 if (la == 0) {
211                         log(LOG_DEBUG, "%s: malloc failed\n", __func__);
212                         break;
213                 }
214                 arp_allocated++;
215                 /*
216                  * We are storing a route entry outside of radix tree. So,
217                  * it can be found and accessed by other means than radix
218                  * lookup. The routing code assumes that any rtentry detached
219                  * from radix can be destroyed safely. To prevent this, we
220                  * add an additional reference.
221                  */
222                 RT_ADDREF(rt);
223                 la->la_rt = rt;
224                 rt->rt_flags |= RTF_LLINFO;
225                 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
226                     CALLOUT_RETURNUNLOCKED);
227
228 #ifdef INET
229                 /*
230                  * This keeps the multicast addresses from showing up
231                  * in `arp -a' listings as unresolved.  It's not actually
232                  * functional.  Then the same for broadcast.
233                  */
234                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
235                     rt->rt_ifp->if_type != IFT_ARCNET) {
236                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
237                                                LLADDR(SDL(gate)));
238                         SDL(gate)->sdl_alen = 6;
239                         rt->rt_expire = 0;
240                 }
241                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
242                         memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
243                                rt->rt_ifp->if_addrlen);
244                         SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
245                         rt->rt_expire = 0;
246                 }
247 #endif
248
249                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
250                         if (ia->ia_ifp == rt->rt_ifp &&
251                             SIN(rt_key(rt))->sin_addr.s_addr ==
252                             (IA_SIN(ia))->sin_addr.s_addr)
253                                 break;
254                 }
255                 if (ia) {
256                     /*
257                      * This test used to be
258                      *  if (loif.if_flags & IFF_UP)
259                      * It allowed local traffic to be forced
260                      * through the hardware by configuring the loopback down.
261                      * However, it causes problems during network configuration
262                      * for boards that can't receive packets they send.
263                      * It is now necessary to clear "useloopback" and remove
264                      * the route to force traffic out to the hardware.
265                      */
266                         rt->rt_expire = 0;
267                         bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
268                               SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
269                         if (useloopback) {
270                                 rt->rt_ifp = loif;
271                                 rt->rt_rmx.rmx_mtu = loif->if_mtu;
272                         }
273
274                     /*
275                      * make sure to set rt->rt_ifa to the interface
276                      * address we are using, otherwise we will have trouble
277                      * with source address selection.
278                      */
279                         ifa = &ia->ia_ifa;
280                         if (ifa != rt->rt_ifa) {
281                                 IFAFREE(rt->rt_ifa);
282                                 IFAREF(ifa);
283                                 rt->rt_ifa = ifa;
284                         }
285                 }
286                 break;
287
288         case RTM_DELETE:
289                 if (la == NULL) /* XXX: at least CARP does this. */
290                         break;
291                 callout_stop(&la->la_timer);
292                 rt->rt_llinfo = NULL;
293                 rt->rt_flags &= ~RTF_LLINFO;
294                 RT_REMREF(rt);
295                 if (la->la_hold)
296                         m_freem(la->la_hold);
297                 Free((caddr_t)la);
298         }
299 }
300
301 /*
302  * Broadcast an ARP request. Caller specifies:
303  *      - arp header source ip address
304  *      - arp header target ip address
305  *      - arp header source ethernet address
306  */
307 static void
308 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
309     u_char *enaddr)
310 {
311         struct mbuf *m;
312         struct arphdr *ah;
313         struct sockaddr sa;
314
315         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
316                 return;
317         m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
318                 2*ifp->if_data.ifi_addrlen;
319         m->m_pkthdr.len = m->m_len;
320         MH_ALIGN(m, m->m_len);
321         ah = mtod(m, struct arphdr *);
322         bzero((caddr_t)ah, m->m_len);
323 #ifdef MAC
324         mac_create_mbuf_linklayer(ifp, m);
325 #endif
326         ah->ar_pro = htons(ETHERTYPE_IP);
327         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
328         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
329         ah->ar_op = htons(ARPOP_REQUEST);
330         bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
331         bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
332         bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
333         sa.sa_family = AF_ARP;
334         sa.sa_len = 2;
335         m->m_flags |= M_BCAST;
336         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
337
338         return;
339 }
340
341 /*
342  * Resolve an IP address into an ethernet address.
343  * On input:
344  *    ifp is the interface we use
345  *    dst is the next hop,
346  *    rt0 is the route to the final destination (possibly useless)
347  *    m is the mbuf
348  *    desten is where we want the address.
349  *
350  * On success, desten is filled in and the function returns 0;
351  * If the packet must be held pending resolution, we return EWOULDBLOCK
352  * On other errors, we return the corresponding error code.
353  */
354 int
355 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
356     struct sockaddr *dst, u_char *desten)
357 {
358         struct llinfo_arp *la = NULL;
359         struct rtentry *rt = NULL;
360         struct sockaddr_dl *sdl;
361         int error;
362
363         if (m->m_flags & M_BCAST) {     /* broadcast */
364                 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
365                 return (0);
366         }
367         if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
368                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
369                 return (0);
370         }
371
372         if (rt0 != NULL) {
373                 error = rt_check(&rt, &rt0, dst);
374                 if (error) {
375                         m_freem(m);
376                         return error;
377                 }
378                 la = (struct llinfo_arp *)rt->rt_llinfo;
379                 if (la == NULL)
380                         RT_UNLOCK(rt);
381         }
382         if (la == NULL) {
383                 /*
384                  * We enter this block in case if rt0 was NULL,
385                  * or if rt found by rt_check() didn't have llinfo.
386                  */
387                 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
388                 if (rt == NULL) {
389                         log(LOG_DEBUG,
390                             "arpresolve: can't allocate route for %s\n",
391                             inet_ntoa(SIN(dst)->sin_addr));
392                         m_freem(m);
393                         return (EINVAL); /* XXX */
394                 }
395                 la = (struct llinfo_arp *)rt->rt_llinfo;
396                 if (la == NULL) {
397                         RT_UNLOCK(rt);
398                         log(LOG_DEBUG,
399                             "arpresolve: can't allocate llinfo for %s\n",
400                             inet_ntoa(SIN(dst)->sin_addr));
401                         m_freem(m);
402                         return (EINVAL); /* XXX */
403                 }
404         }
405         sdl = SDL(rt->rt_gateway);
406         /*
407          * Check the address family and length is valid, the address
408          * is resolved; otherwise, try to resolve.
409          */
410         if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
411             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
412
413                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
414
415                 /*
416                  * If entry has an expiry time and it is approaching,
417                  * send an ARP request.
418                  */
419                 if ((rt->rt_expire != 0) &&
420                     (time_uptime + la->la_preempt > rt->rt_expire)) {
421                         struct in_addr sin = 
422                             SIN(rt->rt_ifa->ifa_addr)->sin_addr;
423
424                         la->la_preempt--;
425                         RT_UNLOCK(rt);
426                         arprequest(ifp, &sin, &SIN(dst)->sin_addr,
427                             IF_LLADDR(ifp));
428                         return (0);
429                 } 
430
431                 RT_UNLOCK(rt);
432                 return (0);
433         }
434         /*
435          * If ARP is disabled or static on this interface, stop.
436          * XXX
437          * Probably should not allocate empty llinfo struct if we are
438          * not going to be sending out an arp request.
439          */
440         if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
441                 RT_UNLOCK(rt);
442                 m_freem(m);
443                 return (EINVAL);
444         }
445         /*
446          * There is an arptab entry, but no ethernet address
447          * response yet.  Replace the held mbuf with this
448          * latest one.
449          */
450         if (la->la_hold)
451                 m_freem(la->la_hold);
452         la->la_hold = m;
453
454         KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
455
456         /*
457          * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
458          * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
459          * if we have already sent arp_maxtries ARP requests. Retransmit the
460          * ARP request, but not faster than one request per second.
461          */
462         if (la->la_asked < arp_maxtries)
463                 error = EWOULDBLOCK;    /* First request. */
464         else
465                 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
466
467         if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
468                 struct in_addr sin =
469                     SIN(rt->rt_ifa->ifa_addr)->sin_addr;
470
471                 rt->rt_expire = time_uptime;
472                 callout_reset(&la->la_timer, hz, arptimer, rt);
473                 la->la_asked++;
474                 RT_UNLOCK(rt);
475
476                 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
477                     IF_LLADDR(ifp));
478         } else
479                 RT_UNLOCK(rt);
480
481         return (error);
482 }
483
484 /*
485  * Common length and type checks are done here,
486  * then the protocol-specific routine is called.
487  */
488 static void
489 arpintr(struct mbuf *m)
490 {
491         struct arphdr *ar;
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                 return;
497         }
498         ar = mtod(m, struct arphdr *);
499
500         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
501             ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
502             ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
503             ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
504                 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
505                     (unsigned char *)&ar->ar_hrd, "");
506                 m_freem(m);
507                 return;
508         }
509
510         if (m->m_len < arphdr_len(ar)) {
511                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
512                         log(LOG_ERR, "arp: runt packet\n");
513                         m_freem(m);
514                         return;
515                 }
516                 ar = mtod(m, struct arphdr *);
517         }
518
519         switch (ntohs(ar->ar_pro)) {
520 #ifdef INET
521         case ETHERTYPE_IP:
522                 in_arpinput(m);
523                 return;
524 #endif
525         }
526         m_freem(m);
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 static int log_arp_movements = 1;
546 static int log_arp_permanent_modify = 1;
547
548 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
549         &log_arp_wrong_iface, 0,
550         "log arp packets arriving on the wrong interface");
551 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
552         &log_arp_movements, 0,
553         "log arp replies from MACs different than the one in the cache");
554 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
555         &log_arp_permanent_modify, 0,
556         "log arp replies from MACs different than the one in the permanent arp entry");
557
558
559 static void
560 in_arpinput(struct mbuf *m)
561 {
562         struct arphdr *ah;
563         struct ifnet *ifp = m->m_pkthdr.rcvif;
564         struct llinfo_arp *la;
565         struct rtentry *rt;
566         struct ifaddr *ifa;
567         struct in_ifaddr *ia;
568         struct sockaddr_dl *sdl;
569         struct sockaddr sa;
570         struct in_addr isaddr, itaddr, myaddr;
571         struct mbuf *hold;
572         u_int8_t *enaddr = NULL;
573         int op, rif_len;
574         int req_len;
575         int bridged = 0;
576 #ifdef DEV_CARP
577         int carp_match = 0;
578 #endif
579
580         if (ifp->if_bridge)
581                 bridged = 1;
582
583         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
584         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
585                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
586                 return;
587         }
588
589         ah = mtod(m, struct arphdr *);
590         op = ntohs(ah->ar_op);
591         (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
592         (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
593
594         /*
595          * For a bridge, we want to check the address irrespective
596          * of the receive interface. (This will change slightly
597          * when we have clusters of interfaces).
598          * If the interface does not match, but the recieving interface
599          * is part of carp, we call carp_iamatch to see if this is a
600          * request for the virtual host ip.
601          * XXX: This is really ugly!
602          */
603         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
604                 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
605                     (ia->ia_ifp == ifp)) &&
606                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
607                         goto match;
608 #ifdef DEV_CARP
609                 if (ifp->if_carp != NULL &&
610                     carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
611                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
612                         carp_match = 1;
613                         goto match;
614                 }
615 #endif
616         }
617         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
618                 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
619                     (ia->ia_ifp == ifp)) &&
620                     isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
621                         goto match;
622         /*
623          * No match, use the first inet address on the receive interface
624          * as a dummy address for the rest of the function.
625          */
626         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
627                 if (ifa->ifa_addr->sa_family == AF_INET) {
628                         ia = ifatoia(ifa);
629                         goto match;
630                 }
631         /*
632          * If bridging, fall back to using any inet address.
633          */
634         if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
635                 goto drop;
636 match:
637         if (!enaddr)
638                 enaddr = (u_int8_t *)IF_LLADDR(ifp);
639         myaddr = ia->ia_addr.sin_addr;
640         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
641                 goto drop;      /* it's from me, ignore it. */
642         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
643                 log(LOG_ERR,
644                     "arp: link address is broadcast for IP address %s!\n",
645                     inet_ntoa(isaddr));
646                 goto drop;
647         }
648         /*
649          * Warn if another host is using the same IP address, but only if the
650          * IP address isn't 0.0.0.0, which is used for DHCP only, in which
651          * case we suppress the warning to avoid false positive complaints of
652          * potential misconfiguration.
653          */
654         if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
655                 log(LOG_ERR,
656                    "arp: %*D is using my IP address %s!\n",
657                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
658                    inet_ntoa(isaddr));
659                 itaddr = myaddr;
660                 goto reply;
661         }
662         if (ifp->if_flags & IFF_STATICARP)
663                 goto reply;
664         rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
665         if (rt != NULL) {
666                 la = (struct llinfo_arp *)rt->rt_llinfo;
667                 if (la == NULL) {
668                         RT_UNLOCK(rt);
669                         goto reply;
670                 }
671         } else
672                 goto reply;
673
674         /* The following is not an error when doing bridging. */
675         if (!bridged && rt->rt_ifp != ifp
676 #ifdef DEV_CARP
677             && (ifp->if_type != IFT_CARP || !carp_match)
678 #endif
679                                                         ) {
680                 if (log_arp_wrong_iface)
681                         log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
682                             inet_ntoa(isaddr),
683                             rt->rt_ifp->if_xname,
684                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
685                             ifp->if_xname);
686                 RT_UNLOCK(rt);
687                 goto reply;
688         }
689         sdl = SDL(rt->rt_gateway);
690         if (sdl->sdl_alen &&
691             bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
692                 if (rt->rt_expire) {
693                     if (log_arp_movements)
694                         log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
695                             inet_ntoa(isaddr),
696                             ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
697                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
698                             ifp->if_xname);
699                 } else {
700                         RT_UNLOCK(rt);
701                         if (log_arp_permanent_modify)
702                                 log(LOG_ERR, "arp: %*D attempts to modify "
703                                     "permanent entry for %s on %s\n",
704                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
705                                     inet_ntoa(isaddr), ifp->if_xname);
706                         goto reply;
707                 }
708         }
709         /*
710          * sanity check for the address length.
711          * XXX this does not work for protocols with variable address
712          * length. -is
713          */
714         if (sdl->sdl_alen &&
715             sdl->sdl_alen != ah->ar_hln) {
716                 log(LOG_WARNING,
717                     "arp from %*D: new addr len %d, was %d",
718                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
719                     ah->ar_hln, sdl->sdl_alen);
720         }
721         if (ifp->if_addrlen != ah->ar_hln) {
722                 log(LOG_WARNING,
723                     "arp from %*D: addr len: new %d, i/f %d (ignored)",
724                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
725                     ah->ar_hln, ifp->if_addrlen);
726                 RT_UNLOCK(rt);
727                 goto reply;
728         }
729         (void)memcpy(LLADDR(sdl), ar_sha(ah),
730             sdl->sdl_alen = ah->ar_hln);
731         /*
732          * If we receive an arp from a token-ring station over
733          * a token-ring nic then try to save the source
734          * routing info.
735          */
736         if (ifp->if_type == IFT_ISO88025) {
737                 struct iso88025_header *th = NULL;
738                 struct iso88025_sockaddr_dl_data *trld;
739
740                 th = (struct iso88025_header *)m->m_pkthdr.header;
741                 trld = SDL_ISO88025(sdl);
742                 rif_len = TR_RCF_RIFLEN(th->rcf);
743                 if ((th->iso88025_shost[0] & TR_RII) &&
744                     (rif_len > 2)) {
745                         trld->trld_rcf = th->rcf;
746                         trld->trld_rcf ^= htons(TR_RCF_DIR);
747                         memcpy(trld->trld_route, th->rd, rif_len - 2);
748                         trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
749                         /*
750                          * Set up source routing information for
751                          * reply packet (XXX)
752                          */
753                         m->m_data -= rif_len;
754                         m->m_len  += rif_len;
755                         m->m_pkthdr.len += rif_len;
756                 } else {
757                         th->iso88025_shost[0] &= ~TR_RII;
758                         trld->trld_rcf = 0;
759                 }
760                 m->m_data -= 8;
761                 m->m_len  += 8;
762                 m->m_pkthdr.len += 8;
763                 th->rcf = trld->trld_rcf;
764         }
765         if (rt->rt_expire) {
766                 rt->rt_expire = time_uptime + arpt_keep;
767                 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
768         }
769         la->la_asked = 0;
770         la->la_preempt = arp_maxtries;
771         hold = la->la_hold;
772         la->la_hold = NULL;
773         RT_UNLOCK(rt);
774         if (hold != NULL)
775                 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
776
777 reply:
778         if (op != ARPOP_REQUEST)
779                 goto drop;
780         if (itaddr.s_addr == myaddr.s_addr) {
781                 /* I am the target */
782                 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
783                 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
784         } else {
785                 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
786                 if (rt == NULL) {
787                         struct sockaddr_in sin;
788
789                         if (!arp_proxyall)
790                                 goto drop;
791
792                         bzero(&sin, sizeof sin);
793                         sin.sin_family = AF_INET;
794                         sin.sin_len = sizeof sin;
795                         sin.sin_addr = itaddr;
796
797                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
798                         if (!rt)
799                                 goto drop;
800                         /*
801                          * Don't send proxies for nodes on the same interface
802                          * as this one came out of, or we'll get into a fight
803                          * over who claims what Ether address.
804                          */
805                         if (rt->rt_ifp == ifp) {
806                                 rtfree(rt);
807                                 goto drop;
808                         }
809                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
810                         (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
811                         rtfree(rt);
812
813                         /*
814                          * Also check that the node which sent the ARP packet
815                          * is on the the interface we expect it to be on. This
816                          * avoids ARP chaos if an interface is connected to the
817                          * wrong network.
818                          */
819                         sin.sin_addr = isaddr;
820
821                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
822                         if (!rt)
823                                 goto drop;
824                         if (rt->rt_ifp != ifp) {
825                                 log(LOG_INFO, "arp_proxy: ignoring request"
826                                     " from %s via %s, expecting %s\n",
827                                     inet_ntoa(isaddr), ifp->if_xname,
828                                     rt->rt_ifp->if_xname);
829                                 rtfree(rt);
830                                 goto drop;
831                         }
832                         rtfree(rt);
833
834 #ifdef DEBUG_PROXY
835                         printf("arp: proxying for %s\n",
836                                inet_ntoa(itaddr));
837 #endif
838                 } else {
839                         /*
840                          * Return proxied ARP replies only on the interface
841                          * or bridge cluster where this network resides.
842                          * Otherwise we may conflict with the host we are
843                          * proxying for.
844                          */
845                         if (rt->rt_ifp != ifp &&
846                             (rt->rt_ifp->if_bridge != ifp->if_bridge ||
847                             ifp->if_bridge == NULL)) {
848                                 RT_UNLOCK(rt);
849                                 goto drop;
850                         }
851                         sdl = SDL(rt->rt_gateway);
852                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
853                         (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
854                         RT_UNLOCK(rt);
855                 }
856         }
857
858         if (itaddr.s_addr == myaddr.s_addr &&
859             IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
860                 /* RFC 3927 link-local IPv4; always reply by broadcast. */
861 #ifdef DEBUG_LINKLOCAL
862                 printf("arp: sending reply for link-local addr %s\n",
863                     inet_ntoa(itaddr));
864 #endif
865                 m->m_flags |= M_BCAST;
866                 m->m_flags &= ~M_MCAST;
867         } else {
868                 /* default behaviour; never reply by broadcast. */
869                 m->m_flags &= ~(M_BCAST|M_MCAST);
870         }
871         (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
872         (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
873         ah->ar_op = htons(ARPOP_REPLY);
874         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
875         m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);   
876         m->m_pkthdr.len = m->m_len;   
877         sa.sa_family = AF_ARP;
878         sa.sa_len = 2;
879         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
880         return;
881
882 drop:
883         m_freem(m);
884 }
885 #endif
886
887 /*
888  * Lookup or enter a new address in arptab.
889  */
890 static struct rtentry *
891 arplookup(u_long addr, int create, int proxy)
892 {
893         struct rtentry *rt;
894         struct sockaddr_inarp sin;
895         const char *why = 0;
896
897         bzero(&sin, sizeof(sin));
898         sin.sin_len = sizeof(sin);
899         sin.sin_family = AF_INET;
900         sin.sin_addr.s_addr = addr;
901         if (proxy)
902                 sin.sin_other = SIN_PROXY;
903         rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
904         if (rt == 0)
905                 return (0);
906
907         if (rt->rt_flags & RTF_GATEWAY)
908                 why = "host is not on local network";
909         else if ((rt->rt_flags & RTF_LLINFO) == 0)
910                 why = "could not allocate llinfo";
911         else if (rt->rt_gateway->sa_family != AF_LINK)
912                 why = "gateway route is not ours";
913
914         if (why) {
915 #define ISDYNCLONE(_rt) \
916         (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
917                 if (create)
918                         log(LOG_DEBUG, "arplookup %s failed: %s\n",
919                             inet_ntoa(sin.sin_addr), why);
920                 /*
921                  * If there are no references to this Layer 2 route,
922                  * and it is a cloned route, and not static, and
923                  * arplookup() is creating the route, then purge
924                  * it from the routing table as it is probably bogus.
925                  */
926                 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
927                         rtexpunge(rt);
928                 RTFREE_LOCKED(rt);
929                 return (0);
930 #undef ISDYNCLONE
931         } else {
932                 RT_REMREF(rt);
933                 return (rt);
934         }
935 }
936
937 void
938 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
939 {
940         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
941                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
942                                 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
943         ifa->ifa_rtrequest = arp_rtrequest;
944         ifa->ifa_flags |= RTF_CLONING;
945 }
946
947 void
948 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
949 {
950         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
951                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
952                                 &IA_SIN(ifa)->sin_addr, enaddr);
953         ifa->ifa_rtrequest = arp_rtrequest;
954         ifa->ifa_flags |= RTF_CLONING;
955 }
956
957 static void
958 arp_init(void)
959 {
960
961         arpintrq.ifq_maxlen = 50;
962         mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
963         netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
964 }
965 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);