handle_charon.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2015 flannel 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. package ipsec
  15. import (
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "syscall"
  20. "time"
  21. "github.com/bronze1man/goStrongswanVici"
  22. log "github.com/golang/glog"
  23. "github.com/coreos/flannel/subnet"
  24. )
  25. type CharonIKEDaemon struct {
  26. path string
  27. }
  28. func NewCharonIKEDaemon(charonPath string) (*CharonIKEDaemon, error) {
  29. path, err := exec.LookPath(charonPath)
  30. if err != nil {
  31. return nil, err
  32. }
  33. log.Info("Launching IKE charon path: ", path)
  34. return &CharonIKEDaemon{
  35. path: path,
  36. }, nil
  37. }
  38. func (charon *CharonIKEDaemon) Run() error {
  39. cmd := exec.Cmd{
  40. Path: charon.path,
  41. SysProcAttr: &syscall.SysProcAttr{
  42. Pdeathsig: syscall.SIGTERM,
  43. },
  44. }
  45. cmd.Stderr = os.Stderr
  46. return cmd.Run()
  47. }
  48. func (charon *CharonIKEDaemon) LoadSharedKey(remotePublicIP, password string) error {
  49. var err error
  50. var client *goStrongswanVici.ClientConn
  51. for {
  52. client, err = goStrongswanVici.NewClientConnFromDefaultSocket()
  53. if err == nil {
  54. break
  55. } else {
  56. log.Error("ClientConnection failed: ", err)
  57. log.Infof("Retrying in 1 second ...")
  58. time.Sleep(1 * time.Second)
  59. }
  60. }
  61. defer client.Close()
  62. sharedKey := &goStrongswanVici.Key{
  63. Typ: "IKE",
  64. Data: password,
  65. Owners: []string{remotePublicIP},
  66. }
  67. err = client.LoadShared(sharedKey)
  68. if err != nil {
  69. return err
  70. }
  71. log.Infof("Loaded shared key for: %v", remotePublicIP)
  72. return nil
  73. }
  74. func (charon *CharonIKEDaemon) LoadConnection(localLease, remoteLease *subnet.Lease, reqID, encap string) error {
  75. var err error
  76. var client *goStrongswanVici.ClientConn
  77. for {
  78. client, err = goStrongswanVici.NewClientConnFromDefaultSocket()
  79. if err == nil {
  80. break
  81. } else {
  82. log.Info("ClientConnection failed: ", err)
  83. log.Infof("Retying in 1 second ...")
  84. time.Sleep(1 * time.Second)
  85. }
  86. }
  87. defer client.Close()
  88. childConfMap := make(map[string]goStrongswanVici.ChildSAConf)
  89. childSAConf := goStrongswanVici.ChildSAConf{
  90. Local_ts: []string{localLease.Subnet.String()},
  91. Remote_ts: []string{remoteLease.Subnet.String()},
  92. ESPProposals: []string{"aes256-sha256-modp4096"},
  93. StartAction: "start",
  94. CloseAction: "trap",
  95. Mode: "tunnel",
  96. ReqID: reqID,
  97. // RekeyTime: rekeyTime,
  98. InstallPolicy: "no",
  99. }
  100. childSAConfName := formatChildSAConfName(localLease, remoteLease)
  101. childConfMap[childSAConfName] = childSAConf
  102. localAuthConf := goStrongswanVici.AuthConf{
  103. AuthMethod: "psk",
  104. }
  105. remoteAuthConf := goStrongswanVici.AuthConf{
  106. AuthMethod: "psk",
  107. }
  108. ikeConf := goStrongswanVici.IKEConf{
  109. LocalAddrs: []string{localLease.Attrs.PublicIP.String()},
  110. RemoteAddrs: []string{remoteLease.Attrs.PublicIP.String()},
  111. Proposals: []string{"aes256-sha256-modp4096"},
  112. Version: "1",
  113. KeyingTries: "0", //continues to retry
  114. LocalAuth: localAuthConf,
  115. RemoteAuth: remoteAuthConf,
  116. Children: childConfMap,
  117. Encap: encap,
  118. }
  119. ikeConfMap := make(map[string]goStrongswanVici.IKEConf)
  120. connectionName := formatConnectionName(localLease, remoteLease)
  121. ikeConfMap[connectionName] = ikeConf
  122. err = client.LoadConn(&ikeConfMap)
  123. if err != nil {
  124. return err
  125. }
  126. log.Infof("Loaded connection: %v", connectionName)
  127. return nil
  128. }
  129. func (charon *CharonIKEDaemon) UnloadCharonConnection(localLease, remoteLease *subnet.Lease) error {
  130. client, err := goStrongswanVici.NewClientConnFromDefaultSocket()
  131. if err != nil {
  132. return err
  133. }
  134. defer client.Close()
  135. connectionName := formatConnectionName(localLease, remoteLease)
  136. unloadConnRequest := &goStrongswanVici.UnloadConnRequest{
  137. Name: connectionName,
  138. }
  139. err = client.UnloadConn(unloadConnRequest)
  140. if err != nil {
  141. return err
  142. }
  143. log.Infof("Unloaded connection: %v", connectionName)
  144. return nil
  145. }
  146. func formatConnectionName(localLease, remoteLease *subnet.Lease) string {
  147. return fmt.Sprintf("%s-%s-%s-%s", localLease.Attrs.PublicIP, localLease.Subnet, remoteLease.Subnet, remoteLease.Attrs.PublicIP)
  148. }
  149. func formatChildSAConfName(localLease, remoteLease *subnet.Lease) string {
  150. return fmt.Sprintf("%s-%s", localLease.Subnet, remoteLease.Subnet)
  151. }