1 /* 2 * Copyright (c) 2026, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef __SFCP_DEFS_H__ 9 #define __SFCP_DEFS_H__ 10 11 12 #include <cdefs.h> 13 #include <stdbool.h> 14 #include <stddef.h> 15 16 #include "sfcp_trusted_subnet.h" 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #define SFCP_PROTOCOL_VERSION (0b10) 23 24 /* 25 * Crptography 26 */ 27 struct __packed sfcp_cryptography_config_t { 28 uint16_t seq_num; 29 enum sfcp_cryptography_mode_t mode; 30 uint8_t trusted_subnet_id; 31 }; 32 33 struct __packed sfcp_cryptography_metadata_t { 34 struct sfcp_cryptography_config_t config; 35 uint8_t tag[16]; 36 }; 37 38 /* 39 * Header 40 */ 41 enum sfcp_packet_type_t { 42 SFCP_PACKET_TYPE_MSG_NEEDS_REPLY = 0b00, 43 SFCP_PACKET_TYPE_MSG_NO_REPLY = 0b01, 44 SFCP_PACKET_TYPE_REPLY = 0b10, 45 SFCP_PACKET_TYPE_PROTOCOL_ERROR_REPLY = 0b11, 46 }; 47 48 #define METADATA_PACKET_TYPE_OFFSET (6) 49 #define METADATA_PACKET_TYPE_MASK (0b11) 50 #define METADATA_USES_CRYPTOGRAPHY_OFFSET (5) 51 #define METADATA_USES_CRYPTOGRAPHY_MASK (0b1) 52 #define METADATA_USES_ID_EXTENSION_OFFSET (4) 53 #define METADATA_USES_ID_EXTENSION_MASK (0b1) 54 #define METADATA_PROTOCOL_VERSION_OFFSET (0) 55 #define METADATA_PROTOCOL_VERSION_MASK (0b111) 56 57 #define GET_METADATA_FIELD(_field, _metadata) \ 58 (((_metadata) >> METADATA_##_field##_OFFSET) & \ 59 (METADATA_##_field##_MASK)) 60 #define SET_METADATA_FIELD(_field, _value) \ 61 (((_value) & METADATA_##_field##_MASK) << (METADATA_##_field##_OFFSET)) 62 #define SET_ALL_METADATA_FIELDS(_packet_type, _uses_crypto, _uses_id, \ 63 _protocol_version) \ 64 (SET_METADATA_FIELD(PACKET_TYPE, (_packet_type)) | \ 65 SET_METADATA_FIELD(USES_CRYPTOGRAPHY, (_uses_crypto)) | \ 66 SET_METADATA_FIELD(USES_ID_EXTENSION, (_uses_id)) | \ 67 SET_METADATA_FIELD(PROTOCOL_VERSION, (_protocol_version))) 68 69 struct __packed sfcp_header_t { 70 /* 71 * Metadata field consists of (in descending bit order): 72 * - packet_type : 1 73 * - uses_cryptography : 1 74 * - uses_id_extension: 1 75 * - reserved: 2 76 * - protocol_version: 3 77 */ 78 uint8_t metadata; 79 uint8_t sender_id; 80 uint8_t receiver_id; 81 uint8_t message_id; 82 }; 83 84 /* 85 * Packet definition 86 */ 87 /* 88 * C standard dictates that a fleixble array member cannot by the only element in a 89 * struct and therefore we use uint8_t payload[1] to make the struct definition valid C 90 */ 91 struct __packed sfcp_id_extension_payload { 92 union __packed { 93 struct __packed { 94 uint16_t client_id; 95 uint16_t application_id; 96 uint8_t payload[1]; 97 } id_extension_used; 98 struct __packed { 99 uint8_t payload[1]; 100 } id_extension_not_used; 101 }; 102 }; 103 104 struct __packed sfcp_packet_t { 105 struct sfcp_header_t header; 106 union __packed { 107 struct __packed { 108 struct sfcp_cryptography_metadata_t 109 cryptography_metadata; 110 struct sfcp_id_extension_payload id_extension_payload; 111 } cryptography_used; 112 struct __packed { 113 struct sfcp_id_extension_payload id_extension_payload; 114 } cryptography_not_used; 115 struct __packed { 116 uint16_t client_id; 117 uint16_t protocol_error; 118 } error_reply; 119 }; 120 }; 121 122 /* 123 * Helper macros 124 */ 125 #define GET_SFCP_ID_EXTENSION_PAYLOAD(_packet, _uses_crypto) \ 126 ((_uses_crypto) ? \ 127 &(_packet)->cryptography_used.id_extension_payload : \ 128 &(_packet)->cryptography_not_used.id_extension_payload) 129 #define GET_SFCP_GENERIC_FIELD_PTR(_packet, _uses_crypto, _uses_id_extension, \ 130 _field_name) \ 131 ((_uses_id_extension) ? \ 132 &GET_SFCP_ID_EXTENSION_PAYLOAD(_packet, _uses_crypto) \ 133 ->id_extension_used._field_name : \ 134 &GET_SFCP_ID_EXTENSION_PAYLOAD(_packet, _uses_crypto) \ 135 ->id_extension_not_used._field_name) 136 137 #define GET_SFCP_PAYLOAD_PTR(_packet, _uses_crypto, _uses_id_extension) \ 138 GET_SFCP_GENERIC_FIELD_PTR(_packet, _uses_crypto, _uses_id_extension, \ 139 payload) 140 141 #define GET_SFCP_ID_EXTENSION_FIELD(_packet, _uses_crypto, _field_name) \ 142 GET_SFCP_ID_EXTENSION_PAYLOAD(_packet, _uses_crypto) \ 143 ->id_extension_used._field_name 144 #define GET_SFCP_CLIENT_ID(_packet, _uses_crypto) \ 145 GET_SFCP_ID_EXTENSION_FIELD(_packet, _uses_crypto, client_id) 146 #define GET_SFCP_APPLICATION_ID(_packet, _uses_crypto) \ 147 GET_SFCP_ID_EXTENSION_FIELD(_packet, _uses_crypto, application_id) 148 149 #define GET_SFCP_ERROR_REPLY_FIELD(_packet, _field) \ 150 ((_packet)->error_reply._field) 151 #define GET_SFCP_ERROR_REPLY_CLIENT_ID(_packet) \ 152 GET_SFCP_ERROR_REPLY_FIELD(_packet, client_id) 153 #define GET_SFCP_ERROR_REPLY_PROTCOL_ERROR(_packet) \ 154 GET_SFCP_ERROR_REPLY_FIELD(_packet, protocol_error) 155 156 #define SFCP_PACKET_SIZE_WITHOUT_PAYLOAD(_uses_crypto, _uses_id_extension) \ 157 ((_uses_crypto) ? \ 158 ((_uses_id_extension) ? \ 159 offsetof(struct sfcp_packet_t, \ 160 cryptography_used.id_extension_payload \ 161 .id_extension_used.payload) : \ 162 offsetof(struct sfcp_packet_t, \ 163 cryptography_used.id_extension_payload \ 164 .id_extension_not_used.payload)) : \ 165 ((_uses_id_extension) ? \ 166 offsetof(struct sfcp_packet_t, \ 167 cryptography_not_used.id_extension_payload \ 168 .id_extension_used.payload) : \ 169 offsetof(struct sfcp_packet_t, \ 170 cryptography_not_used.id_extension_payload \ 171 .id_extension_not_used.payload))) 172 #define SFCP_PACKET_SIZE_ERROR_REPLY \ 173 (offsetof(struct sfcp_packet_t, error_reply) + \ 174 sizeof(((struct sfcp_packet_t *)0)->error_reply)) 175 176 #ifdef __cplusplus 177 } 178 #endif 179 180 #endif /* __SFCP_DEFS_H__ */ 181