BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file net_ipv4_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 | * IPv4 address module. |
||
32 | * |
||
33 | * Synopsis: |
||
34 | * net.ipv4.addr(string ifname, string addr, string prefix) |
||
35 | * net.ipv4.addr(string ifname, string cidr_addr) |
||
36 | * |
||
37 | * Description: |
||
38 | * Adds the given address to the given network interface on initialization, |
||
39 | * and removes it on deinitialization. The second form takes the address and |
||
40 | * prefix in CIDR notation (a.b.c.d/n). |
||
41 | */ |
||
42 | |||
43 | #include <stdlib.h> |
||
44 | #include <string.h> |
||
45 | |||
46 | #include <ncd/extra/NCDIfConfig.h> |
||
47 | |||
48 | #include <ncd/module_common.h> |
||
49 | |||
50 | #include <generated/blog_channel_ncd_net_ipv4_addr.h> |
||
51 | |||
52 | struct instance { |
||
53 | NCDModuleInst *i; |
||
54 | NCDValNullTermString ifname_nts; |
||
55 | struct ipv4_ifaddr ifaddr; |
||
56 | }; |
||
57 | |||
58 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
59 | { |
||
60 | struct instance *o = vo; |
||
61 | o->i = i; |
||
62 | |||
63 | // read arguments |
||
64 | NCDValRef ifname_arg; |
||
65 | NCDValRef addr_arg; |
||
66 | NCDValRef prefix_arg = NCDVal_NewInvalid(); |
||
67 | if (!NCDVal_ListRead(params->args, 2, &ifname_arg, &addr_arg) && |
||
68 | !NCDVal_ListRead(params->args, 3, &ifname_arg, &addr_arg, &prefix_arg) |
||
69 | ) { |
||
70 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
71 | goto fail0; |
||
72 | } |
||
73 | if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsString(addr_arg) || |
||
74 | (!NCDVal_IsInvalid(prefix_arg) && !NCDVal_IsString(prefix_arg)) |
||
75 | ) { |
||
76 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
77 | goto fail0; |
||
78 | } |
||
79 | |||
80 | // null terminate ifname |
||
81 | if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) { |
||
82 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
83 | goto fail0; |
||
84 | } |
||
85 | |||
86 | if (NCDVal_IsInvalid(prefix_arg)) { |
||
87 | if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(addr_arg), &o->ifaddr)) { |
||
88 | ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation address"); |
||
89 | goto fail1; |
||
90 | } |
||
91 | } else { |
||
92 | if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(addr_arg), &o->ifaddr.addr)) { |
||
93 | ModuleLog(o->i, BLOG_ERROR, "wrong address"); |
||
94 | goto fail1; |
||
95 | } |
||
96 | |||
97 | if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(prefix_arg), &o->ifaddr.prefix)) { |
||
98 | ModuleLog(o->i, BLOG_ERROR, "wrong prefix"); |
||
99 | goto fail1; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // add address |
||
104 | if (!NCDIfConfig_add_ipv4_addr(o->ifname_nts.data, o->ifaddr)) { |
||
105 | ModuleLog(o->i, BLOG_ERROR, "failed to add IP address"); |
||
106 | goto fail1; |
||
107 | } |
||
108 | |||
109 | // signal up |
||
110 | NCDModuleInst_Backend_Up(o->i); |
||
111 | return; |
||
112 | |||
113 | fail1: |
||
114 | NCDValNullTermString_Free(&o->ifname_nts); |
||
115 | fail0: |
||
116 | NCDModuleInst_Backend_DeadError(i); |
||
117 | } |
||
118 | |||
119 | static void func_die (void *vo) |
||
120 | { |
||
121 | struct instance *o = vo; |
||
122 | |||
123 | // remove address |
||
124 | if (!NCDIfConfig_remove_ipv4_addr(o->ifname_nts.data, o->ifaddr)) { |
||
125 | ModuleLog(o->i, BLOG_ERROR, "failed to remove IP address"); |
||
126 | } |
||
127 | |||
128 | // free ifname nts |
||
129 | NCDValNullTermString_Free(&o->ifname_nts); |
||
130 | |||
131 | NCDModuleInst_Backend_Dead(o->i); |
||
132 | } |
||
133 | |||
134 | static struct NCDModule modules[] = { |
||
135 | { |
||
136 | .type = "net.ipv4.addr", |
||
137 | .func_new2 = func_new, |
||
138 | .func_die = func_die, |
||
139 | .alloc_size = sizeof(struct instance) |
||
140 | }, { |
||
141 | .type = NULL |
||
142 | } |
||
143 | }; |
||
144 | |||
145 | const struct NCDModuleGroup ncdmodule_net_ipv4_addr = { |
||
146 | .modules = modules |
||
147 | }; |