go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/wireguardplugin/vppcalls/vpp2106/wireguard_vppcalls.go (about)

     1  //  Copyright (c) 2021 Doc.ai and/or its affiliates.
     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  
    15  package vpp2106
    16  
    17  import (
    18  	"encoding/base64"
    19  	"fmt"
    20  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/interface_types"
    21  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/ip_types"
    22  	vpp_wg "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/wireguard"
    23  	wg "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/wireguard"
    24  )
    25  
    26  func (h *WgVppHandler) AddPeer(peer *wg.Peer) (uint32, error) {
    27  	invalidIdx := ^uint32(0)
    28  
    29  	peer_vpp := vpp_wg.WireguardPeer{
    30  		Port:                uint16(peer.Port),
    31  		PersistentKeepalive: uint16(peer.PersistentKeepalive),
    32  	}
    33  
    34  	publicKeyBin, err := base64.StdEncoding.DecodeString(peer.PublicKey)
    35  	if err != nil {
    36  		return invalidIdx, err
    37  	}
    38  	peer_vpp.PublicKey = publicKeyBin
    39  
    40  	ifaceMeta, found := h.ifIndexes.LookupByName(peer.WgIfName)
    41  	if !found {
    42  		return invalidIdx, fmt.Errorf("failed to get interface metadata")
    43  	}
    44  	peer_vpp.SwIfIndex = interface_types.InterfaceIndex(ifaceMeta.SwIfIndex)
    45  	peer_vpp.TableID = ifaceMeta.Vrf
    46  
    47  	peer_vpp.Endpoint, err = ip_types.ParseAddress(peer.Endpoint)
    48  	if err != nil {
    49  		return invalidIdx, err
    50  	}
    51  
    52  	for _, allowedIp := range peer.AllowedIps {
    53  		prefix, err := ip_types.ParsePrefix(allowedIp);
    54  		if err != nil {
    55  			return invalidIdx, err
    56  		}
    57  		peer_vpp.AllowedIps = append(peer_vpp.AllowedIps, prefix);
    58  	}
    59  
    60  	request := &vpp_wg.WireguardPeerAdd {
    61  		Peer: peer_vpp,
    62  	};
    63  	// prepare reply
    64  	reply := &vpp_wg.WireguardPeerAddReply{}
    65  	// send request and obtain reply
    66  	if err := h.callsChannel.SendRequest(request).ReceiveReply(reply); err != nil {
    67  		return invalidIdx, err
    68  	}
    69  	return reply.PeerIndex, nil;
    70  }
    71  
    72  func (h *WgVppHandler) RemovePeer(peer_idx uint32) error {
    73  	// prepare request
    74  	request := &vpp_wg.WireguardPeerRemove{
    75  		PeerIndex: peer_idx,
    76  	}
    77  	// prepare reply
    78  	reply := &vpp_wg.WireguardPeerRemoveReply{}
    79  
    80  	// send request and obtain reply
    81  	if err := h.callsChannel.SendRequest(request).ReceiveReply(reply); err != nil {
    82  		return err
    83  	}
    84  	return nil;
    85  }