if_ethersubr.c revision 1.3 1 /*
2 * Copyright (c) 1982, 1989 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 * from: @(#)if_ethersubr.c 7.13 (Berkeley) 4/20/91
34 * $Id: if_ethersubr.c,v 1.3 1993/12/06 04:50:19 hpeyerl Exp $
35 */
36
37 #include "param.h"
38 #include "systm.h"
39 #include "kernel.h"
40 #include "malloc.h"
41 #include "mbuf.h"
42 #include "protosw.h"
43 #include "socket.h"
44 #include "ioctl.h"
45 #include "errno.h"
46 #include "syslog.h"
47
48 #include "if.h"
49 #include "netisr.h"
50 #include "route.h"
51 #include "if_llc.h"
52 #include "if_dl.h"
53
54 #include "machine/mtpr.h"
55
56 #ifdef INET
57 #include "../netinet/in.h"
58 #include "../netinet/in_var.h"
59 #endif
60 #include "../netinet/if_ether.h"
61
62 #ifdef NS
63 #include "../netns/ns.h"
64 #include "../netns/ns_if.h"
65 #endif
66
67 #ifdef ISO
68 #include "../netiso/argo_debug.h"
69 #include "../netiso/iso.h"
70 #include "../netiso/iso_var.h"
71 #include "../netiso/iso_snpac.h"
72 #endif
73
74 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
75 extern struct ifnet loif;
76
77 /*
78 * Ethernet output routine.
79 * Encapsulate a packet of type family for the local net.
80 * Use trailer local net encapsulation if enough data in first
81 * packet leaves a multiple of 512 bytes of data in remainder.
82 * Assumes that ifp is actually pointer to arpcom structure.
83 */
84 ether_output(ifp, m0, dst, rt)
85 register struct ifnet *ifp;
86 struct mbuf *m0;
87 struct sockaddr *dst;
88 struct rtentry *rt;
89 {
90 short type;
91 int s, error = 0;
92 u_char edst[6];
93 struct in_addr idst;
94 register struct mbuf *m = m0;
95 struct mbuf *mcopy = (struct mbuf *)0;
96 register struct ether_header *eh;
97 int usetrailers, off, len = m->m_pkthdr.len;
98 #define ac ((struct arpcom *)ifp)
99
100 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
101 error = ENETDOWN;
102 goto bad;
103 }
104 ifp->if_lastchange = time;
105 switch (dst->sa_family) {
106
107 #ifdef INET
108 case AF_INET:
109 idst = ((struct sockaddr_in *)dst)->sin_addr;
110 if (!arpresolve(ac, m, &idst, edst, &usetrailers))
111 return (0); /* if not yet resolved */
112 /* If broadcasting on a simplex interface, loopback a copy */
113 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
114 mcopy = m_copy(m, 0, (int)M_COPYALL);
115 off = m->m_pkthdr.len - m->m_len;
116 if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
117 (m->m_flags & M_EXT) == 0 &&
118 m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
119 type = ETHERTYPE_TRAIL + (off>>9);
120 m->m_data -= 2 * sizeof (u_short);
121 m->m_len += 2 * sizeof (u_short);
122 len += 2 * sizeof (u_short);
123 *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
124 *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
125 goto gottrailertype;
126 }
127 type = ETHERTYPE_IP;
128 goto gottype;
129 #endif
130 #ifdef NS
131 case AF_NS:
132 type = ETHERTYPE_NS;
133 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
134 (caddr_t)edst, sizeof (edst));
135 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
136 return (looutput(ifp, m, dst, rt));
137 /* If broadcasting on a simplex interface, loopback a copy */
138 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
139 mcopy = m_copy(m, 0, (int)M_COPYALL);
140 goto gottype;
141 #endif
142 #ifdef ISO
143 case AF_ISO: {
144 int snpalen;
145 struct llc *l;
146
147 iso_again:
148 if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
149 if (rt->rt_flags & RTF_GATEWAY) {
150 if (rt->rt_llinfo) {
151 rt = (struct rtentry *)rt->rt_llinfo;
152 goto iso_again;
153 }
154 } else {
155 register struct sockaddr_dl *sdl =
156 (struct sockaddr_dl *)rt->rt_gateway;
157 if (sdl && sdl->sdl_family == AF_LINK
158 && sdl->sdl_alen > 0) {
159 bcopy(LLADDR(sdl), (char *)edst,
160 sizeof(edst));
161 goto iso_resolved;
162 }
163 }
164 }
165 if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
166 (char *)edst, &snpalen)) > 0)
167 goto bad; /* Not Resolved */
168 iso_resolved:
169 /* If broadcasting on a simplex interface, loopback a copy */
170 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
171 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
172 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
173 if (mcopy) {
174 eh = mtod(mcopy, struct ether_header *);
175 bcopy((caddr_t)edst,
176 (caddr_t)eh->ether_dhost, sizeof (edst));
177 bcopy((caddr_t)ac->ac_enaddr,
178 (caddr_t)eh->ether_shost, sizeof (edst));
179 }
180 }
181 M_PREPEND(m, 3, M_DONTWAIT);
182 if (m == NULL)
183 return (0);
184 type = m->m_pkthdr.len;
185 l = mtod(m, struct llc *);
186 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
187 l->llc_control = LLC_UI;
188 len += 3;
189 IFDEBUG(D_ETHER)
190 int i;
191 printf("unoutput: sending pkt to: ");
192 for (i=0; i<6; i++)
193 printf("%x ", edst[i] & 0xff);
194 printf("\n");
195 ENDDEBUG
196 }
197 goto gottype;
198 #endif ISO
199 #ifdef RMP
200 case AF_RMP:
201 /*
202 * This is IEEE 802.3 -- the Ethernet `type' field is
203 * really a `length' field.
204 */
205 type = m->m_len;
206 bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
207 break;
208 #endif
209
210 case AF_UNSPEC:
211 eh = (struct ether_header *)dst->sa_data;
212 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
213 type = eh->ether_type;
214 goto gottype;
215
216 default:
217 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
218 dst->sa_family);
219 error = EAFNOSUPPORT;
220 goto bad;
221 }
222
223 gottrailertype:
224 /*
225 * Packet to be sent as trailer: move first packet
226 * (control information) to end of chain.
227 */
228 while (m->m_next)
229 m = m->m_next;
230 m->m_next = m0;
231 m = m0->m_next;
232 m0->m_next = 0;
233
234 gottype:
235 if (mcopy)
236 (void) looutput(ifp, mcopy, dst, rt);
237 /*
238 * Add local net header. If no space in first mbuf,
239 * allocate another.
240 */
241 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
242 if (m == 0) {
243 error = ENOBUFS;
244 goto bad;
245 }
246 eh = mtod(m, struct ether_header *);
247 type = htons((u_short)type);
248 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
249 sizeof(eh->ether_type));
250 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
251 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
252 sizeof(eh->ether_shost));
253 s = splimp();
254 /*
255 * Queue message on interface, and start output if interface
256 * not yet active.
257 */
258 if (IF_QFULL(&ifp->if_snd)) {
259 IF_DROP(&ifp->if_snd);
260 splx(s);
261 error = ENOBUFS;
262 goto bad;
263 }
264 IF_ENQUEUE(&ifp->if_snd, m);
265 if ((ifp->if_flags & IFF_OACTIVE) == 0)
266 (*ifp->if_start)(ifp);
267 splx(s);
268 ifp->if_obytes += len + sizeof (struct ether_header);
269 #ifdef MULTICAST
270 if (m->m_flags & M_MCAST)
271 #else
272 if (edst[0] & 1)
273 #endif
274 ifp->if_omcasts++;
275 return (error);
276
277 bad:
278 if (m)
279 m_freem(m);
280 return (error);
281 }
282
283 /*
284 * Process a received Ethernet packet;
285 * the packet is in the mbuf chain m without
286 * the ether header, which is provided separately.
287 */
288 ether_input(ifp, eh, m)
289 struct ifnet *ifp;
290 register struct ether_header *eh;
291 struct mbuf *m;
292 {
293 register struct ifqueue *inq;
294 register struct llc *l;
295 int s;
296
297 ifp->if_lastchange = time;
298 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
299 #ifdef MULTICAST
300 if (eh->ether_dhost[0] & 1) {
301 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
302 sizeof(etherbroadcastaddr)) == 0)
303 m->m_flags |= M_BCAST;
304 else
305 m->m_flags |= M_MCAST;
306 }
307 #else
308 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
309 sizeof(etherbroadcastaddr)) == 0)
310 m->m_flags |= M_BCAST;
311 else if (eh->ether_dhost[0] & 1)
312 m->m_flags |= M_MCAST;
313 #endif
314 if (m->m_flags & (M_BCAST|M_MCAST))
315 ifp->if_imcasts++;
316
317 switch (eh->ether_type) {
318 #ifdef INET
319 case ETHERTYPE_IP:
320 schednetisr(NETISR_IP);
321 inq = &ipintrq;
322 break;
323
324 case ETHERTYPE_ARP:
325 arpinput((struct arpcom *)ifp, m);
326 return;
327 #endif
328 #ifdef NS
329 case ETHERTYPE_NS:
330 schednetisr(NETISR_NS);
331 inq = &nsintrq;
332 break;
333
334 #endif
335 default:
336 #ifdef ISO
337 if (eh->ether_type > ETHERMTU)
338 goto dropanyway;
339 l = mtod(m, struct llc *);
340 switch (l->llc_control) {
341 case LLC_UI:
342 /* LLC_UI_P forbidden in class 1 service */
343 if ((l->llc_dsap == LLC_ISO_LSAP) &&
344 (l->llc_ssap == LLC_ISO_LSAP)) {
345 /* LSAP for ISO */
346 if (m->m_pkthdr.len > eh->ether_type)
347 m_adj(m, eh->ether_type - m->m_pkthdr.len);
348 m->m_data += 3; /* XXX */
349 m->m_len -= 3; /* XXX */
350 m->m_pkthdr.len -= 3; /* XXX */
351 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
352 if (m == 0)
353 return;
354 *mtod(m, struct ether_header *) = *eh;
355 IFDEBUG(D_ETHER)
356 printf("clnp packet");
357 ENDDEBUG
358 schednetisr(NETISR_ISO);
359 inq = &clnlintrq;
360 break;
361 }
362 goto dropanyway;
363
364 case LLC_XID:
365 case LLC_XID_P:
366 if(m->m_len < 6)
367 goto dropanyway;
368 l->llc_window = 0;
369 l->llc_fid = 9;
370 l->llc_class = 1;
371 l->llc_dsap = l->llc_ssap = 0;
372 /* Fall through to */
373 case LLC_TEST:
374 case LLC_TEST_P:
375 {
376 struct sockaddr sa;
377 register struct ether_header *eh2;
378 int i;
379 u_char c = l->llc_dsap;
380 l->llc_dsap = l->llc_ssap;
381 l->llc_ssap = c;
382 if (m->m_flags & (M_BCAST | M_MCAST))
383 bcopy((caddr_t)ac->ac_enaddr,
384 (caddr_t)eh->ether_dhost, 6);
385 sa.sa_family = AF_UNSPEC;
386 sa.sa_len = sizeof(sa);
387 eh2 = (struct ether_header *)sa.sa_data;
388 for (i = 0; i < 6; i++) {
389 eh2->ether_shost[i] = c = eh->ether_dhost[i];
390 eh2->ether_dhost[i] =
391 eh->ether_dhost[i] = eh->ether_shost[i];
392 eh->ether_shost[i] = c;
393 }
394 ifp->if_output(ifp, m, &sa);
395 return;
396 }
397 dropanyway:
398 default:
399 m_freem(m);
400 return;
401 }
402 #else
403 m_freem(m);
404 return;
405 #endif ISO
406 }
407
408 s = splimp();
409 if (IF_QFULL(inq)) {
410 IF_DROP(inq);
411 m_freem(m);
412 } else
413 IF_ENQUEUE(inq, m);
414 splx(s);
415 }
416
417 /*
418 * Convert Ethernet address to printable (loggable) representation.
419 */
420 static char digits[] = "0123456789abcdef";
421 char *
422 ether_sprintf(ap)
423 register u_char *ap;
424 {
425 register i;
426 static char etherbuf[18];
427 register char *cp = etherbuf;
428
429 for (i = 0; i < 6; i++) {
430 *cp++ = digits[*ap >> 4];
431 *cp++ = digits[*ap++ & 0xf];
432 *cp++ = ':';
433 }
434 *--cp = 0;
435 return (etherbuf);
436 }
437
438 #ifdef MULTICAST
439 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
440 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
441
442 /* XXX */
443 #undef ac
444 /*
445 * Add an Ethernet multicast address or range of addresses to the list for a
446 * given interface.
447 */
448 int
449 ether_addmulti(ifr, ac)
450 struct ifreq *ifr;
451 register struct arpcom *ac;
452 {
453 register struct ether_multi *enm;
454 struct sockaddr_in *sin;
455 u_char addrlo[6];
456 u_char addrhi[6];
457 int s = splimp();
458
459 switch (ifr->ifr_addr.sa_family) {
460
461 case AF_UNSPEC:
462 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
463 bcopy(addrlo, addrhi, 6);
464 break;
465
466 #ifdef INET
467 case AF_INET:
468 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
469 if (sin->sin_addr.s_addr == INADDR_ANY) {
470 /*
471 * An IP address of INADDR_ANY means listen to all
472 * of the Ethernet multicast addresses used for IP.
473 * (This is for the sake of IP multicast routers.)
474 */
475 bcopy(ether_ipmulticast_min, addrlo, 6);
476 bcopy(ether_ipmulticast_max, addrhi, 6);
477 }
478 else {
479 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
480 bcopy(addrlo, addrhi, 6);
481 }
482 break;
483 #endif
484
485 default:
486 splx(s);
487 return (EAFNOSUPPORT);
488 }
489
490 /*
491 * Verify that we have valid Ethernet multicast addresses.
492 */
493 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
494 splx(s);
495 return (EINVAL);
496 }
497 /*
498 * See if the address range is already in the list.
499 */
500 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
501 if (enm != NULL) {
502 /*
503 * Found it; just increment the reference count.
504 */
505 ++enm->enm_refcount;
506 splx(s);
507 return (0);
508 }
509 /*
510 * New address or range; malloc a new multicast record
511 * and link it into the interface's multicast list.
512 */
513 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
514 if (enm == NULL) {
515 splx(s);
516 return (ENOBUFS);
517 }
518 bcopy(addrlo, enm->enm_addrlo, 6);
519 bcopy(addrhi, enm->enm_addrhi, 6);
520 enm->enm_ac = ac;
521 enm->enm_refcount = 1;
522 enm->enm_next = ac->ac_multiaddrs;
523 ac->ac_multiaddrs = enm;
524 ac->ac_multicnt++;
525 splx(s);
526 /*
527 * Return ENETRESET to inform the driver that the list has changed
528 * and its reception filter should be adjusted accordingly.
529 */
530 return (ENETRESET);
531 }
532
533 /*
534 * Delete a multicast address record.
535 */
536 int
537 ether_delmulti(ifr, ac)
538 struct ifreq *ifr;
539 register struct arpcom *ac;
540 {
541 register struct ether_multi *enm;
542 register struct ether_multi **p;
543 struct sockaddr_in *sin;
544 u_char addrlo[6];
545 u_char addrhi[6];
546 int s = splimp();
547
548 switch (ifr->ifr_addr.sa_family) {
549
550 case AF_UNSPEC:
551 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
552 bcopy(addrlo, addrhi, 6);
553 break;
554
555 #ifdef INET
556 case AF_INET:
557 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
558 if (sin->sin_addr.s_addr == INADDR_ANY) {
559 /*
560 * An IP address of INADDR_ANY means stop listening
561 * to the range of Ethernet multicast addresses used
562 * for IP.
563 */
564 bcopy(ether_ipmulticast_min, addrlo, 6);
565 bcopy(ether_ipmulticast_max, addrhi, 6);
566 }
567 else {
568 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
569 bcopy(addrlo, addrhi, 6);
570 }
571 break;
572 #endif
573
574 default:
575 splx(s);
576 return (EAFNOSUPPORT);
577 }
578
579 /*
580 * Look up the address in our list.
581 */
582 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
583 if (enm == NULL) {
584 splx(s);
585 return (ENXIO);
586 }
587 if (--enm->enm_refcount != 0) {
588 /*
589 * Still some claims to this record.
590 */
591 splx(s);
592 return (0);
593 }
594 /*
595 * No remaining claims to this record; unlink and free it.
596 */
597 for (p = &enm->enm_ac->ac_multiaddrs;
598 *p != enm;
599 p = &(*p)->enm_next)
600 continue;
601 *p = (*p)->enm_next;
602 free(enm, M_IFMADDR);
603 ac->ac_multicnt--;
604 splx(s);
605 /*
606 * Return ENETRESET to inform the driver that the list has changed
607 * and its reception filter should be adjusted accordingly.
608 */
609 return (ENETRESET);
610 }
611 #endif
612