Revision 4324025f proto/babel/packets.c

View differences:

proto/babel/packets.c
112 112
  struct babel_proto *proto;
113 113
  struct babel_iface *ifa;
114 114
  ip_addr saddr;
115
  ip_addr next_hop;
115
  ip_addr next_hop_ip4;
116
  ip_addr next_hop_ip6;
116 117
  u64 router_id;		/* Router ID used in subsequent updates */
117 118
  u8 def_ip6_prefix[16];	/* Implicit IPv6 prefix in network order */
118 119
  u8 def_ip4_prefix[4];		/* Implicit IPv4 prefix in network order */
......
130 131
struct babel_write_state {
131 132
  u64 router_id;
132 133
  u8 router_id_seen;
133
//  ip_addr next_hop;
134
  ip_addr next_hop_ip4;
135
  ip_addr next_hop_ip6;
134 136
};
135 137

  
136 138

  
......
163 165
}
164 166

  
165 167
static inline void
168
read_ip4_px(net_addr *n, const void *p, uint plen)
169
{
170
  ip4_addr addr = {0};
171
  memcpy(&addr, p, BYTES(plen));
172
  net_fill_ip4(n, ip4_ntoh(addr), plen);
173
}
174

  
175
static inline void
176
put_ip4_px(void *p, net_addr *n)
177
{
178
  ip4_addr addr = ip4_hton(net4_prefix(n));
179
  memcpy(p, &addr, NET_SIZE(n));
180
}
181

  
182
static inline void
166 183
read_ip6_px(net_addr *n, const void *p, uint plen)
167 184
{
168 185
  ip6_addr addr = IPA_NONE;
......
432 449
    return PARSE_ERROR;
433 450

  
434 451
  case BABEL_AE_IP4:
435
    /* TODO */
452
    if (TLV_OPT_LENGTH(tlv) < sizeof(ip4_addr))
453
      return PARSE_ERROR;
454

  
455
    state->next_hop_ip4 = ipa_from_ip4(get_ip4(&tlv->addr));
436 456
    return PARSE_IGNORE;
437 457

  
438 458
  case BABEL_AE_IP6:
439 459
    if (TLV_OPT_LENGTH(tlv) < sizeof(ip6_addr))
440 460
      return PARSE_ERROR;
441 461

  
442
    state->next_hop = ipa_from_ip6(get_ip6(&tlv->addr));
462
    state->next_hop_ip6 = ipa_from_ip6(get_ip6(&tlv->addr));
443 463
    return PARSE_IGNORE;
444 464

  
445 465
  case BABEL_AE_IP6_LL:
446 466
    if (TLV_OPT_LENGTH(tlv) < 8)
447 467
      return PARSE_ERROR;
448 468

  
449
    state->next_hop = ipa_from_ip6(get_ip6_ll(&tlv->addr));
469
    state->next_hop_ip6 = ipa_from_ip6(get_ip6_ll(&tlv->addr));
450 470
    return PARSE_IGNORE;
451 471

  
452 472
  default:
......
456 476
  return PARSE_IGNORE;
457 477
}
458 478

  
479
/* This is called directly from babel_write_update() and returns -1 if a next
480
   hop should be written but there is not enough space. */
481
static int
482
babel_write_next_hop(struct babel_tlv *hdr, ip_addr addr,
483
		     struct babel_write_state *state, uint max_len)
484
{
485
  struct babel_tlv_next_hop *tlv = (void *) hdr;
486

  
487
  if (ipa_zero(addr))
488
  {
489
    /* Should not happen */
490
    return 0;
491
  }
492
  else if (ipa_is_ip4(addr) && !ipa_equal(addr, state->next_hop_ip4))
493
  {
494
    uint len = sizeof(struct babel_tlv_next_hop) + sizeof(ip4_addr);
495
    if (len > max_len)
496
      return -1;
497

  
498
    TLV_HDR(tlv, BABEL_TLV_NEXT_HOP, len);
499

  
500
    tlv->ae = BABEL_AE_IP4;
501
    put_ip4(&tlv->addr, ipa_to_ip4(addr));
502
    state->next_hop_ip4 = addr;
503

  
504
    return len;
505
  }
506
  else if (ipa_is_ip6(addr) && !ipa_equal(addr, state->next_hop_ip6))
507
  {
508
    uint len = sizeof(struct babel_tlv_next_hop) + sizeof(ip6_addr);
509
    if (len > max_len)
510
      return -1;
511

  
512
    TLV_HDR(tlv, BABEL_TLV_NEXT_HOP, len);
513

  
514
    tlv->ae = BABEL_AE_IP6;
515
    put_ip6(&tlv->addr, ipa_to_ip6(addr));
516
    state->next_hop_ip6 = addr;
517

  
518
    return len;
519
  }
520

  
521
  return 0;
522
}
523

  
459 524
static int
460 525
babel_read_update(struct babel_tlv *hdr, union babel_msg *m,
461 526
                  struct babel_parse_state *state)
......
488 553
    break;
489 554

  
490 555
  case BABEL_AE_IP4:
491
    /* TODO */
492
    return PARSE_IGNORE;
556
    if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
557
      return PARSE_ERROR;
558

  
559
    /* Cannot omit data if there is no saved prefix */
560
    if (tlv->omitted && !state->def_ip4_prefix_seen)
561
      return PARSE_ERROR;
562

  
563
    /* Need next hop for v4 routes */
564
    if (ipa_zero(state->next_hop_ip4))
565
      return PARSE_ERROR;
566

  
567
    /* Merge saved prefix and received prefix parts */
568
    memcpy(buf, state->def_ip4_prefix, tlv->omitted);
569
    memcpy(buf + tlv->omitted, tlv->addr, len);
570

  
571
    ip4_addr prefix4 = get_ip4(buf);
572
    net_fill_ip4(&msg->net, prefix4, tlv->plen);
573

  
574
    if (tlv->flags & BABEL_FLAG_DEF_PREFIX)
575
    {
576
      put_ip4(state->def_ip4_prefix, prefix4);
577
      state->def_ip4_prefix_seen = 1;
578
    }
579

  
580
    msg->next_hop = state->next_hop_ip4;
581

  
582
    break;
493 583

  
494 584
  case BABEL_AE_IP6:
495 585
    if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
......
503 593
    memcpy(buf, state->def_ip6_prefix, tlv->omitted);
504 594
    memcpy(buf + tlv->omitted, tlv->addr, len);
505 595

  
506
    ip6_addr prefix = get_ip6(buf);
507
    net_fill_ip6(&msg->net, prefix, tlv->plen);
596
    ip6_addr prefix6 = get_ip6(buf);
597
    net_fill_ip6(&msg->net, prefix6, tlv->plen);
508 598

  
509 599
    if (tlv->flags & BABEL_FLAG_DEF_PREFIX)
510 600
    {
511
      put_ip6(state->def_ip6_prefix, prefix);
601
      put_ip6(state->def_ip6_prefix, prefix6);
512 602
      state->def_ip6_prefix_seen = 1;
513 603
    }
514 604

  
515 605
    if (tlv->flags & BABEL_FLAG_ROUTER_ID)
516 606
    {
517
      state->router_id = ((u64) _I2(prefix)) << 32 | _I3(prefix);
607
      state->router_id = ((u64) _I2(prefix6)) << 32 | _I3(prefix6);
518 608
      state->router_id_seen = 1;
519 609
    }
610

  
611
    msg->next_hop = state->next_hop_ip6;
612

  
520 613
    break;
521 614

  
522 615
  case BABEL_AE_IP6_LL:
......
535 628
  }
536 629

  
537 630
  msg->router_id = state->router_id;
538
  msg->next_hop = state->next_hop;
539 631
  msg->sender = state->saddr;
540 632

  
541 633
  return PARSE_SUCCESS;
......
545 637
babel_write_update(struct babel_tlv *hdr, union babel_msg *m,
546 638
                   struct babel_write_state *state, uint max_len)
547 639
{
548
  struct babel_tlv_update *tlv = (void *) hdr;
549 640
  struct babel_msg_update *msg = &m->update;
550 641
  uint len0 = 0;
551 642

  
......
554 645
   * both of them. There is enough space for the Router-ID TLV, because
555 646
   * sizeof(struct babel_tlv_router_id) == sizeof(struct babel_tlv_update).
556 647
   *
557
   * Router ID is not used for retractions, so do not us it in such case.
648
   * Router ID is not used for retractions, so do not use it in such case.
558 649
   */
559 650
  if ((msg->metric < BABEL_INFINITY) &&
560 651
      (!state->router_id_seen || (msg->router_id != state->router_id)))
561 652
  {
562 653
    len0 = babel_write_router_id(hdr, msg->router_id, state, max_len);
563
    tlv = (struct babel_tlv_update *) NEXT_TLV(tlv);
654
    hdr = NEXT_TLV(hdr);
564 655
  }
565 656

  
657
  /*
658
   * We also may add Next Hop TLV for regular updates. It may fail for not
659
   * enough space or it may be unnecessary as the next hop is the same as the
660
   * last one already announced. So we handle all three cases.
661
   */
662
  if (msg->metric < BABEL_INFINITY)
663
  {
664
    int l = babel_write_next_hop(hdr, msg->next_hop, state, max_len - len0);
665
    if (l < 0)
666
      return 0;
667

  
668
    if (l)
669
    {
670
      len0 += l;
671
      hdr = NEXT_TLV(hdr);
672
    }
673
  }
674

  
675
  struct babel_tlv_update *tlv = (void *) hdr;
566 676
  uint len = sizeof(struct babel_tlv_update) + NET_SIZE(&msg->net);
567 677

  
568 678
  if (len0 + len > max_len)
......
576 686
    tlv->ae = BABEL_AE_WILDCARD;
577 687
    tlv->plen = 0;
578 688
  }
689
  else if (msg->net.type == NET_IP4)
690
  {
691
    tlv->ae = BABEL_AE_IP4;
692
    tlv->plen = net4_pxlen(&msg->net);
693
    put_ip4_px(tlv->addr, &msg->net);
694
  }
579 695
  else
580 696
  {
581 697
    tlv->ae = BABEL_AE_IP6;
......
610 726
    return PARSE_SUCCESS;
611 727

  
612 728
  case BABEL_AE_IP4:
613
    /* TODO */
614
    return PARSE_IGNORE;
729
    if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
730
      return PARSE_ERROR;
731

  
732
    if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
733
      return PARSE_ERROR;
734

  
735
    read_ip4_px(&msg->net, tlv->addr, tlv->plen);
736
    return PARSE_SUCCESS;
615 737

  
616 738
  case BABEL_AE_IP6:
617 739
    if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
......
652 774
    tlv->ae = BABEL_AE_WILDCARD;
653 775
    tlv->plen = 0;
654 776
  }
777
  else if (msg->net.type == NET_IP4)
778
  {
779
    tlv->ae = BABEL_AE_IP4;
780
    tlv->plen = net4_pxlen(&msg->net);
781
    put_ip4_px(tlv->addr, &msg->net);
782
  }
655 783
  else
656 784
  {
657 785
    tlv->ae = BABEL_AE_IP6;
......
684 812
    return PARSE_ERROR;
685 813

  
686 814
  case BABEL_AE_IP4:
687
    /* TODO */
688
    return PARSE_IGNORE;
815
    if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
816
      return PARSE_ERROR;
817

  
818
    if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
819
      return PARSE_ERROR;
820

  
821
    read_ip4_px(&msg->net, tlv->addr, tlv->plen);
822
    return PARSE_SUCCESS;
689 823

  
690 824
  case BABEL_AE_IP6:
691 825
    if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
......
720 854
    return 0;
721 855

  
722 856
  TLV_HDR(tlv, BABEL_TLV_SEQNO_REQUEST, len);
723
  tlv->ae = BABEL_AE_IP6;
724
  tlv->plen = net6_pxlen(&msg->net);
857

  
858
  if (msg->net.type == NET_IP4)
859
  {
860
    tlv->ae = BABEL_AE_IP4;
861
    tlv->plen = net4_pxlen(&msg->net);
862
    put_ip4_px(tlv->addr, &msg->net);
863
  }
864
  else
865
  {
866
    tlv->ae = BABEL_AE_IP6;
867
    tlv->plen = net6_pxlen(&msg->net);
868
    put_ip6_px(tlv->addr, &msg->net);
869
  }
870

  
725 871
  put_u16(&tlv->seqno, msg->seqno);
726 872
  tlv->hop_count = msg->hop_count;
727 873
  put_u64(&tlv->router_id, msg->router_id);
728
  put_ip6_px(tlv->addr, &msg->net);
729 874

  
730 875
  return len;
731 876
}
......
799 944
babel_write_queue(struct babel_iface *ifa, list *queue)
800 945
{
801 946
  struct babel_proto *p = ifa->proto;
802
  struct babel_write_state state = {};
947
  struct babel_write_state state = { .next_hop_ip6 = ifa->addr };
803 948

  
804 949
  if (EMPTY_LIST(*queue))
805 950
    return 0;
......
935 1080
  byte *end = (byte *)pkt + plen;
936 1081

  
937 1082
  struct babel_parse_state state = {
938
    .proto	= p,
939
    .ifa	= ifa,
940
    .saddr	= saddr,
941
    .next_hop	= saddr,
1083
    .proto	  = p,
1084
    .ifa	  = ifa,
1085
    .saddr	  = saddr,
1086
    .next_hop_ip6 = saddr,
942 1087
  };
943 1088

  
944 1089
  if ((pkt->magic != BABEL_MAGIC) || (pkt->version != BABEL_VERSION))

Also available in: Unified diff