net/packet: add ICMP6Header, like ICMP4Header

So we can generate IPv6 ping replies.

Change-Id: I79a9a38d8aa242e5dfca4cd15dfaffaea6cb1aee
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2021-12-08 22:05:07 -08:00
committed by Brad Fitzpatrick
parent 7b9c7bc42b
commit 21741e111b
4 changed files with 226 additions and 7 deletions

View File

@ -39,6 +39,16 @@ type Header interface {
Marshal(buf []byte) error
}
// HeaderChecksummer is implemented by Header implementations that
// need to do a checksum over their paylods.
type HeaderChecksummer interface {
Header
// WriteCheck writes the correct checksum into buf, which should
// be be the already-marshalled header and payload.
WriteChecksum(buf []byte)
}
// Generate generates a new packet with the given Header and
// payload. This function allocates memory, see Header.Marshal for an
// allocation-free option.
@ -49,5 +59,9 @@ func Generate(h Header, payload []byte) []byte {
copy(buf[hlen:], payload)
h.Marshal(buf)
if hc, ok := h.(HeaderChecksummer); ok {
hc.WriteChecksum(buf)
}
return buf
}