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