xref: /openbsd-src/usr.sbin/eigrpd/query.c (revision a7928e1e02ccdd875c879410f15f509d364306a4)
1*a7928e1eSrenato /*	$OpenBSD: query.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 /* query packet handling */
3143509a12Srenato 
3243509a12Srenato void
send_query(struct eigrp_iface * ei,struct nbr * nbr,struct rinfo_head * rinfo_list,int siaquery)3343509a12Srenato send_query(struct eigrp_iface *ei, struct nbr *nbr,
3443509a12Srenato     struct rinfo_head *rinfo_list, int siaquery)
3543509a12Srenato {
3643509a12Srenato 	struct eigrp		*eigrp = ei->eigrp;
3743509a12Srenato 	struct ibuf		*buf;
3843509a12Srenato 	uint16_t		 opcode;
3943509a12Srenato 	struct rinfo_entry	*re;
4043509a12Srenato 	int			 size;
4143509a12Srenato 	int			 route_len;
4243509a12Srenato 
4343509a12Srenato 	if (rinfo_list == NULL || TAILQ_EMPTY(rinfo_list))
4443509a12Srenato 		return;
4543509a12Srenato 
4643509a12Srenato 	do {
4743509a12Srenato 		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
4843509a12Srenato 		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
4943509a12Srenato 			fatal("send_query");
5043509a12Srenato 
5143509a12Srenato 		if (!siaquery)
5243509a12Srenato 			 opcode = EIGRP_OPC_QUERY;
5343509a12Srenato 		else
5443509a12Srenato 			 opcode = EIGRP_OPC_SIAQUERY;
5543509a12Srenato 
5643509a12Srenato 		/* EIGRP header */
5743509a12Srenato 		if (gen_eigrp_hdr(buf, opcode, 0, eigrp->seq_num, eigrp->as))
5843509a12Srenato 			goto fail;
5943509a12Srenato 
6043509a12Srenato 		switch (eigrp->af) {
6143509a12Srenato 		case AF_INET:
6243509a12Srenato 			size = sizeof(struct ip);
6343509a12Srenato 			break;
6443509a12Srenato 		case AF_INET6:
6543509a12Srenato 			size = sizeof(struct ip6_hdr);
6643509a12Srenato 			break;
6743509a12Srenato 		default:
683c8071b0Srenato 			fatalx("send_query: unknown af");
6943509a12Srenato 		}
7043509a12Srenato 		size += sizeof(struct eigrp_hdr);
7143509a12Srenato 
7243509a12Srenato 		while ((re = TAILQ_FIRST(rinfo_list)) != NULL) {
7343509a12Srenato 			route_len = len_route_tlv(&re->rinfo);
74*a7928e1eSrenato 			/* don't exceed the MTU to avoid IP fragmentation */
7543509a12Srenato 			if (size + route_len > ei->iface->mtu) {
7643509a12Srenato 				rtp_send(ei, nbr, buf);
7743509a12Srenato 				break;
7843509a12Srenato 			}
7943509a12Srenato 			size += route_len;
8043509a12Srenato 
8143509a12Srenato 			if (gen_route_tlv(buf, &re->rinfo))
8243509a12Srenato 				goto fail;
8343509a12Srenato 			TAILQ_REMOVE(rinfo_list, re, entry);
8443509a12Srenato 			free(re);
8543509a12Srenato 		}
8643509a12Srenato 	} while (!TAILQ_EMPTY(rinfo_list));
8743509a12Srenato 
8843509a12Srenato 	rtp_send(ei, 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_query(struct nbr * nbr,struct rinfo_head * rinfo_list,int siaquery)9943509a12Srenato recv_query(struct nbr *nbr, struct rinfo_head *rinfo_list, int siaquery)
10043509a12Srenato {
10143509a12Srenato 	int			 type;
10243509a12Srenato 	struct rinfo_entry	*re;
10343509a12Srenato 
10443509a12Srenato 	rtp_ack_start_timer(nbr);
10543509a12Srenato 
10643509a12Srenato 	if (!siaquery)
10743509a12Srenato 		type = IMSG_RECV_QUERY;
10843509a12Srenato 	else
10943509a12Srenato 		type = IMSG_RECV_SIAQUERY;
11043509a12Srenato 
11143509a12Srenato 	TAILQ_FOREACH(re, rinfo_list, entry)
11243509a12Srenato 		eigrpe_imsg_compose_rde(type, nbr->peerid, 0, &re->rinfo,
11343509a12Srenato 		    sizeof(re->rinfo));
11443509a12Srenato }
115