Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cert-v2: more lighthousing #1251

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 75 additions & 41 deletions lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,56 +860,90 @@ func (lh *LightHouse) SendUpdate() {
}
}

v := lh.ifce.GetCertState().defaultVersion
msg := &NebulaMeta{
Type: NebulaMeta_HostUpdateNotification,
Details: &NebulaMetaDetails{
V4AddrPorts: v4,
V6AddrPorts: v6,
},
}
nb := make([]byte, 12, 12)
out := make([]byte, mtu)

if v == 1 {
var relays []uint32
for _, r := range lh.GetRelaysForMe() {
if !r.Is4() {
continue
}
b := r.As4()
relays = append(relays, binary.BigEndian.Uint32(b[:]))
var v1Update, v2Update []byte
var err error
updated := 0
lighthouses := lh.GetLighthouses()

for lhVpnAddr := range lighthouses {
var v cert.Version
hi := lh.ifce.GetHostInfo(lhVpnAddr)
if hi != nil {
v = hi.ConnectionState.myCert.Version()
} else {
v = lh.ifce.GetCertState().defaultVersion
}
if v == cert.Version1 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are reusing the msg struct we need to zero out the v2 fields in v1 and v1 fields in v2.

if v1Update == nil {
var relays []uint32
for _, r := range lh.GetRelaysForMe() {
if !r.Is4() {
continue
}
b := r.As4()
relays = append(relays, binary.BigEndian.Uint32(b[:]))
}
//TODO: assert ipv4
b := lh.myVpnNetworks[0].Addr().As4()
msg := NebulaMeta{
Type: NebulaMeta_HostUpdateNotification,
Details: &NebulaMetaDetails{
V4AddrPorts: v4,
V6AddrPorts: v6,
OldRelayVpnAddrs: relays,
OldVpnAddr: binary.BigEndian.Uint32(b[:]),
},
}

v1Update, err = msg.Marshal()
if err != nil {
lh.l.WithError(err).WithField("lighthouseAddr", lhVpnAddr).
Error("Error while marshaling for lighthouse v1 update")
continue
}
}

msg.Details.OldRelayVpnAddrs = relays
//TODO: assert ipv4
b := lh.myVpnNetworks[0].Addr().As4()
msg.Details.OldVpnAddr = binary.BigEndian.Uint32(b[:])
lh.ifce.SendMessageToVpnIp(header.LightHouse, 0, lhVpnAddr, v1Update, nb, out)
updated++

} else if v == 2 {
var relays []*Addr
for _, r := range lh.GetRelaysForMe() {
relays = append(relays, netAddrToProtoAddr(r))
}
msg.Details.RelayVpnAddrs = relays
msg.Details.VpnAddr = netAddrToProtoAddr(lh.myVpnNetworks[0].Addr())
} else if v == cert.Version2 {
if v2Update == nil {
var relays []*Addr
for _, r := range lh.GetRelaysForMe() {
relays = append(relays, netAddrToProtoAddr(r))
}

} else {
panic("protocol version not supported")
}
msg := NebulaMeta{
Type: NebulaMeta_HostUpdateNotification,
Details: &NebulaMetaDetails{
V4AddrPorts: v4,
V6AddrPorts: v6,
RelayVpnAddrs: relays,
VpnAddr: netAddrToProtoAddr(lh.myVpnNetworks[0].Addr()),
},
}

lighthouses := lh.GetLighthouses()
lh.metricTx(NebulaMeta_HostUpdateNotification, int64(len(lighthouses)))
nb := make([]byte, 12, 12)
out := make([]byte, mtu)
v2Update, err = msg.Marshal()
if err != nil {
lh.l.WithError(err).WithField("lighthouseAddr", lhVpnAddr).
Error("Error while marshaling for lighthouse v2 update")
continue
}
}

mm, err := msg.Marshal()
if err != nil {
lh.l.WithError(err).Error("Error while marshaling for lighthouse update")
return
}
lh.ifce.SendMessageToVpnIp(header.LightHouse, 0, lhVpnAddr, v2Update, nb, out)
updated++

for vpnIp := range lighthouses {
lh.ifce.SendMessageToVpnIp(header.LightHouse, 0, vpnIp, mm, nb, out)
} else {
lh.l.Debugf("Can not update lighthouse using unknown protocol version: %v", v)
continue
}
}

lh.metricTx(NebulaMeta_HostUpdateNotification, int64(updated))
}

type LightHouseHandler struct {
Expand Down
Loading