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