| 1 | #!/bin/sh |
| 2 | # 6to4.sh - IPv6-in-IPv4 tunnel backend |
| 3 | # Copyright (c) 2010-2012 OpenWrt.org |
| 4 | |
| 5 | [ -n "$INCLUDE_ONLY" ] || { |
| 6 | . /lib/functions.sh |
| 7 | . /lib/functions/network.sh |
| 8 | . ../netifd-proto.sh |
| 9 | init_proto "$@" |
| 10 | } |
| 11 | |
| 12 | find_6to4_prefix() { |
| 13 | local ip4="$1" |
| 14 | local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS" |
| 15 | |
| 16 | printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4 |
| 17 | } |
| 18 | |
| 19 | test_6to4_rfc1918() |
| 20 | { |
| 21 | local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS" |
| 22 | [ $1 -eq 10 ] && return 0 |
| 23 | [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0 |
| 24 | [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0 |
| 25 | |
| 26 | # RFC 6598 |
| 27 | [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0 |
| 28 | |
| 29 | return 1 |
| 30 | } |
| 31 | |
| 32 | proto_6to4_setup() { |
| 33 | local cfg="$1" |
| 34 | local iface="$2" |
| 35 | local link="6to4-$cfg" |
| 36 | |
| 37 | local mtu ttl ipaddr |
| 38 | json_get_vars mtu ttl ipaddr |
| 39 | |
| 40 | ( proto_add_host_dependency "$cfg" 0.0.0.0 ) |
| 41 | |
| 42 | local wanif |
| 43 | if ! network_find_wan wanif; then |
| 44 | proto_notify_error "$cfg" "NO_WAN_LINK" |
| 45 | return |
| 46 | fi |
| 47 | |
| 48 | [ -z "$ipaddr" ] && { |
| 49 | if ! network_get_ipaddr ipaddr "$wanif"; then |
| 50 | proto_notify_error "$cfg" "NO_WAN_ADDRESS" |
| 51 | return |
| 52 | fi |
| 53 | } |
| 54 | |
| 55 | test_6to4_rfc1918 "$ipaddr" && { |
| 56 | proto_notify_error "$cfg" "INVALID_LOCAL_ADDRESS" |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | # find our local prefix |
| 61 | local prefix6=$(find_6to4_prefix "$ipaddr") |
| 62 | local local6="$prefix6::1" |
| 63 | |
| 64 | proto_init_update "$link" 1 |
| 65 | proto_add_ipv6_address "$local6" 16 |
| 66 | proto_add_ipv6_route "::" 0 "::192.88.99.1" |
| 67 | |
| 68 | proto_add_tunnel |
| 69 | json_add_string mode sit |
| 70 | json_add_int mtu "${mtu:-1280}" |
| 71 | json_add_int ttl "${ttl:-64}" |
| 72 | json_add_string local "$ipaddr" |
| 73 | proto_close_tunnel |
| 74 | |
| 75 | proto_send_update "$cfg" |
| 76 | } |
| 77 | |
| 78 | proto_6to4_teardown() { |
| 79 | local cfg="$1" |
| 80 | } |
| 81 | |
| 82 | proto_6to4_init_config() { |
| 83 | no_device=1 |
| 84 | available=1 |
| 85 | |
| 86 | proto_config_add_string "ipaddr" |
| 87 | proto_config_add_int "mtu" |
| 88 | proto_config_add_int "ttl" |
| 89 | } |
| 90 | |
| 91 | [ -n "$INCLUDE_ONLY" ] || { |
| 92 | add_protocol 6to4 |
| 93 | } |
| 94 | |