30441b9e4739e86b928d237d4dd9a9715faee62e
[project/odhcp6c.git] / src / script.c
1 /**
2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15 #include <stdio.h>
16 #include <netdb.h>
17 #include <resolv.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <syslog.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <inttypes.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26
27 #include "odhcp6c.h"
28
29 static const char hexdigits[] = "0123456789abcdef";
30 static const int8_t hexvals[] = {
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
35 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39 };
40
41
42
43 static char *argv[4] = {NULL, NULL, NULL, NULL};
44
45
46 int script_init(const char *path, const char *ifname)
47 {
48 argv[0] = (char*)path;
49 argv[1] = (char*)ifname;
50 return 0;
51 }
52
53
54 ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src)
55 {
56 size_t c;
57 for (c = 0; c < len && src[0] && src[1]; ++c) {
58 int8_t x = (int8_t)*src++;
59 int8_t y = (int8_t)*src++;
60 if (x < 0 || (x = hexvals[x]) < 0
61 || y < 0 || (y = hexvals[y]) < 0)
62 return -1;
63 dst[c] = x << 4 | y;
64 while (((int8_t)*src) < 0 ||
65 (*src && hexvals[(uint8_t)*src] < 0))
66 src++;
67 }
68
69 return c;
70 }
71
72
73 static void script_hexlify(char *dst, const uint8_t *src, size_t len) {
74 for (size_t i = 0; i < len; ++i) {
75 *dst++ = hexdigits[src[i] >> 4];
76 *dst++ = hexdigits[src[i] & 0x0f];
77 }
78 *dst = 0;
79 }
80
81
82 static void ipv6_to_env(const char *name,
83 const struct in6_addr *addr, size_t cnt)
84 {
85 size_t buf_len = strlen(name);
86 char *buf = realloc(NULL, cnt * INET6_ADDRSTRLEN + buf_len + 2);
87 memcpy(buf, name, buf_len);
88 buf[buf_len++] = '=';
89 for (size_t i = 0; i < cnt; ++i) {
90 inet_ntop(AF_INET6, &addr[i], &buf[buf_len], INET6_ADDRSTRLEN);
91 buf_len += strlen(&buf[buf_len]);
92 buf[buf_len++] = ' ';
93 }
94 buf[buf_len - 1] = '\0';
95 putenv(buf);
96 }
97
98
99 static void fqdn_to_env(const char *name, const uint8_t *fqdn, size_t len)
100 {
101 size_t buf_len = strlen(name);
102 size_t buf_size = len + buf_len + 2;
103 const uint8_t *fqdn_end = fqdn + len;
104 char *buf = realloc(NULL, len + buf_len + 2);
105 memcpy(buf, name, buf_len);
106 buf[buf_len++] = '=';
107 int l = 1;
108 while (l > 0 && fqdn < fqdn_end) {
109 l = dn_expand(fqdn, fqdn_end, fqdn, &buf[buf_len], buf_size - buf_len);
110 fqdn += l;
111 buf_len += strlen(&buf[buf_len]);
112 buf[buf_len++] = ' ';
113 }
114 buf[buf_len - 1] = '\0';
115 putenv(buf);
116 }
117
118 static void bin_to_env(uint8_t *opts, size_t len)
119 {
120 uint8_t *oend = opts + len, *odata;
121 uint16_t otype, olen;
122 dhcpv6_for_each_option(opts, oend, otype, olen, odata) {
123 char *buf = realloc(NULL, 14 + (olen * 2));
124 size_t buf_len = 0;
125
126 snprintf(buf, 14, "OPTION_%hu=", otype);
127 buf_len += strlen(buf);
128
129 script_hexlify(&buf[buf_len], odata, olen);
130 putenv(buf);
131 }
132 }
133
134 enum entry_type {
135 ENTRY_ADDRESS,
136 ENTRY_HOST,
137 ENTRY_ROUTE,
138 ENTRY_PREFIX
139 };
140
141 static void entry_to_env(const char *name, const void *data, size_t len, enum entry_type type)
142 {
143 size_t buf_len = strlen(name);
144 const struct odhcp6c_entry *e = data;
145 char *buf = realloc(NULL, buf_len + 2 + (len / sizeof(*e)) * 144);
146 memcpy(buf, name, buf_len);
147 buf[buf_len++] = '=';
148
149 for (size_t i = 0; i < len / sizeof(*e); ++i) {
150 inet_ntop(AF_INET6, &e[i].target, &buf[buf_len], INET6_ADDRSTRLEN);
151 buf_len += strlen(&buf[buf_len]);
152 if (type != ENTRY_HOST) {
153 buf_len += snprintf(&buf[buf_len], 6, "/%"PRIu16, e[i].length);
154 if (type == ENTRY_ROUTE) {
155 buf[buf_len++] = ',';
156 if (!IN6_IS_ADDR_UNSPECIFIED(&e[i].router)) {
157 inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
158 buf_len += strlen(&buf[buf_len]);
159 }
160 buf_len += snprintf(&buf[buf_len], 24, ",%u", e[i].valid);
161 buf_len += snprintf(&buf[buf_len], 12, ",%u", e[i].priority);
162 } else {
163 buf_len += snprintf(&buf[buf_len], 24, ",%u,%u", e[i].preferred, e[i].valid);
164 }
165
166 if (type == ENTRY_PREFIX && ntohl(e[i].iaid) != 1)
167 buf_len += snprintf(&buf[buf_len], 16, ",class=%08x", ntohl(e[i].iaid));
168
169 if (type == ENTRY_PREFIX && e[i].priority) {
170 // priority and router are abused for prefix exclusion
171 buf_len += snprintf(&buf[buf_len], 12, ",excluded=");
172 inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN);
173 buf_len += strlen(&buf[buf_len]);
174 buf_len += snprintf(&buf[buf_len], 24, "/%u", e[i].priority);
175 }
176 }
177 buf[buf_len++] = ' ';
178 }
179
180 buf[buf_len - 1] = '\0';
181 putenv(buf);
182 }
183
184
185 static void search_to_env(const char *name, const uint8_t *start, size_t len)
186 {
187 size_t buf_len = strlen(name);
188 char *buf = realloc(NULL, buf_len + 2 + len);
189 char *c = mempcpy(buf, name, buf_len);
190 *c++ = '=';
191
192 for (struct odhcp6c_entry *e = (struct odhcp6c_entry*)start;
193 (uint8_t*)e < &start[len] && &e->auxtarget[e->auxlen] <= &start[len];
194 e = (struct odhcp6c_entry*)(&e->auxtarget[e->auxlen])) {
195 c = mempcpy(c, e->auxtarget, e->auxlen);
196 *c++ = ' ';
197 }
198
199 c[-1] = '\0';
200 putenv(buf);
201 }
202
203
204 static void int_to_env(const char *name, int value)
205 {
206 size_t len = 12 + strlen(name);
207 char *buf = realloc(NULL, len);
208 snprintf(buf, len, "%s=%d", name, value);
209 putenv(buf);
210 }
211
212
213 static void s46_to_env_portparams(const uint8_t *data, size_t len, FILE *fp)
214 {
215 uint8_t *odata;
216 uint16_t otype, olen;
217 dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
218 if (otype == DHCPV6_OPT_S46_PORTPARAMS &&
219 olen == sizeof(struct dhcpv6_s46_portparams)) {
220 struct dhcpv6_s46_portparams *params = (void*)odata;
221 fprintf(fp, "offset=%d,psidlen=%d,psid=%d,",
222 params->offset, params->psid_len, ntohs(params->psid));
223 }
224 }
225 }
226
227
228 static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len)
229 {
230 const char *name = (state == STATE_S46_MAPE) ? "MAPE" :
231 (state == STATE_S46_MAPT) ? "MAPT" : "LW4O6";
232
233 if (len == 0)
234 return;
235
236 char *str;
237 size_t strsize;
238
239 FILE *fp = open_memstream(&str, &strsize);
240 fputs(name, fp);
241 fputc('=', fp);
242
243 const char *type = (state == STATE_S46_MAPE) ? "map-e" :
244 (state == STATE_S46_MAPT) ? "map-t" : "lw4o6";
245
246 uint8_t *odata;
247 uint16_t otype, olen;
248 dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
249 struct dhcpv6_s46_rule *rule = (struct dhcpv6_s46_rule*)odata;
250 struct dhcpv6_s46_dmr *dmr = (struct dhcpv6_s46_dmr*)odata;
251 struct dhcpv6_s46_v4v6bind *bind = (struct dhcpv6_s46_v4v6bind*)odata;
252
253 if (state != STATE_S46_LW && otype == DHCPV6_OPT_S46_RULE &&
254 olen >= sizeof(struct dhcpv6_s46_rule)) {
255 char buf4[INET_ADDRSTRLEN];
256 char buf6[INET6_ADDRSTRLEN];
257 struct in6_addr in6 = IN6ADDR_ANY_INIT;
258
259 size_t prefix6len = rule->prefix6_len;
260 prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
261
262 if (olen < sizeof(struct dhcpv6_s46_rule) + prefix6len)
263 continue;
264
265 memcpy(&in6, rule->ipv6_prefix, prefix6len);
266
267 inet_ntop(AF_INET, &rule->ipv4_prefix, buf4, sizeof(buf4));
268 inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
269
270 if (rule->flags & 1)
271 fputs("fmr,", fp);
272
273 fprintf(fp, "type=%s,ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
274 type, rule->ea_len, rule->prefix4_len, rule->prefix6_len, buf4, buf6);
275
276 s46_to_env_portparams(&rule->ipv6_prefix[prefix6len],
277 olen - sizeof(*rule) - prefix6len, fp);
278
279 dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
280 if (state != STATE_S46_MAPT && otype == DHCPV6_OPT_S46_BR &&
281 olen == sizeof(struct in6_addr)) {
282 inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
283 fprintf(fp, "br=%s,", buf6);
284 } else if (state == STATE_S46_MAPT && otype == DHCPV6_OPT_S46_DMR &&
285 olen >= sizeof(struct dhcpv6_s46_dmr)) {
286 dmr = (struct dhcpv6_s46_dmr*)odata;
287 memset(&in6, 0, sizeof(in6));
288 size_t prefix6len = dmr->dmr_prefix6_len;
289 prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
290
291 if (olen < sizeof(struct dhcpv6_s46_dmr) + prefix6len)
292 continue;
293
294 memcpy(&in6, dmr->dmr_ipv6_prefix, prefix6len);
295 inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
296 fprintf(fp, "dmr=%s/%d,", buf6, dmr->dmr_prefix6_len);
297 }
298 }
299 } else if (state == STATE_S46_LW && otype == DHCPV6_OPT_S46_V4V6BIND &&
300 olen >= sizeof(struct dhcpv6_s46_v4v6bind)) {
301 char buf4[INET_ADDRSTRLEN];
302 char buf6[INET6_ADDRSTRLEN];
303 struct in6_addr in6 = IN6ADDR_ANY_INIT;
304
305 size_t prefix6len = bind->bindprefix6_len;
306 prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
307
308 if (olen < sizeof(struct dhcpv6_s46_v4v6bind) + prefix6len)
309 continue;
310
311 memcpy(&in6, bind->bind_ipv6_prefix, prefix6len);
312
313 inet_ntop(AF_INET, &bind->ipv4_address, buf4, sizeof(buf4));
314 inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
315
316 fprintf(fp, "type=%s,ipv4address=%s,prefix6len=%d,ipv6prefix=%s,",
317 type, buf4, bind->bindprefix6_len, buf6);
318
319 s46_to_env_portparams(&bind->bind_ipv6_prefix[prefix6len],
320 olen - sizeof(*bind) - prefix6len, fp);
321
322 dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
323 if (otype == DHCPV6_OPT_S46_BR && olen == sizeof(struct in6_addr)) {
324 inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
325 fprintf(fp, "br=%s,", buf6);
326 }
327 }
328 }
329
330 fputc(' ', fp);
331 }
332
333 fclose(fp);
334 putenv(str);
335 }
336
337
338 void script_call(const char *status)
339 {
340 size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len;
341 size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len, addr_len;
342 size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len;
343
344 odhcp6c_expire();
345
346 struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len);
347 struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len);
348 uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len);
349 uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len);
350 struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
351 struct in6_addr *ntp = odhcp6c_get_state(STATE_NTP_IP, &ntp_ip_len);
352 uint8_t *ntp_dns = odhcp6c_get_state(STATE_NTP_FQDN, &ntp_dns_len);
353 struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
354 uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
355 uint8_t *aftr_name = odhcp6c_get_state(STATE_AFTR_NAME, &aftr_name_len);
356 struct in6_addr *cer = odhcp6c_get_state(STATE_CER, &cer_len);
357 uint8_t *s46_mapt = odhcp6c_get_state(STATE_S46_MAPT, &s46_mapt_len);
358 uint8_t *s46_mape = odhcp6c_get_state(STATE_S46_MAPE, &s46_mape_len);
359 uint8_t *s46_lw = odhcp6c_get_state(STATE_S46_LW, &s46_lw_len);
360 uint8_t *passthru = odhcp6c_get_state(STATE_PASSTHRU, &passthru_len);
361
362 size_t prefix_len, address_len, ra_pref_len,
363 ra_route_len, ra_dns_len, ra_search_len;
364 uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len);
365 uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len);
366 uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len);
367 uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len);
368 uint8_t *ra_dns = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len);
369 uint8_t *ra_search = odhcp6c_get_state(STATE_RA_SEARCH, &ra_search_len);
370
371 // Don't set environment before forking, because env is leaky.
372 if (fork() == 0) {
373 ipv6_to_env("SERVER", addr, addr_len / sizeof(*addr));
374 ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns));
375 ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp));
376 ipv6_to_env("NTP_IP", ntp, ntp_ip_len / sizeof(*ntp));
377 fqdn_to_env("NTP_FQDN", ntp_dns, ntp_dns_len);
378 ipv6_to_env("SIP_IP", sip, sip_ip_len / sizeof(*sip));
379 fqdn_to_env("DOMAINS", search, search_len);
380 fqdn_to_env("SIP_DOMAIN", sip_fqdn, sip_fqdn_len);
381 fqdn_to_env("AFTR", aftr_name, aftr_name_len);
382 ipv6_to_env("CER", cer, cer_len / sizeof(*cer));
383 s46_to_env(STATE_S46_MAPE, s46_mape, s46_mape_len);
384 s46_to_env(STATE_S46_MAPT, s46_mapt, s46_mapt_len);
385 s46_to_env(STATE_S46_LW, s46_lw, s46_lw_len);
386 bin_to_env(custom, custom_len);
387
388 if (odhcp6c_is_bound()) {
389 entry_to_env("PREFIXES", prefix, prefix_len, ENTRY_PREFIX);
390 entry_to_env("ADDRESSES", address, address_len, ENTRY_ADDRESS);
391 }
392
393 entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len, ENTRY_ADDRESS);
394 entry_to_env("RA_ROUTES", ra_route, ra_route_len, ENTRY_ROUTE);
395 entry_to_env("RA_DNS", ra_dns, ra_dns_len, ENTRY_HOST);
396 search_to_env("RA_DOMAINS", ra_search, ra_search_len);
397
398 int_to_env("RA_HOPLIMIT", ra_conf_hoplimit(0));
399 int_to_env("RA_MTU", ra_conf_mtu(0));
400 int_to_env("RA_REACHABLE", ra_conf_reachable(0));
401 int_to_env("RA_RETRANSMIT", ra_conf_retransmit(0));
402
403 char *buf = malloc(10 + passthru_len * 2);
404 strncpy(buf, "PASSTHRU=", 10);
405 script_hexlify(&buf[9], passthru, passthru_len);
406 putenv(buf);
407
408 argv[2] = (char*)status;
409 execv(argv[0], argv);
410 _exit(128);
411 }
412
413 // Delete lost prefixes and user opts
414 odhcp6c_clear_state(STATE_CUSTOM_OPTS);
415 }