]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/aurp_aurpd.c
xnu-1228.3.13.tar.gz
[apple/xnu.git] / bsd / netat / aurp_aurpd.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1996 Apple Computer, Inc.
30 *
31 * Created April 25, 1996, by Justin C. Walker
32 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
33 *
34 * File: aurpd.c
35 */
36
37/*
38 * Kernel process to implement the AURP daemon:
39 * manage tunnels to remote AURP servers across IP networks
40 */
0c530ab8 41#ifdef AURP_SUPPORT
1c79356b
A
42
43#include <sys/errno.h>
44#include <sys/types.h>
45#include <sys/param.h>
46#include <machine/spl.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/proc.h>
91447636 50#include <sys/kauth.h>
1c79356b
A
51#include <sys/filedesc.h>
52#include <sys/fcntl.h>
53#include <sys/mbuf.h>
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#include <sys/protosw.h>
57#include <sys/malloc.h>
58#include <sys/proc.h>
91447636
A
59#include <sys/uio_internal.h>
60#include <kern/locks.h>
1c79356b
A
61#include <netinet/in.h>
62#include <net/if.h>
63
64#include <netat/sysglue.h>
65#include <netat/appletalk.h>
2d21ac55 66#include <netat/at_pcb.h>
1c79356b
A
67#include <netat/at_var.h>
68#include <netat/routing_tables.h>
69#include <netat/at_pcb.h>
70#include <netat/aurp.h>
71#include <netat/debug.h>
72
73#define M_RCVBUF (64 * 1024)
74#define M_SNDBUF (64 * 1024)
75
91447636
A
76extern lck_mtx_t * atalk_mutex;
77
1c79356b
A
78static int ip_to_atalk(struct sockaddr_in *fp, register gbuf_t *p_mbuf);
79static int aurp_bindrp(struct socket *so);
80
81struct aurp_global_t aurp_global;
82
83/*
84 * Initialize the aurp pipe -
85 * -Create, initialize, and start the aurpd kernel process; we need
86 * a process to permit queueing between the socket and the stream,
87 * which is necessary for orderly access to the socket structure.
88 * -The user process (aurpd) is there to 'build' the AURP
89 * stream, act as a 'logging agent' (:-}), and hold open the stream
90 * during its use.
91 * -Data and AURP packets from the DDP stream will be fed into the
92 * UDP tunnel (AURPsend())
93 * -Data and AURP packets from the UDP tunnel will be fed into the
94 * DDP stream (ip_to_atalk(), via the kernel process).
95 */
96int
97aurpd_start()
98{
99 register int error;
100 register struct socket *so;
101 struct mbuf *m;
102 int maxbuf;
103 struct sockopt sopt;
104
91447636 105 if (suser(kauth_cred_get(), 0) != 0 )
1c79356b
A
106 return(EPERM);
107
108 /*
109 * Set up state prior to starting kernel process so we can back out
110 * (error return) if something goes wrong.
111 */
112 bzero((char *)&aurp_global.tunnel, sizeof(aurp_global.tunnel));
113 /*lock_alloc(&aurp_global.glock, LOCK_ALLOC_PIN, AURP_EVNT_LOCK, -1);*/
1c79356b
A
114 ATEVENTINIT(aurp_global.event_anchor);
115
116 /* open udp socket */
117 if (aurp_global.udp_port == 0)
118 aurp_global.udp_port = AURP_SOCKNUM;
119 error = socreate(AF_INET, &aurp_global.tunnel, SOCK_DGRAM,
120 IPPROTO_UDP);
121 if (error)
122 { dPrintf(D_M_AURP, D_L_FATAL, ("AURP: Can't get socket (%d)\n",
123 error));
124 return(error);
125 }
126
127 so = aurp_global.tunnel;
128
129 if ((error = aurp_bindrp(so)) != 0)
130 { dPrintf(D_M_AURP, D_L_FATAL,
131 ("AURP: Can't bind to port %d (error %d)\n",
132 aurp_global.udp_port, error));
133 soclose(so);
134 return(error);
135 }
136
137 sblock(&so->so_rcv, M_WAIT);
138 sblock(&so->so_snd, M_WAIT);
139
140 /*
141 * Set socket Receive buffer size
142 */
143 m = m_get(M_WAIT, MT_SOOPTS);
144 if (m == NULL) {
145 error = ENOBUFS;
146 goto out;
147 } else {
148 maxbuf = M_RCVBUF;
91447636 149 sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf);
1c79356b
A
150 sopt.sopt_valsize = sizeof(maxbuf);
151 sopt.sopt_level = SOL_SOCKET;
152 sopt.sopt_name = SO_RCVBUF;
153 sopt.sopt_dir = SOPT_SET;
0b4e3aa0 154 sopt.sopt_p = NULL;
1c79356b
A
155 if ((error = sosetopt(so, &sopt)) != 0)
156 goto out;
157 }
158
159 /*
160 * Set socket Send buffer size
161 */
162 m = m_get(M_WAIT, MT_SOOPTS);
163 if (m == NULL) {
164 error = ENOBUFS;
165 goto out;
166 } else {
167
168 maxbuf = M_SNDBUF;
91447636 169 sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf);
1c79356b
A
170 sopt.sopt_valsize = sizeof(maxbuf);
171 sopt.sopt_level = SOL_SOCKET;
172 sopt.sopt_name = SO_SNDBUF;
173 sopt.sopt_dir = SOPT_SET;
0b4e3aa0 174 sopt.sopt_p = NULL;
1c79356b
A
175 if ((error = sosetopt(so, &sopt)) != 0)
176 goto out;
177 }
178
179 so->so_upcall = aurp_wakeup;
180 so->so_upcallarg = (caddr_t)AE_UDPIP; /* Yuck */
181 so->so_state |= SS_NBIO;
0b4e3aa0
A
182 so->so_rcv.sb_flags |=(SB_SEL|SB_NOINTR);
183 so->so_snd.sb_flags |=(SB_SEL|SB_NOINTR);
1c79356b
A
184
185out:
91447636
A
186 sbunlock(&so->so_snd, 0);
187 sbunlock(&so->so_rcv, 0);
1c79356b
A
188
189 return(error);
190}
191
192int
193AURPgetmsg(err)
194 int *err;
195{ register struct socket *so;
91447636 196 register int events;
1c79356b
A
197
198 so = aurp_global.tunnel;
199 *err = 0;
200
201 for (;;)
202 { gbuf_t *from, *p_mbuf;
203 int flags = MSG_DONTWAIT;
91447636
A
204 uio_t auio;
205 char uio_buf[ UIO_SIZEOF(0) ];
1c79356b
A
206
207 /*
208 * Wait for a package to arrive. This will be from the
209 * IP side - sowakeup() calls aurp_wakeup()
210 * when a packet arrives
211 */
212
1c79356b
A
213 events = aurp_global.event;
214 if (((*err == 0) || (*err == EWOULDBLOCK)) && events == 0)
215 {
91447636
A
216 lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
217 *err = msleep(&aurp_global.event_anchor, atalk_mutex, PSOCK | PCATCH, "AURPgetmsg", 0);
1c79356b
A
218 events = aurp_global.event;
219 aurp_global.event = 0;
220 }
1c79356b
A
221
222 /*
223 * Shut down if we have the AE_SHUTDOWN event or if we got
224 * a system error other than EWOULDBLOCK, such as EINTR.
225 */
226 if (((*err != EWOULDBLOCK) && (*err != 0)) || events & AE_SHUTDOWN)
227 {
228 dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
229 ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
230 aurp_global.shutdown = 1;
231 while (aurp_global.running)
232 ;
233 /*lock_free(&aurp_global.glock);*/
234 aurp_global.tunnel = 0;
235 aurp_global.event = 0;
236 aurp_global.shutdown = 0;
237 soclose(so);
238 if (*err == 0)
239 *err = ESHUTDOWN;
240 dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
241 ("AURPgetmsg: shutdown completed\n"));
242 return -1;
243 }
244
245
246
247 /*
248 * Set up the nominal uio structure -
249 * give it no iov's, point off to non-existant user space,
250 * but make sure the 'resid' count means somehting.
251 */
91447636
A
252 auio = uio_createwithbuffer(0, 0, UIO_SYSSPACE, UIO_READ,
253 &uio_buf[0], sizeof(uio_buf));
1c79356b
A
254
255 /* Keep up an even flow... */
256 for (;;)
257 {
258/*
259 * This should be large enough to encompass a full DDP packet plus
260 * domain header.
261 */
262#define A_LARGE_SIZE 700
263
264 flags = MSG_DONTWAIT;
91447636
A
265 uio_setresid(auio, A_LARGE_SIZE);
266 *err = soreceive(so, (struct sockaddr **)&from, auio, &p_mbuf, 0, &flags);
1c79356b
A
267 dPrintf(D_M_AURP, D_L_VERBOSE,
268 ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err, events));
269 /* soreceive() sets *mp to zero! at start */
270 if (p_mbuf)
55e303ae 271 ip_to_atalk((struct sockaddr_in *)from, p_mbuf);
1c79356b
A
272 if (*err || (p_mbuf == NULL)) {
273 /*
274 * An error occurred in soreceive(),
275 * so clear the data input event flag
276 * and break out of this inner loop.
277 *
278 * XXX Note that clearing AE_UDPIP here could
279 * cause us to lose an AE_UDPIP event that
280 * was posted in aurp_global.event between
281 * the soreceive() above and the code here.
282 * The protocol should recover from this
283 * lost event, though, since the next
284 * request (a tickle, for example) from
285 * the other end of the tunnel will cause
286 * another AE_UDPIP event to be posted,
287 * which will wake us from the sleep at
288 * the top of the outer loop.
289 */
1c79356b 290 aurp_global.event &= ~AE_UDPIP;
1c79356b
A
291 dPrintf(D_M_AURP, D_L_WARNING, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err, (unsigned int) p_mbuf));
292 break;
293 }
294 }
295 }
296 return -1;
297}
298
299/*
300 * Wakeup the sleeping giant - we've put a message on his queue(s).
301 * The arg indicates what queue has been updated.
302 *
303 * This conforms to the so_upcall function pointer member of struct sockbuf.
304 */
91447636 305void aurp_wakeup(__unused struct socket *so, register caddr_t p, __unused int state)
1c79356b 306{
1c79356b
A
307 register int bit;
308
309 bit = (int) p;
1c79356b 310 aurp_global.event |= bit;
1c79356b
A
311
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));
315
9bccf70c 316 wakeup(&aurp_global.event_anchor);
1c79356b
A
317}
318
319/*
320 * Try to bind to the specified reserved port.
321 * Sort of like sobind(), but no suser() check.
322 */
323static int
324aurp_bindrp(struct socket *so)
325{
326 struct sockaddr_in sin;
327 struct proc *p = current_proc();
1c79356b
A
328 int error;
329
330
331 bzero(&sin, sizeof(sin));
332 sin.sin_family = AF_INET;
333 sin.sin_addr.s_addr = htons(aurp_global.src_addr);
334 sin.sin_port = htons(aurp_global.udp_port);
335 sin.sin_len = sizeof(struct sockaddr_in);
336
337 sblock(&so->so_rcv, M_WAIT);
338 sblock(&so->so_snd, M_WAIT);
339 so->so_state |= SS_PRIV;
340 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, (struct sockaddr *) &sin, p);
91447636
A
341 sbunlock(&so->so_snd, 0);
342 sbunlock(&so->so_rcv, 0);
1c79356b
A
343
344 return (error);
345}
346
347/*
348 * receive from UDP
349 * fp is the 'source address' mbuf; p_mbuf is the data mbuf.
350 * Use the source address to find the 'node number' (index of the address),
351 * and pass that to the next stage.
352 */
353int ip_to_atalk(register struct sockaddr_in *rem_addr, register gbuf_t *p_mbuf)
354{
355 register aurp_domain_t *domain;
356 unsigned char node;
357
358
359 /* determine the node where the packet came from */
360 for (node=1; node <= dst_addr_cnt; node++) {
361 if (aurp_global.dst_addr[node] == *(long *)&rem_addr->sin_addr)
362 break;
363 }
364 if (node > dst_addr_cnt) {
365 dPrintf(D_M_AURP, D_L_WARNING,
366 ("AURPrecv: invalid node, %d.%lx\n",
367 rem_addr->sin_port,
368 rem_addr->sin_addr.s_addr));
369
370 gbuf_freem(p_mbuf);
371 FREE(rem_addr, M_SONAME);
372 return -1;
373 }
374
375 /* validate the domain */
376 domain = (aurp_domain_t *)gbuf_rptr(p_mbuf);
377 if ( (domain->dst_length != IP_LENGTH) ||
378 (domain->dst_authority != IP_AUTHORITY) ||
379 (domain->version != AUD_Version) ||
380 ((domain->type != AUD_Atalk) && (domain->type != AUD_AURP)) ) {
381 dPrintf(D_M_AURP, D_L_WARNING,
382 ("AURPrecv: invalid domain, %d.%lx\n",
383 rem_addr->sin_port,
384 rem_addr->sin_addr.s_addr));
385
386 gbuf_freem(p_mbuf);
387 FREE(rem_addr, M_SONAME);
388 return -1;
389 }
390
391 /* Remove domain header */
392 p_mbuf->m_pkthdr.len -= IP_DOMAINSIZE;
393 gbuf_rinc(p_mbuf,IP_DOMAINSIZE);
394 gbuf_set_type(p_mbuf, MSG_DATA);
395
396 /* forward the packet to the local AppleTalk stack */
397
398 at_insert(p_mbuf, domain->type, node);
399 FREE(rem_addr, M_SONAME);
400 return 0;
401}
402
403/*
404 * send to UDP
405 * The real work has been done already. Here, we just cobble together
406 * a sockaddr for the destination and call sosend().
407 */
408void
409atalk_to_ip(register gbuf_t *m)
410{ register aurp_domain_t *domain;
411 int error;
412 int flags = MSG_DONTWAIT;
413 struct sockaddr_in rem_addr;
1c79356b 414
2d21ac55 415 m_mchtype(m, MT_HEADER);
1c79356b
A
416 m->m_pkthdr.len = gbuf_msgsize(m);
417 m->m_pkthdr.rcvif = 0;
418
419 bzero((char *) &rem_addr, sizeof(rem_addr));
420 rem_addr.sin_family = PF_INET;
421 rem_addr.sin_port = aurp_global.udp_port;
422 rem_addr.sin_len = sizeof (struct sockaddr_in);
423 domain = (aurp_domain_t *)gbuf_rptr(m);
424 *(long *) &rem_addr.sin_addr = domain->dst_address;
425
1c79356b 426 aurp_global.running++;
1c79356b
A
427 if (aurp_global.shutdown) {
428 gbuf_freem(m);
1c79356b 429 aurp_global.running--;
1c79356b
A
430 dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
431 ("atalk_to_ip: detected aurp_global.shutdown state\n"));
432 return;
433 }
434 dPrintf(D_M_AURP, D_L_VERBOSE, ("atalk_to_ip: calling sosend\n"));
435 error = sosend(aurp_global.tunnel, (struct sockaddr *) &rem_addr, NULL, m, NULL, flags);
436 if (error)
437 { /*log error*/
438 dPrintf(D_M_AURP, D_L_ERROR, ("AURP: sosend error (%d)\n",
439 error));
440 }
441
1c79356b 442 aurp_global.running--;
1c79356b
A
443 return;
444}
445
2d21ac55 446#endif /* AURP_SUPPORT */