ipsec.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "crypto/rand"
  17. "encoding/base64"
  18. "encoding/json"
  19. "fmt"
  20. log "github.com/golang/glog"
  21. "golang.org/x/net/context"
  22. "github.com/coreos/flannel/backend"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "github.com/coreos/flannel/subnet"
  25. )
  26. var CharonPath string
  27. const (
  28. defaultCharonPath = "/opt/flannel/libexec/ipsec/charon"
  29. passwordLength = 40
  30. )
  31. func init() {
  32. backend.Register("ipsec", New)
  33. }
  34. type IPSECBackend struct {
  35. sm subnet.Manager
  36. extIface *backend.ExternalInterface
  37. }
  38. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  39. be := &IPSECBackend{
  40. sm: sm,
  41. extIface: extIface,
  42. }
  43. return be, nil
  44. }
  45. func (be *IPSECBackend) RegisterNetwork(ctx context.Context, netname string, config *subnet.Config) (backend.Network, error) {
  46. cfg := struct {
  47. UDPEncap bool
  48. }{}
  49. if len(config.Backend) > 0 {
  50. log.Info("i.config.backend length > 0")
  51. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  52. return nil, fmt.Errorf("error decoding IPSEC backend config: %v", err)
  53. }
  54. }
  55. attrs := subnet.LeaseAttrs{
  56. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  57. BackendType: "ipsec",
  58. }
  59. l, err := be.sm.AcquireLease(ctx, netname, &attrs)
  60. switch err {
  61. case nil:
  62. case context.Canceled, context.DeadlineExceeded:
  63. return nil, err
  64. default:
  65. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  66. }
  67. if CharonPath == "" {
  68. CharonPath = defaultCharonPath
  69. }
  70. ikeDaemon, err := NewCharonIKEDaemon(CharonPath)
  71. if err != nil {
  72. return nil, fmt.Errorf("error creating CharonIKEDaemon struct: %v", err)
  73. }
  74. log.Info("UDPEncap: ", cfg.UDPEncap)
  75. password, err := GenerateRandomString(passwordLength)
  76. if err != nil {
  77. return nil, fmt.Errorf("error generating random string: %v", err)
  78. }
  79. err = be.sm.CreateBackendData(ctx, netname, password)
  80. if err != nil {
  81. return nil, fmt.Errorf("error creating password: %v", err)
  82. }
  83. password, err = be.sm.GetBackendData(ctx, netname)
  84. if err != nil {
  85. return nil, fmt.Errorf("error getting password: %v", err)
  86. }
  87. return newNetwork(netname, be.sm, be.extIface, cfg.UDPEncap, password, ikeDaemon, l)
  88. }
  89. func (be *IPSECBackend) Run(ctx context.Context) {
  90. <-ctx.Done()
  91. }
  92. func generateRandomBytes(n int) ([]byte, error) {
  93. b := make([]byte, n)
  94. _, err := rand.Read(b)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return b, nil
  99. }
  100. func GenerateRandomString(s int) (string, error) {
  101. b, err := generateRandomBytes(s)
  102. if err != nil {
  103. return "", err
  104. }
  105. return base64.StdEncoding.EncodeToString(b), nil
  106. }