2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1996 Apple Computer, Inc.
28 * Created April 25, 1996, by Justin C. Walker
29 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
35 * Kernel process to implement the AURP daemon:
36 * manage tunnels to remote AURP servers across IP networks
39 #include <sys/errno.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <machine/spl.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/filedesc.h>
47 #include <sys/fcntl.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/malloc.h>
54 #include <netinet/in.h>
57 #include <netat/sysglue.h>
58 #include <netat/appletalk.h>
59 #include <netat/at_var.h>
60 #include <netat/routing_tables.h>
61 #include <netat/at_pcb.h>
62 #include <netat/aurp.h>
63 #include <netat/debug.h>
65 #define M_RCVBUF (64 * 1024)
66 #define M_SNDBUF (64 * 1024)
68 static int ip_to_atalk(struct sockaddr_in
*fp
, register gbuf_t
*p_mbuf
);
69 static int aurp_bindrp(struct socket
*so
);
71 struct aurp_global_t aurp_global
;
74 * Initialize the aurp pipe -
75 * -Create, initialize, and start the aurpd kernel process; we need
76 * a process to permit queueing between the socket and the stream,
77 * which is necessary for orderly access to the socket structure.
78 * -The user process (aurpd) is there to 'build' the AURP
79 * stream, act as a 'logging agent' (:-}), and hold open the stream
81 * -Data and AURP packets from the DDP stream will be fed into the
82 * UDP tunnel (AURPsend())
83 * -Data and AURP packets from the UDP tunnel will be fed into the
84 * DDP stream (ip_to_atalk(), via the kernel process).
90 register struct socket
*so
;
95 if (suser(current_proc()->p_ucred
, ¤t_proc()->p_acflag
) != 0 )
99 * Set up state prior to starting kernel process so we can back out
100 * (error return) if something goes wrong.
102 bzero((char *)&aurp_global
.tunnel
, sizeof(aurp_global
.tunnel
));
103 /*lock_alloc(&aurp_global.glock, LOCK_ALLOC_PIN, AURP_EVNT_LOCK, -1);*/
104 ATLOCKINIT(aurp_global
.glock
);
105 ATEVENTINIT(aurp_global
.event_anchor
);
107 /* open udp socket */
108 if (aurp_global
.udp_port
== 0)
109 aurp_global
.udp_port
= AURP_SOCKNUM
;
110 error
= socreate(AF_INET
, &aurp_global
.tunnel
, SOCK_DGRAM
,
113 { dPrintf(D_M_AURP
, D_L_FATAL
, ("AURP: Can't get socket (%d)\n",
118 so
= aurp_global
.tunnel
;
120 if ((error
= aurp_bindrp(so
)) != 0)
121 { dPrintf(D_M_AURP
, D_L_FATAL
,
122 ("AURP: Can't bind to port %d (error %d)\n",
123 aurp_global
.udp_port
, error
));
128 sblock(&so
->so_rcv
, M_WAIT
);
129 sblock(&so
->so_snd
, M_WAIT
);
132 * Set socket Receive buffer size
134 m
= m_get(M_WAIT
, MT_SOOPTS
);
140 sopt
.sopt_val
= &maxbuf
;
141 sopt
.sopt_valsize
= sizeof(maxbuf
);
142 sopt
.sopt_level
= SOL_SOCKET
;
143 sopt
.sopt_name
= SO_RCVBUF
;
144 sopt
.sopt_dir
= SOPT_SET
;
146 if ((error
= sosetopt(so
, &sopt
)) != 0)
151 * Set socket Send buffer size
153 m
= m_get(M_WAIT
, MT_SOOPTS
);
160 sopt
.sopt_val
= &maxbuf
;
161 sopt
.sopt_valsize
= sizeof(maxbuf
);
162 sopt
.sopt_level
= SOL_SOCKET
;
163 sopt
.sopt_name
= SO_SNDBUF
;
164 sopt
.sopt_dir
= SOPT_SET
;
166 if ((error
= sosetopt(so
, &sopt
)) != 0)
170 so
->so_upcall
= aurp_wakeup
;
171 so
->so_upcallarg
= (caddr_t
)AE_UDPIP
; /* Yuck */
172 so
->so_state
|= SS_NBIO
;
173 so
->so_rcv
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
174 so
->so_snd
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
177 sbunlock(&so
->so_snd
);
178 sbunlock(&so
->so_rcv
);
186 { register struct socket
*so
;
187 register int s
, events
;
189 so
= aurp_global
.tunnel
;
193 { gbuf_t
*from
, *p_mbuf
;
194 int flags
= MSG_DONTWAIT
;
198 * Wait for a package to arrive. This will be from the
199 * IP side - sowakeup() calls aurp_wakeup()
200 * when a packet arrives
203 ATDISABLE(s
, aurp_global
.glock
);
204 events
= aurp_global
.event
;
205 if (((*err
== 0) || (*err
== EWOULDBLOCK
)) && events
== 0)
207 *err
= tsleep(&aurp_global
.event_anchor
, PSOCK
| PCATCH
, "AURPgetmsg", 0);
208 events
= aurp_global
.event
;
209 aurp_global
.event
= 0;
211 ATENABLE(s
, aurp_global
.glock
);
214 * Shut down if we have the AE_SHUTDOWN event or if we got
215 * a system error other than EWOULDBLOCK, such as EINTR.
217 if (((*err
!= EWOULDBLOCK
) && (*err
!= 0)) || events
& AE_SHUTDOWN
)
219 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
220 ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
221 aurp_global
.shutdown
= 1;
222 while (aurp_global
.running
)
224 /*lock_free(&aurp_global.glock);*/
225 aurp_global
.tunnel
= 0;
226 aurp_global
.event
= 0;
227 aurp_global
.shutdown
= 0;
231 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
232 ("AURPgetmsg: shutdown completed\n"));
239 * Set up the nominal uio structure -
240 * give it no iov's, point off to non-existant user space,
241 * but make sure the 'resid' count means somehting.
246 auio
.uio_segflg
= UIO_SYSSPACE
;
247 auio
.uio_offset
= 0; /* XXX */
249 /* Keep up an even flow... */
253 * This should be large enough to encompass a full DDP packet plus
256 #define A_LARGE_SIZE 700
258 flags
= MSG_DONTWAIT
;
259 auio
.uio_resid
= A_LARGE_SIZE
;
260 *err
= soreceive(so
, (struct sockaddr
**)&from
, &auio
, &p_mbuf
, 0, &flags
);
261 dPrintf(D_M_AURP
, D_L_VERBOSE
,
262 ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err
, events
));
263 /* soreceive() sets *mp to zero! at start */
265 ip_to_atalk(from
, p_mbuf
);
266 if (*err
|| (p_mbuf
== NULL
)) {
268 * An error occurred in soreceive(),
269 * so clear the data input event flag
270 * and break out of this inner loop.
272 * XXX Note that clearing AE_UDPIP here could
273 * cause us to lose an AE_UDPIP event that
274 * was posted in aurp_global.event between
275 * the soreceive() above and the code here.
276 * The protocol should recover from this
277 * lost event, though, since the next
278 * request (a tickle, for example) from
279 * the other end of the tunnel will cause
280 * another AE_UDPIP event to be posted,
281 * which will wake us from the sleep at
282 * the top of the outer loop.
285 ATDISABLE(s
, aurp_global
.glock
);
286 aurp_global
.event
&= ~AE_UDPIP
;
287 ATENABLE(s
, aurp_global
.glock
);
288 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err
, (unsigned int) p_mbuf
));
297 * Wakeup the sleeping giant - we've put a message on his queue(s).
298 * The arg indicates what queue has been updated.
300 * This conforms to the so_upcall function pointer member of struct sockbuf.
302 void aurp_wakeup(struct socket
*so
, register caddr_t p
, int state
)
308 ATDISABLE(s
, aurp_global
.glock
);
309 aurp_global
.event
|= bit
;
310 ATENABLE(s
, aurp_global
.glock
);
312 dPrintf(D_M_AURP
, D_L_STATE_CHG
,
313 ("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n",
314 bit
, aurp_global
.event
));
316 wakeup(&aurp_global
.event_anchor
);
320 * Try to bind to the specified reserved port.
321 * Sort of like sobind(), but no suser() check.
324 aurp_bindrp(struct socket
*so
)
326 struct sockaddr_in sin
;
327 struct proc
*p
= current_proc();
332 bzero(&sin
, sizeof(sin
));
333 sin
.sin_family
= AF_INET
;
334 sin
.sin_addr
.s_addr
= htons(aurp_global
.src_addr
);
335 sin
.sin_port
= htons(aurp_global
.udp_port
);
336 sin
.sin_len
= sizeof(struct sockaddr_in
);
338 sblock(&so
->so_rcv
, M_WAIT
);
339 sblock(&so
->so_snd
, M_WAIT
);
340 so
->so_state
|= SS_PRIV
;
341 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, (struct sockaddr
*) &sin
, p
);
342 sbunlock(&so
->so_snd
);
343 sbunlock(&so
->so_rcv
);
350 * fp is the 'source address' mbuf; p_mbuf is the data mbuf.
351 * Use the source address to find the 'node number' (index of the address),
352 * and pass that to the next stage.
354 int ip_to_atalk(register struct sockaddr_in
*rem_addr
, register gbuf_t
*p_mbuf
)
356 register aurp_domain_t
*domain
;
360 /* determine the node where the packet came from */
361 for (node
=1; node
<= dst_addr_cnt
; node
++) {
362 if (aurp_global
.dst_addr
[node
] == *(long *)&rem_addr
->sin_addr
)
365 if (node
> dst_addr_cnt
) {
366 dPrintf(D_M_AURP
, D_L_WARNING
,
367 ("AURPrecv: invalid node, %d.%lx\n",
369 rem_addr
->sin_addr
.s_addr
));
372 FREE(rem_addr
, M_SONAME
);
376 /* validate the domain */
377 domain
= (aurp_domain_t
*)gbuf_rptr(p_mbuf
);
378 if ( (domain
->dst_length
!= IP_LENGTH
) ||
379 (domain
->dst_authority
!= IP_AUTHORITY
) ||
380 (domain
->version
!= AUD_Version
) ||
381 ((domain
->type
!= AUD_Atalk
) && (domain
->type
!= AUD_AURP
)) ) {
382 dPrintf(D_M_AURP
, D_L_WARNING
,
383 ("AURPrecv: invalid domain, %d.%lx\n",
385 rem_addr
->sin_addr
.s_addr
));
388 FREE(rem_addr
, M_SONAME
);
392 /* Remove domain header */
393 p_mbuf
->m_pkthdr
.len
-= IP_DOMAINSIZE
;
394 gbuf_rinc(p_mbuf
,IP_DOMAINSIZE
);
395 gbuf_set_type(p_mbuf
, MSG_DATA
);
397 /* forward the packet to the local AppleTalk stack */
399 at_insert(p_mbuf
, domain
->type
, node
);
400 FREE(rem_addr
, M_SONAME
);
406 * The real work has been done already. Here, we just cobble together
407 * a sockaddr for the destination and call sosend().
410 atalk_to_ip(register gbuf_t
*m
)
411 { register aurp_domain_t
*domain
;
413 int flags
= MSG_DONTWAIT
;
414 struct sockaddr_in rem_addr
;
417 m
->m_type
= MT_HEADER
;
418 m
->m_pkthdr
.len
= gbuf_msgsize(m
);
419 m
->m_pkthdr
.rcvif
= 0;
421 bzero((char *) &rem_addr
, sizeof(rem_addr
));
422 rem_addr
.sin_family
= PF_INET
;
423 rem_addr
.sin_port
= aurp_global
.udp_port
;
424 rem_addr
.sin_len
= sizeof (struct sockaddr_in
);
425 domain
= (aurp_domain_t
*)gbuf_rptr(m
);
426 *(long *) &rem_addr
.sin_addr
= domain
->dst_address
;
428 ATDISABLE(s
, aurp_global
.glock
);
429 aurp_global
.running
++;
430 ATENABLE(s
, aurp_global
.glock
);
431 if (aurp_global
.shutdown
) {
433 ATDISABLE(s
, aurp_global
.glock
);
434 aurp_global
.running
--;
435 ATENABLE(s
, aurp_global
.glock
);
436 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
437 ("atalk_to_ip: detected aurp_global.shutdown state\n"));
440 dPrintf(D_M_AURP
, D_L_VERBOSE
, ("atalk_to_ip: calling sosend\n"));
441 error
= sosend(aurp_global
.tunnel
, (struct sockaddr
*) &rem_addr
, NULL
, m
, NULL
, flags
);
444 dPrintf(D_M_AURP
, D_L_ERROR
, ("AURP: sosend error (%d)\n",
448 ATDISABLE(s
, aurp_global
.glock
);
449 aurp_global
.running
--;
450 ATENABLE(s
, aurp_global
.glock
);