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
;
143 if ((error
= sosetopt(so
, &sopt
)) != 0)
148 * Set socket Send buffer size
150 m
= m_get(M_WAIT
, MT_SOOPTS
);
157 sopt
.sopt_val
= &maxbuf
;
158 sopt
.sopt_valsize
= sizeof(maxbuf
);
159 sopt
.sopt_level
= SOL_SOCKET
;
160 sopt
.sopt_name
= SO_SNDBUF
;
161 sopt
.sopt_dir
= SOPT_SET
;
163 if ((error
= sosetopt(so
, &sopt
)) != 0)
167 so
->so_upcall
= aurp_wakeup
;
168 so
->so_upcallarg
= (caddr_t
)AE_UDPIP
; /* Yuck */
169 so
->so_state
|= SS_NBIO
;
170 so
->so_rcv
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
171 so
->so_snd
.sb_flags
|=(SB_SEL
|SB_NOINTR
);
174 sbunlock(&so
->so_snd
);
175 sbunlock(&so
->so_rcv
);
183 { register struct socket
*so
;
184 register int s
, events
;
186 so
= aurp_global
.tunnel
;
190 { gbuf_t
*from
, *p_mbuf
;
191 int flags
= MSG_DONTWAIT
;
195 * Wait for a package to arrive. This will be from the
196 * IP side - sowakeup() calls aurp_wakeup()
197 * when a packet arrives
200 ATDISABLE(s
, aurp_global
.glock
);
201 events
= aurp_global
.event
;
202 if (((*err
== 0) || (*err
== EWOULDBLOCK
)) && events
== 0)
204 *err
= tsleep(&aurp_global
.event_anchor
, PSOCK
| PCATCH
, "AURPgetmsg", 0);
205 events
= aurp_global
.event
;
206 aurp_global
.event
= 0;
208 ATENABLE(s
, aurp_global
.glock
);
211 * Shut down if we have the AE_SHUTDOWN event or if we got
212 * a system error other than EWOULDBLOCK, such as EINTR.
214 if (((*err
!= EWOULDBLOCK
) && (*err
!= 0)) || events
& AE_SHUTDOWN
)
216 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
217 ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
218 aurp_global
.shutdown
= 1;
219 while (aurp_global
.running
)
221 /*lock_free(&aurp_global.glock);*/
222 aurp_global
.tunnel
= 0;
223 aurp_global
.event
= 0;
224 aurp_global
.shutdown
= 0;
228 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
229 ("AURPgetmsg: shutdown completed\n"));
236 * Set up the nominal uio structure -
237 * give it no iov's, point off to non-existant user space,
238 * but make sure the 'resid' count means somehting.
243 auio
.uio_segflg
= UIO_SYSSPACE
;
244 auio
.uio_offset
= 0; /* XXX */
246 /* Keep up an even flow... */
250 * This should be large enough to encompass a full DDP packet plus
253 #define A_LARGE_SIZE 700
255 flags
= MSG_DONTWAIT
;
256 auio
.uio_resid
= A_LARGE_SIZE
;
257 *err
= soreceive(so
, (struct sockaddr
**)&from
, &auio
, &p_mbuf
, 0, &flags
);
258 dPrintf(D_M_AURP
, D_L_VERBOSE
,
259 ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err
, events
));
260 /* soreceive() sets *mp to zero! at start */
262 ip_to_atalk(from
, p_mbuf
);
263 if (*err
|| (p_mbuf
== NULL
)) {
265 * An error occurred in soreceive(),
266 * so clear the data input event flag
267 * and break out of this inner loop.
269 * XXX Note that clearing AE_UDPIP here could
270 * cause us to lose an AE_UDPIP event that
271 * was posted in aurp_global.event between
272 * the soreceive() above and the code here.
273 * The protocol should recover from this
274 * lost event, though, since the next
275 * request (a tickle, for example) from
276 * the other end of the tunnel will cause
277 * another AE_UDPIP event to be posted,
278 * which will wake us from the sleep at
279 * the top of the outer loop.
282 ATDISABLE(s
, aurp_global
.glock
);
283 aurp_global
.event
&= ~AE_UDPIP
;
284 ATENABLE(s
, aurp_global
.glock
);
285 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err
, (unsigned int) p_mbuf
));
294 * Wakeup the sleeping giant - we've put a message on his queue(s).
295 * The arg indicates what queue has been updated.
297 * This conforms to the so_upcall function pointer member of struct sockbuf.
299 void aurp_wakeup(struct socket
*so
, register caddr_t p
, int state
)
305 ATDISABLE(s
, aurp_global
.glock
);
306 aurp_global
.event
|= bit
;
307 ATENABLE(s
, aurp_global
.glock
);
309 dPrintf(D_M_AURP
, D_L_STATE_CHG
,
310 ("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n",
311 bit
, aurp_global
.event
));
313 wakeup(&aurp_global
.event_anchor
);
317 * Try to bind to the specified reserved port.
318 * Sort of like sobind(), but no suser() check.
321 aurp_bindrp(struct socket
*so
)
323 struct sockaddr_in sin
;
324 struct proc
*p
= current_proc();
329 bzero(&sin
, sizeof(sin
));
330 sin
.sin_family
= AF_INET
;
331 sin
.sin_addr
.s_addr
= htons(aurp_global
.src_addr
);
332 sin
.sin_port
= htons(aurp_global
.udp_port
);
333 sin
.sin_len
= sizeof(struct sockaddr_in
);
335 sblock(&so
->so_rcv
, M_WAIT
);
336 sblock(&so
->so_snd
, M_WAIT
);
337 so
->so_state
|= SS_PRIV
;
338 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, (struct sockaddr
*) &sin
, p
);
339 sbunlock(&so
->so_snd
);
340 sbunlock(&so
->so_rcv
);
347 * fp is the 'source address' mbuf; p_mbuf is the data mbuf.
348 * Use the source address to find the 'node number' (index of the address),
349 * and pass that to the next stage.
351 int ip_to_atalk(register struct sockaddr_in
*rem_addr
, register gbuf_t
*p_mbuf
)
353 register aurp_domain_t
*domain
;
357 /* determine the node where the packet came from */
358 for (node
=1; node
<= dst_addr_cnt
; node
++) {
359 if (aurp_global
.dst_addr
[node
] == *(long *)&rem_addr
->sin_addr
)
362 if (node
> dst_addr_cnt
) {
363 dPrintf(D_M_AURP
, D_L_WARNING
,
364 ("AURPrecv: invalid node, %d.%lx\n",
366 rem_addr
->sin_addr
.s_addr
));
369 FREE(rem_addr
, M_SONAME
);
373 /* validate the domain */
374 domain
= (aurp_domain_t
*)gbuf_rptr(p_mbuf
);
375 if ( (domain
->dst_length
!= IP_LENGTH
) ||
376 (domain
->dst_authority
!= IP_AUTHORITY
) ||
377 (domain
->version
!= AUD_Version
) ||
378 ((domain
->type
!= AUD_Atalk
) && (domain
->type
!= AUD_AURP
)) ) {
379 dPrintf(D_M_AURP
, D_L_WARNING
,
380 ("AURPrecv: invalid domain, %d.%lx\n",
382 rem_addr
->sin_addr
.s_addr
));
385 FREE(rem_addr
, M_SONAME
);
389 /* Remove domain header */
390 p_mbuf
->m_pkthdr
.len
-= IP_DOMAINSIZE
;
391 gbuf_rinc(p_mbuf
,IP_DOMAINSIZE
);
392 gbuf_set_type(p_mbuf
, MSG_DATA
);
394 /* forward the packet to the local AppleTalk stack */
396 at_insert(p_mbuf
, domain
->type
, node
);
397 FREE(rem_addr
, M_SONAME
);
403 * The real work has been done already. Here, we just cobble together
404 * a sockaddr for the destination and call sosend().
407 atalk_to_ip(register gbuf_t
*m
)
408 { register aurp_domain_t
*domain
;
410 int flags
= MSG_DONTWAIT
;
411 struct sockaddr_in rem_addr
;
414 m
->m_type
= MT_HEADER
;
415 m
->m_pkthdr
.len
= gbuf_msgsize(m
);
416 m
->m_pkthdr
.rcvif
= 0;
418 bzero((char *) &rem_addr
, sizeof(rem_addr
));
419 rem_addr
.sin_family
= PF_INET
;
420 rem_addr
.sin_port
= aurp_global
.udp_port
;
421 rem_addr
.sin_len
= sizeof (struct sockaddr_in
);
422 domain
= (aurp_domain_t
*)gbuf_rptr(m
);
423 *(long *) &rem_addr
.sin_addr
= domain
->dst_address
;
425 ATDISABLE(s
, aurp_global
.glock
);
426 aurp_global
.running
++;
427 ATENABLE(s
, aurp_global
.glock
);
428 if (aurp_global
.shutdown
) {
430 ATDISABLE(s
, aurp_global
.glock
);
431 aurp_global
.running
--;
432 ATENABLE(s
, aurp_global
.glock
);
433 dPrintf(D_M_AURP
, D_L_SHUTDN_INFO
,
434 ("atalk_to_ip: detected aurp_global.shutdown state\n"));
437 dPrintf(D_M_AURP
, D_L_VERBOSE
, ("atalk_to_ip: calling sosend\n"));
438 error
= sosend(aurp_global
.tunnel
, (struct sockaddr
*) &rem_addr
, NULL
, m
, NULL
, flags
);
441 dPrintf(D_M_AURP
, D_L_ERROR
, ("AURP: sosend error (%d)\n",
445 ATDISABLE(s
, aurp_global
.glock
);
446 aurp_global
.running
--;
447 ATENABLE(s
, aurp_global
.glock
);