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