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