1 /* $OpenBSD: packet.c,v 1.14 2015/05/05 01:26:37 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 #include <net/if_dl.h> 28 29 #include <errno.h> 30 #include <event.h> 31 #include <limits.h> 32 #include <stdlib.h> 33 #include <string.h> 34 35 #include "ospf6d.h" 36 #include "ospf6.h" 37 #include "log.h" 38 #include "ospfe.h" 39 40 int ip_hdr_sanity_check(const struct ip6_hdr *, u_int16_t); 41 int ospf_hdr_sanity_check(struct ospf_hdr *, u_int16_t, 42 const struct iface *, struct in6_addr *); 43 struct iface *find_iface(struct ospfd_conf *, unsigned int, 44 struct in6_addr *); 45 46 int 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 64 upd_ospf_hdr(struct ibuf *buf, struct iface *iface) 65 { 66 struct ospf_hdr *ospf_hdr; 67 68 if ((ospf_hdr = ibuf_seek(buf, 0, sizeof(*ospf_hdr))) == NULL) 69 fatalx("upd_ospf_hdr: buf_seek failed"); 70 71 /* update length */ 72 if (buf->wpos > USHRT_MAX) 73 fatalx("upd_ospf_hdr: resulting ospf packet too big"); 74 ospf_hdr->len = htons((u_int16_t)buf->wpos); 75 ospf_hdr->chksum = 0; /* calculated via IPV6_CHECKSUM */ 76 77 return (0); 78 } 79 80 /* send and receive packets */ 81 int 82 send_packet(struct iface *iface, void *pkt, size_t len, 83 struct in6_addr *dst) 84 { 85 struct sockaddr_in6 sa6; 86 87 /* setup buffer */ 88 bzero(&sa6, sizeof(sa6)); 89 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, pkt, len, 0, (struct sockaddr *)&sa6, 107 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 /* setup buffer */ 142 bzero(&msg, sizeof(msg)); 143 iov.iov_base = buf = pkt_ptr; 144 iov.iov_len = READ_BUF_SIZE; 145 msg.msg_name = &src; 146 msg.msg_namelen = sizeof(src); 147 msg.msg_iov = &iov; 148 msg.msg_iovlen = 1; 149 msg.msg_control = &cmsgbuf.buf; 150 msg.msg_controllen = sizeof(cmsgbuf.buf); 151 152 if ((r = recvmsg(fd, &msg, 0)) == -1) { 153 if (errno != EAGAIN && errno != EINTR) 154 log_debug("recv_packet: read error: %s", 155 strerror(errno)); 156 return; 157 } 158 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 159 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 160 if (cmsg->cmsg_level == IPPROTO_IPV6 && 161 cmsg->cmsg_type == IPV6_PKTINFO) { 162 ifindex = ((struct in6_pktinfo *) 163 CMSG_DATA(cmsg))->ipi6_ifindex; 164 dest = ((struct in6_pktinfo *) 165 CMSG_DATA(cmsg))->ipi6_addr; 166 break; 167 } 168 } 169 170 /* find a matching interface */ 171 if ((iface = find_iface(xconf, ifindex, &src.sin6_addr)) == NULL) { 172 /* XXX add a counter here */ 173 return; 174 } 175 /* 176 * Packet needs to be sent to AllSPFRouters or AllDRouters 177 * or to the address of the interface itself. 178 * AllDRouters is only valid for DR and BDR but this is checked later. 179 */ 180 inet_pton(AF_INET6, AllSPFRouters, &addr); 181 182 if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) { 183 inet_pton(AF_INET6, AllDRouters, &addr); 184 if (!IN6_ARE_ADDR_EQUAL(&dest, &addr)) { 185 if (!IN6_ARE_ADDR_EQUAL(&dest, &iface->addr)) { 186 log_debug("recv_packet: packet sent to wrong " 187 "address %s, interface %s", 188 log_in6addr(&dest), iface->name); 189 return; 190 } 191 } 192 } 193 194 len = (u_int16_t)r; 195 /* OSPF header sanity checks */ 196 if (len < sizeof(*ospf_hdr)) { 197 log_debug("recv_packet: bad packet size"); 198 return; 199 } 200 ospf_hdr = (struct ospf_hdr *)buf; 201 202 if ((l = ospf_hdr_sanity_check(ospf_hdr, len, iface, &dest)) == -1) 203 return; 204 205 nbr = nbr_find_id(iface, ospf_hdr->rtr_id); 206 if (ospf_hdr->type != PACKET_TYPE_HELLO && nbr == NULL) { 207 log_debug("recv_packet: unknown neighbor ID"); 208 return; 209 } 210 211 buf += sizeof(*ospf_hdr); 212 len = l - sizeof(*ospf_hdr); 213 214 /* switch OSPF packet type */ 215 switch (ospf_hdr->type) { 216 case PACKET_TYPE_HELLO: 217 inet_pton(AF_INET6, AllDRouters, &addr); 218 if (IN6_ARE_ADDR_EQUAL(&dest, &addr)) { 219 log_debug("recv_packet: invalid destination IP " 220 "address"); 221 break; 222 } 223 224 recv_hello(iface, &src.sin6_addr, ospf_hdr->rtr_id, buf, len); 225 break; 226 case PACKET_TYPE_DD: 227 recv_db_description(nbr, buf, len); 228 break; 229 case PACKET_TYPE_LS_REQUEST: 230 recv_ls_req(nbr, buf, len); 231 break; 232 case PACKET_TYPE_LS_UPDATE: 233 recv_ls_update(nbr, buf, len); 234 break; 235 case PACKET_TYPE_LS_ACK: 236 recv_ls_ack(nbr, buf, len); 237 break; 238 default: 239 log_debug("recv_packet: unknown OSPF packet type, interface %s", 240 iface->name); 241 } 242 } 243 244 int 245 ospf_hdr_sanity_check(struct ospf_hdr *ospf_hdr, u_int16_t len, 246 const struct iface *iface, struct in6_addr *dst) 247 { 248 struct in6_addr addr; 249 struct in_addr id; 250 251 if (ospf_hdr->version != OSPF6_VERSION) { 252 log_debug("recv_packet: invalid OSPF version %d", 253 ospf_hdr->version); 254 return (-1); 255 } 256 257 if (ntohs(ospf_hdr->len) > len || 258 len <= sizeof(struct ospf_hdr)) { 259 log_debug("recv_packet: invalid OSPF packet length %d", 260 ntohs(ospf_hdr->len)); 261 return (-1); 262 } 263 264 if (iface->type != IF_TYPE_VIRTUALLINK) { 265 if (ospf_hdr->area_id != iface->area_id.s_addr) { 266 id.s_addr = ospf_hdr->area_id; 267 log_debug("recv_packet: invalid area ID %s, " 268 "interface %s", inet_ntoa(id), iface->name); 269 return (-1); 270 } 271 } else { 272 if (ospf_hdr->area_id != 0) { 273 id.s_addr = ospf_hdr->area_id; 274 log_debug("recv_packet: invalid area ID %s, " 275 "interface %s", inet_ntoa(id), iface->name); 276 return (-1); 277 } 278 } 279 280 if (iface->type == IF_TYPE_BROADCAST || iface->type == IF_TYPE_NBMA) { 281 if (inet_pton(AF_INET6, AllDRouters, &addr) == 0) 282 fatalx("recv_packet: inet_pton"); 283 if (IN6_ARE_ADDR_EQUAL(dst, &addr) && 284 (iface->state & IF_STA_DRORBDR) == 0) { 285 log_debug("recv_packet: invalid destination IP in " 286 "state %s, interface %s", 287 if_state_name(iface->state), iface->name); 288 return (-1); 289 } 290 } 291 292 return (ntohs(ospf_hdr->len)); 293 } 294 295 struct iface * 296 find_iface(struct ospfd_conf *xconf, unsigned int ifindex, struct in6_addr *src) 297 { 298 struct area *area; 299 struct iface *iface, *match = NULL; 300 301 /* 302 * Returned interface needs to be active. 303 * Virtual-Links have higher precedence so the full interface 304 * list needs to be scanned for possible matches. 305 */ 306 LIST_FOREACH(area, &xconf->area_list, entry) { 307 LIST_FOREACH(iface, &area->iface_list, entry) { 308 switch (iface->type) { 309 case IF_TYPE_VIRTUALLINK: 310 if (IN6_ARE_ADDR_EQUAL(src, &iface->dst) && 311 !(iface->cflags & F_IFACE_PASSIVE)) 312 return (iface); 313 break; 314 default: 315 if (ifindex == iface->ifindex && 316 !(iface->cflags & F_IFACE_PASSIVE)) 317 match = iface; 318 break; 319 } 320 } 321 } 322 323 return (match); 324 } 325