| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020-2024 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <endian.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include <arpa/inet.h> |
| 16 | |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <linux/if_ether.h> |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "log.h" |
| 24 | #include "net.h" |
| 25 | #include "net_def.h" |
| 26 | #include "security.h" |
| 27 | #include "stat.h" |
| 28 | #include "thread.h" |
| 29 | #include "utils.h" |
| 30 | #include "xdp.h" |
| 31 | |
| 32 | void increment_period(struct timespec *time, int64_t period_ns) |
| 33 | { |
| 34 | time->tv_nsec += period_ns; |
| 35 | |
| 36 | while (time->tv_nsec >= NSEC_PER_SEC) { |
| 37 | /* timespec nsec overflow */ |
| 38 | time->tv_sec++; |
| 39 | time->tv_nsec -= NSEC_PER_SEC; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void pthread_error(int ret, const char *message) |
| 44 | { |
| 45 | fprintf(stderr, "%s: %s\n", message, strerror(ret)); |
| 46 | } |
| 47 | |
| 48 | void swap_mac_addresses(void *buffer, size_t len) |
| 49 | { |
| 50 | unsigned char tmp[ETH_ALEN]; |
| 51 | struct ethhdr *eth = buffer; |
| 52 | |
| 53 | if (len < sizeof(*eth)) |
| 54 | return; |
| 55 | |
| 56 | memcpy(tmp, eth->h_source, sizeof(tmp)); |
| 57 | memcpy(eth->h_source, eth->h_dest, sizeof(tmp)); |
| 58 | memcpy(eth->h_dest, tmp, sizeof(tmp)); |
| 59 | } |
| 60 | |
| 61 | void insert_vlan_tag(void *buffer, size_t len, uint16_t vlan_tci) |
| 62 | { |
| 63 | struct vlan_ethernet_header *veth; |
| 64 | |
| 65 | if (len + sizeof(struct vlan_header) > XDP_FRAME_SIZE) |
| 66 | return; |
| 67 | |
| 68 | memmove(buffer + 2 * ETH_ALEN + sizeof(struct vlan_header), buffer + 2 * ETH_ALEN, |
| 69 | len - 2 * ETH_ALEN); |
| 70 | |
| 71 | veth = buffer; |
| 72 | |
| 73 | veth->vlan_encapsulated_proto = htons(ETH_P_PROFINET_RT); |
| 74 | veth->vlan_proto = htons(ETH_P_8021Q); |
| 75 | veth->vlantci = htons(vlan_tci); |
| 76 | } |
| 77 | |
| 78 | void build_vlan_frame_from_rx(const unsigned char *old_frame, size_t old_frame_len, |
| 79 | unsigned char *new_frame, size_t new_frame_len, uint16_t ether_type, |
| 80 | uint16_t vlan_tci) |
| 81 | { |
| 82 | struct vlan_ethernet_header *eth_new, *eth_old; |
| 83 | |
| 84 | if (new_frame_len < old_frame_len + sizeof(struct vlan_header)) |
| 85 | return; |
| 86 | |
| 87 | /* Copy payload */ |
| 88 | memcpy(new_frame + ETH_ALEN * 2 + sizeof(struct vlan_header), old_frame + ETH_ALEN * 2, |
| 89 | old_frame_len - ETH_ALEN * 2); |
| 90 | |
| 91 | /* Swap source destination */ |
| 92 | eth_new = (struct vlan_ethernet_header *)new_frame; |
| 93 | eth_old = (struct vlan_ethernet_header *)old_frame; |
| 94 | |
| 95 | memcpy(eth_new->destination, eth_old->source, ETH_ALEN); |
| 96 | memcpy(eth_new->source, eth_old->destination, ETH_ALEN); |
| 97 | |
| 98 | /* Inject VLAN info */ |
| 99 | eth_new->vlan_proto = htons(ETH_P_8021Q); |
| 100 | eth_new->vlantci = htons(vlan_tci); |
| 101 | eth_new->vlan_encapsulated_proto = htons(ether_type); |
| 102 | } |
| 103 | |
| 104 | static void initialize_secure_profinet_frame(enum security_mode mode, unsigned char *frame_data, |
| 105 | size_t frame_length, const unsigned char *source, |
| 106 | const unsigned char *destination, |
| 107 | const char *payload_pattern, |
| 108 | size_t payload_pattern_length, uint16_t vlan_tci, |
| 109 | uint16_t frame_id) |
| 110 | { |
| 111 | struct profinet_secure_header *rt; |
| 112 | struct vlan_ethernet_header *eth; |
| 113 | uint16_t security_length; |
| 114 | size_t payload_offset; |
| 115 | |
| 116 | /* Initialize to zero */ |
| 117 | memset(frame_data, '\0', frame_length); |
| 118 | |
| 119 | /* |
| 120 | * Profinet Frame: |
| 121 | * Destination |
| 122 | * Source |
| 123 | * VLAN tag: tpid 8100, id 0x00/0x101/102, dei 0, prio 6/5/4/3/2 |
| 124 | * Ether type: 8892 |
| 125 | * Frame id: TSN, RTC, RTA, DCP |
| 126 | * SecurityHeader |
| 127 | * Cycle counter |
| 128 | * Payload |
| 129 | * Padding to maxFrame - Checksum |
| 130 | * security_checksum |
| 131 | */ |
| 132 | |
| 133 | eth = (struct vlan_ethernet_header *)frame_data; |
| 134 | rt = (struct profinet_secure_header *)(frame_data + sizeof(*eth)); |
| 135 | |
| 136 | /* Ethernet header */ |
| 137 | memcpy(eth->destination, destination, ETH_ALEN); |
| 138 | memcpy(eth->source, source, ETH_ALEN); |
| 139 | |
| 140 | /* VLAN Header */ |
| 141 | eth->vlan_proto = htons(ETH_P_8021Q); |
| 142 | eth->vlantci = htons(vlan_tci); |
| 143 | eth->vlan_encapsulated_proto = htons(ETH_P_PROFINET_RT); |
| 144 | |
| 145 | /* Profinet Secure header */ |
| 146 | security_length = frame_length - sizeof(*eth) - sizeof(struct security_checksum); |
| 147 | rt->frame_id = htons(frame_id); |
| 148 | rt->security_meta_data.security_information = mode == SECURITY_MODE_AO ? 0x00 : 0x01; |
| 149 | rt->security_meta_data.security_length = htobe16(security_length); |
| 150 | rt->meta_data.frame_counter = 0; |
| 151 | rt->meta_data.cycle_counter = 0; |
| 152 | |
| 153 | /* Payload */ |
| 154 | payload_offset = sizeof(*eth) + sizeof(*rt); |
| 155 | memcpy(frame_data + payload_offset, payload_pattern, payload_pattern_length); |
| 156 | |
| 157 | /* security_checksum is set to zero and calculated for each frame on Tx */ |
| 158 | } |
| 159 | |
| 160 | static void initialize_rt_profinet_frame(unsigned char *frame_data, size_t frame_length, |
| 161 | const unsigned char *source, |
| 162 | const unsigned char *destination, |
| 163 | const char *payload_pattern, size_t payload_pattern_length, |
| 164 | uint16_t vlan_tci, uint16_t frame_id) |
| 165 | { |
| 166 | struct vlan_ethernet_header *eth; |
| 167 | struct profinet_rt_header *rt; |
| 168 | size_t payload_offset; |
| 169 | |
| 170 | /* Initialize to zero */ |
| 171 | memset(frame_data, '\0', frame_length); |
| 172 | |
| 173 | /* |
| 174 | * Profinet Frame: |
| 175 | * Destination |
| 176 | * Source |
| 177 | * VLAN tag: tpid 8100, id 0x00/0x101/102, dei 0, prio 6/5/4/3/2 |
| 178 | * Ether type: 8892 |
| 179 | * Frame id: TSN, RTC, RTA, DCP |
| 180 | * Cycle counter |
| 181 | * Payload |
| 182 | * Padding to maxFrame |
| 183 | */ |
| 184 | |
| 185 | eth = (struct vlan_ethernet_header *)frame_data; |
| 186 | rt = (struct profinet_rt_header *)(frame_data + sizeof(*eth)); |
| 187 | |
| 188 | /* Ethernet header */ |
| 189 | memcpy(eth->destination, destination, ETH_ALEN); |
| 190 | memcpy(eth->source, source, ETH_ALEN); |
| 191 | |
| 192 | /* VLAN Header */ |
| 193 | eth->vlan_proto = htons(ETH_P_8021Q); |
| 194 | eth->vlantci = htons(vlan_tci); |
| 195 | eth->vlan_encapsulated_proto = htons(ETH_P_PROFINET_RT); |
| 196 | |
| 197 | /* Profinet RT header */ |
| 198 | rt->frame_id = htons(frame_id); |
| 199 | rt->meta_data.frame_counter = 0; |
| 200 | rt->meta_data.cycle_counter = 0; |
| 201 | |
| 202 | /* Payload */ |
| 203 | payload_offset = sizeof(*eth) + sizeof(*rt); |
| 204 | memcpy(frame_data + payload_offset, payload_pattern, payload_pattern_length); |
| 205 | } |
| 206 | |
| 207 | void initialize_profinet_frame(enum security_mode mode, unsigned char *frame_data, |
| 208 | size_t frame_length, const unsigned char *source, |
| 209 | const unsigned char *destination, const char *payload_pattern, |
| 210 | size_t payload_pattern_length, uint16_t vlan_tci, uint16_t frame_id) |
| 211 | { |
| 212 | switch (mode) { |
| 213 | case SECURITY_MODE_NONE: |
| 214 | initialize_rt_profinet_frame(frame_data, frame_length, source, destination, |
| 215 | payload_pattern, payload_pattern_length, vlan_tci, |
| 216 | frame_id); |
| 217 | break; |
| 218 | case SECURITY_MODE_AE: |
| 219 | case SECURITY_MODE_AO: |
| 220 | initialize_secure_profinet_frame(mode, frame_data, frame_length, source, |
| 221 | destination, payload_pattern, |
| 222 | payload_pattern_length, vlan_tci, frame_id); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | int receive_profinet_frame(void *data, unsigned char *frame_data, size_t len) |
| 228 | { |
| 229 | struct thread_context *thread_context = data; |
| 230 | const struct traffic_class_config *class_config = thread_context->conf; |
| 231 | const unsigned char *expected_pattern = |
| 232 | (const unsigned char *)class_config->payload_pattern; |
| 233 | struct security_context *security_context = thread_context->rx_security_context; |
| 234 | const size_t expected_pattern_length = class_config->payload_pattern_length; |
| 235 | const bool mirror_enabled = class_config->rx_mirror_enabled; |
| 236 | const bool ignore_rx_errors = class_config->ignore_rx_errors; |
| 237 | size_t expected_frame_length = class_config->frame_length; |
| 238 | uint64_t tx_timestamp, rx_hw_timestamp, rx_sw_timestamp; |
| 239 | bool out_of_order, payload_mismatch, frame_id_mismatch; |
| 240 | unsigned char plaintext[MAX_FRAME_SIZE]; |
| 241 | unsigned char new_frame[MAX_FRAME_SIZE]; |
| 242 | struct profinet_secure_header *srt; |
| 243 | struct profinet_rt_header *rt; |
| 244 | uint64_t sequence_counter; |
| 245 | bool vlan_tag_missing; |
| 246 | void *p = frame_data; |
| 247 | struct ethhdr *eth; |
| 248 | uint16_t frame_id; |
| 249 | uint16_t proto; |
| 250 | |
| 251 | if (len < sizeof(struct vlan_ethernet_header)) { |
| 252 | log_message(LOG_LEVEL_WARNING, "%sRx: Too small frame received!\n", |
| 253 | thread_context->traffic_class); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
| 257 | eth = p; |
| 258 | if (eth->h_proto == htons(ETH_P_8021Q)) { |
| 259 | struct vlan_ethernet_header *veth = p; |
| 260 | |
| 261 | proto = veth->vlan_encapsulated_proto; |
| 262 | p += sizeof(*veth); |
| 263 | vlan_tag_missing = false; |
| 264 | } else { |
| 265 | proto = eth->h_proto; |
| 266 | p += sizeof(*eth); |
| 267 | expected_frame_length -= sizeof(struct vlan_header); |
| 268 | vlan_tag_missing = true; |
| 269 | } |
| 270 | |
| 271 | if (proto != htons(ETH_P_PROFINET_RT)) { |
| 272 | log_message(LOG_LEVEL_WARNING, "%sRx: Not a Profinet frame received!\n", |
| 273 | thread_context->traffic_class); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | /* Check frame length: VLAN tag might be stripped or not. Check it. */ |
| 278 | if (len != expected_frame_length) { |
| 279 | log_message(LOG_LEVEL_WARNING, "%sRx: Frame with wrong length received!\n", |
| 280 | thread_context->traffic_class); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
| 284 | /* Check cycle counter, frame id range and payload. */ |
| 285 | if (class_config->security_mode == SECURITY_MODE_NONE) { |
| 286 | rt = p; |
| 287 | p += sizeof(*rt); |
| 288 | |
| 289 | frame_id = be16toh(rt->frame_id); |
| 290 | sequence_counter = meta_data_to_sequence_counter( |
| 291 | &rt->meta_data, class_config->num_frames_per_cycle); |
| 292 | |
| 293 | tx_timestamp = meta_data_to_tx_timestamp(&rt->meta_data); |
| 294 | set_mirror_tx_timestamp(&rt->meta_data); |
| 295 | |
| 296 | } else if (class_config->security_mode == SECURITY_MODE_AO) { |
| 297 | |
| 298 | unsigned char *begin_of_security_checksum; |
| 299 | unsigned char *begin_of_aad_data; |
| 300 | size_t size_of_eth_header; |
| 301 | size_t size_of_aad_data; |
| 302 | struct security_iv iv; |
| 303 | int ret; |
| 304 | |
| 305 | srt = p; |
| 306 | p += sizeof(*srt); |
| 307 | |
| 308 | frame_id = be16toh(srt->frame_id); |
| 309 | sequence_counter = meta_data_to_sequence_counter( |
| 310 | &srt->meta_data, class_config->num_frames_per_cycle); |
| 311 | |
| 312 | tx_timestamp = meta_data_to_tx_timestamp(&srt->meta_data); |
| 313 | |
| 314 | /* Authenticate received Profinet Frame */ |
| 315 | size_of_eth_header = vlan_tag_missing ? sizeof(struct ethhdr) |
| 316 | : sizeof(struct vlan_ethernet_header); |
| 317 | |
| 318 | begin_of_aad_data = frame_data + size_of_eth_header; |
| 319 | size_of_aad_data = len - size_of_eth_header - sizeof(struct security_checksum); |
| 320 | begin_of_security_checksum = frame_data + (len - sizeof(struct security_checksum)); |
| 321 | |
| 322 | prepare_iv((const unsigned char *)class_config->security_iv_prefix, |
| 323 | sequence_counter, &iv); |
| 324 | |
| 325 | ret = security_decrypt(security_context, NULL, 0, begin_of_aad_data, |
| 326 | size_of_aad_data, begin_of_security_checksum, |
| 327 | (unsigned char *)&iv, NULL); |
| 328 | if (ret) |
| 329 | log_message(LOG_LEVEL_WARNING, |
| 330 | "%sRx: frame[%" PRIu64 "] Not authentificated\n", |
| 331 | thread_context->traffic_class, sequence_counter); |
| 332 | |
| 333 | set_mirror_tx_timestamp(&srt->meta_data); |
| 334 | |
| 335 | security_encrypt(security_context, NULL, 0, begin_of_aad_data, size_of_aad_data, |
| 336 | (unsigned char *)&iv, NULL, begin_of_security_checksum); |
| 337 | |
| 338 | } else { |
| 339 | unsigned char *begin_of_security_checksum; |
| 340 | unsigned char *begin_of_ciphertext; |
| 341 | unsigned char *begin_of_aad_data; |
| 342 | size_t size_of_ciphertext; |
| 343 | size_t size_of_eth_header; |
| 344 | size_t size_of_aad_data; |
| 345 | struct security_iv iv; |
| 346 | int ret; |
| 347 | |
| 348 | srt = p; |
| 349 | |
| 350 | frame_id = be16toh(srt->frame_id); |
| 351 | sequence_counter = meta_data_to_sequence_counter( |
| 352 | &srt->meta_data, class_config->num_frames_per_cycle); |
| 353 | |
| 354 | tx_timestamp = meta_data_to_tx_timestamp(&srt->meta_data); |
| 355 | |
| 356 | /* Authenticate received Profinet Frame */ |
| 357 | size_of_eth_header = vlan_tag_missing ? sizeof(struct ethhdr) |
| 358 | : sizeof(struct vlan_ethernet_header); |
| 359 | |
| 360 | begin_of_aad_data = frame_data + size_of_eth_header; |
| 361 | size_of_aad_data = sizeof(*srt); |
| 362 | begin_of_security_checksum = frame_data + (len - sizeof(struct security_checksum)); |
| 363 | begin_of_ciphertext = frame_data + size_of_eth_header + sizeof(*srt); |
| 364 | size_of_ciphertext = len - sizeof(struct vlan_ethernet_header) - |
| 365 | sizeof(struct profinet_secure_header) - |
| 366 | sizeof(struct security_checksum); |
| 367 | |
| 368 | prepare_iv((const unsigned char *)class_config->security_iv_prefix, |
| 369 | sequence_counter, &iv); |
| 370 | |
| 371 | ret = security_decrypt(security_context, begin_of_ciphertext, size_of_ciphertext, |
| 372 | begin_of_aad_data, size_of_aad_data, |
| 373 | begin_of_security_checksum, (unsigned char *)&iv, plaintext); |
| 374 | if (ret) |
| 375 | log_message(LOG_LEVEL_WARNING, |
| 376 | "%sRx: frame[%" PRIu64 "] Not authentificated and decrypted\n", |
| 377 | thread_context->traffic_class, sequence_counter); |
| 378 | |
| 379 | /* plaintext points to the decrypted payload */ |
| 380 | p = plaintext; |
| 381 | |
| 382 | set_mirror_tx_timestamp(&srt->meta_data); |
| 383 | |
| 384 | security_encrypt(security_context, thread_context->payload_pattern, |
| 385 | thread_context->payload_pattern_length, begin_of_aad_data, |
| 386 | size_of_aad_data, (unsigned char *)&iv, begin_of_ciphertext, |
| 387 | begin_of_security_checksum); |
| 388 | } |
| 389 | |
| 390 | xdp_get_timestamp_metadata(frame_data, &rx_hw_timestamp, &rx_sw_timestamp); |
| 391 | out_of_order = sequence_counter != thread_context->rx_sequence_counter; |
| 392 | payload_mismatch = memcmp(p, expected_pattern, expected_pattern_length); |
| 393 | frame_id_mismatch = frame_id != thread_context->frame_id; |
| 394 | |
| 395 | stat_frame_received(thread_context->frame_type, sequence_counter, out_of_order, |
| 396 | payload_mismatch, frame_id_mismatch, tx_timestamp, rx_hw_timestamp, |
| 397 | rx_sw_timestamp); |
| 398 | |
| 399 | if (frame_id_mismatch) |
| 400 | log_message( |
| 401 | LOG_LEVEL_WARNING, "%sRx: frame[%" PRIu64 "] FrameId mismatch: 0x%4x!\n", |
| 402 | thread_context->traffic_class, sequence_counter, thread_context->frame_id); |
| 403 | |
| 404 | if (out_of_order) { |
| 405 | if (!ignore_rx_errors) |
| 406 | log_message(LOG_LEVEL_WARNING, |
| 407 | "%sRx: frame[%" PRIu64 "] SequenceCounter mismatch: %" PRIu64 |
| 408 | "!\n", |
| 409 | thread_context->traffic_class, sequence_counter, |
| 410 | thread_context->rx_sequence_counter); |
| 411 | thread_context->rx_sequence_counter++; |
| 412 | } |
| 413 | |
| 414 | if (payload_mismatch) |
| 415 | log_message(LOG_LEVEL_WARNING, |
| 416 | "%sRx: frame[%" PRIu64 "] Payload Pattern mismatch!\n", |
| 417 | thread_context->traffic_class, sequence_counter); |
| 418 | |
| 419 | thread_context->rx_sequence_counter++; |
| 420 | |
| 421 | /* |
| 422 | * If mirror enabled, assemble and store the frame for Tx later. |
| 423 | * |
| 424 | * In case of XDP the Rx umem area will be reused for Tx. |
| 425 | */ |
| 426 | if (!mirror_enabled) |
| 427 | return 0; |
| 428 | |
| 429 | if (class_config->xdp_enabled) { |
| 430 | /* Re-add vlan tag */ |
| 431 | if (vlan_tag_missing) |
| 432 | insert_vlan_tag(frame_data, len, |
| 433 | class_config->vid | class_config->pcp << VLAN_PCP_SHIFT); |
| 434 | |
| 435 | /* Swap mac addresses inline */ |
| 436 | swap_mac_addresses(frame_data, len); |
| 437 | } else { |
| 438 | /* Build new frame for Tx with VLAN info. */ |
| 439 | build_vlan_frame_from_rx(frame_data, len, new_frame, sizeof(new_frame), |
| 440 | ETH_P_PROFINET_RT, |
| 441 | class_config->vid | class_config->pcp << VLAN_PCP_SHIFT); |
| 442 | |
| 443 | /* Store the new frame. */ |
| 444 | ring_buffer_add(thread_context->mirror_buffer, new_frame, |
| 445 | len + sizeof(struct vlan_header)); |
| 446 | } |
| 447 | |
| 448 | /* RTA is a burst traffic class and needs to update num_frames_available */ |
| 449 | if (thread_context->frame_type == RTA_FRAME_TYPE) { |
| 450 | pthread_mutex_lock(&thread_context->data_mutex); |
| 451 | thread_context->num_frames_available++; |
| 452 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | int prepare_frame_for_tx(const struct prepare_frame_config *frame_config) |
| 459 | { |
| 460 | /* mode == NONE may be called from PROFINET or GenericL2 */ |
| 461 | if (frame_config->mode == SECURITY_MODE_NONE) { |
| 462 | /* Adjust meta data in frame */ |
| 463 | struct reference_meta_data *meta_data = |
| 464 | (struct reference_meta_data *)(frame_config->frame_data + |
| 465 | frame_config->meta_data_offset); |
| 466 | |
| 467 | sequence_counter_to_meta_data(meta_data, frame_config->sequence_counter, |
| 468 | frame_config->num_frames_per_cycle); |
| 469 | |
| 470 | tx_timestamp_to_meta_data(meta_data, frame_config->tx_timestamp); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | /* mode == AO is PROFINET specific */ |
| 475 | else if (frame_config->mode == SECURITY_MODE_AO) { |
| 476 | unsigned char *begin_of_security_checksum; |
| 477 | struct profinet_secure_header *srt; |
| 478 | struct vlan_ethernet_header *eth; |
| 479 | unsigned char *begin_of_aad_data; |
| 480 | struct security_iv iv; |
| 481 | size_t size_of_aad_data; |
| 482 | |
| 483 | /* Adjust meta data first */ |
| 484 | srt = (struct profinet_secure_header *)(frame_config->frame_data + sizeof(*eth)); |
| 485 | sequence_counter_to_meta_data(&srt->meta_data, frame_config->sequence_counter, |
| 486 | frame_config->num_frames_per_cycle); |
| 487 | |
| 488 | tx_timestamp_to_meta_data(&srt->meta_data, frame_config->tx_timestamp); |
| 489 | |
| 490 | /* |
| 491 | * Then, calculate checksum over data and store it at the end of the frame. The |
| 492 | * authenfication spans begins with the FrameID and ends before the final security |
| 493 | * checksum. |
| 494 | */ |
| 495 | prepare_iv(frame_config->iv_prefix, frame_config->sequence_counter, &iv); |
| 496 | begin_of_aad_data = frame_config->frame_data + sizeof(*eth); |
| 497 | size_of_aad_data = frame_config->frame_length - sizeof(*eth) - |
| 498 | sizeof(struct security_checksum); |
| 499 | begin_of_security_checksum = |
| 500 | frame_config->frame_data + |
| 501 | (frame_config->frame_length - sizeof(struct security_checksum)); |
| 502 | return security_encrypt(frame_config->security_context, NULL, 0, begin_of_aad_data, |
| 503 | size_of_aad_data, (unsigned char *)&iv, NULL, |
| 504 | begin_of_security_checksum); |
| 505 | } |
| 506 | /* mode == AE is PROFINET specific too */ |
| 507 | else { |
| 508 | unsigned char *begin_of_security_checksum; |
| 509 | unsigned char *begin_of_ciphertext; |
| 510 | struct profinet_secure_header *srt; |
| 511 | struct vlan_ethernet_header *eth; |
| 512 | unsigned char *begin_of_aad_data; |
| 513 | struct security_iv iv; |
| 514 | size_t size_of_aad_data; |
| 515 | |
| 516 | /* Adjust cycle counter first */ |
| 517 | srt = (struct profinet_secure_header *)(frame_config->frame_data + sizeof(*eth)); |
| 518 | sequence_counter_to_meta_data(&srt->meta_data, frame_config->sequence_counter, |
| 519 | frame_config->num_frames_per_cycle); |
| 520 | |
| 521 | tx_timestamp_to_meta_data(&srt->meta_data, frame_config->tx_timestamp); |
| 522 | |
| 523 | /* |
| 524 | * Then, calculate checksum over data and store it at the end of the frame. The |
| 525 | * authenfication spans begins with the FrameID and ends before the final security |
| 526 | * checksum. In addition, the payload pattern in encrypted and stored in the frame. |
| 527 | */ |
| 528 | prepare_iv(frame_config->iv_prefix, frame_config->sequence_counter, &iv); |
| 529 | begin_of_aad_data = frame_config->frame_data + sizeof(*eth); |
| 530 | size_of_aad_data = sizeof(*srt); |
| 531 | begin_of_security_checksum = |
| 532 | frame_config->frame_data + |
| 533 | (frame_config->frame_length - sizeof(struct security_checksum)); |
| 534 | begin_of_ciphertext = frame_config->frame_data + sizeof(*eth) + sizeof(*srt); |
| 535 | return security_encrypt( |
| 536 | frame_config->security_context, frame_config->payload_pattern, |
| 537 | frame_config->payload_pattern_length, begin_of_aad_data, size_of_aad_data, |
| 538 | (unsigned char *)&iv, begin_of_ciphertext, begin_of_security_checksum); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void prepare_iv(const unsigned char *iv_prefix, uint64_t sequence_counter, struct security_iv *iv) |
| 543 | { |
| 544 | /* |
| 545 | * The initial vector is constructed by concatenating IvPrefix | sequenceCounter. The prefix |
| 546 | * and the counter consist of six bytes each. Therefore, the sequenceCounter is converted to |
| 547 | * LE to ignore the last two upper bytes. That leaves 2^48 possible counter values to create |
| 548 | * unique IVs. |
| 549 | */ |
| 550 | |
| 551 | memcpy(iv->iv_prefix, iv_prefix, SECURITY_IV_PREFIX_LEN); |
| 552 | iv->counter = htole64(sequence_counter); |
| 553 | } |
| 554 | |
| 555 | void prepare_openssl(struct security_context *context) |
| 556 | { |
| 557 | unsigned char iv[SECURITY_IV_LEN] = "012345678901"; |
| 558 | unsigned char dummy_frame[2048] = {5}; |
| 559 | |
| 560 | if (!context) |
| 561 | return; |
| 562 | |
| 563 | security_encrypt(context, NULL, 0, dummy_frame, |
| 564 | sizeof(dummy_frame) - sizeof(struct security_checksum), iv, NULL, |
| 565 | dummy_frame + sizeof(dummy_frame) - sizeof(struct security_checksum)); |
| 566 | |
| 567 | security_decrypt(context, NULL, 0, dummy_frame, |
| 568 | sizeof(dummy_frame) - sizeof(struct security_checksum), |
| 569 | dummy_frame + sizeof(dummy_frame) - sizeof(struct security_checksum), iv, |
| 570 | NULL); |
| 571 | } |
| 572 | |
| 573 | int get_thread_start_time(uint64_t base_offset, struct timespec *wakeup_time) |
| 574 | { |
| 575 | const uint64_t base_start_offset = app_config.application_base_start_offset_ns; |
| 576 | const uint64_t base_start_time = app_config.application_base_start_time_ns; |
| 577 | const clockid_t profinet_clock_id = app_config.application_clock_id; |
| 578 | int ret = 0; |
| 579 | |
| 580 | if (base_start_time) |
| 581 | ns_to_ts(base_start_time + base_start_offset + base_offset, wakeup_time); |
| 582 | else |
| 583 | ret = clock_gettime(profinet_clock_id, wakeup_time); |
| 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | static int latency_fd = -1; |
| 589 | |
| 590 | void configure_cpu_latency(void) |
| 591 | { |
| 592 | /* Avoid the CPU to enter deep sleep states */ |
| 593 | int32_t lat = 0; |
| 594 | ssize_t ret; |
| 595 | int fd; |
| 596 | |
| 597 | fd = open("/dev/cpu_dma_latency", O_RDWR); |
| 598 | if (fd == -1) |
| 599 | return; |
| 600 | |
| 601 | ret = write(fd, &lat, sizeof(lat)); |
| 602 | if (ret != sizeof(lat)) { |
| 603 | close(latency_fd); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | latency_fd = fd; |
| 608 | } |
| 609 | |
| 610 | void restore_cpu_latency(void) |
| 611 | { |
| 612 | if (latency_fd > 0) |
| 613 | close(latency_fd); |
| 614 | } |
| 615 | |
| 616 | void print_mac_address(const unsigned char *mac_address) |
| 617 | { |
| 618 | int i; |
| 619 | |
| 620 | for (i = 0; i < ETH_ALEN; ++i) { |
| 621 | printf("%02x", mac_address[i]); |
| 622 | if (i != ETH_ALEN - 1) |
| 623 | printf("-"); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void print_payload_pattern(const char *payload_pattern, size_t payload_pattern_length) |
| 628 | { |
| 629 | size_t i; |
| 630 | |
| 631 | for (i = 0; i < payload_pattern_length; ++i) |
| 632 | printf("0x%02x ", payload_pattern[i]); |
| 633 | } |
| 634 | |
| 635 | uint32_t get_meta_data_offset(enum stat_frame_type frame_type, enum security_mode security_mode) |
| 636 | { |
| 637 | uint32_t meta_data_offset = 0; |
| 638 | |
| 639 | switch (frame_type) { |
| 640 | /* PROFINET Frames w/o security headers */ |
| 641 | case TSN_HIGH_FRAME_TYPE: |
| 642 | case TSN_LOW_FRAME_TYPE: |
| 643 | case RTC_FRAME_TYPE: |
| 644 | case RTA_FRAME_TYPE: |
| 645 | case DCP_FRAME_TYPE: |
| 646 | switch (security_mode) { |
| 647 | case SECURITY_MODE_NONE: |
| 648 | meta_data_offset = sizeof(struct vlan_ethernet_header) + |
| 649 | offsetof(struct profinet_rt_header, meta_data); |
| 650 | break; |
| 651 | default: |
| 652 | meta_data_offset = sizeof(struct vlan_ethernet_header) + |
| 653 | offsetof(struct profinet_secure_header, meta_data); |
| 654 | } |
| 655 | break; |
| 656 | /* LLDP without VLAN */ |
| 657 | case LLDP_FRAME_TYPE: |
| 658 | meta_data_offset = sizeof(struct ethhdr); |
| 659 | break; |
| 660 | /* UDP without any headers */ |
| 661 | case UDP_HIGH_FRAME_TYPE: |
| 662 | case UDP_LOW_FRAME_TYPE: |
| 663 | meta_data_offset = 0; |
| 664 | break; |
| 665 | /* GenericL2 has its own frame layout */ |
| 666 | case GENERICL2_FRAME_TYPE: |
| 667 | meta_data_offset = sizeof(struct vlan_ethernet_header) + |
| 668 | offsetof(struct generic_l2_header, meta_data); |
| 669 | break; |
| 670 | case NUM_FRAME_TYPES: |
| 671 | meta_data_offset = 0; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | return meta_data_offset; |
| 676 | } |