Skip to content

Commit 0dfcb9f

Browse files
committed
incusd/network/bridge: Fix deletion of tunnels and dummy devices
The previous code relied purely on the name of the device to decide if a device should be deleted which was not correct and could result in the deletion of unwanted devices. The new code ensures that the device is part of the bridge and that its kind is correct before deleting it. Signed-off-by: montag451 <[email protected]>
1 parent ebfa97f commit 0dfcb9f

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

internal/server/device/nic_macvlan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (d *nicMACVLAN) Start() (*deviceConfig.RunConfig, error) {
219219

220220
if d.inst.Type() == instancetype.VM {
221221
// Enable all multicast processing which is required for IPv6 NDP functionality.
222-
link.AllMutlicast = true
222+
link.AllMulticast = true
223223

224224
// Bring the interface up on host side.
225225
link.Up = true

internal/server/ip/link.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,30 @@ import (
1717
// Link represents base arguments for link device.
1818
type Link struct {
1919
Name string
20+
Kind string
2021
MTU uint32
2122
Parent string
2223
Address net.HardwareAddr
2324
TXQueueLength uint32
24-
AllMutlicast bool
25+
AllMulticast bool
2526
Master string
2627
Up bool
2728
}
2829

30+
type jsonLink struct {
31+
Name string `json:"ifname"`
32+
MTU uint32 `json:"mtu"`
33+
Parent string `json:"link"`
34+
Address string `json:"address"`
35+
TXQueueLength uint32 `json:"txqlen"`
36+
AllMulticast int `json:"allmulti"`
37+
Master string `json:"master"`
38+
Up string `json:"operstate"`
39+
Info struct {
40+
Kind string `json:"info_kind"`
41+
} `json:"linkinfo"`
42+
}
43+
2944
// args generate common arguments for the virtual link.
3045
func (l *Link) args() []string {
3146
var result []string
@@ -50,7 +65,7 @@ func (l *Link) args() []string {
5065
result = append(result, "txqueuelen", fmt.Sprintf("%d", l.TXQueueLength))
5166
}
5267

53-
if l.AllMutlicast {
68+
if l.AllMulticast {
5469
result = append(result, "allmulticast", "on")
5570
}
5671

@@ -79,6 +94,46 @@ func (l *Link) add(linkType string, additionalArgs []string) error {
7994
return nil
8095
}
8196

97+
func LinkFromName(name string) (*Link, error) {
98+
out, err := subprocess.RunCommand("ip", "-d", "-j", "link", "show", name)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
var links []jsonLink
104+
err = json.Unmarshal([]byte(out), &links)
105+
if err != nil {
106+
return nil, fmt.Errorf("failed to decode JSON link representation: %w", err)
107+
}
108+
109+
jl := &links[0]
110+
l := &Link{
111+
Name: jl.Name,
112+
Kind: jl.Info.Kind,
113+
MTU: jl.MTU,
114+
Parent: jl.Parent,
115+
TXQueueLength: jl.TXQueueLength,
116+
Master: jl.Master,
117+
}
118+
119+
if jl.Address != "" {
120+
l.Address, err = net.ParseMAC(jl.Address)
121+
if err != nil {
122+
return nil, err
123+
}
124+
}
125+
126+
if jl.AllMulticast == 1 {
127+
l.AllMulticast = true
128+
}
129+
130+
if jl.Up == "UP" {
131+
l.Up = true
132+
}
133+
134+
return l, err
135+
}
136+
82137
// SetUp enables the link device.
83138
func (l *Link) SetUp() error {
84139
_, err := subprocess.RunCommand("ip", "link", "set", "dev", l.Name, "up")

internal/server/network/driver_bridge.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,20 @@ func (n *bridge) setup(oldConfig map[string]string) error {
671671
return err
672672
}
673673

674-
// Cleanup any existing tunnel device.
674+
// Cleanup any existing tunnel and dummy devices.
675675
for _, iface := range ifaces {
676-
if strings.HasPrefix(iface.Name, fmt.Sprintf("%s-", n.name)) {
677-
tunLink := &ip.Link{Name: iface.Name}
678-
err = tunLink.Delete()
679-
if err != nil {
680-
return err
681-
}
676+
l, err := ip.LinkFromName(iface.Name)
677+
if err != nil {
678+
return err
679+
}
680+
681+
if l.Master != n.name || l.Kind != "vxlan" && l.Kind != "gretap" && l.Kind != "dummy" {
682+
continue
683+
}
684+
685+
err = l.Delete()
686+
if err != nil {
687+
return err
682688
}
683689
}
684690

0 commit comments

Comments
 (0)