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