b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | gre_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 ipv6 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast |
| 17 | json_get_vars mtu ipv6 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast |
| 18 | |
| 19 | [ -z "$multicast" ] && multicast=1 |
| 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_boolean ipv6 "${ipv6:-1}" |
| 27 | [ -n "$df" ] && json_add_boolean df "$df" |
| 28 | [ -n "$ttl" ] && json_add_int ttl "$ttl" |
| 29 | [ -n "$tos" ] && json_add_string tos "$tos" |
| 30 | json_add_boolean multicast "$multicast" |
| 31 | json_add_string local "$local" |
| 32 | json_add_string remote "$remote" |
| 33 | [ -n "$tunlink" ] && json_add_string link "$tunlink" |
| 34 | |
| 35 | json_add_object 'data' |
| 36 | [ -n "$ikey" ] && json_add_int ikey "$ikey" |
| 37 | [ -n "$okey" ] && json_add_int okey "$okey" |
| 38 | [ -n "$icsum" ] && json_add_boolean icsum "$icsum" |
| 39 | [ -n "$ocsum" ] && json_add_boolean ocsum "$ocsum" |
| 40 | [ -n "$iseqno" ] && json_add_boolean iseqno "$iseqno" |
| 41 | [ -n "$oseqno" ] && json_add_boolean oseqno "$oseqno" |
| 42 | [ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit" |
| 43 | json_close_object |
| 44 | |
| 45 | proto_close_tunnel |
| 46 | |
| 47 | proto_add_data |
| 48 | [ -n "$zone" ] && json_add_string zone "$zone" |
| 49 | proto_close_data |
| 50 | |
| 51 | proto_send_update "$cfg" |
| 52 | } |
| 53 | |
| 54 | gre_setup() { |
| 55 | local cfg="$1" |
| 56 | local mode="$2" |
| 57 | local remoteip |
| 58 | |
| 59 | local ipaddr peeraddr |
| 60 | json_get_vars df ipaddr peeraddr tunlink nohostroute |
| 61 | |
| 62 | [ -z "$peeraddr" ] && { |
| 63 | proto_notify_error "$cfg" "MISSING_PEER_ADDRESS" |
| 64 | proto_block_restart "$cfg" |
| 65 | exit |
| 66 | } |
| 67 | |
| 68 | remoteip=$(resolveip -t 10 -4 "$peeraddr") |
| 69 | |
| 70 | if [ -z "$remoteip" ]; then |
| 71 | proto_notify_error "$cfg" "PEER_RESOLVE_FAIL" |
| 72 | exit |
| 73 | fi |
| 74 | |
| 75 | for ip in $remoteip; do |
| 76 | peeraddr=$ip |
| 77 | break |
| 78 | done |
| 79 | |
| 80 | if [ "${nohostroute}" != "1" ]; then |
| 81 | ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" ) |
| 82 | fi |
| 83 | |
| 84 | [ -z "$ipaddr" ] && { |
| 85 | local wanif="$tunlink" |
| 86 | if [ -z $wanif ] && ! network_find_wan wanif; then |
| 87 | proto_notify_error "$cfg" "NO_WAN_LINK" |
| 88 | exit |
| 89 | fi |
| 90 | |
| 91 | if ! network_get_ipaddr ipaddr "$wanif"; then |
| 92 | proto_notify_error "$cfg" "NO_WAN_LINK" |
| 93 | exit |
| 94 | fi |
| 95 | } |
| 96 | |
| 97 | [ -z "$df" ] && df="1" |
| 98 | |
| 99 | case "$mode" in |
| 100 | gretapip) |
| 101 | gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4t-$cfg" |
| 102 | ;; |
| 103 | *) |
| 104 | gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4-$cfg" |
| 105 | ;; |
| 106 | esac |
| 107 | } |
| 108 | |
| 109 | proto_gre_setup() { |
| 110 | local cfg="$1" |
| 111 | |
| 112 | gre_setup $cfg "greip" |
| 113 | } |
| 114 | |
| 115 | proto_gretap_setup() { |
| 116 | local cfg="$1" |
| 117 | |
| 118 | local network |
| 119 | json_get_vars network |
| 120 | |
| 121 | gre_setup $cfg "gretapip" |
| 122 | |
| 123 | json_init |
| 124 | json_add_string name "gre4t-$cfg" |
| 125 | json_add_boolean link-ext 0 |
| 126 | json_close_object |
| 127 | |
| 128 | for i in $network; do |
| 129 | ubus call network.interface."$i" add_device "$(json_dump)" |
| 130 | done |
| 131 | } |
| 132 | |
| 133 | grev6_setup() { |
| 134 | local cfg="$1" |
| 135 | local mode="$2" |
| 136 | local remoteip6 |
| 137 | |
| 138 | local ip6addr peer6addr weakif |
| 139 | json_get_vars ip6addr peer6addr tunlink weakif encaplimit nohostroute |
| 140 | |
| 141 | [ -z "$peer6addr" ] && { |
| 142 | proto_notify_error "$cfg" "MISSING_PEER_ADDRESS" |
| 143 | proto_block_restart "$cfg" |
| 144 | exit |
| 145 | } |
| 146 | |
| 147 | remoteip6=$(resolveip -t 10 -6 "$peer6addr") |
| 148 | |
| 149 | if [ -z "$remoteip6" ]; then |
| 150 | proto_notify_error "$cfg" "PEER_RESOLVE_FAIL" |
| 151 | exit |
| 152 | fi |
| 153 | |
| 154 | for ip6 in $remoteip6; do |
| 155 | peer6addr=$ip6 |
| 156 | break |
| 157 | done |
| 158 | |
| 159 | if [ "${nohostroute}" != "1" ]; then |
| 160 | ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" ) |
| 161 | fi |
| 162 | |
| 163 | [ -z "$ip6addr" ] && { |
| 164 | local wanif="$tunlink" |
| 165 | if [ -z $wanif ] && ! network_find_wan6 wanif; then |
| 166 | proto_notify_error "$cfg" "NO_WAN_LINK" |
| 167 | exit |
| 168 | fi |
| 169 | |
| 170 | if ! network_get_ipaddr6 ip6addr "$wanif"; then |
| 171 | [ -z "$weakif" ] && weakif="lan" |
| 172 | if ! network_get_ipaddr6 ip6addr "$weakif"; then |
| 173 | proto_notify_error "$cfg" "NO_WAN_LINK" |
| 174 | exit |
| 175 | fi |
| 176 | fi |
| 177 | } |
| 178 | |
| 179 | case "$mode" in |
| 180 | gretapip6) |
| 181 | gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6t-$cfg" |
| 182 | ;; |
| 183 | *) |
| 184 | gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6-$cfg" |
| 185 | ;; |
| 186 | esac |
| 187 | } |
| 188 | |
| 189 | proto_grev6_setup() { |
| 190 | local cfg="$1" |
| 191 | |
| 192 | grev6_setup $cfg "greip6" |
| 193 | } |
| 194 | |
| 195 | proto_grev6tap_setup() { |
| 196 | local cfg="$1" |
| 197 | |
| 198 | local network |
| 199 | json_get_vars network |
| 200 | |
| 201 | grev6_setup $cfg "gretapip6" |
| 202 | |
| 203 | json_init |
| 204 | json_add_string name "gre6t-$cfg" |
| 205 | json_add_boolean link-ext 0 |
| 206 | json_close_object |
| 207 | |
| 208 | for i in $network; do |
| 209 | ubus call network.interface."$i" add_device "$(json_dump)" |
| 210 | done |
| 211 | } |
| 212 | |
| 213 | gretap_generic_teardown() { |
| 214 | local network |
| 215 | json_get_vars network |
| 216 | |
| 217 | json_init |
| 218 | json_add_string name "$1" |
| 219 | json_add_boolean link-ext 0 |
| 220 | json_close_object |
| 221 | |
| 222 | for i in $network; do |
| 223 | ubus call network.interface."$i" remove_device "$(json_dump)" |
| 224 | done |
| 225 | } |
| 226 | |
| 227 | proto_gre_teardown() { |
| 228 | local cfg="$1" |
| 229 | } |
| 230 | |
| 231 | proto_gretap_teardown() { |
| 232 | local cfg="$1" |
| 233 | |
| 234 | gretap_generic_teardown "gre4t-$cfg" |
| 235 | } |
| 236 | |
| 237 | proto_grev6_teardown() { |
| 238 | local cfg="$1" |
| 239 | } |
| 240 | |
| 241 | proto_grev6tap_teardown() { |
| 242 | local cfg="$1" |
| 243 | |
| 244 | gretap_generic_teardown "gre6t-$cfg" |
| 245 | } |
| 246 | |
| 247 | gre_generic_init_config() { |
| 248 | no_device=1 |
| 249 | available=1 |
| 250 | |
| 251 | proto_config_add_int "mtu" |
| 252 | proto_config_add_boolean "ipv6" |
| 253 | proto_config_add_int "ttl" |
| 254 | proto_config_add_string "tos" |
| 255 | proto_config_add_string "tunlink" |
| 256 | proto_config_add_string "zone" |
| 257 | proto_config_add_int "ikey" |
| 258 | proto_config_add_int "okey" |
| 259 | proto_config_add_boolean "icsum" |
| 260 | proto_config_add_boolean "ocsum" |
| 261 | proto_config_add_boolean "iseqno" |
| 262 | proto_config_add_boolean "oseqno" |
| 263 | proto_config_add_boolean "multicast" |
| 264 | } |
| 265 | |
| 266 | proto_gre_init_config() { |
| 267 | gre_generic_init_config |
| 268 | proto_config_add_string "ipaddr" |
| 269 | proto_config_add_string "peeraddr" |
| 270 | proto_config_add_boolean "df" |
| 271 | proto_config_add_boolean "nohostroute" |
| 272 | } |
| 273 | |
| 274 | proto_gretap_init_config() { |
| 275 | proto_gre_init_config |
| 276 | proto_config_add_string "network" |
| 277 | } |
| 278 | |
| 279 | proto_grev6_init_config() { |
| 280 | gre_generic_init_config |
| 281 | proto_config_add_string "ip6addr" |
| 282 | proto_config_add_string "peer6addr" |
| 283 | proto_config_add_string "weakif" |
| 284 | proto_config_add_string "encaplimit" |
| 285 | proto_config_add_boolean "nohostroute" |
| 286 | } |
| 287 | |
| 288 | proto_grev6tap_init_config() { |
| 289 | proto_grev6_init_config |
| 290 | proto_config_add_string "network" |
| 291 | } |
| 292 | |
| 293 | [ -n "$INCLUDE_ONLY" ] || { |
| 294 | [ -d /sys/module/ip_gre ] && { add_protocol gre; add_protocol gretap; } |
| 295 | [ -d /sys/module/ip6_gre ] && { add_protocol grev6; add_protocol grev6tap; } |
| 296 | } |