]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/drv_dep.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / netat / drv_dep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright 1994 Apple Computer, Inc.
30 * All Rights Reserved.
31 *
32 * Tuyen A. Nguyen. (December 5, 1994)
33 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
34 */
35
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
45 #include <sys/mbuf.h>
46 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sockio.h>
51
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/if_dl.h>
55 #include <net/ethernet.h>
56
57 #include <netat/sysglue.h>
58 #include <netat/appletalk.h>
59 #include <netat/at_pcb.h>
60 #include <netat/at_var.h>
61 #include <netat/ddp.h>
62 #include <netat/at_aarp.h>
63 #include <netat/at_pat.h>
64 #include <netat/debug.h>
65
66 #define DSAP_SNAP 0xaa
67
68 extern void gref_init(), atp_init(), atp_link(), atp_unlink();
69
70 extern int adspInited;
71
72 static llc_header_t snap_hdr_at = SNAP_HDR_AT;
73 static llc_header_t snap_hdr_aarp = SNAP_HDR_AARP;
74 static unsigned char snap_proto_ddp[5] = SNAP_PROTO_AT;
75 static unsigned char snap_proto_aarp[5] = SNAP_PROTO_AARP;
76
77 static void at_input_packet(protocol_family_t protocol, mbuf_t m);
78
79 int pktsIn, pktsOut;
80
81 struct ifqueue atalkintrq; /* appletalk and aarp packet input queue */
82
83 short appletalk_inited = 0;
84
85 extern atlock_t
86 ddpall_lock, ddpinp_lock, arpinp_lock, refall_lock, nve_lock,
87 aspall_lock, asptmo_lock, atpall_lock, atptmo_lock, atpgen_lock;
88
89 extern int (*sys_ATsocket )(), (*sys_ATgetmsg)(), (*sys_ATputmsg)();
90 extern int (*sys_ATPsndreq)(), (*sys_ATPsndrsp)();
91 extern int (*sys_ATPgetreq)(), (*sys_ATPgetrsp)();
92
93 void atalk_load()
94 {
95 extern int _ATsocket(), _ATgetmsg(), _ATputmsg();
96 extern int _ATPsndreq(), _ATPsndrsp(), _ATPgetreq(), _ATPgetrsp();
97 extern lck_mtx_t *domain_proto_mtx;
98
99 sys_ATsocket = _ATsocket;
100 sys_ATgetmsg = _ATgetmsg;
101 sys_ATputmsg = _ATputmsg;
102 sys_ATPsndreq = _ATPsndreq;
103 sys_ATPsndrsp = _ATPsndrsp;
104 sys_ATPgetreq = _ATPgetreq;
105 sys_ATPgetrsp = _ATPgetrsp;
106
107 ATLOCKINIT(ddpall_lock);
108 ATLOCKINIT(ddpinp_lock);
109 ATLOCKINIT(arpinp_lock);
110 ATLOCKINIT(refall_lock);
111 ATLOCKINIT(aspall_lock);
112 ATLOCKINIT(asptmo_lock);
113 ATLOCKINIT(atpall_lock);
114 ATLOCKINIT(atptmo_lock);
115 ATLOCKINIT(atpgen_lock);
116 ATLOCKINIT(nve_lock);
117
118 atp_init();
119 atp_link();
120 adspInited = 0;
121
122 /* adsp_init();
123 for 2225395
124 this happens in adsp_open and is undone on ADSP_UNLINK
125 */
126 lck_mtx_unlock(domain_proto_mtx);
127 proto_register_input(PF_APPLETALK, at_input_packet, NULL);
128 lck_mtx_lock(domain_proto_mtx);
129 } /* atalk_load */
130
131 /* Undo everything atalk_load() did. */
132 void atalk_unload() /* not currently used */
133 {
134 extern gbuf_t *scb_resource_m;
135 extern gbuf_t *atp_resource_m;
136
137 sys_ATsocket = 0;
138 sys_ATgetmsg = 0;
139 sys_ATputmsg = 0;
140 sys_ATPsndreq = 0;
141 sys_ATPsndrsp = 0;
142 sys_ATPgetreq = 0;
143 sys_ATPgetrsp = 0;
144
145 atp_unlink();
146
147 #ifdef NOT_YET
148 if (scb_resource_m) {
149 gbuf_freem(scb_resource_m);
150 scb_resource_m = 0;
151 scb_free_list = 0;
152 }
153 /* allocated in atp_trans_alloc() */
154 if (atp_resource_m) {
155 gbuf_freem(atp_resource_m);
156 atp_resource_m = 0;
157 atp_trans_free_list = 0;
158 }
159 #endif
160
161 appletalk_inited = 0;
162 } /* atalk_unload */
163
164 void appletalk_hack_start()
165 {
166 if (!appletalk_inited) {
167 atalk_load();
168 atalkintrq.ifq_maxlen = IFQ_MAXLEN;
169 appletalk_inited = 1;
170 }
171 } /* appletalk_hack_start */
172
173 int pat_output(patp, mlist, dst_addr, type)
174 at_ifaddr_t *patp;
175 struct mbuf *mlist; /* packet chain */
176 unsigned char *dst_addr;
177 int type;
178 {
179 struct mbuf *m, *m1;
180 llc_header_t *llc_header;
181 struct sockaddr dst;
182
183 if (! patp->aa_ifp) {
184 for (m = mlist; m; m = mlist) {
185 mlist = m->m_nextpkt;
186 m->m_nextpkt = 0;
187 m_freem(m);
188 }
189 return ENOTREADY;
190 }
191
192 /* this is for ether_output */
193 dst.sa_family = AF_APPLETALK;
194 dst.sa_len = 2 + sizeof(struct etalk_addr);
195 bcopy (dst_addr, &dst.sa_data[0], sizeof(struct etalk_addr));
196
197 /* packet chains are used on output and can be tested using aufs */
198 for (m = mlist; m; m = mlist) {
199 mlist = m->m_nextpkt;
200 m->m_nextpkt = 0;
201
202 M_PREPEND(m, sizeof(llc_header_t), M_DONTWAIT);
203 if (m == 0) {
204 continue;
205 }
206
207 llc_header = mtod(m, llc_header_t *);
208 *llc_header =
209 (type == AARP_AT_TYPE) ? snap_hdr_aarp : snap_hdr_at;
210
211 for (m->m_pkthdr.len = 0, m1 = m; m1; m1 = m1->m_next)
212 m->m_pkthdr.len += m1->m_len;
213 m->m_pkthdr.rcvif = 0;
214
215 /* *** Note: AT is sending out mbufs of type MSG_DATA,
216 not MT_DATA. *** */
217 #ifdef APPLETALK_DEBUG
218 if (m->m_next &&
219 !((m->m_next)->m_flags & M_EXT))
220 kprintf("po: mlen= %d, m2len= %d\n", m->m_len,
221 (m->m_next)->m_len);
222 #endif
223 atalk_unlock();
224 dlil_output(patp->aa_ifp, PF_APPLETALK, m, NULL, &dst, 0);
225 atalk_lock();
226
227 pktsOut++;
228 }
229
230 return 0;
231 } /* pat_output */
232
233 static void
234 at_input_packet(
235 __unused protocol_family_t protocol,
236 mbuf_t m)
237 {
238 struct mbuf *m1;
239 struct ifnet *ifp;
240 llc_header_t *llc_header;
241 at_ifaddr_t *ifID;
242 char src[6];
243 enet_header_t *enet_header;
244
245 if (!appletalk_inited) {
246 m_freem(m);
247 return;
248 }
249
250 if ((m->m_flags & M_PKTHDR) == 0) {
251 #ifdef APPLETALK_DEBUG
252 kprintf("atalkintr: no HDR on packet received");
253 #endif
254 m_freem(m);
255 return;
256 }
257
258 /* make sure the interface this packet was received on is configured
259 for AppleTalk */
260 ifp = m->m_pkthdr.rcvif;
261 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
262 if (ifID->aa_ifp && (ifID->aa_ifp == ifp))
263 break;
264 }
265 /* if we didn't find a matching interface */
266 if (!ifID) {
267 m_freem(m);
268 return; /* was EAFNOSUPPORT */
269 }
270
271 /* make sure the entire packet header is in the current mbuf */
272 if (m->m_len < ENET_LLC_SIZE &&
273 (m = m_pullup(m, ENET_LLC_SIZE)) == 0) {
274 #ifdef APPLETALK_DEBUG
275 kprintf("atalkintr: packet too small\n");
276 #endif
277 m_freem(m);
278 return;
279 }
280 enet_header = mtod(m, enet_header_t *);
281
282 /* Ignore multicast packets from local station */
283 /* *** Note: code for IFTYPE_TOKENTALK may be needed here. *** */
284 if (ifID->aa_ifp->if_type == IFT_ETHER ||
285 ifID->aa_ifp->if_type == IFT_L2VLAN ||
286 ifID->aa_ifp->if_type == IFT_IEEE8023ADLAG) {
287 bcopy((char *)enet_header->src, src, sizeof(src));
288
289 #ifdef COMMENT /* In order to receive packets from the Blue Box, we cannot
290 reject packets whose source address matches our local address.
291 */
292 if ((enet_header->dst[0] & 1) &&
293 (bcmp(src, ifID->xaddr, sizeof(src)) == 0)) {
294 /* Packet rejected: think it's a local mcast. */
295 m_freem(m);
296 return; /* was EAFNOSUPPORT */
297 }
298 #endif /* COMMENT */
299
300 llc_header = (llc_header_t *)(enet_header+1);
301
302 /* advance the mbuf pointers past the ethernet header */
303 m->m_data += ENET_LLC_SIZE;
304 m->m_len -= ENET_LLC_SIZE;
305
306 pktsIn++;
307
308 if (LLC_PROTO_EQUAL(llc_header->protocol,snap_proto_aarp)) {
309 (void)aarp_rcv_pkt(mtod(m, aarp_pkt_t *), ifID);
310 m_freem(m);
311 }
312 else if (LLC_PROTO_EQUAL(llc_header->protocol, snap_proto_ddp)) {
313 /* if we're a router take all pkts */
314 if (!ROUTING_MODE) {
315 if (aarp_chk_addr(mtod(m, at_ddp_t *), ifID)
316 == AARP_ERR_NOT_OURS) {
317 #ifdef APPLETALK_DEBUG
318 kprintf("pat_input: Packet Rejected: not for us? dest=%x.%x.%x.%x.%x.%x LLC_PROTO= %02x%02x\n",
319 enet_header->dst[0], enet_header->dst[1],
320 enet_header->dst[2], enet_header->dst[3],
321 enet_header->dst[4], enet_header->dst[5],
322 llc_header->protocol[3],
323 llc_header->protocol[4]);
324 #endif
325 m_freem(m);
326 return; /* was EAFNOSUPPORT */
327 }
328 }
329 MCHTYPE(m, MSG_DATA); /* set the mbuf type */
330
331 ifID->stats.rcv_packets++;
332 for (m1 = m; m1; m1 = m1->m_next)
333 ifID->stats.rcv_bytes += m1->m_len;
334
335 if (!MULTIPORT_MODE)
336 ddp_glean(m, ifID, src);
337
338 ddp_input(m, ifID);
339 } else {
340 #ifdef APPLETALK_DEBUG
341 kprintf("pat_input: Packet Rejected: wrong LLC_PROTO = %02x%02x\n",
342 llc_header->protocol[3],
343 llc_header->protocol[4]);
344 #endif
345 m_freem(m);
346 }
347 }
348 }