2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017-2018 Hans Dedecker <dedeckeh@gmail.com>
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.
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.
33 #include <sys/syscall.h>
34 #include <arpa/inet.h>
35 #include <linux/if_addr.h>
40 #ifndef IN6_IS_ADDR_UNIQUELOCAL
41 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
42 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
43 == htonl (0xfc000000))
45 #define ARRAY_SEP " ,\t"
47 static void sighandler(int signal
);
48 static int usage(void);
49 static int add_opt(const uint16_t code
, const uint8_t *data
,
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
);
55 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
56 static size_t state_len
[_STATE_MAX
] = {0};
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;
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
;
68 static unsigned int script_sync_delay
= 10;
69 static unsigned int script_accu_delay
= 1;
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
},
120 { .code
= DHCPV6_OPT_CER_ID
, .flags
= OPT_INTERNAL
| OPT_NO_PASSTHRU
, .str
= NULL
},
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
},
166 int main(_unused
int argc
, char* const argv
[])
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";
173 uint8_t buf
[134], *o_data
;
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;
183 bool help
= false, daemonize
= false;
184 int logopt
= LOG_PID
;
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
;
190 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:Ux:s:kt:m:D:Lhedp:fav")) != -1) {
193 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
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
;
209 opt
= odhcp6c_find_opt(DHCPV6_OPT_VENDOR_CLASS
);
211 syslog(LOG_ERR
, "Failed to set vendor-class option");
216 res
= parse_opt_data(optarg
, &o_data
, opt
->flags
& OPT_MASK_SIZE
,
217 (opt
->flags
& OPT_ARRAY
) == OPT_ARRAY
);
219 res
= add_opt(opt
->code
, o_data
, res
);
233 if (ia_pd_mode
== IA_MODE_NONE
)
234 ia_pd_mode
= IA_MODE_TRY
;
236 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
237 allow_slaac_only
= 10;
241 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
243 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
244 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
248 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
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));
255 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
257 if (odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
))) {
258 syslog(LOG_ERR
, "Failed to set request IPv6-Prefix");
264 allow_slaac_only
= -1;
265 ia_pd_mode
= IA_MODE_FORCE
;
269 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
272 buf
[1] = DHCPV6_OPT_CLIENTID
;
275 if (odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4)) {
276 syslog(LOG_ERR
, "Failed to override client-ID");
284 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
291 opttype
= htons(strtoul(optarg
, &optpos
, 10));
292 if (optpos
== optarg
)
297 if (odhcp6c_add_state(STATE_ORO
, &opttype
, 2)) {
298 syslog(LOG_ERR
, "Failed to add requested option");
305 client_options
|= DHCPV6_STRICT_OPTIONS
;
309 opt
= odhcp6c_find_opt(DHCPV6_OPT_USER_CLASS
);
311 syslog(LOG_ERR
, "Failed to set user-class option");
316 res
= parse_opt_data(optarg
, &o_data
, opt
->flags
& OPT_MASK_SIZE
,
317 (opt
->flags
& OPT_ARRAY
) == OPT_ARRAY
);
319 res
= add_opt(opt
->code
, o_data
, res
);
333 client_options
|= DHCPV6_IGNORE_OPT_UNICAST
;
345 sol_timeout
= atoi(optarg
);
349 ra_holdoff_interval
= atoi(optarg
);
353 ia_pd_safe_valid
= atoi(optarg
);
354 if (ia_pd_safe_valid
> 60)
355 ia_pd_safe_valid
= 60;
359 ra_options
&= ~RA_RDNSS_DEFAULT_LIFETIME
;
363 logopt
|= LOG_PERROR
;
375 client_options
&= ~DHCPV6_CLIENT_FQDN
;
379 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
387 res
= parse_opt(optarg
);
402 if (allow_slaac_only
> 0)
403 script_sync_delay
= allow_slaac_only
;
405 openlog("odhcp6c", logopt
, LOG_DAEMON
);
407 setlogmask(LOG_UPTO(LOG_WARNING
));
409 ifname
= argv
[optind
];
414 signal(SIGIO
, sighandler
);
415 signal(SIGHUP
, sighandler
);
416 signal(SIGINT
, sighandler
);
417 signal(SIGTERM
, sighandler
);
418 signal(SIGUSR1
, sighandler
);
419 signal(SIGUSR2
, sighandler
);
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
));
430 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
432 syslog(LOG_ERR
, "Failed to daemonize: %s",
438 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
439 pidfile
= (char*)buf
;
442 FILE *fp
= fopen(pidfile
, "w");
444 fprintf(fp
, "%i\n", getpid());
449 script_call("started", 0, false);
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
);
463 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
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
);
470 odhcp6c_signal_process();
476 res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
477 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
478 bool signalled
= odhcp6c_signal_process();
482 else if (signalled
) {
487 mode
= dhcpv6_promote_server_cand();
488 } while (mode
> DHCPV6_UNKNOWN
);
494 case DHCPV6_STATELESS
:
496 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
498 while (!signal_usr2
&& !signal_term
) {
500 script_call("informed", script_sync_delay
, true);
502 res
= dhcpv6_poll_reconfigure();
503 odhcp6c_signal_process();
509 signal_usr1
= false; // Acknowledged
513 if (signal_usr2
|| signal_term
)
516 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
517 odhcp6c_signal_process();
526 case DHCPV6_STATEFUL
:
528 script_call("bound", script_sync_delay
, true);
529 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
531 while (!signal_usr2
&& !signal_term
) {
533 // Wait for T1 to expire or until we get a reconfigure
534 res
= dhcpv6_poll_reconfigure();
535 odhcp6c_signal_process();
537 script_call("updated", 0, false);
541 // Handle signal, if necessary
543 signal_usr1
= false; // Acknowledged
545 if (signal_usr2
|| signal_term
)
546 break; // Other signal type
548 // Send renew as T1 expired
549 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
550 odhcp6c_signal_process();
552 if (res
> 0) { // Renew was succesfull
554 script_call("updated", 0, false);
555 continue; // Renew was successful
558 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
559 odhcp6c_clear_state(STATE_SERVER_ADDR
);
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
);
565 if (ia_pd_len
== 0 && ia_na_len
== 0)
568 // If we have IAs, try rebind otherwise restart
569 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
570 odhcp6c_signal_process();
573 script_call("rebound", 0, true);
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
);
590 // Add all prefixes to lost prefixes
592 script_call("unbound", 0, true);
594 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
595 dhcpv6_request(DHCPV6_MSG_RELEASE
);
597 odhcp6c_clear_state(STATE_IA_NA
);
598 odhcp6c_clear_state(STATE_IA_PD
);
601 script_call("stopped", 0, true);
606 static int usage(void)
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"
639 " -e Write logmessages to stderr\n"
640 " -v Increase logging verbosity\n"
641 " -h Show this help\n\n";
647 // Don't want to pull-in librt and libpthread just for a monotonic clock...
648 uint64_t odhcp6c_get_milli_time(void)
652 clock_gettime(CLOCK_MONOTONIC
, &t
);
654 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
657 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
660 return state_data
[state
] + state_len
[state
];
661 else if (state_len
[state
] + len
> 1024)
664 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
666 if (n
|| state_len
[state
] + len
== 0) {
667 state_data
[state
] = n
;
668 n
+= state_len
[state
];
669 state_len
[state
] += len
;
675 bool odhcp6c_signal_process(void)
680 bool ra_updated
= ra_process();
687 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
688 script_call("ra-updated", (!ra
&& !bound
) ?
689 script_sync_delay
: script_accu_delay
, false);
694 return signal_usr1
|| signal_usr2
|| signal_term
;
697 void odhcp6c_clear_state(enum odhcp6c_state state
)
699 state_len
[state
] = 0;
702 int odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
704 uint8_t *n
= odhcp6c_resize_state(state
, len
);
709 memcpy(n
, data
, len
);
714 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
716 ssize_t len_after
= state_len
[state
] - offset
;
720 uint8_t *n
= odhcp6c_resize_state(state
, len
);
723 uint8_t *sdata
= state_data
[state
];
725 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
726 memcpy(sdata
+ offset
, data
, len
);
732 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
734 uint8_t *data
= state_data
[state
];
735 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
738 return state_len
[state
];
740 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
742 return state_len
[state
] -= len
;
745 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
747 *len
= state_len
[state
];
748 void *data
= state_data
[state
];
750 state_len
[state
] = 0;
751 state_data
[state
] = NULL
;
756 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
758 *len
= state_len
[state
];
760 return state_data
[state
];
763 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
765 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
766 uint8_t *start
= odhcp6c_get_state(state
, &len
);
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
))
779 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
780 uint32_t safe
, unsigned int holdoff_interval
)
783 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
784 uint8_t *start
= odhcp6c_get_state(state
, &len
);
786 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
789 if (new->valid
> 0) {
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
)
799 x
->valid
= new->valid
;
800 x
->preferred
= new->preferred
;
804 } else if (odhcp6c_add_state(state
, new, odhcp6c_entry_size(new)))
807 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, odhcp6c_entry_size(x
));
812 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
815 uint8_t *start
= odhcp6c_get_state(state
, &len
);
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
];
823 else if (c
->t1
!= UINT32_MAX
)
828 else if (c
->t2
!= UINT32_MAX
)
831 if (c
->preferred
< elapsed
)
833 else if (c
->preferred
!= UINT32_MAX
)
834 c
->preferred
-= elapsed
;
836 if (c
->valid
< elapsed
)
838 else if (c
->valid
!= UINT32_MAX
)
842 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, odhcp6c_entry_size(c
));
843 start
= odhcp6c_get_state(state
, &len
);
845 c
= odhcp6c_next_entry(c
);
849 static uint8_t *odhcp6c_state_find_opt(const uint16_t code
)
852 uint8_t *odata
, *opts
= odhcp6c_get_state(STATE_OPTS
, &opts_len
);
853 uint16_t otype
, olen
;
855 dhcpv6_for_each_option(opts
, &opts
[opts_len
], otype
, olen
, odata
) {
863 void odhcp6c_expire(void)
865 time_t now
= odhcp6c_get_milli_time() / 1000;
866 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
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
);
878 uint32_t odhcp6c_elapsed(void)
880 return odhcp6c_get_milli_time() / 1000 - last_update
;
883 int odhcp6c_random(void *buf
, size_t len
)
885 return read(urandom_fd
, buf
, len
);
888 bool odhcp6c_is_bound(void)
893 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
895 FILE *fd
= fopen("/proc/net/if_inet6", "r");
903 while (fgets(buf
, sizeof(buf
), fd
)) {
904 struct in6_addr inet6_addr
;
905 uint32_t flags
, dummy
;
907 char name
[IF_NAMESIZE
], addr_buf
[33];
911 if ((len
<= 0) || buf
[len
- 1] != '\n')
916 if (sscanf(buf
, "%s %x %x %x %x %s",
917 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
920 if (strcmp(name
, ifname
) ||
921 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
924 for (i
= 0; i
< strlen(addr_buf
); i
++) {
925 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
929 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
930 for (i
= 0; i
< (strlen(addr_buf
) / 2); i
++) {
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
;
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
))) {
949 static void sighandler(int signal
)
951 if (signal
== SIGUSR1
)
953 else if (signal
== SIGUSR2
)
955 else if (signal
== SIGIO
)
961 static int add_opt(const uint16_t code
, const uint8_t *data
, const uint16_t len
)
966 } opt_hdr
= { htons(code
), htons(len
) };
968 if (odhcp6c_state_find_opt(code
))
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
);
980 struct odhcp6c_opt
*odhcp6c_find_opt(const uint16_t code
)
982 struct odhcp6c_opt
*opt
= opts
;
985 if (opt
->code
== code
)
994 static struct odhcp6c_opt
*odhcp6c_find_opt_by_name(const char *name
)
996 struct odhcp6c_opt
*opt
= opts
;
998 if (!name
|| !strlen(name
))
1001 while (opt
->code
&& (!opt
->str
|| strcmp(opt
->str
, name
)))
1004 return (opt
->code
> 0 ? opt
: NULL
);
1007 static int parse_opt_u8(const char *src
, uint8_t **dst
)
1009 int len
= strlen(src
);
1011 *dst
= realloc(*dst
, len
/2);
1015 return script_unhexlify(*dst
, len
, src
);
1018 static int parse_opt_string(const char *src
, uint8_t **dst
, const bool array
)
1021 char *sep
= strpbrk(src
, ARRAY_SEP
);
1032 int len
= strlen(src
);
1034 *dst
= realloc(*dst
, o_len
+ len
);
1038 memcpy(&((*dst
)[o_len
]), src
, len
);
1044 sep
= strpbrk(src
, ARRAY_SEP
);
1050 static int parse_opt_dns_string(const char *src
, uint8_t **dst
, const bool array
)
1053 char *sep
= strpbrk(src
, ARRAY_SEP
);
1066 int len
= dn_comp(src
, tmp
, sizeof(tmp
), NULL
, NULL
);
1070 *dst
= realloc(*dst
, o_len
+ len
);
1074 memcpy(&((*dst
)[o_len
]), tmp
, len
);
1080 sep
= strpbrk(src
, ARRAY_SEP
);
1086 static int parse_opt_ip6(const char *src
, uint8_t **dst
, const bool array
)
1089 char *sep
= strpbrk(src
, ARRAY_SEP
);
1095 int len
= sizeof(struct in6_addr
);
1102 *dst
= realloc(*dst
, o_len
+ len
);
1106 if (inet_pton(AF_INET6
, src
, &((*dst
)[o_len
])) < 1)
1113 sep
= strpbrk(src
, ARRAY_SEP
);
1119 static int parse_opt_user_class(const char *src
, uint8_t **dst
, const bool array
)
1122 char *sep
= strpbrk(src
, ARRAY_SEP
);
1132 uint16_t str_len
= strlen(src
);
1134 *dst
= realloc(*dst
, o_len
+ str_len
+ 2);
1141 } *e
= (struct user_class
*)&((*dst
)[o_len
]);
1143 e
->len
= ntohs(str_len
);
1144 memcpy(e
->data
, src
, str_len
);
1146 o_len
+= str_len
+ 2;
1150 sep
= strpbrk(src
, ARRAY_SEP
);
1156 static int parse_opt_data(const char *data
, uint8_t **dst
, const unsigned int type
,
1163 ret
= parse_opt_u8(data
, dst
);
1167 ret
= parse_opt_string(data
, dst
, array
);
1171 ret
= parse_opt_dns_string(data
, dst
, array
);
1175 ret
= parse_opt_ip6(data
, dst
, array
);
1178 case OPT_USER_CLASS
:
1179 ret
= parse_opt_user_class(data
, dst
, array
);
1190 static int parse_opt(const char *opt
)
1194 uint8_t *payload
= NULL
;
1196 unsigned int type
= OPT_U8
;
1198 struct odhcp6c_opt
*dopt
= NULL
;
1201 data
= strpbrk(opt
, ":");
1208 if (strlen(opt
) == 0 || strlen(data
) == 0)
1211 dopt
= odhcp6c_find_opt_by_name(opt
);
1214 optn
= strtoul(opt
, &e
, 0);
1215 if (*e
|| e
== opt
|| optn
> USHRT_MAX
)
1218 dopt
= odhcp6c_find_opt(optn
);
1222 /* Check if the type for the content is well-known */
1224 /* Refuse internal options */
1225 if (dopt
->flags
& OPT_INTERNAL
)
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]);
1233 if (end
&& (end
== (data
+ strlen(data
) - 1))) {
1234 /* Raw option is specified as a string */
1242 payload_len
= parse_opt_data(data
, &payload
, type
, array
);
1243 if (payload_len
> 0)
1244 ret
= add_opt(optn
, payload
, payload_len
);