]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/ddp_usrreq.c
fde242f787bb7d9e7a837098e07e8e5b51da1584
[apple/xnu.git] / bsd / netat / ddp_usrreq.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1998 Apple Computer, Inc.
32 */
33
34 /* ddp_usrreq.c
35 */
36
37 #include <sys/errno.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <machine/spl.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/filedesc.h>
45 #include <sys/fcntl.h>
46 #include <sys/mbuf.h>
47 #include <sys/ioctl.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52
53 #include <net/if.h>
54
55 #include <netat/appletalk.h>
56 #include <netat/at_var.h>
57 #include <netat/sysglue.h>
58 #include <netat/ddp.h>
59 #include <netat/ep.h>
60 #include <netat/rtmp.h>
61 #include <netat/zip.h>
62 #include <netat/at_pcb.h>
63 #include <netat/routing_tables.h>
64 #include <netat/nbp.h>
65
66 extern int at_control(), at_memzone_init();
67 extern void nbp_input(), ep_input(), zip_router_input(),
68 sip_input(), add_ddp_handler(), init_ddp_handler(),
69 ddp_start(), ddp_input(), appletalk_hack_start();
70 extern u_short ddp_checksum();
71 extern at_ifaddr_t *forUs();
72 extern struct mbuf *m_dup(struct mbuf *, int);
73
74 extern at_ifaddr_t *ifID_home;
75 extern int xpatcnt;
76
77 struct atpcb ddp_head;
78 u_long ddp_sendspace = 600, /* *** what should this value be? *** */
79 ddp_recvspace = 50 * (600 + sizeof(struct sockaddr_at));
80
81 int ddp_pru_control(struct socket *so, u_long cmd, caddr_t data,
82 struct ifnet *ifp, struct proc *p)
83 {
84 return(at_control(so, cmd, data, ifp));
85 }
86
87
88 int ddp_pru_attach(struct socket *so, int proto,
89 struct proc *p)
90 {
91 int s, error = 0;
92 at_ddp_t *ddp = NULL;
93 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
94
95 error = soreserve(so, ddp_sendspace, ddp_recvspace);
96 if (error != 0)
97 return error;
98
99 s = splnet();
100 error = at_pcballoc(so, &ddp_head);
101 splx(s);
102 if (error)
103 return error;
104 pcb = (struct atpcb *)((so)->so_pcb);
105 pcb->pid = proc_selfpid();
106 pcb->ddptype = (u_char) proto; /* set in socreate() */
107 pcb->proto = ATPROTO_DDP;
108
109 return error;
110 }
111
112
113 int ddp_pru_disconnect(struct socket *so)
114 {
115
116 int s, error = 0;
117 at_ddp_t *ddp = NULL;
118 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
119
120 if (pcb == NULL)
121 return (EINVAL);
122
123 if ((so->so_state & SS_ISCONNECTED) == 0)
124 return ENOTCONN;
125
126 soisdisconnected(so);
127 s = splnet();
128 at_pcbdetach(pcb);
129 splx(s);
130
131 return error;
132 }
133
134
135 int ddp_pru_abort(struct socket *so)
136 {
137 int s;
138 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
139
140 if (pcb == NULL)
141 return (EINVAL);
142
143 soisdisconnected(so);
144 s = splnet();
145 at_pcbdetach(pcb);
146 splx(s);
147
148 return 0;
149 }
150
151 int ddp_pru_detach(struct socket *so)
152 {
153 int s;
154 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
155
156 if (pcb == NULL)
157 return (EINVAL);
158
159 s = splnet();
160 at_pcbdetach(pcb);
161 splx(s);
162 return 0;
163 }
164
165 int ddp_pru_shutdown(struct socket *so)
166 {
167 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
168
169 if (pcb == NULL)
170 return (EINVAL);
171
172 socantsendmore(so);
173 return 0;
174 }
175
176
177 int ddp_pru_bind(struct socket *so, struct sockaddr *nam,
178 struct proc *p)
179 {
180 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
181
182 if (pcb == NULL)
183 return (EINVAL);
184
185 return (at_pcbbind(pcb, nam));
186 }
187
188
189 int ddp_pru_send(struct socket *so, int flags, struct mbuf *m,
190 struct sockaddr *addr, struct mbuf *control,
191 struct proc *p)
192 {
193 at_ddp_t *ddp = NULL;
194 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
195
196 if (pcb == NULL)
197 return (EINVAL);
198
199 /*
200 * Set type to MSG_DATA. Otherwise looped back packet is not
201 * recognized by atp_input() and possibly other protocols.
202 */
203
204 MCHTYPE(m, MSG_DATA);
205
206 if (!(pcb->ddp_flags & DDPFLG_HDRINCL)) {
207 /* prepend a DDP header */
208 M_PREPEND(m, DDP_X_HDR_SIZE, M_WAIT);
209 ddp = mtod(m, at_ddp_t *);
210 }
211
212 if (so->so_state & SS_ISCONNECTED) {
213 if (addr)
214 return EISCONN;
215
216 if (ddp) {
217 NET_ASSIGN(ddp->dst_net, pcb->raddr.s_net);
218 ddp->dst_node = pcb->raddr.s_node;
219 ddp->dst_socket = pcb->rport;
220 }
221 } else {
222 if (addr == NULL)
223 return ENOTCONN;
224
225 if (ddp) {
226 struct sockaddr_at *dst =
227 (struct sockaddr_at *) addr;
228 NET_ASSIGN(ddp->dst_net, dst->sat_addr.s_net);
229 ddp->dst_node = dst->sat_addr.s_node;
230 ddp->dst_socket = dst->sat_port;
231 }
232 }
233 if (ddp) {
234 ddp->length = m->m_pkthdr.len;
235 UAS_ASSIGN(ddp->checksum,
236 (pcb->ddp_flags & DDPFLG_CHKSUM)? 1: 0);
237 ddp->type = (pcb->ddptype)? pcb->ddptype: DEFAULT_OT_DDPTYPE;
238 #ifdef NOT_YET
239 NET_ASSIGN(ddp->src_net, pcb->laddr.s_net);
240 ddp->src_node = pcb->laddr.s_node;
241 ddp->src_socket = pcb->lport;
242 #endif
243 } else {
244 ddp = mtod(m, at_ddp_t *);
245 }
246 if (NET_VALUE(ddp->dst_net) == ATADDR_ANYNET &&
247 ddp->dst_node == ATADDR_BCASTNODE &&
248 (pcb->ddp_flags & DDPFLG_SLFSND)) {
249 struct mbuf *n;
250
251 if ((n = m_dup(m, M_DONTWAIT))) {
252 at_ifaddr_t
253 *ifID = ifID_home,
254 *ifIDTmp = (at_ifaddr_t *)NULL;
255
256 /* as in ddp_output() loop processing, fill in the
257 rest of the header */
258 ddp = mtod(n, at_ddp_t *);
259 if (MULTIHOME_MODE && (ifIDTmp = forUs(ddp)))
260 ifID = ifIDTmp;
261 NET_ASSIGN(ddp->src_net, ifID->ifThisNode.s_net);
262 ddp->src_node = ifID->ifThisNode.s_node;
263 ddp->src_socket = pcb->lport;
264 if (UAS_VALUE(ddp->checksum))
265 UAS_ASSIGN(ddp->checksum, ddp_checksum(m, 4));
266 ddp_input(n, ifID);
267 }
268 }
269 return(ddp_output(&m, pcb->lport, FALSE));
270 } /* ddp_pru_send */
271
272 int ddp_pru_sockaddr(struct socket *so,
273 struct sockaddr **nam)
274 {
275 int s;
276 struct atpcb *pcb;
277 struct sockaddr_at *sat;
278
279 MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
280 if (sat == NULL)
281 return(ENOMEM);
282 bzero((caddr_t)sat, sizeof(*sat));
283
284 s = splnet();
285 if ((pcb = sotoatpcb(so)) == NULL) {
286 splx(s);
287 FREE(sat, M_SONAME);
288 return(EINVAL);
289 }
290
291 sat->sat_family = AF_APPLETALK;
292 sat->sat_len = sizeof(*sat);
293 sat->sat_port = pcb->lport;
294 sat->sat_addr = pcb->laddr;
295 splx(s);
296
297 *nam = (struct sockaddr *)sat;
298 return(0);
299 }
300
301
302 int ddp_pru_peeraddr(struct socket *so,
303 struct sockaddr **nam)
304 {
305 int s;
306 struct atpcb *pcb;
307 struct sockaddr_at *sat;
308
309 MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
310 if (sat == NULL)
311 return (ENOMEM);
312 bzero((caddr_t)sat, sizeof(*sat));
313
314 s = splnet();
315 if ((pcb = sotoatpcb(so)) == NULL) {
316 splx(s);
317 FREE(sat, M_SONAME);
318 return(EINVAL);
319 }
320
321 sat->sat_family = AF_APPLETALK;
322 sat->sat_len = sizeof(*sat);
323 sat->sat_port = pcb->rport;
324 sat->sat_addr = pcb->raddr;
325 splx(s);
326
327 *nam = (struct sockaddr *)sat;
328 return(0);
329 }
330
331
332 int ddp_pru_connect(struct socket *so, struct sockaddr *nam,
333 struct proc *p)
334 {
335 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
336 struct sockaddr_at *faddr = (struct sockaddr_at *) nam;
337
338 if (pcb != NULL)
339 return (EINVAL);
340
341 if (xpatcnt == 0)
342 return (EADDRNOTAVAIL);
343
344 if (faddr->sat_family != AF_APPLETALK)
345 return (EAFNOSUPPORT);
346
347 pcb->raddr = faddr->sat_addr;
348 soisconnected(so);
349 return 0;
350 }
351
352
353
354 /*
355 * One-time AppleTalk initialization
356 */
357 void ddp_init()
358 {
359 at_memzone_init();
360 ddp_head.atpcb_next = ddp_head.atpcb_prev = &ddp_head;
361 init_ddp_handler();
362
363 /* Initialize protocols implemented in the kernel */
364 add_ddp_handler(EP_SOCKET, ep_input);
365 add_ddp_handler(ZIP_SOCKET, zip_router_input);
366 add_ddp_handler(NBP_SOCKET, nbp_input);
367 add_ddp_handler(DDP_SOCKET_1st_DYNAMIC, sip_input);
368
369 ddp_start();
370
371 appletalk_hack_start();
372 } /* ddp_init */
373