diff --git a/nest/iface.h b/nest/iface.h index a982c17f..b7c3c19c 100644 --- a/nest/iface.h +++ b/nest/iface.h @@ -82,6 +82,15 @@ struct iface *if_find_by_index(unsigned); struct iface *if_find_by_name(char *); void ifa_recalc_all_primary_addresses(void); +static inline int +ifa_match_addr(struct ifa *ifa, ip_addr addr) +{ + if (ifa->flags & IA_UNNUMBERED) + return ipa_equal(addr, ifa->opposite); + else + return ipa_in_net(addr, ifa->prefix, ifa->pxlen); +} + /* The Neighbor Cache */ typedef struct neighbor { diff --git a/proto/ospf/dbdes.c b/proto/ospf/dbdes.c index 9f126821..8cd7c8b2 100644 --- a/proto/ospf/dbdes.c +++ b/proto/ospf/dbdes.c @@ -93,7 +93,7 @@ ospf_dbdes_send(struct ospf_neighbor *n, int next) { case NEIGHBOR_EXSTART: /* Send empty packets */ n->myimms.bit.i = 1; - pkt = ospf_tx_buffer(); + pkt = ospf_tx_buffer(ifa); op = &pkt->ospf_packet; ospf_pkt_fill_hdr(ifa, pkt, DBDES_P); pkt->iface_mtu = htons(ifa->iface->mtu); @@ -185,7 +185,7 @@ ospf_dbdes_send(struct ospf_neighbor *n, int next) } /* Copy last sent packet again */ - pkt = ospf_tx_buffer(); + pkt = ospf_tx_buffer(ifa); memcpy(pkt, n->ldbdes, length); OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet sent to %I via %s", n->ip, ifa->iface->name); diff --git a/proto/ospf/hello.c b/proto/ospf/hello.c index eaf7d230..9f174df8 100644 --- a/proto/ospf/hello.c +++ b/proto/ospf/hello.c @@ -258,7 +258,7 @@ ospf_hello_send(timer *timer, int poll, struct ospf_neighbor *dirn) p->name, ifa->iface->name); /* Now we should send a hello packet */ - pkt = ospf_tx_buffer(); + pkt = ospf_tx_buffer(ifa); op = &pkt->ospf_packet; /* Now fill ospf_hello header */ diff --git a/proto/ospf/iface.c b/proto/ospf/iface.c index 3f1e74dc..a99ceb12 100644 --- a/proto/ospf/iface.c +++ b/proto/ospf/iface.c @@ -82,9 +82,6 @@ ospf_sk_open(struct ospf_iface *ifa, int mc) #endif sk->tos = IP_PREC_INTERNET_CONTROL; - sk->ttl = 1; - if (ifa->type == OSPF_IT_VLINK) - sk->ttl = 255; sk->rx_hook = ospf_rx_hook; sk->tx_hook = ospf_tx_hook; sk->err_hook = ospf_err_hook; @@ -191,10 +188,12 @@ ospf_iface_chstate(struct ospf_iface *ifa, u8 state) OSPF_TRACE(D_EVENTS, "Changing state of virtual link %R from \"%s\" into \"%s\".", ifa->vid, ospf_is[oldstate], ospf_is[state]); + /* if (state == OSPF_IS_PTP) ospf_sk_open(ifa, 0); if (state == OSPF_IS_DOWN) ospf_sk_close(ifa); + */ } else { diff --git a/proto/ospf/lsack.c b/proto/ospf/lsack.c index 73d79a27..c05f0196 100644 --- a/proto/ospf/lsack.c +++ b/proto/ospf/lsack.c @@ -66,7 +66,7 @@ ospf_lsack_send(struct ospf_neighbor *n, int queue) if (EMPTY_LIST(n->ackl[queue])) return; - pk = ospf_tx_buffer(); + pk = ospf_tx_buffer(ifa); op = &pk->ospf_packet; ospf_pkt_fill_hdr(n->ifa, pk, LSACK_P); diff --git a/proto/ospf/lsreq.c b/proto/ospf/lsreq.c index b61e0ce5..1ba4fff9 100644 --- a/proto/ospf/lsreq.c +++ b/proto/ospf/lsreq.c @@ -44,7 +44,7 @@ ospf_lsreq_send(struct ospf_neighbor *n) int i, j; struct proto *p = &n->ifa->oa->po->proto; - pk = ospf_tx_buffer(); + pk = ospf_tx_buffer(n->ifa); op = &pk->ospf_packet; ospf_pkt_fill_hdr(n->ifa, pk, LSREQ_P); diff --git a/proto/ospf/lsupd.c b/proto/ospf/lsupd.c index 133a036d..39d91b9a 100644 --- a/proto/ospf/lsupd.c +++ b/proto/ospf/lsupd.c @@ -269,7 +269,7 @@ ospf_lsupd_flood(struct proto_ospf *po, struct ospf_packet *op; struct ospf_lsa_header *lh; - pk = ospf_tx_buffer(); + pk = ospf_tx_buffer(ifa); op = &pk->ospf_packet; ospf_pkt_fill_hdr(ifa, pk, LSUPD_P); @@ -350,7 +350,7 @@ ospf_lsupd_send_list(struct ospf_neighbor *n, list * l) DBG("LSupd: 1st packet\n"); - pk= ospf_tx_buffer(); + pk= ospf_tx_buffer(n->ifa); op = &pk->ospf_packet; ospf_pkt_fill_hdr(n->ifa, pk, LSUPD_P); diff --git a/proto/ospf/packet.c b/proto/ospf/packet.c index 81686d25..6697057b 100644 --- a/proto/ospf/packet.c +++ b/proto/ospf/packet.c @@ -255,55 +255,6 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_ #endif -static struct ospf_iface * -find_matching_iface(struct ospf_packet *ps, unsigned ifindex, ip_addr faddr) -{ - u32 areaid = ntohl(ps->areaid); - u32 rid = ntohl(ps->routerid); - node *nd; - - /* First, we will try to match real ifaces */ - - WALK_LIST(nd, ospf_ifaces) - { - struct ospf_iface *ifa = SKIP_BACK(struct ospf_iface, sk_node, nd); - struct ifa *addr = ifa->addr; - - if ((ifa->iface->index == ifindex) && - (ifa->oa->areaid == areaid) && -#ifdef OSPFv2 - ((addr->flags & IA_UNNUMBERED) ? - ipa_equal(faddr, addr->opposite) : - ipa_in_net(faddr, addr->prefix, addr->pxlen)) -#else /* OSPFv3 */ - (ifa->instance_id == ps->instance_id) -#endif - ) - return ifa; - } - - /* Second, we will try to match vlinks */ - - if (areaid != 0) - return NULL; - - /* FIXME: There should be some more checks to distinquish parallel - vlinks to the same ABR. */ - - WALK_LIST(nd, ospf_vlinks) - { - struct ospf_iface *ifa = SKIP_BACK(struct ospf_iface, sk_node, nd); - - if ((ifa->vid == rid) -#ifdef OSPFv3 - && (ifa->instance_id == ps->instance_id) -#endif - ) - return ifa; - } - - return NULL; -} /** * ospf_rx_hook @@ -319,9 +270,47 @@ ospf_rx_hook(sock *sk, int size) { char *mesg = "OSPF: Bad packet from "; - DBG("OSPF: RX_Hook called (from %I)\n", sk->faddr); + /* We want just packets from sk->iface. Unfortunately, on BSD we + cannot filter out other packets at kernel level and we receive + all packets on all sockets */ + if (sk->lifindex != sk->iface->index) + return 1; - /* First, we check packet size, checksum, and the protocol version */ + DBG("OSPF: RX hook called (iface %s, src %I, dst %I)\n", + sk->iface->name, sk->faddr, sk->laddr); + + /* Initially, the packet is associated with the 'master' iface */ + struct ospf_iface *ifa = sk->data; + struct proto_ospf *po = ifa->oa->po; + struct proto *p = &po->proto; + + int src_local = ifa_match_addr(ifa->addr, sk->faddr); + int dst_local = ipa_equal(sk->laddr, ifa->addr->ip); + int dst_mcast = ipa_equal(sk->laddr, AllSPFRouters) || ipa_equal(sk->laddr, AllDRouters); + +#ifdef OSPFv2 + /* First, we eliminate packets with strange address combinations. + * In OSPFv2, they might be for other ospf_ifaces (with different IP + * prefix) on the same real iface, so we don't log it. We enforce + * that (src_local || dst_local), therefore we are eliminating all + * such cases. + */ + if (dst_mcast && !src_local) + return 1; + if (!dst_mcast && !dst_local) + return 1; + +#else /* OSPFv3 */ + + /* In OSPFv3, src_local and dst_local mean link-local. + * RFC 5340 says that local (non-vlink) packets use + * link-local src address, but does not enforce it. Strange. + */ + if (dst_mcast && !src_local) + log(L_WARN "OSPF: Received multicast packet from %I (not link-local)", sk->faddr); +#endif + + /* Second, we check packet size, checksum, and the protocol version */ struct ospf_packet *ps = (struct ospf_packet *) ip_skip_header(sk->rbuf, &size); if (ps == NULL) @@ -366,38 +355,78 @@ ospf_rx_hook(sock *sk, int size) #endif - /* Now, we would like to associate the packet with an OSPF iface */ - struct ospf_iface *ifa = find_matching_iface(ps, sk->lifindex, sk->faddr); - if (ifa == NULL) + /* Third, we resolve associated iface and handle vlinks. */ + + u32 areaid = ntohl(ps->areaid); + u32 rid = ntohl(ps->routerid); + + if ((areaid == ifa->oa->areaid) +#ifdef OSPFv3 + && (ps->instance_id != ifa->instance_id) +#endif + ) { - /* We limit logging of unmatched packets as it may be perfectly OK */ - if (unmatched_count < 8) + /* It is real iface, source should be local (in OSPFv2) */ +#ifdef OSPFv2 + if (!src_local) + return 1; +#endif + } + else if (dst_mcast || (areaid != 0)) + { + /* Obvious mismatch */ + +#ifdef OSPFv2 + /* We ignore mismatch in OSPFv3, because there might be + other instance with different instance ID */ + log(L_ERR "%s%I - area does not match (%R vs %R)", + mesg, sk->faddr, areaid, ifa->oa->areaid); +#endif + return 1; + } + else + { + /* Some vlink? */ + struct ospf_iface *iff = NULL; + + WALK_LIST(iff, po->iface_list) { - struct iface *ifc = if_find_by_index(sk->lifindex); - log(L_WARN "OSPF: Received unmatched packet (src %I, iface %s, rtid %R, area %R)", - sk->faddr, ifc ? ifc->name : "?", ntohl(ps->routerid), ntohl(ps->areaid)); - unmatched_count++; + if ((iff->type == OSPF_IT_VLINK) && + (iff->voa == ifa->oa) && +#ifdef OSPFv3 + (iff->instance_id == ps->instance_id) && +#endif + (iff->vid == rid)) + { + /* Vlink should be UP */ + if (iff->state != OSPF_IS_PTP) + return 1; + + ifa = iff; + goto found; + } } +#ifdef OSPFv2 + log(L_WARN "OSPF: Received packet for uknown vlink (ID %R, IP %I)", rid, sk->faddr); +#endif return 1; } - struct proto_ospf *po = ifa->oa->po; - struct proto *p = &po->proto; - + found: if (ifa->stub) /* This shouldn't happen */ return 1; if (ipa_equal(sk->laddr, AllDRouters) && (ifa->sk_dr == 0)) return 1; - if (ntohl(ps->routerid) == po->router_id) + if (rid == po->router_id) { log(L_ERR "%s%I - received my own router ID!", mesg, sk->faddr); return 1; } - if (ntohl(ps->routerid) == 0) + if (rid == 0) { log(L_ERR "%s%I - router id = 0.0.0.0", mesg, sk->faddr); return 1; @@ -406,7 +435,7 @@ ospf_rx_hook(sock *sk, int size) /* This is deviation from RFC 2328 - neighbours should be identified by * IP address on broadcast and NBMA networks. */ - struct ospf_neighbor *n = find_neigh(ifa, ntohl(ps->routerid)); + struct ospf_neighbor *n = find_neigh(ifa, rid); if(!n && (ps->type != HELLO_P)) { diff --git a/proto/ospf/packet.h b/proto/ospf/packet.h index 053aba3b..1c74a703 100644 --- a/proto/ospf/packet.h +++ b/proto/ospf/packet.h @@ -19,6 +19,6 @@ void ospf_send_to_agt(struct ospf_iface *ifa, u8 state); void ospf_send_to_bdr(struct ospf_iface *ifa); void ospf_send_to(struct ospf_iface *ifa, ip_addr ip); -static inline void * ospf_tx_buffer() { return ospf_socket->tbuf; } +static inline void * ospf_tx_buffer(struct ospf_iface *ifa) { return ifa->sk->tbuf; } #endif /* _BIRD_OSPF_PACKET_H_ */