BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file net_ipv6_wait_dynamic_addr.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Synopsis: |
||
32 | * net.ipv6.wait_dynamic_addr(string ifname) |
||
33 | * |
||
34 | * Description: |
||
35 | * Waits for a dynamic IPv6 address to be obtained on the interface, |
||
36 | * and goes up when it is obtained. |
||
37 | * If the address is subsequently lost, goes back down and again waits |
||
38 | * for an address. |
||
39 | * |
||
40 | * Variables: |
||
41 | * string addr - dynamic address obtained on the interface |
||
42 | * string prefix - prefix length |
||
43 | * string cidr_addr - address and prefix (addr/prefix) |
||
44 | */ |
||
45 | |||
46 | #include <stdlib.h> |
||
47 | #include <string.h> |
||
48 | #include <stdio.h> |
||
49 | |||
50 | #include <misc/get_iface_info.h> |
||
51 | #include <misc/ipaddr6.h> |
||
52 | #include <ncd/extra/NCDInterfaceMonitor.h> |
||
53 | |||
54 | #include <ncd/module_common.h> |
||
55 | |||
56 | #include <generated/blog_channel_ncd_net_ipv6_wait_dynamic_addr.h> |
||
57 | |||
58 | struct instance { |
||
59 | NCDModuleInst *i; |
||
60 | NCDInterfaceMonitor monitor; |
||
61 | struct ipv6_ifaddr ifaddr; |
||
62 | int up; |
||
63 | }; |
||
64 | |||
65 | static void instance_free (struct instance *o, int is_error); |
||
66 | |||
67 | static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event) |
||
68 | { |
||
69 | if (!o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED && (event.u.ipv6_addr.addr_flags & NCDIFMONITOR_ADDR_FLAG_DYNAMIC)) { |
||
70 | // rememeber address, set up |
||
71 | o->ifaddr = event.u.ipv6_addr.addr; |
||
72 | o->up = 1; |
||
73 | |||
74 | // signal up |
||
75 | NCDModuleInst_Backend_Up(o->i); |
||
76 | } |
||
77 | else if (o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED && !memcmp(event.u.ipv6_addr.addr.addr.bytes, o->ifaddr.addr.bytes, 16) && event.u.ipv6_addr.addr.prefix == o->ifaddr.prefix) { |
||
78 | // set not up |
||
79 | o->up = 0; |
||
80 | |||
81 | // signal down |
||
82 | NCDModuleInst_Backend_Down(o->i); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | static void monitor_handler_error (struct instance *o) |
||
87 | { |
||
88 | ModuleLog(o->i, BLOG_ERROR, "monitor error"); |
||
89 | |||
90 | instance_free(o, 1); |
||
91 | } |
||
92 | |||
93 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
94 | { |
||
95 | struct instance *o = vo; |
||
96 | o->i = i; |
||
97 | |||
98 | // read arguments |
||
99 | NCDValRef ifname_arg; |
||
100 | if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) { |
||
101 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
102 | goto fail0; |
||
103 | } |
||
104 | if (!NCDVal_IsStringNoNulls(ifname_arg)) { |
||
105 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
106 | goto fail0; |
||
107 | } |
||
108 | |||
109 | // null terminate ifname |
||
110 | NCDValNullTermString ifname_nts; |
||
111 | if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) { |
||
112 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
113 | goto fail0; |
||
114 | } |
||
115 | |||
116 | // get interface index |
||
117 | int ifindex; |
||
118 | int res = badvpn_get_iface_info(ifname_nts.data, NULL, NULL, &ifindex); |
||
119 | NCDValNullTermString_Free(&ifname_nts); |
||
120 | if (!res) { |
||
121 | ModuleLog(o->i, BLOG_ERROR, "failed to get interface index"); |
||
122 | goto fail0; |
||
123 | } |
||
124 | |||
125 | // init monitor |
||
126 | if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_IPV6_ADDR, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) { |
||
127 | ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed"); |
||
128 | goto fail0; |
||
129 | } |
||
130 | |||
131 | // set not up |
||
132 | o->up = 0; |
||
133 | return; |
||
134 | |||
135 | fail0: |
||
136 | NCDModuleInst_Backend_DeadError(i); |
||
137 | } |
||
138 | |||
139 | static void instance_free (struct instance *o, int is_error) |
||
140 | { |
||
141 | // free monitor |
||
142 | NCDInterfaceMonitor_Free(&o->monitor); |
||
143 | |||
144 | if (is_error) { |
||
145 | NCDModuleInst_Backend_DeadError(o->i); |
||
146 | } else { |
||
147 | NCDModuleInst_Backend_Dead(o->i); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | static void func_die (void *vo) |
||
152 | { |
||
153 | struct instance *o = vo; |
||
154 | instance_free(o, 0); |
||
155 | } |
||
156 | |||
157 | static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) |
||
158 | { |
||
159 | struct instance *o = vo; |
||
160 | ASSERT(o->up) |
||
161 | |||
162 | if (!strcmp(name, "addr")) { |
||
163 | char str[IPADDR6_PRINT_MAX]; |
||
164 | ipaddr6_print_addr(o->ifaddr.addr, str); |
||
165 | *out = NCDVal_NewString(mem, str); |
||
166 | return 1; |
||
167 | } |
||
168 | |||
169 | if (!strcmp(name, "prefix")) { |
||
170 | char str[10]; |
||
171 | sprintf(str, "%d", o->ifaddr.prefix); |
||
172 | *out = NCDVal_NewString(mem, str); |
||
173 | return 1; |
||
174 | } |
||
175 | |||
176 | if (!strcmp(name, "cidr_addr")) { |
||
177 | char str[IPADDR6_PRINT_MAX]; |
||
178 | ipaddr6_print_ifaddr(o->ifaddr, str); |
||
179 | *out = NCDVal_NewString(mem, str); |
||
180 | return 1; |
||
181 | } |
||
182 | |||
183 | return 0; |
||
184 | } |
||
185 | |||
186 | static struct NCDModule modules[] = { |
||
187 | { |
||
188 | .type = "net.ipv6.wait_dynamic_addr", |
||
189 | .func_new2 = func_new, |
||
190 | .func_die = func_die, |
||
191 | .func_getvar = func_getvar, |
||
192 | .alloc_size = sizeof(struct instance) |
||
193 | }, { |
||
194 | .type = NULL |
||
195 | } |
||
196 | }; |
||
197 | |||
198 | const struct NCDModuleGroup ncdmodule_net_ipv6_wait_dynamic_addr = { |
||
199 | .modules = modules |
||
200 | }; |