-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
proto_arp.c
330 lines (270 loc) · 8.44 KB
/
proto_arp.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* arp.c ARP processing.
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright (C) 2013 Network RADIUS SARL <info@networkradius.com>
*/
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/protocol.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/process.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/pcap.h>
#include <net/if_arp.h>
typedef struct arp_socket_t {
listen_socket_t lsock;
uint64_t counter;
RADCLIENT client;
} arp_socket_t;
/*
* ARP for ethernet && IPv4.
*/
typedef struct arp_over_ether {
uint16_t htype; //!< Format of hardware address.
uint16_t ptype; //!< Format of protocol address.
uint8_t hlen; //!< Length of hardware address.
uint8_t plen; //!< Length of protocol address.
uint8_t op; //!< 1 - Request, 2 - Reply.
uint8_t sha[ETHER_ADDR_LEN]; //!< sender hardware address.
uint8_t spa[4]; //!< Sender protocol address.
uint8_t tha[ETHER_ADDR_LEN]; //!< Target hardware address.
uint8_t tpa[4]; //!< Target protocol address.
} arp_over_ether_t;
static rlm_rcode_t arp_process(REQUEST *request)
{
CONF_SECTION *unlang;
request->server = request->listener->server;
request->server_cs = request->listener->server_cs;
unlang = cf_subsection_find(request->server_cs, "arp");
request->component = "arp";
return unlang_interpret(request, unlang, RLM_MODULE_NOOP);
}
/*
* Check if an incoming request is "ok"
*
* It takes packets, not requests. It sees if the packet looks
* OK. If so, it does a number of sanity checks on it.
*/
static int arp_socket_recv(rad_listen_t *listener)
{
int ret;
arp_socket_t *sock = listener->data;
pcap_t *handle = sock->lsock.pcap->handle;
const uint8_t *data;
struct pcap_pkthdr *header;
ssize_t link_len;
arp_over_ether_t const *arp;
RADIUS_PACKET *packet;
ret = pcap_next_ex(handle, &header, &data);
if (ret == 0) {
DEBUG("No packet retrieved from pcap.");
return 0; /* no packet */
}
if (ret < 0) {
ERROR("Error requesting next packet, got (%i): %s", ret, pcap_geterr(handle));
return 0;
}
link_len = fr_link_layer_offset(data, header->caplen, sock->lsock.pcap->link_layer);
if (link_len < 0) {
PERROR("Failed determining link layer header offset");
return 0;
}
/*
* Silently ignore it if it's too small to be ARP.
*
* This can happen when pcap gets overloaded and starts truncating packets.
*/
if (header->caplen < (link_len + sizeof(*arp))) {
ERROR("Packet too small, we require at least %zu bytes, got %i bytes",
link_len + sizeof(*arp), header->caplen);
return 0;
}
data += link_len;
arp = (arp_over_ether_t const *) data;
if (ntohs(arp->htype) != ARPHRD_ETHER) return 0;
if (ntohs(arp->ptype) != 0x0800) return 0;
if (arp->hlen != ETHER_ADDR_LEN) return 0; /* FIXME: malformed error */
if (arp->plen != 4) return 0; /* FIXME: malformed packet error */
packet = talloc_zero(NULL, RADIUS_PACKET);
if (!packet) return 0;
packet->dst_port = 1; /* so it's not a "fake" request */
packet->data_len = header->caplen - link_len;
packet->data = talloc_memdup(packet, arp, packet->data_len);
talloc_set_type(packet->data, uint8_t);
DEBUG("ARP received on interface %s", sock->lsock.interface);
if (!request_receive(NULL, listener, packet, &sock->client, arp_process)) {
fr_radius_free(&packet);
return 0;
}
return 1;
}
static int arp_socket_send(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
{
return 0;
}
static int arp_socket_encode(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
{
return 0;
}
typedef struct arp_decode_t {
char const *name;
size_t len;
} arp_decode_t;
static const arp_decode_t header_names[] = {
{ "ARP-Hardware-Format", 2 },
{ "ARP-Protocol-Format", 2 },
{ "ARP-Hardware-Address-Length", 1 },
{ "ARP-Protocol-Address-Length", 1 },
{ "ARP-Operation", 2 },
{ "ARP-Sender-Hardware-Address", 6 },
{ "ARP-Sender-Protocol-Address", 4 },
{ "ARP-Target-Hardware-Address", 6 },
{ "ARP-Target-Protocol-Address", 4 },
{ NULL, 0 }
};
static int arp_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
{
int i;
arp_over_ether_t const *arp;
uint8_t const *p;
arp = (arp_over_ether_t const *) request->packet->data;
/*
* arp_socket_recv() takes care of validating it's really
* our kind of ARP.
*/
for (i = 0, p = (uint8_t const *) arp; header_names[i].name != NULL; p += header_names[i].len, i++) {
ssize_t len;
fr_dict_attr_t const *da;
VALUE_PAIR *vp = NULL;;
vp_cursor_t cursor;
fr_radius_ctx_t decoder_ctx = {
.packet = request->packet,
.original = request->packet
};
da = fr_dict_attr_by_name(NULL, header_names[i].name);
if (!da) return 0;
fr_pair_cursor_init(&cursor, &vp);
len = fr_radius_decode_pair_value(request->packet, &cursor, da, p, header_names[i].len,
header_names[i].len, &decoder_ctx);
if (len <= 0) {
RDEBUG("Failed decoding %s: %s",
header_names[i].name, fr_strerror());
return 0;
}
debug_pair(vp);
fr_pair_add(&request->packet->vps, vp);
}
return 0;
}
/** Build PCAP filter string to pass to libpcap
* Will be called by init_pcap.
*
* @param this listen section (not used)
* @return PCAP filter string
*/
static const char * arp_pcap_filter_builder(UNUSED rad_listen_t *this)
{
return "arp";
}
static int arp_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
{
int rcode;
arp_socket_t *sock = this->data;
RADCLIENT *client;
CONF_PAIR *cp = NULL;
sock->lsock.pcap_filter_builder = arp_pcap_filter_builder;
sock->lsock.pcap_type = PCAP_INTERFACE_IN;
this->nodup = true; /* don't check for duplicates */
/* Add ipaddress to conf section as it is not required by ARP config */
cp = cf_pair_alloc(cs, "ipv4addr", "0.0.0.0", T_OP_SET, T_BARE_WORD, T_BARE_WORD);
cf_pair_add(cs, cp);
rcode = common_socket_parse(cs, this);
if (rcode != 0) return rcode;
if (!sock->lsock.interface) {
cf_log_err_cs(cs, "'interface' is required for arp");
return -1;
}
/*
* The server core is still RADIUS, and needs a client.
* So we fake one here.
*/
client = &sock->client;
memset(client, 0, sizeof(*client));
client->ipaddr.af = AF_INET;
client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
client->ipaddr.prefix = 0;
client->longname = client->shortname = sock->lsock.interface;
client->secret = client->shortname;
client->nas_type = talloc_typed_strdup(sock, "none");
return 0;
}
static int arp_socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
{
arp_socket_t *sock = this->data;
snprintf(buffer, bufsize, "arp interface %s", sock->lsock.interface);
return 1;
}
/*
* If there's no "arp" section, we can't bootstrap anything.
*/
static int arp_socket_bootstrap(CONF_SECTION *server_cs, UNUSED CONF_SECTION *listen_cs)
{
CONF_SECTION *cs;
cs = cf_subsection_find(server_cs, "arp");
if (!cs) {
cf_log_err_cs(server_cs, "No 'arp' sub-section found");
return -1;
}
return 0;
}
/*
* Ensure that the "arp" section is compiled.
*/
static int arp_socket_compile(CONF_SECTION *server_cs, UNUSED CONF_SECTION *listen_cs)
{
CONF_SECTION *cs;
cs = cf_subsection_find(server_cs, "arp");
if (!cs) {
cf_log_err_cs(server_cs, "No 'arp' sub-section found");
return -1;
}
cf_log_module(cs, "Loading arp {...}");
if (unlang_compile(cs, MOD_POST_AUTH) < 0) {
cf_log_err_cs(cs, "Failed compiling 'arp' section");
return -1;
}
return 0;
}
extern rad_protocol_t proto_arp;
rad_protocol_t proto_arp = {
.magic = RLM_MODULE_INIT,
.name = "arp",
.inst_size = sizeof(arp_socket_t),
.transports = 0,
.tls = false,
.bootstrap = arp_socket_bootstrap,
.compile = arp_socket_compile,
.parse = arp_socket_parse,
.open = common_socket_open,
.recv = arp_socket_recv,
.send = arp_socket_send,
.print = arp_socket_print,
.debug = common_packet_debug,
.encode = arp_socket_encode,
.decode = arp_socket_decode
};