Statistics
| Branch: | Revision:

iof-bird-daemon / proto / rip / packets.c @ fe9f1a6d

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 MD5Context ctxt;
244
    MD5Init(&ctxt);
245
    MD5Update(&ctxt, (byte *) pkt, *plen);
246
    MD5Final(tail->auth_data, &ctxt);
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
    char computed[RIP_MD5_LENGTH];
315

    
316
    memcpy(received, tail->auth_data, RIP_MD5_LENGTH);
317
    strncpy(tail->auth_data, pass->password, RIP_MD5_LENGTH);
318

    
319
    struct MD5Context ctxt;
320
    MD5Init(&ctxt);
321
    MD5Update(&ctxt, (byte *) pkt, *plen);
322
    MD5Final(computed, &ctxt);
323

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

    
327
    *plen = data_len;
328
    n->csn = rcv_csn;
329

    
330
    return 1;
331
  }
332

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

    
337
  return 0;
338
}
339

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

    
346
  return sk_send_to(ifa->sk, plen, dst, 0);
347
}
348

    
349

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

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

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

    
365
  rip_update_csn(p, ifa);
366

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

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

    
376
  byte *pos = (byte *) pkt + rip_pkt_hdrlen(ifa);
377

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

    
382
  struct rip_block b = { .no_af = 1 };
383

    
384
  if (!rip_get_block(p, pos, &b))
385
    return;
386

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

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

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

    
403

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

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

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

    
422
  FIB_ITERATE_START(&p->rtable, &ifa->tx_fit, z)
423
  {
424
    struct rip_entry *en = (struct rip_entry *) z;
425

    
426
    /* Dummy entries */
427
    if (!en->valid)
428
      goto next_entry;
429

    
430
    /* Stale entries that should be removed */
431
    if ((en->valid == RIP_ENTRY_STALE) &&
432
        ((en->changed + ifa->cf->garbage_time) <= now))
433
      goto next_entry;
434

    
435
    /* Triggered updates */
436
    if (en->changed < ifa->tx_changed)
437
      goto next_entry;
438

    
439
    /* Not enough space for current entry */
440
    if (pos > max)
441
    {
442
      FIB_ITERATE_PUT(&ifa->tx_fit, z);
443
      goto break_loop;
444
    }
445

    
446
    struct rip_block rte = {
447
      .metric = en->metric,
448
      .tag = en->tag
449
    };
450

    
451
    net_copy(&rte.net, en->n.addr);
452

    
453
    if (en->iface == ifa->iface)
454
      rte.next_hop = en->next_hop;
455

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

    
462
      rte.tag = 0;
463
      rte.net.pxlen = 0;
464
      rte.next_hop = IPA_NONE;
465
    }
466

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

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

    
481
    /* RIPng next hop entry */
482
    if (rip_is_ng(p) && !ipa_equal(rte.next_hop, last_next_hop))
483
    {
484
      last_next_hop = rte.next_hop;
485
      rip_put_next_hop(p, pos, &rte);
486
      pos += RIP_BLOCK_LENGTH;
487
    }
488

    
489
    rip_put_block(p, pos, &rte);
490
    pos += RIP_BLOCK_LENGTH;
491
    send = 1;
492

    
493
  next_entry: ;
494
  }
495
  FIB_ITERATE_END(z);
496
  ifa->tx_active = 0;
497

    
498
  /* Do not send empty packet */
499
  if (!send)
500
    return 0;
501

    
502
break_loop:
503
  TRACE(D_PACKETS, "Sending response via %s", ifa->iface->name);
504
  return rip_send_to(p, ifa, pkt, pos - (byte *) pkt, ifa->tx_addr);
505
}
506

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

    
526
  rip_reset_tx_session(p, ifa);
527

    
528
  ifa->tx_active = 1;
529
  ifa->tx_addr = addr;
530
  ifa->tx_changed = changed;
531
  FIB_ITERATE_INIT(&ifa->tx_fit, &p->rtable);
532

    
533
  rip_update_csn(p, ifa);
534

    
535
  while (rip_send_response(p, ifa) > 0)
536
    ;
537
}
538

    
539
static void
540
rip_tx_hook(sock *sk)
541
{
542
  struct rip_iface *ifa = sk->data;
543
  struct rip_proto *p = ifa->rip;
544

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

    
548
  while (rip_send_response(p, ifa) > 0)
549
    ;
550
}
551

    
552
static void
553
rip_err_hook(sock *sk, int err)
554
{
555
  struct rip_iface *ifa = sk->data;
556
  struct rip_proto *p = ifa->rip;
557

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

    
560
  rip_reset_tx_session(p, ifa);
561
}
562

    
563
static void
564
rip_receive_response(struct rip_proto *p, struct rip_iface *ifa, struct rip_packet *pkt, uint plen, struct rip_neighbor *from)
565
{
566
  struct rip_block rte = {};
567
  const char *err_dsc = NULL;
568

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

    
571
  byte *pos = (byte *) pkt + sizeof(struct rip_packet);
572
  byte *end = (byte *) pkt + plen;
573

    
574
  for (; pos < end; pos += RIP_BLOCK_LENGTH)
575
  {
576
    /* Find next regular RTE */
577
    if (!rip_get_block(p, pos, &rte))
578
      continue;
579

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

    
585
      rte.tag = 0;
586
      rte.net.pxlen = ip4_masklen(ip4_class_mask(net4_prefix(&rte.net)));
587
      rte.next_hop = IPA_NONE;
588
    }
589

    
590
    if (rte.net.pxlen == 255)
591
      SKIP("invalid prefix length");
592

    
593
    net_normalize(&rte.net);
594

    
595
    int c = net_classify(&rte.net);
596
    if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
597
      SKIP("invalid prefix");
598

    
599
    if (rte.metric > p->infinity)
600
      SKIP("invalid metric");
601

    
602
    if (ipa_nonzero(rte.next_hop))
603
    {
604
      neighbor *nbr = neigh_find2(&p->p, &rte.next_hop, ifa->iface, 0);
605
      if (!nbr || (nbr->scope <= 0))
606
        rte.next_hop = IPA_NONE;
607
    }
608

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

    
611
    rte.metric += ifa->cf->metric;
612

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

    
623
      rip_update_rte(p, &rte.net, &new);
624
    }
625
    else
626
      rip_withdraw_rte(p, &rte.net, from);
627

    
628
    continue;
629

    
630
  skip:
631
    LOG_RTE("Ignoring route %N received from %I - %s",
632
            &rte.net, from->nbr->addr, err_dsc);
633
  }
634
}
635

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

    
644
  if (sk->lifindex != sk->iface->index)
645
    return 1;
646

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

    
650
  /* Silently ignore my own packets */
651
  /* FIXME: Better local address check */
652
  if (ipa_equal(ifa->iface->addr->ip, sk->faddr))
653
    return 1;
654

    
655
  if (rip_is_ng(p) && !ipa_is_link_local(sk->faddr))
656
    DROP1("wrong src address");
657

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

    
660
  if (!n)
661
    DROP1("not from neighbor");
662

    
663
  if ((ifa->cf->ttl_security == 1) && (sk->rcv_ttl < 255))
664
    DROP("wrong TTL", sk->rcv_ttl);
665

    
666
  if (sk->fport != sk->dport)
667
    DROP("wrong src port", sk->fport);
668

    
669
  if (len < sizeof(struct rip_packet))
670
    DROP("too short", len);
671

    
672
  if (sk->flags & SKF_TRUNCATED)
673
    DROP("truncated", len);
674

    
675
  struct rip_packet *pkt = (struct rip_packet *) sk->rbuf;
676
  uint plen = len;
677

    
678
  if (!pkt->version || (ifa->cf->version_only && (pkt->version != ifa->cf->version)))
679
    DROP("wrong version", pkt->version);
680

    
681
  /* rip_check_authentication() has its own error logging */
682
  if (rip_is_v2(p) && !rip_check_authentication(p, ifa, pkt, &plen, n))
683
    return 1;
684

    
685
  if ((plen - sizeof(struct rip_packet)) % RIP_BLOCK_LENGTH)
686
    DROP("invalid length", plen);
687

    
688
  n->last_seen = now;
689
  rip_update_bfd(p, n);
690

    
691
  switch (pkt->command)
692
  {
693
  case RIP_CMD_REQUEST:
694
    rip_receive_request(p, ifa, pkt, plen, n);
695
    break;
696

    
697
  case RIP_CMD_RESPONSE:
698
    rip_receive_response(p, ifa, pkt, plen, n);
699
    break;
700

    
701
  default:
702
    DROP("unknown command", pkt->command);
703
  }
704
  return 1;
705

    
706
drop:
707
  LOG_PKT("Bad packet from %I via %s - %s (%u)",
708
          sk->faddr, sk->iface->name, err_dsc, err_val);
709

    
710
  return 1;
711
}
712

    
713
int
714
rip_open_socket(struct rip_iface *ifa)
715
{
716
  struct rip_proto *p = ifa->rip;
717

    
718
  sock *sk = sk_new(p->p.pool);
719
  sk->type = SK_UDP;
720
  sk->sport = ifa->cf->port;
721
  sk->dport = ifa->cf->port;
722
  sk->iface = ifa->iface;
723

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

    
732
  sk->rx_hook = rip_rx_hook;
733
  sk->tx_hook = rip_tx_hook;
734
  sk->err_hook = rip_err_hook;
735
  sk->data = ifa;
736

    
737
  sk->tos = ifa->cf->tx_tos;
738
  sk->priority = ifa->cf->tx_priority;
739
  sk->ttl = ifa->cf->ttl_security ? 255 : 1;
740
  sk->flags = SKF_LADDR_RX | ((ifa->cf->ttl_security == 1) ? SKF_TTL_RX : 0);
741

    
742
  /* sk->rbsize and sk->tbsize are handled in rip_iface_update_buffers() */
743

    
744
  if (sk_open(sk) < 0)
745
    goto err;
746

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

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

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

    
767
  ifa->sk = sk;
768
  return 1;
769

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