~hipl-s2e-devs/hipl/s2e-integration

« back to all changes in this revision

Viewing changes to hipd/hidb.c

  • Committer: Diego Biurrun
  • Date: 2011-08-10 18:11:13 UTC
  • Revision ID: diego@biurrun.de-20110810181113-9iyqt9xk936ng0mq
hipd: eliminate some HIP_IFE(L) abuse; simplify return handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
269
269
 */
270
270
static int hip_del_host_id(HIP_HASHTABLE *db, hip_hit_t hit)
271
271
{
272
 
    int                   err = -ENOENT;
273
 
    struct local_host_id *id  = NULL;
 
272
    struct local_host_id *id = NULL;
274
273
 
275
274
    id = hip_get_hostid_entry_by_lhi_and_algo(db, &hit, HIP_ANY_ALGO, -1);
276
275
    if (id == NULL) {
277
276
        HIP_ERROR("hit not found\n");
278
 
        err = -ENOENT;
279
 
        return err;
 
277
        return -ENOENT;
280
278
    }
281
279
 
282
280
    /* Call the handler to execute whatever required after the
303
301
    free(id);
304
302
    id = NULL;
305
303
 
306
 
    err = 0;
307
 
    return err;
 
304
    return 0;
308
305
}
309
306
 
310
307
/**
390
387
{
391
388
    struct local_host_id *id_entry;
392
389
    LHASH_NODE           *item;
393
 
    int                   c, err = 1;
 
390
    int                   c;
394
391
 
395
392
    list_for_each(item, hip_local_hostid_db, c) {
396
393
        id_entry = list_entry(item);
399
396
            return 0;
400
397
        }
401
398
    }
402
 
    return err;
 
399
    return 1;
403
400
}
404
401
 
405
402
/**
414
411
    struct local_host_id *id_entry_aux;
415
412
    LHASH_NODE           *item;
416
413
    hip_lsi_t             lsi_aux;
417
 
    int                   err = 0, used_lsi, c, i;
 
414
    int                   used_lsi, c, i;
418
415
    int                   len = sizeof(lsi_addresses) / sizeof(*lsi_addresses);
419
416
 
420
417
    for (i = 0; i < len; i++) {
434
431
            break;
435
432
        }
436
433
    }
437
 
    return err;
 
434
    return 0;
438
435
}
439
436
 
440
437
/*
590
587
 */
591
588
int hip_handle_add_local_hi(const struct hip_common *input)
592
589
{
593
 
    int                            err           = 0;
 
590
    int                            err;
594
591
    const struct hip_host_id_priv *host_identity = NULL;
595
592
    hip_hit_t                      hit;
596
593
    const struct hip_tlv_common   *param        = NULL;
604
601
    HIP_DEBUG_IN6ADDR("input->hitr = ", &input->hitr);
605
602
    if ((err = hip_get_msg_err(input)) != 0) {
606
603
        HIP_ERROR("daemon failed (%d)\n", err);
607
 
        goto out_err;
 
604
        return err;
608
605
    }
609
606
 
610
607
    /* Iterate through all host identities in the input */
618
615
 
619
616
        eid_endpoint = (const struct hip_eid_endpoint *) param;
620
617
 
621
 
        HIP_IFEL(!eid_endpoint, -ENOENT, "No host endpoint in input\n");
 
618
        if (!eid_endpoint) {
 
619
            HIP_ERROR("No host endpoint in input\n");
 
620
            return -ENOENT;
 
621
        }
622
622
 
623
623
        host_identity = &eid_endpoint->endpoint.id.host_id;
624
624
 
625
 
        HIP_IFEL(hip_private_host_id_to_hit(host_identity, &hit,
626
 
                                            HIP_HIT_TYPE_HASH100),
627
 
                 -EFAULT, "Host id to hit conversion failed\n");
 
625
        if (hip_private_host_id_to_hit(host_identity, &hit, HIP_HIT_TYPE_HASH100)) {
 
626
            HIP_ERROR("Host id to hit conversion failed\n");
 
627
            return -EFAULT;
 
628
        }
628
629
 
629
630
        anonymous = (eid_endpoint->endpoint.flags & HIP_ENDPOINT_FLAG_ANON);
630
631
 
634
635
        /* Currently only RSA pub is added by default (bug id 592127).
635
636
         * Ignore redundant adding in case user wants to enable
636
637
         * multiple HITs. */
637
 
        HIP_IFEL(err == -EEXIST, 0, "Ignoring redundant HI\n");
 
638
        if (err == -EEXIST) {
 
639
            HIP_ERROR("Ignoring redundant HI\n");
 
640
            return 0;
 
641
        }
638
642
 
639
643
        /* Adding the pair <HI,LSI> */
640
 
        HIP_IFEL(err,
641
 
                 -EFAULT, "adding of local host identity failed\n");
 
644
        if (err) {
 
645
            HIP_ERROR("adding of local host identity failed\n");
 
646
            return -EFAULT;
 
647
        }
642
648
 
643
649
 
644
650
        IPV4_TO_IPV6_MAP(&lsi, &in6_lsi);
647
653
        hip_add_iface_local_route(&in6_lsi);
648
654
 
649
655
        /* Adding HITs and LSIs to the interface */
650
 
        HIP_IFEL(hip_add_iface_local_hit(&hit), -1,
651
 
                 "Failed to add HIT to the device\n");
652
 
        HIP_IFEL(hip_add_iface_local_hit(&in6_lsi), -1,
653
 
                 "Failed to add LSI to the device\n");
 
656
        if (hip_add_iface_local_hit(&hit)) {
 
657
            HIP_ERROR("Failed to add HIT to the device\n");
 
658
            return -1;
 
659
        }
 
660
        if (hip_add_iface_local_hit(&in6_lsi)) {
 
661
            HIP_ERROR("Failed to add LSI to the device\n");
 
662
            return -1;
 
663
        }
654
664
    }
655
665
 
656
666
    HIP_DEBUG("Adding of HIP localhost identities was successful\n");
657
667
 
658
 
out_err:
659
 
    return err;
 
668
    return 0;
660
669
}
661
670
 
662
671
/**
669
678
{
670
679
    const struct in6_addr *hit;
671
680
    char                   buf[46];
672
 
    int                    err = 0;
 
681
    int                    err;
673
682
 
674
683
    hit = hip_get_param_contents(input, HIP_PARAM_HIT);
675
 
    HIP_IFEL(!hit, -ENODATA, "no hit\n");
 
684
    if (!hit) {
 
685
        HIP_ERROR("no hit\n");
 
686
        return -ENODATA;
 
687
    }
676
688
 
677
689
    hip_in6_ntop(hit, buf);
678
690
    HIP_INFO("del HIT: %s\n", buf);
679
691
 
680
692
    if ((err = hip_del_host_id(HIP_DB_LOCAL_HID, *hit))) {
681
693
        HIP_ERROR("deleting of local host identity failed\n");
682
 
        goto out_err;
 
694
        return err;
683
695
    }
684
696
 
685
697
    /** @todo remove associations from hadb & beetdb by the deleted HI. */
686
698
    HIP_DEBUG("Removal of HIP localhost identity was successful\n");
687
 
out_err:
688
 
    return err;
 
699
 
 
700
    return 0;
689
701
}
690
702
 
691
703
/**
701
713
                                     int anon)
702
714
{
703
715
    struct local_host_id *entry;
704
 
    int                   err = 0;
705
716
 
706
717
    entry = hip_get_hostid_entry_by_lhi_and_algo(hip_local_hostid_db,
707
718
                                                 NULL, algo, anon);
708
719
    if (!entry) {
709
 
        err = -ENOENT;
710
 
        goto out;
 
720
        return -ENOENT;
711
721
    }
712
722
 
713
723
    ipv6_addr_copy(target, &entry->hit);
714
 
    err = 0;
715
724
 
716
 
out:
717
 
    return err;
 
725
    return 0;
718
726
}
719
727
 
720
728
/**
751
759
{
752
760
    LHASH_NODE           *curr, *iter;
753
761
    struct local_host_id *tmp;
754
 
    int                   err = 0, c;
 
762
    int                   err, c;
755
763
 
756
764
    list_for_each_safe(curr, iter, hip_local_hostid_db, c)
757
765
    {
760
768
        HIP_DEBUG_LSI("Found LSI", &tmp->lsi);
761
769
        err = func(tmp, opaque);
762
770
        if (err) {
763
 
            goto out_err;
 
771
            return err;
764
772
        }
765
773
    }
766
774
 
767
 
out_err:
768
 
    return err;
 
775
    return 0;
769
776
}
770
777
 
771
778
/**
800
807
 */
801
808
int hip_hidb_associate_default_hit_lsi(hip_hit_t *default_hit, hip_lsi_t *default_lsi)
802
809
{
803
 
    int                   err = 0;
804
810
    hip_lsi_t             aux_lsi;
805
811
    struct local_host_id *tmp1;
806
812
    struct local_host_id *tmp2;
807
813
 
808
814
    //1. Check if default_hit already associated with default_lsi
809
 
    HIP_IFEL((err = hip_hidb_get_lsi_by_hit(default_hit, &aux_lsi)),
810
 
             -1,
811
 
             "Error no lsi associated to hit\n");
812
 
 
 
815
    if (!hip_hidb_get_lsi_by_hit(default_hit, &aux_lsi)) {
 
816
        HIP_ERROR("Error no lsi associated to hit\n");
 
817
        return -1;
 
818
    }
813
819
    if (ipv4_addr_cmp(&aux_lsi, default_lsi)) {
814
 
        HIP_IFEL(!(tmp1 = hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID,
815
 
                                                               default_hit,
816
 
                                                               HIP_ANY_ALGO,
817
 
                                                               -1)),
818
 
                 -1, "Default hit not found in hidb\n");
819
 
        HIP_IFEL(!(tmp2 = hip_hidb_get_entry_by_lsi(HIP_DB_LOCAL_HID, default_lsi)), -1,
820
 
                 "Default lsi not found in hidb\n");
 
820
        if (!(tmp1 = hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID,
 
821
                                                          default_hit,
 
822
                                                          HIP_ANY_ALGO,
 
823
                                                          -1))) {
 
824
            HIP_ERROR("Default hit not found in hidb\n");
 
825
            return -1;
 
826
        }
 
827
        if (!(tmp2 = hip_hidb_get_entry_by_lsi(HIP_DB_LOCAL_HID, default_lsi))) {
 
828
            HIP_ERROR("Default lsi not found in hidb\n");
 
829
            return -1;
 
830
        }
821
831
 
822
832
        memcpy(&tmp2->lsi, &tmp1->lsi, sizeof(tmp1->lsi));
823
833
        memcpy(&tmp1->lsi, default_lsi, sizeof(tmp2->lsi));
824
834
    }
825
835
 
826
 
out_err:
827
 
    return err;
 
836
    return 0;
828
837
}
829
838
 
830
839
/**
840
849
int hip_get_host_id_and_priv_key(HIP_HASHTABLE *db, struct in6_addr *hit,
841
850
                                 int algo, struct hip_host_id **host_id, void **key)
842
851
{
843
 
    int                   err   = 0, host_id_len;
 
852
    int                   host_id_len;
844
853
    struct local_host_id *entry = NULL;
845
854
 
846
855
    entry = hip_get_hostid_entry_by_lhi_and_algo(db, hit, algo, -1);
847
 
    HIP_IFE(!entry, -1);
 
856
    if (!entry) {
 
857
        return -1;
 
858
    }
848
859
 
849
860
    host_id_len = hip_get_param_total_len(&entry->host_id);
850
 
    HIP_IFE(host_id_len > HIP_MAX_HOST_ID_LEN, -1);
 
861
    if (host_id_len > HIP_MAX_HOST_ID_LEN) {
 
862
        return -1;
 
863
    }
851
864
 
852
865
    *host_id = malloc(host_id_len);
853
 
    HIP_IFE(!*host_id, -ENOMEM);
 
866
    if (!*host_id) {
 
867
        return -ENOMEM;
 
868
    }
854
869
    memcpy(*host_id, &entry->host_id, host_id_len);
855
870
 
856
871
    *key = entry->private_key;
857
 
    HIP_IFE(!*key, -1);
 
872
    if (!*key) {
 
873
        return -1;
 
874
    }
858
875
 
859
 
out_err:
860
 
    return err;
 
876
    return 0;
861
877
}
862
878
 
863
879
/**
880
896
 */
881
897
int hip_get_default_hit_msg(struct hip_common *msg)
882
898
{
883
 
    int       err = 0;
884
899
    hip_hit_t hit;
885
900
    hip_lsi_t lsi;
886
901
 
887
 
    HIP_IFE(hip_get_default_hit(&hit), -1);
888
 
    HIP_IFE(hip_get_default_lsi(&lsi), -1);
 
902
    if (hip_get_default_hit(&hit) || hip_get_default_lsi(&lsi)) {
 
903
        return -1;
 
904
    }
 
905
 
889
906
    HIP_DEBUG_HIT("Default hit is ", &hit);
890
907
    HIP_DEBUG_LSI("Default lsi is ", &lsi);
891
 
    HIP_IFE(hip_build_param_contents(msg, &hit, HIP_PARAM_HIT, sizeof(hit)),
892
 
            -1);
893
 
    HIP_IFE(hip_build_param_contents(msg, &lsi, HIP_PARAM_LSI, sizeof(lsi)),
894
 
            -1);
895
 
 
896
 
out_err:
897
 
 
898
 
    return err;
 
908
 
 
909
    if (hip_build_param_contents(msg, &hit, HIP_PARAM_HIT, sizeof(hit)) ||
 
910
        hip_build_param_contents(msg, &lsi, HIP_PARAM_LSI, sizeof(lsi))) {
 
911
        return -1;
 
912
    }
 
913
 
 
914
    return 0;
899
915
}
900
916
 
901
917
/**
906
922
 */
907
923
int hip_get_default_lsi(struct in_addr *lsi)
908
924
{
909
 
    int             err        = 0, family = AF_INET;
 
925
    int             family     = AF_INET;
910
926
    struct idxmap  *idxmap[16] = { 0 };
911
927
    struct in6_addr lsi_addr;
912
928
    struct in6_addr lsi_aux6;
914
930
 
915
931
    set_lsi_prefix(&lsi_tmpl);
916
932
    IPV4_TO_IPV6_MAP(&lsi_tmpl, &lsi_addr);
917
 
    HIP_IFEL(hip_iproute_get(&hip_nl_route, &lsi_aux6, &lsi_addr, NULL,
918
 
                             NULL, family, idxmap), -1,
919
 
             "Failed to find IP route.\n");
 
933
    if (hip_iproute_get(&hip_nl_route, &lsi_aux6, &lsi_addr, NULL,
 
934
                        NULL, family, idxmap)) {
 
935
        HIP_ERROR("Failed to find IP route.\n");
 
936
        return -1;
 
937
    }
920
938
 
921
939
    if (IN6_IS_ADDR_V4MAPPED(&lsi_aux6)) {
922
940
        IPV6_TO_IPV4_MAP(&lsi_aux6, lsi);
923
941
    }
924
 
out_err:
925
942
 
926
 
    return err;
 
943
    return 0;
927
944
}