|
| 1 | +// Copyright 2025 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package nftables |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "net" |
| 22 | + "time" |
| 23 | + |
| 24 | + "gvisor.dev/gvisor/test/netutils" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + dropPort = 2401 |
| 29 | + acceptPort = 2402 |
| 30 | + sendloopDuration = 2 * time.Second |
| 31 | + chainName = "foochain" |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + RegisterTestCase(&FilterInputDropAll{}) |
| 36 | +} |
| 37 | + |
| 38 | +// FilterInputDropAll tests that we can drop all traffic to the INPUT chain. |
| 39 | +type FilterInputDropAll struct{ containerCase } |
| 40 | + |
| 41 | +var _ TestCase = (*FilterInputDropAll)(nil) |
| 42 | + |
| 43 | +// Name implements TestCase.Name. |
| 44 | +func (*FilterInputDropAll) Name() string { |
| 45 | + return "FilterInputDropAll" |
| 46 | +} |
| 47 | + |
| 48 | +// ContainerAction implements TestCase.ContainerAction. |
| 49 | +func (*FilterInputDropAll) ContainerAction(ctx context.Context, ip net.IP, ipv6 bool) error { |
| 50 | + if err := createDropAllTable(ipv6, "filterTab"); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Listen for all packets on dropPort. |
| 55 | + timedCtx, cancel := context.WithTimeout(ctx, NegativeTimeout) |
| 56 | + defer cancel() |
| 57 | + if err := netutils.ListenUDP(timedCtx, dropPort, ipv6); err == nil { |
| 58 | + return fmt.Errorf("packets should have been dropped, but got a packet") |
| 59 | + } else if !errors.Is(err, context.DeadlineExceeded) { |
| 60 | + return fmt.Errorf("error reading: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + // At this point we know that reading timed out and never received a |
| 64 | + // packet. |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// LocalAction implements TestCase.LocalAction. |
| 69 | +func (*FilterInputDropAll) LocalAction(ctx context.Context, ip net.IP, ipv6 bool) error { |
| 70 | + return netutils.SendUDPLoop(ctx, ip, dropPort, ipv6) |
| 71 | +} |
0 commit comments