blob: 822a2a39b9fb0023a01e3d7bbba8febbbfafc9d6 [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() {
Andrew Geissler8f840682023-07-21 09:09:43 -050019 echo "sudo $(basename $0) <tap-dev>"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020}
21
22if [ $EUID -ne 0 ]; then
23 echo "Error: This script (runqemu-ifdown) must be run with root privileges"
24 exit 1
25fi
26
Andrew Geissler8f840682023-07-21 09:09:43 -050027if [ $# -gt 2 ] || [ $# -lt 1 ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 usage
29 exit 1
30fi
31
Andrew Geissler8f840682023-07-21 09:09:43 -050032# backward compatibility
33if [ $# -eq 2 ] ; then
34 echo "Warning: native-sysroot-basedir parameter is ignored. It is no longer needed." >&2
35fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
Andrew Geissler8f840682023-07-21 09:09:43 -050037TAP=$1
38
39if ! ip tuntap del $TAP mode tap 2>/dev/null; then
Patrick Williams520786c2023-06-25 16:20:36 -050040 echo "Error: Unable to run up tuntap del"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 exit 1
42fi
43
Patrick Williams520786c2023-06-25 16:20:36 -050044IPTOOL=`which ip 2> /dev/null`
45if [ "x$IPTOOL" = "x" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080046 # better than nothing...
Patrick Williams520786c2023-06-25 16:20:36 -050047 IPTOOL=/sbin/ip
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048fi
Patrick Williams520786c2023-06-25 16:20:36 -050049if [ -x "$IPTOOL" ]; then
50 if `$IPTOOL link show $TAP > /dev/null 2>&1`; then
51 $IPTOOL link del $TAP
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080052 fi
53fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054# 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
Andrew Geissler8f840682023-07-21 09:09:43 -050063
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 ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070$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
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000072true