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