| 1 | |
| 2 | |
| 3 | @@ -33,6 +33,8 @@ |
| 4 | } |
| 5 | |
| 6 | type Switch struct { |
| 7 | + droppedIPv6 func([]byte) |
| 8 | + |
| 9 | Sent uint64 |
| 10 | Received uint64 |
| 11 | |
| 12 | @@ -59,6 +61,9 @@ |
| 13 | cam: make(map[tcpip.LinkAddress]int), |
| 14 | } |
| 15 | } |
| 16 | + |
| 17 | +// SetDroppedIPv6 is configured before Accept, only for an IPv4-only stack. |
| 18 | +func (e *Switch) SetDroppedIPv6(observe func([]byte)) { e.droppedIPv6 = observe } |
| 19 | |
| 20 | func (e *Switch) CAM() map[string]int { |
| 21 | e.camLock.RLock() |
| 22 | @@ -275,7 +280,15 @@ |
| 23 | log.Info(packet.String()) |
| 24 | } |
| 25 | |
| 26 | + if len(buf) < header.EthernetMinimumSize { |
| 27 | + return |
| 28 | + } |
| 29 | eth := header.Ethernet(buf) |
| 30 | + if e.droppedIPv6 != nil && eth.Type() == header.IPv6ProtocolNumber { |
| 31 | + e.droppedIPv6(buf[header.EthernetMinimumSize:]) |
| 32 | + atomic.AddUint64(&e.Received, uint64(len(buf))) |
| 33 | + return |
| 34 | + } |
| 35 | |
| 36 | e.camLock.Lock() |
| 37 | _, exists := e.cam[eth.SourceAddress()] |
| 38 | |
| 39 | |
| 40 | @@ -20,6 +20,10 @@ |
| 41 | type DialContext func(context.Context, string, string) (net.Conn, error) |
| 42 | |
| 43 | type Configuration struct { |
| 44 | + // DroppedIPv6 observes guest IPv6 payloads discarded at ingress by an |
| 45 | + // IPv4-only stack. It does not receive frames when IPv6 is supported. |
| 46 | + DroppedIPv6 func([]byte) `yaml:"-" json:"-"` |
| 47 | + |
| 48 | DialContext DialContext `yaml:"-"` |
| 49 | DNSExchange func(context.Context, *dns.Msg) (*dns.Msg, error) `yaml:"-"` |
| 50 | // Print packets on stderr |
| 51 | |
| 52 | |
| 53 | @@ -12,6 +12,7 @@ |
| 54 | "github.com/containers/gvisor-tap-vsock/pkg/tap" |
| 55 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 56 | "gvisor.dev/gvisor/pkg/tcpip" |
| 57 | + "gvisor.dev/gvisor/pkg/tcpip/header" |
| 58 | "gvisor.dev/gvisor/pkg/tcpip/link/sniffer" |
| 59 | "gvisor.dev/gvisor/pkg/tcpip/network/arp" |
| 60 | "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 61 | @@ -78,6 +79,9 @@ |
| 62 | return nil, fmt.Errorf("cannot create network stack: %w", err) |
| 63 | } |
| 64 | |
| 65 | + if configuration.DroppedIPv6 != nil && stack.NetworkProtocolInstance(header.IPv6ProtocolNumber) == nil { |
| 66 | + networkSwitch.SetDroppedIPv6(configuration.DroppedIPv6) |
| 67 | + } |
| 68 | mux, err := addServices(configuration, stack, ipPool) |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf("cannot add network services: %w", err) |
| 71 | |
| 72 | |
| 73 | @@ -0,0 +1,22 @@ |
| 74 | +package tap |
| 75 | + |
| 76 | +import ( |
| 77 | + "bytes" |
| 78 | + "context" |
| 79 | + "testing" |
| 80 | +) |
| 81 | + |
| 82 | +// The observer must run on data read from the guest, before forwarding the |
| 83 | +// frame to any other peer or to the IPv4 stack. |
| 84 | +func TestIPv6DropAtGuestIngress(t *testing.T) { |
| 85 | + s := NewSwitch(false) |
| 86 | + payload := []byte{0x60, 0, 0, 0, 0, 0, 17, 1} |
| 87 | + ethernet := make([]byte, 14) |
| 88 | + ethernet[12], ethernet[13] = 0x86, 0xdd |
| 89 | + var seen []byte |
| 90 | + s.SetDroppedIPv6(func(packet []byte) { seen = append(seen, packet...) }) |
| 91 | + s.rxBuf(context.Background(), 1, append(ethernet, payload...)) |
| 92 | + if !bytes.Equal(seen, payload) || s.Sent != 0 || s.Received != uint64(len(ethernet)+len(payload)) { |
| 93 | + t.Fatal("IPv6 frame was not observed and dropped at ingress") |
| 94 | + } |
| 95 | +} |