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