2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
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.
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.
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
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,
43 static char *argv
[4] = {NULL
, NULL
, NULL
, NULL
};
46 int script_init(const char *path
, const char *ifname
)
48 argv
[0] = (char*)path
;
49 argv
[1] = (char*)ifname
;
54 ssize_t
script_unhexlify(uint8_t *dst
, size_t len
, const char *src
)
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)
64 while (((int8_t)*src
) < 0 ||
65 (*src
&& hexvals
[(uint8_t)*src
] < 0))
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];
82 static void ipv6_to_env(const char *name
,
83 const struct in6_addr
*addr
, size_t cnt
)
85 size_t buf_len
= strlen(name
);
86 char *buf
= realloc(NULL
, cnt
* INET6_ADDRSTRLEN
+ buf_len
+ 2);
87 memcpy(buf
, name
, 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
]);
94 buf
[buf_len
- 1] = '\0';
99 static void fqdn_to_env(const char *name
, const uint8_t *fqdn
, size_t len
)
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
++] = '=';
108 while (l
> 0 && fqdn
< fqdn_end
) {
109 l
= dn_expand(fqdn
, fqdn_end
, fqdn
, &buf
[buf_len
], buf_size
- buf_len
);
111 buf_len
+= strlen(&buf
[buf_len
]);
112 buf
[buf_len
++] = ' ';
114 buf
[buf_len
- 1] = '\0';
119 static void fqdn_to_ip_env(const char *name
, const uint8_t *fqdn
, size_t len
)
121 size_t buf_len
= strlen(name
);
122 char *buf
= realloc(NULL
, INET6_ADDRSTRLEN
+ buf_len
+ 3);
123 memcpy(buf
, name
, buf_len
);
124 buf
[buf_len
++] = '=';
127 if (dn_expand(fqdn
, fqdn
+ len
, fqdn
, namebuf
, sizeof(namebuf
)) <= 0)
130 struct addrinfo hints
= {.ai_family
= AF_INET6
}, *r
;
131 if (getaddrinfo(namebuf
, NULL
, &hints
, &r
))
134 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)r
->ai_addr
;
135 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
142 static void bin_to_env(uint8_t *opts
, size_t len
)
144 uint8_t *oend
= opts
+ len
, *odata
;
145 uint16_t otype
, olen
;
146 dhcpv6_for_each_option(opts
, oend
, otype
, olen
, odata
) {
147 char *buf
= realloc(NULL
, 14 + (olen
* 2));
150 snprintf(buf
, 14, "OPTION_%hu=", otype
);
151 buf_len
+= strlen(buf
);
153 script_hexlify(&buf
[buf_len
], odata
, olen
);
165 static void entry_to_env(const char *name
, const void *data
, size_t len
, enum entry_type type
)
167 size_t buf_len
= strlen(name
);
168 const struct odhcp6c_entry
*e
= data
;
169 char *buf
= realloc(NULL
, buf_len
+ 2 + (len
/ sizeof(*e
)) * 144);
170 memcpy(buf
, name
, buf_len
);
171 buf
[buf_len
++] = '=';
173 for (size_t i
= 0; i
< len
/ sizeof(*e
); ++i
) {
174 inet_ntop(AF_INET6
, &e
[i
].target
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
175 buf_len
+= strlen(&buf
[buf_len
]);
176 if (type
!= ENTRY_HOST
) {
177 buf_len
+= snprintf(&buf
[buf_len
], 6, "/%"PRIu16
, e
[i
].length
);
178 if (type
== ENTRY_ROUTE
) {
179 buf
[buf_len
++] = ',';
180 if (!IN6_IS_ADDR_UNSPECIFIED(&e
[i
].router
)) {
181 inet_ntop(AF_INET6
, &e
[i
].router
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
182 buf_len
+= strlen(&buf
[buf_len
]);
184 buf_len
+= snprintf(&buf
[buf_len
], 24, ",%u", e
[i
].valid
);
185 buf_len
+= snprintf(&buf
[buf_len
], 12, ",%u", e
[i
].priority
);
187 buf_len
+= snprintf(&buf
[buf_len
], 24, ",%u,%u", e
[i
].preferred
, e
[i
].valid
);
190 if ((type
== ENTRY_PREFIX
|| type
== ENTRY_ADDRESS
) && e
[i
].class)
191 buf_len
+= snprintf(&buf
[buf_len
], 12, ",class=%u", e
[i
].class);
192 else if (type
== ENTRY_PREFIX
&& ntohl(e
[i
].iaid
) != 1)
193 buf_len
+= snprintf(&buf
[buf_len
], 16, ",class=%08x", ntohl(e
[i
].iaid
));
195 if (type
== ENTRY_PREFIX
&& e
[i
].priority
) {
196 // priority and router are abused for prefix exclusion
197 buf_len
+= snprintf(&buf
[buf_len
], 12, ",excluded=");
198 inet_ntop(AF_INET6
, &e
[i
].router
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
199 buf_len
+= strlen(&buf
[buf_len
]);
200 buf_len
+= snprintf(&buf
[buf_len
], 24, "/%u", e
[i
].priority
);
203 buf
[buf_len
++] = ' ';
206 buf
[buf_len
- 1] = '\0';
211 static void s46_to_env_portparams(const uint8_t *data
, size_t len
, FILE *fp
)
214 uint16_t otype
, olen
;
215 dhcpv6_for_each_option(data
, &data
[len
], otype
, olen
, odata
) {
216 if (otype
== DHCPV6_OPT_S46_PORTPARAMS
&&
217 olen
== sizeof(struct dhcpv6_s46_portparams
)) {
218 struct dhcpv6_s46_portparams
*params
= (void*)odata
;
219 fprintf(fp
, "offset=%d,psidlen=%d,psid=%d,",
220 params
->offset
, params
->psid_len
, ntohs(params
->psid
));
226 static void s46_to_env(enum odhcp6c_state state
, const uint8_t *data
, size_t len
)
228 const char *name
= (state
== STATE_S46_MAPE
) ? "MAPE" :
229 (state
== STATE_S46_MAPT
) ? "MAPT" : "LW4O6";
237 FILE *fp
= open_memstream(&str
, &strsize
);
241 const char *type
= (state
== STATE_S46_MAPE
) ? "map-e" :
242 (state
== STATE_S46_MAPT
) ? "map-t" : "lw4o6";
245 uint16_t otype
, olen
;
246 dhcpv6_for_each_option(data
, &data
[len
], otype
, olen
, odata
) {
247 struct dhcpv6_s46_rule
*rule
= (struct dhcpv6_s46_rule
*)odata
;
248 struct dhcpv6_s46_dmr
*dmr
= (struct dhcpv6_s46_dmr
*)odata
;
249 struct dhcpv6_s46_v4v6bind
*bind
= (struct dhcpv6_s46_v4v6bind
*)odata
;
251 if (state
!= STATE_S46_LW
&& otype
== DHCPV6_OPT_S46_RULE
&&
252 olen
>= sizeof(struct dhcpv6_s46_rule
)) {
253 char buf4
[INET_ADDRSTRLEN
];
254 char buf6
[INET6_ADDRSTRLEN
];
255 struct in6_addr in6
= IN6ADDR_ANY_INIT
;
257 size_t prefix6len
= rule
->prefix6_len
;
258 prefix6len
= (prefix6len
% 8 == 0) ? prefix6len
/ 8 : prefix6len
/ 8 + 1;
260 if (olen
< sizeof(struct dhcpv6_s46_rule
) + prefix6len
)
263 memcpy(&in6
, rule
->ipv6_prefix
, prefix6len
);
265 inet_ntop(AF_INET
, &rule
->ipv4_prefix
, buf4
, sizeof(buf4
));
266 inet_ntop(AF_INET6
, &in6
, buf6
, sizeof(buf6
));
271 fprintf(fp
, "type=%s,ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
272 type
, rule
->ea_len
, rule
->prefix4_len
, rule
->prefix6_len
, buf4
, buf6
);
274 s46_to_env_portparams(&rule
->ipv6_prefix
[prefix6len
],
275 olen
- sizeof(*rule
) - prefix6len
, fp
);
277 dhcpv6_for_each_option(data
, &data
[len
], otype
, olen
, odata
) {
278 if (state
!= STATE_S46_MAPT
&& otype
== DHCPV6_OPT_S46_BR
&&
279 olen
== sizeof(struct in6_addr
)) {
280 inet_ntop(AF_INET6
, odata
, buf6
, sizeof(buf6
));
281 fprintf(fp
, "br=%s,", buf6
);
282 } else if (state
== STATE_S46_MAPT
&& otype
== DHCPV6_OPT_S46_DMR
&&
283 olen
>= sizeof(struct dhcpv6_s46_dmr
)) {
284 dmr
= (struct dhcpv6_s46_dmr
*)odata
;
285 memset(&in6
, 0, sizeof(in6
));
286 size_t prefix6len
= dmr
->dmr_prefix6_len
;
287 prefix6len
= (prefix6len
% 8 == 0) ? prefix6len
/ 8 : prefix6len
/ 8 + 1;
289 if (olen
< sizeof(struct dhcpv6_s46_dmr
) + prefix6len
)
292 memcpy(&in6
, dmr
->dmr_ipv6_prefix
, prefix6len
);
293 inet_ntop(AF_INET6
, &in6
, buf6
, sizeof(buf6
));
294 fprintf(fp
, "dmr=%s/%d,", buf6
, dmr
->dmr_prefix6_len
);
297 } else if (state
== STATE_S46_LW
&& otype
== DHCPV6_OPT_S46_V4V6BIND
&&
298 olen
>= sizeof(struct dhcpv6_s46_v4v6bind
)) {
299 char buf4
[INET_ADDRSTRLEN
];
300 char buf6
[INET6_ADDRSTRLEN
];
301 struct in6_addr in6
= IN6ADDR_ANY_INIT
;
303 size_t prefix6len
= bind
->bindprefix6_len
;
304 prefix6len
= (prefix6len
% 8 == 0) ? prefix6len
/ 8 : prefix6len
/ 8 + 1;
306 if (olen
< sizeof(struct dhcpv6_s46_v4v6bind
) + prefix6len
)
309 memcpy(&in6
, bind
->bind_ipv6_prefix
, prefix6len
);
311 inet_ntop(AF_INET
, &bind
->ipv4_address
, buf4
, sizeof(buf4
));
312 inet_ntop(AF_INET6
, &in6
, buf6
, sizeof(buf6
));
314 fprintf(fp
, "type=%s,ipv4address=%s,prefix6len=%d,ipv6prefix=%s,",
315 type
, buf4
, bind
->bindprefix6_len
, buf6
);
317 s46_to_env_portparams(&bind
->bind_ipv6_prefix
[prefix6len
],
318 olen
- sizeof(*bind
) - prefix6len
, fp
);
320 dhcpv6_for_each_option(data
, &data
[len
], otype
, olen
, odata
) {
321 if (otype
== DHCPV6_OPT_S46_BR
&& olen
== sizeof(struct in6_addr
)) {
322 inet_ntop(AF_INET6
, odata
, buf6
, sizeof(buf6
));
323 fprintf(fp
, "br=%s,", buf6
);
336 void script_call(const char *status
)
338 size_t dns_len
, search_len
, custom_len
, sntp_ip_len
, ntp_ip_len
, ntp_dns_len
;
339 size_t sip_ip_len
, sip_fqdn_len
, aftr_name_len
, cer_len
, addr_len
;
340 size_t s46_mapt_len
, s46_mape_len
, s46_lw_len
, passthru_len
;
344 struct in6_addr
*addr
= odhcp6c_get_state(STATE_SERVER_ADDR
, &addr_len
);
345 struct in6_addr
*dns
= odhcp6c_get_state(STATE_DNS
, &dns_len
);
346 uint8_t *search
= odhcp6c_get_state(STATE_SEARCH
, &search_len
);
347 uint8_t *custom
= odhcp6c_get_state(STATE_CUSTOM_OPTS
, &custom_len
);
348 struct in6_addr
*sntp
= odhcp6c_get_state(STATE_SNTP_IP
, &sntp_ip_len
);
349 struct in6_addr
*ntp
= odhcp6c_get_state(STATE_NTP_IP
, &ntp_ip_len
);
350 uint8_t *ntp_dns
= odhcp6c_get_state(STATE_NTP_FQDN
, &ntp_dns_len
);
351 struct in6_addr
*sip
= odhcp6c_get_state(STATE_SIP_IP
, &sip_ip_len
);
352 uint8_t *sip_fqdn
= odhcp6c_get_state(STATE_SIP_FQDN
, &sip_fqdn_len
);
353 uint8_t *aftr_name
= odhcp6c_get_state(STATE_AFTR_NAME
, &aftr_name_len
);
354 struct in6_addr
*cer
= odhcp6c_get_state(STATE_CER
, &cer_len
);
355 uint8_t *s46_mapt
= odhcp6c_get_state(STATE_S46_MAPT
, &s46_mapt_len
);
356 uint8_t *s46_mape
= odhcp6c_get_state(STATE_S46_MAPE
, &s46_mape_len
);
357 uint8_t *s46_lw
= odhcp6c_get_state(STATE_S46_LW
, &s46_lw_len
);
358 uint8_t *passthru
= odhcp6c_get_state(STATE_PASSTHRU
, &passthru_len
);
360 size_t prefix_len
, address_len
, ra_pref_len
, ra_route_len
, ra_dns_len
;
361 uint8_t *prefix
= odhcp6c_get_state(STATE_IA_PD
, &prefix_len
);
362 uint8_t *address
= odhcp6c_get_state(STATE_IA_NA
, &address_len
);
363 uint8_t *ra_pref
= odhcp6c_get_state(STATE_RA_PREFIX
, &ra_pref_len
);
364 uint8_t *ra_route
= odhcp6c_get_state(STATE_RA_ROUTE
, &ra_route_len
);
365 uint8_t *ra_dns
= odhcp6c_get_state(STATE_RA_DNS
, &ra_dns_len
);
367 // Don't set environment before forking, because env is leaky.
369 ipv6_to_env("SERVER", addr
, addr_len
/ sizeof(*addr
));
370 ipv6_to_env("RDNSS", dns
, dns_len
/ sizeof(*dns
));
371 ipv6_to_env("SNTP_IP", sntp
, sntp_ip_len
/ sizeof(*sntp
));
372 ipv6_to_env("NTP_IP", ntp
, ntp_ip_len
/ sizeof(*ntp
));
373 fqdn_to_env("NTP_FQDN", ntp_dns
, ntp_dns_len
);
374 ipv6_to_env("SIP_IP", sip
, sip_ip_len
/ sizeof(*sip
));
375 fqdn_to_env("DOMAINS", search
, search_len
);
376 fqdn_to_env("SIP_DOMAIN", sip_fqdn
, sip_fqdn_len
);
377 fqdn_to_env("AFTR", aftr_name
, aftr_name_len
);
378 fqdn_to_ip_env("AFTR_IP", aftr_name
, aftr_name_len
);
379 ipv6_to_env("CER", cer
, cer_len
/ sizeof(*cer
));
380 s46_to_env(STATE_S46_MAPE
, s46_mape
, s46_mape_len
);
381 s46_to_env(STATE_S46_MAPT
, s46_mapt
, s46_mapt_len
);
382 s46_to_env(STATE_S46_LW
, s46_lw
, s46_lw_len
);
383 bin_to_env(custom
, custom_len
);
385 if (odhcp6c_is_bound()) {
386 entry_to_env("PREFIXES", prefix
, prefix_len
, ENTRY_PREFIX
);
387 entry_to_env("ADDRESSES", address
, address_len
, ENTRY_ADDRESS
);
390 entry_to_env("RA_ADDRESSES", ra_pref
, ra_pref_len
, ENTRY_ADDRESS
);
391 entry_to_env("RA_ROUTES", ra_route
, ra_route_len
, ENTRY_ROUTE
);
392 entry_to_env("RA_DNS", ra_dns
, ra_dns_len
, ENTRY_HOST
);
394 char *buf
= malloc(10 + passthru_len
* 2);
395 strncpy(buf
, "PASSTHRU=", 10);
396 script_hexlify(&buf
[9], passthru
, passthru_len
);
399 argv
[2] = (char*)status
;
400 execv(argv
[0], argv
);
404 // Delete lost prefixes and user opts
405 odhcp6c_clear_state(STATE_CUSTOM_OPTS
);