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