bootp.c revision a39ab93d71daee43e60b32f614c426492d5cf2d2
0N/A/*
2362N/A * QEMU BOOTP/DHCP server
0N/A *
0N/A * Copyright (c) 2004 Fabrice Bellard
0N/A *
0N/A * Permission is hereby granted, free of charge, to any person obtaining a copy
2362N/A * of this software and associated documentation files (the "Software"), to deal
0N/A * in the Software without restriction, including without limitation the rights
2362N/A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0N/A * copies of the Software, and to permit persons to whom the Software is
0N/A * furnished to do so, subject to the following conditions:
0N/A *
0N/A * The above copyright notice and this permission notice shall be included in
0N/A * all copies or substantial portions of the Software.
0N/A *
0N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2362N/A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2362N/A * THE SOFTWARE.
2362N/A */
0N/A#include <slirp.h>
0N/A
0N/A/* XXX: only DHCP is supported */
0N/A
0N/A#ifndef VBOX
0N/A#define NB_ADDR 16
0N/A
0N/A#define START_ADDR 15
0N/A
0N/A#define LEASE_TIME (24 * 3600)
0N/A
0N/Atypedef struct {
0N/A uint8_t allocated;
0N/A uint8_t macaddr[6];
0N/A} BOOTPClient;
0N/A
0N/ABOOTPClient bootp_clients[NB_ADDR];
0N/A
0N/Aconst char *bootp_filename;
0N/A#endif /* !VBOX */
0N/A
0N/Astatic const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
0N/A
0N/A#ifndef VBOX
0N/A#ifdef DEBUG
0N/A#define dprintf(fmt, args...) \
0N/Aif (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## args); fflush(dfd); }
0N/A#else
0N/A#define dprintf(fmt, args...)
0N/A#endif
0N/A#else /* VBOX */
0N/ADECLINLINE(void) dprintf(const char *pszFormat, ...)
0N/A{
0N/A#ifdef LOG_ENABLED
0N/A va_list args;
0N/A va_start(args, pszFormat);
0N/A Log(("dhcp: %N", pszFormat, &args));
0N/A va_end(args);
0N/A#endif
0N/A}
0N/A#endif /* VBOX */
0N/A
0N/A#ifdef VBOX
0N/Astatic BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
0N/A#else /* !VBOX */
0N/Astatic BOOTPClient *get_new_addr(struct in_addr *paddr)
0N/A#endif /* !VBOX */
0N/A{
0N/A BOOTPClient *bc;
0N/A int i;
0N/A
0N/A for(i = 0; i < NB_ADDR; i++) {
0N/A if (!bootp_clients[i].allocated)
0N/A goto found;
0N/A }
0N/A return NULL;
0N/A found:
0N/A bc = &bootp_clients[i];
0N/A bc->allocated = 1;
0N/A paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
0N/A return bc;
0N/A}
0N/A
0N/A#ifdef VBOX
0N/Astatic void release_addr(PNATState pData, struct in_addr *paddr)
0N/A{
0N/A int i;
0N/A
0N/A i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
0N/A if (i >= NB_ADDR)
0N/A return;
0N/A memset(bootp_clients[i].macaddr, '\0', 6);
0N/A bootp_clients[i].allocated = 0;
0N/A}
0N/A#endif /* VBOX */
0N/A
0N/A#ifdef VBOX
0N/Astatic BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
0N/A#else /* !VBOX */
0N/Astatic BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
0N/A#endif /* !VBOX */
0N/A{
0N/A BOOTPClient *bc;
0N/A int i;
0N/A
0N/A for(i = 0; i < NB_ADDR; i++) {
0N/A if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
0N/A goto found;
0N/A }
0N/A return NULL;
0N/A found:
0N/A bc = &bootp_clients[i];
0N/A bc->allocated = 1;
0N/A paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
0N/A return bc;
0N/A}
0N/A
0N/Astatic void dhcp_decode(const uint8_t *buf, int size,
0N/A int *pmsg_type)
0N/A{
0N/A const uint8_t *p, *p_end;
0N/A int len, tag;
0N/A
0N/A *pmsg_type = 0;
0N/A
0N/A p = buf;
0N/A p_end = buf + size;
0N/A if (size < 5)
0N/A return;
0N/A if (memcmp(p, rfc1533_cookie, 4) != 0)
0N/A return;
0N/A p += 4;
0N/A while (p < p_end) {
0N/A tag = p[0];
0N/A if (tag == RFC1533_PAD) {
0N/A p++;
0N/A } else if (tag == RFC1533_END) {
0N/A break;
0N/A } else {
0N/A p++;
0N/A if (p >= p_end)
0N/A break;
0N/A len = *p++;
0N/A dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
0N/A
0N/A switch(tag) {
0N/A case RFC2132_MSG_TYPE:
0N/A if (len >= 1)
0N/A *pmsg_type = p[0];
0N/A break;
0N/A default:
0N/A break;
0N/A }
0N/A p += len;
0N/A }
0N/A }
0N/A}
0N/A
0N/A#ifdef VBOX
0N/Astatic void bootp_reply(PNATState pData, struct bootp_t *bp)
0N/A#else /* !VBOX */
0N/Astatic void bootp_reply(struct bootp_t *bp)
0N/A#endif /* !VBOX */
0N/A{
0N/A BOOTPClient *bc;
0N/A struct mbuf *m;
0N/A struct bootp_t *rbp;
0N/A struct sockaddr_in saddr, daddr;
0N/A#ifdef VBOX
0N/A struct in_addr dns_addr_dhcp;
0N/A#else /* !VBOX */
0N/A struct in_addr dns_addr;
0N/A#endif /* !VBOX */
0N/A int dhcp_msg_type, val;
0N/A uint8_t *q;
0N/A
0N/A /* extract exact DHCP msg type */
0N/A dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
0N/A dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
0N/A
0N/A if (dhcp_msg_type == 0)
0N/A dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
0N/A
0N/A#ifdef VBOX
0N/A if (dhcp_msg_type == DHCPRELEASE) {
0N/A uint32_t addr = ntohl(bp->bp_ciaddr.s_addr);
0N/A release_addr(pData, &bp->bp_ciaddr);
0N/A LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
0N/A addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
0N/A dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
0N/A /* This message is not to be answered in any way. */
0N/A return;
0N/A }
0N/A#endif /* VBOX */
0N/A if (dhcp_msg_type != DHCPDISCOVER &&
0N/A dhcp_msg_type != DHCPREQUEST)
0N/A return;
0N/A /* XXX: this is a hack to get the client mac address */
0N/A memcpy(client_ethaddr, bp->bp_hwaddr, 6);
0N/A
0N/A#ifdef VBOX
if ((m = m_get(pData)) == NULL)
#else /* !VBOX */
if ((m = m_get()) == NULL)
#endif /* !VBOX */
return;
m->m_data += if_maxlinkhdr;
rbp = (struct bootp_t *)m->m_data;
m->m_data += sizeof(struct udpiphdr);
memset(rbp, 0, sizeof(struct bootp_t));
if (dhcp_msg_type == DHCPDISCOVER) {
#ifdef VBOX
/* Do not allocate a new lease for clients that forgot that they had a lease. */
bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
if (bc)
goto reuse_lease;
#endif /* VBOX */
new_addr:
#ifdef VBOX
bc = get_new_addr(pData, &daddr.sin_addr);
#else /* !VBOX */
bc = get_new_addr(&daddr.sin_addr);
#endif /* !VBOX */
if (!bc) {
#ifdef VBOX
LogRel(("NAT: DHCP no IP address left\n"));
#endif /* VBOX */
dprintf("no address left\n");
return;
}
memcpy(bc->macaddr, client_ethaddr, 6);
#ifdef VBOX
reuse_lease: ;
#endif /* VBOX */
} else {
#ifdef VBOX
bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
#else /* !VBOX */
bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
#endif /* !VBOX */
if (!bc) {
/* if never assigned, behaves as if it was already
assigned (windows fix because it remembers its address) */
goto new_addr;
}
}
if (bootp_filename)
#ifndef VBOX
snprintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
#else
RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
#endif
#ifdef VBOX
{
uint32_t addr = ntohl(daddr.sin_addr.s_addr);
LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
}
#endif /* VBOX */
dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
saddr.sin_port = htons(BOOTP_SERVER);
daddr.sin_port = htons(BOOTP_CLIENT);
rbp->bp_op = BOOTP_REPLY;
rbp->bp_xid = bp->bp_xid;
rbp->bp_htype = 1;
rbp->bp_hlen = 6;
memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
q = rbp->bp_vend;
memcpy(q, rfc1533_cookie, 4);
q += 4;
if (dhcp_msg_type == DHCPDISCOVER) {
*q++ = RFC2132_MSG_TYPE;
*q++ = 1;
*q++ = DHCPOFFER;
} else if (dhcp_msg_type == DHCPREQUEST) {
*q++ = RFC2132_MSG_TYPE;
*q++ = 1;
*q++ = DHCPACK;
}
if (dhcp_msg_type == DHCPDISCOVER ||
dhcp_msg_type == DHCPREQUEST) {
*q++ = RFC2132_SRV_ID;
*q++ = 4;
memcpy(q, &saddr.sin_addr, 4);
q += 4;
*q++ = RFC1533_NETMASK;
*q++ = 4;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0x00;
*q++ = RFC1533_GATEWAY;
*q++ = 4;
memcpy(q, &saddr.sin_addr, 4);
q += 4;
*q++ = RFC1533_DNS;
*q++ = 4;
#ifdef VBOX
dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
memcpy(q, &dns_addr_dhcp, 4);
#else /* !VBOX */
dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
memcpy(q, &dns_addr, 4);
#endif /* !VBOX */
q += 4;
*q++ = RFC2132_LEASE_TIME;
*q++ = 4;
val = htonl(LEASE_TIME);
memcpy(q, &val, 4);
q += 4;
if (*slirp_hostname) {
val = strlen(slirp_hostname);
*q++ = RFC1533_HOSTNAME;
*q++ = val;
memcpy(q, slirp_hostname, val);
q += val;
}
}
*q++ = RFC1533_END;
m->m_len = sizeof(struct bootp_t) -
sizeof(struct ip) - sizeof(struct udphdr);
#ifdef VBOX
/* Reply to the broadcast address, as some clients perform paranoid checks. */
daddr.sin_addr.s_addr = INADDR_BROADCAST;
udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
#else /* !VBOX */
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
#endif /* !VBOX */
}
#ifdef VBOX
void bootp_input(PNATState pData, struct mbuf *m)
#else /* !VBOX */
void bootp_input(struct mbuf *m)
#endif /* !VBOX */
{
struct bootp_t *bp = mtod(m, struct bootp_t *);
if (bp->bp_op == BOOTP_REQUEST) {
#ifdef VBOX
bootp_reply(pData, bp);
#else /* !VBOX */
bootp_reply(bp);
#endif /* !VBOX */
}
}