development.7: Add style(9) xref and a pointer to dragonfly.el.
[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 $
b95665d8 67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.59 2008/11/22 11:03:35 sephe 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"
0d16ba1d 77#include "opt_carp.h"
984263bc
MD
78
79#include <sys/param.h>
80#include <sys/kernel.h>
81#include <sys/queue.h>
82#include <sys/sysctl.h>
83#include <sys/systm.h>
84#include <sys/mbuf.h>
85#include <sys/malloc.h>
86#include <sys/socket.h>
87#include <sys/syslog.h>
65fd4168 88#include <sys/lock.h>
984263bc
MD
89
90#include <net/if.h>
91#include <net/if_dl.h>
92#include <net/if_types.h>
93#include <net/route.h>
94#include <net/netisr.h>
95#include <net/if_llc.h>
984263bc
MD
96
97#include <netinet/in.h>
98#include <netinet/in_var.h>
99#include <netinet/if_ether.h>
100
4599cf19
MD
101#include <sys/thread2.h>
102#include <sys/msgport2.h>
103#include <net/netmsg2.h>
104
0d16ba1d
MD
105#ifdef CARP
106#include <netinet/ip_carp.h>
107#endif
108
984263bc
MD
109#define SIN(s) ((struct sockaddr_in *)s)
110#define SDL(s) ((struct sockaddr_dl *)s)
111
112SYSCTL_DECL(_net_link_ether);
113SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
114
115/* timer values */
116static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
117static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
118static int arpt_down = 20; /* once declared down, don't send for 20 sec */
119
120SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
121 &arpt_prune, 0, "");
f23061d4 122SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
984263bc
MD
123 &arpt_keep, 0, "");
124SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
125 &arpt_down, 0, "");
126
f23061d4 127#define rt_expire rt_rmx.rmx_expire
984263bc
MD
128
129struct llinfo_arp {
130 LIST_ENTRY(llinfo_arp) la_le;
131 struct rtentry *la_rt;
132 struct mbuf *la_hold; /* last packet until resolved/timeout */
a6f79f3a 133 struct lwkt_port *la_msgport; /* last packet's msgport */
a916e847 134 u_short la_preempt; /* countdown for pre-expiry arps */
984263bc 135 u_short la_asked; /* #times we QUERIED following expiration */
984263bc
MD
136};
137
ecdefdda 138static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
984263bc 139
984263bc
MD
140static int arp_maxtries = 5;
141static int useloopback = 1; /* use loopback interface for local traffic */
142static int arp_proxyall = 0;
143
144SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
19a2058e 145 &arp_maxtries, 0, "ARP resolution attempts before returning error");
984263bc 146SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
19a2058e 147 &useloopback, 0, "Use the loopback interface for local traffic");
984263bc 148SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
19a2058e 149 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
984263bc 150
730902da 151static int arp_mpsafe = 1;
ed2327fe
SZ
152TUNABLE_INT("net.link.ether.inet.arp_mpsafe", &arp_mpsafe);
153
799975d0 154static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
1dfeb73c
SZ
155static void arprequest(struct ifnet *, const struct in_addr *,
156 const struct in_addr *, const u_char *);
157static void arprequest_async(struct ifnet *, const struct in_addr *,
158 const struct in_addr *, const u_char *);
4599cf19 159static void arpintr(struct netmsg *);
799975d0
SZ
160static void arptfree(struct llinfo_arp *);
161static void arptimer(void *);
162static struct llinfo_arp *
125186e0 163 arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
984263bc 164#ifdef INET
799975d0 165static void in_arpinput(struct mbuf *);
984263bc
MD
166#endif
167
ecdefdda 168static struct callout arptimer_ch[MAXCPU];
ac311540 169
984263bc
MD
170/*
171 * Timeout routine. Age arp_tab entries periodically.
172 */
173/* ARGSUSED */
174static void
f23061d4 175arptimer(void *ignored_arg)
984263bc 176{
2e9572df 177 struct llinfo_arp *la, *nla;
984263bc 178
1cae611f 179 crit_enter();
ecdefdda 180 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
2e9572df 181 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
799975d0 182 arptfree(la);
984263bc 183 }
ecdefdda 184 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
1cae611f 185 crit_exit();
984263bc
MD
186}
187
188/*
189 * Parallel to llc_rtrequest.
190 */
191static void
f23061d4 192arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 193{
2256ba69 194 struct sockaddr *gate = rt->rt_gateway;
f23061d4
JH
195 struct llinfo_arp *la = rt->rt_llinfo;
196
5fe66e68 197 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
ecdefdda 198 static boolean_t arpinit_done[MAXCPU];
984263bc 199
ecdefdda
MD
200 if (!arpinit_done[mycpuid]) {
201 arpinit_done[mycpuid] = TRUE;
202 callout_init(&arptimer_ch[mycpuid]);
203 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
984263bc
MD
204 }
205 if (rt->rt_flags & RTF_GATEWAY)
206 return;
984263bc 207
f23061d4 208 switch (req) {
984263bc
MD
209 case RTM_ADD:
210 /*
211 * XXX: If this is a manually added route to interface
212 * such as older version of routed or gated might provide,
213 * restore cloning bit.
214 */
f23061d4 215 if (!(rt->rt_flags & RTF_HOST) &&
984263bc
MD
216 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
217 rt->rt_flags |= RTF_CLONING;
218 if (rt->rt_flags & RTF_CLONING) {
219 /*
220 * Case 1: This route should come from a route to iface.
221 */
222 rt_setgate(rt, rt_key(rt),
afe44b68
SZ
223 (struct sockaddr *)&null_sdl,
224 RTL_DONTREPORT);
984263bc
MD
225 gate = rt->rt_gateway;
226 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
227 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
228 rt->rt_expire = time_second;
229 break;
230 }
231 /* Announce a new entry if requested. */
9c94ab97 232 if (rt->rt_flags & RTF_ANNOUNCE) {
77e3dc77 233 arprequest_async(rt->rt_ifp,
984263bc
MD
234 &SIN(rt_key(rt))->sin_addr,
235 &SIN(rt_key(rt))->sin_addr,
f23061d4 236 LLADDR(SDL(gate)));
9c94ab97 237 }
984263bc
MD
238 /*FALLTHROUGH*/
239 case RTM_RESOLVE:
240 if (gate->sa_family != AF_LINK ||
f23061d4 241 gate->sa_len < sizeof(struct sockaddr_dl)) {
984263bc
MD
242 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
243 break;
244 }
245 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
246 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
f23061d4 247 if (la != NULL)
984263bc
MD
248 break; /* This happens on a route change */
249 /*
250 * Case 2: This route may come from cloning, or a manual route
251 * add with a LL address.
252 */
0c3c561c 253 R_Malloc(la, struct llinfo_arp *, sizeof *la);
f23061d4
JH
254 rt->rt_llinfo = la;
255 if (la == NULL) {
984263bc
MD
256 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
257 break;
258 }
f23061d4 259 bzero(la, sizeof *la);
984263bc
MD
260 la->la_rt = rt;
261 rt->rt_flags |= RTF_LLINFO;
ecdefdda 262 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
984263bc
MD
263
264#ifdef INET
265 /*
266 * This keeps the multicast addresses from showing up
267 * in `arp -a' listings as unresolved. It's not actually
268 * functional. Then the same for broadcast.
269 */
ce6da440 270 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
984263bc
MD
271 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
272 LLADDR(SDL(gate)));
273 SDL(gate)->sdl_alen = 6;
274 rt->rt_expire = 0;
275 }
276 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
c401f0fd 277 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
984263bc
MD
278 rt->rt_ifp->if_addrlen);
279 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
280 rt->rt_expire = 0;
281 }
282#endif
283
284 if (SIN(rt_key(rt))->sin_addr.s_addr ==
285 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
f23061d4
JH
286 /*
287 * This test used to be
288 * if (loif.if_flags & IFF_UP)
289 * It allowed local traffic to be forced
290 * through the hardware by configuring the
291 * loopback down. However, it causes problems
292 * during network configuration for boards
293 * that can't receive packets they send. It
294 * is now necessary to clear "useloopback" and
295 * remove the route to force traffic out to
296 * the hardware.
297 */
984263bc 298 rt->rt_expire = 0;
2e9572df 299 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
984263bc
MD
300 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
301 if (useloopback)
302 rt->rt_ifp = loif;
984263bc
MD
303 }
304 break;
305
306 case RTM_DELETE:
f23061d4 307 if (la == NULL)
984263bc 308 break;
984263bc 309 LIST_REMOVE(la, la_le);
f23061d4 310 rt->rt_llinfo = NULL;
984263bc 311 rt->rt_flags &= ~RTF_LLINFO;
f23061d4 312 if (la->la_hold != NULL)
984263bc 313 m_freem(la->la_hold);
f23061d4 314 Free(la);
799975d0 315 break;
984263bc
MD
316 }
317}
318
77e3dc77 319static struct mbuf *
1dfeb73c
SZ
320arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
321 const struct in_addr *tip, const u_char *enaddr)
984263bc 322{
2256ba69 323 struct mbuf *m;
2256ba69 324 struct arphdr *ah;
984263bc
MD
325 u_short ar_hrd;
326
74f1caca 327 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
77e3dc77 328 return NULL;
69e8ff02 329 m->m_pkthdr.rcvif = NULL;
f23061d4 330
984263bc 331 switch (ifp->if_type) {
984263bc
MD
332 case IFT_ETHER:
333 /*
334 * This may not be correct for types not explicitly
335 * listed, but this is our best guess
336 */
337 default:
338 ar_hrd = htons(ARPHRD_ETHER);
339
340 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
341 m->m_pkthdr.len = m->m_len;
342 MH_ALIGN(m, m->m_len);
343
984263bc
MD
344 ah = mtod(m, struct arphdr *);
345 break;
346 }
347
348 ah->ar_hrd = ar_hrd;
349 ah->ar_pro = htons(ETHERTYPE_IP);
350 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
351 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
352 ah->ar_op = htons(ARPOP_REQUEST);
f23061d4 353 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 354 memset(ar_tha(ah), 0, ah->ar_hln);
f23061d4
JH
355 memcpy(ar_spa(ah), sip, ah->ar_pln);
356 memcpy(ar_tpa(ah), tip, ah->ar_pln);
984263bc 357
77e3dc77
SZ
358 return m;
359}
360
361static void
362arpreq_send(struct ifnet *ifp, struct mbuf *m)
363{
364 struct sockaddr sa;
365 struct ether_header *eh;
366
367 switch (ifp->if_type) {
368 case IFT_ETHER:
369 /*
370 * This may not be correct for types not explicitly
371 * listed, but this is our best guess
372 */
373 default:
374 eh = (struct ether_header *)sa.sa_data;
375 /* if_output() will not swap */
376 eh->ether_type = htons(ETHERTYPE_ARP);
377 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
378 break;
379 }
380
984263bc 381 sa.sa_family = AF_UNSPEC;
77e3dc77 382 sa.sa_len = sizeof(sa);
799975d0 383 ifp->if_output(ifp, m, &sa, NULL);
984263bc
MD
384}
385
77e3dc77
SZ
386static void
387arpreq_send_handler(struct netmsg *nmsg)
388{
389 struct mbuf *m = ((struct netmsg_packet *)nmsg)->nm_packet;
390 struct ifnet *ifp = nmsg->nm_lmsg.u.ms_resultp;
391
392 arpreq_send(ifp, m);
393 /* nmsg was embedded in the mbuf, do not reply! */
394}
395
396/*
397 * Broadcast an ARP request. Caller specifies:
398 * - arp header source ip address
399 * - arp header target ip address
400 * - arp header source ethernet address
401 *
402 * NOTE: Caller MUST NOT hold ifp's serializer
403 */
404static void
1dfeb73c
SZ
405arprequest(struct ifnet *ifp, const struct in_addr *sip,
406 const struct in_addr *tip, const u_char *enaddr)
77e3dc77
SZ
407{
408 struct mbuf *m;
409
410 m = arpreq_alloc(ifp, sip, tip, enaddr);
411 if (m == NULL)
412 return;
413 arpreq_send(ifp, m);
414}
415
416/*
417 * Same as arprequest(), except:
418 * - Caller is allowed to hold ifp's serializer
419 * - Network output is done in TDF_NETWORK kernel thread
420 */
421static void
1dfeb73c
SZ
422arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
423 const struct in_addr *tip, const u_char *enaddr)
77e3dc77
SZ
424{
425 struct mbuf *m;
426 struct netmsg_packet *pmsg;
427
428 m = arpreq_alloc(ifp, sip, tip, enaddr);
429 if (m == NULL)
430 return;
431
432 pmsg = &m->m_hdr.mh_netmsg;
433 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
434 arpreq_send_handler);
435 pmsg->nm_packet = m;
436 pmsg->nm_netmsg.nm_lmsg.u.ms_resultp = ifp;
437
438 lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->nm_netmsg.nm_lmsg);
439}
440
984263bc
MD
441/*
442 * Resolve an IP address into an ethernet address. If success,
443 * desten is filled in. If there is no entry in arptab,
444 * set one up and broadcast a request for the IP address.
445 * Hold onto this mbuf and resend it once the address
446 * is finally resolved. A return value of 1 indicates
447 * that desten has been filled in and the packet should be sent
448 * normally; a 0 return indicates that the packet has been
449 * taken over here, either now or for later transmission.
450 */
451int
799975d0
SZ
452arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
453 struct sockaddr *dst, u_char *desten)
984263bc 454{
f23061d4
JH
455 struct rtentry *rt;
456 struct llinfo_arp *la = NULL;
984263bc
MD
457 struct sockaddr_dl *sdl;
458
459 if (m->m_flags & M_BCAST) { /* broadcast */
c401f0fd 460 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
461 return (1);
462 }
ce6da440 463 if (m->m_flags & M_MCAST) {/* multicast */
984263bc 464 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
f23061d4 465 return (1);
984263bc 466 }
f23061d4
JH
467 if (rt0 != NULL) {
468 if (rt_llroute(dst, rt0, &rt) != 0) {
469 m_freem(m);
470 return 0;
471 }
472 la = rt->rt_llinfo;
473 }
474 if (la == NULL) {
125186e0
SZ
475 la = arplookup(SIN(dst)->sin_addr.s_addr,
476 TRUE, RTL_REPORTMSG, FALSE);
f23061d4 477 if (la != NULL)
984263bc
MD
478 rt = la->la_rt;
479 }
f23061d4 480 if (la == NULL || rt == NULL) {
984263bc 481 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
5fe66e68
JH
482 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
483 rt ? "rt" : "");
984263bc
MD
484 m_freem(m);
485 return (0);
486 }
487 sdl = SDL(rt->rt_gateway);
488 /*
489 * Check the address family and length is valid, the address
490 * is resolved; otherwise, try to resolve.
491 */
492 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
493 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
494 /*
495 * If entry has an expiry time and it is approaching,
496 * see if we need to send an ARP request within this
497 * arpt_down interval.
498 */
499 if ((rt->rt_expire != 0) &&
a916e847 500 (time_second + la->la_preempt > rt->rt_expire)) {
984263bc
MD
501 arprequest(ifp,
502 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
503 &SIN(dst)->sin_addr,
504 IF_LLADDR(ifp));
a916e847 505 la->la_preempt--;
f23061d4 506 }
984263bc
MD
507
508 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
509 return 1;
510 }
511 /*
07813904 512 * If ARP is disabled or static on this interface, stop.
984263bc
MD
513 * XXX
514 * Probably should not allocate empty llinfo struct if we are
515 * not going to be sending out an arp request.
516 */
07813904 517 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
984263bc
MD
518 m_freem(m);
519 return (0);
520 }
521 /*
522 * There is an arptab entry, but no ethernet address
523 * response yet. Replace the held mbuf with this
524 * latest one.
525 */
f23061d4 526 if (la->la_hold != NULL)
984263bc
MD
527 m_freem(la->la_hold);
528 la->la_hold = m;
c244d613 529 la->la_msgport = curnetport;
03f869ef 530 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
984263bc
MD
531 rt->rt_flags &= ~RTF_REJECT;
532 if (la->la_asked == 0 || rt->rt_expire != time_second) {
533 rt->rt_expire = time_second;
534 if (la->la_asked++ < arp_maxtries) {
535 arprequest(ifp,
536 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
537 &SIN(dst)->sin_addr,
538 IF_LLADDR(ifp));
539 } else {
540 rt->rt_flags |= RTF_REJECT;
541 rt->rt_expire += arpt_down;
a916e847
JH
542 la->la_asked = 0;
543 la->la_preempt = arp_maxtries;
984263bc 544 }
984263bc
MD
545 }
546 }
547 return (0);
548}
549
550/*
551 * Common length and type checks are done here,
552 * then the protocol-specific routine is called.
553 */
4599cf19 554static void
9eeaa8a9 555arpintr(struct netmsg *msg)
984263bc 556{
9eeaa8a9 557 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
2256ba69 558 struct arphdr *ar;
8bde602d 559 u_short ar_hrd;
984263bc 560
8bde602d 561 if (m->m_len < sizeof(struct arphdr) &&
db597c8a 562 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
8bde602d 563 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
db597c8a 564 return;
8bde602d
JH
565 }
566 ar = mtod(m, struct arphdr *);
567
568 ar_hrd = ntohs(ar->ar_hrd);
db597c8a
SZ
569 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
570 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
8bde602d 571 (unsigned char *)&ar->ar_hrd, "");
db597c8a
SZ
572 m_freem(m);
573 return;
8bde602d 574 }
984263bc 575
db597c8a
SZ
576 if (m->m_pkthdr.len < arphdr_len(ar)) {
577 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
578 log(LOG_ERR, "arp: runt packet\n");
579 return;
580 }
581 ar = mtod(m, struct arphdr *);
8bde602d
JH
582 }
583
584 switch (ntohs(ar->ar_pro)) {
984263bc 585#ifdef INET
db597c8a
SZ
586 case ETHERTYPE_IP:
587 in_arpinput(m);
588 return;
984263bc 589#endif
984263bc 590 }
8bde602d 591 m_freem(m);
6aad077d 592 /* msg was embedded in the mbuf, do not reply! */
984263bc
MD
593}
594
595#ifdef INET
596/*
597 * ARP for Internet protocols on 10 Mb/s Ethernet.
598 * Algorithm is that given in RFC 826.
599 * In addition, a sanity check is performed on the sender
600 * protocol address, to catch impersonators.
601 * We no longer handle negotiations for use of trailer protocol:
602 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
603 * along with IP replies if we wanted trailers sent to us,
604 * and also sent them in response to IP replies.
605 * This allowed either end to announce the desire to receive
606 * trailer packets.
607 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
608 * but formerly didn't normally send requests.
609 */
87ccd6a5
SZ
610
611static int log_arp_wrong_iface = 1;
612static int log_arp_movements = 1;
613static int log_arp_permanent_modify = 1;
614
984263bc 615SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
799975d0 616 &log_arp_wrong_iface, 0,
87ccd6a5
SZ
617 "Log arp packets arriving on the wrong interface");
618SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
619 &log_arp_movements, 0,
620 "Log arp replies from MACs different than the one in the cache");
621SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
622 &log_arp_permanent_modify, 0,
623 "Log arp replies from MACs different than the one "
624 "in the permanent arp entry");
625
984263bc 626
a6f79f3a
SZ
627static void
628arp_hold_output(struct netmsg *nmsg)
629{
630 struct mbuf *m = ((struct netmsg_packet *)nmsg)->nm_packet;
631 struct rtentry *rt;
632 struct ifnet *ifp;
633
634 rt = nmsg->nm_lmsg.u.ms_resultp;
635 ifp = m->m_pkthdr.rcvif;
636 m->m_pkthdr.rcvif = NULL;
637
638 ifp->if_output(ifp, m, rt_key(rt), rt);
639
640 /* Drop the reference count bumped by the sender */
641 RTFREE(rt);
642
643 /* nmsg was embedded in the mbuf, do not reply! */
644}
645
ecdefdda
MD
646static void
647arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
125186e0 648 boolean_t generate_report, boolean_t dologging)
ecdefdda
MD
649{
650 struct arphdr *ah = mtod(m, struct arphdr *);
651 struct ifnet *ifp = m->m_pkthdr.rcvif;
652 struct llinfo_arp *la;
653 struct sockaddr_dl *sdl;
654 struct rtentry *rt;
ecdefdda 655
125186e0 656 la = arplookup(saddr, create, generate_report, FALSE);
ecdefdda
MD
657 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
658 struct in_addr isaddr = { saddr };
659
660 /* the following is not an error when doing bridging */
a8d45119 661 if (rt->rt_ifp != ifp) {
e090f4c1 662 if (dologging && log_arp_wrong_iface) {
ecdefdda
MD
663 log(LOG_ERR,
664 "arp: %s is on %s "
665 "but got reply from %*D on %s\n",
666 inet_ntoa(isaddr),
667 rt->rt_ifp->if_xname,
668 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
669 ifp->if_xname);
670 }
671 return;
672 }
673 if (sdl->sdl_alen &&
674 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
675 if (rt->rt_expire != 0) {
87ccd6a5 676 if (dologging && log_arp_movements) {
ecdefdda
MD
677 log(LOG_INFO,
678 "arp: %s moved from %*D to %*D on %s\n",
679 inet_ntoa(isaddr),
680 ifp->if_addrlen, (u_char *)LLADDR(sdl),
681 ":", ifp->if_addrlen,
682 (u_char *)ar_sha(ah), ":",
683 ifp->if_xname);
684 }
685 } else {
87ccd6a5 686 if (dologging && log_arp_permanent_modify) {
ecdefdda 687 log(LOG_ERR,
799975d0
SZ
688 "arp: %*D attempts to modify "
689 "permanent entry for %s on %s\n",
690 ifp->if_addrlen, (u_char *)ar_sha(ah),
691 ":", inet_ntoa(isaddr), ifp->if_xname);
ecdefdda
MD
692 }
693 return;
694 }
695 }
696 /*
697 * sanity check for the address length.
698 * XXX this does not work for protocols with variable address
699 * length. -is
700 */
e090f4c1 701 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
ecdefdda
MD
702 log(LOG_WARNING,
703 "arp from %*D: new addr len %d, was %d",
704 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
705 ah->ar_hln, sdl->sdl_alen);
706 }
707 if (ifp->if_addrlen != ah->ar_hln) {
e090f4c1 708 if (dologging) {
ecdefdda 709 log(LOG_WARNING,
799975d0
SZ
710 "arp from %*D: addr len: new %d, i/f %d "
711 "(ignored)",
ecdefdda
MD
712 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
713 ah->ar_hln, ifp->if_addrlen);
714 }
715 return;
716 }
717 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
ecdefdda
MD
718 if (rt->rt_expire != 0)
719 rt->rt_expire = time_second + arpt_keep;
720 rt->rt_flags &= ~RTF_REJECT;
721 la->la_asked = 0;
722 la->la_preempt = arp_maxtries;
723
724 /*
725 * This particular cpu might have been holding an mbuf
726 * pending ARP resolution. If so, transmit the mbuf now.
727 */
728 if (la->la_hold != NULL) {
a6f79f3a
SZ
729 struct mbuf *m = la->la_hold;
730 struct lwkt_port *port = la->la_msgport;
731 struct netmsg_packet *pmsg;
732
ecdefdda 733 la->la_hold = NULL;
a6f79f3a
SZ
734 la->la_msgport = NULL;
735
736 m_adj(m, sizeof(struct ether_header));
737
738 /*
739 * Make sure that this rtentry will not be freed
740 * before the packet is processed on the target
741 * msgport. The reference count will be dropped
742 * in the handler associated with this packet.
743 */
744 rt->rt_refcnt++;
745
746 pmsg = &m->m_hdr.mh_netmsg;
747 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport,
d6f338fc
SZ
748 MSGF_MPSAFE | MSGF_PRIORITY,
749 arp_hold_output);
a6f79f3a
SZ
750 pmsg->nm_packet = m;
751
752 /* Record necessary information */
753 m->m_pkthdr.rcvif = ifp;
754 pmsg->nm_netmsg.nm_lmsg.u.ms_resultp = rt;
755
756 lwkt_sendmsg(port, &pmsg->nm_netmsg.nm_lmsg);
ecdefdda
MD
757 }
758 }
759}
760
761#ifdef SMP
762
763struct netmsg_arp_update {
4599cf19 764 struct netmsg netmsg;
ecdefdda
MD
765 struct mbuf *m;
766 in_addr_t saddr;
767 boolean_t create;
768};
769
799975d0 770static void arp_update_msghandler(struct netmsg *);
ecdefdda
MD
771
772#endif
773
774/*
775 * Called from arpintr() - this routine is run from a single cpu.
776 */
984263bc 777static void
f23061d4 778in_arpinput(struct mbuf *m)
984263bc 779{
2256ba69
RG
780 struct arphdr *ah;
781 struct ifnet *ifp = m->m_pkthdr.rcvif;
984263bc 782 struct ether_header *eh;
2256ba69 783 struct rtentry *rt;
b2632176 784 struct ifaddr_container *ifac;
f8983475 785 struct in_ifaddr_container *iac;
5bd4422e 786 struct in_ifaddr *ia = NULL;
984263bc
MD
787 struct sockaddr sa;
788 struct in_addr isaddr, itaddr, myaddr;
ecdefdda
MD
789#ifdef SMP
790 struct netmsg_arp_update msg;
791#endif
5bd4422e 792 uint8_t *enaddr = NULL;
ecdefdda 793 int op;
984263bc
MD
794 int req_len;
795
796 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
797 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
798 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
799 return;
800 }
801
802 ah = mtod(m, struct arphdr *);
803 op = ntohs(ah->ar_op);
f23061d4
JH
804 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
805 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
5bd4422e
SZ
806
807 myaddr.s_addr = INADDR_ANY;
808#ifdef CARP
809 if (ifp->if_carp != NULL) {
810 get_mplock();
811 if (ifp->if_carp != NULL &&
812 carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) {
813 rel_mplock();
814 myaddr = itaddr;
815 goto match;
816 }
817 rel_mplock();
818 }
819#endif
820
984263bc 821 /*
ed5bc924
SS
822 * Check both target and sender IP addresses:
823 *
824 * If we receive the packet on the interface owning the address,
825 * then accept the address.
826 *
827 * For a bridge, we accept the address if the receive interface and
828 * the interface owning the address are on the same bridge.
829 * (This will change slightly when we have clusters of interfaces).
984263bc 830 */
f8983475
SZ
831 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
832 ia = iac->ia;
833
ed5bc924
SS
834 /* Skip all ia's which don't match */
835 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
836 continue;
5bd4422e
SZ
837#ifdef CARP
838 if (ia->ia_ifp->if_type == IFT_CARP)
839 continue;
840#endif
ed5bc924
SS
841 if (ia->ia_ifp == ifp)
842 goto match;
843
ac93838f 844 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 845 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 846 goto match;
ac93838f 847 }
f8983475
SZ
848 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
849 ia = iac->ia;
850
ed5bc924
SS
851 /* Skip all ia's which don't match */
852 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
853 continue;
5bd4422e
SZ
854#ifdef CARP
855 if (ia->ia_ifp->if_type == IFT_CARP)
856 continue;
857#endif
ed5bc924
SS
858 if (ia->ia_ifp == ifp)
859 goto match;
860
ac93838f 861 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 862 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 863 goto match;
ac93838f 864 }
984263bc
MD
865 /*
866 * No match, use the first inet address on the receive interface
867 * as a dummy address for the rest of the function.
868 */
b2632176
SZ
869 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
870 struct ifaddr *ifa = ifac->ifa;
871
984263bc
MD
872 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
873 ia = ifatoia(ifa);
874 goto match;
875 }
984263bc 876 }
ed5bc924
SS
877 /*
878 * If we got here, we didn't find any suitable interface,
879 * so drop the packet.
880 */
881 m_freem(m);
882 return;
883
984263bc 884match:
0d16ba1d 885 if (!enaddr)
5bd4422e
SZ
886 enaddr = (uint8_t *)IF_LLADDR(ifp);
887 if (myaddr.s_addr == INADDR_ANY)
888 myaddr = ia->ia_addr.sin_addr;
0d16ba1d 889 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
984263bc
MD
890 m_freem(m); /* it's from me, ignore it. */
891 return;
892 }
c401f0fd 893 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
984263bc
MD
894 log(LOG_ERR,
895 "arp: link address is broadcast for IP address %s!\n",
896 inet_ntoa(isaddr));
897 m_freem(m);
898 return;
899 }
c3749f74 900 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
984263bc
MD
901 log(LOG_ERR,
902 "arp: %*D is using my IP address %s!\n",
903 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
904 inet_ntoa(isaddr));
905 itaddr = myaddr;
906 goto reply;
907 }
07813904
SZ
908 if (ifp->if_flags & IFF_STATICARP)
909 goto reply;
ecdefdda 910#ifdef SMP
4599cf19
MD
911 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
912 arp_update_msghandler);
ecdefdda
MD
913 msg.m = m;
914 msg.saddr = isaddr.s_addr;
915 msg.create = (itaddr.s_addr == myaddr.s_addr);
a22c590e 916 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
e090f4c1 917#else
ecdefdda 918 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
125186e0 919 RTL_REPORTMSG, TRUE);
e090f4c1 920#endif
984263bc
MD
921reply:
922 if (op != ARPOP_REQUEST) {
923 m_freem(m);
924 return;
925 }
926 if (itaddr.s_addr == myaddr.s_addr) {
927 /* I am the target */
f23061d4 928 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
0d16ba1d 929 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 930 } else {
ecdefdda
MD
931 struct llinfo_arp *la;
932
125186e0 933 la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
984263bc
MD
934 if (la == NULL) {
935 struct sockaddr_in sin;
936
937 if (!arp_proxyall) {
938 m_freem(m);
939 return;
940 }
941
942 bzero(&sin, sizeof sin);
943 sin.sin_family = AF_INET;
944 sin.sin_len = sizeof sin;
945 sin.sin_addr = itaddr;
946
f3ed2586 947 rt = rtpurelookup((struct sockaddr *)&sin);
0c3c561c 948 if (rt == NULL) {
984263bc
MD
949 m_freem(m);
950 return;
951 }
be23faf1 952 --rt->rt_refcnt;
984263bc
MD
953 /*
954 * Don't send proxies for nodes on the same interface
955 * as this one came out of, or we'll get into a fight
956 * over who claims what Ether address.
957 */
958 if (rt->rt_ifp == ifp) {
984263bc
MD
959 m_freem(m);
960 return;
961 }
f23061d4 962 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
0d16ba1d 963 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 964#ifdef DEBUG_PROXY
a6ec04bc 965 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
984263bc
MD
966#endif
967 } else {
ecdefdda
MD
968 struct sockaddr_dl *sdl;
969
984263bc 970 rt = la->la_rt;
f23061d4 971 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
984263bc 972 sdl = SDL(rt->rt_gateway);
f23061d4 973 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
984263bc
MD
974 }
975 }
976
f23061d4
JH
977 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
978 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
984263bc
MD
979 ah->ar_op = htons(ARPOP_REPLY);
980 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
981 switch (ifp->if_type) {
984263bc 982 case IFT_ETHER:
984263bc
MD
983 /*
984 * May not be correct for types not explictly
985 * listed, but it is our best guess.
986 */
987 default:
988 eh = (struct ether_header *)sa.sa_data;
0c3c561c 989 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
984263bc
MD
990 eh->ether_type = htons(ETHERTYPE_ARP);
991 break;
992 }
993 sa.sa_family = AF_UNSPEC;
0c3c561c 994 sa.sa_len = sizeof sa;
799975d0 995 ifp->if_output(ifp, m, &sa, NULL);
984263bc 996}
ecdefdda
MD
997
998#ifdef SMP
999
799975d0 1000static void
4599cf19 1001arp_update_msghandler(struct netmsg *netmsg)
ecdefdda 1002{
4599cf19 1003 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
ecdefdda
MD
1004 int nextcpu;
1005
125186e0
SZ
1006 /*
1007 * This message handler will be called on all of the CPUs,
1008 * however, we only need to generate rtmsg on CPU0.
1009 */
1010 arp_update_oncpu(msg->m, msg->saddr, msg->create,
1011 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1012 mycpuid == 0);
ecdefdda
MD
1013
1014 nextcpu = mycpuid + 1;
799975d0 1015 if (nextcpu < ncpus)
4599cf19 1016 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
799975d0 1017 else
4599cf19 1018 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
ecdefdda
MD
1019}
1020
799975d0 1021#endif /* SMP */
ecdefdda 1022
799975d0 1023#endif /* INET */
984263bc
MD
1024
1025/*
03f869ef
MD
1026 * Free an arp entry. If the arp entry is actively referenced or represents
1027 * a static entry we only clear it back to an unresolved state, otherwise
1028 * we destroy the entry entirely.
1029 *
1030 * Note that static entries are created when route add ... -interface is used
1031 * to create an interface route to a (direct) destination.
984263bc
MD
1032 */
1033static void
5fe66e68 1034arptfree(struct llinfo_arp *la)
984263bc 1035{
2256ba69
RG
1036 struct rtentry *rt = la->la_rt;
1037 struct sockaddr_dl *sdl;
f23061d4 1038
03f869ef 1039 if (rt == NULL)
984263bc 1040 panic("arptfree");
03f869ef 1041 sdl = SDL(rt->rt_gateway);
5fe66e68
JH
1042 if (sdl != NULL &&
1043 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1044 (rt->rt_flags & RTF_STATIC))) {
984263bc
MD
1045 sdl->sdl_alen = 0;
1046 la->la_preempt = la->la_asked = 0;
1047 rt->rt_flags &= ~RTF_REJECT;
1048 return;
1049 }
f23061d4 1050 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
984263bc 1051}
0c3c561c 1052
984263bc
MD
1053/*
1054 * Lookup or enter a new address in arptab.
1055 */
1056static struct llinfo_arp *
125186e0
SZ
1057arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1058 boolean_t proxy)
984263bc 1059{
2256ba69 1060 struct rtentry *rt;
f3ed2586 1061 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
f23061d4 1062 const char *why = NULL;
984263bc
MD
1063
1064 sin.sin_addr.s_addr = addr;
1065 sin.sin_other = proxy ? SIN_PROXY : 0;
125186e0
SZ
1066 if (create) {
1067 rt = _rtlookup((struct sockaddr *)&sin,
1068 generate_report, RTL_DOCLONE);
1069 } else {
f3ed2586 1070 rt = rtpurelookup((struct sockaddr *)&sin);
125186e0 1071 }
f23061d4
JH
1072 if (rt == NULL)
1073 return (NULL);
984263bc
MD
1074 rt->rt_refcnt--;
1075
1076 if (rt->rt_flags & RTF_GATEWAY)
1077 why = "host is not on local network";
f23061d4 1078 else if (!(rt->rt_flags & RTF_LLINFO))
984263bc
MD
1079 why = "could not allocate llinfo";
1080 else if (rt->rt_gateway->sa_family != AF_LINK)
1081 why = "gateway route is not ours";
1082
8fd3e627
MD
1083 if (why) {
1084 if (create) {
1085 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1086 inet_ntoa(sin.sin_addr), why);
1087 }
f23061d4
JH
1088 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1089 /* No references to this route. Purge it. */
1090 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
be23faf1 1091 rt_mask(rt), rt->rt_flags, NULL);
8fd3e627 1092 }
f23061d4 1093 return (NULL);
984263bc 1094 }
f23061d4 1095 return (rt->rt_llinfo);
984263bc
MD
1096}
1097
1098void
f23061d4 1099arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
984263bc 1100{
57dff79c 1101 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
77e3dc77
SZ
1102 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1103 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
57dff79c 1104 }
984263bc
MD
1105 ifa->ifa_rtrequest = arp_rtrequest;
1106 ifa->ifa_flags |= RTF_CLONING;
1107}
1108
0d16ba1d 1109void
5bd4422e 1110arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr)
0d16ba1d 1111{
5bd4422e
SZ
1112 if (addr->s_addr != INADDR_ANY)
1113 arprequest_async(ifp, addr, addr, enaddr);
0d16ba1d
MD
1114}
1115
984263bc
MD
1116static void
1117arp_init(void)
1118{
ed2327fe 1119 uint32_t flags;
ecdefdda
MD
1120 int cpu;
1121
1122 for (cpu = 0; cpu < ncpus2; cpu++)
1123 LIST_INIT(&llinfo_arp_list[cpu]);
ed2327fe
SZ
1124
1125 if (arp_mpsafe) {
1126 flags = NETISR_FLAG_MPSAFE;
1127 kprintf("arp: MPSAFE\n");
1128 } else {
1129 flags = NETISR_FLAG_NOTMPSAFE;
1130 }
f92bfd8c
SZ
1131 netisr_register(NETISR_ARP, cpu0_portfn, pktinfo_portfn_cpu0,
1132 arpintr, flags);
984263bc
MD
1133}
1134
1135SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);