1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
53#include <netat/sysglue.h>
54#include <netat/appletalk.h>
55#include <netat/at_pcb.h>
56#include <netat/at_var.h>
57#include <netat/ddp.h>
58#include <netat/ep.h>
59#include <netat/rtmp.h>
60#include <netat/zip.h>
61#include <netat/routing_tables.h>
62#include <netat/nbp.h>
63
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,
73		struct ifnet *ifp, __unused struct proc *p)
74{
75	return(at_control(so, cmd, data, ifp));
76}
77
78
79int	ddp_pru_attach(struct socket *so, int proto,
80		   __unused struct proc *p)
81{
82	int error = 0;
83	struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
84
85        error = soreserve(so, ddp_sendspace, ddp_recvspace);
86	if (error != 0)
87		return error;
88
89	error = at_pcballoc(so, &ddp_head);
90	if (error)
91		return error;
92	pcb = (struct atpcb *)((so)->so_pcb);
93	pcb->pid = proc_selfpid();
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
104	int error = 0;
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);
114	at_pcbdetach(pcb);
115
116	return error;
117}
118
119
120int  ddp_pru_abort(struct socket *so)
121{
122	struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
123
124	if (pcb == NULL)
125		return (EINVAL);
126
127	soisdisconnected(so);
128	at_pcbdetach(pcb);
129
130	return 0;
131}
132
133int  ddp_pru_detach(struct socket *so)
134{
135	struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
136
137	if (pcb == NULL)
138		return (EINVAL);
139
140	at_pcbdetach(pcb);
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,
157		__unused struct proc *p)
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
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)
171{
172	at_ddp_t *ddp = NULL;
173	struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
174
175	if (pcb == NULL)
176		return (EINVAL);
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
185	if (!(pcb->ddp_flags & DDPFLG_HDRINCL)) {
186		/* prepend a DDP header */
187		M_PREPEND(m, DDP_X_HDR_SIZE, M_WAIT);
188		if (m == NULL)
189			return ENOBUFS;
190		ddp = mtod(m, at_ddp_t *);
191	}
192
193	if (so->so_state & SS_ISCONNECTED) {
194		if (addr)
195		        return EISCONN;
196
197		if (ddp) {
198			NET_ASSIGN(ddp->dst_net, pcb->raddr.s_net);
199			ddp->dst_node = pcb->raddr.s_node;
200			ddp->dst_socket = pcb->rport;
201		}
202	} else {
203		if (addr == NULL)
204			return ENOTCONN;
205
206		if (ddp) {
207			struct sockaddr_at *dst =
208				(struct sockaddr_at *) addr;
209			NET_ASSIGN(ddp->dst_net, dst->sat_addr.s_net);
210			ddp->dst_node = dst->sat_addr.s_node;
211			ddp->dst_socket = dst->sat_port;
212		}
213	}
214	if (ddp) {
215		DDPLEN_ASSIGN(ddp, m->m_pkthdr.len);
216		UAS_ASSIGN_HTON(ddp->checksum,
217			   (pcb->ddp_flags & DDPFLG_CHKSUM)? 1: 0);
218		ddp->type = (pcb->ddptype)? pcb->ddptype: DEFAULT_OT_DDPTYPE;
219#ifdef NOT_YET
220		NET_ASSIGN(ddp->src_net, pcb->laddr.s_net);
221		ddp->src_node = pcb->laddr.s_node;
222		ddp->src_socket = pcb->lport;
223#endif
224	} else {
225		ddp = mtod(m, at_ddp_t *);
226	}
227	if (NET_VALUE(ddp->dst_net) == ATADDR_ANYNET &&
228	    ddp->dst_node == ATADDR_BCASTNODE &&
229	    (pcb->ddp_flags & DDPFLG_SLFSND)) {
230		struct mbuf *n;
231
232		if ((n = m_dup(m, M_DONTWAIT))) {
233			at_ifaddr_t
234			  *ifID = ifID_home,
235			  *ifIDTmp = (at_ifaddr_t *)NULL;
236
237			/* as in ddp_output() loop processing, fill in the
238			   rest of the header */
239			ddp = mtod(n, at_ddp_t *);
240			if (MULTIHOME_MODE && (ifIDTmp = forUs(ddp)))
241				ifID = ifIDTmp;
242			NET_ASSIGN(ddp->src_net, ifID->ifThisNode.s_net);
243			ddp->src_node = ifID->ifThisNode.s_node;
244			ddp->src_socket = pcb->lport;
245			if (UAS_VALUE_NTOH(ddp->checksum))
246				UAS_ASSIGN_HTON(ddp->checksum, ddp_checksum(m, 4));
247			ddp_input(n, ifID);
248		}
249	}
250	return(ddp_output(&m, pcb->lport, FALSE));
251} /* ddp_pru_send */
252
253int   ddp_pru_sockaddr(struct socket *so,
254		   struct sockaddr **nam)
255{
256	struct atpcb *pcb;
257	struct sockaddr_at *sat;
258
259	MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
260	if (sat == NULL)
261		return(ENOMEM);
262	bzero((caddr_t)sat, sizeof(*sat));
263
264	if ((pcb = sotoatpcb(so)) == NULL) {
265		FREE(sat, M_SONAME);
266		return(EINVAL);
267	}
268
269	sat->sat_family = AF_APPLETALK;
270	sat->sat_len = sizeof(*sat);
271	sat->sat_port = pcb->lport;
272	sat->sat_addr = pcb->laddr;
273
274	*nam = (struct sockaddr *)sat;
275	return(0);
276}
277
278
279int  ddp_pru_peeraddr(struct socket *so,
280		  struct sockaddr **nam)
281{
282	struct atpcb *pcb;
283	struct sockaddr_at *sat;
284
285	MALLOC(sat, struct sockaddr_at *, sizeof *sat, M_SONAME, M_WAITOK);
286	if (sat == NULL)
287		return (ENOMEM);
288	bzero((caddr_t)sat, sizeof(*sat));
289
290	if ((pcb = sotoatpcb(so)) == NULL) {
291		FREE(sat, M_SONAME);
292		return(EINVAL);
293	}
294
295	sat->sat_family = AF_APPLETALK;
296	sat->sat_len = sizeof(*sat);
297	sat->sat_port = pcb->rport;
298	sat->sat_addr = pcb->raddr;
299
300	*nam = (struct sockaddr *)sat;
301	return(0);
302}
303
304
305int ddp_pru_connect(struct socket *so, struct sockaddr *nam,
306		   __unused struct proc *p)
307{
308	struct atpcb *pcb = (struct atpcb *)((so)->so_pcb);
309	struct sockaddr_at *faddr = (struct sockaddr_at *) nam;
310
311	if (pcb != NULL)
312		return (EINVAL);
313
314	if (xpatcnt == 0)
315		return (EADDRNOTAVAIL);
316
317	if (faddr->sat_family != AF_APPLETALK)
318		return (EAFNOSUPPORT);
319
320	pcb->raddr = faddr->sat_addr;
321	soisconnected(so);
322	return 0;
323}
324
325
326/*
327 * One-time AppleTalk initialization
328 */
329void ddp_init()
330{
331	at_memzone_init();
332	ddp_head.atpcb_next = ddp_head.atpcb_prev = &ddp_head;
333	init_ddp_handler();
334
335    /* Initialize protocols implemented in the kernel */
336	add_ddp_handler(EP_SOCKET, ep_input);
337	add_ddp_handler(ZIP_SOCKET, zip_router_input);
338	add_ddp_handler(NBP_SOCKET, nbp_input);
339	add_ddp_handler(DDP_SOCKET_1st_DYNAMIC, sip_input);
340
341	ddp_start();
342
343	appletalk_hack_start();
344} /* ddp_init */
345