ipsec.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "encoding/json"
  17. "fmt"
  18. log "github.com/golang/glog"
  19. "golang.org/x/net/context"
  20. "sync"
  21. "github.com/coreos/flannel/backend"
  22. "github.com/coreos/flannel/pkg/ip"
  23. "github.com/coreos/flannel/subnet"
  24. )
  25. var CharonViciUri string
  26. const (
  27. defaultESPProposal = "aes128gcm16-sha256-prfsha256-ecp256"
  28. minPasswordLength = 96
  29. )
  30. func init() {
  31. backend.Register("ipsec", New)
  32. }
  33. type IPSECBackend struct {
  34. sm subnet.Manager
  35. extIface *backend.ExternalInterface
  36. }
  37. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (
  38. backend.Backend, error) {
  39. be := &IPSECBackend{
  40. sm: sm,
  41. extIface: extIface,
  42. }
  43. return be, nil
  44. }
  45. func (be *IPSECBackend) RegisterNetwork(
  46. ctx context.Context, wg sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  47. cfg := struct {
  48. UDPEncap bool
  49. ESPProposal string
  50. PSK string
  51. }{
  52. UDPEncap: false,
  53. ESPProposal: defaultESPProposal,
  54. }
  55. if len(config.Backend) > 0 {
  56. log.Info("i.config.backend length > 0")
  57. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  58. return nil, fmt.Errorf("error decoding IPSEC backend config: %v", err)
  59. }
  60. }
  61. if len(cfg.PSK) < minPasswordLength {
  62. return nil, fmt.Errorf(
  63. "config error, password should be at least %s characters long",
  64. minPasswordLength)
  65. }
  66. attrs := subnet.LeaseAttrs{
  67. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  68. BackendType: "ipsec",
  69. }
  70. l, err := be.sm.AcquireLease(ctx, &attrs)
  71. switch err {
  72. case nil:
  73. case context.Canceled, context.DeadlineExceeded:
  74. return nil, err
  75. default:
  76. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  77. }
  78. ikeDaemon, err := NewCharonIKEDaemon(ctx, wg, CharonViciUri, cfg.ESPProposal)
  79. if err != nil {
  80. return nil, fmt.Errorf("error creating CharonIKEDaemon struct: %v", err)
  81. }
  82. log.Info("UDPEncap: ", cfg.UDPEncap)
  83. return newNetwork(be.sm, be.extIface, cfg.UDPEncap, cfg.PSK, ikeDaemon, l)
  84. }
  85. func (be *IPSECBackend) Run(ctx context.Context) {
  86. <-ctx.Done()
  87. }