-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootp.c
185 lines (158 loc) · 3.8 KB
/
bootp.c
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
/*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/
#include <stdio.h>
#include "ip.h"
#include "dat.h"
#include "protos.h"
enum
{
OfferTimeout= 60, /* when an offer times out */
MaxLease= 60*60, /* longest lease for dynamic binding */
MinLease= 15*60, /* shortest lease for dynamic binding */
StaticLease= 30*60, /* lease for static binding */
IPUDPHDRSIZE= 28, /* size of an IP plus UDP header */
MINSUPPORTED= 576, /* biggest IP message the client must support */
/* lengths of some bootp fields */
Maxhwlen= 16,
Maxfilelen= 128,
Maxoptlen= 312-4,
/* bootp types */
Bootrequest= 1,
Bootreply= 2,
/* bootp flags */
Fbroadcast= 1<<15,
};
typedef struct Hdr Hdr;
struct Hdr
{
uint8_t op; /* opcode */
uint8_t htype; /* hardware type */
uint8_t hlen; /* hardware address len */
uint8_t hops; /* hops */
uint8_t xid[4]; /* a random number */
uint8_t secs[2]; /* elapsed since client started booting */
uint8_t flags[2];
uint8_t ciaddr[IPv4addrlen]; /* client IP address (client tells server) */
uint8_t yiaddr[IPv4addrlen]; /* client IP address (server tells client) */
uint8_t siaddr[IPv4addrlen]; /* server IP address */
uint8_t giaddr[IPv4addrlen]; /* gateway IP address */
uint8_t chaddr[Maxhwlen]; /* client hardware address */
char sname[64]; /* server host name (optional) */
char file[Maxfilelen]; /* boot file name */
uint8_t optmagic[4];
uint8_t optdata[Maxoptlen];
};
enum
{
Oca,
Osa,
Ot,
};
static Field p_fields[] =
{
{"ca", Fv4ip, Oca, "client IP addr", } ,
{"sa", Fv4ip, Osa, "server IP addr", } ,
{0}
};
#define plan9opt ((uint32_t)(('p'<<24) | ('9'<<16) | (' '<<8) | ' '))
#define genericopt (0x63825363UL)
static Mux p_mux[] =
{
{"dhcp", genericopt,},
{"plan9bootp", plan9opt,},
{"dump", 0,},
{0}
};
static void
p_compile(Filter *f)
{
Mux *m;
if(f->op == '='){
compile_cmp(bootp.name, f, p_fields);
return;
}
for(m = p_mux; m->name != NULL; m++)
if(strcmp(f->s, m->name) == 0){
f->pr = m->pr;
f->ulv = m->val;
f->subop = Ot;
return;
}
sysfatal( "unknown bootp field: %s", f->s);
}
static int
p_filter(Filter *f, Msg *m)
{
Hdr *h;
h = (Hdr*)m->ps;
if(m->pe < (uint8_t*)h->sname)
return 0;
m->ps = h->optdata;
switch(f->subop){
case Oca:
return NetL(h->ciaddr) == f->ulv || NetL(h->yiaddr) == f->ulv;
case Osa:
return NetL(h->siaddr) == f->ulv;
case Ot:
return NetL(h->optmagic) == f->ulv;
}
return 0;
}
static char*
op(int i)
{
static char x[20];
switch(i){
case Bootrequest:
return "Req";
case Bootreply:
return "Rep";
default:
sprintf(x, "%d", i);
return x;
}
}
static int
p_seprint(Msg *m)
{
Hdr *h;
uint32_t x;
h = (Hdr*)m->ps;
if(m->pe < (uint8_t*)h->sname)
return -1;
/* point past data */
m->ps = h->optdata;
/* next protocol */
m->pr = NULL;
if(m->pe >= (uint8_t*)h->optdata){
x = NetL(h->optmagic);
demux(p_mux, x, x, m, &dump);
}
m->p = seprint(m->p, m->e, "t=%s ht=%d hl=%d hp=%d xid=%x sec=%d fl=%4.4x ca=%V ya=%V sa=%V ga=%V cha=%E magic=%lx",
op(h->op), h->htype, h->hlen, h->hops,
NetL(h->xid), NetS(h->secs), NetS(h->flags),
h->ciaddr, h->yiaddr, h->siaddr, h->giaddr, h->chaddr,
(uint32_t)NetL(h->optmagic));
if(m->pe > (uint8_t*)h->sname && *h->sname)
m->p = seprint(m->p, m->e, " snam=%s", h->sname);
if(m->pe > (uint8_t*)h->file && *h->file)
m->p = seprint(m->p, m->e, " file=%s", h->file);
return 0;
}
Proto bootp =
{
"bootp",
p_compile,
p_filter,
p_seprint,
p_mux,
"%#.8lux",
p_fields,
defaultframer,
};