VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/proxy_dhcp6ds.c@ 48306

Last change on this file since 48306 was 48001, checked in by vboxsync, 11 years ago

Move proxy sources from Devices/Network/lwip-new/vbox
to NetworkServices/NAT where they belong.

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1/* -*- indent-tabs-mode: nil; -*- */
2/**
3 * Simple stateless DHCPv6 (RFC 3736) server.
4 */
5#include "winutils.h"
6#include "dhcp6.h"
7#include "proxytest.h"
8
9#include <string.h>
10
11#include "lwip/opt.h"
12#include "lwip/mld6.h"
13#include "lwip/udp.h"
14
15
16static void dhcp6ds_recv(void *, struct udp_pcb *, struct pbuf *, ip6_addr_t *, u16_t);
17
18
19/* ff02::1:2 - "All_DHCP_Relay_Agents_and_Servers" link-scoped multicast */
20static /* const */ ip6_addr_t all_dhcp_relays_and_servers = {
21 { PP_HTONL(0xff020000UL), 0, 0, PP_HTONL(0x00010002UL) }
22};
23
24/* ff05::1:3 - "All_DHCP_Servers" site-scoped multicast */
25static /* const */ ip6_addr_t all_dhcp_servers = {
26 { PP_HTONL(0xff050000UL), 0, 0, PP_HTONL(0x00010003UL) }
27};
28
29
30static struct udp_pcb *dhcp6ds_pcb;
31
32/* prebuilt Server ID option */
33#define DUID_LL_LEN (/* duid type */ 2 + /* hw type */ 2 + /* ether addr */ 6)
34static u8_t dhcp6ds_serverid[/* opt */ 2 + /* optlen */ 2 + DUID_LL_LEN];
35
36/* prebuilt DNS Servers option */
37static u8_t dhcp6ds_dns[/* opt */ 2 + /* optlen */ 2 + /* IPv6 addr */ 16];
38
39
40/**
41 * Initialize DHCP6 server.
42 *
43 * Join DHCP6 multicast groups.
44 * Create and bind server pcb.
45 * Prebuild fixed parts of reply.
46 */
47err_t
48dhcp6ds_init(struct netif *proxy_netif)
49{
50 ip6_addr_t *pxaddr, *pxaddr_nonlocal;
51 int i;
52 err_t error;
53
54 LWIP_ASSERT1(proxy_netif != NULL);
55 LWIP_ASSERT1(proxy_netif->hwaddr_len == 6); /* ethernet */
56
57 pxaddr = netif_ip6_addr(proxy_netif, 0); /* link local */
58
59 /* advertise ourself as DNS resolver - will be proxied to host */
60 pxaddr_nonlocal = NULL;
61 for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
62 if (ip6_addr_ispreferred(netif_ip6_addr_state(proxy_netif, i))
63 && !ip6_addr_islinklocal(netif_ip6_addr(proxy_netif, i)))
64 {
65 pxaddr_nonlocal = netif_ip6_addr(proxy_netif, i);
66 break;
67 }
68 }
69 LWIP_ASSERT1(pxaddr_nonlocal != NULL); /* must be configured on the netif */
70
71
72 error = mld6_joingroup(pxaddr, &all_dhcp_relays_and_servers);
73 if (error != ERR_OK) {
74 DPRINTF0(("%s: failed to join All_DHCP_Relay_Agents_and_Servers: %s\n",
75 __func__, proxy_lwip_strerr(error)));
76 goto err;
77 }
78
79 error = mld6_joingroup(pxaddr, &all_dhcp_servers);
80 if (error != ERR_OK) {
81 DPRINTF0(("%s: failed to join All_DHCP_Servers: %s\n",
82 __func__, proxy_lwip_strerr(error)));
83 goto err1;
84 }
85
86
87 dhcp6ds_pcb = udp_new_ip6();
88 if (dhcp6ds_pcb == NULL) {
89 DPRINTF0(("%s: failed to allocate PCB\n", __func__));
90 error = ERR_MEM;
91 goto err2;
92 }
93
94 udp_recv_ip6(dhcp6ds_pcb, dhcp6ds_recv, NULL);
95
96 error = udp_bind_ip6(dhcp6ds_pcb, pxaddr, DHCP6_SERVER_PORT);
97 if (error != ERR_OK) {
98 DPRINTF0(("%s: failed to bind PCB\n", __func__));
99 goto err3;
100 }
101
102
103#define OPT_SET(buf, off, c) do { \
104 u16_t _s = PP_HTONS(c); \
105 memcpy(&(buf)[off], &_s, sizeof(u16_t)); \
106 } while (0)
107
108#define SERVERID_SET(off, c) OPT_SET(dhcp6ds_serverid, (off), (c))
109#define DNSSRV_SET(off, c) OPT_SET(dhcp6ds_dns, (off), (c))
110
111 SERVERID_SET(0, DHCP6_OPTION_SERVERID);
112 SERVERID_SET(2, DUID_LL_LEN);
113 SERVERID_SET(4, DHCP6_DUID_LL);
114 SERVERID_SET(6, ARES_HRD_ETHERNET);
115 memcpy(&dhcp6ds_serverid[8], proxy_netif->hwaddr, 6);
116
117 DNSSRV_SET(0, DHCP6_OPTION_DNS_SERVERS);
118 DNSSRV_SET(2, 16); /* one IPv6 address */
119 memcpy(&dhcp6ds_dns[4], pxaddr_nonlocal, sizeof(ip6_addr_t));
120
121#undef SERVERID_SET
122#undef DNSSRV_SET
123
124 return ERR_OK;
125
126
127 err3:
128 udp_remove(dhcp6ds_pcb);
129 dhcp6ds_pcb = NULL;
130 err2:
131 mld6_leavegroup(pxaddr, &all_dhcp_servers);
132 err1:
133 mld6_leavegroup(pxaddr, &all_dhcp_relays_and_servers);
134 err:
135 return error;
136}
137
138
139static u8_t dhcp6ds_reply_buf[1024];
140
141static void
142dhcp6ds_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
143 ip6_addr_t *addr, u16_t port)
144{
145 u8_t msg_header[4];
146 unsigned int msg_type, msg_tid;
147 int copied;
148 size_t roff;
149 struct pbuf *q;
150 err_t error;
151
152 LWIP_UNUSED_ARG(arg);
153 LWIP_ASSERT1(p != NULL);
154
155 copied = pbuf_copy_partial(p, msg_header, sizeof(msg_header), 0);
156 if (copied != sizeof(msg_header)) {
157 DPRINTF(("%s: message header truncated\n", __func__));
158 pbuf_free(p);
159 return;
160 }
161 pbuf_header(p, -(s16_t)sizeof(msg_header));
162
163 msg_type = msg_header[0];
164 msg_tid = (msg_header[1] << 16) | (msg_header[2] << 8) | msg_header[3];
165 DPRINTF(("%s: type %u, tid 0x%6x\n", __func__, msg_type, msg_tid));
166 if (msg_type != DHCP6_INFORMATION_REQUEST) { /* TODO:? RELAY_FORW */
167 pbuf_free(p);
168 return;
169 }
170
171 roff = 0;
172
173 msg_header[0] = DHCP6_REPLY;
174 memcpy(dhcp6ds_reply_buf + roff, msg_header, sizeof(msg_header));
175 roff += sizeof(msg_header);
176
177
178 /* loop over options */
179 while (p->tot_len > 0) {
180 u16_t opt, optlen;
181
182 /* fetch option code */
183 copied = pbuf_copy_partial(p, &opt, sizeof(opt), 0);
184 if (copied != sizeof(opt)) {
185 DPRINTF(("%s: option header truncated\n", __func__));
186 pbuf_free(p);
187 return;
188 }
189 pbuf_header(p, -(s16_t)sizeof(opt));
190 opt = ntohs(opt);
191
192 /* fetch option length */
193 copied = pbuf_copy_partial(p, &optlen, sizeof(optlen), 0);
194 if (copied != sizeof(optlen)) {
195 DPRINTF(("%s: option %u length truncated\n", __func__, opt));
196 pbuf_free(p);
197 return;
198 }
199 pbuf_header(p, -(s16_t)sizeof(optlen));
200 optlen = ntohs(optlen);
201
202 /* enough data? */
203 if (optlen > p->tot_len) {
204 DPRINTF(("%s: option %u truncated: expect %u, got %u\n",
205 __func__, opt, optlen, p->tot_len));
206 pbuf_free(p);
207 return;
208 }
209
210 DPRINTF2(("%s: option %u length %u\n", __func__, opt, optlen));
211
212 if (opt == DHCP6_OPTION_CLIENTID) {
213 u16_t s;
214
215 /* "A DUID can be no more than 128 octets long (not
216 including the type code)." */
217 if (optlen > 130) {
218 DPRINTF(("%s: client DUID too long: %u\n", __func__, optlen));
219 pbuf_free(p);
220 return;
221 }
222
223 s = PP_HTONS(DHCP6_OPTION_CLIENTID);
224 memcpy(dhcp6ds_reply_buf + roff, &s, sizeof(s));
225 roff += sizeof(s);
226
227 s = ntohs(optlen);
228 memcpy(dhcp6ds_reply_buf + roff, &s, sizeof(s));
229 roff += sizeof(s);
230
231 pbuf_copy_partial(p, dhcp6ds_reply_buf + roff, optlen, 0);
232 roff += optlen;
233 }
234 else if (opt == DHCP6_OPTION_ORO) {
235 u16_t *opts;
236 int i, nopts;
237
238 if (optlen % 2 != 0) {
239 DPRINTF2(("%s: Option Request of odd length\n", __func__));
240 goto bad_oro;
241 }
242 nopts = optlen / 2;
243
244 opts = (u16_t *)malloc(optlen);
245 if (opts == NULL) {
246 DPRINTF2(("%s: failed to allocate space for Option Request\n",
247 __func__));
248 goto bad_oro;
249 }
250
251 pbuf_copy_partial(p, opts, optlen, 0);
252 for (i = 0; i < nopts; ++i) {
253 opt = ntohs(opts[i]);
254 DPRINTF2(("> request option %u\n", opt));
255 };
256 free(opts);
257
258 bad_oro: /* empty */;
259 }
260
261 pbuf_header(p, -optlen); /* go to next option */
262 }
263 pbuf_free(p); /* done */
264
265
266 memcpy(dhcp6ds_reply_buf + roff, dhcp6ds_serverid, sizeof(dhcp6ds_serverid));
267 roff += sizeof(dhcp6ds_serverid);
268
269 memcpy(dhcp6ds_reply_buf + roff, dhcp6ds_dns, sizeof(dhcp6ds_dns));
270 roff += sizeof(dhcp6ds_dns);
271
272 q = pbuf_alloc(PBUF_RAW, roff, PBUF_RAM);
273 if (q == NULL) {
274 DPRINTF(("%s: pbuf_alloc(%d) failed\n", __func__, (int)roff));
275 return;
276 }
277
278 error = pbuf_take(q, dhcp6ds_reply_buf, roff);
279 if (error != ERR_OK) {
280 DPRINTF(("%s: pbuf_take(%d) failed: %s\n",
281 __func__, (int)roff, proxy_lwip_strerr(error)));
282 pbuf_free(q);
283 return;
284 }
285
286 error = udp_sendto_ip6(pcb, q, addr, port);
287 if (error != ERR_OK) {
288 DPRINTF(("%s: udp_sendto failed: %s\n",
289 __func__, proxy_lwip_strerr(error)));
290 }
291
292 pbuf_free(q);
293}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette