-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathppp_ctrl.c
508 lines (408 loc) · 12.9 KB
/
ppp_ctrl.c
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
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <stdio.h>
#include <assert.h>
#include <modem/lte_lc.h>
#include <zephyr/net/ppp.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_event.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/posix/unistd.h>
#include <zephyr/posix/netdb.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/posix/poll.h>
#include <zephyr/posix/sys/socket.h>
#include <zephyr/shell/shell.h>
#include "link_api.h"
#include "mosh_print.h"
#include "ppp_settings.h"
#include "ppp_ctrl.h"
extern struct k_work_q mosh_common_work_q;
/* ppp globals: */
struct net_if *ppp_iface_global;
K_SEM_DEFINE(ppp_sockets_sem, 0, 2);
/* Socket to send and recv data to/from modem */
int ppp_modem_data_socket_fd = PPP_MODEM_DATA_RAW_SCKT_FD_NONE;
/* Socket to send and recv data to/from Zephyr PPP link */
int ppp_data_socket_fd = PPP_MODEM_DATA_RAW_SCKT_FD_NONE;
/* LTE link MTU */
uint16_t used_mtu_mru;
/* Work for auto starting/stopping ppp according to default PDN
* activation status
*/
struct ppp_ctrl_pdn_status_worker_data {
struct k_work work;
bool default_pdn_active;
} ppp_ctrl_pdn_status_worker_data;
static const struct device *ppp_uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_ppp_uart));
/* Work for a re-starting PPP */
static struct k_work ppp_ctrl_restart_work;
static bool ppp_up;
static volatile bool ppp_restarting;
static volatile bool ppp_prot_running;
/******************************************************************************/
static void ppp_ctrl_stop_net_if(void)
{
struct ppp_context *ctx;
int idx = 0; /* Note: PPP iface index assumed to be 0 */
ctx = net_ppp_context_get(idx);
if (!ctx && !ctx->iface) {
return;
}
net_if_down(ctx->iface);
}
static int ppp_ctrl_start_net_if(void)
{
struct ppp_context *ctx;
struct net_if *iface;
struct pdp_context_info *pdp_context_info;
int idx = 0; /* Note: PPP iface index assumed to be 0 */
int ret = 0;
ctx = net_ppp_context_get(idx);
if (!ctx) {
mosh_error("PPP context not found.\n");
goto return_error;
}
/* Note: no multicontext support, only at default PDN context */
pdp_context_info = link_api_get_pdp_context_info_by_pdn_cid(0);
if (pdp_context_info == NULL) {
mosh_error("Default PDN context not active.\n");
goto return_error;
}
iface = ctx->iface;
ppp_iface_global = iface;
/* Set PPP MTU according to LTE link IPv4 MTU: */
if (pdp_context_info->ipv4_mtu) {
if (pdp_context_info->ipv4_mtu <= PPP_MODEM_DATA_RCV_SND_BUFF_SIZE) {
used_mtu_mru = pdp_context_info->ipv4_mtu;
} else {
mosh_warn("LTE link MTU (%d) cannot be set as PPP MTU. Setting to the max: %d\n",
pdp_context_info->ipv4_mtu,
PPP_MODEM_DATA_RCV_SND_BUFF_SIZE);
used_mtu_mru = PPP_MODEM_DATA_RCV_SND_BUFF_SIZE;
}
net_if_set_mtu(iface, used_mtu_mru);
} /* else: link MTU not known: default CONFIG_NET_PPP_MTU_MRU is used
* as set by PPP Zephyr driver during initialization.
*/
memcpy(&(ctx->ipcp.my_options.address), &(pdp_context_info->ip_addr4),
sizeof(ctx->ipcp.my_options.address));
memcpy(&ctx->ipcp.my_options.dns1_address,
&pdp_context_info->dns_addr4_primary,
sizeof(ctx->ipcp.my_options.dns1_address));
memcpy(&ctx->ipcp.my_options.dns2_address,
&pdp_context_info->dns_addr4_secondary,
sizeof(ctx->ipcp.my_options.dns2_address));
free(pdp_context_info);
net_if_flag_set(iface, NET_IF_POINTOPOINT);
ret = net_if_up(iface);
if (ret) {
mosh_warn("Cannot bring PPP up, err: %d", ret);
goto return_error;
}
return 0;
return_error:
return -1;
}
/******************************************************************************/
static void ppp_ctrl_link_default_pdn_status_handler(struct k_work *work_item)
{
struct ppp_ctrl_pdn_status_worker_data *data_ptr =
CONTAINER_OF(work_item, struct ppp_ctrl_pdn_status_worker_data, work);
if (data_ptr->default_pdn_active == true) {
mosh_print("Default PDN is active: starting PPP automatically.");
ppp_ctrl_start();
} else {
mosh_print("Default PDN is not active: stopping PPP automatically.");
ppp_ctrl_stop();
}
}
/******************************************************************************/
static void ppp_ctrl_restart_worker(struct k_work *work_item)
{
ARG_UNUSED(work_item);
ppp_restarting = true;
ppp_ctrl_stop();
ppp_ctrl_start();
ppp_restarting = false;
mosh_print("PPP: restarting is DONE");
}
/******************************************************************************/
static struct net_mgmt_event_callback ppp_ctrl_net_if_mgmt_event_ppp_cb;
static void ppp_ctrl_net_if_mgmt_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event,
struct net_if *iface)
{
if (net_if_l2(iface) != &NET_L2_GET_NAME(PPP)) {
return;
}
if (mgmt_event == NET_EVENT_IF_UP) {
mosh_print("PPP net if up");
return;
}
if (mgmt_event == NET_EVENT_IF_DOWN) {
mosh_print("PPP net if down");
return;
}
}
/******************************************************************************/
static struct net_mgmt_event_callback ppp_ctrl_net_mgmt_event_ppp_cb;
static void ppp_ctrl_net_mgmt_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event,
struct net_if *iface)
{
switch (mgmt_event) {
case NET_EVENT_PPP_PHASE_RUNNING: {
ppp_prot_running = true;
mosh_print("PPP in running phase");
break;
}
case NET_EVENT_PPP_PHASE_DEAD: {
mosh_print("PPP in dead phase");
if (ppp_prot_running) {
/* To be able to reconnect the dial up from Windows UI,
* we need to bring PPP net_if and sockets down/up:
*/
if (!ppp_restarting && ppp_up) {
mosh_print("PPP: restarting...");
k_work_submit_to_queue(&mosh_common_work_q, &ppp_ctrl_restart_work);
}
ppp_prot_running = false;
}
break;
}
default:
break;
}
}
static struct net_mgmt_event_callback ipv4_level_net_mgmt_event_cb;
static void
ppp_ctrl_net_mgmt_event_ipv4_levelhandler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event,
struct net_if *iface)
{
if (iface != ppp_iface_global) {
return;
}
if (mgmt_event == NET_EVENT_IPV4_ADDR_ADD) {
mosh_print("Dial up (IPv4) connection up");
} else if (mgmt_event == NET_EVENT_IPV4_ADDR_DEL) {
mosh_print("Dial up (IPv4) connection down");
}
}
static struct net_mgmt_event_callback ipv6_level_net_mgmt_event_cb;
static void
ppp_ctrl_net_mgmt_event_ipv6_levelhandler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event,
struct net_if *iface)
{
if (iface != ppp_iface_global) {
return;
}
if (mgmt_event == NET_EVENT_IPV6_ADDR_ADD) {
mosh_print("Dial up (IPv6) connection up");
} else if (mgmt_event == NET_EVENT_IPV6_ADDR_DEL) {
mosh_print("Dial up (IPv6) connection down");
}
}
static void ppp_ctrl_net_mgmt_events_subscribe(void)
{
net_mgmt_init_event_callback(&ppp_ctrl_net_if_mgmt_event_ppp_cb,
ppp_ctrl_net_if_mgmt_event_handler,
(NET_EVENT_IF_UP |
NET_EVENT_IF_DOWN));
net_mgmt_init_event_callback(&ppp_ctrl_net_mgmt_event_ppp_cb,
ppp_ctrl_net_mgmt_event_handler,
(NET_EVENT_IF_UP |
NET_EVENT_IF_DOWN |
NET_EVENT_PPP_PHASE_RUNNING |
NET_EVENT_PPP_PHASE_DEAD));
net_mgmt_init_event_callback(&ipv4_level_net_mgmt_event_cb,
ppp_ctrl_net_mgmt_event_ipv4_levelhandler,
(NET_EVENT_IPV4_ADDR_ADD |
NET_EVENT_IPV4_ADDR_DEL));
net_mgmt_init_event_callback(&ipv6_level_net_mgmt_event_cb,
ppp_ctrl_net_mgmt_event_ipv6_levelhandler,
(NET_EVENT_IPV6_ADDR_ADD |
NET_EVENT_IPV6_ADDR_DEL));
net_mgmt_add_event_callback(&ppp_ctrl_net_if_mgmt_event_ppp_cb);
net_mgmt_add_event_callback(&ppp_ctrl_net_mgmt_event_ppp_cb);
net_mgmt_add_event_callback(&ipv4_level_net_mgmt_event_cb);
net_mgmt_add_event_callback(&ipv6_level_net_mgmt_event_cb);
}
/******************************************************************************/
void ppp_ctrl_init(void)
{
ppp_up = false;
ppp_restarting = false;
ppp_prot_running = true;
used_mtu_mru = CONFIG_NET_PPP_MTU_MRU;
ppp_ctrl_net_mgmt_events_subscribe();
k_work_init(&ppp_ctrl_pdn_status_worker_data.work,
ppp_ctrl_link_default_pdn_status_handler);
/* A work for doing the restart for ppp: */
k_work_init(&ppp_ctrl_restart_work, ppp_ctrl_restart_worker);
if (!device_is_ready(ppp_uart_dev)) {
mosh_warn("PPP UART device not ready");
}
ppp_settings_init();
}
void ppp_ctrl_default_pdn_active(bool default_pdn_active)
{
ppp_ctrl_pdn_status_worker_data.default_pdn_active = default_pdn_active;
k_work_submit_to_queue(&mosh_common_work_q, &ppp_ctrl_pdn_status_worker_data.work);
}
/******************************************************************************/
static int ppp_ctrl_modem_sckt_create(void)
{
if (ppp_modem_data_socket_fd != PPP_MODEM_DATA_RAW_SCKT_FD_NONE) {
mosh_warn("PPP modem sckt already up - nothing to do");
return 0;
}
ppp_modem_data_socket_fd = socket(AF_PACKET, SOCK_RAW, 0);
if (ppp_modem_data_socket_fd < 0) {
mosh_error("modem data socket creation failed: (%d)\n", -errno);
goto return_error;
} else {
mosh_print("modem data socket %d created for modem data", ppp_modem_data_socket_fd);
}
#ifdef SO_SNDTIMEO
struct timeval tv;
/* blocking socket and we do not want to block for long: 1 sec timeout
* for both sending and receiving:
*/
tv.tv_sec = 1;
tv.tv_usec = 0;
if (setsockopt(ppp_modem_data_socket_fd, SOL_SOCKET, SO_SNDTIMEO,
(struct timeval *)&tv, sizeof(struct timeval)) < 0) {
mosh_warn("Unable to set socket SO_SNDTIMEO - continue");
}
#ifdef SO_RCVTIMEO
/* We want to set SO_RCVTIMEO for workarounding problems that are seen
* if closing sckt from another thread while in nrf_recv().
*/
if (setsockopt(ppp_modem_data_socket_fd, SOL_SOCKET, SO_RCVTIMEO,
(struct timeval *)&tv, sizeof(struct timeval)) < 0) {
mosh_warn("Unable to set socket SO_RCVTIMEO - continue");
}
#endif
#endif
return 0;
return_error:
return -1;
}
static int ppp_ctrl_zephyr_sckt_create(void)
{
int ret;
if (ppp_data_socket_fd != PPP_MODEM_DATA_RAW_SCKT_FD_NONE) {
mosh_warn("PPP Zephyr data socket already up - nothing to do");
return 0;
}
/* Create raw Zephyr socket for passing data to/from ppp link: */
ppp_data_socket_fd = socket(AF_PACKET, SOCK_RAW | SOCK_NATIVE,
htons(IPPROTO_RAW));
if (ppp_data_socket_fd < 0) {
mosh_error("PPP Zephyr data socket creation failed: (%d)\n", -errno);
goto return_error;
} else {
mosh_print("PPP Zephyr data socket %d created", ppp_data_socket_fd);
}
/* Bind to PPP net if: */
struct sockaddr_ll dst = { 0 };
dst.sll_ifindex = net_if_get_by_iface(ppp_iface_global);
dst.sll_family = AF_PACKET;
ret = bind(ppp_data_socket_fd, (const struct sockaddr *)&dst,
sizeof(struct sockaddr_ll));
if (ret < 0) {
mosh_error("Failed to bind PPP data socket : %d", errno);
(void)close(ppp_data_socket_fd);
goto return_error;
}
return 0;
return_error:
return -1;
}
/******************************************************************************/
static int ppp_ctrl_sckts_create(void)
{
int err;
err = ppp_ctrl_zephyr_sckt_create();
if (err) {
mosh_error("Cannot create zephyr sckt for ppp: %d", err);
return err;
}
err = ppp_ctrl_modem_sckt_create();
if (err) {
mosh_error("Cannot create modem sckt for ppp: %d", err);
return err;
}
/* Give a semaphore for both snd and rcv */
k_sem_give(&ppp_sockets_sem);
k_sem_give(&ppp_sockets_sem);
return 0;
}
static void ppp_ctrl_close_sckts(void)
{
int err;
int tmp_sckt_id;
k_sem_reset(&ppp_sockets_sem);
if (ppp_data_socket_fd != PPP_MODEM_DATA_RAW_SCKT_FD_NONE) {
tmp_sckt_id = ppp_data_socket_fd;
/* Change global sckt variables already before actual close to avoid going
* to poll() etc in UL/DL threads when closing.
*/
ppp_data_socket_fd = PPP_MODEM_DATA_RAW_SCKT_FD_NONE;
mosh_print("Closing PPP zephyr sckt");
err = close(tmp_sckt_id);
if (err) {
mosh_print("Closing of PPP zephyr sckt failed, errno %d", -errno);
}
}
/* Give some time for closing zephyr socket. */
k_sleep(K_SECONDS(2));
if (ppp_modem_data_socket_fd != PPP_MODEM_DATA_RAW_SCKT_FD_NONE) {
mosh_print("Closing PPP modem sckt");
tmp_sckt_id = ppp_modem_data_socket_fd;
ppp_modem_data_socket_fd = PPP_MODEM_DATA_RAW_SCKT_FD_NONE;
err = close(tmp_sckt_id);
if (err) {
mosh_print("Closing of PPP modem sckt failed, errno %d", -errno);
}
}
}
/******************************************************************************/
int ppp_ctrl_start(void)
{
if (ppp_up) {
mosh_warn("PPP already up.\n");
return 0;
}
if (ppp_ctrl_start_net_if()) {
goto clear;
}
if (ppp_ctrl_sckts_create()) {
goto clear;
}
ppp_up = true;
mosh_print("PPP: started");
return 0;
clear:
ppp_up = false;
mosh_error("PPP cannot be started");
ppp_ctrl_stop_net_if();
ppp_ctrl_close_sckts();
return -1;
}
/******************************************************************************/
void ppp_ctrl_stop(void)
{
ppp_up = false;
ppp_ctrl_stop_net_if();
ppp_ctrl_close_sckts();
mosh_print("PPP: stopped");
}