e9e3bcf67c4fee6ec419c05cb8dfce928024a2bb
[vpp.git] / src / plugins / dhcp / client.h
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 /*
16  * client.h: dhcp client
17  */
18
19 #ifndef included_dhcp_client_h
20 #define included_dhcp_client_h
21
22 #include <vnet/ip/ip.h>
23 #include <dhcp/dhcp4_packet.h>
24
25 #define foreach_dhcp_client_state               \
26 _(DHCP_DISCOVER)                                \
27 _(DHCP_REQUEST)                                 \
28 _(DHCP_BOUND)
29
30 typedef enum
31 {
32 #define _(a) a,
33   foreach_dhcp_client_state
34 #undef _
35 } dhcp_client_state_t;
36
37 struct dhcp_client_t_;
38
39 /**
40  * Callback function for DHCP complete events
41  */
42 typedef void (*dhcp_event_cb_t) (u32 client_index,
43                                  const struct dhcp_client_t_ * client);
44
45 /**
46  * The set of addresses/mask that contribute forwarding info
47  * and are installed.
48  */
49 typedef struct dhcp_client_fwd_addresses_t_
50 {
51   /** the address assigned to this client and it's mask */
52   ip4_address_t leased_address;
53   u32 subnet_mask_width;
54
55   /** the address of the DHCP server handing out the address.
56       this is used to send any unicast messages */
57   ip4_address_t dhcp_server;
58
59   /** The address of this client's default gateway - may not be present */
60   ip4_address_t router_address;
61 } dhcp_client_fwd_addresses_t;
62
63 typedef struct dhcp_client_t_
64 {
65   dhcp_client_state_t state;
66
67   /* the interface in question */
68   u32 sw_if_index;
69
70   /* State machine retry counter */
71   u32 retry_count;
72
73   /* Send next pkt at this time */
74   f64 next_transmit;
75   f64 lease_expires;
76
77   /* DHCP transaction ID, a random number */
78   u32 transaction_id;
79
80   /**
81    * leased address, other learned info DHCP
82    *  the learned set is updated by new messages recieved in the DP
83    *  the installed set is what's actually been added
84    */
85   dhcp_client_fwd_addresses_t learned;
86   dhcp_client_fwd_addresses_t installed;
87   /* have local Addresses and default route been installed */
88   u8 addresses_installed;
89
90   ip4_address_t *domain_server_address; /* option 6 */
91   u32 lease_renewal_interval;   /* option 51 */
92   u32 lease_lifetime;           /* option 59 */
93
94   /* Requested data (option 55) */
95   u8 *option_55_data;
96
97   /* hostname and software client identifiers */
98   u8 *hostname;
99   u8 *client_identifier;        /* software version, e.g. vpe 1.0 */
100
101   /* Information used for event callback */
102   u32 client_index;
103   u32 pid;
104
105   /* Set the broadcast Flag in the Discover/Request messages */
106   u8 set_broadcast_flag;
107   /* Interface MAC address, so we can do an rx-packet-for-us check */
108   u8 client_hardware_address[6];
109   u8 client_detect_feature_enabled;
110
111   /* the unicast adjacency for the DHCP server */
112   adj_index_t ai_ucast;
113   /* the broadcast adjacency on the link */
114   adj_index_t ai_bcast;
115   /* IP DSCP to set in sent packets */
116   ip_dscp_t dscp;
117
118   dhcp_event_cb_t event_callback;
119 } dhcp_client_t;
120
121 typedef struct
122 {
123   /* DHCP client pool */
124   dhcp_client_t *clients;
125   uword *client_by_sw_if_index;
126   u32 seed;
127
128   /* convenience */
129   vlib_main_t *vlib_main;
130   vnet_main_t *vnet_main;
131 } dhcp_client_main_t;
132
133 typedef struct
134 {
135   int is_add;
136   u32 sw_if_index;
137   u8 set_broadcast_flag;
138
139   /* vectors, consumed by dhcp client code */
140   u8 *hostname;
141   u8 *client_identifier;
142
143   /* Bytes containing requested option numbers */
144   u8 *option_55_data;
145
146   /* Information used for event callback */
147   u32 client_index;
148   u32 pid;
149   ip_dscp_t dscp;
150   dhcp_event_cb_t event_callback;
151 } dhcp_client_add_del_args_t;
152
153 extern dhcp_client_main_t dhcp_client_main;
154
155 #define EVENT_DHCP_CLIENT_WAKEUP        1
156
157 int dhcp_client_for_us (u32 bi0,
158                         vlib_buffer_t * b0,
159                         ip4_header_t * ip0,
160                         udp_header_t * u0, dhcp_header_t * dh0);
161
162 /**
163  * Add/Delete DHCP clients
164  */
165 extern int dhcp_client_config (u32 is_add,
166                                u32 client_index,
167                                vlib_main_t * vm,
168                                u32 sw_if_index,
169                                u8 * hostname,
170                                u8 * client_id,
171                                dhcp_event_cb_t event_callback,
172                                u8 set_broadcast_flag,
173                                ip_dscp_t dscp, u32 pid);
174
175 /**
176  * callback function for clients walking the DHCP client configurations
177  *
178  * @param client The client being visitsed
179  * @param data   The data passed during the call to 'walk'
180  * @return !0 to continue walking 0 to stop.
181  */
182 typedef int (*dhcp_client_walk_cb_t) (const dhcp_client_t * client,
183                                       void *data);
184
185 /**
186  * Walk (visit each) DHCP client configuration
187  *
188  * @param cb The callback function invoked as each client is visited
189  * @param ctx Context data passed back to the client in the invocation of
190  *             the callback.
191  */
192 extern void dhcp_client_walk (dhcp_client_walk_cb_t cb, void *ctx);
193
194 #endif /* included_dhcp_client_h */
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */