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