|
| 1 | +package restserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/Azure/azure-container-networking/cns" |
| 9 | + "github.com/Azure/azure-container-networking/cns/logger" |
| 10 | + "github.com/Azure/azure-container-networking/cns/types" |
| 11 | + "github.com/Azure/azure-container-networking/iptables" |
| 12 | + "github.com/Azure/azure-container-networking/network/networkutils" |
| 13 | + goiptables "github.com/coreos/go-iptables/iptables" |
| 14 | +) |
| 15 | + |
| 16 | +const SWIFT = "SWIFT-POSTROUTING" |
| 17 | + |
| 18 | +// nolint |
| 19 | +func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainerRequest) (types.ResponseCode, string) { |
| 20 | + service.Lock() |
| 21 | + defer service.Unlock() |
| 22 | + |
| 23 | + // Parse primary ip and ipnet from nnc |
| 24 | + ncPrimaryIP, ncIPNet, _ := net.ParseCIDR(req.IPConfiguration.IPSubnet.IPAddress + "/" + fmt.Sprintf("%d", req.IPConfiguration.IPSubnet.PrefixLength)) |
| 25 | + ipt, err := goiptables.New() |
| 26 | + if err != nil { |
| 27 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to create iptables interface : %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + chainExist, err := ipt.ChainExists(iptables.Nat, SWIFT) |
| 31 | + if err != nil { |
| 32 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SWIFT chain: %v", err) |
| 33 | + } |
| 34 | + if !chainExist { // create and append chain if it doesn't exist |
| 35 | + logger.Printf("[Azure CNS] Creating SWIFT Chain ...") |
| 36 | + err = ipt.NewChain(iptables.Nat, SWIFT) |
| 37 | + if err != nil { |
| 38 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to create SWIFT chain : " + err.Error() |
| 39 | + } |
| 40 | + logger.Printf("[Azure CNS] Append SWIFT Chain to POSTROUTING ...") |
| 41 | + err = ipt.Append(iptables.Nat, iptables.Postrouting, "-j", SWIFT) |
| 42 | + if err != nil { |
| 43 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to append SWIFT chain : " + err.Error() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + postroutingToSwiftJumpexist, err := ipt.Exists(iptables.Nat, iptables.Postrouting, "-j", SWIFT) |
| 48 | + if err != nil { |
| 49 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of POSTROUTING to SWIFT chain jump: %v", err) |
| 50 | + } |
| 51 | + if !postroutingToSwiftJumpexist { |
| 52 | + logger.Printf("[Azure CNS] Append SWIFT Chain to POSTROUTING ...") |
| 53 | + err = ipt.Append(iptables.Nat, iptables.Postrouting, "-j", SWIFT) |
| 54 | + if err != nil { |
| 55 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to append SWIFT chain : " + err.Error() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + snatUDPRuleexist, err := ipt.Exists(iptables.Nat, SWIFT, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureDNS, "-p", iptables.UDP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", ncPrimaryIP.String()) |
| 60 | + if err != nil { |
| 61 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SNAT UDP rule : %v", err) |
| 62 | + } |
| 63 | + if !snatUDPRuleexist { |
| 64 | + logger.Printf("[Azure CNS] Inserting SNAT UDP rule ...") |
| 65 | + err = ipt.Insert(iptables.Nat, SWIFT, 1, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureDNS, "-p", iptables.UDP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", ncPrimaryIP.String()) |
| 66 | + if err != nil { |
| 67 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to inset SNAT UDP rule : " + err.Error() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + snatTCPRuleexist, err := ipt.Exists(iptables.Nat, SWIFT, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureDNS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", ncPrimaryIP.String()) |
| 72 | + if err != nil { |
| 73 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SNAT TCP rule : %v", err) |
| 74 | + } |
| 75 | + if !snatTCPRuleexist { |
| 76 | + logger.Printf("[Azure CNS] Inserting SNAT TCP rule ...") |
| 77 | + err = ipt.Insert(iptables.Nat, SWIFT, 1, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureDNS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.DNSPort), "-j", iptables.Snat, "--to", ncPrimaryIP.String()) |
| 78 | + if err != nil { |
| 79 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to insert SNAT TCP rule : " + err.Error() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + snatIMDSRuleexist, err := ipt.Exists(iptables.Nat, SWIFT, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP) |
| 84 | + if err != nil { |
| 85 | + return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SNAT IMDS rule : %v", err) |
| 86 | + } |
| 87 | + if !snatIMDSRuleexist { |
| 88 | + logger.Printf("[Azure CNS] Inserting SNAT IMDS rule ...") |
| 89 | + err = ipt.Insert(iptables.Nat, SWIFT, 1, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP) |
| 90 | + if err != nil { |
| 91 | + return types.FailedToRunIPTableCmd, "[Azure CNS] failed to insert SNAT IMDS rule : " + err.Error() |
| 92 | + } |
| 93 | + } |
| 94 | + return types.Success, "" |
| 95 | +} |
0 commit comments