Drop dhcp pkts w/ hardware address mismatches
[vpp.git] / src / vnet / dhcp / client.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vlib/vlib.h>
16 #include <vnet/dhcp/client.h>
17 #include <vnet/dhcp/dhcp_proxy.h>
18 #include <vnet/fib/fib_table.h>
19
20 dhcp_client_main_t dhcp_client_main;
21 static u8 *format_dhcp_client_state (u8 * s, va_list * va);
22 static vlib_node_registration_t dhcp_client_process_node;
23
24 #define foreach_dhcp_sent_packet_stat           \
25 _(DISCOVER, "DHCP discover packets sent")       \
26 _(OFFER, "DHCP offer packets sent")             \
27 _(REQUEST, "DHCP request packets sent")         \
28 _(ACK, "DHCP ack packets sent")
29
30 #define foreach_dhcp_error_counter                                      \
31 _(NOT_FOR_US, "DHCP packets for other hosts, dropped")                  \
32 _(NAK, "DHCP nak packets received")                                     \
33 _(NON_OFFER_DISCOVER, "DHCP non-offer packets in discover state")       \
34 _(ODDBALL, "DHCP non-ack, non-offer packets received")                  \
35 _(BOUND, "DHCP bind success")
36
37 typedef enum
38 {
39 #define _(sym,str) DHCP_STAT_##sym,
40   foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
41 #undef _
42     DHCP_STAT_UNKNOWN,
43   DHCP_STAT_N_STAT,
44 } sample_error_t;
45
46 static char *dhcp_client_process_stat_strings[] = {
47 #define _(sym,string) string,
48   foreach_dhcp_sent_packet_stat foreach_dhcp_error_counter
49 #undef _
50     "DHCP unknown packets sent",
51 };
52
53
54 static void
55 dhcp_client_acquire_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
56 {
57   /*
58    * Install any/all info gleaned from dhcp, right here
59    */
60   ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
61                                  (void *) &c->leased_address,
62                                  c->subnet_mask_width, 0 /*is_del */ );
63 }
64
65 static void
66 dhcp_client_release_address (dhcp_client_main_t * dcm, dhcp_client_t * c)
67 {
68   /*
69    * Remove any/all info gleaned from dhcp, right here. Caller(s)
70    * have not wiped out the info yet.
71    */
72
73   ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
74                                  (void *) &c->leased_address,
75                                  c->subnet_mask_width, 1 /*is_del */ );
76 }
77
78 static void
79 set_l2_rewrite (dhcp_client_main_t * dcm, dhcp_client_t * c)
80 {
81   /* Acquire the L2 rewrite string for the indicated sw_if_index */
82   c->l2_rewrite = vnet_build_rewrite_for_sw_interface (dcm->vnet_main,
83                                                        c->sw_if_index,
84                                                        VNET_LINK_IP4,
85                                                        0 /* broadcast */ );
86 }
87
88 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
89
90 static void
91 dhcp_client_proc_callback (uword * client_index)
92 {
93   vlib_main_t *vm = vlib_get_main ();
94   ASSERT (vlib_get_thread_index () == 0);
95   vlib_process_signal_event (vm, dhcp_client_process_node.index,
96                              EVENT_DHCP_CLIENT_WAKEUP, *client_index);
97 }
98
99 static void
100 dhcp_client_addr_callback (dhcp_client_t * c)
101 {
102   dhcp_client_main_t *dcm = &dhcp_client_main;
103   void (*fp) (u32, u32, u8 *, u8, u8, u8 *, u8 *, u8 *) = c->event_callback;
104
105   /* add the advertised subnet and disable the feature */
106   dhcp_client_acquire_address (dcm, c);
107   vnet_feature_enable_disable ("ip4-unicast",
108                                "ip4-dhcp-client-detect",
109                                c->sw_if_index, 0 /* disable */ , 0, 0);
110
111   /*
112    * Configure default IP route:
113    */
114   if (c->router_address.as_u32)
115     {
116       fib_prefix_t all_0s = {
117         .fp_len = 0,
118         .fp_addr.ip4.as_u32 = 0x0,
119         .fp_proto = FIB_PROTOCOL_IP4,
120       };
121       ip46_address_t nh = {
122         .ip4 = c->router_address,
123       };
124
125       /* *INDENT-OFF* */
126       fib_table_entry_path_add (
127         fib_table_get_index_for_sw_if_index (
128           FIB_PROTOCOL_IP4,
129           c->sw_if_index),
130           &all_0s,
131           FIB_SOURCE_DHCP,
132           FIB_ENTRY_FLAG_NONE,
133           DPO_PROTO_IP4,
134           &nh, c->sw_if_index,
135           ~0, 1, NULL,  // no label stack
136           FIB_ROUTE_PATH_FLAG_NONE);
137       /* *INDENT-ON* */
138     }
139
140   /*
141    * Call the user's event callback to report DHCP information
142    */
143   if (fp)
144     (*fp) (c->client_index,     /* clinet index */
145            c->pid, c->hostname, c->subnet_mask_width, 0,        /* is_ipv6 */
146            (u8 *) & c->leased_address,  /* host IP address */
147            (u8 *) & c->router_address,  /* router IP address */
148            (u8 *) (c->l2_rewrite + 6)); /* host MAC address */
149 }
150
151 /*
152  * dhcp_client_for_us - server-to-client callback.
153  * Called from proxy_node.c:dhcp_proxy_to_client_input().
154  * This function first decides that the packet in question is
155  * actually for the dhcp client code in case we're also acting as
156  * a dhcp proxy. Ay caramba, what a folly!
157  */
158 int
159 dhcp_client_for_us (u32 bi, vlib_buffer_t * b,
160                     ip4_header_t * ip,
161                     udp_header_t * udp, dhcp_header_t * dhcp)
162 {
163   dhcp_client_main_t *dcm = &dhcp_client_main;
164   vlib_main_t *vm = dcm->vlib_main;
165   dhcp_client_t *c;
166   uword *p;
167   f64 now = vlib_time_now (dcm->vlib_main);
168   u8 dhcp_message_type = 0;
169   dhcp_option_t *o;
170
171   /*
172    * Doing dhcp client on this interface?
173    * Presumably we will always receive dhcp clnt for-us pkts on
174    * the interface that's asking for an address.
175    */
176   p = hash_get (dcm->client_by_sw_if_index,
177                 vnet_buffer (b)->sw_if_index[VLIB_RX]);
178   if (p == 0)
179     return 0;                   /* no */
180
181   c = pool_elt_at_index (dcm->clients, p[0]);
182
183   /* Mixing dhcp relay and dhcp proxy? DGMS... */
184   if (c->state == DHCP_BOUND && c->retry_count == 0)
185     return 0;
186
187   /* Packet not for us? Turf it... */
188   if (memcmp (dhcp->client_hardware_address, c->client_hardware_address,
189               sizeof (c->client_hardware_address)))
190     {
191       vlib_node_increment_counter (vm, dhcp_client_process_node.index,
192                                    DHCP_STAT_NOT_FOR_US, 1);
193       return 0;
194     }
195
196   /* parse through the packet, learn what we can */
197   if (dhcp->your_ip_address.as_u32)
198     c->leased_address.as_u32 = dhcp->your_ip_address.as_u32;
199
200   c->dhcp_server.as_u32 = dhcp->server_ip_address.as_u32;
201
202   o = (dhcp_option_t *) dhcp->options;
203
204   while (o->option != 0xFF /* end of options */  &&
205          (u8 *) o < (b->data + b->current_data + b->current_length))
206     {
207       switch (o->option)
208         {
209         case 53:                /* dhcp message type */
210           dhcp_message_type = o->data[0];
211           break;
212
213         case 51:                /* lease time */
214           {
215             u32 lease_time_in_seconds =
216               clib_host_to_net_u32 (o->data_as_u32[0]);
217             c->lease_expires = now + (f64) lease_time_in_seconds;
218             c->lease_lifetime = lease_time_in_seconds;
219             /* Set a sensible default, in case we don't get opt 58 */
220             c->lease_renewal_interval = lease_time_in_seconds / 2;
221           }
222           break;
223
224         case 58:                /* lease renew time in seconds */
225           {
226             u32 lease_renew_time_in_seconds =
227               clib_host_to_net_u32 (o->data_as_u32[0]);
228             c->lease_renewal_interval = lease_renew_time_in_seconds;
229           }
230           break;
231
232         case 54:                /* dhcp server address */
233           c->dhcp_server.as_u32 = o->data_as_u32[0];
234           break;
235
236         case 1:         /* subnet mask */
237           {
238             u32 subnet_mask = clib_host_to_net_u32 (o->data_as_u32[0]);
239             c->subnet_mask_width = count_set_bits (subnet_mask);
240           }
241           break;
242         case 3:         /* router address */
243           {
244             u32 router_address = o->data_as_u32[0];
245             c->router_address.as_u32 = router_address;
246           }
247           break;
248
249         case 12:                /* hostname */
250           {
251             /* Replace the existing hostname if necessary */
252             vec_free (c->hostname);
253             vec_validate (c->hostname, o->length - 1);
254             clib_memcpy (c->hostname, o->data, o->length);
255           }
256           break;
257
258           /* $$$$ Your message in this space, parse more options */
259         default:
260           break;
261         }
262
263       o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
264     }
265
266   switch (c->state)
267     {
268     case DHCP_DISCOVER:
269       if (dhcp_message_type != DHCP_PACKET_OFFER)
270         {
271           vlib_node_increment_counter (vm, dhcp_client_process_node.index,
272                                        DHCP_STAT_NON_OFFER_DISCOVER, 1);
273           c->next_transmit = now + 5.0;
274           break;
275         }
276
277       /* Received an offer, go send a request */
278       c->state = DHCP_REQUEST;
279       c->retry_count = 0;
280       c->next_transmit = 0;     /* send right now... */
281       /* Poke the client process, which will send the request */
282       uword client_id = c - dcm->clients;
283       vl_api_rpc_call_main_thread (dhcp_client_proc_callback,
284                                    (u8 *) & client_id, sizeof (uword));
285       break;
286
287     case DHCP_BOUND:
288     case DHCP_REQUEST:
289       if (dhcp_message_type == DHCP_PACKET_NAK)
290         {
291           vlib_node_increment_counter (vm, dhcp_client_process_node.index,
292                                        DHCP_STAT_NAK, 1);
293           /* Probably never happens in bound state, but anyhow... */
294           if (c->state == DHCP_BOUND)
295             {
296               ip4_add_del_interface_address (dcm->vlib_main, c->sw_if_index,
297                                              (void *) &c->leased_address,
298                                              c->subnet_mask_width,
299                                              1 /*is_del */ );
300               vnet_feature_enable_disable ("ip4-unicast",
301                                            "ip4-dhcp-client-detect",
302                                            c->sw_if_index, 1 /* enable */ ,
303                                            0, 0);
304             }
305           /* Wipe out any memory of the address we had... */
306           c->state = DHCP_DISCOVER;
307           c->next_transmit = now;
308           c->retry_count = 0;
309           c->leased_address.as_u32 = 0;
310           c->subnet_mask_width = 0;
311           c->router_address.as_u32 = 0;
312           c->lease_renewal_interval = 0;
313           c->dhcp_server.as_u32 = 0;
314           break;
315         }
316
317       if (dhcp_message_type != DHCP_PACKET_ACK &&
318           dhcp_message_type != DHCP_PACKET_OFFER)
319         {
320           vlib_node_increment_counter (vm, dhcp_client_process_node.index,
321                                        DHCP_STAT_NON_OFFER_DISCOVER, 1);
322           clib_warning ("sw_if_index %d state %U message type %d",
323                         c->sw_if_index, format_dhcp_client_state,
324                         c->state, dhcp_message_type);
325           c->next_transmit = now + 5.0;
326           break;
327         }
328       /* OK, we own the address (etc), add to the routing table(s) */
329       if (c->state == DHCP_REQUEST)
330         vl_api_rpc_call_main_thread (dhcp_client_addr_callback,
331                                      (u8 *) c, sizeof (*c));
332
333       c->state = DHCP_BOUND;
334       c->retry_count = 0;
335       c->next_transmit = now + (f64) c->lease_renewal_interval;
336       c->lease_expires = now + (f64) c->lease_lifetime;
337       vlib_node_increment_counter (vm, dhcp_client_process_node.index,
338                                    DHCP_STAT_BOUND, 1);
339       break;
340
341     default:
342       clib_warning ("client %d bogus state %d", c - dcm->clients, c->state);
343       break;
344     }
345
346   /* drop the pkt, return 1 */
347   vlib_buffer_free (vm, &bi, 1);
348   return 1;
349 }
350
351 static void
352 send_dhcp_pkt (dhcp_client_main_t * dcm, dhcp_client_t * c,
353                dhcp_packet_type_t type, int is_broadcast)
354 {
355   vlib_main_t *vm = dcm->vlib_main;
356   vnet_main_t *vnm = dcm->vnet_main;
357   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, c->sw_if_index);
358   vnet_sw_interface_t *sup_sw
359     = vnet_get_sup_sw_interface (vnm, c->sw_if_index);
360   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, c->sw_if_index);
361   vlib_buffer_t *b;
362   u32 bi;
363   ip4_header_t *ip;
364   udp_header_t *udp;
365   dhcp_header_t *dhcp;
366   u32 *to_next;
367   vlib_frame_t *f;
368   dhcp_option_t *o;
369   u16 udp_length, ip_length;
370   u32 counter_index;
371
372   /* Interface(s) down? */
373   if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
374     return;
375   if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
376     return;
377   if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
378     return;
379
380   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
381     {
382       clib_warning ("buffer allocation failure");
383       c->next_transmit = 0;
384       return;
385     }
386
387   /* Build a dhcpv4 pkt from whole cloth */
388   b = vlib_get_buffer (vm, bi);
389
390   ASSERT (b->current_data == 0);
391
392   vnet_buffer (b)->sw_if_index[VLIB_RX] = c->sw_if_index;
393   if (is_broadcast)
394     {
395       f = vlib_get_frame_to_node (vm, hw->output_node_index);
396       vnet_buffer (b)->sw_if_index[VLIB_TX] = c->sw_if_index;
397       clib_memcpy (b->data, c->l2_rewrite, vec_len (c->l2_rewrite));
398       ip = (void *)
399         (((u8 *) vlib_buffer_get_current (b)) + vec_len (c->l2_rewrite));
400     }
401   else
402     {
403       f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
404       vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;       /* use interface VRF */
405       ip = vlib_buffer_get_current (b);
406     }
407
408   /* Enqueue the packet right now */
409   to_next = vlib_frame_vector_args (f);
410   to_next[0] = bi;
411   f->n_vectors = 1;
412
413   if (is_broadcast)
414     vlib_put_frame_to_node (vm, hw->output_node_index, f);
415   else
416     vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
417
418   udp = (udp_header_t *) (ip + 1);
419   dhcp = (dhcp_header_t *) (udp + 1);
420
421   /* $$$ optimize, maybe */
422   memset (ip, 0, sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp));
423
424   ip->ip_version_and_header_length = 0x45;
425   ip->ttl = 128;
426   ip->protocol = IP_PROTOCOL_UDP;
427
428   if (is_broadcast)
429     {
430       /* src = 0.0.0.0, dst = 255.255.255.255 */
431       ip->dst_address.as_u32 = ~0;
432     }
433   else
434     {
435       /* Renewing an active lease, plain old ip4 src/dst */
436       ip->src_address.as_u32 = c->leased_address.as_u32;
437       ip->dst_address.as_u32 = c->dhcp_server.as_u32;
438     }
439
440   udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client);
441   udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server);
442
443   /* Send the interface MAC address */
444   clib_memcpy (dhcp->client_hardware_address, c->l2_rewrite + 6, 6);
445
446   /* And remember it for rx-packet-for-us checking */
447   clib_memcpy (c->client_hardware_address, dhcp->client_hardware_address,
448                sizeof (c->client_hardware_address));
449
450   /* Lease renewal, set up client_ip_address */
451   if (is_broadcast == 0)
452     dhcp->client_ip_address.as_u32 = c->leased_address.as_u32;
453
454   dhcp->opcode = 1;             /* request, all we send */
455   dhcp->hardware_type = 1;      /* ethernet */
456   dhcp->hardware_address_length = 6;
457   dhcp->transaction_identifier = c->transaction_id;
458   dhcp->flags =
459     clib_host_to_net_u16 (is_broadcast && c->set_broadcast_flag ?
460                           DHCP_FLAG_BROADCAST : 0);
461   dhcp->magic_cookie.as_u32 = DHCP_MAGIC;
462
463   o = (dhcp_option_t *) dhcp->options;
464
465   /* Send option 53, the DHCP message type */
466   o->option = DHCP_PACKET_OPTION_MSG_TYPE;
467   o->length = 1;
468   o->data[0] = type;
469   o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
470
471   /* Send option 57, max msg length */
472   if (0 /* not needed, apparently */ )
473     {
474       o->option = 57;
475       o->length = 2;
476       {
477         u16 *o2 = (u16 *) o->data;
478         *o2 = clib_host_to_net_u16 (1152);
479         o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
480       }
481     }
482
483   /*
484    * If server ip address is available with non-zero value,
485    * option 54 (DHCP Server Identifier) is sent.
486    */
487   if (c->dhcp_server.as_u32)
488     {
489       o->option = 54;
490       o->length = 4;
491       clib_memcpy (o->data, &c->dhcp_server.as_u32, 4);
492       o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
493     }
494
495   /* send option 50, requested IP address */
496   if (c->leased_address.as_u32)
497     {
498       o->option = 50;
499       o->length = 4;
500       clib_memcpy (o->data, &c->leased_address.as_u32, 4);
501       o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
502     }
503
504   /* send option 12, host name */
505   if (vec_len (c->hostname))
506     {
507       o->option = 12;
508       o->length = vec_len (c->hostname);
509       clib_memcpy (o->data, c->hostname, vec_len (c->hostname));
510       o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
511     }
512
513   /* send option 61, client_id */
514   if (vec_len (c->client_identifier))
515     {
516       o->option = 61;
517       o->length = vec_len (c->client_identifier);
518       clib_memcpy (o->data, c->client_identifier,
519                    vec_len (c->client_identifier));
520       o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
521     }
522
523   /* $$ maybe send the client s/w version if anyone cares */
524
525   /*
526    * send option 55, parameter request list
527    * The current list - see below, matches the Linux dhcp client's list
528    * Any specific dhcp server config and/or dhcp server may or may
529    * not yield specific options.
530    */
531   o->option = 55;
532   o->length = vec_len (c->option_55_data);
533   clib_memcpy (o->data, c->option_55_data, vec_len (c->option_55_data));
534   o = (dhcp_option_t *) (((uword) o) + (o->length + 2));
535
536   /* End of list */
537   o->option = 0xff;
538   o->length = 0;
539   o++;
540
541   b->current_length = ((u8 *) o) - b->data;
542
543   /* fix ip length, checksum and udp length */
544   ip_length = vlib_buffer_length_in_chain (vm, b);
545   if (is_broadcast)
546     ip_length -= vec_len (c->l2_rewrite);
547
548   ip->length = clib_host_to_net_u16 (ip_length);
549   ip->checksum = ip4_header_checksum (ip);
550
551   udp_length = ip_length - (sizeof (*ip));
552   udp->length = clib_host_to_net_u16 (udp_length);
553
554   switch (type)
555     {
556 #define _(a,b) case DHCP_PACKET_##a: {counter_index = DHCP_STAT_##a; break;}
557       foreach_dhcp_sent_packet_stat
558 #undef _
559     default:
560       counter_index = DHCP_STAT_UNKNOWN;
561       break;
562     }
563
564   vlib_node_increment_counter (vm, dhcp_client_process_node.index,
565                                counter_index, 1);
566 }
567
568 static int
569 dhcp_discover_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
570 {
571   /*
572    * State machine "DISCOVER" state. Send a dhcp discover packet,
573    * eventually back off the retry rate.
574    */
575   send_dhcp_pkt (dcm, c, DHCP_PACKET_DISCOVER, 1 /* is_broadcast */ );
576
577   c->retry_count++;
578   if (c->retry_count > 10)
579     c->next_transmit = now + 5.0;
580   else
581     c->next_transmit = now + 1.0;
582   return 0;
583 }
584
585 static int
586 dhcp_request_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
587 {
588   /*
589    * State machine "REQUEST" state. Send a dhcp request packet,
590    * eventually drop back to the discover state.
591    */
592   send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 1 /* is_broadcast */ );
593
594   c->retry_count++;
595   if (c->retry_count > 7 /* lucky you */ )
596     {
597       c->state = DHCP_DISCOVER;
598       c->next_transmit = now;
599       c->retry_count = 0;
600       return 1;
601     }
602   c->next_transmit = now + 1.0;
603   return 0;
604 }
605
606 static int
607 dhcp_bound_state (dhcp_client_main_t * dcm, dhcp_client_t * c, f64 now)
608 {
609   /*
610    * State machine "BOUND" state. Send a dhcp request packet,
611    * eventually, when the lease expires, forget the dhcp data
612    * and go back to the stone age.
613    */
614   send_dhcp_pkt (dcm, c, DHCP_PACKET_REQUEST, 0 /* is_broadcast */ );
615
616   c->retry_count++;
617   if (c->retry_count > 10)
618     c->next_transmit = now + 5.0;
619   else
620     c->next_transmit = now + 1.0;
621
622   if (now > c->lease_expires)
623     {
624       /* Remove the default route */
625       if (c->router_address.as_u32)
626         {
627           fib_prefix_t all_0s = {
628             .fp_len = 0,
629             .fp_addr.ip4.as_u32 = 0x0,
630             .fp_proto = FIB_PROTOCOL_IP4,
631           };
632           ip46_address_t nh = {
633             .ip4 = c->router_address,
634           };
635
636           fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
637                                        (FIB_PROTOCOL_IP4, c->sw_if_index),
638                                        &all_0s, FIB_SOURCE_DHCP,
639                                        DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
640                                        1, FIB_ROUTE_PATH_FLAG_NONE);
641         }
642       /* Remove the interface address */
643       dhcp_client_release_address (dcm, c);
644       c->state = DHCP_DISCOVER;
645       c->next_transmit = now;
646       c->retry_count = 0;
647       /* Wipe out any memory of the address we had... */
648       c->leased_address.as_u32 = 0;
649       c->subnet_mask_width = 0;
650       c->router_address.as_u32 = 0;
651       c->lease_renewal_interval = 0;
652       c->dhcp_server.as_u32 = 0;
653       /*
654        * We disable the client detect feature when we bind a
655        * DHCP address. Turn it back on again here.
656        * Otherwise, if the DHCP server replies after an outage,
657        * we'll never see it.
658        */
659       vnet_feature_enable_disable ("ip4-unicast",
660                                    "ip4-dhcp-client-detect",
661                                    c->sw_if_index, 1 /* enable */ , 0, 0);
662       return 1;
663     }
664   return 0;
665 }
666
667 static f64
668 dhcp_client_sm (f64 now, f64 timeout, uword pool_index)
669 {
670   dhcp_client_main_t *dcm = &dhcp_client_main;
671   dhcp_client_t *c;
672
673   /* deleted, pooched, yadda yadda yadda */
674   if (pool_is_free_index (dcm->clients, pool_index))
675     return timeout;
676
677   c = pool_elt_at_index (dcm->clients, pool_index);
678
679   /* Time for us to do something with this client? */
680   if (now < c->next_transmit)
681     return timeout;
682
683 again:
684   switch (c->state)
685     {
686     case DHCP_DISCOVER: /* send a discover */
687       if (dhcp_discover_state (dcm, c, now))
688         goto again;
689       break;
690
691     case DHCP_REQUEST:          /* send a request */
692       if (dhcp_request_state (dcm, c, now))
693         goto again;
694       break;
695
696     case DHCP_BOUND:            /* bound, renew needed? */
697       if (dhcp_bound_state (dcm, c, now))
698         goto again;
699       break;
700
701     default:
702       clib_warning ("dhcp client %d bogus state %d",
703                     c - dcm->clients, c->state);
704       break;
705     }
706
707   if (c->next_transmit < now + timeout)
708     return c->next_transmit - now;
709
710   return timeout;
711 }
712
713 static uword
714 dhcp_client_process (vlib_main_t * vm,
715                      vlib_node_runtime_t * rt, vlib_frame_t * f)
716 {
717   f64 timeout = 100.0;
718   f64 now;
719   uword event_type;
720   uword *event_data = 0;
721   dhcp_client_main_t *dcm = &dhcp_client_main;
722   dhcp_client_t *c;
723   int i;
724
725   while (1)
726     {
727       vlib_process_wait_for_event_or_clock (vm, timeout);
728
729       event_type = vlib_process_get_events (vm, &event_data);
730
731       now = vlib_time_now (vm);
732
733       switch (event_type)
734         {
735         case EVENT_DHCP_CLIENT_WAKEUP:
736           for (i = 0; i < vec_len (event_data); i++)
737             timeout = dhcp_client_sm (now, timeout, event_data[i]);
738           break;
739
740         case ~0:
741           /* *INDENT-OFF* */
742           pool_foreach (c, dcm->clients,
743           ({
744             timeout = dhcp_client_sm (now, timeout,
745                                       (uword) (c - dcm->clients));
746           }));
747           /* *INDENT-ON* */
748           if (pool_elts (dcm->clients) == 0)
749             timeout = 100.0;
750           break;
751         }
752
753       vec_reset_length (event_data);
754     }
755
756   /* NOTREACHED */
757   return 0;
758 }
759
760 /* *INDENT-OFF* */
761 VLIB_REGISTER_NODE (dhcp_client_process_node,static) = {
762     .function = dhcp_client_process,
763     .type = VLIB_NODE_TYPE_PROCESS,
764     .name = "dhcp-client-process",
765     .process_log2_n_stack_bytes = 16,
766     .n_errors = ARRAY_LEN(dhcp_client_process_stat_strings),
767     .error_strings = dhcp_client_process_stat_strings,
768 };
769 /* *INDENT-ON* */
770
771 static u8 *
772 format_dhcp_client_state (u8 * s, va_list * va)
773 {
774   dhcp_client_state_t state = va_arg (*va, dhcp_client_state_t);
775   char *str = "BOGUS!";
776
777   switch (state)
778     {
779 #define _(a)                                    \
780     case a:                                     \
781       str = #a;                                 \
782         break;
783       foreach_dhcp_client_state;
784 #undef _
785     default:
786       break;
787     }
788
789   s = format (s, "%s", str);
790   return s;
791 }
792
793 static u8 *
794 format_dhcp_client (u8 * s, va_list * va)
795 {
796   dhcp_client_main_t *dcm = va_arg (*va, dhcp_client_main_t *);
797   dhcp_client_t *c = va_arg (*va, dhcp_client_t *);
798   int verbose = va_arg (*va, int);
799
800   s = format (s, "[%d] %U state %U ", c - dcm->clients,
801               format_vnet_sw_if_index_name, dcm->vnet_main, c->sw_if_index,
802               format_dhcp_client_state, c->state);
803
804   if (c->leased_address.as_u32)
805     s = format (s, "addr %U/%d gw %U\n",
806                 format_ip4_address, &c->leased_address,
807                 c->subnet_mask_width, format_ip4_address, &c->router_address);
808   else
809     s = format (s, "no address\n");
810
811   if (verbose)
812     {
813       s = format (s, "retry count %d, next xmt %.2f",
814                   c->retry_count, c->next_transmit);
815     }
816   return s;
817 }
818
819 static clib_error_t *
820 show_dhcp_client_command_fn (vlib_main_t * vm,
821                              unformat_input_t * input,
822                              vlib_cli_command_t * cmd)
823 {
824   dhcp_client_main_t *dcm = &dhcp_client_main;
825   dhcp_client_t *c;
826   int verbose = 0;
827   u32 sw_if_index = ~0;
828   uword *p;
829
830   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
831     {
832       if (unformat (input, "intfc %U",
833                     unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
834         ;
835       else if (unformat (input, "verbose"))
836         verbose = 1;
837       else
838         break;
839     }
840
841   if (sw_if_index != ~0)
842     {
843       p = hash_get (dcm->client_by_sw_if_index, sw_if_index);
844       if (p == 0)
845         return clib_error_return (0, "dhcp client not configured");
846       c = pool_elt_at_index (dcm->clients, p[0]);
847       vlib_cli_output (vm, "%U", format_dhcp_client, dcm, c, verbose);
848       return 0;
849     }
850
851   /* *INDENT-OFF* */
852   pool_foreach (c, dcm->clients,
853   ({
854     vlib_cli_output (vm, "%U",
855                      format_dhcp_client, dcm,
856                      c, verbose);
857   }));
858   /* *INDENT-ON* */
859
860   return 0;
861 }
862
863 /* *INDENT-OFF* */
864 VLIB_CLI_COMMAND (show_dhcp_client_command, static) = {
865   .path = "show dhcp client",
866   .short_help = "show dhcp client [intfc <intfc>][verbose]",
867   .function = show_dhcp_client_command_fn,
868 };
869 /* *INDENT-ON* */
870
871
872 int
873 dhcp_client_add_del (dhcp_client_add_del_args_t * a)
874 {
875   dhcp_client_main_t *dcm = &dhcp_client_main;
876   vlib_main_t *vm = dcm->vlib_main;
877   dhcp_client_t *c;
878   uword *p;
879   fib_prefix_t all_0s = {
880     .fp_len = 0,
881     .fp_addr.ip4.as_u32 = 0x0,
882     .fp_proto = FIB_PROTOCOL_IP4,
883   };
884
885   p = hash_get (dcm->client_by_sw_if_index, a->sw_if_index);
886
887   if ((p && a->is_add) || (!p && a->is_add == 0))
888     return VNET_API_ERROR_INVALID_VALUE;
889
890   if (a->is_add)
891     {
892       pool_get (dcm->clients, c);
893       memset (c, 0, sizeof (*c));
894       c->state = DHCP_DISCOVER;
895       c->sw_if_index = a->sw_if_index;
896       c->client_index = a->client_index;
897       c->pid = a->pid;
898       c->event_callback = a->event_callback;
899       c->option_55_data = a->option_55_data;
900       c->hostname = a->hostname;
901       c->client_identifier = a->client_identifier;
902       c->set_broadcast_flag = a->set_broadcast_flag;
903       do
904         {
905           c->transaction_id = random_u32 (&dcm->seed);
906         }
907       while (c->transaction_id == 0);
908       set_l2_rewrite (dcm, c);
909       hash_set (dcm->client_by_sw_if_index, a->sw_if_index, c - dcm->clients);
910
911       /*
912        * In order to accept any OFFER, whether broadcasted or unicasted, we
913        * need to configure the dhcp-client-detect feature as an input feature
914        * so the DHCP OFFER is sent to the ip4-local node. Without this a
915        * broadcasted OFFER hits the 255.255.255.255/32 address and a unicast
916        * hits 0.0.0.0/0 both of which default to drop and the latter may forward
917        * of box - not what we want. Nor to we want to change these route for
918        * all interfaces in this table
919        */
920       vnet_feature_enable_disable ("ip4-unicast",
921                                    "ip4-dhcp-client-detect",
922                                    c->sw_if_index, 1 /* enable */ , 0, 0);
923
924       vlib_process_signal_event (vm, dhcp_client_process_node.index,
925                                  EVENT_DHCP_CLIENT_WAKEUP, c - dcm->clients);
926     }
927   else
928     {
929       c = pool_elt_at_index (dcm->clients, p[0]);
930
931       if (c->router_address.as_u32)
932         {
933           ip46_address_t nh = {
934             .ip4 = c->router_address,
935           };
936
937           fib_table_entry_path_remove (fib_table_get_index_for_sw_if_index
938                                        (FIB_PROTOCOL_IP4, c->sw_if_index),
939                                        &all_0s, FIB_SOURCE_DHCP,
940                                        DPO_PROTO_IP4, &nh, c->sw_if_index, ~0,
941                                        1, FIB_ROUTE_PATH_FLAG_NONE);
942         }
943       dhcp_client_release_address (dcm, c);
944
945       vec_free (c->option_55_data);
946       vec_free (c->hostname);
947       vec_free (c->client_identifier);
948       vec_free (c->l2_rewrite);
949       hash_unset (dcm->client_by_sw_if_index, c->sw_if_index);
950       pool_put (dcm->clients, c);
951     }
952   return 0;
953 }
954
955 int
956 dhcp_client_config (vlib_main_t * vm,
957                     u32 sw_if_index,
958                     u8 * hostname,
959                     u8 * client_id,
960                     u32 is_add,
961                     u32 client_index,
962                     void *event_callback, u8 set_broadcast_flag, u32 pid)
963 {
964   dhcp_client_add_del_args_t _a, *a = &_a;
965   int rv;
966
967   memset (a, 0, sizeof (*a));
968   a->is_add = is_add;
969   a->sw_if_index = sw_if_index;
970   a->client_index = client_index;
971   a->pid = pid;
972   a->event_callback = event_callback;
973   a->set_broadcast_flag = set_broadcast_flag;
974   vec_validate (a->hostname, strlen ((char *) hostname) - 1);
975   strncpy ((char *) a->hostname, (char *) hostname, vec_len (a->hostname));
976   vec_validate (a->client_identifier, strlen ((char *) client_id) - 1);
977   strncpy ((char *) a->client_identifier, (char *) client_id,
978            vec_len (a->client_identifier));
979
980   /*
981    * Option 55 request list. These data precisely match
982    * the Ubuntu dhcp client. YMMV.
983    */
984
985   /* Subnet Mask */
986   vec_add1 (a->option_55_data, 1);
987   /* Broadcast address */
988   vec_add1 (a->option_55_data, 28);
989   /* time offset */
990   vec_add1 (a->option_55_data, 2);
991   /* Router */
992   vec_add1 (a->option_55_data, 3);
993   /* Domain Name */
994   vec_add1 (a->option_55_data, 15);
995   /* DNS */
996   vec_add1 (a->option_55_data, 6);
997   /* Domain search */
998   vec_add1 (a->option_55_data, 119);
999   /* Host name */
1000   vec_add1 (a->option_55_data, 12);
1001   /* NetBIOS name server */
1002   vec_add1 (a->option_55_data, 44);
1003   /* NetBIOS Scope */
1004   vec_add1 (a->option_55_data, 47);
1005   /* MTU */
1006   vec_add1 (a->option_55_data, 26);
1007   /* Classless static route */
1008   vec_add1 (a->option_55_data, 121);
1009   /* NTP servers */
1010   vec_add1 (a->option_55_data, 42);
1011
1012   rv = dhcp_client_add_del (a);
1013
1014   switch (rv)
1015     {
1016     case 0:
1017       break;
1018
1019     case VNET_API_ERROR_INVALID_VALUE:
1020
1021       vec_free (a->hostname);
1022       vec_free (a->client_identifier);
1023       vec_free (a->option_55_data);
1024
1025       if (is_add)
1026         clib_warning ("dhcp client already enabled on intf_idx %d",
1027                       sw_if_index);
1028       else
1029         clib_warning ("dhcp client not enabled on on intf_idx %d",
1030                       sw_if_index);
1031       break;
1032
1033     default:
1034       clib_warning ("dhcp_client_add_del returned %d", rv);
1035     }
1036
1037   return rv;
1038 }
1039
1040 static clib_error_t *
1041 dhcp_client_set_command_fn (vlib_main_t * vm,
1042                             unformat_input_t * input,
1043                             vlib_cli_command_t * cmd)
1044 {
1045
1046   dhcp_client_main_t *dcm = &dhcp_client_main;
1047   u32 sw_if_index;
1048   u8 *hostname = 0;
1049   u8 sw_if_index_set = 0;
1050   u8 set_broadcast_flag = 1;
1051   int is_add = 1;
1052   dhcp_client_add_del_args_t _a, *a = &_a;
1053   int rv;
1054
1055   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1056     {
1057       if (unformat (input, "intfc %U",
1058                     unformat_vnet_sw_interface, dcm->vnet_main, &sw_if_index))
1059         sw_if_index_set = 1;
1060       else if (unformat (input, "hostname %v", &hostname))
1061         ;
1062       else if (unformat (input, "del"))
1063         is_add = 0;
1064       else if (unformat (input, "broadcast", &set_broadcast_flag))
1065         is_add = 0;
1066       else
1067         break;
1068     }
1069
1070   if (sw_if_index_set == 0)
1071     return clib_error_return (0, "interface not specified");
1072
1073   memset (a, 0, sizeof (*a));
1074   a->is_add = is_add;
1075   a->sw_if_index = sw_if_index;
1076   a->hostname = hostname;
1077   a->client_identifier = format (0, "vpe 1.0%c", 0);
1078   a->set_broadcast_flag = set_broadcast_flag;
1079
1080   /*
1081    * Option 55 request list. These data precisely match
1082    * the Ubuntu dhcp client. YMMV.
1083    */
1084
1085   /* Subnet Mask */
1086   vec_add1 (a->option_55_data, 1);
1087   /* Broadcast address */
1088   vec_add1 (a->option_55_data, 28);
1089   /* time offset */
1090   vec_add1 (a->option_55_data, 2);
1091   /* Router */
1092   vec_add1 (a->option_55_data, 3);
1093   /* Domain Name */
1094   vec_add1 (a->option_55_data, 15);
1095   /* DNS */
1096   vec_add1 (a->option_55_data, 6);
1097   /* Domain search */
1098   vec_add1 (a->option_55_data, 119);
1099   /* Host name */
1100   vec_add1 (a->option_55_data, 12);
1101   /* NetBIOS name server */
1102   vec_add1 (a->option_55_data, 44);
1103   /* NetBIOS Scope */
1104   vec_add1 (a->option_55_data, 47);
1105   /* MTU */
1106   vec_add1 (a->option_55_data, 26);
1107   /* Classless static route */
1108   vec_add1 (a->option_55_data, 121);
1109   /* NTP servers */
1110   vec_add1 (a->option_55_data, 42);
1111
1112   rv = dhcp_client_add_del (a);
1113
1114   switch (rv)
1115     {
1116     case 0:
1117       break;
1118
1119     case VNET_API_ERROR_INVALID_VALUE:
1120
1121       vec_free (a->hostname);
1122       vec_free (a->client_identifier);
1123       vec_free (a->option_55_data);
1124       if (is_add)
1125         return clib_error_return (0, "dhcp client already enabled on %U",
1126                                   format_vnet_sw_if_index_name,
1127                                   dcm->vnet_main, sw_if_index);
1128       else
1129         return clib_error_return (0, "dhcp client not enabled on %U",
1130                                   format_vnet_sw_if_index_name,
1131                                   dcm->vnet_main, sw_if_index);
1132       break;
1133
1134     default:
1135       vlib_cli_output (vm, "dhcp_client_add_del returned %d", rv);
1136     }
1137
1138   return 0;
1139 }
1140
1141 /* *INDENT-OFF* */
1142 VLIB_CLI_COMMAND (dhcp_client_set_command, static) = {
1143   .path = "set dhcp client",
1144   .short_help = "set dhcp client [del] intfc <interface> [hostname <name>]",
1145   .function = dhcp_client_set_command_fn,
1146 };
1147 /* *INDENT-ON* */
1148
1149 static clib_error_t *
1150 dhcp_client_init (vlib_main_t * vm)
1151 {
1152   dhcp_client_main_t *dcm = &dhcp_client_main;
1153
1154   dcm->vlib_main = vm;
1155   dcm->vnet_main = vnet_get_main ();
1156   dcm->seed = 0xdeaddabe;
1157   return 0;
1158 }
1159
1160 VLIB_INIT_FUNCTION (dhcp_client_init);
1161
1162 /*
1163  * fd.io coding-style-patch-verification: ON
1164  *
1165  * Local Variables:
1166  * eval: (c-set-style "gnu")
1167  * End:
1168  */