0e01f3aa6d094c2fb78d4b440b73660e91835942
2 * Copyright (C) 2012 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.
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
26 static const char hexdigits
[] = "0123456789abcdef";
27 static const int8_t hexvals
[] = {
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
32 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40 static char *argv
[4] = {NULL
, NULL
, NULL
, NULL
};
43 int script_init(const char *path
, const char *ifname
)
45 argv
[0] = (char*)path
;
46 argv
[1] = (char*)ifname
;
51 ssize_t
script_unhexlify(uint8_t *dst
, size_t len
, const char *src
)
54 for (c
= 0; c
< len
&& src
[0] && src
[1]; ++c
) {
55 int8_t x
= (int8_t)*src
++;
56 int8_t y
= (int8_t)*src
++;
57 if (x
< 0 || (x
= hexvals
[x
]) < 0
58 || y
< 0 || (y
= hexvals
[y
]) < 0)
61 while (((int8_t)*src
) < 0 ||
62 (*src
&& hexvals
[(uint8_t)*src
] < 0))
70 static void script_hexlify(char *dst
, const uint8_t *src
, size_t len
) {
71 for (size_t i
= 0; i
< len
; ++i
) {
72 *dst
++ = hexdigits
[src
[i
] >> 4];
73 *dst
++ = hexdigits
[src
[i
] & 0x0f];
79 static void ipv6_to_env(const char *name
,
80 const struct in6_addr
*addr
, size_t cnt
)
82 size_t buf_len
= strlen(name
);
83 char *buf
= realloc(NULL
, cnt
* INET6_ADDRSTRLEN
+ buf_len
+ 2);
84 memcpy(buf
, name
, buf_len
);
86 for (size_t i
= 0; i
< cnt
; ++i
) {
87 inet_ntop(AF_INET6
, &addr
[i
], &buf
[buf_len
], INET6_ADDRSTRLEN
);
88 buf_len
+= strlen(&buf
[buf_len
]);
91 buf
[buf_len
- 1] = '\0';
96 static void fqdn_to_env(const char *name
, const uint8_t *fqdn
, size_t len
)
98 size_t buf_len
= strlen(name
);
99 const uint8_t *fqdn_end
= fqdn
+ len
;
100 char *buf
= realloc(NULL
, len
+ buf_len
+ 2);
101 memcpy(buf
, name
, buf_len
);
102 buf
[buf_len
++] = '=';
104 while (l
> 0 && fqdn
< fqdn_end
) {
105 l
= dn_expand(fqdn
, &fqdn
[len
], fqdn
, &buf
[buf_len
], len
);
107 buf_len
+= strlen(&buf
[buf_len
]);
108 buf
[buf_len
++] = ' ';
110 buf
[buf_len
- 1] = '\0';
115 static void bin_to_env(uint8_t *opts
, size_t len
)
117 uint8_t *oend
= opts
+ len
, *odata
;
118 uint16_t otype
, olen
;
119 dhcpv6_for_each_option(opts
, oend
, otype
, olen
, odata
) {
120 char *buf
= realloc(NULL
, 14 + (olen
* 2));
123 snprintf(buf
, 14, "OPTION_%hu=", otype
);
124 buf_len
+= strlen(buf
);
126 script_hexlify(&buf
[buf_len
], odata
, olen
);
132 static void entry_to_env(const char *name
, const void *data
, size_t len
)
134 size_t buf_len
= strlen(name
);
135 const struct odhcp6c_entry
*e
= data
;
136 const struct in6_addr any
= IN6ADDR_ANY_INIT
;
137 char *buf
= realloc(NULL
, buf_len
+ 2 + (len
/ sizeof(*e
)) * 144);
138 memcpy(buf
, name
, buf_len
);
139 buf
[buf_len
++] = '=';
141 for (size_t i
= 0; i
< len
/ sizeof(*e
); ++i
) {
142 inet_ntop(AF_INET6
, &e
[i
].target
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
143 buf_len
+= strlen(&buf
[buf_len
]);
144 buf_len
+= snprintf(&buf
[buf_len
], 6, "/%hhu", e
[i
].length
);
145 if (!IN6_ARE_ADDR_EQUAL(&any
, &e
[i
].router
)) {
146 buf
[buf_len
++] = '@';
147 inet_ntop(AF_INET6
, &e
[i
].router
, &buf
[buf_len
], INET6_ADDRSTRLEN
);
148 buf_len
+= strlen(&buf
[buf_len
]);
150 buf_len
+= snprintf(&buf
[buf_len
], 24, ",%u,%u", e
[i
].preferred
, e
[i
].valid
);
152 buf_len
+= snprintf(&buf
[buf_len
], 12, ",%u", e
[i
].priority
);
153 buf
[buf_len
++] = ' ';
156 buf
[buf_len
- 1] = '\0';
161 void script_call(const char *status
)
163 syslog(LOG_WARNING
, "State for %s changed to %s", argv
[1], status
);
164 size_t dns_len
, search_len
, custom_len
, sntp_ip_len
, sntp_dns_len
;
165 size_t sip_ip_len
, sip_fqdn_len
;
167 struct in6_addr
*dns
= odhcp6c_get_state(STATE_DNS
, &dns_len
);
168 uint8_t *search
= odhcp6c_get_state(STATE_SEARCH
, &search_len
);
169 uint8_t *custom
= odhcp6c_get_state(STATE_CUSTOM_OPTS
, &custom_len
);
170 struct in6_addr
*sntp
= odhcp6c_get_state(STATE_SNTP_IP
, &sntp_ip_len
);
171 uint8_t *sntp_dns
= odhcp6c_get_state(STATE_SNTP_FQDN
, &sntp_dns_len
);
172 struct in6_addr
*sip
= odhcp6c_get_state(STATE_SIP_IP
, &sip_ip_len
);
173 uint8_t *sip_fqdn
= odhcp6c_get_state(STATE_SIP_FQDN
, &sip_fqdn_len
);
175 size_t prefix_len
, address_len
, ra_pref_len
, ra_route_len
;
176 uint8_t *prefix
= odhcp6c_get_state(STATE_IA_PD
, &prefix_len
);
177 uint8_t *address
= odhcp6c_get_state(STATE_IA_NA
, &address_len
);
178 uint8_t *ra_pref
= odhcp6c_get_state(STATE_RA_PREFIX
, &ra_pref_len
);
179 uint8_t *ra_route
= odhcp6c_get_state(STATE_RA_ROUTE
, &ra_route_len
);
181 // Don't set environment before forking, because env is leaky.
183 ipv6_to_env("RDNSS", dns
, dns_len
/ sizeof(*dns
));
184 ipv6_to_env("SNTP_IP", sntp
, sntp_ip_len
/ sizeof(*sntp
));
185 ipv6_to_env("SIP_IP", sip
, sip_ip_len
/ sizeof(*sip
));
186 fqdn_to_env("DOMAINS", search
, search_len
);
187 fqdn_to_env("SNTP_FQDN", sntp_dns
, sntp_dns_len
);
188 fqdn_to_env("SIP_DOMAIN", sip_fqdn
, sip_fqdn_len
);
189 bin_to_env(custom
, custom_len
);
190 entry_to_env("PREFIXES", prefix
, prefix_len
);
191 entry_to_env("ADDRESSES", address
, address_len
);
192 entry_to_env("RA_ADDRESSES", ra_pref
, ra_pref_len
);
193 entry_to_env("RA_ROUTES", ra_route
, ra_route_len
);
195 argv
[2] = (char*)status
;
196 execv(argv
[0], argv
);
200 // Delete lost prefixes and user opts
201 odhcp6c_clear_state(STATE_CUSTOM_OPTS
);