]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 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 | * |
37839358 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, | |
37839358 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> | |
91447636 | 43 | #include <sys/kauth.h> |
1c79356b A |
44 | #include <sys/filedesc.h> |
45 | #include <sys/fcntl.h> | |
46 | #include <sys/mbuf.h> | |
47 | #include <sys/socket.h> | |
48 | #include <sys/socketvar.h> | |
49 | #include <sys/protosw.h> | |
50 | #include <sys/malloc.h> | |
51 | #include <sys/proc.h> | |
91447636 A |
52 | #include <sys/uio_internal.h> |
53 | #include <kern/locks.h> | |
1c79356b A |
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 | ||
91447636 A |
68 | extern lck_mtx_t * atalk_mutex; |
69 | ||
1c79356b A |
70 | static int ip_to_atalk(struct sockaddr_in *fp, register gbuf_t *p_mbuf); |
71 | static int aurp_bindrp(struct socket *so); | |
72 | ||
73 | struct aurp_global_t aurp_global; | |
74 | ||
75 | /* | |
76 | * Initialize the aurp pipe - | |
77 | * -Create, initialize, and start the aurpd kernel process; we need | |
78 | * a process to permit queueing between the socket and the stream, | |
79 | * which is necessary for orderly access to the socket structure. | |
80 | * -The user process (aurpd) is there to 'build' the AURP | |
81 | * stream, act as a 'logging agent' (:-}), and hold open the stream | |
82 | * during its use. | |
83 | * -Data and AURP packets from the DDP stream will be fed into the | |
84 | * UDP tunnel (AURPsend()) | |
85 | * -Data and AURP packets from the UDP tunnel will be fed into the | |
86 | * DDP stream (ip_to_atalk(), via the kernel process). | |
87 | */ | |
88 | int | |
89 | aurpd_start() | |
90 | { | |
91 | register int error; | |
92 | register struct socket *so; | |
93 | struct mbuf *m; | |
94 | int maxbuf; | |
95 | struct sockopt sopt; | |
96 | ||
91447636 | 97 | if (suser(kauth_cred_get(), 0) != 0 ) |
1c79356b A |
98 | return(EPERM); |
99 | ||
100 | /* | |
101 | * Set up state prior to starting kernel process so we can back out | |
102 | * (error return) if something goes wrong. | |
103 | */ | |
104 | bzero((char *)&aurp_global.tunnel, sizeof(aurp_global.tunnel)); | |
105 | /*lock_alloc(&aurp_global.glock, LOCK_ALLOC_PIN, AURP_EVNT_LOCK, -1);*/ | |
106 | ATLOCKINIT(aurp_global.glock); | |
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; | |
91447636 | 142 | sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf); |
1c79356b A |
143 | sopt.sopt_valsize = sizeof(maxbuf); |
144 | sopt.sopt_level = SOL_SOCKET; | |
145 | sopt.sopt_name = SO_RCVBUF; | |
146 | sopt.sopt_dir = SOPT_SET; | |
0b4e3aa0 | 147 | sopt.sopt_p = NULL; |
1c79356b A |
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; | |
91447636 | 162 | sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf); |
1c79356b A |
163 | sopt.sopt_valsize = sizeof(maxbuf); |
164 | sopt.sopt_level = SOL_SOCKET; | |
165 | sopt.sopt_name = SO_SNDBUF; | |
166 | sopt.sopt_dir = SOPT_SET; | |
0b4e3aa0 | 167 | sopt.sopt_p = NULL; |
1c79356b A |
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; | |
0b4e3aa0 A |
175 | so->so_rcv.sb_flags |=(SB_SEL|SB_NOINTR); |
176 | so->so_snd.sb_flags |=(SB_SEL|SB_NOINTR); | |
1c79356b A |
177 | |
178 | out: | |
91447636 A |
179 | sbunlock(&so->so_snd, 0); |
180 | sbunlock(&so->so_rcv, 0); | |
1c79356b A |
181 | |
182 | return(error); | |
183 | } | |
184 | ||
185 | int | |
186 | AURPgetmsg(err) | |
187 | int *err; | |
188 | { register struct socket *so; | |
91447636 | 189 | register int events; |
1c79356b A |
190 | |
191 | so = aurp_global.tunnel; | |
192 | *err = 0; | |
193 | ||
194 | for (;;) | |
195 | { gbuf_t *from, *p_mbuf; | |
196 | int flags = MSG_DONTWAIT; | |
91447636 A |
197 | uio_t auio; |
198 | char uio_buf[ UIO_SIZEOF(0) ]; | |
1c79356b A |
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 | ATDISABLE(s, aurp_global.glock); | |
207 | events = aurp_global.event; | |
208 | if (((*err == 0) || (*err == EWOULDBLOCK)) && events == 0) | |
209 | { | |
91447636 A |
210 | lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED); |
211 | *err = msleep(&aurp_global.event_anchor, atalk_mutex, PSOCK | PCATCH, "AURPgetmsg", 0); | |
1c79356b A |
212 | events = aurp_global.event; |
213 | aurp_global.event = 0; | |
214 | } | |
215 | ATENABLE(s, aurp_global.glock); | |
216 | ||
217 | /* | |
218 | * Shut down if we have the AE_SHUTDOWN event or if we got | |
219 | * a system error other than EWOULDBLOCK, such as EINTR. | |
220 | */ | |
221 | if (((*err != EWOULDBLOCK) && (*err != 0)) || events & AE_SHUTDOWN) | |
222 | { | |
223 | dPrintf(D_M_AURP, D_L_SHUTDN_INFO, | |
224 | ("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n")); | |
225 | aurp_global.shutdown = 1; | |
226 | while (aurp_global.running) | |
227 | ; | |
228 | /*lock_free(&aurp_global.glock);*/ | |
229 | aurp_global.tunnel = 0; | |
230 | aurp_global.event = 0; | |
231 | aurp_global.shutdown = 0; | |
232 | soclose(so); | |
233 | if (*err == 0) | |
234 | *err = ESHUTDOWN; | |
235 | dPrintf(D_M_AURP, D_L_SHUTDN_INFO, | |
236 | ("AURPgetmsg: shutdown completed\n")); | |
237 | return -1; | |
238 | } | |
239 | ||
240 | ||
241 | ||
242 | /* | |
243 | * Set up the nominal uio structure - | |
244 | * give it no iov's, point off to non-existant user space, | |
245 | * but make sure the 'resid' count means somehting. | |
246 | */ | |
91447636 A |
247 | auio = uio_createwithbuffer(0, 0, UIO_SYSSPACE, UIO_READ, |
248 | &uio_buf[0], sizeof(uio_buf)); | |
1c79356b A |
249 | |
250 | /* Keep up an even flow... */ | |
251 | for (;;) | |
252 | { | |
253 | /* | |
254 | * This should be large enough to encompass a full DDP packet plus | |
255 | * domain header. | |
256 | */ | |
257 | #define A_LARGE_SIZE 700 | |
258 | ||
259 | flags = MSG_DONTWAIT; | |
91447636 A |
260 | uio_setresid(auio, A_LARGE_SIZE); |
261 | *err = soreceive(so, (struct sockaddr **)&from, auio, &p_mbuf, 0, &flags); | |
1c79356b A |
262 | dPrintf(D_M_AURP, D_L_VERBOSE, |
263 | ("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err, events)); | |
264 | /* soreceive() sets *mp to zero! at start */ | |
265 | if (p_mbuf) | |
55e303ae | 266 | ip_to_atalk((struct sockaddr_in *)from, p_mbuf); |
1c79356b A |
267 | if (*err || (p_mbuf == NULL)) { |
268 | /* | |
269 | * An error occurred in soreceive(), | |
270 | * so clear the data input event flag | |
271 | * and break out of this inner loop. | |
272 | * | |
273 | * XXX Note that clearing AE_UDPIP here could | |
274 | * cause us to lose an AE_UDPIP event that | |
275 | * was posted in aurp_global.event between | |
276 | * the soreceive() above and the code here. | |
277 | * The protocol should recover from this | |
278 | * lost event, though, since the next | |
279 | * request (a tickle, for example) from | |
280 | * the other end of the tunnel will cause | |
281 | * another AE_UDPIP event to be posted, | |
282 | * which will wake us from the sleep at | |
283 | * the top of the outer loop. | |
284 | */ | |
1c79356b A |
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 | */ | |
91447636 | 302 | void aurp_wakeup(__unused struct socket *so, register caddr_t p, __unused int state) |
1c79356b | 303 | { |
1c79356b A |
304 | register int bit; |
305 | ||
306 | bit = (int) p; | |
307 | ATDISABLE(s, aurp_global.glock); | |
308 | aurp_global.event |= bit; | |
309 | ATENABLE(s, aurp_global.glock); | |
310 | ||
311 | dPrintf(D_M_AURP, D_L_STATE_CHG, | |
312 | ("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n", | |
313 | bit, aurp_global.event)); | |
314 | ||
9bccf70c | 315 | wakeup(&aurp_global.event_anchor); |
1c79356b A |
316 | } |
317 | ||
318 | /* | |
319 | * Try to bind to the specified reserved port. | |
320 | * Sort of like sobind(), but no suser() check. | |
321 | */ | |
322 | static int | |
323 | aurp_bindrp(struct socket *so) | |
324 | { | |
325 | struct sockaddr_in sin; | |
326 | struct proc *p = current_proc(); | |
1c79356b A |
327 | int error; |
328 | ||
329 | ||
330 | bzero(&sin, sizeof(sin)); | |
331 | sin.sin_family = AF_INET; | |
332 | sin.sin_addr.s_addr = htons(aurp_global.src_addr); | |
333 | sin.sin_port = htons(aurp_global.udp_port); | |
334 | sin.sin_len = sizeof(struct sockaddr_in); | |
335 | ||
336 | sblock(&so->so_rcv, M_WAIT); | |
337 | sblock(&so->so_snd, M_WAIT); | |
338 | so->so_state |= SS_PRIV; | |
339 | error = (*so->so_proto->pr_usrreqs->pru_bind)(so, (struct sockaddr *) &sin, p); | |
91447636 A |
340 | sbunlock(&so->so_snd, 0); |
341 | sbunlock(&so->so_rcv, 0); | |
1c79356b A |
342 | |
343 | return (error); | |
344 | } | |
345 | ||
346 | /* | |
347 | * receive from UDP | |
348 | * fp is the 'source address' mbuf; p_mbuf is the data mbuf. | |
349 | * Use the source address to find the 'node number' (index of the address), | |
350 | * and pass that to the next stage. | |
351 | */ | |
352 | int ip_to_atalk(register struct sockaddr_in *rem_addr, register gbuf_t *p_mbuf) | |
353 | { | |
354 | register aurp_domain_t *domain; | |
355 | unsigned char node; | |
356 | ||
357 | ||
358 | /* determine the node where the packet came from */ | |
359 | for (node=1; node <= dst_addr_cnt; node++) { | |
360 | if (aurp_global.dst_addr[node] == *(long *)&rem_addr->sin_addr) | |
361 | break; | |
362 | } | |
363 | if (node > dst_addr_cnt) { | |
364 | dPrintf(D_M_AURP, D_L_WARNING, | |
365 | ("AURPrecv: invalid node, %d.%lx\n", | |
366 | rem_addr->sin_port, | |
367 | rem_addr->sin_addr.s_addr)); | |
368 | ||
369 | gbuf_freem(p_mbuf); | |
370 | FREE(rem_addr, M_SONAME); | |
371 | return -1; | |
372 | } | |
373 | ||
374 | /* validate the domain */ | |
375 | domain = (aurp_domain_t *)gbuf_rptr(p_mbuf); | |
376 | if ( (domain->dst_length != IP_LENGTH) || | |
377 | (domain->dst_authority != IP_AUTHORITY) || | |
378 | (domain->version != AUD_Version) || | |
379 | ((domain->type != AUD_Atalk) && (domain->type != AUD_AURP)) ) { | |
380 | dPrintf(D_M_AURP, D_L_WARNING, | |
381 | ("AURPrecv: invalid domain, %d.%lx\n", | |
382 | rem_addr->sin_port, | |
383 | rem_addr->sin_addr.s_addr)); | |
384 | ||
385 | gbuf_freem(p_mbuf); | |
386 | FREE(rem_addr, M_SONAME); | |
387 | return -1; | |
388 | } | |
389 | ||
390 | /* Remove domain header */ | |
391 | p_mbuf->m_pkthdr.len -= IP_DOMAINSIZE; | |
392 | gbuf_rinc(p_mbuf,IP_DOMAINSIZE); | |
393 | gbuf_set_type(p_mbuf, MSG_DATA); | |
394 | ||
395 | /* forward the packet to the local AppleTalk stack */ | |
396 | ||
397 | at_insert(p_mbuf, domain->type, node); | |
398 | FREE(rem_addr, M_SONAME); | |
399 | return 0; | |
400 | } | |
401 | ||
402 | /* | |
403 | * send to UDP | |
404 | * The real work has been done already. Here, we just cobble together | |
405 | * a sockaddr for the destination and call sosend(). | |
406 | */ | |
407 | void | |
408 | atalk_to_ip(register gbuf_t *m) | |
409 | { register aurp_domain_t *domain; | |
410 | int error; | |
411 | int flags = MSG_DONTWAIT; | |
412 | struct sockaddr_in rem_addr; | |
1c79356b A |
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 |