]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/at_pcb.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / netat / at_pcb.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29/*
30 * Copyright (c) 1997-1999 Apple Computer, Inc.
31 * All Rights Reserved.
32 */
33/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
34/*
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)at_pcb.c 8.2 (Berkeley) 1/4/94
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/protosw.h>
74#include <sys/socket.h>
75#include <sys/socketvar.h>
76#include <sys/ioctl.h>
77#include <sys/errno.h>
78#include <sys/time.h>
79#include <sys/proc.h>
80#include <kern/kern_types.h>
81#include <kern/zalloc.h>
82#include <kern/queue.h>
83
84#include <net/if.h>
85
86#include <netat/sysglue.h>
87#include <netat/appletalk.h>
88#include <netat/ddp.h>
89#include <netat/at_pcb.h>
90#include <netat/debug.h>
91#include <netat/at_var.h>
92#include <netat/adsp.h>
93#include <netat/adsp_internal.h>
94
95extern struct atpcb ddp_head;
96extern struct atpcb *atp_inputQ[];
97extern CCB *adsp_inputQ[];
98extern at_ifaddr_t *ifID_home;
99extern struct {
100 void (*func)();
101 } ddp_handler[];
102
103int DDP_chksum_on = FALSE;
104int DDP_slfsnd_on = FALSE;
105
106zone_t atpcb_zone;
107
108void at_memzone_init()
109{
110 vm_size_t str_size;
111
112 str_size = (vm_size_t)sizeof(struct atpcb);
113 atpcb_zone = (zone_t)zinit(str_size, 1000*str_size, 8192, "atpcb zone");
114}
115
116int at_pcballoc(so, head)
117 struct socket *so;
118 struct atpcb *head;
119{
120 register struct atpcb *pcb;
121
122 pcb = (struct atpcb *)zalloc(atpcb_zone);
123 if (pcb == NULL)
124 return (ENOBUFS);
125 bzero((caddr_t)pcb, sizeof(*pcb));
126
127 /* set the flags to the system defaults */
128 if (DDP_chksum_on)
129 pcb->ddp_flags |= DDPFLG_CHKSUM;
130 else
131 pcb->ddp_flags &= ~DDPFLG_CHKSUM;
132 if (DDP_slfsnd_on)
133 pcb->ddp_flags |= DDPFLG_SLFSND;
134 else
135 pcb->ddp_flags &= ~DDPFLG_SLFSND;
136
137 pcb->atpcb_head = head;
138 pcb->atpcb_socket = so;
91447636 139 atalk_lock(); /* makes sure the list is locked while inserting atpcb */
1c79356b
A
140 if (head)
141 insque((queue_t)pcb, (queue_t)head);
142 so->so_pcb = (caddr_t)pcb;
91447636 143 atalk_unlock();
1c79356b
A
144
145 return (0);
146}
147
148int at_pcbdetach(pcb)
149 struct atpcb *pcb;
150{
151 struct socket *so = pcb->atpcb_socket;
152
153 /* Notify NBP that we are closing this DDP socket */
154 if (pcb->lport) {
155 ddp_notify_nbp(pcb->lport, pcb->pid, pcb->ddptype);
156 pcb->lport = 0;
157 }
158
159 so->so_pcb = 0;
91447636 160 so->so_flags |= SOF_PCBCLEARING;
1c79356b
A
161 if ((pcb->atpcb_next) && (pcb->atpcb_prev))
162 remque((queue_t)pcb);
91447636 163 zfree(atpcb_zone, pcb);
1c79356b
A
164 sofree(so);
165 return(0);
166}
167
168int ddp_socket_inuse(ddpsock, proto)
169 u_char ddpsock, proto;
170{
171 struct atpcb *pcb;
172
173 if ((!proto || (proto == DDP_ATP)) && atp_inputQ[ddpsock])
174 return TRUE;
175 if ((!proto || (proto == DDP_ADSP)) && adsp_inputQ[ddpsock])
176 return TRUE;
177 if (ddp_handler[ddpsock].func)
178 return TRUE;
179 for (pcb = ddp_head.atpcb_next; pcb != &ddp_head;
180 pcb = pcb->atpcb_next) {
181 if (pcb->lport == ddpsock &&
182 (!pcb->ddptype || !proto || (pcb->ddptype == proto)))
183 return TRUE;
184 }
185 return FALSE;
186}
187
188int at_pcbbind(pcb, nam)
189 register struct atpcb *pcb;
190 struct sockaddr *nam;
191{
1c79356b
A
192 register struct sockaddr_at *local = (struct sockaddr_at *) nam;
193 u_char ddpsock = local->sat_port;
194
195 if ((!ifID_home) || (local->sat_family != AF_APPLETALK))
196 return(EADDRNOTAVAIL);
197
198 if (pcb->lport != ATADDR_ANYPORT ||
199 pcb->laddr.s_node != ATADDR_ANYNODE ||
200 pcb->laddr.s_net != ATADDR_ANYNET)
201 return(EINVAL);
202
203 /* Request for dynamic socket? */
204 if (ddpsock == 0) {
205 /* Search table for free one */
206 /* *** borrow IP algorithm, instead? *** */
207 for (ddpsock = DDP_SOCKET_LAST;
208 ddpsock >= (DDP_SOCKET_1st_DYNAMIC + 1);
209 /* sip has 1st */
210 ddpsock--) {
211 if (! ddp_socket_inuse(ddpsock, pcb->ddptype))
212 break;
213 }
214 if (ddpsock < (DDP_SOCKET_1st_DYNAMIC + 1))
215 return(EADDRNOTAVAIL); /* Error if no free sockets */
216 } else {
217 /* Asking to open a socket by its number.
218 Check if its legal & free. */
219 if (ddpsock > DDP_SOCKET_LAST)
220 return(EINVAL);
221 if (ddp_socket_inuse(ddpsock, pcb->ddptype))
222 return(EADDRNOTAVAIL);
223 }
224
225 pcb->lport = ddpsock;
226 /* if address is specified, make sure address matches one of the
227 interfaces configured for AppleTalk */
228 if (local->sat_addr.s_net || local->sat_addr.s_node) {
229 if (MULTIHOME_MODE) {
230 at_ifaddr_t *ifID;
231 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
232 if (ifID->ifThisNode.s_net == local->sat_addr.s_net &&
233 ifID->ifThisNode.s_node == local->sat_addr.s_node) {
234 pcb->laddr = local->sat_addr;
235 return(0);
236 }
237 }
238 return(EINVAL);
239 } else {
240 /* for single-port and router modes if the local address is
241 specified, it must match the default interface, which is
242 what will be put into packets' source address anyway */
243 if (ifID_home->ifThisNode.s_net == local->sat_addr.s_net &&
244 ifID_home->ifThisNode.s_node == local->sat_addr.s_node) {
245 pcb->laddr = local->sat_addr;
246 return(0);
247 }
248 return(EINVAL);
249
250 }
251 }
252 return(0);
253}