aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/dhcp/dhcp_proxy.c
blob: dae631236de02fb0765460b173eb7e69b0a4898b (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * proxy_node.c: common dhcp v4 and v6 proxy node processing
 *
 * Copyright (c) 2013 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/dhcp/dhcp_proxy.h>
#include <vnet/fib/fib_table.h>
#include <vnet/mfib/mfib_table.h>

/**
 * @brief Shard 4/6 instance of DHCP main
 */
dhcp_proxy_main_t dhcp_proxy_main;

static void
dhcp_proxy_rx_table_lock (fib_protocol_t proto,
                          u32 fib_index)
{
    if (FIB_PROTOCOL_IP4 == proto)
        fib_table_lock(fib_index, proto, FIB_SOURCE_DHCP);
    else
        mfib_table_lock(fib_index, proto, MFIB_SOURCE_DHCP);
}

static void
dhcp_proxy_rx_table_unlock (fib_protocol_t proto,
                            u32 fib_index)
{
    if (FIB_PROTOCOL_IP4 == proto)
        fib_table_unlock(fib_index, proto, FIB_SOURCE_DHCP);
    else
        mfib_table_unlock(fib_index, proto, MFIB_SOURCE_DHCP);
}

 u32
dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto,
                                  u32 fib_index)
{
    if (FIB_PROTOCOL_IP4 == proto)
      {
        fib_table_t *fib;

        fib = fib_table_get(fib_index, proto);

        return (fib->ft_table_id);
      }
    else
      {
        mfib_table_t *mfib;

        mfib = mfib_table_get(fib_index, proto);

        return (mfib->mft_table_id);
      }
}

void
dhcp_proxy_walk (fib_protocol_t proto,
                 dhcp_proxy_walk_fn_t fn,
                 void *ctx)
{
  dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
  dhcp_proxy_t * server;
  u32 server_index, i;

  vec_foreach_index (i, dpm->dhcp_server_index_by_rx_fib_index[proto])
  {
      server_index = dpm->dhcp_server_index_by_rx_fib_index[proto][i];
      if (~0 == server_index)
          continue;

      server = pool_elt_at_index (dpm->dhcp_servers[proto], server_index);

      if (!fn(server, ctx))
          break;
    }
}

void
dhcp_vss_walk (fib_protocol_t proto,
               dhcp_vss_walk_fn_t fn,
               void *ctx)
{
  dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
  mfib_table_t *mfib;
  dhcp_vss_t * vss;
  u32 vss_index, i;
  fib_table_t *fib;

  vec_foreach_index (i, dpm->vss_index_by_rx_fib_index[proto])
  {
      vss_index = dpm->vss_index_by_rx_fib_index[proto][i];
      if (~0 == vss_index)
          continue;

      vss = pool_elt_at_index (dpm->vss[proto], vss_index);

      if (FIB_PROTOCOL_IP4 == proto)
        {
          fib = fib_table_get(i, proto);

          if (!fn(vss, fib->ft_table_id, ctx))
              break;
        }
      else
        {
          mfib = mfib_table_get(i, proto);

          if (!fn(vss, mfib->mft_table_id, ctx))
              break;
        }
    }
}

static u32
dhcp_proxy_server_find (dhcp_proxy_t *proxy,
                        fib_protocol_t proto,
                        ip46_address_t *addr,
                        u32 server_table_id)
{
    dhcp_server_t *server;
    u32 ii, fib_index;

    vec_foreach_index(ii, proxy->dhcp_servers)
    {
        server = &proxy->dhcp_servers[ii];
        fib_index = fib_table_find(proto, server_table_id);

        if (ip46_address_is_equal(&server->dhcp_server,
                                  addr) &&
            (server->server_fib_index == fib_index))
        {
            return (ii);
        }
    }
    return (~0);
}

int
dhcp_proxy_server_del (fib_protocol_t proto,
                       u32 rx_fib_index,
                       ip46_address_t *addr,
                       u32 server_table_id)
{
  dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
  dhcp_proxy_t *proxy = 0;

  proxy = dhcp_get_proxy(dpm, rx_fib_index, proto);

  if (NULL != proxy)
  {
      dhcp_server_t *server;
      u32 index;

      index = dhcp_proxy_server_find(proxy, proto, addr, server_table_id);

      if (~0 != index)
      {
          server = &proxy->dhcp_servers[index];
          fib_table_unlock (server->server_fib_index, proto, FIB_SOURCE_DHCP);

          vec_del1(proxy->dhcp_servers, index);

          if (0 == vec_len(proxy->dhcp_servers))
          {
              /* no servers left, delete the proxy config */
              dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
              vec_free(proxy->dhcp_servers);
              pool_put (dpm->dhcp_servers[proto], proxy);
              return (1);
          }
      }
  }

  /* the proxy still exists */
  return (0);
}

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)
{
  dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
  dhcp_proxy_t * proxy = 0;
  int new = 0;

  proxy = dhcp_get_proxy(dpm, rx_fib_index, proto);

  if (NULL == proxy)
  {
      vec_validate_init_empty(dpm->dhcp_server_index_by_rx_fib_index[proto],
                              rx_fib_index,
                              ~0);

      pool_get (dpm->dhcp_servers[proto], proxy);
      memset (proxy, 0, sizeof (*proxy));
      new = 1;

      dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] =
          proxy - dpm->dhcp_servers[proto];

      proxy->dhcp_src_address = *src_address;
      proxy->rx_fib_index = rx_fib_index;
  }
  else
  {
      if (~0 != dhcp_proxy_server_find(proxy, proto, addr, server_table_id))
      {
          return (new);
      }
  }

  dhcp_server_t server = {
      .dhcp_server = *addr,
      .server_fib_index = fib_table_find_or_create_and_lock(proto,
                                                            server_table_id,
                                                            FIB_SOURCE_DHCP),
  };

  vec_add1(proxy->dhcp_servers, server);

  return (new);
}

typedef struct dhcp4_proxy_dump_walk_ctx_t_
{
    fib_protocol_t proto;
    void *opaque;
    u32 context;
} dhcp_proxy_dump_walk_cxt_t;

static int
dhcp_proxy_dump_walk (dhcp_proxy_t *proxy,
                      void *arg)
{
  dhcp_proxy_dump_walk_cxt_t *ctx = arg;

  dhcp_send_details(ctx->proto,
                    ctx->opaque,
                    ctx->context,
                    proxy);

  return (1);
}

void
dhcp_proxy_dump (fib_protocol_t proto,
                 void *opaque,
                 u32 context)
{
    dhcp_proxy_dump_walk_cxt_t ctx =  {
        .proto = proto,
        .opaque = opaque,
        .context = context,
    };
    dhcp_proxy_walk(proto, dhcp_proxy_dump_walk, &ctx);
}

int
dhcp_vss_show_walk (dhcp_vss_t *vss,
                    u32 rx_table_id,
                    void *ctx)
{
    vlib_main_t * vm = ctx;

    if (vss->vss_type == VSS_TYPE_VPN_ID)
      {
	u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
	    + ((u32) vss->vpn_id[2]);
	u32 fib_id = ((u32) vss->vpn_id[3] << 24) + ((u32) vss->vpn_id[4] << 16)
	    + ((u32) vss->vpn_id[5] << 8) + ((u32) vss->vpn_id[6]);
	vlib_cli_output (vm, " fib_table: %d  oui: %d vpn_index: %d",
			 rx_table_id, oui, fib_id);
      }
    else if (vss->vss_type == VSS_TYPE_ASCII)
	vlib_cli_output (vm, " fib_table: %d  vpn_id: %s",
			 rx_table_id, vss->vpn_ascii_id);
    else
	vlib_cli_output (vm, " fib_table: %d  default global vpn", rx_table_id);

    return (1);
}

void update_vss (dhcp_vss_t *v,
		 u8 vss_type,
		 u8 *vpn_ascii_id,
		 u32 oui,
		 u32 vpn_index)
{
  v->vss_type = vss_type;
  if (v->vpn_ascii_id)
    {
	if (v->vpn_ascii_id == (u8 *) ~0)
	v->vpn_ascii_id = 0;
      else
	vec_free (v->vpn_ascii_id);
    }

  if (vss_type == VSS_TYPE_ASCII)
      v->vpn_ascii_id = vpn_ascii_id;
  else if (vss_type == VSS_TYPE_VPN_ID)
    {
      v->vpn_id[0] = (oui >> 16) & 0xff;
      v->vpn_id[1] = (oui >> 8) & 0xff;
      v->vpn_id[2] = (oui >> 0) & 0xff;
      v->vpn_id[3] = (vpn_index >> 24) & 0xff;
      v->vpn_id[4] = (vpn_index >> 16) & 0xff;
      v->vpn_id[5] = (vpn_index >> 8) & 0xff;
      v->vpn_id[6] = (vpn_index >> 0) & 0xff;
    }
}

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)
{
  dhcp_proxy_main_t *dm = &dhcp_proxy_main;
  dhcp_vss_t *v = NULL;
  u32  rx_fib_index;
  int rc = 0;
  
  if (proto == FIB_PROTOCOL_IP4)
      rx_fib_index = fib_table_find_or_create_and_lock(proto, tbl_id,
                                                       FIB_SOURCE_DHCP);
  else
      rx_fib_index = mfib_table_find_or_create_and_lock(proto, tbl_id,
                                                        MFIB_SOURCE_DHCP);
  v = dhcp_get_vss_info(dm, rx_fib_index, proto);

  if (NULL != v)
    {
      if (is_del)
        {
          /* release the lock held on the table when the VSS
           * info was created */
          dhcp_proxy_rx_table_unlock (proto, rx_fib_index);

	  vec_free (v->vpn_ascii_id);
          pool_put (dm->vss[proto], v);
          dm->vss_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
        }
      else
        {
	  update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
        }
    }
  else
    {
      if (is_del)
        rc = VNET_API_ERROR_NO_SUCH_ENTRY;
      else
        {
          /* create a new entry */
          vec_validate_init_empty(dm->vss_index_by_rx_fib_index[proto],
                                  rx_fib_index, ~0);

          /* hold a lock on the table whilst the VSS info exist */
          pool_get (dm->vss[proto], v);
	  update_vss (v, vss_type, vpn_ascii_id, oui, vpn_index);
          dm->vss_index_by_rx_fib_index[proto][rx_fib_index] =
              v - dm->vss[proto];
          dhcp_proxy_rx_table_lock (proto, rx_fib_index);
        }
    }

  /* Release the lock taken during the create_or_lock at the start */
  dhcp_proxy_rx_table_unlock (proto, rx_fib_index);

  return (rc);
}