map.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. # fixme: handle RA/DHCPv6 address race for LW
  36. [ "$type" = lw4o6 ] && sleep 5
  37. if [ -z "$rule" ]; then
  38. rule="type=$type,ipv6prefix=$ip6prefix,prefix6len=$ip6prefixlen,ipv4prefix=$ipaddr,prefix4len=$ip4prefixlen"
  39. [ -n "$psid" ] && rule="$rule,psid=$psid"
  40. [ -n "$psidlen" ] && rule="$rule,psidlen=$psidlen"
  41. [ -n "$offset" ] && rule="$rule,offset=$offset"
  42. [ -n "$ealen" ] && rule="$rule,ealen=$ealen"
  43. if [ "$type" = "map-t" ]; then
  44. rule="$rule,dmr=$peeraddr"
  45. else
  46. rule="$rule,br=$peeraddr"
  47. fi
  48. fi
  49. echo "rule=$rule" > /tmp/map-$cfg.rules
  50. RULE_DATA=$(mapcalc ${tunlink:-\*} $rule)
  51. if [ "$?" != 0 ]; then
  52. proto_notify_error "$cfg" "INVALID_MAP_RULE"
  53. proto_block_restart "$cfg"
  54. return
  55. fi
  56. echo "$RULE_DATA" >> /tmp/map-$cfg.rules
  57. eval $RULE_DATA
  58. if [ -z "$RULE_BMR" ]; then
  59. proto_notify_error "$cfg" "NO_MATCHING_PD"
  60. proto_block_restart "$cfg"
  61. return
  62. fi
  63. k=$RULE_BMR
  64. if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
  65. proto_init_update "$link" 1
  66. proto_add_ipv4_address $(eval "echo \$RULE_${k}_IPV4ADDR") "" "" ""
  67. proto_add_tunnel
  68. json_add_string mode ipip6
  69. json_add_int mtu "${mtu:-1280}"
  70. json_add_int ttl "${ttl:-64}"
  71. json_add_string local $(eval "echo \$RULE_${k}_IPV6ADDR")
  72. json_add_string remote $(eval "echo \$RULE_${k}_BR")
  73. json_add_string link $(eval "echo \$RULE_${k}_PD6IFACE")
  74. if [ "$type" = "map-e" ]; then
  75. json_add_object "data"
  76. json_add_array "fmrs"
  77. for i in $(seq $RULE_COUNT); do
  78. [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
  79. json_add_object ""
  80. json_add_string prefix6 "$(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
  81. json_add_string prefix4 "$(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
  82. json_add_int ealen $(eval "echo \$RULE_${i}_EALEN")
  83. json_add_int offset $(eval "echo \$RULE_${i}_OFFSET")
  84. json_close_object
  85. done
  86. json_close_array
  87. json_close_object
  88. fi
  89. proto_close_tunnel
  90. elif [ "$type" = "map-t" -a -f "/proc/net/nat46/control" ]; then
  91. proto_init_update "$link" 1
  92. local style="MAP"
  93. [ "$LEGACY" = 1 ] && style="MAP0"
  94. echo add $link > /proc/net/nat46/control
  95. local cfgstr="local.style $style local.v4 $(eval "echo \$RULE_${k}_IPV4PREFIX")/$(eval "echo \$RULE_${k}_PREFIX4LEN")"
  96. cfgstr="$cfgstr local.v6 $(eval "echo \$RULE_${k}_IPV6PREFIX")/$(eval "echo \$RULE_${k}_PREFIX6LEN")"
  97. cfgstr="$cfgstr local.ea-len $(eval "echo \$RULE_${k}_EALEN") local.psid-offset $(eval "echo \$RULE_${k}_OFFSET")"
  98. 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"
  99. echo config $link $cfgstr > /proc/net/nat46/control
  100. for i in $(seq $RULE_COUNT); do
  101. [ "$(eval "echo \$RULE_${i}_FMR")" != 1 ] && continue
  102. local cfgstr="remote.style $style remote.v4 $(eval "echo \$RULE_${i}_IPV4PREFIX")/$(eval "echo \$RULE_${i}_PREFIX4LEN")"
  103. cfgstr="$cfgstr remote.v6 $(eval "echo \$RULE_${i}_IPV6PREFIX")/$(eval "echo \$RULE_${i}_PREFIX6LEN")"
  104. cfgstr="$cfgstr remote.ea-len $(eval "echo \$RULE_${i}_EALEN") remote.psid-offset $(eval "echo \$RULE_${i}_OFFSET")"
  105. echo insert $link $cfgstr > /proc/net/nat46/control
  106. done
  107. else
  108. proto_notify_error "$cfg" "UNSUPPORTED_TYPE"
  109. proto_block_restart "$cfg"
  110. fi
  111. proto_add_ipv4_route "0.0.0.0" 0
  112. proto_add_data
  113. [ "$zone" != "-" ] && json_add_string zone "$zone"
  114. json_add_array firewall
  115. if [ -z "$(eval "echo \$RULE_${k}_PORTSETS")" ]; then
  116. json_add_object ""
  117. json_add_string type nat
  118. json_add_string target SNAT
  119. json_add_string family inet
  120. json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
  121. json_close_object
  122. else
  123. for portset in $(eval "echo \$RULE_${k}_PORTSETS"); do
  124. for proto in icmp tcp udp; do
  125. json_add_object ""
  126. json_add_string type nat
  127. json_add_string target SNAT
  128. json_add_string family inet
  129. json_add_string proto "$proto"
  130. json_add_boolean connlimit_ports 1
  131. json_add_string snat_ip $(eval "echo \$RULE_${k}_IPV4ADDR")
  132. json_add_string snat_port "$portset"
  133. json_close_object
  134. done
  135. done
  136. fi
  137. if [ "$type" = "map-t" ]; then
  138. json_add_object ""
  139. json_add_string type rule
  140. json_add_string family inet6
  141. json_add_string proto all
  142. json_add_string direction in
  143. json_add_string dest "$zone"
  144. json_add_string src "$zone"
  145. json_add_string src_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
  146. json_add_string target ACCEPT
  147. json_close_object
  148. json_add_object ""
  149. json_add_string type rule
  150. json_add_string family inet6
  151. json_add_string proto all
  152. json_add_string direction out
  153. json_add_string dest "$zone"
  154. json_add_string src "$zone"
  155. json_add_string dest_ip $(eval "echo \$RULE_${k}_IPV6ADDR")
  156. json_add_string target ACCEPT
  157. json_close_object
  158. proto_add_ipv6_route $(eval "echo \$RULE_${k}_IPV6ADDR") 128
  159. fi
  160. json_close_array
  161. proto_close_data
  162. proto_send_update "$cfg"
  163. if [ "$type" = "lw4o6" -o "$type" = "map-e" ]; then
  164. json_init
  165. json_add_string name "${cfg}_"
  166. json_add_string ifname "@$(eval "echo \$RULE_${k}_PD6IFACE")"
  167. json_add_string proto "static"
  168. json_add_array ip6addr
  169. json_add_string "" "$(eval "echo \$RULE_${k}_IPV6ADDR")"
  170. json_close_array
  171. json_close_object
  172. ubus call network add_dynamic "$(json_dump)"
  173. fi
  174. }
  175. proto_map_teardown() {
  176. local cfg="$1"
  177. local link="map-$cfg"
  178. json_get_var type type
  179. [ -z "$type" ] && type="map-e"
  180. case "$type" in
  181. "map-e"|"lw4o6") ifdown "${cfg}_" ;;
  182. "map-t") [ -f "/proc/net/nat46/control" ] && echo del $link > /proc/net/nat46/control ;;
  183. esac
  184. rm -f /tmp/map-$cfg.rules
  185. }
  186. proto_map_init_config() {
  187. no_device=1
  188. available=1
  189. proto_config_add_string "rule"
  190. proto_config_add_string "ipaddr"
  191. proto_config_add_int "ip4prefixlen"
  192. proto_config_add_string "ip6prefix"
  193. proto_config_add_int "ip6prefixlen"
  194. proto_config_add_string "peeraddr"
  195. proto_config_add_int "ealen"
  196. proto_config_add_int "psidlen"
  197. proto_config_add_int "psid"
  198. proto_config_add_int "offset"
  199. proto_config_add_string "tunlink"
  200. proto_config_add_int "mtu"
  201. proto_config_add_int "ttl"
  202. proto_config_add_string "zone"
  203. }
  204. [ -n "$INCLUDE_ONLY" ] || {
  205. add_protocol map
  206. }