config.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. # DEBUG="echo"
  4. find_config() {
  5. local iftype device iface ifaces ifn
  6. for ifn in $interfaces; do
  7. config_get iftype "$ifn" type
  8. config_get iface "$ifn" ifname
  9. case "$iftype" in
  10. bridge) config_get ifaces "$ifn" ifnames;;
  11. esac
  12. config_get device "$ifn" device
  13. for ifc in $device $iface $ifaces; do
  14. [ "$ifc" = "$1" ] && {
  15. echo "$ifn"
  16. return 0
  17. }
  18. done
  19. done
  20. return 1;
  21. }
  22. scan_interfaces() {
  23. local mode iftype iface ifname device
  24. interfaces=
  25. config_cb() {
  26. case "$1" in
  27. interface)
  28. config_set "$2" auto 1
  29. ;;
  30. esac
  31. config_get iftype "$CONFIG_SECTION" TYPE
  32. case "$iftype" in
  33. interface)
  34. config_get proto "$CONFIG_SECTION" proto
  35. append interfaces "$CONFIG_SECTION"
  36. config_get iftype "$CONFIG_SECTION" type
  37. config_get ifname "$CONFIG_SECTION" ifname
  38. config_set "$CONFIG_SECTION" device "$ifname"
  39. case "$iftype" in
  40. bridge)
  41. config_set "$CONFIG_SECTION" ifnames "$ifname"
  42. config_set "$CONFIG_SECTION" ifname br-"$CONFIG_SECTION"
  43. ;;
  44. esac
  45. ( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
  46. ;;
  47. esac
  48. }
  49. config_load network
  50. }
  51. add_vlan() {
  52. local vif="${1%\.*}"
  53. [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
  54. ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
  55. $DEBUG vconfig add "$vif" "${1##*\.}"
  56. }
  57. }
  58. setup_interface() {
  59. local iface="$1"
  60. local config="$2"
  61. local proto
  62. [ -n "$config" ] || {
  63. config=$(find_config "$iface")
  64. [ "$?" = 0 ] || return 1
  65. }
  66. proto="${3:-$(config_get "$config" proto)}"
  67. config_get iftype "$config" type
  68. ifconfig "$iface" 2>/dev/null >/dev/null && {
  69. # make sure the interface is removed from any existing bridge
  70. unbridge "$iface"
  71. }
  72. # Setup VLAN interfaces
  73. add_vlan "$iface"
  74. # Setup bridging
  75. case "$iftype" in
  76. bridge)
  77. ifconfig "$iface" up 2>/dev/null >/dev/null
  78. ifconfig "br-$config" 2>/dev/null >/dev/null && {
  79. $DEBUG brctl addif "br-$config" "$iface"
  80. return 0
  81. } || {
  82. $DEBUG brctl addbr "br-$config"
  83. $DEBUG brctl setfd "br-$config" 0
  84. $DEBUG brctl addif "br-$config" "$iface"
  85. iface="br-$config"
  86. }
  87. ;;
  88. esac
  89. # Interface settings
  90. config_get mtu "$config" mtu
  91. $DEBUG ifconfig "$iface" ${mtu:+mtu $mtu} up
  92. pidfile="/var/run/$iface.pid"
  93. case "$proto" in
  94. static)
  95. config_get ipaddr "$config" ipaddr
  96. config_get netmask "$config" netmask
  97. [ -z "$ipaddr" -o -z "$netmask" ] && return 1
  98. config_get ip6addr "$config" ip6addr
  99. config_get gateway "$config" gateway
  100. config_get dns "$config" dns
  101. $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask"
  102. [ -z "$gateway" ] || route add default gw "$gateway"
  103. [ -z "$dns" -o -f /tmp/resolv.conf ] || {
  104. for ns in $dns; do
  105. echo "nameserver $ns" >> /tmp/resolv.conf
  106. done
  107. }
  108. env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=static /sbin/hotplug "iface" &
  109. ;;
  110. dhcp)
  111. # prevent udhcpc from starting more than once
  112. lock "/var/lock/dhcp-$iface"
  113. pid="$(cat "$pidfile" 2>/dev/null)"
  114. [ -d "/proc/$pid" ] && grep udhcpc "/proc/${pid}/cmdline" >/dev/null 2>/dev/null && {
  115. lock -u "/var/lock/dhcp-$iface"
  116. return 0
  117. }
  118. config_get ipaddr "$config" ipaddr
  119. config_get netmask "$config" netmask
  120. config_get hostname "$config" hostname
  121. config_get proto1 "$config" proto
  122. [ -z "$ipaddr" ] || \
  123. $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
  124. # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
  125. [ "$proto1" != "$proto" ] && dhcpopts="-n -q"
  126. $DEBUG eval udhcpc -t 0 -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} -b -p "$pidfile" ${dhcpopts:- -R &}
  127. lock -u "/var/lock/dhcp-$iface"
  128. ;;
  129. *)
  130. if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
  131. eval "setup_interface_$proto '$iface' '$config' '$proto'"
  132. else
  133. echo "Interface type $proto not supported."
  134. return 1
  135. fi
  136. ;;
  137. esac
  138. }
  139. unbridge() {
  140. local dev="$1"
  141. local brdev
  142. brctl show | grep "$dev" >/dev/null && {
  143. # interface is still part of a bridge, correct that
  144. for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
  145. brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
  146. done
  147. }
  148. }