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