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