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