network: add virtual tunnel interface (VTI) support
[openwrt/openwrt.git] / package / network / config / vti / files / vti.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . /lib/functions/network.sh
6 . ../netifd-proto.sh
7 init_proto "$@"
8 }
9
10 vti_generic_setup() {
11 local cfg="$1"
12 local mode="$2"
13 local local="$3"
14 local remote="$4"
15 local link="$5"
16 local mtu zone ikey
17 json_get_vars mtu zone ikey okey
18
19 [ -z "$zone" ] && zone="wan"
20
21 proto_init_update "$link" 1
22
23 proto_add_tunnel
24 json_add_string mode "$mode"
25 json_add_int mtu "${mtu:-1280}"
26 json_add_string local "$local"
27 json_add_string remote "$remote"
28 [ -n "$tunlink" ] && json_add_string link "$tunlink"
29 json_add_string info "${ikey:-0},${okey:-0}"
30 proto_close_tunnel
31
32 proto_add_data
33 [ -n "$zone" ] && json_add_string zone "$zone"
34 proto_close_data
35
36 proto_send_update "$cfg"
37 }
38
39 vti_setup() {
40 local cfg="$1"
41 local mode="$2"
42
43 local ipaddr peeraddr
44 json_get_vars df ipaddr peeraddr tunlink
45
46 [ -z "$peeraddr" ] && {
47 proto_notify_error "$cfg" "MISSING_ADDRESS"
48 proto_block_restart "$cfg"
49 exit
50 }
51
52 ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
53
54 [ -z "$ipaddr" ] && {
55 local wanif="$tunlink"
56 if [ -z $wanif ] && ! network_find_wan wanif; then
57 proto_notify_error "$cfg" "NO_WAN_LINK"
58 exit
59 fi
60
61 if ! network_get_ipaddr ipaddr "$wanif"; then
62 proto_notify_error "$cfg" "NO_WAN_LINK"
63 exit
64 fi
65 }
66
67 vti_generic_setup $cfg $mode $ipaddr $peeraddr "vti-$cfg"
68 }
69
70 proto_vti_setup() {
71 local cfg="$1"
72
73 vti_setup $cfg "vtiip"
74 }
75
76 vti6_setup() {
77 local cfg="$1"
78 local mode="$2"
79
80 local ip6addr peer6addr weakif
81 json_get_vars ip6addr peer6addr tunlink weakif
82
83 [ -z "$peer6addr" ] && {
84 proto_notify_error "$cfg" "MISSING_ADDRESS"
85 proto_block_restart "$cfg"
86 exit
87 }
88
89 ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
90
91 [ -z "$ip6addr" ] && {
92 local wanif="$tunlink"
93 if [ -z $wanif ] && ! network_find_wan6 wanif; then
94 proto_notify_error "$cfg" "NO_WAN_LINK"
95 exit
96 fi
97
98 if ! network_get_ipaddr6 ip6addr "$wanif"; then
99 [ -z "$weakif" ] && weakif="lan"
100 if ! network_get_ipaddr6 ip6addr "$weakif"; then
101 proto_notify_error "$cfg" "NO_WAN_LINK"
102 exit
103 fi
104 fi
105 }
106
107 vti_generic_setup $cfg $mode $ip6addr $peer6addr "vti6-$cfg"
108 }
109
110 proto_vti6_setup() {
111 local cfg="$1"
112
113 vti6_setup $cfg "vtiip6"
114 }
115
116 proto_vti_teardown() {
117 local cfg="$1"
118 }
119
120 proto_vti6_teardown() {
121 local cfg="$1"
122 }
123
124 vti_generic_init_config() {
125 no_device=1
126 available=1
127
128 proto_config_add_int "mtu"
129 proto_config_add_string "tunlink"
130 proto_config_add_string "zone"
131 proto_config_add_int "ikey"
132 proto_config_add_int "okey"
133 }
134
135 proto_vti_init_config() {
136 vti_generic_init_config
137 proto_config_add_string "ipaddr"
138 proto_config_add_string "peeraddr"
139 }
140
141 proto_vti6_init_config() {
142 vti_generic_init_config
143 proto_config_add_string "ip6addr"
144 proto_config_add_string "peer6addr"
145 proto_config_add_string "weakif"
146 }
147
148 [ -n "$INCLUDE_ONLY" ] || {
149 [ -f /lib/modules/$(uname -r)/ip_vti.ko ] && add_protocol vti
150 [ -f /lib/modules/$(uname -r)/ip6_vti.ko ] && add_protocol vti6
151 }