summaryrefslogtreecommitdiff
path: root/accel-pppd/ctrl/ipoe/dhcpv4_options.c
blob: b5f2b3bf317b7b24b6bebb9f2f1fa0e5583c4e34 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>

#include "dhcpv4.h"

struct known_option
{
	int type;
	int min_len;
	int max_len;
	int elem_size;
	const char *name;
	void (*print)(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
};

static void print_int(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_uint(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_ip(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_str(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_hex(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_route(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_classless_route(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_message_type(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_request_list(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));
static void print_relay_agent(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...));

static struct known_option options[] = {
	{   1,  4,   4,  4, "Subnet", print_ip },
	{   2,  4,   4,  4, "Time-Offset", print_int },
	{   3,  4, 255,  4, "Router", print_ip },
	{   4,  4, 255,  4, "Time-Server", print_ip },
	{   5,  4, 255,  4, "Name-Server", print_ip },
	{   6,  4, 255,  4, "DNS", print_ip },
	//{   7,  4, 255,  4, "log-server", print_ip },
	//{   8,  4, 255,  4, "cookie-server", print_ip },
	//{   9,  4, 255,  4, "lpr-server", print_ip },
	//{  10,  4, 255,  4, "impress-server", print_ip },
	//{  11,  4, 255,  4, "resourse-location", print_ip },
	{  12,  1, 255,  1, "Host-Name", print_str },
	//{  13,  4, 255,  4, "impress-server", print_ip },
	{  15,  1, 255,  1, "Domain-Name", print_str },
	{  26,  2,   2,  2, "MTU", print_int },
	{  28,  4,   4,  4, "Broadcast", print_ip },
	{  33,  8, 255,  8, "Route", print_route },
	{  42,  4,   4,  4, "NTP", print_ip },
	{  43,  1, 255,  1, "Vendor-Specific", print_hex },
	{  50,  4,   4,  4, "Request-IP", print_ip },
	{  51,  4,   4,  4, "Lease-Time", print_uint },
	{  53,  1,   1,  1, "Message-Type", print_message_type },
	{  54,  4,   4,  4, "Server-ID", print_ip },
	{  55,  1, 255,  1, "Request-List", print_request_list },
	{  56,  1, 255,  1, "Message", print_str },
	{  57,  2,   2,  2, "Max-Message-Size", print_uint },
	{  58,  4,   4,  4, "T1", print_uint },
	{  59,  4,   4,  4, "T2", print_uint },
	{  60,  1, 255,  1, "Vendor-Class", print_hex },
	{  61,  2, 255,  1, "Client-ID", print_hex },
	{  82,  3, 255,  1, "Relay-Agent", print_relay_agent },
	{ 121,  5, 255,  1, "Classless-Route", print_classless_route },
  { 0 },
};

int dhcpv4_check_options(struct dhcpv4_packet *pack)
{
	struct dhcpv4_option *opt;
	struct known_option *kopt;

	list_for_each_entry(opt, &pack->options, entry) {
		for (kopt = options; kopt->type; kopt++) {
			if (kopt->type != opt->type)
				continue;
			if (opt->len < kopt->min_len)
				return -1;
			if (opt->len > kopt->max_len)
				return -1;
			if (opt->len % kopt->elem_size != 0)
				return -1;
			break;
		}
	}

	return 0;
}

void dhcpv4_print_options(struct dhcpv4_packet *pack, void (*print)(const char *fmt, ...))
{
	struct dhcpv4_option *opt;
	struct known_option *kopt;
	int n = 0;

	list_for_each_entry(opt, &pack->options, entry) {
		if (n)
			print(" <");
		else
			print("<");
		n++;
		for (kopt = options; kopt->type && kopt->type != opt->type; kopt++);
		if (kopt->type) {
			print("%s ", kopt->name);
			kopt->print(opt, kopt->elem_size, print);
		} else {
			print("Option-%i ", opt->type);
			print_hex(opt, 1, print);
		}
		print(">");
	}
}


static void print_int(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	if (opt->len == 2)
		print("%i", ntohs(*(int16_t *)(opt->data)));
	else
		print("%i", ntohl(*(int32_t *)(opt->data)));
}

static void print_uint(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	if (opt->len == 2)
		print("%u", ntohs(*(uint16_t *)(opt->data)));
	else
		print("%u", ntohl(*(uint32_t *)(opt->data)));
}

static void print_ip(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	int i, n = opt->len / elem_size;
	uint32_t ip;

	for (i = 0; i < n; i++) {
		ip = ntohl(*(uint32_t *)(opt->data + i*elem_size));

		if (i)
			print(",");

		print("%i.%i.%i.%i",
				(ip >> 24) & 0xff,
				(ip >> 16) & 0xff,
				(ip >> 8) & 0xff,
				ip & 0xff);
	}
}

static void print_str(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	const char *ptr = (const char *)opt->data;
	const char *endptr = ptr + opt->len;

	for(; ptr < endptr; ptr++)
		print("%c", *ptr);
}

static void print_hex(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	const uint8_t *ptr = opt->data;
	const uint8_t *endptr = ptr + opt->len;

	for(; ptr < endptr; ptr++)
		print("%02x", *ptr);
}

static void print_route(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	int i, n = opt->len / 8;
	uint32_t ip, gw;

	for (i = 0; i < n; i++) {
		ip = ntohl(*(uint32_t *)(opt->data + i*8));
		gw = ntohl(*(uint32_t *)(opt->data + i*8 + 4));

		if (i)
			print(",");

		print("%i.%i.%i.%i via %i.%i.%i.%i",
				(ip >> 24) & 0xff,
				(ip >> 16) & 0xff,
				(ip >> 8) & 0xff,
				ip & 0xff,
				(gw >> 24) & 0xff,
				(gw >> 16) & 0xff,
				(gw >> 8) & 0xff,
				gw & 0xff);
	}
}

static void print_message_type(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	const char *msg_name[] = {"", "Discover", "Offer", "Request", "Decline", "Ack", "Nak", "Release", "Inform"};

	print("%s", msg_name[opt->data[0]]);
}

static void print_request_list(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	int i;
	struct known_option *kopt;

	for (i = 0; i < opt->len; i++) {
		if (i)
			print(",");
		for (kopt = options; kopt->type && kopt->type != opt->data[i]; kopt++);
		if (kopt->type)
			print("%s", kopt->name);
		else
			print("%i", opt->data[i]);
	}
}

static void print_relay_agent(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	const uint8_t *ptr = opt->data;
	const uint8_t *endptr = ptr + opt->len;
	const uint8_t *endptr1;
	int type, len;

	while (ptr < endptr) {
		if (ptr != opt->data)
			print(" ");
		type = *ptr++;
		len = *ptr++;
		/*if (ptr + len > endptr) {
			print(" invalid");
			return;
		}*/
		if (type == 1)
			print("{Agent-Circuit-ID ");
		else if (type == 2)
			print("{Agent-Remote-ID ");
		else
			print("{Option-%i ", type);

		endptr1 = ptr + len;
		for (;ptr < endptr1; ptr++) {
			if (!isprint(*ptr)) {
				print("_");
				break;
			}
			print("%c", *ptr);
		}
		for (;ptr < endptr1; ptr++)
			print("%02x", *ptr);
		print("}");
	}
}

static void print_classless_route(const struct dhcpv4_option *opt, int elem_size, void (*print)(const char *fmt, ...))
{
	const uint8_t *ptr = opt->data;
	const uint8_t *endptr = ptr + opt->len;
	int mask, i, mask1 = 0;
	uint32_t ip;
	uint32_t gw;

	while (ptr < endptr) {
		if (ptr != opt->data)
			print(",");

		mask = *ptr++;
		ip = ntohl(*(uint32_t *)ptr);
		for (i = 0; i < mask; i++)
			mask1 |= (1 << (32 - i));
		ip &= mask1;
		if (mask <= 8)
			ptr++;
		else if (mask <= 16)
			ptr += 2;
		else if (mask <= 24)
			ptr += 3;
		else
			ptr += 4;
		gw = ntohl(*(uint32_t *)ptr);
		ptr += 4;

		print("%i.%i.%i.%i/%i via %i.%i.%i.%i",
				(ip >> 24) & 0xff,
				(ip >> 16) & 0xff,
				(ip >> 8) & 0xff,
				ip & 0xff,
				mask,
				(gw >> 24) & 0xff,
				(gw >> 16) & 0xff,
				(gw >> 8) & 0xff,
				gw & 0xff);
	}
}