dhcpv6.script 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. done
  69. for entry in $RA_ROUTES; do
  70. local duplicate=$NOSOURCEFILTER
  71. local addr="${entry%%/*}"
  72. entry="${entry#*/}"
  73. local mask="${entry%%,*}"
  74. entry="${entry#*,}"
  75. local gw="${entry%%,*}"
  76. entry="${entry#*,}"
  77. local valid="${entry%%,*}"
  78. entry="${entry#*,}"
  79. local metric="${entry%%,*}"
  80. for xentry in $RA_ROUTES; do
  81. local xprefix="${xentry%%,*}"
  82. xentry="${xentry#*,}"
  83. local xgw="${xentry%%,*}"
  84. [ -n "$gw" -a -z "$xgw" -a "$addr/$mask" = "$xprefix" ] && duplicate=1
  85. done
  86. if [ -z "$gw" -o "$duplicate" = 1 ]; then
  87. proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid"
  88. else
  89. for prefix in $PREFIXES $ADDRESSES; do
  90. local paddr="${prefix%%,*}"
  91. proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "$paddr"
  92. done
  93. fi
  94. done
  95. proto_add_data
  96. [ -n "$CER" ] && json_add_string cer "$CER"
  97. [ -n "$PASSTHRU" ] && json_add_string passthru "$PASSTHRU"
  98. [ -n "$ZONE" ] && json_add_string zone "$ZONE"
  99. proto_close_data
  100. proto_send_update "$INTERFACE"
  101. MAPTYPE=""
  102. MAPRULE=""
  103. if [ -n "$MAPE" -a -f /lib/netifd/proto/map.sh ]; then
  104. MAPTYPE="map-e"
  105. MAPRULE="$MAPE"
  106. elif [ -n "$MAPT" -a -f /lib/netifd/proto/map.sh -a -f /proc/net/nat46/control ]; then
  107. MAPTYPE="map-t"
  108. MAPRULE="$MAPT"
  109. elif [ -n "$LW4O6" -a -f /lib/netifd/proto/map.sh ]; then
  110. MAPTYPE="lw4o6"
  111. MAPRULE="$LW4O6"
  112. fi
  113. [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE 2>/dev/null)
  114. if [ "$IFACE_MAP" != 0 -a -n "$MAPTYPE" -a -n "$MAPRULE" ]; then
  115. [ -z "$IFACE_MAP" -o "$IFACE_MAP" = 1 ] && IFACE_MAP=${INTERFACE}_4
  116. json_init
  117. json_add_string name "$IFACE_MAP"
  118. json_add_string ifname "@$INTERFACE"
  119. json_add_string proto map
  120. json_add_string type "$MAPTYPE"
  121. json_add_string _prefsig "$prefsig"
  122. [ "$MAPTYPE" = lw4o6 ] && json_add_string _addrsig "$addrsig"
  123. json_add_string rule "$MAPRULE"
  124. json_add_string tunlink "$INTERFACE"
  125. [ -n "$ZONE_MAP" ] || ZONE_MAP=$ZONE
  126. [ -n "$ZONE_MAP" ] && json_add_string zone "$ZONE_MAP"
  127. [ -n "$IFACE_MAP_DELEGATE" ] && json_add_boolean delegate "$IFACE_MAP_DELEGATE"
  128. json_close_object
  129. ubus call network add_dynamic "$(json_dump)"
  130. elif [ -n "$AFTR" -a "$IFACE_DSLITE" != 0 -a -f /lib/netifd/proto/dslite.sh ]; then
  131. [ -z "$IFACE_DSLITE" -o "$IFACE_DSLITE" = 1 ] && IFACE_DSLITE=${INTERFACE}_4
  132. json_init
  133. json_add_string name "$IFACE_DSLITE"
  134. json_add_string ifname "@$INTERFACE"
  135. json_add_string proto "dslite"
  136. json_add_string peeraddr "$AFTR"
  137. json_add_string tunlink "$INTERFACE"
  138. [ -n "$ZONE_DSLITE" ] || ZONE_DSLITE=$ZONE
  139. [ -n "$ZONE_DSLITE" ] && json_add_string zone "$ZONE_DSLITE"
  140. [ -n "$IFACE_DSLITE_DELEGATE" ] && json_add_boolean delegate "$IFACE_DSLITE_DELEGATE"
  141. json_close_object
  142. ubus call network add_dynamic "$(json_dump)"
  143. elif [ "$IFACE_464XLAT" != 0 -a -f /lib/netifd/proto/464xlat.sh ]; then
  144. [ -z "$IFACE_464XLAT" -o "$IFACE_464XLAT" = 1 ] && IFACE_464XLAT=${INTERFACE}_4
  145. json_init
  146. json_add_string name "$IFACE_464XLAT"
  147. json_add_string ifname "@$INTERFACE"
  148. json_add_string proto "464xlat"
  149. json_add_string tunlink "$INTERFACE"
  150. json_add_string _addrsig "$addrsig"
  151. [ -n "$ZONE_464XLAT" ] || ZONE_464XLAT=$ZONE
  152. [ -n "$ZONE_464XLAT" ] && json_add_string zone "$ZONE_464XLAT"
  153. [ -n "$IFACE_464XLAT_DELEGATE" ] && json_add_boolean delegate "$IFACE_464XLAT_DELEGATE"
  154. json_close_object
  155. ubus call network add_dynamic "$(json_dump)"
  156. fi
  157. # Apply IPv6 / ND configuration
  158. HOPLIMIT=$(cat /proc/sys/net/ipv6/conf/$device/hop_limit)
  159. [ -n "$RA_HOPLIMIT" -a -n "$HOPLIMIT" ] && [ "$RA_HOPLIMIT" -gt "$HOPLIMIT" ] && echo "$RA_HOPLIMIT" > /proc/sys/net/ipv6/conf/$device/hop_limit
  160. [ -n "$RA_MTU" ] && [ "$RA_MTU" -gt 0 ] && echo "$RA_MTU" > /proc/sys/net/ipv6/conf/$device/mtu
  161. [ -n "$RA_REACHABLE" ] && [ "$RA_REACHABLE" -gt 0 ] && echo "$RA_REACHABLE" > /proc/sys/net/ipv6/neigh/$device/base_reachable_time_ms
  162. [ -n "$RA_RETRANSMIT" ] && [ "$RA_RETRANSMIT" -gt 0 ] && echo "$RA_RETRANSMIT" > /proc/sys/net/ipv6/neigh/$device/retrans_time_ms
  163. # TODO: $SNTP_IP $SIP_IP $SNTP_FQDN $SIP_DOMAIN
  164. }
  165. teardown_interface() {
  166. proto_init_update "*" 0
  167. proto_send_update "$INTERFACE"
  168. }
  169. case "$2" in
  170. bound)
  171. teardown_interface "$1"
  172. setup_interface "$1"
  173. ;;
  174. informed|updated|rebound)
  175. setup_interface "$1"
  176. ;;
  177. ra-updated)
  178. [ -n "$ADDRESSES$RA_ADDRESSES$PREFIXES$USERPREFIX" ] && setup_interface "$1"
  179. ;;
  180. started|stopped|unbound)
  181. teardown_interface "$1"
  182. ;;
  183. esac
  184. # user rules
  185. [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user
  186. exit 0