6 #include <netinet/in.h>
10 #include "interface-ip.h"
13 static struct avl_tree handlers
;
26 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
27 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
28 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
29 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
30 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
31 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
32 [OPT_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
33 [OPT_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
36 static const union config_param_info proto_ip_attr_info
[__OPT_MAX
] = {
37 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
38 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
39 [OPT_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
42 const struct config_param_list proto_ip_attr
= {
43 .n_params
= __OPT_MAX
,
44 .params
= proto_ip_attributes
,
45 .info
= proto_ip_attr_info
,
50 parse_netmask_string(const char *str
, bool v6
)
56 if (!strchr(str
, '.')) {
57 ret
= strtoul(str
, &err
, 0);
67 if (inet_aton(str
, &addr
) != 1)
70 return 32 - fls(~(ntohl(addr
.s_addr
)));
77 split_netmask(char *str
, unsigned int *netmask
, bool v6
)
79 char *delim
= strchr(str
, '/');
84 *netmask
= parse_netmask_string(delim
, v6
);
90 parse_ip_and_netmask(int af
, const char *str
, void *addr
, unsigned int *netmask
)
92 char *astr
= alloca(strlen(str
) + 1);
95 if (!split_netmask(astr
, netmask
, af
== AF_INET6
))
106 return inet_pton(af
, astr
, addr
);
109 static struct device_addr
*
110 proto_parse_ip_addr_string(const char *str
, bool v6
, int mask
)
112 struct device_addr
*addr
;
113 int af
= v6
? AF_INET6
: AF_INET
;
115 addr
= calloc(1, sizeof(*addr
));
116 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
118 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
126 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
127 bool ext
, uint32_t broadcast
)
129 struct device_addr
*addr
;
131 addr
= proto_parse_ip_addr_string(str
, v6
, mask
);
133 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
138 addr
->broadcast
= broadcast
;
141 addr
->flags
|= DEVADDR_EXTERNAL
;
143 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
);
148 parse_address_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
149 int netmask
, bool ext
, uint32_t broadcast
)
151 struct blob_attr
*cur
;
155 blobmsg_for_each_attr(cur
, attr
, rem
) {
156 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
160 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
170 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
172 struct device_route
*route
;
173 const char *str
= blobmsg_data(attr
);
174 int af
= v6
? AF_INET6
: AF_INET
;
176 route
= calloc(1, sizeof(*route
));
177 if (!inet_pton(af
, str
, &route
->nexthop
)) {
178 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
184 route
->flags
= DEVADDR_DEVICE
| (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
185 vlist_add(&iface
->proto_ip
.route
, &route
->node
);
191 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
193 struct blob_attr
*tb
[__OPT_MAX
];
195 unsigned int netmask
= 32;
196 int n_v4
= 0, n_v6
= 0;
197 uint32_t broadcast
= 0;
199 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
201 if (tb
[OPT_NETMASK
]) {
202 netmask
= parse_netmask_string(blobmsg_data(tb
[OPT_NETMASK
]), false);
204 error
= "INVALID_NETMASK";
210 n_v4
= parse_address_option(iface
, tb
[OPT_IPADDR
], false,
211 netmask
, ext
, broadcast
);
214 n_v6
= parse_address_option(iface
, tb
[OPT_IP6ADDR
], true,
217 if (!n_v4
&& !n_v6
) {
218 error
= "NO_ADDRESS";
222 if (n_v4
< 0 || n_v6
< 0)
225 if (n_v4
&& tb
[OPT_GATEWAY
]) {
226 if (!parse_gateway_option(iface
, tb
[OPT_GATEWAY
], false))
230 if (n_v6
&& tb
[OPT_IP6GW
]) {
231 if (!parse_gateway_option(iface
, tb
[OPT_IP6GW
], true))
236 interface_add_dns_server_list(&iface
->proto_ip
, tb
[OPT_DNS
]);
238 if (tb
[OPT_DNS_SEARCH
])
239 interface_add_dns_search_list(&iface
->proto_ip
, tb
[OPT_DNS_SEARCH
]);
244 interface_add_error(iface
, "proto", error
, NULL
, 0);
249 void add_proto_handler(struct proto_handler
*p
)
252 avl_init(&handlers
, avl_strcmp
, false, NULL
);
257 p
->avl
.key
= p
->name
;
258 avl_insert(&handlers
, &p
->avl
);
262 default_proto_free(struct interface_proto_state
*proto
)
268 invalid_proto_handler(struct interface_proto_state
*proto
,
269 enum interface_proto_cmd cmd
, bool force
)
275 no_proto_handler(struct interface_proto_state
*proto
,
276 enum interface_proto_cmd cmd
, bool force
)
281 static struct interface_proto_state
*
282 default_proto_attach(const struct proto_handler
*h
,
283 struct interface
*iface
, struct blob_attr
*attr
)
285 struct interface_proto_state
*proto
;
287 proto
= calloc(1, sizeof(*proto
));
288 proto
->free
= default_proto_free
;
289 proto
->cb
= no_proto_handler
;
294 static const struct proto_handler no_proto
= {
296 .flags
= PROTO_FLAG_IMMEDIATE
,
297 .attach
= default_proto_attach
,
300 static const struct proto_handler
*
301 get_proto_handler(const char *name
)
303 struct proto_handler
*proto
;
305 if (!strcmp(name
, "none"))
311 return avl_find_element(&handlers
, name
, proto
, avl
);
315 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
317 const struct proto_handler
*proto
= iface
->proto_handler
;
318 struct interface_proto_state
*state
= NULL
;
323 state
= proto
->attach(proto
, iface
, attr
);
325 state
= no_proto
.attach(&no_proto
, iface
, attr
);
326 state
->cb
= invalid_proto_handler
;
329 state
->handler
= proto
;
330 interface_set_proto_state(iface
, state
);
334 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
336 const struct proto_handler
*proto
= &no_proto
;
339 proto
= get_proto_handler(proto_name
);
341 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
346 iface
->proto_handler
= proto
;
350 interface_proto_event(struct interface_proto_state
*proto
,
351 enum interface_proto_cmd cmd
, bool force
)
353 enum interface_proto_event ev
;
356 ret
= proto
->cb(proto
, cmd
, force
);
357 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
361 case PROTO_CMD_SETUP
:
364 case PROTO_CMD_TEARDOWN
:
370 proto
->proto_event(proto
, ev
);