xref: /openbmc/openbmc/poky/scripts/runqemu-ifdown (revision 8f840685)
1#!/bin/bash
2#
3# QEMU network configuration script to bring down tap devices. This
4# utility needs to be run as root, and will use the ip utility
5#
6# If you find yourself calling this script a lot, you can add the
7# the following to your /etc/sudoers file to be able to run this
8# command without entering your password each time:
9#
10# <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup
11# <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown
12#
13# Copyright (c) 2006-2011 Linux Foundation
14#
15# SPDX-License-Identifier: GPL-2.0-only
16#
17
18usage() {
19	echo "sudo $(basename $0) <tap-dev>"
20}
21
22if [ $EUID -ne 0 ]; then
23	echo "Error: This script (runqemu-ifdown) must be run with root privileges"
24	exit 1
25fi
26
27if [ $# -gt 2 ] || [ $# -lt 1 ]; then
28	usage
29	exit 1
30fi
31
32# backward compatibility
33if [ $# -eq 2 ] ; then
34	echo "Warning: native-sysroot-basedir parameter is ignored. It is no longer needed." >&2
35fi
36
37TAP=$1
38
39if ! ip tuntap del $TAP mode tap 2>/dev/null; then
40	echo "Error: Unable to run up tuntap del"
41	exit 1
42fi
43
44IPTOOL=`which ip 2> /dev/null`
45if [ "x$IPTOOL" = "x" ]; then
46	# better than nothing...
47	IPTOOL=/sbin/ip
48fi
49if [ -x "$IPTOOL" ]; then
50	if `$IPTOOL link show $TAP > /dev/null 2>&1`; then
51		$IPTOOL link del $TAP
52	fi
53fi
54# cleanup the remaining iptables rules
55IPTABLES=`which iptables 2> /dev/null`
56if [ "x$IPTABLES" = "x" ]; then
57	IPTABLES=/sbin/iptables
58fi
59if [ ! -x "$IPTABLES" ]; then
60	echo "$IPTABLES cannot be executed"
61	exit 1
62fi
63
64if [ -z "$OE_TAP_NAME" ]; then
65	OE_TAP_NAME=tap
66fi
67
68n=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 1 ]
69dest=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 2 ]
70$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
71$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
72true
73