summaryrefslogtreecommitdiffstats
path: root/src/plugins/ikev2/ikev2_api.c
blob: 00d71143a5d2967ae0d358b55a8062526fcede1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.
 */
#ifndef __included_lacp_node_h__
#define __included_lacp_node_h__

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vppinfra/format.h>
#include <vppinfra/hash.h>
#include <lacp/protocol.h>
#include <lacp/rx_machine.h>
#include <lacp/tx_machine.h>
#include <lacp/mux_machine.h>
#include <lacp/ptx_machine.h>

typedef enum
{
  LACP_PACKET_TEMPLATE_ETHERNET,
  LACP_N_PACKET_TEMPLATES,
} lacp_packet_template_id_t;

typedef enum
{
  MARKER_PACKET_TEMPLATE_ETHERNET,
  MARKER_N_PACKET_TEMPLATES,
} marker_packet_template_id_t;

enum
{
  LACP_PROCESS_EVENT_START = 1,
  LACP_PROCESS_EVENT_STOP = 2,
} lacp_process_event_t;

#define LACP_DBG(n, args...)			\
  {						\
    lacp_main_t *_lm = &lacp_main;              \
    if (_lm->debug || n->debug)			\
      clib_warning (args);			\
  }

#define LACP_DBG2(n, e, s, m, t)   		  \
  {						  \
    lacp_main_t *_lm = &lacp_main;                \
    if ((m)->debug && (_lm->debug || (n)->debug)) \
      (*m->debug)(n, e, s, t);			  \
  }

/* Packet counters */
#define foreach_lacp_error                                               \
_ (NONE, "good lacp packets -- consumed")	                         \
_ (CACHE_HIT, "good lacp packets -- cache hit")                          \
_ (UNSUPPORTED, "unsupported slow protocol packets")                     \
_ (TOO_SMALL, "bad lacp packets -- packet too small")                    \
_ (BAD_TLV, "bad lacp packets -- bad TLV length")                        \
_ (DISABLED, "lacp packets received on disabled interfaces")

typedef enum
{
#define _(sym,str) LACP_ERROR_##sym,
  foreach_lacp_error
#undef _
    LACP_N_ERROR,
} lacp_error_t;

/* lacp packet trace capture */
typedef struct
{
  u32 sw_if_index;
  u32 len;
  union
  {
    marker_pdu_t marker;
    lacp_pdu_t lacpdu;
  } pkt;
} lacp_input_trace_t;

/** LACP interface details struct */
typedef struct
{
  u32 sw_if_index;
  u8 interface_name[64];
  u32 rx_state;
  u32 tx_state;
  u32 mux_state;
  u32 ptx_state;
  u8 bond_interface_name[64];
  u16 actor_system_priority;
  u8 actor_system[6];
  u16 actor_key;
  u16 actor_port_priority;
  u16 actor_port_number;
  u8 actor_state;
  u16 partner_system_priority;
  u8 partner_system[6];
  u16 partner_key;
  u16 partner_port_priority;
  u16 partner_port_number;
  u8 partner_state;
} lacp_interface_details_t;

typedef struct
{
  /** API message ID base */
  u16 msg_id_base;

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;

  /* Background process node index */
  u32 lacp_process_node_index;

  /* Packet templates for different encap types */
  vlib_packet_template_t packet_templates[LACP_N_PACKET_TEMPLATES];

  /* Packet templates for different encap types */
  vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES];

  /* LACP interface count */
  u32 lacp_int;

  /* debug is on or off */
  u8 debug;
} lacp_main_t;

extern lacp_state_struct lacp_state_array[];
extern lacp_main_t lacp_main;

clib_error_t *lacp_plugin_api_hookup (vlib_main_t * vm);
int lacp_dump_ifs (lacp_interface_details_t ** out_bondids);
lacp_error_t lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0);
void lacp_periodic (vlib_main_t * vm);
u8 *lacp_input_format_trace (u8 * s, va_list * args);
void lacp_init_neighbor (slave_if_t * sif, u8 * hw_address,
			 u16 port_number, u32 group);
void lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_rx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_tx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_ptx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_mux_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_selection_logic (vlib_main_t * vm, slave_if_t * sif);
void lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif);

static inline void
lacp_stop_timer (f64 * timer)
{
  *timer = 0.0;
}

static inline u8
lacp_timer_is_running (f64 timer)
{
  return (timer != 0.0);
}

static inline u8
lacp_timer_is_expired (vlib_main_t * vm, f64 timer)
{
  f64 now = vlib_time_now (vm);

  return (now >= 
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
/*
 *------------------------------------------------------------------
 * ipsec_api.c - ipsec api
 *
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * 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.
 *------------------------------------------------------------------
 */

#include <vnet/vnet.h>
#include <vlibmemory/api.h>
#include <vnet/api_errno.h>
#include <vpp/app/version.h>
#include <vnet/ip/ip_types_api.h>
#include <ikev2/ikev2.h>
#include <ikev2/ikev2_priv.h>

/* define message IDs */
#include <vnet/format_fns.h>
#include <plugins/ikev2/ikev2.api_enum.h>
#include <plugins/ikev2/ikev2.api_types.h>


#define vl_endianfun		/* define message structures */
#include <plugins/ikev2/ikev2.api.h>
#include <plugins/ikev2/ikev2_types.api.h>
#undef vl_endianfun

extern ikev2_main_t ikev2_main;

#define IKEV2_PLUGIN_VERSION_MAJOR 1
#define IKEV2_PLUGIN_VERSION_MINOR 0
#define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
#include <vlibapi/api_helper_macros.h>

static u32
ikev2_encode_sa_index (u32 sai, u32 ti)
{
  return (ti << 16) | sai;
}

static void
ikev2_decode_sa_index (u32 api_sai, u32 * sai, u32 * ti)
{
  *sai = api_sai & 0xffff;
  *ti = api_sai >> 16;
}

static void
cp_ike_transforms (vl_api_ikev2_ike_transforms_t * vl_api_ts,
		   ikev2_transforms_set * ts)
{
  vl_api_ts->crypto_alg = ts->crypto_alg;
  vl_api_ts->integ_alg = ts->integ_alg;
  vl_api_ts->dh_group = ts->dh_type;
  vl_api_ts->crypto_key_size = ts->crypto_key_size;
}

static void
cp_esp_transforms (vl_api_ikev2_esp_transforms_t * vl_api_ts,
		   ikev2_transforms_set * ts)
{
  vl_api_ts->crypto_alg = ts->crypto_alg;
  vl_api_ts->integ_alg = ts->integ_alg;
  vl_api_ts->crypto_key_size = ts->crypto_key_size;
}

static void
cp_id (vl_api_ikev2_id_t * vl_api_id, ikev2_id_t * id)
{
  if (!id->data)
    return;

  int size_data = 0;
  vl_api_id->type = id->type;
  size_data = sizeof (vl_api_id->data) - 1;	// size without zero ending character
  if (vec_len (id->data) < size_data)
    size_data = vec_len (id->data);

  vl_api_id->data_len = size_data;
  clib_memcpy (vl_api_id->data, id->data, size_data);
}

static void
cp_ts (vl_api_ikev2_ts_t * vl_api_ts, ikev2_ts_t * ts, u8 is_local)
{
  vl_api_ts->is_local = is_local;
  vl_api_ts->protocol_id = ts->protocol_id;
  vl_api_ts->start_port = ts->start_port;
  vl_api_ts->end_port = ts->end_port;
  ip_address_encode2 (&ts->start_addr, &vl_api_ts->start_addr);
  ip_address_encode2 (&ts->end_addr, &vl_api_ts->end_addr);
}

static void
cp_auth (vl_api_ikev2_auth_t * vl_api_auth, ikev2_auth_t * auth)
{
  vl_api_auth->method = auth->method;
  vl_api_auth->data_len = vec_len (auth->data);
  vl_api_auth->hex = auth->hex;
  clib_memcpy (&vl_api_auth->data, auth->data, vec_len (auth->data));
}

static void
cp_responder (vl_api_ikev2_responder_t * vl_api_responder,
	      ikev2_responder_t * responder)
{
  vl_api_responder->sw_if_index = responder->sw_if_index;
  ip_address_encode2 (&responder->addr, &vl_api_responder->addr);
}

void
cp_sa_transform (vl_api_ikev2_sa_transform_t * vl_tr,
		 ikev2_sa_transform_t * tr)
{
  vl_tr->transform_type = tr->type;
  vl_tr->key_len = tr->key_len;
  vl_tr->key_trunc = tr->key_trunc;
  vl_tr->block_size = tr->block_size;
  vl_tr->dh_group = tr->dh_group;
  vl_tr->transform_id = tr->encr_type;
}

static void
send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg,
	      u32 context)
{
  vl_api_ikev2_profile_details_t *rmp = 0;

  rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (profile->auth.data));
  clib_memset (rmp, 0, sizeof (*rmp) + vec_len (profile->auth.data));
  ikev2_main_t *im = &ikev2_main;
  rmp->_vl_msg_id = ntohs (VL_API_IKEV2_PROFILE_DETAILS + im->msg_id_base);
  rmp->context = context;

  int size_data = sizeof (rmp->profile.name) - 1;
  if (vec_len (profile->name) < size_data)
    size_data = vec_len (profile->name);
  clib_memcpy (rmp->profile.name, profile->name, size_data);

  cp_ike_transforms (&rmp->profile.ike_ts, &profile->ike_ts);
  cp_esp_transforms (&rmp->profile.esp_ts, &profile->esp_ts);

  cp_id (&rmp->profile.loc_id, &profile->loc_id);
  cp_id (&rmp->profile.rem_id, &profile->rem_id);

  cp_ts (&rmp->profile.rem_ts, &profile->rem_ts, 0 /* is_local */ );
  cp_ts (&rmp->profile.loc_ts, &profile->loc_ts, 1 /* is_local */ );

  cp_auth (&rmp->profile.auth, &profile->auth);

  cp_responder (&rmp->profile.responder, &profile->responder);

  rmp->profile.udp_encap = profile->udp_encap;
  rmp->profile.tun_itf = profile->tun_itf;
  rmp->profile.natt_disabled = profile->natt_disabled;
  rmp->profile.ipsec_over_udp_port = profile->ipsec_over_udp_port;

  rmp->profile.lifetime = profile->lifetime;
  rmp->profile.lifetime_maxdata = profile->lifetime_maxdata;
  rmp->profile.lifetime_jitter = profile->lifetime_jitter;
  rmp->profile.handover = profile->handover;

  vl_api_ikev2_profile_t_endian (&rmp->profile);

  vl_api_send_msg (reg, (u8 *) rmp);
}

static void
vl_api_ikev2_profile_dump_t_handler (vl_api_ikev2_profile_dump_t * mp)
{
  ikev2_main_t *im = &ikev2_main;
  ikev2_profile_t *profile;
  vl_api_registration_t *reg;
  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  /* *INDENT-OFF* */
  pool_foreach (profile, im->profiles)
   {
    send_profile (profile, reg, mp->context);
  }
  /* *INDENT-ON* */
}

static void
send_sa (ikev2_sa_t * sa, vl_api_ikev2_sa_dump_t * mp, u32 api_sa_index)
{
  vl_api_ikev2_sa_details_t *rmp = 0;
  int rv = 0;
  ikev2_sa_transform_t *tr;

  /* *INDENT-OFF* */
  REPLY_MACRO2_ZERO (VL_API_IKEV2_SA_DETAILS,
  {
    vl_api_ikev2_sa_t *rsa = &rmp->sa;
    vl_api_ikev2_keys_t* k = &rsa->keys;
    rsa->profile_index = rsa->profile_index;
    rsa->sa_index = api_sa_index;
    ip_address_encode2 (&sa->iaddr, &rsa->iaddr);
    ip_address_encode2 (&sa->raddr, &rsa->raddr);
    rsa->ispi = sa->ispi;
    rsa->rspi = sa->rspi;
    cp_id(&rsa->i_id, &sa->i_id);
    cp_id(&rsa->r_id, &sa->r_id);

    tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
    if (tr)
      cp_sa_transform (&rsa->encryption, tr);

    tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
    if (tr)
      cp_sa_transform (&rsa->prf, tr);

    tr = ikev2_sa_get_td_for_type (sa->r_proposals,
                                   IKEV2_TRANSFORM_TYPE_INTEG);
    if (tr)
      cp_sa_transform (&rsa->integrity, tr);

    tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
    if (tr)
      cp_sa_transform (&rsa->dh, tr);

    k->sk_d_len = vec_len (sa->sk_d);
    clib_memcpy (&k->sk_d, sa->sk_d, k->sk_d_len);

    k->sk_ai_len = vec_len (sa->sk_ai);
    clib_memcpy (&k->sk_ai, sa->sk_ai, k->sk_ai_len);

    k->sk_ar_len = vec_len (sa->sk_ar);
    clib_memcpy (&k->sk_ar, sa->sk_ar, k->sk_ar_len);

    k->sk_ei_len = vec_len (sa->sk_ei);
    clib_memcpy (&k->sk_ei, sa->sk_ei, k->sk_ei_len);

    k->sk_er_len = vec_len (sa->sk_er);
    clib_memcpy (&k->sk_er, sa->sk_er, k->sk_er_len);

    k->sk_pi_len = vec_len (sa->sk_pi);
    clib_memcpy (&k->sk_pi, sa->sk_pi, k->sk_pi_len);

    k->sk_pr_len = vec_len (sa->sk_pr);
    clib_memcpy (&k->sk_pr, sa->sk_pr, k->sk_pr_len);

    vl_api_ikev2_sa_t_endian(rsa);
  });
  /* *INDENT-ON* */
}

static void
vl_api_ikev2_sa_dump_t_handler (vl_api_ikev2_sa_dump_t * mp)
{
  ikev2_main_t *km = &ikev2_main;
  ikev2_main_per_thread_data_t *tkm;
  ikev2_sa_t *sa;

  vec_foreach (tkm, km->per_thread_data)
  {
    /* *INDENT-OFF* */
    pool_foreach (sa, tkm->sas)
     {
      u32 api_sa_index = ikev2_encode_sa_index (sa - tkm->sas,
                                              tkm - km->per_thread_data);
      send_sa (sa, mp, api_sa_index);
    }
    /* *INDENT-ON* */
  }
}


static void
send_child_sa (ikev2_child_sa_t * child,
	       vl_api_ikev2_child_sa_dump_t * mp, u32 child_sa_index,
	       u32 sa_index)
{
  vl_api_ikev2_child_sa_details_t *rmp = 0;
  int rv = 0;
  ikev2_sa_transform_t *tr;

  /* *INDENT-OFF* */
  REPLY_MACRO2_ZERO (VL_API_IKEV2_CHILD_SA_DETAILS,
  {
    vl_api_ikev2_keys_t *k = &rmp->child_sa.keys;
    rmp->child_sa.child_sa_index = child_sa_index;
    rmp->child_sa.sa_index = sa_index;
    rmp->child_sa.i_spi =
      child->i_proposals ? child->i_proposals[0].spi : 0;
    rmp->child_sa.r_spi =
      child->r_proposals ? child->r_proposals[0].spi : 0;

    tr = ikev2_sa_get_td_for_type (child->r_proposals,
                                   IKEV2_TRANSFORM_TYPE_ENCR);
    if (tr)
      cp_sa_transform (&rmp->child_sa.encryption, tr);

    tr = ikev2_sa_get_td_for_type (child->r_proposals,
                                   IKEV2_TRANSFORM_TYPE_INTEG);
    if (tr)
      cp_sa_transform (&rmp->child_sa.integrity, tr);

    tr = ikev2_sa_get_td_for_type (child->r_proposals,
                                   IKEV2_TRANSFORM_TYPE_ESN);
    if (tr)
      cp_sa_transform (&rmp->child_sa.esn, tr);

    k->sk_ei_len = vec_len (child->sk_ei);
    clib_memcpy (&k->sk_ei, child->sk_ei, k->sk_ei_len);

    k->sk_er_len = vec_len (child->sk_er);
    clib_memcpy (&k->sk_er, child->sk_er, k->sk_er_len);

    if (vec_len (child->sk_ai))
      {
        k->sk_ai_len = vec_len (child->sk_ai);
        clib_memcpy (&k->sk_ai, child->sk_ai,
		     k->sk_ai_len);

        k->sk_ar_len = vec_len (child->sk_ar);
        clib_memcpy (&k->sk_ar, child->sk_ar,
		     k->sk_ar_len);
      }

    vl_api_ikev2_child_sa_t_endian (&rmp->child_sa);
  });
  /* *INDENT-ON* */
}

static void
vl_api_ikev2_child_sa_dump_t_handler (vl_api_ikev2_child_sa_dump_t * mp)
{
  ikev2_main_t *im = &ikev2_main;
  ikev2_main_per_thread_data_t *tkm;
  ikev2_sa_t *sa;
  ikev2_child_sa_t *child;
  u32 sai = ~0, ti = ~0;

  ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);

  if (vec_len (im->per_thread_data) <= ti)
    return;

  tkm = vec_elt_at_index (im->per_thread_data, ti);

  if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
    return;

  sa = pool_elt_at_index (tkm->sas, sai);

  vec_foreach (child, sa->childs)
  {
    u32 child_sa_index = child - sa->childs;
    send_child_sa (child, mp, child_sa_index, sai);
  }
}

static void
  vl_api_ikev2_traffic_selector_dump_t_handler
  (vl_api_ikev2_traffic_selector_dump_t * mp)
{
  ikev2_main_t *im = &ikev2_main;
  ikev2_main_per_thread_data_t *tkm;
  ikev2_sa_t *sa;
  ikev2_child_sa_t *child;
  ikev2_ts_t *ts;
  u32 sai = ~0, ti = ~0;

  u32 api_sa_index = clib_net_to_host_u32 (mp->sa_index);
  u32 child_sa_index = clib_net_to_host_u32 (mp->child_sa_index);
  ikev2_decode_sa_index (api_sa_index, &sai, &ti);

  if (vec_len (im->per_thread_data) <= ti)
    return;

  tkm = vec_elt_at_index (im->per_thread_data, ti);

  if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
    return;

  sa = pool_elt_at_index (tkm->sas, sai);

  if (vec_len (sa->childs) <= child_sa_index)
    return;

  child = vec_elt_at_index (sa->childs, child_sa_index);

  vec_foreach (ts, mp->is_initiator ? child->tsi : child->tsr)
  {
    vl_api_ikev2_traffic_selector_details_t *rmp = 0;
    int rv = 0;

    /* *INDENT-OFF* */
    REPLY_MACRO2_ZERO (VL_API_IKEV2_TRAFFIC_SELECTOR_DETAILS,
    {
      rmp->ts.sa_index = api_sa_index;
      rmp->ts.child_sa_index = child_sa_index;
      cp_ts (&rmp->ts, ts, mp->is_initiator);
      vl_api_ikev2_ts_t_endian (&rmp->ts);
    });
    /* *INDENT-ON* */
  }
}

static void
vl_api_ikev2_nonce_get_t_handler (vl_api_ikev2_nonce_get_t * mp)
{
  ikev2_main_t *im = &ikev2_main;
  ikev2_main_per_thread_data_t *tkm;
  ikev2_sa_t *sa;
  u32 sai = ~0, ti = ~0;

  ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);

  if (vec_len (im->per_thread_data) <= ti)
    return;

  tkm = vec_elt_at_index (im->per_thread_data, ti);

  if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
    return;

  sa = pool_elt_at_index (tkm->sas, sai);

  u8 *nonce = mp->is_initiator ? sa->i_nonce : sa->r_nonce;
  vl_api_ikev2_nonce_get_reply_t *rmp = 0;
  int data_len = vec_len (nonce);
  int rv = 0;

  /* *INDENT-OFF* */
  REPLY_MACRO3_ZERO (VL_API_IKEV2_NONCE_GET_REPLY, data_len,
  {
    rmp->data_len = clib_host_to_net_u32 (data_len);
    clib_memcpy (rmp->nonce, nonce, data_len);
  });
  /* *INDENT-ON* */
}

static void
vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
					   mp)
{
  ikev2_main_t *im = &ikev2_main;
  vl_api_ikev2_plugin_get_version_reply_t *rmp;
  int msg_size = sizeof (*rmp);
  vl_api_registration_t *reg;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  rmp = vl_msg_api_alloc (msg_size);
  clib_memset (rmp, 0, msg_size);
  rmp->_vl_msg_id =
    ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
  rmp->context = mp->context;
  rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
  rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);

  vl_api_send_msg (reg, (u8 *) rmp);
}

static void
  vl_api_ikev2_profile_set_liveness_t_handler
  (vl_api_ikev2_profile_set_liveness_t * mp)
{
  vl_api_ikev2_profile_set_liveness_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  clib_error_t *error;
  error = ikev2_set_liveness_params (clib_net_to_host_u32 (mp->period),
				     clib_net_to_host_u32 (mp->max_retries));
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
}

static void
vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
{
  vl_api_ikev2_profile_add_del_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  u8 *tmp = format (0, "%s", mp->name);
  error = ikev2_add_del_profile (vm, tmp, mp->is_add);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
}

static void
  vl_api_ikev2_profile_set_auth_t_handler
  (vl_api_ikev2_profile_set_auth_t * mp)
{
  vl_api_ikev2_profile_set_auth_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  int data_len = ntohl (mp->data_len);
  u8 *tmp = format (0, "%s", mp->name);
  u8 *data = vec_new (u8, data_len);
  clib_memcpy (data, mp->data, data_len);
  error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
  vec_free (tmp);
  vec_free (data);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
}

static void
vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
{
  vl_api_ikev2_profile_set_id_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  u8 *tmp = format (0, "%s", mp->name);
  int data_len = ntohl (mp->data_len);
  u8 *data = vec_new (u8, data_len);
  clib_memcpy (data, mp->data, data_len);
  error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
  vec_free (tmp);
  vec_free (data);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
}

static void
  vl_api_ikev2_profile_set_udp_encap_t_handler
  (vl_api_ikev2_profile_set_udp_encap_t * mp)
{
  vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  u8 *tmp = format (0, "%s", mp->name);
  error = ikev2_set_profile_udp_encap (vm, tmp);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP_REPLY);
}

static void
vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
{
  vl_api_ikev2_profile_set_ts_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  u8 *tmp = format (0, "%s", mp->name);
  ip_address_t start_addr, end_addr;
  ip_address_decode2 (&mp->ts.start_addr, &start_addr);
  ip_address_decode2 (&mp->ts.end_addr, &end_addr);
  error =
    ikev2_set_profile_ts (vm, tmp, mp->ts.protocol_id,
			  clib_net_to_host_u16 (mp->ts.start_port),
			  clib_net_to_host_u16 (mp->ts.end_port),
			  start_addr, end_addr, mp->ts.is_local);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
}

static void
vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
{
  vl_api_ikev2_set_local_key_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  error = ikev2_set_local_key (vm, mp->key_file);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
}

static void
vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
{
  vl_api_ikev2_set_responder_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);
  ip_address_t ip;
  ip_address_decode2 (&mp->responder.addr, &ip);
  u32 sw_if_index = clib_net_to_host_u32 (mp->responder.sw_if_index);

  error = ikev2_set_profile_responder (vm, tmp, sw_if_index, ip);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
}

static void
vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
					   mp)
{
  vl_api_ikev2_set_ike_transforms_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);

  error =
    ikev2_set_profile_ike_transforms (vm, tmp, mp->tr.crypto_alg,
				      mp->tr.integ_alg,
				      mp->tr.dh_group,
				      ntohl (mp->tr.crypto_key_size));
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
}

static void
vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
					   mp)
{
  vl_api_ikev2_set_esp_transforms_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);

  error =
    ikev2_set_profile_esp_transforms (vm, tmp, mp->tr.crypto_alg,
				      mp->tr.integ_alg,
				      ntohl (mp->tr.crypto_key_size));
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
}

static void
vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
{
  vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);

  error =
    ikev2_set_profile_sa_lifetime (vm, tmp,
				   clib_net_to_host_u64 (mp->lifetime),
				   ntohl (mp->lifetime_jitter),
				   ntohl (mp->handover),
				   clib_net_to_host_u64
				   (mp->lifetime_maxdata));
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
}

static void
  vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
  (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
{
  vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();

  u8 *tmp = format (0, "%s", mp->name);

  rv =
    ikev2_set_profile_ipsec_udp_port (vm, tmp,
				      clib_net_to_host_u16 (mp->port),
				      mp->is_set);
  vec_free (tmp);
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
}

static void
  vl_api_ikev2_set_tunnel_interface_t_handler
  (vl_api_ikev2_set_tunnel_interface_t * mp)
{
  vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
  int rv = 0;

  VALIDATE_SW_IF_INDEX (mp);

#if WITH_LIBSSL > 0
  u8 *tmp = format (0, "%s", mp->name);
  clib_error_t *error;

  error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
					      ntohl (mp->sw_if_index));

  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
  vec_free (tmp);
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
}

static void
vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
{
  vl_api_ikev2_initiate_sa_init_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);

  error = ikev2_initiate_sa_init (vm, tmp);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
}

static void
vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
					    * mp)
{
  vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
}

static void
  vl_api_ikev2_initiate_del_child_sa_t_handler
  (vl_api_ikev2_initiate_del_child_sa_t * mp)
{
  vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
}

static void
  vl_api_ikev2_profile_disable_natt_t_handler
  (vl_api_ikev2_profile_disable_natt_t * mp)
{
  vl_api_ikev2_profile_disable_natt_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  clib_error_t *error;

  u8 *tmp = format (0, "%s", mp->name);
  error = ikev2_profile_natt_disable (tmp);
  vec_free (tmp);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_PROFILE_DISABLE_NATT_REPLY);
}

static void
  vl_api_ikev2_initiate_rekey_child_sa_t_handler
  (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
{
  vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
  int rv = 0;

#if WITH_LIBSSL > 0
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;

  error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
  if (error)
    {
      ikev2_log_error ("%U", format_clib_error, error);
      clib_error_free (error);
      rv = VNET_API_ERROR_UNSPECIFIED;
    }
#else
  rv = VNET_API_ERROR_UNIMPLEMENTED;
#endif

  REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
}

#include <ikev2/ikev2.api.c>
static clib_error_t *
ikev2_api_init (vlib_main_t * vm)
{
  ikev2_main_t *im = &ikev2_main;

  /* Ask for a correctly-sized block of API message decode slots */
  im->msg_id_base = setup_message_id_table ();

  return 0;
}

VLIB_INIT_FUNCTION (ikev2_api_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */