2 * EIGRP Sending and Receiving EIGRP Hello Packets.
3 * Copyright (C) 2013-2016
15 * This file is part of GNU Zebra.
17 * GNU Zebra is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2, or (at your option) any
22 * GNU Zebra is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; see the file COPYING; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "sockunion.h"
48 #include "eigrpd/eigrp_structs.h"
49 #include "eigrpd/eigrpd.h"
50 #include "eigrpd/eigrp_interface.h"
51 #include "eigrpd/eigrp_neighbor.h"
52 #include "eigrpd/eigrp_packet.h"
53 #include "eigrpd/eigrp_zebra.h"
54 #include "eigrpd/eigrp_vty.h"
55 #include "eigrpd/eigrp_dump.h"
56 #include "eigrpd/eigrp_macros.h"
57 #include "eigrpd/eigrp_errors.h"
59 /* Packet Type String. */
60 static const struct message eigrp_general_tlv_type_str
[] = {
61 {EIGRP_TLV_PARAMETER
, "PARAMETER"},
62 {EIGRP_TLV_AUTH
, "AUTH"},
63 {EIGRP_TLV_SEQ
, "SEQ"},
64 {EIGRP_TLV_SW_VERSION
, "SW_VERSION"},
65 {EIGRP_TLV_NEXT_MCAST_SEQ
, "NEXT_MCAST_SEQ"},
66 {EIGRP_TLV_PEER_TERMINATION
, "PEER_TERMINATION"},
67 {EIGRP_TLV_PEER_MTRLIST
, "PEER_MTRLIST"},
68 {EIGRP_TLV_PEER_TIDLIST
, "PEER_TIDLIST"},
73 * @fn eigrp_hello_timer
75 * @param[in] thread current execution thread timer is associated with
77 * @return int always returns 0
80 * Called once per "hello" time interval, default 5 seconds
81 * Sends hello packet via multicast for all interfaces eigrp
84 int eigrp_hello_timer(struct thread
*thread
)
86 struct eigrp_interface
*ei
;
88 ei
= THREAD_ARG(thread
);
91 if (IS_DEBUG_EIGRP(0, TIMERS
))
92 zlog_debug("Start Hello Timer (%s) Expire [%u]", IF_NAME(ei
),
95 /* Sending hello packet. */
96 eigrp_hello_send(ei
, EIGRP_HELLO_NORMAL
, NULL
);
98 /* Hello timer set. */
100 thread_add_timer(master
, eigrp_hello_timer
, ei
, ei
->params
.v_hello
,
107 * @fn eigrp_hello_parameter_decode
109 * @param[in] nbr neighbor the ACK should be sent to
110 * @param[in] param pointer packet TLV is stored to
112 * @return uint16_t number of bytes added to packet stream
115 * Encode Parameter TLV, used to convey metric weights and the hold time.
118 * Note the addition of K6 for the new extended metrics, and does not apply to
119 * older TLV packet formats.
121 static struct eigrp_neighbor
*
122 eigrp_hello_parameter_decode(struct eigrp_neighbor
*nbr
,
123 struct eigrp_tlv_hdr_type
*tlv
)
125 struct eigrp
*eigrp
= nbr
->ei
->eigrp
;
126 struct TLV_Parameter_Type
*param
= (struct TLV_Parameter_Type
*)tlv
;
128 /* copy over the values passed in by the neighbor */
135 nbr
->v_holddown
= ntohs(param
->hold_time
);
138 * Check K1-K5 have the correct values to be able to become neighbors
139 * K6 does not have to match
141 if ((eigrp
->k_values
[0] == nbr
->K1
) && (eigrp
->k_values
[1] == nbr
->K2
)
142 && (eigrp
->k_values
[2] == nbr
->K3
)
143 && (eigrp
->k_values
[3] == nbr
->K4
)
144 && (eigrp
->k_values
[4] == nbr
->K5
)) {
146 if (eigrp_nbr_state_get(nbr
) == EIGRP_NEIGHBOR_DOWN
) {
147 zlog_info("Neighbor %s (%s) is pending: new adjacency",
149 ifindex2ifname(nbr
->ei
->ifp
->ifindex
,
152 /* Expedited hello sent */
153 eigrp_hello_send(nbr
->ei
, EIGRP_HELLO_NORMAL
, NULL
);
155 // if(ntohl(nbr->ei->address->u.prefix4.s_addr) >
156 // ntohl(nbr->src.s_addr))
157 eigrp_update_send_init(nbr
);
159 eigrp_nbr_state_set(nbr
, EIGRP_NEIGHBOR_PENDING
);
162 if (eigrp_nbr_state_get(nbr
) != EIGRP_NEIGHBOR_DOWN
) {
163 if ((param
->K1
& param
->K2
& param
->K3
& param
->K4
167 "Neighbor %s (%s) is down: Interface PEER-TERMINATION received",
169 ifindex2ifname(nbr
->ei
->ifp
->ifindex
,
171 eigrp_nbr_delete(nbr
);
175 "Neighbor %s (%s) going down: Kvalue mismatch",
177 ifindex2ifname(nbr
->ei
->ifp
->ifindex
,
179 eigrp_nbr_state_set(nbr
, EIGRP_NEIGHBOR_DOWN
);
188 eigrp_hello_authentication_decode(struct stream
*s
,
189 struct eigrp_tlv_hdr_type
*tlv_header
,
190 struct eigrp_neighbor
*nbr
)
192 struct TLV_MD5_Authentication_Type
*md5
;
194 md5
= (struct TLV_MD5_Authentication_Type
*)tlv_header
;
196 if (md5
->auth_type
== EIGRP_AUTH_TYPE_MD5
)
197 return eigrp_check_md5_digest(s
, md5
, nbr
,
198 EIGRP_AUTH_BASIC_HELLO_FLAG
);
199 else if (md5
->auth_type
== EIGRP_AUTH_TYPE_SHA256
)
200 return eigrp_check_sha256_digest(
201 s
, (struct TLV_SHA256_Authentication_Type
*)tlv_header
,
202 nbr
, EIGRP_AUTH_BASIC_HELLO_FLAG
);
208 * @fn eigrp_sw_version_decode
210 * @param[in] nbr neighbor the ACK shoudl be sent to
211 * @param[in] param pointer to TLV software version information
216 * Read the software version in the specified location.
217 * This consists of two bytes of OS version, and two bytes of EIGRP
220 static void eigrp_sw_version_decode(struct eigrp_neighbor
*nbr
,
221 struct eigrp_tlv_hdr_type
*tlv
)
223 struct TLV_Software_Type
*version
= (struct TLV_Software_Type
*)tlv
;
225 nbr
->os_rel_major
= version
->vender_major
;
226 nbr
->os_rel_minor
= version
->vender_minor
;
227 nbr
->tlv_rel_major
= version
->eigrp_major
;
228 nbr
->tlv_rel_minor
= version
->eigrp_minor
;
233 * @fn eigrp_peer_termination_decode
235 * @param[in] nbr neighbor the ACK shoudl be sent to
236 * @param[in] tlv pointer to TLV software version information
241 * Read the address in the TLV and match to out address. If
242 * a match is found, move the sending neighbor to the down state. If
243 * out address is not in the TLV, then ignore the peer termination
245 static void eigrp_peer_termination_decode(struct eigrp_neighbor
*nbr
,
246 struct eigrp_tlv_hdr_type
*tlv
)
248 struct eigrp
*eigrp
= nbr
->ei
->eigrp
;
249 struct TLV_Peer_Termination_type
*param
=
250 (struct TLV_Peer_Termination_type
*)tlv
;
252 uint32_t my_ip
= nbr
->ei
->address
.u
.prefix4
.s_addr
;
253 uint32_t received_ip
= param
->neighbor_ip
;
255 if (my_ip
== received_ip
) {
256 zlog_info("Neighbor %s (%s) is down: Peer Termination received",
258 ifindex2ifname(nbr
->ei
->ifp
->ifindex
, eigrp
->vrf_id
));
259 /* set neighbor to DOWN */
260 nbr
->state
= EIGRP_NEIGHBOR_DOWN
;
261 /* delete neighbor */
262 eigrp_nbr_delete(nbr
);
267 * @fn eigrp_peer_termination_encode
269 * @param[in,out] s packet stream TLV is stored to
270 * @param[in] nbr_addr pointer to neighbor address for Peer
273 * @return uint16_t number of bytes added to packet stream
276 * Function used to encode Peer Termination TLV to Hello packet.
278 static uint16_t eigrp_peer_termination_encode(struct stream
*s
,
279 struct in_addr
*nbr_addr
)
281 uint16_t length
= EIGRP_TLV_PEER_TERMINATION_LEN
;
283 /* fill in type and length */
284 stream_putw(s
, EIGRP_TLV_PEER_TERMINATION
);
285 stream_putw(s
, length
);
287 /* fill in unknown field 0x04 */
288 stream_putc(s
, 0x04);
290 /* finally neighbor IP address */
291 stream_put_ipv4(s
, nbr_addr
->s_addr
);
297 * @fn eigrp_hello_receive
299 * @param[in] eigrp eigrp routing process
300 * @param[in] iph pointer to ip header
301 * @param[in] eigrph pointer to eigrp header
302 * @param[in] s input ip stream
303 * @param[in] ei eigrp interface packet arrived on
304 * @param[in] size size of eigrp packet
309 * This is the main worker function for processing hello packets. It
310 * will validate the peer associated with the src ip address of the ip
311 * header, and then decode each of the general TLVs which the packet
315 * Not all TLVs are current decoder. This is a work in progress..
317 void eigrp_hello_receive(struct eigrp
*eigrp
, struct ip
*iph
,
318 struct eigrp_header
*eigrph
, struct stream
*s
,
319 struct eigrp_interface
*ei
, int size
)
321 struct eigrp_tlv_hdr_type
*tlv_header
;
322 struct eigrp_neighbor
*nbr
;
326 /* get neighbor struct */
327 nbr
= eigrp_nbr_get(ei
, eigrph
, iph
);
329 /* neighbor must be valid, eigrp_nbr_get creates if none existed */
332 if (IS_DEBUG_EIGRP_PACKET(eigrph
->opcode
- 1, RECV
))
333 zlog_debug("Processing Hello size[%u] int(%s) nbr(%s)", size
,
334 ifindex2ifname(nbr
->ei
->ifp
->ifindex
, eigrp
->vrf_id
),
335 inet_ntoa(nbr
->src
));
337 size
-= EIGRP_HEADER_LEN
;
341 tlv_header
= (struct eigrp_tlv_hdr_type
*)eigrph
->tlv
;
344 type
= ntohs(tlv_header
->type
);
345 length
= ntohs(tlv_header
->length
);
347 if ((length
> 0) && (length
<= size
)) {
348 if (IS_DEBUG_EIGRP_PACKET(0, RECV
))
351 lookup_msg(eigrp_general_tlv_type_str
,
354 // determine what General TLV is being processed
356 case EIGRP_TLV_PARAMETER
:
357 nbr
= eigrp_hello_parameter_decode(nbr
,
362 case EIGRP_TLV_AUTH
: {
363 if (eigrp_hello_authentication_decode(
373 case EIGRP_TLV_SW_VERSION
:
374 eigrp_sw_version_decode(nbr
, tlv_header
);
376 case EIGRP_TLV_NEXT_MCAST_SEQ
:
378 case EIGRP_TLV_PEER_TERMINATION
:
379 eigrp_peer_termination_decode(nbr
, tlv_header
);
382 case EIGRP_TLV_PEER_MTRLIST
:
383 case EIGRP_TLV_PEER_TIDLIST
:
390 tlv_header
= (struct eigrp_tlv_hdr_type
*)(((char *)tlv_header
)
397 /*If received packet is hello with Parameter TLV*/
398 if (ntohl(eigrph
->ack
) == 0) {
399 /* increment statistics. */
402 eigrp_nbr_state_update(nbr
);
405 if (IS_DEBUG_EIGRP_PACKET(0, RECV
))
406 zlog_debug("Hello Packet received from %s",
407 inet_ntoa(nbr
->src
));
413 void eigrp_sw_version_initialize(void)
415 char ver_string
[] = VERSION
;
416 char *dash
= strstr(ver_string
, "-");
422 ret
= sscanf(ver_string
, "%" SCNu32
".%" SCNu32
, &FRR_MAJOR
,
425 flog_err(EC_EIGRP_PACKET
,
426 "Did not Properly parse %s, please fix VERSION string",
431 * @fn eigrp_sw_version_encode
433 * @param[in,out] s packet stream TLV is stored to
435 * @return uint16_t number of bytes added to packet stream
438 * Store the software version in the specified location.
439 * This consists of two bytes of OS version, and two bytes of EIGRP
442 static uint16_t eigrp_sw_version_encode(struct stream
*s
)
444 uint16_t length
= EIGRP_TLV_SW_VERSION_LEN
;
446 // setup the tlv fields
447 stream_putw(s
, EIGRP_TLV_SW_VERSION
);
448 stream_putw(s
, length
);
450 stream_putc(s
, FRR_MAJOR
); //!< major os version
451 stream_putc(s
, FRR_MINOR
); //!< minor os version
453 /* and the core eigrp version */
454 stream_putc(s
, EIGRP_MAJOR_VERSION
);
455 stream_putc(s
, EIGRP_MINOR_VERSION
);
461 * @fn eigrp_tidlist_encode
463 * @param[in,out] s packet stream TLV is stored to
468 * If doing mutli-topology, then store the supported TID list.
469 * This is currently a place holder function
471 static uint16_t eigrp_tidlist_encode(struct stream
*s
)
473 // uint16_t length = EIGRP_TLV_SW_VERSION_LEN;
478 * @fn eigrp_sequence_encode
480 * @param[in,out] s packet stream TLV is stored to
482 * @return uint16_t number of bytes added to packet stream
485 * Part of conditional receive process
488 static uint16_t eigrp_sequence_encode(struct eigrp
*eigrp
, struct stream
*s
)
490 uint16_t length
= EIGRP_TLV_SEQ_BASE_LEN
;
491 struct eigrp_interface
*ei
;
492 struct listnode
*node
, *node2
, *nnode2
;
493 struct eigrp_neighbor
*nbr
;
494 size_t backup_end
, size_end
;
497 // add in the parameters TLV
498 backup_end
= stream_get_endp(s
);
499 stream_putw(s
, EIGRP_TLV_SEQ
);
501 stream_putw(s
, 0x0000);
502 stream_putc(s
, IPV4_MAX_BYTELEN
);
505 for (ALL_LIST_ELEMENTS_RO(eigrp
->eiflist
, node
, ei
)) {
506 for (ALL_LIST_ELEMENTS(ei
->nbrs
, node2
, nnode2
, nbr
)) {
507 if (nbr
->multicast_queue
->count
> 0) {
508 length
+= (uint16_t)stream_put_ipv4(
516 stream_set_endp(s
, backup_end
);
520 backup_end
= stream_get_endp(s
);
521 stream_set_endp(s
, size_end
);
522 stream_putw(s
, length
);
523 stream_set_endp(s
, backup_end
);
529 * @fn eigrp_sequence_encode
531 * @param[in,out] s packet stream TLV is stored to
533 * @return uint16_t number of bytes added to packet stream
536 * Part of conditional receive process
539 static uint16_t eigrp_next_sequence_encode(struct eigrp
*eigrp
,
542 uint16_t length
= EIGRP_NEXT_SEQUENCE_TLV_SIZE
;
544 // add in the parameters TLV
545 stream_putw(s
, EIGRP_TLV_NEXT_MCAST_SEQ
);
546 stream_putw(s
, EIGRP_NEXT_SEQUENCE_TLV_SIZE
);
547 stream_putl(s
, eigrp
->sequence_number
+ 1);
553 * @fn eigrp_hello_parameter_encode
555 * @param[in] ei pointer to interface hello packet came in on
556 * @param[in,out] s packet stream TLV is stored to
558 * @return uint16_t number of bytes added to packet stream
561 * Encode Parameter TLV, used to convey metric weights and the hold time.
564 * Note the addition of K6 for the new extended metrics, and does not apply to
565 * older TLV packet formats.
567 static uint16_t eigrp_hello_parameter_encode(struct eigrp_interface
*ei
,
568 struct stream
*s
, uint8_t flags
)
570 // add in the parameters TLV
571 stream_putw(s
, EIGRP_TLV_PARAMETER
);
572 stream_putw(s
, EIGRP_TLV_PARAMETER_LEN
);
574 // if graceful shutdown is needed to be announced, send all 255 in K
576 if (flags
& EIGRP_HELLO_GRACEFUL_SHUTDOWN
) {
577 stream_putc(s
, 0xff); /* K1 */
578 stream_putc(s
, 0xff); /* K2 */
579 stream_putc(s
, 0xff); /* K3 */
580 stream_putc(s
, 0xff); /* K4 */
581 stream_putc(s
, 0xff); /* K5 */
582 stream_putc(s
, 0xff); /* K6 */
583 } else // set k values
585 stream_putc(s
, ei
->eigrp
->k_values
[0]); /* K1 */
586 stream_putc(s
, ei
->eigrp
->k_values
[1]); /* K2 */
587 stream_putc(s
, ei
->eigrp
->k_values
[2]); /* K3 */
588 stream_putc(s
, ei
->eigrp
->k_values
[3]); /* K4 */
589 stream_putc(s
, ei
->eigrp
->k_values
[4]); /* K5 */
590 stream_putc(s
, ei
->eigrp
->k_values
[5]); /* K6 */
593 // and set hold time value..
594 stream_putw(s
, ei
->params
.v_wait
);
596 return EIGRP_TLV_PARAMETER_LEN
;
600 * @fn eigrp_hello_encode
602 * @param[in] ei pointer to interface hello packet came in on
603 * @param[in] s packet stream TLV is stored to
604 * @param[in] ack if non-zero, neigbors sequence packet to ack
605 * @param[in] flags type of hello packet
606 * @param[in] nbr_addr pointer to neighbor address for Peer
609 * @return eigrp_packet pointer initialize hello packet
612 * Allocate an EIGRP hello packet, and add in the the approperate TLVs
615 static struct eigrp_packet
*eigrp_hello_encode(struct eigrp_interface
*ei
,
616 in_addr_t addr
, uint32_t ack
,
618 struct in_addr
*nbr_addr
)
620 struct eigrp_packet
*ep
;
621 uint16_t length
= EIGRP_HEADER_LEN
;
623 // allocate a new packet to be sent
624 ep
= eigrp_packet_new(EIGRP_PACKET_MTU(ei
->ifp
->mtu
), NULL
);
627 // encode common header feilds
628 eigrp_packet_header_init(EIGRP_OPC_HELLO
, ei
->eigrp
, ep
->s
, 0,
631 // encode Authentication TLV
632 if ((ei
->params
.auth_type
== EIGRP_AUTH_TYPE_MD5
)
633 && (ei
->params
.auth_keychain
!= NULL
)) {
634 length
+= eigrp_add_authTLV_MD5_to_stream(ep
->s
, ei
);
635 } else if ((ei
->params
.auth_type
== EIGRP_AUTH_TYPE_SHA256
)
636 && (ei
->params
.auth_keychain
!= NULL
)) {
637 length
+= eigrp_add_authTLV_SHA256_to_stream(ep
->s
, ei
);
640 /* encode appropriate parameters to Hello packet */
641 if (flags
& EIGRP_HELLO_GRACEFUL_SHUTDOWN
)
642 length
+= eigrp_hello_parameter_encode(
643 ei
, ep
->s
, EIGRP_HELLO_GRACEFUL_SHUTDOWN
);
645 length
+= eigrp_hello_parameter_encode(
646 ei
, ep
->s
, EIGRP_HELLO_NORMAL
);
648 // figure out the version of code we're running
649 length
+= eigrp_sw_version_encode(ep
->s
);
651 if (flags
& EIGRP_HELLO_ADD_SEQUENCE
) {
652 length
+= eigrp_sequence_encode(ei
->eigrp
, ep
->s
);
653 length
+= eigrp_next_sequence_encode(ei
->eigrp
, ep
->s
);
656 // add in the TID list if doing multi-topology
657 length
+= eigrp_tidlist_encode(ep
->s
);
659 /* encode Peer Termination TLV if needed */
660 if (flags
& EIGRP_HELLO_GRACEFUL_SHUTDOWN_NBR
)
662 eigrp_peer_termination_encode(ep
->s
, nbr_addr
);
667 // set soruce address for the hello packet
668 ep
->dst
.s_addr
= addr
;
670 if ((ei
->params
.auth_type
== EIGRP_AUTH_TYPE_MD5
)
671 && (ei
->params
.auth_keychain
!= NULL
)) {
672 eigrp_make_md5_digest(ei
, ep
->s
,
673 EIGRP_AUTH_BASIC_HELLO_FLAG
);
674 } else if ((ei
->params
.auth_type
== EIGRP_AUTH_TYPE_SHA256
)
675 && (ei
->params
.auth_keychain
!= NULL
)) {
676 eigrp_make_sha256_digest(ei
, ep
->s
,
677 EIGRP_AUTH_BASIC_HELLO_FLAG
);
681 eigrp_packet_checksum(ei
, ep
->s
, length
);
688 * @fn eigrp_hello_send
690 * @param[in] nbr neighbor the ACK should be sent to
695 * Send (unicast) a hello packet with the destination address
696 * associated with the neighbor. The eigrp header ACK feild will be
697 * updated to the neighbor's sequence number to acknolodge any
698 * outstanding packets
700 void eigrp_hello_send_ack(struct eigrp_neighbor
*nbr
)
702 struct eigrp_packet
*ep
;
704 /* if packet succesfully created, add it to the interface queue */
705 ep
= eigrp_hello_encode(nbr
->ei
, nbr
->src
.s_addr
,
706 nbr
->recv_sequence_number
, EIGRP_HELLO_NORMAL
,
710 if (IS_DEBUG_EIGRP_PACKET(0, SEND
))
711 zlog_debug("Queueing [Hello] Ack Seq [%u] nbr [%s]",
712 nbr
->recv_sequence_number
,
713 inet_ntoa(nbr
->src
));
715 /* Add packet to the top of the interface output queue*/
716 eigrp_fifo_push(nbr
->ei
->obuf
, ep
);
718 /* Hook thread to write packet. */
719 if (nbr
->ei
->on_write_q
== 0) {
720 listnode_add(nbr
->ei
->eigrp
->oi_write_q
, nbr
->ei
);
721 nbr
->ei
->on_write_q
= 1;
723 thread_add_write(master
, eigrp_write
, nbr
->ei
->eigrp
,
724 nbr
->ei
->eigrp
->fd
, &nbr
->ei
->eigrp
->t_write
);
729 * @fn eigrp_hello_send
731 * @param[in] ei pointer to interface hello should be sent
732 * @param[in] flags type of hello packet
733 * @param[in] nbr_addr pointer to neighbor address for Peer
739 * Build and enqueue a generic (multicast) periodic hello packet for
740 * sending. If no packets are currently queues, the packet will be
743 void eigrp_hello_send(struct eigrp_interface
*ei
, uint8_t flags
,
744 struct in_addr
*nbr_addr
)
746 struct eigrp_packet
*ep
= NULL
;
748 /* If this is passive interface, do not send EIGRP Hello.
749 if ((EIGRP_IF_PASSIVE_STATUS (ei) == EIGRP_IF_PASSIVE) ||
750 (ei->type != EIGRP_IFTYPE_NBMA))
754 if (IS_DEBUG_EIGRP_PACKET(0, SEND
))
755 zlog_debug("Queueing [Hello] Interface(%s)", IF_NAME(ei
));
757 /* if packet was succesfully created, then add it to the interface queue
759 ep
= eigrp_hello_encode(ei
, htonl(EIGRP_MULTICAST_ADDRESS
), 0, flags
,
763 // Add packet to the top of the interface output queue
764 eigrp_fifo_push(ei
->obuf
, ep
);
766 /* Hook thread to write packet. */
767 if (ei
->on_write_q
== 0) {
768 listnode_add(ei
->eigrp
->oi_write_q
, ei
);
772 if (ei
->eigrp
->t_write
== NULL
) {
773 if (flags
& EIGRP_HELLO_GRACEFUL_SHUTDOWN
) {
774 thread_execute(master
, eigrp_write
, ei
->eigrp
,
777 thread_add_write(master
, eigrp_write
, ei
->eigrp
,
779 &ei
->eigrp
->t_write
);