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