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