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