]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/at_pcb.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1997-1999 Apple Computer, Inc.
31 * All Rights Reserved.
33 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)at_pcb.c 8.2 (Berkeley) 1/4/94
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.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>
80 #include <kern/kern_types.h>
81 #include <kern/zalloc.h>
82 #include <kern/queue.h>
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>
95 extern struct atpcb ddp_head
;
96 extern struct atpcb
*atp_inputQ
[];
97 extern CCB
*adsp_inputQ
[];
98 extern at_ifaddr_t
*ifID_home
;
100 ddp_handler_func func
;
103 int DDP_chksum_on
= FALSE
;
104 int DDP_slfsnd_on
= FALSE
;
108 void at_memzone_init(void)
112 str_size
= (vm_size_t
)sizeof(struct atpcb
);
113 atpcb_zone
= (zone_t
)zinit(str_size
, 1000*str_size
, 8192, "atpcb zone");
116 int at_pcballoc(so
, head
)
120 register struct atpcb
*pcb
;
122 pcb
= (struct atpcb
*)zalloc(atpcb_zone
);
125 bzero((caddr_t
)pcb
, sizeof(*pcb
));
127 /* set the flags to the system defaults */
129 pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
131 pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
133 pcb
->ddp_flags
|= DDPFLG_SLFSND
;
135 pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
137 pcb
->atpcb_head
= head
;
138 pcb
->atpcb_socket
= so
;
139 atalk_lock(); /* makes sure the list is locked while inserting atpcb */
141 insque((queue_t
)pcb
, (queue_t
)head
);
142 so
->so_pcb
= (caddr_t
)pcb
;
148 int at_pcbdetach(pcb
)
151 struct socket
*so
= pcb
->atpcb_socket
;
153 /* Notify NBP that we are closing this DDP socket */
155 ddp_notify_nbp(pcb
->lport
, pcb
->pid
, pcb
->ddptype
);
160 so
->so_flags
|= SOF_PCBCLEARING
;
161 if ((pcb
->atpcb_next
) && (pcb
->atpcb_prev
))
162 remque((queue_t
)pcb
);
163 zfree(atpcb_zone
, pcb
);
168 int ddp_socket_inuse(ddpsock
, proto
)
169 u_char ddpsock
, proto
;
173 if ((!proto
|| (proto
== DDP_ATP
)) && atp_inputQ
[ddpsock
])
175 if ((!proto
|| (proto
== DDP_ADSP
)) && adsp_inputQ
[ddpsock
])
177 if (ddp_handler
[ddpsock
].func
)
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
)))
188 int at_pcbbind(pcb
, nam
)
189 register struct atpcb
*pcb
;
190 struct sockaddr
*nam
;
192 register struct sockaddr_at
*local
= (struct sockaddr_at
*) nam
;
193 u_char ddpsock
= local
->sat_port
;
195 if ((!ifID_home
) || (local
->sat_family
!= AF_APPLETALK
))
196 return(EADDRNOTAVAIL
);
198 if (pcb
->lport
!= ATADDR_ANYPORT
||
199 pcb
->laddr
.s_node
!= ATADDR_ANYNODE
||
200 pcb
->laddr
.s_net
!= ATADDR_ANYNET
)
203 /* Request for dynamic socket? */
205 /* Search table for free one */
206 /* *** borrow IP algorithm, instead? *** */
207 for (ddpsock
= DDP_SOCKET_LAST
;
208 ddpsock
>= (DDP_SOCKET_1st_DYNAMIC
+ 1);
211 if (! ddp_socket_inuse(ddpsock
, pcb
->ddptype
))
214 if (ddpsock
< (DDP_SOCKET_1st_DYNAMIC
+ 1))
215 return(EADDRNOTAVAIL
); /* Error if no free sockets */
217 /* Asking to open a socket by its number.
218 Check if its legal & free. */
219 if (ddpsock
> DDP_SOCKET_LAST
)
221 if (ddp_socket_inuse(ddpsock
, pcb
->ddptype
))
222 return(EADDRNOTAVAIL
);
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
) {
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
;
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
;