2
0

interface.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //go:build !windows
  2. // +build !windows
  3. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along
  16. // with this program; if not, see <http://www.gnu.org/licenses/>.
  17. package vpn
  18. import (
  19. "github.com/songgao/water"
  20. "github.com/vishvananda/netlink"
  21. )
  22. func createInterface(c *Config) (*water.Interface, error) {
  23. config := water.Config{
  24. DeviceType: c.DeviceType,
  25. PlatformSpecificParams: water.PlatformSpecificParams{Persist: !c.NetLinkBootstrap},
  26. }
  27. config.Name = c.InterfaceName
  28. return water.New(config)
  29. }
  30. func prepareInterface(c *Config) error {
  31. link, err := netlink.LinkByName(c.InterfaceName)
  32. if err != nil {
  33. return err
  34. }
  35. addr, err := netlink.ParseAddr(c.InterfaceAddress)
  36. if err != nil {
  37. return err
  38. }
  39. err = netlink.LinkSetMTU(link, c.InterfaceMTU)
  40. if err != nil {
  41. return err
  42. }
  43. err = netlink.AddrAdd(link, addr)
  44. if err != nil {
  45. return err
  46. }
  47. err = netlink.LinkSetUp(link)
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. }