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