]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/ddp_usrreq.c
7d327ab6008cb05d72c13e12c7df27f99e39f9a8
[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 s = splnet();
88 error = at_pcballoc(so, &ddp_head);
89 splx(s);
90 if (error)
91 return error;
92 error = soreserve(so, ddp_sendspace, ddp_recvspace);
93 pcb = (struct atpcb *)((so)->so_pcb);
94 pcb->pid = current_proc()->p_pid;
95 pcb->ddptype = (u_char) proto; /* set in socreate() */
96 pcb->proto = ATPROTO_DDP;
97
98 return error;
99 }
100
101
102 int ddp_pru_disconnect(struct socket *so)
103 {
104
105 int s, error = 0;
106 at_ddp_t *ddp = NULL;
107 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
108
109 if (pcb == NULL)
110 return (EINVAL);
111
112 if ((so->so_state & SS_ISCONNECTED) == 0)
113 return ENOTCONN;
114
115 soisdisconnected(so);
116 s = splnet();
117 at_pcbdetach(pcb);
118 splx(s);
119
120 return error;
121 }
122
123
124 int ddp_pru_abort(struct socket *so)
125 {
126 int s;
127 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
128
129 if (pcb == NULL)
130 return (EINVAL);
131
132 soisdisconnected(so);
133 s = splnet();
134 at_pcbdetach(pcb);
135 splx(s);
136
137 return 0;
138 }
139
140 int ddp_pru_detach(struct socket *so)
141 {
142 int s;
143 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
144
145 if (pcb == NULL)
146 return (EINVAL);
147
148 s = splnet();
149 at_pcbdetach(pcb);
150 splx(s);
151 return 0;
152 }
153
154 int ddp_pru_shutdown(struct socket *so)
155 {
156 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
157
158 if (pcb == NULL)
159 return (EINVAL);
160
161 socantsendmore(so);
162 return 0;
163 }
164
165
166 int ddp_pru_bind(struct socket *so, struct sockaddr *nam,
167 struct proc *p)
168 {
169 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
170
171 if (pcb == NULL)
172 return (EINVAL);
173
174 return (at_pcbbind(pcb, nam));
175 }
176
177
178 int ddp_pru_send(struct socket *so, int flags, struct mbuf *m,
179 struct sockaddr *addr, struct mbuf *control,
180 struct proc *p)
181 {
182 at_ddp_t *ddp = NULL;
183 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
184
185 if (pcb == NULL)
186 return (EINVAL);
187
188 /*
189 * Set type to MSG_DATA. Otherwise looped back packet is not
190 * recognized by atp_input() and possibly other protocols.
191 */
192
193 MCHTYPE(m, MSG_DATA);
194
195 if (!(pcb->ddp_flags & DDPFLG_HDRINCL)) {
196 /* prepend a DDP header */
197 M_PREPEND(m, DDP_X_HDR_SIZE, M_WAIT);
198 ddp = mtod(m, at_ddp_t *);
199 }
200
201 if (so->so_state & SS_ISCONNECTED) {
202 if (addr)
203 return EISCONN;
204
205 if (ddp) {
206 NET_ASSIGN(ddp->dst_net, pcb->raddr.s_net);
207 ddp->dst_node = pcb->raddr.s_node;
208 ddp->dst_socket = pcb->rport;
209 }
210 } else {
211 if (addr == NULL)
212 return ENOTCONN;
213
214 if (ddp) {
215 struct sockaddr_at *dst =
216 (struct sockaddr_at *) addr;
217 NET_ASSIGN(ddp->dst_net, dst->sat_addr.s_net);
218 ddp->dst_node = dst->sat_addr.s_node;
219 ddp->dst_socket = dst->sat_port;
220 }
221 }
222 if (ddp) {
223 ddp->length = m->m_pkthdr.len;
224 UAS_ASSIGN(ddp->checksum,
225 (pcb->ddp_flags & DDPFLG_CHKSUM)? 1: 0);
226 ddp->type = (pcb->ddptype)? pcb->ddptype: DEFAULT_OT_DDPTYPE;
227 #ifdef NOT_YET
228 NET_ASSIGN(ddp->src_net, pcb->laddr.s_net);
229 ddp->src_node = pcb->laddr.s_node;
230 ddp->src_socket = pcb->lport;
231 #endif
232 } else {
233 ddp = mtod(m, at_ddp_t *);
234 }
235 if (NET_VALUE(ddp->dst_net) == ATADDR_ANYNET &&
236 ddp->dst_node == ATADDR_BCASTNODE &&
237 (pcb->ddp_flags & DDPFLG_SLFSND)) {
238 struct mbuf *n;
239
240 if ((n = m_dup(m, M_DONTWAIT))) {
241 at_ifaddr_t
242 *ifID = ifID_home,
243 *ifIDTmp = (at_ifaddr_t *)NULL;
244
245 /* as in ddp_output() loop processing, fill in the
246 rest of the header */
247 ddp = mtod(n, at_ddp_t *);
248 if (MULTIHOME_MODE && (ifIDTmp = forUs(ddp)))
249 ifID = ifIDTmp;
250 NET_ASSIGN(ddp->src_net, ifID->ifThisNode.s_net);
251 ddp->src_node = ifID->ifThisNode.s_node;
252 ddp->src_socket = pcb->lport;
253 if (UAS_VALUE(ddp->checksum))
254 UAS_ASSIGN(ddp->checksum, ddp_checksum(m, 4));
255 ddp_input(n, ifID);
256 }
257 }
258 return(ddp_output(&m, pcb->lport, FALSE));
259 } /* ddp_pru_send */
260
261 int ddp_pru_sockaddr(struct socket *so,
262 struct sockaddr **nam)
263 {
264 int s;
265 struct atpcb *pcb;
266 struct sockaddr_at *sat;
267
268 MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
269 if (sat == NULL)
270 return(ENOMEM);
271 bzero((caddr_t)sat, sizeof(*sat));
272
273 s = splnet();
274 if ((pcb = sotoatpcb(so)) == NULL) {
275 splx(s);
276 FREE(sat, M_SONAME);
277 return(EINVAL);
278 }
279
280 sat->sat_family = AF_APPLETALK;
281 sat->sat_len = sizeof(*sat);
282 sat->sat_port = pcb->lport;
283 sat->sat_addr = pcb->laddr;
284 splx(s);
285
286 *nam = (struct sockaddr *)sat;
287 return(0);
288 }
289
290
291 int ddp_pru_peeraddr(struct socket *so,
292 struct sockaddr **nam)
293 {
294 int s;
295 struct atpcb *pcb;
296 struct sockaddr_at *sat;
297
298 MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
299 if (sat == NULL)
300 return (ENOMEM);
301 bzero((caddr_t)sat, sizeof(*sat));
302
303 s = splnet();
304 if ((pcb = sotoatpcb(so)) == NULL) {
305 splx(s);
306 FREE(sat, M_SONAME);
307 return(EINVAL);
308 }
309
310 sat->sat_family = AF_APPLETALK;
311 sat->sat_len = sizeof(*sat);
312 sat->sat_port = pcb->rport;
313 sat->sat_addr = pcb->raddr;
314 splx(s);
315
316 *nam = (struct sockaddr *)sat;
317 return(0);
318 }
319
320
321 int ddp_pru_connect(struct socket *so, struct sockaddr *nam,
322 struct proc *p)
323 {
324 struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
325 struct sockaddr_at *faddr = (struct sockaddr_at *) nam;
326
327 if (pcb != NULL)
328 return (EINVAL);
329
330 if (xpatcnt == 0)
331 return (EADDRNOTAVAIL);
332
333 if (faddr->sat_family != AF_APPLETALK)
334 return (EAFNOSUPPORT);
335
336 pcb->raddr = faddr->sat_addr;
337 soisconnected(so);
338 return 0;
339 }
340
341
342
343 /*
344 * One-time AppleTalk initialization
345 */
346 void ddp_init()
347 {
348 at_memzone_init();
349 ddp_head.atpcb_next = ddp_head.atpcb_prev = &ddp_head;
350 init_ddp_handler();
351
352 /* Initialize protocols implemented in the kernel */
353 add_ddp_handler(EP_SOCKET, ep_input);
354 add_ddp_handler(ZIP_SOCKET, zip_router_input);
355 add_ddp_handler(NBP_SOCKET, nbp_input);
356 add_ddp_handler(DDP_SOCKET_1st_DYNAMIC, sip_input);
357
358 ddp_start();
359
360 appletalk_hack_start();
361 } /* ddp_init */
362