| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # QEMU network interface configuration script. This utility needs to | 
|  | 4 | # be run as root, and will use the tunctl binary from a native sysroot. | 
|  | 5 | # Note: many Linux distros these days still use an older version of | 
|  | 6 | # tunctl which does not support the group permissions option, hence | 
|  | 7 | # the need to use build system's version. | 
|  | 8 | # | 
|  | 9 | # If you find yourself calling this script a lot, you can add the | 
|  | 10 | # the following to your /etc/sudoers file to be able to run this | 
|  | 11 | # command without entering your password each time: | 
|  | 12 | # | 
|  | 13 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifup | 
|  | 14 | # <my-username> ALL=NOPASSWD: /path/to/runqemu-ifdown | 
|  | 15 | # | 
|  | 16 | # If you'd like to create a bank of tap devices at once, you should use | 
|  | 17 | # the runqemu-gen-tapdevs script instead. If tap devices are set up using | 
|  | 18 | # that script, the runqemu script will never end up calling this | 
|  | 19 | # script. | 
|  | 20 | # | 
|  | 21 | # Copyright (c) 2006-2011 Linux Foundation | 
|  | 22 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 23 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 |  | 
|  | 26 | usage() { | 
|  | 27 | echo "sudo $(basename $0) <uid> <gid> <native-sysroot-basedir>" | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | if [ $EUID -ne 0 ]; then | 
|  | 31 | echo "Error: This script (runqemu-ifup) must be run with root privileges" | 
|  | 32 | exit 1 | 
|  | 33 | fi | 
|  | 34 |  | 
|  | 35 | if [ $# -ne 3 ]; then | 
|  | 36 | usage | 
|  | 37 | exit 1 | 
|  | 38 | fi | 
|  | 39 |  | 
|  | 40 | USERID="-u $1" | 
|  | 41 | GROUP="-g $2" | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | STAGING_BINDIR_NATIVE=$3 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 44 | TUNCTL=$STAGING_BINDIR_NATIVE/tunctl | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | if [ ! -x "$TUNCTL" ]; then | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | echo "Error: Unable to find tunctl binary in '$STAGING_BINDIR_NATIVE', please bitbake qemu-helper-native" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | exit 1 | 
|  | 48 | fi | 
|  | 49 |  | 
|  | 50 | TAP=`$TUNCTL -b $GROUP 2>&1` | 
|  | 51 | STATUS=$? | 
|  | 52 | if [ $STATUS -ne 0 ]; then | 
|  | 53 | # If tunctl -g fails, try using tunctl -u, for older host kernels | 
|  | 54 | # which do not support the TUNSETGROUP ioctl | 
|  | 55 | TAP=`$TUNCTL -b $USERID 2>&1` | 
|  | 56 | STATUS=$? | 
|  | 57 | if [ $STATUS -ne 0 ]; then | 
|  | 58 | echo "tunctl failed:" | 
|  | 59 | exit 1 | 
|  | 60 | fi | 
|  | 61 | fi | 
|  | 62 |  | 
|  | 63 | IFCONFIG=`which ip 2> /dev/null` | 
|  | 64 | if [ "x$IFCONFIG" = "x" ]; then | 
|  | 65 | # better than nothing... | 
|  | 66 | IFCONFIG=/sbin/ip | 
|  | 67 | fi | 
|  | 68 | if [ ! -x "$IFCONFIG" ]; then | 
|  | 69 | echo "$IFCONFIG cannot be executed" | 
|  | 70 | exit 1 | 
|  | 71 | fi | 
|  | 72 |  | 
|  | 73 | IPTABLES=`which iptables 2> /dev/null` | 
|  | 74 | if [ "x$IPTABLES" = "x" ]; then | 
|  | 75 | IPTABLES=/sbin/iptables | 
|  | 76 | fi | 
|  | 77 | if [ ! -x "$IPTABLES" ]; then | 
|  | 78 | echo "$IPTABLES cannot be executed" | 
|  | 79 | exit 1 | 
|  | 80 | fi | 
|  | 81 |  | 
|  | 82 | n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] | 
|  | 83 | $IFCONFIG addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 84 | STATUS=$? | 
|  | 85 | if [ $STATUS -ne 0 ]; then | 
|  | 86 | echo "Failed to set up IP addressing on $TAP" | 
|  | 87 | exit 1 | 
|  | 88 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | $IFCONFIG link set dev $TAP up | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 90 | STATUS=$? | 
|  | 91 | if [ $STATUS -ne 0 ]; then | 
|  | 92 | echo "Failed to bring up $TAP" | 
|  | 93 | exit 1 | 
|  | 94 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 |  | 
|  | 96 | dest=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ] | 
|  | 97 | $IFCONFIG route add to 192.168.7.$dest dev $TAP | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 98 | STATUS=$? | 
|  | 99 | if [ $STATUS -ne 0 ]; then | 
|  | 100 | echo "Failed to add route to 192.168.7.$dest using $TAP" | 
|  | 101 | exit 1 | 
|  | 102 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 |  | 
|  | 104 | # setup NAT for tap0 interface to have internet access in QEMU | 
|  | 105 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$n/32 | 
|  | 106 | $IPTABLES -A POSTROUTING -t nat -j MASQUERADE -s 192.168.7.$dest/32 | 
|  | 107 | echo 1 > /proc/sys/net/ipv4/ip_forward | 
|  | 108 | echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp | 
|  | 109 | $IPTABLES -P FORWARD ACCEPT | 
|  | 110 |  | 
|  | 111 | echo $TAP |