blob: 9a69041bc1dab49bfb64537124602372bae80cee [file] [log] [blame]
Neale Ranns2dd68522017-02-16 03:38:59 -08001/*
2 * proxy_node.c: common dhcp v4 and v6 proxy node processing
3 *
4 * Copyright (c) 2013 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 <vnet/dhcp/dhcp_proxy.h>
19#include <vnet/fib/fib_table.h>
Neale Ranns358425b2017-02-20 09:42:36 -080020#include <vnet/mfib/mfib_table.h>
Neale Ranns2dd68522017-02-16 03:38:59 -080021
22/**
23 * @brief Shard 4/6 instance of DHCP main
24 */
25dhcp_proxy_main_t dhcp_proxy_main;
26
Neale Ranns358425b2017-02-20 09:42:36 -080027static void
khemendra kumar34719e32017-12-08 18:06:52 +053028dhcp_proxy_rx_table_lock (fib_protocol_t proto, u32 fib_index)
Neale Ranns358425b2017-02-20 09:42:36 -080029{
khemendra kumar34719e32017-12-08 18:06:52 +053030 if (FIB_PROTOCOL_IP4 == proto)
31 fib_table_lock (fib_index, proto, FIB_SOURCE_DHCP);
32 else
33 mfib_table_lock (fib_index, proto, MFIB_SOURCE_DHCP);
Neale Ranns358425b2017-02-20 09:42:36 -080034}
35
36static void
khemendra kumar34719e32017-12-08 18:06:52 +053037dhcp_proxy_rx_table_unlock (fib_protocol_t proto, u32 fib_index)
Neale Ranns358425b2017-02-20 09:42:36 -080038{
khemendra kumar34719e32017-12-08 18:06:52 +053039 if (FIB_PROTOCOL_IP4 == proto)
40 fib_table_unlock (fib_index, proto, FIB_SOURCE_DHCP);
41 else
42 mfib_table_unlock (fib_index, proto, MFIB_SOURCE_DHCP);
Neale Ranns358425b2017-02-20 09:42:36 -080043}
44
khemendra kumar34719e32017-12-08 18:06:52 +053045u32
46dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto, u32 fib_index)
Neale Ranns358425b2017-02-20 09:42:36 -080047{
khemendra kumar34719e32017-12-08 18:06:52 +053048 if (FIB_PROTOCOL_IP4 == proto)
49 {
50 fib_table_t *fib;
Neale Ranns358425b2017-02-20 09:42:36 -080051
khemendra kumar34719e32017-12-08 18:06:52 +053052 fib = fib_table_get (fib_index, proto);
Neale Ranns358425b2017-02-20 09:42:36 -080053
khemendra kumar34719e32017-12-08 18:06:52 +053054 return (fib->ft_table_id);
55 }
56 else
57 {
58 mfib_table_t *mfib;
Neale Ranns358425b2017-02-20 09:42:36 -080059
khemendra kumar34719e32017-12-08 18:06:52 +053060 mfib = mfib_table_get (fib_index, proto);
Neale Ranns358425b2017-02-20 09:42:36 -080061
khemendra kumar34719e32017-12-08 18:06:52 +053062 return (mfib->mft_table_id);
Neale Ranns2dd68522017-02-16 03:38:59 -080063 }
64}
65
66void
khemendra kumar34719e32017-12-08 18:06:52 +053067dhcp_proxy_walk (fib_protocol_t proto, dhcp_proxy_walk_fn_t fn, void *ctx)
Neale Ranns2dd68522017-02-16 03:38:59 -080068{
khemendra kumar34719e32017-12-08 18:06:52 +053069 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
70 dhcp_proxy_t *server;
71 u32 server_index, i;
72
73 vec_foreach_index (i, dpm->dhcp_server_index_by_rx_fib_index[proto])
74 {
75 server_index = dpm->dhcp_server_index_by_rx_fib_index[proto][i];
76 if (~0 == server_index)
77 continue;
78
79 server = pool_elt_at_index (dpm->dhcp_servers[proto], server_index);
80
81 if (!fn (server, ctx))
82 break;
83 }
84}
85
86void
87dhcp_vss_walk (fib_protocol_t proto, dhcp_vss_walk_fn_t fn, void *ctx)
88{
89 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
Neale Ranns358425b2017-02-20 09:42:36 -080090 mfib_table_t *mfib;
khemendra kumar34719e32017-12-08 18:06:52 +053091 dhcp_vss_t *vss;
Neale Ranns2dd68522017-02-16 03:38:59 -080092 u32 vss_index, i;
93 fib_table_t *fib;
94
Neale Ranns2dd68522017-02-16 03:38:59 -080095 vec_foreach_index (i, dpm->vss_index_by_rx_fib_index[proto])
96 {
khemendra kumar34719e32017-12-08 18:06:52 +053097 vss_index = dpm->vss_index_by_rx_fib_index[proto][i];
98 if (~0 == vss_index)
99 continue;
Neale Ranns2dd68522017-02-16 03:38:59 -0800100
khemendra kumar34719e32017-12-08 18:06:52 +0530101 vss = pool_elt_at_index (dpm->vss[proto], vss_index);
Neale Ranns2dd68522017-02-16 03:38:59 -0800102
khemendra kumar34719e32017-12-08 18:06:52 +0530103 if (FIB_PROTOCOL_IP4 == proto)
104 {
105 fib = fib_table_get (i, proto);
Neale Ranns2dd68522017-02-16 03:38:59 -0800106
khemendra kumar34719e32017-12-08 18:06:52 +0530107 if (!fn (vss, fib->ft_table_id, ctx))
108 break;
109 }
110 else
111 {
112 mfib = mfib_table_get (i, proto);
Neale Ranns358425b2017-02-20 09:42:36 -0800113
khemendra kumar34719e32017-12-08 18:06:52 +0530114 if (!fn (vss, mfib->mft_table_id, ctx))
115 break;
116 }
117 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800118}
119
Neale Ranns3466c302017-02-16 07:45:03 -0800120static u32
khemendra kumar34719e32017-12-08 18:06:52 +0530121dhcp_proxy_server_find (dhcp_proxy_t * proxy,
122 fib_protocol_t proto,
123 ip46_address_t * addr, u32 server_table_id)
Neale Ranns3466c302017-02-16 07:45:03 -0800124{
khemendra kumar34719e32017-12-08 18:06:52 +0530125 dhcp_server_t *server;
126 u32 ii, fib_index;
Neale Ranns3466c302017-02-16 07:45:03 -0800127
khemendra kumar34719e32017-12-08 18:06:52 +0530128 vec_foreach_index (ii, proxy->dhcp_servers)
129 {
130 server = &proxy->dhcp_servers[ii];
131 fib_index = fib_table_find (proto, server_table_id);
Neale Ranns3466c302017-02-16 07:45:03 -0800132
khemendra kumar34719e32017-12-08 18:06:52 +0530133 if (ip46_address_is_equal (&server->dhcp_server,
134 addr) &&
135 (server->server_fib_index == fib_index))
136 {
137 return (ii);
138 }
139 }
140 return (~0);
Neale Ranns3466c302017-02-16 07:45:03 -0800141}
142
Neale Ranns2dd68522017-02-16 03:38:59 -0800143int
144dhcp_proxy_server_del (fib_protocol_t proto,
khemendra kumar34719e32017-12-08 18:06:52 +0530145 u32 rx_fib_index,
146 ip46_address_t * addr, u32 server_table_id)
Neale Ranns2dd68522017-02-16 03:38:59 -0800147{
khemendra kumar34719e32017-12-08 18:06:52 +0530148 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
Neale Ranns3466c302017-02-16 07:45:03 -0800149 dhcp_proxy_t *proxy = 0;
Neale Ranns2dd68522017-02-16 03:38:59 -0800150
khemendra kumar34719e32017-12-08 18:06:52 +0530151 proxy = dhcp_get_proxy (dpm, rx_fib_index, proto);
Neale Ranns2dd68522017-02-16 03:38:59 -0800152
Neale Ranns3466c302017-02-16 07:45:03 -0800153 if (NULL != proxy)
khemendra kumar34719e32017-12-08 18:06:52 +0530154 {
Neale Ranns3466c302017-02-16 07:45:03 -0800155 dhcp_server_t *server;
156 u32 index;
Neale Ranns2dd68522017-02-16 03:38:59 -0800157
khemendra kumar34719e32017-12-08 18:06:52 +0530158 index = dhcp_proxy_server_find (proxy, proto, addr, server_table_id);
Neale Ranns2dd68522017-02-16 03:38:59 -0800159
Neale Ranns3466c302017-02-16 07:45:03 -0800160 if (~0 != index)
khemendra kumar34719e32017-12-08 18:06:52 +0530161 {
162 server = &proxy->dhcp_servers[index];
163 fib_table_unlock (server->server_fib_index, proto, FIB_SOURCE_DHCP);
Neale Ranns3466c302017-02-16 07:45:03 -0800164
khemendra kumar34719e32017-12-08 18:06:52 +0530165 vec_del1 (proxy->dhcp_servers, index);
Neale Ranns3466c302017-02-16 07:45:03 -0800166
khemendra kumar34719e32017-12-08 18:06:52 +0530167 if (0 == vec_len (proxy->dhcp_servers))
168 {
169 /* no servers left, delete the proxy config */
170 dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] =
171 ~0;
172 vec_free (proxy->dhcp_servers);
173 pool_put (dpm->dhcp_servers[proto], proxy);
174 return (1);
175 }
176 }
177 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800178
Neale Ranns3466c302017-02-16 07:45:03 -0800179 /* the proxy still exists */
180 return (0);
Neale Ranns2dd68522017-02-16 03:38:59 -0800181}
182
183int
184dhcp_proxy_server_add (fib_protocol_t proto,
khemendra kumar34719e32017-12-08 18:06:52 +0530185 ip46_address_t * addr,
186 ip46_address_t * src_address,
187 u32 rx_fib_index, u32 server_table_id)
Neale Ranns2dd68522017-02-16 03:38:59 -0800188{
khemendra kumar34719e32017-12-08 18:06:52 +0530189 dhcp_proxy_main_t *dpm = &dhcp_proxy_main;
190 dhcp_proxy_t *proxy = 0;
Neale Ranns2dd68522017-02-16 03:38:59 -0800191 int new = 0;
192
khemendra kumar34719e32017-12-08 18:06:52 +0530193 proxy = dhcp_get_proxy (dpm, rx_fib_index, proto);
Neale Ranns2dd68522017-02-16 03:38:59 -0800194
Neale Ranns3466c302017-02-16 07:45:03 -0800195 if (NULL == proxy)
khemendra kumar34719e32017-12-08 18:06:52 +0530196 {
197 vec_validate_init_empty (dpm->dhcp_server_index_by_rx_fib_index[proto],
198 rx_fib_index, ~0);
Neale Ranns2dd68522017-02-16 03:38:59 -0800199
Neale Ranns3466c302017-02-16 07:45:03 -0800200 pool_get (dpm->dhcp_servers[proto], proxy);
Dave Barachb7b92992018-10-17 10:38:51 -0400201 clib_memset (proxy, 0, sizeof (*proxy));
Neale Ranns2dd68522017-02-16 03:38:59 -0800202 new = 1;
203
204 dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] =
khemendra kumar34719e32017-12-08 18:06:52 +0530205 proxy - dpm->dhcp_servers[proto];
Neale Ranns2dd68522017-02-16 03:38:59 -0800206
Neale Ranns3466c302017-02-16 07:45:03 -0800207 proxy->dhcp_src_address = *src_address;
208 proxy->rx_fib_index = rx_fib_index;
khemendra kumar34719e32017-12-08 18:06:52 +0530209 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800210 else
khemendra kumar34719e32017-12-08 18:06:52 +0530211 {
212 if (~0 != dhcp_proxy_server_find (proxy, proto, addr, server_table_id))
213 {
214 return (new);
215 }
216 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800217
Neale Ranns3466c302017-02-16 07:45:03 -0800218 dhcp_server_t server = {
khemendra kumar34719e32017-12-08 18:06:52 +0530219 .dhcp_server = *addr,
220 .server_fib_index = fib_table_find_or_create_and_lock (proto,
221 server_table_id,
222 FIB_SOURCE_DHCP),
Neale Ranns3466c302017-02-16 07:45:03 -0800223 };
224
khemendra kumar34719e32017-12-08 18:06:52 +0530225 vec_add1 (proxy->dhcp_servers, server);
Neale Ranns2dd68522017-02-16 03:38:59 -0800226
227 return (new);
228}
229
230typedef struct dhcp4_proxy_dump_walk_ctx_t_
231{
khemendra kumar34719e32017-12-08 18:06:52 +0530232 fib_protocol_t proto;
233 void *opaque;
234 u32 context;
Neale Ranns2dd68522017-02-16 03:38:59 -0800235} dhcp_proxy_dump_walk_cxt_t;
236
237static int
khemendra kumar34719e32017-12-08 18:06:52 +0530238dhcp_proxy_dump_walk (dhcp_proxy_t * proxy, void *arg)
Neale Ranns2dd68522017-02-16 03:38:59 -0800239{
240 dhcp_proxy_dump_walk_cxt_t *ctx = arg;
Neale Ranns2dd68522017-02-16 03:38:59 -0800241
khemendra kumar34719e32017-12-08 18:06:52 +0530242 dhcp_send_details (ctx->proto, ctx->opaque, ctx->context, proxy);
Neale Ranns2dd68522017-02-16 03:38:59 -0800243
244 return (1);
245}
246
247void
khemendra kumar34719e32017-12-08 18:06:52 +0530248dhcp_proxy_dump (fib_protocol_t proto, void *opaque, u32 context)
Neale Ranns2dd68522017-02-16 03:38:59 -0800249{
khemendra kumar34719e32017-12-08 18:06:52 +0530250 dhcp_proxy_dump_walk_cxt_t ctx = {
251 .proto = proto,
252 .opaque = opaque,
253 .context = context,
254 };
255 dhcp_proxy_walk (proto, dhcp_proxy_dump_walk, &ctx);
Neale Ranns2dd68522017-02-16 03:38:59 -0800256}
257
258int
khemendra kumar34719e32017-12-08 18:06:52 +0530259dhcp_vss_show_walk (dhcp_vss_t * vss, u32 rx_table_id, void *ctx)
Neale Ranns2dd68522017-02-16 03:38:59 -0800260{
khemendra kumar34719e32017-12-08 18:06:52 +0530261 vlib_main_t *vm = ctx;
Neale Ranns2dd68522017-02-16 03:38:59 -0800262
khemendra kumar34719e32017-12-08 18:06:52 +0530263 if (vss->vss_type == VSS_TYPE_VPN_ID)
264 {
265 u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
266 + ((u32) vss->vpn_id[2]);
267 u32 fib_id = ((u32) vss->vpn_id[3] << 24) + ((u32) vss->vpn_id[4] << 16)
268 + ((u32) vss->vpn_id[5] << 8) + ((u32) vss->vpn_id[6]);
269 vlib_cli_output (vm, " fib_table: %d oui: %d vpn_index: %d",
270 rx_table_id, oui, fib_id);
271 }
272 else if (vss->vss_type == VSS_TYPE_ASCII)
273 vlib_cli_output (vm, " fib_table: %d vpn_id: %s",
274 rx_table_id, vss->vpn_ascii_id);
275 else
276 vlib_cli_output (vm, " fib_table: %d default global vpn", rx_table_id);
Neale Ranns2dd68522017-02-16 03:38:59 -0800277
khemendra kumar34719e32017-12-08 18:06:52 +0530278 return (1);
Neale Ranns2dd68522017-02-16 03:38:59 -0800279}
280
khemendra kumar34719e32017-12-08 18:06:52 +0530281void
282update_vss (dhcp_vss_t * v,
283 u8 vss_type, u8 * vpn_ascii_id, u32 oui, u32 vpn_index)
John Lo70bfcaf2017-11-14 13:19:26 -0500284{
285 v->vss_type = vss_type;
286 if (v->vpn_ascii_id)
287 {
khemendra kumar34719e32017-12-08 18:06:52 +0530288 if (v->vpn_ascii_id == (u8 *) ~ 0)
John Lo70bfcaf2017-11-14 13:19:26 -0500289 v->vpn_ascii_id = 0;
290 else
291 vec_free (v->vpn_ascii_id);
292 }
293
294 if (vss_type == VSS_TYPE_ASCII)
khemendra kumar34719e32017-12-08 18:06:52 +0530295 v->vpn_ascii_id = vpn_ascii_id;
John Lo70bfcaf2017-11-14 13:19:26 -0500296 else if (vss_type == VSS_TYPE_VPN_ID)
297 {
298 v->vpn_id[0] = (oui >> 16) & 0xff;
299 v->vpn_id[1] = (oui >> 8) & 0xff;
300 v->vpn_id[2] = (oui >> 0) & 0xff;
301 v->vpn_id[3] = (vpn_index >> 24) & 0xff;
302 v->vpn_id[4] = (vpn_index >> 16) & 0xff;
303 v->vpn_id[5] = (vpn_index >> 8) & 0xff;
304 v->vpn_id[6] = (vpn_index >> 0) & 0xff;
305 }
306}
307
khemendra kumar34719e32017-12-08 18:06:52 +0530308int
309dhcp_proxy_set_vss (fib_protocol_t proto,
310 u32 tbl_id,
311 u8 vss_type,
312 u8 * vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del)
Neale Ranns2dd68522017-02-16 03:38:59 -0800313{
314 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
315 dhcp_vss_t *v = NULL;
khemendra kumar34719e32017-12-08 18:06:52 +0530316 u32 rx_fib_index;
Neale Ranns2dd68522017-02-16 03:38:59 -0800317 int rc = 0;
khemendra kumar34719e32017-12-08 18:06:52 +0530318
Neale Ranns358425b2017-02-20 09:42:36 -0800319 if (proto == FIB_PROTOCOL_IP4)
khemendra kumar34719e32017-12-08 18:06:52 +0530320 rx_fib_index = fib_table_find_or_create_and_lock (proto, tbl_id,
321 FIB_SOURCE_DHCP);
Neale Ranns358425b2017-02-20 09:42:36 -0800322 else
khemendra kumar34719e32017-12-08 18:06:52 +0530323 rx_fib_index = mfib_table_find_or_create_and_lock (proto, tbl_id,
324 MFIB_SOURCE_DHCP);
325 v = dhcp_get_vss_info (dm, rx_fib_index, proto);
Neale Ranns2dd68522017-02-16 03:38:59 -0800326
327 if (NULL != v)
John Lo70bfcaf2017-11-14 13:19:26 -0500328 {
Neale Ranns2dd68522017-02-16 03:38:59 -0800329 if (is_del)
khemendra kumar34719e32017-12-08 18:06:52 +0530330 {
331 /* release the lock held on the table when the VSS
332 * info was created */
333 dhcp_proxy_rx_table_unlock (proto, rx_fib_index);
Neale Ranns2dd68522017-02-16 03:38:59 -0800334
John Lo70bfcaf2017-11-14 13:19:26 -0500335 vec_free (v->vpn_ascii_id);
khemendra kumar34719e32017-12-08 18:06:52 +0530336 pool_put (dm->vss[proto], v);
337 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
338 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800339 else
khemendra kumar34719e32017-12-08 18:06:52 +0530340 {
John Lo70bfcaf2017-11-14 13:19:26 -0500341 update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530342 }
John Lo70bfcaf2017-11-14 13:19:26 -0500343 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800344 else
John Lo70bfcaf2017-11-14 13:19:26 -0500345 {
Neale Ranns2dd68522017-02-16 03:38:59 -0800346 if (is_del)
khemendra kumar34719e32017-12-08 18:06:52 +0530347 rc = VNET_API_ERROR_NO_SUCH_ENTRY;
Neale Ranns2dd68522017-02-16 03:38:59 -0800348 else
khemendra kumar34719e32017-12-08 18:06:52 +0530349 {
350 /* create a new entry */
351 vec_validate_init_empty (dm->vss_index_by_rx_fib_index[proto],
352 rx_fib_index, ~0);
Neale Ranns2dd68522017-02-16 03:38:59 -0800353
khemendra kumar34719e32017-12-08 18:06:52 +0530354 /* hold a lock on the table whilst the VSS info exist */
355 pool_get (dm->vss[proto], v);
John Lo70bfcaf2017-11-14 13:19:26 -0500356 update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
khemendra kumar34719e32017-12-08 18:06:52 +0530357 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] =
358 v - dm->vss[proto];
359 dhcp_proxy_rx_table_lock (proto, rx_fib_index);
360 }
John Lo70bfcaf2017-11-14 13:19:26 -0500361 }
Neale Ranns2dd68522017-02-16 03:38:59 -0800362
363 /* Release the lock taken during the create_or_lock at the start */
Neale Ranns358425b2017-02-20 09:42:36 -0800364 dhcp_proxy_rx_table_unlock (proto, rx_fib_index);
365
Neale Ranns2dd68522017-02-16 03:38:59 -0800366 return (rc);
367}
khemendra kumar34719e32017-12-08 18:06:52 +0530368
369/*
370 * fd.io coding-style-patch-verification: ON
371 *
372 * Local Variables:
373 * eval: (c-set-style "gnu")
374 * End:
375 */