Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / packets.c @ 153f02da

History | View | Annotate | Download (20.2 KB)

1
/*
2
 *        BIRD -- Routing Information Protocol (RIP)
3
 *
4
 *        (c) 1998--1999 Pavel Machek <pavel@ucw.cz>
5
 *        (c) 2004--2013 Ondrej Filip <feela@network.cz>
6
 *        (c) 2009--2015 Ondrej Zajicek <santiago@crfreenet.org>
7
 *        (c) 2009--2015 CZ.NIC z.s.p.o.
8
 *
9
 *        Can be freely distributed and used under the terms of the GNU GPL.
10
 */
11

    
12
#undef LOCAL_DEBUG
13

    
14
#include "rip.h"
15
#include "lib/mac.h"
16

    
17

    
18
#define RIP_CMD_REQUEST                1        /* want info */
19
#define RIP_CMD_RESPONSE        2        /* responding to request */
20

    
21
#define RIP_BLOCK_LENGTH        20
22
#define RIP_PASSWD_LENGTH        16
23

    
24
#define RIP_AF_IPV4                2
25
#define RIP_AF_AUTH                0xffff
26

    
27

    
28
/* RIP packet header */
29
struct rip_packet
30
{
31
  u8 command;
32
  u8 version;
33
  u16 unused;
34
};
35

    
36
/* RTE block for RIPv2 */
37
struct rip_block_v2
38
{
39
  u16 family;
40
  u16 tag;
41
  ip4_addr network;
42
  ip4_addr netmask;
43
  ip4_addr next_hop;
44
  u32 metric;
45
};
46

    
47
/* RTE block for RIPng */
48
struct rip_block_ng
49
{
50
  ip6_addr prefix;
51
  u16 tag;
52
  u8 pxlen;
53
  u8 metric;
54
};
55

    
56
/* Authentication block for RIPv2 */
57
struct rip_block_auth
58
{
59
  u16 must_be_ffff;
60
  u16 auth_type;
61
  char password[0];
62
  u16 packet_len;
63
  u8 key_id;
64
  u8 auth_len;
65
  u32 seq_num;
66
  u32 unused1;
67
  u32 unused2;
68
};
69

    
70
/* Authentication tail, RFC 4822 */
71
struct rip_auth_tail
72
{
73
  u16 must_be_ffff;
74
  u16 must_be_0001;
75
  byte auth_data[0];
76
};
77

    
78
/* Internal representation of RTE block data */
79
struct rip_block
80
{
81
  net_addr net;
82
  u32 metric;
83
  u16 tag;
84
  u16 no_af;
85
  ip_addr next_hop;
86
};
87

    
88

    
89
#define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0)
90
#define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0)
91
#define SKIP(DSC) do { err_dsc = DSC; goto skip; } while(0)
92

    
93
#define LOG_PKT(msg, args...) \
94
  log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
95

    
96
#define LOG_PKT_AUTH(msg, args...) \
97
  log_rl(&p->log_pkt_tbf, L_AUTH "%s: " msg, p->p.name, args)
98

    
99
#define LOG_RTE(msg, args...) \
100
  log_rl(&p->log_rte_tbf, L_REMOTE "%s: " msg, p->p.name, args)
101

    
102

    
103
static inline void * rip_tx_buffer(struct rip_iface *ifa)
104
{ return ifa->sk->tbuf; }
105

    
106
static inline uint rip_pkt_hdrlen(struct rip_iface *ifa)
107
{ return sizeof(struct rip_packet) + (ifa->cf->auth_type ? RIP_BLOCK_LENGTH : 0); }
108

    
109
static inline void
110
rip_put_block(struct rip_proto *p, byte *pos, struct rip_block *rte)
111
{
112
  if (rip_is_v2(p))
113
  {
114
    struct rip_block_v2 *block = (void *) pos;
115
    block->family = rte->no_af ? 0 : htons(RIP_AF_IPV4);
116
    block->tag = htons(rte->tag);
117
    block->network = ip4_hton(net4_prefix(&rte->net));
118
    block->netmask = ip4_hton(ip4_mkmask(net4_pxlen(&rte->net)));
119
    block->next_hop = ip4_hton(ipa_to_ip4(rte->next_hop));
120
    block->metric = htonl(rte->metric);
121
  }
122
  else /* RIPng */
123
  {
124
    struct rip_block_ng *block = (void *) pos;
125
    block->prefix = ip6_hton(net6_prefix(&rte->net));
126
    block->tag = htons(rte->tag);
127
    block->pxlen = net6_pxlen(&rte->net);
128
    block->metric = rte->metric;
129
  }
130
}
131

    
132
static inline void
133
rip_put_next_hop(struct rip_proto *p UNUSED, byte *pos, struct rip_block *rte)
134
{
135
  struct rip_block_ng *block = (void *) pos;
136
  block->prefix = ip6_hton(ipa_to_ip6(rte->next_hop));
137
  block->tag = 0;
138
  block->pxlen = 0;
139
  block->metric = 0xff;
140
}
141

    
142
static inline int
143
rip_get_block(struct rip_proto *p, byte *pos, struct rip_block *rte)
144
{
145
  if (rip_is_v2(p))
146
  {
147
    struct rip_block_v2 *block = (void *) pos;
148

    
149
    /* Skip blocks with strange AF, including authentication blocks */
150
    if (block->family != (rte->no_af ? 0 : htons(RIP_AF_IPV4)))
151
      return 0;
152

    
153
    uint pxlen = ip4_masklen(ip4_ntoh(block->netmask));
154
    net_fill_ip4(&rte->net, ip4_ntoh(block->network), pxlen);
155
    rte->metric = ntohl(block->metric);
156
    rte->tag = ntohs(block->tag);
157
    rte->next_hop = ipa_from_ip4(ip4_ntoh(block->next_hop));
158

    
159
    return 1;
160
  }
161
  else /* RIPng */
162
  {
163
    struct rip_block_ng *block = (void *) pos;
164

    
165
    /* Handle and skip next hop blocks */
166
    if (block->metric == 0xff)
167
    {
168
      rte->next_hop = ipa_from_ip6(ip6_ntoh(block->prefix));
169
      if (!ipa_is_link_local(rte->next_hop)) rte->next_hop = IPA_NONE;
170
      return 0;
171
    }
172

    
173
    uint pxlen = (block->pxlen <= IP6_MAX_PREFIX_LENGTH) ? block->pxlen : 255;
174
    net_fill_ip6(&rte->net, ip6_ntoh(block->prefix), pxlen);
175
    rte->metric = block->metric;
176
    rte->tag = ntohs(block->tag);
177
    /* rte->next_hop is deliberately kept unmodified */;
178

    
179
    return 1;
180
  }
181
}
182

    
183
static inline void
184
rip_update_csn(struct rip_proto *p UNUSED, struct rip_iface *ifa)
185
{
186
  /*
187
   * We update crypto sequence numbers at the beginning of update session to
188
   * avoid issues with packet reordering, so packets inside one update session
189
   * have the same CSN. We are using real time, but enforcing monotonicity.
190
   */
191
  if (ifa->cf->auth_type == RIP_AUTH_CRYPTO)
192
    ifa->csn = (ifa->csn < (u32) now_real) ? (u32) now_real : ifa->csn + 1;
193
}
194

    
195
static void
196
rip_fill_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint *plen)
197
{
198
  struct rip_block_auth *auth = (void *) (pkt + 1);
199
  struct password_item *pass = password_find(ifa->cf->passwords, 0);
200

    
201
  if (!pass)
202
  {
203
    /* FIXME: This should not happen */
204
    log(L_ERR "%s: No suitable password found for authentication", p->p.name);
205
    memset(auth, 0, sizeof(struct rip_block_auth));
206
    return;
207
  }
208

    
209
  switch (ifa->cf->auth_type)
210
  {
211
  case RIP_AUTH_PLAIN:
212
    auth->must_be_ffff = htons(0xffff);
213
    auth->auth_type = htons(RIP_AUTH_PLAIN);
214
    strncpy(auth->password, pass->password, RIP_PASSWD_LENGTH);
215
    return;
216

    
217
  case RIP_AUTH_CRYPTO:
218
    auth->must_be_ffff = htons(0xffff);
219
    auth->auth_type = htons(RIP_AUTH_CRYPTO);
220
    auth->packet_len = htons(*plen);
221
    auth->key_id = pass->id;
222
    auth->auth_len = mac_type_length(pass->alg);
223
    auth->seq_num = ifa->csn_ready ? htonl(ifa->csn) : 0;
224
    auth->unused1 = 0;
225
    auth->unused2 = 0;
226
    ifa->csn_ready = 1;
227

    
228
    if (pass->alg < ALG_HMAC)
229
      auth->auth_len += sizeof(struct rip_auth_tail);
230

    
231
    /*
232
     * Note that RFC 4822 is unclear whether auth_len should cover whole
233
     * authentication trailer or just auth_data length.
234
     *
235
     * FIXME: We should use just auth_data length by default. Currently we put
236
     * the whole auth trailer length in keyed hash case to keep old behavior,
237
     * but we put just auth_data length in the new HMAC case. Note that Quagga
238
     * has config option for this.
239
     *
240
     * Crypto sequence numbers are increased by sender in rip_update_csn().
241
     * First CSN should be zero, this is handled by csn_ready.
242
     */
243

    
244
    struct rip_auth_tail *tail = (void *) ((byte *) pkt + *plen);
245
    tail->must_be_ffff = htons(0xffff);
246
    tail->must_be_0001 = htons(0x0001);
247

    
248
    uint auth_len = mac_type_length(pass->alg);
249
    *plen += sizeof(struct rip_auth_tail) + auth_len;
250

    
251
    /* Append key for keyed hash, append padding for HMAC (RFC 4822 2.5) */
252
    if (pass->alg < ALG_HMAC)
253
      strncpy(tail->auth_data, pass->password, auth_len);
254
    else
255
      memset32(tail->auth_data, HMAC_MAGIC, auth_len / 4);
256

    
257
    mac_fill(pass->alg, pass->password, pass->length,
258
             (byte *) pkt, *plen, tail->auth_data);
259
    return;
260

    
261
  default:
262
    bug("Unknown authentication type");
263
  }
264
}
265

    
266
static int
267
rip_check_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint *plen, struct rip_neighbor *n)
268
{
269
  struct rip_block_auth *auth = (void *) (pkt + 1);
270
  struct password_item *pass = NULL;
271
  const char *err_dsc = NULL;
272
  uint err_val = 0;
273
  uint auth_type = 0;
274

    
275
  /* Check for authentication entry */
276
  if ((*plen >= (sizeof(struct rip_packet) + sizeof(struct rip_block_auth))) &&
277
      (auth->must_be_ffff == htons(0xffff)))
278
    auth_type = ntohs(auth->auth_type);
279

    
280
  if (auth_type != ifa->cf->auth_type)
281
    DROP("authentication method mismatch", auth_type);
282

    
283
  switch (auth_type)
284
  {
285
  case RIP_AUTH_NONE:
286
    return 1;
287

    
288
  case RIP_AUTH_PLAIN:
289
    pass = password_find_by_value(ifa->cf->passwords, auth->password, RIP_PASSWD_LENGTH);
290
    if (!pass)
291
      DROP1("wrong password");
292

    
293
    return 1;
294

    
295
  case RIP_AUTH_CRYPTO:
296
    pass = password_find_by_id(ifa->cf->passwords, auth->key_id);
297
    if (!pass)
298
      DROP("no suitable password found", auth->key_id);
299

    
300
    uint data_len = ntohs(auth->packet_len);
301
    uint auth_len = mac_type_length(pass->alg);
302
    uint auth_len2 = sizeof(struct rip_auth_tail) + auth_len;
303

    
304
    /*
305
     * Ideally, first check should be check for internal consistency:
306
     *   (data_len + sizeof(struct rip_auth_tail) + auth->auth_len) != *plen
307
     *
308
     * Second one should check expected code length:
309
     *   auth->auth_len != auth_len
310
     *
311
     * But as auth->auth_len has two interpretations, we simplify this
312
     */
313

    
314
    if (data_len + auth_len2 != *plen)
315
      DROP("packet length mismatch", *plen);
316

    
317
    /* Warning: two interpretations of auth_len field */
318
    if ((auth->auth_len != auth_len) && (auth->auth_len != auth_len2))
319
      DROP("wrong authentication length", auth->auth_len);
320

    
321
    struct rip_auth_tail *tail = (void *) ((byte *) pkt + data_len);
322
    if ((tail->must_be_ffff != htons(0xffff)) || (tail->must_be_0001 != htons(0x0001)))
323
      DROP1("authentication trailer is missing");
324

    
325
    /* Accept higher sequence number, or zero if connectivity is lost */
326
    /* FIXME: sequence number must be password/SA specific */
327
    u32 rcv_csn = ntohl(auth->seq_num);
328
    if ((rcv_csn < n->csn) && (rcv_csn || n->uc))
329
    {
330
      /* We want to report both new and old CSN */
331
      LOG_PKT_AUTH("Authentication failed for %I on %s - "
332
                   "lower sequence number (rcv %u, old %u)",
333
                   n->nbr->addr, ifa->iface->name, rcv_csn, n->csn);
334
      return 0;
335
    }
336

    
337
    byte *auth_data = alloca(auth_len);
338
    memcpy(auth_data, tail->auth_data, auth_len);
339

    
340
    /* Append key for keyed hash, append padding for HMAC (RFC 4822 2.5) */
341
    if (pass->alg < ALG_HMAC)
342
      strncpy(tail->auth_data, pass->password, auth_len);
343
    else
344
      memset32(tail->auth_data, HMAC_MAGIC, auth_len / 4);
345

    
346
    if (!mac_verify(pass->alg, pass->password, pass->length,
347
                    (byte *) pkt, *plen, auth_data))
348
      DROP("wrong authentication code", pass->id);
349

    
350
    *plen = data_len;
351
    n->csn = rcv_csn;
352

    
353
    return 1;
354
  }
355

    
356
drop:
357
  LOG_PKT_AUTH("Authentication failed for %I on %s - %s (%u)",
358
               n->nbr->addr, ifa->iface->name, err_dsc, err_val);
359

    
360
  return 0;
361
}
362

    
363
static inline int
364
rip_send_to(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, ip_addr dst)
365
{
366
  if (ifa->cf->auth_type)
367
    rip_fill_authentication(p, ifa, pkt, &plen);
368

    
369
  return sk_send_to(ifa->sk, plen, dst, 0);
370
}
371

    
372

    
373
void
374
rip_send_request(struct rip_proto *p, struct rip_iface *ifa)
375
{
376
  byte *pos = rip_tx_buffer(ifa);
377

    
378
  struct rip_packet *pkt = (void *) pos;
379
  pkt->command = RIP_CMD_REQUEST;
380
  pkt->version = ifa->cf->version;
381
  pkt->unused = 0;
382
  pos += rip_pkt_hdrlen(ifa);
383

    
384
  struct rip_block b = { .no_af = 1, .metric = p->infinity };
385
  rip_put_block(p, pos, &b);
386
  pos += RIP_BLOCK_LENGTH;
387

    
388
  rip_update_csn(p, ifa);
389

    
390
  TRACE(D_PACKETS, "Sending request via %s", ifa->iface->name);
391
  rip_send_to(p, ifa, pkt, pos - (byte *) pkt, ifa->addr);
392
}
393

    
394
static void
395
rip_receive_request(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, struct rip_neighbor *from)
396
{
397
  TRACE(D_PACKETS, "Request received from %I on %s", from->nbr->addr, ifa->iface->name);
398

    
399
  byte *pos = (byte *) pkt + rip_pkt_hdrlen(ifa);
400

    
401
  /* We expect one regular block */
402
  if (plen != (rip_pkt_hdrlen(ifa) + RIP_BLOCK_LENGTH))
403
    return;
404

    
405
  struct rip_block b = { .no_af = 1 };
406

    
407
  if (!rip_get_block(p, pos, &b))
408
    return;
409

    
410
  /* Special case - infinity metric, for RIPng also zero prefix */
411
  if ((b.metric != p->infinity) ||
412
      (rip_is_ng(p) && !net_zero_ip6((net_addr_ip6 *) &b.net)))
413
    return;
414

    
415
  /* We do nothing if TX is already active */
416
  if (ifa->tx_active)
417
  {
418
    TRACE(D_EVENTS, "Skipping request from %I on %s, TX is busy", from->nbr->addr, ifa->iface->name);
419
    return;
420
  }
421

    
422
  if (!ifa->cf->passive)
423
    rip_send_table(p, ifa, from->nbr->addr, 0);
424
}
425

    
426

    
427
static int
428
rip_send_response(struct rip_proto *p, struct rip_iface *ifa)
429
{
430
  if (! ifa->tx_active)
431
    return 0;
432

    
433
  byte *pos = rip_tx_buffer(ifa);
434
  byte *max = rip_tx_buffer(ifa) + ifa->tx_plen -
435
    (rip_is_v2(p) ? RIP_BLOCK_LENGTH : 2*RIP_BLOCK_LENGTH);
436
  ip_addr last_next_hop = IPA_NONE;
437
  int send = 0;
438

    
439
  struct rip_packet *pkt = (void *) pos;
440
  pkt->command = RIP_CMD_RESPONSE;
441
  pkt->version = ifa->cf->version;
442
  pkt->unused = 0;
443
  pos += rip_pkt_hdrlen(ifa);
444

    
445
  FIB_ITERATE_START(&p->rtable, &ifa->tx_fit, struct rip_entry, en)
446
  {
447
    /* Dummy entries */
448
    if (!en->valid)
449
      goto next_entry;
450

    
451
    /* Stale entries that should be removed */
452
    if ((en->valid == RIP_ENTRY_STALE) &&
453
        ((en->changed + (bird_clock_t) ifa->cf->garbage_time) <= now))
454
      goto next_entry;
455

    
456
    /* Triggered updates */
457
    if (en->changed < ifa->tx_changed)
458
      goto next_entry;
459

    
460
    /* Not enough space for current entry */
461
    if (pos > max)
462
    {
463
      FIB_ITERATE_PUT(&ifa->tx_fit);
464
      goto break_loop;
465
    }
466

    
467
    struct rip_block rte = {
468
      .metric = en->metric,
469
      .tag = en->tag
470
    };
471

    
472
    net_copy(&rte.net, en->n.addr);
473

    
474
    if (en->iface == ifa->iface)
475
      rte.next_hop = en->next_hop;
476

    
477
    if (rip_is_v2(p) && (ifa->cf->version == RIP_V1))
478
    {
479
      /* Skipping subnets (i.e. not hosts, classful networks or default route) */
480
      if (ip4_masklen(ip4_class_mask(net4_prefix(&rte.net))) != rte.net.pxlen)
481
        goto next_entry;
482

    
483
      rte.tag = 0;
484
      rte.net.pxlen = 0;
485
      rte.next_hop = IPA_NONE;
486
    }
487

    
488
    /* Split horizon */
489
    if (en->from == ifa->iface && ifa->cf->split_horizon)
490
    {
491
      if (ifa->cf->poison_reverse)
492
      {
493
        rte.metric = p->infinity;
494
        rte.next_hop = IPA_NONE;
495
      }
496
      else
497
        goto next_entry;
498
    }
499

    
500
    // TRACE(D_PACKETS, "    %N -> %I metric %d", &rte.net, rte.next_hop, rte.metric);
501

    
502
    /* RIPng next hop entry */
503
    if (rip_is_ng(p) && !ipa_equal(rte.next_hop, last_next_hop))
504
    {
505
      last_next_hop = rte.next_hop;
506
      rip_put_next_hop(p, pos, &rte);
507
      pos += RIP_BLOCK_LENGTH;
508
    }
509

    
510
    rip_put_block(p, pos, &rte);
511
    pos += RIP_BLOCK_LENGTH;
512
    send = 1;
513

    
514
  next_entry: ;
515
  }
516
  FIB_ITERATE_END;
517
  ifa->tx_active = 0;
518

    
519
  /* Do not send empty packet */
520
  if (!send)
521
    return 0;
522

    
523
break_loop:
524
  TRACE(D_PACKETS, "Sending response via %s", ifa->iface->name);
525
  return rip_send_to(p, ifa, pkt, pos - (byte *) pkt, ifa->tx_addr);
526
}
527

    
528
/**
529
 * rip_send_table - RIP interface timer hook
530
 * @p: RIP instance
531
 * @ifa: RIP interface
532
 * @addr: destination IP address
533
 * @changed: time limit for triggered updates
534
 *
535
 * The function activates an update session and starts sending routing update
536
 * packets (using rip_send_response()). The session may be finished during the
537
 * call or may continue in rip_tx_hook() until all appropriate routes are
538
 * transmitted. Note that there may be at most one active update session per
539
 * interface, the function will terminate the old active session before
540
 * activating the new one.
541
 */
542
void
543
rip_send_table(struct rip_proto *p, struct rip_iface *ifa, ip_addr addr, bird_clock_t changed)
544
{
545
  DBG("RIP: Opening TX session to %I on %s\n", addr, ifa->iface->name);
546

    
547
  rip_reset_tx_session(p, ifa);
548

    
549
  ifa->tx_active = 1;
550
  ifa->tx_addr = addr;
551
  ifa->tx_changed = changed;
552
  FIB_ITERATE_INIT(&ifa->tx_fit, &p->rtable);
553

    
554
  rip_update_csn(p, ifa);
555

    
556
  while (rip_send_response(p, ifa) > 0)
557
    ;
558
}
559

    
560
static void
561
rip_tx_hook(sock *sk)
562
{
563
  struct rip_iface *ifa = sk->data;
564
  struct rip_proto *p = ifa->rip;
565

    
566
  DBG("RIP: TX hook called (iface %s, src %I, dst %I)\n",
567
      sk->iface->name, sk->saddr, sk->daddr);
568

    
569
  while (rip_send_response(p, ifa) > 0)
570
    ;
571
}
572

    
573
static void
574
rip_err_hook(sock *sk, int err)
575
{
576
  struct rip_iface *ifa = sk->data;
577
  struct rip_proto *p = ifa->rip;
578

    
579
  log(L_ERR "%s: Socket error on %s: %M", p->p.name, ifa->iface->name, err);
580

    
581
  rip_reset_tx_session(p, ifa);
582
}
583

    
584
static void
585
rip_receive_response(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, struct rip_neighbor *from)
586
{
587
  struct rip_block rte = {};
588
  const char *err_dsc = NULL;
589

    
590
  TRACE(D_PACKETS, "Response received from %I on %s", from->nbr->addr, ifa->iface->name);
591

    
592
  byte *pos = (byte *) pkt + sizeof(struct rip_packet);
593
  byte *end = (byte *) pkt + plen;
594

    
595
  for (; pos < end; pos += RIP_BLOCK_LENGTH)
596
  {
597
    /* Find next regular RTE */
598
    if (!rip_get_block(p, pos, &rte))
599
      continue;
600

    
601
    if (rip_is_v2(p) && (pkt->version == RIP_V1))
602
    {
603
      if (ifa->cf->check_zero && (rte.tag || rte.net.pxlen || ipa_nonzero(rte.next_hop)))
604
        SKIP("RIPv1 reserved field is nonzero");
605

    
606
      rte.tag = 0;
607
      rte.net.pxlen = ip4_masklen(ip4_class_mask(net4_prefix(&rte.net)));
608
      rte.next_hop = IPA_NONE;
609
    }
610

    
611
    if (rte.net.pxlen == 255)
612
      SKIP("invalid prefix length");
613

    
614
    net_normalize(&rte.net);
615

    
616
    int c = net_classify(&rte.net);
617
    if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
618
      SKIP("invalid prefix");
619

    
620
    if (rte.metric > p->infinity)
621
      SKIP("invalid metric");
622

    
623
    if (ipa_nonzero(rte.next_hop))
624
    {
625
      neighbor *nbr = neigh_find2(&p->p, &rte.next_hop, ifa->iface, 0);
626
      if (!nbr || (nbr->scope <= 0))
627
        rte.next_hop = IPA_NONE;
628
    }
629

    
630
    // TRACE(D_PACKETS, "    %N -> %I metric %d", &rte.net.n, rte.next_hop, rte.metric);
631

    
632
    rte.metric += ifa->cf->metric;
633

    
634
    if (rte.metric < p->infinity)
635
    {
636
      struct rip_rte new = {
637
        .from = from,
638
        .next_hop = ipa_nonzero(rte.next_hop) ? rte.next_hop : from->nbr->addr,
639
        .metric = rte.metric,
640
        .tag = rte.tag,
641
        .expires = now + ifa->cf->timeout_time
642
      };
643

    
644
      rip_update_rte(p, &rte.net, &new);
645
    }
646
    else
647
      rip_withdraw_rte(p, &rte.net, from);
648

    
649
    continue;
650

    
651
  skip:
652
    LOG_RTE("Ignoring route %N received from %I - %s",
653
            &rte.net, from->nbr->addr, err_dsc);
654
  }
655
}
656

    
657
static int
658
rip_rx_hook(sock *sk, uint len)
659
{
660
  struct rip_iface *ifa = sk->data;
661
  struct rip_proto *p = ifa->rip;
662
  const char *err_dsc = NULL;
663
  uint err_val = 0;
664

    
665
  if (sk->lifindex != sk->iface->index)
666
    return 1;
667

    
668
  DBG("RIP: RX hook called (iface %s, src %I, dst %I)\n",
669
      sk->iface->name, sk->faddr, sk->laddr);
670

    
671
  /* Silently ignore my own packets */
672
  if (ipa_equal(sk->faddr, sk->saddr))
673
    return 1;
674

    
675
  if (rip_is_ng(p) && !ipa_is_link_local(sk->faddr))
676
    DROP1("wrong src address");
677

    
678
  struct rip_neighbor *n = rip_get_neighbor(p, &sk->faddr, ifa);
679

    
680
  if (!n)
681
    DROP1("not from neighbor");
682

    
683
  if ((ifa->cf->ttl_security == 1) && (sk->rcv_ttl < 255))
684
    DROP("wrong TTL", sk->rcv_ttl);
685

    
686
  if (sk->fport != sk->dport)
687
    DROP("wrong src port", sk->fport);
688

    
689
  if (len < sizeof(struct rip_packet))
690
    DROP("too short", len);
691

    
692
  if (sk->flags & SKF_TRUNCATED)
693
    DROP("truncated", len);
694

    
695
  struct rip_packet *pkt = (struct rip_packet *) sk->rbuf;
696
  uint plen = len;
697

    
698
  if (!pkt->version || (ifa->cf->version_only && (pkt->version != ifa->cf->version)))
699
    DROP("wrong version", pkt->version);
700

    
701
  /* rip_check_authentication() has its own error logging */
702
  if (rip_is_v2(p) && !rip_check_authentication(p, ifa, pkt, &plen, n))
703
    return 1;
704

    
705
  if ((plen - sizeof(struct rip_packet)) % RIP_BLOCK_LENGTH)
706
    DROP("invalid length", plen);
707

    
708
  n->last_seen = now;
709
  rip_update_bfd(p, n);
710

    
711
  switch (pkt->command)
712
  {
713
  case RIP_CMD_REQUEST:
714
    rip_receive_request(p, ifa, pkt, plen, n);
715
    break;
716

    
717
  case RIP_CMD_RESPONSE:
718
    rip_receive_response(p, ifa, pkt, plen, n);
719
    break;
720

    
721
  default:
722
    DROP("unknown command", pkt->command);
723
  }
724
  return 1;
725

    
726
drop:
727
  LOG_PKT("Bad packet from %I via %s - %s (%u)",
728
          sk->faddr, sk->iface->name, err_dsc, err_val);
729

    
730
  return 1;
731
}
732

    
733
int
734
rip_open_socket(struct rip_iface *ifa)
735
{
736
  struct rip_proto *p = ifa->rip;
737

    
738
  sock *sk = sk_new(p->p.pool);
739
  sk->type = SK_UDP;
740
  sk->subtype = rip_is_v2(p) ? SK_IPV4 : SK_IPV6;
741
  sk->sport = ifa->cf->port;
742
  sk->dport = ifa->cf->port;
743
  sk->iface = ifa->iface;
744
  sk->saddr = rip_is_v2(p) ? ifa->iface->addr4->ip : ifa->iface->llv6->ip;
745

    
746
  sk->rx_hook = rip_rx_hook;
747
  sk->tx_hook = rip_tx_hook;
748
  sk->err_hook = rip_err_hook;
749
  sk->data = ifa;
750

    
751
  sk->tos = ifa->cf->tx_tos;
752
  sk->priority = ifa->cf->tx_priority;
753
  sk->ttl = ifa->cf->ttl_security ? 255 : 1;
754
  sk->flags = SKF_LADDR_RX | ((ifa->cf->ttl_security == 1) ? SKF_TTL_RX : 0);
755

    
756
  /* sk->rbsize and sk->tbsize are handled in rip_iface_update_buffers() */
757

    
758
  if (sk_open(sk) < 0)
759
    goto err;
760

    
761
  if (ifa->cf->mode == RIP_IM_MULTICAST)
762
  {
763
    if (sk_setup_multicast(sk) < 0)
764
      goto err;
765

    
766
    if (sk_join_group(sk, ifa->addr) < 0)
767
      goto err;
768
  }
769
  else /* Broadcast */
770
  {
771
    if (sk_setup_broadcast(sk) < 0)
772
      goto err;
773

    
774
    if (ipa_zero(ifa->addr))
775
    {
776
      sk->err = "Missing broadcast address";
777
      goto err;
778
    }
779
  }
780

    
781
  ifa->sk = sk;
782
  return 1;
783

    
784
err:
785
  sk_log_error(sk, p->p.name);
786
  rfree(sk);
787
  return 0;
788
}