debug options survived to mgmt close (and new mgmt open).
[vde.git] / vde / slirpvde / udp.c
blob7154cec0f620169ccf5ad748ad9c5bc833a64775
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 <config.h>
46 #include <slirp.h>
48 #include <ip_icmp.h>
50 #ifdef VDE
51 extern int dhcpmgmt;
52 #endif
54 struct udpstat udpstat;
56 struct socket udb;
59 * UDP protocol implementation.
60 * Per RFC 768, August, 1980.
62 #ifndef COMPAT_42
63 int udpcksum = 1;
64 #else
65 int udpcksum = 0; /* XXX */
66 #endif
68 struct socket *udp_last_so = &udb;
70 void
71 udp_init()
73 udb.so_next = udb.so_prev = &udb;
75 /* m->m_data points at ip packet header
76 * m->m_len length ip packet
77 * ip->ip_len length data (IPDU)
79 void
80 udp_input(register struct mbuf *m, int iphlen)
82 register struct ip *ip;
83 register struct udphdr *uh;
84 /* struct mbuf *opts = 0;*/
85 int len;
86 struct ip save_ip;
87 struct socket *so;
89 DEBUG_CALL("udp_input");
90 DEBUG_ARG("m = %lx", (long)m);
91 DEBUG_ARG("iphlen = %d", iphlen);
93 udpstat.udps_ipackets++;
96 * Strip IP options, if any; should skip this,
97 * make available to user, and use on returned packets,
98 * but we don't yet have a way to check the checksum
99 * with options still present.
101 if(iphlen > sizeof(struct ip)) {
102 ip_stripoptions(m, (struct mbuf *)0);
103 iphlen = sizeof(struct ip);
107 * Get IP and UDP header together in first mbuf.
109 ip = mtod(m, struct ip *);
110 uh = (struct udphdr *)((caddr_t)ip + iphlen);
113 * Make mbuf data length reflect UDP length.
114 * If not enough data to reflect UDP length, drop.
116 len = ntohs((u_int16_t)uh->uh_ulen);
118 if (ip->ip_len != len) {
119 if (len > ip->ip_len) {
120 udpstat.udps_badlen++;
121 goto bad;
123 m_adj(m, len - ip->ip_len);
124 ip->ip_len = len;
128 * Save a copy of the IP header in case we want restore it
129 * for sending an ICMP error message in response.
131 save_ip = *ip;
132 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
135 * Checksum extended UDP header and data.
137 if (udpcksum && uh->uh_sum) {
138 ((struct ipovly *)ip)->ih_next = 0;
139 ((struct ipovly *)ip)->ih_prev = 0;
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 udpstat.udps_badsum++;
148 goto bad;
153 * handle DHCP/BOOTP
155 #ifdef VDE
156 if (ntohs(uh->uh_dport) == BOOTP_SERVER && dhcpmgmt) {
157 #else
158 if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
159 #endif
160 bootp_input(m);
161 goto bad;
165 * Locate pcb for datagram.
167 so = udp_last_so;
168 if (so->so_lport != uh->uh_sport ||
169 so->so_laddr.s_addr != ip->ip_src.s_addr) {
170 struct socket *tmp;
172 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
173 if (tmp->so_lport == uh->uh_sport &&
174 tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
175 tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
176 tmp->so_fport = uh->uh_dport;
177 so = tmp;
178 break;
181 if (tmp == &udb) {
182 so = NULL;
183 } else {
184 udpstat.udpps_pcbcachemiss++;
185 udp_last_so = so;
189 if (so == NULL) {
191 * If there's no socket for this packet,
192 * create one
194 if ((so = socreate()) == NULL) goto bad;
195 if(udp_attach(so) == -1) {
196 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
197 errno,strerror(errno)));
198 sofree(so);
199 goto bad;
203 * Setup fields
205 /* udp_last_so = so; */
206 so->so_laddr = ip->ip_src;
207 so->so_lport = uh->uh_sport;
208 so->so_faddr = ip->ip_dst; /* XXX */
209 so->so_fport = uh->uh_dport; /* XXX */
211 if ((so->so_iptos = udp_tos(so)) == 0)
212 so->so_iptos = ip->ip_tos;
215 * XXXXX Here, check if it's in udpexec_list,
216 * and if it is, do the fork_exec() etc.
220 iphlen += sizeof(struct udphdr);
221 m->m_len -= iphlen;
222 m->m_data += iphlen;
225 * Now we sendto() the packet.
227 if (so->so_emu)
228 udp_emu(so, m);
230 if(sosendto(so,m) == -1) {
231 m->m_len += iphlen;
232 m->m_data -= iphlen;
233 *ip=save_ip;
234 DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
235 icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
238 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
240 /* restore the orig mbuf packet */
241 m->m_len += iphlen;
242 m->m_data -= iphlen;
243 *ip=save_ip;
244 so->so_m=m; /* ICMP backup */
246 return;
247 bad:
248 m_freem(m);
249 /* if (opts) m_freem(opts); */
250 return;
253 int udp_output2(struct socket *so, struct mbuf *m,
254 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
255 int iptos)
257 register struct udpiphdr *ui;
258 int error = 0;
260 DEBUG_CALL("udp_output");
261 DEBUG_ARG("so = %lx", (long)so);
262 DEBUG_ARG("m = %lx", (long)m);
263 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
264 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
267 * Adjust for header
269 m->m_data -= sizeof(struct udpiphdr);
270 m->m_len += sizeof(struct udpiphdr);
273 * Fill in mbuf with extended UDP header
274 * and addresses and length put into network format.
276 ui = mtod(m, struct udpiphdr *);
277 ui->ui_next = ui->ui_prev = 0;
278 ui->ui_x1 = 0;
279 ui->ui_pr = IPPROTO_UDP;
280 ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
281 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
282 ui->ui_src = saddr->sin_addr;
283 ui->ui_dst = daddr->sin_addr;
284 ui->ui_sport = saddr->sin_port;
285 ui->ui_dport = daddr->sin_port;
286 ui->ui_ulen = ui->ui_len;
289 * Stuff checksum and output datagram.
291 ui->ui_sum = 0;
292 if (udpcksum) {
293 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
294 ui->ui_sum = 0xffff;
296 ((struct ip *)ui)->ip_len = m->m_len;
298 ((struct ip *)ui)->ip_ttl = ip_defttl;
299 ((struct ip *)ui)->ip_tos = iptos;
301 udpstat.udps_opackets++;
303 error = ip_output(so, m);
305 return (error);
308 int udp_output(struct socket *so, struct mbuf *m,
309 struct sockaddr_in *addr)
312 struct sockaddr_in saddr, daddr;
314 saddr = *addr;
315 if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr)
316 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
317 daddr.sin_addr = so->so_laddr;
318 daddr.sin_port = so->so_lport;
320 return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
324 udp_attach(so)
325 struct socket *so;
327 struct sockaddr_in addr;
329 if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
331 * Here, we bind() the socket. Although not really needed
332 * (sendto() on an unbound socket will bind it), it's done
333 * here so that emulation of ytalk etc. don't have to do it
335 addr.sin_family = AF_INET;
336 addr.sin_port = 0;
337 addr.sin_addr.s_addr = INADDR_ANY;
338 if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
339 int lasterrno=errno;
340 close(so->s);
341 so->s=-1;
342 errno=lasterrno;
343 } else {
344 /* success, insert in queue */
345 so->so_expire = curtime + SO_EXPIRE;
346 insque(so,&udb);
349 return(so->s);
352 void
353 udp_detach(so)
354 struct socket *so;
356 close(so->s);
357 /* if (so->so_m) m_free(so->so_m); done by sofree */
359 sofree(so);
362 struct tos_t udptos[] = {
363 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
364 {517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
365 {518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
366 {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
367 {0, 0, 0, 0}
370 u_int8_t
371 udp_tos(so)
372 struct socket *so;
374 int i = 0;
376 while(udptos[i].tos) {
377 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
378 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
379 so->so_emu = udptos[i].emu;
380 return udptos[i].tos;
382 i++;
385 return 0;
388 #ifdef EMULATE_TALK
389 #include <talkd.h>
390 #endif
393 * Here, talk/ytalk/ntalk requests must be emulated
395 void
396 udp_emu(so, m)
397 struct socket *so;
398 struct mbuf *m;
400 struct sockaddr_in addr;
401 int addrlen = sizeof(addr);
402 #ifdef EMULATE_TALK
403 CTL_MSG_OLD *omsg;
404 CTL_MSG *nmsg;
405 char buff[sizeof(CTL_MSG)];
406 u_char type;
408 struct talk_request {
409 struct talk_request *next;
410 struct socket *udp_so;
411 struct socket *tcp_so;
412 } *req;
414 static struct talk_request *req_tbl = 0;
416 #endif
418 struct cu_header {
419 char dest[8];
420 short family;
421 u_short port;
422 u_long addr;
423 } *cu_head;
425 switch(so->so_emu) {
427 #ifdef EMULATE_TALK
428 case EMU_TALK:
429 case EMU_NTALK:
431 * Talk emulation. We always change the ctl_addr to get
432 * some answers from the daemon. When an ANNOUNCE comes,
433 * we send LEAVE_INVITE to the local daemons. Also when a
434 * DELETE comes, we send copies to the local daemons.
436 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
437 return;
439 #define IS_OLD (so->so_emu == EMU_TALK)
441 #define COPY_MSG(dest, src) { dest->type = src->type; \
442 dest->id_num = src->id_num; \
443 dest->pid = src->pid; \
444 dest->addr = src->addr; \
445 dest->ctl_addr = src->ctl_addr; \
446 memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
447 memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
448 memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
450 #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
451 /* old_sockaddr to sockaddr_in */
454 if (IS_OLD) { /* old talk */
455 omsg = mtod(m, CTL_MSG_OLD*);
456 nmsg = (CTL_MSG *) buff;
457 type = omsg->type;
458 OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
459 OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
460 strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
461 } else { /* new talk */
462 omsg = (CTL_MSG_OLD *) buff;
463 nmsg = mtod(m, CTL_MSG *);
464 type = nmsg->type;
465 OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
466 OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
467 strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
470 if (type == LOOK_UP)
471 return; /* for LOOK_UP this is enough */
473 if (IS_OLD) { /* make a copy of the message */
474 COPY_MSG(nmsg, omsg);
475 nmsg->vers = 1;
476 nmsg->answer = 0;
477 } else
478 COPY_MSG(omsg, nmsg);
481 * If if is an ANNOUNCE message, we go through the
482 * request table to see if a tcp port has already
483 * been redirected for this socket. If not, we solisten()
484 * a new socket and add this entry to the table.
485 * The port number of the tcp socket and our IP
486 * are put to the addr field of the message structures.
487 * Then a LEAVE_INVITE is sent to both local daemon
488 * ports, 517 and 518. This is why we have two copies
489 * of the message, one in old talk and one in new talk
490 * format.
493 if (type == ANNOUNCE) {
494 int s;
495 u_short temp_port;
497 for(req = req_tbl; req; req = req->next)
498 if (so == req->udp_so)
499 break; /* found it */
501 if (!req) { /* no entry for so, create new */
502 req = (struct talk_request *)
503 malloc(sizeof(struct talk_request));
504 req->udp_so = so;
505 req->tcp_so = solisten(0,
506 OTOSIN(omsg, addr)->sin_addr.s_addr,
507 OTOSIN(omsg, addr)->sin_port,
508 SS_FACCEPTONCE);
509 req->next = req_tbl;
510 req_tbl = req;
513 /* replace port number in addr field */
514 addrlen = sizeof(addr);
515 getsockname(req->tcp_so->s,
516 (struct sockaddr *) &addr,
517 &addrlen);
518 OTOSIN(omsg, addr)->sin_port = addr.sin_port;
519 OTOSIN(omsg, addr)->sin_addr = our_addr;
520 OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
521 OTOSIN(nmsg, addr)->sin_addr = our_addr;
523 /* send LEAVE_INVITEs */
524 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
525 OTOSIN(omsg, ctl_addr)->sin_port = 0;
526 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
527 omsg->type = nmsg->type = LEAVE_INVITE;
529 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
530 addr.sin_addr = our_addr;
531 addr.sin_family = AF_INET;
532 addr.sin_port = htons(517);
533 sendto(s, (char *)omsg, sizeof(*omsg), 0,
534 (struct sockaddr *)&addr, sizeof(addr));
535 addr.sin_port = htons(518);
536 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
537 (struct sockaddr *) &addr, sizeof(addr));
538 close(s) ;
540 omsg->type = nmsg->type = ANNOUNCE;
541 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
542 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
546 * If it is a DELETE message, we send a copy to the
547 * local daemons. Then we delete the entry corresponding
548 * to our socket from the request table.
551 if (type == DELETE) {
552 struct talk_request *temp_req, *req_next;
553 int s;
554 u_short temp_port;
556 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
557 OTOSIN(omsg, ctl_addr)->sin_port = 0;
558 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
560 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
561 addr.sin_addr = our_addr;
562 addr.sin_family = AF_INET;
563 addr.sin_port = htons(517);
564 sendto(s, (char *)omsg, sizeof(*omsg), 0,
565 (struct sockaddr *)&addr, sizeof(addr));
566 addr.sin_port = htons(518);
567 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
568 (struct sockaddr *)&addr, sizeof(addr));
569 close(s);
571 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
572 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
574 /* delete table entry */
575 if (so == req_tbl->udp_so) {
576 temp_req = req_tbl;
577 req_tbl = req_tbl->next;
578 free(temp_req);
579 } else {
580 temp_req = req_tbl;
581 for(req = req_tbl->next; req; req = req_next) {
582 req_next = req->next;
583 if (so == req->udp_so) {
584 temp_req->next = req_next;
585 free(req);
586 break;
587 } else {
588 temp_req = req;
594 return;
595 #endif
597 case EMU_CUSEEME:
600 * Cu-SeeMe emulation.
601 * Hopefully the packet is more that 16 bytes long. We don't
602 * do any other tests, just replace the address and port
603 * fields.
605 if (m->m_len >= sizeof (*cu_head)) {
606 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
607 return;
608 cu_head = mtod(m, struct cu_header *);
609 cu_head->port = addr.sin_port;
610 cu_head->addr = (u_long) our_addr.s_addr;
613 return;
617 struct socket *
618 udp_listen(port, laddr, lport, flags)
619 u_int port;
620 u_int32_t laddr;
621 u_int lport;
622 int flags;
624 struct sockaddr_in addr;
625 struct socket *so;
626 int addrlen = sizeof(struct sockaddr_in), opt = 1;
628 if ((so = socreate()) == NULL) {
629 free(so);
630 return NULL;
632 so->s = socket(AF_INET,SOCK_DGRAM,0);
633 so->so_expire = curtime + SO_EXPIRE;
634 insque(so,&udb);
636 addr.sin_family = AF_INET;
637 addr.sin_addr.s_addr = INADDR_ANY;
638 addr.sin_port = port;
640 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
641 udp_detach(so);
642 return NULL;
644 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
645 /* setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
647 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
648 so->so_fport = addr.sin_port;
649 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
650 so->so_faddr = our_addr;
651 else
652 so->so_faddr = addr.sin_addr;
654 so->so_lport = lport;
655 so->so_laddr.s_addr = laddr;
656 if (flags != SS_FACCEPTONCE)
657 so->so_expire = 0;
659 so->so_state = SS_ISFCONNECTED;
661 return so;