1 /*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: releng/5.3/sys/netinet/if_ether.c 136973 2004-10-26 17:28:36Z bms $
31 */
32
33 /*
34 * Ethernet address resolution protocol.
35 * TODO:
36 * add "inuse/lock" bit (or ref. count) along with valid bit
37 */
38
39 #include "opt_inet.h"
40 #include "opt_bdg.h"
41 #include "opt_mac.h"
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/mac.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
53
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/netisr.h>
59 #include <net/if_llc.h>
60 #include <net/ethernet.h>
61 #include <net/bridge.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/if_ether.h>
66
67 #include <net/if_arc.h>
68 #include <net/iso88025.h>
69
70 #define SIN(s) ((struct sockaddr_in *)s)
71 #define SDL(s) ((struct sockaddr_dl *)s)
72
73 SYSCTL_DECL(_net_link_ether);
74 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
75
76 /* timer values */
77 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
78 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
79 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
80
81 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
82 &arpt_prune, 0, "");
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
84 &arpt_keep, 0, "");
85 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
86 &arpt_down, 0, "");
87
88 #define rt_expire rt_rmx.rmx_expire
89
90 struct llinfo_arp {
91 LIST_ENTRY(llinfo_arp) la_le;
92 struct rtentry *la_rt;
93 struct mbuf *la_hold; /* last packet until resolved/timeout */
94 u_short la_preempt; /* countdown for pre-expiry arps */
95 u_short la_asked; /* #times we QUERIED following expiration */
96 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
97 };
98
99 static LIST_HEAD(, llinfo_arp) llinfo_arp;
100
101 static struct ifqueue arpintrq;
102 static int arp_allocated;
103
104 static int arp_maxtries = 5;
105 static int useloopback = 1; /* use loopback interface for local traffic */
106 static int arp_proxyall = 0;
107 static struct callout arp_callout;
108
109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
110 &arp_maxtries, 0, "");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
112 &useloopback, 0, "");
113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
114 &arp_proxyall, 0, "");
115
116 static void arp_init(void);
117 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
118 static void arprequest(struct ifnet *,
119 struct in_addr *, struct in_addr *, u_char *);
120 static void arpintr(struct mbuf *);
121 static void arptfree(struct llinfo_arp *);
122 static void arptimer(void *);
123 static struct llinfo_arp
124 *arplookup(u_long, int, int);
125 #ifdef INET
126 static void in_arpinput(struct mbuf *);
127 #endif
128
129 /*
130 * Timeout routine. Age arp_tab entries periodically.
131 */
132 /* ARGSUSED */
133 static void
134 arptimer(ignored_arg)
135 void *ignored_arg;
136 {
137 struct llinfo_arp *la, *ola;
138
139 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
140 la = LIST_FIRST(&llinfo_arp);
141 while (la != NULL) {
142 struct rtentry *rt = la->la_rt;
143 ola = la;
144 la = LIST_NEXT(la, la_le);
145 if (rt->rt_expire && rt->rt_expire <= time_second)
146 arptfree(ola); /* timer has expired, clear */
147 }
148 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
149
150 callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
151 }
152
153 /*
154 * Parallel to llc_rtrequest.
155 */
156 static void
157 arp_rtrequest(req, rt, info)
158 int req;
159 struct rtentry *rt;
160 struct rt_addrinfo *info;
161 {
162 struct sockaddr *gate;
163 struct llinfo_arp *la;
164 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
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_second;
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 arp_allocated++;
227 la->la_rt = rt;
228 rt->rt_flags |= RTF_LLINFO;
229 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
230 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
231
232 #ifdef INET
233 /*
234 * This keeps the multicast addresses from showing up
235 * in `arp -a' listings as unresolved. It's not actually
236 * functional. Then the same for broadcast.
237 */
238 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
239 rt->rt_ifp->if_type != IFT_ARCNET) {
240 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
241 LLADDR(SDL(gate)));
242 SDL(gate)->sdl_alen = 6;
243 rt->rt_expire = 0;
244 }
245 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
246 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
247 rt->rt_ifp->if_addrlen);
248 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
249 rt->rt_expire = 0;
250 }
251 #endif
252
253 if (SIN(rt_key(rt))->sin_addr.s_addr ==
254 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
255 /*
256 * This test used to be
257 * if (loif.if_flags & IFF_UP)
258 * It allowed local traffic to be forced
259 * through the hardware by configuring the loopback down.
260 * However, it causes problems during network configuration
261 * for boards that can't receive packets they send.
262 * It is now necessary to clear "useloopback" and remove
263 * the route to force traffic out to the hardware.
264 */
265 rt->rt_expire = 0;
266 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
267 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
268 if (useloopback)
269 rt->rt_ifp = loif;
270
271 }
272 break;
273
274 case RTM_DELETE:
275 if (la == 0)
276 break;
277 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
278 LIST_REMOVE(la, la_le);
279 rt->rt_llinfo = 0;
280 rt->rt_flags &= ~RTF_LLINFO;
281 if (la->la_hold)
282 m_freem(la->la_hold);
283 Free((caddr_t)la);
284 }
285 }
286
287 /*
288 * Broadcast an ARP request. Caller specifies:
289 * - arp header source ip address
290 * - arp header target ip address
291 * - arp header source ethernet address
292 */
293 static void
294 arprequest(ifp, sip, tip, enaddr)
295 struct ifnet *ifp;
296 struct in_addr *sip, *tip;
297 u_char *enaddr;
298 {
299 struct mbuf *m;
300 struct arphdr *ah;
301 struct sockaddr sa;
302
303 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
304 return;
305 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
306 2*ifp->if_data.ifi_addrlen;
307 m->m_pkthdr.len = m->m_len;
308 MH_ALIGN(m, m->m_len);
309 ah = mtod(m, struct arphdr *);
310 bzero((caddr_t)ah, m->m_len);
311 #ifdef MAC
312 mac_create_mbuf_linklayer(ifp, m);
313 #endif
314 ah->ar_pro = htons(ETHERTYPE_IP);
315 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
316 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
317 ah->ar_op = htons(ARPOP_REQUEST);
318 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
319 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
320 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
321 sa.sa_family = AF_ARP;
322 sa.sa_len = 2;
323 m->m_flags |= M_BCAST;
324 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
325
326 return;
327 }
328
329 /*
330 * Resolve an IP address into an ethernet address.
331 * On input:
332 * ifp is the interface we use
333 * dst is the next hop,
334 * rt0 is the route to the final destination (possibly useless)
335 * m is the mbuf
336 * desten is where we want the address.
337 *
338 * On success, desten is filled in and the function returns 0;
339 * If the packet must be held pending resolution, we return EWOULDBLOCK
340 * On other errors, we return the corresponding error code.
341 */
342 int
343 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
344 struct sockaddr *dst, u_char *desten)
345 {
346 struct llinfo_arp *la = 0;
347 struct sockaddr_dl *sdl;
348 int error;
349 struct rtentry *rt;
350
351 error = rt_check(&rt, &rt0, dst);
352 if (error) {
353 m_freem(m);
354 return error;
355 }
356
357 if (m->m_flags & M_BCAST) { /* broadcast */
358 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
359 return (0);
360 }
361 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
362 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
363 return (0);
364 }
365 if (rt)
366 la = (struct llinfo_arp *)rt->rt_llinfo;
367 if (la == 0) {
368 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
369 if (la)
370 rt = la->la_rt;
371 }
372 if (la == 0 || rt == 0) {
373 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
374 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
375 rt ? "rt" : "");
376 m_freem(m);
377 return (EINVAL); /* XXX */
378 }
379 sdl = SDL(rt->rt_gateway);
380 /*
381 * Check the address family and length is valid, the address
382 * is resolved; otherwise, try to resolve.
383 */
384 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
385 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
386 /*
387 * If entry has an expiry time and it is approaching,
388 * see if we need to send an ARP request within this
389 * arpt_down interval.
390 */
391 if ((rt->rt_expire != 0) &&
392 (time_second + la->la_preempt > rt->rt_expire)) {
393 arprequest(ifp,
394 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
395 &SIN(dst)->sin_addr,
396 IF_LLADDR(ifp));
397 la->la_preempt--;
398 }
399
400 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
401 return (0);
402 }
403 /*
404 * If ARP is disabled or static on this interface, stop.
405 * XXX
406 * Probably should not allocate empty llinfo struct if we are
407 * not going to be sending out an arp request.
408 */
409 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
410 m_freem(m);
411 return (EINVAL);
412 }
413 /*
414 * There is an arptab entry, but no ethernet address
415 * response yet. Replace the held mbuf with this
416 * latest one.
417 */
418 if (la->la_hold)
419 m_freem(la->la_hold);
420 la->la_hold = m;
421 if (rt->rt_expire) {
422 RT_LOCK(rt);
423 rt->rt_flags &= ~RTF_REJECT;
424 if (la->la_asked == 0 || rt->rt_expire != time_second) {
425 rt->rt_expire = time_second;
426 if (la->la_asked++ < arp_maxtries) {
427 arprequest(ifp,
428 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
429 &SIN(dst)->sin_addr,
430 IF_LLADDR(ifp));
431 } else {
432 rt->rt_flags |= RTF_REJECT;
433 rt->rt_expire += arpt_down;
434 la->la_asked = 0;
435 la->la_preempt = arp_maxtries;
436 }
437
438 }
439 RT_UNLOCK(rt);
440 }
441 return (EWOULDBLOCK);
442 }
443
444 /*
445 * Common length and type checks are done here,
446 * then the protocol-specific routine is called.
447 */
448 static void
449 arpintr(struct mbuf *m)
450 {
451 struct arphdr *ar;
452
453 if (m->m_len < sizeof(struct arphdr) &&
454 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
455 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
456 return;
457 }
458 ar = mtod(m, struct arphdr *);
459
460 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
461 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
462 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
463 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
464 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
465 (unsigned char *)&ar->ar_hrd, "");
466 m_freem(m);
467 return;
468 }
469
470 if (m->m_len < arphdr_len(ar)) {
471 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
472 log(LOG_ERR, "arp: runt packet\n");
473 m_freem(m);
474 return;
475 }
476 ar = mtod(m, struct arphdr *);
477 }
478
479 switch (ntohs(ar->ar_pro)) {
480 #ifdef INET
481 case ETHERTYPE_IP:
482 in_arpinput(m);
483 return;
484 #endif
485 }
486 m_freem(m);
487 }
488
489 #ifdef INET
490 /*
491 * ARP for Internet protocols on 10 Mb/s Ethernet.
492 * Algorithm is that given in RFC 826.
493 * In addition, a sanity check is performed on the sender
494 * protocol address, to catch impersonators.
495 * We no longer handle negotiations for use of trailer protocol:
496 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
497 * along with IP replies if we wanted trailers sent to us,
498 * and also sent them in response to IP replies.
499 * This allowed either end to announce the desire to receive
500 * trailer packets.
501 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
502 * but formerly didn't normally send requests.
503 */
504 static int log_arp_wrong_iface = 1;
505 static int log_arp_movements = 1;
506
507 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
508 &log_arp_wrong_iface, 0,
509 "log arp packets arriving on the wrong interface");
510 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
511 &log_arp_movements, 0,
512 "log arp replies from MACs different than the one in the cache");
513
514
515 static void
516 in_arpinput(m)
517 struct mbuf *m;
518 {
519 struct arphdr *ah;
520 struct ifnet *ifp = m->m_pkthdr.rcvif;
521 struct iso88025_header *th = (struct iso88025_header *)0;
522 struct iso88025_sockaddr_dl_data *trld;
523 struct llinfo_arp *la = 0;
524 struct rtentry *rt;
525 struct ifaddr *ifa;
526 struct in_ifaddr *ia;
527 struct sockaddr_dl *sdl;
528 struct sockaddr sa;
529 struct in_addr isaddr, itaddr, myaddr;
530 int op, rif_len;
531 int req_len;
532
533 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
534 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
535 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
536 return;
537 }
538
539 ah = mtod(m, struct arphdr *);
540 op = ntohs(ah->ar_op);
541 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
542 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
543
544 /*
545 * For a bridge, we want to check the address irrespective
546 * of the receive interface. (This will change slightly
547 * when we have clusters of interfaces).
548 */
549 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
550 if ((do_bridge || (ia->ia_ifp == ifp)) &&
551 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
552 goto match;
553 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
554 if ((do_bridge || (ia->ia_ifp == ifp)) &&
555 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
556 goto match;
557 /*
558 * No match, use the first inet address on the receive interface
559 * as a dummy address for the rest of the function.
560 */
561 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
562 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
563 ia = ifatoia(ifa);
564 goto match;
565 }
566 /*
567 * If bridging, fall back to using any inet address.
568 */
569 if (!do_bridge || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
570 goto drop;
571 match:
572 myaddr = ia->ia_addr.sin_addr;
573 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen))
574 goto drop; /* it's from me, ignore it. */
575 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
576 log(LOG_ERR,
577 "arp: link address is broadcast for IP address %s!\n",
578 inet_ntoa(isaddr));
579 goto drop;
580 }
581 if (isaddr.s_addr == myaddr.s_addr) {
582 log(LOG_ERR,
583 "arp: %*D is using my IP address %s!\n",
584 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
585 inet_ntoa(isaddr));
586 itaddr = myaddr;
587 goto reply;
588 }
589 if (ifp->if_flags & IFF_STATICARP)
590 goto reply;
591 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
592 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
593 /* the following is not an error when doing bridging */
594 if (!do_bridge && rt->rt_ifp != ifp) {
595 if (log_arp_wrong_iface)
596 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
597 inet_ntoa(isaddr),
598 rt->rt_ifp->if_xname,
599 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
600 ifp->if_xname);
601 goto reply;
602 }
603 if (sdl->sdl_alen &&
604 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
605 if (rt->rt_expire) {
606 if (log_arp_movements)
607 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
608 inet_ntoa(isaddr),
609 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
610 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
611 ifp->if_xname);
612 } else {
613 log(LOG_ERR,
614 "arp: %*D attempts to modify permanent entry for %s on %s\n",
615 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
616 inet_ntoa(isaddr), ifp->if_xname);
617 goto reply;
618 }
619 }
620 /*
621 * sanity check for the address length.
622 * XXX this does not work for protocols with variable address
623 * length. -is
624 */
625 if (sdl->sdl_alen &&
626 sdl->sdl_alen != ah->ar_hln) {
627 log(LOG_WARNING,
628 "arp from %*D: new addr len %d, was %d",
629 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
630 ah->ar_hln, sdl->sdl_alen);
631 }
632 if (ifp->if_addrlen != ah->ar_hln) {
633 log(LOG_WARNING,
634 "arp from %*D: addr len: new %d, i/f %d (ignored)",
635 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
636 ah->ar_hln, ifp->if_addrlen);
637 goto reply;
638 }
639 (void)memcpy(LLADDR(sdl), ar_sha(ah),
640 sdl->sdl_alen = ah->ar_hln);
641 /*
642 * If we receive an arp from a token-ring station over
643 * a token-ring nic then try to save the source
644 * routing info.
645 */
646 if (ifp->if_type == IFT_ISO88025) {
647 th = (struct iso88025_header *)m->m_pkthdr.header;
648 trld = SDL_ISO88025(sdl);
649 rif_len = TR_RCF_RIFLEN(th->rcf);
650 if ((th->iso88025_shost[0] & TR_RII) &&
651 (rif_len > 2)) {
652 trld->trld_rcf = th->rcf;
653 trld->trld_rcf ^= htons(TR_RCF_DIR);
654 memcpy(trld->trld_route, th->rd, rif_len - 2);
655 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
656 /*
657 * Set up source routing information for
658 * reply packet (XXX)
659 */
660 m->m_data -= rif_len;
661 m->m_len += rif_len;
662 m->m_pkthdr.len += rif_len;
663 } else {
664 th->iso88025_shost[0] &= ~TR_RII;
665 trld->trld_rcf = 0;
666 }
667 m->m_data -= 8;
668 m->m_len += 8;
669 m->m_pkthdr.len += 8;
670 th->rcf = trld->trld_rcf;
671 }
672 RT_LOCK(rt);
673 if (rt->rt_expire)
674 rt->rt_expire = time_second + arpt_keep;
675 rt->rt_flags &= ~RTF_REJECT;
676 RT_UNLOCK(rt);
677 la->la_asked = 0;
678 la->la_preempt = arp_maxtries;
679 if (la->la_hold) {
680 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
681 la->la_hold = 0;
682 }
683 }
684 reply:
685 if (op != ARPOP_REQUEST)
686 goto drop;
687 if (itaddr.s_addr == myaddr.s_addr) {
688 /* I am the target */
689 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
690 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
691 } else {
692 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
693 if (la == NULL) {
694 struct sockaddr_in sin;
695
696 if (!arp_proxyall)
697 goto drop;
698
699 bzero(&sin, sizeof sin);
700 sin.sin_family = AF_INET;
701 sin.sin_len = sizeof sin;
702 sin.sin_addr = itaddr;
703
704 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
705 if (!rt)
706 goto drop;
707 /*
708 * Don't send proxies for nodes on the same interface
709 * as this one came out of, or we'll get into a fight
710 * over who claims what Ether address.
711 */
712 if (rt->rt_ifp == ifp) {
713 rtfree(rt);
714 goto drop;
715 }
716 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
717 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
718 rtfree(rt);
719
720 /*
721 * Also check that the node which sent the ARP packet
722 * is on the the interface we expect it to be on. This
723 * avoids ARP chaos if an interface is connected to the
724 * wrong network.
725 */
726 sin.sin_addr = isaddr;
727
728 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
729 if (!rt)
730 goto drop;
731 if (rt->rt_ifp != ifp) {
732 log(LOG_INFO, "arp_proxy: ignoring request"
733 " from %s via %s, expecting %s\n",
734 inet_ntoa(isaddr), ifp->if_xname,
735 rt->rt_ifp->if_xname);
736 rtfree(rt);
737 goto drop;
738 }
739 rtfree(rt);
740
741 #ifdef DEBUG_PROXY
742 printf("arp: proxying for %s\n",
743 inet_ntoa(itaddr));
744 #endif
745 } else {
746 rt = la->la_rt;
747 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
748 sdl = SDL(rt->rt_gateway);
749 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
750 }
751 }
752
753 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
754 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
755 ah->ar_op = htons(ARPOP_REPLY);
756 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
757 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
758 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
759 m->m_pkthdr.len = m->m_len;
760 sa.sa_family = AF_ARP;
761 sa.sa_len = 2;
762 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
763 return;
764
765 drop:
766 m_freem(m);
767 }
768 #endif
769
770 /*
771 * Free an arp entry.
772 */
773 static void
774 arptfree(la)
775 struct llinfo_arp *la;
776 {
777 struct rtentry *rt = la->la_rt;
778 struct sockaddr_dl *sdl;
779
780 if (rt == 0)
781 panic("arptfree");
782 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
783 sdl->sdl_family == AF_LINK) {
784 sdl->sdl_alen = 0;
785 la->la_preempt = la->la_asked = 0;
786 RT_LOCK(rt); /* XXX needed or move higher? */
787 rt->rt_flags &= ~RTF_REJECT;
788 RT_UNLOCK(rt);
789 return;
790 }
791 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
792 0, (struct rtentry **)0);
793 }
794 /*
795 * Lookup or enter a new address in arptab.
796 */
797 static struct llinfo_arp *
798 arplookup(addr, create, proxy)
799 u_long addr;
800 int create, proxy;
801 {
802 struct rtentry *rt;
803 struct sockaddr_inarp sin;
804 const char *why = 0;
805
806 bzero(&sin, sizeof(sin));
807 sin.sin_len = sizeof(sin);
808 sin.sin_family = AF_INET;
809 sin.sin_addr.s_addr = addr;
810 if (proxy)
811 sin.sin_other = SIN_PROXY;
812 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
813 if (rt == 0)
814 return (0);
815
816 if (rt->rt_flags & RTF_GATEWAY)
817 why = "host is not on local network";
818 else if ((rt->rt_flags & RTF_LLINFO) == 0)
819 why = "could not allocate llinfo";
820 else if (rt->rt_gateway->sa_family != AF_LINK)
821 why = "gateway route is not ours";
822
823 if (why) {
824 #define ISDYNCLONE(_rt) \
825 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
826 if (create)
827 log(LOG_DEBUG, "arplookup %s failed: %s\n",
828 inet_ntoa(sin.sin_addr), why);
829 /*
830 * If there are no references to this Layer 2 route,
831 * and it is a cloned route, and not static, and
832 * arplookup() is creating the route, then purge
833 * it from the routing table as it is probably bogus.
834 */
835 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
836 rtexpunge(rt);
837 RTFREE_LOCKED(rt);
838 return (0);
839 #undef ISDYNCLONE
840 } else {
841 RT_REMREF(rt);
842 RT_UNLOCK(rt);
843 return ((struct llinfo_arp *)rt->rt_llinfo);
844 }
845 }
846
847 void
848 arp_ifinit(ifp, ifa)
849 struct ifnet *ifp;
850 struct ifaddr *ifa;
851 {
852 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
853 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
854 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
855 ifa->ifa_rtrequest = arp_rtrequest;
856 ifa->ifa_flags |= RTF_CLONING;
857 }
858
859 static void
860 arp_init(void)
861 {
862
863 arpintrq.ifq_maxlen = 50;
864 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
865 LIST_INIT(&llinfo_arp);
866 callout_init(&arp_callout, CALLOUT_MPSAFE);
867 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
868 callout_reset(&arp_callout, hz, arptimer, NULL);
869 }
870 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
Cache object: eebf67f0fed3cdbccdec753d0ad0c558
|