blob: bb9df69dd90ca453509ccf7dd235514f93f8a676 [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
Simon Kelleyc72daea2012-01-05 21:33:27 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17
18#include "dnsmasq.h"
19
20#ifdef HAVE_DHCP6
21
22static size_t outpacket_counter;
23
24static int make_duid1(unsigned short type, unsigned int flags, char *mac,
25 size_t maclen, void *parm);
26
27void make_duid(time_t now)
28{
29 iface_enumerate(AF_LOCAL, &now, make_duid1);
30
31 if (!daemon->duid)
32 die("Cannot create DHCPv6 server DUID", NULL, EC_MISC);
33}
34
35static int make_duid1(unsigned short type, unsigned int flags, char *mac,
36 size_t maclen, void *parm)
37{
38 /* create DUID as specified in RFC3315. We use the MAC of the
39 first interface we find that isn't loopback or P-to-P */
40
41 unsigned char *p;
42
43 if (flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
44 return 1;
45
46 daemon->duid = p = safe_malloc(maclen + 8);
47 daemon->duid_len = maclen + 8;
48 PUTSHORT(1, p); /* DUID_LLT */
49 PUTSHORT(type, p); /* address type */
50 PUTLONG(*((time_t *)parm), p); /* time */
51 memcpy(p, mac, maclen);
52
53 return 0;
54}
55
56void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize)
57{
58 u16 opt, opt_len;
59 void *start;
60
61 if (!opts)
62 return NULL;
63
64 while (1)
65 {
66 if (end - opts < 4)
67 return NULL;
68
69 start = opts;
70 GETSHORT(opt, opts);
71 GETSHORT(opt_len, opts);
72
73 if (opt_len > (end - opts))
74 return NULL;
75
76 if (opt == search && (opt_len >= minsize))
77 return start;
78
79 opts += opt_len;
80 }
81}
82
83void *opt6_next(void *opts, void *end)
84{
85 u16 opt_len;
86
87 if (end - opts < 4)
88 return NULL;
89
90 opts += 2;
91 GETSHORT(opt_len, opts);
92
93 if (opt_len >= (end - opts))
94 return NULL;
95
96 return opts + opt_len;
97}
98
99
100#define opt6_len(opt) ((int)(((unsigned short *)(opt))[1]))
101#define opt6_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[4u+(unsigned int)(i)]))
102
103
104static unsigned int opt6_uint(unsigned char *opt, int offset, int size)
105{
106 /* this worries about unaligned data and byte order */
107 unsigned int ret = 0;
108 int i;
109 unsigned char *p = opt6_ptr(opt, offset);
110
111 for (i = 0; i < size; i++)
112 ret = (ret << 8) | *p++;
113
114 return ret;
115}
116
117/*
118 daemon->outpacket_counter = 4; message type and ID
119
120 elapsed time:
121 int o = new_opt(OPTION_ELAPSED_TIME);
122 put_opt_short(o, 100)
123 finalise_opt(o);
124
125 IA_NA
126
127 int o = new_opt(OPTION_IA_NA);
128 put_opt_long(o, IAID);
129 put_opt_long(o, T1);
130 put_opt_long(0, T2);
131 int o1 = new_opt(OPTION_IAADDR);
132 put_opt(o1, &addr, sizeof(addr));
133 put_opt_long(o1, preferred_lifetime);
134 put_opt_long(o1, valid_lifetime);
135 finalise_opt(o1);
136 finalise_opt(o);
137
138
139*/
140
141
142
143
144
145void end_opt6(int container)
146{
147 void *p = daemon->outpacket.iov_base + container + 2;
148 u16 len = outpacket_counter - container - 4 ;
149
150 PUTSHORT(len, p);
151}
152
153static void *expand(size_t headroom)
154{
155 void *ret;
156
157 if (expand_buf(&daemon->outpacket, outpacket_counter + headroom))
158 {
159 ret = daemon->outpacket.iov_base + outpacket_counter;
160 outpacket_counter += headroom;
161 return ret;
162 }
163
164 return NULL;
165}
166
167int new_opt6(int opt)
168{
169 int ret = outpacket_counter;
170 void *p;
171
172 if ((p = expand(4)))
173 {
174 PUTSHORT(opt, p);
175 PUTSHORT(0, p);
176 }
177
178 return ret;
179}
180
181
182
183
184void *put_opt6(void *data, size_t len)
185{
186 void *p;
187
188 if (data && (p = expand(len)))
189 memcpy(p, data, len);
190
191 return p;
192}
193
194void put_opt6_long(unsigned int val)
195{
196 void *p;
197
198 if (( p = expand(4)))
199 PUTLONG(p, val);
200}
201
202void put_opt6_short(unsigned int val)
203{
204 void *p;
205
206 if ((p = expand(2)))
207 PUTSHORT(val, p);
208}
209
210void put_opt6_byte(unsigned int val)
211{
212 void *p;
213
214 if ((p = expand(1)))
215 *((unsigned char *)p) = val;
216}
217
218size_t dhcp6_reply(struct dhcp_context *context, size_t sz)
219{
220 void *packet_options = ((void *)daemon->dhcp_packet.iov_base) + 4;
221 void *end = ((void *)daemon->dhcp_packet.iov_base) + sz;
222 void *na_option, *na_end;
223 void *opt, *p;
224 int o;
225
226 outpacket_counter = 4; /* skip message type and transaction-id */
227
228 if (!(opt = opt6_find(packet_options, end, OPTION6_CLIENT_ID, 1)))
229 return;
230
231 o = new_opt6(OPTION6_CLIENT_ID);
232 put_opt6(opt6_ptr(opt, 0), opt6_len(opt));
233 end_opt6(o);
234
235 o = new_opt6(OPTION6_SERVER_ID);
236 put_opt6(daemon->duid, daemon->duid_len);
237 end_opt6(o);
238
239 if ((opt = opt6_find(packet_options, end, OPTION6_IA_NA, 12)))
240 {
241 while (opt = opt6_find(opt, end, OPTION6_IA_NA, 12))
242 {
243 void *ia_end = opt6_ptr(opt, opt6_len(opt));
244 void *ia_option = opt6_find(opt6_ptr(opt, 12), ia_end, OPTION6_IAADDR, 24);
245
246 unsigned int iaid = opt6_uint(opt, 0, 4);
247 unsigned int t1 = opt6_uint(opt, 4, 4);
248 unsigned int t2 = opt6_uint(opt, 8, 4);
249
250
251 if (ia_option)
252 while ((ia_option = ia_option, ia_end, OPTION6_IAADDR, 24))
253 {
254 /* do address option */
255
256 ia_option = opt6_next(ia_option, ia_end);
257 }
258 else
259 {
260 /* no preferred address call address allocate */
261
262 }
263
264 opt = opt6_next(opt, end);
265 }
266 }
267 else if ((opt = opt6_find(packet_options, end, OPTION6_IA_TA, 4)))
268 while (opt = opt6_find(opt, end, OPTION6_IA_TA, 4))
269 {
270 void *ia_end = opt6_ptr(opt, opt6_len(opt));
271 void *ia_option = opt6_find(opt6_ptr(opt, 4), ia_end, OPTION6_IAADDR, 24);
272
273 unsigned int iaid = opt6_uint(opt, 0, 4);
274
275 if (ia_option)
276 while ((ia_option = ia_option, ia_end, OPTION6_IAADDR, 24))
277 {
278 /* do address option */
279
280 ia_option = opt6_next(ia_option, ia_end);
281 }
282 else
283 {
284 /* no preferred address */
285
286 }
287
288 opt = opt6_next(opt, end);
289 }
290 else
291 return; /* no IA_NA and no IA_TA */
292
293
294
295}
296
297#endif
298