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