• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt