240e1bf38aa9fc35c8a32941983299beee69fccd
[qemu.git] / slirp / udp.c
blob240e1bf38aa9fc35c8a32941983299beee69fccd
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
30 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
41 #include <slirp.h>
42 #include "ip_icmp.h"
44 struct socket udb;
46 static u_int8_t udp_tos(struct socket *so);
47 static void udp_emu(struct socket *so, struct mbuf *m);
49 struct socket *udp_last_so = &udb;
51 void
52 udp_init(void)
54 udb.so_next = udb.so_prev = &udb;
56 /* m->m_data points at ip packet header
57 * m->m_len length ip packet
58 * ip->ip_len length data (IPDU)
60 void
61 udp_input(register struct mbuf *m, int iphlen)
63 register struct ip *ip;
64 register struct udphdr *uh;
65 int len;
66 struct ip save_ip;
67 struct socket *so;
69 DEBUG_CALL("udp_input");
70 DEBUG_ARG("m = %lx", (long)m);
71 DEBUG_ARG("iphlen = %d", iphlen);
74 * Strip IP options, if any; should skip this,
75 * make available to user, and use on returned packets,
76 * but we don't yet have a way to check the checksum
77 * with options still present.
79 if(iphlen > sizeof(struct ip)) {
80 ip_stripoptions(m, (struct mbuf *)0);
81 iphlen = sizeof(struct ip);
85 * Get IP and UDP header together in first mbuf.
87 ip = mtod(m, struct ip *);
88 uh = (struct udphdr *)((caddr_t)ip + iphlen);
91 * Make mbuf data length reflect UDP length.
92 * If not enough data to reflect UDP length, drop.
94 len = ntohs((u_int16_t)uh->uh_ulen);
96 if (ip->ip_len != len) {
97 if (len > ip->ip_len) {
98 goto bad;
100 m_adj(m, len - ip->ip_len);
101 ip->ip_len = len;
105 * Save a copy of the IP header in case we want restore it
106 * for sending an ICMP error message in response.
108 save_ip = *ip;
109 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
112 * Checksum extended UDP header and data.
114 if (uh->uh_sum) {
115 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
116 ((struct ipovly *)ip)->ih_x1 = 0;
117 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
118 if(cksum(m, len + sizeof(struct ip))) {
119 goto bad;
124 * handle DHCP/BOOTP
126 if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
127 bootp_input(m);
128 goto bad;
131 if (slirp_restrict)
132 goto bad;
135 * handle TFTP
137 if (ntohs(uh->uh_dport) == TFTP_SERVER) {
138 tftp_input(m);
139 goto bad;
143 * Locate pcb for datagram.
145 so = udp_last_so;
146 if (so->so_lport != uh->uh_sport ||
147 so->so_laddr.s_addr != ip->ip_src.s_addr) {
148 struct socket *tmp;
150 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
151 if (tmp->so_lport == uh->uh_sport &&
152 tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
153 so = tmp;
154 break;
157 if (tmp == &udb) {
158 so = NULL;
159 } else {
160 udp_last_so = so;
164 if (so == NULL) {
166 * If there's no socket for this packet,
167 * create one
169 if ((so = socreate()) == NULL) goto bad;
170 if(udp_attach(so) == -1) {
171 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
172 errno,strerror(errno)));
173 sofree(so);
174 goto bad;
178 * Setup fields
180 so->so_laddr = ip->ip_src;
181 so->so_lport = uh->uh_sport;
183 if ((so->so_iptos = udp_tos(so)) == 0)
184 so->so_iptos = ip->ip_tos;
187 * XXXXX Here, check if it's in udpexec_list,
188 * and if it is, do the fork_exec() etc.
192 so->so_faddr = ip->ip_dst; /* XXX */
193 so->so_fport = uh->uh_dport; /* XXX */
195 iphlen += sizeof(struct udphdr);
196 m->m_len -= iphlen;
197 m->m_data += iphlen;
200 * Now we sendto() the packet.
202 if (so->so_emu)
203 udp_emu(so, m);
205 if(sosendto(so,m) == -1) {
206 m->m_len += iphlen;
207 m->m_data -= iphlen;
208 *ip=save_ip;
209 DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
210 icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
213 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
215 /* restore the orig mbuf packet */
216 m->m_len += iphlen;
217 m->m_data -= iphlen;
218 *ip=save_ip;
219 so->so_m=m; /* ICMP backup */
221 return;
222 bad:
223 m_freem(m);
224 return;
227 int udp_output2(struct socket *so, struct mbuf *m,
228 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
229 int iptos)
231 register struct udpiphdr *ui;
232 int error = 0;
234 DEBUG_CALL("udp_output");
235 DEBUG_ARG("so = %lx", (long)so);
236 DEBUG_ARG("m = %lx", (long)m);
237 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
238 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
241 * Adjust for header
243 m->m_data -= sizeof(struct udpiphdr);
244 m->m_len += sizeof(struct udpiphdr);
247 * Fill in mbuf with extended UDP header
248 * and addresses and length put into network format.
250 ui = mtod(m, struct udpiphdr *);
251 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
252 ui->ui_x1 = 0;
253 ui->ui_pr = IPPROTO_UDP;
254 ui->ui_len = htons(m->m_len - sizeof(struct ip));
255 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
256 ui->ui_src = saddr->sin_addr;
257 ui->ui_dst = daddr->sin_addr;
258 ui->ui_sport = saddr->sin_port;
259 ui->ui_dport = daddr->sin_port;
260 ui->ui_ulen = ui->ui_len;
263 * Stuff checksum and output datagram.
265 ui->ui_sum = 0;
266 if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
267 ui->ui_sum = 0xffff;
268 ((struct ip *)ui)->ip_len = m->m_len;
270 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
271 ((struct ip *)ui)->ip_tos = iptos;
273 error = ip_output(so, m);
275 return (error);
278 int udp_output(struct socket *so, struct mbuf *m,
279 struct sockaddr_in *addr)
282 struct sockaddr_in saddr, daddr;
284 saddr = *addr;
285 if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) {
286 if ((so->so_faddr.s_addr & ~vnetwork_mask.s_addr) ==
287 ~vnetwork_mask.s_addr) {
288 saddr.sin_addr = vhost_addr;
289 } else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
290 so->so_faddr.s_addr != vhost_addr.s_addr) {
291 saddr.sin_addr = so->so_faddr;
294 daddr.sin_addr = so->so_laddr;
295 daddr.sin_port = so->so_lport;
297 return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
301 udp_attach(struct socket *so)
303 struct sockaddr_in addr;
305 if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
307 * Here, we bind() the socket. Although not really needed
308 * (sendto() on an unbound socket will bind it), it's done
309 * here so that emulation of ytalk etc. don't have to do it
311 addr.sin_family = AF_INET;
312 addr.sin_port = 0;
313 addr.sin_addr.s_addr = INADDR_ANY;
314 if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
315 int lasterrno=errno;
316 closesocket(so->s);
317 so->s=-1;
318 #ifdef _WIN32
319 WSASetLastError(lasterrno);
320 #else
321 errno=lasterrno;
322 #endif
323 } else {
324 /* success, insert in queue */
325 so->so_expire = curtime + SO_EXPIRE;
326 insque(so,&udb);
329 return(so->s);
332 void
333 udp_detach(struct socket *so)
335 closesocket(so->s);
336 sofree(so);
339 static const struct tos_t udptos[] = {
340 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
341 {517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
342 {518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
343 {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
344 {0, 0, 0, 0}
347 static u_int8_t
348 udp_tos(struct socket *so)
350 int i = 0;
352 while(udptos[i].tos) {
353 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
354 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
355 so->so_emu = udptos[i].emu;
356 return udptos[i].tos;
358 i++;
361 return 0;
364 #ifdef EMULATE_TALK
365 #include "talkd.h"
366 #endif
369 * Here, talk/ytalk/ntalk requests must be emulated
371 static void
372 udp_emu(struct socket *so, struct mbuf *m)
374 struct sockaddr_in addr;
375 socklen_t addrlen = sizeof(addr);
376 #ifdef EMULATE_TALK
377 CTL_MSG_OLD *omsg;
378 CTL_MSG *nmsg;
379 char buff[sizeof(CTL_MSG)];
380 u_char type;
382 struct talk_request {
383 struct talk_request *next;
384 struct socket *udp_so;
385 struct socket *tcp_so;
386 } *req;
388 static struct talk_request *req_tbl = 0;
390 #endif
392 struct cu_header {
393 uint16_t d_family; // destination family
394 uint16_t d_port; // destination port
395 uint32_t d_addr; // destination address
396 uint16_t s_family; // source family
397 uint16_t s_port; // source port
398 uint32_t so_addr; // source address
399 uint32_t seqn; // sequence number
400 uint16_t message; // message
401 uint16_t data_type; // data type
402 uint16_t pkt_len; // packet length
403 } *cu_head;
405 switch(so->so_emu) {
407 #ifdef EMULATE_TALK
408 case EMU_TALK:
409 case EMU_NTALK:
411 * Talk emulation. We always change the ctl_addr to get
412 * some answers from the daemon. When an ANNOUNCE comes,
413 * we send LEAVE_INVITE to the local daemons. Also when a
414 * DELETE comes, we send copies to the local daemons.
416 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
417 return;
419 #define IS_OLD (so->so_emu == EMU_TALK)
421 #define COPY_MSG(dest, src) { dest->type = src->type; \
422 dest->id_num = src->id_num; \
423 dest->pid = src->pid; \
424 dest->addr = src->addr; \
425 dest->ctl_addr = src->ctl_addr; \
426 memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
427 memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
428 memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
430 #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
431 /* old_sockaddr to sockaddr_in */
434 if (IS_OLD) { /* old talk */
435 omsg = mtod(m, CTL_MSG_OLD*);
436 nmsg = (CTL_MSG *) buff;
437 type = omsg->type;
438 OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
439 OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
440 pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
441 } else { /* new talk */
442 omsg = (CTL_MSG_OLD *) buff;
443 nmsg = mtod(m, CTL_MSG *);
444 type = nmsg->type;
445 OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
446 OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
447 pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
450 if (type == LOOK_UP)
451 return; /* for LOOK_UP this is enough */
453 if (IS_OLD) { /* make a copy of the message */
454 COPY_MSG(nmsg, omsg);
455 nmsg->vers = 1;
456 nmsg->answer = 0;
457 } else
458 COPY_MSG(omsg, nmsg);
461 * If if is an ANNOUNCE message, we go through the
462 * request table to see if a tcp port has already
463 * been redirected for this socket. If not, we solisten()
464 * a new socket and add this entry to the table.
465 * The port number of the tcp socket and our IP
466 * are put to the addr field of the message structures.
467 * Then a LEAVE_INVITE is sent to both local daemon
468 * ports, 517 and 518. This is why we have two copies
469 * of the message, one in old talk and one in new talk
470 * format.
473 if (type == ANNOUNCE) {
474 int s;
475 u_short temp_port;
477 for(req = req_tbl; req; req = req->next)
478 if (so == req->udp_so)
479 break; /* found it */
481 if (!req) { /* no entry for so, create new */
482 req = (struct talk_request *)
483 malloc(sizeof(struct talk_request));
484 req->udp_so = so;
485 req->tcp_so = solisten(0,
486 OTOSIN(omsg, addr)->sin_addr.s_addr,
487 OTOSIN(omsg, addr)->sin_port,
488 SS_FACCEPTONCE);
489 req->next = req_tbl;
490 req_tbl = req;
493 /* replace port number in addr field */
494 addrlen = sizeof(addr);
495 getsockname(req->tcp_so->s,
496 (struct sockaddr *) &addr,
497 &addrlen);
498 OTOSIN(omsg, addr)->sin_port = addr.sin_port;
499 OTOSIN(omsg, addr)->sin_addr = our_addr;
500 OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
501 OTOSIN(nmsg, addr)->sin_addr = our_addr;
503 /* send LEAVE_INVITEs */
504 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
505 OTOSIN(omsg, ctl_addr)->sin_port = 0;
506 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
507 omsg->type = nmsg->type = LEAVE_INVITE;
509 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
510 addr.sin_addr = our_addr;
511 addr.sin_family = AF_INET;
512 addr.sin_port = htons(517);
513 sendto(s, (char *)omsg, sizeof(*omsg), 0,
514 (struct sockaddr *)&addr, sizeof(addr));
515 addr.sin_port = htons(518);
516 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
517 (struct sockaddr *) &addr, sizeof(addr));
518 closesocket(s) ;
520 omsg->type = nmsg->type = ANNOUNCE;
521 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
522 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
526 * If it is a DELETE message, we send a copy to the
527 * local daemons. Then we delete the entry corresponding
528 * to our socket from the request table.
531 if (type == DELETE) {
532 struct talk_request *temp_req, *req_next;
533 int s;
534 u_short temp_port;
536 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
537 OTOSIN(omsg, ctl_addr)->sin_port = 0;
538 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
540 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
541 addr.sin_addr = our_addr;
542 addr.sin_family = AF_INET;
543 addr.sin_port = htons(517);
544 sendto(s, (char *)omsg, sizeof(*omsg), 0,
545 (struct sockaddr *)&addr, sizeof(addr));
546 addr.sin_port = htons(518);
547 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
548 (struct sockaddr *)&addr, sizeof(addr));
549 closesocket(s);
551 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
552 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
554 /* delete table entry */
555 if (so == req_tbl->udp_so) {
556 temp_req = req_tbl;
557 req_tbl = req_tbl->next;
558 free(temp_req);
559 } else {
560 temp_req = req_tbl;
561 for(req = req_tbl->next; req; req = req_next) {
562 req_next = req->next;
563 if (so == req->udp_so) {
564 temp_req->next = req_next;
565 free(req);
566 break;
567 } else {
568 temp_req = req;
574 return;
575 #endif
577 case EMU_CUSEEME:
580 * Cu-SeeMe emulation.
581 * Hopefully the packet is more that 16 bytes long. We don't
582 * do any other tests, just replace the address and port
583 * fields.
585 if (m->m_len >= sizeof (*cu_head)) {
586 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
587 return;
588 cu_head = mtod(m, struct cu_header *);
589 cu_head->s_port = addr.sin_port;
590 cu_head->so_addr = our_addr.s_addr;
593 return;
597 struct socket *
598 udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
599 int flags)
601 struct sockaddr_in addr;
602 struct socket *so;
603 socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
605 if ((so = socreate()) == NULL) {
606 free(so);
607 return NULL;
609 so->s = socket(AF_INET,SOCK_DGRAM,0);
610 so->so_expire = curtime + SO_EXPIRE;
611 insque(so,&udb);
613 addr.sin_family = AF_INET;
614 addr.sin_addr.s_addr = haddr;
615 addr.sin_port = hport;
617 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
618 udp_detach(so);
619 return NULL;
621 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
623 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
624 so->so_fport = addr.sin_port;
625 if (addr.sin_addr.s_addr == 0 ||
626 addr.sin_addr.s_addr == loopback_addr.s_addr) {
627 so->so_faddr = vhost_addr;
628 } else {
629 so->so_faddr = addr.sin_addr;
631 so->so_lport = lport;
632 so->so_laddr.s_addr = laddr;
633 if (flags != SS_FACCEPTONCE)
634 so->so_expire = 0;
636 so->so_state &= SS_PERSISTENT_MASK;
637 so->so_state |= SS_ISFCONNECTED | flags;
639 return so;