aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/core/messageHandler.h
blob: cf8ab3672bf422ff92fef1240bc65ae0718fe559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef messageHandler
#define messageHandler

#include <stdlib.h>

#include <hicn/hicn.h>
#include <src/core/messagePacketType.h>

#define H(packet) ((hicn_header_t *)packet)
#define H6(packet) (H(packet)->v6.ip)
#define H6T(packet) (H(packet)->v6.tcp)
#define H4(packet) (H(packet)->v4.ip)
#define H4T(packet) (H(packet)->v4.tcp)

#define HICN_V6_LEN(packet) (H6(packet).len)
#define HICN_V4_LEN(packet) (H4(packet).len)

/*** codes and types ***/
#define IPv6_TYPE 6
#define IPv4_TYPE 4
#define ICMP_WLDR_TYPE 42
#define ICMP_WLDR_CODE 0
#define ICMP_LB_TYPE 43

/*** masks and constants ***/
#define PATH_LABEL_MASK 0x8000      // 1000 0000 0000 0000
#define NOT_PATH_LABEL_MASK 0x7fff  // 0111 0000 0000 0000
#define UINT16_T_MASK 0x0000ffff    // 1111 1111 1111 1111
#define NEVER_EXPIRE \
  16777216  // 2^16 (max urgent pointer) * 2^8 (max reserved + NS bits)

/*** HICN ALLOWED PORTS ***/
#define CONTROL_PORT 9695
#define HTTP_PORT 8080

#define IPV6_DEFAULT_VERSION 6
#define IPV6_DEFAULT_TRAFFIC_CLASS 0
#define IPV6_DEFAULT_FLOW_LABEL 0

#define expected_lbl wldr_notification_lbl.expected_lbl
#define received_lbl wldr_notification_lbl.received_lbl

static inline uint8_t messageHandler_GetIPPacketType(const uint8_t *message) {
  return HICN_IP_VERSION(message);
}

static inline void messageHandler_UpdateTCPCheckSum(uint8_t *message,
                                                    uint16_t *old_val,
                                                    uint16_t *new_val,
                                                    uint8_t size) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv4_TYPE:
      for (uint8_t i = 0; i < size; i++) {
        uint16_t old_csum = ~(H4T(message).csum);
        uint16_t not_old_val = ~(*old_val);
        uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

        while (sum >> 16) {
          sum = (sum >> 16) + (sum & UINT16_T_MASK);
        }

        H4T(message).csum = ~sum;
        ++old_val;
        ++new_val;
      }
      break;
    case IPv6_TYPE:
      for (uint8_t i = 0; i < size; i++) {
        uint16_t old_csum = ~(H6T(message).csum);
        uint16_t not_old_val = ~(*old_val);
        uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

        while (sum >> 16) {
          sum = (sum >> 16) + (sum & UINT16_T_MASK);
        }

        H6T(message).csum = ~sum;
        ++old_val;
        ++new_val;
      }
      break;
    default:
      return;
  }
}

static inline void messageHandler_UpdateIPv4CheckSum(uint8_t *message,
                                                     uint16_t *old_val,
                                                     uint16_t *new_val,
                                                     uint8_t size) {
  for (uint8_t i = 0; i < size; i++) {
    uint16_t old_csum = ~(H4(message).csum);
    uint16_t not_old_val = ~(*old_val);
    uint32_t sum = (uint32_t)old_csum + not_old_val + *new_val;

    while (sum >> 16) {
      sum = (sum >> 16) + (sum & UINT16_T_MASK);
    }

    H4(message).csum = ~sum;
    ++old_val;
    ++new_val;
  }
}

static inline size_t messageHandler_GetEmptyTCPPacketSize(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN + TCP_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN + TCP_HDRLEN;
  else
    return 0;
}

static inline size_t messageHandler_GetICMPPacketSize(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN + ICMP_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN + ICMP_HDRLEN;
  else
    return 0;
}

static inline size_t messageHandler_GetIPHeaderLength(unsigned ipVersion) {
  if (ipVersion == IPv4_TYPE)
    return IPV4_HDRLEN;
  else if (ipVersion == IPv6_TYPE)
    return IPV6_HDRLEN;
  else
    return 0;
}

static inline bool messageHandler_IsValidHicnPacket(const uint8_t *message) {
  uint8_t version = messageHandler_GetIPPacketType(message);
  if (version == IPv6_TYPE || version == IPv4_TYPE) {
    return true;
  }
  return false;
}

static inline uint8_t messageHandler_NextHeaderType(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return (uint8_t)H6(message).nxt;
    case IPv4_TYPE:
      return (uint8_t)H4(message).protocol;
    default:
      return 0;
  }
}

static inline bool messageHandler_IsTCP(const uint8_t *message) {
  if (messageHandler_NextHeaderType(message) != IPPROTO_TCP) return false;
  return true;
}

static inline bool messageHandler_IsInterest(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  bool flag;
  hicn_packet_test_ece((hicn_header_t *)message,
                       &flag);  // ECE flag is set to 0 in interest packets
  if (flag == false) return true;
  return false;
}

static inline bool messageHandler_IsData(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  bool flag;
  hicn_packet_test_ece((hicn_header_t *)message,
                       &flag);  // ECE flag is set to 1 in data packets
  if (flag == true) return true;
  return false;
}

static inline bool messageHandler_IsWldrNotification(const uint8_t *message) {
  // this function returns true only if the packet is an ICMP packet in Wldr
  // form. type must be equal to ICMP_WLDR_TYPE and code equal to ICMP_WLDR_CODE
  uint8_t next_header = messageHandler_NextHeaderType(message);

  const uint8_t *icmp_ptr;
  if (next_header == IPPROTO_ICMP) {
    icmp_ptr = message + IPV4_HDRLEN;
  } else if (next_header == IPPROTO_ICMPV6) {
    icmp_ptr = message + IPV6_HDRLEN;
  } else {
    return false;
  }

  uint8_t type = ((_icmp_header_t *)icmp_ptr)->type;
  uint8_t code = ((_icmp_header_t *)icmp_ptr)->code;
  if (type == ICMP_WLDR_TYPE && code == ICMP_WLDR_CODE) {
    return true;
  }

  return false;
}

static inline bool messageHandler_IsLoadBalancerProbe(const uint8_t *message) {
  uint8_t next_header = messageHandler_NextHeaderType(message);

  const uint8_t *icmp_ptr;
  if (next_header == IPPROTO_ICMP) {
    icmp_ptr = message + IPV4_HDRLEN;
  } else if (next_header == IPPROTO_ICMPV6) {
    icmp_ptr = message + IPV6_HDRLEN;
  } else {
    return false;
  }

  uint8_t type = ((_icmp_header_t *)icmp_ptr)->type;
  if (type == ICMP_LB_TYPE) {
    return true;
  }

  return false;
}

static inline uint16_t messageHandler_GetTotalPacketLength(
    const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohs((uint16_t)HICN_V6_LEN(message)) + IPV6_HDRLEN;
    case IPv4_TYPE:
      return ntohs((uint16_t)HICN_V4_LEN(message));
    default:
      return 0;
  }
}

static inline uint32_t messageHandler_GetSegment(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohl((uint32_t)H6T(message).seq);
    case IPv4_TYPE:
      return ntohl((uint32_t)H4T(message).seq);
    default:
      return 0;
  }
}

static inline uint16_t messageHandler_GetExpectedWldrLabel(
    const uint8_t *message) {
  const uint8_t *icmp_ptr;
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      icmp_ptr = message + IPV6_HDRLEN;
      break;
    case IPv4_TYPE:
      icmp_ptr = message + IPV4_HDRLEN;
      break;
    default:
      return 0;
  }

  return ntohs(((_icmp_wldr_header_t *)icmp_ptr)->expected_lbl);
}

static inline uint16_t messageHandler_GetWldrLastReceived(
    const uint8_t *message) {
  const uint8_t *icmp_ptr;
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      icmp_ptr = message + IPV6_HDRLEN;
      break;
    case IPv4_TYPE:
      icmp_ptr = message + IPV4_HDRLEN;
      break;
    default:
      return 0;
  }

  return ntohs(((_icmp_wldr_header_t *)icmp_ptr)->received_lbl);
}

static inline uint16_t messageHandler_GetWldrLabel(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return ntohs((uint16_t)H6T(message).window);
    case IPv4_TYPE:
      return ntohs((uint16_t)H4T(message).window);
    default:
      return 0;
  }
}

static inline void messageHandler_SetWldrLabel(uint8_t *message,
                                               uint16_t label) {
  uint16_t old_val = messageHandler_GetWldrLabel(message);

  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      H6T(message).window = htons(label);
      break;
    case IPv4_TYPE:
      H4T(message).window = htons(label);
      break;
    default:
      break;
  }

  messageHandler_UpdateTCPCheckSum(message, &old_val, &label, 1);
}

static inline void messageHandler_ResetWldrLabel(uint8_t *message) {
  messageHandler_SetWldrLabel(message, 0);
}

static inline bool messageHandler_HasWldr(const uint8_t *message) {
  if (messageHandler_IsTCP(message)) {
    uint16_t lbl = messageHandler_GetWldrLabel(message);
    if (lbl != 0) {
      return true;
    }
  }
  return false;
}

static inline uint8_t messageHandler_GetProbePacketType(
    const uint8_t *message) {
  const uint8_t *icmp_ptr;
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      icmp_ptr = message + IPV6_HDRLEN;
      break;
    case IPv4_TYPE:
      icmp_ptr = message + IPV4_HDRLEN;
      break;
    default:
      return 0;
  }

  return ((_icmp_header_t *)icmp_ptr)->code;
}

static inline uint32_t messageHandler_GetPathLabel(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t path_label;
  int res = hicn_data_get_path_label((hicn_header_t *)message, &path_label);
  if (res < 0) return 0;
  return path_label;
}

static inline void messageHandler_SetPathLabel(uint8_t *message,
                                               uint32_t new_path_label) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t old_path_label;
  int res = hicn_data_get_path_label((hicn_header_t *)message, &old_path_label);
  if (res < 0) return;

  hicn_data_set_path_label((hicn_header_t *)message, new_path_label);

  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&old_path_label,
                                   (uint16_t *)&new_path_label, 2);
}

static inline void messageHandler_UpdatePathLabel(uint8_t *message,
                                                  uint8_t outFace) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t pl_old_32bit = messageHandler_GetPathLabel(message);
  uint8_t pl_old_8bit = (uint8_t)(pl_old_32bit >> 24UL);
  uint32_t pl_new_32bit =
      (uint32_t)((((pl_old_8bit << 1) | (pl_old_8bit >> 7)) ^ outFace) << 24UL);

  hicn_data_set_path_label((hicn_header_t *)message, pl_new_32bit);

  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&pl_old_32bit,
                                   (uint16_t *)&pl_new_32bit, 2);
}

static inline void messageHandler_ResetPathLabel(uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return;

  uint32_t pl_old_32bit = messageHandler_GetPathLabel(message);
  uint32_t pl_new_32bit = 0;
  hicn_data_set_path_label((hicn_header_t *)message, pl_new_32bit);
  messageHandler_UpdateTCPCheckSum(message, (uint16_t *)&pl_old_32bit,
                                   (uint16_t *)&pl_new_32bit, 2);
}

static inline uint16_t messageHandler_GetInterestLifetime(
    const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  hicn_lifetime_t lifetime;
  int res = hicn_interest_get_lifetime((hicn_header_t *)message, &lifetime);
  if (res < 0) return 0;
  return lifetime;
}

static inline bool messageHandler_HasInterestLifetime(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return false;

  if (messageHandler_GetInterestLifetime(message) == 0) return false;
  return true;
}

static inline uint32_t messageHandler_GetContentExpiryTime(
    const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t expirationTime;
  int res =
      hicn_data_get_expiry_time((hicn_header_t *)message, &expirationTime);
  if (res < 0) return 0;
  return expirationTime;
}

static inline bool messageHandler_HasContentExpiryTime(const uint8_t *message) {
  if (!messageHandler_IsTCP(message)) return 0;

  uint32_t expirationTime;
  int res =
      hicn_data_get_expiry_time((hicn_header_t *)message, &expirationTime);
  if (res < 0) return false;

  if (expirationTime == NEVER_EXPIRE) return false;

  return true;
}

static inline void *messageHandler_GetSource(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return &H6(message).saddr;
      break;
    case IPv4_TYPE:
      return &H4(message).saddr;
      break;
    default:
      return NULL;
  }
}

static inline void *messageHandler_GetDestination(const uint8_t *message) {
  switch (messageHandler_GetIPPacketType(message)) {
    case IPv6_TYPE:
      return &H6(message).daddr;
      break;
    case IPv4_TYPE:
      return &H4(message).daddr;
      break;
    default:
      return NULL;
  }
}

static inline void messageHandler_SetSource_IPv6(uint8_t *message,
                                                 struct in6_addr *address) {
  if (messageHandler_IsTCP(message)) {
    uint16_t *old_src = (uint16_t *)messageHandler_GetSource(message);
    messageHandler_UpdateTCPCheckSum(message, old_src, (uint16_t *)address, 8);
  }
  H6(message).saddr.as_in6addr = *address;
}

static inline void messageHandler_SetDestination_IPv6(
    uint8_t *message, struct in6_addr *address) {
  if (messageHandler_IsTCP(message)) {
    uint16_t *old_dst = (uint16_t *)messageHandler_GetDestination(message);
    messageHandler_UpdateTCPCheckSum(message, old_dst, (uint16_t *)address, 8);
  }
  H6(message).daddr.as_in6addr = *address;
}

static inline void messageHandler_SetSource_IPv4(uint8_t *message,
                                                 uint32_t *address) {
  // update tcp checksum
  uint16_t *old_src = (uint16_t *)messageHandler_GetSource(message);
  if (messageHandler_IsTCP(message)) {
    messageHandler_UpdateTCPCheckSum(message, old_src, (uint16_t *)address, 2);
  }
  // update IPv4 cheksum
  // the IPv4 checksum is not part of the psudo header for TCP checksum
  // calculation we can update them separetelly
  messageHandler_UpdateIPv4CheckSum(message, old_src, (uint16_t *)address, 2);

  H4(message).saddr.as_u32 = *address;
}

static inline void messageHandler_SetDestination_IPv4(uint8_t *message,
                                                      uint32_t *address) {
  uint16_t *old_dst = (uint16_t *)messageHandler_GetDestination(message);
  if (messageHandler_IsTCP(message)) {
    messageHandler_UpdateTCPCheckSum(message, old_dst, (uint16_t *)address, 2);
  }
  messageHandler_UpdateIPv4CheckSum(message, old_dst, (uint16_t *)address, 2);
  H4(message).daddr.as_u32 = *address;
}

static inline void messageHandler_SetWldrNotification(uint8_t *notification,
                                                      uint8_t *original,
                                                      uint16_t expected,
                                                      uint16_t received) {
  hicn_header_t *h = (hicn_header_t *)notification;
  switch (messageHandler_GetIPPacketType(original)) {
    case IPv6_TYPE: {
      *h = (hicn_header_t){.v6 = {
                               .ip =
                                   {
                                       .version_class_flow = htonl(
                                           (IPV6_DEFAULT_VERSION << 28) |
                                           (IPV6_DEFAULT_TRAFFIC_CLASS << 20) |
                                           (IPV6_DEFAULT_FLOW_LABEL & 0xfffff)),
                                       .len = htons(ICMP_HDRLEN),
                                       .nxt = IPPROTO_ICMPV6,
                                       .hlim = 5,
                                   },
                               .wldr =
                                   {
                                       .type = ICMP_WLDR_TYPE,
                                       .code = ICMP_WLDR_CODE,
                                       .expected_lbl = htons(expected),
                                       .received_lbl = htons(received),
                                   },
                           }};
      messageHandler_SetSource_IPv6(
          notification,
          (struct in6_addr *)messageHandler_GetDestination(original));
      messageHandler_SetDestination_IPv6(
          notification, (struct in6_addr *)messageHandler_GetSource(original));
      break;
    }
    case IPv4_TYPE: {
      break;
    }
    default:
      break;
  }
}

static inline void messageHandler_SetProbePacket(uint8_t *message,
                                                 uint8_t probeType,
                                                 struct in6_addr *src,
                                                 struct in6_addr *dst) {
  hicn_header_t *h = (hicn_header_t *)message;
  *h = (hicn_header_t){
      .v6 = {
          .ip =
              {
                  .version_class_flow =
                      htonl((IPV6_DEFAULT_VERSION << 28) |
                            (IPV6_DEFAULT_TRAFFIC_CLASS << 20) |
                            (IPV6_DEFAULT_FLOW_LABEL & 0xfffff)),
                  .len = htons(ICMP_HDRLEN),
                  .nxt = IPPROTO_ICMPV6,
                  .hlim = 5,  // this should be 1, but ... just to be safe
              },
          .icmp =
              {
                  .type = ICMP_LB_TYPE,
                  .code = probeType,
              },
      }};
  messageHandler_SetSource_IPv6(message, src);
  messageHandler_SetDestination_IPv6(message, dst);
}

#endif  // Metis_metis_MessageHandler