Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / proto / ospf / packet.c @ 6b3f1a54

History | View | Annotate | Download (12.7 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2005 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
#include "ospf.h"
12
#include "nest/password.h"
13
#include "lib/md5.h"
14
#include "lib/mac.h"
15
#include "lib/socket.h"
16

    
17
void
18
ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
19
{
20
  struct ospf_proto *p = ifa->oa->po;
21
  struct ospf_packet *pkt;
22

    
23
  pkt = (struct ospf_packet *) buf;
24

    
25
  pkt->version = ospf_get_version(p);
26
  pkt->type = h_type;
27
  pkt->length = htons(ospf_pkt_maxsize(ifa));
28
  pkt->routerid = htonl(p->router_id);
29
  pkt->areaid = htonl(ifa->oa->areaid);
30
  pkt->checksum = 0;
31
  pkt->instance_id = ifa->instance_id;
32
  pkt->autype = ifa->autype;
33
}
34

    
35
/* We assume OSPFv2 in ospf_pkt_finalize() */
36
static void
37
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt, uint *plen)
38
{
39
  struct password_item *pass = NULL;
40
  union ospf_auth *auth = (void *) (pkt + 1);
41

    
42
  pkt->checksum = 0;
43
  pkt->autype = ifa->autype;
44
  bzero(auth, sizeof(union ospf_auth));
45

    
46
  /* Compatibility note: auth may contain anything if autype is
47
     none, but nonzero values do not work with Mikrotik OSPF */
48

    
49
  switch (ifa->autype)
50
  {
51
  case OSPF_AUTH_SIMPLE:
52
    pass = password_find(ifa->passwords, 1);
53
    if (!pass)
54
    {
55
      log(L_ERR "No suitable password found for authentication");
56
      return;
57
    }
58
    strncpy(auth->password, pass->password, sizeof(auth->password));
59

    
60
  case OSPF_AUTH_NONE:
61
    {
62
      void *body = (void *) (auth + 1);
63
      uint blen = *plen - sizeof(struct ospf_packet) - sizeof(union ospf_auth);
64
      pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet), body, blen, NULL);
65
    }
66
    break;
67

    
68
  case OSPF_AUTH_CRYPT:
69
    pass = password_find(ifa->passwords, 0);
70
    if (!pass)
71
    {
72
      log(L_ERR "No suitable password found for authentication");
73
      return;
74
    }
75

    
76
    /* Perhaps use random value to prevent replay attacks after
77
       reboot when system does not have independent RTC? */
78
    if (!ifa->csn)
79
    {
80
      ifa->csn = (u32) (current_real_time() TO_S);
81
      ifa->csn_use = current_time();
82
    }
83

    
84
    /* We must have sufficient delay between sending a packet and increasing
85
       CSN to prevent reordering of packets (in a network) with different CSNs */
86
    if ((current_time() - ifa->csn_use) > 1 S)
87
      ifa->csn++;
88

    
89
    ifa->csn_use = current_time();
90

    
91
    uint auth_len = mac_type_length(pass->alg);
92
    byte *auth_tail = ((byte *) pkt + *plen);
93
    *plen += auth_len;
94

    
95
    ASSERT(*plen < ifa->sk->tbsize);
96

    
97
    auth->c32.zero = 0;
98
    auth->c32.keyid = pass->id;
99
    auth->c32.len = auth_len;
100
    auth->c32.csn = htonl(ifa->csn);
101

    
102
    /* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
103
    if (pass->alg < ALG_HMAC)
104
      strncpy(auth_tail, pass->password, auth_len);
105
    else
106
      memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
107

    
108
    mac_fill(pass->alg, pass->password, pass->length,
109
             (byte *) pkt, *plen, auth_tail);
110
    break;
111

    
112
  default:
113
    bug("Unknown authentication type");
114
  }
115
}
116

    
117

    
118
/* We assume OSPFv2 in ospf_pkt_checkauth() */
119
static int
120
ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_packet *pkt, uint len)
121
{
122
  struct ospf_proto *p = ifa->oa->po;
123
  union ospf_auth *auth = (void *) (pkt + 1);
124
  struct password_item *pass = NULL;
125
  const char *err_dsc = NULL;
126
  uint err_val = 0;
127

    
128
  uint plen = ntohs(pkt->length);
129
  u8 autype = pkt->autype;
130

    
131
  if (autype != ifa->autype)
132
    DROP("authentication method mismatch", autype);
133

    
134
  switch (autype)
135
  {
136
  case OSPF_AUTH_NONE:
137
    return 1;
138

    
139
  case OSPF_AUTH_SIMPLE:
140
    pass = password_find(ifa->passwords, 1);
141
    if (!pass)
142
      DROP1("no password found");
143

    
144
    if (!password_verify(pass, auth->password, sizeof(auth->password)))
145
      DROP("wrong password", pass->id);
146

    
147
    return 1;
148

    
149
  case OSPF_AUTH_CRYPT:
150
    pass = password_find_by_id(ifa->passwords, auth->c32.keyid);
151
    if (!pass)
152
      DROP("no suitable password found", auth->c32.keyid);
153

    
154
    uint auth_len = mac_type_length(pass->alg);
155

    
156
    if (plen + auth->c32.len > len)
157
      DROP("packet length mismatch", len);
158

    
159
    if (auth->c32.len != auth_len)
160
      DROP("wrong authentication length", auth->c32.len);
161

    
162
    u32 rcv_csn = ntohl(auth->c32.csn);
163
    if (n && (rcv_csn < n->csn))
164
      // DROP("lower sequence number", rcv_csn);
165
    {
166
      /* We want to report both new and old CSN */
167
      LOG_PKT_AUTH("Authentication failed for nbr %R on %s - "
168
                   "lower sequence number (rcv %u, old %u)",
169
                   n->rid, ifa->ifname, rcv_csn, n->csn);
170
      return 0;
171
    }
172

    
173
    byte *auth_tail = ((byte *) pkt) + plen;
174
    byte *auth_data = alloca(auth_len);
175
    memcpy(auth_data, auth_tail, auth_len);
176

    
177
    /* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
178
    if (pass->alg < ALG_HMAC)
179
      strncpy(auth_tail, pass->password, auth_len);
180
    else
181
      memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
182

    
183
    if (!mac_verify(pass->alg, pass->password, pass->length,
184
                    (byte *) pkt, plen + auth_len, auth_data))
185
      DROP("wrong authentication code", pass->id);
186

    
187
    if (n)
188
      n->csn = rcv_csn;
189

    
190
    return 1;
191

    
192
  default:
193
    bug("Unknown authentication type");
194
  }
195

    
196
drop:
197
  LOG_PKT_AUTH("Authentication failed for nbr %R on %s - %s (%u)",
198
               (n ? n->rid : ntohl(pkt->routerid)), ifa->ifname, err_dsc, err_val);
199

    
200
  return 0;
201
}
202

    
203
/**
204
 * ospf_rx_hook
205
 * @sk: socket we received the packet.
206
 * @len: length of the packet
207
 *
208
 * This is the entry point for messages from neighbors. Many checks (like
209
 * authentication, checksums, size) are done before the packet is passed to
210
 * non generic functions.
211
 */
212
int
213
ospf_rx_hook(sock *sk, uint len)
214
{
215
  /* We want just packets from sk->iface. Unfortunately, on BSD we cannot filter
216
     out other packets at kernel level and we receive all packets on all sockets */
217
  if (sk->lifindex != sk->iface->index)
218
    return 1;
219

    
220
  DBG("OSPF: RX hook called (iface %s, src %I, dst %I)\n",
221
      sk->iface->name, sk->faddr, sk->laddr);
222

    
223
  /* Initially, the packet is associated with the 'master' iface */
224
  struct ospf_iface *ifa = sk->data;
225
  struct ospf_proto *p = ifa->oa->po;
226
  const char *err_dsc = NULL;
227
  uint err_val = 0;
228

    
229
  /* Should not happen */
230
  if (ifa->state <= OSPF_IS_LOOP)
231
    return 1;
232

    
233
  int src_local, dst_local, dst_mcast;
234
  src_local = ipa_in_netX(sk->faddr, &ifa->addr->prefix);
235
  dst_local = ipa_equal(sk->laddr, ifa->addr->ip);
236
  dst_mcast = ipa_equal(sk->laddr, ifa->all_routers) || ipa_equal(sk->laddr, ifa->des_routers);
237

    
238
  if (ospf_is_v2(p))
239
  {
240
    /* First, we eliminate packets with strange address combinations.
241
     * In OSPFv2, they might be for other ospf_ifaces (with different IP
242
     * prefix) on the same real iface, so we don't log it. We enforce
243
     * that (src_local || dst_local), therefore we are eliminating all
244
     * such cases.
245
     */
246
    if (dst_mcast && !src_local)
247
      return 1;
248
    if (!dst_mcast && !dst_local)
249
      return 1;
250

    
251
    /* Ignore my own broadcast packets */
252
    if (ifa->cf->real_bcast && ipa_equal(sk->faddr, ifa->addr->ip))
253
      return 1;
254
  }
255
  else
256
  {
257
    /* In OSPFv3, src_local and dst_local mean link-local.
258
     * RFC 5340 says that local (non-vlink) packets use
259
     * link-local src address, but does not enforce it. Strange.
260
     */
261
    if (dst_mcast && !src_local)
262
      LOG_PKT_WARN("Multicast packet received from non-link-local %I via %s",
263
                   sk->faddr, ifa->ifname);
264
  }
265

    
266
  /* Second, we check packet length, checksum, and the protocol version */
267
  struct ospf_packet *pkt = (void *) sk_rx_buffer(sk, &len);
268

    
269

    
270
  if (pkt == NULL)
271
    DROP("bad IP header", len);
272

    
273
  if (len < sizeof(struct ospf_packet))
274
    DROP("too short", len);
275

    
276
  if (pkt->version != ospf_get_version(p))
277
    DROP("version mismatch", pkt->version);
278

    
279
  uint plen = ntohs(pkt->length);
280
  if ((plen < sizeof(struct ospf_packet)) || ((plen % 4) != 0))
281
    DROP("invalid length", plen);
282

    
283
  if (sk->flags & SKF_TRUNCATED)
284
  {
285
    /* If we have dynamic buffers and received truncated message, we expand RX buffer */
286

    
287
    uint bs = plen + 256;
288
    bs = BIRD_ALIGN(bs, 1024);
289

    
290
    if (!ifa->cf->rx_buffer && (bs > sk->rbsize))
291
      sk_set_rbsize(sk, bs);
292

    
293
    DROP("truncated", plen);
294
  }
295

    
296
  if (plen > len)
297
    DROP("length mismatch", plen);
298

    
299
  if (ospf_is_v2(p) && (pkt->autype != OSPF_AUTH_CRYPT))
300
  {
301
    uint hlen = sizeof(struct ospf_packet) + sizeof(union ospf_auth);
302
    uint blen = plen - hlen;
303
    void *body = ((void *) pkt) + hlen;
304

    
305
    if (!ipsum_verify(pkt, sizeof(struct ospf_packet), body, blen, NULL))
306
      DROP1("invalid checksum");
307
  }
308

    
309
  /* Third, we resolve associated iface and handle vlinks. */
310

    
311
  u32 areaid = ntohl(pkt->areaid);
312
  u32 rid = ntohl(pkt->routerid);
313
  u8 instance_id = pkt->instance_id;
314

    
315
  if (areaid == ifa->oa->areaid)
316
  {
317
    /* Matching area ID */
318

    
319
    if (instance_id != ifa->instance_id)
320
      return 1;
321

    
322
    /* It is real iface, source should be local (in OSPFv2) */
323
    if (ospf_is_v2(p) && !src_local)
324
      DROP1("strange source address");
325

    
326
    goto found;
327
  }
328
  else if ((areaid == 0) && !dst_mcast)
329
  {
330
    /* Backbone area ID and possible vlink packet */
331

    
332
    if ((p->areano == 1) || !oa_is_ext(ifa->oa))
333
      return 1;
334

    
335
    struct ospf_iface *iff = NULL;
336
    WALK_LIST(iff, p->iface_list)
337
    {
338
      if ((iff->type == OSPF_IT_VLINK) &&
339
          (iff->voa == ifa->oa) &&
340
          (iff->instance_id == instance_id) &&
341
          (iff->vid == rid))
342
      {
343
        /* Vlink should be UP */
344
        if (iff->state != OSPF_IS_PTP)
345
          return 1;
346

    
347
        ifa = iff;
348
        goto found;
349
      }
350
    }
351

    
352
    /*
353
     * Cannot find matching vlink. It is either misconfigured vlink; NBMA or
354
     * PtMP with misconfigured area ID, or packet for some other instance (that
355
     * is possible even if instance_id == ifa->instance_id, because it may be
356
     * also vlink packet in the other instance, which is different namespace).
357
     */
358

    
359
    return 1;
360
  }
361
  else
362
  {
363
    /* Non-matching area ID but cannot be vlink packet */
364

    
365
    if (instance_id != ifa->instance_id)
366
      return 1;
367

    
368
    DROP("area mismatch", areaid);
369
  }
370

    
371

    
372
found:
373
  if (ifa->stub)            /* This shouldn't happen */
374
    return 1;
375

    
376
  if (ipa_equal(sk->laddr, ifa->des_routers) && (ifa->sk_dr == 0))
377
    return 1;
378

    
379
  /* TTL check must be done after instance dispatch */
380
  if (ifa->check_ttl && (sk->rcv_ttl < 255))
381
    DROP("wrong TTL", sk->rcv_ttl);
382

    
383
  if (rid == p->router_id)
384
    DROP1("my own router ID");
385

    
386
  if (rid == 0)
387
    DROP1("zero router ID");
388

    
389
  /* In OSPFv2, neighbors are identified by either IP or Router ID, based on network type */
390
  uint t = ifa->type;
391
  struct ospf_neighbor *n;
392
  if (ospf_is_v2(p) && ((t == OSPF_IT_BCAST) || (t == OSPF_IT_NBMA) || (t == OSPF_IT_PTMP)))
393
    n = find_neigh_by_ip(ifa, sk->faddr);
394
  else
395
    n = find_neigh(ifa, rid);
396

    
397
  if (!n && (pkt->type != HELLO_P))
398
  {
399
    OSPF_TRACE(D_PACKETS, "Non-HELLO packet received from unknown nbr %R on %s, src %I",
400
               rid, ifa->ifname, sk->faddr);
401
    return 1;
402
  }
403

    
404
  /* ospf_pkt_checkauth() has its own error logging */
405
  if (ospf_is_v2(p) && !ospf_pkt_checkauth(n, ifa, pkt, len))
406
    return 1;
407

    
408
  switch (pkt->type)
409
  {
410
  case HELLO_P:
411
    ospf_receive_hello(pkt, ifa, n, sk->faddr);
412
    break;
413

    
414
  case DBDES_P:
415
    ospf_receive_dbdes(pkt, ifa, n);
416
    break;
417

    
418
  case LSREQ_P:
419
    ospf_receive_lsreq(pkt, ifa, n);
420
    break;
421

    
422
  case LSUPD_P:
423
    ospf_receive_lsupd(pkt, ifa, n);
424
    break;
425

    
426
  case LSACK_P:
427
    ospf_receive_lsack(pkt, ifa, n);
428
    break;
429

    
430
  default:
431
    DROP("invalid packet type", pkt->type);
432
  };
433
  return 1;
434

    
435
drop:
436
  LOG_PKT("Bad packet from %I via %s - %s (%u)",
437
          sk->faddr, ifa->ifname, err_dsc, err_val);
438

    
439
  return 1;
440
}
441

    
442
/*
443
void
444
ospf_tx_hook(sock * sk)
445
{
446
  struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
447
//  struct proto *p = (struct proto *) (ifa->oa->p);
448
  log(L_ERR "OSPF: TX hook called on %s", ifa->ifname);
449
}
450
*/
451

    
452
void
453
ospf_err_hook(sock * sk, int err)
454
{
455
  struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
456
  struct ospf_proto *p = ifa->oa->po;
457
  log(L_ERR "%s: Socket error on %s: %M", p->p.name, ifa->ifname, err);
458
}
459

    
460
void
461
ospf_verr_hook(sock *sk, int err)
462
{
463
  struct ospf_proto *p = (struct ospf_proto *) (sk->data);
464
  log(L_ERR "%s: Vlink socket error: %M", p->p.name, err);
465
}
466

    
467
void
468
ospf_send_to(struct ospf_iface *ifa, ip_addr dst)
469
{
470
  sock *sk = ifa->sk;
471
  struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf;
472
  uint plen = ntohs(pkt->length);
473

    
474
  if (ospf_is_v2(ifa->oa->po))
475
    ospf_pkt_finalize(ifa, pkt, &plen);
476

    
477
  int done = sk_send_to(sk, plen, dst, 0);
478
  if (!done)
479
    log(L_WARN "OSPF: TX queue full on %s", ifa->ifname);
480
}
481

    
482
void
483
ospf_send_to_agt(struct ospf_iface *ifa, u8 state)
484
{
485
  struct ospf_neighbor *n;
486

    
487
  WALK_LIST(n, ifa->neigh_list)
488
    if (n->state >= state)
489
      ospf_send_to(ifa, n->ip);
490
}
491

    
492
void
493
ospf_send_to_bdr(struct ospf_iface *ifa)
494
{
495
  if (ipa_nonzero2(ifa->drip))
496
    ospf_send_to(ifa, ifa->drip);
497
  if (ipa_nonzero2(ifa->bdrip))
498
    ospf_send_to(ifa, ifa->bdrip);
499
}