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