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