ifupdown.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/sh
  2. #####################################################################
  3. ## Purpose
  4. # This file is executed by ifupdown in pre-up, post-up, pre-down and
  5. # post-down phases of network interface configuration. It allows
  6. # ifup(8), and ifdown(8) to manage wpa_supplicant(8) and wpa_cli(8)
  7. # processes running in daemon mode.
  8. #
  9. # /etc/wpa_supplicant/functions.sh is sourced by this file.
  10. #
  11. # This file is provided by the wpasupplicant package.
  12. #####################################################################
  13. # Copyright (C) 2006 - 2009 Debian/Ubuntu wpasupplicant Maintainers
  14. # <pkg-wpa-devel@lists.alioth.debian.org>
  15. #
  16. # This program is free software; you can redistribute it and/or
  17. # modify it under the terms of the GNU General Public License
  18. # as published by the Free Software Foundation; either version 2
  19. # of the License, or (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU General Public License for more details.
  25. #
  26. # On Debian GNU/Linux systems, the text of the GPL license,
  27. # version 2, can be found in /usr/share/common-licenses/GPL-2.
  28. if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
  29. set -x
  30. fi
  31. # quit if we're called for the loopback
  32. if [ "$IFACE" = lo ]; then
  33. exit 0
  34. fi
  35. # allow wpa_supplicant interface to be specified via wpa-iface
  36. # useful for starting wpa_supplicant on one interface of a bridge
  37. if [ -n "$IF_WPA_IFACE" ]; then
  38. WPA_IFACE="$IF_WPA_IFACE"
  39. else
  40. WPA_IFACE="$IFACE"
  41. fi
  42. # source functions
  43. if [ -f /etc/wpa_supplicant/functions.sh ]; then
  44. . /etc/wpa_supplicant/functions.sh
  45. else
  46. exit 0
  47. fi
  48. # quit if executables are not installed
  49. if [ ! -x "$WPA_SUP_BIN" ] || [ ! -x "$WPA_CLI_BIN" ]; then
  50. exit 0
  51. fi
  52. do_start () {
  53. if test_wpa_cli; then
  54. # if wpa_action is active for this IFACE, do nothing
  55. ifupdown_locked && exit 0
  56. # if the administrator is calling ifup, say something useful
  57. if [ "$PHASE" = "pre-up" ]; then
  58. wpa_msg stderr "wpa_action is managing ifup/ifdown state of $WPA_IFACE"
  59. wpa_msg stderr "execute \`ifdown --force $WPA_IFACE' to stop wpa_action"
  60. fi
  61. exit 1
  62. elif ! set | grep -q "^IF_WPA"; then
  63. # no wpa- option defined for IFACE, do nothing
  64. exit 0
  65. fi
  66. # ensure stale ifupdown_lock marker is purged
  67. ifupdown_unlock
  68. # preliminary sanity checks for roaming daemon
  69. if [ -n "$IF_WPA_ROAM" ]; then
  70. if [ "$METHOD" != "manual" ]; then
  71. wpa_msg stderr "wpa-roam can only be used with the \"manual\" inet METHOD"
  72. exit 1
  73. fi
  74. if [ -n "$IF_WPA_MAPPING_SCRIPT" ]; then
  75. if ! type "$IF_WPA_MAPPING_SCRIPT" >/dev/null; then
  76. wpa_msg stderr "wpa-mapping-script \"$IF_WPA_MAPPING_SCRIPT\" is not valid"
  77. exit 1
  78. fi
  79. fi
  80. if [ -n "$IF_WPA_MAPPING_SCRIPT_PRIORITY" ] && [ -z "$IF_WPA_MAPPING_SCRIPT" ]; then
  81. wpa_msg stderr "\"wpa-mapping-script-priority 1\" is invalid without a wpa-mapping-script"
  82. exit 1
  83. fi
  84. IF_WPA_CONF="$IF_WPA_ROAM"
  85. WPA_ACTION_SCRIPT="/sbin/wpa_action"
  86. fi
  87. # master function; determines if ifupdown.sh should do something or not
  88. if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
  89. if [ ! -s "$IF_WPA_CONF" ]; then
  90. wpa_msg stderr "cannot read contents of $IF_WPA_CONF"
  91. exit 1
  92. fi
  93. WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g' -e 's/[[:space:]]\+.*$//g' \
  94. -e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p' "$IF_WPA_CONF")
  95. if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
  96. WPA_CTRL_DIR="$WPA_SUP_CONF_CTRL_DIR"
  97. WPA_SUP_CONF="-c $IF_WPA_CONF"
  98. else
  99. # specify the default ctrl_interface since none was defined in
  100. # the given IF_WPA_CONF
  101. WPA_SUP_CONF="-c $IF_WPA_CONF -C $WPA_CTRL_DIR"
  102. fi
  103. else
  104. # specify the default ctrl_interface
  105. WPA_SUP_CONF="-C $WPA_CTRL_DIR"
  106. fi
  107. }
  108. do_stop () {
  109. if test_wpa_cli; then
  110. # if wpa_action is active for this IFACE and calling ifdown,
  111. # do nothing
  112. ifupdown_locked && exit 0
  113. elif test_wpa_supplicant; then
  114. # wpa_supplicant process exists for this IFACE, but wpa_cli
  115. # process does not. Allow stop mode to kill this process.
  116. :
  117. else
  118. exit 0
  119. fi
  120. }
  121. case "$MODE" in
  122. start)
  123. do_start
  124. case "$PHASE" in
  125. pre-up)
  126. kill_wpa_supplicant
  127. init_wpa_supplicant || exit 1
  128. conf_wpa_supplicant || { kill_wpa_supplicant; exit 1; }
  129. ;;
  130. post-up)
  131. init_wpa_cli || { kill_wpa_supplicant; exit 1; }
  132. ;;
  133. esac
  134. ;;
  135. stop)
  136. do_stop
  137. case "$PHASE" in
  138. pre-down)
  139. kill_wpa_cli
  140. ;;
  141. post-down)
  142. kill_wpa_supplicant
  143. ;;
  144. *)
  145. wpa_msg stderr "unknown phase: \"$PHASE\""
  146. exit 1
  147. ;;
  148. esac
  149. ;;
  150. *)
  151. wpa_msg stderr "unknown mode: \"$MODE\""
  152. exit 1
  153. ;;
  154. esac
  155. exit 0