xref: /openbsd/usr.sbin/ospfd/lsack.c (revision 09467b48)
1 /*	$OpenBSD: lsack.c,v 1.22 2019/07/15 18:26:39 remi 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 <netinet/ip.h>
23 #include <arpa/inet.h>
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "ospfd.h"
29 #include "ospf.h"
30 #include "log.h"
31 #include "ospfe.h"
32 
33 int		 send_ls_ack(struct iface *, struct in_addr, struct ibuf *);
34 struct ibuf	*prepare_ls_ack(struct iface *);
35 void		 start_ls_ack_tx_timer_now(struct iface *);
36 
37 /* link state acknowledgement packet handling */
38 struct ibuf *
39 prepare_ls_ack(struct iface *iface)
40 {
41 	struct ibuf	*buf;
42 
43 	if ((buf = ibuf_open(iface->mtu - sizeof(struct ip))) == NULL) {
44 		log_warn("prepare_ls_ack");
45 		return (NULL);
46 	}
47 
48 	/* OSPF header */
49 	if (gen_ospf_hdr(buf, iface, PACKET_TYPE_LS_ACK)) {
50 		log_warn("prepare_ls_ack");
51 		ibuf_free(buf);
52 		return (NULL);
53 	}
54 
55 	return (buf);
56 }
57 
58 int
59 send_ls_ack(struct iface *iface, struct in_addr addr, struct ibuf *buf)
60 {
61 	struct sockaddr_in	dst;
62 
63 	/* update authentication and calculate checksum */
64 	if (auth_gen(buf, iface)) {
65 		log_warn("send_ls_ack");
66 		return (-1);
67 	}
68 
69 	dst.sin_family = AF_INET;
70 	dst.sin_len = sizeof(struct sockaddr_in);
71 	dst.sin_addr.s_addr = addr.s_addr;
72 
73 	if (send_packet(iface, buf, &dst) == -1) {
74 		log_warn("%s", __func__);
75 		return (-1);
76 	}
77 	return (0);
78 }
79 
80 int
81 send_direct_ack(struct iface *iface, struct in_addr addr, void *d, size_t len)
82 {
83 	struct ibuf	*buf;
84 	int		 ret;
85 
86 	if ((buf = prepare_ls_ack(iface)) == NULL)
87 		return (-1);
88 
89 	/* LS ack(s) */
90 	if (ibuf_add(buf, d, len)) {
91 		log_warn("send_direct_ack");
92 		ibuf_free(buf);
93 		return (-1);
94 	}
95 
96 	ret = send_ls_ack(iface, addr, buf);
97 	ibuf_free(buf);
98 	return (ret);
99 }
100 
101 void
102 recv_ls_ack(struct nbr *nbr, char *buf, u_int16_t len)
103 {
104 	struct lsa_hdr	 lsa_hdr;
105 
106 	switch (nbr->state) {
107 	case NBR_STA_DOWN:
108 	case NBR_STA_ATTEMPT:
109 	case NBR_STA_INIT:
110 	case NBR_STA_2_WAY:
111 	case NBR_STA_XSTRT:
112 	case NBR_STA_SNAP:
113 		log_debug("recv_ls_ack: packet ignored in state %s, "
114 		    "neighbor ID %s", nbr_state_name(nbr->state),
115 		    inet_ntoa(nbr->id));
116 		break;
117 	case NBR_STA_XCHNG:
118 	case NBR_STA_LOAD:
119 	case NBR_STA_FULL:
120 		while (len >= sizeof(lsa_hdr)) {
121 			memcpy(&lsa_hdr, buf, sizeof(lsa_hdr));
122 
123 			if (lsa_hdr_check(nbr, &lsa_hdr)) {
124 				/* try both list in case of DROTHER */
125 				if (nbr->iface->state & IF_STA_DROTHER)
126 					(void)ls_retrans_list_del(
127 					    nbr->iface->self, &lsa_hdr);
128 				(void)ls_retrans_list_del(nbr, &lsa_hdr);
129 			}
130 
131 			buf += sizeof(lsa_hdr);
132 			len -= sizeof(lsa_hdr);
133 		}
134 		if (len > 0) {
135 			log_warnx("recv_ls_ack: bad packet size, "
136 			    "neighbor ID %s", inet_ntoa(nbr->id));
137 			return;
138 		}
139 		break;
140 	default:
141 		fatalx("recv_ls_ack: unknown neighbor state");
142 	}
143 }
144 
145 int
146 lsa_hdr_check(struct nbr *nbr, struct lsa_hdr *lsa_hdr)
147 {
148 	/* invalid age */
149 	if ((ntohs(lsa_hdr->age) < 1) || (ntohs(lsa_hdr->age) > MAX_AGE)) {
150 		log_debug("lsa_hdr_check: invalid age, neighbor ID %s",
151 		     inet_ntoa(nbr->id));
152 		return (0);
153 	}
154 
155 	/* invalid type */
156 	switch (lsa_hdr->type) {
157 	case LSA_TYPE_ROUTER:
158 	case LSA_TYPE_NETWORK:
159 	case LSA_TYPE_SUM_NETWORK:
160 	case LSA_TYPE_SUM_ROUTER:
161 	case LSA_TYPE_EXTERNAL:
162 		break;
163 	default:
164 		log_debug("lsa_hdr_check: invalid LSA type %d, neighbor ID %s",
165 		    lsa_hdr->type, inet_ntoa(nbr->id));
166 		return (0);
167 	}
168 
169 	/* invalid sequence number */
170 	if (ntohl(lsa_hdr->seq_num) == RESV_SEQ_NUM) {
171 		log_debug("ls_hdr_check: invalid seq num, neighbor ID %s",
172 			inet_ntoa(nbr->id));
173 		return (0);
174 	}
175 
176 	return (1);
177 }
178 
179 /* link state ack list */
180 void
181 ls_ack_list_add(struct iface *iface, struct lsa_hdr *lsa)
182 {
183 	struct lsa_entry	*le;
184 
185 	if (lsa == NULL)
186 		fatalx("ls_ack_list_add: no LSA header");
187 
188 	if ((le = calloc(1, sizeof(*le))) == NULL)
189 		fatal("ls_ack_list_add");
190 
191 	if (ls_ack_list_empty(iface))
192 		start_ls_ack_tx_timer(iface);
193 
194 	TAILQ_INSERT_TAIL(&iface->ls_ack_list, le, entry);
195 	le->le_lsa = lsa;
196 	iface->ls_ack_cnt++;
197 
198 	/* reschedule now if we have enough for a reasonably sized packet */
199 	if (iface->ls_ack_cnt > IP_MSS / sizeof(struct lsa_hdr))
200 		start_ls_ack_tx_timer_now(iface);
201 }
202 
203 void
204 ls_ack_list_free(struct iface *iface, struct lsa_entry *le)
205 {
206 	TAILQ_REMOVE(&iface->ls_ack_list, le, entry);
207 	free(le->le_lsa);
208 	free(le);
209 
210 	iface->ls_ack_cnt--;
211 }
212 
213 void
214 ls_ack_list_clr(struct iface *iface)
215 {
216 	struct lsa_entry	*le;
217 
218 	while ((le = TAILQ_FIRST(&iface->ls_ack_list)) != NULL) {
219 		TAILQ_REMOVE(&iface->ls_ack_list, le, entry);
220 		free(le->le_lsa);
221 		free(le);
222 	}
223 	iface->ls_ack_cnt = 0;
224 }
225 
226 int
227 ls_ack_list_empty(struct iface *iface)
228 {
229 	return (TAILQ_EMPTY(&iface->ls_ack_list));
230 }
231 
232 /* timers */
233 /* ARGSUSED */
234 void
235 ls_ack_tx_timer(int fd, short event, void *arg)
236 {
237 	struct in_addr		 addr;
238 	struct iface		*iface = arg;
239 	struct lsa_entry	*le, *nle;
240 	struct nbr		*nbr;
241 	struct ibuf		*buf;
242 	int			 cnt;
243 
244 	while (!ls_ack_list_empty(iface)) {
245 		if ((buf = prepare_ls_ack(iface)) == NULL)
246 			fatal("ls_ack_tx_timer");
247 		cnt = 0;
248 
249 		for (le = TAILQ_FIRST(&iface->ls_ack_list); le != NULL;
250 		    le = nle) {
251 			nle = TAILQ_NEXT(le, entry);
252 			if (ibuf_left(buf) < sizeof(struct lsa_hdr) +
253 			    MD5_DIGEST_LENGTH)
254 				break;
255 			if (ibuf_add(buf, le->le_lsa, sizeof(struct lsa_hdr)))
256 				break;
257 			ls_ack_list_free(iface, le);
258 			cnt++;
259 		}
260 		if (cnt == 0) {
261 			log_warnx("ls_ack_tx_timer: lost in space");
262 			ibuf_free(buf);
263 			return;
264 		}
265 
266 		/* send LS ack(s) but first set correct destination */
267 		switch (iface->type) {
268 		case IF_TYPE_POINTOPOINT:
269 			inet_aton(AllSPFRouters, &addr);
270 			send_ls_ack(iface, addr, buf);
271 			break;
272 		case IF_TYPE_BROADCAST:
273 			if (iface->state & IF_STA_DRORBDR)
274 				inet_aton(AllSPFRouters, &addr);
275 			else
276 				inet_aton(AllDRouters, &addr);
277 			send_ls_ack(iface, addr, buf);
278 			break;
279 		case IF_TYPE_NBMA:
280 		case IF_TYPE_POINTOMULTIPOINT:
281 		case IF_TYPE_VIRTUALLINK:
282 			LIST_FOREACH(nbr, &iface->nbr_list, entry) {
283 				if (nbr == iface->self)
284 					continue;
285 				if (!(nbr->state & NBR_STA_FLOOD))
286 					continue;
287 				send_ls_ack(iface, nbr->addr, buf);
288 			}
289 			break;
290 		default:
291 			fatalx("lsa_ack_tx_timer: unknown interface type");
292 		}
293 		ibuf_free(buf);
294 	}
295 }
296 
297 void
298 start_ls_ack_tx_timer(struct iface *iface)
299 {
300 	struct timeval tv;
301 
302 	timerclear(&tv);
303 	tv.tv_sec = iface->rxmt_interval / 2;
304 
305 	if (evtimer_add(&iface->lsack_tx_timer, &tv) == -1)
306 		fatal("start_ls_ack_tx_timer");
307 }
308 
309 void
310 start_ls_ack_tx_timer_now(struct iface *iface)
311 {
312 	struct timeval tv;
313 
314 	timerclear(&tv);
315 	if (evtimer_add(&iface->lsack_tx_timer, &tv) == -1)
316 		fatal("start_ls_ack_tx_timer_now");
317 }
318 
319 void
320 stop_ls_ack_tx_timer(struct iface *iface)
321 {
322 	if (evtimer_del(&iface->lsack_tx_timer) == -1)
323 		fatal("stop_ls_ack_tx_timer");
324 }
325