Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / packets.c @ 600998fc

History | View | Annotate | Download (19.5 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
#include "rip.h"
13
#include "lib/md5.h"
14

    
15

    
16
#define RIP_CMD_REQUEST                1        /* want info */
17
#define RIP_CMD_RESPONSE        2        /* responding to request */
18

    
19
#define RIP_BLOCK_LENGTH        20
20

    
21
#define RIP_PASSWD_LENGTH        16
22
#define RIP_MD5_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[];
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, 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 = sizeof(struct rip_auth_tail) + RIP_MD5_LENGTH;
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
    /*
229
     * Note that RFC 4822 is unclear whether auth_len should cover whole
230
     * authentication trailer or just auth_data length.
231
     *
232
     * Crypto sequence numbers are increased by sender in rip_update_csn().
233
     * First CSN should be zero, this is handled by csn_ready.
234
     */
235

    
236
    struct rip_auth_tail *tail = (void *) ((byte *) pkt + *plen);
237
    tail->must_be_ffff = htons(0xffff);
238
    tail->must_be_0001 = htons(0x0001);
239
    strncpy(tail->auth_data, pass->password, RIP_MD5_LENGTH);
240

    
241
    *plen += sizeof(struct rip_auth_tail) + RIP_MD5_LENGTH;
242

    
243
    struct md5_context ctx;
244
    md5_init(&ctx);
245
    md5_update(&ctx, (byte *) pkt, *plen);
246
    memcpy(tail->auth_data, md5_final(&ctx), RIP_MD5_LENGTH);
247
    return;
248

    
249
  default:
250
    bug("Unknown authentication type");
251
  }
252
}
253

    
254
static int
255
rip_check_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint *plen, struct rip_neighbor *n)
256
{
257
  struct rip_block_auth *auth = (void *) (pkt + 1);
258
  struct password_item *pass = NULL;
259
  const char *err_dsc = NULL;
260
  uint err_val = 0;
261
  uint auth_type = 0;
262

    
263
  /* Check for authentication entry */
264
  if ((*plen >= (sizeof(struct rip_packet) + sizeof(struct rip_block_auth))) &&
265
      (auth->must_be_ffff == htons(0xffff)))
266
    auth_type = ntohs(auth->auth_type);
267

    
268
  if (auth_type != ifa->cf->auth_type)
269
    DROP("authentication method mismatch", auth_type);
270

    
271
  switch (auth_type)
272
  {
273
  case RIP_AUTH_NONE:
274
    return 1;
275

    
276
  case RIP_AUTH_PLAIN:
277
    pass = password_find_by_value(ifa->cf->passwords, auth->password, RIP_PASSWD_LENGTH);
278
    if (!pass)
279
      DROP1("wrong password");
280

    
281
    return 1;
282

    
283
  case RIP_AUTH_CRYPTO:
284
    pass = password_find_by_id(ifa->cf->passwords, auth->key_id);
285
    if (!pass)
286
      DROP("no suitable password found", auth->key_id);
287

    
288
    uint data_len = ntohs(auth->packet_len);
289
    uint auth_len = sizeof(struct rip_auth_tail) + RIP_MD5_LENGTH;
290

    
291
    if (data_len + auth_len != *plen)
292
      DROP("packet length mismatch", data_len);
293

    
294
    if ((auth->auth_len != RIP_MD5_LENGTH) && (auth->auth_len != auth_len))
295
      DROP("authentication data length mismatch", auth->auth_len);
296

    
297
    struct rip_auth_tail *tail = (void *) ((byte *) pkt + data_len);
298
    if ((tail->must_be_ffff != htons(0xffff)) || (tail->must_be_0001 != htons(0x0001)))
299
      DROP1("authentication trailer is missing");
300

    
301
    /* Accept higher sequence number, or zero if connectivity is lost */
302
    /* FIXME: sequence number must be password/SA specific */
303
    u32 rcv_csn = ntohl(auth->seq_num);
304
    if ((rcv_csn < n->csn) && (rcv_csn || n->uc))
305
    {
306
      /* We want to report both new and old CSN */
307
      LOG_PKT_AUTH("Authentication failed for %I on %s - "
308
                   "lower sequence number (rcv %u, old %u)",
309
                   n->nbr->addr, ifa->iface->name, rcv_csn, n->csn);
310
      return 0;
311
    }
312

    
313
    char received[RIP_MD5_LENGTH];
314
    memcpy(received, tail->auth_data, RIP_MD5_LENGTH);
315
    strncpy(tail->auth_data, pass->password, RIP_MD5_LENGTH);
316

    
317
    struct md5_context ctx;
318
    md5_init(&ctx);
319
    md5_update(&ctx, (byte *) pkt, *plen);
320
    char *computed = md5_final(&ctx);
321

    
322
    if (memcmp(received, computed, RIP_MD5_LENGTH))
323
      DROP("wrong MD5 digest", pass->id);
324

    
325
    *plen = data_len;
326
    n->csn = rcv_csn;
327

    
328
    return 1;
329
  }
330

    
331
drop:
332
  LOG_PKT_AUTH("Authentication failed for %I on %s - %s (%u)",
333
               n->nbr->addr, ifa->iface->name, err_dsc, err_val);
334

    
335
  return 0;
336
}
337

    
338
static inline int
339
rip_send_to(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, ip_addr dst)
340
{
341
  if (ifa->cf->auth_type)
342
    rip_fill_authentication(p, ifa, pkt, &plen);
343

    
344
  return sk_send_to(ifa->sk, plen, dst, 0);
345
}
346

    
347

    
348
void
349
rip_send_request(struct rip_proto *p, struct rip_iface *ifa)
350
{
351
  byte *pos = rip_tx_buffer(ifa);
352

    
353
  struct rip_packet *pkt = (void *) pos;
354
  pkt->command = RIP_CMD_REQUEST;
355
  pkt->version = ifa->cf->version;
356
  pkt->unused = 0;
357
  pos += rip_pkt_hdrlen(ifa);
358

    
359
  struct rip_block b = { .no_af = 1, .metric = p->infinity };
360
  rip_put_block(p, pos, &b);
361
  pos += RIP_BLOCK_LENGTH;
362

    
363
  rip_update_csn(p, ifa);
364

    
365
  TRACE(D_PACKETS, "Sending request via %s", ifa->iface->name);
366
  rip_send_to(p, ifa, pkt, pos - (byte *) pkt, ifa->addr);
367
}
368

    
369
static void
370
rip_receive_request(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, struct rip_neighbor *from)
371
{
372
  TRACE(D_PACKETS, "Request received from %I on %s", from->nbr->addr, ifa->iface->name);
373

    
374
  byte *pos = (byte *) pkt + rip_pkt_hdrlen(ifa);
375

    
376
  /* We expect one regular block */
377
  if (plen != (rip_pkt_hdrlen(ifa) + RIP_BLOCK_LENGTH))
378
    return;
379

    
380
  struct rip_block b = { .no_af = 1 };
381

    
382
  if (!rip_get_block(p, pos, &b))
383
    return;
384

    
385
  /* Special case - infinity metric, for RIPng also zero prefix */
386
  if ((b.metric != p->infinity) ||
387
      (rip_is_ng(p) && !net_zero_ip6((net_addr_ip6 *) &b.net)))
388
    return;
389

    
390
  /* We do nothing if TX is already active */
391
  if (ifa->tx_active)
392
  {
393
    TRACE(D_EVENTS, "Skipping request from %I on %s, TX is busy", from->nbr->addr, ifa->iface->name);
394
    return;
395
  }
396

    
397
  if (!ifa->cf->passive)
398
    rip_send_table(p, ifa, from->nbr->addr, 0);
399
}
400

    
401

    
402
static int
403
rip_send_response(struct rip_proto *p, struct rip_iface *ifa)
404
{
405
  if (! ifa->tx_active)
406
    return 0;
407

    
408
  byte *pos = rip_tx_buffer(ifa);
409
  byte *max = rip_tx_buffer(ifa) + ifa->tx_plen -
410
    (rip_is_v2(p) ? RIP_BLOCK_LENGTH : 2*RIP_BLOCK_LENGTH);
411
  ip_addr last_next_hop = IPA_NONE;
412
  int send = 0;
413

    
414
  struct rip_packet *pkt = (void *) pos;
415
  pkt->command = RIP_CMD_RESPONSE;
416
  pkt->version = ifa->cf->version;
417
  pkt->unused = 0;
418
  pos += rip_pkt_hdrlen(ifa);
419

    
420
  FIB_ITERATE_START(&p->rtable, &ifa->tx_fit, struct rip_entry, en)
421
  {
422
    /* Dummy entries */
423
    if (!en->valid)
424
      goto next_entry;
425

    
426
    /* Stale entries that should be removed */
427
    if ((en->valid == RIP_ENTRY_STALE) &&
428
        ((en->changed + ifa->cf->garbage_time) <= now))
429
      goto next_entry;
430

    
431
    /* Triggered updates */
432
    if (en->changed < ifa->tx_changed)
433
      goto next_entry;
434

    
435
    /* Not enough space for current entry */
436
    if (pos > max)
437
    {
438
      FIB_ITERATE_PUT(&ifa->tx_fit);
439
      goto break_loop;
440
    }
441

    
442
    struct rip_block rte = {
443
      .metric = en->metric,
444
      .tag = en->tag
445
    };
446

    
447
    net_copy(&rte.net, en->n.addr);
448

    
449
    if (en->iface == ifa->iface)
450
      rte.next_hop = en->next_hop;
451

    
452
    if (rip_is_v2(p) && (ifa->cf->version == RIP_V1))
453
    {
454
      /* Skipping subnets (i.e. not hosts, classful networks or default route) */
455
      if (ip4_masklen(ip4_class_mask(net4_prefix(&rte.net))) != rte.net.pxlen)
456
        goto next_entry;
457

    
458
      rte.tag = 0;
459
      rte.net.pxlen = 0;
460
      rte.next_hop = IPA_NONE;
461
    }
462

    
463
    /* Split horizon */
464
    if (en->from == ifa->iface && ifa->cf->split_horizon)
465
    {
466
      if (ifa->cf->poison_reverse)
467
      {
468
        rte.metric = p->infinity;
469
        rte.next_hop = IPA_NONE;
470
      }
471
      else
472
        goto next_entry;
473
    }
474

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

    
477
    /* RIPng next hop entry */
478
    if (rip_is_ng(p) && !ipa_equal(rte.next_hop, last_next_hop))
479
    {
480
      last_next_hop = rte.next_hop;
481
      rip_put_next_hop(p, pos, &rte);
482
      pos += RIP_BLOCK_LENGTH;
483
    }
484

    
485
    rip_put_block(p, pos, &rte);
486
    pos += RIP_BLOCK_LENGTH;
487
    send = 1;
488

    
489
  next_entry: ;
490
  }
491
  FIB_ITERATE_END;
492
  ifa->tx_active = 0;
493

    
494
  /* Do not send empty packet */
495
  if (!send)
496
    return 0;
497

    
498
break_loop:
499
  TRACE(D_PACKETS, "Sending response via %s", ifa->iface->name);
500
  return rip_send_to(p, ifa, pkt, pos - (byte *) pkt, ifa->tx_addr);
501
}
502

    
503
/**
504
 * rip_send_table - RIP interface timer hook
505
 * @p: RIP instance
506
 * @ifa: RIP interface
507
 * @addr: destination IP address
508
 * @changed: time limit for triggered updates
509
 *
510
 * The function activates an update session and starts sending routing update
511
 * packets (using rip_send_response()). The session may be finished during the
512
 * call or may continue in rip_tx_hook() until all appropriate routes are
513
 * transmitted. Note that there may be at most one active update session per
514
 * interface, the function will terminate the old active session before
515
 * activating the new one.
516
 */
517
void
518
rip_send_table(struct rip_proto *p, struct rip_iface *ifa, ip_addr addr, bird_clock_t changed)
519
{
520
  DBG("RIP: Opening TX session to %I on %s\n", dst, ifa->iface->name);
521

    
522
  rip_reset_tx_session(p, ifa);
523

    
524
  ifa->tx_active = 1;
525
  ifa->tx_addr = addr;
526
  ifa->tx_changed = changed;
527
  FIB_ITERATE_INIT(&ifa->tx_fit, &p->rtable);
528

    
529
  rip_update_csn(p, ifa);
530

    
531
  while (rip_send_response(p, ifa) > 0)
532
    ;
533
}
534

    
535
static void
536
rip_tx_hook(sock *sk)
537
{
538
  struct rip_iface *ifa = sk->data;
539
  struct rip_proto *p = ifa->rip;
540

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

    
544
  while (rip_send_response(p, ifa) > 0)
545
    ;
546
}
547

    
548
static void
549
rip_err_hook(sock *sk, int err)
550
{
551
  struct rip_iface *ifa = sk->data;
552
  struct rip_proto *p = ifa->rip;
553

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

    
556
  rip_reset_tx_session(p, ifa);
557
}
558

    
559
static void
560
rip_receive_response(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, struct rip_neighbor *from)
561
{
562
  struct rip_block rte = {};
563
  const char *err_dsc = NULL;
564

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

    
567
  byte *pos = (byte *) pkt + sizeof(struct rip_packet);
568
  byte *end = (byte *) pkt + plen;
569

    
570
  for (; pos < end; pos += RIP_BLOCK_LENGTH)
571
  {
572
    /* Find next regular RTE */
573
    if (!rip_get_block(p, pos, &rte))
574
      continue;
575

    
576
    if (rip_is_v2(p) && (pkt->version == RIP_V1))
577
    {
578
      if (ifa->cf->check_zero && (rte.tag || rte.net.pxlen || ipa_nonzero(rte.next_hop)))
579
        SKIP("RIPv1 reserved field is nonzero");
580

    
581
      rte.tag = 0;
582
      rte.net.pxlen = ip4_masklen(ip4_class_mask(net4_prefix(&rte.net)));
583
      rte.next_hop = IPA_NONE;
584
    }
585

    
586
    if (rte.net.pxlen == 255)
587
      SKIP("invalid prefix length");
588

    
589
    net_normalize(&rte.net);
590

    
591
    int c = net_classify(&rte.net);
592
    if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
593
      SKIP("invalid prefix");
594

    
595
    if (rte.metric > p->infinity)
596
      SKIP("invalid metric");
597

    
598
    if (ipa_nonzero(rte.next_hop))
599
    {
600
      neighbor *nbr = neigh_find2(&p->p, &rte.next_hop, ifa->iface, 0);
601
      if (!nbr || (nbr->scope <= 0))
602
        rte.next_hop = IPA_NONE;
603
    }
604

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

    
607
    rte.metric += ifa->cf->metric;
608

    
609
    if (rte.metric < p->infinity)
610
    {
611
      struct rip_rte new = {
612
        .from = from,
613
        .next_hop = ipa_nonzero(rte.next_hop) ? rte.next_hop : from->nbr->addr,
614
        .metric = rte.metric,
615
        .tag = rte.tag,
616
        .expires = now + ifa->cf->timeout_time
617
      };
618

    
619
      rip_update_rte(p, &rte.net, &new);
620
    }
621
    else
622
      rip_withdraw_rte(p, &rte.net, from);
623

    
624
    continue;
625

    
626
  skip:
627
    LOG_RTE("Ignoring route %N received from %I - %s",
628
            &rte.net, from->nbr->addr, err_dsc);
629
  }
630
}
631

    
632
static int
633
rip_rx_hook(sock *sk, int len)
634
{
635
  struct rip_iface *ifa = sk->data;
636
  struct rip_proto *p = ifa->rip;
637
  const char *err_dsc = NULL;
638
  uint err_val = 0;
639

    
640
  if (sk->lifindex != sk->iface->index)
641
    return 1;
642

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

    
646
  /* Silently ignore my own packets */
647
  /* FIXME: Better local address check */
648
  if (ipa_equal(ifa->iface->addr->ip, sk->faddr))
649
    return 1;
650

    
651
  if (rip_is_ng(p) && !ipa_is_link_local(sk->faddr))
652
    DROP1("wrong src address");
653

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

    
656
  if (!n)
657
    DROP1("not from neighbor");
658

    
659
  if ((ifa->cf->ttl_security == 1) && (sk->rcv_ttl < 255))
660
    DROP("wrong TTL", sk->rcv_ttl);
661

    
662
  if (sk->fport != sk->dport)
663
    DROP("wrong src port", sk->fport);
664

    
665
  if (len < sizeof(struct rip_packet))
666
    DROP("too short", len);
667

    
668
  if (sk->flags & SKF_TRUNCATED)
669
    DROP("truncated", len);
670

    
671
  struct rip_packet *pkt = (struct rip_packet *) sk->rbuf;
672
  uint plen = len;
673

    
674
  if (!pkt->version || (ifa->cf->version_only && (pkt->version != ifa->cf->version)))
675
    DROP("wrong version", pkt->version);
676

    
677
  /* rip_check_authentication() has its own error logging */
678
  if (rip_is_v2(p) && !rip_check_authentication(p, ifa, pkt, &plen, n))
679
    return 1;
680

    
681
  if ((plen - sizeof(struct rip_packet)) % RIP_BLOCK_LENGTH)
682
    DROP("invalid length", plen);
683

    
684
  n->last_seen = now;
685
  rip_update_bfd(p, n);
686

    
687
  switch (pkt->command)
688
  {
689
  case RIP_CMD_REQUEST:
690
    rip_receive_request(p, ifa, pkt, plen, n);
691
    break;
692

    
693
  case RIP_CMD_RESPONSE:
694
    rip_receive_response(p, ifa, pkt, plen, n);
695
    break;
696

    
697
  default:
698
    DROP("unknown command", pkt->command);
699
  }
700
  return 1;
701

    
702
drop:
703
  LOG_PKT("Bad packet from %I via %s - %s (%u)",
704
          sk->faddr, sk->iface->name, err_dsc, err_val);
705

    
706
  return 1;
707
}
708

    
709
int
710
rip_open_socket(struct rip_iface *ifa)
711
{
712
  struct rip_proto *p = ifa->rip;
713

    
714
  sock *sk = sk_new(p->p.pool);
715
  sk->type = SK_UDP;
716
  sk->af = rip_is_v2(p) ? AF_INET : AF_INET6;
717
  sk->sport = ifa->cf->port;
718
  sk->dport = ifa->cf->port;
719
  sk->iface = ifa->iface;
720

    
721
  /*
722
   * For RIPv2, we explicitly choose a primary address, mainly to ensure that
723
   * RIP and BFD uses the same one. For RIPng, we left it to kernel, which
724
   * should choose some link-local address based on the same scope rule.
725
   */
726
  if (rip_is_v2(p))
727
    sk->saddr = ifa->iface->addr->ip;
728

    
729
  sk->rx_hook = rip_rx_hook;
730
  sk->tx_hook = rip_tx_hook;
731
  sk->err_hook = rip_err_hook;
732
  sk->data = ifa;
733

    
734
  sk->tos = ifa->cf->tx_tos;
735
  sk->priority = ifa->cf->tx_priority;
736
  sk->ttl = ifa->cf->ttl_security ? 255 : 1;
737
  sk->flags = SKF_LADDR_RX | (rip_is_ng(p) ? SKF_V6ONLY : 0) |
738
    ((ifa->cf->ttl_security == 1) ? SKF_TTL_RX : 0);
739

    
740
  /* sk->rbsize and sk->tbsize are handled in rip_iface_update_buffers() */
741

    
742
  if (sk_open(sk) < 0)
743
    goto err;
744

    
745
  if (ifa->cf->mode == RIP_IM_MULTICAST)
746
  {
747
    if (sk_setup_multicast(sk) < 0)
748
      goto err;
749

    
750
    if (sk_join_group(sk, ifa->addr) < 0)
751
      goto err;
752
  }
753
  else /* Broadcast */
754
  {
755
    if (sk_setup_broadcast(sk) < 0)
756
      goto err;
757

    
758
    if (ipa_zero(ifa->addr))
759
    {
760
      sk->err = "Missing broadcast address";
761
      goto err;
762
    }
763
  }
764

    
765
  ifa->sk = sk;
766
  return 1;
767

    
768
err:
769
  sk_log_error(sk, p->p.name);
770
  rfree(sk);
771
  return 0;
772
}