github.com/amnezia-vpn/amnezia-wg@v0.1.8/device/tun.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package device
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/amnezia-vpn/amnezia-wg/tun"
    12  )
    13  
    14  const DefaultMTU = 1420
    15  
    16  func (device *Device) RoutineTUNEventReader() {
    17  	device.log.Verbosef("Routine: event worker - started")
    18  
    19  	for event := range device.tun.device.Events() {
    20  		if event&tun.EventMTUUpdate != 0 {
    21  			mtu, err := device.tun.device.MTU()
    22  			if err != nil {
    23  				device.log.Errorf("Failed to load updated MTU of device: %v", err)
    24  				continue
    25  			}
    26  			if mtu < 0 {
    27  				device.log.Errorf("MTU not updated to negative value: %v", mtu)
    28  				continue
    29  			}
    30  			var tooLarge string
    31  			if mtu > MaxContentSize {
    32  				tooLarge = fmt.Sprintf(" (too large, capped at %v)", MaxContentSize)
    33  				mtu = MaxContentSize
    34  			}
    35  			old := device.tun.mtu.Swap(int32(mtu))
    36  			if int(old) != mtu {
    37  				device.log.Verbosef("MTU updated: %v%s", mtu, tooLarge)
    38  			}
    39  		}
    40  
    41  		if event&tun.EventUp != 0 {
    42  			device.log.Verbosef("Interface up requested")
    43  			device.Up()
    44  		}
    45  
    46  		if event&tun.EventDown != 0 {
    47  			device.log.Verbosef("Interface down requested")
    48  			device.Down()
    49  		}
    50  	}
    51  
    52  	device.log.Verbosef("Routine: event worker - stopped")
    53  }