1
0

dhcpv6.script 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/bin/sh
  2. [ -z "$2" ] && echo "Error: should be run by odhcpc6c" && exit 1
  3. . /lib/functions.sh
  4. . /lib/netifd/netifd-proto.sh
  5. setup_interface () {
  6. local device="$1"
  7. local prefsig=""
  8. local addrsig=""
  9. # Apply IPv6 / ND configuration
  10. HOPLIMIT=$(cat /proc/sys/net/ipv6/conf/$device/hop_limit)
  11. [ -n "$RA_HOPLIMIT" -a -n "$HOPLIMIT" ] && [ "$RA_HOPLIMIT" -gt "$HOPLIMIT" ] && echo "$RA_HOPLIMIT" > /proc/sys/net/ipv6/conf/$device/hop_limit
  12. [ -n "$RA_MTU" ] && [ "$RA_MTU" -ge 1280 ] && echo "$RA_MTU" > /proc/sys/net/ipv6/conf/$device/mtu 2>/dev/null
  13. [ -n "$RA_REACHABLE" ] && [ "$RA_REACHABLE" -gt 0 ] && echo "$RA_REACHABLE" > /proc/sys/net/ipv6/neigh/$device/base_reachable_time_ms
  14. [ -n "$RA_RETRANSMIT" ] && [ "$RA_RETRANSMIT" -gt 0 ] && echo "$RA_RETRANSMIT" > /proc/sys/net/ipv6/neigh/$device/retrans_time_ms
  15. proto_init_update "*" 1
  16. # Merge RA-DNS
  17. for radns in $RA_DNS; do
  18. local duplicate=0
  19. for dns in $RDNSS; do
  20. [ "$radns" = "$dns" ] && duplicate=1
  21. done
  22. [ "$duplicate" = 0 ] && RDNSS="$RDNSS $radns"
  23. done
  24. for dns in $RDNSS; do
  25. proto_add_dns_server "$dns"
  26. done
  27. for radomain in $RA_DOMAINS; do
  28. local duplicate=0
  29. for domain in $DOMAINS; do
  30. [ "$radomain" = "$domain" ] && duplicate=1
  31. done
  32. [ "$duplicate" = 0 ] && DOMAINS="$DOMAINS $radomain"
  33. done
  34. for domain in $DOMAINS; do
  35. proto_add_dns_search "$domain"
  36. done
  37. for prefix in $PREFIXES; do
  38. proto_add_ipv6_prefix "$prefix"
  39. prefsig="$prefsig ${prefix%%,*}"
  40. local entry="${prefix#*/}"
  41. entry="${entry#*,}"
  42. entry="${entry#*,}"
  43. local valid="${entry%%,*}"
  44. if [ -z "$RA_ADDRESSES" -a -z "$RA_ROUTES" -a \
  45. -z "$RA_DNS" -a "$FAKE_ROUTES" = 1 ]; then
  46. RA_ROUTES="::/0,$SERVER,$valid,4096"
  47. fi
  48. done
  49. for prefix in $USERPREFIX; do
  50. proto_add_ipv6_prefix "$prefix"
  51. done
  52. # Merge addresses
  53. for entry in $RA_ADDRESSES; do
  54. local duplicate=0
  55. local addr="${entry%%/*}"
  56. for dentry in $ADDRESSES; do
  57. local daddr="${dentry%%/*}"
  58. [ "$addr" = "$daddr" ] && duplicate=1
  59. done
  60. [ "$duplicate" = "0" ] && ADDRESSES="$ADDRESSES $entry"
  61. done
  62. for entry in $ADDRESSES; do
  63. local addr="${entry%%/*}"
  64. entry="${entry#*/}"
  65. local mask="${entry%%,*}"
  66. entry="${entry#*,}"
  67. local preferred="${entry%%,*}"
  68. entry="${entry#*,}"
  69. local valid="${entry%%,*}"
  70. proto_add_ipv6_address "$addr" "$mask" "$preferred" "$valid" 1
  71. addrsig="$addrsig $addr/$mask"
  72. if [ -z "$RA_ADDRESSES" -a -z "$RA_ROUTES" -a \
  73. -z "$RA_DNS" -a "$FAKE_ROUTES" = 1 ]; then
  74. RA_ROUTES="::/0,$SERVER,$valid,4096"
  75. fi
  76. # RFC 7278
  77. if [ "$mask" -eq 64 -a -z "$PREFIXES" -a -n "$EXTENDPREFIX" ]; then
  78. proto_add_ipv6_prefix "$addr/$mask,$preferred,$valid"
  79. local raroutes=""
  80. for route in $RA_ROUTES; do
  81. local prefix="${route%%/*}"
  82. local entry="${route#*/}"
  83. local pmask="${entry%%,*}"
  84. entry="${entry#*,}"
  85. local gw="${entry%%,*}"
  86. [ -z "$gw" -a "$mask" = "$pmask" ] && {
  87. case "$addr" in
  88. "${prefix%*::}"*) continue;;
  89. esac
  90. }
  91. raroutes="$raroutes $route"
  92. done
  93. RA_ROUTES="$raroutes"
  94. fi
  95. done
  96. for entry in $RA_ROUTES; do
  97. local duplicate=$NOSOURCEFILTER
  98. local addr="${entry%%/*}"
  99. entry="${entry#*/}"
  100. local mask="${entry%%,*}"
  101. entry="${entry#*,}"
  102. local gw="${entry%%,*}"
  103. entry="${entry#*,}"
  104. local valid="${entry%%,*}"
  105. entry="${entry#*,}"
  106. local metric="${entry%%,*}"
  107. for xentry in $RA_ROUTES; do
  108. local xprefix="${xentry%%,*}"
  109. xentry="${xentry#*,}"
  110. local xgw="${xentry%%,*}"
  111. [ -n "$gw" -a -z "$xgw" -a "$addr/$mask" = "$xprefix" ] && duplicate=1
  112. done
  113. if [ -z "$gw" -o "$duplicate" = 1 ]; then
  114. proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid"
  115. else
  116. for prefix in $PREFIXES $ADDRESSES; do
  117. local paddr="${prefix%%,*}"
  118. proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "$paddr"
  119. done
  120. fi
  121. done
  122. proto_add_data
  123. [ -n "$CER" ] && json_add_string cer "$CER"
  124. [ -n "$PASSTHRU" ] && json_add_string passthru "$PASSTHRU"
  125. [ -n "$ZONE" ] && json_add_string zone "$ZONE"
  126. proto_close_data
  127. proto_send_update "$INTERFACE"
  128. MAPTYPE=""
  129. MAPRULE=""
  130. if [ -n "$MAPE" -a -f /lib/netifd/proto/map.sh ]; then
  131. MAPTYPE="map-e"
  132. MAPRULE="$MAPE"
  133. elif [ -n "$MAPT" -a -f /lib/netifd/proto/map.sh -a -f /proc/net/nat46/control ]; then
  134. MAPTYPE="map-t"
  135. MAPRULE="$MAPT"
  136. elif [ -n "$LW4O6" -a -f /lib/netifd/proto/map.sh ]; then
  137. MAPTYPE="lw4o6"
  138. MAPRULE="$LW4O6"
  139. fi
  140. [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE 2>/dev/null)
  141. if [ "$IFACE_MAP" != 0 -a -n "$MAPTYPE" -a -n "$MAPRULE" ]; then
  142. [ -z "$IFACE_MAP" -o "$IFACE_MAP" = 1 ] && IFACE_MAP=${INTERFACE}_4
  143. json_init
  144. json_add_string name "$IFACE_MAP"
  145. json_add_string ifname "@$INTERFACE"
  146. json_add_string proto map
  147. json_add_string type "$MAPTYPE"
  148. json_add_string _prefsig "$prefsig"
  149. [ "$MAPTYPE" = lw4o6 ] && json_add_string _addrsig "$addrsig"
  150. json_add_string rule "$MAPRULE"
  151. json_add_string tunlink "$INTERFACE"
  152. [ -n "$ZONE_MAP" ] || ZONE_MAP=$ZONE
  153. [ -n "$ZONE_MAP" ] && json_add_string zone "$ZONE_MAP"
  154. [ -n "$ENCAPLIMIT_MAP" ] && json_add_string encaplimit "$ENCAPLIMIT_MAP"
  155. [ -n "$IFACE_MAP_DELEGATE" ] && json_add_boolean delegate "$IFACE_MAP_DELEGATE"
  156. json_close_object
  157. ubus call network add_dynamic "$(json_dump)"
  158. elif [ -n "$AFTR" -a "$IFACE_DSLITE" != 0 -a -f /lib/netifd/proto/dslite.sh ]; then
  159. [ -z "$IFACE_DSLITE" -o "$IFACE_DSLITE" = 1 ] && IFACE_DSLITE=${INTERFACE}_4
  160. json_init
  161. json_add_string name "$IFACE_DSLITE"
  162. json_add_string ifname "@$INTERFACE"
  163. json_add_string proto "dslite"
  164. json_add_string peeraddr "$AFTR"
  165. json_add_string tunlink "$INTERFACE"
  166. [ -n "$ZONE_DSLITE" ] || ZONE_DSLITE=$ZONE
  167. [ -n "$ZONE_DSLITE" ] && json_add_string zone "$ZONE_DSLITE"
  168. [ -n "$ENCAPLIMIT_DSLITE" ] && json_add_string encaplimit "$ENCAPLIMIT_DSLITE"
  169. [ -n "$IFACE_DSLITE_DELEGATE" ] && json_add_boolean delegate "$IFACE_DSLITE_DELEGATE"
  170. json_close_object
  171. ubus call network add_dynamic "$(json_dump)"
  172. elif [ "$IFACE_464XLAT" != 0 -a -f /lib/netifd/proto/464xlat.sh ]; then
  173. [ -z "$IFACE_464XLAT" -o "$IFACE_464XLAT" = 1 ] && IFACE_464XLAT=${INTERFACE}_4
  174. json_init
  175. json_add_string name "$IFACE_464XLAT"
  176. json_add_string ifname "@$INTERFACE"
  177. json_add_string proto "464xlat"
  178. json_add_string tunlink "$INTERFACE"
  179. json_add_string _addrsig "$addrsig"
  180. [ -n "$ZONE_464XLAT" ] || ZONE_464XLAT=$ZONE
  181. [ -n "$ZONE_464XLAT" ] && json_add_string zone "$ZONE_464XLAT"
  182. [ -n "$IFACE_464XLAT_DELEGATE" ] && json_add_boolean delegate "$IFACE_464XLAT_DELEGATE"
  183. json_close_object
  184. ubus call network add_dynamic "$(json_dump)"
  185. fi
  186. # TODO: $SNTP_IP $SIP_IP $SNTP_FQDN $SIP_DOMAIN
  187. }
  188. teardown_interface() {
  189. proto_init_update "*" 0
  190. proto_send_update "$INTERFACE"
  191. }
  192. case "$2" in
  193. bound)
  194. teardown_interface "$1"
  195. setup_interface "$1"
  196. ;;
  197. informed|updated|rebound)
  198. setup_interface "$1"
  199. ;;
  200. ra-updated)
  201. [ -n "$ADDRESSES$RA_ADDRESSES$PREFIXES$USERPREFIX" ] && setup_interface "$1"
  202. ;;
  203. started|stopped|unbound)
  204. teardown_interface "$1"
  205. ;;
  206. esac
  207. # user rules
  208. [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user "$@"
  209. exit 0