Rename printf -> kprintf in sys/ and add some defines where necessary
[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
33/*
34 * Copyright (c) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
35 *
36 * License terms: all terms for the DragonFly license above plus the following:
37 *
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 *
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
43 *
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
48 * apply.
49 */
50
984263bc
MD
51/*
52 * Copyright (c) 1982, 1986, 1988, 1993
53 * The Regents of the University of California. All rights reserved.
54 *
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
57 * are met:
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81 * SUCH DAMAGE.
82 *
83 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
84 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
ed5bc924 85 * $DragonFly: src/sys/netinet/if_ether.c,v 1.34 2006/07/20 16:22:48 corecode Exp $
984263bc
MD
86 */
87
88/*
89 * Ethernet address resolution protocol.
90 * TODO:
91 * add "inuse/lock" bit (or ref. count) along with valid bit
92 */
93
94#include "opt_inet.h"
984263bc
MD
95
96#include <sys/param.h>
97#include <sys/kernel.h>
98#include <sys/queue.h>
99#include <sys/sysctl.h>
100#include <sys/systm.h>
101#include <sys/mbuf.h>
102#include <sys/malloc.h>
103#include <sys/socket.h>
104#include <sys/syslog.h>
105
a80cf23b
JH
106#include <sys/thread2.h>
107#include <sys/msgport2.h>
108
984263bc
MD
109#include <net/if.h>
110#include <net/if_dl.h>
111#include <net/if_types.h>
112#include <net/route.h>
113#include <net/netisr.h>
114#include <net/if_llc.h>
984263bc
MD
115
116#include <netinet/in.h>
117#include <netinet/in_var.h>
118#include <netinet/if_ether.h>
119
120#include <net/if_arc.h>
121#include <net/iso88025.h>
122
123#define SIN(s) ((struct sockaddr_in *)s)
124#define SDL(s) ((struct sockaddr_dl *)s)
125
126SYSCTL_DECL(_net_link_ether);
127SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
128
129/* timer values */
130static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
131static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
132static int arpt_down = 20; /* once declared down, don't send for 20 sec */
133
134SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
135 &arpt_prune, 0, "");
f23061d4 136SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
984263bc
MD
137 &arpt_keep, 0, "");
138SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
139 &arpt_down, 0, "");
140
f23061d4 141#define rt_expire rt_rmx.rmx_expire
984263bc
MD
142
143struct llinfo_arp {
144 LIST_ENTRY(llinfo_arp) la_le;
145 struct rtentry *la_rt;
146 struct mbuf *la_hold; /* last packet until resolved/timeout */
a916e847 147 u_short la_preempt; /* countdown for pre-expiry arps */
984263bc 148 u_short la_asked; /* #times we QUERIED following expiration */
984263bc
MD
149};
150
ecdefdda 151static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
984263bc 152
984263bc
MD
153static int arp_maxtries = 5;
154static int useloopback = 1; /* use loopback interface for local traffic */
155static int arp_proxyall = 0;
156
157SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
158 &arp_maxtries, 0, "");
159SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
160 &useloopback, 0, "");
161SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
162 &arp_proxyall, 0, "");
163
42a7fc75
RG
164static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
165static void arprequest (struct ifnet *,
166 struct in_addr *, struct in_addr *, u_char *);
b76bed62 167static int arpintr(struct netmsg *);
42a7fc75
RG
168static void arptfree (struct llinfo_arp *);
169static void arptimer (void *);
984263bc 170static struct llinfo_arp
f23061d4 171 *arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
984263bc 172#ifdef INET
42a7fc75 173static void in_arpinput (struct mbuf *);
984263bc
MD
174#endif
175
ecdefdda 176static struct callout arptimer_ch[MAXCPU];
ac311540 177
984263bc
MD
178/*
179 * Timeout routine. Age arp_tab entries periodically.
180 */
181/* ARGSUSED */
182static void
f23061d4 183arptimer(void *ignored_arg)
984263bc 184{
2e9572df 185 struct llinfo_arp *la, *nla;
984263bc 186
1cae611f 187 crit_enter();
ecdefdda 188 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
2e9572df 189 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
ecdefdda 190 arptfree(la);
984263bc 191 }
ecdefdda 192 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
1cae611f 193 crit_exit();
984263bc
MD
194}
195
196/*
197 * Parallel to llc_rtrequest.
198 */
199static void
f23061d4 200arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 201{
2256ba69 202 struct sockaddr *gate = rt->rt_gateway;
f23061d4
JH
203 struct llinfo_arp *la = rt->rt_llinfo;
204
5fe66e68 205 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
ecdefdda 206 static boolean_t arpinit_done[MAXCPU];
984263bc 207
ecdefdda
MD
208 if (!arpinit_done[mycpuid]) {
209 arpinit_done[mycpuid] = TRUE;
210 callout_init(&arptimer_ch[mycpuid]);
211 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
984263bc
MD
212 }
213 if (rt->rt_flags & RTF_GATEWAY)
214 return;
984263bc 215
f23061d4 216 switch (req) {
984263bc
MD
217 case RTM_ADD:
218 /*
219 * XXX: If this is a manually added route to interface
220 * such as older version of routed or gated might provide,
221 * restore cloning bit.
222 */
f23061d4 223 if (!(rt->rt_flags & RTF_HOST) &&
984263bc
MD
224 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
225 rt->rt_flags |= RTF_CLONING;
226 if (rt->rt_flags & RTF_CLONING) {
227 /*
228 * Case 1: This route should come from a route to iface.
229 */
230 rt_setgate(rt, rt_key(rt),
0c3c561c 231 (struct sockaddr *)&null_sdl);
984263bc
MD
232 gate = rt->rt_gateway;
233 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
234 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
235 rt->rt_expire = time_second;
236 break;
237 }
238 /* Announce a new entry if requested. */
239 if (rt->rt_flags & RTF_ANNOUNCE)
240 arprequest(rt->rt_ifp,
241 &SIN(rt_key(rt))->sin_addr,
242 &SIN(rt_key(rt))->sin_addr,
f23061d4 243 LLADDR(SDL(gate)));
984263bc
MD
244 /*FALLTHROUGH*/
245 case RTM_RESOLVE:
246 if (gate->sa_family != AF_LINK ||
f23061d4 247 gate->sa_len < sizeof(struct sockaddr_dl)) {
984263bc
MD
248 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
249 break;
250 }
251 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
252 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
f23061d4 253 if (la != NULL)
984263bc
MD
254 break; /* This happens on a route change */
255 /*
256 * Case 2: This route may come from cloning, or a manual route
257 * add with a LL address.
258 */
0c3c561c 259 R_Malloc(la, struct llinfo_arp *, sizeof *la);
f23061d4
JH
260 rt->rt_llinfo = la;
261 if (la == NULL) {
984263bc
MD
262 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
263 break;
264 }
f23061d4 265 bzero(la, sizeof *la);
984263bc
MD
266 la->la_rt = rt;
267 rt->rt_flags |= RTF_LLINFO;
ecdefdda 268 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
984263bc
MD
269
270#ifdef INET
271 /*
272 * This keeps the multicast addresses from showing up
273 * in `arp -a' listings as unresolved. It's not actually
274 * functional. Then the same for broadcast.
275 */
f23061d4
JH
276 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
277 rt->rt_ifp->if_type != IFT_ARCNET) {
984263bc
MD
278 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
279 LLADDR(SDL(gate)));
280 SDL(gate)->sdl_alen = 6;
281 rt->rt_expire = 0;
282 }
283 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
c401f0fd 284 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
984263bc
MD
285 rt->rt_ifp->if_addrlen);
286 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
287 rt->rt_expire = 0;
288 }
289#endif
290
291 if (SIN(rt_key(rt))->sin_addr.s_addr ==
292 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
f23061d4
JH
293 /*
294 * This test used to be
295 * if (loif.if_flags & IFF_UP)
296 * It allowed local traffic to be forced
297 * through the hardware by configuring the
298 * loopback down. However, it causes problems
299 * during network configuration for boards
300 * that can't receive packets they send. It
301 * is now necessary to clear "useloopback" and
302 * remove the route to force traffic out to
303 * the hardware.
304 */
984263bc 305 rt->rt_expire = 0;
2e9572df 306 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
984263bc
MD
307 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
308 if (useloopback)
309 rt->rt_ifp = loif;
984263bc
MD
310 }
311 break;
312
313 case RTM_DELETE:
f23061d4 314 if (la == NULL)
984263bc 315 break;
984263bc 316 LIST_REMOVE(la, la_le);
f23061d4 317 rt->rt_llinfo = NULL;
984263bc 318 rt->rt_flags &= ~RTF_LLINFO;
f23061d4 319 if (la->la_hold != NULL)
984263bc 320 m_freem(la->la_hold);
f23061d4 321 Free(la);
984263bc
MD
322 }
323}
324
325/*
326 * Broadcast an ARP request. Caller specifies:
327 * - arp header source ip address
328 * - arp header target ip address
329 * - arp header source ethernet address
330 */
331static void
f23061d4
JH
332arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
333 u_char *enaddr)
984263bc 334{
2256ba69
RG
335 struct mbuf *m;
336 struct ether_header *eh;
337 struct arc_header *arh;
338 struct arphdr *ah;
984263bc 339 struct sockaddr sa;
f23061d4
JH
340 static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
341 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
984263bc
MD
342 u_short ar_hrd;
343
74f1caca 344 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
984263bc 345 return;
f23061d4
JH
346 m->m_pkthdr.rcvif = (struct ifnet *)NULL;
347
984263bc
MD
348 switch (ifp->if_type) {
349 case IFT_ARCNET:
350 ar_hrd = htons(ARPHRD_ARCNET);
351
352 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
353 m->m_pkthdr.len = m->m_len;
354 MH_ALIGN(m, m->m_len);
355
356 arh = (struct arc_header *)sa.sa_data;
c401f0fd 357 arh->arc_dhost = ifp->if_broadcastaddr[0];
984263bc
MD
358 arh->arc_type = ARCTYPE_ARP;
359
360 ah = mtod(m, struct arphdr *);
361 break;
362
363 case IFT_ISO88025:
364 ar_hrd = htons(ARPHRD_IEEE802);
365
0c3c561c 366 m->m_len = (sizeof llcx) +
984263bc
MD
367 arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
368 m->m_pkthdr.len = m->m_len;
369 MH_ALIGN(m, m->m_len);
370
0c3c561c 371 memcpy(mtod(m, caddr_t), llcx, sizeof llcx);
c401f0fd 372 memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
f23061d4 373 memcpy(sa.sa_data + 6, enaddr, 6);
984263bc
MD
374 sa.sa_data[6] |= TR_RII;
375 sa.sa_data[12] = TR_AC;
376 sa.sa_data[13] = TR_LLC_FRAME;
377
0c3c561c 378 ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx);
984263bc
MD
379 break;
380 case IFT_FDDI:
381 case IFT_ETHER:
382 /*
383 * This may not be correct for types not explicitly
384 * listed, but this is our best guess
385 */
386 default:
387 ar_hrd = htons(ARPHRD_ETHER);
388
389 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
390 m->m_pkthdr.len = m->m_len;
391 MH_ALIGN(m, m->m_len);
392
393 eh = (struct ether_header *)sa.sa_data;
f23061d4 394 /* if_output() will not swap */
984263bc 395 eh->ether_type = htons(ETHERTYPE_ARP);
c401f0fd 396 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
397
398 ah = mtod(m, struct arphdr *);
399 break;
400 }
401
402 ah->ar_hrd = ar_hrd;
403 ah->ar_pro = htons(ETHERTYPE_IP);
404 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
405 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
406 ah->ar_op = htons(ARPOP_REQUEST);
f23061d4 407 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 408 memset(ar_tha(ah), 0, ah->ar_hln);
f23061d4
JH
409 memcpy(ar_spa(ah), sip, ah->ar_pln);
410 memcpy(ar_tpa(ah), tip, ah->ar_pln);
984263bc
MD
411
412 sa.sa_family = AF_UNSPEC;
0c3c561c 413 sa.sa_len = sizeof sa;
f23061d4 414 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
984263bc
MD
415}
416
417/*
418 * Resolve an IP address into an ethernet address. If success,
419 * desten is filled in. If there is no entry in arptab,
420 * set one up and broadcast a request for the IP address.
421 * Hold onto this mbuf and resend it once the address
422 * is finally resolved. A return value of 1 indicates
423 * that desten has been filled in and the packet should be sent
424 * normally; a 0 return indicates that the packet has been
425 * taken over here, either now or for later transmission.
426 */
427int
f23061d4
JH
428arpresolve(
429 struct ifnet *ifp,
430 struct rtentry *rt0,
431 struct mbuf *m,
432 struct sockaddr *dst,
433 u_char *desten)
984263bc 434{
f23061d4
JH
435 struct rtentry *rt;
436 struct llinfo_arp *la = NULL;
984263bc
MD
437 struct sockaddr_dl *sdl;
438
439 if (m->m_flags & M_BCAST) { /* broadcast */
c401f0fd 440 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
441 return (1);
442 }
443 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
444 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
f23061d4 445 return (1);
984263bc 446 }
f23061d4
JH
447 if (rt0 != NULL) {
448 if (rt_llroute(dst, rt0, &rt) != 0) {
449 m_freem(m);
450 return 0;
451 }
452 la = rt->rt_llinfo;
453 }
454 if (la == NULL) {
455 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
456 if (la != NULL)
984263bc
MD
457 rt = la->la_rt;
458 }
f23061d4 459 if (la == NULL || rt == NULL) {
984263bc 460 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
5fe66e68
JH
461 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
462 rt ? "rt" : "");
984263bc
MD
463 m_freem(m);
464 return (0);
465 }
466 sdl = SDL(rt->rt_gateway);
467 /*
468 * Check the address family and length is valid, the address
469 * is resolved; otherwise, try to resolve.
470 */
471 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
472 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
473 /*
474 * If entry has an expiry time and it is approaching,
475 * see if we need to send an ARP request within this
476 * arpt_down interval.
477 */
478 if ((rt->rt_expire != 0) &&
a916e847 479 (time_second + la->la_preempt > rt->rt_expire)) {
984263bc
MD
480 arprequest(ifp,
481 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
482 &SIN(dst)->sin_addr,
483 IF_LLADDR(ifp));
a916e847 484 la->la_preempt--;
f23061d4 485 }
984263bc
MD
486
487 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
488 return 1;
489 }
490 /*
491 * If ARP is disabled on this interface, stop.
492 * XXX
493 * Probably should not allocate empty llinfo struct if we are
494 * not going to be sending out an arp request.
495 */
496 if (ifp->if_flags & IFF_NOARP) {
497 m_freem(m);
498 return (0);
499 }
500 /*
501 * There is an arptab entry, but no ethernet address
502 * response yet. Replace the held mbuf with this
503 * latest one.
504 */
f23061d4 505 if (la->la_hold != NULL)
984263bc
MD
506 m_freem(la->la_hold);
507 la->la_hold = m;
03f869ef 508 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
984263bc
MD
509 rt->rt_flags &= ~RTF_REJECT;
510 if (la->la_asked == 0 || rt->rt_expire != time_second) {
511 rt->rt_expire = time_second;
512 if (la->la_asked++ < arp_maxtries) {
513 arprequest(ifp,
514 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
515 &SIN(dst)->sin_addr,
516 IF_LLADDR(ifp));
517 } else {
518 rt->rt_flags |= RTF_REJECT;
519 rt->rt_expire += arpt_down;
a916e847
JH
520 la->la_asked = 0;
521 la->la_preempt = arp_maxtries;
984263bc
MD
522 }
523
524 }
525 }
526 return (0);
527}
528
529/*
530 * Common length and type checks are done here,
531 * then the protocol-specific routine is called.
532 */
b76bed62 533static int
9eeaa8a9 534arpintr(struct netmsg *msg)
984263bc 535{
9eeaa8a9 536 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
2256ba69 537 struct arphdr *ar;
8bde602d 538 u_short ar_hrd;
984263bc 539
8bde602d
JH
540 if (m->m_len < sizeof(struct arphdr) &&
541 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
542 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
b76bed62 543 goto out2;
8bde602d
JH
544 }
545 ar = mtod(m, struct arphdr *);
546
547 ar_hrd = ntohs(ar->ar_hrd);
548 if (ar_hrd != ARPHRD_ETHER &&
549 ar_hrd != ARPHRD_IEEE802 &&
550 ar_hrd != ARPHRD_ARCNET) {
551 log(LOG_ERR,
552 "arp: unknown hardware address format (0x%2D)\n",
553 (unsigned char *)&ar->ar_hrd, "");
b76bed62 554 goto out1;
8bde602d 555 }
984263bc 556
8bde602d
JH
557 if (m->m_pkthdr.len < arphdr_len(ar) &&
558 (m = m_pullup(m, arphdr_len(ar))) == NULL) {
559 log(LOG_ERR, "arp: runt packet\n");
b76bed62 560 goto out1;
8bde602d
JH
561 }
562
563 switch (ntohs(ar->ar_pro)) {
984263bc 564#ifdef INET
8bde602d
JH
565 case ETHERTYPE_IP:
566 in_arpinput(m);
b76bed62 567 goto out2;
984263bc 568#endif
984263bc 569 }
b76bed62 570out1:
8bde602d 571 m_freem(m);
b76bed62 572out2:
6aad077d 573 /* msg was embedded in the mbuf, do not reply! */
b76bed62 574 return(EASYNC);
984263bc
MD
575}
576
577#ifdef INET
578/*
579 * ARP for Internet protocols on 10 Mb/s Ethernet.
580 * Algorithm is that given in RFC 826.
581 * In addition, a sanity check is performed on the sender
582 * protocol address, to catch impersonators.
583 * We no longer handle negotiations for use of trailer protocol:
584 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
585 * along with IP replies if we wanted trailers sent to us,
586 * and also sent them in response to IP replies.
587 * This allowed either end to announce the desire to receive
588 * trailer packets.
589 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
590 * but formerly didn't normally send requests.
591 */
592static int log_arp_wrong_iface = 1;
984263bc
MD
593SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
594 &log_arp_wrong_iface, 0,
595 "log arp packets arriving on the wrong interface");
596
ecdefdda
MD
597static void
598arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
599 boolean_t dologging)
600{
601 struct arphdr *ah = mtod(m, struct arphdr *);
602 struct ifnet *ifp = m->m_pkthdr.rcvif;
603 struct llinfo_arp *la;
604 struct sockaddr_dl *sdl;
605 struct rtentry *rt;
606 int cpu = mycpuid;
607
608 la = arplookup(saddr, create, FALSE);
609 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
610 struct in_addr isaddr = { saddr };
611
612 /* the following is not an error when doing bridging */
a8d45119 613 if (rt->rt_ifp != ifp) {
ecdefdda
MD
614 if (dologging && log_arp_wrong_iface && cpu == 0) {
615 log(LOG_ERR,
616 "arp: %s is on %s "
617 "but got reply from %*D on %s\n",
618 inet_ntoa(isaddr),
619 rt->rt_ifp->if_xname,
620 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
621 ifp->if_xname);
622 }
623 return;
624 }
625 if (sdl->sdl_alen &&
626 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
627 if (rt->rt_expire != 0) {
628 if (dologging && cpu == 0) {
629 log(LOG_INFO,
630 "arp: %s moved from %*D to %*D on %s\n",
631 inet_ntoa(isaddr),
632 ifp->if_addrlen, (u_char *)LLADDR(sdl),
633 ":", ifp->if_addrlen,
634 (u_char *)ar_sha(ah), ":",
635 ifp->if_xname);
636 }
637 } else {
638 if (dologging && cpu == 0) {
639 log(LOG_ERR,
640 "arp: %*D attempts to modify permanent entry for %s on %s\n",
641 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
642 inet_ntoa(isaddr), ifp->if_xname);
643 }
644 return;
645 }
646 }
647 /*
648 * sanity check for the address length.
649 * XXX this does not work for protocols with variable address
650 * length. -is
651 */
652 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
653 cpu == 0)
654 {
655 log(LOG_WARNING,
656 "arp from %*D: new addr len %d, was %d",
657 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
658 ah->ar_hln, sdl->sdl_alen);
659 }
660 if (ifp->if_addrlen != ah->ar_hln) {
661 if (dologging && cpu == 0) {
662 log(LOG_WARNING,
663 "arp from %*D: addr len: new %d, i/f %d (ignored)",
664 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
665 ah->ar_hln, ifp->if_addrlen);
666 }
667 return;
668 }
669 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
670 /*
671 * If we receive an arp from a token-ring station over
672 * a token-ring nic then try to save the source
673 * routing info.
674 */
675 if (ifp->if_type == IFT_ISO88025) {
676 struct iso88025_header *th =
677 (struct iso88025_header *)m->m_pkthdr.header;
678 struct iso88025_sockaddr_dl_data *trld =
679 SDL_ISO88025(sdl);
680 int rif_len;
681
682 rif_len = TR_RCF_RIFLEN(th->rcf);
683 if ((th->iso88025_shost[0] & TR_RII) &&
684 (rif_len > 2)) {
685 trld->trld_rcf = th->rcf;
686 trld->trld_rcf ^= htons(TR_RCF_DIR);
687 memcpy(trld->trld_route, th->rd, rif_len - 2);
688 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
689 /*
690 * Set up source routing information for
691 * reply packet (XXX)
692 */
693 m->m_data -= rif_len;
694 m->m_len += rif_len;
695 m->m_pkthdr.len += rif_len;
696 } else {
697 th->iso88025_shost[0] &= ~TR_RII;
698 trld->trld_rcf = 0;
699 }
700 m->m_data -= 8;
701 m->m_len += 8;
702 m->m_pkthdr.len += 8;
703 th->rcf = trld->trld_rcf;
704 }
705 if (rt->rt_expire != 0)
706 rt->rt_expire = time_second + arpt_keep;
707 rt->rt_flags &= ~RTF_REJECT;
708 la->la_asked = 0;
709 la->la_preempt = arp_maxtries;
710
711 /*
712 * This particular cpu might have been holding an mbuf
713 * pending ARP resolution. If so, transmit the mbuf now.
714 */
715 if (la->la_hold != NULL) {
716 m_adj(la->la_hold, sizeof(struct ether_header));
717 lwkt_serialize_enter(ifp->if_serializer);
718 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
719 lwkt_serialize_exit(ifp->if_serializer);
720 la->la_hold = NULL;
721 }
722 }
723}
724
725#ifdef SMP
726
727struct netmsg_arp_update {
728 struct lwkt_msg lmsg;
729 struct mbuf *m;
730 in_addr_t saddr;
731 boolean_t create;
732};
733
734static int arp_update_msghandler(struct lwkt_msg *lmsg);
735
736#endif
737
738/*
739 * Called from arpintr() - this routine is run from a single cpu.
740 */
984263bc 741static void
f23061d4 742in_arpinput(struct mbuf *m)
984263bc 743{
2256ba69
RG
744 struct arphdr *ah;
745 struct ifnet *ifp = m->m_pkthdr.rcvif;
984263bc
MD
746 struct ether_header *eh;
747 struct arc_header *arh;
5fe66e68 748 struct iso88025_header *th = (struct iso88025_header *)NULL;
2256ba69 749 struct rtentry *rt;
984263bc
MD
750 struct ifaddr *ifa;
751 struct in_ifaddr *ia;
984263bc
MD
752 struct sockaddr sa;
753 struct in_addr isaddr, itaddr, myaddr;
ecdefdda
MD
754#ifdef SMP
755 struct netmsg_arp_update msg;
756#endif
757 int op;
984263bc
MD
758 int req_len;
759
760 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
761 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
762 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
763 return;
764 }
765
766 ah = mtod(m, struct arphdr *);
767 op = ntohs(ah->ar_op);
f23061d4
JH
768 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
769 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
984263bc 770 /*
ed5bc924
SS
771 * Check both target and sender IP addresses:
772 *
773 * If we receive the packet on the interface owning the address,
774 * then accept the address.
775 *
776 * For a bridge, we accept the address if the receive interface and
777 * the interface owning the address are on the same bridge.
778 * (This will change slightly when we have clusters of interfaces).
984263bc 779 */
ac93838f 780 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
ed5bc924
SS
781 /* Skip all ia's which don't match */
782 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
783 continue;
784
785 if (ia->ia_ifp == ifp)
786 goto match;
787
ac93838f 788 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 789 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 790 goto match;
ac93838f
SS
791 }
792 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) {
ed5bc924
SS
793 /* Skip all ia's which don't match */
794 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
795 continue;
796
797 if (ia->ia_ifp == ifp)
798 goto match;
799
ac93838f 800 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 801 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 802 goto match;
ac93838f 803 }
984263bc
MD
804 /*
805 * No match, use the first inet address on the receive interface
806 * as a dummy address for the rest of the function.
807 */
ed5bc924 808 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
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
MD
821match:
822 myaddr = ia->ia_addr.sin_addr;
823 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
824 m_freem(m); /* it's from me, ignore it. */
825 return;
826 }
c401f0fd 827 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
984263bc
MD
828 log(LOG_ERR,
829 "arp: link address is broadcast for IP address %s!\n",
830 inet_ntoa(isaddr));
831 m_freem(m);
832 return;
833 }
c3749f74 834 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
984263bc
MD
835 log(LOG_ERR,
836 "arp: %*D is using my IP address %s!\n",
837 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
838 inet_ntoa(isaddr));
839 itaddr = myaddr;
840 goto reply;
841 }
ecdefdda
MD
842#ifdef SMP
843 lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0,
844 lwkt_cmd_func(arp_update_msghandler), lwkt_cmd_op_none);
845 msg.m = m;
846 msg.saddr = isaddr.s_addr;
847 msg.create = (itaddr.s_addr == myaddr.s_addr);
848 lwkt_domsg(rtable_portfn(0), &msg.lmsg);
849#endif
850 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
851 TRUE);
984263bc
MD
852reply:
853 if (op != ARPOP_REQUEST) {
854 m_freem(m);
855 return;
856 }
857 if (itaddr.s_addr == myaddr.s_addr) {
858 /* I am the target */
f23061d4
JH
859 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
860 memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
984263bc 861 } else {
ecdefdda
MD
862 struct llinfo_arp *la;
863
f23061d4 864 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
984263bc
MD
865 if (la == NULL) {
866 struct sockaddr_in sin;
867
868 if (!arp_proxyall) {
869 m_freem(m);
870 return;
871 }
872
873 bzero(&sin, sizeof sin);
874 sin.sin_family = AF_INET;
875 sin.sin_len = sizeof sin;
876 sin.sin_addr = itaddr;
877
f3ed2586 878 rt = rtpurelookup((struct sockaddr *)&sin);
0c3c561c 879 if (rt == NULL) {
984263bc
MD
880 m_freem(m);
881 return;
882 }
be23faf1 883 --rt->rt_refcnt;
984263bc
MD
884 /*
885 * Don't send proxies for nodes on the same interface
886 * as this one came out of, or we'll get into a fight
887 * over who claims what Ether address.
888 */
889 if (rt->rt_ifp == ifp) {
984263bc
MD
890 m_freem(m);
891 return;
892 }
f23061d4
JH
893 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
894 memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
984263bc 895#ifdef DEBUG_PROXY
0c3c561c 896 printf("arp: proxying for %s\n", inet_ntoa(itaddr));
984263bc
MD
897#endif
898 } else {
ecdefdda
MD
899 struct sockaddr_dl *sdl;
900
984263bc 901 rt = la->la_rt;
f23061d4 902 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
984263bc 903 sdl = SDL(rt->rt_gateway);
f23061d4 904 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
984263bc
MD
905 }
906 }
907
f23061d4
JH
908 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
909 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
984263bc
MD
910 ah->ar_op = htons(ARPOP_REPLY);
911 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
912 switch (ifp->if_type) {
913 case IFT_ARCNET:
914 arh = (struct arc_header *)sa.sa_data;
915 arh->arc_dhost = *ar_tha(ah);
916 arh->arc_type = ARCTYPE_ARP;
917 break;
984263bc
MD
918 case IFT_ISO88025:
919 /* Re-arrange the source/dest address */
920 memcpy(th->iso88025_dhost, th->iso88025_shost,
0c3c561c 921 sizeof th->iso88025_dhost);
984263bc 922 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
0c3c561c 923 sizeof th->iso88025_shost);
984263bc
MD
924 /* Set the source routing bit if neccesary */
925 if (th->iso88025_dhost[0] & TR_RII) {
926 th->iso88025_dhost[0] &= ~TR_RII;
927 if (TR_RCF_RIFLEN(th->rcf) > 2)
928 th->iso88025_shost[0] |= TR_RII;
929 }
930 /* Copy the addresses, ac and fc into sa_data */
931 memcpy(sa.sa_data, th->iso88025_dhost,
0c3c561c
JH
932 (sizeof th->iso88025_dhost) * 2);
933 sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC;
934 sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME;
984263bc
MD
935 break;
936 case IFT_ETHER:
937 case IFT_FDDI:
938 /*
939 * May not be correct for types not explictly
940 * listed, but it is our best guess.
941 */
942 default:
943 eh = (struct ether_header *)sa.sa_data;
0c3c561c 944 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
984263bc
MD
945 eh->ether_type = htons(ETHERTYPE_ARP);
946 break;
947 }
948 sa.sa_family = AF_UNSPEC;
0c3c561c 949 sa.sa_len = sizeof sa;
78195a76 950 lwkt_serialize_enter(ifp->if_serializer);
984263bc 951 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
78195a76 952 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
953 return;
954}
ecdefdda
MD
955
956#ifdef SMP
957
958static
959int
960arp_update_msghandler(struct lwkt_msg *lmsg)
961{
962 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)lmsg;
963 int nextcpu;
964
965 arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
966
967 nextcpu = mycpuid + 1;
968 if (nextcpu < ncpus) {
969 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
970 } else {
971 lwkt_replymsg(&msg->lmsg, 0);
972 }
973 return (0);
974}
975
976#endif
977
984263bc
MD
978#endif
979
980/*
03f869ef
MD
981 * Free an arp entry. If the arp entry is actively referenced or represents
982 * a static entry we only clear it back to an unresolved state, otherwise
983 * we destroy the entry entirely.
984 *
985 * Note that static entries are created when route add ... -interface is used
986 * to create an interface route to a (direct) destination.
984263bc
MD
987 */
988static void
5fe66e68 989arptfree(struct llinfo_arp *la)
984263bc 990{
2256ba69
RG
991 struct rtentry *rt = la->la_rt;
992 struct sockaddr_dl *sdl;
f23061d4 993
03f869ef 994 if (rt == NULL)
984263bc 995 panic("arptfree");
03f869ef 996 sdl = SDL(rt->rt_gateway);
5fe66e68
JH
997 if (sdl != NULL &&
998 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
999 (rt->rt_flags & RTF_STATIC))) {
984263bc
MD
1000 sdl->sdl_alen = 0;
1001 la->la_preempt = la->la_asked = 0;
1002 rt->rt_flags &= ~RTF_REJECT;
1003 return;
1004 }
f23061d4 1005 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
984263bc 1006}
0c3c561c 1007
984263bc
MD
1008/*
1009 * Lookup or enter a new address in arptab.
1010 */
1011static struct llinfo_arp *
f23061d4 1012arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
984263bc 1013{
2256ba69 1014 struct rtentry *rt;
f3ed2586 1015 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
f23061d4 1016 const char *why = NULL;
984263bc
MD
1017
1018 sin.sin_addr.s_addr = addr;
1019 sin.sin_other = proxy ? SIN_PROXY : 0;
f3ed2586
JH
1020 if (create)
1021 rt = rtlookup((struct sockaddr *)&sin);
1022 else
1023 rt = rtpurelookup((struct sockaddr *)&sin);
f23061d4
JH
1024 if (rt == NULL)
1025 return (NULL);
984263bc
MD
1026 rt->rt_refcnt--;
1027
1028 if (rt->rt_flags & RTF_GATEWAY)
1029 why = "host is not on local network";
f23061d4 1030 else if (!(rt->rt_flags & RTF_LLINFO))
984263bc
MD
1031 why = "could not allocate llinfo";
1032 else if (rt->rt_gateway->sa_family != AF_LINK)
1033 why = "gateway route is not ours";
1034
8fd3e627
MD
1035 if (why) {
1036 if (create) {
1037 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1038 inet_ntoa(sin.sin_addr), why);
1039 }
f23061d4
JH
1040 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1041 /* No references to this route. Purge it. */
1042 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
be23faf1 1043 rt_mask(rt), rt->rt_flags, NULL);
8fd3e627 1044 }
f23061d4 1045 return (NULL);
984263bc 1046 }
f23061d4 1047 return (rt->rt_llinfo);
984263bc
MD
1048}
1049
1050void
f23061d4 1051arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
984263bc 1052{
f23061d4
JH
1053 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1054 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1055 IF_LLADDR(ifp));
984263bc
MD
1056 ifa->ifa_rtrequest = arp_rtrequest;
1057 ifa->ifa_flags |= RTF_CLONING;
1058}
1059
1060static void
1061arp_init(void)
1062{
ecdefdda
MD
1063 int cpu;
1064
1065 for (cpu = 0; cpu < ncpus2; cpu++)
1066 LIST_INIT(&llinfo_arp_list[cpu]);
bf82f9b7 1067 netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
984263bc
MD
1068}
1069
1070SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);