| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # QEMU network configuration script to bring down tap devices. This | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 4 | # utility needs to be run as root, and will use the ip utility | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 15 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 |  | 
|  | 18 | usage() { | 
| Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame^] | 19 | echo "sudo $(basename $0) <tap-dev>" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | } | 
|  | 21 |  | 
|  | 22 | if [ $EUID -ne 0 ]; then | 
|  | 23 | echo "Error: This script (runqemu-ifdown) must be run with root privileges" | 
|  | 24 | exit 1 | 
|  | 25 | fi | 
|  | 26 |  | 
| Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame^] | 27 | if [ $# -gt 2 ] || [ $# -lt 1 ]; then | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | usage | 
|  | 29 | exit 1 | 
|  | 30 | fi | 
|  | 31 |  | 
| Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame^] | 32 | # backward compatibility | 
|  | 33 | if [ $# -eq 2 ] ; then | 
|  | 34 | echo "Warning: native-sysroot-basedir parameter is ignored. It is no longer needed." >&2 | 
|  | 35 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 |  | 
| Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame^] | 37 | TAP=$1 | 
|  | 38 |  | 
|  | 39 | if ! ip tuntap del $TAP mode tap 2>/dev/null; then | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 40 | echo "Error: Unable to run up tuntap del" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | exit 1 | 
|  | 42 | fi | 
|  | 43 |  | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 44 | IPTOOL=`which ip 2> /dev/null` | 
|  | 45 | if [ "x$IPTOOL" = "x" ]; then | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 46 | # better than nothing... | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 47 | IPTOOL=/sbin/ip | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 48 | fi | 
| Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 49 | if [ -x "$IPTOOL" ]; then | 
|  | 50 | if `$IPTOOL link show $TAP > /dev/null 2>&1`; then | 
|  | 51 | $IPTOOL link del $TAP | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 52 | fi | 
|  | 53 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | # cleanup the remaining iptables rules | 
|  | 55 | IPTABLES=`which iptables 2> /dev/null` | 
|  | 56 | if [ "x$IPTABLES" = "x" ]; then | 
|  | 57 | IPTABLES=/sbin/iptables | 
|  | 58 | fi | 
|  | 59 | if [ ! -x "$IPTABLES" ]; then | 
|  | 60 | echo "$IPTABLES cannot be executed" | 
|  | 61 | exit 1 | 
|  | 62 | fi | 
| Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame^] | 63 |  | 
|  | 64 | if [ -z "$OE_TAP_NAME" ]; then | 
|  | 65 | OE_TAP_NAME=tap | 
|  | 66 | fi | 
|  | 67 |  | 
|  | 68 | n=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 1 ] | 
|  | 69 | dest=$[ (`echo $TAP | sed "s/$OE_TAP_NAME//"` * 2) + 2 ] | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | 
| Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 72 | true |