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