xref: /openbsd-src/usr.sbin/ospfd/lsreq.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: lsreq.c,v 1.16 2009/01/31 11:44:49 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005 Esben Norby <norby@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 <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <stdlib.h>
24 
25 #include "ospfd.h"
26 #include "ospf.h"
27 #include "log.h"
28 #include "ospfe.h"
29 
30 extern struct imsgbuf		*ibuf_rde;
31 
32 /* link state request packet handling */
33 int
34 send_ls_req(struct nbr *nbr)
35 {
36 	struct sockaddr_in	 dst;
37 	struct ls_req_hdr	 ls_req_hdr;
38 	struct lsa_entry	*le, *nle;
39 	struct buf		*buf;
40 	int			 ret;
41 
42 	if ((buf = buf_open(nbr->iface->mtu - sizeof(struct ip))) == NULL)
43 		fatal("send_ls_req");
44 
45 	/* set destination */
46 	dst.sin_family = AF_INET;
47 	dst.sin_len = sizeof(struct sockaddr_in);
48 
49 	switch (nbr->iface->type) {
50 	case IF_TYPE_POINTOPOINT:
51 		inet_aton(AllSPFRouters, &dst.sin_addr);
52 		break;
53 	case IF_TYPE_BROADCAST:
54 	case IF_TYPE_NBMA:
55 	case IF_TYPE_POINTOMULTIPOINT:
56 	case IF_TYPE_VIRTUALLINK:
57 		dst.sin_addr.s_addr = nbr->addr.s_addr;
58 		break;
59 	default:
60 		fatalx("send_ls_req: unknown interface type");
61 	}
62 
63 	/* OSPF header */
64 	if (gen_ospf_hdr(buf, nbr->iface, PACKET_TYPE_LS_REQUEST))
65 		goto fail;
66 
67 	/* LSA header(s), keep space for a possible md5 sum */
68 	for (le = TAILQ_FIRST(&nbr->ls_req_list); le != NULL &&
69 	    buf_left(buf) >= sizeof(struct ls_req_hdr) + MD5_DIGEST_LENGTH;
70 	    le = nle) {
71 		nbr->ls_req = nle = TAILQ_NEXT(le, entry);
72 		ls_req_hdr.type = htonl(le->le_lsa->type);
73 		ls_req_hdr.ls_id = le->le_lsa->ls_id;
74 		ls_req_hdr.adv_rtr = le->le_lsa->adv_rtr;
75 		if (buf_add(buf, &ls_req_hdr, sizeof(ls_req_hdr)))
76 			goto fail;
77 	}
78 
79 	/* update authentication and calculate checksum */
80 	if (auth_gen(buf, nbr->iface))
81 		goto fail;
82 
83 	ret = send_packet(nbr->iface, buf, &dst);
84 
85 	buf_free(buf);
86 	return (ret);
87 fail:
88 	log_warn("send_ls_req");
89 	buf_free(buf);
90 	return (-1);
91 }
92 
93 void
94 recv_ls_req(struct nbr *nbr, char *buf, u_int16_t len)
95 {
96 	switch (nbr->state) {
97 	case NBR_STA_DOWN:
98 	case NBR_STA_ATTEMPT:
99 	case NBR_STA_INIT:
100 	case NBR_STA_2_WAY:
101 	case NBR_STA_XSTRT:
102 	case NBR_STA_SNAP:
103 		log_debug("recv_ls_req: packet ignored in state %s, "
104 		    "neighbor ID %s", nbr_state_name(nbr->state),
105 		    inet_ntoa(nbr->id));
106 		break;
107 	case NBR_STA_XCHNG:
108 	case NBR_STA_LOAD:
109 	case NBR_STA_FULL:
110 		imsg_compose(ibuf_rde, IMSG_LS_REQ, nbr->peerid, 0, buf, len);
111 		break;
112 	default:
113 		fatalx("recv_ls_req: unknown neighbor state");
114 	}
115 }
116 
117 /* link state request list */
118 void
119 ls_req_list_add(struct nbr *nbr, struct lsa_hdr *lsa)
120 {
121 	struct lsa_entry	*le;
122 
123 	if (lsa == NULL)
124 		fatalx("ls_req_list_add: no LSA header");
125 
126 	if ((le = calloc(1, sizeof(*le))) == NULL)
127 		fatal("ls_req_list_add");
128 
129 	TAILQ_INSERT_TAIL(&nbr->ls_req_list, le, entry);
130 	le->le_lsa = lsa;
131 	nbr->ls_req_cnt++;
132 }
133 
134 struct lsa_entry *
135 ls_req_list_get(struct nbr *nbr, struct lsa_hdr *lsa_hdr)
136 {
137 	struct lsa_entry	*le;
138 
139 	TAILQ_FOREACH(le, &nbr->ls_req_list, entry) {
140 		if ((lsa_hdr->type == le->le_lsa->type) &&
141 		    (lsa_hdr->ls_id == le->le_lsa->ls_id) &&
142 		    (lsa_hdr->adv_rtr == le->le_lsa->adv_rtr))
143 			return (le);
144 	}
145 	return (NULL);
146 }
147 
148 void
149 ls_req_list_free(struct nbr *nbr, struct lsa_entry *le)
150 {
151 	if (nbr->ls_req == le) {
152 		nbr->ls_req = TAILQ_NEXT(le, entry);
153 	}
154 
155 	TAILQ_REMOVE(&nbr->ls_req_list, le, entry);
156 	free(le->le_lsa);
157 	free(le);
158 	nbr->ls_req_cnt--;
159 
160 	/* received all requested LSA(s), send a new LS req */
161 	if (nbr->ls_req != NULL &&
162 	    nbr->ls_req == TAILQ_FIRST(&nbr->ls_req_list)) {
163 		start_ls_req_tx_timer(nbr);
164 	}
165 
166 	if (ls_req_list_empty(nbr) && nbr->dd_pending == 0)
167 		nbr_fsm(nbr, NBR_EVT_LOAD_DONE);
168 }
169 
170 void
171 ls_req_list_clr(struct nbr *nbr)
172 {
173 	struct lsa_entry	*le;
174 
175 	while ((le = TAILQ_FIRST(&nbr->ls_req_list)) != NULL) {
176 		TAILQ_REMOVE(&nbr->ls_req_list, le, entry);
177 		free(le->le_lsa);
178 		free(le);
179 	}
180 
181 	nbr->ls_req_cnt = 0;
182 	nbr->ls_req = NULL;
183 }
184 
185 int
186 ls_req_list_empty(struct nbr *nbr)
187 {
188 	return (TAILQ_EMPTY(&nbr->ls_req_list));
189 }
190 
191 /* timers */
192 /* ARGSUSED */
193 void
194 ls_req_tx_timer(int fd, short event, void *arg)
195 {
196 	struct nbr	*nbr = arg;
197 	struct timeval	 tv;
198 
199 	switch (nbr->state) {
200 	case NBR_STA_DOWN:
201 	case NBR_STA_ATTEMPT:
202 	case NBR_STA_INIT:
203 	case NBR_STA_2_WAY:
204 	case NBR_STA_SNAP:
205 	case NBR_STA_XSTRT:
206 	case NBR_STA_XCHNG:
207 		return;
208 	case NBR_STA_LOAD:
209 		send_ls_req(nbr);
210 		break;
211 	case NBR_STA_FULL:
212 		return;
213 	default:
214 		log_debug("ls_req_tx_timer: unknown neighbor state, "
215 		    "neighbor ID %s", inet_ntoa(nbr->id));
216 		break;
217 	}
218 
219 	/* reschedule lsreq_tx_timer */
220 	if (nbr->state == NBR_STA_LOAD) {
221 		timerclear(&tv);
222 		tv.tv_sec = nbr->iface->rxmt_interval;
223 		if (evtimer_add(&nbr->lsreq_tx_timer, &tv) == -1)
224 			fatal("ls_req_tx_timer");
225 	}
226 }
227 
228 void
229 start_ls_req_tx_timer(struct nbr *nbr)
230 {
231 	struct timeval tv;
232 
233 	if (nbr == nbr->iface->self)
234 		return;
235 
236 	timerclear(&tv);
237 	if (evtimer_add(&nbr->lsreq_tx_timer, &tv) == -1)
238 		fatal("start_ls_req_tx_timer");
239 }
240 
241 void
242 stop_ls_req_tx_timer(struct nbr *nbr)
243 {
244 	if (nbr == nbr->iface->self)
245 		return;
246 
247 	if (evtimer_del(&nbr->lsreq_tx_timer) == -1)
248 		fatal("stop_ls_req_tx_timer");
249 }
250