slirp: remove now useless QEMU headers inclusions
[qemu/ar7.git] / slirp / dhcpv6.c
blobe27d9a46f845d2713aee1fccc5d103bd972dbfd6
1 /*
2 * SLIRP stateless DHCPv6
4 * We only support stateless DHCPv6, e.g. for network booting.
5 * See RFC 3315, RFC 3736, RFC 3646 and RFC 5970 for details.
7 * Copyright 2016 Thomas Huth, Red Hat Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "qemu/osdep.h"
24 #include "slirp.h"
25 #include "dhcpv6.h"
27 /* DHCPv6 message types */
28 #define MSGTYPE_REPLY 7
29 #define MSGTYPE_INFO_REQUEST 11
31 /* DHCPv6 option types */
32 #define OPTION_CLIENTID 1
33 #define OPTION_IAADDR 5
34 #define OPTION_ORO 6
35 #define OPTION_DNS_SERVERS 23
36 #define OPTION_BOOTFILE_URL 59
38 struct requested_infos {
39 uint8_t *client_id;
40 int client_id_len;
41 bool want_dns;
42 bool want_boot_url;
45 /**
46 * Analyze the info request message sent by the client to see what data it
47 * provided and what it wants to have. The information is gathered in the
48 * "requested_infos" struct. Note that client_id (if provided) points into
49 * the odata region, thus the caller must keep odata valid as long as it
50 * needs to access the requested_infos struct.
52 static int dhcpv6_parse_info_request(Slirp *slirp, uint8_t *odata, int olen,
53 struct requested_infos *ri)
55 int i, req_opt;
57 while (olen > 4) {
58 /* Parse one option */
59 int option = odata[0] << 8 | odata[1];
60 int len = odata[2] << 8 | odata[3];
62 if (len + 4 > olen) {
63 slirp->cb->guest_error("Guest sent bad DHCPv6 packet!");
64 return -E2BIG;
67 switch (option) {
68 case OPTION_IAADDR:
69 /* According to RFC3315, we must discard requests with IA option */
70 return -EINVAL;
71 case OPTION_CLIENTID:
72 if (len > 256) {
73 /* Avoid very long IDs which could cause problems later */
74 return -E2BIG;
76 ri->client_id = odata + 4;
77 ri->client_id_len = len;
78 break;
79 case OPTION_ORO: /* Option request option */
80 if (len & 1) {
81 return -EINVAL;
83 /* Check which options the client wants to have */
84 for (i = 0; i < len; i += 2) {
85 req_opt = odata[4 + i] << 8 | odata[4 + i + 1];
86 switch (req_opt) {
87 case OPTION_DNS_SERVERS:
88 ri->want_dns = true;
89 break;
90 case OPTION_BOOTFILE_URL:
91 ri->want_boot_url = true;
92 break;
93 default:
94 DEBUG_MISC("dhcpv6: Unsupported option request %d",
95 req_opt);
98 break;
99 default:
100 DEBUG_MISC("dhcpv6 info req: Unsupported option %d, len=%d",
101 option, len);
104 odata += len + 4;
105 olen -= len + 4;
108 return 0;
113 * Handle information request messages
115 static void dhcpv6_info_request(Slirp *slirp, struct sockaddr_in6 *srcsas,
116 uint32_t xid, uint8_t *odata, int olen)
118 struct requested_infos ri = { NULL };
119 struct sockaddr_in6 sa6, da6;
120 struct mbuf *m;
121 uint8_t *resp;
123 if (dhcpv6_parse_info_request(slirp, odata, olen, &ri) < 0) {
124 return;
127 m = m_get(slirp);
128 if (!m) {
129 return;
131 memset(m->m_data, 0, m->m_size);
132 m->m_data += IF_MAXLINKHDR;
133 resp = (uint8_t *)m->m_data + sizeof(struct ip6) + sizeof(struct udphdr);
135 /* Fill in response */
136 *resp++ = MSGTYPE_REPLY;
137 *resp++ = (uint8_t)(xid >> 16);
138 *resp++ = (uint8_t)(xid >> 8);
139 *resp++ = (uint8_t)xid;
141 if (ri.client_id) {
142 *resp++ = OPTION_CLIENTID >> 8; /* option-code high byte */
143 *resp++ = OPTION_CLIENTID; /* option-code low byte */
144 *resp++ = ri.client_id_len >> 8; /* option-len high byte */
145 *resp++ = ri.client_id_len; /* option-len low byte */
146 memcpy(resp, ri.client_id, ri.client_id_len);
147 resp += ri.client_id_len;
149 if (ri.want_dns) {
150 *resp++ = OPTION_DNS_SERVERS >> 8; /* option-code high byte */
151 *resp++ = OPTION_DNS_SERVERS; /* option-code low byte */
152 *resp++ = 0; /* option-len high byte */
153 *resp++ = 16; /* option-len low byte */
154 memcpy(resp, &slirp->vnameserver_addr6, 16);
155 resp += 16;
157 if (ri.want_boot_url) {
158 uint8_t *sa = slirp->vhost_addr6.s6_addr;
159 int slen, smaxlen;
161 *resp++ = OPTION_BOOTFILE_URL >> 8; /* option-code high byte */
162 *resp++ = OPTION_BOOTFILE_URL; /* option-code low byte */
163 smaxlen = (uint8_t *)m->m_data + IF_MTU - (resp + 2);
164 slen = snprintf((char *)resp + 2, smaxlen,
165 "tftp://[%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
166 "%02x%02x:%02x%02x:%02x%02x:%02x%02x]/%s",
167 sa[0], sa[1], sa[2], sa[3], sa[4], sa[5], sa[6], sa[7],
168 sa[8], sa[9], sa[10], sa[11], sa[12], sa[13], sa[14],
169 sa[15], slirp->bootp_filename);
170 slen = MIN(slen, smaxlen);
171 *resp++ = slen >> 8; /* option-len high byte */
172 *resp++ = slen; /* option-len low byte */
173 resp += slen;
176 sa6.sin6_addr = slirp->vhost_addr6;
177 sa6.sin6_port = DHCPV6_SERVER_PORT;
178 da6.sin6_addr = srcsas->sin6_addr;
179 da6.sin6_port = srcsas->sin6_port;
180 m->m_data += sizeof(struct ip6) + sizeof(struct udphdr);
181 m->m_len = resp - (uint8_t *)m->m_data;
182 udp6_output(NULL, m, &sa6, &da6);
186 * Handle DHCPv6 messages sent by the client
188 void dhcpv6_input(struct sockaddr_in6 *srcsas, struct mbuf *m)
190 uint8_t *data = (uint8_t *)m->m_data + sizeof(struct udphdr);
191 int data_len = m->m_len - sizeof(struct udphdr);
192 uint32_t xid;
194 if (data_len < 4) {
195 return;
198 xid = ntohl(*(uint32_t *)data) & 0xffffff;
200 switch (data[0]) {
201 case MSGTYPE_INFO_REQUEST:
202 dhcpv6_info_request(m->slirp, srcsas, xid, &data[4], data_len - 4);
203 break;
204 default:
205 DEBUG_MISC("dhcpv6_input: Unsupported message type 0x%x", data[0]);