1/*	$OpenBSD: query.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/* query packet handling */
31
32void
33send_query(struct eigrp_iface *ei, struct nbr *nbr,
34    struct rinfo_head *rinfo_list, int siaquery)
35{
36	struct eigrp		*eigrp = ei->eigrp;
37	struct ibuf		*buf;
38	uint16_t		 opcode;
39	struct rinfo_entry	*re;
40	int			 size;
41	int			 route_len;
42
43	if (rinfo_list == NULL || TAILQ_EMPTY(rinfo_list))
44		return;
45
46	do {
47		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
48		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
49			fatal("send_query");
50
51		if (!siaquery)
52			 opcode = EIGRP_OPC_QUERY;
53		else
54			 opcode = EIGRP_OPC_SIAQUERY;
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			fatalx("send_query: unknown af");
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			/* don't exceed the MTU to avoid IP fragmentation */
75			if (size + route_len > ei->iface->mtu) {
76				rtp_send(ei, nbr, buf);
77				break;
78			}
79			size += route_len;
80
81			if (gen_route_tlv(buf, &re->rinfo))
82				goto fail;
83			TAILQ_REMOVE(rinfo_list, re, entry);
84			free(re);
85		}
86	} while (!TAILQ_EMPTY(rinfo_list));
87
88	rtp_send(ei, nbr, buf);
89	return;
90fail:
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
98void
99recv_query(struct nbr *nbr, struct rinfo_head *rinfo_list, int siaquery)
100{
101	int			 type;
102	struct rinfo_entry	*re;
103
104	rtp_ack_start_timer(nbr);
105
106	if (!siaquery)
107		type = IMSG_RECV_QUERY;
108	else
109		type = IMSG_RECV_SIAQUERY;
110
111	TAILQ_FOREACH(re, rinfo_list, entry)
112		eigrpe_imsg_compose_rde(type, nbr->peerid, 0, &re->rinfo,
113		    sizeof(re->rinfo));
114}
115