xref: /openbsd/usr.sbin/eigrpd/reply.c (revision 73471bf0)
1 /*	$OpenBSD: reply.c,v 1.5 2016/09/02 16:46:29 renato Exp $ */
2 
3 /*
4  * Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22 #include <netinet/ip6.h>
23 
24 #include <stdlib.h>
25 
26 #include "eigrpd.h"
27 #include "eigrpe.h"
28 #include "log.h"
29 
30 /* reply packet handling */
31 
32 void
33 send_reply(struct nbr *nbr, struct rinfo_head *rinfo_list, int siareply)
34 {
35 	struct eigrp		*eigrp = nbr->ei->eigrp;
36 	struct ibuf		*buf;
37 	uint16_t		 opcode;
38 	struct rinfo_entry	*re;
39 	int			 size;
40 	int			 route_len;
41 
42 	if (rinfo_list == NULL || TAILQ_EMPTY(rinfo_list))
43 		return;
44 
45 	do {
46 		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
47 		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
48 			fatal("send_reply");
49 
50 		if (!siareply)
51 			 opcode = EIGRP_OPC_REPLY;
52 		else
53 			 opcode = EIGRP_OPC_SIAREPLY;
54 
55 		/* EIGRP header */
56 		if (gen_eigrp_hdr(buf, opcode, 0, eigrp->seq_num, eigrp->as))
57 			goto fail;
58 
59 		switch (eigrp->af) {
60 		case AF_INET:
61 			size = sizeof(struct ip);
62 			break;
63 		case AF_INET6:
64 			size = sizeof(struct ip6_hdr);
65 			break;
66 		default:
67 			fatalx("send_reply: unknown af");
68 		}
69 		size += sizeof(struct eigrp_hdr);
70 
71 		while ((re = TAILQ_FIRST(rinfo_list)) != NULL) {
72 			route_len = len_route_tlv(&re->rinfo);
73 			/* don't exceed the MTU to avoid IP fragmentation */
74 			if (size + route_len > nbr->ei->iface->mtu) {
75 				rtp_send_ucast(nbr, buf);
76 				break;
77 			}
78 			size += route_len;
79 
80 			if (gen_route_tlv(buf, &re->rinfo))
81 				goto fail;
82 			TAILQ_REMOVE(rinfo_list, re, entry);
83 			free(re);
84 		}
85 	} while (!TAILQ_EMPTY(rinfo_list));
86 
87 	/* reply packets are always unicast */
88 	rtp_send_ucast(nbr, buf);
89 	return;
90 fail:
91 	log_warnx("%s: failed to send message", __func__);
92 	if (rinfo_list)
93 		message_list_clr(rinfo_list);
94 	ibuf_free(buf);
95 	return;
96 }
97 
98 void
99 recv_reply(struct nbr *nbr, struct rinfo_head *rinfo_list, int siareply)
100 {
101 	int			 type;
102 	struct rinfo_entry	*re;
103 
104 	/*
105 	 * draft-savage-eigrp-02 - Section 4.3:
106 	 * "When a REPLY packet is received, there is no reason to process
107 	 * the packet before an acknowledgment is sent. Therefore, an Ack
108 	 * packet is sent immediately and then the packet is processed."
109 	 */
110 	rtp_send_ack(nbr);
111 
112 	if (!siareply)
113 		type = IMSG_RECV_REPLY;
114 	else
115 		type = IMSG_RECV_SIAREPLY;
116 
117 	TAILQ_FOREACH(re, rinfo_list, entry)
118 		eigrpe_imsg_compose_rde(type, nbr->peerid, 0, &re->rinfo,
119 		    sizeof(re->rinfo));
120 }
121