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
|
/*
* Copyright (c) 2020 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 <vlibmemory/api.h>
#include <nat/nat66/nat66.h>
#include <nat/nat66/nat66.api_enum.h>
#include <nat/nat66/nat66.api_types.h>
#include <vnet/fib/fib_table.h>
#define REPLY_MSG_ID_BASE nm->msg_id_base
#include <vlibapi/api_helper_macros.h>
/*************/
/*** NAT66 ***/
/*************/
static void
vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
mp)
{
nat66_main_t *nm = &nat66_main;
vl_api_nat66_add_del_interface_reply_t *rmp;
int rv = 0;
VALIDATE_SW_IF_INDEX (mp);
rv =
nat66_interface_add_del (ntohl (mp->sw_if_index),
mp->flags & NAT_IS_INSIDE, mp->is_add);
BAD_SW_IF_INDEX_LABEL;
REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
}
static void
vl_api_nat66_add_del_static_mapping_t_handler
(vl_api_nat66_add_del_static_mapping_t * mp)
{
nat66_main_t *nm = &nat66_main;
vl_api_nat66_add_del_static_mapping_reply_t *rmp;
ip6_address_t l_addr, e_addr;
int rv = 0;
memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
rv =
nat66_static_mapping_add_del (&l_addr, &e_addr,
clib_net_to_host_u32 (mp->vrf_id),
mp->is_add);
REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
}
typedef struct nat66_api_walk_ctx_t_
{
vl_api_registration_t *rp;
u32 context;
} nat66_api_walk_ctx_t;
static int
nat66_api_interface_walk (nat66_interface_t * i, void *arg)
{
vl_api_nat66_interface_details_t *rmp;
nat66_main_t *nm = &nat66_main;
nat66_api_walk_ctx_t *ctx = arg;
rmp = vl_msg_api_alloc (sizeof (*rmp));
clib_memset (rmp, 0, sizeof (*rmp));
rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + nm->msg_id_base);
rmp->sw_if_index = ntohl (i->sw_if_index);
if (nat66_interface_is_inside (i))
rmp->flags |= NAT_IS_INSIDE;
rmp->context = ctx->context;
vl_api_send_msg (ctx->rp, (u8 *) rmp);
return 0;
}
static void
vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
{
vl_api_registration_t *rp;
rp = vl_api_client_index_to_registration (mp->client_index);
if (rp == 0)
return;
nat66_api_walk_ctx_t ctx = {
.rp = rp,
.context = mp->context,
};
nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
}
static int
nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
{
vl_api_nat66_static_mapping_details_t *rmp;
nat66_main_t *nm = &nat66_main;
nat66_api_walk_ctx_t *ctx = arg;
fib_table_t *fib;
vlib_counter_t vc;
fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
if (!fib)
return -1;
vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
rmp = vl_msg_api_alloc (sizeof (*rmp));
clib_memset (rmp, 0, sizeof (*rmp));
rmp->_vl_msg_id =
ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + nm->msg_id_base);
clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
rmp->vrf_id = ntohl (fib->ft_table_id);
rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
rmp->context = ctx->context;
vl_api_send_msg (ctx->rp, (u8 *) rmp);
return 0;
}
static void
vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
* mp)
{
vl_api_registration_t *rp;
rp = vl_api_client_index_to_registration (mp->client_index);
if (rp == 0)
return;
nat66_api_walk_ctx_t ctx = {
.rp = rp,
.context = mp->context,
};
nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
}
/* API definitions */
#include <vnet/format_fns.h>
#include <nat/nat66/nat66.api.c>
/* Set up the API message handling tables */
clib_error_t *
nat66_plugin_api_hookup (vlib_main_t * vm)
{
nat66_main_t *nm = &nat66_main;
nm->msg_id_base = setup_message_id_table ();
return 0;
}
|