2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1996 Apple Computer, Inc.
25 * Created April 25, 1996, by Justin C. Walker
26 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
32 * Kernel process to implement the AURP daemon:
33 * manage tunnels to remote AURP servers across IP networks
37 #include <sys/errno.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <machine/spl.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/kauth.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/malloc.h>
53 #include <sys/uio_internal.h>
54 #include <kern/locks.h>
55 #include <netinet/in.h>
58 #include <netat/sysglue.h>
59 #include <netat/appletalk.h>
60 #include <netat/at_var.h>
61 #include <netat/routing_tables.h>
62 #include <netat/at_pcb.h>
63 #include <netat/aurp.h>
64 #include <netat/debug.h>
66 #define M_RCVBUF (64 * 1024)
67 #define M_SNDBUF (64 * 1024)
69 extern lck_mtx_t
* atalk_mutex
;
71 static int ip_to_atalk(struct sockaddr_in
*fp
, register gbuf_t
*p_mbuf
);
72 static int aurp_bindrp(struct socket
*so
);
74 struct aurp_global_t aurp_global
;
77 * Initialize the aurp pipe -
78 * -Create, initialize, and start the aurpd kernel process; we need
79 * a process to permit queueing between the socket and the stream,
80 * which is necessary for orderly access to the socket structure.
81 * -The user process (aurpd) is there to 'build' the AURP
82 * stream, act as a 'logging agent' (:-}), and hold open the stream
84 * -Data and AURP packets from the DDP stream will be fed into the
85 * UDP tunnel (AURPsend())
86 * -Data and AURP packets from the UDP tunnel will be fed into the
87 * DDP stream (ip_to_atalk(), via the kernel process).
93 register struct socket
*so
;
98 if (suser(kauth_cred_get(), 0) != 0 )
102 * Set up state prior to starting kernel process so we can back out
103 * (error return) if something goes wrong.
105 bzero((char *)&aurp_global
.tunnel
, sizeof(aurp_global
.tunnel
));
106 /*lock_alloc(&aurp_global.glock, LOCK_ALLOC_PIN, AURP_EVNT_LOCK, -1);*/
107 ATEVENTINIT(aurp_global
.event_anchor
);
109 /* open udp socket */
110 if (aurp_global
.udp_port
== 0)
111 aurp_global
.udp_port
= AURP_SOCKNUM
;
112 error
= socreate(AF_INET
, &aurp_global
.tunnel
, SOCK_DGRAM
,
115 { dPrintf(D_M_AURP
, D_L_FATAL
, ("AURP: Can't get socket (%d)\n",
120 so
= aurp_global
.tunnel
;
122 if ((error
= aurp_bindrp(so
)) != 0)
123 { dPrintf(D_M_AURP
, D_L_FATAL
,
124 ("AURP: Can't bind to port %d (error %d)\n",
125 aurp_global
.udp_port
, error
));
130 sblock(&so
->so_rcv
, M_WAIT
);
131 sblock(&so
->so_snd
, M_WAIT
);
134 * Set socket Receive buffer size
136 m
= m_get(M_WAIT
, MT_SOOPTS
);
142 sopt
.sopt_val
= CAST_USER_ADDR_T(&maxbuf
);
143 sopt
.sopt_valsize
= sizeof(maxbuf
);
144 sopt
.sopt_level
= SOL_SOCKET
;
145 sopt
.sopt_name
= SO_RCVBUF
;
146 sopt
.sopt_dir
= SOPT_SET
;
148 if ((error
= sosetopt(so
, &sopt
)) != 0)
153 * Set socket Send buffer size
155 m
= m_get(M_WAIT
, MT_SOOPTS
);
162 sopt
.sopt_val
= CAST_USER_ADDR_T(&maxbuf
);
163 sopt
.sopt_valsize
= sizeof(maxbuf
);
164 sopt
.sopt_level
= SOL_SOCKET
;
165 sopt
.sopt_name
= SO_SNDBUF
;
166 sopt
.sopt_dir
= SOPT_SET
;
168 if ((error
= sosetopt(so
, &sopt
)) != 0)
172 so
->so_upcall
= aurp_wakeup
;
173 so
->so_upcallarg
= (caddr_t
)AE_UDPIP
; /* Yuck */
174 so
->so_state
|= SS_NBIO
;
175 so
->so_rcv
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
176 so
->so_snd
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
179 sbunlock(&so
->so_snd
, 0);
180 sbunlock(&so
->so_rcv
, 0);
188 { register struct socket
*so
;
191 so
= aurp_global
.tunnel
;
195 { gbuf_t
*from
, *p_mbuf
;
196 int flags
= MSG_DONTWAIT
;
198 char uio_buf
[ UIO_SIZEOF(0) ];
201 * Wait for a package to arrive. This will be from the
202 * IP side - sowakeup() calls aurp_wakeup()
203 * when a packet arrives
206 events
= aurp_global
.event
;
207 if (((*err
== 0) || (*err
== EWOULDBLOCK
)) && events
== 0)
209 lck_mtx_assert(atalk_mutex
, LCK_MTX_ASSERT_OWNED
);
210 *err
= msleep(&aurp_global
.event_anchor
, atalk_mutex
, PSOCK
| PCATCH
, "AURPgetmsg", 0);
211 events
= aurp_global
.event
;
212 aurp_global
.event
= 0;
216 * Shut down if we have the AE_SHUTDOWN event or if we got
217 * a system error other than EWOULDBLOCK, such as EINTR.
219 if (((*err
!= EWOULDBLOCK
) && (*err
!= 0)) || events
& AE_SHUTDOWN
)
221 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
222 ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
223 aurp_global
.shutdown
= 1;
224 while (aurp_global
.running
)
226 /*lock_free(&aurp_global.glock);*/
227 aurp_global
.tunnel
= 0;
228 aurp_global
.event
= 0;
229 aurp_global
.shutdown
= 0;
233 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
234 ("AURPgetmsg: shutdown completed\n"));
241 * Set up the nominal uio structure -
242 * give it no iov's, point off to non-existant user space,
243 * but make sure the 'resid' count means somehting.
245 auio
= uio_createwithbuffer(0, 0, UIO_SYSSPACE
, UIO_READ
,
246 &uio_buf
[0], sizeof(uio_buf
));
248 /* Keep up an even flow... */
252 * This should be large enough to encompass a full DDP packet plus
255 #define A_LARGE_SIZE 700
257 flags
= MSG_DONTWAIT
;
258 uio_setresid(auio
, A_LARGE_SIZE
);
259 *err
= soreceive(so
, (struct sockaddr
**)&from
, auio
, &p_mbuf
, 0, &flags
);
260 dPrintf(D_M_AURP
, D_L_VERBOSE
,
261 ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err
, events
));
262 /* soreceive() sets *mp to zero! at start */
264 ip_to_atalk((struct sockaddr_in
*)from
, p_mbuf
);
265 if (*err
|| (p_mbuf
== NULL
)) {
267 * An error occurred in soreceive(),
268 * so clear the data input event flag
269 * and break out of this inner loop.
271 * XXX Note that clearing AE_UDPIP here could
272 * cause us to lose an AE_UDPIP event that
273 * was posted in aurp_global.event between
274 * the soreceive() above and the code here.
275 * The protocol should recover from this
276 * lost event, though, since the next
277 * request (a tickle, for example) from
278 * the other end of the tunnel will cause
279 * another AE_UDPIP event to be posted,
280 * which will wake us from the sleep at
281 * the top of the outer loop.
283 aurp_global
.event
&= ~AE_UDPIP
;
284 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err
, (unsigned int) p_mbuf
));
293 * Wakeup the sleeping giant - we've put a message on his queue(s).
294 * The arg indicates what queue has been updated.
296 * This conforms to the so_upcall function pointer member of struct sockbuf.
298 void aurp_wakeup(__unused
struct socket
*so
, register caddr_t p
, __unused
int state
)
303 aurp_global
.event
|= bit
;
305 dPrintf(D_M_AURP
, D_L_STATE_CHG
,
306 ("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n",
307 bit
, aurp_global
.event
));
309 wakeup(&aurp_global
.event_anchor
);
313 * Try to bind to the specified reserved port.
314 * Sort of like sobind(), but no suser() check.
317 aurp_bindrp(struct socket
*so
)
319 struct sockaddr_in sin
;
320 struct proc
*p
= current_proc();
324 bzero(&sin
, sizeof(sin
));
325 sin
.sin_family
= AF_INET
;
326 sin
.sin_addr
.s_addr
= htons(aurp_global
.src_addr
);
327 sin
.sin_port
= htons(aurp_global
.udp_port
);
328 sin
.sin_len
= sizeof(struct sockaddr_in
);
330 sblock(&so
->so_rcv
, M_WAIT
);
331 sblock(&so
->so_snd
, M_WAIT
);
332 so
->so_state
|= SS_PRIV
;
333 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, (struct sockaddr
*) &sin
, p
);
334 sbunlock(&so
->so_snd
, 0);
335 sbunlock(&so
->so_rcv
, 0);
342 * fp is the 'source address' mbuf; p_mbuf is the data mbuf.
343 * Use the source address to find the 'node number' (index of the address),
344 * and pass that to the next stage.
346 int ip_to_atalk(register struct sockaddr_in
*rem_addr
, register gbuf_t
*p_mbuf
)
348 register aurp_domain_t
*domain
;
352 /* determine the node where the packet came from */
353 for (node
=1; node
<= dst_addr_cnt
; node
++) {
354 if (aurp_global
.dst_addr
[node
] == *(long *)&rem_addr
->sin_addr
)
357 if (node
> dst_addr_cnt
) {
358 dPrintf(D_M_AURP
, D_L_WARNING
,
359 ("AURPrecv: invalid node, %d.%lx\n",
361 rem_addr
->sin_addr
.s_addr
));
364 FREE(rem_addr
, M_SONAME
);
368 /* validate the domain */
369 domain
= (aurp_domain_t
*)gbuf_rptr(p_mbuf
);
370 if ( (domain
->dst_length
!= IP_LENGTH
) ||
371 (domain
->dst_authority
!= IP_AUTHORITY
) ||
372 (domain
->version
!= AUD_Version
) ||
373 ((domain
->type
!= AUD_Atalk
) && (domain
->type
!= AUD_AURP
)) ) {
374 dPrintf(D_M_AURP
, D_L_WARNING
,
375 ("AURPrecv: invalid domain, %d.%lx\n",
377 rem_addr
->sin_addr
.s_addr
));
380 FREE(rem_addr
, M_SONAME
);
384 /* Remove domain header */
385 p_mbuf
->m_pkthdr
.len
-= IP_DOMAINSIZE
;
386 gbuf_rinc(p_mbuf
,IP_DOMAINSIZE
);
387 gbuf_set_type(p_mbuf
, MSG_DATA
);
389 /* forward the packet to the local AppleTalk stack */
391 at_insert(p_mbuf
, domain
->type
, node
);
392 FREE(rem_addr
, M_SONAME
);
398 * The real work has been done already. Here, we just cobble together
399 * a sockaddr for the destination and call sosend().
402 atalk_to_ip(register gbuf_t
*m
)
403 { register aurp_domain_t
*domain
;
405 int flags
= MSG_DONTWAIT
;
406 struct sockaddr_in rem_addr
;
408 m
->m_type
= MT_HEADER
;
409 m
->m_pkthdr
.len
= gbuf_msgsize(m
);
410 m
->m_pkthdr
.rcvif
= 0;
412 bzero((char *) &rem_addr
, sizeof(rem_addr
));
413 rem_addr
.sin_family
= PF_INET
;
414 rem_addr
.sin_port
= aurp_global
.udp_port
;
415 rem_addr
.sin_len
= sizeof (struct sockaddr_in
);
416 domain
= (aurp_domain_t
*)gbuf_rptr(m
);
417 *(long *) &rem_addr
.sin_addr
= domain
->dst_address
;
419 aurp_global
.running
++;
420 if (aurp_global
.shutdown
) {
422 aurp_global
.running
--;
423 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
424 ("atalk_to_ip: detected aurp_global.shutdown state\n"));
427 dPrintf(D_M_AURP
, D_L_VERBOSE
, ("atalk_to_ip: calling sosend\n"));
428 error
= sosend(aurp_global
.tunnel
, (struct sockaddr
*) &rem_addr
, NULL
, m
, NULL
, flags
);
431 dPrintf(D_M_AURP
, D_L_ERROR
, ("AURP: sosend error (%d)\n",
435 aurp_global
.running
--;
439 #endif /* AURP_SUPPORT */