1 /* $OpenBSD: packet.c,v 1.23 2024/05/18 11:17:30 jsg 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 <netinet/ip6.h>
26 #include <arpa/inet.h>
27
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "ospf6d.h"
35 #include "ospf6.h"
36 #include "log.h"
37 #include "ospfe.h"
38
39 int ospf_hdr_sanity_check(struct ospf_hdr *, u_int16_t,
40 const struct iface *, struct in6_addr *);
41 struct iface *find_iface(struct ospfd_conf *, unsigned int,
42 struct in6_addr *);
43
44 static u_int8_t *recv_buf;
45
46 int
gen_ospf_hdr(struct ibuf * buf,struct iface * iface,u_int8_t type)47 gen_ospf_hdr(struct ibuf *buf, struct iface *iface, u_int8_t type)
48 {
49 struct ospf_hdr ospf_hdr;
50
51 bzero(&ospf_hdr, sizeof(ospf_hdr));
52 ospf_hdr.version = OSPF6_VERSION;
53 ospf_hdr.type = type;
54 ospf_hdr.rtr_id = ospfe_router_id();
55 if (iface->type != IF_TYPE_VIRTUALLINK)
56 ospf_hdr.area_id = iface->area->id.s_addr;
57 ospf_hdr.instance = DEFAULT_INSTANCE_ID;
58 ospf_hdr.zero = 0; /* must be zero */
59
60 return (ibuf_add(buf, &ospf_hdr, sizeof(ospf_hdr)));
61 }
62
63 int
upd_ospf_hdr(struct ibuf * buf,struct iface * iface)64 upd_ospf_hdr(struct ibuf *buf, struct iface *iface)
65 {
66 /* update length */
67 if (ibuf_size(buf) > USHRT_MAX)
68 fatalx("upd_ospf_hdr: resulting ospf packet too big");
69 if (ibuf_set_n16(buf, offsetof(struct ospf_hdr, len),
70 ibuf_size(buf)) == -1)
71 fatalx("upd_ospf_hdr: ibuf_set_n16 failed");
72
73 /* checksum calculated via IPV6_CHECKSUM */
74 if (ibuf_set_n16(buf, offsetof(struct ospf_hdr, chksum), 0) == -1)
75 fatalx("upd_ospf_hdr: ibuf_set_n16 failed");
76
77 return (0);
78 }
79
80 /* send and receive packets */
81 int
send_packet(struct iface * iface,struct ibuf * buf,struct in6_addr * dst)82 send_packet(struct iface *iface, struct ibuf *buf,
83 struct in6_addr *dst)
84 {
85 struct sockaddr_in6 sa6;
86
87 /* setup sockaddr */
88 bzero(&sa6, sizeof(sa6));
89 sa6.sin6_family = AF_INET6;
90 sa6.sin6_len = sizeof(sa6);
91 sa6.sin6_addr = *dst;
92
93 /* don't we all love link local scope and all the needed hacks for it */
94 if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst))
95 sa6.sin6_scope_id = iface->ifindex;
96
97 /* set outgoing interface for multicast traffic */
98 if (IN6_IS_ADDR_MULTICAST(dst))
99 if (if_set_mcast(iface) == -1) {
100 log_warn("send_packet: error setting multicast "
101 "interface, %s", iface->name);
102 return (-1);
103 }
104
105 if (sendto(iface->fd, ibuf_data(buf), ibuf_size(buf), 0,
106 (struct sockaddr *)&sa6, sizeof(sa6)) == -1) {
107 log_warn("send_packet: error sending packet on interface %s",
108 iface->name);
109 return (-1);
110 }
111
112 return (0);
113 }
114
115 void
recv_packet(int fd,short event,void * bula)116 recv_packet(int fd, short event, void *bula)
117 {
118 union {
119 struct cmsghdr hdr;
120 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
121 } cmsgbuf;
122 struct msghdr msg;
123 struct iovec iov;
124 struct in6_addr addr, dest;
125 struct sockaddr_in6 src;
126 struct ospfd_conf *xconf = bula;
127 struct ospf_hdr *ospf_hdr;
128 struct iface *iface;
129 struct nbr *nbr = NULL;
130 char *buf;
131 struct cmsghdr *cmsg;
132 ssize_t r;
133 u_int16_t len;
134 int l;
135 unsigned int ifindex = 0;
136
137 if (event != EV_READ)
138 return;
139
140 if (recv_buf == NULL)
141 if ((recv_buf = malloc(READ_BUF_SIZE)) == NULL)
142 fatal(__func__);
143
144 /* setup buffer */
145 bzero(&msg, sizeof(msg));
146 iov.iov_base = buf = recv_buf;
147 iov.iov_len = READ_BUF_SIZE;
148 msg.msg_name = &src;
149 msg.msg_namelen = sizeof(src);
150 msg.msg_iov = &iov;
151 msg.msg_iovlen = 1;
152 msg.msg_control = &cmsgbuf.buf;
153 msg.msg_controllen = sizeof(cmsgbuf.buf);
154
155 if ((r = recvmsg(fd, &msg, 0)) == -1) {
156 if (errno != EAGAIN && errno != EINTR)
157 log_debug("recv_packet: read error: %s",
158 strerror(errno));
159 return;
160 }
161 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
162 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
163 if (cmsg->cmsg_level == IPPROTO_IPV6 &&
164 cmsg->cmsg_type == IPV6_PKTINFO) {
165 ifindex = ((struct in6_pktinfo *)
166 CMSG_DATA(cmsg))->ipi6_ifindex;
167 dest = ((struct in6_pktinfo *)
168 CMSG_DATA(cmsg))->ipi6_addr;
169 break;
170 }
171 }
172
173 /* find a matching interface */
174 if ((iface = find_iface(xconf, ifindex, &src.sin6_addr)) == NULL) {
175 /* XXX add a counter here */
176 return;
177 }
178 /*
179 * Packet needs to be sent to AllSPFRouters or AllDRouters
180 * or to the address of the interface itself.
181 * AllDRouters is only valid for DR and BDR but this is checked later.
182 */
183 inet_pton(AF_INET6, AllSPFRouters, &addr);
184 if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
185 inet_pton(AF_INET6, AllDRouters, &addr);
186 if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
187 struct iface_addr *ia;
188
189 TAILQ_FOREACH(ia, &iface->ifa_list, entry) {
190 if (IN6_ARE_ADDR_EQUAL(&dest, &ia->addr))
191 break;
192 }
193 if (ia == NULL) {
194 log_debug("recv_packet: packet sent to wrong "
195 "address %s, interface %s",
196 log_in6addr(&dest), iface->name);
197 return;
198 }
199 }
200 }
201
202 len = (u_int16_t)r;
203 /* OSPF header sanity checks */
204 if (len < sizeof(*ospf_hdr)) {
205 log_debug("recv_packet: bad packet size");
206 return;
207 }
208 ospf_hdr = (struct ospf_hdr *)buf;
209
210 if ((l = ospf_hdr_sanity_check(ospf_hdr, len, iface, &dest)) == -1)
211 return;
212
213 nbr = nbr_find_id(iface, ospf_hdr->rtr_id);
214 if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) {
215 log_debug("recv_packet: unknown neighbor ID");
216 return;
217 }
218
219 buf += sizeof(*ospf_hdr);
220 len = l - sizeof(*ospf_hdr);
221
222 /* switch OSPF packet type */
223 switch (ospf_hdr->type) {
224 case PACKET_TYPE_HELLO:
225 inet_pton(AF_INET6, AllDRouters, &addr);
226 if (IN6_ARE_ADDR_EQUAL(&dest, &addr)) {
227 log_debug("recv_packet: invalid destination IP "
228 "address");
229 break;
230 }
231
232 recv_hello(iface, &src.sin6_addr, ospf_hdr->rtr_id, buf, len);
233 break;
234 case PACKET_TYPE_DD:
235 recv_db_description(nbr, buf, len);
236 break;
237 case PACKET_TYPE_LS_REQUEST:
238 recv_ls_req(nbr, buf, len);
239 break;
240 case PACKET_TYPE_LS_UPDATE:
241 recv_ls_update(nbr, buf, len);
242 break;
243 case PACKET_TYPE_LS_ACK:
244 recv_ls_ack(nbr, buf, len);
245 break;
246 default:
247 log_debug("recv_packet: unknown OSPF packet type, interface %s",
248 iface->name);
249 }
250 }
251
252 int
ospf_hdr_sanity_check(struct ospf_hdr * ospf_hdr,u_int16_t len,const struct iface * iface,struct in6_addr * dst)253 ospf_hdr_sanity_check(struct ospf_hdr *ospf_hdr, u_int16_t len,
254 const struct iface *iface, struct in6_addr *dst)
255 {
256 struct in6_addr addr;
257 struct in_addr id;
258
259 if (ospf_hdr->version != OSPF6_VERSION) {
260 log_debug("recv_packet: invalid OSPF version %d",
261 ospf_hdr->version);
262 return (-1);
263 }
264
265 if (ntohs(ospf_hdr->len) > len ||
266 len <= sizeof(struct ospf_hdr)) {
267 log_debug("recv_packet: invalid OSPF packet length %d",
268 ntohs(ospf_hdr->len));
269 return (-1);
270 }
271
272 if (iface->type != IF_TYPE_VIRTUALLINK) {
273 if (ospf_hdr->area_id != iface->area->id.s_addr) {
274 id.s_addr = ospf_hdr->area_id;
275 log_debug("recv_packet: invalid area ID %s, "
276 "interface %s", inet_ntoa(id), iface->name);
277 return (-1);
278 }
279 } else {
280 if (ospf_hdr->area_id != 0) {
281 id.s_addr = ospf_hdr->area_id;
282 log_debug("recv_packet: invalid area ID %s, "
283 "interface %s", inet_ntoa(id), iface->name);
284 return (-1);
285 }
286 }
287
288 if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) {
289 if (inet_pton(AF_INET6, AllDRouters, &addr) == 0)
290 fatalx("recv_packet: inet_pton");
291 if (IN6_ARE_ADDR_EQUAL(dst, &addr) &&
292 (iface->state & IF_STA_DRORBDR) == 0) {
293 log_debug("recv_packet: invalid destination IP in "
294 "state %s, interface %s",
295 if_state_name(iface->state), iface->name);
296 return (-1);
297 }
298 }
299
300 return (ntohs(ospf_hdr->len));
301 }
302
303 struct iface *
find_iface(struct ospfd_conf * xconf,unsigned int ifindex,struct in6_addr * src)304 find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in6_addr *src)
305 {
306 struct area *area;
307 struct iface *iface, *match = NULL;
308
309 /*
310 * Returned interface needs to be active.
311 * Virtual-Links have higher precedence so the full interface
312 * list needs to be scanned for possible matches.
313 */
314 LIST_FOREACH(area, &xconf->area_list, entry) {
315 LIST_FOREACH(iface, &area->iface_list, entry) {
316 switch (iface->type) {
317 case IF_TYPE_VIRTUALLINK:
318 if (IN6_ARE_ADDR_EQUAL(src, &iface->dst) &&
319 !(iface->cflags & F_IFACE_PASSIVE))
320 return (iface);
321 break;
322 default:
323 if (ifindex == iface->ifindex &&
324 !(iface->cflags & F_IFACE_PASSIVE))
325 match = iface;
326 break;
327 }
328 }
329 }
330
331 return (match);
332 }
333