1*414c2f5bSrenato /* $OpenBSD: packet.c,v 1.17 2016/09/02 16:36:33 renato Exp $ */ 243509a12Srenato 343509a12Srenato /* 443509a12Srenato * Copyright (c) 2015 Renato Westphal <renato@openbsd.org> 543509a12Srenato * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org> 643509a12Srenato * 743509a12Srenato * Permission to use, copy, modify, and distribute this software for any 843509a12Srenato * purpose with or without fee is hereby granted, provided that the above 943509a12Srenato * copyright notice and this permission notice appear in all copies. 1043509a12Srenato * 1143509a12Srenato * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1243509a12Srenato * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1343509a12Srenato * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1443509a12Srenato * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1543509a12Srenato * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1643509a12Srenato * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1743509a12Srenato * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1843509a12Srenato */ 1943509a12Srenato 208072de9bSrenato #include <sys/types.h> 2143509a12Srenato #include <net/if_dl.h> 228072de9bSrenato #include <netinet/in.h> 238072de9bSrenato #include <netinet/ip.h> 248072de9bSrenato 258072de9bSrenato #include <arpa/inet.h> 2643509a12Srenato #include <errno.h> 278072de9bSrenato #include <stdio.h> 288072de9bSrenato #include <stdlib.h> 2943509a12Srenato #include <string.h> 3043509a12Srenato 3143509a12Srenato #include "eigrpd.h" 3243509a12Srenato #include "eigrpe.h" 338072de9bSrenato #include "log.h" 3443509a12Srenato 3543509a12Srenato extern struct eigrpd_conf *econf; 3643509a12Srenato 3743509a12Srenato int eigrp_hdr_sanity_check(int, union eigrpd_addr *, 3843509a12Srenato struct eigrp_hdr *, uint16_t, const struct iface *); 3943509a12Srenato struct iface *find_iface(unsigned int, int, union eigrpd_addr *); 4043509a12Srenato 4143509a12Srenato int 4243509a12Srenato gen_eigrp_hdr(struct ibuf *buf, uint16_t opcode, uint8_t flags, 4343509a12Srenato uint32_t seq_num, uint16_t as) 4443509a12Srenato { 4543509a12Srenato struct eigrp_hdr eigrp_hdr; 4643509a12Srenato 4743509a12Srenato memset(&eigrp_hdr, 0, sizeof(eigrp_hdr)); 4843509a12Srenato eigrp_hdr.version = EIGRP_VERSION; 4943509a12Srenato eigrp_hdr.opcode = opcode; 5043509a12Srenato /* chksum will be set later */ 5143509a12Srenato eigrp_hdr.flags = htonl(flags); 5243509a12Srenato eigrp_hdr.seq_num = htonl(seq_num); 5343509a12Srenato /* ack_num will be set later */ 5443509a12Srenato eigrp_hdr.vrid = htons(EIGRP_VRID_UNICAST_AF); 5543509a12Srenato eigrp_hdr.as = htons(as); 5643509a12Srenato 5743509a12Srenato return (ibuf_add(buf, &eigrp_hdr, sizeof(eigrp_hdr))); 5843509a12Srenato } 5943509a12Srenato 6043509a12Srenato /* send and receive packets */ 6143509a12Srenato static int 6243509a12Srenato send_packet_v4(struct iface *iface, struct nbr *nbr, struct ibuf *buf) 6343509a12Srenato { 6443509a12Srenato struct sockaddr_in dst; 6543509a12Srenato struct msghdr msg; 6643509a12Srenato struct iovec iov[2]; 6743509a12Srenato struct ip ip_hdr; 6843509a12Srenato 6943509a12Srenato /* setup sockaddr */ 7043509a12Srenato dst.sin_family = AF_INET; 7143509a12Srenato dst.sin_len = sizeof(struct sockaddr_in); 7243509a12Srenato if (nbr) 73bce3b5b3Srenato dst.sin_addr = nbr->addr.v4; 7443509a12Srenato else 75e14e95d7Srenato dst.sin_addr = global.mcast_addr_v4; 7643509a12Srenato 7743509a12Srenato /* setup IP hdr */ 7843509a12Srenato memset(&ip_hdr, 0, sizeof(ip_hdr)); 7943509a12Srenato ip_hdr.ip_v = IPVERSION; 8043509a12Srenato ip_hdr.ip_hl = sizeof(ip_hdr) >> 2; 8143509a12Srenato ip_hdr.ip_tos = IPTOS_PREC_INTERNETCONTROL; 8243509a12Srenato ip_hdr.ip_len = htons(ibuf_size(buf) + sizeof(ip_hdr)); 8343509a12Srenato ip_hdr.ip_id = 0; /* 0 means kernel set appropriate value */ 8443509a12Srenato ip_hdr.ip_off = 0; 8543509a12Srenato ip_hdr.ip_ttl = EIGRP_IP_TTL; 8643509a12Srenato ip_hdr.ip_p = IPPROTO_EIGRP; 8743509a12Srenato ip_hdr.ip_sum = 0; 8843509a12Srenato ip_hdr.ip_src.s_addr = if_primary_addr(iface); 8943509a12Srenato ip_hdr.ip_dst = dst.sin_addr; 9043509a12Srenato 9143509a12Srenato /* setup buffer */ 9243509a12Srenato memset(&msg, 0, sizeof(msg)); 9343509a12Srenato iov[0].iov_base = &ip_hdr; 9443509a12Srenato iov[0].iov_len = sizeof(ip_hdr); 9543509a12Srenato iov[1].iov_base = buf->buf; 9643509a12Srenato iov[1].iov_len = ibuf_size(buf); 9743509a12Srenato msg.msg_name = &dst; 9843509a12Srenato msg.msg_namelen = sizeof(dst); 9943509a12Srenato msg.msg_iov = iov; 10043509a12Srenato msg.msg_iovlen = 2; 10143509a12Srenato 10243509a12Srenato /* set outgoing interface for multicast traffic */ 10343509a12Srenato if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) 10443509a12Srenato if (if_set_ipv4_mcast(iface) == -1) { 10543509a12Srenato log_warn("%s: error setting multicast interface, %s", 10643509a12Srenato __func__, iface->name); 10743509a12Srenato return (-1); 10843509a12Srenato } 10943509a12Srenato 11002eb61efSrenato if (sendmsg(global.eigrp_socket_v4, &msg, 0) == -1) { 11143509a12Srenato log_warn("%s: error sending packet on interface %s", 11243509a12Srenato __func__, iface->name); 11343509a12Srenato return (-1); 11443509a12Srenato } 11543509a12Srenato 11643509a12Srenato return (0); 11743509a12Srenato } 11843509a12Srenato 11943509a12Srenato static int 12043509a12Srenato send_packet_v6(struct iface *iface, struct nbr *nbr, struct ibuf *buf) 12143509a12Srenato { 12243509a12Srenato struct sockaddr_in6 sa6; 12343509a12Srenato 12443509a12Srenato /* setup sockaddr */ 12543509a12Srenato memset(&sa6, 0, sizeof(sa6)); 12643509a12Srenato sa6.sin6_family = AF_INET6; 12743509a12Srenato sa6.sin6_len = sizeof(struct sockaddr_in6); 1283d32bb37Srenato if (nbr) { 12943509a12Srenato sa6.sin6_addr = nbr->addr.v6; 13043509a12Srenato addscope(&sa6, iface->ifindex); 1313d32bb37Srenato } else 132e14e95d7Srenato sa6.sin6_addr = global.mcast_addr_v6; 13343509a12Srenato 13443509a12Srenato /* set outgoing interface for multicast traffic */ 13543509a12Srenato if (IN6_IS_ADDR_MULTICAST(&sa6.sin6_addr)) 13643509a12Srenato if (if_set_ipv6_mcast(iface) == -1) { 13743509a12Srenato log_warn("%s: error setting multicast interface, %s", 13843509a12Srenato __func__, iface->name); 13943509a12Srenato return (-1); 14043509a12Srenato } 14143509a12Srenato 14202eb61efSrenato if (sendto(global.eigrp_socket_v6, buf->buf, buf->wpos, 0, 14343509a12Srenato (struct sockaddr *)&sa6, sizeof(sa6)) == -1) { 14443509a12Srenato log_warn("%s: error sending packet on interface %s", 14543509a12Srenato __func__, iface->name); 14643509a12Srenato return (-1); 14743509a12Srenato } 14843509a12Srenato 14943509a12Srenato return (0); 15043509a12Srenato } 15143509a12Srenato 15243509a12Srenato int 15343509a12Srenato send_packet(struct eigrp_iface *ei, struct nbr *nbr, uint32_t flags, 15443509a12Srenato struct ibuf *buf) 15543509a12Srenato { 15643509a12Srenato struct eigrp *eigrp = ei->eigrp; 15743509a12Srenato struct iface *iface = ei->iface; 15843509a12Srenato struct eigrp_hdr *eigrp_hdr; 15943509a12Srenato 16043509a12Srenato if (!(iface->flags & IFF_UP) || !LINK_STATE_IS_UP(iface->linkstate)) 16143509a12Srenato return (-1); 16243509a12Srenato 16343509a12Srenato /* update ack number, flags and checksum */ 16443509a12Srenato if ((eigrp_hdr = ibuf_seek(buf, 0, sizeof(*eigrp_hdr))) == NULL) 16543509a12Srenato fatalx("send_packet: buf_seek failed"); 16643509a12Srenato if (nbr) { 16743509a12Srenato eigrp_hdr->ack_num = htonl(nbr->recv_seq); 16843509a12Srenato rtp_ack_stop_timer(nbr); 16943509a12Srenato } 17043509a12Srenato if (flags) { 17143509a12Srenato eigrp_hdr->flags = ntohl(eigrp_hdr->flags) | flags; 17243509a12Srenato eigrp_hdr->flags = htonl(eigrp_hdr->flags); 17343509a12Srenato } 17443509a12Srenato eigrp_hdr->chksum = 0; 17543509a12Srenato eigrp_hdr->chksum = in_cksum(buf->buf, ibuf_size(buf)); 17643509a12Srenato 17743509a12Srenato /* log packet being sent */ 17843509a12Srenato if (eigrp_hdr->opcode != EIGRP_OPC_HELLO) { 17943509a12Srenato char buffer[64]; 18043509a12Srenato 18143509a12Srenato if (nbr) 18243509a12Srenato snprintf(buffer, sizeof(buffer), "nbr %s", 18343509a12Srenato log_addr(eigrp->af, &nbr->addr)); 18443509a12Srenato else 18543509a12Srenato snprintf(buffer, sizeof(buffer), "(multicast)"); 18643509a12Srenato 18743509a12Srenato log_debug("%s: type %s iface %s %s AS %u seq %u ack %u", 18843509a12Srenato __func__, opcode_name(eigrp_hdr->opcode), iface->name, 18943509a12Srenato buffer, ntohs(eigrp_hdr->as), ntohl(eigrp_hdr->seq_num), 19043509a12Srenato ntohl(eigrp_hdr->ack_num)); 19143509a12Srenato } 19243509a12Srenato 19343509a12Srenato switch (eigrp->af) { 19443509a12Srenato case AF_INET: 195dcfaa8d4Srenato if (send_packet_v4(iface, nbr, buf) < 0) 196dcfaa8d4Srenato return (-1); 19743509a12Srenato break; 19843509a12Srenato case AF_INET6: 199dcfaa8d4Srenato if (send_packet_v6(iface, nbr, buf) < 0) 200dcfaa8d4Srenato return (-1); 20143509a12Srenato break; 20243509a12Srenato default: 2033c8071b0Srenato fatalx("send_packet: unknown af"); 20443509a12Srenato } 20543509a12Srenato 206dcfaa8d4Srenato switch (eigrp_hdr->opcode) { 207dcfaa8d4Srenato case EIGRP_OPC_HELLO: 208dcfaa8d4Srenato if (ntohl(eigrp_hdr->ack_num) == 0) 209dcfaa8d4Srenato ei->eigrp->stats.hellos_sent++; 210dcfaa8d4Srenato else 211dcfaa8d4Srenato ei->eigrp->stats.acks_sent++; 212dcfaa8d4Srenato break; 213dcfaa8d4Srenato case EIGRP_OPC_UPDATE: 214dcfaa8d4Srenato ei->eigrp->stats.updates_sent++; 215dcfaa8d4Srenato break; 216dcfaa8d4Srenato case EIGRP_OPC_QUERY: 217dcfaa8d4Srenato ei->eigrp->stats.queries_sent++; 218dcfaa8d4Srenato break; 219dcfaa8d4Srenato case EIGRP_OPC_REPLY: 220dcfaa8d4Srenato ei->eigrp->stats.replies_sent++; 221dcfaa8d4Srenato break; 222dcfaa8d4Srenato case EIGRP_OPC_SIAQUERY: 223dcfaa8d4Srenato ei->eigrp->stats.squeries_sent++; 224dcfaa8d4Srenato break; 225dcfaa8d4Srenato case EIGRP_OPC_SIAREPLY: 226dcfaa8d4Srenato ei->eigrp->stats.sreplies_sent++; 227dcfaa8d4Srenato break; 228dcfaa8d4Srenato default: 229dcfaa8d4Srenato break; 230dcfaa8d4Srenato } 231dcfaa8d4Srenato 23243509a12Srenato return (0); 23343509a12Srenato } 23443509a12Srenato 23543509a12Srenato static int 23643509a12Srenato recv_packet_nbr(struct nbr *nbr, struct eigrp_hdr *eigrp_hdr, 23743509a12Srenato struct seq_addr_head *seq_addr_list, struct tlv_mcast_seq *tm) 23843509a12Srenato { 23943509a12Srenato uint32_t seq, ack; 24043509a12Srenato struct seq_addr_entry *sa; 24143509a12Srenato 24243509a12Srenato seq = ntohl(eigrp_hdr->seq_num); 24343509a12Srenato ack = ntohl(eigrp_hdr->ack_num); 24443509a12Srenato 24543509a12Srenato /* 24643509a12Srenato * draft-savage-eigrp-04 - Section 5.3.1: 24743509a12Srenato * "In addition to the HELLO packet, if any packet is received within 24843509a12Srenato * the hold time period, then the Hold Time period will be reset." 24943509a12Srenato */ 25043509a12Srenato nbr_start_timeout(nbr); 25143509a12Srenato 25243509a12Srenato /* handle the sequence tlv */ 25343509a12Srenato if (eigrp_hdr->opcode == EIGRP_OPC_HELLO && 25443509a12Srenato !TAILQ_EMPTY(seq_addr_list)) { 25543509a12Srenato nbr->flags |= F_EIGRP_NBR_CR_MODE; 25643509a12Srenato 25743509a12Srenato TAILQ_FOREACH(sa, seq_addr_list, entry) { 25843509a12Srenato switch (sa->af) { 25943509a12Srenato case AF_INET: 26043509a12Srenato if (sa->addr.v4.s_addr == 26143509a12Srenato if_primary_addr(nbr->ei->iface)) { 26243509a12Srenato nbr->flags &= ~F_EIGRP_NBR_CR_MODE; 26343509a12Srenato break; 26443509a12Srenato } 26543509a12Srenato break; 26643509a12Srenato case AF_INET6: 26743509a12Srenato if (IN6_ARE_ADDR_EQUAL(&sa->addr.v6, 26843509a12Srenato &nbr->ei->iface->linklocal)) { 26943509a12Srenato nbr->flags &= ~F_EIGRP_NBR_CR_MODE; 27043509a12Srenato break; 27143509a12Srenato } 27243509a12Srenato break; 27343509a12Srenato default: 27443509a12Srenato break; 27543509a12Srenato } 27643509a12Srenato } 27743509a12Srenato if (tm) 278bb7278f1Srenato nbr->next_mcast_seq = ntohl(tm->seq); 27943509a12Srenato } 28043509a12Srenato 28143509a12Srenato if ((ntohl(eigrp_hdr->flags) & EIGRP_HDR_FLAG_CR)) { 28243509a12Srenato if (!(nbr->flags & F_EIGRP_NBR_CR_MODE)) 28343509a12Srenato return (-1); 284bb7278f1Srenato nbr->flags &= ~F_EIGRP_NBR_CR_MODE; 28543509a12Srenato if (ntohl(eigrp_hdr->seq_num) != nbr->next_mcast_seq) 28643509a12Srenato return (-1); 28743509a12Srenato } 28843509a12Srenato 289bb7278f1Srenato /* ack processing */ 290bb7278f1Srenato if (ack != 0) 291bb7278f1Srenato rtp_process_ack(nbr, ack); 292bb7278f1Srenato if (seq != 0) { 293bb7278f1Srenato /* check for sequence wraparound */ 294bb7278f1Srenato if (nbr->recv_seq >= seq && 295bb7278f1Srenato !(nbr->recv_seq == UINT32_MAX && seq == 1)) { 296bb7278f1Srenato log_debug("%s: duplicate packet", __func__); 297bb7278f1Srenato rtp_send_ack(nbr); 298bb7278f1Srenato return (-1); 299bb7278f1Srenato } 300bb7278f1Srenato nbr->recv_seq = seq; 301bb7278f1Srenato } 302bb7278f1Srenato 30343509a12Srenato return (0); 30443509a12Srenato } 30543509a12Srenato 30643509a12Srenato static void 307*414c2f5bSrenato recv_packet_eigrp(int af, union eigrpd_addr *src, union eigrpd_addr *dest, 30843509a12Srenato struct iface *iface, struct eigrp_hdr *eigrp_hdr, char *buf, uint16_t len) 30943509a12Srenato { 31043509a12Srenato struct eigrp_iface *ei; 31143509a12Srenato struct nbr *nbr; 3123c8071b0Srenato struct tlv_parameter *tp = NULL; 3133c8071b0Srenato struct tlv_sw_version *tv = NULL; 3143c8071b0Srenato struct tlv_mcast_seq *tm = NULL; 31543509a12Srenato struct rinfo ri; 31643509a12Srenato struct rinfo_entry *re; 31743509a12Srenato struct seq_addr_head seq_addr_list; 31843509a12Srenato struct rinfo_head rinfo_list; 31943509a12Srenato 32043509a12Srenato /* EIGRP header sanity checks */ 32143509a12Srenato if (eigrp_hdr_sanity_check(af, dest, eigrp_hdr, len, iface) == -1) 32243509a12Srenato return; 32343509a12Srenato 32443509a12Srenato buf += sizeof(*eigrp_hdr); 32543509a12Srenato len -= sizeof(*eigrp_hdr); 32643509a12Srenato 32743509a12Srenato TAILQ_INIT(&seq_addr_list); 32843509a12Srenato TAILQ_INIT(&rinfo_list); 32943509a12Srenato while (len > 0) { 33043509a12Srenato struct tlv tlv; 3310d45ad68Srenato uint16_t tlv_type; 33243509a12Srenato 33343509a12Srenato if (len < sizeof(tlv)) { 33443509a12Srenato log_debug("%s: malformed packet (bad length)", 33543509a12Srenato __func__); 33643509a12Srenato goto error; 33743509a12Srenato } 33843509a12Srenato 33943509a12Srenato memcpy(&tlv, buf, sizeof(tlv)); 34043509a12Srenato if (ntohs(tlv.length) > len) { 34143509a12Srenato log_debug("%s: malformed packet (bad length)", 34243509a12Srenato __func__); 34343509a12Srenato goto error; 34443509a12Srenato } 34543509a12Srenato 3460d45ad68Srenato tlv_type = ntohs(tlv.type); 3470d45ad68Srenato switch (tlv_type) { 34843509a12Srenato case TLV_TYPE_PARAMETER: 34943509a12Srenato if ((tp = tlv_decode_parameter(&tlv, buf)) == NULL) 35043509a12Srenato goto error; 35143509a12Srenato break; 35243509a12Srenato case TLV_TYPE_SEQ: 353f90d6a5dSrenato if (tlv_decode_seq(af, &tlv, buf, &seq_addr_list) < 0) 35443509a12Srenato goto error; 35543509a12Srenato break; 35643509a12Srenato case TLV_TYPE_SW_VERSION: 35743509a12Srenato if ((tv = tlv_decode_sw_version(&tlv, buf)) == NULL) 35843509a12Srenato goto error; 35943509a12Srenato break; 36043509a12Srenato case TLV_TYPE_MCAST_SEQ: 36143509a12Srenato if ((tm = tlv_decode_mcast_seq(&tlv, buf)) == NULL) 36243509a12Srenato goto error; 36343509a12Srenato break; 36443509a12Srenato case TLV_TYPE_IPV4_INTERNAL: 36543509a12Srenato case TLV_TYPE_IPV4_EXTERNAL: 36643509a12Srenato case TLV_TYPE_IPV6_INTERNAL: 36743509a12Srenato case TLV_TYPE_IPV6_EXTERNAL: 368f90d6a5dSrenato /* silently ignore TLV from different address-family */ 3690d45ad68Srenato if ((tlv_type & TLV_PROTO_MASK) == TLV_PROTO_IPV4 && 3700d45ad68Srenato af != AF_INET) 371f90d6a5dSrenato break; 3720d45ad68Srenato if ((tlv_type & TLV_PROTO_MASK) == TLV_PROTO_IPV6 && 3730d45ad68Srenato af != AF_INET6) 374f90d6a5dSrenato break; 375f90d6a5dSrenato 3760d45ad68Srenato if (tlv_decode_route(af, &tlv, buf, &ri) < 0) 37743509a12Srenato goto error; 37843509a12Srenato if ((re = calloc(1, sizeof(*re))) == NULL) 379*414c2f5bSrenato fatal("recv_packet_eigrp"); 3803eb03b29Srenato re->rinfo = ri; 38143509a12Srenato TAILQ_INSERT_TAIL(&rinfo_list, re, entry); 38243509a12Srenato break; 38343509a12Srenato case TLV_TYPE_AUTH: 38443509a12Srenato case TLV_TYPE_PEER_TERM: 38543509a12Srenato /* 38643509a12Srenato * XXX There is no enough information in the draft 38743509a12Srenato * to implement these TLVs properly. 38843509a12Srenato */ 38943509a12Srenato case TLV_TYPE_IPV4_COMMUNITY: 39043509a12Srenato case TLV_TYPE_IPV6_COMMUNITY: 39143509a12Srenato /* TODO */ 39243509a12Srenato default: 39343509a12Srenato /* ignore unknown tlv */ 39443509a12Srenato break; 39543509a12Srenato } 39643509a12Srenato buf += ntohs(tlv.length); 39743509a12Srenato len -= ntohs(tlv.length); 39843509a12Srenato } 39943509a12Srenato 40043509a12Srenato ei = eigrp_if_lookup(iface, af, ntohs(eigrp_hdr->as)); 40143509a12Srenato if (ei == NULL || ei->passive) 40243509a12Srenato goto error; 40343509a12Srenato 40443509a12Srenato nbr = nbr_find(ei, src); 40543509a12Srenato if (nbr == NULL && (eigrp_hdr->opcode != EIGRP_OPC_HELLO || 40643509a12Srenato ntohl(eigrp_hdr->ack_num) != 0)) { 40743509a12Srenato log_debug("%s: unknown neighbor", __func__); 40843509a12Srenato goto error; 40943509a12Srenato } else if (nbr && recv_packet_nbr(nbr, eigrp_hdr, &seq_addr_list, 41043509a12Srenato tm) < 0) 41143509a12Srenato goto error; 41243509a12Srenato 41343509a12Srenato /* log packet being received */ 41443509a12Srenato if (eigrp_hdr->opcode != EIGRP_OPC_HELLO) 41543509a12Srenato log_debug("%s: type %s nbr %s AS %u seq %u ack %u", __func__, 41643509a12Srenato opcode_name(eigrp_hdr->opcode), log_addr(af, &nbr->addr), 41743509a12Srenato ei->eigrp->as, ntohl(eigrp_hdr->seq_num), 41843509a12Srenato ntohl(eigrp_hdr->ack_num)); 41943509a12Srenato 42043509a12Srenato /* switch EIGRP packet type */ 42143509a12Srenato switch (eigrp_hdr->opcode) { 42243509a12Srenato case EIGRP_OPC_HELLO: 423dcfaa8d4Srenato if (ntohl(eigrp_hdr->ack_num) == 0) { 42443509a12Srenato recv_hello(ei, src, nbr, tp); 425dcfaa8d4Srenato ei->eigrp->stats.hellos_recv++; 426dcfaa8d4Srenato } else 427dcfaa8d4Srenato ei->eigrp->stats.acks_recv++; 42843509a12Srenato break; 42943509a12Srenato case EIGRP_OPC_UPDATE: 43043509a12Srenato recv_update(nbr, &rinfo_list, ntohl(eigrp_hdr->flags)); 431dcfaa8d4Srenato ei->eigrp->stats.updates_recv++; 43243509a12Srenato break; 43343509a12Srenato case EIGRP_OPC_QUERY: 43443509a12Srenato recv_query(nbr, &rinfo_list, 0); 435dcfaa8d4Srenato ei->eigrp->stats.queries_recv++; 43643509a12Srenato break; 43743509a12Srenato case EIGRP_OPC_REPLY: 43843509a12Srenato recv_reply(nbr, &rinfo_list, 0); 439dcfaa8d4Srenato ei->eigrp->stats.replies_recv++; 44043509a12Srenato break; 44143509a12Srenato case EIGRP_OPC_SIAQUERY: 44243509a12Srenato recv_query(nbr, &rinfo_list, 1); 443dcfaa8d4Srenato ei->eigrp->stats.squeries_recv++; 44443509a12Srenato break; 44543509a12Srenato case EIGRP_OPC_SIAREPLY: 44643509a12Srenato recv_reply(nbr, &rinfo_list, 1); 447dcfaa8d4Srenato ei->eigrp->stats.sreplies_recv++; 44843509a12Srenato break; 44943509a12Srenato default: 45043509a12Srenato log_debug("%s: unknown EIGRP packet type, interface %s", 45143509a12Srenato __func__, iface->name); 45243509a12Srenato } 45343509a12Srenato 45443509a12Srenato error: 45543509a12Srenato /* free rinfo tlvs */ 45643509a12Srenato message_list_clr(&rinfo_list); 45743509a12Srenato /* free seq addresses tlvs */ 45843509a12Srenato seq_addr_list_clr(&seq_addr_list); 45943509a12Srenato } 46043509a12Srenato 461*414c2f5bSrenato #define CMSG_MAXLEN max(sizeof(struct sockaddr_dl), sizeof(struct in6_pktinfo)) 46243509a12Srenato void 463*414c2f5bSrenato recv_packet(int fd, short event, void *bula) 46443509a12Srenato { 46543509a12Srenato union { 46643509a12Srenato struct cmsghdr hdr; 467*414c2f5bSrenato char buf[CMSG_SPACE(CMSG_MAXLEN)]; 46843509a12Srenato } cmsgbuf; 46943509a12Srenato struct msghdr msg; 470*414c2f5bSrenato struct sockaddr_storage from; 47143509a12Srenato struct iovec iov; 47243509a12Srenato struct ip ip_hdr; 47343509a12Srenato char *buf; 47443509a12Srenato struct cmsghdr *cmsg; 47543509a12Srenato ssize_t r; 47643509a12Srenato uint16_t len; 477*414c2f5bSrenato int af; 47843509a12Srenato union eigrpd_addr src, dest; 479*414c2f5bSrenato unsigned int ifindex = 0; 480*414c2f5bSrenato struct iface *iface; 481*414c2f5bSrenato struct eigrp_hdr *eigrp_hdr; 48243509a12Srenato 48343509a12Srenato if (event != EV_READ) 48443509a12Srenato return; 48543509a12Srenato 48643509a12Srenato /* setup buffer */ 48743509a12Srenato memset(&msg, 0, sizeof(msg)); 48843509a12Srenato iov.iov_base = buf = pkt_ptr; 48943509a12Srenato iov.iov_len = READ_BUF_SIZE; 490*414c2f5bSrenato msg.msg_name = &from; 491*414c2f5bSrenato msg.msg_namelen = sizeof(from); 49243509a12Srenato msg.msg_iov = &iov; 49343509a12Srenato msg.msg_iovlen = 1; 49443509a12Srenato msg.msg_control = &cmsgbuf.buf; 49543509a12Srenato msg.msg_controllen = sizeof(cmsgbuf.buf); 49643509a12Srenato 49743509a12Srenato if ((r = recvmsg(fd, &msg, 0)) == -1) { 49843509a12Srenato if (errno != EAGAIN && errno != EINTR) 49943509a12Srenato log_debug("%s: read error: %s", __func__, 50043509a12Srenato strerror(errno)); 50143509a12Srenato return; 50243509a12Srenato } 503*414c2f5bSrenato len = (uint16_t)r; 504*414c2f5bSrenato 505*414c2f5bSrenato sa2addr((struct sockaddr *)&from, &af, &src); 506*414c2f5bSrenato if (bad_addr(af, &src)) { 507*414c2f5bSrenato log_debug("%s: invalid source address: %s", __func__, 508*414c2f5bSrenato log_addr(af, &src)); 509*414c2f5bSrenato return; 510*414c2f5bSrenato } 511*414c2f5bSrenato 51243509a12Srenato for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 51343509a12Srenato cmsg = CMSG_NXTHDR(&msg, cmsg)) { 514*414c2f5bSrenato if (af == AF_INET && cmsg->cmsg_level == IPPROTO_IP && 51543509a12Srenato cmsg->cmsg_type == IP_RECVIF) { 51643509a12Srenato ifindex = ((struct sockaddr_dl *) 51743509a12Srenato CMSG_DATA(cmsg))->sdl_index; 51843509a12Srenato break; 51943509a12Srenato } 520*414c2f5bSrenato if (af == AF_INET6 && cmsg->cmsg_level == IPPROTO_IPV6 && 52143509a12Srenato cmsg->cmsg_type == IPV6_PKTINFO) { 52243509a12Srenato ifindex = ((struct in6_pktinfo *) 52343509a12Srenato CMSG_DATA(cmsg))->ipi6_ifindex; 52443509a12Srenato dest.v6 = ((struct in6_pktinfo *) 52543509a12Srenato CMSG_DATA(cmsg))->ipi6_addr; 52643509a12Srenato break; 52743509a12Srenato } 52843509a12Srenato } 5296c428d2eSrenato 53043509a12Srenato /* find a matching interface */ 531*414c2f5bSrenato if ((iface = find_iface(ifindex, af, &src)) == NULL) 53243509a12Srenato return; 53343509a12Srenato 534*414c2f5bSrenato /* the IPv4 raw sockets API gives us direct access to the IP header */ 535*414c2f5bSrenato if (af == AF_INET) { 536*414c2f5bSrenato if (len < sizeof(ip_hdr)) { 537*414c2f5bSrenato log_debug("%s: bad packet size", __func__); 538*414c2f5bSrenato return; 539*414c2f5bSrenato } 540*414c2f5bSrenato memcpy(&ip_hdr, buf, sizeof(ip_hdr)); 541*414c2f5bSrenato if (ntohs(ip_hdr.ip_len) != len) { 542*414c2f5bSrenato log_debug("%s: invalid IP packet length %u", __func__, 543*414c2f5bSrenato ntohs(ip_hdr.ip_len)); 544*414c2f5bSrenato return; 545*414c2f5bSrenato } 546*414c2f5bSrenato buf += ip_hdr.ip_hl << 2; 547*414c2f5bSrenato len -= ip_hdr.ip_hl << 2; 548*414c2f5bSrenato dest.v4 = ip_hdr.ip_dst; 549*414c2f5bSrenato } 550*414c2f5bSrenato 551*414c2f5bSrenato /* validate destination address */ 552*414c2f5bSrenato switch (af) { 553*414c2f5bSrenato case AF_INET: 55443509a12Srenato /* 555*414c2f5bSrenato * Packet needs to be sent to 224.0.0.10 or to one of the 556*414c2f5bSrenato * interface addresses. 557*414c2f5bSrenato */ 558*414c2f5bSrenato if (dest.v4.s_addr != global.mcast_addr_v4.s_addr) { 559*414c2f5bSrenato struct if_addr *if_addr; 560*414c2f5bSrenato int found = 0; 561*414c2f5bSrenato 562*414c2f5bSrenato TAILQ_FOREACH(if_addr, &iface->addr_list, entry) 563*414c2f5bSrenato if (if_addr->af == AF_INET && 564*414c2f5bSrenato dest.v4.s_addr == if_addr->addr.v4.s_addr) { 565*414c2f5bSrenato found = 1; 566*414c2f5bSrenato break; 567*414c2f5bSrenato } 568*414c2f5bSrenato if (found == 0) { 569*414c2f5bSrenato log_debug("%s: packet sent to wrong address " 570*414c2f5bSrenato "%s, interface %s", __func__, 571*414c2f5bSrenato inet_ntoa(dest.v4), iface->name); 572*414c2f5bSrenato return; 573*414c2f5bSrenato } 574*414c2f5bSrenato } 575*414c2f5bSrenato break; 576*414c2f5bSrenato case AF_INET6: 577*414c2f5bSrenato /* 578*414c2f5bSrenato * Packet needs to be sent to ff02::a or to the link local 579*414c2f5bSrenato * address of the interface. 58043509a12Srenato */ 581e14e95d7Srenato if (!IN6_ARE_ADDR_EQUAL(&dest.v6, &global.mcast_addr_v6) && 58243509a12Srenato !IN6_ARE_ADDR_EQUAL(&dest.v6, &iface->linklocal)) { 583*414c2f5bSrenato log_debug("%s: packet sent to wrong address %s, " 584*414c2f5bSrenato "interface %s", __func__, log_in6addr(&dest.v6), 585*414c2f5bSrenato iface->name); 58643509a12Srenato return; 58743509a12Srenato } 588*414c2f5bSrenato break; 589*414c2f5bSrenato default: 590*414c2f5bSrenato fatalx("recv_packet: unknown af"); 591*414c2f5bSrenato break; 592*414c2f5bSrenato } 59343509a12Srenato 59443509a12Srenato if (len < sizeof(*eigrp_hdr)) { 59543509a12Srenato log_debug("%s: bad packet size", __func__); 59643509a12Srenato return; 59743509a12Srenato } 59843509a12Srenato eigrp_hdr = (struct eigrp_hdr *)buf; 59943509a12Srenato 600*414c2f5bSrenato recv_packet_eigrp(af, &src, &dest, iface, eigrp_hdr, buf, len); 60143509a12Srenato } 60243509a12Srenato 60343509a12Srenato int 60443509a12Srenato eigrp_hdr_sanity_check(int af, union eigrpd_addr *addr, 60543509a12Srenato struct eigrp_hdr *eigrp_hdr, uint16_t len, const struct iface *iface) 60643509a12Srenato { 60743509a12Srenato if (in_cksum(eigrp_hdr, len)) { 60843509a12Srenato log_debug("%s: invalid checksum, interface %s", __func__, 60943509a12Srenato iface->name); 61043509a12Srenato return (-1); 61143509a12Srenato } 61243509a12Srenato 61343509a12Srenato if (eigrp_hdr->version != EIGRP_HEADER_VERSION) { 61443509a12Srenato log_debug("%s: invalid EIGRP version %d, interface %s", 61543509a12Srenato __func__, eigrp_hdr->version, iface->name); 61643509a12Srenato return (-1); 61743509a12Srenato } 61843509a12Srenato 61943509a12Srenato if (ntohs(eigrp_hdr->vrid) != EIGRP_VRID_UNICAST_AF) { 62043509a12Srenato log_debug("%s: unknown or unsupported vrid %u, interface %s", 62143509a12Srenato __func__, ntohs(eigrp_hdr->vrid), iface->name); 62243509a12Srenato return (-1); 62343509a12Srenato } 62443509a12Srenato 62543509a12Srenato if (eigrp_hdr->opcode == EIGRP_OPC_HELLO && 62643509a12Srenato eigrp_hdr->ack_num != 0) { 62743509a12Srenato switch (af) { 62843509a12Srenato case AF_INET: 62943509a12Srenato if (IN_MULTICAST(addr->v4.s_addr)) { 63043509a12Srenato log_debug("%s: multicast ack (ipv4), " 63143509a12Srenato "interface %s", __func__, iface->name); 63243509a12Srenato return (-1); 63343509a12Srenato } 63443509a12Srenato break; 63543509a12Srenato case AF_INET6: 63643509a12Srenato if (IN6_IS_ADDR_MULTICAST(&addr->v6)) { 63743509a12Srenato log_debug("%s: multicast ack (ipv6), " 63843509a12Srenato "interface %s", __func__, iface->name); 63943509a12Srenato return (-1); 64043509a12Srenato } 64143509a12Srenato break; 64243509a12Srenato default: 6433c8071b0Srenato fatalx("eigrp_hdr_sanity_check: unknown af"); 64443509a12Srenato } 64543509a12Srenato } 64643509a12Srenato 64743509a12Srenato return (0); 64843509a12Srenato } 64943509a12Srenato 65043509a12Srenato struct iface * 65143509a12Srenato find_iface(unsigned int ifindex, int af, union eigrpd_addr *src) 65243509a12Srenato { 65343509a12Srenato struct iface *iface; 65443509a12Srenato struct if_addr *if_addr; 65544faa115Srenato in_addr_t mask; 65643509a12Srenato 65743509a12Srenato iface = if_lookup(econf, ifindex); 65843509a12Srenato if (iface == NULL) 65943509a12Srenato return (NULL); 66043509a12Srenato 66143509a12Srenato switch (af) { 66243509a12Srenato case AF_INET: 66343509a12Srenato /* 66443509a12Srenato * From CCNP ROUTE 642-902 OCG: 66543509a12Srenato * "EIGRP's rules about neighbor IP addresses being in the same 66643509a12Srenato * subnet are less exact than OSPF. OSPF requires matching 66743509a12Srenato * subnet numbers and masks. EIGRP just asks the question of 66843509a12Srenato * whether the neighbor's IP address is in the range of 66943509a12Srenato * addresses for the subnet as known to the local router." 67043509a12Srenato */ 67143509a12Srenato TAILQ_FOREACH(if_addr, &iface->addr_list, entry) { 67246ee0129Srenato if (if_addr->af == AF_INET) { 67343509a12Srenato mask = prefixlen2mask(if_addr->prefixlen); 67446ee0129Srenato 67543509a12Srenato if ((if_addr->addr.v4.s_addr & mask) == 67643509a12Srenato (src->v4.s_addr & mask)) 67743509a12Srenato return (iface); 67843509a12Srenato } 67946ee0129Srenato } 68043509a12Srenato break; 68143509a12Srenato case AF_INET6: 68243509a12Srenato /* 68343509a12Srenato * draft-savage-eigrp-04 - Section 10.1: 68443509a12Srenato * "EIGRP IPv6 will check that a received HELLO contains a valid 68543509a12Srenato * IPv6 link-local source address." 68643509a12Srenato */ 68743509a12Srenato if (IN6_IS_ADDR_LINKLOCAL(&src->v6)) 68843509a12Srenato return (iface); 68943509a12Srenato break; 69043509a12Srenato default: 6913c8071b0Srenato fatalx("find_iface: unknown af"); 69243509a12Srenato } 69343509a12Srenato 69443509a12Srenato return (NULL); 69543509a12Srenato } 696