|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +#include "nsconfig.h" |
| 20 | +#include <string.h> |
| 21 | +#include "ns_types.h" |
| 22 | +#include "ns_list.h" |
| 23 | +#include "ns_trace.h" |
| 24 | +#include "nsdynmemLIB.h" |
| 25 | +#include "fhss_config.h" |
| 26 | +#include "NWK_INTERFACE/Include/protocol.h" |
| 27 | +#include "mac_api.h" |
| 28 | +#include "mac_mcps.h" |
| 29 | +#include "Common_Protocols/ipv6_constants.h" |
| 30 | +#include "socket_api.h" |
| 31 | +#include "6LoWPAN/MAC/mac_helper.h" |
| 32 | +#include "6LoWPAN/MAC/mpx_api.h" |
| 33 | +#include "6LoWPAN/ws/ws_config.h" |
| 34 | +#include "6LoWPAN/ws/ws_eapol_pdu.h" |
| 35 | +#include "6LoWPAN/ws/ws_eapol_relay.h" |
| 36 | +#include "6LoWPAN/ws/ws_eapol_auth_relay.h" |
| 37 | +#include "Security/kmp/kmp_socket_api.h" |
| 38 | + |
| 39 | +#ifdef HAVE_WS |
| 40 | + |
| 41 | +#define TRACE_GROUP "wspsu" |
| 42 | + |
| 43 | +static int8_t ws_eapol_auth_relay_eapol_pdu_address_check(const uint8_t *eui_64); |
| 44 | +static int8_t ws_eapol_auth_relay_eapol_pdu_receive(const uint8_t *eui_64, const void *pdu, uint16_t size); |
| 45 | +static int8_t ws_eapol_auth_relay_socket_pdu_receive(const ns_address_t *src_addr, void *data, uint16_t len); |
| 46 | +static int8_t ws_eapol_auth_relay_send_to_kmp(const uint8_t *eui_64, const uint8_t *ip_addr, uint16_t port, const void *data, uint16_t data_len); |
| 47 | + |
| 48 | +// Wi-Sun EAPOL relay UDP data format |
| 49 | +typedef struct { |
| 50 | + uint8_t eui_64[8]; /**< Supplicant EUI-64 */ |
| 51 | + uint8_t kmp_id; /**< KMP id */ |
| 52 | +} eapol_relay_udp_pdu_t; |
| 53 | + |
| 54 | +int8_t ws_eapol_auth_relay_init(protocol_interface_info_entry_t *interface_ptr) |
| 55 | +{ |
| 56 | + ws_eapol_relay_init(interface_ptr); |
| 57 | + ws_eapol_relay_cb_register(ws_eapol_auth_relay_socket_pdu_receive); |
| 58 | + |
| 59 | + eapol_pdu_recv_cb_data_t cb_data; |
| 60 | + cb_data.priority = EAPOL_PDU_RECV_LOW_PRIORITY; |
| 61 | + cb_data.addr_check = ws_eapol_auth_relay_eapol_pdu_address_check; |
| 62 | + cb_data.receive = ws_eapol_auth_relay_eapol_pdu_receive; |
| 63 | + |
| 64 | + if (ws_eapol_pdu_cb_register(&cb_data) < 0) { |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +int8_t ws_eapol_auth_relay_start(void) |
| 72 | +{ |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +static int8_t ws_eapol_auth_relay_eapol_pdu_address_check(const uint8_t *eui_64) |
| 77 | +{ |
| 78 | + (void) eui_64; |
| 79 | + |
| 80 | + // Low priority, always route all here if asked |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +static int8_t ws_eapol_auth_relay_eapol_pdu_receive(const uint8_t *eui_64, const void *pdu, uint16_t size) |
| 85 | +{ |
| 86 | + ws_eapol_auth_relay_send_to_kmp(eui_64, ADDR_UNSPECIFIED, 0, pdu, size); |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +static int8_t ws_eapol_auth_relay_socket_pdu_receive(const ns_address_t *src_addr, void *data, uint16_t len) |
| 91 | +{ |
| 92 | + int8_t res = 0; |
| 93 | + |
| 94 | + // KMP message from source port 10254 |
| 95 | + if (src_addr->identifier == 10254) { |
| 96 | + kmp_udp_pdu_t *pdu = data; |
| 97 | + uint16_t data_len = len - sizeof(kmp_udp_pdu_t) + 2 * sizeof(uint8_t); |
| 98 | + |
| 99 | + // KMP protocol to relay -> packet with IP relay address -> to IP relay of node |
| 100 | + if (memcmp(pdu->relay_ip_addr, ADDR_UNSPECIFIED, 16) != 0) { |
| 101 | + res = ws_eapol_relay_send_to_relay(pdu->eui_64, pdu->relay_ip_addr, pdu->relay_port, |
| 102 | + &pdu->kmp_id, data_len); |
| 103 | + ns_dyn_mem_free(pdu); |
| 104 | + |
| 105 | + // KMP protocol to relay -> no IP relay address -> to MPX |
| 106 | + } else { |
| 107 | + void *eapol_pdu = ns_dyn_mem_temporary_alloc(data_len); |
| 108 | + if (!eapol_pdu) { |
| 109 | + ns_dyn_mem_free(pdu); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + memcpy(eapol_pdu, &pdu->kmp_id, data_len); |
| 113 | + |
| 114 | + res = ws_eapol_pdu_send_to_mpx(pdu->eui_64, eapol_pdu, data_len); |
| 115 | + ns_dyn_mem_free(pdu); |
| 116 | + } |
| 117 | + |
| 118 | + // Other source port (either 10253 or node relay source port) |
| 119 | + } else { |
| 120 | + eapol_relay_udp_pdu_t *pdu = data; |
| 121 | + uint16_t data_len = len - sizeof(eapol_relay_udp_pdu_t) + sizeof(uint8_t); |
| 122 | + res = ws_eapol_auth_relay_send_to_kmp(pdu->eui_64, src_addr->address, src_addr->identifier, |
| 123 | + &pdu->kmp_id, data_len); |
| 124 | + ns_dyn_mem_free(pdu); |
| 125 | + } |
| 126 | + |
| 127 | + return res; |
| 128 | +} |
| 129 | + |
| 130 | +static int8_t ws_eapol_auth_relay_send_to_kmp(const uint8_t *eui_64, const uint8_t *ip_addr, uint16_t port, const void *data, uint16_t data_len) |
| 131 | +{ |
| 132 | + uint16_t pdu_len = data_len + sizeof(kmp_udp_pdu_t) - 2 * sizeof(uint8_t); |
| 133 | + kmp_udp_pdu_t *pdu = ns_dyn_mem_temporary_alloc(pdu_len); |
| 134 | + if (!pdu) { |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + memcpy(pdu->relay_ip_addr, ip_addr, 16); |
| 139 | + pdu->relay_port = port; |
| 140 | + |
| 141 | + memcpy(&pdu->eui_64[0], eui_64, 8); |
| 142 | + memcpy(&pdu->kmp_id, data, data_len); |
| 143 | + |
| 144 | + ns_address_t dest_addr; |
| 145 | + if (ws_eapol_relay_kmp_ll_address_get(&dest_addr) < 0) { |
| 146 | + ns_dyn_mem_free(pdu); |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + ws_eapol_relay_socket_pdu_send(&dest_addr, pdu, pdu_len); |
| 151 | + ns_dyn_mem_free(pdu); |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +#endif /* HAVE_WS */ |
| 157 | + |
0 commit comments