]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_fddisubr.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1995, 1996
27 * Matt Thomas <matt@3am-software.com>. All rights reserved.
28 * Copyright (c) 1982, 1989, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
62 #include "opt_atalk.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/socket.h>
72 #include <net/netisr.h>
73 #include <net/route.h>
74 #include <net/if_llc.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/if_ether.h>
83 #if defined(__FreeBSD__)
84 #include <netinet/if_fddi.h>
86 #include <net/if_fddi.h>
90 #include <netipx/ipx.h>
91 #include <netipx/ipx_if.h>
95 #include <netdnet/dn.h>
100 #define senderr(e) { error = (e); goto bad;}
103 * This really should be defined in if_llc.h but in case it isn't.
106 #define llc_snap llc_un.type_snap
109 #if defined(__bsdi__) || defined(__NetBSD__)
110 #define RTALLOC1(a, b) rtalloc1(a, b)
111 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
112 #elif defined(__FreeBSD__)
113 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
114 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
117 * FDDI output routine.
118 * Encapsulate a packet of type family for the local net.
119 * Use trailer local net encapsulation if enough data in first
120 * packet leaves a multiple of 512 bytes of data in remainder.
121 * Assumes that ifp is actually pointer to arpcom structure.
124 fddi_output(ifp
, m0
, dst
, rt0
)
125 register struct ifnet
*ifp
;
127 struct sockaddr
*dst
;
131 int s
, loop_copy
= 0, error
= 0;
133 register struct mbuf
*m
= m0
;
134 register struct rtentry
*rt
;
135 register struct fddi_header
*fh
;
136 struct arpcom
*ac
= (struct arpcom
*)ifp
;
138 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
140 getmicrotime(&ifp
->if_lastchange
);
141 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
143 if ((rt
->rt_flags
& RTF_UP
) == 0) {
144 if (rt0
= rt
= RTALLOC1(dst
, 1))
147 senderr(EHOSTUNREACH
);
149 if (rt
->rt_flags
& RTF_GATEWAY
) {
150 if (rt
->rt_gwroute
== 0)
152 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
153 rtfree(rt
); rt
= rt0
;
154 lookup
: rt
->rt_gwroute
= RTALLOC1(rt
->rt_gateway
, 1);
155 if ((rt
= rt
->rt_gwroute
) == 0)
156 senderr(EHOSTUNREACH
);
159 if (rt
->rt_flags
& RTF_REJECT
)
160 if (rt
->rt_rmx
.rmx_expire
== 0 ||
161 time_second
< rt
->rt_rmx
.rmx_expire
)
162 senderr(rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
165 switch (dst
->sa_family
) {
169 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
170 if (!ARPRESOLVE(ac
, rt
, m
, dst
, edst
, rt0
))
171 return (0); /* if not yet resolved */
174 if (!arpresolve(ac
, m
, &((struct sockaddr_in
*)dst
)->sin_addr
, edst
, &usetrailers
))
175 return (0); /* if not yet resolved */
177 type
= htons(ETHERTYPE_IP
);
183 type
= htons(ETHERTYPE_IPX
);
184 bcopy((caddr_t
)&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
185 (caddr_t
)edst
, sizeof (edst
));
191 type
= htons(ETHERTYPE_NS
);
192 bcopy((caddr_t
)&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
),
193 (caddr_t
)edst
, sizeof (edst
));
200 register struct sockaddr_dl
*sdl
;
202 if (rt
&& (sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
) &&
203 sdl
->sdl_family
== AF_LINK
&& sdl
->sdl_alen
> 0) {
204 bcopy(LLADDR(sdl
), (caddr_t
)edst
, sizeof(edst
));
206 iso_snparesolve(ifp
, (struct sockaddr_iso
*)dst
,
207 (char *)edst
, &snpalen
))
208 goto bad
; /* Not Resolved */
209 /* If broadcasting on a simplex interface, loopback a copy */
211 m
->m_flags
|= (M_BCAST
|M_MCAST
);
212 M_PREPEND(m
, 3, M_DONTWAIT
);
216 l
= mtod(m
, struct llc
*);
217 l
->llc_dsap
= l
->llc_ssap
= LLC_ISO_LSAP
;
218 l
->llc_control
= LLC_UI
;
221 printf("unoutput: sending pkt to: ");
223 printf("%x ", edst
[i
] & 0xff);
231 register struct sockaddr_dl
*sdl
=
232 (struct sockaddr_dl
*) rt
-> rt_gateway
;
234 if (sdl
&& sdl
->sdl_family
!= AF_LINK
&& sdl
->sdl_alen
<= 0)
235 goto bad
; /* Not a link interface ? Funny ... */
236 bcopy(LLADDR(sdl
), (char *)edst
, sizeof(edst
));
243 register struct llc
*l
= mtod(m
, struct llc
*);
245 printf("fddi_output: sending LLC2 pkt to: ");
247 printf("%x ", edst
[i
] & 0xff);
248 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
249 type
& 0xff, l
->llc_dsap
& 0xff, l
->llc_ssap
&0xff,
250 l
->llc_control
& 0xff);
253 #endif /* LLC_DEBUG */
259 struct ether_header
*eh
;
261 eh
= (struct ether_header
*)dst
->sa_data
;
262 (void)memcpy((caddr_t
)edst
, (caddr_t
)eh
->ether_dhost
, sizeof (edst
));
264 m
->m_flags
|= (M_BCAST
|M_MCAST
);
265 type
= eh
->ether_type
;
272 fh
= mtod(m
, struct fddi_header
*);
273 error
= EPROTONOSUPPORT
;
274 switch (fh
->fddi_fc
& (FDDIFC_C
|FDDIFC_L
|FDDIFC_F
)) {
275 case FDDIFC_LLC_ASYNC
: {
276 /* legal priorities are 0 through 7 */
277 if ((fh
->fddi_fc
& FDDIFC_Z
) > 7)
281 case FDDIFC_LLC_SYNC
: {
282 /* FDDIFC_Z bits reserved, must be zero */
283 if (fh
->fddi_fc
& FDDIFC_Z
)
288 /* FDDIFC_Z bits must be non zero */
289 if ((fh
->fddi_fc
& FDDIFC_Z
) == 0)
294 /* anything else is too dangerous */
299 if (fh
->fddi_dhost
[0] & 1)
300 m
->m_flags
|= (M_BCAST
|M_MCAST
);
305 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
307 senderr(EAFNOSUPPORT
);
311 register struct llc
*l
;
312 M_PREPEND(m
, sizeof (struct llc
), M_DONTWAIT
);
315 l
= mtod(m
, struct llc
*);
316 l
->llc_control
= LLC_UI
;
317 l
->llc_dsap
= l
->llc_ssap
= LLC_SNAP_LSAP
;
318 l
->llc_snap
.org_code
[0] = l
->llc_snap
.org_code
[1] = l
->llc_snap
.org_code
[2] = 0;
319 (void)memcpy((caddr_t
) &l
->llc_snap
.ether_type
, (caddr_t
) &type
,
324 * Add local net header. If no space in first mbuf,
327 M_PREPEND(m
, sizeof (struct fddi_header
), M_DONTWAIT
);
330 fh
= mtod(m
, struct fddi_header
*);
331 fh
->fddi_fc
= FDDIFC_LLC_ASYNC
|FDDIFC_LLC_PRIO4
;
332 (void)memcpy((caddr_t
)fh
->fddi_dhost
, (caddr_t
)edst
, sizeof (edst
));
334 (void)memcpy((caddr_t
)fh
->fddi_shost
, (caddr_t
)ac
->ac_enaddr
,
335 sizeof(fh
->fddi_shost
));
338 * If a simplex interface, and the packet is being sent to our
339 * Ethernet address or a broadcast address, loopback a copy.
340 * XXX To make a simplex device behave exactly like a duplex
341 * device, we should copy in the case of sending to our own
342 * ethernet address (thus letting the original actually appear
343 * on the wire). However, we don't do that here for security
344 * reasons and compatibility with the original behavior.
346 if ((ifp
->if_flags
& IFF_SIMPLEX
) &&
348 if ((m
->m_flags
& M_BCAST
) || loop_copy
) {
349 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
351 (void) if_simloop(ifp
,
352 n
, dst
, sizeof(struct fddi_header
));
353 } else if (bcmp(fh
->fddi_dhost
,
354 fh
->fddi_shost
, sizeof(fh
->fddi_shost
)) == 0) {
355 (void) if_simloop(ifp
,
356 m
, dst
, sizeof(struct fddi_header
));
363 * Queue message on interface, and start output if interface
366 if (IF_QFULL(&ifp
->if_snd
)) {
367 IF_DROP(&ifp
->if_snd
);
371 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
372 IF_ENQUEUE(&ifp
->if_snd
, m
);
373 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
374 (*ifp
->if_start
)(ifp
);
376 if (m
->m_flags
& M_MCAST
)
387 * Process a received FDDI packet;
388 * the packet is in the mbuf chain m without
389 * the fddi header, which is provided separately.
392 fddi_input(ifp
, fh
, m
)
394 register struct fddi_header
*fh
;
397 register struct ifqueue
*inq
;
398 register struct llc
*l
;
401 if ((ifp
->if_flags
& IFF_UP
) == 0) {
405 getmicrotime(&ifp
->if_lastchange
);
406 ifp
->if_ibytes
+= m
->m_pkthdr
.len
+ sizeof (*fh
);
407 if (fh
->fddi_dhost
[0] & 1) {
408 if (bcmp((caddr_t
)fddibroadcastaddr
, (caddr_t
)fh
->fddi_dhost
,
409 sizeof(fddibroadcastaddr
)) == 0)
410 m
->m_flags
|= M_BCAST
;
412 m
->m_flags
|= M_MCAST
;
414 } else if ((ifp
->if_flags
& IFF_PROMISC
)
415 && bcmp(((struct arpcom
*)ifp
)->ac_enaddr
, (caddr_t
)fh
->fddi_dhost
,
416 sizeof(fh
->fddi_dhost
)) != 0) {
423 * If this has a LLC priority of 0, then mark it so upper
424 * layers have a hint that it really came via a FDDI/Ethernet
427 if ((fh
->fddi_fc
& FDDIFC_LLC_PRIO7
) == FDDIFC_LLC_PRIO0
)
428 m
->m_flags
|= M_LINK0
;
431 l
= mtod(m
, struct llc
*);
432 switch (l
->llc_dsap
) {
433 #if defined(INET) || NS || IPX || defined(NETATALK)
437 if (l
->llc_control
!= LLC_UI
|| l
->llc_ssap
!= LLC_SNAP_LSAP
)
440 if (l
->llc_snap
.org_code
[0] != 0 || l
->llc_snap
.org_code
[1] != 0|| l
->llc_snap
.org_code
[2] != 0)
442 type
= ntohs(l
->llc_snap
.ether_type
);
447 if (ipflow_fastforward(m
))
449 schednetisr(NETISR_IP
);
454 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
455 schednetisr(NETISR_ARP
);
459 arpinput((struct arpcom
*)ifp
, m
);
465 schednetisr(NETISR_IPX
);
471 schednetisr(NETISR_NS
);
476 case ETHERTYPE_DECNET
:
477 schednetisr(NETISR_DECNET
);
483 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
489 #endif /* INET || NS */
492 switch (l
->llc_control
) {
494 /* LLC_UI_P forbidden in class 1 service */
495 if ((l
->llc_dsap
== LLC_ISO_LSAP
) &&
496 (l
->llc_ssap
== LLC_ISO_LSAP
)) {
498 m
->m_data
+= 3; /* XXX */
499 m
->m_len
-= 3; /* XXX */
500 m
->m_pkthdr
.len
-= 3; /* XXX */
501 M_PREPEND(m
, sizeof *fh
, M_DONTWAIT
);
504 *mtod(m
, struct fddi_header
*) = *fh
;
506 printf("clnp packet");
508 schednetisr(NETISR_ISO
);
521 l
->llc_dsap
= l
->llc_ssap
= 0;
522 /* Fall through to */
527 register struct ether_header
*eh
;
528 struct arpcom
*ac
= (struct arpcom
*) ifp
;
530 u_char c
= l
->llc_dsap
;
532 l
->llc_dsap
= l
->llc_ssap
;
534 if (m
->m_flags
& (M_BCAST
| M_MCAST
))
535 bcopy((caddr_t
)ac
->ac_enaddr
,
536 (caddr_t
)eh
->ether_dhost
, 6);
537 sa
.sa_family
= AF_UNSPEC
;
538 sa
.sa_len
= sizeof(sa
);
539 eh
= (struct ether_header
*)sa
.sa_data
;
540 for (i
= 0; i
< 6; i
++) {
541 eh
->ether_shost
[i
] = fh
->fddi_dhost
[i
];
542 eh
->ether_dhost
[i
] = fh
->fddi_shost
[i
];
545 ifp
->if_output(ifp
, m
, &sa
, NULL
);
557 M_PREPEND(m
, sizeof(struct sdl_hdr
) , M_DONTWAIT
);
560 if ( !sdl_sethdrif(ifp
, fh
->fddi_shost
, LLC_X25_LSAP
,
561 fh
->fddi_dhost
, LLC_X25_LSAP
, 6,
562 mtod(m
, struct sdl_hdr
*)))
563 panic("ETHER cons addr failure");
564 mtod(m
, struct sdl_hdr
*)->sdlhdr_len
= m
->m_pkthdr
.len
- sizeof(struct sdl_hdr
);
566 printf("llc packet\n");
567 #endif /* LLC_DEBUG */
568 schednetisr(NETISR_CCITT
);
575 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
591 * Perform common duties while attaching to interface list
594 #define ifa_next ifa_list.tqe_next
599 register struct ifnet
*ifp
;
601 register struct ifaddr
*ifa
;
602 register struct sockaddr_dl
*sdl
;
604 ifp
->if_type
= IFT_FDDI
;
607 ifp
->if_mtu
= FDDIMTU
;
608 ifp
->if_baudrate
= 100000000;
610 ifp
->if_flags
|= IFF_NOTRAILERS
;
612 #if defined(__FreeBSD__)
613 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
614 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
615 sdl
->sdl_type
= IFT_FDDI
;
616 sdl
->sdl_alen
= ifp
->if_addrlen
;
617 bcopy(((struct arpcom
*)ifp
)->ac_enaddr
, LLADDR(sdl
), ifp
->if_addrlen
);
618 #elif defined(__NetBSD__)
619 LIST_INIT(&((struct arpcom
*)ifp
)->ac_multiaddrs
);
620 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
!= NULL
; ifa
= ifa
->ifa_list
.tqe_next
)
622 for (ifa
= ifp
->if_addrlist
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
624 #if !defined(__FreeBSD__)
625 if ((sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
) &&
626 sdl
->sdl_family
== AF_LINK
) {
627 sdl
->sdl_type
= IFT_FDDI
;
628 sdl
->sdl_alen
= ifp
->if_addrlen
;
629 bcopy((caddr_t
)((struct arpcom
*)ifp
)->ac_enaddr
,
630 LLADDR(sdl
), ifp
->if_addrlen
);