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