map.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/bin/sh
  2. # map.sh - IPv4-in-IPv6 tunnel backend
  3. #
  4. # Author: Steven Barth <[email protected]>
  5. # Copyright (c) 2014 cisco Systems, Inc.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2
  9. # as published by the Free Software Foundation
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. [ -n "$INCLUDE_ONLY" ] || {
  16. . /lib/functions.sh
  17. . /lib/functions/network.sh
  18. . ../netifd-proto.sh
  19. init_proto "$@"
  20. }
  21. proto_map_setup() {
  22. local cfg="$1"
  23. local iface="$2"
  24. local link="map-$cfg"
  25. # uncomment for legacy MAP0 mode
  26. #export LEGACY=1
  27. local type mtu ttl tunlink zone
  28. local rule ipaddr ip4prefixlen ip6prefix ip6prefixlen peeraddr ealen psidlen psid offset
  29. json_get_vars type mtu ttl tunlink zone
  30. json_get_vars rule ipaddr ip4prefixlen ip6prefix ip6prefixlen peeraddr ealen psidlen psid offset
  31. [ -z "$zone" ] && zone="wan"
  32. [ -z "$type" ] && type="map-e"
  33. [ -z "$ip4prefixlen" ] && ip4prefixlen=32
  34. ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
  35. if [ -z "$rule" ]; then
  36. rule="type=$type,ipv6prefix=$ip6prefix,prefix6len=$ip6prefixlen,ipv4prefix=$ipaddr,prefix4len=$ip4prefixlen"
  37. [ -n "$psid" ] && rule="$rule,psid=$psid"
  38. [ -n "$psidlen" ] && rule="$rule,psidlen=$psidlen"
  39. [ -n "$offset" ] && rule="$rule,offset=$offset"
  40. [ -n "$ealen" ] && rule="$rule,ealen=$ealen"
  41. if [ "$type" = "map-t" ]; then
  42. rule="$rule,dmr=$peeraddr"
  43. else
  44. rule="$rule,br=$peeraddr"
  45. fi
  46. fi
  47. RULE_DATA=$(mapcalc ${tunlink:-\*} $rule)
  48. if [ "$?" != 0 ]; then
  49. proto_notify_error "$cfg" "INVALID_MAP_RULE"
  50. proto_block_restart "$cfg"
  51. return
  52. fi
  53. eval $RULE_DATA
  54. if [ -z "$RULE_BMR" ]; then
  55. proto_notify_error "$cfg" "NO_MATCHING_PD"
  56. proto_block_restart "$cfg"
  57. return
  58. fi
  59. k=$RULE_BMR
  60. if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
  61. proto_init_update "$link" 1
  62. proto_add_ipv4_address $(eval "echo \$RULE_${k}_IPV4ADDR") "" "" ""
  63. proto_add_tunnel
  64. json_add_string mode ipip6
  65. json_add_int mtu "${mtu:-1280}"
  66. json_add_int ttl "${ttl:-64}"
  67. json_add_string local $(eval "echo \$RULE_${k}_IPV6ADDR")
  68. json_add_string remote $(eval "echo \$RULE_${k}_BR")
  69. json_add_string link $(eval "echo \$RULE_${k}_PD6IFACE")
  70. if [ "$type" = "map-e" ]; then
  71. json_add_array "fmrs"
  72. for i in $(seq $RULE_COUNT); do
  73. [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
  74. fmr="$(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
  75. fmr="$fmr,$(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
  76. fmr="$fmr,$(eval "echo \$RULE_${i}_EALEN"),$(eval "echo \$RULE_${i}_OFFSET")"
  77. json_add_string "" "$fmr"
  78. done
  79. json_close_array
  80. fi
  81. proto_close_tunnel
  82. elif [ "$type" = "map-t" -a -f "/proc/net/nat46/control" ]; then
  83. proto_init_update "$link" 1
  84. local style="MAP"
  85. [ "$LEGACY" = 1 ] && style="MAP0"
  86. echo add $link > /proc/net/nat46/control
  87. local cfgstr="local.style $style local.v4 $(eval "echo \$RULE_${k}_IPV4PREFIX")/$(eval "echo \$RULE_${k}_PREFIX4LEN")"
  88. cfgstr="$cfgstr local.v6 $(eval "echo \$RULE_${k}_IPV6PREFIX")/$(eval "echo \$RULE_${k}_PREFIX6LEN")"
  89. cfgstr="$cfgstr local.ea-len $(eval "echo \$RULE_${k}_EALEN") local.psid-offset $(eval "echo \$RULE_${k}_OFFSET")"
  90. cfgstr="$cfgstr remote.v4 0.0.0.0/0 remote.v6 $(eval "echo \$RULE_${k}_DMR") remote.style RFC6052 remote.ea-len 0 remote.psid-offset 0"
  91. echo config $link $cfgstr > /proc/net/nat46/control
  92. for i in $(seq $RULE_COUNT); do
  93. [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
  94. local cfgstr="remote.style $style remote.v4 $(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
  95. cfgstr="$cfgstr remote.v6 $(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
  96. cfgstr="$cfgstr remote.ea-len $(eval "echo \$RULE_${i}_EALEN") remote.psid-offset $(eval "echo \$RULE_${i}_OFFSET")"
  97. echo insert $link $cfgstr > /proc/net/nat46/control
  98. done
  99. else
  100. proto_notify_error "$cfg" "UNSUPPORTED_TYPE"
  101. proto_block_restart "$cfg"
  102. fi
  103. proto_add_ipv4_route "0.0.0.0" 0
  104. proto_add_data
  105. [ "$zone" != "-" ] && json_add_string zone "$zone"
  106. json_add_array firewall
  107. if [ -z "$(eval "echo \$RULE_${k}_PORTSETS")" ]; then
  108. json_add_object ""
  109. json_add_string type nat
  110. json_add_string target SNAT
  111. json_add_string family inet
  112. json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
  113. json_close_object
  114. else
  115. for portset in $(eval "echo \$RULE_${k}_PORTSETS"); do
  116. for proto in icmp tcp udp; do
  117. json_add_object ""
  118. json_add_string type nat
  119. json_add_string target SNAT
  120. json_add_string family inet
  121. json_add_string proto "$proto"
  122. json_add_boolean connlimit_ports 1
  123. json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
  124. json_add_string snat_port "$portset"
  125. json_close_object
  126. done
  127. done
  128. fi
  129. if [ "$type" = "map-t" ]; then
  130. json_add_object ""
  131. json_add_string type rule
  132. json_add_string family inet6
  133. json_add_string proto all
  134. json_add_string direction in
  135. json_add_string dest "$zone"
  136. json_add_string src "$zone"
  137. json_add_string src_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
  138. json_add_string target ACCEPT
  139. json_close_object
  140. json_add_object ""
  141. json_add_string type rule
  142. json_add_string family inet6
  143. json_add_string proto all
  144. json_add_string direction out
  145. json_add_string dest "$zone"
  146. json_add_string src "$zone"
  147. json_add_string dest_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
  148. json_add_string target ACCEPT
  149. json_close_object
  150. proto_add_ipv6_route $(eval "echo \$RULE_${k}_IPV6ADDR") 128
  151. fi
  152. json_close_array
  153. proto_close_data
  154. proto_send_update "$cfg"
  155. if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
  156. json_init
  157. json_add_string name "${cfg}_local"
  158. json_add_string ifname "@$(eval "echo \$RULE_${k}_PD6IFACE")"
  159. json_add_string proto "static"
  160. json_add_array ip6addr
  161. json_add_string "" "$(eval "echo \$RULE_${k}_IPV6ADDR")"
  162. json_close_array
  163. json_close_object
  164. ubus call network add_dynamic "$(json_dump)"
  165. fi
  166. }
  167. proto_map_teardown() {
  168. local cfg="$1"
  169. ifdown "${cfg}_local"
  170. }
  171. proto_map_init_config() {
  172. no_device=1
  173. available=1
  174. proto_config_add_string "rule"
  175. proto_config_add_string "ipaddr"
  176. proto_config_add_int "ip4prefixlen"
  177. proto_config_add_string "ip6prefix"
  178. proto_config_add_int "ip6prefixlen"
  179. proto_config_add_string "peeraddr"
  180. proto_config_add_int "psidlen"
  181. proto_config_add_int "psid"
  182. proto_config_add_int "offset"
  183. proto_config_add_string "tunlink"
  184. proto_config_add_int "mtu"
  185. proto_config_add_int "ttl"
  186. proto_config_add_string "zone"
  187. }
  188. [ -n "$INCLUDE_ONLY" ] || {
  189. add_protocol map
  190. }