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