unetd: update to the latest version
[openwrt/staging/aparcar.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_array "tunnels:list(string)"
20 proto_config_add_array "connect:list(string)"
21 proto_config_add_array "peer_data:list(string)"
22 no_device=1
23 available=1
24 no_proto_task=1
25 }
26
27 proto_unet_setup() {
28 local config="$1"
29
30 local device type key file keepalive domain tunnels
31 json_get_vars device type auth_key key file keepalive domain
32 json_get_values tunnels tunnels
33 json_get_values connect connect
34 json_get_values peer_data peer_data
35 device="${device:-$config}"
36
37 [ -n "$auth_key" ] && type="${type:-dynamic}"
38 [ -n "$file" ] && type="${type:-file}"
39
40 json_init
41 json_add_string name "$device"
42 json_add_string type "$type"
43 json_add_string interface "$config"
44 json_add_string auth_key "$auth_key"
45 json_add_string key "$key"
46 json_add_string file "$file"
47 [ -n "$keepalive" ] && json_add_int keepalive "$keepalive"
48 json_add_string domain "$domain"
49
50 json_add_object tunnels
51 for t in $tunnels; do
52 local ifname="${t%%=*}"
53 local service="${t#*=}"
54 [ -n "$ifname" -a -n "$service" -a "$ifname" != "$t" ] || continue
55 json_add_string "$ifname" "$service"
56 done
57 json_close_object
58
59 json_add_array auth_connect
60 for c in $connect; do
61 json_add_string "" "$c"
62 done
63 json_close_array
64
65 json_add_array peer_data
66 for c in $peer_data; do
67 json_add_string "" "$c"
68 done
69 json_close_array
70
71 ip link del dev "$device" >/dev/null 2>&1
72 ip link add dev "$device" type wireguard || {
73 echo "Could not create wireguard device $device"
74 proto_setup_failed "$config"
75 exit 1
76 }
77
78 ubus call unetd network_add "$(json_dump)"
79 }
80
81 proto_unet_teardown() {
82 local config="$1"
83 local iface="$2"
84
85 local device
86 json_get_vars device
87 device="${device:-$iface}"
88
89 json_init
90 json_add_string name "$device"
91
92 ip link del dev "$device"
93
94 ubus call unetd network_del "$(json_dump)"
95 }
96
97 add_protocol unet