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