packet.c 5.62 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF
 *
4
 *	(c) 1999 - 2003 Ondrej Filip <feela@network.cz>
5 6 7 8 9 10 11
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

void
Ondřej Filip's avatar
Ondřej Filip committed
12
fill_ospf_pkt_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
13 14 15 16
{
  struct ospf_packet *pkt;
  struct proto *p;

17
  p = (struct proto *) (ifa->proto);
18

19
  pkt = (struct ospf_packet *) buf;
20

21
  pkt->version = OSPF_VERSION;
22

23 24
  pkt->type = h_type;

Ondřej Filip's avatar
Ondřej Filip committed
25 26 27
  pkt->routerid = htonl(p->cf->global->router_id);
  pkt->areaid = htonl(ifa->an);
  pkt->autype = htons(ifa->autype);
28
  pkt->checksum = 0;
29 30 31
}

void
Ondřej Filip's avatar
Ondřej Filip committed
32
ospf_tx_authenticate(struct ospf_iface *ifa, struct ospf_packet *pkt)
33
{
Ondřej Filip's avatar
Ondřej Filip committed
34 35
  pkt->autype = htons(ifa->autype);
  memcpy(pkt->authetication, ifa->aukey, 8);
36 37 38
  return;
}

39
static int
Ondřej Filip's avatar
Ondřej Filip committed
40
ospf_rx_authenticate(struct ospf_iface *ifa, struct ospf_packet *pkt)
41 42
{
  int i;
Ondřej Filip's avatar
Ondřej Filip committed
43
  if (pkt->autype != htons(ifa->autype))
44 45 46 47
    return 0;
  if (ifa->autype == AU_NONE)
    return 1;
  if (ifa->autype == AU_SIMPLE)
Ondřej Filip's avatar
Ondřej Filip committed
48 49
  {
    for (i = 0; i < 8; i++)
50
    {
Ondřej Filip's avatar
Ondřej Filip committed
51 52
      if (pkt->authetication[i] != ifa->aukey[i])
	return 0;
53
    }
Ondřej Filip's avatar
Ondřej Filip committed
54 55
    return 1;
  }
56 57 58
  return 0;
}

59
void
Ondřej Filip's avatar
Ondřej Filip committed
60
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
61 62
{

Ondřej Filip's avatar
Ondřej Filip committed
63
  ospf_tx_authenticate(ifa, pkt);
64 65

  /* Count checksum */
Ondřej Filip's avatar
Ondřej Filip committed
66 67 68 69
  pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet) - 8,
				  (pkt + 1),
				  ntohs(pkt->length) -
				  sizeof(struct ospf_packet), NULL);
70 71
}

Ondřej Filip's avatar
Ondřej Filip committed
72 73
/**
 * ospf_rx_hook
74
 * @sk: socket we received the packet. Its ignored.
Ondřej Filip's avatar
Ondřej Filip committed
75 76
 * @size: size of the packet
 *
77 78
 * This is the entry point for messages from neighbors. Many checks (like
 * authentication, checksums, size) are done before the packet is passed to
Ondřej Filip's avatar
Ondřej Filip committed
79 80
 * non generic functions.
 */
81
int
Ondřej Filip's avatar
Ondřej Filip committed
82
ospf_rx_hook(sock * sk, int size)
83 84
{
  struct ospf_packet *ps;
85 86
  struct ospf_iface *ifa = (struct ospf_iface *) (sk->data);
  struct proto *p = (struct proto *) (ifa->proto);
87 88
  struct ospf_neighbor *n;
  char *mesg = "Bad OSPF packet from ";
89

90 91 92
  if (ifa->stub)
    return (1);

Ondřej Filip's avatar
Ondřej Filip committed
93
  DBG("%s: RX_Hook called on interface %s.\n", p->name, sk->iface->name);
94

Ondřej Filip's avatar
Ondřej Filip committed
95
  ps = (struct ospf_packet *) ipv4_skip_header(sk->rbuf, &size);
96
  if (ps == NULL)
Ondřej Filip's avatar
Ondřej Filip committed
97
  {
98 99
    log(L_ERR "%s%I - bad IP header", mesg, sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
100
  }
101

Ondřej Filip's avatar
Ondřej Filip committed
102 103
  if ((unsigned) size < sizeof(struct ospf_packet))
  {
104 105
    log(L_ERR "%s%I - too short (%u bytes)", mesg, sk->faddr, size);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
106
  }
107

Ondřej Filip's avatar
Ondřej Filip committed
108 109
  if ((ntohs(ps->length) != size) || (size != (4 * (size / 4))))
  {
110 111
    log(L_ERR "%s%I - size field does not match", mesg, sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
112
  }
113 114

  if (ps->version != OSPF_VERSION)
Ondřej Filip's avatar
Ondřej Filip committed
115
  {
116 117
    log(L_ERR "%s%I - version %u", mesg, sk->faddr, ps->version);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
118
  }
119

Ondřej Filip's avatar
Ondřej Filip committed
120 121 122
  if (!ipsum_verify(ps, 16, (void *) ps + sizeof(struct ospf_packet),
		    ntohs(ps->length) - sizeof(struct ospf_packet), NULL))
  {
123 124
    log(L_ERR "%s%I - bad checksum", mesg, sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
125
  }
126

Ondřej Filip's avatar
Ondřej Filip committed
127 128
  if (!ospf_rx_authenticate(ifa, ps))
  {
129 130
    log(L_ERR "%s%I - bad password", mesg, sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
131
  }
132

Ondřej Filip's avatar
Ondřej Filip committed
133 134
  if (ntohl(ps->areaid) != ifa->an)
  {
135 136
    log(L_ERR "%s%I - other area %ld", mesg, sk->faddr, ps->areaid);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
137
  }
138

Ondřej Filip's avatar
Ondřej Filip committed
139 140
  if (ntohl(ps->routerid) == p->cf->global->router_id)
  {
141 142
    log(L_ERR "%s%I - received my own router ID!", mesg, sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
143
  }
144

Ondřej Filip's avatar
Ondřej Filip committed
145 146
  if (ntohl(ps->routerid) == 0)
  {
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    log(L_ERR "%s%I - router id = 0.0.0.0", mesg, sk->faddr);
    return 1;
  }

  if ((unsigned) size > ifa->iface->mtu)
  {
    log(L_ERR "%s%I - received larger packet than MTU", mesg, sk->faddr);
    return 1;
  }

  n = find_neigh(ifa, ntohl(((struct ospf_packet *) ps)->routerid));

  if(!n && (ps->type != HELLO_P))
  {
    OSPF_TRACE(D_PACKETS, "Received non-hello packet from uknown neighbor (%I)", sk->faddr);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
163
  }
164

Ondřej Filip's avatar
Ondřej Filip committed
165
  /* Dump packet 
166 167 168 169 170 171 172 173
     pu8=(u8 *)(sk->rbuf+5*4);
     for(i=0;i<ntohs(ps->length);i+=4)
     DBG("%s: received %u,%u,%u,%u\n",p->name, pu8[i+0], pu8[i+1], pu8[i+2],
     pu8[i+3]);
     DBG("%s: received size: %u\n",p->name,size);
   */

  switch (ps->type)
Ondřej Filip's avatar
Ondřej Filip committed
174 175 176
  {
  case HELLO_P:
    DBG("%s: Hello received.\n", p->name);
177
    ospf_hello_receive((struct ospf_hello_packet *) ps, ifa, n, sk->faddr);
Ondřej Filip's avatar
Ondřej Filip committed
178 179 180
    break;
  case DBDES_P:
    DBG("%s: Database description received.\n", p->name);
181
    ospf_dbdes_receive((struct ospf_dbdes_packet *) ps, ifa, n);
Ondřej Filip's avatar
Ondřej Filip committed
182 183 184
    break;
  case LSREQ_P:
    DBG("%s: Link state request received.\n", p->name);
185
    ospf_lsreq_receive((struct ospf_lsreq_packet *) ps, ifa, n);
Ondřej Filip's avatar
Ondřej Filip committed
186 187 188
    break;
  case LSUPD_P:
    DBG("%s: Link state update received.\n", p->name);
189
    ospf_lsupd_receive((struct ospf_lsupd_packet *) ps, ifa, n);
Ondřej Filip's avatar
Ondřej Filip committed
190 191 192
    break;
  case LSACK_P:
    DBG("%s: Link state ack received.\n", p->name);
193
    ospf_lsack_receive((struct ospf_lsack_packet *) ps, ifa, n);
Ondřej Filip's avatar
Ondřej Filip committed
194 195
    break;
  default:
196 197
    log(L_ERR "%s%I - wrong type %u", mesg, sk->faddr, ps->type);
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
198
  };
199
  return 1;
200 201 202
}

void
Ondřej Filip's avatar
Ondřej Filip committed
203
ospf_tx_hook(sock * sk)
204 205 206 207
{
  struct ospf_iface *ifa;
  struct proto *p;

208
  ifa = (struct ospf_iface *) (sk->data);
209

210
  p = (struct proto *) (ifa->proto);
Ondřej Filip's avatar
Ondřej Filip committed
211
  DBG("%s: TX_Hook called on interface %s\n", p->name, sk->iface->name);
212 213 214
}

void
Ondřej Filip's avatar
Ondřej Filip committed
215
ospf_err_hook(sock * sk, int err UNUSED)
216 217 218 219
{
  struct ospf_iface *ifa;
  struct proto *p;

220
  ifa = (struct ospf_iface *) (sk->data);
221

222
  p = (struct proto *) (ifa->proto);
Ondřej Filip's avatar
Ondřej Filip committed
223
  DBG("%s: Err_Hook called on interface %s\n", p->name, sk->iface->name);
224 225
}

226
void
Ondřej Filip's avatar
Ondřej Filip committed
227
sk_send_to_agt(sock * sk, u16 len, struct ospf_iface *ifa, u8 state)
228 229 230
{
  struct ospf_neighbor *n;

Ondřej Filip's avatar
Ondřej Filip committed
231 232
  WALK_LIST(NODE n, ifa->neigh_list) if (n->state >= state)
    sk_send_to(sk, len, n->ip, OSPF_PROTO);
233
}
234 235

void
Ondřej Filip's avatar
Ondřej Filip committed
236
sk_send_to_bdr(sock * sk, u16 len, struct ospf_iface *ifa)
237
{
Ondřej Filip's avatar
Ondřej Filip committed
238 239 240 241
  if (ipa_compare(ifa->drip, ipa_from_u32(0)) != 0)
    sk_send_to(sk, len, ifa->drip, OSPF_PROTO);
  if (ipa_compare(ifa->bdrip, ipa_from_u32(0)) != 0)
    sk_send_to(sk, len, ifa->bdrip, OSPF_PROTO);
242
}