2 * Copyright (c) 2000 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
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
51 #include <netinet/in.h>
54 #include <netat/sysglue.h>
55 #include <netat/appletalk.h>
56 #include <netat/at_var.h>
57 #include <netat/routing_tables.h>
58 #include <netat/at_pcb.h>
59 #include <netat/aurp.h>
60 #include <netat/debug.h>
62 #define M_RCVBUF (64 * 1024)
63 #define M_SNDBUF (64 * 1024)
65 static int ip_to_atalk(struct sockaddr_in
*fp
, register gbuf_t
*p_mbuf
);
66 static int aurp_bindrp(struct socket
*so
);
68 struct aurp_global_t aurp_global
;
71 * Initialize the aurp pipe -
72 * -Create, initialize, and start the aurpd kernel process; we need
73 * a process to permit queueing between the socket and the stream,
74 * which is necessary for orderly access to the socket structure.
75 * -The user process (aurpd) is there to 'build' the AURP
76 * stream, act as a 'logging agent' (:-}), and hold open the stream
78 * -Data and AURP packets from the DDP stream will be fed into the
79 * UDP tunnel (AURPsend())
80 * -Data and AURP packets from the UDP tunnel will be fed into the
81 * DDP stream (ip_to_atalk(), via the kernel process).
87 register struct socket
*so
;
92 if (suser(current_proc()->p_ucred
, ¤t_proc()->p_acflag
) != 0 )
96 * Set up state prior to starting kernel process so we can back out
97 * (error return) if something goes wrong.
99 bzero((char *)&aurp_global
.tunnel
, sizeof(aurp_global
.tunnel
));
100 /*lock_alloc(&aurp_global.glock, LOCK_ALLOC_PIN, AURP_EVNT_LOCK, -1);*/
101 ATLOCKINIT(aurp_global
.glock
);
102 ATEVENTINIT(aurp_global
.event_anchor
);
104 /* open udp socket */
105 if (aurp_global
.udp_port
== 0)
106 aurp_global
.udp_port
= AURP_SOCKNUM
;
107 error
= socreate(AF_INET
, &aurp_global
.tunnel
, SOCK_DGRAM
,
110 { dPrintf(D_M_AURP
, D_L_FATAL
, ("AURP: Can't get socket (%d)\n",
115 so
= aurp_global
.tunnel
;
117 if ((error
= aurp_bindrp(so
)) != 0)
118 { dPrintf(D_M_AURP
, D_L_FATAL
,
119 ("AURP: Can't bind to port %d (error %d)\n",
120 aurp_global
.udp_port
, error
));
125 sblock(&so
->so_rcv
, M_WAIT
);
126 sblock(&so
->so_snd
, M_WAIT
);
129 * Set socket Receive buffer size
131 m
= m_get(M_WAIT
, MT_SOOPTS
);
137 sopt
.sopt_val
= &maxbuf
;
138 sopt
.sopt_valsize
= sizeof(maxbuf
);
139 sopt
.sopt_level
= SOL_SOCKET
;
140 sopt
.sopt_name
= SO_RCVBUF
;
141 sopt
.sopt_dir
= SOPT_SET
;
142 if ((error
= sosetopt(so
, &sopt
)) != 0)
147 * Set socket Send buffer size
149 m
= m_get(M_WAIT
, MT_SOOPTS
);
156 sopt
.sopt_val
= &maxbuf
;
157 sopt
.sopt_valsize
= sizeof(maxbuf
);
158 sopt
.sopt_level
= SOL_SOCKET
;
159 sopt
.sopt_name
= SO_SNDBUF
;
160 sopt
.sopt_dir
= SOPT_SET
;
161 if ((error
= sosetopt(so
, &sopt
)) != 0)
165 so
->so_upcall
= aurp_wakeup
;
166 so
->so_upcallarg
= (caddr_t
)AE_UDPIP
; /* Yuck */
167 so
->so_state
|= SS_NBIO
;
168 so
->so_rcv
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
169 so
->so_snd
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
172 sbunlock(&so
->so_snd
);
173 sbunlock(&so
->so_rcv
);
181 { register struct socket
*so
;
182 register int s
, events
;
184 so
= aurp_global
.tunnel
;
188 { gbuf_t
*from
, *p_mbuf
;
189 int flags
= MSG_DONTWAIT
;
193 * Wait for a package to arrive. This will be from the
194 * IP side - sowakeup() calls aurp_wakeup()
195 * when a packet arrives
198 ATDISABLE(s
, aurp_global
.glock
);
199 events
= aurp_global
.event
;
200 if (((*err
== 0) || (*err
== EWOULDBLOCK
)) && events
== 0)
202 *err
= tsleep(&aurp_global
.event_anchor
, PSOCK
| PCATCH
, "AURPgetmsg", 0);
203 events
= aurp_global
.event
;
204 aurp_global
.event
= 0;
206 ATENABLE(s
, aurp_global
.glock
);
209 * Shut down if we have the AE_SHUTDOWN event or if we got
210 * a system error other than EWOULDBLOCK, such as EINTR.
212 if (((*err
!= EWOULDBLOCK
) && (*err
!= 0)) || events
& AE_SHUTDOWN
)
214 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
215 ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
216 aurp_global
.shutdown
= 1;
217 while (aurp_global
.running
)
219 /*lock_free(&aurp_global.glock);*/
220 aurp_global
.tunnel
= 0;
221 aurp_global
.event
= 0;
222 aurp_global
.shutdown
= 0;
226 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
227 ("AURPgetmsg: shutdown completed\n"));
234 * Set up the nominal uio structure -
235 * give it no iov's, point off to non-existant user space,
236 * but make sure the 'resid' count means somehting.
241 auio
.uio_segflg
= UIO_SYSSPACE
;
242 auio
.uio_offset
= 0; /* XXX */
244 /* Keep up an even flow... */
248 * This should be large enough to encompass a full DDP packet plus
251 #define A_LARGE_SIZE 700
253 flags
= MSG_DONTWAIT
;
254 auio
.uio_resid
= A_LARGE_SIZE
;
255 *err
= soreceive(so
, (struct sockaddr
**)&from
, &auio
, &p_mbuf
, 0, &flags
);
256 dPrintf(D_M_AURP
, D_L_VERBOSE
,
257 ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err
, events
));
258 /* soreceive() sets *mp to zero! at start */
260 ip_to_atalk(from
, p_mbuf
);
261 if (*err
|| (p_mbuf
== NULL
)) {
263 * An error occurred in soreceive(),
264 * so clear the data input event flag
265 * and break out of this inner loop.
267 * XXX Note that clearing AE_UDPIP here could
268 * cause us to lose an AE_UDPIP event that
269 * was posted in aurp_global.event between
270 * the soreceive() above and the code here.
271 * The protocol should recover from this
272 * lost event, though, since the next
273 * request (a tickle, for example) from
274 * the other end of the tunnel will cause
275 * another AE_UDPIP event to be posted,
276 * which will wake us from the sleep at
277 * the top of the outer loop.
280 ATDISABLE(s
, aurp_global
.glock
);
281 aurp_global
.event
&= ~AE_UDPIP
;
282 ATENABLE(s
, aurp_global
.glock
);
283 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err
, (unsigned int) p_mbuf
));
292 * Wakeup the sleeping giant - we've put a message on his queue(s).
293 * The arg indicates what queue has been updated.
295 * This conforms to the so_upcall function pointer member of struct sockbuf.
297 void aurp_wakeup(struct socket
*so
, register caddr_t p
, int state
)
303 ATDISABLE(s
, aurp_global
.glock
);
304 aurp_global
.event
|= bit
;
305 ATENABLE(s
, aurp_global
.glock
);
307 dPrintf(D_M_AURP
, D_L_STATE_CHG
,
308 ("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n",
309 bit
, aurp_global
.event
));
311 thread_wakeup(&aurp_global
.event_anchor
);
315 * Try to bind to the specified reserved port.
316 * Sort of like sobind(), but no suser() check.
319 aurp_bindrp(struct socket
*so
)
321 struct sockaddr_in sin
;
322 struct proc
*p
= current_proc();
327 bzero(&sin
, sizeof(sin
));
328 sin
.sin_family
= AF_INET
;
329 sin
.sin_addr
.s_addr
= htons(aurp_global
.src_addr
);
330 sin
.sin_port
= htons(aurp_global
.udp_port
);
331 sin
.sin_len
= sizeof(struct sockaddr_in
);
333 sblock(&so
->so_rcv
, M_WAIT
);
334 sblock(&so
->so_snd
, M_WAIT
);
335 so
->so_state
|= SS_PRIV
;
336 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, (struct sockaddr
*) &sin
, p
);
337 sbunlock(&so
->so_snd
);
338 sbunlock(&so
->so_rcv
);
345 * fp is the 'source address' mbuf; p_mbuf is the data mbuf.
346 * Use the source address to find the 'node number' (index of the address),
347 * and pass that to the next stage.
349 int ip_to_atalk(register struct sockaddr_in
*rem_addr
, register gbuf_t
*p_mbuf
)
351 register aurp_domain_t
*domain
;
355 /* determine the node where the packet came from */
356 for (node
=1; node
<= dst_addr_cnt
; node
++) {
357 if (aurp_global
.dst_addr
[node
] == *(long *)&rem_addr
->sin_addr
)
360 if (node
> dst_addr_cnt
) {
361 dPrintf(D_M_AURP
, D_L_WARNING
,
362 ("AURPrecv: invalid node, %d.%lx\n",
364 rem_addr
->sin_addr
.s_addr
));
367 FREE(rem_addr
, M_SONAME
);
371 /* validate the domain */
372 domain
= (aurp_domain_t
*)gbuf_rptr(p_mbuf
);
373 if ( (domain
->dst_length
!= IP_LENGTH
) ||
374 (domain
->dst_authority
!= IP_AUTHORITY
) ||
375 (domain
->version
!= AUD_Version
) ||
376 ((domain
->type
!= AUD_Atalk
) && (domain
->type
!= AUD_AURP
)) ) {
377 dPrintf(D_M_AURP
, D_L_WARNING
,
378 ("AURPrecv: invalid domain, %d.%lx\n",
380 rem_addr
->sin_addr
.s_addr
));
383 FREE(rem_addr
, M_SONAME
);
387 /* Remove domain header */
388 p_mbuf
->m_pkthdr
.len
-= IP_DOMAINSIZE
;
389 gbuf_rinc(p_mbuf
,IP_DOMAINSIZE
);
390 gbuf_set_type(p_mbuf
, MSG_DATA
);
392 /* forward the packet to the local AppleTalk stack */
394 at_insert(p_mbuf
, domain
->type
, node
);
395 FREE(rem_addr
, M_SONAME
);
401 * The real work has been done already. Here, we just cobble together
402 * a sockaddr for the destination and call sosend().
405 atalk_to_ip(register gbuf_t
*m
)
406 { register aurp_domain_t
*domain
;
408 int flags
= MSG_DONTWAIT
;
409 struct sockaddr_in rem_addr
;
412 m
->m_type
= MT_HEADER
;
413 m
->m_pkthdr
.len
= gbuf_msgsize(m
);
414 m
->m_pkthdr
.rcvif
= 0;
416 bzero((char *) &rem_addr
, sizeof(rem_addr
));
417 rem_addr
.sin_family
= PF_INET
;
418 rem_addr
.sin_port
= aurp_global
.udp_port
;
419 rem_addr
.sin_len
= sizeof (struct sockaddr_in
);
420 domain
= (aurp_domain_t
*)gbuf_rptr(m
);
421 *(long *) &rem_addr
.sin_addr
= domain
->dst_address
;
423 ATDISABLE(s
, aurp_global
.glock
);
424 aurp_global
.running
++;
425 ATENABLE(s
, aurp_global
.glock
);
426 if (aurp_global
.shutdown
) {
428 ATDISABLE(s
, aurp_global
.glock
);
429 aurp_global
.running
--;
430 ATENABLE(s
, aurp_global
.glock
);
431 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
432 ("atalk_to_ip: detected aurp_global.shutdown state\n"));
435 dPrintf(D_M_AURP
, D_L_VERBOSE
, ("atalk_to_ip: calling sosend\n"));
436 error
= sosend(aurp_global
.tunnel
, (struct sockaddr
*) &rem_addr
, NULL
, m
, NULL
, flags
);
439 dPrintf(D_M_AURP
, D_L_ERROR
, ("AURP: sosend error (%d)\n",
443 ATDISABLE(s
, aurp_global
.glock
);
444 aurp_global
.running
--;
445 ATENABLE(s
, aurp_global
.glock
);