15
15
* limitations under the License.
16
16
*/
17
17
18
-
19
18
#include "nsconfig.h"
20
19
#include <string.h>
21
20
#include "ns_types.h"
32
31
#include "6LoWPAN/MAC/mpx_api.h"
33
32
#include "6LoWPAN/ws/ws_config.h"
34
33
#include "6LoWPAN/ws/ws_eapol_pdu.h"
35
- #include "6LoWPAN/ws/ws_eapol_relay .h"
34
+ #include "6LoWPAN/ws/ws_eapol_relay_lib .h"
36
35
#include "6LoWPAN/ws/ws_eapol_auth_relay.h"
37
36
#include "common_functions.h"
38
37
39
38
#ifdef HAVE_WS
40
39
#ifdef HAVE_PAE_AUTH
41
40
42
- #define TRACE_GROUP "wspsu"
41
+ #define TRACE_GROUP "earl"
42
+
43
+ typedef struct {
44
+ protocol_interface_info_entry_t * interface_ptr ; /**< Interface pointer */
45
+ int8_t socket_id ; /**< Socket ID for relay */
46
+ ns_list_link_t link ; /**< Link */
47
+ } eapol_auth_relay_t ;
43
48
44
- static int8_t ws_eapol_auth_relay_eapol_pdu_address_check (const uint8_t * eui_64 );
45
- static int8_t ws_eapol_auth_relay_eapol_pdu_receive (const uint8_t * eui_64 , const void * pdu , uint16_t size );
46
- static int8_t ws_eapol_auth_relay_socket_pdu_receive (const ns_address_t * src_addr , void * data , uint16_t len );
47
- 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 );
49
+ static eapol_auth_relay_t * ws_eapol_auth_relay_get (protocol_interface_info_entry_t * interface_ptr );
50
+ static void ws_eapol_auth_relay_socket_cb (void * cb );
51
+ static int8_t ws_eapol_auth_relay_send_to_kmp (const eapol_auth_relay_t * eapol_auth_relay , const uint8_t * eui_64 , const uint8_t * ip_addr , uint16_t port , const void * data , uint16_t data_len );
48
52
49
- static bool relay_initiated = false ;
53
+ static NS_LIST_DEFINE ( eapol_auth_relay_list , eapol_auth_relay_t , link ) ;
50
54
51
- int8_t ws_eapol_auth_relay_init (protocol_interface_info_entry_t * interface_ptr )
55
+ int8_t ws_eapol_auth_relay_start (protocol_interface_info_entry_t * interface_ptr )
52
56
{
53
- if (relay_initiated ) {
54
- return 0 ;
57
+ if (! interface_ptr ) {
58
+ return -1 ;
55
59
}
56
- relay_initiated = true;
57
60
58
- ws_eapol_relay_init (interface_ptr );
59
- ws_eapol_relay_cb_register (ws_eapol_auth_relay_socket_pdu_receive );
61
+ if (ws_eapol_auth_relay_get (interface_ptr )) {
62
+ return -1 ;
63
+ }
60
64
61
- eapol_pdu_recv_cb_data_t cb_data ;
62
- cb_data . priority = EAPOL_PDU_RECV_LOW_PRIORITY ;
63
- cb_data . addr_check = ws_eapol_auth_relay_eapol_pdu_address_check ;
64
- cb_data . receive = ws_eapol_auth_relay_eapol_pdu_receive ;
65
+ eapol_auth_relay_t * eapol_auth_relay = ns_dyn_mem_alloc ( sizeof ( eapol_auth_relay_t )) ;
66
+ if (! eapol_auth_relay ) {
67
+ return -1 ;
68
+ }
65
69
66
- if (ws_eapol_pdu_cb_register (& cb_data ) < 0 ) {
70
+ eapol_auth_relay -> interface_ptr = interface_ptr ;
71
+ eapol_auth_relay -> socket_id = socket_open (IPV6_NH_UDP , 10253 , & ws_eapol_auth_relay_socket_cb );
72
+ if (eapol_auth_relay -> socket_id < 0 ) {
73
+ ns_dyn_mem_free (eapol_auth_relay );
67
74
return -1 ;
68
75
}
69
76
70
- return 0 ;
71
- }
77
+ ns_list_add_to_end (& eapol_auth_relay_list , eapol_auth_relay );
72
78
73
- int8_t ws_eapol_auth_relay_start (void )
74
- {
75
79
return 0 ;
76
80
}
77
81
78
- static int8_t ws_eapol_auth_relay_eapol_pdu_address_check ( const uint8_t * eui_64 )
82
+ int8_t ws_eapol_auth_relay_delete ( protocol_interface_info_entry_t * interface_ptr )
79
83
{
80
- (void ) eui_64 ;
84
+ if (!interface_ptr ) {
85
+ return -1 ;
86
+ }
87
+
88
+ eapol_auth_relay_t * eapol_auth_relay = ws_eapol_auth_relay_get (interface_ptr );
89
+ if (!eapol_auth_relay ) {
90
+ return -1 ;
91
+ }
92
+
93
+ socket_close (eapol_auth_relay -> socket_id );
94
+
95
+ ns_list_remove (& eapol_auth_relay_list , eapol_auth_relay );
96
+ ns_dyn_mem_free (eapol_auth_relay );
81
97
82
- // Low priority, always route all here if asked
83
98
return 0 ;
84
99
}
85
100
86
- static int8_t ws_eapol_auth_relay_eapol_pdu_receive ( const uint8_t * eui_64 , const void * pdu , uint16_t size )
101
+ static eapol_auth_relay_t * ws_eapol_auth_relay_get ( protocol_interface_info_entry_t * interface_ptr )
87
102
{
88
- ws_eapol_auth_relay_send_to_kmp (eui_64 , ADDR_UNSPECIFIED , 0 , pdu , size );
89
- return 0 ;
103
+ ns_list_foreach (eapol_auth_relay_t , entry , & eapol_auth_relay_list ) {
104
+ if (entry -> interface_ptr == interface_ptr ) {
105
+ return entry ;
106
+ }
107
+ }
108
+
109
+ return NULL ;
90
110
}
91
111
92
- static int8_t ws_eapol_auth_relay_socket_pdu_receive ( const ns_address_t * src_addr , void * data , uint16_t len )
112
+ static void ws_eapol_auth_relay_socket_cb ( void * cb )
93
113
{
94
- int8_t res = 0 ;
114
+ socket_callback_t * cb_data = cb ;
115
+
116
+ if (cb_data -> event_type != SOCKET_DATA ) {
117
+ return ;
118
+ }
119
+
120
+ eapol_auth_relay_t * eapol_auth_relay = NULL ;
121
+
122
+ ns_list_foreach (eapol_auth_relay_t , entry , & eapol_auth_relay_list ) {
123
+ if (entry -> socket_id == cb_data -> socket_id ) {
124
+ eapol_auth_relay = entry ;
125
+ break ;
126
+ }
127
+ }
128
+
129
+ if (!eapol_auth_relay ) {
130
+ return ;
131
+ }
132
+
133
+ uint8_t * socket_pdu = ns_dyn_mem_temporary_alloc (cb_data -> d_len );
134
+ if (!socket_pdu ) {
135
+ return ;
136
+ }
137
+
138
+ ns_address_t src_addr ;
139
+
140
+ if (socket_recvfrom (cb_data -> socket_id , socket_pdu , cb_data -> d_len , 0 , & src_addr ) != cb_data -> d_len ) {
141
+ ns_dyn_mem_free (socket_pdu );
142
+ return ;
143
+ }
95
144
96
- // KMP message from source port 10254
97
- if (src_addr -> identifier == 10254 ) {
98
- uint8_t * ptr = data ;
145
+ // Message from source port 10254 (KMP service) -> to IP relay on node or on authenticator
146
+ if (src_addr . identifier == 10254 ) {
147
+ uint8_t * ptr = socket_pdu ;
99
148
uint8_t * relay_ip_addr , * eui_64 ;
100
149
uint16_t relay_port ;
101
150
relay_ip_addr = ptr ;
@@ -104,65 +153,48 @@ static int8_t ws_eapol_auth_relay_socket_pdu_receive(const ns_address_t *src_add
104
153
ptr += 2 ;
105
154
eui_64 = ptr ;
106
155
ptr += 8 ;
107
- uint16_t data_len = len - 26 ;
108
- // KMP protocol to relay -> packet with IP relay address -> to IP relay of node
109
- if (memcmp (relay_ip_addr , ADDR_UNSPECIFIED , 16 ) != 0 ) {
110
- res = ws_eapol_relay_send_to_relay (eui_64 , relay_ip_addr , relay_port ,
111
- ptr , data_len );
112
- ns_dyn_mem_free (data );
113
-
114
- // KMP protocol to relay -> no IP relay address -> to MPX
115
- } else {
116
- res = ws_eapol_pdu_send_to_mpx (eui_64 , ptr , data_len , data );
117
- }
118
-
119
-
120
- // Other source port (either 10253 or node relay source port)
156
+ uint16_t data_len = cb_data -> d_len - 26 ;
157
+ ws_eapol_relay_lib_send_to_relay (eapol_auth_relay -> socket_id , eui_64 , relay_ip_addr , relay_port ,
158
+ ptr , data_len );
159
+ ns_dyn_mem_free (socket_pdu );
160
+ // Other source port (either 10253 or node relay source port) -> to KMP service
121
161
} else {
122
- uint8_t * ptr = data ;
123
- res = ws_eapol_auth_relay_send_to_kmp (ptr , src_addr -> address , src_addr -> identifier ,
124
- ptr + 8 , len - 8 );
125
- ns_dyn_mem_free (data );
126
- }
127
-
128
- return res ;
162
+ uint8_t * ptr = socket_pdu ;
163
+ ws_eapol_auth_relay_send_to_kmp (eapol_auth_relay , ptr , src_addr .address , src_addr .identifier ,
164
+ ptr + 8 , cb_data -> d_len - 8 );
165
+ ns_dyn_mem_free (socket_pdu );
166
+ }
129
167
}
130
168
131
- 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 )
169
+ static int8_t ws_eapol_auth_relay_send_to_kmp (const eapol_auth_relay_t * eapol_auth_relay , const uint8_t * eui_64 , const uint8_t * ip_addr , uint16_t port , const void * data , uint16_t data_len )
132
170
{
133
171
ns_address_t dest_addr ;
134
172
135
- if (ws_eapol_relay_kmp_ll_address_get ( & dest_addr ) < 0 ) {
173
+ if (ws_eapol_relay_lib_ll_address_get ( eapol_auth_relay -> interface_ptr , & dest_addr ) < 0 ) {
136
174
return -1 ;
137
175
}
138
176
139
- int8_t socket_id = ws_eapol_relay_socket_id_get ();
140
- if (socket_id >= 0 ) {
141
- uint8_t temp_array [26 ];
142
- ns_iovec_t msg_iov [2 ];
143
- ns_msghdr_t msghdr ;
144
- //Set messages name buffer
145
- msghdr .msg_name = & dest_addr ;
146
- msghdr .msg_namelen = sizeof (dest_addr );
147
- msghdr .msg_iov = & msg_iov [0 ];
148
- msghdr .msg_iovlen = 2 ;
149
- msghdr .msg_control = NULL ;
150
- msghdr .msg_controllen = 0 ;
151
- uint8_t * ptr = temp_array ;
152
- memcpy (ptr , ip_addr , 16 );
153
- ptr += 16 ;
154
- ptr = common_write_16_bit (port , ptr );
155
- memcpy (ptr , eui_64 , 8 );
156
- msg_iov [0 ].iov_base = temp_array ;
157
- msg_iov [0 ].iov_len = 26 ;
158
- msg_iov [1 ].iov_base = (void * )data ;
159
- msg_iov [1 ].iov_len = data_len ;
160
- socket_sendmsg (socket_id , & msghdr , NS_MSG_LEGACY0 );
161
- return 0 ;
162
-
163
- }
164
-
165
- return -1 ;
177
+ uint8_t temp_array [26 ];
178
+ ns_iovec_t msg_iov [2 ];
179
+ ns_msghdr_t msghdr ;
180
+ //Set messages name buffer
181
+ msghdr .msg_name = & dest_addr ;
182
+ msghdr .msg_namelen = sizeof (dest_addr );
183
+ msghdr .msg_iov = & msg_iov [0 ];
184
+ msghdr .msg_iovlen = 2 ;
185
+ msghdr .msg_control = NULL ;
186
+ msghdr .msg_controllen = 0 ;
187
+ uint8_t * ptr = temp_array ;
188
+ memcpy (ptr , ip_addr , 16 );
189
+ ptr += 16 ;
190
+ ptr = common_write_16_bit (port , ptr );
191
+ memcpy (ptr , eui_64 , 8 );
192
+ msg_iov [0 ].iov_base = temp_array ;
193
+ msg_iov [0 ].iov_len = 26 ;
194
+ msg_iov [1 ].iov_base = (void * )data ;
195
+ msg_iov [1 ].iov_len = data_len ;
196
+ socket_sendmsg (eapol_auth_relay -> socket_id , & msghdr , NS_MSG_LEGACY0 );
197
+ return 0 ;
166
198
}
167
199
168
200
#endif /* HAVE_PAE_AUTH */
0 commit comments