e2d4fca094a9d6fb5b0943f4a26e54facebd6a0c
[project/odhcp6c.git] / src / odhcp6c.c
1 /**
2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017-2018 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <time.h>
17 #include <errno.h>
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <resolv.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <syslog.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <stdbool.h>
31
32 #include <net/if.h>
33 #include <sys/syscall.h>
34 #include <arpa/inet.h>
35 #include <linux/if_addr.h>
36
37 #include "odhcp6c.h"
38 #include "ra.h"
39
40 #ifndef IN6_IS_ADDR_UNIQUELOCAL
41 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
42 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
43 == htonl (0xfc000000))
44 #endif
45 #define ARRAY_SEP " ,\t"
46
47 static void sighandler(int signal);
48 static int usage(void);
49 static int add_opt(const uint16_t code, const uint8_t *data,
50 const uint16_t len);
51 static int parse_opt_data(const char *data, uint8_t **dst,
52 const unsigned int type, const bool array);
53 static int parse_opt(const char *opt);
54
55 static uint8_t *state_data[_STATE_MAX] = {NULL};
56 static size_t state_len[_STATE_MAX] = {0};
57
58 static volatile bool signal_io = false;
59 static volatile bool signal_usr1 = false;
60 static volatile bool signal_usr2 = false;
61 static volatile bool signal_term = false;
62
63 static int urandom_fd = -1, allow_slaac_only = 0;
64 static bool bound = false, release = true, ra = false;
65 static time_t last_update = 0;
66 static char *ifname = NULL;
67
68 static unsigned int script_sync_delay = 10;
69 static unsigned int script_accu_delay = 1;
70
71 static struct odhcp6c_opt opts[] = {
72 { .code = DHCPV6_OPT_CLIENTID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
73 { .code = DHCPV6_OPT_SERVERID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
74 { .code = DHCPV6_OPT_IA_NA, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str= NULL },
75 { .code = DHCPV6_OPT_IA_TA, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
76 { .code = DHCPV6_OPT_IA_ADDR, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
77 { .code = DHCPV6_OPT_ORO, .flags = OPT_INTERNAL, .str = NULL },
78 { .code = DHCPV6_OPT_PREF, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
79 { .code = DHCPV6_OPT_ELAPSED, .flags = OPT_INTERNAL, .str = NULL },
80 { .code = DHCPV6_OPT_RELAY_MSG, .flags = OPT_INTERNAL, .str = NULL },
81 { .code = DHCPV6_OPT_AUTH, .flags = OPT_U8 | OPT_NO_PASSTHRU, .str = "authentication" },
82 { .code = DHCPV6_OPT_UNICAST, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
83 { .code = DHCPV6_OPT_STATUS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
84 { .code = DHCPV6_OPT_RAPID_COMMIT, .flags = OPT_INTERNAL, .str = NULL },
85 { .code = DHCPV6_OPT_USER_CLASS, .flags = OPT_USER_CLASS | OPT_ARRAY, .str = "userclass" },
86 { .code = DHCPV6_OPT_VENDOR_CLASS, .flags = OPT_U8, .str = "vendorclass" },
87 { .code = DHCPV6_OPT_INTERFACE_ID, .flags = OPT_INTERNAL, .str = NULL },
88 { .code = DHCPV6_OPT_RECONF_MESSAGE, .flags = OPT_INTERNAL, .str = NULL },
89 { .code = DHCPV6_OPT_RECONF_ACCEPT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
90 { .code = DHCPV6_OPT_SIP_SERVER_D, .flags = OPT_DNS_STR | OPT_ORO, .str = "sipserver_d" },
91 { .code = DHCPV6_OPT_SIP_SERVER_A, .flags = OPT_IP6 | OPT_ARRAY | OPT_ORO, .str = "sipserver_a" },
92 { .code = DHCPV6_OPT_DNS_SERVERS, .flags = OPT_IP6 | OPT_ARRAY | OPT_ORO, .str = "dns" },
93 { .code = DHCPV6_OPT_DNS_DOMAIN, .flags = OPT_DNS_STR | OPT_ORO, .str = "search" },
94 { .code = DHCPV6_OPT_IA_PD, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
95 { .code = DHCPV6_OPT_IA_PREFIX, .flags = OPT_INTERNAL, .str = NULL },
96 { .code = DHCPV6_OPT_SNTP_SERVERS, .flags = OPT_IP6 | OPT_ARRAY | OPT_ORO, .str = "sntpservers" },
97 { .code = DHCPV6_OPT_INFO_REFRESH, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO | OPT_ORO_STATELESS, .str = NULL },
98 { .code = DHCPV6_OPT_REMOTE_ID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
99 { .code = DHCPV6_OPT_SUBSCRIBER_ID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
100 { .code = DHCPV6_OPT_FQDN, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO, .str = NULL },
101 { .code = DHCPV6_OPT_ERO, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
102 { .code = DHCPV6_OPT_LQ_QUERY, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
103 { .code = DHCPV6_OPT_CLIENT_DATA, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
104 { .code = DHCPV6_OPT_CLT_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
105 { .code = DHCPV6_OPT_LQ_RELAY_DATA, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
106 { .code = DHCPV6_OPT_LQ_CLIENT_LINK, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
107 { .code = DHCPV6_OPT_RELAY_ID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
108 { .code = DHCPV6_OPT_NTP_SERVER, .flags = OPT_U8 | OPT_ORO, .str = "ntpserver" },
109 { .code = DHCPV6_OPT_CLIENT_ARCH_TYPE, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
110 { .code = DHCPV6_OPT_AFTR_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO, .str = NULL },
111 { .code = DHCPV6_OPT_RSOO, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
112 { .code = DHCPV6_OPT_PD_EXCLUDE, .flags = OPT_INTERNAL | OPT_ORO | OPT_ORO_STATEFUL, .str = NULL },
113 { .code = DHCPV6_OPT_VSS, .flags = OPT_U8, .str = "vss" },
114 { .code = DHCPV6_OPT_LINK_LAYER_ADDRESS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
115 { .code = DHCPV6_OPT_LINK_ADDRESS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
116 { .code = DHCPV6_OPT_RADIUS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
117 { .code = DHCPV6_OPT_SOL_MAX_RT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO | OPT_ORO_SOLICIT, .str = NULL },
118 { .code = DHCPV6_OPT_INF_MAX_RT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO | OPT_ORO_STATELESS, .str = NULL },
119 #ifdef EXT_CER_ID
120 { .code = DHCPV6_OPT_CER_ID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
121 #endif
122 { .code = DHCPV6_OPT_DHCPV4_MSG, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
123 { .code = DHCPV6_OPT_S46_RULE, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
124 { .code = DHCPV6_OPT_S46_BR, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
125 { .code = DHCPV6_OPT_S46_DMR, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
126 { .code = DHCPV6_OPT_S46_V4V6BIND, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
127 { .code = DHCPV6_OPT_S46_PORTPARAMS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
128 { .code = DHCPV6_OPT_S46_CONT_MAPE, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO, .str = NULL },
129 { .code = DHCPV6_OPT_S46_CONT_MAPT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO, .str = NULL },
130 { .code = DHCPV6_OPT_S46_CONT_LW, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU | OPT_ORO, .str = NULL },
131 { .code = DHCPV6_OPT_LQ_BASE_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
132 { .code = DHCPV6_OPT_LQ_START_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
133 { .code = DHCPV6_OPT_LQ_END_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
134 { .code = DHCPV6_OPT_ANI_ATT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
135 { .code = DHCPV6_OPT_ANI_NETWORK_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
136 { .code = DHCPV6_OPT_ANI_AP_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
137 { .code = DHCPV6_OPT_ANI_AP_BSSID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
138 { .code = DHCPV6_OPT_ANI_OPERATOR_ID, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
139 { .code = DHCPV6_OPT_ANI_OPERATOR_REALM, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
140 { .code = DHCPV6_OPT_MUD_URL_V6, .flags = OPT_STR | OPT_NO_PASSTHRU, .str = "mud_url_v6" },
141 { .code = DHCPV6_OPT_F_BINDING_STATUS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
142 { .code = DHCPV6_OPT_F_CONNECT_FLAGS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
143 { .code = DHCPV6_OPT_F_DNS_REMOVAL_INFO, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
144 { .code = DHCPV6_OPT_F_DNS_HOST_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
145 { .code = DHCPV6_OPT_F_DNS_ZONE_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
146 { .code = DHCPV6_OPT_F_DNS_FLAGS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
147 { .code = DHCPV6_OPT_F_EXPIRATION_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
148 { .code = DHCPV6_OPT_F_MAX_UNACKED_BNDUPD, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
149 { .code = DHCPV6_OPT_F_MCLT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
150 { .code = DHCPV6_OPT_F_PARTNER_LIFETIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
151 { .code = DHCPV6_OPT_F_PARTNER_LIFETIME_SENT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
152 { .code = DHCPV6_OPT_F_PARTNER_DOWN_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
153 { .code = DHCPV6_OPT_F_PARTNER_RAW_CLT_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
154 { .code = DHCPV6_OPT_F_PROTOCOL_VERSION, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
155 { .code = DHCPV6_OPT_F_KEEPALIVE_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
156 { .code = DHCPV6_OPT_F_RECONFIGURE_DATA, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
157 { .code = DHCPV6_OPT_F_RELATIONSHIP_NAME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
158 { .code = DHCPV6_OPT_F_SERVER_FLAGS, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
159 { .code = DHCPV6_OPT_F_SERVER_STATE, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
160 { .code = DHCPV6_OPT_F_START_TIME_OF_STATE, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
161 { .code = DHCPV6_OPT_F_STATE_EXPIRATION_TIME, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
162 { .code = DHCPV6_OPT_RELAY_PORT, .flags = OPT_INTERNAL | OPT_NO_PASSTHRU, .str = NULL },
163 { .code = 0, .flags = 0, .str = NULL },
164 };
165
166 int main(_unused int argc, char* const argv[])
167 {
168 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
169 // Allocate resources
170 const char *pidfile = NULL;
171 const char *script = "/usr/sbin/odhcp6c-update";
172 ssize_t l;
173 uint8_t buf[134], *o_data;
174 char *optpos;
175 uint16_t opttype;
176 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
177 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE;
178 struct odhcp6c_opt *opt;
179 int ia_pd_iaid_index = 0;
180 int sol_timeout = DHCPV6_SOL_MAX_RT;
181 unsigned int ia_pd_safe_valid = 5;
182 int verbosity = 0;
183 bool help = false, daemonize = false;
184 int logopt = LOG_PID;
185 int c, res;
186 unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
187 unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME;
188 unsigned int ra_holdoff_interval = RA_MIN_ADV_INTERVAL;
189
190 while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:Ux:s:kt:m:D:Lhedp:fav")) != -1) {
191 switch (c) {
192 case 'S':
193 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
194 break;
195
196 case 'N':
197 if (!strcmp(optarg, "force")) {
198 ia_na_mode = IA_MODE_FORCE;
199 allow_slaac_only = -1;
200 } else if (!strcmp(optarg, "none"))
201 ia_na_mode = IA_MODE_NONE;
202 else if (!strcmp(optarg, "try"))
203 ia_na_mode = IA_MODE_TRY;
204 else
205 help = true;
206 break;
207
208 case 'V':
209 opt = odhcp6c_find_opt(DHCPV6_OPT_VENDOR_CLASS);
210 if (!opt) {
211 syslog(LOG_ERR, "Failed to set vendor-class option");
212 return 1;
213 }
214
215 o_data = NULL;
216 res = parse_opt_data(optarg, &o_data, opt->flags & OPT_MASK_SIZE,
217 (opt->flags & OPT_ARRAY) == OPT_ARRAY);
218 if (res > 0) {
219 res = add_opt(opt->code, o_data, res);
220 if (res) {
221 if (res > 0)
222 return 1;
223
224 help = true;
225 }
226 } else
227 help = true;
228
229 free(o_data);
230 break;
231
232 case 'P':
233 if (ia_pd_mode == IA_MODE_NONE)
234 ia_pd_mode = IA_MODE_TRY;
235
236 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
237 allow_slaac_only = 10;
238
239 char *iaid_begin;
240 int iaid_len = 0;
241 int prefix_length = strtoul(optarg, &iaid_begin, 10);
242
243 if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') {
244 syslog(LOG_ERR, "invalid argument: '%s'", optarg);
245 return 1;
246 }
247
248 struct odhcp6c_request_prefix prefix = { 0, prefix_length };
249
250 if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1)
251 memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len);
252 else if (*iaid_begin == ':')
253 prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16));
254 else
255 prefix.iaid = htonl(++ia_pd_iaid_index);
256
257 if (odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix))) {
258 syslog(LOG_ERR, "Failed to set request IPv6-Prefix");
259 return 1;
260 }
261 break;
262
263 case 'F':
264 allow_slaac_only = -1;
265 ia_pd_mode = IA_MODE_FORCE;
266 break;
267
268 case 'c':
269 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
270 if (l > 0) {
271 buf[0] = 0;
272 buf[1] = DHCPV6_OPT_CLIENTID;
273 buf[2] = 0;
274 buf[3] = l;
275 if (odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4)) {
276 syslog(LOG_ERR, "Failed to override client-ID");
277 return 1;
278 }
279 } else
280 help = true;
281 break;
282
283 case 'i':
284 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
285 help = true;
286 break;
287
288 case 'r':
289 optpos = optarg;
290 while (optpos[0]) {
291 opttype = htons(strtoul(optarg, &optpos, 10));
292 if (optpos == optarg)
293 break;
294 else if (optpos[0])
295 optarg = &optpos[1];
296
297 if (odhcp6c_add_state(STATE_ORO, &opttype, 2)) {
298 syslog(LOG_ERR, "Failed to add requested option");
299 return 1;
300 }
301 }
302 break;
303
304 case 'R':
305 client_options |= DHCPV6_STRICT_OPTIONS;
306 break;
307
308 case 'u':
309 opt = odhcp6c_find_opt(DHCPV6_OPT_USER_CLASS);
310 if (!opt) {
311 syslog(LOG_ERR, "Failed to set user-class option");
312 return 1;
313 }
314
315 o_data = NULL;
316 res = parse_opt_data(optarg, &o_data, opt->flags & OPT_MASK_SIZE,
317 (opt->flags & OPT_ARRAY) == OPT_ARRAY);
318 if (res > 0) {
319 res = add_opt(opt->code, o_data, res);
320 if (res) {
321 if (res > 0)
322 return 1;
323
324 help = true;
325 }
326 } else
327 help = true;
328
329 free(o_data);
330 break;
331
332 case 'U':
333 client_options |= DHCPV6_IGNORE_OPT_UNICAST;
334 break;
335
336 case 's':
337 script = optarg;
338 break;
339
340 case 'k':
341 release = false;
342 break;
343
344 case 't':
345 sol_timeout = atoi(optarg);
346 break;
347
348 case 'm':
349 ra_holdoff_interval = atoi(optarg);
350 break;
351
352 case 'D':
353 ia_pd_safe_valid = atoi(optarg);
354 if (ia_pd_safe_valid > 60)
355 ia_pd_safe_valid = 60;
356 break;
357
358 case 'L':
359 ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME;
360 break;
361
362 case 'e':
363 logopt |= LOG_PERROR;
364 break;
365
366 case 'd':
367 daemonize = true;
368 break;
369
370 case 'p':
371 pidfile = optarg;
372 break;
373
374 case 'f':
375 client_options &= ~DHCPV6_CLIENT_FQDN;
376 break;
377
378 case 'a':
379 client_options &= ~DHCPV6_ACCEPT_RECONFIGURE;
380 break;
381
382 case 'v':
383 ++verbosity;
384 break;
385
386 case 'x':
387 res = parse_opt(optarg);
388 if (res) {
389 if (res > 0)
390 return res;
391
392 help = true;
393 }
394 break;
395
396 default:
397 help = true;
398 break;
399 }
400 }
401
402 if (allow_slaac_only > 0)
403 script_sync_delay = allow_slaac_only;
404
405 openlog("odhcp6c", logopt, LOG_DAEMON);
406 if (!verbosity)
407 setlogmask(LOG_UPTO(LOG_WARNING));
408
409 ifname = argv[optind];
410
411 if (help || !ifname)
412 return usage();
413
414 signal(SIGIO, sighandler);
415 signal(SIGHUP, sighandler);
416 signal(SIGINT, sighandler);
417 signal(SIGTERM, sighandler);
418 signal(SIGUSR1, sighandler);
419 signal(SIGUSR2, sighandler);
420
421 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
422 init_dhcpv6(ifname, client_options, sol_timeout, ia_pd_safe_valid) ||
423 ra_init(ifname, &ifid, ra_options, ra_holdoff_interval) ||
424 script_init(script, ifname)) {
425 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
426 return 3;
427 }
428
429 if (daemonize) {
430 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
431 if (daemon(0, 0)) {
432 syslog(LOG_ERR, "Failed to daemonize: %s",
433 strerror(errno));
434 return 4;
435 }
436
437 if (!pidfile) {
438 snprintf((char*)buf, sizeof(buf), "/var/run/odhcp6c.%s.pid", ifname);
439 pidfile = (char*)buf;
440 }
441
442 FILE *fp = fopen(pidfile, "w");
443 if (fp) {
444 fprintf(fp, "%i\n", getpid());
445 fclose(fp);
446 }
447 }
448
449 script_call("started", 0, false);
450
451 while (!signal_term) { // Main logic
452 odhcp6c_clear_state(STATE_SERVER_ID);
453 odhcp6c_clear_state(STATE_SERVER_ADDR);
454 odhcp6c_clear_state(STATE_IA_NA);
455 odhcp6c_clear_state(STATE_IA_PD);
456 odhcp6c_clear_state(STATE_SNTP_IP);
457 odhcp6c_clear_state(STATE_NTP_IP);
458 odhcp6c_clear_state(STATE_NTP_FQDN);
459 odhcp6c_clear_state(STATE_SIP_IP);
460 odhcp6c_clear_state(STATE_SIP_FQDN);
461 bound = false;
462
463 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
464
465 signal_usr1 = signal_usr2 = false;
466 int mode = dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
467 if (mode != DHCPV6_STATELESS)
468 mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
469
470 odhcp6c_signal_process();
471
472 if (mode < 0)
473 continue;
474
475 do {
476 res = dhcpv6_request(mode == DHCPV6_STATELESS ?
477 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
478 bool signalled = odhcp6c_signal_process();
479
480 if (res > 0)
481 break;
482 else if (signalled) {
483 mode = -1;
484 break;
485 }
486
487 mode = dhcpv6_promote_server_cand();
488 } while (mode > DHCPV6_UNKNOWN);
489
490 if (mode < 0)
491 continue;
492
493 switch (mode) {
494 case DHCPV6_STATELESS:
495 bound = true;
496 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
497
498 while (!signal_usr2 && !signal_term) {
499 signal_usr1 = false;
500 script_call("informed", script_sync_delay, true);
501
502 res = dhcpv6_poll_reconfigure();
503 odhcp6c_signal_process();
504
505 if (res > 0)
506 continue;
507
508 if (signal_usr1) {
509 signal_usr1 = false; // Acknowledged
510 continue;
511 }
512
513 if (signal_usr2 || signal_term)
514 break;
515
516 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
517 odhcp6c_signal_process();
518
519 if (signal_usr1)
520 continue;
521 else if (res < 0)
522 break;
523 }
524 break;
525
526 case DHCPV6_STATEFUL:
527 bound = true;
528 script_call("bound", script_sync_delay, true);
529 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
530
531 while (!signal_usr2 && !signal_term) {
532 // Renew Cycle
533 // Wait for T1 to expire or until we get a reconfigure
534 res = dhcpv6_poll_reconfigure();
535 odhcp6c_signal_process();
536 if (res > 0) {
537 script_call("updated", 0, false);
538 continue;
539 }
540
541 // Handle signal, if necessary
542 if (signal_usr1)
543 signal_usr1 = false; // Acknowledged
544
545 if (signal_usr2 || signal_term)
546 break; // Other signal type
547
548 // Send renew as T1 expired
549 res = dhcpv6_request(DHCPV6_MSG_RENEW);
550 odhcp6c_signal_process();
551
552 if (res > 0) { // Renew was succesfull
553 // Publish updates
554 script_call("updated", 0, false);
555 continue; // Renew was successful
556 }
557
558 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
559 odhcp6c_clear_state(STATE_SERVER_ADDR);
560
561 size_t ia_pd_len, ia_na_len;
562 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
563 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
564
565 if (ia_pd_len == 0 && ia_na_len == 0)
566 break;
567
568 // If we have IAs, try rebind otherwise restart
569 res = dhcpv6_request(DHCPV6_MSG_REBIND);
570 odhcp6c_signal_process();
571
572 if (res > 0)
573 script_call("rebound", 0, true);
574 else
575 break;
576 }
577 break;
578
579 default:
580 break;
581 }
582
583 odhcp6c_expire(false);
584
585 size_t ia_pd_len, ia_na_len, server_id_len;
586 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
587 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
588 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
589
590 // Add all prefixes to lost prefixes
591 bound = false;
592 script_call("unbound", 0, true);
593
594 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
595 dhcpv6_request(DHCPV6_MSG_RELEASE);
596
597 odhcp6c_clear_state(STATE_IA_NA);
598 odhcp6c_clear_state(STATE_IA_PD);
599 }
600
601 script_call("stopped", 0, true);
602
603 return 0;
604 }
605
606 static int usage(void)
607 {
608 const char buf[] =
609 "Usage: odhcp6c [options] <interface>\n"
610 "\nFeature options:\n"
611 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
612 " -N <mode> Mode for requesting addresses [try|force|none]\n"
613 " -P <length> Request IPv6-Prefix (0 = auto)\n"
614 " -F Force IPv6-Prefix\n"
615 " -V <class> Set vendor-class option (base-16 encoded)\n"
616 " -u <user-class> Set user-class option string\n"
617 " -x <opt>:<val> Add option opt (with value val) in sent packets (cumulative)\n"
618 " Examples of IPv6 address, string and base-16 encoded options:\n"
619 " -x dns:2001:2001::1,2001:2001::2 - option 23\n"
620 " -x 15:office - option 15 (userclass)\n"
621 " -x 0x1f4:ABBA - option 500\n"
622 " -x 202:'\"file\"' - option 202\n"
623 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
624 " -i <iface-id> Use a custom interface identifier for RA handling\n"
625 " -r <options> Options to be requested (comma-separated)\n"
626 " -R Do not request any options except those specified with -r\n"
627 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
628 " -a Don't send Accept Reconfigure option\n"
629 " -f Don't send Client FQDN option\n"
630 " -k Don't send a RELEASE when stopping\n"
631 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
632 " -m <seconds> Minimum time between accepting RA updates (3)\n"
633 " -D <seconds> Minimum valid lifetime for IA_PD updates (5)\n"
634 " -L Ignore default lifetime for RDNSS records\n"
635 " -U Ignore Server Unicast option\n"
636 "\nInvocation options:\n"
637 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
638 " -d Daemonize\n"
639 " -e Write logmessages to stderr\n"
640 " -v Increase logging verbosity\n"
641 " -h Show this help\n\n";
642 fputs(buf, stderr);
643
644 return 1;
645 }
646
647 // Don't want to pull-in librt and libpthread just for a monotonic clock...
648 uint64_t odhcp6c_get_milli_time(void)
649 {
650 struct timespec t;
651
652 clock_gettime(CLOCK_MONOTONIC, &t);
653
654 return ((uint64_t)t.tv_sec) * 1000 + ((uint64_t)t.tv_nsec) / 1000000;
655 }
656
657 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
658 {
659 if (len == 0)
660 return state_data[state] + state_len[state];
661 else if (state_len[state] + len > 1024)
662 return NULL;
663
664 uint8_t *n = realloc(state_data[state], state_len[state] + len);
665
666 if (n || state_len[state] + len == 0) {
667 state_data[state] = n;
668 n += state_len[state];
669 state_len[state] += len;
670 }
671
672 return n;
673 }
674
675 bool odhcp6c_signal_process(void)
676 {
677 while (signal_io) {
678 signal_io = false;
679
680 bool ra_updated = ra_process();
681
682 if (ra_link_up()) {
683 signal_usr2 = true;
684 ra = false;
685 }
686
687 if (ra_updated && (bound || allow_slaac_only >= 0)) {
688 script_call("ra-updated", (!ra && !bound) ?
689 script_sync_delay : script_accu_delay, false);
690 ra = true;
691 }
692 }
693
694 return signal_usr1 || signal_usr2 || signal_term;
695 }
696
697 void odhcp6c_clear_state(enum odhcp6c_state state)
698 {
699 state_len[state] = 0;
700 }
701
702 int odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
703 {
704 uint8_t *n = odhcp6c_resize_state(state, len);
705
706 if (!n)
707 return -1;
708
709 memcpy(n, data, len);
710
711 return 0;
712 }
713
714 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
715 {
716 ssize_t len_after = state_len[state] - offset;
717 if (len_after < 0)
718 return -1;
719
720 uint8_t *n = odhcp6c_resize_state(state, len);
721
722 if (n) {
723 uint8_t *sdata = state_data[state];
724
725 memmove(sdata + offset + len, sdata + offset, len_after);
726 memcpy(sdata + offset, data, len);
727 }
728
729 return 0;
730 }
731
732 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
733 {
734 uint8_t *data = state_data[state];
735 ssize_t len_after = state_len[state] - (offset + len);
736
737 if (len_after < 0)
738 return state_len[state];
739
740 memmove(data + offset, data + offset + len, len_after);
741
742 return state_len[state] -= len;
743 }
744
745 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
746 {
747 *len = state_len[state];
748 void *data = state_data[state];
749
750 state_len[state] = 0;
751 state_data[state] = NULL;
752
753 return data;
754 }
755
756 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
757 {
758 *len = state_len[state];
759
760 return state_data[state];
761 }
762
763 static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
764 {
765 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + ((new->length + 7) / 8);
766 uint8_t *start = odhcp6c_get_state(state, &len);
767
768 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
769 (uint8_t*)c < &start[len] &&
770 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
771 c = odhcp6c_next_entry(c)) {
772 if (!memcmp(c, new, cmplen) && !memcmp(c->auxtarget, new->auxtarget, new->auxlen))
773 return c;
774 }
775
776 return NULL;
777 }
778
779 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
780 uint32_t safe, unsigned int holdoff_interval)
781 {
782 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
783
784 if (x && x->valid > new->valid && new->valid < safe)
785 new->valid = safe;
786
787 if (x) {
788 if (holdoff_interval && new->valid >= x->valid &&
789 new->valid != UINT32_MAX &&
790 new->valid - x->valid < holdoff_interval &&
791 new->preferred >= x->preferred &&
792 new->preferred != UINT32_MAX &&
793 new->preferred - x->preferred < holdoff_interval)
794 return false;
795
796 x->valid = new->valid;
797 x->preferred = new->preferred;
798 x->t1 = new->t1;
799 x->t2 = new->t2;
800 x->iaid = new->iaid;
801 } else if (odhcp6c_add_state(state, new, odhcp6c_entry_size(new)))
802 return false;
803
804 return true;
805 }
806
807 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed, bool remove_expired)
808 {
809 size_t len;
810 uint8_t *start = odhcp6c_get_state(state, &len);
811
812 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
813 (uint8_t*)c < &start[len] &&
814 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
815 ) {
816 if (c->t1 < elapsed)
817 c->t1 = 0;
818 else if (c->t1 != UINT32_MAX)
819 c->t1 -= elapsed;
820
821 if (c->t2 < elapsed)
822 c->t2 = 0;
823 else if (c->t2 != UINT32_MAX)
824 c->t2 -= elapsed;
825
826 if (c->preferred < elapsed)
827 c->preferred = 0;
828 else if (c->preferred != UINT32_MAX)
829 c->preferred -= elapsed;
830
831 if (c->valid < elapsed)
832 c->valid = 0;
833 else if (c->valid != UINT32_MAX)
834 c->valid -= elapsed;
835
836 if (!c->valid && remove_expired) {
837 odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
838 start = odhcp6c_get_state(state, &len);
839 } else
840 c = odhcp6c_next_entry(c);
841 }
842 }
843
844 static uint8_t *odhcp6c_state_find_opt(const uint16_t code)
845 {
846 size_t opts_len;
847 uint8_t *odata, *opts = odhcp6c_get_state(STATE_OPTS, &opts_len);
848 uint16_t otype, olen;
849
850 dhcpv6_for_each_option(opts, &opts[opts_len], otype, olen, odata) {
851 if (otype == code)
852 return &odata[-4];
853 }
854
855 return NULL;
856 }
857
858 void odhcp6c_expire(bool expire_ia_pd)
859 {
860 time_t now = odhcp6c_get_milli_time() / 1000;
861 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
862
863 last_update = now;
864
865 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed, true);
866 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed, true);
867 odhcp6c_expire_list(STATE_RA_DNS, elapsed, true);
868 odhcp6c_expire_list(STATE_RA_SEARCH, elapsed, true);
869 odhcp6c_expire_list(STATE_IA_NA, elapsed, true);
870 odhcp6c_expire_list(STATE_IA_PD, elapsed, expire_ia_pd);
871 }
872
873 uint32_t odhcp6c_elapsed(void)
874 {
875 return odhcp6c_get_milli_time() / 1000 - last_update;
876 }
877
878 int odhcp6c_random(void *buf, size_t len)
879 {
880 return read(urandom_fd, buf, len);
881 }
882
883 bool odhcp6c_is_bound(void)
884 {
885 return bound;
886 }
887
888 bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
889 {
890 FILE *fd = fopen("/proc/net/if_inet6", "r");
891 int len;
892 bool ret = false;
893 char buf[256];
894
895 if (fd == NULL)
896 return false;
897
898 while (fgets(buf, sizeof(buf), fd)) {
899 struct in6_addr inet6_addr;
900 uint32_t flags, dummy;
901 unsigned int i;
902 char name[IF_NAMESIZE], addr_buf[33];
903
904 len = strlen(buf);
905
906 if ((len <= 0) || buf[len - 1] != '\n')
907 break;
908
909 buf[--len] = '\0';
910
911 if (sscanf(buf, "%s %x %x %x %x %s",
912 addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
913 break;
914
915 if (strcmp(name, ifname) ||
916 (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
917 continue;
918
919 for (i = 0; i < strlen(addr_buf); i++) {
920 if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
921 break;
922 }
923
924 memset(&inet6_addr, 0, sizeof(inet6_addr));
925 for (i = 0; i < (strlen(addr_buf) / 2); i++) {
926 unsigned char byte;
927 static const char hex[] = "0123456789abcdef";
928 byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
929 (index(hex, addr_buf[i * 2 + 1]) - hex);
930 inet6_addr.s6_addr[i] = byte;
931 }
932
933 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
934 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr))) {
935 ret = true;
936 break;
937 }
938 }
939
940 fclose(fd);
941 return ret;
942 }
943
944 static void sighandler(int signal)
945 {
946 if (signal == SIGUSR1)
947 signal_usr1 = true;
948 else if (signal == SIGUSR2)
949 signal_usr2 = true;
950 else if (signal == SIGIO)
951 signal_io = true;
952 else
953 signal_term = true;
954 }
955
956 static int add_opt(const uint16_t code, const uint8_t *data, const uint16_t len)
957 {
958 struct {
959 uint16_t code;
960 uint16_t len;
961 } opt_hdr = { htons(code), htons(len) };
962
963 if (odhcp6c_state_find_opt(code))
964 return -1;
965
966 if (odhcp6c_add_state(STATE_OPTS, &opt_hdr, sizeof(opt_hdr)) ||
967 odhcp6c_add_state(STATE_OPTS, data, len)) {
968 syslog(LOG_ERR, "Failed to add option %hu", code);
969 return 1;
970 }
971
972 return 0;
973 }
974
975 struct odhcp6c_opt *odhcp6c_find_opt(const uint16_t code)
976 {
977 struct odhcp6c_opt *opt = opts;
978
979 while (opt->code) {
980 if (opt->code == code)
981 return opt;
982
983 opt++;
984 }
985
986 return NULL;
987 }
988
989 static struct odhcp6c_opt *odhcp6c_find_opt_by_name(const char *name)
990 {
991 struct odhcp6c_opt *opt = opts;
992
993 if (!name || !strlen(name))
994 return NULL;
995
996 while (opt->code && (!opt->str || strcmp(opt->str, name)))
997 opt++;
998
999 return (opt->code > 0 ? opt : NULL);
1000 }
1001
1002 static int parse_opt_u8(const char *src, uint8_t **dst)
1003 {
1004 int len = strlen(src);
1005
1006 *dst = realloc(*dst, len/2);
1007 if (!*dst)
1008 return -1;
1009
1010 return script_unhexlify(*dst, len, src);
1011 }
1012
1013 static int parse_opt_string(const char *src, uint8_t **dst, const bool array)
1014 {
1015 int o_len = 0;
1016 char *sep = strpbrk(src, ARRAY_SEP);
1017
1018 if (sep && !array)
1019 return -1;
1020
1021 do {
1022 if (sep) {
1023 *sep = 0;
1024 sep++;
1025 }
1026
1027 int len = strlen(src);
1028
1029 *dst = realloc(*dst, o_len + len);
1030 if (!*dst)
1031 return -1;
1032
1033 memcpy(&((*dst)[o_len]), src, len);
1034
1035 o_len += len;
1036 src = sep;
1037
1038 if (sep)
1039 sep = strpbrk(src, ARRAY_SEP);
1040 } while (src);
1041
1042 return o_len;
1043 }
1044
1045 static int parse_opt_dns_string(const char *src, uint8_t **dst, const bool array)
1046 {
1047 int o_len = 0;
1048 char *sep = strpbrk(src, ARRAY_SEP);
1049
1050 if (sep && !array)
1051 return -1;
1052
1053 do {
1054 uint8_t tmp[256];
1055
1056 if (sep) {
1057 *sep = 0;
1058 sep++;
1059 }
1060
1061 int len = dn_comp(src, tmp, sizeof(tmp), NULL, NULL);
1062 if (len < 0)
1063 return -1;
1064
1065 *dst = realloc(*dst, o_len + len);
1066 if (!*dst)
1067 return -1;
1068
1069 memcpy(&((*dst)[o_len]), tmp, len);
1070
1071 o_len += len;
1072 src = sep;
1073
1074 if (sep)
1075 sep = strpbrk(src, ARRAY_SEP);
1076 } while (src);
1077
1078 return o_len;
1079 }
1080
1081 static int parse_opt_ip6(const char *src, uint8_t **dst, const bool array)
1082 {
1083 int o_len = 0;
1084 char *sep = strpbrk(src, ARRAY_SEP);
1085
1086 if (sep && !array)
1087 return -1;
1088
1089 do {
1090 int len = sizeof(struct in6_addr);
1091
1092 if (sep) {
1093 *sep = 0;
1094 sep++;
1095 }
1096
1097 *dst = realloc(*dst, o_len + len);
1098 if (!*dst)
1099 return -1;
1100
1101 if (inet_pton(AF_INET6, src, &((*dst)[o_len])) < 1)
1102 return -1;
1103
1104 o_len += len;
1105 src = sep;
1106
1107 if (sep)
1108 sep = strpbrk(src, ARRAY_SEP);
1109 } while (src);
1110
1111 return o_len;
1112 }
1113
1114 static int parse_opt_user_class(const char *src, uint8_t **dst, const bool array)
1115 {
1116 int o_len = 0;
1117 char *sep = strpbrk(src, ARRAY_SEP);
1118
1119 if (sep && !array)
1120 return -1;
1121
1122 do {
1123 if (sep) {
1124 *sep = 0;
1125 sep++;
1126 }
1127 uint16_t str_len = strlen(src);
1128
1129 *dst = realloc(*dst, o_len + str_len + 2);
1130 if (!*dst)
1131 return -1;
1132
1133 struct user_class {
1134 uint16_t len;
1135 uint8_t data[];
1136 } *e = (struct user_class *)&((*dst)[o_len]);
1137
1138 e->len = ntohs(str_len);
1139 memcpy(e->data, src, str_len);
1140
1141 o_len += str_len + 2;
1142 src = sep;
1143
1144 if (sep)
1145 sep = strpbrk(src, ARRAY_SEP);
1146 } while (src);
1147
1148 return o_len;
1149 }
1150
1151 static int parse_opt_data(const char *data, uint8_t **dst, const unsigned int type,
1152 const bool array)
1153 {
1154 int ret = 0;
1155
1156 switch (type) {
1157 case OPT_U8:
1158 ret = parse_opt_u8(data, dst);
1159 break;
1160
1161 case OPT_STR:
1162 ret = parse_opt_string(data, dst, array);
1163 break;
1164
1165 case OPT_DNS_STR:
1166 ret = parse_opt_dns_string(data, dst, array);
1167 break;
1168
1169 case OPT_IP6:
1170 ret = parse_opt_ip6(data, dst, array);
1171 break;
1172
1173 case OPT_USER_CLASS:
1174 ret = parse_opt_user_class(data, dst, array);
1175 break;
1176
1177 default:
1178 ret = -1;
1179 break;
1180 }
1181
1182 return ret;
1183 }
1184
1185 static int parse_opt(const char *opt)
1186 {
1187 uint32_t optn;
1188 char *data;
1189 uint8_t *payload = NULL;
1190 int payload_len;
1191 unsigned int type = OPT_U8;
1192 bool array = false;
1193 struct odhcp6c_opt *dopt = NULL;
1194 int ret = -1;
1195
1196 data = strpbrk(opt, ":");
1197 if (!data)
1198 return -1;
1199
1200 *data = '\0';
1201 data++;
1202
1203 if (strlen(opt) == 0 || strlen(data) == 0)
1204 return -1;
1205
1206 dopt = odhcp6c_find_opt_by_name(opt);
1207 if (!dopt) {
1208 char *e;
1209 optn = strtoul(opt, &e, 0);
1210 if (*e || e == opt || optn > USHRT_MAX)
1211 return -1;
1212
1213 dopt = odhcp6c_find_opt(optn);
1214 } else
1215 optn = dopt->code;
1216
1217 /* Check if the type for the content is well-known */
1218 if (dopt) {
1219 /* Refuse internal options */
1220 if (dopt->flags & OPT_INTERNAL)
1221 return -1;
1222
1223 type = dopt->flags & OPT_MASK_SIZE;
1224 array = ((dopt->flags & OPT_ARRAY) == OPT_ARRAY) ? true : false;
1225 } else if (data[0] == '"' || data[0] == '\'') {
1226 char *end = strrchr(data + 1, data[0]);
1227
1228 if (end && (end == (data + strlen(data) - 1))) {
1229 /* Raw option is specified as a string */
1230 type = OPT_STR;
1231 data++;
1232 *end = '\0';
1233 }
1234
1235 }
1236
1237 payload_len = parse_opt_data(data, &payload, type, array);
1238 if (payload_len > 0)
1239 ret = add_opt(optn, payload, payload_len);
1240
1241 free(payload);
1242
1243 return ret;
1244 }