BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file net_ipv4_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 * IPv4 route module.
32 *
33 * Synopsis:
34 * net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
35 * net.ipv4.route(string cidr_dest, string gateway, string metric, string ifname)
36 *
37 * Description:
38 * Adds an IPv4 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 (a.b.c.d/n).
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 */
44  
45 #include <stdlib.h>
46 #include <string.h>
47 #include <limits.h>
48  
49 #include <misc/debug.h>
50 #include <ncd/extra/NCDIfConfig.h>
51  
52 #include <ncd/module_common.h>
53  
54 #include <generated/blog_channel_ncd_net_ipv4_route.h>
55  
56 #define TYPE_NORMAL 1
57 #define TYPE_IFONLY 2
58 #define TYPE_BLACKHOLE 3
59  
60 struct instance {
61 NCDModuleInst *i;
62 struct ipv4_ifaddr dest;
63 int type;
64 uint32_t gateway;
65 int metric;
66 NCDValNullTermString ifname_nts;
67 };
68  
69 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
70 {
71 struct instance *o = vo;
72 o->i = i;
73  
74 // read arguments
75 NCDValRef dest_arg;
76 NCDValRef dest_prefix_arg = NCDVal_NewInvalid();
77 NCDValRef gateway_arg;
78 NCDValRef metric_arg;
79 NCDValRef ifname_arg;
80 if (!NCDVal_ListRead(params->args, 4, &dest_arg, &gateway_arg, &metric_arg, &ifname_arg) &&
81 !NCDVal_ListRead(params->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)
82 ) {
83 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
84 goto fail0;
85 }
86 if (!NCDVal_IsString(dest_arg) || !NCDVal_IsString(gateway_arg) ||
87 !NCDVal_IsStringNoNulls(ifname_arg) ||
88 (!NCDVal_IsInvalid(dest_prefix_arg) && !NCDVal_IsString(dest_prefix_arg))
89 ) {
90 ModuleLog(o->i, BLOG_ERROR, "wrong type");
91 goto fail0;
92 }
93  
94 // read dest
95 if (NCDVal_IsInvalid(dest_prefix_arg)) {
96 if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(dest_arg), &o->dest)) {
97 ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation dest");
98 goto fail0;
99 }
100 } else {
101 if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(dest_arg), &o->dest.addr)) {
102 ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
103 goto fail0;
104 }
105 if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(dest_prefix_arg), &o->dest.prefix)) {
106 ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
107 goto fail0;
108 }
109 }
110  
111 // read gateway and choose type
112 if (NCDVal_StringEquals(gateway_arg, "none")) {
113 o->type = TYPE_IFONLY;
114 }
115 else if (NCDVal_StringEquals(gateway_arg, "blackhole")) {
116 o->type = TYPE_BLACKHOLE;
117 } else {
118 if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(gateway_arg), &o->gateway)) {
119 ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
120 goto fail0;
121 }
122 o->type = TYPE_NORMAL;
123 }
124  
125 // read metric
126 uintmax_t metric;
127 if (!ncd_read_uintmax(metric_arg, &metric) || metric > INT_MAX) {
128 ModuleLog(i, BLOG_ERROR, "bad metric");
129 goto fail0;
130 }
131 o->metric = metric;
132  
133 // null terminate ifname
134 if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) {
135 ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
136 goto fail0;
137 }
138  
139 // add route
140 int res = 0; // to remove warning
141 switch (o->type) {
142 case TYPE_NORMAL:
143 res = NCDIfConfig_add_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data);
144 break;
145 case TYPE_IFONLY:
146 res = NCDIfConfig_add_ipv4_route(o->dest, NULL, o->metric, o->ifname_nts.data);
147 break;
148 case TYPE_BLACKHOLE:
149 res = NCDIfConfig_add_ipv4_blackhole_route(o->dest, o->metric);
150 break;
151 default: ASSERT(0);
152 }
153 if (!res) {
154 ModuleLog(o->i, BLOG_ERROR, "failed to add route");
155 goto fail1;
156 }
157  
158 // signal up
159 NCDModuleInst_Backend_Up(o->i);
160 return;
161  
162 fail1:
163 NCDValNullTermString_Free(&o->ifname_nts);
164 fail0:
165 NCDModuleInst_Backend_DeadError(i);
166 }
167  
168 static void func_die (void *vo)
169 {
170 struct instance *o = vo;
171  
172 // remove route
173 int res = 0; // to remove warning
174 switch (o->type) {
175 case TYPE_NORMAL:
176 res = NCDIfConfig_remove_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data);
177 break;
178 case TYPE_IFONLY:
179 res = NCDIfConfig_remove_ipv4_route(o->dest, NULL, o->metric, o->ifname_nts.data);
180 break;
181 case TYPE_BLACKHOLE:
182 res = NCDIfConfig_remove_ipv4_blackhole_route(o->dest, o->metric);
183 break;
184 default: ASSERT(0);
185 }
186 if (!res) {
187 ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
188 }
189  
190 // free ifname nts
191 NCDValNullTermString_Free(&o->ifname_nts);
192  
193 NCDModuleInst_Backend_Dead(o->i);
194 }
195  
196 static struct NCDModule modules[] = {
197 {
198 .type = "net.ipv4.route",
199 .func_new2 = func_new,
200 .func_die = func_die,
201 .alloc_size = sizeof(struct instance)
202 }, {
203 .type = NULL
204 }
205 };
206  
207 const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
208 .modules = modules
209 };