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