|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. |
| 3 | + |
| 4 | +// Package evpnipsec implements the ipsec related CLI commands |
| 5 | +package evpnipsec |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/opiproject/godpu/cmd/common" |
| 14 | + "github.com/opiproject/godpu/evpnipsec" |
| 15 | + pb "github.com/opiproject/opi-evpn-bridge/pkg/ipsec/gen/go" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +// AddSaCommand Add Sa Command |
| 20 | +func AddSaCommand() *cobra.Command { |
| 21 | + var ( |
| 22 | + src string |
| 23 | + dst string |
| 24 | + spi uint32 |
| 25 | + proto int32 |
| 26 | + ifID uint32 |
| 27 | + reqid uint32 |
| 28 | + mode int32 |
| 29 | + intrface string |
| 30 | + encAlg string |
| 31 | + encKey string |
| 32 | + intAlg string |
| 33 | + intKey string |
| 34 | + replayWindow uint32 |
| 35 | + tfc uint32 |
| 36 | + encap int32 |
| 37 | + esn int32 |
| 38 | + copyDf int32 |
| 39 | + copyEcn int32 |
| 40 | + copyDscp int32 |
| 41 | + initiator int32 |
| 42 | + inbound int32 |
| 43 | + update int32 |
| 44 | + ) |
| 45 | + // Create the map of string to CryptoAlgorithm |
| 46 | + var EncAlgorithms = map[string]pb.CryptoAlgorithm{ |
| 47 | + "rsvd": pb.CryptoAlgorithm_ENCR_RSVD, |
| 48 | + "null": pb.CryptoAlgorithm_ENCR_NULL, |
| 49 | + "aes_cbc": pb.CryptoAlgorithm_ENCR_AES_CBC, |
| 50 | + "aes_ctr": pb.CryptoAlgorithm_ENCR_AES_CTR, |
| 51 | + "aes_ccm_icv_8": pb.CryptoAlgorithm_ENCR_AES_CCM_8, |
| 52 | + "aes_ccm_icv_12": pb.CryptoAlgorithm_ENCR_AES_CCM_12, |
| 53 | + "aes_ccm_icv_16": pb.CryptoAlgorithm_ENCR_AES_CCM_16, |
| 54 | + "aes_gcm_icv_8": pb.CryptoAlgorithm_ENCR_AES_GCM_8, |
| 55 | + "aes_gcm_icv_12": pb.CryptoAlgorithm_ENCR_AES_GCM_12, |
| 56 | + "aes_gcm_icv_16": pb.CryptoAlgorithm_ENCR_AES_GCM_16, |
| 57 | + "aes_gmac": pb.CryptoAlgorithm_ENCR_NULL_AUTH_AES_GMAC, |
| 58 | + "chacha_poly": pb.CryptoAlgorithm_ENCR_CHACHA20_POLY1305, |
| 59 | + } |
| 60 | + var IntAlgorithms = map[string]pb.IntegAlgorithm{ |
| 61 | + "sha1_96": pb.IntegAlgorithm_AUTH_HMAC_SHA1_96, |
| 62 | + "xcbc_96": pb.IntegAlgorithm_AUTH_AES_XCBC_96, |
| 63 | + "cmac_96": pb.IntegAlgorithm_AUTH_AES_CMAC_96, |
| 64 | + "gmac_128": pb.IntegAlgorithm_AUTH_AES_128_GMAC, |
| 65 | + "gmac_192": pb.IntegAlgorithm_AUTH_AES_192_GMAC, |
| 66 | + "gmac_256": pb.IntegAlgorithm_AUTH_AES_256_GMAC, |
| 67 | + "sha2_128": pb.IntegAlgorithm_AUTH_HMAC_SHA2_256_128, |
| 68 | + "sha2_192": pb.IntegAlgorithm_AUTH_HMAC_SHA2_384_192, |
| 69 | + "sha2_256": pb.IntegAlgorithm_AUTH_HMAC_SHA2_512_256, |
| 70 | + "none": pb.IntegAlgorithm_NONE, |
| 71 | + } |
| 72 | + |
| 73 | + var cmd = &cobra.Command{ |
| 74 | + Use: "add-sa", |
| 75 | + Aliases: []string{"c"}, |
| 76 | + Short: "add-sa functionality", |
| 77 | + Args: cobra.NoArgs, |
| 78 | + Run: func(c *cobra.Command, _ []string) { |
| 79 | + tlsFiles, err := c.Flags().GetString(common.TLSFiles) |
| 80 | + cobra.CheckErr(err) |
| 81 | + |
| 82 | + addr, err := c.Flags().GetString(common.AddrCmdLineArg) |
| 83 | + cobra.CheckErr(err) |
| 84 | + |
| 85 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 86 | + defer cancel() |
| 87 | + |
| 88 | + IPSecEvpnClient, err := evpnipsec.NewIPSecClient(addr, tlsFiles) |
| 89 | + if err != nil { |
| 90 | + log.Printf("error Adding SA: %s\n", err) |
| 91 | + log.Println("ONE") |
| 92 | + } |
| 93 | + |
| 94 | + data, err := IPSecEvpnClient.AddSA(ctx, |
| 95 | + src, dst, spi, proto, ifID, reqid, mode, intrface, int32(EncAlgorithms[encAlg]), encKey, int32(IntAlgorithms[intAlg]), intKey, |
| 96 | + replayWindow, tfc, encap, esn, copyDf, copyEcn, copyDscp, initiator, inbound, update, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + log.Printf("error error Adding SA: %s\n", err) |
| 100 | + log.Println("TWO") |
| 101 | + } |
| 102 | + fmt.Println("Add SA Req marshaled successfully:", data) |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + cmd.Flags().StringVar(&src, "src", "", "Source address or hostname") |
| 107 | + cmd.Flags().StringVar(&dst, "dst", "", "Destination address or hostname") |
| 108 | + cmd.Flags().Uint32Var(&spi, "spi", 0, "SPI") |
| 109 | + cmd.Flags().Int32Var(&proto, "proto", 0, "Protocol (ESP/AH)") |
| 110 | + cmd.Flags().Uint32Var(&ifID, "if_id", 0, "Interface ID") |
| 111 | + cmd.Flags().Uint32Var(&reqid, "reqid", 0, "Reqid") |
| 112 | + cmd.Flags().Int32Var(&mode, "mode", 0, "Mode (tunnel, transport...)") |
| 113 | + cmd.Flags().StringVar(&intrface, "interface", "", "Network interface restricting policy") |
| 114 | + cmd.Flags().StringVar(&encAlg, "enc_alg", "aes_cbc", "rsvd, null, aes_cbc, aes_ctr, aes_ccm_icv_8, aes_ccm_icv_12, aes_ccm_icv_16, aes_gcm_icv_8, aes_gcm_icv_12, aes_gcm_icv_16, aes_gmac, chacha_poly") |
| 115 | + cmd.Flags().StringVar(&encKey, "enc_key", "", "Encryption key") |
| 116 | + cmd.Flags().StringVar(&intAlg, "int_alg", "none", "Integrity protection algorithm: sha1_96, xcbc_96, cmac_96, gmac_128, gmac_192, gmac_256, sha2_128, sha2_192, sha2_256, none") |
| 117 | + cmd.Flags().StringVar(&intKey, "int_key", "", "Integrity protection key") |
| 118 | + cmd.Flags().Uint32Var(&replayWindow, "replay_window", 0, "Anti-replay window size") |
| 119 | + cmd.Flags().Uint32Var(&tfc, "tfc", 0, "Traffic Flow Confidentiality padding") |
| 120 | + cmd.Flags().Int32Var(&encap, "encap", 0, "Enable UDP encapsulation for NAT traversal") |
| 121 | + cmd.Flags().Int32Var(&esn, "esn", 0, "Mark the SA should apply to packets after processing") |
| 122 | + cmd.Flags().Int32Var(©Df, "copy_df", 0, "Copy the DF bit to the outer IPv4 header in tunnel mode") |
| 123 | + cmd.Flags().Int32Var(©Ecn, "copy_ecn", 0, "Copy the ECN header field to/from the outer header") |
| 124 | + cmd.Flags().Int32Var(©Dscp, "copy_dscp", 0, "Copy the DSCP header field to/from the outer header") |
| 125 | + cmd.Flags().Int32Var(&initiator, "initiator", 0, "TRUE if initiator of the exchange creating the SA") |
| 126 | + cmd.Flags().Int32Var(&inbound, "inbound", 0, "TRUE if this is an inbound SA") |
| 127 | + cmd.Flags().Int32Var(&update, "update", 0, "TRUE if an SPI has already been allocated for this SA") |
| 128 | + |
| 129 | + if err := cmd.MarkFlagRequired("src"); err != nil { |
| 130 | + log.Fatalf("Error marking flag as required: %v", err) |
| 131 | + } |
| 132 | + if err := cmd.MarkFlagRequired("dst"); err != nil { |
| 133 | + log.Fatalf("Error marking flag as required: %v", err) |
| 134 | + } |
| 135 | + if err := cmd.MarkFlagRequired("spi"); err != nil { |
| 136 | + log.Fatalf("Error marking flag as required: %v", err) |
| 137 | + } |
| 138 | + if err := cmd.MarkFlagRequired("if_id"); err != nil { |
| 139 | + log.Fatalf("Error marking flag as required: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + return cmd |
| 143 | +} |
| 144 | + |
| 145 | +// DelSaCommand tests the del SA |
| 146 | +func DelSaCommand() *cobra.Command { |
| 147 | + var ( |
| 148 | + src string |
| 149 | + dst string |
| 150 | + spi uint32 |
| 151 | + proto int32 |
| 152 | + ifID uint32 |
| 153 | + ) |
| 154 | + |
| 155 | + var cmd = &cobra.Command{ |
| 156 | + Use: "Del-sa", |
| 157 | + Aliases: []string{"c"}, |
| 158 | + Short: "add-sa functionality", |
| 159 | + Args: cobra.NoArgs, |
| 160 | + Run: func(c *cobra.Command, _ []string) { |
| 161 | + tlsFiles, err := c.Flags().GetString(common.TLSFiles) |
| 162 | + cobra.CheckErr(err) |
| 163 | + |
| 164 | + addr, err := c.Flags().GetString(common.AddrCmdLineArg) |
| 165 | + cobra.CheckErr(err) |
| 166 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 167 | + defer cancel() |
| 168 | + IPSecEvpnClient, err := evpnipsec.NewIPSecClient(addr, tlsFiles) |
| 169 | + if err != nil { |
| 170 | + log.Printf("error Deleting SA %s\n", err) |
| 171 | + } |
| 172 | + data, err := IPSecEvpnClient.DelSA(ctx, src, dst, spi, proto, ifID) |
| 173 | + if err != nil { |
| 174 | + log.Printf("error Deleting SA %s\n", err) |
| 175 | + } |
| 176 | + fmt.Println("Deleting SA successfully:", data) |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + cmd.Flags().StringVar(&src, "src", "", "Source address or hostname") |
| 181 | + cmd.Flags().StringVar(&dst, "dst", "", "Destination address or hostname") |
| 182 | + cmd.Flags().Uint32Var(&spi, "spi", 0, "SPI") |
| 183 | + cmd.Flags().Int32Var(&proto, "proto", 0, "Protocol (ESP/AH)") |
| 184 | + cmd.Flags().Uint32Var(&ifID, "if_id", 0, "Interface ID") |
| 185 | + if err := cmd.MarkFlagRequired("src"); err != nil { |
| 186 | + log.Fatalf("Error marking flag as required: %v", err) |
| 187 | + } |
| 188 | + if err := cmd.MarkFlagRequired("dst"); err != nil { |
| 189 | + log.Fatalf("Error marking flag as required: %v", err) |
| 190 | + } |
| 191 | + if err := cmd.MarkFlagRequired("spi"); err != nil { |
| 192 | + log.Fatalf("Error marking flag as required: %v", err) |
| 193 | + } |
| 194 | + if err := cmd.MarkFlagRequired("if_id"); err != nil { |
| 195 | + log.Fatalf("Error marking flag as required: %v", err) |
| 196 | + } |
| 197 | + return cmd |
| 198 | +} |
| 199 | + |
| 200 | +// NewEvpnIPSecCommand tests the inventory |
| 201 | +func NewEvpnIPSecCommand() *cobra.Command { |
| 202 | + cmd := &cobra.Command{ |
| 203 | + Use: "evpnipsec", |
| 204 | + Aliases: []string{"g"}, |
| 205 | + Short: "Tests ipsec functionality", |
| 206 | + Args: cobra.NoArgs, |
| 207 | + Run: func(cmd *cobra.Command, _ []string) { |
| 208 | + err := cmd.Help() |
| 209 | + if err != nil { |
| 210 | + log.Fatalf("[ERROR] %s", err.Error()) |
| 211 | + } |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + cmd.AddCommand(AddSaCommand()) |
| 216 | + cmd.AddCommand(DelSaCommand()) |
| 217 | + return cmd |
| 218 | +} |
0 commit comments