1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
8 #define WANT_SYS_IOCTL_H
9 #include <slirp.h>
10 #include "ip_icmp.h"
11 #include "main.h"
12 #ifdef __sun__
13 #include <sys/filio.h>
14 #endif
15 #define SLIRP_COMPILATION 1
16 #include "sockets.h"
17 #include "proxy_common.h"
18
19 void
so_init()20 so_init()
21 {
22 /* Nothing yet */
23 }
24
25
26 struct socket *
solookup(head,laddr,lport,faddr,fport)27 solookup(head, laddr, lport, faddr, fport)
28 struct socket *head;
29 uint32_t laddr;
30 u_int lport;
31 uint32_t faddr;
32 u_int fport;
33 {
34 struct socket *so;
35
36 for (so = head->so_next; so != head; so = so->so_next) {
37 if (so->so_laddr_port == lport &&
38 so->so_laddr_ip == laddr &&
39 so->so_faddr_ip == faddr &&
40 so->so_faddr_port == fport)
41 break;
42 }
43
44 if (so == head)
45 return (struct socket *)NULL;
46 return so;
47
48 }
49
50 /*
51 * Create a new socket, initialise the fields
52 * It is the responsibility of the caller to
53 * insque() it into the correct linked-list
54 */
55 struct socket *
socreate()56 socreate()
57 {
58 struct socket *so;
59
60 so = (struct socket *)malloc(sizeof(struct socket));
61 if(so) {
62 memset(so, 0, sizeof(struct socket));
63 so->so_state = SS_NOFDREF;
64 so->s = -1;
65 }
66 return(so);
67 }
68
69 /*
70 * remque and free a socket, clobber cache
71 */
72 void
sofree(so)73 sofree(so)
74 struct socket *so;
75 {
76 if (so->so_state & SS_PROXIFIED)
77 proxy_manager_del(so);
78
79 if (so->extra) {
80 sofree(so->extra);
81 so->extra=NULL;
82 }
83 if (so == tcp_last_so)
84 tcp_last_so = &tcb;
85 else if (so == udp_last_so)
86 udp_last_so = &udb;
87
88 mbuf_free(so->so_m);
89
90 if(so->so_next && so->so_prev)
91 remque(so); /* crashes if so is not in a queue */
92
93 free(so);
94 }
95
96 /*
97 * Read from so's socket into sb_snd, updating all relevant sbuf fields
98 * NOTE: This will only be called if it is select()ed for reading, so
99 * a read() of 0 (or less) means it's disconnected
100 */
101 int
soread(so)102 soread(so)
103 struct socket *so;
104 {
105 int n, nn, lss, total;
106 SBuf sb = &so->so_snd;
107 int len = sb->sb_datalen - sb->sb_cc;
108 struct iovec iov[2];
109 int mss = so->so_tcpcb->t_maxseg;
110
111 DEBUG_CALL("soread");
112 DEBUG_ARG("so = %lx", (long )so);
113
114 /*
115 * No need to check if there's enough room to read.
116 * soread wouldn't have been called if there weren't
117 */
118
119 len = sb->sb_datalen - sb->sb_cc;
120
121 iov[0].iov_base = sb->sb_wptr;
122 if (sb->sb_wptr < sb->sb_rptr) {
123 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
124 /* Should never succeed, but... */
125 if (iov[0].iov_len > len)
126 iov[0].iov_len = len;
127 if (iov[0].iov_len > mss)
128 iov[0].iov_len -= iov[0].iov_len%mss;
129 n = 1;
130 } else {
131 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
132 /* Should never succeed, but... */
133 if (iov[0].iov_len > len) iov[0].iov_len = len;
134 len -= iov[0].iov_len;
135 if (len) {
136 iov[1].iov_base = sb->sb_data;
137 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
138 if(iov[1].iov_len > len)
139 iov[1].iov_len = len;
140 total = iov[0].iov_len + iov[1].iov_len;
141 if (total > mss) {
142 lss = total%mss;
143 if (iov[1].iov_len > lss) {
144 iov[1].iov_len -= lss;
145 n = 2;
146 } else {
147 lss -= iov[1].iov_len;
148 iov[0].iov_len -= lss;
149 n = 1;
150 }
151 } else
152 n = 2;
153 } else {
154 if (iov[0].iov_len > mss)
155 iov[0].iov_len -= iov[0].iov_len%mss;
156 n = 1;
157 }
158 }
159
160 #ifdef HAVE_READV
161 nn = readv(so->s, (struct iovec *)iov, n);
162 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
163 #else
164 nn = socket_recv(so->s, iov[0].iov_base, iov[0].iov_len);
165 #endif
166 if (nn <= 0) {
167 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
168 return 0;
169 else {
170 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,errno_str));
171 sofcantrcvmore(so);
172 tcp_sockclosed(sototcpcb(so));
173 return -1;
174 }
175 }
176
177 #ifndef HAVE_READV
178 /*
179 * If there was no error, try and read the second time round
180 * We read again if n = 2 (ie, there's another part of the buffer)
181 * and we read as much as we could in the first read
182 * We don't test for <= 0 this time, because there legitimately
183 * might not be any more data (since the socket is non-blocking),
184 * a close will be detected on next iteration.
185 * A return of -1 wont (shouldn't) happen, since it didn't happen above
186 */
187 if (n == 2 && nn == iov[0].iov_len) {
188 int ret;
189 ret = socket_recv(so->s, iov[1].iov_base, iov[1].iov_len);
190 if (ret > 0)
191 nn += ret;
192 }
193
194 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
195 #endif
196
197 /* Update fields */
198 sb->sb_cc += nn;
199 sb->sb_wptr += nn;
200 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
201 sb->sb_wptr -= sb->sb_datalen;
202 return nn;
203 }
204
205 /*
206 * Get urgent data
207 *
208 * When the socket is created, we set it SO_OOBINLINE,
209 * so when OOB data arrives, we soread() it and everything
210 * in the send buffer is sent as urgent data
211 */
212 void
sorecvoob(so)213 sorecvoob(so)
214 struct socket *so;
215 {
216 struct tcpcb *tp = sototcpcb(so);
217
218 DEBUG_CALL("sorecvoob");
219 DEBUG_ARG("so = %lx", (long)so);
220
221 /*
222 * We take a guess at how much urgent data has arrived.
223 * In most situations, when urgent data arrives, the next
224 * read() should get all the urgent data. This guess will
225 * be wrong however if more data arrives just after the
226 * urgent data, or the read() doesn't return all the
227 * urgent data.
228 */
229 soread(so);
230 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
231 tp->t_force = 1;
232 tcp_output(tp);
233 tp->t_force = 0;
234 }
235
236 /*
237 * Send urgent data
238 * There's a lot duplicated code here, but...
239 */
240 int
sosendoob(so)241 sosendoob(so)
242 struct socket *so;
243 {
244 SBuf sb = &so->so_rcv;
245 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
246 int n, len;
247
248 DEBUG_CALL("sosendoob");
249 DEBUG_ARG("so = %lx", (long)so);
250 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
251
252 if (so->so_urgc > 2048)
253 so->so_urgc = 2048; /* XXXX */
254
255 if (sb->sb_rptr < sb->sb_wptr) {
256 /* We can send it directly */
257 n = socket_send_oob(so->s, sb->sb_rptr, so->so_urgc); /* |MSG_DONTWAIT)); */
258 so->so_urgc -= n;
259
260 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
261 } else {
262 /*
263 * Since there's no sendv or sendtov like writev,
264 * we must copy all data to a linear buffer then
265 * send it all
266 */
267 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
268 if (len > so->so_urgc) len = so->so_urgc;
269 memcpy(buff, sb->sb_rptr, len);
270 so->so_urgc -= len;
271 if (so->so_urgc) {
272 n = sb->sb_wptr - sb->sb_data;
273 if (n > so->so_urgc) n = so->so_urgc;
274 memcpy((buff + len), sb->sb_data, n);
275 so->so_urgc -= n;
276 len += n;
277 }
278 n = socket_send_oob(so->s, buff, len); /* |MSG_DONTWAIT)); */
279 #ifdef DEBUG
280 if (n != len)
281 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
282 #endif
283 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
284 }
285
286 sb->sb_cc -= n;
287 sb->sb_rptr += n;
288 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
289 sb->sb_rptr -= sb->sb_datalen;
290
291 return n;
292 }
293
294 /*
295 * Write data from so_rcv to so's socket,
296 * updating all sbuf field as necessary
297 */
298 int
sowrite(so)299 sowrite(so)
300 struct socket *so;
301 {
302 int n,nn;
303 SBuf sb = &so->so_rcv;
304 int len = sb->sb_cc;
305 struct iovec iov[2];
306
307 DEBUG_CALL("sowrite");
308 DEBUG_ARG("so = %lx", (long)so);
309
310 if (so->so_urgc) {
311 sosendoob(so);
312 if (sb->sb_cc == 0)
313 return 0;
314 }
315
316 /*
317 * No need to check if there's something to write,
318 * sowrite wouldn't have been called otherwise
319 */
320
321 len = sb->sb_cc;
322
323 iov[0].iov_base = sb->sb_rptr;
324 if (sb->sb_rptr < sb->sb_wptr) {
325 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
326 /* Should never succeed, but... */
327 if (iov[0].iov_len > len) iov[0].iov_len = len;
328 n = 1;
329 } else {
330 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
331 if (iov[0].iov_len > len) iov[0].iov_len = len;
332 len -= iov[0].iov_len;
333 if (len) {
334 iov[1].iov_base = sb->sb_data;
335 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
336 if (iov[1].iov_len > len) iov[1].iov_len = len;
337 n = 2;
338 } else
339 n = 1;
340 }
341 /* Check if there's urgent data to send, and if so, send it */
342
343 #ifdef HAVE_READV
344 nn = writev(so->s, (const struct iovec *)iov, n);
345
346 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
347 #else
348 nn = socket_send(so->s, iov[0].iov_base, iov[0].iov_len);
349 #endif
350 /* This should never happen, but people tell me it does *shrug* */
351 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
352 return 0;
353
354 if (nn <= 0) {
355 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
356 so->so_state, errno));
357 sofcantsendmore(so);
358 tcp_sockclosed(sototcpcb(so));
359 return -1;
360 }
361
362 #ifndef HAVE_READV
363 if (n == 2 && nn == iov[0].iov_len) {
364 int ret;
365 ret = socket_send(so->s, iov[1].iov_base, iov[1].iov_len);
366 if (ret > 0)
367 nn += ret;
368 }
369 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
370 #endif
371
372 /* Update sbuf */
373 sb->sb_cc -= nn;
374 sb->sb_rptr += nn;
375 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
376 sb->sb_rptr -= sb->sb_datalen;
377
378 /*
379 * If in DRAIN mode, and there's no more data, set
380 * it CANTSENDMORE
381 */
382 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
383 sofcantsendmore(so);
384
385 return nn;
386 }
387
388 /*
389 * recvfrom() a UDP socket
390 */
391 void
sorecvfrom(so)392 sorecvfrom(so)
393 struct socket *so;
394 {
395 SockAddress addr;
396
397 DEBUG_CALL("sorecvfrom");
398 DEBUG_ARG("so = %lx", (long)so);
399
400 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
401 char buff[256];
402 int len;
403
404 len = socket_recvfrom(so->s, buff, 256, &addr);
405 /* XXX Check if reply is "correct"? */
406
407 if(len == -1 || len == 0) {
408 u_char code=ICMP_UNREACH_PORT;
409
410 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
411 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
412
413 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
414 errno,errno_str));
415 icmp_error(so->so_m, ICMP_UNREACH,code, 0,errno_str);
416 } else {
417 icmp_reflect(so->so_m);
418 so->so_m = 0; /* Don't mbuf_free() it again! */
419 }
420 /* No need for this socket anymore, udp_detach it */
421 udp_detach(so);
422 } else { /* A "normal" UDP packet */
423 MBuf m;
424 int len, n;
425
426 if (!(m = mbuf_alloc())) return;
427 m->m_data += if_maxlinkhdr;
428
429 /*
430 * XXX Shouldn't FIONREAD packets destined for port 53,
431 * but I don't know the max packet size for DNS lookups
432 */
433 len = mbuf_freeroom(m);
434 /* if (so->so_fport != htons(53)) { */
435 n = socket_can_read(so->s);
436
437 if (n > len) {
438 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
439 mbuf_ensure(m, n);
440 len = mbuf_freeroom(m);
441 }
442 /* } */
443
444 m->m_len = socket_recvfrom(so->s, m->m_data, len, &addr);
445 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
446 m->m_len, errno,errno_str));
447 if(m->m_len<0) {
448 u_char code=ICMP_UNREACH_PORT;
449
450 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
451 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
452
453 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
454 icmp_error(so->so_m, ICMP_UNREACH,code, 0,errno_str);
455 mbuf_free(m);
456 } else {
457 /*
458 * Hack: domain name lookup will be used the most for UDP,
459 * and since they'll only be used once there's no need
460 * for the 4 minute (or whatever) timeout... So we time them
461 * out much quicker (10 seconds for now...)
462 */
463 if (so->so_expire) {
464 if (so->so_faddr_port == 53)
465 so->so_expire = curtime + SO_EXPIREFAST;
466 else
467 so->so_expire = curtime + SO_EXPIRE;
468 }
469
470 /* if (m->m_len == len) {
471 * mbuf_ensure(m, MINCSIZE);
472 * m->m_len = 0;
473 * }
474 */
475
476 /*
477 * If this packet was destined for CTL_ADDR,
478 * make it look like that's where it came from, done by udp_output
479 */
480 udp_output_(so, m, &addr);
481 } /* rx error */
482 } /* if ping packet */
483 }
484
485 /*
486 * sendto() a socket
487 */
488 int
sosendto(so,m)489 sosendto(so, m)
490 struct socket *so;
491 MBuf m;
492 {
493 SockAddress addr;
494 uint32_t addr_ip;
495 uint16_t addr_port;
496 int ret;
497
498 DEBUG_CALL("sosendto");
499 DEBUG_ARG("so = %lx", (long)so);
500 DEBUG_ARG("m = %lx", (long)m);
501
502 if ((so->so_faddr_ip & 0xffffff00) == special_addr_ip) {
503 /* It's an alias */
504 int low = so->so_faddr_ip & 0xff;
505
506 if ( CTL_IS_DNS(low) )
507 addr_ip = dns_addr[low - CTL_DNS];
508 else
509 addr_ip = loopback_addr_ip;
510 } else
511 addr_ip = so->so_faddr_ip;
512
513 addr_port = so->so_faddr_port;
514
515 sock_address_init_inet(&addr, addr_ip, addr_port);
516
517 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%08x\n", addr_port, addr_ip));
518
519 /* Don't care what port we get */
520 ret = socket_sendto(so->s, m->m_data, m->m_len,&addr);
521 if (ret < 0)
522 return -1;
523
524 /*
525 * Kill the socket if there's no reply in 4 minutes,
526 * but only if it's an expirable socket
527 */
528 if (so->so_expire)
529 so->so_expire = curtime + SO_EXPIRE;
530 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
531 return 0;
532 }
533
534 /*
535 * XXX This should really be tcp_listen
536 */
537 struct socket *
solisten(port,laddr,lport,flags)538 solisten(port, laddr, lport, flags)
539 u_int port;
540 u_int32_t laddr;
541 u_int lport;
542 int flags;
543 {
544 SockAddress addr;
545 uint32_t addr_ip;
546 struct socket *so;
547 int s;
548
549 DEBUG_CALL("solisten");
550 DEBUG_ARG("port = %d", port);
551 DEBUG_ARG("laddr = %x", laddr);
552 DEBUG_ARG("lport = %d", lport);
553 DEBUG_ARG("flags = %x", flags);
554
555 if ((so = socreate()) == NULL) {
556 /* free(so); Not sofree() ??? free(NULL) == NOP */
557 return NULL;
558 }
559
560 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
561 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
562 free(so);
563 return NULL;
564 }
565 insque(so,&tcb);
566
567 /*
568 * SS_FACCEPTONCE sockets must time out.
569 */
570 if (flags & SS_FACCEPTONCE)
571 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
572
573 so->so_state = (SS_FACCEPTCONN|flags);
574 so->so_laddr_port = lport; /* Kept in host format */
575 so->so_laddr_ip = laddr; /* Ditto */
576 so->so_haddr_port = port;
577
578 s = socket_loopback_server( port, SOCKET_STREAM );
579 if (s < 0)
580 return NULL;
581
582 socket_get_address(s, &addr);
583
584 so->so_faddr_port = sock_address_get_port(&addr);
585
586 addr_ip = (uint32_t) sock_address_get_ip(&addr);
587
588 if (addr_ip == 0 || addr_ip == loopback_addr_ip)
589 so->so_faddr_ip = alias_addr_ip;
590 else
591 so->so_faddr_ip = addr_ip;
592
593 so->s = s;
594 return so;
595 }
596
597
598 int
sounlisten(u_int port)599 sounlisten(u_int port)
600 {
601 struct socket *so;
602
603 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
604 if (so->so_haddr_port == port) {
605 break;
606 }
607 }
608
609 if (so == &tcb) {
610 return -1;
611 }
612
613 sofcantrcvmore( so );
614 sofcantsendmore( so );
615 close( so->s );
616 so->s = -1;
617 sofree( so );
618 return 0;
619 }
620
621
622 /*
623 * Data is available in so_rcv
624 * Just write() the data to the socket
625 * XXX not yet...
626 */
627 void
sorwakeup(so)628 sorwakeup(so)
629 struct socket *so;
630 {
631 /* sowrite(so); */
632 /* FD_CLR(so->s,&writefds); */
633 }
634
635 /*
636 * Data has been freed in so_snd
637 * We have room for a read() if we want to
638 * For now, don't read, it'll be done in the main loop
639 */
640 void
sowwakeup(so)641 sowwakeup(so)
642 struct socket *so;
643 {
644 /* Nothing, yet */
645 }
646
647 /*
648 * Various session state calls
649 * XXX Should be #define's
650 * The socket state stuff needs work, these often get call 2 or 3
651 * times each when only 1 was needed
652 */
653 void
soisfconnecting(so)654 soisfconnecting(so)
655 register struct socket *so;
656 {
657 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
658 SS_FCANTSENDMORE|SS_FWDRAIN);
659 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
660 }
661
662 void
soisfconnected(so)663 soisfconnected(so)
664 register struct socket *so;
665 {
666 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
667 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
668 }
669
670 void
sofcantrcvmore(so)671 sofcantrcvmore(so)
672 struct socket *so;
673 {
674 if ((so->so_state & SS_NOFDREF) == 0) {
675 shutdown(so->s,0);
676 if(global_writefds) {
677 FD_CLR(so->s,global_writefds);
678 }
679 }
680 so->so_state &= ~(SS_ISFCONNECTING);
681 if (so->so_state & SS_FCANTSENDMORE)
682 so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
683 else
684 so->so_state |= SS_FCANTRCVMORE;
685 }
686
687 void
sofcantsendmore(so)688 sofcantsendmore(so)
689 struct socket *so;
690 {
691 if ((so->so_state & SS_NOFDREF) == 0) {
692 shutdown(so->s,1); /* send FIN to fhost */
693 if (global_readfds) {
694 FD_CLR(so->s,global_readfds);
695 }
696 if (global_xfds) {
697 FD_CLR(so->s,global_xfds);
698 }
699 }
700 so->so_state &= ~(SS_ISFCONNECTING);
701 if (so->so_state & SS_FCANTRCVMORE)
702 so->so_state = SS_NOFDREF; /* as above */
703 else
704 so->so_state |= SS_FCANTSENDMORE;
705 }
706
707 void
soisfdisconnected(so)708 soisfdisconnected(so)
709 struct socket *so;
710 {
711 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
712 /* close(so->s); */
713 /* so->so_state = SS_ISFDISCONNECTED; */
714 /*
715 * XXX Do nothing ... ?
716 */
717 }
718
719 /*
720 * Set write drain mode
721 * Set CANTSENDMORE once all data has been write()n
722 */
723 void
sofwdrain(so)724 sofwdrain(so)
725 struct socket *so;
726 {
727 if (so->so_rcv.sb_cc)
728 so->so_state |= SS_FWDRAIN;
729 else
730 sofcantsendmore(so);
731 }
732
733