packet.c 5.75 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
  struct ospf_neighbor *n;
88
  int osize;
89
  char *mesg = "Bad OSPF packet from ";
90

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

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

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

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

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

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

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

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

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

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

Ondřej Filip's avatar
Ondřej Filip committed
147 148
  if (ntohl(ps->routerid) == 0)
  {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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
165
  }
166

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

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

210
  ifa = (struct ospf_iface *) (sk->data);
211

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

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

222
  ifa = (struct ospf_iface *) (sk->data);
223

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

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

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

void
Ondřej Filip's avatar
Ondřej Filip committed
238
ospf_send_to_bdr(sock * sk, u16 len, struct ospf_iface *ifa)
239
{
Ondřej Filip's avatar
Ondřej Filip committed
240
  if (ipa_compare(ifa->drip, ipa_from_u32(0)) != 0)
Ondřej Filip's avatar
Ondřej Filip committed
241
    ospf_send_to(sk, len, ifa->drip);
Ondřej Filip's avatar
Ondřej Filip committed
242
  if (ipa_compare(ifa->bdrip, ipa_from_u32(0)) != 0)
Ondřej Filip's avatar
Ondřej Filip committed
243
    ospf_send_to(sk, len, ifa->bdrip);
244
}
Ondřej Filip's avatar
Ondřej Filip committed
245 246 247 248 249 250 251

void
ospf_send_to(sock *sk, u16 len, ip_addr ip)
{
  sk_send_to(sk, len, ip, OSPF_PROTO);
}