GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ospfd/packet.c Lines: 0 170 0.0 %
Date: 2017-11-07 Branches: 0 99 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: packet.c,v 1.31 2014/10/25 03:23:49 lteo 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 <sys/uio.h>
22
23
#include <netinet/in.h>
24
#include <netinet/ip.h>
25
#include <arpa/inet.h>
26
#include <net/if_dl.h>
27
28
#include <errno.h>
29
#include <event.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
#include "ospfd.h"
34
#include "ospf.h"
35
#include "log.h"
36
#include "ospfe.h"
37
38
int		 ip_hdr_sanity_check(const struct ip *, u_int16_t);
39
int		 ospf_hdr_sanity_check(const struct ip *,
40
		    struct ospf_hdr *, u_int16_t, const struct iface *);
41
struct iface	*find_iface(struct ospfd_conf *, unsigned int, struct in_addr);
42
43
int
44
gen_ospf_hdr(struct ibuf *buf, struct iface *iface, u_int8_t type)
45
{
46
	struct ospf_hdr	ospf_hdr;
47
48
	bzero(&ospf_hdr, sizeof(ospf_hdr));
49
	ospf_hdr.version = OSPF_VERSION;
50
	ospf_hdr.type = type;
51
	ospf_hdr.rtr_id = ospfe_router_id();
52
	if (iface->type != IF_TYPE_VIRTUALLINK)
53
		ospf_hdr.area_id = iface->area->id.s_addr;
54
	ospf_hdr.auth_type = htons(iface->auth_type);
55
56
	return (ibuf_add(buf, &ospf_hdr, sizeof(ospf_hdr)));
57
}
58
59
/* send and receive packets */
60
int
61
send_packet(struct iface *iface, struct ibuf *buf, struct sockaddr_in *dst)
62
{
63
	struct msghdr		 msg;
64
	struct iovec		 iov[2];
65
	struct ip		 ip_hdr;
66
67
	/* setup IP hdr */
68
	bzero(&ip_hdr, sizeof(ip_hdr));
69
	ip_hdr.ip_v = IPVERSION;
70
	ip_hdr.ip_hl = sizeof(ip_hdr) >> 2;
71
	ip_hdr.ip_tos = IPTOS_PREC_INTERNETCONTROL;
72
	ip_hdr.ip_len = htons(ibuf_size(buf) + sizeof(ip_hdr));
73
	ip_hdr.ip_id = 0;  /* 0 means kernel set appropriate value */
74
	ip_hdr.ip_off = 0;
75
	ip_hdr.ip_ttl = iface->type != IF_TYPE_VIRTUALLINK ?
76
	    IP_DEFAULT_MULTICAST_TTL : MAXTTL;
77
	ip_hdr.ip_p = IPPROTO_OSPF;
78
	ip_hdr.ip_sum = 0;
79
	ip_hdr.ip_src = iface->addr;
80
	ip_hdr.ip_dst = dst->sin_addr;
81
82
	/* setup buffer */
83
	bzero(&msg, sizeof(msg));
84
	iov[0].iov_base = &ip_hdr;
85
	iov[0].iov_len = sizeof(ip_hdr);
86
	iov[1].iov_base = buf->buf;
87
	iov[1].iov_len = ibuf_size(buf);
88
	msg.msg_name = dst;
89
	msg.msg_namelen = sizeof(*dst);
90
	msg.msg_iov = iov;
91
	msg.msg_iovlen = 2;
92
93
	/* set outgoing interface for multicast traffic */
94
	if (IN_MULTICAST(ntohl(dst->sin_addr.s_addr)))
95
		if (if_set_mcast(iface) == -1)
96
			return (-1);
97
98
	if (sendmsg(iface->fd, &msg, 0) == -1) {
99
		log_warn("send_packet: error sending packet on interface %s",
100
		    iface->name);
101
		return (-1);
102
	}
103
104
	return (0);
105
}
106
107
void
108
recv_packet(int fd, short event, void *bula)
109
{
110
	union {
111
		struct cmsghdr hdr;
112
		char	buf[CMSG_SPACE(sizeof(struct sockaddr_dl))];
113
	} cmsgbuf;
114
	struct msghdr		 msg;
115
	struct iovec		 iov;
116
	struct ip		 ip_hdr;
117
	struct in_addr		 addr;
118
	struct ospfd_conf	*xconf = bula;
119
	struct ospf_hdr		*ospf_hdr;
120
	struct iface		*iface;
121
	struct nbr		*nbr = NULL;
122
	char			*buf;
123
	struct cmsghdr		*cmsg;
124
	ssize_t			 r;
125
	u_int16_t		 len;
126
	int			 l;
127
	unsigned int		 ifindex = 0;
128
129
	if (event != EV_READ)
130
		return;
131
132
	/* setup buffer */
133
	bzero(&msg, sizeof(msg));
134
	iov.iov_base = buf = pkt_ptr;
135
	iov.iov_len = READ_BUF_SIZE;
136
	msg.msg_iov = &iov;
137
	msg.msg_iovlen = 1;
138
	msg.msg_control = &cmsgbuf.buf;
139
	msg.msg_controllen = sizeof(cmsgbuf.buf);
140
141
	if ((r = recvmsg(fd, &msg, 0)) == -1) {
142
		if (errno != EAGAIN && errno != EINTR)
143
			log_debug("recv_packet: read error: %s",
144
			    strerror(errno));
145
		return;
146
	}
147
	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
148
	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
149
		if (cmsg->cmsg_level == IPPROTO_IP &&
150
		    cmsg->cmsg_type == IP_RECVIF) {
151
			ifindex = ((struct sockaddr_dl *)
152
			    CMSG_DATA(cmsg))->sdl_index;
153
			break;
154
		}
155
	}
156
157
	len = (u_int16_t)r;
158
159
	/* IP header sanity checks */
160
	if (len < sizeof(ip_hdr)) {
161
		log_warnx("recv_packet: bad packet size");
162
		return;
163
	}
164
	memcpy(&ip_hdr, buf, sizeof(ip_hdr));
165
	if ((l = ip_hdr_sanity_check(&ip_hdr, len)) == -1)
166
		return;
167
	buf += l;
168
	len -= l;
169
170
	/* find a matching interface */
171
	if ((iface = find_iface(xconf, ifindex, ip_hdr.ip_src)) == NULL) {
172
		/* XXX add a counter here */
173
		return;
174
	}
175
176
	/*
177
	 * Packet needs to be sent to AllSPFRouters or AllDRouters
178
	 * or to the address of the interface itself.
179
	 * AllDRouters is only valid for DR and BDR but this is checked later.
180
	 */
181
	inet_aton(AllSPFRouters, &addr);
182
	if (ip_hdr.ip_dst.s_addr != addr.s_addr) {
183
		inet_aton(AllDRouters, &addr);
184
		if (ip_hdr.ip_dst.s_addr != addr.s_addr) {
185
			if (ip_hdr.ip_dst.s_addr != iface->addr.s_addr) {
186
				log_debug("recv_packet: packet sent to wrong "
187
				    "address %s, interface %s",
188
				    inet_ntoa(ip_hdr.ip_dst), iface->name);
189
				return;
190
			}
191
		}
192
	}
193
194
	/* OSPF header sanity checks */
195
	if (len < sizeof(*ospf_hdr)) {
196
		log_debug("recv_packet: bad packet size");
197
		return;
198
	}
199
	ospf_hdr = (struct ospf_hdr *)buf;
200
201
	if ((l = ospf_hdr_sanity_check(&ip_hdr, ospf_hdr, len, iface)) == -1)
202
		return;
203
204
	nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
205
	if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
206
		log_debug("recv_packet: unknown neighbor ID");
207
		return;
208
	}
209
210
	if (auth_validate(buf, len, iface, nbr)) {
211
		log_warnx("recv_packet: authentication error, "
212
		    "interface %s", iface->name);
213
		return;
214
	}
215
216
	buf += sizeof(*ospf_hdr);
217
	len = l - sizeof(*ospf_hdr);
218
219
	/* switch OSPF packet type */
220
	switch (ospf_hdr->type) {
221
	case PACKET_TYPE_HELLO:
222
		inet_aton(AllDRouters, &addr);
223
		if (ip_hdr.ip_dst.s_addr == addr.s_addr) {
224
			log_debug("recv_packet: invalid destination IP "
225
			     "address");
226
			break;
227
		}
228
229
		recv_hello(iface, ip_hdr.ip_src, ospf_hdr->rtr_id, buf, len);
230
		break;
231
	case PACKET_TYPE_DD:
232
		recv_db_description(nbr, buf, len);
233
		break;
234
	case PACKET_TYPE_LS_REQUEST:
235
		recv_ls_req(nbr, buf, len);
236
		break;
237
	case PACKET_TYPE_LS_UPDATE:
238
		recv_ls_update(nbr, buf, len);
239
		break;
240
	case PACKET_TYPE_LS_ACK:
241
		recv_ls_ack(nbr, buf, len);
242
		break;
243
	default:
244
		log_debug("recv_packet: unknown OSPF packet type, interface %s",
245
		    iface->name);
246
	}
247
}
248
249
int
250
ip_hdr_sanity_check(const struct ip *ip_hdr, u_int16_t len)
251
{
252
	if (ntohs(ip_hdr->ip_len) != len) {
253
		log_debug("recv_packet: invalid IP packet length %u",
254
		    ntohs(ip_hdr->ip_len));
255
		return (-1);
256
	}
257
258
	if (ip_hdr->ip_p != IPPROTO_OSPF)
259
		/* this is enforced by the socket itself */
260
		fatalx("recv_packet: invalid IP proto");
261
262
	return (ip_hdr->ip_hl << 2);
263
}
264
265
int
266
ospf_hdr_sanity_check(const struct ip *ip_hdr, struct ospf_hdr *ospf_hdr,
267
    u_int16_t len, const struct iface *iface)
268
{
269
	struct in_addr		 addr;
270
271
	if (ospf_hdr->version != OSPF_VERSION) {
272
		log_debug("recv_packet: invalid OSPF version %d",
273
		    ospf_hdr->version);
274
		return (-1);
275
	}
276
277
	if (ntohs(ospf_hdr->len) > len ||
278
	    len <= sizeof(struct ospf_hdr)) {
279
		log_debug("recv_packet: invalid OSPF packet length %d",
280
		    ntohs(ospf_hdr->len));
281
		return (-1);
282
	}
283
284
	if (iface->type != IF_TYPE_VIRTUALLINK) {
285
		if (ospf_hdr->area_id != iface->area->id.s_addr) {
286
			addr.s_addr = ospf_hdr->area_id;
287
			log_debug("recv_packet: invalid area ID %s, "
288
			    "interface %s", inet_ntoa(addr), iface->name);
289
			return (-1);
290
		}
291
	} else {
292
		if (ospf_hdr->area_id != 0) {
293
			addr.s_addr = ospf_hdr->area_id;
294
			log_debug("recv_packet: invalid area ID %s, "
295
			    "interface %s", inet_ntoa(addr), iface->name);
296
			return (-1);
297
		}
298
	}
299
300
	if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
301
		if (inet_aton(AllDRouters, &addr) == 0)
302
			fatalx("recv_packet: inet_aton");
303
		if (ip_hdr->ip_dst.s_addr == addr.s_addr &&
304
		    (iface->state & IF_STA_DRORBDR) == 0) {
305
			log_debug("recv_packet: invalid destination IP in "
306
			    "state %s, interface %s",
307
			    if_state_name(iface->state), iface->name);
308
			return (-1);
309
		}
310
	}
311
312
	return (ntohs(ospf_hdr->len));
313
}
314
315
struct iface *
316
find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in_addr src)
317
{
318
	struct area	*area = NULL;
319
	struct iface	*iface = NULL;
320
321
	/* returned interface needs to be active */
322
	LIST_FOREACH(area, &xconf->area_list, entry) {
323
		LIST_FOREACH(iface, &area->iface_list, entry) {
324
			switch (iface->type) {
325
			case IF_TYPE_VIRTUALLINK:
326
				if ((src.s_addr == iface->dst.s_addr) &&
327
				    !iface->passive)
328
					return (iface);
329
				break;
330
			case IF_TYPE_POINTOPOINT:
331
				if (ifindex == iface->ifindex &&
332
				    !iface->passive)
333
					return (iface);
334
				break;
335
			default:
336
				if (ifindex == iface->ifindex &&
337
				    (iface->addr.s_addr & iface->mask.s_addr) ==
338
				    (src.s_addr & iface->mask.s_addr) &&
339
				    !iface->passive)
340
					return (iface);
341
				break;
342
			}
343
		}
344
	}
345
346
	return (NULL);
347
}