summaryrefslogtreecommitdiff
path: root/usr.sbin/eigrpd/packet.c
blob: 6b343ce7414c86e9de8954674781ff23fcb5edb7 (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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*	$OpenBSD: packet.c,v 1.14 2016/04/15 13:21:45 renato Exp $ */

/*
 * Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <net/if_dl.h>
#include <errno.h>
#include <string.h>

#include "eigrpd.h"
#include "eigrp.h"
#include "log.h"
#include "eigrpe.h"

extern struct eigrpd_conf	*econf;

int		 ip_hdr_sanity_check(const struct ip *, uint16_t);
int		 eigrp_hdr_sanity_check(int, union eigrpd_addr *,
		    struct eigrp_hdr *, uint16_t, const struct iface *);
struct iface	*find_iface(unsigned int, int, union eigrpd_addr *);

int
gen_eigrp_hdr(struct ibuf *buf, uint16_t opcode, uint8_t flags,
    uint32_t seq_num, uint16_t as)
{
	struct eigrp_hdr	eigrp_hdr;

	memset(&eigrp_hdr, 0, sizeof(eigrp_hdr));
	eigrp_hdr.version = EIGRP_VERSION;
	eigrp_hdr.opcode = opcode;
	/* chksum will be set later */
	eigrp_hdr.flags = htonl(flags);
	eigrp_hdr.seq_num = htonl(seq_num);
	/* ack_num will be set later */
	eigrp_hdr.vrid = htons(EIGRP_VRID_UNICAST_AF);
	eigrp_hdr.as = htons(as);

	return (ibuf_add(buf, &eigrp_hdr, sizeof(eigrp_hdr)));
}

/* send and receive packets */
static int
send_packet_v4(struct iface *iface, struct nbr *nbr, struct ibuf *buf)
{
	struct sockaddr_in	 dst;
	struct msghdr		 msg;
	struct iovec		 iov[2];
	struct ip		 ip_hdr;

	/* setup sockaddr */
	dst.sin_family = AF_INET;
	dst.sin_len = sizeof(struct sockaddr_in);
	if (nbr)
		dst.sin_addr = nbr->addr.v4;
	else
		dst.sin_addr.s_addr = AllEIGRPRouters_v4;

	/* setup IP hdr */
	memset(&ip_hdr, 0, sizeof(ip_hdr));
	ip_hdr.ip_v = IPVERSION;
	ip_hdr.ip_hl = sizeof(ip_hdr) >> 2;
	ip_hdr.ip_tos = IPTOS_PREC_INTERNETCONTROL;
	ip_hdr.ip_len = htons(ibuf_size(buf) + sizeof(ip_hdr));
	ip_hdr.ip_id = 0;  /* 0 means kernel set appropriate value */
	ip_hdr.ip_off = 0;
	ip_hdr.ip_ttl = EIGRP_IP_TTL;
	ip_hdr.ip_p = IPPROTO_EIGRP;
	ip_hdr.ip_sum = 0;
	ip_hdr.ip_src.s_addr = if_primary_addr(iface);
	ip_hdr.ip_dst = dst.sin_addr;

	/* setup buffer */
	memset(&msg, 0, sizeof(msg));
	iov[0].iov_base = &ip_hdr;
	iov[0].iov_len = sizeof(ip_hdr);
	iov[1].iov_base = buf->buf;
	iov[1].iov_len = ibuf_size(buf);
	msg.msg_name = &dst;
	msg.msg_namelen = sizeof(dst);
	msg.msg_iov = iov;
	msg.msg_iovlen = 2;

	/* set outgoing interface for multicast traffic */
	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr)))
		if (if_set_ipv4_mcast(iface) == -1) {
			log_warn("%s: error setting multicast interface, %s",
			    __func__, iface->name);
			return (-1);
		}

	if (sendmsg(global.eigrp_socket_v4, &msg, 0) == -1) {
		log_warn("%s: error sending packet on interface %s",
		    __func__, iface->name);
		return (-1);
	}

	return (0);
}

static int
send_packet_v6(struct iface *iface, struct nbr *nbr, struct ibuf *buf)
{
	struct sockaddr_in6	 sa6;
	struct in6_addr		 maddr = AllEIGRPRouters_v6;

	/* setup sockaddr */
	memset(&sa6, 0, sizeof(sa6));
	sa6.sin6_family = AF_INET6;
	sa6.sin6_len = sizeof(struct sockaddr_in6);
	if (nbr) {
		sa6.sin6_addr = nbr->addr.v6;
		addscope(&sa6, iface->ifindex);
	} else
		sa6.sin6_addr = maddr;

	/* set outgoing interface for multicast traffic */
	if (IN6_IS_ADDR_MULTICAST(&sa6.sin6_addr))
		if (if_set_ipv6_mcast(iface) == -1) {
			log_warn("%s: error setting multicast interface, %s",
			    __func__, iface->name);
			return (-1);
		}

	if (sendto(global.eigrp_socket_v6, buf->buf, buf->wpos, 0,
	    (struct sockaddr *)&sa6, sizeof(sa6)) == -1) {
		log_warn("%s: error sending packet on interface %s",
		    __func__, iface->name);
		return (-1);
	}

	return (0);
}

int
send_packet(struct eigrp_iface *ei, struct nbr *nbr, uint32_t flags,
    struct ibuf *buf)
{
	struct eigrp		*eigrp = ei->eigrp;
	struct iface		*iface = ei->iface;
	struct eigrp_hdr	*eigrp_hdr;

	if (!(iface->flags & IFF_UP) || !LINK_STATE_IS_UP(iface->linkstate))
		return (-1);

	/* update ack number, flags and checksum */
	if ((eigrp_hdr = ibuf_seek(buf, 0, sizeof(*eigrp_hdr))) == NULL)
                fatalx("send_packet: buf_seek failed");
	if (nbr) {
		eigrp_hdr->ack_num = htonl(nbr->recv_seq);
		rtp_ack_stop_timer(nbr);
	}
	if (flags) {
		eigrp_hdr->flags = ntohl(eigrp_hdr->flags) | flags;
		eigrp_hdr->flags = htonl(eigrp_hdr->flags);
	}
	eigrp_hdr->chksum = 0;
	eigrp_hdr->chksum = in_cksum(buf->buf, ibuf_size(buf));

	/* log packet being sent */
	if (eigrp_hdr->opcode != EIGRP_OPC_HELLO) {
		char	buffer[64];

		if (nbr)
			snprintf(buffer, sizeof(buffer), "nbr %s",
			    log_addr(eigrp->af, &nbr->addr));
		else
			snprintf(buffer, sizeof(buffer), "(multicast)");

		log_debug("%s: type %s iface %s %s AS %u seq %u ack %u",
		    __func__, opcode_name(eigrp_hdr->opcode), iface->name,
		    buffer, ntohs(eigrp_hdr->as), ntohl(eigrp_hdr->seq_num),
		    ntohl(eigrp_hdr->ack_num));
	}

	switch (eigrp->af) {
	case AF_INET:
		if (send_packet_v4(iface, nbr, buf) < 0)
			return (-1);
		break;
	case AF_INET6:
		if (send_packet_v6(iface, nbr, buf) < 0)
			return (-1);
		break;
	default:
		fatalx("send_packet: unknown af");
	}

	switch (eigrp_hdr->opcode) {
	case EIGRP_OPC_HELLO:
		if (ntohl(eigrp_hdr->ack_num) == 0)
			ei->eigrp->stats.hellos_sent++;
		else
			ei->eigrp->stats.acks_sent++;
		break;
	case EIGRP_OPC_UPDATE:
		ei->eigrp->stats.updates_sent++;
		break;
	case EIGRP_OPC_QUERY:
		ei->eigrp->stats.queries_sent++;
		break;
	case EIGRP_OPC_REPLY:
		ei->eigrp->stats.replies_sent++;
		break;
	case EIGRP_OPC_SIAQUERY:
		ei->eigrp->stats.squeries_sent++;
		break;
	case EIGRP_OPC_SIAREPLY:
		ei->eigrp->stats.sreplies_sent++;
		break;
	default:
		break;
	}

	return (0);
}

static int
recv_packet_nbr(struct nbr *nbr, struct eigrp_hdr *eigrp_hdr,
    struct seq_addr_head *seq_addr_list, struct tlv_mcast_seq *tm)
{
	uint32_t		 seq, ack;
	struct seq_addr_entry	*sa;

	seq = ntohl(eigrp_hdr->seq_num);
	ack = ntohl(eigrp_hdr->ack_num);

	/*
	 * draft-savage-eigrp-04 - Section 5.3.1:
	 * "In addition to the HELLO packet, if any packet is received within
	 * the hold time period, then the Hold Time period will be reset."
	 */
	nbr_start_timeout(nbr);

	/* handle the sequence tlv */
	if (eigrp_hdr->opcode == EIGRP_OPC_HELLO &&
	    !TAILQ_EMPTY(seq_addr_list)) {
		nbr->flags |= F_EIGRP_NBR_CR_MODE;

		TAILQ_FOREACH(sa, seq_addr_list, entry) {
			switch (sa->af) {
			case AF_INET:
				if (sa->addr.v4.s_addr ==
				    if_primary_addr(nbr->ei->iface)) {
					nbr->flags &= ~F_EIGRP_NBR_CR_MODE;
					break;
				}
				break;
			case AF_INET6:
				if (IN6_ARE_ADDR_EQUAL(&sa->addr.v6,
				    &nbr->ei->iface->linklocal)) {
					nbr->flags &= ~F_EIGRP_NBR_CR_MODE;
					break;
				}
				break;
			default:
				break;
			}
		}
		if (tm)
			nbr->next_mcast_seq = ntohl(tm->seq);
	}

	if ((ntohl(eigrp_hdr->flags) & EIGRP_HDR_FLAG_CR)) {
		if (!(nbr->flags & F_EIGRP_NBR_CR_MODE))
			return (-1);
		nbr->flags &= ~F_EIGRP_NBR_CR_MODE;
		if (ntohl(eigrp_hdr->seq_num) != nbr->next_mcast_seq)
			return (-1);
	}

	/* ack processing */
	if (ack != 0)
		rtp_process_ack(nbr, ack);
	if (seq != 0) {
		/* check for sequence wraparound */
		if (nbr->recv_seq >= seq &&
		   !(nbr->recv_seq == UINT32_MAX && seq == 1)) {
			log_debug("%s: duplicate packet", __func__);
			rtp_send_ack(nbr);
			return (-1);
		}
		nbr->recv_seq = seq;
	}

	return (0);
}

static void
recv_packet(int af, union eigrpd_addr *src, union eigrpd_addr *dest,
    struct iface *iface, struct eigrp_hdr *eigrp_hdr, char *buf, uint16_t len)
{
	struct eigrp_iface	*ei;
	struct nbr		*nbr;
	struct tlv_parameter	*tp = NULL;
	struct tlv_sw_version	*tv = NULL;
	struct tlv_mcast_seq	*tm = NULL;
	struct rinfo		 ri;
	struct rinfo_entry	*re;
	struct seq_addr_head	 seq_addr_list;
	struct rinfo_head	 rinfo_list;

	/* EIGRP header sanity checks */
	if (eigrp_hdr_sanity_check(af, dest, eigrp_hdr, len, iface) == -1)
		return;

	buf += sizeof(*eigrp_hdr);
	len -= sizeof(*eigrp_hdr);

	TAILQ_INIT(&seq_addr_list);
	TAILQ_INIT(&rinfo_list);
	while (len > 0) {
		struct tlv 	tlv;
		uint16_t	tlv_type;

		if (len < sizeof(tlv)) {
			log_debug("%s: malformed packet (bad length)",
			    __func__);
			goto error;
		}

		memcpy(&tlv, buf, sizeof(tlv));
		if (ntohs(tlv.length) > len) {
			log_debug("%s: malformed packet (bad length)",
			    __func__);
			goto error;
		}

		tlv_type = ntohs(tlv.type);
		switch (tlv_type) {
		case TLV_TYPE_PARAMETER:
			if ((tp = tlv_decode_parameter(&tlv, buf)) == NULL)
				goto error;
			break;
		case TLV_TYPE_SEQ:
			if (tlv_decode_seq(af, &tlv, buf, &seq_addr_list) < 0)
				goto error;
			break;
		case TLV_TYPE_SW_VERSION:
			if ((tv = tlv_decode_sw_version(&tlv, buf)) == NULL)
				goto error;
			break;
		case TLV_TYPE_MCAST_SEQ:
			if ((tm = tlv_decode_mcast_seq(&tlv, buf)) == NULL)
				goto error;
			break;
		case TLV_TYPE_IPV4_INTERNAL:
		case TLV_TYPE_IPV4_EXTERNAL:
		case TLV_TYPE_IPV6_INTERNAL:
		case TLV_TYPE_IPV6_EXTERNAL:
			/* silently ignore TLV from different address-family */
			if ((tlv_type & TLV_PROTO_MASK) == TLV_PROTO_IPV4 &&
			    af != AF_INET)
				break;
			if ((tlv_type & TLV_PROTO_MASK) == TLV_PROTO_IPV6 &&
			    af != AF_INET6)
				break;

			if (tlv_decode_route(af, &tlv, buf, &ri) < 0)
				goto error;
			if ((re = calloc(1, sizeof(*re))) == NULL)
				fatal("recv_packet");
			re->rinfo = ri;
			TAILQ_INSERT_TAIL(&rinfo_list, re, entry);
			break;
		case TLV_TYPE_AUTH:
		case TLV_TYPE_PEER_TERM:
			/*
			 * XXX There is no enough information in the draft
			 * to implement these TLVs properly.
			 */
		case TLV_TYPE_IPV4_COMMUNITY:
		case TLV_TYPE_IPV6_COMMUNITY:
			/* TODO */
		default:
			/* ignore unknown tlv */
			break;
		}
		buf += ntohs(tlv.length);
		len -= ntohs(tlv.length);
	}

	ei = eigrp_if_lookup(iface, af, ntohs(eigrp_hdr->as));
	if (ei == NULL || ei->passive)
		goto error;

	nbr = nbr_find(ei, src);
	if (nbr == NULL && (eigrp_hdr->opcode != EIGRP_OPC_HELLO ||
	    ntohl(eigrp_hdr->ack_num) != 0)) {
		log_debug("%s: unknown neighbor", __func__);
		goto error;
	} else if (nbr && recv_packet_nbr(nbr, eigrp_hdr, &seq_addr_list,
	    tm) < 0)
		goto error;

	/* log packet being received */
	if (eigrp_hdr->opcode != EIGRP_OPC_HELLO)
		log_debug("%s: type %s nbr %s AS %u seq %u ack %u", __func__,
		    opcode_name(eigrp_hdr->opcode), log_addr(af, &nbr->addr),
		    ei->eigrp->as, ntohl(eigrp_hdr->seq_num),
		    ntohl(eigrp_hdr->ack_num));

	/* switch EIGRP packet type */
	switch (eigrp_hdr->opcode) {
	case EIGRP_OPC_HELLO:
		if (ntohl(eigrp_hdr->ack_num) == 0) {
			recv_hello(ei, src, nbr, tp);
			ei->eigrp->stats.hellos_recv++;
		} else
			ei->eigrp->stats.acks_recv++;
		break;
	case EIGRP_OPC_UPDATE:
		recv_update(nbr, &rinfo_list, ntohl(eigrp_hdr->flags));
		ei->eigrp->stats.updates_recv++;
		break;
	case EIGRP_OPC_QUERY:
		recv_query(nbr, &rinfo_list, 0);
		ei->eigrp->stats.queries_recv++;
		break;
	case EIGRP_OPC_REPLY:
		recv_reply(nbr, &rinfo_list, 0);
		ei->eigrp->stats.replies_recv++;
		break;
	case EIGRP_OPC_SIAQUERY:
		recv_query(nbr, &rinfo_list, 1);
		ei->eigrp->stats.squeries_recv++;
		break;
	case EIGRP_OPC_SIAREPLY:
		recv_reply(nbr, &rinfo_list, 1);
		ei->eigrp->stats.sreplies_recv++;
		break;
	default:
		log_debug("%s: unknown EIGRP packet type, interface %s",
		    __func__, iface->name);
	}

error:
	/* free rinfo tlvs */
	message_list_clr(&rinfo_list);
	/* free seq addresses tlvs */
	seq_addr_list_clr(&seq_addr_list);
}

void
recv_packet_v4(int fd, short event, void *bula)
{
	union {
		struct	cmsghdr hdr;
		char	buf[CMSG_SPACE(sizeof(struct sockaddr_dl))];
	} cmsgbuf;
	struct msghdr		 msg;
	struct iovec		 iov;
	struct ip		 ip_hdr;
	struct eigrp_hdr	*eigrp_hdr;
	struct iface		*iface;
	char			*buf;
	struct cmsghdr		*cmsg;
	ssize_t			 r;
	uint16_t		 len;
	int			 l;
	unsigned int		 ifindex = 0;
	union eigrpd_addr	 src, dest;

	if (event != EV_READ)
		return;

	/* setup buffer */
	memset(&msg, 0, sizeof(msg));
	iov.iov_base = buf = pkt_ptr;
	iov.iov_len = READ_BUF_SIZE;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = &cmsgbuf.buf;
	msg.msg_controllen = sizeof(cmsgbuf.buf);

	if ((r = recvmsg(fd, &msg, 0)) == -1) {
		if (errno != EAGAIN && errno != EINTR)
			log_debug("%s: read error: %s", __func__,
			    strerror(errno));
		return;
	}
	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
		if (cmsg->cmsg_level == IPPROTO_IP &&
		    cmsg->cmsg_type == IP_RECVIF) {
			ifindex = ((struct sockaddr_dl *)
			    CMSG_DATA(cmsg))->sdl_index;
			break;
		}
	}

	len = (uint16_t)r;

	/* IP header sanity checks */
	if (len < sizeof(ip_hdr)) {
		log_debug("%s: bad packet size", __func__);
		return;
	}
	memcpy(&ip_hdr, buf, sizeof(ip_hdr));
	if ((l = ip_hdr_sanity_check(&ip_hdr, len)) == -1)
		return;
	buf += l;
	len -= l;

	src.v4 = ip_hdr.ip_src;
	dest.v4 = ip_hdr.ip_dst;

	/* find a matching interface */
	if ((iface = find_iface(ifindex, AF_INET, &src)) == NULL)
		return;

	/*
	 * Packet needs to be sent to AllEIGRPRouters_v4 or to one
	 * of the interface addresses.
	 */
	if (ip_hdr.ip_dst.s_addr != AllEIGRPRouters_v4) {
		struct if_addr	*if_addr;
		int		 found = 0;

		TAILQ_FOREACH(if_addr, &iface->addr_list, entry)
			if (if_addr->af == AF_INET &&
			    ip_hdr.ip_dst.s_addr == if_addr->addr.v4.s_addr) {
				found = 1;
				break;
			}
		if (found == 0) {
			log_debug("%s: packet sent to wrong address %s, "
			    "interface %s", __func__, inet_ntoa(ip_hdr.ip_dst),
			    iface->name);
			return;
		}
	}

	if (len < sizeof(*eigrp_hdr)) {
		log_debug("%s: bad packet size", __func__);
		return;
	}
	eigrp_hdr = (struct eigrp_hdr *)buf;

	recv_packet(AF_INET, &src, &dest, iface, eigrp_hdr, buf, len);
}

void
recv_packet_v6(int fd, short event, void *bula)
{
	union {
		struct	cmsghdr hdr;
		char	buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
	} cmsgbuf;
	struct msghdr		 msg;
	struct iovec		 iov;
	struct sockaddr_in6	 sin6;
	struct eigrp_hdr	*eigrp_hdr;
	struct iface		*iface;
	char			*buf;
	struct cmsghdr		*cmsg;
	ssize_t			 r;
	uint16_t		 len;
	unsigned int		 ifindex = 0;
	union eigrpd_addr	 src, dest;
	struct in6_addr		 maddr = AllEIGRPRouters_v6;

	if (event != EV_READ)
		return;

	/* setup buffer */
	memset(&msg, 0, sizeof(msg));
	iov.iov_base = buf = pkt_ptr;
	iov.iov_len = READ_BUF_SIZE;
	msg.msg_name = &sin6;
	msg.msg_namelen = sizeof(sin6);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = &cmsgbuf.buf;
	msg.msg_controllen = sizeof(cmsgbuf.buf);

	if ((r = recvmsg(fd, &msg, 0)) == -1) {
		if (errno != EAGAIN && errno != EINTR)
			log_debug("%s: read error: %s", __func__,
			    strerror(errno));
		return;
	}
	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
		    cmsg->cmsg_type == IPV6_PKTINFO) {
			ifindex = ((struct in6_pktinfo *)
			    CMSG_DATA(cmsg))->ipi6_ifindex;
			dest.v6 = ((struct in6_pktinfo *)
			    CMSG_DATA(cmsg))->ipi6_addr;
			break;
		}
	}
	src.v6 = sin6.sin6_addr;

	/* validate source address */
	if (bad_addr_v6(&src.v6)) {
		log_debug("%s: invalid source address: %s", __func__,
		    log_addr(AF_INET, &src));
		return;
	}

	/* find a matching interface */
	if ((iface = find_iface(ifindex, AF_INET6, &src)) == NULL)
		return;

	/*
	 * Packet needs to be sent to AllEIGRPRouters_v6 or to the
	 * link local address of the interface.
	 */
	if (!IN6_ARE_ADDR_EQUAL(&dest.v6, &maddr) &&
	    !IN6_ARE_ADDR_EQUAL(&dest.v6, &iface->linklocal)) {
		log_debug("%s: packet sent to wrong address %s, interface %s",
		    __func__, log_in6addr(&dest.v6), iface->name);
		return;
	}

	len = (uint16_t)r;
	if (len < sizeof(*eigrp_hdr)) {
		log_debug("%s: bad packet size", __func__);
		return;
	}
	eigrp_hdr = (struct eigrp_hdr *)buf;

	recv_packet(AF_INET6, &src, &dest, iface, eigrp_hdr, buf, len);
}

int
ip_hdr_sanity_check(const struct ip *ip_hdr, uint16_t len)
{
	if (ntohs(ip_hdr->ip_len) != len) {
		log_debug("%s: invalid IP packet length %u", __func__,
		    ntohs(ip_hdr->ip_len));
		return (-1);
	}

	/* validate source address */
	if (bad_addr_v4(ip_hdr->ip_src)) {
		log_debug("%s: invalid source address: %s", __func__,
		    inet_ntoa(ip_hdr->ip_src));
		return (-1);
	}

	if (ip_hdr->ip_p != IPPROTO_EIGRP)
		/* this is enforced by the socket itself */
		fatalx("ip_hdr_sanity_check: invalid IP proto");

	return (ip_hdr->ip_hl << 2);
}

int
eigrp_hdr_sanity_check(int af, union eigrpd_addr *addr,
    struct eigrp_hdr *eigrp_hdr, uint16_t len, const struct iface *iface)
{
	if (in_cksum(eigrp_hdr, len)) {
		log_debug("%s: invalid checksum, interface %s", __func__,
		    iface->name);
		return (-1);
	}

	if (eigrp_hdr->version != EIGRP_HEADER_VERSION) {
		log_debug("%s: invalid EIGRP version %d, interface %s",
		    __func__, eigrp_hdr->version, iface->name);
		return (-1);
	}

	if (ntohs(eigrp_hdr->vrid) != EIGRP_VRID_UNICAST_AF) {
		log_debug("%s: unknown or unsupported vrid %u, interface %s",
		    __func__, ntohs(eigrp_hdr->vrid), iface->name);
		return (-1);
	}

	if (eigrp_hdr->opcode == EIGRP_OPC_HELLO &&
	    eigrp_hdr->ack_num != 0) {
		switch (af) {
		case AF_INET:
			if (IN_MULTICAST(addr->v4.s_addr)) {
				log_debug("%s: multicast ack (ipv4), "
				    "interface %s", __func__, iface->name);
				return (-1);
			}
			break;
		case AF_INET6:
			if (IN6_IS_ADDR_MULTICAST(&addr->v6)) {
				log_debug("%s: multicast ack (ipv6), "
				    "interface %s", __func__, iface->name);
				return (-1);
			}
			break;
		default:
			fatalx("eigrp_hdr_sanity_check: unknown af");
		}
	}

	return (0);
}

struct iface *
find_iface(unsigned int ifindex, int af, union eigrpd_addr *src)
{
	struct iface	*iface;
	struct if_addr	*if_addr;
	in_addr_t	 mask;

	iface = if_lookup(econf, ifindex);
	if (iface == NULL)
		return (NULL);

	switch (af) {
	case AF_INET:
		/*
		 * From CCNP ROUTE 642-902 OCG:
		 * "EIGRP's rules about neighbor IP addresses being in the same
		 * subnet are less exact than OSPF. OSPF requires matching
		 * subnet numbers and masks. EIGRP just asks the question of
		 * whether the neighbor's IP address is in the range of
		 * addresses for the subnet as known to the local router."
		 */
		TAILQ_FOREACH(if_addr, &iface->addr_list, entry) {
			if (if_addr->af == AF_INET) {
				mask = prefixlen2mask(if_addr->prefixlen);

				if ((if_addr->addr.v4.s_addr & mask) ==
				    (src->v4.s_addr & mask))
					return (iface);
			}
		}
		break;
	case AF_INET6:
		/*
		 * draft-savage-eigrp-04 - Section 10.1:
		 * "EIGRP IPv6 will check that a received HELLO contains a valid
		 * IPv6 link-local source address."
		 */
		if (IN6_IS_ADDR_LINKLOCAL(&src->v6))
			return (iface);
		break;
	default:
		fatalx("find_iface: unknown af");
	}

	return (NULL);
}