blob: 40df35cd0597d52f0f05c58cea3891485211e437 [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];
247 server_idx = dpm->dhcp6_server_index_by_rx_fib_index[rx_fib_idx];
248
249 if (PREDICT_FALSE (pool_is_free_index (dpm->dhcp6_servers,
250 server_idx)))
251 {
252 no_server:
253 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
254 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
255 pkts_no_server++;
256 goto do_trace;
257 }
258
259 server = pool_elt_at_index(dpm->dhcp6_servers, server_idx);
260 if (server->valid == 0)
261 goto no_server;
262
263 server_fib_idx = server->server_fib6_index;
264 vnet_buffer(b0)->sw_if_index[VLIB_TX] = server_fib_idx;
265
266
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 /* relay-option header pointer */
268 vlib_buffer_advance(b0, -(sizeof(*fwd_opt)));
269 fwd_opt = vlib_buffer_get_current(b0);
270 /* relay message header pointer */
271 vlib_buffer_advance(b0, -(sizeof(*r1)));
272 r1 = vlib_buffer_get_current(b0);
273
274 vlib_buffer_advance(b0, -(sizeof(*u1)));
275 u1 = vlib_buffer_get_current(b0);
276
277 vlib_buffer_advance(b0, -(sizeof(*ip1)));
278 ip1 = vlib_buffer_get_current(b0);
279
280 /* fill in all that rubbish... */
281 len = clib_net_to_host_u16(u0->length) - sizeof(udp_header_t);
282 copy_ip6_address(&r1->peer_addr, &ip0->src_address);
283
284 r1->msg_type = DHCPV6_MSG_RELAY_FORW;
285 fwd_opt->length = clib_host_to_net_u16(len);
286 fwd_opt->option = clib_host_to_net_u16(DHCPV6_OPTION_RELAY_MSG);
287
288 r1->hop_count++;
289 r1->hop_count = (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW) ? 0 : r1->hop_count;
290
291 if (PREDICT_FALSE(r1->hop_count >= HOP_COUNT_LIMIT))
292 {
293 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
294 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
295 pkts_no_exceeding_max_hop++;
296 goto do_trace;
297 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100298
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
300 /* If relay-fwd and src address is site or global unicast address */
301 if (h0->u.msg_type == DHCPV6_MSG_RELAY_FORW &&
302 ((ip0->src_address.as_u8[0] & 0xe0) == 0x20 ||
303 (ip0->src_address.as_u8[0] & 0xfe) == 0xfc))
304 {
305 /* Set link address to zero */
306 r1->link_addr.as_u64[0] = 0;
307 r1->link_addr.as_u64[1] = 0;
308 goto link_address_set;
309 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100310
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 /* if receiving interface is unnumbered, use receiving interface
312 * IP address as link address, otherwise use the loopback interface
313 * IP address as link address.
314 */
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100315
316 swif = vnet_get_sw_interface (vnm, rx_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
318 sw_if_index = swif->unnumbered_sw_if_index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100319
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 ia0 = ip6_interface_first_global_or_site_address(&ip6_main, sw_if_index);
321 if (ia0 == 0)
322 {
323 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
324 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
325 pkts_no_interface_address++;
326 goto do_trace;
327 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100328
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 copy_ip6_address(&r1->link_addr, ia0);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100330
331 link_address_set:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 fl = vlib_buffer_get_free_list (vm, b0->free_list_index);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 if ((b0->current_length+sizeof(*id1)+sizeof(*vss1)+sizeof(*cmac))
335 > fl->n_data_bytes)
336 {
337 error0 = DHCPV6_PROXY_ERROR_PKT_TOO_BIG;
338 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
339 pkts_too_big++;
340 goto do_trace;
341 }
342
343 id1 = (dhcpv6_int_id_t *) (((uword) ip1) + b0->current_length);
344 b0->current_length += (sizeof (*id1));
345
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100347 fib = ip6_fib_get (rx_fib_idx);
348
349 //TODO: Revisit if hash makes sense here
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 p_vss = hash_get (dpm->vss_index_by_vrf_id,
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100351 fib->table_id);
352 if (p_vss)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 {
354 vss = pool_elt_at_index (dpm->vss, p_vss[0]);
355 oui1 = vss->vpn_id.oui;
356 fib_id1 = vss->vpn_id.fib_id;
357 }
358
359 id1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_INTERFACE_ID);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100360 id1->opt.length = clib_host_to_net_u16(sizeof(rx_sw_if_index));
361 id1->int_idx = clib_host_to_net_u32(rx_sw_if_index);
362
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 u1->length =0;
364 if (h0->u.msg_type != DHCPV6_MSG_RELAY_FORW)
365 {
366 cmac = (dhcpv6_client_mac_t *) (((uword) ip1) + b0->current_length);
367 b0->current_length += (sizeof (*cmac));
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100368 cmac->opt.length =clib_host_to_net_u16(sizeof(*cmac) -
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 sizeof(cmac->opt));
370 cmac->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_CLIENT_LINK_LAYER_ADDRESS);
371 cmac->link_type = clib_host_to_net_u16(1); // ethernet
Damjan Marionf1213b82016-03-13 02:22:06 +0100372 clib_memcpy(cmac->data, client_src_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 u1->length += sizeof(*cmac);
374 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100375 if (server->insert_vss !=0 ) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 vss1 = (dhcpv6_vss_t *) (((uword) ip1) + b0->current_length);
377 b0->current_length += (sizeof (*vss1));
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100378 vss1->opt.length =clib_host_to_net_u16(sizeof(*vss1) -
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 sizeof(vss1->opt));
380 vss1->opt.option = clib_host_to_net_u16(DHCPV6_OPTION_VSS);
381 vss1->data[0] = 1; // type
382 vss1->data[1] = oui1>>16 & 0xff;
383 vss1->data[2] = oui1>>8 & 0xff;
384 vss1->data[3] = oui1 & 0xff;
385 vss1->data[4] = fib_id1>>24 & 0xff;
386 vss1->data[5] = fib_id1>>16 & 0xff;
387 vss1->data[6] = fib_id1>>8 & 0xff;
388 vss1->data[7] = fib_id1 & 0xff;
389 u1->length += sizeof(*vss1);
390 }
391
392 pkts_to_server++;
393 u1->checksum = 0;
394 u1->src_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_client);
395 u1->dst_port = clib_host_to_net_u16(UDP_DST_PORT_dhcpv6_to_server);
396
397 u1->length =
398 clib_host_to_net_u16( clib_net_to_host_u16(fwd_opt->length) +
399 sizeof(*r1) + sizeof(*fwd_opt) +
400 sizeof(*u1) + sizeof(*id1) + u1->length);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100401
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 memset(ip1, 0, sizeof(*ip1));
403 ip1->ip_version_traffic_class_and_flow_label = 0x60;
404 ip1->payload_length = u1->length;
405 ip1->protocol = PROTO_UDP;
406 ip1->hop_limit = HOP_COUNT_LIMIT;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100407 src = (server->dhcp6_server.as_u64[0] || server->dhcp6_server.as_u64[1]) ?
408 &server->dhcp6_server : &dpm->all_dhcpv6_server_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 copy_ip6_address(&ip1->dst_address, src);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100410
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
412 ia0 = ip6_interface_first_global_or_site_address
413 (&ip6_main, vnet_buffer(b0)->sw_if_index[VLIB_RX]);
414
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100415 src = (server->dhcp6_src_address.as_u64[0] || server->dhcp6_src_address.as_u64[1]) ?
416 &server->dhcp6_src_address : ia0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 if (ia0 == 0)
418 {
419 error0 = DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS;
420 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_DROP;
421 pkts_no_src_address++;
422 goto do_trace;
423 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100425 copy_ip6_address (&ip1->src_address, src);
426
427
428 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 &bogus_length);
430 ASSERT(bogus_length == 0);
431
432 next0 = DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP;
433
434 do_trace:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100435 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100437 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 b0, sizeof (*tr));
439 tr->which = 0; /* to server */
440 tr->error = error0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100441 tr->original_sw_if_index = rx_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 tr->sw_if_index = sw_if_index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100443 if (DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_LOOKUP == next0)
444 copy_ip6_address((ip6_address_t *)&tr->packet_data[0], &server->dhcp6_server);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 }
446
447 do_enqueue:
448 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
449 to_next, n_left_to_next,
450 bi0, next0);
451 }
452
453 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
454 }
455
456 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
457 DHCPV6_PROXY_ERROR_RELAY_TO_CLIENT,
458 pkts_to_client);
459 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
460 DHCPV6_PROXY_ERROR_RELAY_TO_SERVER,
461 pkts_to_server);
462 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
463 DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS,
464 pkts_no_interface_address);
465 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
466 DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE,
467 pkts_wrong_msg_type);
468 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
469 DHCPV6_PROXY_ERROR_NO_SRC_ADDRESS,
470 pkts_no_src_address);
471 vlib_node_increment_counter (vm, dhcpv6_proxy_to_server_node.index,
472 DHCPV6_PROXY_ERROR_PKT_TOO_BIG,
473 pkts_too_big);
474 return from_frame->n_vectors;
475}
476
477VLIB_REGISTER_NODE (dhcpv6_proxy_to_server_node) = {
478 .function = dhcpv6_proxy_to_server_input,
479 .name = "dhcpv6-proxy-to-server",
480 /* Takes a vector of packets. */
481 .vector_size = sizeof (u32),
482
483 .n_errors = DHCPV6_PROXY_N_ERROR,
484 .error_strings = dhcpv6_proxy_error_strings,
485
486 .n_next_nodes = DHCPV6_PROXY_TO_SERVER_INPUT_N_NEXT,
487 .next_nodes = {
488#define _(s,n) [DHCPV6_PROXY_TO_SERVER_INPUT_NEXT_##s] = n,
489 foreach_dhcpv6_proxy_to_server_input_next
490#undef _
491 },
492
493 .format_buffer = format_dhcpv6_proxy_header_with_length,
494 .format_trace = format_dhcpv6_proxy_trace,
495#if 0
496 .unformat_buffer = unformat_dhcpv6_proxy_header,
497#endif
498};
499
500static uword
501dhcpv6_proxy_to_client_input (vlib_main_t * vm,
502 vlib_node_runtime_t * node,
503 vlib_frame_t * from_frame)
504{
505
506 u32 n_left_from, * from;
507 ethernet_main_t *em = ethernet_get_main (vm);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100508 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
509 dhcpv6_server_t * server;
510 vnet_main_t * vnm = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 int bogus_length;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100512
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 from = vlib_frame_vector_args (from_frame);
514 n_left_from = from_frame->n_vectors;
515
516 while (n_left_from > 0)
517 {
518 u32 bi0;
519 vlib_buffer_t * b0;
520 udp_header_t * u0, *u1=0;
521 dhcpv6_relay_hdr_t * h0;
522 ip6_header_t * ip1 = 0, *ip0;
523 ip6_address_t _ia0, * ia0 = &_ia0;
524 ip6_address_t client_address;
525 ethernet_interface_t *ei0;
526 ethernet_header_t *mac0;
527 vnet_hw_interface_t *hi0;
528 vlib_frame_t *f0;
529 u32 * to_next0;
530 u32 sw_if_index = ~0;
531 u32 original_sw_if_index = ~0;
532 vnet_sw_interface_t *si0;
533 u32 error0 = (u32)~0;
534 vnet_sw_interface_t *swif;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200535 dhcpv6_option_t *r0 = 0, *o;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 u16 len = 0;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200537 u8 interface_opt_flag = 0;
538 u8 relay_msg_opt_flag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 ip6_fib_t * svr_fib;
540 ip6_main_t * im = &ip6_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100541 u32 server_fib_idx, svr_fib_id, client_fib_idx, server_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
543 bi0 = from[0];
544 from += 1;
545 n_left_from -= 1;
Wojciech Dec96e6f952016-10-20 11:25:37 +0200546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 b0 = vlib_get_buffer (vm, bi0);
548 h0 = vlib_buffer_get_current (b0);
549
550 if (DHCPV6_MSG_RELAY_REPL != h0->msg_type)
551 {
552 error0 = DHCPV6_PROXY_ERROR_WRONG_MESSAGE_TYPE;
553
554 drop_packet:
555 vlib_node_increment_counter (vm, dhcpv6_proxy_to_client_node.index,
556 error0, 1);
557
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100558 f0 = vlib_get_frame_to_node (vm, dm->error_drop_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 to_next0 = vlib_frame_vector_args (f0);
560 to_next0[0] = bi0;
561 f0->n_vectors = 1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100562 vlib_put_frame_to_node (vm, dm->error_drop_node_index, f0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 goto do_trace;
564 }
565 /* hop count seems not need to be checked */
566 if (HOP_COUNT_LIMIT < h0->hop_count)
567 {
568 error0 = DHCPV6_RELAY_PKT_DROP_MAX_HOPS;
569 goto drop_packet;
570 }
571 u0 = (void *)h0 -(sizeof(*u0));
572 ip0 = (void *)u0 -(sizeof(*ip0));
Wojciech Dec96e6f952016-10-20 11:25:37 +0200573
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 vlib_buffer_advance (b0, sizeof(*h0));
Wojciech Dec96e6f952016-10-20 11:25:37 +0200575 o = vlib_buffer_get_current (b0);
576
577 /* Parse through TLVs looking for option 18 (DHCPV6_OPTION_INTERFACE_ID)
578 _and_ option 9 (DHCPV6_OPTION_RELAY_MSG) option which must be there.
579 Currently assuming no other options need to be processed
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 The interface-ID is the FIB number we need
581 to track down the client-facing interface */
Wojciech Dec96e6f952016-10-20 11:25:37 +0200582
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 while ((u8 *) o < (b0->data + b0->current_data + b0->current_length))
584 {
585 if (DHCPV6_OPTION_INTERFACE_ID == clib_net_to_host_u16(o->option))
586 {
Wojciech Dec96e6f952016-10-20 11:25:37 +0200587 interface_opt_flag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 if (clib_net_to_host_u16(o->length) == sizeof(sw_if_index))
589 sw_if_index = clib_net_to_host_u32(((dhcpv6_int_id_t*)o)->int_idx);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100590 if (sw_if_index >= vec_len (im->fib_index_by_sw_if_index))
591 {
592 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
593 goto drop_packet;
594 }
Wojciech Dec96e6f952016-10-20 11:25:37 +0200595 }
596 if (DHCPV6_OPTION_RELAY_MSG == clib_net_to_host_u16(o->option))
597 {
598 relay_msg_opt_flag = 1;
599 r0 = vlib_buffer_get_current (b0);
600 }
601 if ((relay_msg_opt_flag == 1) && (interface_opt_flag == 1))
602 break;
603 vlib_buffer_advance (b0, sizeof(*o) + clib_net_to_host_u16(o->length));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604 o = (dhcpv6_option_t *) (((uword) o) + clib_net_to_host_u16(o->length) + sizeof(*o));
605 }
Wojciech Dec96e6f952016-10-20 11:25:37 +0200606
607 if ((relay_msg_opt_flag == 0) || (r0 == 0))
608 {
609 error0 = DHCPV6_PROXY_ERROR_NO_RELAY_MESSAGE_OPTION;
610 goto drop_packet;
611 }
612
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 if ((u32)~0 == sw_if_index)
614 {
615 error0 = DHCPV6_PROXY_ERROR_NO_CIRCUIT_ID_OPTION;
616 goto drop_packet;
617 }
618
Wojciech Dec96e6f952016-10-20 11:25:37 +0200619 //Advance buffer to start of encapsulated DHCPv6 message
620 vlib_buffer_advance (b0, sizeof(*r0));
621
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100622 client_fib_idx = im->fib_index_by_sw_if_index[sw_if_index];
623 if (client_fib_idx < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
624 server_idx = dm->dhcp6_server_index_by_rx_fib_index[client_fib_idx];
625 else
626 server_idx = 0;
627
628 if (PREDICT_FALSE (pool_is_free_index (dm->dhcp6_servers, server_idx)))
629 {
630 error0 = DHCPV6_PROXY_ERROR_WRONG_INTERFACE_ID_OPTION;
631 goto drop_packet;
632 }
633
634 server = pool_elt_at_index (dm->dhcp6_servers, server_idx);
635 if (server->valid == 0)
636 {
637 error0 = DHCPV6_PROXY_ERROR_NO_SERVER;
638 goto drop_packet;
639 }
640
641
642 server_fib_idx = im->fib_index_by_sw_if_index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 [vnet_buffer(b0)->sw_if_index[VLIB_RX]];
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100644 svr_fib = ip6_fib_get (server_fib_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 svr_fib_id = svr_fib->table_id;
646
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100647 if (svr_fib_id != server->server_fib6_index ||
648 ip0->src_address.as_u64[0] != server->dhcp6_server.as_u64[0] ||
649 ip0->src_address.as_u64[1] != server->dhcp6_server.as_u64[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 {
651 //drop packet if not from server with configured address or FIB
652 error0 = DHCPV6_PROXY_ERROR_BAD_SVR_FIB_OR_ADDRESS;
653 goto drop_packet;
654 }
655
Wojciech Dec96e6f952016-10-20 11:25:37 +0200656 vnet_buffer (b0)->sw_if_index[VLIB_TX] = original_sw_if_index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 = sw_if_index;
658
659 swif = vnet_get_sw_interface (vnm, original_sw_if_index);
660 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
661 sw_if_index = swif->unnumbered_sw_if_index;
662
Wojciech Dec96e6f952016-10-20 11:25:37 +0200663
664 /*
665 * udp_local hands us the DHCPV6 header, need udp hdr,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 * ip hdr to relay to client
667 */
668 vlib_buffer_advance (b0, -(sizeof(*u1)));
669 u1 = vlib_buffer_get_current (b0);
670
671 vlib_buffer_advance (b0, -(sizeof(*ip1)));
672 ip1 = vlib_buffer_get_current (b0);
673
674 copy_ip6_address(&client_address, &h0->peer_addr);
675
676 ia0 = ip6_interface_first_address (&ip6_main, sw_if_index);
677 if (ia0 == 0)
678 {
679 error0 = DHCPV6_PROXY_ERROR_NO_INTERFACE_ADDRESS;
680 goto drop_packet;
681 }
682
683 len = clib_net_to_host_u16(r0->length);
684 memset(ip1, 0, sizeof(*ip1));
685 copy_ip6_address(&ip1->dst_address, &client_address);
686 u1->checksum = 0;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100687 u1->src_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_server);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 u1->dst_port = clib_net_to_host_u16 (UDP_DST_PORT_dhcpv6_to_client);
689 u1->length = clib_host_to_net_u16 (len + sizeof(udp_header_t));
690
691 ip1->ip_version_traffic_class_and_flow_label =
692 ip0->ip_version_traffic_class_and_flow_label &
693 0x00000fff;
694 ip1->payload_length = u1->length;
695 ip1->protocol = PROTO_UDP;
696 ip1->hop_limit = HOP_COUNT_LIMIT;
697 copy_ip6_address(&ip1->src_address, ia0);
698
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100699 u1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0, ip1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 &bogus_length);
701 ASSERT(bogus_length == 0);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100702
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 vlib_buffer_advance (b0, -(sizeof(ethernet_header_t)));
704 si0 = vnet_get_sw_interface (vnm, original_sw_if_index);
705 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
706 vlib_buffer_advance (b0, -4 /* space for VLAN tag */);
707
708 mac0 = vlib_buffer_get_current (b0);
709
710 hi0 = vnet_get_sup_hw_interface (vnm, original_sw_if_index);
711 ei0 = pool_elt_at_index (em->interfaces, hi0->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100712 clib_memcpy (mac0->src_address, ei0->address, sizeof (ei0->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 memset (&mac0->dst_address, 0xff, sizeof (mac0->dst_address));
714 mac0->type = (si0->type == VNET_SW_INTERFACE_TYPE_SUB) ?
715 clib_net_to_host_u16(0x8100) : clib_net_to_host_u16 (0x86dd);
716
717 if (si0->type == VNET_SW_INTERFACE_TYPE_SUB)
718 {
719 u32 * vlan_tag = (u32 *)(mac0+1);
720 u32 tmp;
721 tmp = (si0->sub.id << 16) | 0x0800;
722 *vlan_tag = clib_host_to_net_u32 (tmp);
723 }
724
725 /* $$$ consider adding a dynamic next to the graph node, for performance */
726 f0 = vlib_get_frame_to_node (vm, hi0->output_node_index);
727 to_next0 = vlib_frame_vector_args (f0);
728 to_next0[0] = bi0;
729 f0->n_vectors = 1;
730 vlib_put_frame_to_node (vm, hi0->output_node_index, f0);
731
732 do_trace:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100733 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100735 dhcpv6_proxy_trace_t *tr = vlib_add_trace (vm, node,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 b0, sizeof (*tr));
737 tr->which = 1; /* to client */
738 if (ia0)
739 copy_ip6_address((ip6_address_t*)tr->packet_data, ia0);
740 tr->error = error0;
741 tr->original_sw_if_index = original_sw_if_index;
742 tr->sw_if_index = sw_if_index;
743 }
744 }
745 return from_frame->n_vectors;
746
747}
748
749VLIB_REGISTER_NODE (dhcpv6_proxy_to_client_node) = {
750 .function = dhcpv6_proxy_to_client_input,
751 .name = "dhcpv6-proxy-to-client",
752 /* Takes a vector of packets. */
753 .vector_size = sizeof (u32),
754
755 .n_errors = DHCPV6_PROXY_N_ERROR,
756 .error_strings = dhcpv6_proxy_error_strings,
757 .format_buffer = format_dhcpv6_proxy_header_with_length,
758 .format_trace = format_dhcpv6_proxy_trace,
759#if 0
760 .unformat_buffer = unformat_dhcpv6_proxy_header,
761#endif
762};
763
764clib_error_t * dhcpv6_proxy_init (vlib_main_t * vm)
765{
766 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
767 vlib_node_t * error_drop_node;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100768 dhcpv6_server_t * server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
770 dm->vlib_main = vm;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100771 dm->vnet_main = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
773 dm->error_drop_node_index = error_drop_node->index;
774
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100775 dm->vss_index_by_vrf_id = hash_create (0, sizeof (uword));
776
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 /* RFC says this is the dhcpv6 server address */
778 dm->all_dhcpv6_server_address.as_u64[0] = clib_host_to_net_u64 (0xFF05000000000000);
779 dm->all_dhcpv6_server_address.as_u64[1] = clib_host_to_net_u64 (0x00010003);
780
781 /* RFC says this is the server and agent address */
782 dm->all_dhcpv6_server_relay_agent_address.as_u64[0] = clib_host_to_net_u64 (0xFF02000000000000);
783 dm->all_dhcpv6_server_relay_agent_address.as_u64[1] = clib_host_to_net_u64 (0x00010002);
784
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100785 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client,
786 dhcpv6_proxy_to_client_node.index, 0 /* is_ip6 */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100788 udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_server,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 dhcpv6_proxy_to_server_node.index, 0 /* is_ip6 */);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100790
791 /* Create the default server, don't mark it valid */
792 pool_get (dm->dhcp6_servers, server);
793 memset (server, 0, sizeof (*server));
794
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 return 0;
796}
797
798VLIB_INIT_FUNCTION (dhcpv6_proxy_init);
799
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100800/* Old API, manipulates a single server (only) shared by all Rx VRFs */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801int dhcpv6_proxy_set_server (ip6_address_t *addr, ip6_address_t *src_address,
802 u32 fib_id, int insert_vss, int is_del)
803{
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100804 return dhcpv6_proxy_set_server_2 (addr, src_address,
805 0, fib_id,
806 insert_vss, is_del);
807}
808
809int dhcpv6_proxy_set_server_2 (ip6_address_t *addr, ip6_address_t *src_address,
810 u32 rx_fib_id, u32 server_fib_id,
811 int insert_vss, int is_del)
812{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100814 dhcpv6_server_t * server = 0;
815 u32 server_fib_index = 0;
816 u32 rx_fib_index = 0;
817
818 rx_fib_index = ip6_fib_table_find_or_create_and_lock(rx_fib_id);
819 server_fib_index = ip6_fib_table_find_or_create_and_lock(server_fib_id);
820
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 if (is_del)
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100822 {
823
824 if (rx_fib_index >= vec_len(dm->dhcp6_server_index_by_rx_fib_index))
825 return VNET_API_ERROR_NO_SUCH_ENTRY;
826
827 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
828
829 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] = 0;
830 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
831 memset (server, 0, sizeof (*server));
832 pool_put (dm->dhcp6_servers, server);
833 return 0;
834 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835
836 if (addr->as_u64[0] == 0 &&
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100837 addr->as_u64[1] == 0 )
838 return VNET_API_ERROR_INVALID_DST_ADDRESS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100840 if (src_address->as_u64[0] == 0 &&
841 src_address->as_u64[1] == 0)
842 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
843
844 if (rx_fib_id == 0)
845 {
846 server = pool_elt_at_index (dm->dhcp6_servers, 0);
847
848 goto initialize_it;
849 }
850
851 if (rx_fib_index < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
852 {
853 server_fib_index = dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index];
854 if (server_fib_index != 0)
855 {
856 server = pool_elt_at_index (dm->dhcp6_servers, server_fib_index);
857 goto initialize_it;
858 }
859 }
860
861 /*Allocate a new server*/
862 pool_get (dm->dhcp6_servers, server);
863
864 initialize_it:
865
866 copy_ip6_address(&server->dhcp6_server, addr);
867 copy_ip6_address(&server->dhcp6_src_address, src_address);
868 server->server_fib6_index = server_fib_index;
869 server->valid = 1;
870 server->insert_vss = insert_vss;
871
872 vec_validate (dm->dhcp6_server_index_by_rx_fib_index, rx_fib_index);
873 dm->dhcp6_server_index_by_rx_fib_index[rx_fib_index] =
874 server - dm->dhcp6_servers;
875
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876 return 0;
877}
878
879static clib_error_t *
880dhcpv6_proxy_set_command_fn (vlib_main_t * vm,
881 unformat_input_t * input,
882 vlib_cli_command_t * cmd)
883{
884 ip6_address_t addr, src_addr;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100885 int set_server = 0, set_src_address = 0;
886 u32 rx_fib_id = 0, server_fib_id = 0;
887 int is_del = 0, add_vss = 0;
888
889 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100891 if (unformat (input, "server %U",
892 unformat_ip6_address, &addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 set_server = 1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100894 else if (unformat(input, "src-address %U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 unformat_ip6_address, &src_addr))
896 set_src_address =1;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100897 else if (unformat (input, "server-fib-id %d", &server_fib_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 ;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100899 else if (unformat (input, "rx-fib-id %d", &rx_fib_id))
900 ;
901 else if (unformat (input, "add-vss-option")
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 || unformat (input, "insert-option"))
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100903 add_vss = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 else if (unformat (input, "delete") ||
905 unformat (input, "del"))
906 is_del = 1;
907 else
908 break;
909 }
910
911 if (is_del || (set_server && set_src_address))
912 {
913 int rv;
914
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100915 rv = dhcpv6_proxy_set_server_2 (&addr, &src_addr, rx_fib_id,
916 server_fib_id, add_vss, is_del);
917
918 //TODO: Complete the errors
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 switch (rv)
920 {
921 case 0:
922 return 0;
923
924 case -1:
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100925 return clib_error_return (0, "FIB id %d does not exist", server_fib_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926
927 default:
928 return clib_error_return (0, "BUG: rv %d", rv);
929 }
930 }
931 else
932 return clib_error_return (0, "parse error`%U'",
933 format_unformat_error, input);
934}
935
936VLIB_CLI_COMMAND (dhcpv6_proxy_set_command, static) = {
937 .path = "set dhcpv6 proxy",
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100938 .short_help = "set dhcpv6 proxy [del] server <ipv6-addr> src-address <ipv6-addr> "
939 "[add-vss-option] [server-fib-id <fib-id>] [rx-fib-id <fib-id>] ",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 .function = dhcpv6_proxy_set_command_fn,
941};
942
943u8 * format_dhcpv6_proxy_server (u8 * s, va_list * args)
944{
945 dhcpv6_proxy_main_t * dm = va_arg (*args, dhcpv6_proxy_main_t *);
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100946 dhcpv6_server_t * server = va_arg (*args, dhcpv6_server_t *);
947 u32 rx_fib_index = va_arg (*args, u32);
948 ip6_fib_t * rx_fib, * server_fib;
949 u32 server_fib_id = (u32)~0, rx_fib_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
951 if (dm == 0)
952 {
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100953 s = format (s, "%=40s%=40s%=14s%=14s%=20s", "Server Address", "Source Address",
954 "Server FIB", "RX FIB", "Insert VSS Option");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955 return s;
956 }
957
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100958 server_fib = ip6_fib_get(server->server_fib6_index);
959 if (server_fib)
960 server_fib_id= server_fib->table_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100962 rx_fib= ip6_fib_get(rx_fib_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100964 if (rx_fib)
965 rx_fib_id = rx_fib->table_id;
966
967 s = format (s, "%=40U%=40U%=14u%=14u%=20s",
968 format_ip6_address, &server->dhcp6_server,
969 format_ip6_address, &server->dhcp6_src_address,
970 server_fib_id, rx_fib_id,
971 server->insert_vss ? "yes" : "no");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 return s;
973}
974
975static clib_error_t *
976dhcpv6_proxy_show_command_fn (vlib_main_t * vm,
977 unformat_input_t * input,
978 vlib_cli_command_t * cmd)
979{
980 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100981 ip6_main_t * im = &ip6_main;
982 int i;
983 u32 server_index;
984 dhcpv6_server_t * server;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Wojciech Dec558d5eb2016-11-01 16:44:06 +0100986 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, 0 /* header line */,
987 0, 0);
988 for (i = 0; i < vec_len (im->fibs); i++)
989 {
990 if (i < vec_len(dm->dhcp6_server_index_by_rx_fib_index))
991 server_index = dm->dhcp6_server_index_by_rx_fib_index[i];
992 else
993 server_index = 0;
994 server = pool_elt_at_index (dm->dhcp6_servers, server_index);
995 if (server->valid)
996 vlib_cli_output (vm, "%U", format_dhcpv6_proxy_server, dm,
997 server, i);
998 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999 return 0;
1000}
1001
1002VLIB_CLI_COMMAND (dhcpv6_proxy_show_command, static) = {
1003 .path = "show dhcpv6 proxy",
1004 .short_help = "Display dhcpv6 proxy info",
1005 .function = dhcpv6_proxy_show_command_fn,
1006};
1007
1008int dhcpv6_proxy_set_vss(u32 tbl_id,
1009 u32 oui,
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001010 u32 fib_id,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 int is_del)
1012{
1013 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
1014 u32 old_oui, old_fib_id;
1015 uword *p;
1016 dhcpv6_vss_info *v;
1017
1018 p = hash_get (dm->vss_index_by_vrf_id, tbl_id);
1019
1020 if (p) {
1021 v = pool_elt_at_index (dm->vss, p[0]);
1022 if (!v)
1023 return VNET_API_ERROR_NO_SUCH_FIB;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001024
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 old_oui = v->vpn_id.oui;
1026 old_fib_id = v->vpn_id.fib_id;
1027
1028 if (is_del)
1029 {
1030 if (old_oui == oui &&
1031 old_fib_id == fib_id )
1032 {
1033 pool_put(dm->vss, v);
1034 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1035 return 0;
1036 }
1037 else
1038 return VNET_API_ERROR_NO_SUCH_ENTRY;
1039 }
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001040
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 pool_put(dm->vss, v);
1042 hash_unset (dm->vss_index_by_vrf_id, tbl_id);
1043 } else if (is_del)
1044 return VNET_API_ERROR_NO_SUCH_ENTRY;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001045
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 pool_get (dm->vss, v);
1047 memset (v, ~0, sizeof (*v));
1048 v->vpn_id.fib_id = fib_id;
1049 v->vpn_id.oui = oui;
1050 hash_set (dm->vss_index_by_vrf_id, tbl_id, v - dm->vss);
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001051
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 return 0;
1053}
1054
1055
1056static clib_error_t *
1057dhcpv6_vss_command_fn (vlib_main_t * vm,
1058 unformat_input_t * input,
1059 vlib_cli_command_t * cmd)
1060{
1061 int is_del = 0, got_new_vss=0;
1062 u32 oui=0;
1063 u32 fib_id=0, tbl_id=~0;
1064
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001065 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066 {
1067 if (unformat (input, "oui %d", &oui))
1068 got_new_vss = 1;
1069 else if (unformat (input, "vpn-id %d", &fib_id))
1070 got_new_vss = 1;
1071 else if (unformat (input, "table %d", &tbl_id))
1072 got_new_vss = 1;
1073 else if (unformat(input, "delete") || unformat(input, "del"))
1074 is_del = 1;
1075 else
1076 break;
1077 }
1078
1079 if (tbl_id ==~0)
1080 return clib_error_return (0, "no table ID specified.");
1081
1082 if (is_del || got_new_vss)
1083 {
1084 int rv;
1085
1086 rv = dhcpv6_proxy_set_vss(tbl_id, oui, fib_id, is_del);
1087 switch (rv)
1088 {
1089 case 0:
1090 return 0;
1091
1092 case VNET_API_ERROR_NO_SUCH_FIB:
1093 return clib_error_return (0, "vss info (oui:%d, vpn-id:%d) not found in table %d.",
1094 oui, fib_id, tbl_id);
1095
1096 case VNET_API_ERROR_NO_SUCH_ENTRY:
1097 return clib_error_return (0, "vss for table %d not found in pool.",
1098 tbl_id);
1099
1100 default:
1101 return clib_error_return (0, "BUG: rv %d", rv);
1102 }
1103 }
1104 else
1105 return clib_error_return (0, "parse error`%U'",
1106 format_unformat_error, input);
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001107
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108}
1109
1110VLIB_CLI_COMMAND (dhcpv6_proxy_vss_command, static) = {
1111 .path = "set dhcpv6 vss",
1112 .short_help = "set dhcpv6 vss table <table-id> oui <oui> vpn-idx <vpn-idx>",
1113 .function = dhcpv6_vss_command_fn,
1114};
1115
1116static clib_error_t *
1117dhcpv6_vss_show_command_fn (vlib_main_t * vm,
1118 unformat_input_t * input,
1119 vlib_cli_command_t * cmd)
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001120
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121{
1122 dhcpv6_proxy_main_t * dm = &dhcpv6_proxy_main;
1123 dhcpv6_vss_info *v;
1124 u32 oui;
1125 u32 fib_id;
1126 u32 tbl_id;
1127 uword index;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001128
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 vlib_cli_output (vm, "%=6s%=6s%=12s","Table", "OUI", "VPN ID");
1130 hash_foreach (tbl_id, index, dm->vss_index_by_vrf_id,
1131 ({
1132 v = pool_elt_at_index (dm->vss, index);
1133 oui = v->vpn_id.oui;
1134 fib_id = v->vpn_id.fib_id;
1135 vlib_cli_output (vm, "%=6d%=6d%=12d",
1136 tbl_id, oui, fib_id);
1137 }));
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001138
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139 return 0;
1140}
1141
1142VLIB_CLI_COMMAND (dhcpv6_proxy_vss_show_command, static) = {
1143 .path = "show dhcpv6 vss",
1144 .short_help = "show dhcpv6 VSS",
1145 .function = dhcpv6_vss_show_command_fn,
1146};
1147
1148static clib_error_t *
1149dhcpv6_link_address_show_command_fn (vlib_main_t * vm,
1150 unformat_input_t * input,
1151 vlib_cli_command_t * cmd)
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001152
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153{
1154 dhcpv6_proxy_main_t *dm = &dhcpv6_proxy_main;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001155 vnet_main_t *vnm = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 u32 sw_if_index0=0, sw_if_index;
1157 ip6_address_t *ia0;
1158 vnet_sw_interface_t *swif;
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001159
1160 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161 {
1162
1163 if (unformat(input, "%U",
1164 unformat_vnet_sw_interface, dm->vnet_main, &sw_if_index0))
1165 {
1166 swif = vnet_get_sw_interface (vnm, sw_if_index0);
1167 sw_if_index = (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) ?
1168 swif->unnumbered_sw_if_index : sw_if_index0;
1169 ia0 = ip6_interface_first_address(&ip6_main, sw_if_index);
1170 if (ia0)
1171 {
1172 vlib_cli_output (vm, "%=20s%=48s", "interface", "link-address");
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001173
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174 vlib_cli_output (vm, "%=20U%=48U",
1175 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index0,
1176 format_ip6_address, ia0);
1177 } else
1178 vlib_cli_output (vm, "%=34s%=20U", "No IPv6 address configured on",
Wojciech Dec558d5eb2016-11-01 16:44:06 +01001179 format_vnet_sw_if_index_name, dm->vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180 } else
1181 break;
1182 }
1183
1184 return 0;
1185}
1186
1187VLIB_CLI_COMMAND (dhcpv6_proxy_address_show_command, static) = {
1188 .path = "show dhcpv6 link-address interface",
1189 .short_help = "show dhcpv6 link-address interface <interface>",
1190 .function = dhcpv6_link_address_show_command_fn,
1191};