6 #include <netinet/in.h>
10 #include "interface-ip.h"
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
},
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
},
36 static const struct config_param_list static_attr_list
= {
37 .n_params
= __OPT_MAX
,
38 .params
= static_attrs
,
39 .info
= static_attr_info
,
42 struct static_proto_state
{
43 struct interface_proto_state proto
;
45 struct blob_attr
*config
;
49 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
)
51 struct device_addr
*addr
;
53 addr
= proto_parse_ip_addr_string(str
, v6
, mask
);
55 interface_add_error(iface
, "proto-static", "INVALID_ADDRESS", &str
, 1);
58 vlist_add(&iface
->proto_addr
, &addr
->node
);
63 parse_address_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
, int netmask
)
65 struct blob_attr
*cur
;
69 blobmsg_for_each_attr(cur
, attr
, rem
) {
71 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
))
79 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
81 struct device_route
*route
;
82 const char *str
= blobmsg_data(attr
);
83 int af
= v6
? AF_INET6
: AF_INET
;
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);
93 route
->flags
= DEVADDR_DEVICE
| (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
94 vlist_add(&iface
->proto_route
, &route
->node
);
100 proto_apply_static_settings(struct interface
*iface
, struct blob_attr
*attr
)
102 struct blob_attr
*tb
[__OPT_MAX
];
106 int n_v4
= 0, n_v6
= 0;
108 blobmsg_parse(static_attrs
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
110 if (tb
[OPT_NETMASK
]) {
111 if (!inet_aton(blobmsg_data(tb
[OPT_NETMASK
]), &ina
)) {
112 error
= "INVALID_NETMASK";
116 netmask
= 32 - fls(~(ntohl(ina
.s_addr
)));
120 n_v4
= parse_address_option(iface
, tb
[OPT_IPADDR
], false, netmask
);
123 n_v6
= parse_address_option(iface
, tb
[OPT_IP6ADDR
], true, netmask
);
125 if (!n_v4
&& !n_v6
) {
126 error
= "NO_ADDRESS";
130 if (n_v4
< 0 || n_v6
< 0)
133 if (n_v4
&& tb
[OPT_GATEWAY
]) {
134 if (!parse_gateway_option(iface
, tb
[OPT_GATEWAY
], false))
138 if (n_v6
&& tb
[OPT_IP6GW
]) {
139 if (!parse_gateway_option(iface
, tb
[OPT_IP6GW
], true))
146 interface_add_error(iface
, "proto-static", error
, NULL
, 0);
152 static_proto_setup(struct static_proto_state
*state
)
154 return proto_apply_static_settings(state
->proto
.iface
, state
->config
) == 0;
158 static_handler(struct interface_proto_state
*proto
,
159 enum interface_proto_cmd cmd
, bool force
)
161 struct static_proto_state
*state
;
164 state
= container_of(proto
, struct static_proto_state
, proto
);
167 case PROTO_CMD_SETUP
:
168 if (!static_proto_setup(state
))
172 case PROTO_CMD_TEARDOWN
:
179 static_free(struct interface_proto_state
*proto
)
181 struct static_proto_state
*state
;
183 state
= container_of(proto
, struct static_proto_state
, proto
);
188 struct interface_proto_state
*
189 static_attach(const struct proto_handler
*h
, struct interface
*iface
,
190 struct blob_attr
*attr
)
192 struct static_proto_state
*state
;
194 state
= calloc(1, sizeof(*state
));
198 state
->config
= malloc(blob_pad_len(attr
));
202 memcpy(state
->config
, attr
, blob_pad_len(attr
));
203 state
->proto
.free
= static_free
;
204 state
->proto
.cb
= static_handler
;
206 return &state
->proto
;
213 static struct proto_handler static_proto
= {
215 .flags
= PROTO_FLAG_IMMEDIATE
,
216 .config_params
= &static_attr_list
,
217 .attach
= static_attach
,
221 static_proto_init(void)
223 add_proto_handler(&static_proto
);