1/*
2 *------------------------------------------------------------------
3 * gtpu_api.c - gtpu api
4 *
5 * Copyright (c) 2017 Intel and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/interface.h>
21#include <vnet/api_errno.h>
22#include <vnet/feature/feature.h>
23#include <vnet/fib/fib_table.h>
24
25#include <vppinfra/byte_order.h>
26#include <vlibmemory/api.h>
27#include <vnet/ip/ip_types_api.h>
28#include <gtpu/gtpu.h>
29
30#include <vnet/format_fns.h>
31#include <gtpu/gtpu.api_enum.h>
32#include <gtpu/gtpu.api_types.h>
33
34#define REPLY_MSG_ID_BASE gtm->msg_id_base
35#include <vlibapi/api_helper_macros.h>
36
37static void
38vl_api_gtpu_offload_rx_t_handler (vl_api_gtpu_offload_rx_t * mp)
39{
40  vl_api_gtpu_offload_rx_reply_t *rmp;
41  int rv = 0;
42  vl_api_interface_index_t hw_if_index = ntohl (mp->hw_if_index);
43  vl_api_interface_index_t sw_if_index = ntohl (mp->sw_if_index);
44
45  if (!vnet_hw_interface_is_valid (vnet_get_main (), hw_if_index))
46    {
47      rv = VNET_API_ERROR_NO_SUCH_ENTRY;
48      goto err;
49    }
50  VALIDATE_SW_IF_INDEX (mp);
51
52  u32 t_index = vnet_gtpu_get_tunnel_index (sw_if_index);
53  if (t_index == ~0)
54    {
55      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
56      goto err;
57    }
58
59  gtpu_main_t *gtm = &gtpu_main;
60  gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
61  if (!ip46_address_is_ip4 (&t->dst))
62    {
63      rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
64      goto err;
65    }
66
67  if (t->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT)
68    {
69      rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
70      goto err;
71    }
72
73  vnet_main_t *vnm = vnet_get_main ();
74  vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
75  ip4_main_t *im = &ip4_main;
76  u32 rx_fib_index =
77    vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
78
79  if (t->encap_fib_index != rx_fib_index)
80    {
81      rv = VNET_API_ERROR_NO_SUCH_FIB;
82      goto err;
83    }
84
85  if (vnet_gtpu_add_del_rx_flow (hw_if_index, t_index, mp->enable))
86    {
87      rv = VNET_API_ERROR_UNSPECIFIED;
88      goto err;
89    }
90  BAD_SW_IF_INDEX_LABEL;
91err:
92
93  REPLY_MACRO (VL_API_GTPU_OFFLOAD_RX_REPLY);
94}
95
96static void
97  vl_api_sw_interface_set_gtpu_bypass_t_handler
98  (vl_api_sw_interface_set_gtpu_bypass_t * mp)
99{
100  vl_api_sw_interface_set_gtpu_bypass_reply_t *rmp;
101  int rv = 0;
102  u32 sw_if_index = ntohl (mp->sw_if_index);
103  gtpu_main_t *gtm = &gtpu_main;
104
105  VALIDATE_SW_IF_INDEX (mp);
106
107  vnet_int_gtpu_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
108  BAD_SW_IF_INDEX_LABEL;
109
110  REPLY_MACRO (VL_API_SW_INTERFACE_SET_GTPU_BYPASS_REPLY);
111}
112
113static void vl_api_gtpu_add_del_tunnel_t_handler
114  (vl_api_gtpu_add_del_tunnel_t * mp)
115{
116  vl_api_gtpu_add_del_tunnel_reply_t *rmp;
117  int rv = 0;
118  ip4_main_t *im = &ip4_main;
119  gtpu_main_t *gtm = &gtpu_main;
120
121  uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
122  if (!p)
123    {
124      rv = VNET_API_ERROR_NO_SUCH_FIB;
125      goto out;
126    }
127
128  vnet_gtpu_add_del_tunnel_args_t a = {
129    .is_add = mp->is_add,
130    .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
131    .encap_fib_index = p[0],
132    .decap_next_index = ntohl (mp->decap_next_index),
133    .teid = ntohl (mp->teid),
134  };
135  ip_address_decode (&mp->dst_address, &a.dst);
136  ip_address_decode (&mp->src_address, &a.src);
137
138  /* Check src & dst are different */
139  if (ip46_address_cmp (&a.dst, &a.src) == 0)
140    {
141      rv = VNET_API_ERROR_SAME_SRC_DST;
142      goto out;
143    }
144  if (ip46_address_is_multicast (&a.dst) &&
145      !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
146    {
147      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
148      goto out;
149    }
150
151  u32 sw_if_index = ~0;
152  rv = vnet_gtpu_add_del_tunnel (&a, &sw_if_index);
153
154out:
155  /* *INDENT-OFF* */
156  REPLY_MACRO2(VL_API_GTPU_ADD_DEL_TUNNEL_REPLY,
157  ({
158    rmp->sw_if_index = ntohl (sw_if_index);
159  }));
160  /* *INDENT-ON* */
161}
162
163static void send_gtpu_tunnel_details
164  (gtpu_tunnel_t * t, vl_api_registration_t * reg, u32 context)
165{
166  vl_api_gtpu_tunnel_details_t *rmp;
167  gtpu_main_t *gtm = &gtpu_main;
168  ip4_main_t *im4 = &ip4_main;
169  ip6_main_t *im6 = &ip6_main;
170  u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
171
172  rmp = vl_msg_api_alloc (sizeof (*rmp));
173  clib_memset (rmp, 0, sizeof (*rmp));
174  rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_DETAILS + gtm->msg_id_base);
175
176  ip_address_encode (&t->src, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
177		     &rmp->src_address);
178  ip_address_encode (&t->dst, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
179		     &rmp->dst_address);
180
181  rmp->encap_vrf_id =
182    is_ipv6 ? htonl (im6->fibs[t->encap_fib_index].ft_table_id) :
183    htonl (im4->fibs[t->encap_fib_index].ft_table_id);
184  rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
185  rmp->teid = htonl (t->teid);
186  rmp->decap_next_index = htonl (t->decap_next_index);
187  rmp->sw_if_index = htonl (t->sw_if_index);
188  rmp->context = context;
189
190  vl_api_send_msg (reg, (u8 *) rmp);
191}
192
193static void
194vl_api_gtpu_tunnel_dump_t_handler (vl_api_gtpu_tunnel_dump_t * mp)
195{
196  vl_api_registration_t *reg;
197  gtpu_main_t *gtm = &gtpu_main;
198  gtpu_tunnel_t *t;
199  u32 sw_if_index;
200
201  reg = vl_api_client_index_to_registration (mp->client_index);
202  if (!reg)
203    return;
204
205  sw_if_index = ntohl (mp->sw_if_index);
206
207  if (~0 == sw_if_index)
208    {
209      /* *INDENT-OFF* */
210      pool_foreach (t, gtm->tunnels,
211      ({
212        send_gtpu_tunnel_details(t, reg, mp->context);
213      }));
214      /* *INDENT-ON* */
215    }
216  else
217    {
218      if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
219	  (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
220	{
221	  return;
222	}
223      t = &gtm->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
224      send_gtpu_tunnel_details (t, reg, mp->context);
225    }
226}
227
228#include <gtpu/gtpu.api.c>
229static clib_error_t *
230gtpu_api_hookup (vlib_main_t * vm)
231{
232  gtpu_main_t *gtm = &gtpu_main;
233
234  gtm->msg_id_base = setup_message_id_table ();
235  return 0;
236}
237
238VLIB_API_INIT_FUNCTION (gtpu_api_hookup);
239
240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables:
244 * eval: (c-set-style "gnu")
245 * End:
246 */
247