xref: /openbsd-src/usr.sbin/eigrpd/reply.c (revision a7928e1e02ccdd875c879410f15f509d364306a4)
1*a7928e1eSrenato /*	$OpenBSD: reply.c,v 1.5 2016/09/02 16:46:29 renato Exp $ */
243509a12Srenato 
343509a12Srenato /*
443509a12Srenato  * Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
543509a12Srenato  *
643509a12Srenato  * Permission to use, copy, modify, and distribute this software for any
743509a12Srenato  * purpose with or without fee is hereby granted, provided that the above
843509a12Srenato  * copyright notice and this permission notice appear in all copies.
943509a12Srenato  *
1043509a12Srenato  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1143509a12Srenato  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1243509a12Srenato  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1343509a12Srenato  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1443509a12Srenato  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1543509a12Srenato  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1643509a12Srenato  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1743509a12Srenato  */
1843509a12Srenato 
198072de9bSrenato #include <sys/types.h>
2043509a12Srenato #include <netinet/in.h>
218072de9bSrenato #include <netinet/ip.h>
2243509a12Srenato #include <netinet/ip6.h>
2343509a12Srenato 
248072de9bSrenato #include <stdlib.h>
258072de9bSrenato 
2643509a12Srenato #include "eigrpd.h"
2743509a12Srenato #include "eigrpe.h"
288072de9bSrenato #include "log.h"
2943509a12Srenato 
3043509a12Srenato /* reply packet handling */
3143509a12Srenato 
3243509a12Srenato void
send_reply(struct nbr * nbr,struct rinfo_head * rinfo_list,int siareply)3343509a12Srenato send_reply(struct nbr *nbr, struct rinfo_head *rinfo_list, int siareply)
3443509a12Srenato {
3543509a12Srenato 	struct eigrp		*eigrp = nbr->ei->eigrp;
3643509a12Srenato 	struct ibuf		*buf;
3743509a12Srenato 	uint16_t		 opcode;
3843509a12Srenato 	struct rinfo_entry	*re;
3943509a12Srenato 	int			 size;
4043509a12Srenato 	int			 route_len;
4143509a12Srenato 
4243509a12Srenato 	if (rinfo_list == NULL || TAILQ_EMPTY(rinfo_list))
4343509a12Srenato 		return;
4443509a12Srenato 
4543509a12Srenato 	do {
4643509a12Srenato 		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
4743509a12Srenato 		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
4843509a12Srenato 			fatal("send_reply");
4943509a12Srenato 
5043509a12Srenato 		if (!siareply)
5143509a12Srenato 			 opcode = EIGRP_OPC_REPLY;
5243509a12Srenato 		else
5343509a12Srenato 			 opcode = EIGRP_OPC_SIAREPLY;
5443509a12Srenato 
5543509a12Srenato 		/* EIGRP header */
5643509a12Srenato 		if (gen_eigrp_hdr(buf, opcode, 0, eigrp->seq_num, eigrp->as))
5743509a12Srenato 			goto fail;
5843509a12Srenato 
5943509a12Srenato 		switch (eigrp->af) {
6043509a12Srenato 		case AF_INET:
6143509a12Srenato 			size = sizeof(struct ip);
6243509a12Srenato 			break;
6343509a12Srenato 		case AF_INET6:
6443509a12Srenato 			size = sizeof(struct ip6_hdr);
6543509a12Srenato 			break;
6643509a12Srenato 		default:
673c8071b0Srenato 			fatalx("send_reply: unknown af");
6843509a12Srenato 		}
6943509a12Srenato 		size += sizeof(struct eigrp_hdr);
7043509a12Srenato 
7143509a12Srenato 		while ((re = TAILQ_FIRST(rinfo_list)) != NULL) {
7243509a12Srenato 			route_len = len_route_tlv(&re->rinfo);
73*a7928e1eSrenato 			/* don't exceed the MTU to avoid IP fragmentation */
7443509a12Srenato 			if (size + route_len > nbr->ei->iface->mtu) {
7543509a12Srenato 				rtp_send_ucast(nbr, buf);
7643509a12Srenato 				break;
7743509a12Srenato 			}
7843509a12Srenato 			size += route_len;
7943509a12Srenato 
8043509a12Srenato 			if (gen_route_tlv(buf, &re->rinfo))
8143509a12Srenato 				goto fail;
8243509a12Srenato 			TAILQ_REMOVE(rinfo_list, re, entry);
8343509a12Srenato 			free(re);
8443509a12Srenato 		}
8543509a12Srenato 	} while (!TAILQ_EMPTY(rinfo_list));
8643509a12Srenato 
8743509a12Srenato 	/* reply packets are always unicast */
8843509a12Srenato 	rtp_send_ucast(nbr, buf);
8943509a12Srenato 	return;
9043509a12Srenato fail:
9143509a12Srenato 	log_warnx("%s: failed to send message", __func__);
9243509a12Srenato 	if (rinfo_list)
9343509a12Srenato 		message_list_clr(rinfo_list);
9443509a12Srenato 	ibuf_free(buf);
9543509a12Srenato 	return;
9643509a12Srenato }
9743509a12Srenato 
9843509a12Srenato void
recv_reply(struct nbr * nbr,struct rinfo_head * rinfo_list,int siareply)9943509a12Srenato recv_reply(struct nbr *nbr, struct rinfo_head *rinfo_list, int siareply)
10043509a12Srenato {
10143509a12Srenato 	int			 type;
10243509a12Srenato 	struct rinfo_entry	*re;
10343509a12Srenato 
10443509a12Srenato 	/*
10543509a12Srenato 	 * draft-savage-eigrp-02 - Section 4.3:
10643509a12Srenato 	 * "When a REPLY packet is received, there is no reason to process
10743509a12Srenato 	 * the packet before an acknowledgment is sent. Therefore, an Ack
10843509a12Srenato 	 * packet is sent immediately and then the packet is processed."
10943509a12Srenato 	 */
11043509a12Srenato 	rtp_send_ack(nbr);
11143509a12Srenato 
11243509a12Srenato 	if (!siareply)
11343509a12Srenato 		type = IMSG_RECV_REPLY;
11443509a12Srenato 	else
11543509a12Srenato 		type = IMSG_RECV_SIAREPLY;
11643509a12Srenato 
11743509a12Srenato 	TAILQ_FOREACH(re, rinfo_list, entry)
11843509a12Srenato 		eigrpe_imsg_compose_rde(type, nbr->peerid, 0, &re->rinfo,
11943509a12Srenato 		    sizeof(re->rinfo));
12043509a12Srenato }
121