1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28: #ifndef DHCP_H
29: #define DHCP_H
30:
31: #ifndef T2EX
32: #define T2EX
33: #endif
34:
35: #ifndef T2EX
36: #include <arpa/inet.h>
37: #include <netinet/in.h>
38:
39: #include <stdint.h>
40:
41: #include "common.h"
42: #else
43: #include <t2ex/socket.h>
44: #endif
45:
46:
47: #define MTU_MAX 1500
48: #define MTU_MIN 576
49:
50:
51: #define DHCP_SERVER_PORT 67
52: #define DHCP_CLIENT_PORT 68
53:
54: #define MAGIC_COOKIE 0x63825363
55: #define BROADCAST_FLAG 0x8000
56:
57:
58: #define DHCP_BOOTREQUEST 1
59: #define DHCP_BOOTREPLY 2
60:
61:
62: #define DHCP_DISCOVER 1
63: #define DHCP_OFFER 2
64: #define DHCP_REQUEST 3
65: #define DHCP_DECLINE 4
66: #define DHCP_ACK 5
67: #define DHCP_NAK 6
68: #define DHCP_RELEASE 7
69: #define DHCP_INFORM 8
70:
71:
72: #define T1 0.5
73: #define T2 0.875
74: #define DHCP_BASE 4
75: #define DHCP_MAX 64
76: #define DHCP_RAND_MIN -1
77: #define DHCP_RAND_MAX 1
78: #define DHCP_ARP_FAIL 10
79:
80:
81: #define USECS_SECOND 1000000
82: 83:
84: #define DHCP_RAND_MIN_U DHCP_RAND_MIN * USECS_SECOND
85: #define DHCP_RAND_MAX_U DHCP_RAND_MAX * USECS_SECOND
86: #define PROBE_MIN_U PROBE_MIN * USECS_SECOND
87: #define PROBE_MAX_U PROBE_MAX * USECS_SECOND
88:
89:
90: enum DHO {
91: DHO_PAD = 0,
92: DHO_SUBNETMASK = 1,
93: DHO_ROUTER = 3,
94: DHO_DNSSERVER = 6,
95: DHO_HOSTNAME = 12,
96: DHO_DNSDOMAIN = 15,
97: DHO_MTU = 26,
98: DHO_BROADCAST = 28,
99: DHO_STATICROUTE = 33,
100: DHO_NISDOMAIN = 40,
101: DHO_NISSERVER = 41,
102: DHO_NTPSERVER = 42,
103: DHO_VENDOR = 43,
104: DHO_IPADDRESS = 50,
105: DHO_LEASETIME = 51,
106: DHO_OPTIONSOVERLOADED = 52,
107: DHO_MESSAGETYPE = 53,
108: DHO_SERVERID = 54,
109: DHO_PARAMETERREQUESTLIST = 55,
110: DHO_MESSAGE = 56,
111: DHO_MAXMESSAGESIZE = 57,
112: DHO_RENEWALTIME = 58,
113: DHO_REBINDTIME = 59,
114: DHO_VENDORCLASSID = 60,
115: DHO_CLIENTID = 61,
116: DHO_USERCLASS = 77,
117: DHO_FQDN = 81,
118: DHO_DNSSEARCH = 119,
119: DHO_CSR = 121,
120: DHO_MSCSR = 249,
121: DHO_END = 255
122: };
123:
124: 125: 126: 127:
128: enum FQDN {
129: FQDN_DISABLE = 0x00,
130: FQDN_NONE = 0x18,
131: FQDN_PTR = 0x20,
132: FQDN_BOTH = 0x31
133: };
134:
135:
136: #define DHCP_CHADDR_LEN 16
137: #define SERVERNAME_LEN 64
138: #define BOOTFILE_LEN 128
139: #define DHCP_UDP_LEN (14 + 20 + 8)
140: #define DHCP_FIXED_LEN (DHCP_UDP_LEN + 226)
141: #define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN)
142:
143:
144: #define BOOTP_MESSAGE_LENTH_MIN 300
145:
146: struct dhcp_message {
147: uint8_t op;
148: uint8_t hwtype;
149: uint8_t hwlen;
150: uint8_t hwopcount;
151: uint32_t xid;
152: uint16_t secs;
153: uint16_t flags;
154: uint32_t ciaddr;
155: uint32_t yiaddr;
156: uint32_t siaddr;
157: uint32_t giaddr;
158: uint8_t chaddr[DHCP_CHADDR_LEN];
159: uint8_t servername[SERVERNAME_LEN];
160: uint8_t bootfile[BOOTFILE_LEN];
161: uint32_t cookie;
162: uint8_t options[DHCP_OPTION_LEN];
163: } _packed;
164:
165: struct dhcp_lease {
166: struct in_addr addr;
167: struct in_addr net;
168: struct in_addr brd;
169: uint32_t leasetime;
170: uint32_t renewaltime;
171: uint32_t rebindtime;
172: struct in_addr server;
173: time_t leasedfrom;
174: struct timeval boundtime;
175: uint8_t frominfo;
176: };
177:
178: #ifndef T2EX
179: #include "dhcpcd.h"
180: #include "if-options.h"
181: #include "net.h"
182:
183: #define add_option_mask(var, val) (var[val >> 3] |= 1 << (val & 7))
184: #define del_option_mask(var, val) (var[val >> 3] &= ~(1 << (val & 7)))
185: #define has_option_mask(var, val) (var[val >> 3] & (1 << (val & 7)))
186: int make_option_mask(uint8_t *, const char *, int);
187: void print_options(void);
188: char *get_option_string(const struct dhcp_message *, uint8_t);
189: int get_option_addr(struct in_addr *, const struct dhcp_message *, uint8_t);
190: int get_option_uint32(uint32_t *, const struct dhcp_message *, uint8_t);
191: int get_option_uint16(uint16_t *, const struct dhcp_message *, uint8_t);
192: int get_option_uint8(uint8_t *, const struct dhcp_message *, uint8_t);
193: #define is_bootp(m) (m && \
194: !IN_LINKLOCAL(htonl((m)->yiaddr)) && \
195: get_option_uint8(NULL, m, DHO_MESSAGETYPE) == -1)
196: struct rt *get_option_routes(const char *, const struct dhcp_message *);
197: ssize_t configure_env(char **, const char *, const struct dhcp_message *,
198: const struct if_options *);
199:
200: int dhcp_message_add_addr(struct dhcp_message *, uint8_t, struct in_addr);
201: ssize_t make_message(struct dhcp_message **, const struct interface *,
202: uint8_t);
203: int valid_dhcp_packet(unsigned char *);
204:
205: ssize_t write_lease(const struct interface *, const struct dhcp_message *);
206: struct dhcp_message *read_lease(const struct interface *);
207: void get_lease(struct dhcp_lease *, const struct dhcp_message *);
208: #endif
209:
210: #endif