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