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