Skip to content

Commit ad91098

Browse files
FingerLeaderroot
andauthored
Add flag accept-tcp-flag to network delay (#195)
* add flag to network delay Signed-off-by: FingerLeader <[email protected]> * edit some details Signed-off-by: FingerLeader <[email protected]> * update test Signed-off-by: root <[email protected]> * change the logic of adding chains Signed-off-by: FingerLeader <[email protected]> * edit some details Signed-off-by: FingerLeader <[email protected]> Co-authored-by: root <[email protected]>
1 parent 91bb4c9 commit ad91098

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

cmd/attack/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewNetworkDelayCommand(dep fx.Option, options *core.NetworkCommand) *cobra.
8484
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
8585
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
8686
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
87+
cmd.Flags().StringVarP(&options.AcceptTCPFlags, "accept-tcp-flags", "", "", "only the packet which match the tcp flag can be accepted, others will be dropped. only set when the protocol is tcp.")
8788

8889
return cmd
8990
}

pkg/core/network.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ func (n *NetworkCommand) validNetworkDelay() error {
137137
return errors.Errorf("ip addressed %s not valid", n.IPAddress)
138138
}
139139

140+
if len(n.AcceptTCPFlags) > 0 && n.IPProtocol != "tcp" {
141+
return errors.Errorf("protocol should be 'tcp' when set accept-tcp-flags")
142+
}
143+
140144
return checkProtocolAndPorts(n.IPProtocol, n.SourcePort, n.EgressPort)
141145
}
142146

@@ -519,24 +523,20 @@ func (n *NetworkCommand) NeedApplyTC() bool {
519523
}
520524
}
521525

522-
func (n *NetworkCommand) PartitionChain(ipset string) ([]*pb.Chain, error) {
523-
if n.Action != NetworkPartitionAction {
524-
return nil, nil
525-
}
526-
526+
func (n *NetworkCommand) AdditionalChain(ipset string) ([]*pb.Chain, error) {
527527
chains := make([]*pb.Chain, 0, 2)
528528
var toChains, fromChains []*pb.Chain
529529
var err error
530530

531531
if n.Direction == "to" || n.Direction == "both" {
532-
toChains, err = n.getPartitionChain(ipset, "to")
532+
toChains, err = n.getAdditionalChain(ipset, "to")
533533
if err != nil {
534534
return nil, err
535535
}
536536
}
537537

538538
if n.Direction == "from" || n.Direction == "both" {
539-
fromChains, err = n.getPartitionChain(ipset, "from")
539+
fromChains, err = n.getAdditionalChain(ipset, "from")
540540
if err != nil {
541541
return nil, err
542542
}
@@ -548,7 +548,7 @@ func (n *NetworkCommand) PartitionChain(ipset string) ([]*pb.Chain, error) {
548548
return chains, nil
549549
}
550550

551-
func (n *NetworkCommand) getPartitionChain(ipset, direction string) ([]*pb.Chain, error) {
551+
func (n *NetworkCommand) getAdditionalChain(ipset, direction string) ([]*pb.Chain, error) {
552552
var directionStr string
553553
var directionChain pb.Chain_Direction
554554
if direction == "to" {
@@ -573,14 +573,15 @@ func (n *NetworkCommand) getPartitionChain(ipset, direction string) ([]*pb.Chain
573573
})
574574
}
575575

576-
chains = append(chains, &pb.Chain{
577-
Name: fmt.Sprintf("%s/1", directionStr),
578-
Ipsets: []string{ipset},
579-
Direction: directionChain,
580-
Protocol: n.IPProtocol,
581-
Target: "DROP",
582-
})
583-
576+
if n.Action == NetworkPartitionAction {
577+
chains = append(chains, &pb.Chain{
578+
Name: fmt.Sprintf("%s/1", directionStr),
579+
Ipsets: []string{ipset},
580+
Direction: directionChain,
581+
Protocol: n.IPProtocol,
582+
Target: "DROP",
583+
})
584+
}
584585
return chains, nil
585586
}
586587

@@ -596,6 +597,13 @@ func (n *NetworkCommand) NeedApplyDNSServer() bool {
596597
return len(n.DNSServer) > 0
597598
}
598599

600+
func (n *NetworkCommand) NeedAdditionalChains() bool {
601+
if n.Action != NetworkPartitionAction || (n.Action == NetworkDelayAction && len(n.AcceptTCPFlags) != 0) {
602+
return true
603+
}
604+
return false
605+
}
606+
599607
func NewNetworkCommand() *NetworkCommand {
600608
return &NetworkCommand{
601609
CommonAttackConfig: CommonAttackConfig{

pkg/core/network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestPatitionChain(t *testing.T) {
130130
},
131131
}
132132
for _, tc := range testCases {
133-
chains, err := tc.cmd.PartitionChain("test")
133+
chains, err := tc.cmd.AdditionalChain("test")
134134
if err != nil {
135135
t.Errorf("failed to partition chain: %v", err)
136136
}

pkg/server/chaosd/network.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ func (s *Server) applyIptables(attack *core.NetworkCommand, ipset, uid string) e
140140
return perrors.WithStack(err)
141141
}
142142
chains := core.IptablesRuleList(iptables).ToChains()
143-
newChains, err := attack.PartitionChain(ipset)
144-
if err != nil {
145-
return perrors.WithStack(err)
143+
// Presently, only partition and delay with `accept-tcp-flags` need to add additional chains
144+
if attack.NeedAdditionalChains() {
145+
newChains, err := attack.AdditionalChain(ipset)
146+
if err != nil {
147+
return perrors.WithStack(err)
148+
}
149+
chains = append(chains, newChains...)
146150
}
147-
chains = append(chains, newChains...)
148151

149152
if _, err := s.svr.SetIptablesChains(context.Background(), &pb.IptablesChainsRequest{
150153
Chains: chains,

0 commit comments

Comments
 (0)