1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
7 #include <sys/socket.h>
10 #include <libubox/avl-cmp.h>
11 #include <libubox/utils.h>
12 #include <libubox/blobmsg_json.h>
19 NETDATA_ATTR_SERVICES
,
23 static const struct blobmsg_policy netdata_policy
[__NETDATA_ATTR_MAX
] = {
24 [NETDATA_ATTR_CONFIG
] = { "config", BLOBMSG_TYPE_TABLE
},
25 [NETDATA_ATTR_HOSTS
] = { "hosts", BLOBMSG_TYPE_TABLE
},
26 [NETDATA_ATTR_SERVICES
] = { "services", BLOBMSG_TYPE_TABLE
},
32 NETCONF_ATTR_PEX_PORT
,
33 NETCONF_ATTR_KEEPALIVE
,
37 static const struct blobmsg_policy netconf_policy
[__NETCONF_ATTR_MAX
] = {
38 [NETCONF_ATTR_ID
] = { "id", BLOBMSG_TYPE_STRING
},
39 [NETCONF_ATTR_PORT
] = { "port", BLOBMSG_TYPE_INT32
},
40 [NETCONF_ATTR_PEX_PORT
] = { "peer-exchange-port", BLOBMSG_TYPE_INT32
},
41 [NETCONF_ATTR_KEEPALIVE
] = { "keepalive", BLOBMSG_TYPE_INT32
},
44 const struct blobmsg_policy network_policy
[__NETWORK_ATTR_MAX
] = {
45 [NETWORK_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
46 [NETWORK_ATTR_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
47 [NETWORK_ATTR_KEY
] = { "key", BLOBMSG_TYPE_STRING
},
48 [NETWORK_ATTR_FILE
] = { "file", BLOBMSG_TYPE_STRING
},
49 [NETWORK_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
50 [NETWORK_ATTR_INTERFACE
] = { "interface", BLOBMSG_TYPE_STRING
},
51 [NETWORK_ATTR_KEEPALIVE
] = { "keepalive", BLOBMSG_TYPE_INT32
},
52 [NETWORK_ATTR_DOMAIN
] = { "domain", BLOBMSG_TYPE_STRING
},
53 [NETWORK_ATTR_UPDATE_CMD
] = { "update-cmd", BLOBMSG_TYPE_STRING
},
54 [NETWORK_ATTR_TUNNELS
] = { "tunnels", BLOBMSG_TYPE_TABLE
},
57 AVL_TREE(networks
, avl_strcmp
, false, NULL
);
58 static struct blob_buf b
;
60 static void network_load_config_data(struct network
*net
, struct blob_attr
*data
)
62 struct blob_attr
*tb
[__NETCONF_ATTR_MAX
];
63 struct blob_attr
*cur
;
64 siphash_key_t key
= {};
66 blobmsg_parse(netconf_policy
, __NETCONF_ATTR_MAX
, tb
,
67 blobmsg_data(data
), blobmsg_len(data
));
69 if ((cur
= tb
[NETCONF_ATTR_PORT
]) != NULL
)
70 net
->net_config
.port
= blobmsg_get_u32(cur
);
72 net
->net_config
.port
= 51820;
74 if ((cur
= tb
[NETCONF_ATTR_PEX_PORT
]) != NULL
)
75 net
->net_config
.pex_port
= blobmsg_get_u32(cur
);
77 if ((cur
= tb
[NETCONF_ATTR_ID
]) != NULL
) {
78 const char *id
= blobmsg_get_string(cur
);
79 siphash_to_le64(&net
->net_config
.addr
.network_id
, id
, strlen(id
), &key
);
81 siphash_to_le64(&net
->net_config
.addr
.network_id
, &net
->net_config
.port
,
82 sizeof(net
->net_config
.port
), &key
);
85 net
->net_config
.addr
.network_id
[0] = 0xfd;
86 network_fill_host_addr(&net
->net_config
.addr
, net
->config
.pubkey
);
88 if (net
->config
.keepalive
>= 0)
89 net
->net_config
.keepalive
= net
->config
.keepalive
;
90 else if ((cur
= tb
[NETCONF_ATTR_KEEPALIVE
]) != NULL
)
91 net
->net_config
.keepalive
= blobmsg_get_u32(cur
);
93 net
->net_config
.keepalive
= 0;
96 static int network_load_data(struct network
*net
, struct blob_attr
*data
)
98 struct blob_attr
*tb
[__NETDATA_ATTR_MAX
];
100 blobmsg_parse(netdata_policy
, __NETDATA_ATTR_MAX
, tb
,
101 blobmsg_data(data
), blobmsg_len(data
));
103 network_load_config_data(net
, tb
[NETDATA_ATTR_CONFIG
]);
104 network_hosts_add(net
, tb
[NETDATA_ATTR_HOSTS
]);
105 network_services_add(net
, tb
[NETDATA_ATTR_SERVICES
]);
110 static int network_load_file(struct network
*net
)
112 blob_buf_init(&b
, 0);
114 if (!blobmsg_add_json_from_file(&b
, net
->config
.file
))
117 return network_load_data(net
, b
.head
);
121 network_fill_ip(struct blob_buf
*buf
, int af
, union network_addr
*addr
, int mask
)
126 c
= blobmsg_open_table(buf
, NULL
);
128 blobmsg_printf(buf
, "mask", "%d", mask
);
130 str
= blobmsg_alloc_string_buffer(buf
, "ipaddr", INET6_ADDRSTRLEN
);
131 inet_ntop(af
, addr
, str
, INET6_ADDRSTRLEN
);
132 blobmsg_add_string_buffer(buf
);
134 blobmsg_close_table(buf
, c
);
138 network_fill_ipaddr_list(struct network_host
*host
, struct blob_buf
*b
, bool ipv6
)
140 union network_addr addr
= {};
141 struct blob_attr
*cur
;
146 af
= ipv6
? AF_INET6
: AF_INET
;
147 blobmsg_for_each_attr(cur
, host
->peer
.ipaddr
, rem
) {
148 const char *str
= blobmsg_get_string(cur
);
150 if (!!strchr(str
, ':') != ipv6
)
153 if (inet_pton(af
, str
, &addr
) != 1)
156 c
= blobmsg_open_table(b
, NULL
);
157 blobmsg_add_string(b
, "ipaddr", str
);
158 blobmsg_add_string(b
, "mask", ipv6
? "128" : "32");
159 blobmsg_close_table(b
, c
);
164 network_fill_ip_settings(struct network
*net
, struct blob_buf
*buf
)
166 struct network_host
*host
= net
->net_config
.local_host
;
169 c
= blobmsg_open_array(buf
, "ipaddr");
170 network_fill_ipaddr_list(host
, buf
, false);
171 blobmsg_close_array(buf
, c
);
173 c
= blobmsg_open_array(buf
, "ip6addr");
174 network_fill_ip(buf
, AF_INET6
, &host
->peer
.local_addr
, 64);
175 network_fill_ipaddr_list(host
, buf
, true);
176 blobmsg_close_array(buf
, c
);
180 __network_fill_host_subnets(struct network_host
*host
, struct blob_buf
*b
, bool ipv6
)
182 union network_addr addr
= {};
183 struct blob_attr
*cur
;
189 af
= ipv6
? AF_INET6
: AF_INET
;
190 blobmsg_for_each_attr(cur
, host
->peer
.subnet
, rem
) {
191 const char *str
= blobmsg_get_string(cur
);
194 if (!!strchr(str
, ':') != ipv6
)
197 if (network_get_subnet(af
, &addr
, &mask
, str
))
200 c
= blobmsg_open_table(b
, NULL
);
202 buf
= blobmsg_alloc_string_buffer(b
, "target", INET6_ADDRSTRLEN
);
203 inet_ntop(af
, &addr
, buf
, INET6_ADDRSTRLEN
);
204 blobmsg_add_string_buffer(b
);
206 blobmsg_printf(b
, "netmask", "%d", mask
);
208 blobmsg_close_table(b
, c
);
211 blobmsg_for_each_attr(cur
, host
->peer
.ipaddr
, rem
) {
212 const char *str
= blobmsg_get_string(cur
);
214 if (!!strchr(str
, ':') != ipv6
)
217 if (inet_pton(af
, str
, &addr
) != 1)
220 c
= blobmsg_open_table(b
, NULL
);
221 blobmsg_add_string(b
, "target", str
);
222 blobmsg_add_string(b
, "netmask", ipv6
? "128" : "32");
223 blobmsg_close_table(b
, c
);
228 __network_fill_subnets(struct network
*net
, struct blob_buf
*buf
, bool ipv6
)
230 struct network_host
*host
;
233 c
= blobmsg_open_array(buf
, ipv6
? "routes6": "routes");
234 avl_for_each_element(&net
->hosts
, host
, node
) {
235 if (host
== net
->net_config
.local_host
)
237 __network_fill_host_subnets(host
, buf
, ipv6
);
239 blobmsg_close_array(buf
, c
);
244 network_fill_subnets(struct network
*net
, struct blob_buf
*buf
)
246 __network_fill_subnets(net
, buf
, false);
247 __network_fill_subnets(net
, buf
, true);
251 network_do_update(struct network
*net
, bool up
)
253 if (!net
->net_config
.local_host
)
256 blob_buf_init(&b
, 0);
257 blobmsg_add_u32(&b
, "action", 0);
258 blobmsg_add_string(&b
, "ifname", network_name(net
));
259 blobmsg_add_u8(&b
, "link-up", up
);
262 network_fill_ip_settings(net
, &b
);
263 network_fill_subnets(net
, &b
);
267 char *s
= blobmsg_format_json(b
.head
, true);
268 D_NET(net
, "update: %s", s
);
272 if (net
->config
.update_cmd
) {
273 const char *argv
[] = { net
->config
.update_cmd
, NULL
, NULL
};
278 argv
[1] = blobmsg_format_json(b
.head
, true);
279 execvp(argv
[0], (char **)argv
);
282 waitpid(pid
, &stat
, 0);
285 if (!net
->config
.interface
)
288 blobmsg_add_string(&b
, "interface", net
->config
.interface
);
289 unetd_ubus_netifd_update(b
.head
);
292 static int network_reload(struct network
*net
)
296 net
->prev_local_host
= net
->net_config
.local_host
;
298 memset(&net
->net_config
, 0, sizeof(net
->net_config
));
300 network_pex_close(net
);
301 network_services_free(net
);
302 network_hosts_update_start(net
);
303 network_services_update_start(net
);
305 switch (net
->config
.type
) {
306 case NETWORK_TYPE_FILE
:
307 ret
= network_load_file(net
);
309 case NETWORK_TYPE_INLINE
:
310 ret
= network_load_data(net
, net
->config
.net_data
);
314 network_services_update_done(net
);
315 network_hosts_update_done(net
);
316 uloop_timeout_set(&net
->connect_timer
, 10);
318 net
->prev_local_host
= NULL
;
321 network_do_update(net
, true);
322 network_pex_open(net
);
327 static int network_setup(struct network
*net
)
329 if (wg_init_network(net
)) {
330 fprintf(stderr
, "Setup failed for network %s\n", network_name(net
));
334 net
->ifindex
= if_nametoindex(network_name(net
));
336 fprintf(stderr
, "Could not get ifindex for network %s\n", network_name(net
));
343 static void network_teardown(struct network
*net
)
345 network_do_update(net
, false);
346 network_pex_close(net
);
347 network_hosts_free(net
);
348 network_services_free(net
);
349 wg_cleanup_network(net
);
353 network_destroy(struct network
*net
)
355 network_teardown(net
);
356 avl_delete(&networks
, &net
->node
);
357 free(net
->config
.data
);
362 network_set_config(struct network
*net
, struct blob_attr
*config
)
364 struct blob_attr
*tb
[__NETWORK_ATTR_MAX
];
365 struct blob_attr
*cur
;
367 if (net
->config
.data
&& blob_attr_equal(net
->config
.data
, config
))
370 network_teardown(net
);
372 free(net
->config
.data
);
373 memset(&net
->config
, 0, sizeof(net
->config
));
375 net
->config
.data
= blob_memdup(config
);
376 blobmsg_parse(network_policy
, __NETWORK_ATTR_MAX
, tb
,
377 blobmsg_data(net
->config
.data
),
378 blobmsg_len(net
->config
.data
));
380 if ((cur
= tb
[NETWORK_ATTR_TYPE
]) == NULL
)
383 if (!strcmp(blobmsg_get_string(cur
), "file"))
384 net
->config
.type
= NETWORK_TYPE_FILE
;
385 else if (!strcmp(blobmsg_get_string(cur
), "inline"))
386 net
->config
.type
= NETWORK_TYPE_INLINE
;
390 if ((cur
= tb
[NETWORK_ATTR_KEEPALIVE
]) != NULL
)
391 net
->config
.keepalive
= blobmsg_get_u32(cur
);
393 net
->config
.keepalive
= -1;
395 switch (net
->config
.type
) {
396 case NETWORK_TYPE_FILE
:
397 if ((cur
= tb
[NETWORK_ATTR_FILE
]) != NULL
)
398 net
->config
.file
= blobmsg_get_string(cur
);
402 case NETWORK_TYPE_INLINE
:
403 net
->config
.net_data
= tb
[NETWORK_ATTR_DATA
];
404 if (!net
->config
.net_data
)
409 if ((cur
= tb
[NETWORK_ATTR_INTERFACE
]) != NULL
)
410 net
->config
.interface
= blobmsg_get_string(cur
);
412 if ((cur
= tb
[NETWORK_ATTR_UPDATE_CMD
]) != NULL
)
413 net
->config
.update_cmd
= blobmsg_get_string(cur
);
415 if ((cur
= tb
[NETWORK_ATTR_DOMAIN
]) != NULL
)
416 net
->config
.domain
= blobmsg_get_string(cur
);
418 if ((cur
= tb
[NETWORK_ATTR_TUNNELS
]) != NULL
)
419 net
->config
.tunnels
= cur
;
421 if ((cur
= tb
[NETWORK_ATTR_KEY
]) == NULL
)
424 if (b64_decode(blobmsg_get_string(cur
), net
->config
.key
, sizeof(net
->config
.key
)) !=
425 sizeof(net
->config
.key
))
428 curve25519_generate_public(net
->config
.pubkey
, net
->config
.key
);
430 if (network_setup(net
))
439 network_destroy(net
);
443 static struct network
*
444 network_alloc(const char *name
)
449 net
= calloc_a(sizeof(*net
), &name_buf
, strlen(name
) + 1);
450 net
->node
.key
= strcpy(name_buf
, name
);
451 avl_insert(&networks
, &net
->node
);
453 network_pex_init(net
);
454 network_hosts_init(net
);
455 network_services_init(net
);
460 void network_fill_host_addr(union network_addr
*addr
, uint8_t *pubkey
)
462 siphash_key_t key
= {
464 get_unaligned_le64(addr
->network_id
),
465 get_unaligned_le64(addr
->network_id
)
469 siphash_to_le64(&addr
->host_addr
, pubkey
, CURVE25519_KEY_SIZE
, &key
);
472 int unetd_network_add(const char *name
, struct blob_attr
*config
)
476 if (strchr(name
, '/'))
479 net
= avl_find_element(&networks
, name
, net
, node
);
481 net
= network_alloc(name
);
483 return network_set_config(net
, config
);
486 int unetd_network_remove(const char *name
)
490 net
= avl_find_element(&networks
, name
, net
, node
);
494 network_destroy(net
);
499 void network_free_all(void)
501 struct network
*net
, *tmp
;
503 avl_for_each_element_safe(&networks
, net
, node
, tmp
)
504 network_destroy(net
);