add support for killing running proto-shell tasks with an arbitrary signal and waitin...
[project/netifd.git] / proto-static.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7
8 #include "netifd.h"
9 #include "interface.h"
10 #include "interface-ip.h"
11 #include "proto.h"
12 #include "system.h"
13
14 enum {
15 OPT_IPADDR,
16 OPT_IP6ADDR,
17 OPT_NETMASK,
18 OPT_GATEWAY,
19 OPT_IP6GW,
20 OPT_DNS,
21 __OPT_MAX,
22 };
23
24 static const struct blobmsg_policy static_attrs[__OPT_MAX] = {
25 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
26 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
27 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
28 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
29 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
30 [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
31 };
32
33 static const union config_param_info static_attr_info[__OPT_MAX] = {
34 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
35 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
36 [OPT_DNS] = { .type = BLOBMSG_TYPE_STRING },
37 };
38
39 static const struct config_param_list static_attr_list = {
40 .n_params = __OPT_MAX,
41 .params = static_attrs,
42 .info = static_attr_info,
43 };
44
45 struct static_proto_state {
46 struct interface_proto_state proto;
47
48 struct blob_attr *config;
49 };
50
51 static bool
52 parse_addr(struct interface *iface, const char *str, bool v6, int mask)
53 {
54 struct device_addr *addr;
55
56 addr = proto_parse_ip_addr_string(str, v6, mask);
57 if (!addr) {
58 interface_add_error(iface, "proto-static", "INVALID_ADDRESS", &str, 1);
59 return false;
60 }
61 vlist_add(&iface->proto_addr, &addr->node);
62 return true;
63 }
64
65 static int
66 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask)
67 {
68 struct blob_attr *cur;
69 int n_addr = 0;
70 int rem;
71
72 blobmsg_for_each_attr(cur, attr, rem) {
73 n_addr++;
74 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask))
75 return -1;
76 }
77
78 return n_addr;
79 }
80
81 static bool
82 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
83 {
84 struct device_route *route;
85 const char *str = blobmsg_data(attr);
86 int af = v6 ? AF_INET6 : AF_INET;
87
88 route = calloc(1, sizeof(*route));
89 if (!inet_pton(af, str, &route->nexthop)) {
90 interface_add_error(iface, "proto-static",
91 "INVALID_GATEWAY", &str, 1);
92 free(route);
93 return false;
94 }
95 route->mask = 0;
96 route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
97 vlist_add(&iface->proto_route, &route->node);
98
99 return true;
100 }
101
102 static int
103 proto_apply_static_settings(struct interface *iface, struct blob_attr *attr)
104 {
105 struct blob_attr *tb[__OPT_MAX];
106 struct in_addr ina;
107 const char *error;
108 int netmask = 32;
109 int n_v4 = 0, n_v6 = 0;
110
111 blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
112
113 if (tb[OPT_NETMASK]) {
114 if (!inet_aton(blobmsg_data(tb[OPT_NETMASK]), &ina)) {
115 error = "INVALID_NETMASK";
116 goto error;
117 }
118
119 netmask = 32 - fls(~(ntohl(ina.s_addr)));
120 }
121
122 if (tb[OPT_IPADDR])
123 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask);
124
125 if (tb[OPT_IP6ADDR])
126 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask);
127
128 if (!n_v4 && !n_v6) {
129 error = "NO_ADDRESS";
130 goto error;
131 }
132
133 if (n_v4 < 0 || n_v6 < 0)
134 goto out;
135
136 if (n_v4 && tb[OPT_GATEWAY]) {
137 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
138 goto out;
139 }
140
141 if (n_v6 && tb[OPT_IP6GW]) {
142 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
143 goto out;
144 }
145
146 if (tb[OPT_DNS])
147 interface_add_dns_server_list(iface, tb[OPT_DNS]);
148
149 return 0;
150
151 error:
152 interface_add_error(iface, "proto-static", error, NULL, 0);
153 out:
154 return -1;
155 }
156
157 static bool
158 static_proto_setup(struct static_proto_state *state)
159 {
160 return proto_apply_static_settings(state->proto.iface, state->config) == 0;
161 }
162
163 static int
164 static_handler(struct interface_proto_state *proto,
165 enum interface_proto_cmd cmd, bool force)
166 {
167 struct static_proto_state *state;
168 int ret = 0;
169
170 state = container_of(proto, struct static_proto_state, proto);
171
172 switch (cmd) {
173 case PROTO_CMD_SETUP:
174 if (!static_proto_setup(state))
175 return -1;
176
177 break;
178 case PROTO_CMD_TEARDOWN:
179 break;
180 }
181 return ret;
182 }
183
184 static void
185 static_free(struct interface_proto_state *proto)
186 {
187 struct static_proto_state *state;
188
189 state = container_of(proto, struct static_proto_state, proto);
190 free(state->config);
191 free(state);
192 }
193
194 struct interface_proto_state *
195 static_attach(const struct proto_handler *h, struct interface *iface,
196 struct blob_attr *attr)
197 {
198 struct static_proto_state *state;
199
200 state = calloc(1, sizeof(*state));
201 if (!state)
202 return NULL;
203
204 state->config = malloc(blob_pad_len(attr));
205 if (!state->config)
206 goto error;
207
208 memcpy(state->config, attr, blob_pad_len(attr));
209 state->proto.free = static_free;
210 state->proto.cb = static_handler;
211
212 return &state->proto;
213
214 error:
215 free(state);
216 return NULL;
217 }
218
219 static struct proto_handler static_proto = {
220 .name = "static",
221 .flags = PROTO_FLAG_IMMEDIATE,
222 .config_params = &static_attr_list,
223 .attach = static_attach,
224 };
225
226 static void __init
227 static_proto_init(void)
228 {
229 add_proto_handler(&static_proto);
230 }