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