tstest/natlab/vnet: use mkPacketErr in more places

I'd added this helper for tests, but then moved it to non-test code
and forgot some places to use it. This uses it in more places to
remove some boilerplate.

Updates #13038

Change-Id: Ic4dc339be1c47a55b71d806bab421097ee3d75ed
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/13324/head
Brad Fitzpatrick 3 months ago committed by Brad Fitzpatrick
parent 109d0891e1
commit ffa1c93f59

@ -1481,14 +1481,11 @@ func (n *network) handleIPv6NeighborSolicitation(ep EthernetPacket, ns *layers.I
Type: layers.ICMPv6OptTargetAddress, Type: layers.ICMPv6OptTargetAddress,
Data: srcMAC.HWAddr(), Data: srcMAC.HWAddr(),
}) })
icmp.SetNetworkLayerForChecksum(ip) pkt, err := mkPacketErr(eth, ip, icmp, na)
buffer := gopacket.NewSerializeBuffer() if err != nil {
options := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true} n.logf("serializing ICMPv6 NA: %v", err)
if err := gopacket.SerializeLayers(buffer, options, eth, ip, icmp, na); err != nil {
n.logf("serializing ICMPv6 RA: %v", err)
return
} }
if !n.writeEth(buffer.Bytes()) { if !n.writeEth(pkt) {
n.logf("failed to writeEth for IPv6 NA reply for %v", targetIP) n.logf("failed to writeEth for IPv6 NA reply for %v", targetIP)
} }
} }
@ -1576,7 +1573,6 @@ func (s *Server) createDHCPResponse(request gopacket.Packet) ([]byte, error) {
DstMAC: ethLayer.SrcMAC, DstMAC: ethLayer.SrcMAC,
EthernetType: layers.EthernetTypeIPv4, // never IPv6 for DHCP EthernetType: layers.EthernetTypeIPv4, // never IPv6 for DHCP
} }
ip := &layers.IPv4{ ip := &layers.IPv4{
Version: 4, Version: 4,
TTL: 64, TTL: 64,
@ -1584,25 +1580,11 @@ func (s *Server) createDHCPResponse(request gopacket.Packet) ([]byte, error) {
SrcIP: ipLayer.DstIP, SrcIP: ipLayer.DstIP,
DstIP: ipLayer.SrcIP, DstIP: ipLayer.SrcIP,
} }
udp := &layers.UDP{ udp := &layers.UDP{
SrcPort: udpLayer.DstPort, SrcPort: udpLayer.DstPort,
DstPort: udpLayer.SrcPort, DstPort: udpLayer.SrcPort,
} }
udp.SetNetworkLayerForChecksum(ip) return mkPacketErr(eth, ip, udp, response)
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
if err := gopacket.SerializeLayers(buffer, options,
eth,
ip,
udp,
response,
); err != nil {
return nil, err
}
return buffer.Bytes(), nil
} }
func isDHCPRequest(pkt gopacket.Packet) bool { func isDHCPRequest(pkt gopacket.Packet) bool {
@ -1800,25 +1782,23 @@ func (s *Server) createDNSResponse(pkt gopacket.Packet) ([]byte, error) {
SrcPort: udpLayer.DstPort, SrcPort: udpLayer.DstPort,
DstPort: udpLayer.SrcPort, DstPort: udpLayer.SrcPort,
} }
udp2.SetNetworkLayerForChecksum(ip2)
buffer := gopacket.NewSerializeBuffer() resPkt, err := mkPacketErr(eth2, ip2, udp2, response)
options := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true} if err != nil {
if err := gopacket.SerializeLayers(buffer, options, eth2, ip2, udp2, response); err != nil {
return nil, err return nil, err
} }
const debugDNS = false const debugDNS = false
if debugDNS { if debugDNS {
if len(response.Answers) > 0 { if len(response.Answers) > 0 {
back := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Lazy) back := gopacket.NewPacket(resPkt, layers.LayerTypeEthernet, gopacket.Lazy)
log.Printf("createDNSResponse generated answers: %v", back) log.Printf("createDNSResponse generated answers: %v", back)
} else { } else {
log.Printf("made empty response for %q", names) log.Printf("made empty response for %q", names)
} }
} }
return buffer.Bytes(), nil return resPkt, nil
} }
// doNATOut performs NAT on an outgoing packet from src to dst, where // doNATOut performs NAT on an outgoing packet from src to dst, where

Loading…
Cancel
Save