6 #include <netinet/in.h>
10 #include "interface-ip.h"
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 },
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 },
39 static const struct config_param_list static_attr_list = {
40 .n_params = __OPT_MAX,
41 .params = static_attrs,
42 .info = static_attr_info,
45 struct static_proto_state {
46 struct interface_proto_state proto;
48 struct blob_attr *config;
52 parse_addr(struct interface *iface, const char *str, bool v6, int mask)
54 struct device_addr *addr;
56 addr = proto_parse_ip_addr_string(str, v6, mask);
58 interface_add_error(iface, "proto-static", "INVALID_ADDRESS", &str, 1);
61 vlist_add(&iface->proto_addr, &addr->node);
66 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask)
68 struct blob_attr *cur;
72 blobmsg_for_each_attr(cur, attr, rem) {
74 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask))
82 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
84 struct device_route *route;
85 const char *str = blobmsg_data(attr);
86 int af = v6 ? AF_INET6 : AF_INET;
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);
96 route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
97 vlist_add(&iface->proto_route, &route->node);
103 proto_apply_static_settings(struct interface *iface, struct blob_attr *attr)
105 struct blob_attr *tb[__OPT_MAX];
109 int n_v4 = 0, n_v6 = 0;
111 blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
113 if (tb[OPT_NETMASK]) {
114 if (!inet_aton(blobmsg_data(tb[OPT_NETMASK]), &ina)) {
115 error = "INVALID_NETMASK";
119 netmask = 32 - fls(~(ntohl(ina.s_addr)));
123 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask);
126 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask);
128 if (!n_v4 && !n_v6) {
129 error = "NO_ADDRESS";
133 if (n_v4 < 0 || n_v6 < 0)
136 if (n_v4 && tb[OPT_GATEWAY]) {
137 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
141 if (n_v6 && tb[OPT_IP6GW]) {
142 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
147 interface_add_dns_server_list(iface, tb[OPT_DNS]);
152 interface_add_error(iface, "proto-static", error, NULL, 0);
158 static_proto_setup(struct static_proto_state *state)
160 return proto_apply_static_settings(state->proto.iface, state->config) == 0;
164 static_handler(struct interface_proto_state *proto,
165 enum interface_proto_cmd cmd, bool force)
167 struct static_proto_state *state;
170 state = container_of(proto, struct static_proto_state, proto);
173 case PROTO_CMD_SETUP:
174 if (!static_proto_setup(state))
178 case PROTO_CMD_TEARDOWN:
185 static_free(struct interface_proto_state *proto)
187 struct static_proto_state *state;
189 state = container_of(proto, struct static_proto_state, proto);
194 struct interface_proto_state *
195 static_attach(const struct proto_handler *h, struct interface *iface,
196 struct blob_attr *attr)
198 struct static_proto_state *state;
200 state = calloc(1, sizeof(*state));
204 state->config = malloc(blob_pad_len(attr));
208 memcpy(state->config, attr, blob_pad_len(attr));
209 state->proto.free = static_free;
210 state->proto.cb = static_handler;
212 return &state->proto;
219 static struct proto_handler static_proto = {
221 .flags = PROTO_FLAG_IMMEDIATE,
222 .config_params = &static_attr_list,
223 .attach = static_attach,
227 static_proto_init(void)
229 add_proto_handler(&static_proto);