Skip to content

Commit

Permalink
Merge pull request #6040 from mysteriumnetwork/flag/mtu
Browse files Browse the repository at this point in the history
Add command flag for setting wireguard mtu
  • Loading branch information
Zensey authored Apr 26, 2024
2 parents 3966bfa + f2ad529 commit ee113ab
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/commands/daemon/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func NewCommand() *cli.Command {
config.ParseFlagsServiceWireguard(ctx)
config.ParseFlagsServiceNoop(ctx)
config.ParseFlagsNode(ctx)
if err := config.ValidateWireguardMTUFlag(); err != nil {
return err
}

nodeOptions := node.GetOptions()
if err := di.Bootstrap(*nodeOptions); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/commands/service/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func NewCommand(licenseCommandName string) *cli.Command {
config.ParseFlagsServiceNoop(ctx)
config.ParseFlagsNode(ctx)

if err := config.ValidateWireguardMTUFlag(); err != nil {
log.Error().Msg(err.Error())
return err
}

if err := hasAcceptedTOS(ctx); err != nil {
clio.PrintTOSError(err)
os.Exit(2)
Expand Down
24 changes: 24 additions & 0 deletions config/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -291,6 +292,12 @@ var (
Name: "resident-country",
Usage: "set resident country. If not set initially a default country will be resolved.",
}

// FlagWireguardMTU sets Wireguard myst interface MTU.
FlagWireguardMTU = cli.IntFlag{
Name: "wireguard.mtu",
Usage: "Wireguard interface MTU",
}
)

// RegisterFlagsNode function register node flags to flag list
Expand Down Expand Up @@ -351,6 +358,7 @@ func RegisterFlagsNode(flags *[]cli.Flag) error {
&FlagDocsURL,
&FlagDNSResolutionHeadstart,
&FlagResidentCountry,
&FlagWireguardMTU,
)

return nil
Expand Down Expand Up @@ -411,6 +419,7 @@ func ParseFlagsNode(ctx *cli.Context) {
Current.ParseStringFlag(ctx, FlagDefaultCurrency)
Current.ParseStringFlag(ctx, FlagDocsURL)
Current.ParseDurationFlag(ctx, FlagDNSResolutionHeadstart)
Current.ParseIntFlag(ctx, FlagWireguardMTU)

ValidateAddressFlags(FlagTequilapiAddress)
}
Expand All @@ -425,3 +434,18 @@ func ValidateAddressFlags(flags ...cli.StringFlag) {
"Ensure its set to localhost or protected by firewall.", flag.Name, flag.Value)
}
}

// ValidateWireguardMTUFlag validates given mtu flag
func ValidateWireguardMTUFlag() error {

v := Current.GetInt(FlagWireguardMTU.Name)
if v == 0 {
return nil
}
if v < 68 || v > 1500 {
msg := "Wireguard MTU value is out of possible range: 68..1500"
log.Error().Msg(msg)
return errors.Errorf("Flag validation error: %s", msg)
}
return nil
}
7 changes: 7 additions & 0 deletions services/wireguard/endpoint/kernelspace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/base64"
"fmt"
"net"
"strconv"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -93,6 +94,12 @@ func (c *client) configureDevice(config wgcfg.DeviceConfig) error {
return err
}

if config.MTU > 0 {
if err := cmdutil.SudoExec("ip", "link", "set", "dev", config.IfaceName, "mtu", strconv.Itoa(config.MTU)); err != nil {
return err
}
}

peer, err := peerConfig(config.Peer)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions services/wireguard/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (m *Manager) createProviderConfig(listenPort int, peerPublicKey string) (wg

return wgcfg.DeviceConfig{
IfaceName: "", // Interface name will be generated by connection endpoint.
MTU: config.GetInt(config.FlagWireguardMTU),
Subnet: network,
PrivateKey: privateKey,
ListenPort: listenPort,
Expand Down
1 change: 1 addition & 0 deletions services/wireguard/wgcfg/device_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Stats struct {
// DeviceConfig describes wireguard device configuration.
type DeviceConfig struct {
IfaceName string `json:"iface_name"`
MTU int `json:"mtu"`
Subnet net.IPNet `json:"subnet"`
PrivateKey string `json:"private_key"`
ListenPort int `json:"listen_port"`
Expand Down

0 comments on commit ee113ab

Please sign in to comment.