-
Notifications
You must be signed in to change notification settings - Fork 49
/
arp.c
616 lines (519 loc) · 15.2 KB
/
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* ARP support - needed for DHCP, IPv4LL, and maybe others
*
* Copyright (C) 2010-2012, Olaf Kirch <okir@suse.de>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <errno.h>
#include <wicked/netinfo.h>
#include <wicked/socket.h>
#include "netinfo_priv.h"
#include "socket_priv.h"
#include "array_priv.h"
#include "buffer.h"
static void ni_arp_socket_recv(ni_socket_t *);
static int ni_arp_parse(ni_arp_socket_t *, ni_buffer_t *, ni_arp_packet_t *);
/*
* Open ARP socket
*/
ni_arp_socket_t *
ni_arp_socket_open(const ni_capture_devinfo_t *dev_info, ni_arp_callback_t *callback, void *calldata)
{
ni_capture_protinfo_t prot_info;
ni_arp_socket_t *arph;
arph = calloc(1, sizeof(*arph));
ni_capture_devinfo_copy(&arph->dev_info, dev_info);
arph->callback = callback;
arph->user_data = calldata;
memset(&prot_info, 0, sizeof(prot_info));
prot_info.eth_protocol = ETHERTYPE_ARP;
arph->capture = ni_capture_open(dev_info, &prot_info, ni_arp_socket_recv, "arp");
if (!arph->capture) {
ni_arp_socket_close(arph);
return NULL;
}
ni_capture_set_user_data(arph->capture, arph);
return arph;
}
void
ni_arp_socket_close(ni_arp_socket_t *arph)
{
ni_capture_free(arph->capture);
ni_capture_devinfo_destroy(&arph->dev_info);
free(arph);
}
/*
* This callback is invoked from the socket code when we
* detect an incoming ARP packet on the raw socket.
*/
static void
ni_arp_socket_recv(ni_socket_t *sock)
{
ni_capture_t *capture = sock->user_data;
ni_buffer_t buf;
if (ni_capture_recv(capture, &buf, NULL) >= 0) {
ni_arp_socket_t *arph = ni_capture_get_user_data(capture);
ni_arp_packet_t packet;
if (ni_arp_parse(arph, &buf, &packet) >= 0)
arph->callback(arph, &packet, arph->user_data);
}
}
int
ni_arp_send_request(ni_arp_socket_t *arph, struct in_addr sip, struct in_addr tip)
{
ni_arp_packet_t packet;
memset(&packet, 0, sizeof(packet));
packet.op = ARPOP_REQUEST;
packet.sip = sip;
packet.sha = arph->dev_info.hwaddr;
packet.tip = tip;
return ni_arp_send(arph, &packet);
}
int
ni_arp_send_reply(ni_arp_socket_t *arph, struct in_addr sip,
const ni_hwaddr_t *tha, struct in_addr tip)
{
ni_arp_packet_t packet;
memset(&packet, 0, sizeof(packet));
packet.op = ARPOP_REPLY;
packet.sip = sip;
packet.sha = arph->dev_info.hwaddr;
packet.tip = tip;
if (tha)
packet.tha = *tha;
return ni_arp_send(arph, &packet);
}
int
ni_arp_send_grat_reply(ni_arp_socket_t *arph, struct in_addr sip)
{
ni_arp_packet_t packet;
memset(&packet, 0, sizeof(packet));
packet.op = ARPOP_REPLY;
packet.sip = sip;
packet.sha = arph->dev_info.hwaddr;
packet.tip = sip;
packet.tha = arph->dev_info.hwaddr;
return ni_arp_send(arph, &packet);
}
/*
* See e.g. https://tools.ietf.org/html/rfc5227#section-3:
* Gratuitous ARP request are more likely to work correctly as
* requests are always broadcasts and replies may be expected
* to be unicasts only and dropped by incorrect implementations.
*/
int
ni_arp_send_grat_request(ni_arp_socket_t *arph, struct in_addr sip)
{
ni_arp_packet_t packet;
memset(&packet, 0, sizeof(packet));
packet.op = ARPOP_REQUEST;
packet.sip = sip;
packet.sha = arph->dev_info.hwaddr;
packet.tip = sip;
ni_link_address_get_broadcast(arph->dev_info.hwaddr.type, &packet.tha);
return ni_arp_send(arph, &packet);
}
int
ni_arp_send(ni_arp_socket_t *arph, const ni_arp_packet_t *packet)
{
unsigned int hwlen, pktlen;
struct arphdr *arp;
ni_buffer_t buf;
int rv;
hwlen = ni_link_address_length(arph->dev_info.hwaddr.type);
pktlen = sizeof(*arp) + 2 * hwlen + 2 * 4;
arp = calloc(1, pktlen);
ni_buffer_init(&buf, arp, pktlen);
arp = ni_buffer_push_tail(&buf, sizeof(*arp));
arp->ar_hrd = htons(arph->dev_info.hwaddr.type);
arp->ar_pro = htons(ETHERTYPE_IP);
arp->ar_hln = hwlen;
arp->ar_pln = 4;
arp->ar_op = htons(packet->op);
if (packet->sha.len == hwlen) {
ni_buffer_put(&buf, packet->sha.data, packet->sha.len);
} else {
ni_buffer_put(&buf, NULL, hwlen);
}
ni_buffer_put(&buf, &packet->sip, 4);
if (packet->tha.len == hwlen) {
ni_buffer_put(&buf, packet->tha.data, packet->tha.len);
} else {
ni_buffer_put(&buf, NULL, hwlen);
}
ni_buffer_put(&buf, &packet->tip, 4);
rv = ni_capture_send(arph->capture, &buf, NULL);
free(buf.base);
return rv;
}
int
ni_arp_parse(ni_arp_socket_t *arph, ni_buffer_t *bp, ni_arp_packet_t *p)
{
struct arphdr *arp;
if (!(arp = ni_buffer_pull_head(bp, sizeof(*arp))))
return -1;
if (arp->ar_pro != htons(ETHERTYPE_IP)
|| arp->ar_pln != 4)
return -1;
p->op = ntohs(arp->ar_op);
p->sha.type = arph->dev_info.hwaddr.type;
p->sha.len = ni_link_address_length(arph->dev_info.hwaddr.type);
p->tha = p->sha;
if (ni_buffer_get(bp, p->sha.data, p->sha.len) < 0 || ni_buffer_get(bp, &p->sip, 4) < 0
|| ni_buffer_get(bp, p->tha.data, p->tha.len) < 0 || ni_buffer_get(bp, &p->tip, 4) < 0)
return -1;
return 0;
}
unsigned long
ni_arp_timeout_left(struct timeval *started, const struct timeval *current, unsigned long msec)
{
struct timeval dif, end;
unsigned long left = 0;
if (timerisset(started) && timercmp(current, started, >)) {
dif.tv_sec = msec / 1000;
dif.tv_usec = (msec % 1000) * 1000;
timeradd(started, &dif, &end);
if (timercmp(&end, current, >)) {
timersub(&end, current, &dif);
left = dif.tv_sec * 1000 + dif.tv_usec / 1000;
}
}
return left;
}
static void
ni_arp_address_free(ni_arp_address_t *aa)
{
ni_address_free(aa->address);
free(aa);
}
static ni_define_ptr_array_init(ni_arp_address);
static ni_define_ptr_array_destroy(ni_arp_address);
static ni_define_ptr_array_realloc(ni_arp_address, NI_ARP_ADDRESS_ARRAY_CHUNK);
static ni_define_ptr_array_append(ni_arp_address);
static ni_bool_t
ni_arp_address_array_append_addr(ni_arp_address_array_t *arr, ni_address_t *addr)
{
ni_arp_address_t *e;
if (!(e = calloc(1, sizeof(*e))))
return FALSE;
e->address = ni_address_ref(addr);
if (e->address && ni_arp_address_array_append(arr, e))
return TRUE;
ni_arp_address_free(e);
return FALSE;
}
static ni_arp_address_t *
ni_arp_address_array_find_match_addr(ni_arp_address_array_t *array, ni_address_t *ap,
unsigned int *index, ni_bool_t (*match)(const ni_address_t *, const ni_address_t *))
{
ni_arp_address_t *a;
unsigned int i;
if (array) {
match = match ?: ni_address_equal_ref;
for (i = index ? *index : 0; i < array->count; ++i) {
a = array->data[i];
if (match(a->address, ap)) {
if (index)
*index = i;
return a;
}
}
}
if (index)
*index = -1U;
return NULL;
}
void
ni_arp_verify_init(ni_arp_verify_t *vfy, const ni_config_arp_verify_t *cfg)
{
memset(vfy, 0, sizeof(*vfy));
vfy->nprobes = cfg->count;
vfy->nretry = cfg->retries;
vfy->probe_min_ms = cfg->interval.min;
vfy->probe_max_ms = cfg->interval.max;
}
void
ni_arp_verify_reset(ni_arp_verify_t *vfy, const ni_config_arp_verify_t *cfg)
{
vfy->nprobes = cfg->count;
vfy->nretry = cfg->retries;
vfy->probe_min_ms = cfg->interval.min;
vfy->probe_max_ms = cfg->interval.max;
timerclear(&vfy->started);
vfy->last_timeout = 0;
ni_arp_address_array_destroy(&vfy->ipaddrs);
}
void
ni_arp_verify_destroy(ni_arp_verify_t *vfy)
{
ni_arp_address_array_destroy(&vfy->ipaddrs);
memset(vfy, 0, sizeof(*vfy));
}
unsigned int
ni_arp_verify_add_address(ni_arp_verify_t *vfy, ni_address_t *ap)
{
if (!vfy || !ap || !vfy->nprobes)
return 0;
if (ap->family != AF_INET || !ni_sockaddr_is_ipv4_specified(&ap->local_addr))
return 0;
if (ni_arp_address_array_find_match_addr(&vfy->ipaddrs, ap, NULL, ni_address_equal_local_addr))
return 0; /* already have it */
if (!ni_arp_address_array_append_addr(&vfy->ipaddrs, ap))
return 0;
return vfy->ipaddrs.count;
}
unsigned int
ni_arp_verify_add_in_addr(ni_arp_verify_t * vfy, struct in_addr addr)
{
unsigned int ret;
ni_sockaddr_t sockaddr;
ni_address_t *address;
memset(&sockaddr, 0, sizeof(sockaddr));
ni_sockaddr_set_ipv4(&sockaddr, addr, 0);
if (!(address = ni_address_create(AF_INET, 32, &sockaddr, NULL)))
return 0;
ni_address_set_tentative(address, TRUE);
ret = ni_arp_verify_add_address(vfy, address);
ni_address_free(address);
return ret;
}
ni_arp_address_t *
ni_arp_reply_match_address(ni_arp_socket_t *sock, ni_arp_address_array_t *ipaddrs,
const ni_arp_packet_t *pkt)
{
ni_netconfig_t *nc = ni_global_state_handle(0);
const ni_netdev_t *dev;
ni_bool_t false_alarm = FALSE;
ni_bool_t found_addr = FALSE;
ni_arp_address_t *vap;
ni_address_t sip;
if (!sock || !pkt || pkt->op != ARPOP_REPLY || !ipaddrs)
return NULL;
memset(&sip, 0, sizeof(sip));
ni_sockaddr_set_ipv4(&sip.local_addr, pkt->sip, 0);
vap = ni_arp_address_array_find_match_addr(ipaddrs, &sip, NULL, ni_address_equal_local_addr);
if (!vap) {
ni_debug_application("%s: ignore report about unrelated address %s from %s",
sock->dev_info.ifname, ni_sockaddr_print(&sip.local_addr),
ni_link_address_print(&pkt->sha));
return NULL;
}
if (ni_address_is_duplicate(vap->address))
return vap;
/* Ignore any ARP replies that seem to come from our own MAC
* address. Some helpful switches seem to generate them.
*/
if (ni_link_address_equal(&sock->dev_info.hwaddr, &pkt->sha)) {
ni_debug_application("%s: ignore address %s in use by our own mac address %s",
sock->dev_info.ifname, ni_sockaddr_print(&sip.local_addr),
ni_link_address_print(&pkt->sha));
return NULL;
}
/* As well as ARP replies that seem to come from our own host:
* dup if same address, not a dup if there are two interfaces
* connected to the same broadcast domain.
*/
for (dev = ni_netconfig_devlist(nc); dev; dev = dev->next) {
if (dev->link.ifindex == sock->dev_info.ifindex)
continue;
if (!ni_netdev_link_is_up(dev))
continue;
if (!ni_link_address_equal(&dev->link.hwaddr, &pkt->sha))
continue;
/* OK, we have an interface matching the hwaddr,
* which will answer arp requests when it is on
* the same broadcast domain and causes a false
* alarm, except it really has the IP assigned.
*/
false_alarm = TRUE;
found_addr = !!ni_address_list_find(dev->addrs, &vap->address->local_addr);
}
if (false_alarm && !found_addr) {
ni_debug_application("%s: reply from one of our interfaces",
sock->dev_info.ifname);
return NULL;
}
return vap;
}
ni_arp_send_status_t
ni_arp_verify_send(ni_arp_socket_t *sock, ni_arp_verify_t *vfy, ni_timeout_t *timeout)
{
static struct in_addr null = { 0 };
const struct in_addr *ip;
unsigned int i;
struct timeval now;
ni_address_t *ap;
ni_arp_address_t *vap;
ni_bool_t need_wait;
if (!sock || !vfy || !timeout)
return NI_ARP_SEND_FAILURE;
ni_timer_get_time(&now);
if ((*timeout = ni_arp_timeout_left(&vfy->started, &now, vfy->last_timeout)))
return NI_ARP_SEND_PROGRESS;
for (i = 0; i < vfy->ipaddrs.count; ++i) {
vap = vfy->ipaddrs.data[i];
ap = vap->address;
if (ni_address_is_duplicate(ap))
continue;
if (!ni_address_is_tentative(ap))
continue;
if (vap->nattempts >= vfy->nprobes)
ni_address_set_tentative(ap, FALSE);
}
need_wait = FALSE;
vfy->started = now;
for (i = 0; i < vfy->ipaddrs.count; ++i) {
vap = vfy->ipaddrs.data[i];
ap = vap->address;
if (ni_address_is_duplicate(ap))
continue;
if (!ni_address_is_tentative(ap))
continue;
ni_debug_application("%s: sending arp verify for IP %s (%u/%u)",
sock->dev_info.ifname, ni_sockaddr_print(&ap->local_addr),
vap->nattempts + 1, vfy->nprobes);
ip = &ap->local_addr.sin.sin_addr;
if (ni_arp_send_request(sock, null, *ip) > 0) {
vap->nattempts++;
need_wait = TRUE;
} else {
if (errno == ENOBUFS) {
vap->nerrors++;
if (vap->nerrors >= vfy->nretry) {
ni_error("%s: ARP verify failed for %s - max (%u) retry!",
sock->dev_info.ifname,
ni_sockaddr_print(&ap->local_addr),
vfy->nretry);
ni_address_set_duplicate(ap, TRUE);
} else {
need_wait = TRUE;
ni_debug_application("%s: ARP verify failed for %s -"
" ENOBUFS, probes:%u/%u errors:%u/%u",
sock->dev_info.ifname,
ni_sockaddr_print(&ap->local_addr),
vap->nattempts, vfy->nprobes,
vap->nerrors, vfy->nretry);
}
} else {
ni_error("%s: ARP verify send failed for %s - unexpected error!",
sock->dev_info.ifname,
ni_sockaddr_print(&ap->local_addr));
ni_address_set_duplicate(ap, TRUE);
}
}
}
if (need_wait) {
vfy->last_timeout = ni_timeout_random_range(vfy->probe_min_ms, vfy->probe_max_ms);
*timeout = vfy->last_timeout;
return NI_ARP_SEND_PROGRESS;
}
for (i = 0; i < vfy->ipaddrs.count; ++i) {
vap = vfy->ipaddrs.data[i];
if (vap->nattempts > 0)
return NI_ARP_SEND_COMPLETE;
}
return NI_ARP_SEND_FAILURE;
}
void
ni_arp_notify_init(ni_arp_notify_t *nfy, const ni_config_arp_notify_t *cfg)
{
memset(nfy, 0, sizeof(*nfy));
nfy->nclaims = cfg->count;
nfy->nretry = cfg->retries;
nfy->wait_ms = cfg->interval;
}
void
ni_arp_notify_reset(ni_arp_notify_t *nfy, const ni_config_arp_notify_t *cfg)
{
nfy->nclaims = cfg->count;
nfy->nretry = cfg->retries;
nfy->wait_ms = cfg->interval;
timerclear(&nfy->started);
ni_arp_address_array_destroy(&nfy->ipaddrs);
}
void
ni_arp_notify_destroy(ni_arp_notify_t *nfy)
{
ni_arp_address_array_destroy(&nfy->ipaddrs);
memset(nfy, 0, sizeof(*nfy));
}
unsigned int
ni_arp_notify_add_address(ni_arp_notify_t *nfy, ni_address_t *ap)
{
if (!nfy || !ap || !nfy->nclaims)
return 0;
if (ap->family != AF_INET || !ni_sockaddr_is_ipv4_specified(&ap->local_addr))
return 0;
if (ni_arp_address_array_find_match_addr(&nfy->ipaddrs, ap, NULL, ni_address_equal_local_addr))
return 0; /* already have it */
if (!ni_arp_address_array_append_addr(&nfy->ipaddrs, ap))
return 0;
return nfy->ipaddrs.count;
}
ni_bool_t
ni_arp_notify_send(ni_arp_socket_t *sock, ni_arp_notify_t *nfy, ni_timeout_t *timeout)
{
const struct in_addr *ip;
unsigned int i;
struct timeval now;
ni_arp_address_t *aa;
ni_address_t *ap;
ni_bool_t need_wait;
if (!sock || !nfy || !timeout)
return FALSE;
ni_timer_get_time(&now);
if ((*timeout = ni_arp_timeout_left(&nfy->started, &now, nfy->wait_ms)))
return TRUE;
if (nfy->nclaims && nfy->ipaddrs.count) {
nfy->started = now;
need_wait = FALSE;
for (i = 0; i < nfy->ipaddrs.count; ++i) {
aa = nfy->ipaddrs.data[i];
ap = aa->address;
if (ni_address_is_duplicate(ap))
continue;
if (ni_address_is_tentative(ap))
continue;
if ((nfy->nretry && aa->nerrors >= nfy->nretry) ||
aa->nattempts >= nfy->nclaims)
continue;
ni_debug_application("%s: sending arp notify for IP %s (%u/%u)",
sock->dev_info.ifname, ni_sockaddr_print(&ap->local_addr),
aa->nattempts + 1, nfy->nclaims);
ip = &ap->local_addr.sin.sin_addr;
if (ni_arp_send_grat_request(sock, *ip) > 0) {
aa->nattempts++;
need_wait = TRUE;
} else {
if (errno == ENOBUFS) {
aa->nerrors++;
if (aa->nerrors >= nfy->nretry) {
ni_error("%s: ARP notify failed for %s - max (%u) retry!",
sock->dev_info.ifname,
ni_sockaddr_print(&ap->local_addr),
nfy->nretry);
} else {
need_wait = TRUE;
}
} else {
ni_error("%s: ARP notify send failed for %s - unexpected error!",
sock->dev_info.ifname,
ni_sockaddr_print(&ap->local_addr));
ni_address_set_duplicate(ap, TRUE);
}
}
}
if (need_wait) {
*timeout = nfy->wait_ms;
return TRUE;
}
}
return FALSE;
}