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