xref: /openbsd/usr.sbin/eigrpd/reply.c (revision 43509a12)
1 /*	$OpenBSD: reply.c,v 1.1 2015/10/02 04:26:47 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 <stdlib.h>
20 #include <netinet/in.h>
21 #include <netinet/ip6.h>
22 
23 #include "eigrpd.h"
24 #include "eigrp.h"
25 #include "log.h"
26 #include "eigrpe.h"
27 
28 extern struct eigrpd_conf	*econf;
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 	/* don't exceed the interface's mtu */
46 	do {
47 		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
48 		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
49 			fatal("send_reply");
50 
51 		if (!siareply)
52 			 opcode = EIGRP_OPC_REPLY;
53 		else
54 			 opcode = EIGRP_OPC_SIAREPLY;
55 
56 		/* EIGRP header */
57 		if (gen_eigrp_hdr(buf, opcode, 0, eigrp->seq_num, eigrp->as))
58 			goto fail;
59 
60 		switch (eigrp->af) {
61 		case AF_INET:
62 			size = sizeof(struct ip);
63 			break;
64 		case AF_INET6:
65 			size = sizeof(struct ip6_hdr);
66 			break;
67 		default:
68 			break;
69 		}
70 		size += sizeof(struct eigrp_hdr);
71 
72 		while ((re = TAILQ_FIRST(rinfo_list)) != NULL) {
73 			route_len = len_route_tlv(&re->rinfo);
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