2 * EIGRP General Sending and Receiving of EIGRP Packets.
3 * Copyright (C) 2013-2014
11 * This file is part of GNU Zebra.
13 * GNU Zebra is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
18 * GNU Zebra is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "sockunion.h"
45 #include "lib_errors.h"
47 #include "eigrpd/eigrp_structs.h"
48 #include "eigrpd/eigrpd.h"
49 #include "eigrpd/eigrp_interface.h"
50 #include "eigrpd/eigrp_neighbor.h"
51 #include "eigrpd/eigrp_packet.h"
52 #include "eigrpd/eigrp_zebra.h"
53 #include "eigrpd/eigrp_vty.h"
54 #include "eigrpd/eigrp_dump.h"
55 #include "eigrpd/eigrp_macros.h"
56 #include "eigrpd/eigrp_network.h"
57 #include "eigrpd/eigrp_topology.h"
58 #include "eigrpd/eigrp_fsm.h"
59 #include "eigrpd/eigrp_errors.h"
61 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_FIFO
, "EIGRP FIFO");
62 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_PACKET
, "EIGRP Packet");
63 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_IPV4_INT_TLV
, "EIGRP IPv4 TLV");
64 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_SEQ_TLV
, "EIGRP SEQ TLV");
65 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_AUTH_TLV
, "EIGRP AUTH TLV");
66 DEFINE_MTYPE_STATIC(EIGRPD
, EIGRP_AUTH_SHA256_TLV
, "EIGRP SHA TLV");
68 /* Packet Type String. */
69 const struct message eigrp_packet_type_str
[] = {
70 {EIGRP_OPC_UPDATE
, "Update"},
71 {EIGRP_OPC_REQUEST
, "Request"},
72 {EIGRP_OPC_QUERY
, "Query"},
73 {EIGRP_OPC_REPLY
, "Reply"},
74 {EIGRP_OPC_HELLO
, "Hello"},
75 {EIGRP_OPC_IPXSAP
, "IPX-SAP"},
76 {EIGRP_OPC_PROBE
, "Probe"},
77 {EIGRP_OPC_ACK
, "Ack"},
78 {EIGRP_OPC_SIAQUERY
, "SIAQuery"},
79 {EIGRP_OPC_SIAREPLY
, "SIAReply"},
83 static unsigned char zeropad
[16] = {0};
85 /* Forward function reference*/
86 static struct stream
*eigrp_recv_packet(struct eigrp
*eigrp
, int fd
,
87 struct interface
**ifp
,
89 static int eigrp_verify_header(struct stream
*s
, struct eigrp_interface
*ei
,
90 struct ip
*addr
, struct eigrp_header
*header
);
91 static int eigrp_check_network_mask(struct eigrp_interface
*ei
,
94 static int eigrp_retrans_count_exceeded(struct eigrp_packet
*ep
,
95 struct eigrp_neighbor
*nbr
)
100 int eigrp_make_md5_digest(struct eigrp_interface
*ei
, struct stream
*s
,
103 struct key
*key
= NULL
;
104 struct keychain
*keychain
;
106 unsigned char digest
[EIGRP_AUTH_TYPE_MD5_LEN
];
109 size_t backup_get
, backup_end
;
110 struct TLV_MD5_Authentication_Type
*auth_TLV
;
113 backup_end
= s
->endp
;
114 backup_get
= s
->getp
;
116 auth_TLV
= eigrp_authTLV_MD5_new();
118 stream_set_getp(s
, EIGRP_HEADER_LEN
);
119 stream_get(auth_TLV
, s
, EIGRP_AUTH_MD5_TLV_SIZE
);
120 stream_set_getp(s
, backup_get
);
122 keychain
= keychain_lookup(ei
->params
.auth_keychain
);
124 key
= key_lookup_for_send(keychain
);
126 eigrp_authTLV_MD5_free(auth_TLV
);
127 return EIGRP_AUTH_TYPE_NONE
;
130 memset(&ctx
, 0, sizeof(ctx
));
133 /* Generate a digest. Each situation needs different handling */
134 if (flags
& EIGRP_AUTH_BASIC_HELLO_FLAG
) {
135 MD5Update(&ctx
, ibuf
, EIGRP_MD5_BASIC_COMPUTE
);
136 MD5Update(&ctx
, key
->string
, strlen(key
->string
));
137 if (strlen(key
->string
) < 16)
138 MD5Update(&ctx
, zeropad
, 16 - strlen(key
->string
));
139 } else if (flags
& EIGRP_AUTH_UPDATE_INIT_FLAG
) {
140 MD5Update(&ctx
, ibuf
, EIGRP_MD5_UPDATE_INIT_COMPUTE
);
141 } else if (flags
& EIGRP_AUTH_UPDATE_FLAG
) {
142 MD5Update(&ctx
, ibuf
, EIGRP_MD5_BASIC_COMPUTE
);
143 MD5Update(&ctx
, key
->string
, strlen(key
->string
));
144 if (strlen(key
->string
) < 16)
145 MD5Update(&ctx
, zeropad
, 16 - strlen(key
->string
));
146 if (backup_end
> (EIGRP_HEADER_LEN
+ EIGRP_AUTH_MD5_TLV_SIZE
)) {
148 ibuf
+ (EIGRP_HEADER_LEN
149 + EIGRP_AUTH_MD5_TLV_SIZE
),
152 + EIGRP_AUTH_MD5_TLV_SIZE
));
156 MD5Final(digest
, &ctx
);
158 /* Append md5 digest to the end of the stream. */
159 memcpy(auth_TLV
->digest
, digest
, EIGRP_AUTH_TYPE_MD5_LEN
);
161 stream_set_endp(s
, EIGRP_HEADER_LEN
);
162 stream_put(s
, auth_TLV
, EIGRP_AUTH_MD5_TLV_SIZE
);
163 stream_set_endp(s
, backup_end
);
165 eigrp_authTLV_MD5_free(auth_TLV
);
166 return EIGRP_AUTH_TYPE_MD5_LEN
;
169 int eigrp_check_md5_digest(struct stream
*s
,
170 struct TLV_MD5_Authentication_Type
*authTLV
,
171 struct eigrp_neighbor
*nbr
, uint8_t flags
)
174 unsigned char digest
[EIGRP_AUTH_TYPE_MD5_LEN
];
175 unsigned char orig
[EIGRP_AUTH_TYPE_MD5_LEN
];
176 struct key
*key
= NULL
;
177 struct keychain
*keychain
;
180 struct TLV_MD5_Authentication_Type
*auth_TLV
;
181 struct eigrp_header
*eigrph
;
183 if (ntohl(nbr
->crypt_seqnum
) > ntohl(authTLV
->key_sequence
)) {
185 "interface %s: eigrp_check_md5 bad sequence %d (expect %d)",
186 IF_NAME(nbr
->ei
), ntohl(authTLV
->key_sequence
),
187 ntohl(nbr
->crypt_seqnum
));
191 eigrph
= (struct eigrp_header
*)s
->data
;
192 eigrph
->checksum
= 0;
194 auth_TLV
= (struct TLV_MD5_Authentication_Type
*)(s
->data
196 memcpy(orig
, auth_TLV
->digest
, EIGRP_AUTH_TYPE_MD5_LEN
);
197 memset(digest
, 0, EIGRP_AUTH_TYPE_MD5_LEN
);
198 memset(auth_TLV
->digest
, 0, EIGRP_AUTH_TYPE_MD5_LEN
);
201 backup_end
= s
->endp
;
203 keychain
= keychain_lookup(nbr
->ei
->params
.auth_keychain
);
205 key
= key_lookup_for_send(keychain
);
209 "Interface %s: Expected key value not found in config",
214 memset(&ctx
, 0, sizeof(ctx
));
217 /* Generate a digest. Each situation needs different handling */
218 if (flags
& EIGRP_AUTH_BASIC_HELLO_FLAG
) {
219 MD5Update(&ctx
, ibuf
, EIGRP_MD5_BASIC_COMPUTE
);
220 MD5Update(&ctx
, key
->string
, strlen(key
->string
));
221 if (strlen(key
->string
) < 16)
222 MD5Update(&ctx
, zeropad
, 16 - strlen(key
->string
));
223 } else if (flags
& EIGRP_AUTH_UPDATE_INIT_FLAG
) {
224 MD5Update(&ctx
, ibuf
, EIGRP_MD5_UPDATE_INIT_COMPUTE
);
225 } else if (flags
& EIGRP_AUTH_UPDATE_FLAG
) {
226 MD5Update(&ctx
, ibuf
, EIGRP_MD5_BASIC_COMPUTE
);
227 MD5Update(&ctx
, key
->string
, strlen(key
->string
));
228 if (strlen(key
->string
) < 16)
229 MD5Update(&ctx
, zeropad
, 16 - strlen(key
->string
));
230 if (backup_end
> (EIGRP_HEADER_LEN
+ EIGRP_AUTH_MD5_TLV_SIZE
)) {
232 ibuf
+ (EIGRP_HEADER_LEN
233 + EIGRP_AUTH_MD5_TLV_SIZE
),
236 + EIGRP_AUTH_MD5_TLV_SIZE
));
240 MD5Final(digest
, &ctx
);
242 /* compare the two */
243 if (memcmp(orig
, digest
, EIGRP_AUTH_TYPE_MD5_LEN
) != 0) {
244 zlog_warn("interface %s: eigrp_check_md5 checksum mismatch",
249 /* save neighbor's crypt_seqnum */
250 nbr
->crypt_seqnum
= authTLV
->key_sequence
;
255 int eigrp_make_sha256_digest(struct eigrp_interface
*ei
, struct stream
*s
,
258 struct key
*key
= NULL
;
259 struct keychain
*keychain
;
260 char source_ip
[PREFIX_STRLEN
];
262 unsigned char digest
[EIGRP_AUTH_TYPE_SHA256_LEN
];
263 unsigned char buffer
[1 + PLAINTEXT_LENGTH
+ 45 + 1] = {0};
267 size_t backup_get
, backup_end
;
268 struct TLV_SHA256_Authentication_Type
*auth_TLV
;
271 backup_end
= s
->endp
;
272 backup_get
= s
->getp
;
274 auth_TLV
= eigrp_authTLV_SHA256_new();
276 stream_set_getp(s
, EIGRP_HEADER_LEN
);
277 stream_get(auth_TLV
, s
, EIGRP_AUTH_SHA256_TLV_SIZE
);
278 stream_set_getp(s
, backup_get
);
280 keychain
= keychain_lookup(ei
->params
.auth_keychain
);
282 key
= key_lookup_for_send(keychain
);
286 "Interface %s: Expected key value not found in config",
288 eigrp_authTLV_SHA256_free(auth_TLV
);
292 inet_ntop(AF_INET
, &ei
->address
.u
.prefix4
, source_ip
, PREFIX_STRLEN
);
294 memset(&ctx
, 0, sizeof(ctx
));
296 memcpy(buffer
+ 1, key
, strlen(key
->string
));
297 memcpy(buffer
+ 1 + strlen(key
->string
), source_ip
, strlen(source_ip
));
298 HMAC__SHA256_Init(&ctx
, buffer
,
299 1 + strlen(key
->string
) + strlen(source_ip
));
300 HMAC__SHA256_Update(&ctx
, ibuf
, strlen(ibuf
));
301 HMAC__SHA256_Final(digest
, &ctx
);
304 /* Put hmac-sha256 digest to it's place */
305 memcpy(auth_TLV
->digest
, digest
, EIGRP_AUTH_TYPE_SHA256_LEN
);
307 stream_set_endp(s
, EIGRP_HEADER_LEN
);
308 stream_put(s
, auth_TLV
, EIGRP_AUTH_SHA256_TLV_SIZE
);
309 stream_set_endp(s
, backup_end
);
311 eigrp_authTLV_SHA256_free(auth_TLV
);
313 return EIGRP_AUTH_TYPE_SHA256_LEN
;
316 int eigrp_check_sha256_digest(struct stream
*s
,
317 struct TLV_SHA256_Authentication_Type
*authTLV
,
318 struct eigrp_neighbor
*nbr
, uint8_t flags
)
323 int eigrp_write(struct thread
*thread
)
325 struct eigrp
*eigrp
= THREAD_ARG(thread
);
326 struct eigrp_header
*eigrph
;
327 struct eigrp_interface
*ei
;
328 struct eigrp_packet
*ep
;
329 struct sockaddr_in sa_dst
;
337 struct listnode
*node
;
338 #ifdef WANT_EIGRP_WRITE_FRAGMENT
339 static uint16_t ipid
= 0;
340 #endif /* WANT_EIGRP_WRITE_FRAGMENT */
341 #define EIGRP_WRITE_IPHL_SHIFT 2
343 node
= listhead(eigrp
->oi_write_q
);
345 ei
= listgetdata(node
);
348 #ifdef WANT_EIGRP_WRITE_FRAGMENT
349 /* seed ipid static with low order bits of time */
351 ipid
= (time(NULL
) & 0xffff);
352 #endif /* WANT_EIGRP_WRITE_FRAGMENT */
354 /* Get one packet from queue. */
355 ep
= eigrp_fifo_next(ei
->obuf
);
357 flog_err(EC_LIB_DEVELOPMENT
,
358 "%s: Interface %s no packet on queue?", __func__
,
362 if (ep
->length
< EIGRP_HEADER_LEN
) {
363 flog_err(EC_EIGRP_PACKET
, "%s: Packet just has a header?",
365 eigrp_header_dump((struct eigrp_header
*)ep
->s
->data
);
366 eigrp_packet_delete(ei
);
370 if (ep
->dst
.s_addr
== htonl(EIGRP_MULTICAST_ADDRESS
))
371 eigrp_if_ipmulticast(eigrp
, &ei
->address
, ei
->ifp
->ifindex
);
373 memset(&iph
, 0, sizeof(struct ip
));
374 memset(&sa_dst
, 0, sizeof(sa_dst
));
377 * We build and schedule packets to go out
378 * in the future. In the mean time we may
379 * process some update packets from the
380 * neighbor, thus making it necessary
381 * to update the ack we are using for
382 * this outgoing packet.
384 eigrph
= (struct eigrp_header
*)STREAM_DATA(ep
->s
);
385 seqno
= ntohl(eigrph
->sequence
);
386 ack
= ntohl(eigrph
->ack
);
387 if (ep
->nbr
&& (ack
!= ep
->nbr
->recv_sequence_number
)) {
388 eigrph
->ack
= htonl(ep
->nbr
->recv_sequence_number
);
389 ack
= ep
->nbr
->recv_sequence_number
;
390 eigrph
->checksum
= 0;
391 eigrp_packet_checksum(ei
, ep
->s
, ep
->length
);
394 sa_dst
.sin_family
= AF_INET
;
395 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
396 sa_dst
.sin_len
= sizeof(sa_dst
);
397 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
398 sa_dst
.sin_addr
= ep
->dst
;
399 sa_dst
.sin_port
= htons(0);
401 /* Set DONTROUTE flag if dst is unicast. */
402 if (!IN_MULTICAST(htonl(ep
->dst
.s_addr
)))
403 flags
= MSG_DONTROUTE
;
405 iph
.ip_hl
= sizeof(struct ip
) >> EIGRP_WRITE_IPHL_SHIFT
;
406 /* it'd be very strange for header to not be 4byte-word aligned but.. */
407 if (sizeof(struct ip
)
408 > (unsigned int)(iph
.ip_hl
<< EIGRP_WRITE_IPHL_SHIFT
))
409 iph
.ip_hl
++; /* we presume sizeof struct ip cant overflow
412 iph
.ip_v
= IPVERSION
;
413 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
414 iph
.ip_len
= (iph
.ip_hl
<< EIGRP_WRITE_IPHL_SHIFT
) + ep
->length
;
416 #if defined(__DragonFly__)
418 * DragonFly's raw socket expects ip_len/ip_off in network byte order.
420 iph
.ip_len
= htons(iph
.ip_len
);
424 iph
.ip_ttl
= EIGRP_IP_TTL
;
425 iph
.ip_p
= IPPROTO_EIGRPIGP
;
427 iph
.ip_src
.s_addr
= ei
->address
.u
.prefix4
.s_addr
;
428 iph
.ip_dst
.s_addr
= ep
->dst
.s_addr
;
430 memset(&msg
, 0, sizeof(msg
));
431 msg
.msg_name
= (caddr_t
)&sa_dst
;
432 msg
.msg_namelen
= sizeof(sa_dst
);
436 iov
[0].iov_base
= (char *)&iph
;
437 iov
[0].iov_len
= iph
.ip_hl
<< EIGRP_WRITE_IPHL_SHIFT
;
438 iov
[1].iov_base
= stream_pnt(ep
->s
);
439 iov
[1].iov_len
= ep
->length
;
441 /* send final fragment (could be first) */
442 sockopt_iphdrincl_swab_htosys(&iph
);
443 ret
= sendmsg(eigrp
->fd
, &msg
, flags
);
444 sockopt_iphdrincl_swab_systoh(&iph
);
446 if (IS_DEBUG_EIGRP_TRANSMIT(0, SEND
)) {
447 eigrph
= (struct eigrp_header
*)STREAM_DATA(ep
->s
);
449 "Sending [%s][%d/%d] to [%pI4] via [%s] ret [%d].",
450 lookup_msg(eigrp_packet_type_str
, eigrph
->opcode
, NULL
),
451 seqno
, ack
, &ep
->dst
, IF_NAME(ei
), ret
);
456 "*** sendmsg in eigrp_write failed to %pI4, id %d, off %d, len %d, interface %s, mtu %u: %s",
457 &iph
.ip_dst
, iph
.ip_id
, iph
.ip_off
, iph
.ip_len
,
458 ei
->ifp
->name
, ei
->ifp
->mtu
, safe_strerror(errno
));
460 /* Now delete packet from queue. */
461 eigrp_packet_delete(ei
);
464 if (eigrp_fifo_next(ei
->obuf
) == NULL
) {
466 list_delete_node(eigrp
->oi_write_q
, node
);
469 /* If packets still remain in queue, call write thread. */
470 if (!list_isempty(eigrp
->oi_write_q
)) {
471 thread_add_write(master
, eigrp_write
, eigrp
, eigrp
->fd
,
478 /* Starting point of packet process function. */
479 int eigrp_read(struct thread
*thread
)
484 struct eigrp_interface
*ei
;
486 struct eigrp_header
*eigrph
;
487 struct interface
*ifp
;
488 struct eigrp_neighbor
*nbr
;
489 struct in_addr srcaddr
;
493 /* first of all get interface pointer. */
494 eigrp
= THREAD_ARG(thread
);
496 /* prepare for next packet. */
497 thread_add_read(master
, eigrp_read
, eigrp
, eigrp
->fd
, &eigrp
->t_read
);
499 stream_reset(eigrp
->ibuf
);
500 if (!(ibuf
= eigrp_recv_packet(eigrp
, eigrp
->fd
, &ifp
, eigrp
->ibuf
))) {
501 /* This raw packet is known to be at least as big as its IP
506 /* Note that there should not be alignment problems with this assignment
507 because this is at the beginning of the stream data buffer. */
508 iph
= (struct ip
*)STREAM_DATA(ibuf
);
510 // Substract IPv4 header size from EIGRP Packet itself
512 length
= (iph
->ip_len
) - 20U;
514 srcaddr
= iph
->ip_src
;
516 /* IP Header dump. */
517 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
)
518 && IS_DEBUG_EIGRP_TRANSMIT(0, PACKET_DETAIL
))
519 eigrp_ip_header_dump(iph
);
521 /* Note that sockopt_iphdrincl_swab_systoh was called in
522 * eigrp_recv_packet. */
525 /* Handle cases where the platform does not support retrieving
527 and also platforms (such as Solaris 8) that claim to support
529 retrieval but do not. */
530 c
= if_lookup_address((void *)&srcaddr
, AF_INET
,
539 /* associate packet with eigrp interface */
542 /* eigrp_verify_header() relies on a valid "ei" and thus can be called
544 after the checks below are passed. These checks in turn access the
545 fields of unverified "eigrph" structure for their own purposes and
546 must remain very accurate in doing this.
551 /* Self-originated packet should be discarded silently. */
552 if (eigrp_if_lookup_by_local_addr(eigrp
, NULL
, iph
->ip_src
)
553 || (IPV4_ADDR_SAME(&srcaddr
, &ei
->address
.u
.prefix4
))) {
554 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
556 "eigrp_read[%pI4]: Dropping self-originated packet",
561 /* Advance from IP header to EIGRP header (iph->ip_hl has been verified
562 by eigrp_recv_packet() to be correct). */
564 stream_forward_getp(ibuf
, (iph
->ip_hl
* 4));
565 eigrph
= (struct eigrp_header
*)stream_pnt(ibuf
);
567 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
)
568 && IS_DEBUG_EIGRP_TRANSMIT(0, PACKET_DETAIL
))
569 eigrp_header_dump(eigrph
);
571 if (ntohs(eigrph
->ASNumber
) != eigrp
->AS
) {
572 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
574 "ignoring packet from router %u sent to %pI4, wrong AS Number received: %u",
575 ntohs(eigrph
->vrid
), &iph
->ip_dst
,
576 ntohs(eigrph
->ASNumber
));
580 /* If incoming interface is passive one, ignore it. */
581 if (eigrp_if_is_passive(ei
)) {
582 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
584 "ignoring packet from router %u sent to %pI4, received on a passive interface, %pI4",
585 ntohs(eigrph
->vrid
), &iph
->ip_dst
,
586 &ei
->address
.u
.prefix4
);
588 if (iph
->ip_dst
.s_addr
== htonl(EIGRP_MULTICAST_ADDRESS
)) {
589 eigrp_if_set_multicast(ei
);
594 /* else it must be a local eigrp interface, check it was received on
597 else if (ei
->ifp
!= ifp
) {
598 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
600 "Packet from [%pI4] received on wrong link %s",
601 &iph
->ip_src
, ifp
->name
);
605 /* Verify more EIGRP header fields. */
606 ret
= eigrp_verify_header(ibuf
, ei
, iph
, eigrph
);
608 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
610 "eigrp_read[%pI4]: Header check failed, dropping.",
615 /* calcualte the eigrp packet length, and move the pounter to the
616 start of the eigrp TLVs */
617 opcode
= eigrph
->opcode
;
619 if (IS_DEBUG_EIGRP_TRANSMIT(0, RECV
))
621 "Received [%s][%d/%d] length [%u] via [%s] src [%pI4] dst [%pI4]",
622 lookup_msg(eigrp_packet_type_str
, opcode
, NULL
),
623 ntohl(eigrph
->sequence
), ntohl(eigrph
->ack
), length
,
624 IF_NAME(ei
), &iph
->ip_src
, &iph
->ip_dst
);
626 /* Read rest of the packet and call each sort of packet routine. */
627 stream_forward_getp(ibuf
, EIGRP_HEADER_LEN
);
629 /* New testing block of code for handling Acks */
630 if (ntohl(eigrph
->ack
) != 0) {
631 struct eigrp_packet
*ep
= NULL
;
633 nbr
= eigrp_nbr_get(ei
, eigrph
, iph
);
635 // neighbor must be valid, eigrp_nbr_get creates if none existed
638 ep
= eigrp_fifo_next(nbr
->retrans_queue
);
639 if ((ep
) && (ntohl(eigrph
->ack
) == ep
->sequence_number
)) {
640 ep
= eigrp_fifo_pop(nbr
->retrans_queue
);
641 eigrp_packet_free(ep
);
643 if ((nbr
->state
== EIGRP_NEIGHBOR_PENDING
)
644 && (ntohl(eigrph
->ack
)
645 == nbr
->init_sequence_number
)) {
646 eigrp_nbr_state_set(nbr
, EIGRP_NEIGHBOR_UP
);
648 "Neighbor(%pI4) adjacency became full",
650 nbr
->init_sequence_number
= 0;
651 nbr
->recv_sequence_number
=
652 ntohl(eigrph
->sequence
);
653 eigrp_update_send_EOT(nbr
);
655 eigrp_send_packet_reliably(nbr
);
657 ep
= eigrp_fifo_next(nbr
->multicast_queue
);
659 if (ntohl(eigrph
->ack
) == ep
->sequence_number
) {
660 ep
= eigrp_fifo_pop(nbr
->multicast_queue
);
661 eigrp_packet_free(ep
);
662 if (nbr
->multicast_queue
->count
> 0) {
663 eigrp_send_packet_reliably(nbr
);
671 case EIGRP_OPC_HELLO
:
672 eigrp_hello_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
674 case EIGRP_OPC_PROBE
:
675 // eigrp_probe_receive(eigrp, iph, eigrph, ibuf, ei,
678 case EIGRP_OPC_QUERY
:
679 eigrp_query_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
681 case EIGRP_OPC_REPLY
:
682 eigrp_reply_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
684 case EIGRP_OPC_REQUEST
:
685 // eigrp_request_receive(eigrp, iph, eigrph, ibuf, ei,
688 case EIGRP_OPC_SIAQUERY
:
689 eigrp_query_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
691 case EIGRP_OPC_SIAREPLY
:
692 eigrp_reply_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
694 case EIGRP_OPC_UPDATE
:
695 eigrp_update_receive(eigrp
, iph
, eigrph
, ibuf
, ei
, length
);
699 "interface %s: EIGRP packet header type %d unsupported",
700 IF_NAME(ei
), opcode
);
707 static struct stream
*eigrp_recv_packet(struct eigrp
*eigrp
,
708 int fd
, struct interface
**ifp
,
714 unsigned int ifindex
= 0;
716 /* Header and data both require alignment. */
717 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
720 memset(&msgh
, 0, sizeof(struct msghdr
));
723 msgh
.msg_control
= (caddr_t
)buff
;
724 msgh
.msg_controllen
= sizeof(buff
);
726 ret
= stream_recvmsg(ibuf
, fd
, &msgh
, 0, (EIGRP_PACKET_MAX_LEN
+ 1));
728 zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno
));
731 if ((unsigned int)ret
< sizeof(*iph
)) /* ret must be > 0 now */
734 "eigrp_recv_packet: discarding runt packet of length %d (ip header size is %u)",
735 ret
, (unsigned int)sizeof(*iph
));
739 /* Note that there should not be alignment problems with this assignment
740 because this is at the beginning of the stream data buffer. */
741 iph
= (struct ip
*)STREAM_DATA(ibuf
);
742 sockopt_iphdrincl_swab_systoh(iph
);
744 ip_len
= iph
->ip_len
;
746 #if defined(__FreeBSD__) && (__FreeBSD_version < 1000000)
748 * Kernel network code touches incoming IP header parameters,
749 * before protocol specific processing.
751 * 1) Convert byteorder to host representation.
752 * --> ip_len, ip_id, ip_off
754 * 2) Adjust ip_len to strip IP header size!
755 * --> If user process receives entire IP packet via RAW
756 * socket, it must consider adding IP header size to
757 * the "ip_len" field of "ip" structure.
759 * For more details, see <netinet/ip_input.c>.
761 ip_len
= ip_len
+ (iph
->ip_hl
<< 2);
764 #if defined(__DragonFly__)
766 * in DragonFly's raw socket, ip_len/ip_off are read
767 * in network byte order.
768 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
770 ip_len
= ntohs(iph
->ip_len
) + (iph
->ip_hl
<< 2);
773 ifindex
= getsockopt_ifindex(AF_INET
, &msgh
);
775 *ifp
= if_lookup_by_index(ifindex
, eigrp
->vrf_id
);
779 "eigrp_recv_packet read length mismatch: ip_len is %d, but recvmsg returned %d",
787 struct eigrp_fifo
*eigrp_fifo_new(void)
789 struct eigrp_fifo
*new;
791 new = XCALLOC(MTYPE_EIGRP_FIFO
, sizeof(struct eigrp_fifo
));
795 /* Free eigrp packet fifo. */
796 void eigrp_fifo_free(struct eigrp_fifo
*fifo
)
798 struct eigrp_packet
*ep
;
799 struct eigrp_packet
*next
;
801 for (ep
= fifo
->head
; ep
; ep
= next
) {
803 eigrp_packet_free(ep
);
805 fifo
->head
= fifo
->tail
= NULL
;
808 XFREE(MTYPE_EIGRP_FIFO
, fifo
);
811 /* Free eigrp fifo entries without destroying fifo itself*/
812 void eigrp_fifo_reset(struct eigrp_fifo
*fifo
)
814 struct eigrp_packet
*ep
;
815 struct eigrp_packet
*next
;
817 for (ep
= fifo
->head
; ep
; ep
= next
) {
819 eigrp_packet_free(ep
);
821 fifo
->head
= fifo
->tail
= NULL
;
825 struct eigrp_packet
*eigrp_packet_new(size_t size
, struct eigrp_neighbor
*nbr
)
827 struct eigrp_packet
*new;
829 new = XCALLOC(MTYPE_EIGRP_PACKET
, sizeof(struct eigrp_packet
));
830 new->s
= stream_new(size
);
831 new->retrans_counter
= 0;
837 void eigrp_send_packet_reliably(struct eigrp_neighbor
*nbr
)
839 struct eigrp_packet
*ep
;
841 ep
= eigrp_fifo_next(nbr
->retrans_queue
);
844 struct eigrp_packet
*duplicate
;
845 duplicate
= eigrp_packet_duplicate(ep
, nbr
);
846 /* Add packet to the top of the interface output queue*/
847 eigrp_fifo_push(nbr
->ei
->obuf
, duplicate
);
849 /*Start retransmission timer*/
850 thread_add_timer(master
, eigrp_unack_packet_retrans
, nbr
,
851 EIGRP_PACKET_RETRANS_TIME
,
852 &ep
->t_retrans_timer
);
854 /*Increment sequence number counter*/
855 nbr
->ei
->eigrp
->sequence_number
++;
857 /* Hook thread to write packet. */
858 if (nbr
->ei
->on_write_q
== 0) {
859 listnode_add(nbr
->ei
->eigrp
->oi_write_q
, nbr
->ei
);
860 nbr
->ei
->on_write_q
= 1;
862 thread_add_write(master
, eigrp_write
, nbr
->ei
->eigrp
,
863 nbr
->ei
->eigrp
->fd
, &nbr
->ei
->eigrp
->t_write
);
867 /* Calculate EIGRP checksum */
868 void eigrp_packet_checksum(struct eigrp_interface
*ei
, struct stream
*s
,
871 struct eigrp_header
*eigrph
;
873 eigrph
= (struct eigrp_header
*)STREAM_DATA(s
);
875 /* Calculate checksum. */
876 eigrph
->checksum
= in_cksum(eigrph
, length
);
879 /* Make EIGRP header. */
880 void eigrp_packet_header_init(int type
, struct eigrp
*eigrp
, struct stream
*s
,
881 uint32_t flags
, uint32_t sequence
, uint32_t ack
)
883 struct eigrp_header
*eigrph
;
886 eigrph
= (struct eigrp_header
*)STREAM_DATA(s
);
888 eigrph
->version
= (uint8_t)EIGRP_HEADER_VERSION
;
889 eigrph
->opcode
= (uint8_t)type
;
890 eigrph
->checksum
= 0;
892 eigrph
->vrid
= htons(eigrp
->vrid
);
893 eigrph
->ASNumber
= htons(eigrp
->AS
);
894 eigrph
->ack
= htonl(ack
);
895 eigrph
->sequence
= htonl(sequence
);
896 // if(flags == EIGRP_INIT_FLAG)
897 // eigrph->sequence = htonl(3);
898 eigrph
->flags
= htonl(flags
);
900 if (IS_DEBUG_EIGRP_TRANSMIT(0, PACKET_DETAIL
))
901 zlog_debug("Packet Header Init Seq [%u] Ack [%u]",
902 htonl(eigrph
->sequence
), htonl(eigrph
->ack
));
904 stream_forward_endp(s
, EIGRP_HEADER_LEN
);
907 /* Add new packet to head of fifo. */
908 void eigrp_fifo_push(struct eigrp_fifo
*fifo
, struct eigrp_packet
*ep
)
910 ep
->next
= fifo
->head
;
913 if (fifo
->tail
== NULL
)
916 if (fifo
->count
!= 0)
917 fifo
->head
->previous
= ep
;
924 /* Return last fifo entry. */
925 struct eigrp_packet
*eigrp_fifo_next(struct eigrp_fifo
*fifo
)
930 void eigrp_packet_delete(struct eigrp_interface
*ei
)
932 struct eigrp_packet
*ep
;
934 ep
= eigrp_fifo_pop(ei
->obuf
);
937 eigrp_packet_free(ep
);
940 void eigrp_packet_free(struct eigrp_packet
*ep
)
945 THREAD_OFF(ep
->t_retrans_timer
);
947 XFREE(MTYPE_EIGRP_PACKET
, ep
);
950 /* EIGRP Header verification. */
951 static int eigrp_verify_header(struct stream
*ibuf
, struct eigrp_interface
*ei
,
952 struct ip
*iph
, struct eigrp_header
*eigrph
)
954 /* Check network mask, Silently discarded. */
955 if (!eigrp_check_network_mask(ei
, iph
->ip_src
)) {
957 "interface %s: eigrp_read network address is not same [%pI4]",
958 IF_NAME(ei
), &iph
->ip_src
);
962 // /* Check authentication. The function handles logging actions, where
964 // if (! eigrp_check_auth(ei, eigrph))
970 /* Unbound socket will accept any Raw IP packets if proto is matched.
971 To prevent it, compare src IP address and i/f address with masking
973 static int eigrp_check_network_mask(struct eigrp_interface
*ei
,
974 struct in_addr ip_src
)
976 struct in_addr mask
, me
, him
;
978 if (ei
->type
== EIGRP_IFTYPE_POINTOPOINT
)
981 masklen2ip(ei
->address
.prefixlen
, &mask
);
983 me
.s_addr
= ei
->address
.u
.prefix4
.s_addr
& mask
.s_addr
;
984 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
986 if (IPV4_ADDR_SAME(&me
, &him
))
992 int eigrp_unack_packet_retrans(struct thread
*thread
)
994 struct eigrp_neighbor
*nbr
;
995 nbr
= (struct eigrp_neighbor
*)THREAD_ARG(thread
);
997 struct eigrp_packet
*ep
;
998 ep
= eigrp_fifo_next(nbr
->retrans_queue
);
1001 struct eigrp_packet
*duplicate
;
1002 duplicate
= eigrp_packet_duplicate(ep
, nbr
);
1004 /* Add packet to the top of the interface output queue*/
1005 eigrp_fifo_push(nbr
->ei
->obuf
, duplicate
);
1007 ep
->retrans_counter
++;
1008 if (ep
->retrans_counter
== EIGRP_PACKET_RETRANS_MAX
)
1009 return eigrp_retrans_count_exceeded(ep
, nbr
);
1011 /*Start retransmission timer*/
1012 thread_add_timer(master
, eigrp_unack_packet_retrans
, nbr
,
1013 EIGRP_PACKET_RETRANS_TIME
,
1014 &ep
->t_retrans_timer
);
1016 /* Hook thread to write packet. */
1017 if (nbr
->ei
->on_write_q
== 0) {
1018 listnode_add(nbr
->ei
->eigrp
->oi_write_q
, nbr
->ei
);
1019 nbr
->ei
->on_write_q
= 1;
1021 thread_add_write(master
, eigrp_write
, nbr
->ei
->eigrp
,
1022 nbr
->ei
->eigrp
->fd
, &nbr
->ei
->eigrp
->t_write
);
1028 int eigrp_unack_multicast_packet_retrans(struct thread
*thread
)
1030 struct eigrp_neighbor
*nbr
;
1031 nbr
= (struct eigrp_neighbor
*)THREAD_ARG(thread
);
1033 struct eigrp_packet
*ep
;
1034 ep
= eigrp_fifo_next(nbr
->multicast_queue
);
1037 struct eigrp_packet
*duplicate
;
1038 duplicate
= eigrp_packet_duplicate(ep
, nbr
);
1039 /* Add packet to the top of the interface output queue*/
1040 eigrp_fifo_push(nbr
->ei
->obuf
, duplicate
);
1042 ep
->retrans_counter
++;
1043 if (ep
->retrans_counter
== EIGRP_PACKET_RETRANS_MAX
)
1044 return eigrp_retrans_count_exceeded(ep
, nbr
);
1046 /*Start retransmission timer*/
1047 thread_add_timer(master
, eigrp_unack_multicast_packet_retrans
,
1048 nbr
, EIGRP_PACKET_RETRANS_TIME
,
1049 &ep
->t_retrans_timer
);
1051 /* Hook thread to write packet. */
1052 if (nbr
->ei
->on_write_q
== 0) {
1053 listnode_add(nbr
->ei
->eigrp
->oi_write_q
, nbr
->ei
);
1054 nbr
->ei
->on_write_q
= 1;
1056 thread_add_write(master
, eigrp_write
, nbr
->ei
->eigrp
,
1057 nbr
->ei
->eigrp
->fd
, &nbr
->ei
->eigrp
->t_write
);
1063 /* Get packet from tail of fifo. */
1064 struct eigrp_packet
*eigrp_fifo_pop(struct eigrp_fifo
*fifo
)
1066 struct eigrp_packet
*ep
= NULL
;
1071 fifo
->tail
= ep
->previous
;
1073 if (fifo
->tail
== NULL
)
1076 fifo
->tail
->next
= NULL
;
1084 struct eigrp_packet
*eigrp_packet_duplicate(struct eigrp_packet
*old
,
1085 struct eigrp_neighbor
*nbr
)
1087 struct eigrp_packet
*new;
1089 new = eigrp_packet_new(EIGRP_PACKET_MTU(nbr
->ei
->ifp
->mtu
), nbr
);
1090 new->length
= old
->length
;
1091 new->retrans_counter
= old
->retrans_counter
;
1092 new->dst
= old
->dst
;
1093 new->sequence_number
= old
->sequence_number
;
1094 stream_copy(new->s
, old
->s
);
1099 static struct TLV_IPv4_Internal_type
*eigrp_IPv4_InternalTLV_new(void)
1101 struct TLV_IPv4_Internal_type
*new;
1103 new = XCALLOC(MTYPE_EIGRP_IPV4_INT_TLV
,
1104 sizeof(struct TLV_IPv4_Internal_type
));
1109 struct TLV_IPv4_Internal_type
*eigrp_read_ipv4_tlv(struct stream
*s
)
1111 struct TLV_IPv4_Internal_type
*tlv
;
1112 uint32_t destination_tmp
;
1114 tlv
= eigrp_IPv4_InternalTLV_new();
1116 tlv
->type
= stream_getw(s
);
1117 tlv
->length
= stream_getw(s
);
1118 tlv
->forward
.s_addr
= stream_getl(s
);
1119 tlv
->metric
.delay
= stream_getl(s
);
1120 tlv
->metric
.bandwidth
= stream_getl(s
);
1121 tlv
->metric
.mtu
[0] = stream_getc(s
);
1122 tlv
->metric
.mtu
[1] = stream_getc(s
);
1123 tlv
->metric
.mtu
[2] = stream_getc(s
);
1124 tlv
->metric
.hop_count
= stream_getc(s
);
1125 tlv
->metric
.reliability
= stream_getc(s
);
1126 tlv
->metric
.load
= stream_getc(s
);
1127 tlv
->metric
.tag
= stream_getc(s
);
1128 tlv
->metric
.flags
= stream_getc(s
);
1130 tlv
->prefix_length
= stream_getc(s
);
1132 destination_tmp
= stream_getc(s
) << 24;
1133 if (tlv
->prefix_length
> 8)
1134 destination_tmp
|= stream_getc(s
) << 16;
1135 if (tlv
->prefix_length
> 16)
1136 destination_tmp
|= stream_getc(s
) << 8;
1137 if (tlv
->prefix_length
> 24)
1138 destination_tmp
|= stream_getc(s
);
1140 tlv
->destination
.s_addr
= htonl(destination_tmp
);
1145 uint16_t eigrp_add_internalTLV_to_stream(struct stream
*s
,
1146 struct eigrp_prefix_descriptor
*pe
)
1150 stream_putw(s
, EIGRP_TLV_IPv4_INT
);
1151 switch (pe
->destination
->prefixlen
) {
1161 length
= EIGRP_TLV_IPV4_SIZE_GRT_0_BIT
;
1162 stream_putw(s
, length
);
1172 length
= EIGRP_TLV_IPV4_SIZE_GRT_8_BIT
;
1173 stream_putw(s
, length
);
1183 length
= EIGRP_TLV_IPV4_SIZE_GRT_16_BIT
;
1184 stream_putw(s
, length
);
1194 length
= EIGRP_TLV_IPV4_SIZE_GRT_24_BIT
;
1195 stream_putw(s
, length
);
1198 flog_err(EC_LIB_DEVELOPMENT
, "%s: Unexpected prefix length: %d",
1199 __func__
, pe
->destination
->prefixlen
);
1202 stream_putl(s
, 0x00000000);
1205 stream_putl(s
, pe
->reported_metric
.delay
);
1206 stream_putl(s
, pe
->reported_metric
.bandwidth
);
1207 stream_putc(s
, pe
->reported_metric
.mtu
[2]);
1208 stream_putc(s
, pe
->reported_metric
.mtu
[1]);
1209 stream_putc(s
, pe
->reported_metric
.mtu
[0]);
1210 stream_putc(s
, pe
->reported_metric
.hop_count
);
1211 stream_putc(s
, pe
->reported_metric
.reliability
);
1212 stream_putc(s
, pe
->reported_metric
.load
);
1213 stream_putc(s
, pe
->reported_metric
.tag
);
1214 stream_putc(s
, pe
->reported_metric
.flags
);
1216 stream_putc(s
, pe
->destination
->prefixlen
);
1218 stream_putc(s
, (ntohl(pe
->destination
->u
.prefix4
.s_addr
) >> 24) & 0xFF);
1219 if (pe
->destination
->prefixlen
> 8)
1220 stream_putc(s
, (ntohl(pe
->destination
->u
.prefix4
.s_addr
) >> 16) & 0xFF);
1221 if (pe
->destination
->prefixlen
> 16)
1222 stream_putc(s
, (ntohl(pe
->destination
->u
.prefix4
.s_addr
) >> 8) & 0xFF);
1223 if (pe
->destination
->prefixlen
> 24)
1224 stream_putc(s
, ntohl(pe
->destination
->u
.prefix4
.s_addr
) & 0xFF);
1229 uint16_t eigrp_add_authTLV_MD5_to_stream(struct stream
*s
,
1230 struct eigrp_interface
*ei
)
1233 struct keychain
*keychain
;
1234 struct TLV_MD5_Authentication_Type
*authTLV
;
1236 authTLV
= eigrp_authTLV_MD5_new();
1238 authTLV
->type
= htons(EIGRP_TLV_AUTH
);
1239 authTLV
->length
= htons(EIGRP_AUTH_MD5_TLV_SIZE
);
1240 authTLV
->auth_type
= htons(EIGRP_AUTH_TYPE_MD5
);
1241 authTLV
->auth_length
= htons(EIGRP_AUTH_TYPE_MD5_LEN
);
1242 authTLV
->key_sequence
= 0;
1243 memset(authTLV
->Nullpad
, 0, sizeof(authTLV
->Nullpad
));
1245 keychain
= keychain_lookup(ei
->params
.auth_keychain
);
1247 key
= key_lookup_for_send(keychain
);
1249 free(ei
->params
.auth_keychain
);
1250 ei
->params
.auth_keychain
= NULL
;
1251 eigrp_authTLV_MD5_free(authTLV
);
1256 authTLV
->key_id
= htonl(key
->index
);
1257 memset(authTLV
->digest
, 0, EIGRP_AUTH_TYPE_MD5_LEN
);
1258 stream_put(s
, authTLV
,
1259 sizeof(struct TLV_MD5_Authentication_Type
));
1260 eigrp_authTLV_MD5_free(authTLV
);
1261 return EIGRP_AUTH_MD5_TLV_SIZE
;
1264 eigrp_authTLV_MD5_free(authTLV
);
1269 uint16_t eigrp_add_authTLV_SHA256_to_stream(struct stream
*s
,
1270 struct eigrp_interface
*ei
)
1273 struct keychain
*keychain
;
1274 struct TLV_SHA256_Authentication_Type
*authTLV
;
1276 authTLV
= eigrp_authTLV_SHA256_new();
1278 authTLV
->type
= htons(EIGRP_TLV_AUTH
);
1279 authTLV
->length
= htons(EIGRP_AUTH_SHA256_TLV_SIZE
);
1280 authTLV
->auth_type
= htons(EIGRP_AUTH_TYPE_SHA256
);
1281 authTLV
->auth_length
= htons(EIGRP_AUTH_TYPE_SHA256_LEN
);
1282 authTLV
->key_sequence
= 0;
1283 memset(authTLV
->Nullpad
, 0, sizeof(authTLV
->Nullpad
));
1285 keychain
= keychain_lookup(ei
->params
.auth_keychain
);
1287 key
= key_lookup_for_send(keychain
);
1289 free(ei
->params
.auth_keychain
);
1290 ei
->params
.auth_keychain
= NULL
;
1291 eigrp_authTLV_SHA256_free(authTLV
);
1296 authTLV
->key_id
= 0;
1297 memset(authTLV
->digest
, 0, EIGRP_AUTH_TYPE_SHA256_LEN
);
1298 stream_put(s
, authTLV
,
1299 sizeof(struct TLV_SHA256_Authentication_Type
));
1300 eigrp_authTLV_SHA256_free(authTLV
);
1301 return EIGRP_AUTH_SHA256_TLV_SIZE
;
1304 eigrp_authTLV_SHA256_free(authTLV
);
1309 struct TLV_MD5_Authentication_Type
*eigrp_authTLV_MD5_new(void)
1311 struct TLV_MD5_Authentication_Type
*new;
1313 new = XCALLOC(MTYPE_EIGRP_AUTH_TLV
,
1314 sizeof(struct TLV_MD5_Authentication_Type
));
1319 void eigrp_authTLV_MD5_free(struct TLV_MD5_Authentication_Type
*authTLV
)
1321 XFREE(MTYPE_EIGRP_AUTH_TLV
, authTLV
);
1324 struct TLV_SHA256_Authentication_Type
*eigrp_authTLV_SHA256_new(void)
1326 struct TLV_SHA256_Authentication_Type
*new;
1328 new = XCALLOC(MTYPE_EIGRP_AUTH_SHA256_TLV
,
1329 sizeof(struct TLV_SHA256_Authentication_Type
));
1334 void eigrp_authTLV_SHA256_free(struct TLV_SHA256_Authentication_Type
*authTLV
)
1336 XFREE(MTYPE_EIGRP_AUTH_SHA256_TLV
, authTLV
);
1339 void eigrp_IPv4_InternalTLV_free(
1340 struct TLV_IPv4_Internal_type
*IPv4_InternalTLV
)
1342 XFREE(MTYPE_EIGRP_IPV4_INT_TLV
, IPv4_InternalTLV
);
1345 struct TLV_Sequence_Type
*eigrp_SequenceTLV_new(void)
1347 struct TLV_Sequence_Type
*new;
1349 new = XCALLOC(MTYPE_EIGRP_SEQ_TLV
, sizeof(struct TLV_Sequence_Type
));