i82586 support file for Garrett Wollmans ie driver
[unix-history] / sys / netinet / if_ether.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3 * 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 7.13 (Berkeley) 10/31/90
34 */
35
36/*
37 * Ethernet address resolution protocol.
38 * TODO:
39 * run at splnet (add ARP protocol intr.)
40 * link entries onto hash chains, keep free list
41 * add "inuse/lock" bit (or ref. count) along with valid bit
42 */
43
44#include "param.h"
45#include "systm.h"
46#include "malloc.h"
47#include "mbuf.h"
48#include "socket.h"
49#include "time.h"
50#include "kernel.h"
51#include "errno.h"
52#include "ioctl.h"
53#include "syslog.h"
54
55#include "../net/if.h"
56#include "in.h"
57#include "in_systm.h"
58#include "in_var.h"
59#include "ip.h"
60#include "if_ether.h"
61
62#ifdef GATEWAY
63#define ARPTAB_BSIZ 16 /* bucket size */
64#define ARPTAB_NB 37 /* number of buckets */
65#else
66#define ARPTAB_BSIZ 9 /* bucket size */
67#define ARPTAB_NB 19 /* number of buckets */
68#endif
69#define ARPTAB_SIZE (ARPTAB_BSIZ * ARPTAB_NB)
70struct arptab arptab[ARPTAB_SIZE];
71int arptab_size = ARPTAB_SIZE; /* for arp command */
72
73/*
74 * ARP trailer negotiation. Trailer protocol is not IP specific,
75 * but ARP request/response use IP addresses.
76 */
77#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
78
79#define ARPTAB_HASH(a) \
80 ((u_long)(a) % ARPTAB_NB)
81
82#define ARPTAB_LOOK(at,addr) { \
83 register n; \
84 at = &arptab[ARPTAB_HASH(addr) * ARPTAB_BSIZ]; \
85 for (n = 0 ; n < ARPTAB_BSIZ ; n++,at++) \
86 if (at->at_iaddr.s_addr == addr) \
87 break; \
88 if (n >= ARPTAB_BSIZ) \
89 at = 0; \
90}
91
92/* timer values */
93#define ARPT_AGE (60*1) /* aging timer, 1 min. */
94#define ARPT_KILLC 20 /* kill completed entry in 20 mins. */
95#define ARPT_KILLI 3 /* kill incomplete entry in 3 minutes */
96
97extern struct ifnet loif;
98
99/*
100 * Timeout routine. Age arp_tab entries once a minute.
101 */
102arptimer()
103{
104 register struct arptab *at;
105 register i;
106
107 timeout(arptimer, (caddr_t)0, ARPT_AGE * hz);
108 at = &arptab[0];
109 for (i = 0; i < ARPTAB_SIZE; i++, at++) {
110 if (at->at_flags == 0 || (at->at_flags & ATF_PERM))
111 continue;
112 if (++at->at_timer < ((at->at_flags&ATF_COM) ?
113 ARPT_KILLC : ARPT_KILLI))
114 continue;
115 /* timer has expired, clear entry */
116 arptfree(at);
117 }
118}
119
120/*
121 * Broadcast an ARP packet, asking who has addr on interface ac.
122 */
123arpwhohas(ac, addr)
124 register struct arpcom *ac;
125 struct in_addr *addr;
126{
127 register struct mbuf *m;
128 register struct ether_header *eh;
129 register struct ether_arp *ea;
130 struct sockaddr sa;
131
132 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
133 return;
134 m->m_len = sizeof(*ea);
135 m->m_pkthdr.len = sizeof(*ea);
136 MH_ALIGN(m, sizeof(*ea));
137 ea = mtod(m, struct ether_arp *);
138 eh = (struct ether_header *)sa.sa_data;
139 bzero((caddr_t)ea, sizeof (*ea));
140 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
141 sizeof(eh->ether_dhost));
142 eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */
143 ea->arp_hrd = htons(ARPHRD_ETHER);
144 ea->arp_pro = htons(ETHERTYPE_IP);
145 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
146 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
147 ea->arp_op = htons(ARPOP_REQUEST);
148 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
149 sizeof(ea->arp_sha));
150 bcopy((caddr_t)&ac->ac_ipaddr, (caddr_t)ea->arp_spa,
151 sizeof(ea->arp_spa));
152 bcopy((caddr_t)addr, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
153 sa.sa_family = AF_UNSPEC;
154 sa.sa_len = sizeof(sa);
155 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
156}
157
158int useloopback = 1; /* use loopback interface for local traffic */
159
160/*
161 * Resolve an IP address into an ethernet address. If success,
162 * desten is filled in. If there is no entry in arptab,
163 * set one up and broadcast a request for the IP address.
164 * Hold onto this mbuf and resend it once the address
165 * is finally resolved. A return value of 1 indicates
166 * that desten has been filled in and the packet should be sent
167 * normally; a 0 return indicates that the packet has been
168 * taken over here, either now or for later transmission.
169 *
170 * We do some (conservative) locking here at splimp, since
171 * arptab is also altered from input interrupt service (ecintr/ilintr
172 * calls arpinput when ETHERTYPE_ARP packets come in).
173 */
174arpresolve(ac, m, destip, desten, usetrailers)
175 register struct arpcom *ac;
176 struct mbuf *m;
177 register struct in_addr *destip;
178 register u_char *desten;
179 int *usetrailers;
180{
181 register struct arptab *at;
182 struct sockaddr_in sin;
183 register struct in_ifaddr *ia;
184 u_long lna;
185 int s;
186
187 *usetrailers = 0;
188 if (m->m_flags & M_BCAST) { /* broadcast */
189 bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
190 sizeof(etherbroadcastaddr));
191 return (1);
192 }
193 lna = in_lnaof(*destip);
194 /* if for us, use software loopback driver if up */
195 for (ia = in_ifaddr; ia; ia = ia->ia_next)
196 if ((ia->ia_ifp == &ac->ac_if) &&
197 (destip->s_addr == ia->ia_addr.sin_addr.s_addr)) {
198 /*
199 * This test used to be
200 * if (loif.if_flags & IFF_UP)
201 * It allowed local traffic to be forced
202 * through the hardware by configuring the loopback down.
203 * However, it causes problems during network configuration
204 * for boards that can't receive packets they send.
205 * It is now necessary to clear "useloopback"
206 * to force traffic out to the hardware.
207 */
208 if (useloopback) {
209 sin.sin_family = AF_INET;
210 sin.sin_addr = *destip;
211 (void) looutput(&loif, m, (struct sockaddr *)&sin, 0);
212 /*
213 * The packet has already been sent and freed.
214 */
215 return (0);
216 } else {
217 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten,
218 sizeof(ac->ac_enaddr));
219 return (1);
220 }
221 }
222 s = splimp();
223 ARPTAB_LOOK(at, destip->s_addr);
224 if (at == 0) { /* not found */
225 if (ac->ac_if.if_flags & IFF_NOARP) {
226 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 3);
227 desten[3] = (lna >> 16) & 0x7f;
228 desten[4] = (lna >> 8) & 0xff;
229 desten[5] = lna & 0xff;
230 splx(s);
231 return (1);
232 } else {
233 at = arptnew(destip);
234 if (at == 0)
235 panic("arpresolve: no free entry");
236 at->at_hold = m;
237 arpwhohas(ac, destip);
238 splx(s);
239 return (0);
240 }
241 }
242 at->at_timer = 0; /* restart the timer */
243 if (at->at_flags & ATF_COM) { /* entry IS complete */
47110967
RG
244
245 *(int *) desten = *(int *) at->at_enaddr;
246 ((short *) desten)[2] = ((short *) at->at_enaddr)[2];
247
15637ed4
RG
248 if (at->at_flags & ATF_USETRAILERS)
249 *usetrailers = 1;
250 splx(s);
251 return (1);
252 }
253 /*
254 * There is an arptab entry, but no ethernet address
255 * response yet. Replace the held mbuf with this
256 * latest one.
257 */
258 if (at->at_hold)
259 m_freem(at->at_hold);
260 at->at_hold = m;
261 arpwhohas(ac, destip); /* ask again */
262 splx(s);
263 return (0);
264}
265
266/*
267 * Called from 10 Mb/s Ethernet interrupt handlers
268 * when ether packet type ETHERTYPE_ARP
269 * is received. Common length and type checks are done here,
270 * then the protocol-specific routine is called.
271 */
272arpinput(ac, m)
273 struct arpcom *ac;
274 struct mbuf *m;
275{
276 register struct arphdr *ar;
277
278 if (ac->ac_if.if_flags & IFF_NOARP)
279 goto out;
280 if (m->m_len < sizeof(struct arphdr))
281 goto out;
282 ar = mtod(m, struct arphdr *);
283 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
284 goto out;
285 if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
286 goto out;
287
288 switch (ntohs(ar->ar_pro)) {
289
290 case ETHERTYPE_IP:
291 case ETHERTYPE_IPTRAILERS:
292 in_arpinput(ac, m);
293 return;
294
295 default:
296 break;
297 }
298out:
299 m_freem(m);
300}
301
302/*
303 * ARP for Internet protocols on 10 Mb/s Ethernet.
304 * Algorithm is that given in RFC 826.
305 * In addition, a sanity check is performed on the sender
306 * protocol address, to catch impersonators.
307 * We also handle negotiations for use of trailer protocol:
308 * ARP replies for protocol type ETHERTYPE_TRAIL are sent
309 * along with IP replies if we want trailers sent to us,
310 * and also send them in response to IP replies.
311 * This allows either end to announce the desire to receive
312 * trailer packets.
313 * We reply to requests for ETHERTYPE_TRAIL protocol as well,
314 * but don't normally send requests.
315 */
316in_arpinput(ac, m)
317 register struct arpcom *ac;
318 struct mbuf *m;
319{
320 register struct ether_arp *ea;
321 struct ether_header *eh;
322 register struct arptab *at; /* same as "merge" flag */
323 register struct in_ifaddr *ia;
324 struct in_ifaddr *maybe_ia = 0;
325 struct mbuf *mcopy = 0;
326 struct sockaddr_in sin;
327 struct sockaddr sa;
328 struct in_addr isaddr, itaddr, myaddr;
329 int proto, op, s, completed = 0;
330
331 ea = mtod(m, struct ether_arp *);
332 proto = ntohs(ea->arp_pro);
333 op = ntohs(ea->arp_op);
334 bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
335 bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
336 for (ia = in_ifaddr; ia; ia = ia->ia_next)
337 if (ia->ia_ifp == &ac->ac_if) {
338 maybe_ia = ia;
339 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
340 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
341 break;
342 }
343 if (maybe_ia == 0)
344 goto out;
345 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
346 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
347 sizeof (ea->arp_sha)))
348 goto out; /* it's from me, ignore it. */
349 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
350 sizeof (ea->arp_sha))) {
351 log(LOG_ERR,
352 "arp: ether address is broadcast for IP address %x!\n",
353 ntohl(isaddr.s_addr));
354 goto out;
355 }
356 if (isaddr.s_addr == myaddr.s_addr) {
357 log(LOG_ERR,
358 "duplicate IP address %x!! sent from ethernet address: %s\n",
359 ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
360 itaddr = myaddr;
361 if (op == ARPOP_REQUEST)
362 goto reply;
363 goto out;
364 }
365 s = splimp();
366 ARPTAB_LOOK(at, isaddr.s_addr);
367 if (at) {
368 bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
369 sizeof(ea->arp_sha));
370 if ((at->at_flags & ATF_COM) == 0)
371 completed = 1;
372 at->at_flags |= ATF_COM;
373 if (at->at_hold) {
374 sin.sin_family = AF_INET;
375 sin.sin_addr = isaddr;
376 (*ac->ac_if.if_output)(&ac->ac_if, at->at_hold,
377 (struct sockaddr *)&sin, (struct rtentry *)0);
378 at->at_hold = 0;
379 }
380 }
381 if (at == 0 && itaddr.s_addr == myaddr.s_addr) {
382 /* ensure we have a table entry */
383 if (at = arptnew(&isaddr)) {
384 bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
385 sizeof(ea->arp_sha));
386 completed = 1;
387 at->at_flags |= ATF_COM;
388 }
389 }
390 splx(s);
391reply:
392 switch (proto) {
393
394 case ETHERTYPE_IPTRAILERS:
395 /* partner says trailers are OK */
396 if (at)
397 at->at_flags |= ATF_USETRAILERS;
398 /*
399 * Reply to request iff we want trailers.
400 */
401 if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS)
402 goto out;
403 break;
404
405 case ETHERTYPE_IP:
406 /*
407 * Reply if this is an IP request,
408 * or if we want to send a trailer response.
409 * Send the latter only to the IP response
410 * that completes the current ARP entry.
411 */
412 if (op != ARPOP_REQUEST &&
413 (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS))
414 goto out;
415 }
416 if (itaddr.s_addr == myaddr.s_addr) {
417 /* I am the target */
418 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
419 sizeof(ea->arp_sha));
420 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
421 sizeof(ea->arp_sha));
422 } else {
423 ARPTAB_LOOK(at, itaddr.s_addr);
424 if (at == NULL || (at->at_flags & ATF_PUBL) == 0)
425 goto out;
426 bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
427 sizeof(ea->arp_sha));
428 bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha,
429 sizeof(ea->arp_sha));
430 }
431
432 bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa,
433 sizeof(ea->arp_spa));
434 bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa,
435 sizeof(ea->arp_spa));
436 ea->arp_op = htons(ARPOP_REPLY);
437 /*
438 * If incoming packet was an IP reply,
439 * we are sending a reply for type IPTRAILERS.
440 * If we are sending a reply for type IP
441 * and we want to receive trailers,
442 * send a trailer reply as well.
443 */
444 if (op == ARPOP_REPLY)
445 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
446 else if (proto == ETHERTYPE_IP &&
447 (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0)
448 mcopy = m_copy(m, 0, (int)M_COPYALL);
449 eh = (struct ether_header *)sa.sa_data;
450 bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
451 sizeof(eh->ether_dhost));
452 eh->ether_type = ETHERTYPE_ARP;
453 sa.sa_family = AF_UNSPEC;
454 sa.sa_len = sizeof(sa);
455 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
456 if (mcopy) {
457 ea = mtod(mcopy, struct ether_arp *);
458 ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
459 (*ac->ac_if.if_output)(&ac->ac_if,
460 mcopy, &sa, (struct rtentry *)0);
461 }
462 return;
463out:
464 m_freem(m);
465 return;
466}
467
468/*
469 * Free an arptab entry.
470 */
471arptfree(at)
472 register struct arptab *at;
473{
474 int s = splimp();
475
476 if (at->at_hold)
477 m_freem(at->at_hold);
478 at->at_hold = 0;
479 at->at_timer = at->at_flags = 0;
480 at->at_iaddr.s_addr = 0;
481 splx(s);
482}
483
484/*
485 * Enter a new address in arptab, pushing out the oldest entry
486 * from the bucket if there is no room.
487 * This always succeeds since no bucket can be completely filled
488 * with permanent entries (except from arpioctl when testing whether
489 * another permanent entry will fit).
490 * MUST BE CALLED AT SPLIMP.
491 */
492struct arptab *
493arptnew(addr)
494 struct in_addr *addr;
495{
496 register n;
497 int oldest = -1;
498 register struct arptab *at, *ato = NULL;
499 static int first = 1;
500
501 if (first) {
502 first = 0;
503 timeout(arptimer, (caddr_t)0, hz);
504 }
505 at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ];
506 for (n = 0; n < ARPTAB_BSIZ; n++,at++) {
507 if (at->at_flags == 0)
508 goto out; /* found an empty entry */
509 if (at->at_flags & ATF_PERM)
510 continue;
511 if ((int) at->at_timer > oldest) {
512 oldest = at->at_timer;
513 ato = at;
514 }
515 }
516 if (ato == NULL)
517 return (NULL);
518 at = ato;
519 arptfree(at);
520out:
521 at->at_iaddr = *addr;
522 at->at_flags = ATF_INUSE;
523 return (at);
524}
525
526arpioctl(cmd, data)
527 int cmd;
528 caddr_t data;
529{
530 register struct arpreq *ar = (struct arpreq *)data;
531 register struct arptab *at;
532 register struct sockaddr_in *sin;
533 int s;
534
535 sin = (struct sockaddr_in *)&ar->arp_ha;
536#if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN
537 if (sin->sin_family == 0 && sin->sin_len < 16)
538 sin->sin_family = sin->sin_len;
539#endif
540 sin->sin_len = sizeof(ar->arp_ha);
541 sin = (struct sockaddr_in *)&ar->arp_pa;
542#if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN
543 if (sin->sin_family == 0 && sin->sin_len < 16)
544 sin->sin_family = sin->sin_len;
545#endif
546 sin->sin_len = sizeof(ar->arp_pa);
547 if (ar->arp_pa.sa_family != AF_INET ||
548 ar->arp_ha.sa_family != AF_UNSPEC)
549 return (EAFNOSUPPORT);
550 s = splimp();
551 ARPTAB_LOOK(at, sin->sin_addr.s_addr);
552 if (at == NULL) { /* not found */
553 if (cmd != SIOCSARP) {
554 splx(s);
555 return (ENXIO);
556 }
557 if (ifa_ifwithnet(&ar->arp_pa) == NULL) {
558 splx(s);
559 return (ENETUNREACH);
560 }
561 }
562 switch (cmd) {
563
564 case SIOCSARP: /* set entry */
565 if (at == NULL) {
566 at = arptnew(&sin->sin_addr);
567 if (at == NULL) {
568 splx(s);
569 return (EADDRNOTAVAIL);
570 }
571 if (ar->arp_flags & ATF_PERM) {
572 /* never make all entries in a bucket permanent */
573 register struct arptab *tat;
574
575 /* try to re-allocate */
576 tat = arptnew(&sin->sin_addr);
577 if (tat == NULL) {
578 arptfree(at);
579 splx(s);
580 return (EADDRNOTAVAIL);
581 }
582 arptfree(tat);
583 }
584 }
585 bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr,
586 sizeof(at->at_enaddr));
587 at->at_flags = ATF_COM | ATF_INUSE |
588 (ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS));
589 at->at_timer = 0;
590 break;
591
592 case SIOCDARP: /* delete entry */
593 arptfree(at);
594 break;
595
596 case SIOCGARP: /* get entry */
597 case OSIOCGARP:
598 bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data,
599 sizeof(at->at_enaddr));
600#ifdef COMPAT_43
601 if (cmd == OSIOCGARP)
602 *(u_short *)&ar->arp_ha = ar->arp_ha.sa_family;
603#endif
604 ar->arp_flags = at->at_flags;
605 break;
606 }
607 splx(s);
608 return (0);
609}