void ospf_lsack_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa, struct ospf_neighbor *n) { struct proto *p = &ifa->oa->po->proto; struct ospf_lsa_header lsa; struct top_hash_entry *en; unsigned int i, lsano; unsigned int size = ntohs(ps_i->length); if (size < sizeof(struct ospf_lsack_packet)) { log(L_ERR "Bad OSPF LSACK packet from %I - too short (%u B)", n->ip, size); return; } struct ospf_lsack_packet *ps = (void *) ps_i; OSPF_PACKET(ospf_dump_lsack, ps, "LSACK packet received from %I via %s", n->ip, ifa->iface->name); ospf_neigh_sm(n, INM_HELLOREC); if (n->state < NEIGHBOR_EXCHANGE) return; lsano = (size - sizeof(struct ospf_lsack_packet)) / sizeof(struct ospf_lsa_header); for (i = 0; i < lsano; i++) { ntohlsah(ps->lsh + i, &lsa); u32 dom = ospf_lsa_domain(lsa.type, n->ifa); if ((en = ospf_hash_find_header(n->lsrth, dom, &lsa)) == NULL) continue; /* pg 155 */ if (lsa_comp(&lsa, &en->lsa) != CMP_SAME) /* pg 156 */ { if ((lsa.sn == LSA_MAXSEQNO) && (lsa.age == LSA_MAXAGE)) continue; OSPF_TRACE(D_PACKETS, "Strange LSACK from %I", n->ip); OSPF_TRACE(D_PACKETS, "Type: %04x, Id: %R, Rt: %R", lsa.type, lsa.id, lsa.rt); OSPF_TRACE(D_PACKETS, "I have: Age: %4u, Seq: %08x, Sum: %04x", en->lsa.age, en->lsa.sn, en->lsa.checksum); OSPF_TRACE(D_PACKETS, "He has: Age: %4u, Seq: %08x, Sum: %04x", lsa.age, lsa.sn, lsa.checksum); continue; } DBG("Deleting LS Id: %R RT: %R Type: %u from LS Retl for neighbor %R\n", lsa.id, lsa.rt, lsa.type, n->rid); s_rem_node(SNODE en); if (en->lsa_body != NULL) mb_free(en->lsa_body); en->lsa_body = NULL; ospf_hash_delete(n->lsrth, en); } }
static void ospf_do_send_dbdes(struct ospf_proto *p, struct ospf_neighbor *n) { struct ospf_iface *ifa = n->ifa; OSPF_PACKET(ospf_dump_dbdes, n->ldd_buffer, "DBDES packet sent to nbr %R on %s", n->rid, ifa->ifname); sk_set_tbuf(ifa->sk, n->ldd_buffer); ospf_send_to(ifa, n->ip); sk_set_tbuf(ifa->sk, NULL); }
void ospf_receive_dbdes(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n) { struct ospf_proto *p = ifa->oa->po; const char *err_dsc = NULL; u32 rcv_ddseq, rcv_options; u16 rcv_iface_mtu; u8 rcv_imms; uint plen, err_val = 0; /* RFC 2328 10.6 */ plen = ntohs(pkt->length); if (plen < ospf_dbdes_hdrlen(p)) { LOG_PKT("Bad DBDES packet from nbr %R on %s - %s (%u)", n->rid, ifa->ifname, "too short", plen); return; } OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet received from nbr %R on %s", n->rid, ifa->ifname); ospf_neigh_sm(n, INM_HELLOREC); if (ospf_is_v2(p)) { struct ospf_dbdes2_packet *ps = (void *) pkt; rcv_iface_mtu = ntohs(ps->iface_mtu); rcv_options = ps->options; rcv_imms = ps->imms; rcv_ddseq = ntohl(ps->ddseq); } else /* OSPFv3 */ { struct ospf_dbdes3_packet *ps = (void *) pkt; rcv_options = ntohl(ps->options); rcv_iface_mtu = ntohs(ps->iface_mtu); rcv_imms = ps->imms; rcv_ddseq = ntohl(ps->ddseq); } switch (n->state) { case NEIGHBOR_DOWN: case NEIGHBOR_ATTEMPT: case NEIGHBOR_2WAY: OSPF_TRACE(D_PACKETS, "DBDES packet ignored - lesser state than ExStart"); return; case NEIGHBOR_INIT: ospf_neigh_sm(n, INM_2WAYREC); if (n->state != NEIGHBOR_EXSTART) return; case NEIGHBOR_EXSTART: if ((ifa->type != OSPF_IT_VLINK) && (rcv_iface_mtu != ifa->iface->mtu) && (rcv_iface_mtu != 0) && (ifa->iface->mtu != 0)) LOG_PKT_WARN("MTU mismatch with nbr %R on %s (remote %d, local %d)", n->rid, ifa->ifname, rcv_iface_mtu, ifa->iface->mtu); if ((rcv_imms == DBDES_IMMS) && (n->rid > p->router_id) && (plen == ospf_dbdes_hdrlen(p))) { /* I'm slave! */ n->dds = rcv_ddseq; n->ddr = rcv_ddseq; n->options = rcv_options; n->myimms &= ~DBDES_MS; n->imms = rcv_imms; tm_stop(n->dbdes_timer); ospf_neigh_sm(n, INM_NEGDONE); ospf_send_dbdes(p, n); break; } if (!(rcv_imms & DBDES_I) && !(rcv_imms & DBDES_MS) && (n->rid < p->router_id) && (n->dds == rcv_ddseq)) { /* I'm master! */ n->options = rcv_options; n->ddr = rcv_ddseq - 1; /* It will be set corectly a few lines down */ n->imms = rcv_imms; ospf_neigh_sm(n, INM_NEGDONE); /* Continue to the NEIGHBOR_EXCHANGE case */ } else { DBG("%s: Nothing happend to %I (imms=%d)\n", p->name, n->ip, rcv_imms); break; } case NEIGHBOR_EXCHANGE: if ((rcv_imms == n->imms) && (rcv_options == n->options) && (rcv_ddseq == n->ddr)) goto duplicate; if ((rcv_imms & DBDES_MS) != (n->imms & DBDES_MS)) DROP("MS-bit mismatch", rcv_imms); if (rcv_imms & DBDES_I) DROP("I-bit mismatch", rcv_imms); if (rcv_options != n->options) DROP("options mismatch", rcv_options); n->ddr = rcv_ddseq; n->imms = rcv_imms; if (n->myimms & DBDES_MS) { /* MASTER */ if (rcv_ddseq != n->dds) DROP("DD sequence number mismatch", rcv_ddseq); n->dds++; if (ospf_process_dbdes(p, pkt, n) < 0) return; if (!(n->myimms & DBDES_M) && !(n->imms & DBDES_M)) { tm_stop(n->dbdes_timer); ospf_neigh_sm(n, INM_EXDONE); break; } ospf_send_dbdes(p, n); tm_start(n->dbdes_timer, n->ifa->rxmtint); } else { /* SLAVE */ if (rcv_ddseq != (n->dds + 1)) DROP("DD sequence number mismatch", rcv_ddseq); n->ddr = rcv_ddseq; n->dds = rcv_ddseq; if (ospf_process_dbdes(p, pkt, n) < 0) return; ospf_send_dbdes(p, n); if (!(n->myimms & DBDES_M) && !(n->imms & DBDES_M)) ospf_neigh_sm(n, INM_EXDONE); } break; case NEIGHBOR_LOADING: case NEIGHBOR_FULL: if ((rcv_imms == n->imms) && (rcv_options == n->options) && (rcv_ddseq == n->ddr)) goto duplicate; DROP("too late for DD exchange", n->state); default: bug("Undefined interface state"); } return; duplicate: OSPF_TRACE(D_PACKETS, "DBDES packet is duplicate"); /* Slave should retransmit DBDES packet */ if (!(n->myimms & DBDES_MS)) ospf_rxmt_dbdes(p, n); return; drop: LOG_PKT("Bad DBDES packet from nbr %R on %s - %s (%u)", n->rid, ifa->ifname, err_dsc, err_val); ospf_neigh_sm(n, INM_SEQMIS); return; }
void ospf_lsack_send(struct ospf_neighbor *n, int queue) { struct ospf_packet *op; struct ospf_lsack_packet *pk; u16 len, i = 0; struct ospf_lsa_header *h; struct lsah_n *no; struct ospf_iface *ifa = n->ifa; struct proto *p = &n->ifa->oa->po->proto; if (EMPTY_LIST(n->ackl[queue])) return; pk = (struct ospf_lsack_packet *) ifa->sk->tbuf; op = (struct ospf_packet *) ifa->sk->tbuf; ospf_pkt_fill_hdr(n->ifa, pk, LSACK_P); h = pk->lsh; while (!EMPTY_LIST(n->ackl[queue])) { no = (struct lsah_n *) HEAD(n->ackl[queue]); memcpy(h + i, &no->lsa, sizeof(struct ospf_lsa_header)); DBG("Iter %u ID: %R, RT: %R, Type: %04x\n", i, ntohl((h + i)->id), ntohl((h + i)->rt), (h + i)->type); i++; rem_node(NODE no); mb_free(no); if ((i * sizeof(struct ospf_lsa_header) + sizeof(struct ospf_lsack_packet)) > ospf_pkt_maxsize(n->ifa)) { if (!EMPTY_LIST(n->ackl[queue])) { len = sizeof(struct ospf_lsack_packet) + i * sizeof(struct ospf_lsa_header); op->length = htons(len); DBG("Sending and continuing! Len=%u\n", len); OSPF_PACKET(ospf_dump_lsack, (struct ospf_lsack_packet *) ifa->sk->tbuf, "LSACK packet sent via %s", ifa->iface->name); if (ifa->type == OSPF_IT_BCAST) { if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP)) ospf_send_to(ifa, AllSPFRouters); else ospf_send_to(ifa, AllDRouters); } else { if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP)) ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE); else ospf_send_to_bdr(ifa); } ospf_pkt_fill_hdr(n->ifa, pk, LSACK_P); i = 0; } } } len = sizeof(struct ospf_lsack_packet) + i * sizeof(struct ospf_lsa_header); op->length = htons(len); DBG("Sending! Len=%u\n", len); OSPF_PACKET(ospf_dump_lsack, (struct ospf_lsack_packet *) ifa->sk->tbuf, "LSACK packet sent via %s", ifa->iface->name); if (ifa->type == OSPF_IT_BCAST) { if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP)) ospf_send_to(ifa, AllSPFRouters); else ospf_send_to(ifa, AllDRouters); } else ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE); }
/** * ospf_dbdes_send - transmit database description packet * @n: neighbor * @next: whether to send a next packet in a sequence (1) or to retransmit the old one (0) * * Sending of a database description packet is described in 10.8 of RFC 2328. * Reception of each packet is acknowledged in the sequence number of another. * When I send a packet to a neighbor I keep a copy in a buffer. If the neighbor * does not reply, I don't create a new packet but just send the content * of the buffer. */ void ospf_dbdes_send(struct ospf_neighbor *n, int next) { struct ospf_dbdes_packet *pkt; struct ospf_packet *op; struct ospf_iface *ifa = n->ifa; struct ospf_area *oa = ifa->oa; struct proto_ospf *po = oa->po; struct proto *p = &po->proto; u16 length, i, j; /* FIXME ??? */ if ((oa->rt == NULL) || (EMPTY_LIST(po->lsal))) update_rt_lsa(oa); switch (n->state) { case NEIGHBOR_EXSTART: /* Send empty packets */ n->myimms.bit.i = 1; pkt = ospf_tx_buffer(ifa); op = &pkt->ospf_packet; ospf_pkt_fill_hdr(ifa, pkt, DBDES_P); pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu); pkt->options = hton_opt(oa->options); pkt->imms = n->myimms; pkt->ddseq = htonl(n->dds); length = sizeof(struct ospf_dbdes_packet); op->length = htons(length); OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet sent to %I via %s", n->ip, ifa->iface->name); ospf_send_to(ifa, n->ip); break; case NEIGHBOR_EXCHANGE: n->myimms.bit.i = 0; if (next) { snode *sn; struct ospf_lsa_header *lsa; pkt = n->ldbdes; op = (struct ospf_packet *) pkt; ospf_pkt_fill_hdr(ifa, pkt, DBDES_P); pkt->iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : htons(ifa->iface->mtu); pkt->ddseq = htonl(n->dds); pkt->options = hton_opt(oa->options); j = i = (ospf_pkt_maxsize(ifa) - sizeof(struct ospf_dbdes_packet)) / sizeof(struct ospf_lsa_header); /* Number of possible lsaheaders to send */ lsa = (n->ldbdes + sizeof(struct ospf_dbdes_packet)); if (n->myimms.bit.m) { sn = s_get(&(n->dbsi)); DBG("Number of LSA: %d\n", j); for (; i > 0; i--) { struct top_hash_entry *en= (struct top_hash_entry *) sn; if (ospf_lsa_flooding_allowed(&en->lsa, en->domain, ifa)) { htonlsah(&(en->lsa), lsa); DBG("Working on: %d\n", i); DBG("\tX%01x %-1R %-1R %p\n", en->lsa.type, en->lsa.id, en->lsa.rt, en->lsa_body); lsa++; } else i++; /* No lsa added */ if (sn == STAIL(po->lsal)) { i--; break; } sn = sn->next; } if (sn == STAIL(po->lsal)) { DBG("Number of LSA NOT sent: %d\n", i); DBG("M bit unset.\n"); n->myimms.bit.m = 0; /* Unset more bit */ } s_put(&(n->dbsi), sn); } pkt->imms.byte = n->myimms.byte; length = (j - i) * sizeof(struct ospf_lsa_header) + sizeof(struct ospf_dbdes_packet); op->length = htons(length); DBG("%s: DB_DES (M) prepared for %I.\n", p->name, n->ip); } case NEIGHBOR_LOADING: case NEIGHBOR_FULL: length = ntohs(((struct ospf_packet *) n->ldbdes)->length); if (!length) { OSPF_TRACE(D_PACKETS, "No packet in my buffer for repeating"); ospf_neigh_sm(n, INM_KILLNBR); return; } /* Copy last sent packet again */ 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); ospf_send_to(ifa, n->ip); if(n->myimms.bit.ms) tm_start(n->rxmt_timer, n->ifa->rxmtint); /* Restart timer */ if (!n->myimms.bit.ms) { if ((n->myimms.bit.m == 0) && (n->imms.bit.m == 0) && (n->state == NEIGHBOR_EXCHANGE)) { ospf_neigh_sm(n, INM_EXDONE); } } break; default: /* Ignore it */ break; } }
void ospf_dbdes_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa, struct ospf_neighbor *n) { struct proto_ospf *po = ifa->oa->po; struct proto *p = &po->proto; unsigned int size = ntohs(ps_i->length); if (size < sizeof(struct ospf_dbdes_packet)) { log(L_ERR "Bad OSPF DBDES packet from %I - too short (%u B)", n->ip, size); return; } struct ospf_dbdes_packet *ps = (void *) ps_i; u32 ps_ddseq = ntohl(ps->ddseq); u32 ps_options = ntoh_opt(ps->options); u16 ps_iface_mtu = ntohs(ps->iface_mtu); OSPF_PACKET(ospf_dump_dbdes, ps, "DBDES packet received from %I via %s", n->ip, ifa->iface->name); ospf_neigh_sm(n, INM_HELLOREC); switch (n->state) { case NEIGHBOR_DOWN: case NEIGHBOR_ATTEMPT: case NEIGHBOR_2WAY: return; break; case NEIGHBOR_INIT: ospf_neigh_sm(n, INM_2WAYREC); if (n->state != NEIGHBOR_EXSTART) return; case NEIGHBOR_EXSTART: if ((ps_iface_mtu != ifa->iface->mtu) && (ifa->type != OSPF_IT_VLINK) && (ps_iface_mtu != 0) && (ifa->iface->mtu != 0)) log(L_WARN "OSPF: MTU mismatch with neighbour %I on interface %s (remote %d, local %d)", n->ip, ifa->iface->name, ps_iface_mtu, ifa->iface->mtu); if ((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i) && (n->rid > po->router_id) && (size == sizeof(struct ospf_dbdes_packet))) { /* I'm slave! */ n->dds = ps_ddseq; n->ddr = ps_ddseq; n->options = ps_options; n->myimms.bit.ms = 0; n->imms.byte = ps->imms.byte; OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip); ospf_neigh_sm(n, INM_NEGDONE); ospf_dbdes_send(n, 1); break; } if (((ps->imms.bit.i == 0) && (ps->imms.bit.ms == 0)) && (n->rid < po->router_id) && (n->dds == ps_ddseq)) { /* I'm master! */ n->options = ps_options; n->ddr = ps_ddseq - 1; /* It will be set corectly a few lines down */ n->imms.byte = ps->imms.byte; OSPF_TRACE(D_PACKETS, "I'm master to %I.", n->ip); ospf_neigh_sm(n, INM_NEGDONE); } else { DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip, ps->imms.byte); break; } case NEIGHBOR_EXCHANGE: if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options) && (ps_ddseq == n->ddr)) { /* Duplicate packet */ OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip); if (n->myimms.bit.ms == 0) { /* Slave should retransmit dbdes packet */ ospf_dbdes_send(n, 0); } return; } n->ddr = ps_ddseq; if (ps->imms.bit.ms != n->imms.bit.ms) /* M/S bit differs */ { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit MS)", n->ip); ospf_neigh_sm(n, INM_SEQMIS); break; } if (ps->imms.bit.i) /* I bit is set */ { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit I)", n->ip); ospf_neigh_sm(n, INM_SEQMIS); break; } n->imms.byte = ps->imms.byte; if (ps_options != n->options) /* Options differs */ { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (options)", n->ip); ospf_neigh_sm(n, INM_SEQMIS); break; } if (n->myimms.bit.ms) { if (ps_ddseq != n->dds) /* MASTER */ { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (master)", n->ip); ospf_neigh_sm(n, INM_SEQMIS); break; } n->dds++; DBG("Incrementing dds\n"); ospf_dbdes_reqladd(ps, n); if ((n->myimms.bit.m == 0) && (ps->imms.bit.m == 0)) { ospf_neigh_sm(n, INM_EXDONE); } else { ospf_dbdes_send(n, 1); } } else { if (ps_ddseq != (n->dds + 1)) /* SLAVE */ { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (slave)", n->ip); ospf_neigh_sm(n, INM_SEQMIS); break; } n->ddr = ps_ddseq; n->dds = ps_ddseq; ospf_dbdes_reqladd(ps, n); ospf_dbdes_send(n, 1); } break; case NEIGHBOR_LOADING: case NEIGHBOR_FULL: if ((ps->imms.byte == n->imms.byte) && (ps_options == n->options) && (ps_ddseq == n->ddr)) /* Only duplicate are accepted */ { OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip); if (n->myimms.bit.ms == 0) { /* Slave should retransmit dbdes packet */ ospf_dbdes_send(n, 0); } return; } else { OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (full)", n->ip); DBG("PS=%u, DDR=%u, DDS=%u\n", ps_ddseq, n->ddr, n->dds); ospf_neigh_sm(n, INM_SEQMIS); } break; default: bug("Received dbdes from %I in undefined state.", n->ip); } }