|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018-2019 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +// For DHCP specs see: |
| 28 | +// https://www.ietf.org/rfc/rfc2131.txt |
| 29 | +// https://tools.ietf.org/html/rfc2132 -- DHCP Options and BOOTP Vendor Extensions |
| 30 | + |
| 31 | +#include <stdio.h> |
| 32 | +#include <string.h> |
| 33 | +#include <errno.h> |
| 34 | + |
| 35 | +#define PICO_CYW43_ARCH_THREADSAFE_BACKGROUND 1 |
| 36 | +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) |
| 37 | + |
| 38 | +#include "cyw43_config.h" |
| 39 | +#include "dhcpserver.h" |
| 40 | +#include "lwip/udp.h" |
| 41 | + |
| 42 | +#define DHCPDISCOVER (1) |
| 43 | +#define DHCPOFFER (2) |
| 44 | +#define DHCPREQUEST (3) |
| 45 | +#define DHCPDECLINE (4) |
| 46 | +#define DHCPACK (5) |
| 47 | +#define DHCPNACK (6) |
| 48 | +#define DHCPRELEASE (7) |
| 49 | +#define DHCPINFORM (8) |
| 50 | + |
| 51 | +#define DHCP_OPT_PAD (0) |
| 52 | +#define DHCP_OPT_SUBNET_MASK (1) |
| 53 | +#define DHCP_OPT_ROUTER (3) |
| 54 | +#define DHCP_OPT_DNS (6) |
| 55 | +#define DHCP_OPT_HOST_NAME (12) |
| 56 | +#define DHCP_OPT_REQUESTED_IP (50) |
| 57 | +#define DHCP_OPT_IP_LEASE_TIME (51) |
| 58 | +#define DHCP_OPT_MSG_TYPE (53) |
| 59 | +#define DHCP_OPT_SERVER_ID (54) |
| 60 | +#define DHCP_OPT_PARAM_REQUEST_LIST (55) |
| 61 | +#define DHCP_OPT_MAX_MSG_SIZE (57) |
| 62 | +#define DHCP_OPT_VENDOR_CLASS_ID (60) |
| 63 | +#define DHCP_OPT_CLIENT_ID (61) |
| 64 | +#define DHCP_OPT_END (255) |
| 65 | + |
| 66 | +#define PORT_DHCP_SERVER (67) |
| 67 | +#define PORT_DHCP_CLIENT (68) |
| 68 | + |
| 69 | +#define DEFAULT_DNS MAKE_IP4(8, 8, 8, 8) |
| 70 | +#define DEFAULT_LEASE_TIME_S (24 * 60 * 60) // in seconds |
| 71 | + |
| 72 | +#define MAC_LEN (6) |
| 73 | +#define MAKE_IP4(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) |
| 74 | + |
| 75 | +typedef struct { |
| 76 | + uint8_t op; // message opcode |
| 77 | + uint8_t htype; // hardware address type |
| 78 | + uint8_t hlen; // hardware address length |
| 79 | + uint8_t hops; |
| 80 | + uint32_t xid; // transaction id, chosen by client |
| 81 | + uint16_t secs; // client seconds elapsed |
| 82 | + uint16_t flags; |
| 83 | + uint8_t ciaddr[4]; // client IP address |
| 84 | + uint8_t yiaddr[4]; // your IP address |
| 85 | + uint8_t siaddr[4]; // next server IP address |
| 86 | + uint8_t giaddr[4]; // relay agent IP address |
| 87 | + uint8_t chaddr[16]; // client hardware address |
| 88 | + uint8_t sname[64]; // server host name |
| 89 | + uint8_t file[128]; // boot file name |
| 90 | + uint8_t options[312]; // optional parameters, variable, starts with magic |
| 91 | +} dhcp_msg_t; |
| 92 | + |
| 93 | +static int dhcp_socket_new_dgram(struct udp_pcb **udp, void *cb_data, udp_recv_fn cb_udp_recv) { |
| 94 | + // family is AF_INET |
| 95 | + // type is SOCK_DGRAM |
| 96 | + |
| 97 | + *udp = udp_new(); |
| 98 | + if (*udp == NULL) { |
| 99 | + return -ENOMEM; |
| 100 | + } |
| 101 | + |
| 102 | + // Register callback |
| 103 | + udp_recv(*udp, cb_udp_recv, (void *)cb_data); |
| 104 | + |
| 105 | + return 0; // success |
| 106 | +} |
| 107 | + |
| 108 | +static int dhcp_socket_bind(struct udp_pcb **udp, uint32_t ip, uint16_t port) { |
| 109 | + ip_addr_t addr; |
| 110 | + IP4_ADDR(&addr, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff); |
| 111 | + // TODO convert lwIP errors to errno |
| 112 | + return udp_bind(*udp, &addr, port); |
| 113 | +} |
| 114 | + |
| 115 | +static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len, uint32_t ip, uint16_t port) { |
| 116 | + if (len > 0xffff) { |
| 117 | + len = 0xffff; |
| 118 | + } |
| 119 | + |
| 120 | + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); |
| 121 | + if (p == NULL) { |
| 122 | + return -ENOMEM; |
| 123 | + } |
| 124 | + |
| 125 | + memcpy(p->payload, buf, len); |
| 126 | + |
| 127 | + ip_addr_t dest; |
| 128 | + IP4_ADDR(&dest, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff); |
| 129 | + err_t err = udp_sendto(*udp, p, &dest, port); |
| 130 | + |
| 131 | + pbuf_free(p); |
| 132 | + |
| 133 | + if (err != ERR_OK) { |
| 134 | + return err; |
| 135 | + } |
| 136 | + |
| 137 | + return len; |
| 138 | +} |
| 139 | + |
| 140 | +static uint8_t *opt_find(uint8_t *opt, uint8_t cmd) { |
| 141 | + for (int i = 0; i < 308 && opt[i] != DHCP_OPT_END;) { |
| 142 | + if (opt[i] == cmd) { |
| 143 | + return &opt[i]; |
| 144 | + } |
| 145 | + i += 2 + opt[i + 1]; |
| 146 | + } |
| 147 | + return NULL; |
| 148 | +} |
| 149 | + |
| 150 | +static void opt_write_n(uint8_t **opt, uint8_t cmd, size_t n, void *data) { |
| 151 | + uint8_t *o = *opt; |
| 152 | + *o++ = cmd; |
| 153 | + *o++ = n; |
| 154 | + memcpy(o, data, n); |
| 155 | + *opt = o + n; |
| 156 | +} |
| 157 | + |
| 158 | +static void opt_write_u8(uint8_t **opt, uint8_t cmd, uint8_t val) { |
| 159 | + uint8_t *o = *opt; |
| 160 | + *o++ = cmd; |
| 161 | + *o++ = 1; |
| 162 | + *o++ = val; |
| 163 | + *opt = o; |
| 164 | +} |
| 165 | + |
| 166 | +static void opt_write_u32(uint8_t **opt, uint8_t cmd, uint32_t val) { |
| 167 | + uint8_t *o = *opt; |
| 168 | + *o++ = cmd; |
| 169 | + *o++ = 4; |
| 170 | + *o++ = val >> 24; |
| 171 | + *o++ = val >> 16; |
| 172 | + *o++ = val >> 8; |
| 173 | + *o++ = val; |
| 174 | + *opt = o; |
| 175 | +} |
| 176 | + |
| 177 | +static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *src_addr, u16_t src_port) { |
| 178 | + dhcp_server_t *d = arg; |
| 179 | + (void)upcb; |
| 180 | + (void)src_addr; |
| 181 | + (void)src_port; |
| 182 | + extern void init_httpd(bool doIt); |
| 183 | + |
| 184 | + printf("dhcp_server_process\n"); |
| 185 | + |
| 186 | + // This is around 548 bytes |
| 187 | + dhcp_msg_t dhcp_msg; |
| 188 | + |
| 189 | + #define DHCP_MIN_SIZE (240 + 3) |
| 190 | + if (p->tot_len < DHCP_MIN_SIZE) { |
| 191 | + goto ignore_request; |
| 192 | + } |
| 193 | + |
| 194 | + size_t len = pbuf_copy_partial(p, &dhcp_msg, sizeof(dhcp_msg), 0); |
| 195 | + if (len < DHCP_MIN_SIZE) { |
| 196 | + goto ignore_request; |
| 197 | + } |
| 198 | + |
| 199 | + dhcp_msg.op = DHCPOFFER; |
| 200 | + memcpy(&dhcp_msg.yiaddr, &d->ip.addr, 4); |
| 201 | + |
| 202 | + uint8_t *opt = (uint8_t *)&dhcp_msg.options; |
| 203 | + opt += 4; // assume magic cookie: 99, 130, 83, 99 |
| 204 | + |
| 205 | + switch (opt[2]) { |
| 206 | + case DHCPDISCOVER: { |
| 207 | + int yi = DHCPS_MAX_IP; |
| 208 | + for (int i = 0; i < DHCPS_MAX_IP; ++i) { |
| 209 | + if (memcmp(d->lease[i].mac, dhcp_msg.chaddr, MAC_LEN) == 0) { |
| 210 | + // MAC match, use this IP address |
| 211 | + yi = i; |
| 212 | + break; |
| 213 | + } |
| 214 | + if (yi == DHCPS_MAX_IP) { |
| 215 | + // Look for a free IP address |
| 216 | + if (memcmp(d->lease[i].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) { |
| 217 | + // IP available |
| 218 | + yi = i; |
| 219 | + } |
| 220 | + uint32_t expiry = d->lease[i].expiry << 16 | 0xffff; |
| 221 | + if ((int32_t)(expiry - cyw43_hal_ticks_ms()) < 0) { |
| 222 | + // IP expired, reuse it |
| 223 | + memset(d->lease[i].mac, 0, MAC_LEN); |
| 224 | + yi = i; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + if (yi == DHCPS_MAX_IP) { |
| 229 | + // No more IP addresses left |
| 230 | + goto ignore_request; |
| 231 | + } |
| 232 | + dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi; |
| 233 | + opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPOFFER); |
| 234 | + break; |
| 235 | + } |
| 236 | + |
| 237 | + case DHCPREQUEST: { |
| 238 | + uint8_t *o = opt_find(opt, DHCP_OPT_REQUESTED_IP); |
| 239 | + if (o == NULL) { |
| 240 | + // Should be NACK |
| 241 | + goto ignore_request; |
| 242 | + } |
| 243 | + if (memcmp(o + 2, &d->ip.addr, 3) != 0) { |
| 244 | + // Should be NACK |
| 245 | + goto ignore_request; |
| 246 | + } |
| 247 | + uint8_t yi = o[5] - DHCPS_BASE_IP; |
| 248 | + if (yi >= DHCPS_MAX_IP) { |
| 249 | + // Should be NACK |
| 250 | + goto ignore_request; |
| 251 | + } |
| 252 | + if (memcmp(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN) == 0) { |
| 253 | + // MAC match, ok to use this IP address |
| 254 | + } else if (memcmp(d->lease[yi].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) { |
| 255 | + // IP unused, ok to use this IP address |
| 256 | + memcpy(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN); |
| 257 | + } else { |
| 258 | + // IP already in use |
| 259 | + // Should be NACK |
| 260 | + goto ignore_request; |
| 261 | + } |
| 262 | + d->lease[yi].expiry = (cyw43_hal_ticks_ms() + DEFAULT_LEASE_TIME_S * 1000) >> 16; |
| 263 | + dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi; |
| 264 | + opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPACK); |
| 265 | + printf("DHCPS: client connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x IP=%u.%u.%u.%u\n", |
| 266 | + dhcp_msg.chaddr[0], dhcp_msg.chaddr[1], dhcp_msg.chaddr[2], dhcp_msg.chaddr[3], dhcp_msg.chaddr[4], dhcp_msg.chaddr[5], |
| 267 | + dhcp_msg.yiaddr[0], dhcp_msg.yiaddr[1], dhcp_msg.yiaddr[2], dhcp_msg.yiaddr[3]); |
| 268 | + break; |
| 269 | + } |
| 270 | + |
| 271 | + default:{ |
| 272 | + goto ignore_request; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + opt_write_n(&opt, DHCP_OPT_SERVER_ID, 4, &d->ip.addr); |
| 277 | + opt_write_n(&opt, DHCP_OPT_SUBNET_MASK, 4, &d->nm.addr); |
| 278 | + opt_write_n(&opt, DHCP_OPT_ROUTER, 4, &d->ip.addr); // aka gateway; can have mulitple addresses |
| 279 | + opt_write_u32(&opt, DHCP_OPT_DNS, DEFAULT_DNS); // can have mulitple addresses |
| 280 | + opt_write_u32(&opt, DHCP_OPT_IP_LEASE_TIME, DEFAULT_LEASE_TIME_S); |
| 281 | + *opt++ = DHCP_OPT_END; |
| 282 | + dhcp_socket_sendto(&d->udp, &dhcp_msg, opt - (uint8_t *)&dhcp_msg, 0xffffffff, PORT_DHCP_CLIENT); |
| 283 | + |
| 284 | + init_httpd(true); |
| 285 | + |
| 286 | +ignore_request: |
| 287 | + pbuf_free(p); |
| 288 | +} |
| 289 | + |
| 290 | +void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm) { |
| 291 | + ip_addr_copy(d->ip, *ip); |
| 292 | + ip_addr_copy(d->nm, *nm); |
| 293 | + memset(d->lease, 0, sizeof(d->lease)); |
| 294 | + if (dhcp_socket_new_dgram(&d->udp, d, dhcp_server_process) != 0) { |
| 295 | + return; |
| 296 | + } |
| 297 | + dhcp_socket_bind(&d->udp, 0, PORT_DHCP_SERVER); |
| 298 | +} |
0 commit comments