github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/networking/tuntap/tuntap.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tuntap
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  	"syscall"
    23  	"unsafe"
    24  
    25  	"github.com/hashicorp/errwrap"
    26  )
    27  
    28  const (
    29  	Tun = 0x1
    30  	Tap = 0x2
    31  
    32  	NoPi     = 0x1000
    33  	OneQueue = 0x2000
    34  	VnetHdr  = 0x4000
    35  	TunExcl  = 0x8000
    36  )
    37  
    38  type ifReq struct {
    39  	Name  [0x10]byte
    40  	Flags uint16
    41  	pad   [0x28 - 0x12]byte
    42  }
    43  
    44  type Interface struct {
    45  	name string
    46  	file *os.File
    47  }
    48  
    49  func Open(ifName string, kind uint16) (*Interface, error) {
    50  	if ifName == "" {
    51  		ifName = "tap%d"
    52  	}
    53  	file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	var req ifReq
    59  	copy(req.Name[:15], ifName)
    60  	req.Flags = kind | NoPi | VnetHdr
    61  
    62  	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req)))
    63  	if errno != 0 {
    64  		file.Close()
    65  		return nil, fmt.Errorf("ioctl failed (TUNSETIFF): %v", errno)
    66  	}
    67  
    68  	return &Interface{
    69  		name: string(req.Name[:bytes.IndexByte(req.Name[:], 0)]),
    70  		file: file,
    71  	}, nil
    72  }
    73  
    74  func (iface *Interface) Close() error {
    75  	return iface.file.Close()
    76  }
    77  
    78  func operateOnIface(name string, kind uint16, persistency uintptr) (string, error) {
    79  	iface, err := Open(name, kind)
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  
    84  	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, iface.file.Fd(), uintptr(syscall.TUNSETPERSIST), persistency)
    85  	err = iface.Close()
    86  	if err != nil {
    87  		return "", errwrap.Wrap(errors.New("iface close error "), err)
    88  	}
    89  
    90  	if errno != 0 {
    91  		return "", fmt.Errorf("ioctl failed (TUNSETPERSIST): %v", errno)
    92  	}
    93  
    94  	return iface.name, nil
    95  }
    96  
    97  func CreatePersistentIface(nameTemplate string, kind uint16) (string, error) {
    98  	return operateOnIface(nameTemplate, kind, 1)
    99  }
   100  
   101  func RemovePersistentIface(name string, kind uint16) error {
   102  	_, err := operateOnIface(name, kind, 0)
   103  	return err
   104  }