Skip to content

Commit

Permalink
Merge pull request #633 from djs55/dhcp
Browse files Browse the repository at this point in the history
vmnetd: fix DHCP response parser
  • Loading branch information
ebriney authored Apr 12, 2023
2 parents 2dc2744 + 9578084 commit 6a5c3c5
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions go/pkg/vmnet/vmnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,14 @@ func (v *Vif) dhcp() (net.IP, error) {
if err != nil {
return nil, err
}

ethernet, err = ParseEthernetFrame(response)
if err != nil {
continue
}
for i, x := range ethernet.Dst {
if i > len(v.ClientMAC) || v.ClientMAC[i] != x {
// intended for someone else
continue
}
if !hwaddrIsBroadcast(ethernet.Dst) && !hwaddrIsEqual(ethernet.Dst, v.ClientMAC) {
// intended for someone else
continue
}
ipv4, err = ParseIpv4(ethernet.Data)
if err != nil {
Expand All @@ -604,14 +603,36 @@ func (v *Vif) dhcp() (net.IP, error) {
// truncated
continue
}
if udpv4.Data[240] != 53 || udpv4.Data[241] != 1 || udpv4.Data[242] != 2 {
if udpv4.Data[0] != 2 {
// not a DHCP offer
continue
}
if udpv4.Data[4] != 1 || udpv4.Data[5] != 0 || udpv4.Data[6] != 0 || udpv4.Data[7] != 0 {
// not our XID
continue
}
var ip net.IP
ip = udpv4.Data[16:20]
finished = true // will terminate sending goroutine
return ip, nil
}

}

func hwaddrIsBroadcast(mac net.HardwareAddr) bool {
for _, b := range mac {
if b != 0xff {
return false
}
}
return true
}

func hwaddrIsEqual(a, b net.HardwareAddr) bool {
for i, x := range a {
if i > len(b) || b[i] != x {
return false
}
}
return true
}

0 comments on commit 6a5c3c5

Please sign in to comment.