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