|
|
1.1 ! root 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: BOOTPClient bootp_clients[NB_ADDR]; ! 40: ! 41: static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE }; ! 42: #if BOOTP_VEND_NEXT ! 43: static const uint8_t magic_next[] = {'N','e','X','T'}; ! 44: #endif ! 45: ! 46: ! 47: static BOOTPClient *get_new_addr(struct in_addr *paddr) ! 48: { ! 49: BOOTPClient *bc; ! 50: int i; ! 51: ! 52: for(i = 0; i < NB_ADDR; i++) { ! 53: if (!bootp_clients[i].allocated) ! 54: goto found; ! 55: } ! 56: return NULL; ! 57: found: ! 58: bc = &bootp_clients[i]; ! 59: bc->allocated = 1; ! 60: paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR)); ! 61: return bc; ! 62: } ! 63: ! 64: static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr) ! 65: { ! 66: BOOTPClient *bc; ! 67: int i; ! 68: ! 69: for(i = 0; i < NB_ADDR; i++) { ! 70: if (!memcmp(macaddr, bootp_clients[i].macaddr, 6)) ! 71: goto found; ! 72: } ! 73: return NULL; ! 74: found: ! 75: bc = &bootp_clients[i]; ! 76: bc->allocated = 1; ! 77: paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR)); ! 78: return bc; ! 79: } ! 80: ! 81: static void dhcp_decode(const uint8_t *buf, int size, ! 82: int *pmsg_type) ! 83: { ! 84: const uint8_t *p, *p_end; ! 85: int len, tag; ! 86: ! 87: *pmsg_type = 0; ! 88: ! 89: p = buf; ! 90: p_end = buf + size; ! 91: if (size < 5) ! 92: return; ! 93: if (memcmp(p, rfc1533_cookie, 4) != 0) ! 94: return; ! 95: p += 4; ! 96: while (p < p_end) { ! 97: tag = p[0]; ! 98: if (tag == RFC1533_PAD) { ! 99: p++; ! 100: } else if (tag == RFC1533_END) { ! 101: break; ! 102: } else { ! 103: p++; ! 104: if (p >= p_end) ! 105: break; ! 106: len = *p++; ! 107: ! 108: switch(tag) { ! 109: case RFC2132_MSG_TYPE: ! 110: if (len >= 1) ! 111: *pmsg_type = p[0]; ! 112: break; ! 113: default: ! 114: break; ! 115: } ! 116: p += len; ! 117: } ! 118: } ! 119: } ! 120: ! 121: static void bootp_reply(struct bootp_t *bp) ! 122: { ! 123: BOOTPClient *bc; ! 124: struct mbuf *m; ! 125: struct bootp_t *rbp; ! 126: struct sockaddr_in saddr, daddr; ! 127: struct in_addr dns_addr; ! 128: int dhcp_msg_type, val; ! 129: uint8_t *q; ! 130: ! 131: /* extract exact DHCP msg type */ ! 132: dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type); ! 133: ! 134: if (dhcp_msg_type == 0) ! 135: dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */ ! 136: ! 137: if (dhcp_msg_type != DHCPDISCOVER && ! 138: dhcp_msg_type != DHCPREQUEST) ! 139: return; ! 140: /* XXX: this is a hack to get the client mac address */ ! 141: memcpy(client_ethaddr, bp->bp_hwaddr, 6); ! 142: ! 143: if ((m = m_get()) == NULL) ! 144: return; ! 145: m->m_data += if_maxlinkhdr; ! 146: rbp = (struct bootp_t *)m->m_data; ! 147: m->m_data += sizeof(struct udpiphdr); ! 148: memset(rbp, 0, sizeof(struct bootp_t)); ! 149: ! 150: if (dhcp_msg_type == DHCPDISCOVER) { ! 151: new_addr: ! 152: bc = get_new_addr(&daddr.sin_addr); ! 153: if (!bc) ! 154: return; ! 155: memcpy(bc->macaddr, client_ethaddr, 6); ! 156: } else { ! 157: bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr); ! 158: if (!bc) { ! 159: /* if never assigned, behaves as if it was already ! 160: assigned (windows fix because it remembers its address) */ ! 161: goto new_addr; ! 162: } ! 163: } ! 164: ! 165: saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS); ! 166: saddr.sin_port = htons(BOOTP_SERVER); ! 167: ! 168: daddr.sin_port = htons(BOOTP_CLIENT); ! 169: ! 170: rbp->bp_op = BOOTP_REPLY; ! 171: rbp->bp_xid = bp->bp_xid; ! 172: rbp->bp_htype = 1; ! 173: rbp->bp_hlen = 6; ! 174: memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6); ! 175: ! 176: rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */ ! 177: rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */ ! 178: ! 179: q = rbp->bp_vend; ! 180: #if BOOTP_VEND_NEXT ! 181: memcpy(q, magic_next, 4); ! 182: q += 4; ! 183: *q++ = 1; ! 184: *q++ = 0; ! 185: *q++ = 0; ! 186: memset(q, 0, 56); ! 187: q += 56; ! 188: *q++ = 0; ! 189: #else ! 190: memcpy(q, rfc1533_cookie, 4); ! 191: q += 4; ! 192: if (dhcp_msg_type == DHCPDISCOVER) { ! 193: *q++ = RFC2132_MSG_TYPE; ! 194: *q++ = 1; ! 195: *q++ = DHCPOFFER; ! 196: } else if (dhcp_msg_type == DHCPREQUEST) { ! 197: *q++ = RFC2132_MSG_TYPE; ! 198: *q++ = 1; ! 199: *q++ = DHCPACK; ! 200: } ! 201: ! 202: if (dhcp_msg_type == DHCPDISCOVER || ! 203: dhcp_msg_type == DHCPREQUEST) { ! 204: *q++ = RFC2132_SRV_ID; ! 205: *q++ = 4; ! 206: memcpy(q, &saddr.sin_addr, 4); ! 207: q += 4; ! 208: ! 209: *q++ = RFC1533_NETMASK; ! 210: *q++ = 4; ! 211: *q++ = 0xff; ! 212: *q++ = 0xff; ! 213: *q++ = 0xff; ! 214: *q++ = 0x00; ! 215: ! 216: *q++ = RFC1533_GATEWAY; ! 217: *q++ = 4; ! 218: memcpy(q, &saddr.sin_addr, 4); ! 219: q += 4; ! 220: ! 221: *q++ = RFC1533_DNS; ! 222: *q++ = 4; ! 223: dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS); ! 224: memcpy(q, &dns_addr, 4); ! 225: q += 4; ! 226: ! 227: *q++ = RFC2132_LEASE_TIME; ! 228: *q++ = 4; ! 229: val = htonl(LEASE_TIME); ! 230: memcpy(q, &val, 4); ! 231: q += 4; ! 232: ! 233: if (*slirp_hostname) { ! 234: val = strlen(slirp_hostname); ! 235: *q++ = RFC1533_HOSTNAME; ! 236: *q++ = val; ! 237: memcpy(q, slirp_hostname, val); ! 238: q += val; ! 239: } ! 240: } ! 241: *q++ = RFC1533_END; ! 242: #endif ! 243: m->m_len = sizeof(struct bootp_t) - ! 244: sizeof(struct ip) - sizeof(struct udphdr); ! 245: udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); ! 246: } ! 247: ! 248: void bootp_input(struct mbuf *m) ! 249: { ! 250: struct bootp_t *bp = mtod(m, struct bootp_t *); ! 251: ! 252: if (bp->bp_op == BOOTP_REQUEST) { ! 253: bootp_reply(bp); ! 254: } ! 255: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.