FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
dhcp_proxy.c
Go to the documentation of this file.
1 /*
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 <dhcp/dhcp_proxy.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/mfib/mfib_table.h>
21 
22 /**
23  * @brief Shard 4/6 instance of DHCP main
24  */
26 
27 static void
29 {
30  if (FIB_PROTOCOL_IP4 == proto)
31  fib_table_lock (fib_index, proto, FIB_SOURCE_DHCP);
32  else
34 }
35 
36 static void
38 {
39  if (FIB_PROTOCOL_IP4 == proto)
41  else
43 }
44 
45 u32
47 {
48  if (FIB_PROTOCOL_IP4 == proto)
49  {
50  fib_table_t *fib;
51 
52  fib = fib_table_get (fib_index, proto);
53 
54  return (fib->ft_table_id);
55  }
56  else
57  {
58  mfib_table_t *mfib;
59 
60  mfib = mfib_table_get (fib_index, proto);
61 
62  return (mfib->mft_table_id);
63  }
64 }
65 
66 void
68 {
70  dhcp_proxy_t *server;
71  u32 server_index, i;
72 
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 
86 void
88 {
90  mfib_table_t *mfib;
91  dhcp_vss_t *vss;
92  u32 vss_index, i;
93  fib_table_t *fib;
94 
96  {
97  vss_index = dpm->vss_index_by_rx_fib_index[proto][i];
98  if (~0 == vss_index)
99  continue;
100 
101  vss = pool_elt_at_index (dpm->vss[proto], vss_index);
102 
103  if (FIB_PROTOCOL_IP4 == proto)
104  {
105  fib = fib_table_get (i, proto);
106 
107  if (!fn (vss, fib->ft_table_id, ctx))
108  break;
109  }
110  else
111  {
112  mfib = mfib_table_get (i, proto);
113 
114  if (!fn (vss, mfib->mft_table_id, ctx))
115  break;
116  }
117  }
118 }
119 
120 static u32
123  ip46_address_t * addr, u32 server_table_id)
124 {
125  dhcp_server_t *server;
126  u32 ii, fib_index;
127 
128  vec_foreach_index (ii, proxy->dhcp_servers)
129  {
130  server = &proxy->dhcp_servers[ii];
131  fib_index = fib_table_find (proto, server_table_id);
132 
133  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);
141 }
142 
143 int
145  u32 rx_fib_index,
146  ip46_address_t * addr, u32 server_table_id)
147 {
149  dhcp_proxy_t *proxy = 0;
150 
151  proxy = dhcp_get_proxy (dpm, rx_fib_index, proto);
152 
153  if (NULL != proxy)
154  {
155  dhcp_server_t *server;
156  u32 index;
157 
158  index = dhcp_proxy_server_find (proxy, proto, addr, server_table_id);
159 
160  if (~0 != index)
161  {
162  server = &proxy->dhcp_servers[index];
164 
165  vec_del1 (proxy->dhcp_servers, index);
166 
167  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  }
178 
179  /* the proxy still exists */
180  return (0);
181 }
182 
183 int
185  ip46_address_t * addr,
186  ip46_address_t * src_address,
187  u32 rx_fib_index, u32 server_table_id)
188 {
190  dhcp_proxy_t *proxy = 0;
191  int new = 0;
192 
193  proxy = dhcp_get_proxy (dpm, rx_fib_index, proto);
194 
195  if (NULL == proxy)
196  {
198  rx_fib_index, ~0);
199 
200  pool_get (dpm->dhcp_servers[proto], proxy);
201  clib_memset (proxy, 0, sizeof (*proxy));
202  new = 1;
203 
204  dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] =
205  proxy - dpm->dhcp_servers[proto];
206 
207  proxy->dhcp_src_address = *src_address;
208  proxy->rx_fib_index = rx_fib_index;
209  }
210  else
211  {
212  if (~0 != dhcp_proxy_server_find (proxy, proto, addr, server_table_id))
213  {
214  return (new);
215  }
216  }
217 
218  dhcp_server_t server = {
219  .dhcp_server = *addr,
220  .server_fib_index = fib_table_find_or_create_and_lock (proto,
221  server_table_id,
223  };
224 
225  vec_add1 (proxy->dhcp_servers, server);
226 
227  return (new);
228 }
229 
231 {
233  void *opaque;
236 
237 static int
238 dhcp_proxy_dump_walk (dhcp_proxy_t * proxy, void *arg)
239 {
241 
242  dhcp_send_details (ctx->proto, ctx->opaque, ctx->context, proxy);
243 
244  return (1);
245 }
246 
247 void
249 {
251  .proto = proto,
252  .opaque = opaque,
253  .context = context,
254  };
256 }
257 
258 int
259 dhcp_vss_show_walk (dhcp_vss_t * vss, u32 rx_table_id, void *ctx)
260 {
261  vlib_main_t *vm = ctx;
262 
263  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);
277 
278  return (1);
279 }
280 
281 void
283  u8 vss_type, u8 * vpn_ascii_id, u32 oui, u32 vpn_index)
284 {
285  v->vss_type = vss_type;
286  if (v->vpn_ascii_id)
287  {
288  if (v->vpn_ascii_id == (u8 *) ~ 0)
289  v->vpn_ascii_id = 0;
290  else
291  vec_free (v->vpn_ascii_id);
292  }
293 
294  if (vss_type == VSS_TYPE_ASCII)
295  v->vpn_ascii_id = vpn_ascii_id;
296  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 
308 int
310  u32 tbl_id,
311  u8 vss_type,
312  u8 * vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del)
313 {
315  dhcp_vss_t *v = NULL;
316  u32 rx_fib_index;
317  int rc = 0;
318 
319  if (proto == FIB_PROTOCOL_IP4)
320  rx_fib_index = fib_table_find_or_create_and_lock (proto, tbl_id,
322  else
323  rx_fib_index = mfib_table_find_or_create_and_lock (proto, tbl_id,
325  v = dhcp_get_vss_info (dm, rx_fib_index, proto);
326 
327  if (NULL != v)
328  {
329  if (is_del)
330  {
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);
334 
335  vec_free (v->vpn_ascii_id);
336  pool_put (dm->vss[proto], v);
337  dm->vss_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
338  }
339  else
340  {
341  update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
342  }
343  }
344  else
345  {
346  if (is_del)
347  rc = VNET_API_ERROR_NO_SUCH_ENTRY;
348  else
349  {
350  /* create a new entry */
352  rx_fib_index, ~0);
353 
354  /* hold a lock on the table whilst the VSS info exist */
355  pool_get (dm->vss[proto], v);
356  update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
357  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  }
361  }
362 
363  /* Release the lock taken during the create_or_lock at the start */
364  dhcp_proxy_rx_table_unlock (proto, rx_fib_index);
365 
366  return (rc);
367 }
368 
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */
update_vss
void update_vss(dhcp_vss_t *v, u8 vss_type, u8 *vpn_ascii_id, u32 oui, u32 vpn_index)
Definition: dhcp_proxy.c:282
dhcp_vss_t_::vss_type
u8 vss_type
VSS type as defined in RFC 6607: 0 for NVT ASCII VPN Identifier 1 for RFC 2685 VPN-ID of 7 octects - ...
Definition: dhcp_proxy.h:64
dhcp_proxy_rx_table_lock
static void dhcp_proxy_rx_table_lock(fib_protocol_t proto, u32 fib_index)
Definition: dhcp_proxy.c:28
dhcp_server_t_
A representation of a single DHCP Server within a given VRF config.
Definition: dhcp_proxy.h:82
dhcp_get_proxy
static dhcp_proxy_t * dhcp_get_proxy(dhcp_proxy_main_t *dm, u32 rx_fib_index, fib_protocol_t proto)
Get the DHCP proxy server data for the FIB index.
Definition: dhcp_proxy.h:274
dhcp_vss_t_::vpn_ascii_id
u8 * vpn_ascii_id
Type 0 ASCII VPN Identifier.
Definition: dhcp_proxy.h:76
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
FIB_SOURCE_DHCP
@ FIB_SOURCE_DHCP
DHCP.
Definition: fib_source.h:91
mfib_table_find_or_create_and_lock
u32 mfib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, mfib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: mfib_table.c:613
dhcp_proxy_set_vss
int dhcp_proxy_set_vss(fib_protocol_t proto, u32 tbl_id, u8 vss_type, u8 *vpn_ascii_id, u32 oui, u32 vpn_index, u8 is_del)
Configure/set a new VSS info.
Definition: dhcp_proxy.c:309
fib_table.h
dhcp_proxy_t_
A DHCP proxy representation fpr per-client VRF config.
Definition: dhcp_proxy.h:100
pool_put
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:305
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
dhcp_proxy_server_del
int dhcp_proxy_server_del(fib_protocol_t proto, u32 rx_fib_index, ip46_address_t *addr, u32 server_table_id)
Delete a DHCP proxy config.
Definition: dhcp_proxy.c:144
dhcp_vss_t_
The Virtual Sub-net Selection information for a given RX FIB.
Definition: dhcp_proxy.h:56
dhcp_proxy_main_t::dhcp_server_index_by_rx_fib_index
u32 * dhcp_server_index_by_rx_fib_index[DHCP_N_PROTOS]
Definition: dhcp_proxy.h:145
dhcp4_proxy_dump_walk_ctx_t_::opaque
void * opaque
Definition: dhcp_proxy.c:233
dhcp_proxy_main
dhcp_proxy_main_t dhcp_proxy_main
Shard 4/6 instance of DHCP main.
Definition: dhcp_proxy.c:25
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
mfib_table_t_
A protocol Independent IP multicast FIB table.
Definition: mfib_table.h:71
dhcp_proxy_main_t
Collection of global DHCP proxy data.
Definition: dhcp_proxy.h:139
dhcp_get_vss_info
static dhcp_vss_t * dhcp_get_vss_info(dhcp_proxy_main_t *dm, u32 rx_fib_index, fib_protocol_t proto)
Get the VSS data for the FIB index.
Definition: dhcp_proxy.h:254
dhcp_vss_walk_fn_t
int(* dhcp_vss_walk_fn_t)(dhcp_vss_t *server, u32 rx_table_id, void *ctx)
Callback function invoked for each DHCP VSS entry return 0 to break the walk, non-zero otherwise.
Definition: dhcp_proxy.h:230
dhcp_send_details
void dhcp_send_details(fib_protocol_t proto, void *opaque, u32 context, dhcp_proxy_t *proxy)
Send the details of a proxy session to the API client during a dump.
Definition: dhcp_api.c:176
fib_table_t_
A protocol Independent FIB table.
Definition: fib_table.h:71
mfib_table_lock
void mfib_table_lock(u32 fib_index, fib_protocol_t proto, mfib_source_t source)
Release a reference counting lock on the table.
Definition: mfib_table.c:806
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
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
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
fib_table_t_::ft_table_id
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
fib_table_find_or_create_and_lock
u32 fib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, fib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1170
dhcp_proxy_main_t::dhcp_servers
dhcp_proxy_t * dhcp_servers[DHCP_N_PROTOS]
Definition: dhcp_proxy.h:142
vec_foreach_index
#define vec_foreach_index(var, v)
Iterate over vector indices.
Definition: vec_bootstrap.h:220
VSS_TYPE_ASCII
#define VSS_TYPE_ASCII
Definition: dhcp_proxy.h:65
dhcp_proxy_t_::rx_fib_index
u32 rx_fib_index
The FIB index (not the external Table-ID) in which the client is resides.
Definition: dhcp_proxy.h:131
dhcp_server_t_::server_fib_index
u32 server_fib_index
The FIB index (not the external Table-ID) in which the server is reachable.
Definition: dhcp_proxy.h:94
dhcp_proxy_dump_walk_cxt_t
struct dhcp4_proxy_dump_walk_ctx_t_ dhcp_proxy_dump_walk_cxt_t
mfib_table_get
mfib_table_t * mfib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: mfib_table.c:30
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
dhcp_proxy_dump
void dhcp_proxy_dump(fib_protocol_t proto, void *opaque, u32 context)
Dump the proxy configs to the API.
Definition: dhcp_proxy.c:248
dhcp_proxy_dump_walk
static int dhcp_proxy_dump_walk(dhcp_proxy_t *proxy, void *arg)
Definition: dhcp_proxy.c:238
dhcp_proxy_walk_fn_t
int(* dhcp_proxy_walk_fn_t)(dhcp_proxy_t *server, void *ctx)
Callback function invoked for each DHCP proxy entry return 0 to break the walk, non-zero otherwise.
Definition: dhcp_proxy.h:218
fib_protocol_t
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
dhcp_proxy_walk
void dhcp_proxy_walk(fib_protocol_t proto, dhcp_proxy_walk_fn_t fn, void *ctx)
Walk/Visit each DHCP proxy server.
Definition: dhcp_proxy.c:67
mfib_table.h
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
dhcp4_proxy_dump_walk_ctx_t_::proto
fib_protocol_t proto
Definition: dhcp_proxy.c:232
dhcp_server_t_::dhcp_server
ip46_address_t dhcp_server
The address of the DHCP server to which to relay the client's messages.
Definition: dhcp_proxy.h:88
mfib_table_unlock
void mfib_table_unlock(u32 fib_index, fib_protocol_t proto, mfib_source_t source)
Take a reference counting lock on the table.
Definition: mfib_table.c:777
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
index
u32 index
Definition: flow_types.api:221
dhcp_proxy_server_find
static u32 dhcp_proxy_server_find(dhcp_proxy_t *proxy, fib_protocol_t proto, ip46_address_t *addr, u32 server_table_id)
Definition: dhcp_proxy.c:121
dhcp_vss_t_::vpn_id
u8 vpn_id[7]
Type 1 VPN-ID.
Definition: dhcp_proxy.h:72
dhcp4_proxy_dump_walk_ctx_t_
Definition: dhcp_proxy.c:230
dhcp_proxy_main_t::vss
dhcp_vss_t * vss[DHCP_N_PROTOS]
Definition: dhcp_proxy.h:150
vec_validate_init_empty
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header,...
Definition: vec.h:570
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
dhcp4_proxy_dump_walk_ctx_t_::context
u32 context
Definition: dhcp_proxy.c:234
fib_table_lock
void fib_table_lock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Release a reference counting lock on the table.
Definition: fib_table.c:1361
mfib_table_t_::mft_table_id
u32 mft_table_id
Table ID (hash key) for this FIB.
Definition: mfib_table.h:107
u32
unsigned int u32
Definition: types.h:88
VSS_TYPE_VPN_ID
#define VSS_TYPE_VPN_ID
Definition: dhcp_proxy.h:66
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
dhcp_proxy_rx_table_get_table_id
u32 dhcp_proxy_rx_table_get_table_id(fib_protocol_t proto, u32 fib_index)
Definition: dhcp_proxy.c:46
vss_type
vss_type
Definition: dhcp.api:22
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
dhcp_proxy.h
u8
unsigned char u8
Definition: types.h:56
fib_table_unlock
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1342
i
int i
Definition: flowhash_template.h:376
dhcp_vss_walk
void dhcp_vss_walk(fib_protocol_t proto, dhcp_vss_walk_fn_t fn, void *ctx)
Walk/Visit each DHCP proxy VSS.
Definition: dhcp_proxy.c:87
context
u32 context
Definition: ip.api:852
dhcp_proxy_t_::dhcp_servers
dhcp_server_t * dhcp_servers
The set of DHCP servers to which messages are relayed.
Definition: dhcp_proxy.h:110
dhcp_proxy_t_::dhcp_src_address
ip46_address_t dhcp_src_address
The source address to use in relayed messaes.
Definition: dhcp_proxy.h:125
dhcp_vss_show_walk
int dhcp_vss_show_walk(dhcp_vss_t *vss, u32 rx_table_id, void *ctx)
Show (on CLI) a VSS config during a show walk.
Definition: dhcp_proxy.c:259
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
src_address
vl_api_address_union_t src_address
Definition: ip_types.api:122
dhcp_proxy_rx_table_unlock
static void dhcp_proxy_rx_table_unlock(fib_protocol_t proto, u32 fib_index)
Definition: dhcp_proxy.c:37
dhcp_proxy_server_add
int dhcp_proxy_server_add(fib_protocol_t proto, ip46_address_t *addr, ip46_address_t *src_address, u32 rx_fib_index, u32 server_table_id)
Add a new DHCP proxy server configuration.
Definition: dhcp_proxy.c:184
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:1111
vec_del1
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:896
MFIB_SOURCE_DHCP
@ MFIB_SOURCE_DHCP
Definition: mfib_types.h:168
dhcp_proxy_main_t::vss_index_by_rx_fib_index
u32 * vss_index_by_rx_fib_index[DHCP_N_PROTOS]
Definition: dhcp_proxy.h:153