Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. #
  2. # Copyright (c) 2005 XenSource Ltd.
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of version 2.1 of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation.
  7. #
  8. # This library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Lesser General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU Lesser General Public
  14. # License along with this library; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. #
  17.  
  18.  
  19. dir=$(dirname "$0")
  20. . "$dir/xen-hotplug-common.sh"
  21. . "$dir/xen-network-common.sh"
  22.  
  23. findCommand "$@"
  24.  
  25. if [ "$command" != "online" ] &&
  26. [ "$command" != "offline" ] &&
  27. [ "$command" != "add" ] &&
  28. [ "$command" != "remove" ]
  29. then
  30. log err "Invalid command: $command"
  31. exit 1
  32. fi
  33.  
  34.  
  35. # Parameters may be read from the environment, the command line arguments, and
  36. # the store, with overriding in that order. The environment is given by the
  37. # driver, the command line is given by the Xend global configuration, and
  38. # store details are given by the per-domain or per-device configuration.
  39.  
  40. evalVariables "$@"
  41.  
  42. # Older versions of Xen do not pass in the type as an argument,
  43. # so the default value is vif.
  44. : ${type_if:=vif}
  45.  
  46. case "$type_if" in
  47. vif)
  48. dev=$vif
  49. ;;
  50. tap)
  51. dev=$INTERFACE
  52. ;;
  53. *)
  54. log err "unknown interface type $type_if"
  55. exit 1
  56. ;;
  57. esac
  58.  
  59. case "$command" in
  60. online | offline)
  61. test "$type_if" != vif && exit 0
  62. ;;
  63. add | remove)
  64. test "$type_if" != tap && exit 0
  65. ;;
  66. esac
  67.  
  68. rename_vif() {
  69. local dev=$1
  70. local vifname=$2
  71.  
  72. # if a custom vifname was chosen and a link with that desired name
  73. # already exists, then stop, before messing up whatever is using
  74. # that interface (e.g. another running domU) because it's likely a
  75. # configuration error
  76. if ip link show "$vifname" >&/dev/null
  77. then
  78. fatal "Cannot rename interface $dev. An interface with name $vifname already exists."
  79. fi
  80. do_or_die ip link set "$dev" name "$vifname"
  81. }
  82.  
  83. if [ "$type_if" = vif ]; then
  84. # Check presence of compulsory args.
  85. XENBUS_PATH="${XENBUS_PATH:?}"
  86. dev="${dev:?}"
  87.  
  88. vifname=$(xenstore_read_default "$XENBUS_PATH/vifname" "")
  89. if [ "$vifname" ]
  90. then
  91. if [ "$command" == "online" ]
  92. then
  93. rename_vif "$dev" "$vifname"
  94. fi
  95. dev="$vifname"
  96. fi
  97. elif [ "$type_if" = tap ]; then
  98. # Check presence of compulsory args.
  99. : ${INTERFACE:?}
  100.  
  101. # Get xenbus_path from device name.
  102. # The name is built like that: "vif${domid}.${devid}-emu".
  103. dev_=${dev#vif}
  104. dev_=${dev_%-emu}
  105. domid=${dev_%.*}
  106. devid=${dev_#*.}
  107.  
  108. XENBUS_PATH="/local/domain/0/backend/vif/$domid/$devid"
  109. vifname=$(xenstore_read_default "$XENBUS_PATH/vifname" "")
  110. if [ "$vifname" ]
  111. then
  112. vifname="${vifname}-emu"
  113. if [ "$command" == "add" ]
  114. then
  115. rename_vif "$dev" "$vifname"
  116. fi
  117. dev="$vifname"
  118. fi
  119. fi
  120.  
  121. ip=${ip:-}
  122. ip=$(xenstore_read_default "$XENBUS_PATH/ip" "$ip")
  123.  
  124. frob_iptable()
  125. {
  126. if [ "$command" == "online" ]
  127. then
  128. local c="-I"
  129. else
  130. local c="-D"
  131. fi
  132.  
  133. iptables "$c" FORWARD -m physdev --physdev-is-bridged --physdev-in "$dev" \
  134. "$@" -j ACCEPT 2>/dev/null &&
  135. iptables "$c" FORWARD -m physdev --physdev-is-bridged --physdev-out "$dev" \
  136. -j ACCEPT 2>/dev/null
  137.  
  138. if [ "$command" == "online" -a $? -ne 0 ]
  139. then
  140. log err "iptables setup failed. This may affect guest networking."
  141. fi
  142. }
  143.  
  144.  
  145. ##
  146. # Add or remove the appropriate entries in the iptables. With antispoofing
  147. # turned on, we have to explicitly allow packets to the interface, regardless
  148. # of the ip setting. If ip is set, then we additionally restrict the packets
  149. # to those coming from the specified networks, though we allow DHCP requests
  150. # as well.
  151. #
  152. handle_iptable()
  153. {
  154. # Check for a working iptables installation. Checking for the iptables
  155. # binary is not sufficient, because the user may not have the appropriate
  156. # modules installed. If iptables is not working, then there's no need to do
  157. # anything with it, so we can just return.
  158. if ! iptables -L -n >&/dev/null
  159. then
  160. return
  161. fi
  162.  
  163. claim_lock "iptables"
  164.  
  165. if [ "$ip" != "" ]
  166. then
  167. local addr
  168. for addr in $ip
  169. do
  170. frob_iptable -s "$addr"
  171. done
  172.  
  173. # Always allow the domain to talk to a DHCP server.
  174. frob_iptable -p udp --sport 68 --dport 67
  175. else
  176. # No IP addresses have been specified, so allow anything.
  177. frob_iptable
  178. fi
  179.  
  180. release_lock "iptables"
  181. }
  182.  
  183.  
  184. ##
  185. # ip_of interface
  186. #
  187. # Print the IP address currently in use at the given interface, or nothing if
  188. # the interface is not up.
  189. #
  190. ip_of()
  191. {
  192. ip -4 -o addr show primary dev "$1" | awk '$3 == "inet" {split($4,i,"/"); print i[1]; exit}'
  193. }
  194.  
  195.  
  196. ##
  197. # dom0_ip
  198. #
  199. # Print the IP address of the interface in dom0 through which we are routing.
  200. # This is the IP address on the interface specified as "netdev" as a parameter
  201. # to these scripts, or eth0 by default. This function will call fatal if no
  202. # such interface could be found.
  203. #
  204. dom0_ip()
  205. {
  206. local nd=${netdev:-eth0}
  207. local result=$(ip_of "$nd")
  208. if [ -z "$result" ]
  209. then
  210. fatal
  211. "$netdev is not up. Bring it up or specify another interface with " \
  212. "netdev=<if> as a parameter to $0."
  213. fi
  214. echo "$result"
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement