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