diff options
Diffstat (limited to 'proto/ospf/packet.c')
-rw-r--r-- | proto/ospf/packet.c | 104 |
1 files changed, 76 insertions, 28 deletions
diff --git a/proto/ospf/packet.c b/proto/ospf/packet.c index 783d28ed..9422a9f3 100644 --- a/proto/ospf/packet.c +++ b/proto/ospf/packet.c @@ -24,21 +24,37 @@ ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type) pkt->routerid = htonl(p->cf->global->router_id); pkt->areaid = htonl(ifa->oa->areaid); + +#ifdef OSPFv3 + pkt->instance_id = ifa->instance_id; +#endif + +#ifdef OSPFv2 pkt->autype = htons(ifa->autype); +#endif + pkt->checksum = 0; } unsigned ospf_pkt_maxsize(struct ospf_iface *ifa) { + /* For virtual links use mtu=576, can be mtu < 576? */ unsigned mtu = (ifa->type == OSPF_IT_VLINK) ? OSPF_VLINK_MTU : ifa->iface->mtu; - /* Can be mtu < 576? */ + unsigned add = 0; + +#ifdef OSPFv2 + add = ((ifa->autype == OSPF_AUTH_CRYPT) ? OSPF_AUTH_CRYPT_SIZE : 0); +#endif + return ((mtu <= ifa->iface->mtu) ? mtu : ifa->iface->mtu) - - SIZE_OF_IP_HEADER - ((ifa->autype == OSPF_AUTH_CRYPT) ? OSPF_AUTH_CRYPT_SIZE : 0); - /* For virtual links use mtu=576 */ + SIZE_OF_IP_HEADER - add; } -void + +#ifdef OSPFv2 + +static void ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt) { struct password_item *passwd = NULL; @@ -224,6 +240,20 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_ } } +#else + +/* OSPFv3 authentication not yet supported */ + +static inline void +ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt) +{ } + +static int +ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_packet *pkt, int size) +{ return 1; } + +#endif + /** * ospf_rx_hook * @sk: socket we received the packet. Its ignored. @@ -248,7 +278,7 @@ ospf_rx_hook(sock * sk, int size) if (ifa->stub) return (1); - ps = (struct ospf_packet *) ipv4_skip_header(sk->rbuf, &size); + ps = (struct ospf_packet *) ip_skip_header(sk->rbuf, &size); if (ps == NULL) { @@ -256,6 +286,7 @@ ospf_rx_hook(sock * sk, int size) return 1; } + /* This is strange! */ if ((ifa->oa->areaid != 0) && (ntohl(ps->areaid) == 0)) { WALK_LIST(iff, po->iface_list) @@ -270,17 +301,16 @@ ospf_rx_hook(sock * sk, int size) DBG("%s: RX_Hook called on interface %s.\n", p->name, sk->iface->name); - osize = ntohs(ps->length); - if ((unsigned) size < sizeof(struct ospf_packet)) { log(L_ERR "%s%I - too short (%u bytes)", mesg, sk->faddr, size); return 1; } - if ((osize > size) || (osize != (4 * (osize / 4)))) + osize = ntohs(ps->length); + if ((osize > size) || ((osize % 4) != 0)) { - log(L_ERR "%s%I - size field does not match (%d/%d)", mesg, sk->faddr, ntohs(ps->length), size ); + log(L_ERR "%s%I - size field does not match (%d/%d)", mesg, sk->faddr, osize, size); return 1; } @@ -290,20 +320,32 @@ ospf_rx_hook(sock * sk, int size) return 1; } + /* FIXME - handle checksums in OSPFv3 */ +#ifdef OSPFv2 if ((ps->autype != htons(OSPF_AUTH_CRYPT)) && (!ipsum_verify(ps, 16, (void *) ps + sizeof(struct ospf_packet), - ntohs(ps->length) - sizeof(struct ospf_packet), NULL))) + osize - sizeof(struct ospf_packet), NULL))) { log(L_ERR "%s%I - bad checksum", mesg, sk->faddr); return 1; } +#endif if (ntohl(ps->areaid) != ifa->oa->areaid) { - log(L_ERR "%s%I - different area %ld", mesg, sk->faddr, ntohl(ps->areaid)); + log(L_ERR "%s%I - different area (%u)", mesg, sk->faddr, ntohl(ps->areaid)); return 1; } + /* FIXME - handling of instance id should be better */ +#ifdef OSPFv3 + if (ps->instance_id != ifa->instance_id) + { + log(L_ERR "%s%I - different instance (%u)", mesg, sk->faddr, ps->instance_id); + return 1; + } +#endif + if (ntohl(ps->routerid) == p->cf->global->router_id) { log(L_ERR "%s%I - received my own router ID!", mesg, sk->faddr); @@ -316,17 +358,17 @@ ospf_rx_hook(sock * sk, int size) return 1; } - if (((unsigned) size > sk->rbsize) || (ntohs(ps->length) > sk->rbsize)) + if ((unsigned) size > sk->rbsize) { - log(L_ERR "%s%I - packet is too large (%d-%d vs %d)", - mesg, sk->faddr, size, ntohs(ps->length), sk->rbsize); + log(L_ERR "%s%I - packet is too large (%d vs %d)", + mesg, sk->faddr, size, sk->rbsize); return 1; } /* This is deviation from RFC 2328 - neighbours should be identified by * IP address on broadcast and NBMA networks. */ - n = find_neigh(ifa, ntohl(((struct ospf_packet *) ps)->routerid)); + n = find_neigh(ifa, ntohl(ps->routerid)); if(!n && (ps->type != HELLO_P)) { @@ -352,23 +394,23 @@ ospf_rx_hook(sock * sk, int size) { case HELLO_P: DBG("%s: Hello received.\n", p->name); - ospf_hello_receive((struct ospf_hello_packet *) ps, ifa, n, sk->faddr); + ospf_hello_receive(ps, ifa, n, sk->faddr); break; case DBDES_P: DBG("%s: Database description received.\n", p->name); - ospf_dbdes_receive((struct ospf_dbdes_packet *) ps, ifa, n); + ospf_dbdes_receive(ps, ifa, n); break; case LSREQ_P: DBG("%s: Link state request received.\n", p->name); - ospf_lsreq_receive((struct ospf_lsreq_packet *) ps, ifa, n); + ospf_lsreq_receive(ps, ifa, n); break; case LSUPD_P: DBG("%s: Link state update received.\n", p->name); - ospf_lsupd_receive((struct ospf_lsupd_packet *) ps, ifa, n); + ospf_lsupd_receive(ps, ifa, n); break; case LSACK_P: DBG("%s: Link state ack received.\n", p->name); - ospf_lsack_receive((struct ospf_lsack_packet *) ps, ifa, n); + ospf_lsack_receive(ps, ifa, n); break; default: log(L_ERR "%s%I - wrong type %u", mesg, sk->faddr, ps->type); @@ -395,30 +437,36 @@ ospf_err_hook(sock * sk, int err) } void -ospf_send_to_agt(sock * sk, struct ospf_iface *ifa, u8 state) +ospf_send_to_agt(struct ospf_iface *ifa, u8 state) { struct ospf_neighbor *n; WALK_LIST(n, ifa->neigh_list) if (n->state >= state) - ospf_send_to(sk, n->ip, ifa); + ospf_send_to(ifa, n->ip); } void -ospf_send_to_bdr(sock * sk, struct ospf_iface *ifa) +ospf_send_to_bdr(struct ospf_iface *ifa) { if (!ipa_equal(ifa->drip, IPA_NONE)) - ospf_send_to(sk, ifa->drip, ifa); + ospf_send_to(ifa, ifa->drip); if (!ipa_equal(ifa->bdrip, IPA_NONE)) - ospf_send_to(sk, ifa->bdrip, ifa); + ospf_send_to(ifa, ifa->bdrip); } void -ospf_send_to(sock *sk, ip_addr ip, struct ospf_iface *ifa) +ospf_send_to(struct ospf_iface *ifa, ip_addr ip) { + sock *sk = ifa->sk; struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf; - int len = ntohs(pkt->length) + ((ifa->autype == OSPF_AUTH_CRYPT) ? OSPF_AUTH_CRYPT_SIZE : 0); - ospf_pkt_finalize(ifa, pkt); + int len = ntohs(pkt->length); + +#ifdef OSPFv2 + if (ifa->autype == OSPF_AUTH_CRYPT) + len += OSPF_AUTH_CRYPT_SIZE; +#endif + ospf_pkt_finalize(ifa, pkt); if (sk->tbuf != sk->tpos) log(L_ERR "Aiee, old packet was overwritted in TX buffer"); |