config.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. config_get iftype "$CONFIG_SECTION" TYPE
  27. case "$iftype" in
  28. interface)
  29. config_get proto "$CONFIG_SECTION" proto
  30. append interfaces "$CONFIG_SECTION"
  31. config_get iftype "$CONFIG_SECTION" type
  32. config_get ifname "$CONFIG_SECTION" ifname
  33. config_set "$CONFIG_SECTION" device "$ifname"
  34. case "$iftype" in
  35. bridge)
  36. config_set "$CONFIG_SECTION" ifnames "$ifname"
  37. config_set "$CONFIG_SECTION" ifname br-"$CONFIG_SECTION"
  38. ;;
  39. esac
  40. ( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
  41. ;;
  42. esac
  43. }
  44. config_load network
  45. }
  46. add_vlan() {
  47. local vif="${1%\.*}"
  48. [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
  49. ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
  50. $DEBUG vconfig add "$vif" "${1##*\.}"
  51. }
  52. }
  53. setup_interface() {
  54. local iface="$1"
  55. local config="$2"
  56. local proto
  57. [ -n "$config" ] || {
  58. config=$(find_config "$iface")
  59. [ "$?" = 0 ] || return 1
  60. }
  61. proto="${3:-$(config_get "$config" proto)}"
  62. config_get iftype "$config" type
  63. # Setup VLAN interfaces
  64. add_vlan "$iface"
  65. # Setup bridging
  66. case "$iftype" in
  67. bridge)
  68. ifconfig "$iface" up 2>/dev/null >/dev/null
  69. ifconfig "br-$config" 2>/dev/null >/dev/null && {
  70. $DEBUG brctl addif "br-$config" "$iface"
  71. return 0
  72. } || {
  73. $DEBUG brctl addbr "br-$config"
  74. $DEBUG brctl setfd "br-$config" 0
  75. $DEBUG brctl addif "br-$config" "$iface"
  76. iface="br-$config"
  77. }
  78. ;;
  79. esac
  80. # Interface settings
  81. config_get mtu "$config" mtu
  82. $DEBUG ifconfig "$iface" ${mtu:+mtu $mtu} up
  83. pidfile="/var/run/$iface.pid"
  84. case "$proto" in
  85. static)
  86. config_get ipaddr "$config" ipaddr
  87. config_get netmask "$config" netmask
  88. [ -z "$ipaddr" -o -z "$netmask" ] && return 1
  89. config_get ip6addr "$config" ip6addr
  90. config_get gateway "$config" gateway
  91. config_get dns "$config" dns
  92. $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask"
  93. [ -z "$gateway" ] || route add default gw "$gateway"
  94. [ -z "$dns" -o -f /tmp/resolv.conf ] || {
  95. for ns in $dns; do
  96. echo "nameserver $ns" >> /tmp/resolv.conf
  97. done
  98. }
  99. env -i ACTION="ifup" INTERFACE="config" DEVICE="$iface" PROTO=static /sbin/hotplug "iface" &
  100. ;;
  101. dhcp)
  102. pid="$(cat "$pidfile" 2>/dev/null)"
  103. [ -n "$pid" -a -d "/proc/$pid" ] && kill -9 "$pid"
  104. config_get ipaddr "$config" ipaddr
  105. config_get netmask "$config" netmask
  106. config_get hostname "$config" hostname
  107. config_get proto1 "$config" proto
  108. [ -z "$ipaddr" ] || \
  109. $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
  110. # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
  111. [ "$proto1" != "$proto" ] && dhcpopts="-n -q"
  112. $DEBUG udhcpc -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} -b -p "$pidfile" ${dhcpopts:- -R &}
  113. ;;
  114. *)
  115. if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
  116. eval "setup_interface_$proto '$iface' '$config' '$proto'"
  117. else
  118. echo "Interface type $proto not supported."
  119. return 1
  120. fi
  121. ;;
  122. esac
  123. }