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