Do an even better job discerning between blocked threads and blocked
[dragonfly.git] / sys / netinet / if_ether.c
CommitLineData
f3ed2586
JH
1/*
2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
984263bc
MD
33/*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
4599cf19 67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.37 2007/05/23 08:57:09 dillon Exp $
984263bc
MD
68 */
69
70/*
71 * Ethernet address resolution protocol.
72 * TODO:
73 * add "inuse/lock" bit (or ref. count) along with valid bit
74 */
75
76#include "opt_inet.h"
984263bc
MD
77
78#include <sys/param.h>
79#include <sys/kernel.h>
80#include <sys/queue.h>
81#include <sys/sysctl.h>
82#include <sys/systm.h>
83#include <sys/mbuf.h>
84#include <sys/malloc.h>
85#include <sys/socket.h>
86#include <sys/syslog.h>
87
88#include <net/if.h>
89#include <net/if_dl.h>
90#include <net/if_types.h>
91#include <net/route.h>
92#include <net/netisr.h>
93#include <net/if_llc.h>
984263bc
MD
94
95#include <netinet/in.h>
96#include <netinet/in_var.h>
97#include <netinet/if_ether.h>
98
99#include <net/if_arc.h>
100#include <net/iso88025.h>
101
4599cf19
MD
102#include <sys/thread2.h>
103#include <sys/msgport2.h>
104#include <net/netmsg2.h>
105
106
984263bc
MD
107#define SIN(s) ((struct sockaddr_in *)s)
108#define SDL(s) ((struct sockaddr_dl *)s)
109
110SYSCTL_DECL(_net_link_ether);
111SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
112
113/* timer values */
114static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
115static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
116static int arpt_down = 20; /* once declared down, don't send for 20 sec */
117
118SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
119 &arpt_prune, 0, "");
f23061d4 120SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
984263bc
MD
121 &arpt_keep, 0, "");
122SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
123 &arpt_down, 0, "");
124
f23061d4 125#define rt_expire rt_rmx.rmx_expire
984263bc
MD
126
127struct llinfo_arp {
128 LIST_ENTRY(llinfo_arp) la_le;
129 struct rtentry *la_rt;
130 struct mbuf *la_hold; /* last packet until resolved/timeout */
a916e847 131 u_short la_preempt; /* countdown for pre-expiry arps */
984263bc 132 u_short la_asked; /* #times we QUERIED following expiration */
984263bc
MD
133};
134
ecdefdda 135static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
984263bc 136
984263bc
MD
137static int arp_maxtries = 5;
138static int useloopback = 1; /* use loopback interface for local traffic */
139static int arp_proxyall = 0;
140
141SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
142 &arp_maxtries, 0, "");
143SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
144 &useloopback, 0, "");
145SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
146 &arp_proxyall, 0, "");
147
42a7fc75
RG
148static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
149static void arprequest (struct ifnet *,
150 struct in_addr *, struct in_addr *, u_char *);
4599cf19 151static void arpintr(struct netmsg *);
42a7fc75
RG
152static void arptfree (struct llinfo_arp *);
153static void arptimer (void *);
984263bc 154static struct llinfo_arp
f23061d4 155 *arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
984263bc 156#ifdef INET
42a7fc75 157static void in_arpinput (struct mbuf *);
984263bc
MD
158#endif
159
ecdefdda 160static struct callout arptimer_ch[MAXCPU];
ac311540 161
984263bc
MD
162/*
163 * Timeout routine. Age arp_tab entries periodically.
164 */
165/* ARGSUSED */
166static void
f23061d4 167arptimer(void *ignored_arg)
984263bc 168{
2e9572df 169 struct llinfo_arp *la, *nla;
984263bc 170
1cae611f 171 crit_enter();
ecdefdda 172 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
2e9572df 173 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
ecdefdda 174 arptfree(la);
984263bc 175 }
ecdefdda 176 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
1cae611f 177 crit_exit();
984263bc
MD
178}
179
180/*
181 * Parallel to llc_rtrequest.
182 */
183static void
f23061d4 184arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 185{
2256ba69 186 struct sockaddr *gate = rt->rt_gateway;
f23061d4
JH
187 struct llinfo_arp *la = rt->rt_llinfo;
188
5fe66e68 189 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
ecdefdda 190 static boolean_t arpinit_done[MAXCPU];
984263bc 191
ecdefdda
MD
192 if (!arpinit_done[mycpuid]) {
193 arpinit_done[mycpuid] = TRUE;
194 callout_init(&arptimer_ch[mycpuid]);
195 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
984263bc
MD
196 }
197 if (rt->rt_flags & RTF_GATEWAY)
198 return;
984263bc 199
f23061d4 200 switch (req) {
984263bc
MD
201 case RTM_ADD:
202 /*
203 * XXX: If this is a manually added route to interface
204 * such as older version of routed or gated might provide,
205 * restore cloning bit.
206 */
f23061d4 207 if (!(rt->rt_flags & RTF_HOST) &&
984263bc
MD
208 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
209 rt->rt_flags |= RTF_CLONING;
210 if (rt->rt_flags & RTF_CLONING) {
211 /*
212 * Case 1: This route should come from a route to iface.
213 */
214 rt_setgate(rt, rt_key(rt),
0c3c561c 215 (struct sockaddr *)&null_sdl);
984263bc
MD
216 gate = rt->rt_gateway;
217 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
218 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
219 rt->rt_expire = time_second;
220 break;
221 }
222 /* Announce a new entry if requested. */
223 if (rt->rt_flags & RTF_ANNOUNCE)
224 arprequest(rt->rt_ifp,
225 &SIN(rt_key(rt))->sin_addr,
226 &SIN(rt_key(rt))->sin_addr,
f23061d4 227 LLADDR(SDL(gate)));
984263bc
MD
228 /*FALLTHROUGH*/
229 case RTM_RESOLVE:
230 if (gate->sa_family != AF_LINK ||
f23061d4 231 gate->sa_len < sizeof(struct sockaddr_dl)) {
984263bc
MD
232 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
233 break;
234 }
235 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
236 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
f23061d4 237 if (la != NULL)
984263bc
MD
238 break; /* This happens on a route change */
239 /*
240 * Case 2: This route may come from cloning, or a manual route
241 * add with a LL address.
242 */
0c3c561c 243 R_Malloc(la, struct llinfo_arp *, sizeof *la);
f23061d4
JH
244 rt->rt_llinfo = la;
245 if (la == NULL) {
984263bc
MD
246 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
247 break;
248 }
f23061d4 249 bzero(la, sizeof *la);
984263bc
MD
250 la->la_rt = rt;
251 rt->rt_flags |= RTF_LLINFO;
ecdefdda 252 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
984263bc
MD
253
254#ifdef INET
255 /*
256 * This keeps the multicast addresses from showing up
257 * in `arp -a' listings as unresolved. It's not actually
258 * functional. Then the same for broadcast.
259 */
f23061d4
JH
260 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
261 rt->rt_ifp->if_type != IFT_ARCNET) {
984263bc
MD
262 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
263 LLADDR(SDL(gate)));
264 SDL(gate)->sdl_alen = 6;
265 rt->rt_expire = 0;
266 }
267 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
c401f0fd 268 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
984263bc
MD
269 rt->rt_ifp->if_addrlen);
270 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
271 rt->rt_expire = 0;
272 }
273#endif
274
275 if (SIN(rt_key(rt))->sin_addr.s_addr ==
276 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
f23061d4
JH
277 /*
278 * This test used to be
279 * if (loif.if_flags & IFF_UP)
280 * It allowed local traffic to be forced
281 * through the hardware by configuring the
282 * loopback down. However, it causes problems
283 * during network configuration for boards
284 * that can't receive packets they send. It
285 * is now necessary to clear "useloopback" and
286 * remove the route to force traffic out to
287 * the hardware.
288 */
984263bc 289 rt->rt_expire = 0;
2e9572df 290 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
984263bc
MD
291 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
292 if (useloopback)
293 rt->rt_ifp = loif;
984263bc
MD
294 }
295 break;
296
297 case RTM_DELETE:
f23061d4 298 if (la == NULL)
984263bc 299 break;
984263bc 300 LIST_REMOVE(la, la_le);
f23061d4 301 rt->rt_llinfo = NULL;
984263bc 302 rt->rt_flags &= ~RTF_LLINFO;
f23061d4 303 if (la->la_hold != NULL)
984263bc 304 m_freem(la->la_hold);
f23061d4 305 Free(la);
984263bc
MD
306 }
307}
308
309/*
310 * Broadcast an ARP request. Caller specifies:
311 * - arp header source ip address
312 * - arp header target ip address
313 * - arp header source ethernet address
314 */
315static void
f23061d4
JH
316arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
317 u_char *enaddr)
984263bc 318{
2256ba69
RG
319 struct mbuf *m;
320 struct ether_header *eh;
321 struct arc_header *arh;
322 struct arphdr *ah;
984263bc 323 struct sockaddr sa;
f23061d4
JH
324 static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
325 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
984263bc
MD
326 u_short ar_hrd;
327
74f1caca 328 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
984263bc 329 return;
f23061d4
JH
330 m->m_pkthdr.rcvif = (struct ifnet *)NULL;
331
984263bc
MD
332 switch (ifp->if_type) {
333 case IFT_ARCNET:
334 ar_hrd = htons(ARPHRD_ARCNET);
335
336 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
337 m->m_pkthdr.len = m->m_len;
338 MH_ALIGN(m, m->m_len);
339
340 arh = (struct arc_header *)sa.sa_data;
c401f0fd 341 arh->arc_dhost = ifp->if_broadcastaddr[0];
984263bc
MD
342 arh->arc_type = ARCTYPE_ARP;
343
344 ah = mtod(m, struct arphdr *);
345 break;
346
347 case IFT_ISO88025:
348 ar_hrd = htons(ARPHRD_IEEE802);
349
0c3c561c 350 m->m_len = (sizeof llcx) +
984263bc
MD
351 arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
352 m->m_pkthdr.len = m->m_len;
353 MH_ALIGN(m, m->m_len);
354
0c3c561c 355 memcpy(mtod(m, caddr_t), llcx, sizeof llcx);
c401f0fd 356 memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
f23061d4 357 memcpy(sa.sa_data + 6, enaddr, 6);
984263bc
MD
358 sa.sa_data[6] |= TR_RII;
359 sa.sa_data[12] = TR_AC;
360 sa.sa_data[13] = TR_LLC_FRAME;
361
0c3c561c 362 ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx);
984263bc
MD
363 break;
364 case IFT_FDDI:
365 case IFT_ETHER:
366 /*
367 * This may not be correct for types not explicitly
368 * listed, but this is our best guess
369 */
370 default:
371 ar_hrd = htons(ARPHRD_ETHER);
372
373 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
374 m->m_pkthdr.len = m->m_len;
375 MH_ALIGN(m, m->m_len);
376
377 eh = (struct ether_header *)sa.sa_data;
f23061d4 378 /* if_output() will not swap */
984263bc 379 eh->ether_type = htons(ETHERTYPE_ARP);
c401f0fd 380 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
381
382 ah = mtod(m, struct arphdr *);
383 break;
384 }
385
386 ah->ar_hrd = ar_hrd;
387 ah->ar_pro = htons(ETHERTYPE_IP);
388 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
389 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
390 ah->ar_op = htons(ARPOP_REQUEST);
f23061d4 391 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 392 memset(ar_tha(ah), 0, ah->ar_hln);
f23061d4
JH
393 memcpy(ar_spa(ah), sip, ah->ar_pln);
394 memcpy(ar_tpa(ah), tip, ah->ar_pln);
984263bc
MD
395
396 sa.sa_family = AF_UNSPEC;
0c3c561c 397 sa.sa_len = sizeof sa;
f23061d4 398 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
984263bc
MD
399}
400
401/*
402 * Resolve an IP address into an ethernet address. If success,
403 * desten is filled in. If there is no entry in arptab,
404 * set one up and broadcast a request for the IP address.
405 * Hold onto this mbuf and resend it once the address
406 * is finally resolved. A return value of 1 indicates
407 * that desten has been filled in and the packet should be sent
408 * normally; a 0 return indicates that the packet has been
409 * taken over here, either now or for later transmission.
410 */
411int
f23061d4
JH
412arpresolve(
413 struct ifnet *ifp,
414 struct rtentry *rt0,
415 struct mbuf *m,
416 struct sockaddr *dst,
417 u_char *desten)
984263bc 418{
f23061d4
JH
419 struct rtentry *rt;
420 struct llinfo_arp *la = NULL;
984263bc
MD
421 struct sockaddr_dl *sdl;
422
423 if (m->m_flags & M_BCAST) { /* broadcast */
c401f0fd 424 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
425 return (1);
426 }
427 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
428 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
f23061d4 429 return (1);
984263bc 430 }
f23061d4
JH
431 if (rt0 != NULL) {
432 if (rt_llroute(dst, rt0, &rt) != 0) {
433 m_freem(m);
434 return 0;
435 }
436 la = rt->rt_llinfo;
437 }
438 if (la == NULL) {
439 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
440 if (la != NULL)
984263bc
MD
441 rt = la->la_rt;
442 }
f23061d4 443 if (la == NULL || rt == NULL) {
984263bc 444 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
5fe66e68
JH
445 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
446 rt ? "rt" : "");
984263bc
MD
447 m_freem(m);
448 return (0);
449 }
450 sdl = SDL(rt->rt_gateway);
451 /*
452 * Check the address family and length is valid, the address
453 * is resolved; otherwise, try to resolve.
454 */
455 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
456 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
457 /*
458 * If entry has an expiry time and it is approaching,
459 * see if we need to send an ARP request within this
460 * arpt_down interval.
461 */
462 if ((rt->rt_expire != 0) &&
a916e847 463 (time_second + la->la_preempt > rt->rt_expire)) {
984263bc
MD
464 arprequest(ifp,
465 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
466 &SIN(dst)->sin_addr,
467 IF_LLADDR(ifp));
a916e847 468 la->la_preempt--;
f23061d4 469 }
984263bc
MD
470
471 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
472 return 1;
473 }
474 /*
475 * If ARP is disabled on this interface, stop.
476 * XXX
477 * Probably should not allocate empty llinfo struct if we are
478 * not going to be sending out an arp request.
479 */
480 if (ifp->if_flags & IFF_NOARP) {
481 m_freem(m);
482 return (0);
483 }
484 /*
485 * There is an arptab entry, but no ethernet address
486 * response yet. Replace the held mbuf with this
487 * latest one.
488 */
f23061d4 489 if (la->la_hold != NULL)
984263bc
MD
490 m_freem(la->la_hold);
491 la->la_hold = m;
03f869ef 492 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
984263bc
MD
493 rt->rt_flags &= ~RTF_REJECT;
494 if (la->la_asked == 0 || rt->rt_expire != time_second) {
495 rt->rt_expire = time_second;
496 if (la->la_asked++ < arp_maxtries) {
497 arprequest(ifp,
498 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
499 &SIN(dst)->sin_addr,
500 IF_LLADDR(ifp));
501 } else {
502 rt->rt_flags |= RTF_REJECT;
503 rt->rt_expire += arpt_down;
a916e847
JH
504 la->la_asked = 0;
505 la->la_preempt = arp_maxtries;
984263bc
MD
506 }
507
508 }
509 }
510 return (0);
511}
512
513/*
514 * Common length and type checks are done here,
515 * then the protocol-specific routine is called.
516 */
4599cf19 517static void
9eeaa8a9 518arpintr(struct netmsg *msg)
984263bc 519{
9eeaa8a9 520 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
2256ba69 521 struct arphdr *ar;
8bde602d 522 u_short ar_hrd;
984263bc 523
8bde602d
JH
524 if (m->m_len < sizeof(struct arphdr) &&
525 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
526 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
b76bed62 527 goto out2;
8bde602d
JH
528 }
529 ar = mtod(m, struct arphdr *);
530
531 ar_hrd = ntohs(ar->ar_hrd);
532 if (ar_hrd != ARPHRD_ETHER &&
533 ar_hrd != ARPHRD_IEEE802 &&
534 ar_hrd != ARPHRD_ARCNET) {
535 log(LOG_ERR,
536 "arp: unknown hardware address format (0x%2D)\n",
537 (unsigned char *)&ar->ar_hrd, "");
b76bed62 538 goto out1;
8bde602d 539 }
984263bc 540
8bde602d
JH
541 if (m->m_pkthdr.len < arphdr_len(ar) &&
542 (m = m_pullup(m, arphdr_len(ar))) == NULL) {
543 log(LOG_ERR, "arp: runt packet\n");
b76bed62 544 goto out1;
8bde602d
JH
545 }
546
547 switch (ntohs(ar->ar_pro)) {
984263bc 548#ifdef INET
8bde602d
JH
549 case ETHERTYPE_IP:
550 in_arpinput(m);
b76bed62 551 goto out2;
984263bc 552#endif
984263bc 553 }
b76bed62 554out1:
8bde602d 555 m_freem(m);
b76bed62 556out2:
4599cf19 557 ;
6aad077d 558 /* msg was embedded in the mbuf, do not reply! */
984263bc
MD
559}
560
561#ifdef INET
562/*
563 * ARP for Internet protocols on 10 Mb/s Ethernet.
564 * Algorithm is that given in RFC 826.
565 * In addition, a sanity check is performed on the sender
566 * protocol address, to catch impersonators.
567 * We no longer handle negotiations for use of trailer protocol:
568 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
569 * along with IP replies if we wanted trailers sent to us,
570 * and also sent them in response to IP replies.
571 * This allowed either end to announce the desire to receive
572 * trailer packets.
573 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
574 * but formerly didn't normally send requests.
575 */
576static int log_arp_wrong_iface = 1;
984263bc
MD
577SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
578 &log_arp_wrong_iface, 0,
579 "log arp packets arriving on the wrong interface");
580
ecdefdda
MD
581static void
582arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
583 boolean_t dologging)
584{
585 struct arphdr *ah = mtod(m, struct arphdr *);
586 struct ifnet *ifp = m->m_pkthdr.rcvif;
587 struct llinfo_arp *la;
588 struct sockaddr_dl *sdl;
589 struct rtentry *rt;
590 int cpu = mycpuid;
591
592 la = arplookup(saddr, create, FALSE);
593 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
594 struct in_addr isaddr = { saddr };
595
596 /* the following is not an error when doing bridging */
a8d45119 597 if (rt->rt_ifp != ifp) {
ecdefdda
MD
598 if (dologging && log_arp_wrong_iface && cpu == 0) {
599 log(LOG_ERR,
600 "arp: %s is on %s "
601 "but got reply from %*D on %s\n",
602 inet_ntoa(isaddr),
603 rt->rt_ifp->if_xname,
604 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
605 ifp->if_xname);
606 }
607 return;
608 }
609 if (sdl->sdl_alen &&
610 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
611 if (rt->rt_expire != 0) {
612 if (dologging && cpu == 0) {
613 log(LOG_INFO,
614 "arp: %s moved from %*D to %*D on %s\n",
615 inet_ntoa(isaddr),
616 ifp->if_addrlen, (u_char *)LLADDR(sdl),
617 ":", ifp->if_addrlen,
618 (u_char *)ar_sha(ah), ":",
619 ifp->if_xname);
620 }
621 } else {
622 if (dologging && cpu == 0) {
623 log(LOG_ERR,
624 "arp: %*D attempts to modify permanent entry for %s on %s\n",
625 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
626 inet_ntoa(isaddr), ifp->if_xname);
627 }
628 return;
629 }
630 }
631 /*
632 * sanity check for the address length.
633 * XXX this does not work for protocols with variable address
634 * length. -is
635 */
636 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
637 cpu == 0)
638 {
639 log(LOG_WARNING,
640 "arp from %*D: new addr len %d, was %d",
641 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
642 ah->ar_hln, sdl->sdl_alen);
643 }
644 if (ifp->if_addrlen != ah->ar_hln) {
645 if (dologging && cpu == 0) {
646 log(LOG_WARNING,
647 "arp from %*D: addr len: new %d, i/f %d (ignored)",
648 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
649 ah->ar_hln, ifp->if_addrlen);
650 }
651 return;
652 }
653 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
654 /*
655 * If we receive an arp from a token-ring station over
656 * a token-ring nic then try to save the source
657 * routing info.
658 */
659 if (ifp->if_type == IFT_ISO88025) {
660 struct iso88025_header *th =
661 (struct iso88025_header *)m->m_pkthdr.header;
662 struct iso88025_sockaddr_dl_data *trld =
663 SDL_ISO88025(sdl);
664 int rif_len;
665
666 rif_len = TR_RCF_RIFLEN(th->rcf);
667 if ((th->iso88025_shost[0] & TR_RII) &&
668 (rif_len > 2)) {
669 trld->trld_rcf = th->rcf;
670 trld->trld_rcf ^= htons(TR_RCF_DIR);
671 memcpy(trld->trld_route, th->rd, rif_len - 2);
672 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
673 /*
674 * Set up source routing information for
675 * reply packet (XXX)
676 */
677 m->m_data -= rif_len;
678 m->m_len += rif_len;
679 m->m_pkthdr.len += rif_len;
680 } else {
681 th->iso88025_shost[0] &= ~TR_RII;
682 trld->trld_rcf = 0;
683 }
684 m->m_data -= 8;
685 m->m_len += 8;
686 m->m_pkthdr.len += 8;
687 th->rcf = trld->trld_rcf;
688 }
689 if (rt->rt_expire != 0)
690 rt->rt_expire = time_second + arpt_keep;
691 rt->rt_flags &= ~RTF_REJECT;
692 la->la_asked = 0;
693 la->la_preempt = arp_maxtries;
694
695 /*
696 * This particular cpu might have been holding an mbuf
697 * pending ARP resolution. If so, transmit the mbuf now.
698 */
699 if (la->la_hold != NULL) {
700 m_adj(la->la_hold, sizeof(struct ether_header));
701 lwkt_serialize_enter(ifp->if_serializer);
702 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
703 lwkt_serialize_exit(ifp->if_serializer);
704 la->la_hold = NULL;
705 }
706 }
707}
708
709#ifdef SMP
710
711struct netmsg_arp_update {
4599cf19 712 struct netmsg netmsg;
ecdefdda
MD
713 struct mbuf *m;
714 in_addr_t saddr;
715 boolean_t create;
716};
717
4599cf19 718static void arp_update_msghandler(struct netmsg *netmsg);
ecdefdda
MD
719
720#endif
721
722/*
723 * Called from arpintr() - this routine is run from a single cpu.
724 */
984263bc 725static void
f23061d4 726in_arpinput(struct mbuf *m)
984263bc 727{
2256ba69
RG
728 struct arphdr *ah;
729 struct ifnet *ifp = m->m_pkthdr.rcvif;
984263bc
MD
730 struct ether_header *eh;
731 struct arc_header *arh;
5fe66e68 732 struct iso88025_header *th = (struct iso88025_header *)NULL;
2256ba69 733 struct rtentry *rt;
984263bc
MD
734 struct ifaddr *ifa;
735 struct in_ifaddr *ia;
984263bc
MD
736 struct sockaddr sa;
737 struct in_addr isaddr, itaddr, myaddr;
ecdefdda
MD
738#ifdef SMP
739 struct netmsg_arp_update msg;
740#endif
741 int op;
984263bc
MD
742 int req_len;
743
744 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
745 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
746 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
747 return;
748 }
749
750 ah = mtod(m, struct arphdr *);
751 op = ntohs(ah->ar_op);
f23061d4
JH
752 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
753 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
984263bc 754 /*
ed5bc924
SS
755 * Check both target and sender IP addresses:
756 *
757 * If we receive the packet on the interface owning the address,
758 * then accept the address.
759 *
760 * For a bridge, we accept the address if the receive interface and
761 * the interface owning the address are on the same bridge.
762 * (This will change slightly when we have clusters of interfaces).
984263bc 763 */
ac93838f 764 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
ed5bc924
SS
765 /* Skip all ia's which don't match */
766 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
767 continue;
768
769 if (ia->ia_ifp == ifp)
770 goto match;
771
ac93838f 772 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 773 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 774 goto match;
ac93838f
SS
775 }
776 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) {
ed5bc924
SS
777 /* Skip all ia's which don't match */
778 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
779 continue;
780
781 if (ia->ia_ifp == ifp)
782 goto match;
783
ac93838f 784 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 785 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 786 goto match;
ac93838f 787 }
984263bc
MD
788 /*
789 * No match, use the first inet address on the receive interface
790 * as a dummy address for the rest of the function.
791 */
ed5bc924 792 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
984263bc
MD
793 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
794 ia = ifatoia(ifa);
795 goto match;
796 }
984263bc 797 }
ed5bc924
SS
798 /*
799 * If we got here, we didn't find any suitable interface,
800 * so drop the packet.
801 */
802 m_freem(m);
803 return;
804
984263bc
MD
805match:
806 myaddr = ia->ia_addr.sin_addr;
807 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
808 m_freem(m); /* it's from me, ignore it. */
809 return;
810 }
c401f0fd 811 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
984263bc
MD
812 log(LOG_ERR,
813 "arp: link address is broadcast for IP address %s!\n",
814 inet_ntoa(isaddr));
815 m_freem(m);
816 return;
817 }
c3749f74 818 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
984263bc
MD
819 log(LOG_ERR,
820 "arp: %*D is using my IP address %s!\n",
821 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
822 inet_ntoa(isaddr));
823 itaddr = myaddr;
824 goto reply;
825 }
ecdefdda 826#ifdef SMP
4599cf19
MD
827 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
828 arp_update_msghandler);
ecdefdda
MD
829 msg.m = m;
830 msg.saddr = isaddr.s_addr;
831 msg.create = (itaddr.s_addr == myaddr.s_addr);
4599cf19 832 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg);
ecdefdda
MD
833#endif
834 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
835 TRUE);
984263bc
MD
836reply:
837 if (op != ARPOP_REQUEST) {
838 m_freem(m);
839 return;
840 }
841 if (itaddr.s_addr == myaddr.s_addr) {
842 /* I am the target */
f23061d4
JH
843 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
844 memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
984263bc 845 } else {
ecdefdda
MD
846 struct llinfo_arp *la;
847
f23061d4 848 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
984263bc
MD
849 if (la == NULL) {
850 struct sockaddr_in sin;
851
852 if (!arp_proxyall) {
853 m_freem(m);
854 return;
855 }
856
857 bzero(&sin, sizeof sin);
858 sin.sin_family = AF_INET;
859 sin.sin_len = sizeof sin;
860 sin.sin_addr = itaddr;
861
f3ed2586 862 rt = rtpurelookup((struct sockaddr *)&sin);
0c3c561c 863 if (rt == NULL) {
984263bc
MD
864 m_freem(m);
865 return;
866 }
be23faf1 867 --rt->rt_refcnt;
984263bc
MD
868 /*
869 * Don't send proxies for nodes on the same interface
870 * as this one came out of, or we'll get into a fight
871 * over who claims what Ether address.
872 */
873 if (rt->rt_ifp == ifp) {
984263bc
MD
874 m_freem(m);
875 return;
876 }
f23061d4
JH
877 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
878 memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
984263bc 879#ifdef DEBUG_PROXY
a6ec04bc 880 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
984263bc
MD
881#endif
882 } else {
ecdefdda
MD
883 struct sockaddr_dl *sdl;
884
984263bc 885 rt = la->la_rt;
f23061d4 886 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
984263bc 887 sdl = SDL(rt->rt_gateway);
f23061d4 888 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
984263bc
MD
889 }
890 }
891
f23061d4
JH
892 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
893 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
984263bc
MD
894 ah->ar_op = htons(ARPOP_REPLY);
895 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
896 switch (ifp->if_type) {
897 case IFT_ARCNET:
898 arh = (struct arc_header *)sa.sa_data;
899 arh->arc_dhost = *ar_tha(ah);
900 arh->arc_type = ARCTYPE_ARP;
901 break;
984263bc
MD
902 case IFT_ISO88025:
903 /* Re-arrange the source/dest address */
904 memcpy(th->iso88025_dhost, th->iso88025_shost,
0c3c561c 905 sizeof th->iso88025_dhost);
984263bc 906 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
0c3c561c 907 sizeof th->iso88025_shost);
984263bc
MD
908 /* Set the source routing bit if neccesary */
909 if (th->iso88025_dhost[0] & TR_RII) {
910 th->iso88025_dhost[0] &= ~TR_RII;
911 if (TR_RCF_RIFLEN(th->rcf) > 2)
912 th->iso88025_shost[0] |= TR_RII;
913 }
914 /* Copy the addresses, ac and fc into sa_data */
915 memcpy(sa.sa_data, th->iso88025_dhost,
0c3c561c
JH
916 (sizeof th->iso88025_dhost) * 2);
917 sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC;
918 sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME;
984263bc
MD
919 break;
920 case IFT_ETHER:
921 case IFT_FDDI:
922 /*
923 * May not be correct for types not explictly
924 * listed, but it is our best guess.
925 */
926 default:
927 eh = (struct ether_header *)sa.sa_data;
0c3c561c 928 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
984263bc
MD
929 eh->ether_type = htons(ETHERTYPE_ARP);
930 break;
931 }
932 sa.sa_family = AF_UNSPEC;
0c3c561c 933 sa.sa_len = sizeof sa;
78195a76 934 lwkt_serialize_enter(ifp->if_serializer);
984263bc 935 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
78195a76 936 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
937 return;
938}
ecdefdda
MD
939
940#ifdef SMP
941
942static
4599cf19
MD
943void
944arp_update_msghandler(struct netmsg *netmsg)
ecdefdda 945{
4599cf19 946 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
ecdefdda
MD
947 int nextcpu;
948
949 arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
950
951 nextcpu = mycpuid + 1;
952 if (nextcpu < ncpus) {
4599cf19 953 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
ecdefdda 954 } else {
4599cf19 955 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
ecdefdda 956 }
ecdefdda
MD
957}
958
959#endif
960
984263bc
MD
961#endif
962
963/*
03f869ef
MD
964 * Free an arp entry. If the arp entry is actively referenced or represents
965 * a static entry we only clear it back to an unresolved state, otherwise
966 * we destroy the entry entirely.
967 *
968 * Note that static entries are created when route add ... -interface is used
969 * to create an interface route to a (direct) destination.
984263bc
MD
970 */
971static void
5fe66e68 972arptfree(struct llinfo_arp *la)
984263bc 973{
2256ba69
RG
974 struct rtentry *rt = la->la_rt;
975 struct sockaddr_dl *sdl;
f23061d4 976
03f869ef 977 if (rt == NULL)
984263bc 978 panic("arptfree");
03f869ef 979 sdl = SDL(rt->rt_gateway);
5fe66e68
JH
980 if (sdl != NULL &&
981 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
982 (rt->rt_flags & RTF_STATIC))) {
984263bc
MD
983 sdl->sdl_alen = 0;
984 la->la_preempt = la->la_asked = 0;
985 rt->rt_flags &= ~RTF_REJECT;
986 return;
987 }
f23061d4 988 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
984263bc 989}
0c3c561c 990
984263bc
MD
991/*
992 * Lookup or enter a new address in arptab.
993 */
994static struct llinfo_arp *
f23061d4 995arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
984263bc 996{
2256ba69 997 struct rtentry *rt;
f3ed2586 998 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
f23061d4 999 const char *why = NULL;
984263bc
MD
1000
1001 sin.sin_addr.s_addr = addr;
1002 sin.sin_other = proxy ? SIN_PROXY : 0;
f3ed2586
JH
1003 if (create)
1004 rt = rtlookup((struct sockaddr *)&sin);
1005 else
1006 rt = rtpurelookup((struct sockaddr *)&sin);
f23061d4
JH
1007 if (rt == NULL)
1008 return (NULL);
984263bc
MD
1009 rt->rt_refcnt--;
1010
1011 if (rt->rt_flags & RTF_GATEWAY)
1012 why = "host is not on local network";
f23061d4 1013 else if (!(rt->rt_flags & RTF_LLINFO))
984263bc
MD
1014 why = "could not allocate llinfo";
1015 else if (rt->rt_gateway->sa_family != AF_LINK)
1016 why = "gateway route is not ours";
1017
8fd3e627
MD
1018 if (why) {
1019 if (create) {
1020 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1021 inet_ntoa(sin.sin_addr), why);
1022 }
f23061d4
JH
1023 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1024 /* No references to this route. Purge it. */
1025 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
be23faf1 1026 rt_mask(rt), rt->rt_flags, NULL);
8fd3e627 1027 }
f23061d4 1028 return (NULL);
984263bc 1029 }
f23061d4 1030 return (rt->rt_llinfo);
984263bc
MD
1031}
1032
1033void
f23061d4 1034arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
984263bc 1035{
f23061d4
JH
1036 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1037 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1038 IF_LLADDR(ifp));
984263bc
MD
1039 ifa->ifa_rtrequest = arp_rtrequest;
1040 ifa->ifa_flags |= RTF_CLONING;
1041}
1042
1043static void
1044arp_init(void)
1045{
ecdefdda
MD
1046 int cpu;
1047
1048 for (cpu = 0; cpu < ncpus2; cpu++)
1049 LIST_INIT(&llinfo_arp_list[cpu]);
bf82f9b7 1050 netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
984263bc
MD
1051}
1052
1053SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);