default.script 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/sh
  2. [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1
  3. . /etc/functions.sh
  4. include /lib/network
  5. RESOLV_CONF="/tmp/resolv.conf.auto"
  6. change_state () {
  7. [ -n "$ifc" ] || return
  8. uci_revert_state "$1" "$2" "$3" "$4"
  9. uci_set_state "$1" "$2" "$3" "$4"
  10. }
  11. uci_get() {
  12. [ -n "$ifc" ] || return
  13. uci -P /dev/null get "$1" 2>/dev/null
  14. }
  15. setup_interface () {
  16. local old_ip
  17. local old_broadcast
  18. local old_subnet
  19. local old_router
  20. local old_dns
  21. local user_dns
  22. local user_router
  23. [ -n "$ifc" ] && {
  24. config_get old_ip "$ifc" ipaddr
  25. config_get old_broadcast "$ifc" broadcast
  26. config_get old_subnet "$ifc" netmask
  27. }
  28. [ "$1" = "ifup" ] \
  29. || [ "$ip" != "$old_ip" ] \
  30. || [ "${broadcast:-+}" != "$old_broadcast" ] \
  31. || [ "${subnet:-255.255.255.0}" != "$old_subnet" ] && {
  32. echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
  33. ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}
  34. change_state network "$ifc" ipaddr "$ip"
  35. change_state network "$ifc" broadcast "${broadcast:-+}"
  36. change_state network "$ifc" netmask "${subnet:-255.255.255.0}"
  37. }
  38. # Default Route
  39. [ -n "$ifc" ] && {
  40. change_state network "$ifc" lease_gateway "$router"
  41. config_get old_router "$ifc" gateway
  42. user_router=$(uci_get "network.$ifc.gateway")
  43. [ -n "$user_router" ] && router="$user_router"
  44. }
  45. [ -n "$router" ] && [ "$router" != "0.0.0.0" ] && [ "$router" != "255.255.255.255" ] && [ "$router" != "$old_router" ] && {
  46. echo "udhcpc: setting default routers: $router"
  47. local valid_gw=""
  48. for i in $router ; do
  49. route add default gw $i dev $interface
  50. valid_gw="${valid_gw:+$valid_gw|}$i"
  51. done
  52. eval $(route -n | awk '
  53. /^0.0.0.0\W{9}('$valid_gw')\W/ {next}
  54. /^0.0.0.0/ {print "route del -net "$1" gw "$2";"}
  55. ')
  56. change_state network "$ifc" gateway "$router"
  57. }
  58. # CIDR STATIC ROUTES (rfc3442)
  59. [ -n "$cidrroute" ] && {
  60. # This defines how many CIDR Routes can be assigned so that we do not enter
  61. # an endless loop on malformed data
  62. MAXCIDRROUTES=24;
  63. while [ ${MAXCIDRROUTES} -gt "0" ]; do
  64. # Format is
  65. # $MASK $NW $GW
  66. # $NW == AAA.BBB.CCC.DDD
  67. # $GW == EEE.FFF.CCC.DDD
  68. # $MASK AAA.[BBB].[CCC].[DDD] EEE.FFF.GGG.HHH
  69. # 1 2 3 4 5 6 7 8 9
  70. MASK=$(echo $cidrroute | awk '{ print $1 }')
  71. if [ ${MASK} = "0" ] ; then
  72. # $MASK EEE.FFF.GGG.HHH
  73. # 1 2 3 5 6
  74. NW="0"
  75. GW=$(echo $cidrroute | awk '{ print $2"."$3"."$4"."$5 }' )
  76. elif [ ${MASK} -le "8" ] ; then
  77. # $MASK AAA EEE.FFF.GGG.HHH
  78. # 1 2 3 5 6 7
  79. NW=$(echo $cidrroute | awk '{ print $2 }' )
  80. GW=$(echo $cidrroute | awk '{ print $3"."$4"."$5"."$6 }' )
  81. elif [ ${MASK} -le "16" ] ; then
  82. # $MASK AAA.BBB EEE.FFF.GGG.HHH
  83. # 1 2 3 5 6 7 8
  84. NW=$(echo $cidrroute | awk '{ print $2"."$3 }' )
  85. GW=$(echo $cidrroute | awk '{ print $4"."$5"."$6"."$7 }' )
  86. elif [ ${MASK} -le "24" ] ; then
  87. # $MASK AAA.BBB.CCC EEE.FFF.GGG.HHH
  88. # 1 2 3 4 5 6 7 8
  89. NW=$(echo $cidrroute | awk '{ print $2"."$3"."$4 }' )
  90. GW=$(echo $cidrroute | awk '{ print $5"."$6"."$7"."$8 }' )
  91. else
  92. # $MASK AAA.BBB.CCC.DDD EEE.FFF.GGG.HHH
  93. # 1 2 3 4 5 6 7 8 9
  94. NW=$(echo $cidrroute | awk '{ print $2"."$3"."$4"."$5 }' )
  95. GW=$(echo $cidrroute | awk '{ print $6"."$7"."$8"."$9 }' )
  96. fi
  97. echo [$ROUTECOUNTER] Route Network: $NW/$MASK Gateway: $GW on $interface
  98. # TODO: Check for malformed data here to eliminate counter workaround
  99. # Malformed data is: ... or xxx... or xxx.yyy.. or xxx.yyy.zzz.
  100. [ -n "$NW" ] && [ -n "$GW" ] && {
  101. route add $NW gw $GW dev $interface
  102. }
  103. # Clear the strings incase they don't get set next time around
  104. if [ ${NW} = "0" ]; then
  105. NW=""
  106. fi
  107. TMP="$MASK $NW $GW "
  108. NW=""
  109. GW=""
  110. # Remove the '.' so that we can delete them from the input with sed
  111. TMP=$(echo $TMP | sed "s/\./ /g")
  112. # Remove the previous entry from cidrroute
  113. cidrroute=$(echo $cidrroute | sed "s/$TMP//g")
  114. # Add to counter
  115. let ROUTECOUNTER=$ROUTECOUNTER+1;
  116. let MAXCIDRROUTES=$MAXCIDRROUTES-1;
  117. # Leave the loop if cidrroutes is empty (we've parsed everything)
  118. [ ! -n "$cidrroute" ] && break
  119. done
  120. echo "done."
  121. }
  122. # DNS
  123. config_get old_dns "$ifc" dns
  124. user_dns=$(uci_get "network.$ifc.dns")
  125. [ -n "$user_dns" ] && dns="$user_dns"
  126. [ -n "$dns" ] && [ ! -s "${RESOLV_CONF}" -o "$dns" != "$old_dns" ] && {
  127. echo "udhcpc: setting dns servers: $dns"
  128. echo -n > "${RESOLV_CONF}.tmp"
  129. for i in $dns ; do
  130. echo "nameserver $i" >> "${RESOLV_CONF}.tmp"
  131. done
  132. ${domain:+echo search $domain} >> "${RESOLV_CONF}.tmp"
  133. mv "${RESOLV_CONF}.tmp" "$RESOLV_CONF"
  134. change_state network "$ifc" dnsdomain "$domain"
  135. change_state network "$ifc" dns "$dns"
  136. }
  137. [ -n "$ifc" ] || return
  138. # UCI State
  139. change_state network "$ifc" lease_server "$serverid"
  140. change_state network "$ifc" lease_acquired "$(date '+%s')"
  141. change_state network "$ifc" lease_lifetime "$lease"
  142. [ -n "$ntpsrv" ] && change_state network "$ifc" lease_ntpsrv "$ntpsrv"
  143. [ -n "$timesvr" ] && change_state network "$ifc" lease_timesrv "$timesvr"
  144. [ -n "$hostname" ] && change_state network "$ifc" lease_hostname "$hostname"
  145. [ -n "$timezone" ] && change_state network "$ifc" lease_timezone "$timezone"
  146. # Hotplug
  147. env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
  148. }
  149. scan_interfaces
  150. applied=
  151. for ifc in $interfaces __default; do
  152. if [ "$ifc" = __default ]; then
  153. ifc=""
  154. [ -n "$applied" ] && continue
  155. else
  156. config_get ifname "$ifc" ifname
  157. [ "$ifname" = "$interface" ] || continue
  158. config_get proto "$ifc" proto
  159. [ "$proto" = "dhcp" ] || continue
  160. applied=true
  161. fi
  162. case "$1" in
  163. deconfig)
  164. ifconfig "$interface" 0.0.0.0
  165. [ -n "$ifc" ] && {
  166. env -i ACTION="ifdown" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
  167. config_get device "$ifc" device
  168. config_get ifname "$ifc" ifname
  169. config_get aliases "$ifc" aliases
  170. uci_revert_state network "$ifc"
  171. [ -n "$device" ] && uci_set_state network "$ifc" device "$device"
  172. [ -n "$ifname" ] && uci_set_state network "$ifc" ifname "$ifname"
  173. [ -n "$aliases" ] && uci_set_state network "$ifc" aliases "$aliases"
  174. }
  175. ;;
  176. renew)
  177. setup_interface update
  178. ;;
  179. bound)
  180. setup_interface ifup
  181. ;;
  182. esac
  183. done
  184. # user rules
  185. [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
  186. exit 0