Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPSec remote mechanism #151

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/api/networkservice/mechanisms/ipsec/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ipsec

import (
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/common"
)

const (
// MECHANISM type string
MECHANISM = "IPSEC"

// Mechanism parameters

// SrcIP - source IP
SrcIP = common.SrcIP
// DstIP - destination IP
DstIP = common.DstIP
// SrcPort - Source interface listening port
SrcPort = common.SrcPort
// DstPort - Destination interface listening port
DstPort = common.DstPort
// SrcPublicKey - Source public key
SrcPublicKey = "src_public_key"
// DstPublicKey - Destination public key
DstPublicKey = "dst_public_key"

// MTU - maximum transmission unit
MTU = common.MTU
)
18 changes: 18 additions & 0 deletions pkg/api/networkservice/mechanisms/ipsec/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package ipsec - constants and helper methods for IPSec remote mechanism
package ipsec
154 changes: 154 additions & 0 deletions pkg/api/networkservice/mechanisms/ipsec/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ipsec

import (
"net"
"strconv"

"github.com/networkservicemesh/api/pkg/api/networkservice"
)

// Mechanism is an ipsec mechanism helper
type Mechanism struct {
*networkservice.Mechanism
}

// ToMechanism - convert unified mechanism to useful wrapper
func ToMechanism(m *networkservice.Mechanism) *Mechanism {
if m.GetType() == MECHANISM {
if m.GetParameters() == nil {
m.Parameters = map[string]string{}
}
return &Mechanism{
m,
}
}
return nil
}

// SrcIP returns source ip
func (m *Mechanism) SrcIP() net.IP {
return net.ParseIP(m.GetParameters()[SrcIP])
}

// SetSrcIP sets source ip
func (m *Mechanism) SetSrcIP(ip net.IP) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[SrcIP] = ip.String()
return m
}

// DstIP returns destination ip
func (m *Mechanism) DstIP() net.IP {
return net.ParseIP(m.GetParameters()[DstIP])
}

// SetDstIP sets destination ip
func (m *Mechanism) SetDstIP(ip net.IP) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[DstIP] = ip.String()
return m
}

// SrcPublicKey returns the SrcPublicKey parameter of the Mechanism
func (m *Mechanism) SrcPublicKey() string {
return m.GetParameters()[SrcPublicKey]
}

// SetSrcPublicKey sets new source public key
func (m *Mechanism) SetSrcPublicKey(key string) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[SrcPublicKey] = key
return m
}

// DstPublicKey returns the DstPublicKey parameter of the Mechanism
func (m *Mechanism) DstPublicKey() string {
return m.GetParameters()[DstPublicKey]
}

// SetDstPublicKey sets new destination public key
func (m *Mechanism) SetDstPublicKey(key string) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[DstPublicKey] = key
return m
}

// SrcPort - Source interface listening port
func (m *Mechanism) SrcPort() uint16 {
return atou16(m.GetParameters()[SrcPort])
}

// SetSrcPort sets source udp port
func (m *Mechanism) SetSrcPort(port uint16) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[SrcPort] = strconv.FormatUint(uint64(port), 10)
return m
}

// DstPort - Destination interface listening port
func (m *Mechanism) DstPort() uint16 {
return atou16(m.GetParameters()[DstPort])
}

// SetDstPort sets destination udp port
func (m *Mechanism) SetDstPort(port uint16) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[DstPort] = strconv.FormatUint(uint64(port), 10)
return m
}

// MTU - return MTU value - 0 if unset
func (m *Mechanism) MTU() uint32 {
mtu, err := strconv.ParseUint(m.GetParameters()[MTU], 10, 32)
if err != nil {
return 0
}

return uint32(mtu)
}

// SetMTU - set the MTU value
func (m *Mechanism) SetMTU(mtu uint32) *Mechanism {
if m == nil {
return nil
}
m.GetParameters()[MTU] = strconv.FormatUint(uint64(mtu), 10)

return m
}

func atou16(a string) uint16 {
u, err := strconv.ParseUint(a, 10, 16)
if err != nil {
return 0
}
return uint16(u)
}