fix OneNAND erase/write
[qemu] / slirp / udp.c
index 8cf4cbd..11e78cd 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
 /*
  * Changes and additions relating to SLiRP
  * Copyright (c) 1995 Danny Gasparovski.
- * 
- * Please read the file COPYRIGHT for the 
+ *
+ * Please read the file COPYRIGHT for the
  * terms and conditions of the copyright.
  */
 
 #include <slirp.h>
 #include "ip_icmp.h"
 
+#ifdef LOG_ENABLED
 struct udpstat udpstat;
+#endif
 
 struct socket udb;
 
+static u_int8_t udp_tos(struct socket *so);
+static void udp_emu(struct socket *so, struct mbuf *m);
+
 /*
  * UDP protocol implementation.
  * Per RFC 768, August, 1980.
  */
 #ifndef        COMPAT_42
-int    udpcksum = 1;
+#define UDPCKSUM 1
 #else
-int    udpcksum = 0;           /* XXX */
+#define UDPCKSUM 0 /* XXX */
 #endif
 
 struct socket *udp_last_so = &udb;
 
 void
-udp_init()
+udp_init(void)
 {
        udb.so_next = udb.so_prev = &udb;
 }
-/* m->m_data  points at ip packet header 
- * m->m_len   length ip packet 
+/* m->m_data  points at ip packet header
+ * m->m_len   length ip packet
  * ip->ip_len length data (IPDU)
  */
 void
-udp_input(m, iphlen)
-       register struct mbuf *m;
-       int iphlen;
+udp_input(register struct mbuf *m, int iphlen)
 {
        register struct ip *ip;
        register struct udphdr *uh;
 /*     struct mbuf *opts = 0;*/
        int len;
-       struct ip save_ip; 
+       struct ip save_ip;
        struct socket *so;
-       
+
        DEBUG_CALL("udp_input");
        DEBUG_ARG("m = %lx", (long)m);
        DEBUG_ARG("iphlen = %d", iphlen);
-       
-       udpstat.udps_ipackets++;
+
+       STAT(udpstat.udps_ipackets++);
 
        /*
         * Strip IP options, if any; should skip this,
@@ -113,34 +112,33 @@ udp_input(m, iphlen)
 
        if (ip->ip_len != len) {
                if (len > ip->ip_len) {
-                       udpstat.udps_badlen++;
+                       STAT(udpstat.udps_badlen++);
                        goto bad;
                }
                m_adj(m, len - ip->ip_len);
                ip->ip_len = len;
        }
-       
+
        /*
         * Save a copy of the IP header in case we want restore it
         * for sending an ICMP error message in response.
         */
-       save_ip = *ip; 
+       save_ip = *ip;
        save_ip.ip_len+= iphlen;         /* tcp_input subtracts this */
 
        /*
         * Checksum extended UDP header and data.
         */
-       if (udpcksum && uh->uh_sum) {
-         ((struct ipovly *)ip)->ih_next = 0;
-         ((struct ipovly *)ip)->ih_prev = 0;
+       if (UDPCKSUM && uh->uh_sum) {
+      memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
          ((struct ipovly *)ip)->ih_x1 = 0;
          ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
          /* keep uh_sum for ICMP reply
-          * uh->uh_sum = cksum(m, len + sizeof (struct ip)); 
-          * if (uh->uh_sum) { 
+          * uh->uh_sum = cksum(m, len + sizeof (struct ip));
+          * if (uh->uh_sum) {
           */
          if(cksum(m, len + sizeof(struct ip))) {
-           udpstat.udps_badsum++;
+           STAT(udpstat.udps_badsum++);
            goto bad;
          }
        }
@@ -153,6 +151,9 @@ udp_input(m, iphlen)
             goto bad;
         }
 
+        if (slirp_restrict)
+            goto bad;
+
         /*
          *  handle TFTP
          */
@@ -168,7 +169,7 @@ udp_input(m, iphlen)
        if (so->so_lport != uh->uh_sport ||
            so->so_laddr.s_addr != ip->ip_src.s_addr) {
                struct socket *tmp;
-               
+
                for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
                        if (tmp->so_lport == uh->uh_sport &&
                            tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
@@ -181,11 +182,11 @@ udp_input(m, iphlen)
                if (tmp == &udb) {
                  so = NULL;
                } else {
-                 udpstat.udpps_pcbcachemiss++;
+                 STAT(udpstat.udpps_pcbcachemiss++);
                  udp_last_so = so;
                }
        }
-       
+
        if (so == NULL) {
          /*
           * If there's no socket for this packet,
@@ -193,22 +194,22 @@ udp_input(m, iphlen)
           */
          if ((so = socreate()) == NULL) goto bad;
          if(udp_attach(so) == -1) {
-           DEBUG_MISC((dfd," udp_attach errno = %d-%s\n", 
+           DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
                        errno,strerror(errno)));
            sofree(so);
            goto bad;
          }
-         
+
          /*
           * Setup fields
           */
          /* udp_last_so = so; */
          so->so_laddr = ip->ip_src;
          so->so_lport = uh->uh_sport;
-         
+
          if ((so->so_iptos = udp_tos(so)) == 0)
            so->so_iptos = ip->ip_tos;
-         
+
          /*
           * XXXXX Here, check if it's in udpexec_list,
           * and if it is, do the fork_exec() etc.
@@ -233,7 +234,7 @@ udp_input(m, iphlen)
          m->m_data -= iphlen;
          *ip=save_ip;
          DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
-         icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));  
+         icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
        }
 
        m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */
@@ -251,7 +252,7 @@ bad:
        return;
 }
 
-int udp_output2(struct socket *so, struct mbuf *m, 
+int udp_output2(struct socket *so, struct mbuf *m,
                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                 int iptos)
 {
@@ -269,13 +270,13 @@ int udp_output2(struct socket *so, struct mbuf *m,
         */
        m->m_data -= sizeof(struct udpiphdr);
        m->m_len += sizeof(struct udpiphdr);
-       
+
        /*
         * Fill in mbuf with extended UDP header
         * and addresses and length put into network format.
         */
        ui = mtod(m, struct udpiphdr *);
-       ui->ui_next = ui->ui_prev = 0;
+    memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
        ui->ui_x1 = 0;
        ui->ui_pr = IPPROTO_UDP;
        ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
@@ -290,23 +291,23 @@ int udp_output2(struct socket *so, struct mbuf *m,
         * Stuff checksum and output datagram.
         */
        ui->ui_sum = 0;
-       if (udpcksum) {
+       if (UDPCKSUM) {
            if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
                ui->ui_sum = 0xffff;
        }
        ((struct ip *)ui)->ip_len = m->m_len;
 
-       ((struct ip *)ui)->ip_ttl = ip_defttl;
+       ((struct ip *)ui)->ip_ttl = IPDEFTTL;
        ((struct ip *)ui)->ip_tos = iptos;
-       
-       udpstat.udps_opackets++;
-       
+
+       STAT(udpstat.udps_opackets++);
+
        error = ip_output(so, m);
-       
+
        return (error);
 }
 
-int udp_output(struct socket *so, struct mbuf *m, 
+int udp_output(struct socket *so, struct mbuf *m,
                struct sockaddr_in *addr)
 
 {
@@ -314,22 +315,23 @@ int udp_output(struct socket *so, struct mbuf *m,
 
     saddr = *addr;
     if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
-        saddr.sin_addr.s_addr = so->so_faddr.s_addr;
         if ((so->so_faddr.s_addr & htonl(0x000000ff)) == htonl(0xff))
             saddr.sin_addr.s_addr = alias_addr.s_addr;
+        else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
+                 (ntohl(so->so_faddr.s_addr) & 0xff) != CTL_ALIAS)
+            saddr.sin_addr.s_addr = so->so_faddr.s_addr;
     }
     daddr.sin_addr = so->so_laddr;
     daddr.sin_port = so->so_lport;
-    
+
     return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
 }
 
 int
-udp_attach(so)
-     struct socket *so;
+udp_attach(struct socket *so)
 {
   struct sockaddr_in addr;
-       
+
   if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
     /*
      * Here, we bind() the socket.  Although not really needed
@@ -358,8 +360,7 @@ udp_attach(so)
 }
 
 void
-udp_detach(so)
-       struct socket *so;
+udp_detach(struct socket *so)
 {
        closesocket(so->s);
        /* if (so->so_m) m_free(so->so_m);    done by sofree */
@@ -367,7 +368,7 @@ udp_detach(so)
        sofree(so);
 }
 
-struct tos_t udptos[] = {
+static const struct tos_t udptos[] = {
        {0, 53, IPTOS_LOWDELAY, 0},                     /* DNS */
        {517, 517, IPTOS_LOWDELAY, EMU_TALK},   /* talk */
        {518, 518, IPTOS_LOWDELAY, EMU_NTALK},  /* ntalk */
@@ -375,12 +376,11 @@ struct tos_t udptos[] = {
        {0, 0, 0, 0}
 };
 
-u_int8_t
-udp_tos(so)
-       struct socket *so;
+static u_int8_t
+udp_tos(struct socket *so)
 {
        int i = 0;
-       
+
        while(udptos[i].tos) {
                if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
                    (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
@@ -389,7 +389,7 @@ udp_tos(so)
                }
                i++;
        }
-       
+
        return 0;
 }
 
@@ -400,29 +400,27 @@ udp_tos(so)
 /*
  * Here, talk/ytalk/ntalk requests must be emulated
  */
-void
-udp_emu(so, m)
-       struct socket *so;
-       struct mbuf *m;
+static void
+udp_emu(struct socket *so, struct mbuf *m)
 {
        struct sockaddr_in addr;
-        int addrlen = sizeof(addr);
+       socklen_t addrlen = sizeof(addr);
 #ifdef EMULATE_TALK
        CTL_MSG_OLD *omsg;
        CTL_MSG *nmsg;
        char buff[sizeof(CTL_MSG)];
        u_char type;
-       
+
 struct talk_request {
        struct talk_request *next;
        struct socket *udp_so;
        struct socket *tcp_so;
 } *req;
-       
-       static struct talk_request *req_tbl = 0;        
-       
+
+       static struct talk_request *req_tbl = 0;
+
 #endif
-       
+
 struct cu_header {
        uint16_t        d_family;               // destination family
        uint16_t        d_port;                 // destination port
@@ -449,7 +447,7 @@ struct cu_header {
                 */
                if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
                        return;
-               
+
 #define        IS_OLD  (so->so_emu == EMU_TALK)
 
 #define COPY_MSG(dest, src) { dest->type = src->type; \
@@ -471,19 +469,19 @@ struct cu_header {
                        type = omsg->type;
                        OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
                        OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
-                       strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
-               } else {                /* new talk */  
+                        pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
+               } else {                /* new talk */
                        omsg = (CTL_MSG_OLD *) buff;
                        nmsg = mtod(m, CTL_MSG *);
                        type = nmsg->type;
                        OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
                        OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
-                       strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
+                        pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
                }
-               
-               if (type == LOOK_UP) 
+
+               if (type == LOOK_UP)
                        return;         /* for LOOK_UP this is enough */
-                       
+
                if (IS_OLD) {           /* make a copy of the message */
                        COPY_MSG(nmsg, omsg);
                        nmsg->vers = 1;
@@ -502,75 +500,75 @@ struct cu_header {
                 * ports, 517 and 518. This is why we have two copies
                 * of the message, one in old talk and one in new talk
                 * format.
-                */ 
+                */
 
                if (type == ANNOUNCE) {
                        int s;
                        u_short temp_port;
-                       
+
                        for(req = req_tbl; req; req = req->next)
                                if (so == req->udp_so)
                                        break;          /* found it */
-                                       
+
                        if (!req) {     /* no entry for so, create new */
                                req = (struct talk_request *)
                                        malloc(sizeof(struct talk_request));
                                req->udp_so = so;
-                               req->tcp_so = solisten(0,               
-                                       OTOSIN(omsg, addr)->sin_addr.s_addr,    
+                               req->tcp_so = solisten(0,
+                                       OTOSIN(omsg, addr)->sin_addr.s_addr,
                                        OTOSIN(omsg, addr)->sin_port,
                                        SS_FACCEPTONCE);
                                req->next = req_tbl;
                                req_tbl = req;
-                       }                       
-                       
+                       }
+
                        /* replace port number in addr field */
                        addrlen = sizeof(addr);
-                       getsockname(req->tcp_so->s, 
+                       getsockname(req->tcp_so->s,
                                        (struct sockaddr *) &addr,
-                                       &addrlen);              
+                                       &addrlen);
                        OTOSIN(omsg, addr)->sin_port = addr.sin_port;
                        OTOSIN(omsg, addr)->sin_addr = our_addr;
                        OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
-                       OTOSIN(nmsg, addr)->sin_addr = our_addr;                
-                       
+                       OTOSIN(nmsg, addr)->sin_addr = our_addr;
+
                        /* send LEAVE_INVITEs */
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
-                       omsg->type = nmsg->type = LEAVE_INVITE;                 
-                       
+                       omsg->type = nmsg->type = LEAVE_INVITE;
+
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
                        addr.sin_addr = our_addr;
                        addr.sin_family = AF_INET;
                        addr.sin_port = htons(517);
-                       sendto(s, (char *)omsg, sizeof(*omsg), 0, 
+                       sendto(s, (char *)omsg, sizeof(*omsg), 0,
                                (struct sockaddr *)&addr, sizeof(addr));
                        addr.sin_port = htons(518);
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
                                (struct sockaddr *) &addr, sizeof(addr));
                        closesocket(s) ;
 
-                       omsg->type = nmsg->type = ANNOUNCE; 
+                       omsg->type = nmsg->type = ANNOUNCE;
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
                }
-               
-               /*      
+
+               /*
                 * If it is a DELETE message, we send a copy to the
                 * local daemons. Then we delete the entry corresponding
                 * to our socket from the request table.
                 */
-               
+
                if (type == DELETE) {
                        struct talk_request *temp_req, *req_next;
                        int s;
                        u_short temp_port;
-                       
+
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
-                       
+
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
                        addr.sin_addr = our_addr;
                        addr.sin_family = AF_INET;
@@ -581,7 +579,7 @@ struct cu_header {
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
                                (struct sockaddr *)&addr, sizeof(addr));
                        closesocket(s);
-                       
+
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
 
@@ -604,18 +602,18 @@ struct cu_header {
                                }
                        }
                }
-               
-               return;         
+
+               return;
 #endif
-               
+
        case EMU_CUSEEME:
-       
+
                /*
                 * Cu-SeeMe emulation.
                 * Hopefully the packet is more that 16 bytes long. We don't
                 * do any other tests, just replace the address and port
                 * fields.
-                */ 
+                */
                if (m->m_len >= sizeof (*cu_head)) {
                        if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
                                return;
@@ -623,22 +621,18 @@ struct cu_header {
                        cu_head->s_port = addr.sin_port;
                        cu_head->so_addr = our_addr.s_addr;
                }
-               
+
                return;
        }
 }
 
 struct socket *
-udp_listen(port, laddr, lport, flags)
-       u_int port;
-       u_int32_t laddr;
-       u_int lport;
-       int flags;
+udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
 {
        struct sockaddr_in addr;
        struct socket *so;
-       int addrlen = sizeof(struct sockaddr_in), opt = 1;
-       
+       socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
+
        if ((so = socreate()) == NULL) {
                free(so);
                return NULL;
@@ -657,20 +651,20 @@ udp_listen(port, laddr, lport, flags)
        }
        setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
 /*     setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
-       
+
        getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
        so->so_fport = addr.sin_port;
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
           so->so_faddr = alias_addr;
        else
           so->so_faddr = addr.sin_addr;
-       
+
        so->so_lport = lport;
        so->so_laddr.s_addr = laddr;
        if (flags != SS_FACCEPTONCE)
           so->so_expire = 0;
-       
+
        so->so_state = SS_ISFCONNECTED;
-       
+
        return so;
 }