]>
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_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1995, 1996
30 * Matt Thomas <matt@3am-software.com>. All rights reserved.
31 * Copyright (c) 1982, 1989, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
65 #include "opt_atalk.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/socket.h>
75 #include <net/netisr.h>
76 #include <net/route.h>
77 #include <net/if_llc.h>
78 #include <net/if_dl.h>
79 #include <net/if_types.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
86 #if defined(__FreeBSD__)
87 #include <netinet/if_fddi.h>
89 #include <net/if_fddi.h>
93 #include <netipx/ipx.h>
94 #include <netipx/ipx_if.h>
98 #include <netdnet/dn.h>
101 #include "bpfilter.h"
103 #define senderr(e) { error = (e); goto bad;}
106 * This really should be defined in if_llc.h but in case it isn't.
109 #define llc_snap llc_un.type_snap
112 #if defined(__bsdi__) || defined(__NetBSD__)
113 #define RTALLOC1(a, b) rtalloc1(a, b)
114 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e)
115 #elif defined(__FreeBSD__)
116 #define RTALLOC1(a, b) rtalloc1(a, b, 0UL)
117 #define ARPRESOLVE(a, b, c, d, e, f) arpresolve(a, b, c, d, e, f)
120 * FDDI output routine.
121 * Encapsulate a packet of type family for the local net.
122 * Use trailer local net encapsulation if enough data in first
123 * packet leaves a multiple of 512 bytes of data in remainder.
124 * Assumes that ifp is actually pointer to arpcom structure.
127 fddi_output(ifp
, m0
, dst
, rt0
)
128 register struct ifnet
*ifp
;
130 struct sockaddr
*dst
;
134 int s
, loop_copy
= 0, error
= 0;
136 register struct mbuf
*m
= m0
;
137 register struct rtentry
*rt
;
138 register struct fddi_header
*fh
;
139 struct arpcom
*ac
= (struct arpcom
*)ifp
;
141 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
143 getmicrotime(&ifp
->if_lastchange
);
144 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
146 if ((rt
->rt_flags
& RTF_UP
) == 0) {
147 if (rt0
= rt
= RTALLOC1(dst
, 1))
150 senderr(EHOSTUNREACH
);
152 if (rt
->rt_flags
& RTF_GATEWAY
) {
153 if (rt
->rt_gwroute
== 0)
155 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
156 rtfree(rt
); rt
= rt0
;
157 lookup
: rt
->rt_gwroute
= RTALLOC1(rt
->rt_gateway
, 1);
158 if ((rt
= rt
->rt_gwroute
) == 0)
159 senderr(EHOSTUNREACH
);
162 if (rt
->rt_flags
& RTF_REJECT
)
163 if (rt
->rt_rmx
.rmx_expire
== 0 ||
164 time_second
< rt
->rt_rmx
.rmx_expire
)
165 senderr(rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
168 switch (dst
->sa_family
) {
172 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
173 if (!ARPRESOLVE(ac
, rt
, m
, dst
, edst
, rt0
))
174 return (0); /* if not yet resolved */
177 if (!arpresolve(ac
, m
, &((struct sockaddr_in
*)dst
)->sin_addr
, edst
, &usetrailers
))
178 return (0); /* if not yet resolved */
180 type
= htons(ETHERTYPE_IP
);
186 type
= htons(ETHERTYPE_IPX
);
187 bcopy((caddr_t
)&(((struct sockaddr_ipx
*)dst
)->sipx_addr
.x_host
),
188 (caddr_t
)edst
, sizeof (edst
));
194 type
= htons(ETHERTYPE_NS
);
195 bcopy((caddr_t
)&(((struct sockaddr_ns
*)dst
)->sns_addr
.x_host
),
196 (caddr_t
)edst
, sizeof (edst
));
203 register struct sockaddr_dl
*sdl
;
205 if (rt
&& (sdl
= (struct sockaddr_dl
*)rt
->rt_gateway
) &&
206 sdl
->sdl_family
== AF_LINK
&& sdl
->sdl_alen
> 0) {
207 bcopy(LLADDR(sdl
), (caddr_t
)edst
, sizeof(edst
));
209 iso_snparesolve(ifp
, (struct sockaddr_iso
*)dst
,
210 (char *)edst
, &snpalen
))
211 goto bad
; /* Not Resolved */
212 /* If broadcasting on a simplex interface, loopback a copy */
214 m
->m_flags
|= (M_BCAST
|M_MCAST
);
215 M_PREPEND(m
, 3, M_DONTWAIT
);
219 l
= mtod(m
, struct llc
*);
220 l
->llc_dsap
= l
->llc_ssap
= LLC_ISO_LSAP
;
221 l
->llc_control
= LLC_UI
;
224 printf("unoutput: sending pkt to: ");
226 printf("%x ", edst
[i
] & 0xff);
234 register struct sockaddr_dl
*sdl
=
235 (struct sockaddr_dl
*) rt
-> rt_gateway
;
237 if (sdl
&& sdl
->sdl_family
!= AF_LINK
&& sdl
->sdl_alen
<= 0)
238 goto bad
; /* Not a link interface ? Funny ... */
239 bcopy(LLADDR(sdl
), (char *)edst
, sizeof(edst
));
246 register struct llc
*l
= mtod(m
, struct llc
*);
248 printf("fddi_output: sending LLC2 pkt to: ");
250 printf("%x ", edst
[i
] & 0xff);
251 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
252 type
& 0xff, l
->llc_dsap
& 0xff, l
->llc_ssap
&0xff,
253 l
->llc_control
& 0xff);
256 #endif /* LLC_DEBUG */
262 struct ether_header
*eh
;
264 eh
= (struct ether_header
*)dst
->sa_data
;
265 (void)memcpy((caddr_t
)edst
, (caddr_t
)eh
->ether_dhost
, sizeof (edst
));
267 m
->m_flags
|= (M_BCAST
|M_MCAST
);
268 type
= eh
->ether_type
;
275 fh
= mtod(m
, struct fddi_header
*);
276 error
= EPROTONOSUPPORT
;
277 switch (fh
->fddi_fc
& (FDDIFC_C
|FDDIFC_L
|FDDIFC_F
)) {
278 case FDDIFC_LLC_ASYNC
: {
279 /* legal priorities are 0 through 7 */
280 if ((fh
->fddi_fc
& FDDIFC_Z
) > 7)
284 case FDDIFC_LLC_SYNC
: {
285 /* FDDIFC_Z bits reserved, must be zero */
286 if (fh
->fddi_fc
& FDDIFC_Z
)
291 /* FDDIFC_Z bits must be non zero */
292 if ((fh
->fddi_fc
& FDDIFC_Z
) == 0)
297 /* anything else is too dangerous */
302 if (fh
->fddi_dhost
[0] & 1)
303 m
->m_flags
|= (M_BCAST
|M_MCAST
);
308 printf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
310 senderr(EAFNOSUPPORT
);
314 register struct llc
*l
;
315 M_PREPEND(m
, sizeof (struct llc
), M_DONTWAIT
);
318 l
= mtod(m
, struct llc
*);
319 l
->llc_control
= LLC_UI
;
320 l
->llc_dsap
= l
->llc_ssap
= LLC_SNAP_LSAP
;
321 l
->llc_snap
.org_code
[0] = l
->llc_snap
.org_code
[1] = l
->llc_snap
.org_code
[2] = 0;
322 (void)memcpy((caddr_t
) &l
->llc_snap
.ether_type
, (caddr_t
) &type
,
327 * Add local net header. If no space in first mbuf,
330 M_PREPEND(m
, sizeof (struct fddi_header
), M_DONTWAIT
);
333 fh
= mtod(m
, struct fddi_header
*);
334 fh
->fddi_fc
= FDDIFC_LLC_ASYNC
|FDDIFC_LLC_PRIO4
;
335 (void)memcpy((caddr_t
)fh
->fddi_dhost
, (caddr_t
)edst
, sizeof (edst
));
337 (void)memcpy((caddr_t
)fh
->fddi_shost
, (caddr_t
)ac
->ac_enaddr
,
338 sizeof(fh
->fddi_shost
));
341 * If a simplex interface, and the packet is being sent to our
342 * Ethernet address or a broadcast address, loopback a copy.
343 * XXX To make a simplex device behave exactly like a duplex
344 * device, we should copy in the case of sending to our own
345 * ethernet address (thus letting the original actually appear
346 * on the wire). However, we don't do that here for security
347 * reasons and compatibility with the original behavior.
349 if ((ifp
->if_flags
& IFF_SIMPLEX
) &&
351 if ((m
->m_flags
& M_BCAST
) || loop_copy
) {
352 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
354 (void) if_simloop(ifp
,
355 n
, dst
, sizeof(struct fddi_header
));
356 } else if (bcmp(fh
->fddi_dhost
,
357 fh
->fddi_shost
, sizeof(fh
->fddi_shost
)) == 0) {
358 (void) if_simloop(ifp
,
359 m
, dst
, sizeof(struct fddi_header
));
366 * Queue message on interface, and start output if interface
369 if (IF_QFULL(&ifp
->if_snd
)) {
370 IF_DROP(&ifp
->if_snd
);
374 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
375 IF_ENQUEUE(&ifp
->if_snd
, m
);
376 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
377 (*ifp
->if_start
)(ifp
);
379 if (m
->m_flags
& M_MCAST
)
390 * Process a received FDDI packet;
391 * the packet is in the mbuf chain m without
392 * the fddi header, which is provided separately.
395 fddi_input(ifp
, fh
, m
)
397 register struct fddi_header
*fh
;
400 register struct ifqueue
*inq
;
401 register struct llc
*l
;
404 if ((ifp
->if_flags
& IFF_UP
) == 0) {
408 getmicrotime(&ifp
->if_lastchange
);
409 ifp
->if_ibytes
+= m
->m_pkthdr
.len
+ sizeof (*fh
);
410 if (fh
->fddi_dhost
[0] & 1) {
411 if (bcmp((caddr_t
)fddibroadcastaddr
, (caddr_t
)fh
->fddi_dhost
,
412 sizeof(fddibroadcastaddr
)) == 0)
413 m
->m_flags
|= M_BCAST
;
415 m
->m_flags
|= M_MCAST
;
417 } else if ((ifp
->if_flags
& IFF_PROMISC
)
418 && bcmp(((struct arpcom
*)ifp
)->ac_enaddr
, (caddr_t
)fh
->fddi_dhost
,
419 sizeof(fh
->fddi_dhost
)) != 0) {
426 * If this has a LLC priority of 0, then mark it so upper
427 * layers have a hint that it really came via a FDDI/Ethernet
430 if ((fh
->fddi_fc
& FDDIFC_LLC_PRIO7
) == FDDIFC_LLC_PRIO0
)
431 m
->m_flags
|= M_LINK0
;
434 l
= mtod(m
, struct llc
*);
435 switch (l
->llc_dsap
) {
436 #if defined(INET) || NS || IPX || defined(NETATALK)
440 if (l
->llc_control
!= LLC_UI
|| l
->llc_ssap
!= LLC_SNAP_LSAP
)
443 if (l
->llc_snap
.org_code
[0] != 0 || l
->llc_snap
.org_code
[1] != 0|| l
->llc_snap
.org_code
[2] != 0)
445 type
= ntohs(l
->llc_snap
.ether_type
);
450 if (ipflow_fastforward(m
))
452 schednetisr(NETISR_IP
);
457 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
458 schednetisr(NETISR_ARP
);
462 arpinput((struct arpcom
*)ifp
, m
);
468 schednetisr(NETISR_IPX
);
474 schednetisr(NETISR_NS
);
479 case ETHERTYPE_DECNET
:
480 schednetisr(NETISR_DECNET
);
486 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
492 #endif /* INET || NS */
495 switch (l
->llc_control
) {
497 /* LLC_UI_P forbidden in class 1 service */
498 if ((l
->llc_dsap
== LLC_ISO_LSAP
) &&
499 (l
->llc_ssap
== LLC_ISO_LSAP
)) {
501 m
->m_data
+= 3; /* XXX */
502 m
->m_len
-= 3; /* XXX */
503 m
->m_pkthdr
.len
-= 3; /* XXX */
504 M_PREPEND(m
, sizeof *fh
, M_DONTWAIT
);
507 *mtod(m
, struct fddi_header
*) = *fh
;
509 printf("clnp packet");
511 schednetisr(NETISR_ISO
);
524 l
->llc_dsap
= l
->llc_ssap
= 0;
525 /* Fall through to */
530 register struct ether_header
*eh
;
531 struct arpcom
*ac
= (struct arpcom
*) ifp
;
533 u_char c
= l
->llc_dsap
;
535 l
->llc_dsap
= l
->llc_ssap
;
537 if (m
->m_flags
& (M_BCAST
| M_MCAST
))
538 bcopy((caddr_t
)ac
->ac_enaddr
,
539 (caddr_t
)eh
->ether_dhost
, 6);
540 sa
.sa_family
= AF_UNSPEC
;
541 sa
.sa_len
= sizeof(sa
);
542 eh
= (struct ether_header
*)sa
.sa_data
;
543 for (i
= 0; i
< 6; i
++) {
544 eh
->ether_shost
[i
] = fh
->fddi_dhost
[i
];
545 eh
->ether_dhost
[i
] = fh
->fddi_shost
[i
];
548 ifp
->if_output(ifp
, m
, &sa
, NULL
);
560 M_PREPEND(m
, sizeof(struct sdl_hdr
) , M_DONTWAIT
);
563 if ( !sdl_sethdrif(ifp
, fh
->fddi_shost
, LLC_X25_LSAP
,
564 fh
->fddi_dhost
, LLC_X25_LSAP
, 6,
565 mtod(m
, struct sdl_hdr
*)))
566 panic("ETHER cons addr failure");
567 mtod(m
, struct sdl_hdr
*)->sdlhdr_len
= m
->m_pkthdr
.len
- sizeof(struct sdl_hdr
);
569 printf("llc packet\n");
570 #endif /* LLC_DEBUG */
571 schednetisr(NETISR_CCITT
);
578 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
594 * Perform common duties while attaching to interface list
597 #define ifa_next ifa_list.tqe_next
602 register struct ifnet
*ifp
;
604 register struct ifaddr
*ifa
;
605 register struct sockaddr_dl
*sdl
;
607 ifp
->if_type
= IFT_FDDI
;
610 ifp
->if_mtu
= FDDIMTU
;
611 ifp
->if_baudrate
= 100000000;
613 ifp
->if_flags
|= IFF_NOTRAILERS
;
615 #if defined(__FreeBSD__)
616 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
617 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
618 sdl
->sdl_type
= IFT_FDDI
;
619 sdl
->sdl_alen
= ifp
->if_addrlen
;
620 bcopy(((struct arpcom
*)ifp
)->ac_enaddr
, LLADDR(sdl
), ifp
->if_addrlen
);
621 #elif defined(__NetBSD__)
622 LIST_INIT(&((struct arpcom
*)ifp
)->ac_multiaddrs
);
623 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
!= NULL
; ifa
= ifa
->ifa_list
.tqe_next
)
625 for (ifa
= ifp
->if_addrlist
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
627 #if !defined(__FreeBSD__)
628 if ((sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
) &&
629 sdl
->sdl_family
== AF_LINK
) {
630 sdl
->sdl_type
= IFT_FDDI
;
631 sdl
->sdl_alen
= ifp
->if_addrlen
;
632 bcopy((caddr_t
)((struct arpcom
*)ifp
)->ac_enaddr
,
633 LLADDR(sdl
), ifp
->if_addrlen
);