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