Skip to content

Commit 1043f1a

Browse files
committed
lib/netutils: Add DHCP server component.
1 parent 4173950 commit 1043f1a

2 files changed

Lines changed: 353 additions & 0 deletions

File tree

lib/netutils/dhcpserver.c

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
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 "py/mperrno.h"
34+
#include "py/mphal.h"
35+
36+
#if MICROPY_PY_LWIP
37+
38+
#include "lib/netutils/dhcpserver.h"
39+
#include "lwip/udp.h"
40+
41+
#define DHCPDISCOVER (1)
42+
#define DHCPOFFER (2)
43+
#define DHCPREQUEST (3)
44+
#define DHCPDECLINE (4)
45+
#define DHCPACK (5)
46+
#define DHCPNACK (6)
47+
#define DHCPRELEASE (7)
48+
#define DHCPINFORM (8)
49+
50+
#define DHCP_OPT_PAD (0)
51+
#define DHCP_OPT_SUBNET_MASK (1)
52+
#define DHCP_OPT_ROUTER (3)
53+
#define DHCP_OPT_DNS (6)
54+
#define DHCP_OPT_HOST_NAME (12)
55+
#define DHCP_OPT_REQUESTED_IP (50)
56+
#define DHCP_OPT_IP_LEASE_TIME (51)
57+
#define DHCP_OPT_MSG_TYPE (53)
58+
#define DHCP_OPT_SERVER_ID (54)
59+
#define DHCP_OPT_PARAM_REQUEST_LIST (55)
60+
#define DHCP_OPT_MAX_MSG_SIZE (57)
61+
#define DHCP_OPT_VENDOR_CLASS_ID (60)
62+
#define DHCP_OPT_CLIENT_ID (61)
63+
#define DHCP_OPT_END (255)
64+
65+
#define PORT_DHCP_SERVER (67)
66+
#define PORT_DHCP_CLIENT (68)
67+
68+
#define DEFAULT_DNS MAKE_IP4(8, 8, 8, 8)
69+
#define DEFAULT_LEASE_TIME_S (24 * 60 * 60) // in seconds
70+
71+
#define MAC_LEN (6)
72+
#define MAKE_IP4(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
73+
74+
typedef struct {
75+
uint8_t op; // message opcode
76+
uint8_t htype; // hardware address type
77+
uint8_t hlen; // hardware address length
78+
uint8_t hops;
79+
uint32_t xid; // transaction id, chosen by client
80+
uint16_t secs; // client seconds elapsed
81+
uint16_t flags;
82+
uint8_t ciaddr[4]; // client IP address
83+
uint8_t yiaddr[4]; // your IP address
84+
uint8_t siaddr[4]; // next server IP address
85+
uint8_t giaddr[4]; // relay agent IP address
86+
uint8_t chaddr[16]; // client hardware address
87+
uint8_t sname[64]; // server host name
88+
uint8_t file[128]; // boot file name
89+
uint8_t options[312]; // optional parameters, variable, starts with magic
90+
} dhcp_msg_t;
91+
92+
static int dhcp_socket_new_dgram(struct udp_pcb **udp, void *cb_data, udp_recv_fn cb_udp_recv) {
93+
// family is AF_INET
94+
// type is SOCK_DGRAM
95+
96+
*udp = udp_new();
97+
if (*udp == NULL) {
98+
return -MP_ENOMEM;
99+
}
100+
101+
// Register callback
102+
udp_recv(*udp, cb_udp_recv, (void*)cb_data);
103+
104+
return 0; // success
105+
}
106+
107+
static void dhcp_socket_free(struct udp_pcb **udp) {
108+
if (*udp != NULL) {
109+
udp_remove(*udp);
110+
*udp = NULL;
111+
}
112+
}
113+
114+
static int dhcp_socket_bind(struct udp_pcb **udp, uint32_t ip, uint16_t port) {
115+
ip_addr_t addr;
116+
IP4_ADDR(&addr, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
117+
// TODO convert lwIP errors to errno
118+
return udp_bind(*udp, &addr, port);
119+
}
120+
121+
static int dhcp_socket_sendto(struct udp_pcb **udp, const void *buf, size_t len, uint32_t ip, uint16_t port) {
122+
if (len > 0xffff) {
123+
len = 0xffff;
124+
}
125+
126+
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
127+
if (p == NULL) {
128+
return -MP_ENOMEM;
129+
}
130+
131+
memcpy(p->payload, buf, len);
132+
133+
ip_addr_t dest;
134+
IP4_ADDR(&dest, ip >> 24 & 0xff, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
135+
err_t err = udp_sendto(*udp, p, &dest, port);
136+
137+
pbuf_free(p);
138+
139+
if (err != ERR_OK) {
140+
return err;
141+
}
142+
143+
return len;
144+
}
145+
146+
static uint8_t *opt_find(uint8_t *opt, uint8_t cmd) {
147+
for (int i = 0; i < 308 && opt[i] != DHCP_OPT_END;) {
148+
if (opt[i] == cmd) {
149+
return &opt[i];
150+
}
151+
i += 2 + opt[i + 1];
152+
}
153+
return NULL;
154+
}
155+
156+
static void opt_write_n(uint8_t **opt, uint8_t cmd, size_t n, void *data) {
157+
uint8_t *o = *opt;
158+
*o++ = cmd;
159+
*o++ = n;
160+
memcpy(o, data, n);
161+
*opt = o + n;
162+
}
163+
164+
static void opt_write_u8(uint8_t **opt, uint8_t cmd, uint8_t val) {
165+
uint8_t *o = *opt;
166+
*o++ = cmd;
167+
*o++ = 1;
168+
*o++ = val;
169+
*opt = o;
170+
}
171+
172+
static void opt_write_u32(uint8_t **opt, uint8_t cmd, uint32_t val) {
173+
uint8_t *o = *opt;
174+
*o++ = cmd;
175+
*o++ = 4;
176+
*o++ = val >> 24;
177+
*o++ = val >> 16;
178+
*o++ = val >> 8;
179+
*o++ = val;
180+
*opt = o;
181+
}
182+
183+
static void dhcp_server_process(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *src_addr, u16_t src_port) {
184+
dhcp_server_t *d = arg;
185+
(void)upcb;
186+
(void)src_addr;
187+
(void)src_port;
188+
189+
// This is around 548 bytes
190+
dhcp_msg_t dhcp_msg;
191+
192+
#define DHCP_MIN_SIZE (240 + 3)
193+
if (p->tot_len < DHCP_MIN_SIZE) {
194+
goto ignore_request;
195+
}
196+
197+
size_t len = pbuf_copy_partial(p, &dhcp_msg, sizeof(dhcp_msg), 0);
198+
if (len < DHCP_MIN_SIZE) {
199+
goto ignore_request;
200+
}
201+
202+
dhcp_msg.op = DHCPOFFER;
203+
memcpy(&dhcp_msg.yiaddr, &d->ip.addr, 4);
204+
205+
uint8_t *opt = (uint8_t*)&dhcp_msg.options;
206+
opt += 4; // assume magic cookie: 99, 130, 83, 99
207+
208+
switch (opt[2]) {
209+
case DHCPDISCOVER: {
210+
int yi = DHCPS_MAX_IP;
211+
for (int i = 0; i < DHCPS_MAX_IP; ++i) {
212+
if (memcmp(d->lease[i].mac, dhcp_msg.chaddr, MAC_LEN) == 0) {
213+
// MAC match, use this IP address
214+
yi = i;
215+
break;
216+
}
217+
if (yi == DHCPS_MAX_IP) {
218+
// Look for a free IP address
219+
if (memcmp(d->lease[i].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) {
220+
// IP available
221+
yi = i;
222+
}
223+
uint32_t expiry = d->lease[i].expiry << 16 | 0xffff;
224+
if ((int32_t)(expiry - mp_hal_ticks_ms()) < 0) {
225+
// IP expired, reuse it
226+
memset(d->lease[i].mac, 0, MAC_LEN);
227+
yi = i;
228+
}
229+
}
230+
}
231+
if (yi == DHCPS_MAX_IP) {
232+
// No more IP addresses left
233+
goto ignore_request;
234+
}
235+
dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi;
236+
opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPOFFER);
237+
break;
238+
}
239+
240+
case DHCPREQUEST: {
241+
uint8_t *o = opt_find(opt, DHCP_OPT_REQUESTED_IP);
242+
if (o == NULL) {
243+
// Should be NACK
244+
goto ignore_request;
245+
}
246+
if (memcmp(o + 2, &d->ip.addr, 3) != 0) {
247+
// Should be NACK
248+
goto ignore_request;
249+
}
250+
uint8_t yi = o[5] - DHCPS_BASE_IP;
251+
if (yi >= DHCPS_MAX_IP) {
252+
// Should be NACK
253+
goto ignore_request;
254+
}
255+
if (memcmp(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN) == 0) {
256+
// MAC match, ok to use this IP address
257+
} else if (memcmp(d->lease[yi].mac, "\x00\x00\x00\x00\x00\x00", MAC_LEN) == 0) {
258+
// IP unused, ok to use this IP address
259+
memcpy(d->lease[yi].mac, dhcp_msg.chaddr, MAC_LEN);
260+
} else {
261+
// IP already in use
262+
// Should be NACK
263+
goto ignore_request;
264+
}
265+
d->lease[yi].expiry = (mp_hal_ticks_ms() + DEFAULT_LEASE_TIME_S * 1000) >> 16;
266+
dhcp_msg.yiaddr[3] = DHCPS_BASE_IP + yi;
267+
opt_write_u8(&opt, DHCP_OPT_MSG_TYPE, DHCPACK);
268+
printf("DHCPS: client connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x IP=%u.%u.%u.%u\n",
269+
dhcp_msg.chaddr[0], dhcp_msg.chaddr[1], dhcp_msg.chaddr[2], dhcp_msg.chaddr[3], dhcp_msg.chaddr[4], dhcp_msg.chaddr[5],
270+
dhcp_msg.yiaddr[0], dhcp_msg.yiaddr[1], dhcp_msg.yiaddr[2], dhcp_msg.yiaddr[3]);
271+
break;
272+
}
273+
274+
default:
275+
goto ignore_request;
276+
}
277+
278+
opt_write_n(&opt, DHCP_OPT_SERVER_ID, 4, &d->ip.addr);
279+
opt_write_n(&opt, DHCP_OPT_SUBNET_MASK, 4, &d->nm.addr);
280+
opt_write_n(&opt, DHCP_OPT_ROUTER, 4, &d->ip.addr); // aka gateway; can have mulitple addresses
281+
opt_write_u32(&opt, DHCP_OPT_DNS, DEFAULT_DNS); // can have mulitple addresses
282+
opt_write_u32(&opt, DHCP_OPT_IP_LEASE_TIME, DEFAULT_LEASE_TIME_S);
283+
*opt++ = DHCP_OPT_END;
284+
dhcp_socket_sendto(&d->udp, &dhcp_msg, opt - (uint8_t*)&dhcp_msg, 0xffffffff, PORT_DHCP_CLIENT);
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+
}
299+
300+
void dhcp_server_deinit(dhcp_server_t *d) {
301+
dhcp_socket_free(&d->udp);
302+
}
303+
304+
#endif // MICROPY_PY_LWIP

lib/netutils/dhcpserver.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#ifndef MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H
27+
#define MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H
28+
29+
#include "lwip/ip_addr.h"
30+
31+
#define DHCPS_BASE_IP (16)
32+
#define DHCPS_MAX_IP (8)
33+
34+
typedef struct _dhcp_server_lease_t {
35+
uint8_t mac[6];
36+
uint16_t expiry;
37+
} dhcp_server_lease_t;
38+
39+
typedef struct _dhcp_server_t {
40+
ip_addr_t ip;
41+
ip_addr_t nm;
42+
dhcp_server_lease_t lease[DHCPS_MAX_IP];
43+
struct udp_pcb *udp;
44+
} dhcp_server_t;
45+
46+
void dhcp_server_init(dhcp_server_t *d, ip_addr_t *ip, ip_addr_t *nm);
47+
void dhcp_server_deinit(dhcp_server_t *d);
48+
49+
#endif // MICROPY_INCLUDED_LIB_NETUTILS_DHCPSERVER_H

0 commit comments

Comments
 (0)