xref: /openbsd/usr.sbin/eigrpd/update.c (revision a7928e1e)
1 /*	$OpenBSD: update.c,v 1.6 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 /* update packet handling */
31 
32 void
send_update(struct eigrp_iface * ei,struct nbr * nbr,uint32_t flags,struct rinfo_head * rinfo_list)33 send_update(struct eigrp_iface *ei, struct nbr *nbr, uint32_t flags,
34     struct rinfo_head *rinfo_list)
35 {
36 	struct eigrp		*eigrp = ei->eigrp;
37 	struct ibuf		*buf;
38 	struct rinfo_entry	*re;
39 	int			 size;
40 	int			 route_len;
41 
42 	do {
43 		if ((buf = ibuf_dynamic(PKG_DEF_SIZE,
44 		    IP_MAXPACKET - sizeof(struct ip))) == NULL)
45 			fatal("send_update");
46 
47 		/* EIGRP header */
48 		if (gen_eigrp_hdr(buf, EIGRP_OPC_UPDATE, flags,
49 		    eigrp->seq_num, eigrp->as))
50 			goto fail;
51 
52 		if (rinfo_list == NULL)
53 			break;
54 
55 		switch (eigrp->af) {
56 		case AF_INET:
57 			size = sizeof(struct ip);
58 			break;
59 		case AF_INET6:
60 			size = sizeof(struct ip6_hdr);
61 			break;
62 		default:
63 			fatalx("send_update: unknown af");
64 		}
65 		size += sizeof(struct eigrp_hdr);
66 
67 		while ((re = TAILQ_FIRST(rinfo_list)) != NULL) {
68 			route_len = len_route_tlv(&re->rinfo);
69 			/* don't exceed the MTU to avoid IP fragmentation */
70 			if (size + route_len > ei->iface->mtu) {
71 				rtp_send(ei, nbr, buf);
72 				break;
73 			}
74 			size += route_len;
75 
76 			if (gen_route_tlv(buf, &re->rinfo))
77 				goto fail;
78 			TAILQ_REMOVE(rinfo_list, re, entry);
79 			free(re);
80 		}
81 	} while (!TAILQ_EMPTY(rinfo_list));
82 
83 	rtp_send(ei, nbr, buf);
84 	return;
85 fail:
86 	log_warnx("%s: failed to send message", __func__);
87 	if (rinfo_list)
88 		message_list_clr(rinfo_list);
89 	ibuf_free(buf);
90 	return;
91 }
92 
93 void
recv_update(struct nbr * nbr,struct rinfo_head * rinfo_list,uint32_t flags)94 recv_update(struct nbr *nbr, struct rinfo_head *rinfo_list, uint32_t flags)
95 {
96 	struct rinfo_entry	*re;
97 
98 	rtp_ack_start_timer(nbr);
99 
100 	if (flags & EIGRP_HDR_FLAG_INIT) {
101 		log_debug("%s: INIT flag is set", __func__);
102 
103 		if (nbr->flags & F_EIGRP_NBR_PENDING)
104 			nbr_init(nbr);
105 
106 		/*
107 		 * The INIT flag instructs us to advertise all of our routes,
108 		 * even if the neighbor is not pending.
109 		 */
110 		eigrpe_imsg_compose_rde(IMSG_RECV_UPDATE_INIT, nbr->peerid,
111 		    0, NULL, 0);
112 		return;
113 	}
114 
115 	TAILQ_FOREACH(re, rinfo_list, entry)
116 		eigrpe_imsg_compose_rde(IMSG_RECV_UPDATE, nbr->peerid,
117 		    0, &re->rinfo, sizeof(re->rinfo));
118 }
119