2021-08-22 14:58:39 -07:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-05-29 06:44:58 +00:00
|
|
|
"net/netip"
|
2023-07-06 10:08:59 +03:00
|
|
|
"strings"
|
2021-08-22 14:58:39 -07:00
|
|
|
|
2021-08-23 21:12:28 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/netlink"
|
2021-08-22 14:58:39 -07:00
|
|
|
)
|
|
|
|
|
|
2023-07-06 10:08:59 +03:00
|
|
|
func (w *Wireguard) addRoutes(link netlink.Link, destinations []netip.Prefix,
|
2024-10-11 19:20:48 +00:00
|
|
|
firewallMark uint32,
|
|
|
|
|
) (err error) {
|
2023-07-06 10:08:59 +03:00
|
|
|
for _, dst := range destinations {
|
|
|
|
|
err = w.addRoute(link, dst, firewallMark)
|
|
|
|
|
if err == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dst.Addr().Is6() && strings.Contains(err.Error(), "permission denied") {
|
|
|
|
|
w.logger.Errorf("cannot add route for IPv6 due to a permission denial. "+
|
|
|
|
|
"Ignoring and continuing execution; "+
|
|
|
|
|
"Please report to https://github.com/qdm12/gluetun/issues/998 if you find a fix. "+
|
|
|
|
|
"Full error string: %s", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("adding route for destination %s: %w", dst, err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-22 14:58:39 -07:00
|
|
|
|
2023-05-29 06:44:58 +00:00
|
|
|
func (w *Wireguard) addRoute(link netlink.Link, dst netip.Prefix,
|
2024-10-11 19:20:48 +00:00
|
|
|
firewallMark uint32,
|
|
|
|
|
) (err error) {
|
2025-10-30 17:14:45 +01:00
|
|
|
family := netlink.FamilyV4
|
|
|
|
|
if dst.Addr().Is6() {
|
|
|
|
|
family = netlink.FamilyV6
|
|
|
|
|
}
|
2023-05-29 06:44:58 +00:00
|
|
|
route := netlink.Route{
|
|
|
|
|
LinkIndex: link.Index,
|
2021-08-22 14:58:39 -07:00
|
|
|
Dst: dst,
|
2025-10-30 17:14:45 +01:00
|
|
|
Family: family,
|
2024-08-23 06:46:29 +00:00
|
|
|
Table: int(firewallMark),
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = w.netlink.RouteAdd(route)
|
|
|
|
|
if err != nil {
|
2022-05-27 16:56:20 +00:00
|
|
|
return fmt.Errorf(
|
2023-04-01 16:53:04 +00:00
|
|
|
"adding route for link %s, destination %s and table %d: %w",
|
2023-05-29 06:44:58 +00:00
|
|
|
link.Name, dst, firewallMark, err)
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|