blob: 78be28812d4a9ee6682e8cd9e92f6dc6075bc6ea [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/bash
2#
3# QEMU network configuration script to bring down tap devices. This
Patrick Williams520786c2023-06-25 16:20:36 -05004# utility needs to be run as root, and will use the ip utility
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
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#
Brad Bishopc342db32019-05-15 21:57:59 -040015# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
18usage() {
19 echo "sudo $(basename $0) <tap-dev> <native-sysroot-basedir>"
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 [ $# -ne 2 ]; then
28 usage
29 exit 1
30fi
31
32TAP=$1
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033STAGING_BINDIR_NATIVE=$2
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034
Patrick Williams520786c2023-06-25 16:20:36 -050035if !ip tuntap del $TAP mode tap 2>/dev/null; then
36 echo "Error: Unable to run up tuntap del"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 exit 1
38fi
39
Patrick Williams520786c2023-06-25 16:20:36 -050040IPTOOL=`which ip 2> /dev/null`
41if [ "x$IPTOOL" = "x" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080042 # better than nothing...
Patrick Williams520786c2023-06-25 16:20:36 -050043 IPTOOL=/sbin/ip
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044fi
Patrick Williams520786c2023-06-25 16:20:36 -050045if [ -x "$IPTOOL" ]; then
46 if `$IPTOOL link show $TAP > /dev/null 2>&1`; then
47 $IPTOOL link del $TAP
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048 fi
49fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050# cleanup the remaining iptables rules
51IPTABLES=`which iptables 2> /dev/null`
52if [ "x$IPTABLES" = "x" ]; then
53 IPTABLES=/sbin/iptables
54fi
55if [ ! -x "$IPTABLES" ]; then
56 echo "$IPTABLES cannot be executed"
57 exit 1
58fi
59n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
60dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]
61$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32
62$IPTABLES -D POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000063true