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