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