BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file net_ipv6_route.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 | * IPv6 route module. |
||
32 | * |
||
33 | * Synopsis: |
||
34 | * net.ipv6.route(string dest, string dest_prefix, string gateway, string metric, string ifname) |
||
35 | * net.ipv6.route(string cidr_dest, string gateway, string metric, string ifname) |
||
36 | * |
||
37 | * Description: |
||
38 | * Adds an IPv6 route to the system's routing table on initiailzation, and |
||
39 | * removes it on deinitialization. The second form takes the destination in |
||
40 | * CIDR notation (address/prefix). |
||
41 | * If 'gateway' is "none", the route will only be associated with an interface. |
||
42 | * If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused). |
||
43 | * NOTE: blackhole routes for IPv6 are not yet implemented in Linux; |
||
44 | * adding them via this interface will only work once they |
||
45 | * have been. |
||
46 | */ |
||
47 | |||
48 | #include <stdlib.h> |
||
49 | #include <string.h> |
||
50 | |||
51 | #include <misc/debug.h> |
||
52 | #include <ncd/extra/NCDIfConfig.h> |
||
53 | |||
54 | #include <ncd/module_common.h> |
||
55 | |||
56 | #include <generated/blog_channel_ncd_net_ipv6_route.h> |
||
57 | |||
58 | #define TYPE_NORMAL 1 |
||
59 | #define TYPE_IFONLY 2 |
||
60 | #define TYPE_BLACKHOLE 3 |
||
61 | |||
62 | struct instance { |
||
63 | NCDModuleInst *i; |
||
64 | struct ipv6_ifaddr dest; |
||
65 | int type; |
||
66 | struct ipv6_addr gateway; |
||
67 | int metric; |
||
68 | NCDValNullTermString ifname_nts; |
||
69 | }; |
||
70 | |||
71 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
72 | { |
||
73 | struct instance *o = vo; |
||
74 | o->i = i; |
||
75 | |||
76 | // read arguments |
||
77 | NCDValRef dest_arg; |
||
78 | NCDValRef dest_prefix_arg = NCDVal_NewInvalid(); |
||
79 | NCDValRef gateway_arg; |
||
80 | NCDValRef metric_arg; |
||
81 | NCDValRef ifname_arg; |
||
82 | if (!NCDVal_ListRead(params->args, 4, &dest_arg, &gateway_arg, &metric_arg, &ifname_arg) && |
||
83 | !NCDVal_ListRead(params->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg) |
||
84 | ) { |
||
85 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
86 | goto fail0; |
||
87 | } |
||
88 | if (!NCDVal_IsString(dest_arg) || !NCDVal_IsString(gateway_arg) || |
||
89 | !NCDVal_IsStringNoNulls(ifname_arg) || |
||
90 | (!NCDVal_IsInvalid(dest_prefix_arg) && !NCDVal_IsString(dest_prefix_arg)) |
||
91 | ) { |
||
92 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
93 | goto fail0; |
||
94 | } |
||
95 | |||
96 | // read dest |
||
97 | if (NCDVal_IsInvalid(dest_prefix_arg)) { |
||
98 | if (!ipaddr6_parse_ipv6_ifaddr(NCDVal_StringMemRef(dest_arg), &o->dest)) { |
||
99 | ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation dest"); |
||
100 | goto fail0; |
||
101 | } |
||
102 | } else { |
||
103 | if (!ipaddr6_parse_ipv6_addr(NCDVal_StringMemRef(dest_arg), &o->dest.addr)) { |
||
104 | ModuleLog(o->i, BLOG_ERROR, "wrong dest addr"); |
||
105 | goto fail0; |
||
106 | } |
||
107 | if (!ipaddr6_parse_ipv6_prefix(NCDVal_StringMemRef(dest_prefix_arg), &o->dest.prefix)) { |
||
108 | ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix"); |
||
109 | goto fail0; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // read gateway and choose type |
||
114 | if (NCDVal_StringEquals(gateway_arg, "none")) { |
||
115 | o->type = TYPE_IFONLY; |
||
116 | } |
||
117 | else if (NCDVal_StringEquals(gateway_arg, "blackhole")) { |
||
118 | o->type = TYPE_BLACKHOLE; |
||
119 | } else { |
||
120 | if (!ipaddr6_parse_ipv6_addr(NCDVal_StringMemRef(gateway_arg), &o->gateway)) { |
||
121 | ModuleLog(o->i, BLOG_ERROR, "wrong gateway"); |
||
122 | goto fail0; |
||
123 | } |
||
124 | o->type = TYPE_NORMAL; |
||
125 | } |
||
126 | |||
127 | // read metric |
||
128 | uintmax_t metric; |
||
129 | if (!ncd_read_uintmax(metric_arg, &metric) || metric > INT_MAX) { |
||
130 | ModuleLog(i, BLOG_ERROR, "bad metric"); |
||
131 | goto fail0; |
||
132 | } |
||
133 | o->metric = metric; |
||
134 | |||
135 | // null terminate ifname |
||
136 | if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) { |
||
137 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
138 | goto fail0; |
||
139 | } |
||
140 | |||
141 | // add route |
||
142 | int res = 0; // to remove warning |
||
143 | switch (o->type) { |
||
144 | case TYPE_NORMAL: |
||
145 | res = NCDIfConfig_add_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data); |
||
146 | break; |
||
147 | case TYPE_IFONLY: |
||
148 | res = NCDIfConfig_add_ipv6_route(o->dest, NULL, o->metric, o->ifname_nts.data); |
||
149 | break; |
||
150 | case TYPE_BLACKHOLE: |
||
151 | res = NCDIfConfig_add_ipv6_blackhole_route(o->dest, o->metric); |
||
152 | break; |
||
153 | default: ASSERT(0); |
||
154 | } |
||
155 | if (!res) { |
||
156 | ModuleLog(o->i, BLOG_ERROR, "failed to add route"); |
||
157 | goto fail1; |
||
158 | } |
||
159 | |||
160 | // signal up |
||
161 | NCDModuleInst_Backend_Up(o->i); |
||
162 | return; |
||
163 | |||
164 | fail1: |
||
165 | NCDValNullTermString_Free(&o->ifname_nts); |
||
166 | fail0: |
||
167 | NCDModuleInst_Backend_DeadError(i); |
||
168 | } |
||
169 | |||
170 | static void func_die (void *vo) |
||
171 | { |
||
172 | struct instance *o = vo; |
||
173 | |||
174 | // remove route |
||
175 | int res = 0; // to remove warning |
||
176 | switch (o->type) { |
||
177 | case TYPE_NORMAL: |
||
178 | res = NCDIfConfig_remove_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data); |
||
179 | break; |
||
180 | case TYPE_IFONLY: |
||
181 | res = NCDIfConfig_remove_ipv6_route(o->dest, NULL, o->metric, o->ifname_nts.data); |
||
182 | break; |
||
183 | case TYPE_BLACKHOLE: |
||
184 | res = NCDIfConfig_remove_ipv6_blackhole_route(o->dest, o->metric); |
||
185 | break; |
||
186 | default: ASSERT(0); |
||
187 | } |
||
188 | if (!res) { |
||
189 | ModuleLog(o->i, BLOG_ERROR, "failed to remove route"); |
||
190 | } |
||
191 | |||
192 | // free ifname nts |
||
193 | NCDValNullTermString_Free(&o->ifname_nts); |
||
194 | |||
195 | NCDModuleInst_Backend_Dead(o->i); |
||
196 | } |
||
197 | |||
198 | static struct NCDModule modules[] = { |
||
199 | { |
||
200 | .type = "net.ipv6.route", |
||
201 | .func_new2 = func_new, |
||
202 | .func_die = func_die, |
||
203 | .alloc_size = sizeof(struct instance) |
||
204 | }, { |
||
205 | .type = NULL |
||
206 | } |
||
207 | }; |
||
208 | |||
209 | const struct NCDModuleGroup ncdmodule_net_ipv6_route = { |
||
210 | .modules = modules |
||
211 | }; |