symbian-qemu-0.9.1-12/qemu-symbian-svp/slirp/bootp.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /*
       
     2  * QEMU BOOTP/DHCP server
       
     3  *
       
     4  * Copyright (c) 2004 Fabrice Bellard
       
     5  *
       
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     7  * of this software and associated documentation files (the "Software"), to deal
       
     8  * in the Software without restriction, including without limitation the rights
       
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    10  * copies of the Software, and to permit persons to whom the Software is
       
    11  * furnished to do so, subject to the following conditions:
       
    12  *
       
    13  * The above copyright notice and this permission notice shall be included in
       
    14  * all copies or substantial portions of the Software.
       
    15  *
       
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
       
    19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    22  * THE SOFTWARE.
       
    23  */
       
    24 #include <slirp.h>
       
    25 
       
    26 /* XXX: only DHCP is supported */
       
    27 
       
    28 #define NB_ADDR 16
       
    29 
       
    30 #define START_ADDR 15
       
    31 
       
    32 #define LEASE_TIME (24 * 3600)
       
    33 
       
    34 typedef struct {
       
    35     uint8_t allocated;
       
    36     uint8_t macaddr[6];
       
    37 } BOOTPClient;
       
    38 
       
    39 static BOOTPClient bootp_clients[NB_ADDR];
       
    40 
       
    41 const char *bootp_filename;
       
    42 
       
    43 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
       
    44 
       
    45 #ifdef DEBUG
       
    46 #define dprintf(fmt, args...) \
       
    47 if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
       
    48 #else
       
    49 #define dprintf(fmt, args...)
       
    50 #endif
       
    51 
       
    52 static BOOTPClient *get_new_addr(struct in_addr *paddr)
       
    53 {
       
    54     BOOTPClient *bc;
       
    55     int i;
       
    56 
       
    57     for(i = 0; i < NB_ADDR; i++) {
       
    58         if (!bootp_clients[i].allocated)
       
    59             goto found;
       
    60     }
       
    61     return NULL;
       
    62  found:
       
    63     bc = &bootp_clients[i];
       
    64     bc->allocated = 1;
       
    65     paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
       
    66     return bc;
       
    67 }
       
    68 
       
    69 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
       
    70 {
       
    71     BOOTPClient *bc;
       
    72     int i;
       
    73 
       
    74     for(i = 0; i < NB_ADDR; i++) {
       
    75         if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
       
    76             goto found;
       
    77     }
       
    78     return NULL;
       
    79  found:
       
    80     bc = &bootp_clients[i];
       
    81     bc->allocated = 1;
       
    82     paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
       
    83     return bc;
       
    84 }
       
    85 
       
    86 static void dhcp_decode(const uint8_t *buf, int size,
       
    87                         int *pmsg_type)
       
    88 {
       
    89     const uint8_t *p, *p_end;
       
    90     int len, tag;
       
    91 
       
    92     *pmsg_type = 0;
       
    93 
       
    94     p = buf;
       
    95     p_end = buf + size;
       
    96     if (size < 5)
       
    97         return;
       
    98     if (memcmp(p, rfc1533_cookie, 4) != 0)
       
    99         return;
       
   100     p += 4;
       
   101     while (p < p_end) {
       
   102         tag = p[0];
       
   103         if (tag == RFC1533_PAD) {
       
   104             p++;
       
   105         } else if (tag == RFC1533_END) {
       
   106             break;
       
   107         } else {
       
   108             p++;
       
   109             if (p >= p_end)
       
   110                 break;
       
   111             len = *p++;
       
   112             dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
       
   113 
       
   114             switch(tag) {
       
   115             case RFC2132_MSG_TYPE:
       
   116                 if (len >= 1)
       
   117                     *pmsg_type = p[0];
       
   118                 break;
       
   119             default:
       
   120                 break;
       
   121             }
       
   122             p += len;
       
   123         }
       
   124     }
       
   125 }
       
   126 
       
   127 static void bootp_reply(struct bootp_t *bp)
       
   128 {
       
   129     BOOTPClient *bc;
       
   130     struct mbuf *m;
       
   131     struct bootp_t *rbp;
       
   132     struct sockaddr_in saddr, daddr;
       
   133     struct in_addr dns_addr;
       
   134     int dhcp_msg_type, val;
       
   135     uint8_t *q;
       
   136 
       
   137     /* extract exact DHCP msg type */
       
   138     dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
       
   139     dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
       
   140 
       
   141     if (dhcp_msg_type == 0)
       
   142         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
       
   143 
       
   144     if (dhcp_msg_type != DHCPDISCOVER &&
       
   145         dhcp_msg_type != DHCPREQUEST)
       
   146         return;
       
   147     /* XXX: this is a hack to get the client mac address */
       
   148     memcpy(client_ethaddr, bp->bp_hwaddr, 6);
       
   149 
       
   150     if ((m = m_get()) == NULL)
       
   151         return;
       
   152     m->m_data += IF_MAXLINKHDR;
       
   153     rbp = (struct bootp_t *)m->m_data;
       
   154     m->m_data += sizeof(struct udpiphdr);
       
   155     memset(rbp, 0, sizeof(struct bootp_t));
       
   156 
       
   157     if (dhcp_msg_type == DHCPDISCOVER) {
       
   158     new_addr:
       
   159         bc = get_new_addr(&daddr.sin_addr);
       
   160         if (!bc) {
       
   161             dprintf("no address left\n");
       
   162             return;
       
   163         }
       
   164         memcpy(bc->macaddr, client_ethaddr, 6);
       
   165     } else {
       
   166         bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
       
   167         if (!bc) {
       
   168             /* if never assigned, behaves as if it was already
       
   169                assigned (windows fix because it remembers its address) */
       
   170             goto new_addr;
       
   171         }
       
   172     }
       
   173 
       
   174     if (bootp_filename)
       
   175         snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
       
   176                  bootp_filename);
       
   177 
       
   178     dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
       
   179 
       
   180     saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
       
   181     saddr.sin_port = htons(BOOTP_SERVER);
       
   182 
       
   183     daddr.sin_port = htons(BOOTP_CLIENT);
       
   184 
       
   185     rbp->bp_op = BOOTP_REPLY;
       
   186     rbp->bp_xid = bp->bp_xid;
       
   187     rbp->bp_htype = 1;
       
   188     rbp->bp_hlen = 6;
       
   189     memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
       
   190 
       
   191     rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
       
   192     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
       
   193 
       
   194     q = rbp->bp_vend;
       
   195     memcpy(q, rfc1533_cookie, 4);
       
   196     q += 4;
       
   197 
       
   198     if (dhcp_msg_type == DHCPDISCOVER) {
       
   199         *q++ = RFC2132_MSG_TYPE;
       
   200         *q++ = 1;
       
   201         *q++ = DHCPOFFER;
       
   202     } else if (dhcp_msg_type == DHCPREQUEST) {
       
   203         *q++ = RFC2132_MSG_TYPE;
       
   204         *q++ = 1;
       
   205         *q++ = DHCPACK;
       
   206     }
       
   207 
       
   208     if (dhcp_msg_type == DHCPDISCOVER ||
       
   209         dhcp_msg_type == DHCPREQUEST) {
       
   210         *q++ = RFC2132_SRV_ID;
       
   211         *q++ = 4;
       
   212         memcpy(q, &saddr.sin_addr, 4);
       
   213         q += 4;
       
   214 
       
   215         *q++ = RFC1533_NETMASK;
       
   216         *q++ = 4;
       
   217         *q++ = 0xff;
       
   218         *q++ = 0xff;
       
   219         *q++ = 0xff;
       
   220         *q++ = 0x00;
       
   221 
       
   222         *q++ = RFC1533_GATEWAY;
       
   223         *q++ = 4;
       
   224         memcpy(q, &saddr.sin_addr, 4);
       
   225         q += 4;
       
   226 
       
   227         *q++ = RFC1533_DNS;
       
   228         *q++ = 4;
       
   229         dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
       
   230         memcpy(q, &dns_addr, 4);
       
   231         q += 4;
       
   232 
       
   233         *q++ = RFC2132_LEASE_TIME;
       
   234         *q++ = 4;
       
   235         val = htonl(LEASE_TIME);
       
   236         memcpy(q, &val, 4);
       
   237         q += 4;
       
   238 
       
   239         if (*slirp_hostname) {
       
   240             val = strlen(slirp_hostname);
       
   241             *q++ = RFC1533_HOSTNAME;
       
   242             *q++ = val;
       
   243             memcpy(q, slirp_hostname, val);
       
   244             q += val;
       
   245         }
       
   246     }
       
   247     *q++ = RFC1533_END;
       
   248 
       
   249     m->m_len = sizeof(struct bootp_t) -
       
   250         sizeof(struct ip) - sizeof(struct udphdr);
       
   251     udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
       
   252 }
       
   253 
       
   254 void bootp_input(struct mbuf *m)
       
   255 {
       
   256     struct bootp_t *bp = mtod(m, struct bootp_t *);
       
   257 
       
   258     if (bp->bp_op == BOOTP_REQUEST) {
       
   259         bootp_reply(bp);
       
   260     }
       
   261 }