merage qemu master
[qemu/qemu-JZ.git] / slirp / udp.c
blobc9926181ac8db2eed807e75555677c986a3c2a7d
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
34 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
45 #include <slirp.h>
46 #include "ip_icmp.h"
48 #ifdef LOG_ENABLED
49 struct udpstat udpstat;
50 #endif
52 struct socket udb;
54 static u_int8_t udp_tos(struct socket *so);
55 static void udp_emu(struct socket *so, struct mbuf *m);
58 * UDP protocol implementation.
59 * Per RFC 768, August, 1980.
61 #ifndef COMPAT_42
62 #define UDPCKSUM 1
63 #else
64 #define UDPCKSUM 0 /* XXX */
65 #endif
67 struct socket *udp_last_so = &udb;
69 void
70 udp_init()
72 udb.so_next = udb.so_prev = &udb;
74 /* m->m_data points at ip packet header
75 * m->m_len length ip packet
76 * ip->ip_len length data (IPDU)
78 void
79 udp_input(m, iphlen)
80 register struct mbuf *m;
81 int iphlen;
83 register struct ip *ip;
84 register struct udphdr *uh;
85 /* struct mbuf *opts = 0;*/
86 int len;
87 struct ip save_ip;
88 struct socket *so;
90 DEBUG_CALL("udp_input");
91 DEBUG_ARG("m = %lx", (long)m);
92 DEBUG_ARG("iphlen = %d", iphlen);
94 STAT(udpstat.udps_ipackets++);
97 * Strip IP options, if any; should skip this,
98 * make available to user, and use on returned packets,
99 * but we don't yet have a way to check the checksum
100 * with options still present.
102 if(iphlen > sizeof(struct ip)) {
103 ip_stripoptions(m, (struct mbuf *)0);
104 iphlen = sizeof(struct ip);
108 * Get IP and UDP header together in first mbuf.
110 ip = mtod(m, struct ip *);
111 uh = (struct udphdr *)((caddr_t)ip + iphlen);
114 * Make mbuf data length reflect UDP length.
115 * If not enough data to reflect UDP length, drop.
117 len = ntohs((u_int16_t)uh->uh_ulen);
119 if (ip->ip_len != len) {
120 if (len > ip->ip_len) {
121 STAT(udpstat.udps_badlen++);
122 goto bad;
124 m_adj(m, len - ip->ip_len);
125 ip->ip_len = len;
129 * Save a copy of the IP header in case we want restore it
130 * for sending an ICMP error message in response.
132 save_ip = *ip;
133 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
136 * Checksum extended UDP header and data.
138 if (UDPCKSUM && uh->uh_sum) {
139 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
140 ((struct ipovly *)ip)->ih_x1 = 0;
141 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
142 /* keep uh_sum for ICMP reply
143 * uh->uh_sum = cksum(m, len + sizeof (struct ip));
144 * if (uh->uh_sum) {
146 if(cksum(m, len + sizeof(struct ip))) {
147 STAT(udpstat.udps_badsum++);
148 goto bad;
153 * handle DHCP/BOOTP
155 if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
156 bootp_input(m);
157 goto bad;
160 if (slirp_restrict)
161 goto bad;
164 * handle TFTP
166 if (ntohs(uh->uh_dport) == TFTP_SERVER) {
167 tftp_input(m);
168 goto bad;
172 * Locate pcb for datagram.
174 so = udp_last_so;
175 if (so->so_lport != uh->uh_sport ||
176 so->so_laddr.s_addr != ip->ip_src.s_addr) {
177 struct socket *tmp;
179 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
180 if (tmp->so_lport == uh->uh_sport &&
181 tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
182 tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
183 tmp->so_fport = uh->uh_dport;
184 so = tmp;
185 break;
188 if (tmp == &udb) {
189 so = NULL;
190 } else {
191 STAT(udpstat.udpps_pcbcachemiss++);
192 udp_last_so = so;
196 if (so == NULL) {
198 * If there's no socket for this packet,
199 * create one
201 if ((so = socreate()) == NULL) goto bad;
202 if(udp_attach(so) == -1) {
203 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
204 errno,strerror(errno)));
205 sofree(so);
206 goto bad;
210 * Setup fields
212 /* udp_last_so = so; */
213 so->so_laddr = ip->ip_src;
214 so->so_lport = uh->uh_sport;
216 if ((so->so_iptos = udp_tos(so)) == 0)
217 so->so_iptos = ip->ip_tos;
220 * XXXXX Here, check if it's in udpexec_list,
221 * and if it is, do the fork_exec() etc.
225 so->so_faddr = ip->ip_dst; /* XXX */
226 so->so_fport = uh->uh_dport; /* XXX */
228 iphlen += sizeof(struct udphdr);
229 m->m_len -= iphlen;
230 m->m_data += iphlen;
233 * Now we sendto() the packet.
235 if (so->so_emu)
236 udp_emu(so, m);
238 if(sosendto(so,m) == -1) {
239 m->m_len += iphlen;
240 m->m_data -= iphlen;
241 *ip=save_ip;
242 DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
243 icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
246 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
248 /* restore the orig mbuf packet */
249 m->m_len += iphlen;
250 m->m_data -= iphlen;
251 *ip=save_ip;
252 so->so_m=m; /* ICMP backup */
254 return;
255 bad:
256 m_freem(m);
257 /* if (opts) m_freem(opts); */
258 return;
261 int udp_output2(struct socket *so, struct mbuf *m,
262 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
263 int iptos)
265 register struct udpiphdr *ui;
266 int error = 0;
268 DEBUG_CALL("udp_output");
269 DEBUG_ARG("so = %lx", (long)so);
270 DEBUG_ARG("m = %lx", (long)m);
271 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
272 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
275 * Adjust for header
277 m->m_data -= sizeof(struct udpiphdr);
278 m->m_len += sizeof(struct udpiphdr);
281 * Fill in mbuf with extended UDP header
282 * and addresses and length put into network format.
284 ui = mtod(m, struct udpiphdr *);
285 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
286 ui->ui_x1 = 0;
287 ui->ui_pr = IPPROTO_UDP;
288 ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
289 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
290 ui->ui_src = saddr->sin_addr;
291 ui->ui_dst = daddr->sin_addr;
292 ui->ui_sport = saddr->sin_port;
293 ui->ui_dport = daddr->sin_port;
294 ui->ui_ulen = ui->ui_len;
297 * Stuff checksum and output datagram.
299 ui->ui_sum = 0;
300 if (UDPCKSUM) {
301 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
302 ui->ui_sum = 0xffff;
304 ((struct ip *)ui)->ip_len = m->m_len;
306 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
307 ((struct ip *)ui)->ip_tos = iptos;
309 STAT(udpstat.udps_opackets++);
311 error = ip_output(so, m);
313 return (error);
316 int udp_output(struct socket *so, struct mbuf *m,
317 struct sockaddr_in *addr)
320 struct sockaddr_in saddr, daddr;
322 saddr = *addr;
323 if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
324 if ((so->so_faddr.s_addr & htonl(0x000000ff)) == htonl(0xff))
325 saddr.sin_addr.s_addr = alias_addr.s_addr;
326 else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
327 (ntohl(so->so_faddr.s_addr) & 0xff) != CTL_ALIAS)
328 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
330 daddr.sin_addr = so->so_laddr;
331 daddr.sin_port = so->so_lport;
333 return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
337 udp_attach(so)
338 struct socket *so;
340 struct sockaddr_in addr;
342 if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
344 * Here, we bind() the socket. Although not really needed
345 * (sendto() on an unbound socket will bind it), it's done
346 * here so that emulation of ytalk etc. don't have to do it
348 addr.sin_family = AF_INET;
349 addr.sin_port = 0;
350 addr.sin_addr.s_addr = INADDR_ANY;
351 if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
352 int lasterrno=errno;
353 closesocket(so->s);
354 so->s=-1;
355 #ifdef _WIN32
356 WSASetLastError(lasterrno);
357 #else
358 errno=lasterrno;
359 #endif
360 } else {
361 /* success, insert in queue */
362 so->so_expire = curtime + SO_EXPIRE;
363 insque(so,&udb);
366 return(so->s);
369 void
370 udp_detach(so)
371 struct socket *so;
373 closesocket(so->s);
374 /* if (so->so_m) m_free(so->so_m); done by sofree */
376 sofree(so);
379 static const struct tos_t udptos[] = {
380 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
381 {517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
382 {518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
383 {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
384 {0, 0, 0, 0}
387 static u_int8_t
388 udp_tos(struct socket *so)
390 int i = 0;
392 while(udptos[i].tos) {
393 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
394 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
395 so->so_emu = udptos[i].emu;
396 return udptos[i].tos;
398 i++;
401 return 0;
404 #ifdef EMULATE_TALK
405 #include "talkd.h"
406 #endif
409 * Here, talk/ytalk/ntalk requests must be emulated
411 static void
412 udp_emu(struct socket *so, struct mbuf *m)
414 struct sockaddr_in addr;
415 socklen_t addrlen = sizeof(addr);
416 #ifdef EMULATE_TALK
417 CTL_MSG_OLD *omsg;
418 CTL_MSG *nmsg;
419 char buff[sizeof(CTL_MSG)];
420 u_char type;
422 struct talk_request {
423 struct talk_request *next;
424 struct socket *udp_so;
425 struct socket *tcp_so;
426 } *req;
428 static struct talk_request *req_tbl = 0;
430 #endif
432 struct cu_header {
433 uint16_t d_family; // destination family
434 uint16_t d_port; // destination port
435 uint32_t d_addr; // destination address
436 uint16_t s_family; // source family
437 uint16_t s_port; // source port
438 uint32_t so_addr; // source address
439 uint32_t seqn; // sequence number
440 uint16_t message; // message
441 uint16_t data_type; // data type
442 uint16_t pkt_len; // packet length
443 } *cu_head;
445 switch(so->so_emu) {
447 #ifdef EMULATE_TALK
448 case EMU_TALK:
449 case EMU_NTALK:
451 * Talk emulation. We always change the ctl_addr to get
452 * some answers from the daemon. When an ANNOUNCE comes,
453 * we send LEAVE_INVITE to the local daemons. Also when a
454 * DELETE comes, we send copies to the local daemons.
456 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
457 return;
459 #define IS_OLD (so->so_emu == EMU_TALK)
461 #define COPY_MSG(dest, src) { dest->type = src->type; \
462 dest->id_num = src->id_num; \
463 dest->pid = src->pid; \
464 dest->addr = src->addr; \
465 dest->ctl_addr = src->ctl_addr; \
466 memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
467 memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
468 memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
470 #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
471 /* old_sockaddr to sockaddr_in */
474 if (IS_OLD) { /* old talk */
475 omsg = mtod(m, CTL_MSG_OLD*);
476 nmsg = (CTL_MSG *) buff;
477 type = omsg->type;
478 OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
479 OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
480 pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
481 } else { /* new talk */
482 omsg = (CTL_MSG_OLD *) buff;
483 nmsg = mtod(m, CTL_MSG *);
484 type = nmsg->type;
485 OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
486 OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
487 pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
490 if (type == LOOK_UP)
491 return; /* for LOOK_UP this is enough */
493 if (IS_OLD) { /* make a copy of the message */
494 COPY_MSG(nmsg, omsg);
495 nmsg->vers = 1;
496 nmsg->answer = 0;
497 } else
498 COPY_MSG(omsg, nmsg);
501 * If if is an ANNOUNCE message, we go through the
502 * request table to see if a tcp port has already
503 * been redirected for this socket. If not, we solisten()
504 * a new socket and add this entry to the table.
505 * The port number of the tcp socket and our IP
506 * are put to the addr field of the message structures.
507 * Then a LEAVE_INVITE is sent to both local daemon
508 * ports, 517 and 518. This is why we have two copies
509 * of the message, one in old talk and one in new talk
510 * format.
513 if (type == ANNOUNCE) {
514 int s;
515 u_short temp_port;
517 for(req = req_tbl; req; req = req->next)
518 if (so == req->udp_so)
519 break; /* found it */
521 if (!req) { /* no entry for so, create new */
522 req = (struct talk_request *)
523 malloc(sizeof(struct talk_request));
524 req->udp_so = so;
525 req->tcp_so = solisten(0,
526 OTOSIN(omsg, addr)->sin_addr.s_addr,
527 OTOSIN(omsg, addr)->sin_port,
528 SS_FACCEPTONCE);
529 req->next = req_tbl;
530 req_tbl = req;
533 /* replace port number in addr field */
534 addrlen = sizeof(addr);
535 getsockname(req->tcp_so->s,
536 (struct sockaddr *) &addr,
537 &addrlen);
538 OTOSIN(omsg, addr)->sin_port = addr.sin_port;
539 OTOSIN(omsg, addr)->sin_addr = our_addr;
540 OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
541 OTOSIN(nmsg, addr)->sin_addr = our_addr;
543 /* send LEAVE_INVITEs */
544 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
545 OTOSIN(omsg, ctl_addr)->sin_port = 0;
546 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
547 omsg->type = nmsg->type = LEAVE_INVITE;
549 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
550 addr.sin_addr = our_addr;
551 addr.sin_family = AF_INET;
552 addr.sin_port = htons(517);
553 sendto(s, (char *)omsg, sizeof(*omsg), 0,
554 (struct sockaddr *)&addr, sizeof(addr));
555 addr.sin_port = htons(518);
556 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
557 (struct sockaddr *) &addr, sizeof(addr));
558 closesocket(s) ;
560 omsg->type = nmsg->type = ANNOUNCE;
561 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
562 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
566 * If it is a DELETE message, we send a copy to the
567 * local daemons. Then we delete the entry corresponding
568 * to our socket from the request table.
571 if (type == DELETE) {
572 struct talk_request *temp_req, *req_next;
573 int s;
574 u_short temp_port;
576 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
577 OTOSIN(omsg, ctl_addr)->sin_port = 0;
578 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
580 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
581 addr.sin_addr = our_addr;
582 addr.sin_family = AF_INET;
583 addr.sin_port = htons(517);
584 sendto(s, (char *)omsg, sizeof(*omsg), 0,
585 (struct sockaddr *)&addr, sizeof(addr));
586 addr.sin_port = htons(518);
587 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
588 (struct sockaddr *)&addr, sizeof(addr));
589 closesocket(s);
591 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
592 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
594 /* delete table entry */
595 if (so == req_tbl->udp_so) {
596 temp_req = req_tbl;
597 req_tbl = req_tbl->next;
598 free(temp_req);
599 } else {
600 temp_req = req_tbl;
601 for(req = req_tbl->next; req; req = req_next) {
602 req_next = req->next;
603 if (so == req->udp_so) {
604 temp_req->next = req_next;
605 free(req);
606 break;
607 } else {
608 temp_req = req;
614 return;
615 #endif
617 case EMU_CUSEEME:
620 * Cu-SeeMe emulation.
621 * Hopefully the packet is more that 16 bytes long. We don't
622 * do any other tests, just replace the address and port
623 * fields.
625 if (m->m_len >= sizeof (*cu_head)) {
626 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
627 return;
628 cu_head = mtod(m, struct cu_header *);
629 cu_head->s_port = addr.sin_port;
630 cu_head->so_addr = our_addr.s_addr;
633 return;
637 struct socket *
638 udp_listen(port, laddr, lport, flags)
639 u_int port;
640 u_int32_t laddr;
641 u_int lport;
642 int flags;
644 struct sockaddr_in addr;
645 struct socket *so;
646 socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
648 if ((so = socreate()) == NULL) {
649 free(so);
650 return NULL;
652 so->s = socket(AF_INET,SOCK_DGRAM,0);
653 so->so_expire = curtime + SO_EXPIRE;
654 insque(so,&udb);
656 addr.sin_family = AF_INET;
657 addr.sin_addr.s_addr = INADDR_ANY;
658 addr.sin_port = port;
660 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
661 udp_detach(so);
662 return NULL;
664 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
665 /* setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
667 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
668 so->so_fport = addr.sin_port;
669 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
670 so->so_faddr = alias_addr;
671 else
672 so->so_faddr = addr.sin_addr;
674 so->so_lport = lport;
675 so->so_laddr.s_addr = laddr;
676 if (flags != SS_FACCEPTONCE)
677 so->so_expire = 0;
679 so->so_state = SS_ISFCONNECTED;
681 return so;