1
0

dhcpv6.script 6.4 KB

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