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