0

I am working on an application which can create TUN/TAP interface on host machine. I can create such interfaces using:

sudo ip tuntap add tap0 mode tap 

Now the thing is, tap0 is hardcoded here. It should be dynamic.

When I create next tap device, it should be tap1 and so on.

Now when I go ahead to give range to the device, I can manually do like this:

sudo ip addr add 172.16.0.1/24 dev tap0 

Now when I create tap1, I don't want to overlap the ip.

Is there any easy way to manage these tap devices?

3 Answers 3

1

For next available tap device, you can use following function:

function nextTapDevice() { counter=-1 while [ $? -eq 0 ]; do counter=$(($counter+1)) ip link show tap$counter &> /dev/null done echo tap$counter } nextTapDevice 

It runs on same principle as @stony described, but it keeps running till ip link show throws an error. Which means no device named tap$counter is available. That device name will be next available device name.


For next available IP range, you first need to adhere to policy or IP range. Make sure this IP range does not collides with other application such as of Docker or so.

For my use case, I have decided to use 172.16.0.0/16. I'll be allocating last octet to each VM (application I am working with). I can run 256 VMs.

I am going to reuse modified version of my nextTapDevice function written above. Instead of returning tap$counter in above function, I'm going to return just the counter.

Here is the rest of the code:

function nextTapDevice() { counter=-1 while [ $? -eq 0 ]; do counter=$(($counter+1)) ip link show tap$counter &> /dev/null done echo $counter } # nextTapDevice function nextIPRange() { for ((i=0; i<$(nextTapDevice);++i)); do output=$(ip -br addr show tap$i | awk '{print $3}') done echo $output | awk -F. '{ print $1"."$2"."$3+1"."$4 }' } nextIPRange 

Drawback: The 3rd octet would keep incrementing even after 256. You might want to implement additional checks if your use case exceeds that point.

0
0
#!/bin/bash i=0; while [ $i -ne 5 ] do sudo ip tuntap add tap$i mode tap &>/dev/null if [[ $? -eq 0 ]]; then echo "$i" exit 0 fi i=$(($i+1)) done exit 1 

Tries tun0 to tun4, creates the first free interface and returns the number

0

Just for fun, here's a FreeBSD solution, based on the assumption that tunN will use local IP 172.16.N.1 and remote IP 10.172.N.1:

#!/usr/bin/env bash # what's the base name of the network interface? NO SPACES! devBase='tun' # Maximum of N interfaces, numbered 0 .. N-1 NO HIGHER THAN 256! N=256 # what's the printf spec of the tun's local address? NO SPACES! ipLocal='172.16.%d.1' # what's the printf spec of the tun's remote address? NO SPACES! ipRemote='10.172.%d.1' nextTun() { # $1 is the device base name result="$(jot -w "${1}%d" $N 0 | grep -vxFf <(ifconfig -l | grep -ow "${1}[0-9]\+") | head -1)" if [[ -n "$result" ]] then printf '%s\n' "$result" else exit $? fi } if tun="$(nextTun $devBase)" then devN=${tun#$devBase} printf 'Creating %s ...\n' "$tun" ipl=$(printf $ipLocal $devN) ipr=$(printf $ipRemote $devN) ifcfg="$(printf 'sudo ifconfig %s create %s %s' "$tun" $ipl $ipr )" sudo $ifcfg || { printf '"%s" failed.\n' $tun exit 2 } else printf 'No %s devices available.\n' "$devBase" exit 1 fi 

This declares a function nextTun which accepts a base interface name, such as tun. It quickly jots down a list of the N possible tun devices, removes any tun devices already in use, and takes the first (lowest) tun device that is available. If no tun device is available, the function returns no result, and a non-zero exit code.

The main code tests for a successful result from nextTun. If found, then it constructs the necessary ifconfig syntax to create the tun device number devN with the appropriate local and remote IPs. Once the syntax is built, it's passed to sudo to execute. If nextTun indicates a failure to allocate an available tun device, the code indicates that no more devices are available, and returns a non-zero exit code.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.