directly pass the device name to the device create function
[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         const char *error;
107         unsigned int netmask = 32;
108         int n_v4 = 0, n_v6 = 0;
109
110         blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
111
112         if (tb[OPT_NETMASK]) {
113                 netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
114                 if (netmask > 32) {
115                         error = "INVALID_NETMASK";
116                         goto error;
117                 }
118         }
119
120         if (tb[OPT_IPADDR])
121                 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask);
122
123         if (tb[OPT_IP6ADDR])
124                 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask);
125
126         if (!n_v4 && !n_v6) {
127                 error = "NO_ADDRESS";
128                 goto error;
129         }
130
131         if (n_v4 < 0 || n_v6 < 0)
132                 goto out;
133
134         if (n_v4 && tb[OPT_GATEWAY]) {
135                 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
136                         goto out;
137         }
138
139         if (n_v6 && tb[OPT_IP6GW]) {
140                 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
141                         goto out;
142         }
143
144         if (tb[OPT_DNS])
145                 interface_add_dns_server_list(iface, tb[OPT_DNS]);
146
147         return 0;
148
149 error:
150         interface_add_error(iface, "proto-static", error, NULL, 0);
151 out:
152         return -1;
153 }
154
155 static bool
156 static_proto_setup(struct static_proto_state *state)
157 {
158         return proto_apply_static_settings(state->proto.iface, state->config) == 0;
159 }
160
161 static int
162 static_handler(struct interface_proto_state *proto,
163                enum interface_proto_cmd cmd, bool force)
164 {
165         struct static_proto_state *state;
166         int ret = 0;
167
168         state = container_of(proto, struct static_proto_state, proto);
169
170         switch (cmd) {
171         case PROTO_CMD_SETUP:
172                 if (!static_proto_setup(state))
173                         return -1;
174
175                 break;
176         case PROTO_CMD_TEARDOWN:
177                 break;
178         }
179         return ret;
180 }
181
182 static void
183 static_free(struct interface_proto_state *proto)
184 {
185         struct static_proto_state *state;
186
187         state = container_of(proto, struct static_proto_state, proto);
188         free(state->config);
189         free(state);
190 }
191
192 struct interface_proto_state *
193 static_attach(const struct proto_handler *h, struct interface *iface,
194               struct blob_attr *attr)
195 {
196         struct static_proto_state *state;
197
198         state = calloc(1, sizeof(*state));
199         if (!state)
200                 return NULL;
201
202         state->config = malloc(blob_pad_len(attr));
203         if (!state->config)
204                 goto error;
205
206         memcpy(state->config, attr, blob_pad_len(attr));
207         state->proto.free = static_free;
208         state->proto.cb = static_handler;
209
210         return &state->proto;
211
212 error:
213         free(state);
214         return NULL;
215 }
216
217 static struct proto_handler static_proto = {
218         .name = "static",
219         .flags = PROTO_FLAG_IMMEDIATE,
220         .config_params = &static_attr_list,
221         .attach = static_attach,
222 };
223
224 static void __init
225 static_proto_init(void)
226 {
227         add_proto_handler(&static_proto);
228 }