2024-09-04 12:22:10 +00:00
|
|
|
package s6netdev
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-10 13:20:23 +00:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-09-04 12:22:10 +00:00
|
|
|
)
|
|
|
|
|
2024-09-11 17:13:27 +00:00
|
|
|
type IfMap map[string]*Iface
|
|
|
|
|
|
|
|
func (im IfMap) AddIf(i *Iface) {
|
|
|
|
im[i.Name] = i
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
func (i *Iface) NetdevIfaceAddAddr(addr string) (err error) {
|
|
|
|
var (
|
|
|
|
a netip.Prefix
|
|
|
|
)
|
|
|
|
if a, err = netip.ParsePrefix(addr); err != nil {
|
|
|
|
err = fmt.Errorf("Failed to parse address %s, %w", addr, err)
|
|
|
|
}
|
|
|
|
i.Addresses = append(i.Addresses, a)
|
2024-09-04 12:22:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-11 17:13:27 +00:00
|
|
|
func (i *Iface) NetdevIfaceAddRoute(def bool, route, r_type, via string, vrf *Iface, table *RouteTable, metric Metric) (err error) {
|
2024-09-10 13:20:23 +00:00
|
|
|
var (
|
|
|
|
r = Route{
|
2024-09-11 17:13:27 +00:00
|
|
|
Default: def,
|
|
|
|
Type: r_type,
|
|
|
|
Vrf: vrf,
|
|
|
|
Table: table,
|
|
|
|
Metric: metric,
|
2024-09-04 12:22:10 +00:00
|
|
|
}
|
2024-09-10 13:20:23 +00:00
|
|
|
)
|
2024-09-04 12:22:10 +00:00
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
if via != "" {
|
|
|
|
if r.Via, err = netip.ParseAddr(via); err != nil {
|
|
|
|
err = fmt.Errorf("Failed to parse via %s, %w", via, err)
|
|
|
|
}
|
2024-09-04 12:22:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 17:13:27 +00:00
|
|
|
if !def {
|
|
|
|
if r.Net, err = netip.ParsePrefix(route); err != nil {
|
|
|
|
err = fmt.Errorf("Failed to parse via %s, %w", via, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
i.Routes = append(i.Routes, r)
|
2024-09-04 12:22:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
func (i *Iface) NetdevIfaceMAC(mac string) (err error) {
|
|
|
|
i.MACAddr, err = net.ParseMAC(mac)
|
|
|
|
return
|
|
|
|
}
|
2024-09-04 12:22:10 +00:00
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
func (i *Iface) NetdevIfaceEnslave(s ...*Iface) {
|
|
|
|
i.Slaves = append(i.Slaves, s...)
|
|
|
|
}
|
2024-09-04 12:22:10 +00:00
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
func (i *Iface) NetdevIfaceSysctl(ipv int, key, value, def string) {
|
|
|
|
l := &i.Sysctls.V4
|
|
|
|
if ipv == 6 {
|
|
|
|
l = &i.Sysctls.V6
|
|
|
|
}
|
|
|
|
*l = append(*l, Property{Key: key, Value: value, Default: def})
|
|
|
|
}
|
2024-09-04 12:22:10 +00:00
|
|
|
|
2024-09-10 13:20:23 +00:00
|
|
|
func (i *Iface) NetdevIfaceProperty(key, value, def string) {
|
|
|
|
i.Properties = append(i.Properties, Property{Key: key, Value: value, Default: def})
|
2024-09-04 12:22:10 +00:00
|
|
|
}
|