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