~diego-biurrun/hipl/hipl-s2e

« back to all changes in this revision

Viewing changes to libhipl/registration.c

  • Committer: Diego Biurrun
  • Date: 2013-03-26 12:51:51 UTC
  • mfrom: (6052.1.357 hipl)
  • Revision ID: diego@biurrun.de-20130326125151-19tea8nj10m70m8n
Merge current HIPL HEAD revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2010 Aalto University and RWTH Aachen University.
 
2
 * Copyright (c) 2010, 2012 Aalto University and RWTH Aachen University.
3
3
 *
4
4
 * Permission is hereby granted, free of charge, to any person
5
5
 * obtaining a copy of this software and associated documentation
28
28
 * This file defines a registration mechanism for the Host Identity Protocol
29
29
 * (HIP) that allows hosts to register with services.
30
30
 *
31
 
 * @author  Lauri Silvennoinen
32
31
 * @note    Related RFC: <a href="http://www.rfc-editor.org/rfc/rfc5203.txt">
33
32
 *          Host Identity Protocol (HIP) Registration Extension</a>
34
33
 * @see     registration.h
43
42
#include <stdint.h>
44
43
#include <string.h>
45
44
 
46
 
#include "lib/core/builder.h"
47
 
#include "lib/core/ife.h"
48
 
#include "lib/core/linkedlist.h"
49
 
#include "lib/core/prefix.h"
50
 
#include "lib/core/protodefs.h"
 
45
#include "libcore/builder.h"
 
46
#include "libcore/ife.h"
 
47
#include "libcore/linkedlist.h"
 
48
#include "libcore/prefix.h"
 
49
#include "libcore/protodefs.h"
51
50
#include "close.h"
52
51
#include "hadb.h"
53
52
#include "hidb.h"
55
54
#include "netdev.h"
56
55
#include "output.h"
57
56
#include "registration.h"
 
57
#include "init.h"
58
58
 
59
59
/**
60
60
 * Pending request lifetime. Pending requests are created when the requester
106
106
 * Deletes one expired pending request. Deletes the first exipired pending
107
107
 * request from the pending request linked list @c pending_requests.
108
108
 */
109
 
static int hip_del_pending_request_by_expiration(void)
 
109
static int del_pending_request_by_expiration(void)
110
110
{
111
111
    int                         idx     = 0;
112
112
    time_t                      now     = time(NULL);
113
113
    const struct hip_ll_node   *iter    = NULL;
114
114
    struct hip_pending_request *request = NULL;
115
115
 
116
 
    /* See hip_del_pending_request() for a comment. */
117
116
    while ((iter = hip_ll_iterate(&pending_requests, iter)) != NULL) {
118
117
        request = iter->ptr;
119
118
        if (now - request->created > HIP_PENDING_REQUEST_LIFETIME) {
120
 
            HIP_DEBUG("Deleting and freeing a pending request by " \
 
119
            HIP_DEBUG("Deleting and freeing a pending request by "
121
120
                      "expiration (%u seconds) at index %u.\n",
122
121
                      now - request->created, idx);
123
122
            hip_ll_del(&pending_requests, idx, free);
133
132
 * Periodic maintenance function of the registration extension. This function
134
133
 * should be called every now and then. A suitable time between calls could be
135
134
 * 10 minutes for example. This function deletes the expired pending requests by
136
 
 * calling @c hip_del_pending_request_by_expiration() until there are no expired
 
135
 * calling @c del_pending_request_by_expiration() until there are no expired
137
136
 * pending requests left.
138
137
 *
139
138
 * @note This is by no means time critical operation and is not needed to be
145
144
 */
146
145
int hip_registration_maintenance(void)
147
146
{
148
 
    while (hip_del_pending_request_by_expiration() == 0) {
 
147
    while (del_pending_request_by_expiration() == 0) {
149
148
    }
150
149
 
151
150
    return 0;
159
158
 * @param  reg_type the registration type of the service for which the status
160
159
 *                  is to be set.
161
160
 * @param  status   the status to set i.e. ON or OFF.
162
 
 * @return          zero if the status was set succesfully, -1 otherwise.
 
161
 * @return          zero if the status was set successfully, -1 otherwise.
163
162
 */
164
163
int hip_set_srv_status(uint8_t reg_type, enum hip_srv_status status)
165
164
{
217
216
 * added as the last element of the list.
218
217
 *
219
218
 * @param  request a pointer to the pending request to add.
220
 
 * @return         zero if the pending request was added succesfully, -1
 
219
 * @return         zero if the pending request was added successfully, -1
221
220
 *                 otherwise.
222
221
 */
223
222
int hip_add_pending_request(struct hip_pending_request *request)
234
233
}
235
234
 
236
235
/**
237
 
 * Deletes a pending request. Deletes a pending request identified by the host
238
 
 * association @c entry from the linked list @c pending_requests.
239
 
 *
240
 
 * @param  entry a pointer to the host association to which the pending request
241
 
 *               to be deleted is bound.
242
 
 * @return       zero if the pending request was succesfully deleted, -1
243
 
 *               otherwise.
244
 
 */
245
 
int hip_del_pending_request(struct hip_hadb_state *entry)
246
 
{
247
 
    int                       idx  = 0;
248
 
    const struct hip_ll_node *iter = NULL;
249
 
 
250
 
    /* Iterate through the linked list. The iterator itself can't be used
251
 
     * for deleting nodes from the list. Therefore, we just get the index of
252
 
     * the element to be deleted using the iterator and then call
253
 
     * hip_ll_del() to do the actual deletion. */
254
 
    while ((iter = hip_ll_iterate(&pending_requests, iter)) != NULL) {
255
 
        if (((struct hip_pending_request *) (iter->ptr))->entry == entry) {
256
 
            HIP_DEBUG("Deleting and freeing a pending request at " \
257
 
                      "index %u.\n", idx);
258
 
            hip_ll_del(&pending_requests, idx, free);
259
 
            return 0;
260
 
        }
261
 
        idx++;
262
 
    }
263
 
 
264
 
    return -1;
265
 
}
266
 
 
267
 
/**
268
236
 * Deletes a pending request of given type. Deletes a pending request identified
269
237
 * by the host association @c entry and matching the given type @c reg_type from
270
238
 * the linked list @c pending_requests.
272
240
 * @param  entry    a pointer to a host association to which the pending request
273
241
 *                  to be deleted is bound.
274
242
 * @param  reg_type the type of the pending request to delete.
275
 
 * @return          zero if the pending request was succesfully deleted, -1
 
243
 * @return          zero if the pending request was successfully deleted, -1
276
244
 *                  otherwise.
277
245
 */
278
 
static int hip_del_pending_request_by_type(struct hip_hadb_state *entry,
279
 
                                           uint8_t reg_type)
 
246
static int del_pending_request_by_type(struct hip_hadb_state *entry,
 
247
                                       uint8_t reg_type)
280
248
{
281
249
    int                         idx     = 0;
282
250
    const struct hip_ll_node   *iter    = NULL;
283
251
    struct hip_pending_request *request = NULL;
284
252
 
285
 
    /* See hip_del_pending_request() for a comment. */
286
253
    while ((iter = hip_ll_iterate(&pending_requests, iter)) != NULL) {
287
254
        request = iter->ptr;
288
255
        if (request->entry == entry && request->reg_type == reg_type) {
289
 
            HIP_DEBUG("Deleting and freeing a pending request by " \
 
256
            HIP_DEBUG("Deleting and freeing a pending request by "
290
257
                      "type at index %u.\n", idx);
291
258
            hip_ll_del(&pending_requests, idx, free);
292
259
            return 0;
304
271
 *                   to be moved is bound.
305
272
 * @param  entry_new a pointer to the new  host association to which the pending request
306
273
 *                   to be moved
307
 
 * @return       zero if the pending request was succesfully moved, -1
 
274
 * @return       zero if the pending request was successfully moved, -1
308
275
 *               otherwise.
309
276
 */
310
277
int hip_replace_pending_requests(struct hip_hadb_state *entry_old,
328
295
 *
329
296
 * Make sure that the target buffer @c requests has room for at least as many
330
297
 * pending requests that the host association @c entry has currently. You can
331
 
 * have this number by calling hip_get_pending_request_count().
 
298
 * have this number by calling get_pending_request_count().
332
299
 *
333
300
 * @param  entry    a pointer to a host association whose pending requests are
334
301
 *                  to be get.
335
302
 * @param  requests a target buffer for the pending requests.
336
303
 * @return          -1 if @c requests is NULL or if no pending requests were
337
304
 *                  found, zero otherwise.
338
 
 * @see             hip_get_pending_request_count().
 
305
 * @see             get_pending_request_count().
339
306
 */
340
 
static int hip_get_pending_requests(struct hip_hadb_state *entry,
341
 
                                    struct hip_pending_request *requests[])
 
307
static int get_pending_requests(struct hip_hadb_state *entry,
 
308
                                struct hip_pending_request *requests[])
342
309
{
343
310
    const struct hip_ll_node *iter          = 0;
344
311
    int                       request_count = 0;
368
335
 *               is to be get.
369
336
 * @return       the number of pending requests for the host association.
370
337
 */
371
 
static int hip_get_pending_request_count(struct hip_hadb_state *entry)
 
338
static int get_pending_request_count(struct hip_hadb_state *entry)
372
339
{
373
340
    const struct hip_ll_node *iter          = 0;
374
341
    int                       request_count = 0;
386
353
 * Adds new registrations to services at the server. This function tries to add
387
354
 * all new services listed and indentified by @c types. This is server side
388
355
 * addition, meaning that the server calls this function to add entries
389
 
 * to its served client list. After the function finishes, succesful registrations
390
 
 * are listed in @c accepted_requests and unsuccesful registrations in
 
356
 * to its served client list. After the function finishes, successful registrations
 
357
 * are listed in @c accepted_requests and unsuccessful registrations in
391
358
 * @c refused_requests.
392
359
 *
393
360
 * Make sure that you have allocated memory to @c accepted_requests,
416
383
 * @param  refused_count      a target buffer that will store the number of Reg
417
384
 *                            Types in @c refused_requests.
418
385
 * @return                    zero on success, -1 otherwise.
419
 
 * @see                       hip_add_registration_client().
 
386
 * @see                       add_registration_client().
420
387
 */
421
 
static int hip_add_registration_server(struct hip_hadb_state *entry,
422
 
                                       uint8_t lifetime,
423
 
                                       const uint8_t *reg_types,
424
 
                                       int type_count,
425
 
                                       uint8_t accepted_requests[],
426
 
                                       uint8_t accepted_lifetimes[],
427
 
                                       int *accepted_count, uint8_t refused_requests[],
428
 
                                       uint8_t failure_types[], int *refused_count)
 
388
static int add_registration_server(struct hip_hadb_state *entry,
 
389
                                   uint8_t lifetime,
 
390
                                   const uint8_t *reg_types,
 
391
                                   int type_count,
 
392
                                   uint8_t accepted_requests[],
 
393
                                   uint8_t accepted_lifetimes[],
 
394
                                   int *accepted_count, uint8_t refused_requests[],
 
395
                                   uint8_t failure_types[], int *refused_count)
429
396
{
430
397
    int               err = 0, i = 0;
431
398
    struct hip_relrec dummy, *fetch_record = NULL, *new_record = NULL;
440
407
        case HIP_SERVICE_RENDEZVOUS:
441
408
        case HIP_SERVICE_RELAY:
442
409
        case HIP_SERVICE_FULLRELAY:
443
 
            HIP_DEBUG("Client is registering to rendezvous " \
 
410
            HIP_DEBUG("Client is registering to rendezvous "
444
411
                      "service or relay service.\n");
445
412
            /* Validate lifetime. */
446
413
            hip_rvs_validate_lifetime(lifetime, &granted_lifetime);
498
465
 
499
466
                hip_relht_put(new_record);
500
467
 
501
 
                /* Check that the put was succesful. */
 
468
                /* Check that the put was successful. */
502
469
                if (hip_relht_get(new_record) != NULL) {
503
470
                    accepted_requests[*accepted_count]  = reg_types[i];
504
471
                    accepted_lifetimes[*accepted_count] = granted_lifetime;
524
491
                    refused_requests[*refused_count] = reg_types[i];
525
492
                    failure_types[*refused_count]    = HIP_REG_TRANSIENT_CONDITIONS;
526
493
                    (*refused_count)++;
527
 
                    HIP_ERROR("Unable to store new relay " \
528
 
                              "record. Registration " \
529
 
                              "refused.\n");
 
494
                    HIP_ERROR("Unable to store new relay record. Registration refused.\n");
530
495
                }
531
496
            }
532
497
 
533
498
            break;
534
499
        default:
535
500
            HIP_DEBUG("Client is trying to register to an "
536
 
                      "unsupported service.\nRegistration " \
537
 
                      "refused.\n");
 
501
                      "unsupported service.\nRegistration refused.\n");
538
502
            refused_requests[*refused_count] = reg_types[i];
539
503
            failure_types[*refused_count]    =
540
504
                HIP_REG_TYPE_UNAVAILABLE;
551
515
 * Cancels registrations to services at the server. This function tries to
552
516
 * cancel all services listed and indentified by @c types. This is server side
553
517
 * cancellation, meaning that the server calls this function to remove entries
554
 
 * from its served client list. After the function finishes, succesful
555
 
 * cancellations are listed in @c accepted_requests and unsuccesful requests
 
518
 * from its served client list. After the function finishes, successful
 
519
 * cancellations are listed in @c accepted_requests and unsuccessful requests
556
520
 * in @c refused_requests.
557
521
 *
558
522
 * Make sure that you have allocated memory to both @c accepted_requests and
572
536
 *                           Types in @c refused_requests.
573
537
 * @param  failure_types     the failure types
574
538
 * @return                   zero on success, -1 otherwise.
575
 
 * @see                      hip_del_registration_client().
 
539
 * @see                      del_registration_client().
576
540
 */
577
 
static int hip_del_registration_server(struct hip_hadb_state *entry,
578
 
                                       const uint8_t *reg_types,
579
 
                                       int type_count,
580
 
                                       uint8_t accepted_requests[],
581
 
                                       int *accepted_count,
582
 
                                       uint8_t refused_requests[],
583
 
                                       uint8_t failure_types[],
584
 
                                       int *refused_count)
 
541
static int del_registration_server(struct hip_hadb_state *entry,
 
542
                                   const uint8_t *reg_types,
 
543
                                   int type_count,
 
544
                                   uint8_t accepted_requests[],
 
545
                                   int *accepted_count,
 
546
                                   uint8_t refused_requests[],
 
547
                                   uint8_t failure_types[],
 
548
                                   int *refused_count)
585
549
{
586
550
    int               err = 0, i = 0;
587
551
    struct hip_relrec dummy, *fetch_record = NULL;
601
565
            /* RVS and relay deletions are identical except the
602
566
             * relay record type. */
603
567
            if (reg_types[i] == HIP_SERVICE_RENDEZVOUS) {
604
 
                HIP_DEBUG("Client is cancelling registration " \
 
568
                HIP_DEBUG("Client is cancelling registration "
605
569
                          "to rendezvous service.\n");
606
570
                type_to_delete = HIP_RVSRELAY;
607
571
            } else if (reg_types[i] == HIP_SERVICE_RELAY) {
608
 
                HIP_DEBUG("Client is cancelling registration " \
 
572
                HIP_DEBUG("Client is cancelling registration "
609
573
                          "to relay service.\n");
610
574
                type_to_delete = HIP_RELAY;
611
575
            } else {
612
 
                HIP_DEBUG("Client is cancelling registration " \
 
576
                HIP_DEBUG("Client is cancelling registration "
613
577
                          "to full relay service.\n");
614
578
                type_to_delete = HIP_FULLRELAY;
615
579
            }
629
593
                    HIP_REG_TYPE_UNAVAILABLE;
630
594
                (*refused_count)++;
631
595
            } else if (fetch_record == NULL) {
632
 
                HIP_DEBUG("There is no relay record to " \
633
 
                          "cancel.\n");
 
596
                HIP_DEBUG("There is no relay record to cancel.\n");
634
597
                refused_requests[*refused_count] = reg_types[i];
635
598
                failure_types[*refused_count]    =
636
599
                    HIP_REG_TYPE_UNAVAILABLE;
637
600
                (*refused_count)++;
638
601
            } else if (fetch_record->type != type_to_delete) {
639
 
                HIP_DEBUG("The relay record to be cancelled " \
640
 
                          "is of wrong type.\n");
 
602
                HIP_DEBUG("The relay record to be cancelled is of wrong type.\n");
641
603
                refused_requests[*refused_count] = reg_types[i];
642
604
                failure_types[*refused_count]    =
643
605
                    HIP_REG_TYPE_UNAVAILABLE;
671
633
            break;
672
634
        }
673
635
        default:
674
 
            HIP_DEBUG("Client is trying to cancel an unsupported " \
 
636
            HIP_DEBUG("Client is trying to cancel an unsupported "
675
637
                      "service.\nCancellation refused.\n");
676
638
            refused_requests[*refused_count] = reg_types[i];
677
639
            failure_types[*refused_count]    =
698
660
 * @param  reg_types          a pointer to Reg Types found in REG_REQUEST.
699
661
 * @param  type_count         number of Reg Types in @c reg_types.
700
662
 * @return                    zero on success, -1 otherwise.
701
 
 * @see                       hip_add_registration_server().
 
663
 * @see                       add_registration_server().
702
664
 */
703
 
static int hip_add_registration_client(struct hip_hadb_state *entry, uint8_t lifetime,
704
 
                                       const uint8_t *reg_types,
705
 
                                       int type_count)
 
665
static int add_registration_client(struct hip_hadb_state *entry, uint8_t lifetime,
 
666
                                   const uint8_t *reg_types,
 
667
                                   int type_count)
706
668
{
707
669
    int    i       = 0;
708
670
    time_t seconds = 0;
718
680
        switch (reg_types[i]) {
719
681
        case HIP_SERVICE_RENDEZVOUS:
720
682
        {
721
 
            HIP_DEBUG("The server has granted us rendezvous " \
 
683
            HIP_DEBUG("The server has granted us rendezvous "
722
684
                      "service for %u seconds (lifetime 0x%x.)\n",
723
685
                      seconds, lifetime);
724
686
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RVS);
725
687
            hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_GRANTED_RVS);
726
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
 
688
            del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
727
689
            break;
728
690
        }
729
691
        case HIP_SERVICE_RELAY:
730
692
        {
731
 
            HIP_DEBUG("The server has granted us relay " \
 
693
            HIP_DEBUG("The server has granted us relay "
732
694
                      "service for %u seconds (lifetime 0x%x.)\n",
733
695
                      seconds, lifetime);
734
696
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RELAY);
735
697
            hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_GRANTED_RELAY);
736
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
 
698
            del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
737
699
            break;
738
700
        }
739
701
        case HIP_SERVICE_FULLRELAY:
740
702
        {
741
 
            HIP_DEBUG("The server has granted us full relay " \
 
703
            HIP_DEBUG("The server has granted us full relay "
742
704
                      "service for %u seconds (lifetime 0x%x.)\n",
743
705
                      seconds, lifetime);
744
706
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_FULLRELAY);
745
707
            hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_GRANTED_FULLRELAY);
746
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
 
708
            del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
747
709
            /* Delete SAs with relay server to
748
710
             * avoid problems with ESP relay*/
749
711
            entry->disable_sas = 1;
753
715
        }
754
716
        default:
755
717
        {
756
 
            HIP_DEBUG("The server has granted us an unknown " \
 
718
            HIP_DEBUG("The server has granted us an unknown "
757
719
                      "service for %u seconds (lifetime 0x%x.)\n",
758
720
                      seconds, lifetime);
759
721
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_UNSUP);
760
722
            hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_GRANTED_UNSUP);
761
 
            hip_del_pending_request_by_type(entry, reg_types[i]);
 
723
            del_pending_request_by_type(entry, reg_types[i]);
762
724
            break;
763
725
        }
764
726
        }
777
739
 * @param  reg_types         a pointer to Reg Types found in REG_REQUEST.
778
740
 * @param  type_count        number of Reg Types in @c reg_types.
779
741
 * @return                   zero on success, -1 otherwise.
780
 
 * @see                      hip_del_registration_client().
 
742
 * @see                      del_registration_client().
781
743
 */
782
 
static int hip_del_registration_client(struct hip_hadb_state *entry,
783
 
                                       const uint8_t *reg_types,
784
 
                                       int type_count)
 
744
static int del_registration_client(struct hip_hadb_state *entry,
 
745
                                   const uint8_t *reg_types,
 
746
                                   int type_count)
785
747
{
786
748
    int i = 0;
787
749
 
792
754
        switch (reg_types[i]) {
793
755
        case HIP_SERVICE_RENDEZVOUS:
794
756
        {
795
 
            HIP_DEBUG("The server has cancelled our rendezvous " \
796
 
                      "service.\n");
 
757
            HIP_DEBUG("The server has cancelled our rendezvous service.\n");
797
758
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RVS);
798
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
 
759
            del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
799
760
            break;
800
761
        }
801
762
        case HIP_SERVICE_RELAY:
802
763
        {
803
 
            HIP_DEBUG("The server has cancelled our relay " \
804
 
                      "service.\n");
 
764
            HIP_DEBUG("The server has cancelled our relay service.\n");
805
765
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RELAY);
806
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
 
766
            del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
807
767
 
808
768
            break;
809
769
        }
810
770
        case HIP_SERVICE_FULLRELAY:
811
771
        {
812
 
            HIP_DEBUG("The server has cancelled our full relay " \
813
 
                      "service.\n");
 
772
            HIP_DEBUG("The server has cancelled our full relay service.\n");
814
773
            hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_FULLRELAY);
815
 
            hip_del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
 
774
            del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
816
775
 
817
776
            break;
818
777
        }
819
778
        default:
820
779
        {
821
 
            HIP_DEBUG("The server has cancelled our registration " \
 
780
            HIP_DEBUG("The server has cancelled our registration "
822
781
                      "to an unknown service.\n");
823
782
            break;
824
783
        }
860
819
    reg_info = hip_get_param(source_msg, HIP_PARAM_REG_INFO);
861
820
 
862
821
    if (reg_info == NULL) {
863
 
        HIP_DEBUG("No REG_INFO parameter found. The server offers " \
864
 
                  "no services.\n");
 
822
        HIP_DEBUG("No REG_INFO parameter found. The server offers no services.\n");
865
823
 
866
824
        err = -1;
867
825
        goto out_err;
880
838
 
881
839
    /* Check RFC 5203 Chapter 3.1. */
882
840
    if (type_count == 0) {
883
 
        HIP_INFO("The server is currently unable to provide services " \
 
841
        HIP_INFO("The server is currently unable to provide services "
884
842
                 "due to transient conditions.\n");
885
843
        err = 0;
886
844
        goto out_err;
920
878
     * service that we have requested and the server offers. */
921
879
 
922
880
    if (entry->local_controls & HIP_HA_CTRL_LOCAL_REQ_ANY) {
923
 
        int request_count = hip_get_pending_request_count(entry);
 
881
        int request_count = get_pending_request_count(entry);
924
882
        if (request_count > 0) {
925
883
            unsigned int                j                = 0;
926
884
            int                         types_to_request = 0;
929
887
            struct hip_pending_request *requests[request_count];
930
888
 
931
889
            i = 0;
932
 
            hip_get_pending_requests(entry, requests);
 
890
            get_pending_requests(entry, requests);
933
891
 
934
892
            /* If we have requested for a cancellation of a service
935
893
             * we use lifetime of zero. Otherwise we must check
936
894
             * that the requested lifetime falls between the offered
937
895
             * lifetime boundaries. */
938
896
            if (requests[0]->lifetime == 0) {
939
 
                HIP_DEBUG("SERVICE CANCELATION \n");
 
897
                HIP_DEBUG("SERVICE CANCELLATION \n");
940
898
                valid_lifetime = 0;
941
899
            } else {
942
900
                valid_lifetime = MIN(requests[0]->lifetime,
964
922
                                                     valid_lifetime,
965
923
                                                     type_array,
966
924
                                                     types_to_request),
967
 
                         -1,
968
 
                         "Failed to build a REG_REQUEST " \
969
 
                         "parameter.\n");
 
925
                         -1, "Failed to build a REG_REQUEST parameter.\n");
970
926
            }
971
927
        }
972
928
        /* We do not delete the pending requests for this entry yet, but
987
943
 * @param  type_count number of values in the value list.
988
944
 * @return            zero if there are no duplicate values, -1 otherwise.
989
945
 */
990
 
static int hip_has_duplicate_services(const uint8_t *reg_types, int type_count)
 
946
static int has_duplicate_services(const uint8_t *reg_types, int type_count)
991
947
{
992
948
    int i = 0, j = 0;
993
949
 
1011
967
 * message @c source_msg, takes action based on the contents of the REG_REQUEST
1012
968
 * parameter and builds parameters in response to the HIP message @c target_msg.
1013
969
 *
1014
 
 * First hip_has_duplicate_services() is called to check whether the
 
970
 * First has_duplicate_services() is called to check whether the
1015
971
 * parameter is malformed in a way that it has the same services listed more
1016
972
 * than once. If the parameter proves to be malformed, the whole parameter is
1017
973
 * omitted, none of the Reg Types in the REG_REQUEST are handled, and no
1019
975
 * stress the server with malformed service requests. This is considered as a
1020
976
 * protocol error and errno is set to EPROTO.
1021
977
 *
1022
 
 * If the parameter passes the hip_has_duplicate_services() test, the parameter
 
978
 * If the parameter passes the has_duplicate_services() test, the parameter
1023
979
 * lifetime is investigated next. If it is zero i.e. the client is canceling a
1024
 
 * service, hip_del_registration_server() is called. Otherwise the client is
1025
 
 * registering to new services and hip_add_registration_server() is called.
 
980
 * service, del_registration_server() is called. Otherwise the client is
 
981
 * registering to new services and add_registration_server() is called.
1026
982
 *
1027
983
 * Once the aforementioned functions return, a REG_RESPONSE and/or a required
1028
984
 * number of REG_FAILED parameters are built to
1035
991
 * @return           -1 if the message @c source_msg did not contain a
1036
992
 *                   REG_REQUEST parameter or the parameter had duplicate
1037
993
 *                   services, zero otherwise.
1038
 
 * @see              hip_has_duplicate_services().
1039
 
 * @see              hip_add_registration_server().
1040
 
 * @see              hip_del_registration_server().
 
994
 * @see              has_duplicate_services().
 
995
 * @see              add_registration_server().
 
996
 * @see              del_registration_server().
1041
997
 */
1042
998
int hip_handle_param_reg_request(struct hip_hadb_state *entry,
1043
999
                                 struct hip_common *source_msg,
1074
1030
    reg_types = (const uint8_t *) hip_get_param_contents_direct(reg_request) +
1075
1031
                sizeof(reg_request->lifetime);
1076
1032
 
1077
 
    HIP_DEBUG("REG_REQUEST parameter found. Requested lifetime: 0x%x, " \
 
1033
    HIP_DEBUG("REG_REQUEST parameter found. Requested lifetime: 0x%x, "
1078
1034
              "number of service types requested: %d.\n",
1079
1035
              reg_request->lifetime, type_count);
1080
1036
 
1081
1037
    /* Check that the request has at most one value of each type. */
1082
 
    if (hip_has_duplicate_services(reg_types, type_count)) {
 
1038
    if (has_duplicate_services(reg_types, type_count)) {
1083
1039
        /* We consider this as a protocol error, and do not build
1084
1040
         * REG_FAILED parameters. The initiator may be rogue and
1085
1041
         * trying to stress the server with malformed service
1086
1042
         * requests. */
1087
1043
        err   = -1;
1088
1044
        errno = EPROTO;
1089
 
        HIP_ERROR("The REG_REQUEST parameter has duplicate services. " \
 
1045
        HIP_ERROR("The REG_REQUEST parameter has duplicate services. "
1090
1046
                  "The whole parameter is omitted.\n");
1091
1047
        /* As above. */
1092
1048
        return err;
1093
1049
    }
1094
1050
 
1095
1051
    if (reg_request->lifetime == 0) {
1096
 
        hip_del_registration_server(entry, reg_types, type_count,
1097
 
                                    accepted_requests, &accepted_count,
1098
 
                                    refused_requests, failure_types,
1099
 
                                    &refused_count);
 
1052
        del_registration_server(entry, reg_types, type_count,
 
1053
                                accepted_requests, &accepted_count,
 
1054
                                refused_requests, failure_types,
 
1055
                                &refused_count);
1100
1056
    } else {
1101
 
        hip_add_registration_server(entry, reg_request->lifetime, reg_types,
1102
 
                                    type_count, accepted_requests,
1103
 
                                    accepted_lifetimes, &accepted_count,
1104
 
                                    refused_requests, failure_types,
1105
 
                                    &refused_count);
 
1057
        add_registration_server(entry, reg_request->lifetime, reg_types,
 
1058
                                type_count, accepted_requests,
 
1059
                                accepted_lifetimes, &accepted_count,
 
1060
                                refused_requests, failure_types,
 
1061
                                &refused_count);
1106
1062
    }
1107
1063
 
1108
 
    HIP_DEBUG("Number of accepted service requests: %d, number of refused " \
 
1064
    HIP_DEBUG("Number of accepted service requests: %d, number of refused "
1109
1065
              "service requests: %d.\n", accepted_count, refused_count);
1110
1066
 
1111
1067
    /* The registration is now done. Next, we build the REG_RESPONSE and
1172
1128
 * duplicate Reg Type one after the other.
1173
1129
 *
1174
1130
 * The parameter lifetime is investigated first. If it is zero i.e. the server
1175
 
 * has canceled a service and hip_del_registration_client() is called. Otherwise
 
1131
 * has canceled a service and del_registration_client() is called. Otherwise
1176
1132
 * the server has granted us the services we requested and
1177
 
 * hip_add_registration_client() is called.
 
1133
 * add_registration_client() is called.
1178
1134
 *
1179
1135
 * @param     entry a pointer to a host association which is registering.
1180
1136
 * @param     msg   a pointer to HIP message from where to dig out the
1181
1137
 *                  REG_RESPONSE parameter.
1182
1138
 * @return          -1 if the message @c msg did not contain a
1183
1139
 *                  REG_RESPONSE parameter, zero otherwise.
1184
 
 * @see             hip_add_registration_client().
1185
 
 * @see             hip_del_registration_client().
 
1140
 * @see             add_registration_client().
 
1141
 * @see             del_registration_client().
1186
1142
 */
1187
1143
int hip_handle_param_reg_response(struct hip_hadb_state *entry,
1188
1144
                                  struct hip_common *msg)
1207
1163
                sizeof(reg_response->lifetime);
1208
1164
 
1209
1165
    if (reg_response->lifetime == 0) {
1210
 
        hip_del_registration_client(entry, reg_types, type_count);
 
1166
        del_registration_client(entry, reg_types, type_count);
1211
1167
    } else {
1212
 
        hip_add_registration_client(entry, reg_response->lifetime,
1213
 
                                    reg_types, type_count);
 
1168
        add_registration_client(entry, reg_response->lifetime,
 
1169
                                reg_types, type_count);
1214
1170
    }
1215
1171
 
1216
1172
out_err:
1225
1181
 *                      representation. This should be at least 256 bytes long.
1226
1182
 * @return              -1 if @c type_string is NULL, zero otherwise.
1227
1183
 */
1228
 
static int hip_get_registration_failure_string(uint8_t failure_type,
1229
 
                                               char *type_string)
 
1184
static int get_registration_failure_string(uint8_t failure_type,
 
1185
                                           char *type_string)
1230
1186
{
1231
1187
    if (type_string == NULL) {
1232
1188
        return -1;
1244
1200
        break;
1245
1201
    case HIP_REG_CANCEL_REQUIRED:
1246
1202
        memcpy(type_string,
1247
 
               "Cancellation of a previously granted service is " \
1248
 
               "required.",
1249
 
               sizeof("Cancellation of a previously granted service " \
1250
 
                      "is required."));
 
1203
               "Cancellation of a previously granted service is required.",
 
1204
               sizeof("Cancellation of a previously granted service is required."));
1251
1205
        break;
1252
1206
    case HIP_REG_TRANSIENT_CONDITIONS:
1253
1207
        memcpy(type_string,
1254
 
               "The server is currently unable to provide services " \
 
1208
               "The server is currently unable to provide services "
1255
1209
               "due to transient conditions.",
1256
 
               sizeof("The server is currently unable to provide services " \
 
1210
               sizeof("The server is currently unable to provide services "
1257
1211
                      "due to transient conditions."));
1258
1212
        break;
1259
1213
    default:
1301
1255
                     sizeof(reg_failed->failure_type);
1302
1256
        reg_types = (const uint8_t *) hip_get_param_contents_direct(reg_failed) +
1303
1257
                    sizeof(reg_failed->failure_type);
1304
 
        hip_get_registration_failure_string(reg_failed->failure_type,
1305
 
                                            reason);
 
1258
        get_registration_failure_string(reg_failed->failure_type, reason);
1306
1259
 
1307
1260
        for (; i < type_count; i++) {
1308
1261
            switch (reg_types[i]) {
1309
1262
            case HIP_SERVICE_RENDEZVOUS:
1310
1263
            {
1311
 
                HIP_DEBUG("The server has refused to grant us " \
 
1264
                HIP_DEBUG("The server has refused to grant us "
1312
1265
                          "rendezvous service.\n%s\n", reason);
1313
1266
                hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RVS);
1314
 
                hip_del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
 
1267
                del_pending_request_by_type(entry, HIP_SERVICE_RENDEZVOUS);
1315
1268
                hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_REFUSED_RVS);
1316
1269
                break;
1317
1270
            }
1318
1271
            case HIP_SERVICE_RELAY:
1319
1272
            {
1320
 
                HIP_DEBUG("The server has refused to grant us " \
 
1273
                HIP_DEBUG("The server has refused to grant us "
1321
1274
                          "relay service.\n%s\n", reason);
1322
1275
                hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_RELAY);
1323
 
                hip_del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
 
1276
                del_pending_request_by_type(entry, HIP_SERVICE_RELAY);
1324
1277
                hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_REFUSED_RELAY);
1325
1278
                break;
1326
1279
            }
1327
1280
            case HIP_SERVICE_FULLRELAY:
1328
1281
            {
1329
 
                HIP_DEBUG("The server has refused to grant us " \
 
1282
                HIP_DEBUG("The server has refused to grant us "
1330
1283
                          "full relay service.\n%s\n", reason);
1331
1284
                hip_hadb_cancel_local_controls(entry, HIP_HA_CTRL_LOCAL_REQ_FULLRELAY);
1332
 
                hip_del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
 
1285
                del_pending_request_by_type(entry, HIP_SERVICE_FULLRELAY);
1333
1286
                hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_REFUSED_FULLRELAY);
1334
1287
                break;
1335
1288
            }
1336
1289
            default:
1337
 
                HIP_DEBUG("The server has refused to grant us " \
 
1290
                HIP_DEBUG("The server has refused to grant us "
1338
1291
                          "an unknown service (%u).\n%s\n",
1339
1292
                          reg_types[i], reason);
1340
 
                hip_del_pending_request_by_type(entry, reg_types[i]);
 
1293
                del_pending_request_by_type(entry, reg_types[i]);
1341
1294
                hip_hadb_set_peer_controls(entry, HIP_HA_CTRL_PEER_REFUSED_UNSUP);
1342
1295
                break;
1343
1296
            }
1424
1377
    struct sockaddr_in            sock_addr     = { 0 };
1425
1378
    struct in6_addr               server_addr;
1426
1379
    const hip_hit_t              *dst_hit   = NULL;
1427
 
    struct hip_hadb_state        *entry     = NULL;
 
1380
    struct hip_hadb_state        *entry     = NULL, *ha_opp = NULL;
1428
1381
    struct in6_addr               opp_hit   = { { { 0 } } }, src_ip = { { { 0 } } };
1429
1382
    struct in6_addr               hit_local = { { { 0 } } };
1430
1383
    int                           err       = 0;
1459
1412
    }
1460
1413
 
1461
1414
    if (dst_hit == NULL) {
1462
 
        HIP_DEBUG("No HIT parameter found from the user " \
 
1415
        HIP_DEBUG("No HIT parameter found from the user "
1463
1416
                  "message. Trying opportunistic mode \n");
1464
1417
        opp_mode = 1;
1465
1418
    } else if (dst_ip == NULL) {
1466
 
        HIP_ERROR("No IPV6 parameter found from the user " \
1467
 
                  "message.\n");
 
1419
        HIP_ERROR("No IPV6 parameter found from the user message.\n");
1468
1420
        err = -1;
1469
1421
        goto out_err;
1470
1422
    } else if (reg_req == NULL) {
1471
 
        HIP_ERROR("No REG_REQUEST parameter found from the " \
1472
 
                  "user message.\n");
 
1423
        HIP_ERROR("No REG_REQUEST parameter found from the user message.\n");
1473
1424
        err = -1;
1474
1425
        goto out_err;
1475
1426
    }
1477
1428
    if (!opp_mode) {
1478
1429
        HIP_IFEL(hip_hadb_add_peer_info(dst_hit, dst_ip,
1479
1430
                                        NULL, NULL),
1480
 
                 -1, "Error on adding server "  \
 
1431
                 -1, "Error on adding server  "
1481
1432
                     "HIT to IP address mapping to the hadb.\n");
1482
1433
 
1483
1434
        /* Fetch the hadb entry just created. */
1484
1435
        entry = hip_hadb_try_to_find_by_peer_hit(dst_hit);
1485
1436
 
1486
1437
        if (entry == NULL) {
1487
 
            HIP_ERROR("Error on fetching server HIT to IP address " \
 
1438
            HIP_ERROR("Error on fetching server HIT to IP address "
1488
1439
                      "mapping from the haDB.\n");
1489
1440
            err = -1;
1490
1441
            goto out_err;
1508
1459
                 -1,
1509
1460
                 "Cannot find source address\n");
1510
1461
 
 
1462
        /* @todo: Workaround for opp. registration when a mapping already
 
1463
         *        pre-exists. Similarly to the code in the end of this
 
1464
         *        function, this can be removed after bug id 592135 is
 
1465
         *        resolved. */
 
1466
#ifdef CONFIG_HIP_RVS
 
1467
        ha_opp = hip_hadb_find_rvs_candidate_entry(&hit_local, dst_ip);
 
1468
#endif /* CONFIG_HIP_RVS */
 
1469
 
1511
1470
        HIP_IFEL(hip_hadb_add_peer_info_complete(&hit_local,
1512
1471
                                                 &opp_hit,
1513
1472
                                                 NULL,
1522
1481
                 "Did not find entry\n");
1523
1482
    }
1524
1483
 
 
1484
    entry->hip_version = hip_get_default_version();
 
1485
 
1525
1486
    reg_types  = reg_req->reg_type;
1526
1487
    type_count = hip_get_param_contents_len(reg_req) -
1527
1488
                 sizeof(reg_req->lifetime);
1529
1490
    for (; i < type_count; i++) {
1530
1491
        pending_req = malloc(sizeof(struct hip_pending_request));
1531
1492
        if (pending_req == NULL) {
1532
 
            HIP_ERROR("Error on allocating memory for a " \
 
1493
            HIP_ERROR("Error on allocating memory for a "
1533
1494
                      "pending registration request.\n");
1534
1495
            err = -1;
1535
1496
            goto out_err;
1590
1551
        }
1591
1552
    }
1592
1553
 
1593
 
    /* Workaround for registration when a mapping already pre-exists
1594
 
     * (inserted e.g. with "hipconf add map"). This can be removed
1595
 
     * after bug id 592135 is resolved. */
1596
 
    if (entry->state != HIP_STATE_NONE || HIP_STATE_UNASSOCIATED) {
1597
 
        struct hip_common *msg2 = calloc(HIP_MAX_PACKET, 1);
 
1554
    /** @todo Workaround for registration when a mapping already pre-exists
 
1555
     *        due to earlier registration. This and the code filling the
 
1556
     *        ha_opp can be removed after bug id 592135 is resolved. */
 
1557
    if (!(entry->state == HIP_STATE_NONE || entry->state == HIP_STATE_UNASSOCIATED) || ha_opp) {
 
1558
        struct hip_common *msg2     = calloc(HIP_MAX_PACKET, 1);
 
1559
        hip_hit_t         *peer_hit = ha_opp ? &ha_opp->hit_peer : &entry->hit_peer;
 
1560
 
1598
1561
        HIP_IFE(msg2 == 0, -1);
1599
1562
        HIP_IFE(hip_build_user_hdr(msg2, HIP_MSG_RST, 0), -1);
1600
1563
        HIP_IFE(hip_build_param_contents(msg2,
1601
 
                                         &entry->hit_peer,
 
1564
                                         peer_hit,
1602
1565
                                         HIP_PARAM_HIT,
1603
1566
                                         sizeof(hip_hit_t)),
1604
1567
                -1);