1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ip.h>
#include <ndb.h>
#include "ipconfig.h"
enum {
SOLICIT = 1,
ADVERTISE,
REQUEST,
CONFIRM,
RENEW,
REBIND,
REPLY,
RELEASE,
DECLINE,
RECONFIGURE,
INFOREQ,
RELAYFORW,
RELAYREPL,
};
static uchar v6dhcpservers[IPaddrlen] = {
0xff, 0x02, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 1, 0, 2,
};
static uchar sid[256];
static int sidlen;
static int
openlisten(void)
{
int n, fd, cfd;
char data[128], devdir[40];
sprint(data, "%s/udp!%I!546", conf.mpoint, conf.lladdr);
for (n = 0; (cfd = announce(data, devdir)) < 0; n++) {
if(!noconfig)
sysfatal("can't announce for dhcp: %r");
/* might be another client - wait and try again */
warning("can't announce %s: %r", data);
sleep(jitter());
if(n > 10)
return -1;
}
if(fprint(cfd, "headers") < 0)
sysfatal("can't set header mode: %r");
fprint(cfd, "ignoreadvice");
sprint(data, "%s/data", devdir);
fd = open(data, ORDWR);
if(fd < 0)
sysfatal("open %s: %r", data);
close(cfd);
return fd;
}
static int
transaction(int fd, int type, int timeout)
{
union {
Udphdr;
uchar buf[4096];
} ipkt, opkt;
uchar *p, *e, *x;
int tra, opt, len, sleepfor;
tra = lrand() & 0xFFFFFF;
ipmove(opkt.laddr, conf.lladdr);
ipmove(opkt.raddr, v6dhcpservers);
ipmove(opkt.ifcaddr, conf.lladdr);
hnputs(opkt.lport, 546);
hnputs(opkt.rport, 547);
p = opkt.buf + Udphdrsize;
*p++ = type;
*p++ = tra >> 16;
*p++ = tra >> 8;
*p++ = tra >> 0;
/* client identifier */
*p++ = 0x00; *p++ = 0x01;
/* len */
*p++ = conf.duidlen >> 8;
*p++ = conf.duidlen;
memmove(p, conf.duid, conf.duidlen);
p += conf.duidlen;
/* IA for non-temporary address */
len = 12;
if(validip(conf.laddr))
len += 4 + IPaddrlen + 2*4;
*p++ = 0x00; *p++ = 0x03;
*p++ = len >> 8;
*p++ = len;
/* IAID */
*p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p++ = 0x01;
/* T1, T2 */
*p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00;
*p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00;
if(len > 12){
*p++ = 0x00; *p++ = 0x05;
*p++ = 0x00; *p++ = IPaddrlen + 2*4;
memmove(p, conf.laddr, IPaddrlen);
p += IPaddrlen;
memset(p, 0xFF, 2*4);
p += 2*4;
}
/* Option Request */
*p++ = 0x00; *p++ = 0x06;
*p++ = 0x00; *p++ = 0x02;
*p++ = 0x00; *p++ = 0x17; /* DNS servers */
if(sidlen > 0){
*p++ = 0x00; *p++ = 0x02;
/* len */
*p++ = sidlen >> 8;
*p++ = sidlen;;
memmove(p, sid, sidlen);
p += sidlen;
}
len = -1;
for(sleepfor = 500; timeout > 0; sleepfor <<= 1){
DEBUG("sending dhcpv6 request %x", opkt.buf[Udphdrsize]);
alarm(sleepfor);
if(len < 0)
write(fd, opkt.buf, p - opkt.buf);
len = read(fd, ipkt.buf, sizeof(ipkt.buf));
timeout += alarm(0);
timeout -= sleepfor;
if(len == 0)
break;
if(len < Udphdrsize+4)
continue;
if(ipkt.buf[Udphdrsize+1] != ((tra>>16)&0xFF)
|| ipkt.buf[Udphdrsize+2] != ((tra>>8)&0xFF)
|| ipkt.buf[Udphdrsize+3] != ((tra>>0)&0xFF))
continue;
DEBUG("got dhcpv6 reply %x from %I on %I", ipkt.buf[Udphdrsize+0], ipkt.raddr, ipkt.ifcaddr);
type |= (int)ipkt.buf[Udphdrsize+0]<<8;
switch(type){
case ADVERTISE << 8 | SOLICIT:
case REPLY << 8 | REQUEST:
goto Response;
default:
return -1;
}
}
return -1;
Response:
for(p = ipkt.buf + Udphdrsize + 4, e = ipkt.buf + len; p < e; p = x) {
if (p+4 > e)
return -1;
opt = (int)p[0] << 8 | p[1];
len = (int)p[2] << 8 | p[3];
p += 4;
x = p+len;
if (x > e)
return -1;
DEBUG("got dhcpv6 option %x: [%d] %.*H", opt, len, len, p);
switch(opt){
case 0x01: /* client identifier */
continue;
case 0x02: /* server identifier */
if(len < 1 || len > sizeof(sid))
break;
sidlen = len;
memmove(sid, p, sidlen);
continue;
case 0x03: /* IA for non-temporary address */
if(p+12+4+IPaddrlen+2*4 > x)
break;
/* skip IAID, T1, T2 */
p += 12;
/* IA Addresss */
if(p[0] != 0x00 || p[1] != 0x05
|| p[2] != 0x00 || p[3] != IPaddrlen+2*4)
break;
p += 4;
memset(conf.mask, 0xFF, IPaddrlen);
memmove(conf.laddr, p, IPaddrlen);
continue;
case 0x17: /* dns servers */
if(len % IPaddrlen)
break;
addaddrs(conf.dns, sizeof(conf.dns), p, len);
continue;
default:
DEBUG("unknown dhcpv6 option %x", opt);
continue;
}
warning("dhcpv6: malformed option %x: [%d] %.*H", opt, len, len, x-len);
}
return 0;
}
void
dhcpv6query(void)
{
int fd;
fd = openlisten();
if(transaction(fd, SOLICIT, 5000) < 0)
goto out;
if(!validip(conf.laddr))
goto out;
if(transaction(fd, REQUEST, 10000) < 0)
goto out;
out:
close(fd);
}
|