dhcpv6: add a minimum valid lifetime for IA_PD updates
[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();
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 size_t len;
783 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
784 uint8_t *start = odhcp6c_get_state(state, &len);
785
786 if (x && x->valid > new->valid && new->valid < safe)
787 new->valid = safe;
788
789 if (new->valid > 0) {
790 if (x) {
791 if (holdoff_interval && new->valid >= x->valid &&
792 new->valid != UINT32_MAX &&
793 new->valid - x->valid < holdoff_interval &&
794 new->preferred >= x->preferred &&
795 new->preferred != UINT32_MAX &&
796 new->preferred - x->preferred < holdoff_interval)
797 return false;
798
799 x->valid = new->valid;
800 x->preferred = new->preferred;
801 x->t1 = new->t1;
802 x->t2 = new->t2;
803 x->iaid = new->iaid;
804 } else if (odhcp6c_add_state(state, new, odhcp6c_entry_size(new)))
805 return false;
806 } else if (x)
807 odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
808
809 return true;
810 }
811
812 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
813 {
814 size_t len;
815 uint8_t *start = odhcp6c_get_state(state, &len);
816
817 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
818 (uint8_t*)c < &start[len] &&
819 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
820 ) {
821 if (c->t1 < elapsed)
822 c->t1 = 0;
823 else if (c->t1 != UINT32_MAX)
824 c->t1 -= elapsed;
825
826 if (c->t2 < elapsed)
827 c->t2 = 0;
828 else if (c->t2 != UINT32_MAX)
829 c->t2 -= elapsed;
830
831 if (c->preferred < elapsed)
832 c->preferred = 0;
833 else if (c->preferred != UINT32_MAX)
834 c->preferred -= elapsed;
835
836 if (c->valid < elapsed)
837 c->valid = 0;
838 else if (c->valid != UINT32_MAX)
839 c->valid -= elapsed;
840
841 if (!c->valid) {
842 odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
843 start = odhcp6c_get_state(state, &len);
844 } else
845 c = odhcp6c_next_entry(c);
846 }
847 }
848
849 static uint8_t *odhcp6c_state_find_opt(const uint16_t code)
850 {
851 size_t opts_len;
852 uint8_t *odata, *opts = odhcp6c_get_state(STATE_OPTS, &opts_len);
853 uint16_t otype, olen;
854
855 dhcpv6_for_each_option(opts, &opts[opts_len], otype, olen, odata) {
856 if (otype == code)
857 return &odata[-4];
858 }
859
860 return NULL;
861 }
862
863 void odhcp6c_expire(void)
864 {
865 time_t now = odhcp6c_get_milli_time() / 1000;
866 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
867
868 last_update = now;
869
870 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
871 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
872 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
873 odhcp6c_expire_list(STATE_RA_SEARCH, elapsed);
874 odhcp6c_expire_list(STATE_IA_NA, elapsed);
875 odhcp6c_expire_list(STATE_IA_PD, elapsed);
876 }
877
878 uint32_t odhcp6c_elapsed(void)
879 {
880 return odhcp6c_get_milli_time() / 1000 - last_update;
881 }
882
883 int odhcp6c_random(void *buf, size_t len)
884 {
885 return read(urandom_fd, buf, len);
886 }
887
888 bool odhcp6c_is_bound(void)
889 {
890 return bound;
891 }
892
893 bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
894 {
895 FILE *fd = fopen("/proc/net/if_inet6", "r");
896 int len;
897 bool ret = false;
898 char buf[256];
899
900 if (fd == NULL)
901 return false;
902
903 while (fgets(buf, sizeof(buf), fd)) {
904 struct in6_addr inet6_addr;
905 uint32_t flags, dummy;
906 unsigned int i;
907 char name[IF_NAMESIZE], addr_buf[33];
908
909 len = strlen(buf);
910
911 if ((len <= 0) || buf[len - 1] != '\n')
912 break;
913
914 buf[--len] = '\0';
915
916 if (sscanf(buf, "%s %x %x %x %x %s",
917 addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
918 break;
919
920 if (strcmp(name, ifname) ||
921 (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
922 continue;
923
924 for (i = 0; i < strlen(addr_buf); i++) {
925 if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
926 break;
927 }
928
929 memset(&inet6_addr, 0, sizeof(inet6_addr));
930 for (i = 0; i < (strlen(addr_buf) / 2); i++) {
931 unsigned char byte;
932 static const char hex[] = "0123456789abcdef";
933 byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
934 (index(hex, addr_buf[i * 2 + 1]) - hex);
935 inet6_addr.s6_addr[i] = byte;
936 }
937
938 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
939 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr))) {
940 ret = true;
941 break;
942 }
943 }
944
945 fclose(fd);
946 return ret;
947 }
948
949 static void sighandler(int signal)
950 {
951 if (signal == SIGUSR1)
952 signal_usr1 = true;
953 else if (signal == SIGUSR2)
954 signal_usr2 = true;
955 else if (signal == SIGIO)
956 signal_io = true;
957 else
958 signal_term = true;
959 }
960
961 static int add_opt(const uint16_t code, const uint8_t *data, const uint16_t len)
962 {
963 struct {
964 uint16_t code;
965 uint16_t len;
966 } opt_hdr = { htons(code), htons(len) };
967
968 if (odhcp6c_state_find_opt(code))
969 return -1;
970
971 if (odhcp6c_add_state(STATE_OPTS, &opt_hdr, sizeof(opt_hdr)) ||
972 odhcp6c_add_state(STATE_OPTS, data, len)) {
973 syslog(LOG_ERR, "Failed to add option %hu", code);
974 return 1;
975 }
976
977 return 0;
978 }
979
980 struct odhcp6c_opt *odhcp6c_find_opt(const uint16_t code)
981 {
982 struct odhcp6c_opt *opt = opts;
983
984 while (opt->code) {
985 if (opt->code == code)
986 return opt;
987
988 opt++;
989 }
990
991 return NULL;
992 }
993
994 static struct odhcp6c_opt *odhcp6c_find_opt_by_name(const char *name)
995 {
996 struct odhcp6c_opt *opt = opts;
997
998 if (!name || !strlen(name))
999 return NULL;
1000
1001 while (opt->code && (!opt->str || strcmp(opt->str, name)))
1002 opt++;
1003
1004 return (opt->code > 0 ? opt : NULL);
1005 }
1006
1007 static int parse_opt_u8(const char *src, uint8_t **dst)
1008 {
1009 int len = strlen(src);
1010
1011 *dst = realloc(*dst, len/2);
1012 if (!*dst)
1013 return -1;
1014
1015 return script_unhexlify(*dst, len, src);
1016 }
1017
1018 static int parse_opt_string(const char *src, uint8_t **dst, const bool array)
1019 {
1020 int o_len = 0;
1021 char *sep = strpbrk(src, ARRAY_SEP);
1022
1023 if (sep && !array)
1024 return -1;
1025
1026 do {
1027 if (sep) {
1028 *sep = 0;
1029 sep++;
1030 }
1031
1032 int len = strlen(src);
1033
1034 *dst = realloc(*dst, o_len + len);
1035 if (!*dst)
1036 return -1;
1037
1038 memcpy(&((*dst)[o_len]), src, len);
1039
1040 o_len += len;
1041 src = sep;
1042
1043 if (sep)
1044 sep = strpbrk(src, ARRAY_SEP);
1045 } while (src);
1046
1047 return o_len;
1048 }
1049
1050 static int parse_opt_dns_string(const char *src, uint8_t **dst, const bool array)
1051 {
1052 int o_len = 0;
1053 char *sep = strpbrk(src, ARRAY_SEP);
1054
1055 if (sep && !array)
1056 return -1;
1057
1058 do {
1059 uint8_t tmp[256];
1060
1061 if (sep) {
1062 *sep = 0;
1063 sep++;
1064 }
1065
1066 int len = dn_comp(src, tmp, sizeof(tmp), NULL, NULL);
1067 if (len < 0)
1068 return -1;
1069
1070 *dst = realloc(*dst, o_len + len);
1071 if (!*dst)
1072 return -1;
1073
1074 memcpy(&((*dst)[o_len]), tmp, len);
1075
1076 o_len += len;
1077 src = sep;
1078
1079 if (sep)
1080 sep = strpbrk(src, ARRAY_SEP);
1081 } while (src);
1082
1083 return o_len;
1084 }
1085
1086 static int parse_opt_ip6(const char *src, uint8_t **dst, const bool array)
1087 {
1088 int o_len = 0;
1089 char *sep = strpbrk(src, ARRAY_SEP);
1090
1091 if (sep && !array)
1092 return -1;
1093
1094 do {
1095 int len = sizeof(struct in6_addr);
1096
1097 if (sep) {
1098 *sep = 0;
1099 sep++;
1100 }
1101
1102 *dst = realloc(*dst, o_len + len);
1103 if (!*dst)
1104 return -1;
1105
1106 if (inet_pton(AF_INET6, src, &((*dst)[o_len])) < 1)
1107 return -1;
1108
1109 o_len += len;
1110 src = sep;
1111
1112 if (sep)
1113 sep = strpbrk(src, ARRAY_SEP);
1114 } while (src);
1115
1116 return o_len;
1117 }
1118
1119 static int parse_opt_user_class(const char *src, uint8_t **dst, const bool array)
1120 {
1121 int o_len = 0;
1122 char *sep = strpbrk(src, ARRAY_SEP);
1123
1124 if (sep && !array)
1125 return -1;
1126
1127 do {
1128 if (sep) {
1129 *sep = 0;
1130 sep++;
1131 }
1132 uint16_t str_len = strlen(src);
1133
1134 *dst = realloc(*dst, o_len + str_len + 2);
1135 if (!*dst)
1136 return -1;
1137
1138 struct user_class {
1139 uint16_t len;
1140 uint8_t data[];
1141 } *e = (struct user_class *)&((*dst)[o_len]);
1142
1143 e->len = ntohs(str_len);
1144 memcpy(e->data, src, str_len);
1145
1146 o_len += str_len + 2;
1147 src = sep;
1148
1149 if (sep)
1150 sep = strpbrk(src, ARRAY_SEP);
1151 } while (src);
1152
1153 return o_len;
1154 }
1155
1156 static int parse_opt_data(const char *data, uint8_t **dst, const unsigned int type,
1157 const bool array)
1158 {
1159 int ret = 0;
1160
1161 switch (type) {
1162 case OPT_U8:
1163 ret = parse_opt_u8(data, dst);
1164 break;
1165
1166 case OPT_STR:
1167 ret = parse_opt_string(data, dst, array);
1168 break;
1169
1170 case OPT_DNS_STR:
1171 ret = parse_opt_dns_string(data, dst, array);
1172 break;
1173
1174 case OPT_IP6:
1175 ret = parse_opt_ip6(data, dst, array);
1176 break;
1177
1178 case OPT_USER_CLASS:
1179 ret = parse_opt_user_class(data, dst, array);
1180 break;
1181
1182 default:
1183 ret = -1;
1184 break;
1185 }
1186
1187 return ret;
1188 }
1189
1190 static int parse_opt(const char *opt)
1191 {
1192 uint32_t optn;
1193 char *data;
1194 uint8_t *payload = NULL;
1195 int payload_len;
1196 unsigned int type = OPT_U8;
1197 bool array = false;
1198 struct odhcp6c_opt *dopt = NULL;
1199 int ret = -1;
1200
1201 data = strpbrk(opt, ":");
1202 if (!data)
1203 return -1;
1204
1205 *data = '\0';
1206 data++;
1207
1208 if (strlen(opt) == 0 || strlen(data) == 0)
1209 return -1;
1210
1211 dopt = odhcp6c_find_opt_by_name(opt);
1212 if (!dopt) {
1213 char *e;
1214 optn = strtoul(opt, &e, 0);
1215 if (*e || e == opt || optn > USHRT_MAX)
1216 return -1;
1217
1218 dopt = odhcp6c_find_opt(optn);
1219 } else
1220 optn = dopt->code;
1221
1222 /* Check if the type for the content is well-known */
1223 if (dopt) {
1224 /* Refuse internal options */
1225 if (dopt->flags & OPT_INTERNAL)
1226 return -1;
1227
1228 type = dopt->flags & OPT_MASK_SIZE;
1229 array = ((dopt->flags & OPT_ARRAY) == OPT_ARRAY) ? true : false;
1230 } else if (data[0] == '"' || data[0] == '\'') {
1231 char *end = strrchr(data + 1, data[0]);
1232
1233 if (end && (end == (data + strlen(data) - 1))) {
1234 /* Raw option is specified as a string */
1235 type = OPT_STR;
1236 data++;
1237 *end = '\0';
1238 }
1239
1240 }
1241
1242 payload_len = parse_opt_data(data, &payload, type, array);
1243 if (payload_len > 0)
1244 ret = add_opt(optn, payload, payload_len);
1245
1246 free(payload);
1247
1248 return ret;
1249 }