8e56fbd8694c4c417670d5e721ea43f776cb3950
[openwrt/openwrt.git] / package / network / services / unetd / files / unetd.sh
1 #!/bin/sh
2
3 [ -x /usr/sbin/unetd ] || exit 0
4
5 . /lib/functions.sh
6 . /lib/functions/network.sh
7 . ../netifd-proto.sh
8
9 init_proto "$@"
10
11 proto_unet_init_config() {
12 proto_config_add_string device
13 proto_config_add_string type
14 proto_config_add_string auth_key
15 proto_config_add_string key
16 proto_config_add_string file
17 proto_config_add_int keepalive
18 proto_config_add_string domain
19 proto_config_add_string "tunnels:list(string)"
20 proto_config_add_string "connect:list(string)"
21 no_device=1
22 available=1
23 no_proto_task=1
24 }
25
26 proto_unet_setup() {
27 local config="$1"
28
29 local device type key file keepalive domain tunnels
30 json_get_vars device type auth_key key file keepalive domain tunnels connect
31 device="${device:-$config}"
32
33 [ -n "$auth_key" ] && type="${type:-dynamic}"
34 [ -n "$file" ] && type="${type:-file}"
35
36 json_init
37 json_add_string name "$device"
38 json_add_string type "$type"
39 json_add_string interface "$config"
40 json_add_string auth_key "$auth_key"
41 json_add_string key "$key"
42 json_add_string file "$file"
43 [ -n "$keepalive" ] && json_add_int keepalive "$keepalive"
44 json_add_string domain "$domain"
45
46 json_add_object tunnels
47 for t in $tunnels; do
48 local ifname="${t%%=*}"
49 local service="${t#*=}"
50 [ -n "$ifname" -a -n "$service" -a "$ifname" != "$t" ] || continue
51 json_add_string "$ifname" "$service"
52 done
53 json_close_object
54
55 json_add_array auth_connect
56 for c in $connect; do
57 json_add_string "" "$c"
58 done
59 json_close_array
60
61 ip link del dev "$device" >/dev/null 2>&1
62 ip link add dev "$device" type wireguard || {
63 echo "Could not create wireguard device $device"
64 proto_setup_failed "$config"
65 exit 1
66 }
67
68 ubus call unetd network_add "$(json_dump)"
69 }
70
71 proto_unet_teardown() {
72 local config="$1"
73 local iface="$2"
74
75 local device
76 json_get_vars device
77 device="${device:-$iface}"
78
79 json_init
80 json_add_string name "$device"
81
82 ip link del dev "$device"
83
84 ubus call unetd network_del "$(json_dump)"
85 }
86
87 add_protocol unet