FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ipip_api.c
Go to the documentation of this file.
1 /*
2  * ipip_api.c - ipip api
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlibmemory/api.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/interface.h>
22 #include <vnet/ipip/ipip.h>
23 #include <vnet/vnet.h>
24 #include <vnet/ip/ip_types_api.h>
26 
27 #include <vnet/ipip/ipip.api_enum.h>
28 #include <vnet/ipip/ipip.api_types.h>
29 
30 #define REPLY_MSG_ID_BASE im->msg_id_base
32 
33 static void
35 {
38  int rv = 0;
39  u32 fib_index, sw_if_index = ~0;
41  ip46_address_t src, dst;
42  ip46_type_t itype[2];
44 
45  itype[0] = ip_address_decode (&mp->tunnel.src, &src);
46  itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
47 
48  if (itype[0] != itype[1])
49  {
50  rv = VNET_API_ERROR_INVALID_PROTOCOL;
51  goto out;
52  }
53 
54  if (ip46_address_is_equal (&src, &dst))
55  {
56  rv = VNET_API_ERROR_SAME_SRC_DST;
57  goto out;
58  }
59 
61 
62  if (rv)
63  goto out;
64 
65  rv = tunnel_mode_decode (mp->tunnel.mode, &mode);
66 
67  if (rv)
68  goto out;
69 
70  fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
71  ntohl (mp->tunnel.table_id));
72 
73  if (~0 == fib_index)
74  {
75  rv = VNET_API_ERROR_NO_SUCH_FIB;
76  }
77  else
78  {
79  rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
82  ntohl (mp->tunnel.instance), &src, &dst,
83  fib_index, flags,
84  ip_dscp_decode (mp->tunnel.dscp), mode,
85  &sw_if_index);
86  }
87 
88 out:
89  /* *INDENT-OFF* */
90  REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
91  ({
92  rmp->sw_if_index = ntohl(sw_if_index);
93  }));
94  /* *INDENT-ON* */
95 }
96 
97 static void
99 {
101  vl_api_ipip_del_tunnel_reply_t *rmp;
102 
103  int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
104 
105  REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
106 }
107 
108 static void
110 {
113  bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
114  fib_table_t *ft;
115  int rv = 0;
116 
119 
120  /* *INDENT-OFF* */
121  REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
122  ({
125  rmp->tunnel.table_id = htonl (ft->ft_table_id);
126  rmp->tunnel.instance = htonl (t->user_instance);
127  rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
128  rmp->tunnel.dscp = ip_dscp_encode(t->dscp);
130  }));
131  /* *INDENT-ON* */
132 }
133 
134 static void
136 {
138  ipip_tunnel_t *t;
140 
141  sw_if_index = ntohl (mp->sw_if_index);
142 
143  if (sw_if_index == ~0)
144  {
145  /* *INDENT-OFF* */
146  pool_foreach (t, im->tunnels)
147  {
149  }
150  /* *INDENT-ON* */
151  }
152  else
153  {
155  if (t)
156  send_ipip_tunnel_details (t, mp);
157  }
158 }
159 
160 static void
162 {
165  u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
166  int rv;
167 
168  sixrd_tunnel_index = ~0;
169  ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
170  ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
171 
172  if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
173 
174  {
175  rv = VNET_API_ERROR_NO_SUCH_FIB;
176  }
177  else
178  {
179  rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
180  mp->ip6_prefix.len,
181  (ip4_address_t *) & mp->ip4_prefix.address,
182  mp->ip4_prefix.len,
183  (ip4_address_t *) & mp->ip4_src,
184  mp->security_check,
185  ip4_fib_index, ip6_fib_index,
186  &sixrd_tunnel_index);
187  }
188 
189  /* *INDENT-OFF* */
190  REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
191  ({
192  rmp->sw_if_index = htonl (sixrd_tunnel_index);
193  }));
194  /* *INDENT-ON* */
195 }
196 
197 static void
199 {
201  vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
202 
203  int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
204 
205  REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
206 }
207 
208 /*
209  * ipip_api_hookup
210  * Add vpe's API message handlers to the table.
211  * vlib has already mapped shared memory and
212  * added the client registration handlers.
213  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
214  */
215 /* API definitions */
216 #include <vnet/format_fns.h>
217 #include <vnet/ipip/ipip.api.c>
218 
219 static clib_error_t *
221 {
223 
224  /*
225  * Set up the (msg_name, crc, message-id) table
226  */
227  im->msg_id_base = setup_message_id_table ();
228 
229  return 0;
230 }
231 
233 
234 /*
235  * fd.io coding-style-patch-verification: ON
236  *
237  * Local Variables:
238  * eval: (c-set-style "gnu")
239  * End:
240  */
vl_api_ipip_6rd_add_tunnel_t::security_check
bool security_check
Definition: ipip.api:114
vl_api_ipip_del_tunnel_t_handler
static void vl_api_ipip_del_tunnel_t_handler(vl_api_ipip_del_tunnel_t *mp)
Definition: ipip_api.c:98
fib_proto_from_ip46
fib_protocol_t fib_proto_from_ip46(ip46_type_t iproto)
Convert from ip46_type to fib_protocol.
Definition: fib_types.c:409
im
vnet_interface_main_t * im
Definition: interface_output.c:395
api.h
tunnel_encap_decap_flags_t
enum tunnel_encap_decap_flags_t_ tunnel_encap_decap_flags_t
is_ipv6
bool is_ipv6
Definition: dhcp.api:202
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
vl_api_ipip_6rd_add_tunnel_t::ip4_src
vl_api_ip4_address_t ip4_src
Definition: ipip.api:113
vl_api_ipip_add_tunnel_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: ipip.api:89
ipip_tunnel_t::fib_index
u32 fib_index
Definition: ipip.h:84
vl_api_ipip_6rd_del_tunnel_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: ipip.api:133
vl_api_ipip_6rd_add_tunnel_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: ipip.api:123
vl_api_ipip_tunnel_dump_t
List all IPIP tunnels.
Definition: ipip.api:139
ip_dscp_encode
vl_api_ip_dscp_t ip_dscp_encode(ip_dscp_t dscp)
Definition: ip_types_api.c:105
ipip_tunnel_db_find_by_sw_if_index
ipip_tunnel_t * ipip_tunnel_db_find_by_sw_if_index(u32 sw_if_index)
Definition: ipip.c:563
fib_table.h
sixrd_del_tunnel
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:374
ipip_tunnel_t::sw_if_index
u32 sw_if_index
Definition: ipip.h:86
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
tunnel_types_api.h
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
send_ipip_tunnel_details
static void send_ipip_tunnel_details(ipip_tunnel_t *t, vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:109
ipip_tunnel_t::transport
ipip_transport_t transport
Definition: ipip.h:81
vl_api_ipip_6rd_add_tunnel_t_handler
static void vl_api_ipip_6rd_add_tunnel_t_handler(vl_api_ipip_6rd_add_tunnel_t *mp)
Definition: ipip_api.c:161
ipip_tunnel_t
A representation of a IPIP tunnel.
Definition: ipip.h:75
vl_api_ipip_add_tunnel_t_handler
static void vl_api_ipip_add_tunnel_t_handler(vl_api_ipip_add_tunnel_t *mp)
Definition: ipip_api.c:34
ip_address_decode
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:186
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
vl_api_ipip_6rd_add_tunnel_t::ip4_table_id
u32 ip4_table_id
Definition: ipip.api:110
fib_table_t_
A protocol Independent FIB table.
Definition: fib_table.h:71
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vl_api_ipip_tunnel_details_t
Definition: ipip.api:146
tunnel_encap_decap_flags_decode
int tunnel_encap_decap_flags_decode(vl_api_tunnel_encap_decap_flags_t f, tunnel_encap_decap_flags_t *o)
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: tunnel_types_api.c:34
IPIP_TRANSPORT_IP4
@ IPIP_TRANSPORT_IP4
Definition: ipip.h:49
ip46_address_is_equal
static u8 ip46_address_is_equal(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:93
vl_api_ipip_6rd_del_tunnel_t_handler
static void vl_api_ipip_6rd_del_tunnel_t_handler(vl_api_ipip_6rd_del_tunnel_t *mp)
Definition: ipip_api.c:198
fib_table_t_::ft_table_id
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
ipip_main_t
Definition: ipip.h:104
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
ip_dscp_decode
ip_dscp_t ip_dscp_decode(vl_api_ip_dscp_t in)
Definition: ip_types_api.c:99
vl_api_ipip_tunnel_dump_t_handler
static void vl_api_ipip_tunnel_dump_t_handler(vl_api_ipip_tunnel_dump_t *mp)
Definition: ipip_api.c:135
ipip_tunnel_t::tunnel_dst
ip46_address_t tunnel_dst
Definition: ipip.h:83
interface.h
src
vl_api_address_t src
Definition: gre.api:54
ip4_address_t
Definition: ip4_packet.h:50
ipip_del_tunnel
int ipip_del_tunnel(u32 sw_if_index)
Definition: ipip.c:832
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
vl_api_ipip_6rd_del_tunnel_t
Delete an IPv4 over IPv6 automatic tunnel (6RD)
Definition: ipip.api:129
REPLY_MACRO_DETAILS2
#define REPLY_MACRO_DETAILS2(t, body)
Definition: api_helper_macros.h:117
true
#define true
Definition: cJSON.c:65
vl_api_ipip_6rd_add_tunnel_t
Create an IPv4 over IPv6 automatic tunnel (6RD)
Definition: ipip.api:105
vl_api_ipip_del_tunnel_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: ipip.api:99
ipip_tunnel_t::tunnel_src
ip46_address_t tunnel_src
Definition: ipip.h:82
vl_api_ipip_6rd_add_tunnel_t::ip4_prefix
vl_api_ip4_prefix_t ip4_prefix
Definition: ipip.api:112
format_fns.h
fib_table_get
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:29
u32
unsigned int u32
Definition: types.h:88
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
sixrd_add_tunnel
int sixrd_add_tunnel(ip6_address_t *ip6_prefix, u8 ip6_prefix_len, ip4_address_t *ip4_prefix, u8 ip4_prefix_len, ip4_address_t *ip4_src, bool security_check, u32 ip4_fib_index, u32 ip6_fib_index, u32 *sw_if_index)
Definition: sixrd.c:271
vl_api_ipip_tunnel_dump_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: ipip.api:143
vl_api_ipip_add_tunnel_t
Create an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:78
api_helper_macros.h
IPIP_TRANSPORT_IP6
@ IPIP_TRANSPORT_IP6
Definition: ipip.h:50
tunnel_mode_t
enum tunnel_mode_t_ tunnel_mode_t
vlib_main_t
Definition: main.h:102
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(ipip_api_hookup)
vl_api_ipip_add_tunnel_t::tunnel
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:82
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:220
clib_error_t
Definition: clib_error.h:21
ipip_main
ipip_main_t ipip_main
Definition: ipip.c:31
ip46_type_t
ip46_type_t
Definition: ip46_address.h:22
tunnel_encap_decap_flags_encode
vl_api_tunnel_encap_decap_flags_t tunnel_encap_decap_flags_encode(tunnel_encap_decap_flags_t f)
Definition: tunnel_types_api.c:46
ipip.h
vl_api_ipip_add_tunnel_reply_t
Definition: ipip.api:85
ipip_tunnel_t::user_instance
u32 user_instance
Definition: ipip.h:88
tunnel_mode_decode
int tunnel_mode_decode(vl_api_tunnel_mode_t in, tunnel_mode_t *out)
Definition: tunnel_types_api.c:69
rv
int __clib_unused rv
Definition: application.c:491
IP46_TYPE_ANY
@ IP46_TYPE_ANY
Definition: ip46_address.h:24
vl_api_ipip_tunnel_details_t::tunnel
vl_api_ipip_tunnel_t tunnel
Definition: ipip.api:149
ipip_tunnel_t::flags
tunnel_encap_decap_flags_t flags
Definition: ipip.h:89
ipip_api_hookup
static clib_error_t * ipip_api_hookup(vlib_main_t *vm)
Definition: ipip_api.c:220
vnet.h
api_errno.h
vl_api_ipip_6rd_add_tunnel_t::ip6_prefix
vl_api_ip6_prefix_t ip6_prefix
Definition: ipip.api:111
vl_api_ipip_del_tunnel_t
Delete an IP{v4,v6} over IP{v4,v6} tunnel.
Definition: ipip.api:95
vl_api_ipip_6rd_add_tunnel_t::ip6_table_id
u32 ip6_table_id
Definition: ipip.api:109
fib_table_find
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1106
vl_api_ipip_6rd_add_tunnel_reply_t
Definition: ipip.api:119
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ip_types_api.h
ipip_add_tunnel
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, tunnel_encap_decap_flags_t flags, ip_dscp_t dscp, tunnel_mode_t tmode, u32 *sw_if_indexp)
Definition: ipip.c:722
ipip_tunnel_t::dscp
ip_dscp_t dscp
Definition: ipip.h:90
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105