1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 /***
5   Copyright © 2013 Intel Corporation. All rights reserved.
6 ***/
7 
8 #include <netinet/ip.h>
9 #include <netinet/udp.h>
10 #include <stdint.h>
11 
12 #include "macro.h"
13 #include "sparse-endian.h"
14 
15 struct DHCPMessage {
16         uint8_t op;
17         uint8_t htype;
18         uint8_t hlen;
19         uint8_t hops;
20         be32_t xid;
21         be16_t secs;
22         be16_t flags;
23         be32_t ciaddr;
24         be32_t yiaddr;
25         be32_t siaddr;
26         be32_t giaddr;
27         uint8_t chaddr[16];
28         uint8_t sname[64];
29         uint8_t file[128];
30         be32_t magic;
31         uint8_t options[0];
32 } _packed_;
33 
34 typedef struct DHCPMessage DHCPMessage;
35 
36 struct DHCPPacket {
37         struct iphdr ip;
38         struct udphdr udp;
39         DHCPMessage dhcp;
40 } _packed_;
41 
42 typedef struct DHCPPacket DHCPPacket;
43 
44 #define DHCP_IP_SIZE            (int32_t)(sizeof(struct iphdr))
45 #define DHCP_IP_UDP_SIZE        (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
46 #define DHCP_MESSAGE_SIZE       (int32_t)(sizeof(DHCPMessage))
47 #define DHCP_DEFAULT_MIN_SIZE   576 /* the minimum internet hosts must be able to receive */
48 #define DHCP_MIN_OPTIONS_SIZE   (DHCP_DEFAULT_MIN_SIZE - DHCP_IP_UDP_SIZE - DHCP_MESSAGE_SIZE)
49 #define DHCP_MAGIC_COOKIE       (uint32_t)(0x63825363)
50 
51 enum {
52         DHCP_PORT_SERVER                        = 67,
53         DHCP_PORT_CLIENT                        = 68,
54 };
55 
56 enum DHCPState {
57         DHCP_STATE_INIT                         = 0,
58         DHCP_STATE_SELECTING                    = 1,
59         DHCP_STATE_INIT_REBOOT                  = 2,
60         DHCP_STATE_REBOOTING                    = 3,
61         DHCP_STATE_REQUESTING                   = 4,
62         DHCP_STATE_BOUND                        = 5,
63         DHCP_STATE_RENEWING                     = 6,
64         DHCP_STATE_REBINDING                    = 7,
65         DHCP_STATE_STOPPED                      = 8,
66 };
67 
68 typedef enum DHCPState DHCPState;
69 
70 enum {
71         BOOTREQUEST                             = 1,
72         BOOTREPLY                               = 2,
73 };
74 
75 enum {
76         DHCP_DISCOVER                           = 1,  /* [RFC2132] */
77         DHCP_OFFER                              = 2,  /* [RFC2132] */
78         DHCP_REQUEST                            = 3,  /* [RFC2132] */
79         DHCP_DECLINE                            = 4,  /* [RFC2132] */
80         DHCP_ACK                                = 5,  /* [RFC2132] */
81         DHCP_NAK                                = 6,  /* [RFC2132] */
82         DHCP_RELEASE                            = 7,  /* [RFC2132] */
83         DHCP_INFORM                             = 8,  /* [RFC2132] */
84         DHCP_FORCERENEW                         = 9,  /* [RFC3203] */
85         DHCPLEASEQUERY                          = 10, /* [RFC4388] */
86         DHCPLEASEUNASSIGNED                     = 11, /* [RFC4388] */
87         DHCPLEASEUNKNOWN                        = 12, /* [RFC4388] */
88         DHCPLEASEACTIVE                         = 13, /* [RFC4388] */
89         DHCPBULKLEASEQUERY                      = 14, /* [RFC6926] */
90         DHCPLEASEQUERYDONE                      = 15, /* [RFC6926] */
91         DHCPACTIVELEASEQUERY                    = 16, /* [RFC7724] */
92         DHCPLEASEQUERYSTATUS                    = 17, /* [RFC7724] */
93         DHCPTLS                                 = 18, /* [RFC7724] */
94 };
95 
96 enum {
97         DHCP_OVERLOAD_FILE                      = 1,
98         DHCP_OVERLOAD_SNAME                     = 2,
99 };
100 
101 #define DHCP_MAX_FQDN_LENGTH 255
102 
103 enum {
104         DHCP_FQDN_FLAG_S = (1 << 0),
105         DHCP_FQDN_FLAG_O = (1 << 1),
106         DHCP_FQDN_FLAG_E = (1 << 2),
107         DHCP_FQDN_FLAG_N = (1 << 3),
108 };
109