2019-01-01 19:55:51 -05:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 09:04:00 -04:00
|
|
|
*
|
2022-09-20 11:21:32 -04:00
|
|
|
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
|
2018-05-03 09:04:00 -04:00
|
|
|
*/
|
|
|
|
|
2019-03-02 22:04:41 -05:00
|
|
|
package device
|
2017-06-04 15:48:15 -04:00
|
|
|
|
2017-08-22 08:57:32 -04:00
|
|
|
import (
|
2021-01-21 12:23:45 -05:00
|
|
|
"fmt"
|
2019-05-14 03:09:52 -04:00
|
|
|
|
2024-01-07 14:03:11 -05:00
|
|
|
"gitea.hbanafa.com/hesham/wireguard-go/tun"
|
2017-08-22 08:57:32 -04:00
|
|
|
)
|
2017-07-15 10:27:59 -04:00
|
|
|
|
|
|
|
const DefaultMTU = 1420
|
|
|
|
|
2017-08-22 08:57:32 -04:00
|
|
|
func (device *Device) RoutineTUNEventReader() {
|
2021-01-26 17:05:48 -05:00
|
|
|
device.log.Verbosef("Routine: event worker - started")
|
2018-05-16 16:20:15 -04:00
|
|
|
|
2017-08-22 08:57:32 -04:00
|
|
|
for event := range device.tun.device.Events() {
|
2019-06-10 17:33:40 -04:00
|
|
|
if event&tun.EventMTUUpdate != 0 {
|
2017-08-22 08:57:32 -04:00
|
|
|
mtu, err := device.tun.device.MTU()
|
|
|
|
if err != nil {
|
2021-01-26 17:05:48 -05:00
|
|
|
device.log.Errorf("Failed to load updated MTU of device: %v", err)
|
2021-01-21 12:23:45 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if mtu < 0 {
|
|
|
|
device.log.Errorf("MTU not updated to negative value: %v", mtu)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var tooLarge string
|
|
|
|
if mtu > MaxContentSize {
|
|
|
|
tooLarge = fmt.Sprintf(" (too large, capped at %v)", MaxContentSize)
|
|
|
|
mtu = MaxContentSize
|
|
|
|
}
|
2022-08-30 10:43:11 -04:00
|
|
|
old := device.tun.mtu.Swap(int32(mtu))
|
2021-01-21 12:23:45 -05:00
|
|
|
if int(old) != mtu {
|
|
|
|
device.log.Verbosef("MTU updated: %v%s", mtu, tooLarge)
|
2017-08-22 08:57:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:26:14 -05:00
|
|
|
if event&tun.EventUp != 0 {
|
|
|
|
device.log.Verbosef("Interface up requested")
|
2017-12-29 11:42:09 -05:00
|
|
|
device.Up()
|
2017-08-22 08:57:32 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 12:26:14 -05:00
|
|
|
if event&tun.EventDown != 0 {
|
|
|
|
device.log.Verbosef("Interface down requested")
|
2018-01-13 03:00:37 -05:00
|
|
|
device.Down()
|
2017-08-22 08:57:32 -04:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 16:20:15 -04:00
|
|
|
|
2021-01-26 17:05:48 -05:00
|
|
|
device.log.Verbosef("Routine: event worker - stopped")
|
2017-08-22 08:57:32 -04:00
|
|
|
}
|