blob: 4dd2239ffaa26b32deda3f6b44bc18e11f0d6d6f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * proxy_node.c: dhcpv6 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 <vlib/vlib.h>
19#include <vnet/pg/pg.h>
20#include <vnet/dhcpv6/proxy.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/ip6_fib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23static char * dhcpv6_proxy_error_strings[] = {
24#define dhcpv6_proxy_error(n,s) s,
25#include "proxy_error.def"
26#undef dhcpv6_proxy_error
27};
28
29#define foreach_dhcpv6_proxy_to_server_input_next \
30 _ (DROP, "error-drop") \
31 _ (LOOKUP, "ip6-lookup") \
32 _ (SEND_TO_CLIENT, "dhcpv6-proxy-to-client")
Wojciech Dec558d5eb2016-11-01 16:44:06 +010033
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
35typedef enum {
36#define _(s,n) DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s,
37 foreach_dhcpv6_proxy_to_server_input_next
38#undef _
39 DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
40} dhcpv6_proxy_to_server_input_next_t;
41
42typedef struct {
43 /* 0 => to server, 1 => to client */
Wojciech Dec558d5eb2016-11-01 16:44:06 +010044 int which;
45 u8 packet_data[64];
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 u32 error;
47 u32 sw_if_index;
48 u32 original_sw_if_index;
49} dhcpv6_proxy_trace_t;
50
51vlib_node_registration_t dhcpv6_proxy_to_server_node;
52vlib_node_registration_t dhcpv6_proxy_to_client_node;
53
54
55u8 * format_dhcpv6_proxy_trace (u8 * s, va_list * args)
56{
57 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59 dhcpv6_proxy_trace_t * t = va_arg (*args, dhcpv6_proxy_trace_t *);
Wojciech Dec558d5eb2016-11-01 16:44:06 +010060
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 if (t->which == 0)
62 s = format (s, "DHCPV6 proxy: sent to server %U",
63 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
64 else
65 s = format (s, "DHCPV6 proxy: sent to client from %U",
66 format_ip6_address, &t->packet_data, sizeof (ip6_address_t));
67 if (t->error != (u32)~0)
68 s = format (s, " error: %s\n", dhcpv6_proxy_error_strings[t->error]);
69
70 s = format (s, " original_sw_if_index: %d, sw_if_index: %d\n",
71 t->original_sw_if_index, t->sw_if_index);
Wojciech Dec558d5eb2016-11-01 16:44:06 +010072
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 return s;
74}
75
76u8 * format_dhcpv6_proxy_header_with_length (u8 * s, va_list * args)
77{
78 dhcpv6_header_t * h = va_arg (*args, dhcpv6_header_t *);
79 u32 max_header_bytes = va_arg (*args, u32);
80 u32 header_bytes;
81
82 header_bytes = sizeof (h[0]);
83 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
84 return format (s, "dhcpv6 header truncated");
85
86 s = format (s, "DHCPV6 Proxy");
87
88 return s;
89}
90/* get first interface address */
91static ip6_address_t *
92ip6_interface_first_global_or_site_address (ip6_main_t * im, u32 sw_if_index)
93{
94 ip_lookup_main_t * lm = &im->lookup_main;
95 ip_interface_address_t * ia = 0;
96 ip6_address_t * result = 0;
97
98 foreach_ip_interface_address (lm, ia, sw_if_index,
99 1 /* honor unnumbered */,
100 ({
101 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
102 if ((a->as_u8[0] & 0xe0) == 0x20 ||
103 (a->as_u8[0] & 0xfe) == 0xfc) {
104 result = a;
105 break;
106 }
107 }));
108 return result;
109}
110
111/* get first interface address */
112static ip6_address_t *
113ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
114{
115 ip_lookup_main_t * lm = &im->lookup_main;
116 ip_interface_address_t * ia = 0;
117 ip6_address_t * result = 0;
118
119 foreach_ip_interface_address (lm, ia, sw_if_index,
120 1 /* honor unnumbered */,
121 ({
122 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
123 result = a;
124 break;
125 }));
126 return result;
127}
128
129static inline void copy_ip6_address (ip6_address_t *dst, ip6_address_t *src)
130{
131
132 dst->as_u64[0] = src->as_u64[0];
133 dst->as_u64[1] = src->as_u64[1];
134}
135
136static uword
137dhcpv6_proxy_to_server_input (vlib_main_t * vm,
138 vlib_node_runtime_t * node,
139 vlib_frame_t * from_frame)
140{
141 u32 n_left_from, next_index, * from, * to_next;
142 dhcpv6_proxy_main_t * dpm = &dhcpv6_proxy_main;
143 from = vlib_frame_vector_args (from_frame);
144 n_left_from = from_frame->n_vectors;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100145 u32 pkts_to_server=0, pkts_to_client=0, pkts_no_server=0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 u32 pkts_no_interface_address=0, pkts_no_exceeding_max_hop=0;
147 u32 pkts_no_src_address=0;
148 u32 pkts_wrong_msg_type=0;
149 u32 pkts_too_big=0;
150 ip6_main_t * im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 ip6_fib_t * fib;
152 ip6_address_t * src;
153 int bogus_length;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100154 dhcpv6_server_t * server;
155 u32 rx_fib_idx = 0, server_fib_idx = 0;
156 u32 server_idx;
157 u32 fib_id1 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
159 next_index = node->cached_next_index;
160
161 while (n_left_from > 0)
162 {
163 u32 n_left_to_next;
164
165 vlib_get_next_frame (vm, node, next_index,
166 to_next, n_left_to_next);
167
168 while (n_left_from > 0 && n_left_to_next > 0)
169 {
170 vnet_main_t *vnm = vnet_get_main();
171 u32 sw_if_index = 0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100172 u32 rx_sw_if_index = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 vnet_sw_interface_t *swif;
174 u32 bi0;
175 vlib_buffer_t * b0;
176 udp_header_t * u0, *u1;
177 dhcpv6_header_t * h0; // client msg hdr
178 ip6_header_t * ip0, *ip1;
179 ip6_address_t _ia0, *ia0=&_ia0;
180 u32 next0;
181 u32 error0 = (u32) ~0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100182 dhcpv6_option_t *fwd_opt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 dhcpv6_relay_hdr_t *r1;
184 u16 len;
185 dhcpv6_int_id_t *id1;
186 dhcpv6_vss_t *vss1;
187 dhcpv6_client_mac_t *cmac; // client mac
188 ethernet_header_t * e_h0;
189 u8 client_src_mac[6];
190 vlib_buffer_free_list_t *fl;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100191
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 uword *p_vss;
193 u32 oui1=0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 dhcpv6_vss_info *vss;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100195
196
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 bi0 = from[0];
198 to_next[0] = bi0;
199 from += 1;
200 to_next += 1;
201 n_left_from -= 1;
202 n_left_to_next -= 1;
203
204 b0 = vlib_get_buffer (vm, bi0);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100205
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 h0 = vlib_buffer_get_current (b0);
Chris Luke194ebc52016-04-25 14:26:55 -0400207
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100208 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 * udp_local hands us the DHCPV6 header.
210 */
211 u0 = (void *)h0 -(sizeof(*u0));
212 ip0 = (void *)u0 -(sizeof(*ip0));
Chris Luke194ebc52016-04-25 14:26:55 -0400213 e_h0 = (void *)ip0 - ethernet_buffer_header_size(b0);
214
215 clib_memcpy(client_src_mac, e_h0->src_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
217 switch (h0->u.msg_type) {
218 case DHCPV6_MSG_SOLICIT:
219 case DHCPV6_MSG_REQUEST:
220 case DHCPV6_MSG_CONFIRM:
221 case DHCPV6_MSG_RENEW:
222 case DHCPV6_MSG_REBIND:
223 case DHCPV6_MSG_RELEASE:
224 case DHCPV6_MSG_DECLINE:
225 case DHCPV6_MSG_INFORMATION_REQUEST:
226 case DHCPV6_MSG_RELAY_FORW:
227 /* send to server */
228 break;
229 case DHCPV6_MSG_RELAY_REPL:
230 /* send to client */
231 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_SEND_TO_CLIENT;
232 error0 = 0;
233 pkts_to_client++;
234 goto do_enqueue;
235 default:
236 /* drop the packet */
237 pkts_wrong_msg_type++;
238 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
239 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
240 goto do_trace;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100241
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100243
244 /* Send to DHCPV6 server via the configured FIB */
245 rx_sw_if_index = sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
246 rx_fib_idx = im->fib_index_by_sw_if_index [rx_sw_if_index];
Neale Ranns3bd11d72017-01-16 18:09:40 +0100247
248 if (vec_len(dpm->dhcp6_server_index_by_rx_fib_index) <= rx_fib_idx)
249 goto no_server;
250
251 server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx];
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100252
253 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers,
254 server_idx)))
255 {
256 no_server:
257 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
258 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
259 pkts_no_server++;
260 goto do_trace;
261 }
262
263 server = pool_elt_at_index(dpm->dhcp6_servers, server_idx);
264 if (server->valid == 0)
265 goto no_server;
266
267 server_fib_idx = server->server_fib6_index;
268 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
269
270
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 /* relay-option header pointer */
272 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
273 fwd_opt = vlib_buffer_get_current(b0);
274 /* relay message header pointer */
275 vlib_buffer_advance(b0, -(sizeof(*r1)));
276 r1 = vlib_buffer_get_current(b0);
277
278 vlib_buffer_advance(b0, -(sizeof(*u1)));
279 u1 = vlib_buffer_get_current(b0);
280
281 vlib_buffer_advance(b0, -(sizeof(*ip1)));
282 ip1 = vlib_buffer_get_current(b0);
283
284 /* fill in all that rubbish... */
285 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
286 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
287
288 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
289 fwd_opt->length = clib_host_to_net_u16(len);
290 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
291
292 r1->hop_count++;
293 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
294
295 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
296 {
297 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
298 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
299 pkts_no_exceeding_max_hop++;
300 goto do_trace;
301 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100302
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
304 /* If relay-fwd and src address is site or global unicast address */
305 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
306 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
307 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
308 {
309 /* Set link address to zero */
310 r1->link_addr.as_u64[0] = 0;
311 r1->link_addr.as_u64[1] = 0;
312 goto link_address_set;
313 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100314
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 /* if receiving interface is unnumbered, use receiving interface
316 * IP address as link address, otherwise use the loopback interface
317 * IP address as link address.
318 */
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100319
320 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
322 sw_if_index = swif->unnumbered_sw_if_index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100323
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
325 if (ia0 == 0)
326 {
327 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
328 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
329 pkts_no_interface_address++;
330 goto do_trace;
331 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100332
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 copy_ip6_address(&r1->link_addr, ia0);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100334
335 link_address_set:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100337
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
339 > fl->n_data_bytes)
340 {
341 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
342 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
343 pkts_too_big++;
344 goto do_trace;
345 }
346
347 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
348 b0->current_length += (sizeof (*id1));
349
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100351 fib = ip6_fib_get (rx_fib_idx);
352
353 //TODO: Revisit if hash makes sense here
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 p_vss = hash_get (dpm->vss_index_by_vrf_id,
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100355 fib->table_id);
356 if (p_vss)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 {
358 vss = pool_elt_at_index (dpm->vss, p_vss[0]);
359 oui1 = vss->vpn_id.oui;
360 fib_id1 = vss->vpn_id.fib_id;
361 }
362
363 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100364 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
365 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
366
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 u1->length =0;
368 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
369 {
370 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
371 b0->current_length += (sizeof (*cmac));
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100372 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 sizeof(cmac->opt));
374 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
375 cmac->link_type = clib_host_to_net_u16(1); // ethernet
Damjan Marionf1213b82016-03-13 02:22:06 +0100376 clib_memcpy(cmac->data, client_src_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 u1->length += sizeof(*cmac);
378 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100379 if (server->insert_vss !=0 ) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
381 b0->current_length += (sizeof (*vss1));
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100382 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 sizeof(vss1->opt));
384 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
385 vss1->data[0] = 1; // type
386 vss1->data[1] = oui1>>16 & 0xff;
387 vss1->data[2] = oui1>>8 & 0xff;
388 vss1->data[3] = oui1 & 0xff;
389 vss1->data[4] = fib_id1>>24 & 0xff;
390 vss1->data[5] = fib_id1>>16 & 0xff;
391 vss1->data[6] = fib_id1>>8 & 0xff;
392 vss1->data[7] = fib_id1 & 0xff;
393 u1->length += sizeof(*vss1);
394 }
395
396 pkts_to_server++;
397 u1->checksum = 0;
398 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
399 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
400
401 u1->length =
402 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
403 sizeof(*r1) + sizeof(*fwd_opt) +
404 sizeof(*u1) + sizeof(*id1) + u1->length);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100405
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 memset(ip1, 0, sizeof(*ip1));
407 ip1->ip_version_traffic_class_and_flow_label = 0x60;
408 ip1->payload_length = u1->length;
409 ip1->protocol = PROTO_UDP;
410 ip1->hop_limit = HOP_COUNT_LIMIT;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100411 src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ?
412 &server->dhcp6_server : &dpm->all_dhcpv6_server_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 copy_ip6_address(&ip1->dst_address, src);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100414
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
416 ia0 = ip6_interface_first_global_or_site_address
417 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
418
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100419 src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ?
420 &server->dhcp6_src_address : ia0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 if (ia0 == 0)
422 {
423 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
424 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
425 pkts_no_src_address++;
426 goto do_trace;
427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100429 copy_ip6_address (&ip1->src_address, src);
430
431
432 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 &bogus_length);
434 ASSERT(bogus_length == 0);
435
436 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
437
438 do_trace:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100439 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100441 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 b0, sizeof (*tr));
443 tr->which = 0; /* to server */
444 tr->error = error0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100445 tr->original_sw_if_index = rx_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 tr->sw_if_index = sw_if_index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100447 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
448 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 }
450
451 do_enqueue:
452 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
453 to_next, n_left_to_next,
454 bi0, next0);
455 }
456
457 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
458 }
459
460 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
461 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
462 pkts_to_client);
463 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
464 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
465 pkts_to_server);
466 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
467 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
468 pkts_no_interface_address);
469 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
470 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
471 pkts_wrong_msg_type);
472 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
473 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
474 pkts_no_src_address);
475 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
476 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
477 pkts_too_big);
478 return from_frame->n_vectors;
479}
480
481VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
482 .function = dhcpv6_proxy_to_server_input,
483 .name = "dhcpv6-proxy-to-server",
484 /* Takes a vector of packets. */
485 .vector_size = sizeof (u32),
486
487 .n_errors = DHCPV6_PROXY_N_ERROR,
488 .error_strings = dhcpv6_proxy_error_strings,
489
490 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
491 .next_nodes = {
492#define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
493 foreach_dhcpv6_proxy_to_server_input_next
494#undef _
495 },
496
497 .format_buffer = format_dhcpv6_proxy_header_with_length,
498 .format_trace = format_dhcpv6_proxy_trace,
499#if 0
500 .unformat_buffer = unformat_dhcpv6_proxy_header,
501#endif
502};
503
504static uword
505dhcpv6_proxy_to_client_input (vlib_main_t * vm,
506 vlib_node_runtime_t * node,
507 vlib_frame_t * from_frame)
508{
509
510 u32 n_left_from, * from;
511 ethernet_main_t *em = ethernet_get_main (vm);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100512 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
513 dhcpv6_server_t * server;
514 vnet_main_t * vnm = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 int bogus_length;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100516
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 from = vlib_frame_vector_args (from_frame);
518 n_left_from = from_frame->n_vectors;
519
520 while (n_left_from > 0)
521 {
522 u32 bi0;
523 vlib_buffer_t * b0;
524 udp_header_t * u0, *u1=0;
525 dhcpv6_relay_hdr_t * h0;
526 ip6_header_t * ip1 = 0, *ip0;
527 ip6_address_t _ia0, * ia0 = &_ia0;
528 ip6_address_t client_address;
529 ethernet_interface_t *ei0;
530 ethernet_header_t *mac0;
531 vnet_hw_interface_t *hi0;
532 vlib_frame_t *f0;
533 u32 * to_next0;
534 u32 sw_if_index = ~0;
535 u32 original_sw_if_index = ~0;
536 vnet_sw_interface_t *si0;
537 u32 error0 = (u32)~0;
538 vnet_sw_interface_t *swif;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200539 dhcpv6_option_t *r0 = 0, *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 u16 len = 0;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200541 u8 interface_opt_flag = 0;
542 u8 relay_msg_opt_flag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 ip6_fib_t * svr_fib;
544 ip6_main_t * im = &ip6_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100545 u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
547 bi0 = from[0];
548 from += 1;
549 n_left_from -= 1;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200550
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 b0 = vlib_get_buffer (vm, bi0);
552 h0 = vlib_buffer_get_current (b0);
553
554 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
555 {
556 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
557
558 drop_packet:
559 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
560 error0, 1);
561
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100562 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 to_next0 = vlib_frame_vector_args (f0);
564 to_next0[0] = bi0;
565 f0->n_vectors = 1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100566 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567 goto do_trace;
568 }
569 /* hop count seems not need to be checked */
570 if (HOP_COUNT_LIMIT < h0->hop_count)
571 {
572 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
573 goto drop_packet;
574 }
575 u0 = (void *)h0 -(sizeof(*u0));
576 ip0 = (void *)u0 -(sizeof(*ip0));
Wojciech Dec96e6f952016-10-20 11:25:37 +0200577
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 vlib_buffer_advance (b0, sizeof(*h0));
Wojciech Dec96e6f952016-10-20 11:25:37 +0200579 o = vlib_buffer_get_current (b0);
580
581 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
582 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
583 Currently assuming no other options need to be processed
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 The interface-ID is the FIB number we need
585 to track down the client-facing interface */
Wojciech Dec96e6f952016-10-20 11:25:37 +0200586
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
588 {
589 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
590 {
Wojciech Dec96e6f952016-10-20 11:25:37 +0200591 interface_opt_flag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
593 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100594 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
595 {
596 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
597 goto drop_packet;
598 }
Wojciech Dec96e6f952016-10-20 11:25:37 +0200599 }
600 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
601 {
602 relay_msg_opt_flag = 1;
603 r0 = vlib_buffer_get_current (b0);
604 }
605 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
606 break;
607 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
609 }
Wojciech Dec96e6f952016-10-20 11:25:37 +0200610
611 if ((relay_msg_opt_flag == 0) || (r0 == 0))
612 {
613 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
614 goto drop_packet;
615 }
616
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 if ((u32)~0 == sw_if_index)
618 {
619 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
620 goto drop_packet;
621 }
622
Wojciech Dec96e6f952016-10-20 11:25:37 +0200623 //Advance buffer to start of encapsulated DHCPv6 message
624 vlib_buffer_advance (b0, sizeof(*r0));
625
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100626 client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index];
627 if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
628 server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx];
629 else
630 server_idx = 0;
631
632 if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx)))
633 {
634 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
635 goto drop_packet;
636 }
637
638 server = pool_elt_at_index (dm->dhcp6_servers, server_idx);
639 if (server->valid == 0)
640 {
641 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
642 goto drop_packet;
643 }
644
645
646 server_fib_idx = im->fib_index_by_sw_if_index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100648 svr_fib = ip6_fib_get (server_fib_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 svr_fib_id = svr_fib->table_id;
650
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100651 if (svr_fib_id != server->server_fib6_index ||
652 ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] ||
653 ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 {
655 //drop packet if not from server with configured address or FIB
656 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
657 goto drop_packet;
658 }
659
Wojciech Dec96e6f952016-10-20 11:25:37 +0200660 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 = sw_if_index;
662
663 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
664 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
665 sw_if_index = swif->unnumbered_sw_if_index;
666
Wojciech Dec96e6f952016-10-20 11:25:37 +0200667
668 /*
669 * udp_local hands us the DHCPV6 header, need udp hdr,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 * ip hdr to relay to client
671 */
672 vlib_buffer_advance (b0, -(sizeof(*u1)));
673 u1 = vlib_buffer_get_current (b0);
674
675 vlib_buffer_advance (b0, -(sizeof(*ip1)));
676 ip1 = vlib_buffer_get_current (b0);
677
678 copy_ip6_address(&client_address, &h0->peer_addr);
679
680 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
681 if (ia0 == 0)
682 {
683 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
684 goto drop_packet;
685 }
686
687 len = clib_net_to_host_u16(r0->length);
688 memset(ip1, 0, sizeof(*ip1));
689 copy_ip6_address(&ip1->dst_address, &client_address);
690 u1->checksum = 0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100691 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
693 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
694
695 ip1->ip_version_traffic_class_and_flow_label =
696 ip0->ip_version_traffic_class_and_flow_label &
697 0x00000fff;
698 ip1->payload_length = u1->length;
699 ip1->protocol = PROTO_UDP;
700 ip1->hop_limit = HOP_COUNT_LIMIT;
701 copy_ip6_address(&ip1->src_address, ia0);
702
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100703 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 &bogus_length);
705 ASSERT(bogus_length == 0);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100706
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
708 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
709 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
710 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
711
712 mac0 = vlib_buffer_get_current (b0);
713
714 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
715 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100716 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
718 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
719 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
720
721 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
722 {
723 u32 * vlan_tag = (u32 *)(mac0+1);
724 u32 tmp;
725 tmp = (si0->sub.id << 16) | 0x0800;
726 *vlan_tag = clib_host_to_net_u32 (tmp);
727 }
728
729 /* $$$ consider adding a dynamic next to the graph node, for performance */
730 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
731 to_next0 = vlib_frame_vector_args (f0);
732 to_next0[0] = bi0;
733 f0->n_vectors = 1;
734 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
735
736 do_trace:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100737 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100739 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740 b0, sizeof (*tr));
741 tr->which = 1; /* to client */
742 if (ia0)
743 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
744 tr->error = error0;
745 tr->original_sw_if_index = original_sw_if_index;
746 tr->sw_if_index = sw_if_index;
747 }
748 }
749 return from_frame->n_vectors;
750
751}
752
753VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
754 .function = dhcpv6_proxy_to_client_input,
755 .name = "dhcpv6-proxy-to-client",
756 /* Takes a vector of packets. */
757 .vector_size = sizeof (u32),
758
759 .n_errors = DHCPV6_PROXY_N_ERROR,
760 .error_strings = dhcpv6_proxy_error_strings,
761 .format_buffer = format_dhcpv6_proxy_header_with_length,
762 .format_trace = format_dhcpv6_proxy_trace,
763#if 0
764 .unformat_buffer = unformat_dhcpv6_proxy_header,
765#endif
766};
767
768clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
769{
770 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
771 vlib_node_t * error_drop_node;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100772 dhcpv6_server_t * server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
774 dm->vlib_main = vm;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100775 dm->vnet_main = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
777 dm->error_drop_node_index = error_drop_node->index;
778
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100779 dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword));
780
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 /* RFC says this is the dhcpv6 server address */
782 dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
783 dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
784
785 /* RFC says this is the server and agent address */
786 dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
787 dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
788
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100789 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
790 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100792 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100794
795 /* Create the default server, don't mark it valid */
796 pool_get (dm->dhcp6_servers, server);
797 memset (server, 0, sizeof (*server));
798
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 return 0;
800}
801
802VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
803
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100804/* Old API, manipulates a single server (only) shared by all Rx VRFs */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
806 u32 fib_id, int insert_vss, int is_del)
807{
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100808 return dhcpv6_proxy_set_server_2 (addr, src_address,
809 0, fib_id,
810 insert_vss, is_del);
811}
812
813int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address,
814 u32 rx_fib_id, u32 server_fib_id,
815 int insert_vss, int is_del)
816{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100818 dhcpv6_server_t * server = 0;
819 u32 server_fib_index = 0;
820 u32 rx_fib_index = 0;
821
822 rx_fib_index = ip6_fib_table_find_or_create_and_lock(rx_fib_id);
823 server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id);
824
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 if (is_del)
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100826 {
827
828 if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index))
829 return VNET_API_ERROR_NO_SUCH_ENTRY;
830
831 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
832
833 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0;
834 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
835 memset (server, 0, sizeof (*server));
836 pool_put (dm->dhcp6_servers, server);
837 return 0;
838 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
840 if (addr->as_u64[0] == 0 &&
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100841 addr->as_u64[1] == 0 )
842 return VNET_API_ERROR_INVALID_DST_ADDRESS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100844 if (src_address->as_u64[0] == 0 &&
845 src_address->as_u64[1] == 0)
846 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
847
848 if (rx_fib_id == 0)
849 {
850 server = pool_elt_at_index (dm->dhcp6_servers, 0);
851
852 goto initialize_it;
853 }
854
855 if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
856 {
857 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
858 if (server_fib_index != 0)
859 {
860 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
861 goto initialize_it;
862 }
863 }
864
865 /*Allocate a new server*/
866 pool_get (dm->dhcp6_servers, server);
867
868 initialize_it:
869
870 copy_ip6_address(&server->dhcp6_server, addr);
871 copy_ip6_address(&server->dhcp6_src_address, src_address);
872 server->server_fib6_index = server_fib_index;
873 server->valid = 1;
874 server->insert_vss = insert_vss;
875
876 vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index);
877 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] =
878 server - dm->dhcp6_servers;
879
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 return 0;
881}
882
883static clib_error_t *
884dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
885 unformat_input_t * input,
886 vlib_cli_command_t * cmd)
887{
888 ip6_address_t addr, src_addr;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100889 int set_server = 0, set_src_address = 0;
890 u32 rx_fib_id = 0, server_fib_id = 0;
891 int is_del = 0, add_vss = 0;
892
893 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100895 if (unformat (input, "server %U",
896 unformat_ip6_address, &addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897 set_server = 1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100898 else if (unformat(input, "src-address %U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 unformat_ip6_address, &src_addr))
900 set_src_address =1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100901 else if (unformat (input, "server-fib-id %d", &server_fib_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 ;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100903 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
904 ;
905 else if (unformat (input, "add-vss-option")
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 || unformat (input, "insert-option"))
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100907 add_vss = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 else if (unformat (input, "delete") ||
909 unformat (input, "del"))
910 is_del = 1;
911 else
912 break;
913 }
914
915 if (is_del || (set_server && set_src_address))
916 {
917 int rv;
918
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100919 rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id,
920 server_fib_id, add_vss, is_del);
921
922 //TODO: Complete the errors
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 switch (rv)
924 {
925 case 0:
926 return 0;
927
928 case -1:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100929 return clib_error_return (0, "FIB id %d does not exist", server_fib_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
931 default:
932 return clib_error_return (0, "BUG: rv %d", rv);
933 }
934 }
935 else
936 return clib_error_return (0, "parse error`%U'",
937 format_unformat_error, input);
938}
939
940VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
941 .path = "set dhcpv6 proxy",
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100942 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
943 "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944 .function = dhcpv6_proxy_set_command_fn,
945};
946
947u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
948{
949 dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100950 dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *);
951 u32 rx_fib_index = va_arg (*args, u32);
952 ip6_fib_t * rx_fib, * server_fib;
953 u32 server_fib_id = (u32)~0, rx_fib_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
955 if (dm == 0)
956 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100957 s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address",
958 "Server FIB", "RX FIB", "Insert VSS Option");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 return s;
960 }
961
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100962 server_fib = ip6_fib_get(server->server_fib6_index);
963 if (server_fib)
964 server_fib_id= server_fib->table_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100966 rx_fib= ip6_fib_get(rx_fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100968 if (rx_fib)
969 rx_fib_id = rx_fib->table_id;
970
971 s = format (s, "%=40U%=40U%=14u%=14u%=20s",
972 format_ip6_address, &server->dhcp6_server,
973 format_ip6_address, &server->dhcp6_src_address,
974 server_fib_id, rx_fib_id,
975 server->insert_vss ? "yes" : "no");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976 return s;
977}
978
979static clib_error_t *
980dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
981 unformat_input_t * input,
982 vlib_cli_command_t * cmd)
983{
984 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100985 ip6_main_t * im = &ip6_main;
986 int i;
987 u32 server_index;
988 dhcpv6_server_t * server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100990 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */,
991 0, 0);
992 for (i = 0; i < vec_len (im->fibs); i++)
993 {
994 if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
995 server_index = dm->dhcp6_server_index_by_rx_fib_index[i];
996 else
997 server_index = 0;
998 server = pool_elt_at_index (dm->dhcp6_servers, server_index);
999 if (server->valid)
1000 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm,
1001 server, i);
1002 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 return 0;
1004}
1005
1006VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1007 .path = "show dhcpv6 proxy",
1008 .short_help = "Display dhcpv6 proxy info",
1009 .function = dhcpv6_proxy_show_command_fn,
1010};
1011
1012int dhcpv6_proxy_set_vss(u32 tbl_id,
1013 u32 oui,
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001014 u32 fib_id,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 int is_del)
1016{
1017 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1018 u32 old_oui, old_fib_id;
1019 uword *p;
1020 dhcpv6_vss_info *v;
1021
1022 p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
1023
1024 if (p) {
1025 v = pool_elt_at_index (dm->vss, p[0]);
1026 if (!v)
1027 return VNET_API_ERROR_NO_SUCH_FIB;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001028
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 old_oui = v->vpn_id.oui;
1030 old_fib_id = v->vpn_id.fib_id;
1031
1032 if (is_del)
1033 {
1034 if (old_oui == oui &&
1035 old_fib_id == fib_id )
1036 {
1037 pool_put(dm->vss, v);
1038 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1039 return 0;
1040 }
1041 else
1042 return VNET_API_ERROR_NO_SUCH_ENTRY;
1043 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001044
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 pool_put(dm->vss, v);
1046 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1047 } else if (is_del)
1048 return VNET_API_ERROR_NO_SUCH_ENTRY;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001049
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 pool_get (dm->vss, v);
1051 memset (v, ~0, sizeof (*v));
1052 v->vpn_id.fib_id = fib_id;
1053 v->vpn_id.oui = oui;
1054 hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001055
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 return 0;
1057}
1058
1059
1060static clib_error_t *
1061dhcpv6_vss_command_fn (vlib_main_t * vm,
1062 unformat_input_t * input,
1063 vlib_cli_command_t * cmd)
1064{
1065 int is_del = 0, got_new_vss=0;
1066 u32 oui=0;
1067 u32 fib_id=0, tbl_id=~0;
1068
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001069 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070 {
1071 if (unformat (input, "oui %d", &oui))
1072 got_new_vss = 1;
1073 else if (unformat (input, "vpn-id %d", &fib_id))
1074 got_new_vss = 1;
1075 else if (unformat (input, "table %d", &tbl_id))
1076 got_new_vss = 1;
1077 else if (unformat(input, "delete") || unformat(input, "del"))
1078 is_del = 1;
1079 else
1080 break;
1081 }
1082
1083 if (tbl_id ==~0)
1084 return clib_error_return (0, "no table ID specified.");
1085
1086 if (is_del || got_new_vss)
1087 {
1088 int rv;
1089
1090 rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
1091 switch (rv)
1092 {
1093 case 0:
1094 return 0;
1095
1096 case VNET_API_ERROR_NO_SUCH_FIB:
1097 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1098 oui, fib_id, tbl_id);
1099
1100 case VNET_API_ERROR_NO_SUCH_ENTRY:
1101 return clib_error_return (0, "vss for table %d not found in pool.",
1102 tbl_id);
1103
1104 default:
1105 return clib_error_return (0, "BUG: rv %d", rv);
1106 }
1107 }
1108 else
1109 return clib_error_return (0, "parse error`%U'",
1110 format_unformat_error, input);
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001111
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112}
1113
1114VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1115 .path = "set dhcpv6 vss",
1116 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1117 .function = dhcpv6_vss_command_fn,
1118};
1119
1120static clib_error_t *
1121dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1122 unformat_input_t * input,
1123 vlib_cli_command_t * cmd)
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001124
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125{
1126 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1127 dhcpv6_vss_info *v;
1128 u32 oui;
1129 u32 fib_id;
1130 u32 tbl_id;
1131 uword index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001132
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
1134 hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
1135 ({
1136 v = pool_elt_at_index (dm->vss, index);
1137 oui = v->vpn_id.oui;
1138 fib_id = v->vpn_id.fib_id;
1139 vlib_cli_output (vm, "%=6d%=6d%=12d",
1140 tbl_id, oui, fib_id);
1141 }));
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001142
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 return 0;
1144}
1145
1146VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1147 .path = "show dhcpv6 vss",
1148 .short_help = "show dhcpv6 VSS",
1149 .function = dhcpv6_vss_show_command_fn,
1150};
1151
1152static clib_error_t *
1153dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1154 unformat_input_t * input,
1155 vlib_cli_command_t * cmd)
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001156
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157{
1158 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001159 vnet_main_t *vnm = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 u32 sw_if_index0=0, sw_if_index;
1161 ip6_address_t *ia0;
1162 vnet_sw_interface_t *swif;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001163
1164 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 {
1166
1167 if (unformat(input, "%U",
1168 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1169 {
1170 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1171 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1172 swif->unnumbered_sw_if_index : sw_if_index0;
1173 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1174 if (ia0)
1175 {
1176 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001177
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 vlib_cli_output (vm, "%=20U%=48U",
1179 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1180 format_ip6_address, ia0);
1181 } else
1182 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001183 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 } else
1185 break;
1186 }
1187
1188 return 0;
1189}
1190
1191VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1192 .path = "show dhcpv6 link-address interface",
1193 .short_help = "show dhcpv6 link-address interface <interface>",
1194 .function = dhcpv6_link_address_show_command_fn,
1195};