2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright 1994 Apple Computer, Inc.
32 * All Rights Reserved.
34 * Tuyen A. Nguyen. (December 5, 1994)
35 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <machine/spl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sockio.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/ethernet.h>
59 #include <netat/sysglue.h>
60 #include <netat/appletalk.h>
61 #include <netat/at_pcb.h>
62 #include <netat/at_var.h>
63 #include <netat/ddp.h>
64 #include <netat/at_aarp.h>
65 #include <netat/at_pat.h>
66 #include <netat/debug.h>
68 #define DSAP_SNAP 0xaa
70 extern void gref_init(), atp_init(), atp_link(), atp_unlink();
72 extern int adspInited
;
74 static llc_header_t snap_hdr_at
= SNAP_HDR_AT
;
75 static llc_header_t snap_hdr_aarp
= SNAP_HDR_AARP
;
76 static unsigned char snap_proto_ddp
[5] = SNAP_PROTO_AT
;
77 static unsigned char snap_proto_aarp
[5] = SNAP_PROTO_AARP
;
79 static void at_input_packet(protocol_family_t protocol
, mbuf_t m
);
83 struct ifqueue atalkintrq
; /* appletalk and aarp packet input queue */
85 short appletalk_inited
= 0;
91 extern lck_mtx_t
*domain_proto_mtx
;
100 this happens in adsp_open and is undone on ADSP_UNLINK
102 lck_mtx_unlock(domain_proto_mtx
);
103 proto_register_input(PF_APPLETALK
, at_input_packet
, NULL
);
104 lck_mtx_lock(domain_proto_mtx
);
107 /* Undo everything atalk_load() did. */
108 void atalk_unload() /* not currently used */
110 extern gbuf_t
*scb_resource_m
;
111 extern gbuf_t
*atp_resource_m
;
116 if (scb_resource_m
) {
117 gbuf_freem(scb_resource_m
);
121 /* allocated in atp_trans_alloc() */
122 if (atp_resource_m
) {
123 gbuf_freem(atp_resource_m
);
125 atp_trans_free_list
= 0;
129 appletalk_inited
= 0;
132 void appletalk_hack_start()
134 if (!appletalk_inited
) {
136 atalkintrq
.ifq_maxlen
= IFQ_MAXLEN
;
137 appletalk_inited
= 1;
139 } /* appletalk_hack_start */
141 int pat_output(patp
, mlist
, dst_addr
, type
)
143 struct mbuf
*mlist
; /* packet chain */
144 unsigned char *dst_addr
; /* for atalk addr - net # must be in network byte order */
148 llc_header_t
*llc_header
;
151 if (! patp
->aa_ifp
) {
152 for (m
= mlist
; m
; m
= mlist
) {
153 mlist
= m
->m_nextpkt
;
160 /* this is for ether_output */
161 dst
.sa_family
= AF_APPLETALK
;
162 dst
.sa_len
= 2 + sizeof(struct etalk_addr
);
163 bcopy (dst_addr
, &dst
.sa_data
[0], sizeof(struct etalk_addr
));
165 /* packet chains are used on output and can be tested using aufs */
166 for (m
= mlist
; m
; m
= mlist
) {
167 mlist
= m
->m_nextpkt
;
170 M_PREPEND(m
, sizeof(llc_header_t
), M_DONTWAIT
);
175 llc_header
= mtod(m
, llc_header_t
*);
177 (type
== AARP_AT_TYPE
) ? snap_hdr_aarp
: snap_hdr_at
;
179 for (m
->m_pkthdr
.len
= 0, m1
= m
; m1
; m1
= m1
->m_next
)
180 m
->m_pkthdr
.len
+= m1
->m_len
;
181 m
->m_pkthdr
.rcvif
= 0;
183 /* *** Note: AT is sending out mbufs of type MSG_DATA,
185 #ifdef APPLETALK_DEBUG
187 !((m
->m_next
)->m_flags
& M_EXT
))
188 kprintf("po: mlen= %d, m2len= %d\n", m
->m_len
,
192 dlil_output(patp
->aa_ifp
, PF_APPLETALK
, m
, NULL
, &dst
, 0);
203 __unused protocol_family_t protocol
,
208 llc_header_t
*llc_header
;
211 enet_header_t
*enet_header
;
213 if (!appletalk_inited
) {
218 if ((m
->m_flags
& M_PKTHDR
) == 0) {
219 #ifdef APPLETALK_DEBUG
220 kprintf("atalkintr: no HDR on packet received");
226 /* make sure the interface this packet was received on is configured
228 ifp
= m
->m_pkthdr
.rcvif
;
229 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
230 if (ifID
->aa_ifp
&& (ifID
->aa_ifp
== ifp
))
233 /* if we didn't find a matching interface */
236 return; /* was EAFNOSUPPORT */
239 /* make sure the entire packet header is in the current mbuf */
240 if (m
->m_len
< ENET_LLC_SIZE
&&
241 (m
= m_pullup(m
, ENET_LLC_SIZE
)) == 0) {
242 #ifdef APPLETALK_DEBUG
243 kprintf("atalkintr: packet too small\n");
248 enet_header
= mtod(m
, enet_header_t
*);
250 /* Ignore multicast packets from local station */
251 /* *** Note: code for IFTYPE_TOKENTALK may be needed here. *** */
252 if (ifID
->aa_ifp
->if_type
== IFT_ETHER
||
253 ifID
->aa_ifp
->if_type
== IFT_L2VLAN
||
254 ifID
->aa_ifp
->if_type
== IFT_IEEE8023ADLAG
) {
255 bcopy((char *)enet_header
->src
, src
, sizeof(src
));
257 #ifdef COMMENT /* In order to receive packets from the Blue Box, we cannot
258 reject packets whose source address matches our local address.
260 if ((enet_header
->dst
[0] & 1) &&
261 (bcmp(src
, ifID
->xaddr
, sizeof(src
)) == 0)) {
262 /* Packet rejected: think it's a local mcast. */
264 return; /* was EAFNOSUPPORT */
268 llc_header
= (llc_header_t
*)(enet_header
+1);
270 /* advance the mbuf pointers past the ethernet header */
271 m
->m_data
+= ENET_LLC_SIZE
;
272 m
->m_len
-= ENET_LLC_SIZE
;
276 if (LLC_PROTO_EQUAL(llc_header
->protocol
,snap_proto_aarp
)) {
277 (void)aarp_rcv_pkt(mtod(m
, aarp_pkt_t
*), ifID
);
280 else if (LLC_PROTO_EQUAL(llc_header
->protocol
, snap_proto_ddp
)) {
281 /* if we're a router take all pkts */
283 if (aarp_chk_addr(mtod(m
, at_ddp_t
*), ifID
)
284 == AARP_ERR_NOT_OURS
) {
285 #ifdef APPLETALK_DEBUG
286 kprintf("pat_input: Packet Rejected: not for us? dest=%x.%x.%x.%x.%x.%x LLC_PROTO= %02x%02x\n",
287 enet_header
->dst
[0], enet_header
->dst
[1],
288 enet_header
->dst
[2], enet_header
->dst
[3],
289 enet_header
->dst
[4], enet_header
->dst
[5],
290 llc_header
->protocol
[3],
291 llc_header
->protocol
[4]);
294 return; /* was EAFNOSUPPORT */
297 MCHTYPE(m
, MSG_DATA
); /* set the mbuf type */
299 ifID
->stats
.rcv_packets
++;
300 for (m1
= m
; m1
; m1
= m1
->m_next
)
301 ifID
->stats
.rcv_bytes
+= m1
->m_len
;
304 ddp_glean(m
, ifID
, src
);
308 #ifdef APPLETALK_DEBUG
309 kprintf("pat_input: Packet Rejected: wrong LLC_PROTO = %02x%02x\n",
310 llc_header
->protocol
[3],
311 llc_header
->protocol
[4]);