Skip to content

Commit a2f45c6

Browse files
committed
app: Raw socket restrictions on PDN CID
RAW socket cannot share the same PDN with any other socket. PPP is in practice a RAW socket. Prevent socket creation if same PDN already has PPP or raw socket. Prevent PPP connection if same PDN already has another socket. Signed-off-by: Tommi Rantanen <[email protected]>
1 parent b487e76 commit a2f45c6

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

app/src/sm_at_socket.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "sm_at_host.h"
1818
#include "sm_at_socket.h"
1919
#include "sm_sockopt.h"
20+
#include "sm_ppp.h"
2021

2122
LOG_MODULE_REGISTER(sm_sock, CONFIG_SM_LOG_LEVEL);
2223

@@ -155,7 +156,7 @@ static int bind_to_pdn(struct sm_socket *sock)
155156
ret = nrf_setsockopt(sock->fd, NRF_SOL_SOCKET, NRF_SO_BINDTOPDN, &cid_int,
156157
sizeof(int));
157158
if (ret < 0) {
158-
LOG_ERR("nrf_setsockopt(%d) error: %d", NRF_SO_BINDTOPDN, -errno);
159+
LOG_ERR("nrf_setsockopt(NRF_SO_BINDTOPDN) error: %d", -errno);
159160
ret = -errno;
160161
}
161162
}
@@ -1104,6 +1105,37 @@ static int socket_datamode_callback(uint8_t op, const uint8_t *data, int len, ui
11041105
return ret;
11051106
}
11061107

1108+
/* Two RAW sockets cannot share the same CID. PPP is in practice a RAW socket.
1109+
* This function checks if a new socket can be created on the given CID.
1110+
* The CID cannot have a RAW sockets already, and if the new socket is RAW,
1111+
* the CID cannot have any other sockets already.
1112+
*/
1113+
static bool cid_validity_raw_socket_check(uint16_t cid, int type)
1114+
{
1115+
/* PPP is raw socket so new socket cannot be created on the same CID */
1116+
if (sm_ppp_is_running_on_cid(cid)) {
1117+
return false;
1118+
}
1119+
1120+
for (int i = 0; i < SM_MAX_SOCKET_COUNT; i++) {
1121+
if (socks[i].cid == cid) {
1122+
/* If the CID is used for RAW sockets*/
1123+
if (type == NRF_SOCK_RAW) {
1124+
LOG_ERR("Raw socket creation not allowed on CID which already has another socket");
1125+
return false;
1126+
}
1127+
1128+
/* If the CID is used for a RAW socket */
1129+
if (socks[i].type == NRF_SOCK_RAW) {
1130+
LOG_ERR("Socket creation not allowed on CID which already has RAW socket");
1131+
return false;
1132+
}
1133+
}
1134+
}
1135+
1136+
return true;
1137+
}
1138+
11071139
SM_AT_CMD_CUSTOM(xsocket, "AT#XSOCKET", handle_at_socket);
11081140
static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser *parser,
11091141
uint32_t param_count)
@@ -1145,6 +1177,10 @@ static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser *
11451177
goto error;
11461178
}
11471179
}
1180+
if (!cid_validity_raw_socket_check(sock->cid, sock->type)) {
1181+
err = -EINVAL;
1182+
goto error;
1183+
}
11481184
err = do_socket_open(sock);
11491185
if (err) {
11501186
LOG_ERR("do_socket_open() failed: %d", err);
@@ -1255,6 +1291,10 @@ static int handle_at_secure_socket(enum at_parser_cmd_type cmd_type,
12551291
goto error;
12561292
}
12571293
}
1294+
if (!cid_validity_raw_socket_check(sock->cid, sock->type)) {
1295+
err = -EINVAL;
1296+
goto error;
1297+
}
12581298
err = do_secure_socket_open(sock, peer_verify);
12591299
if (err) {
12601300
LOG_ERR("do_secure_socket_open() failed: %d", err);

app/src/sm_ppp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ bool sm_ppp_is_stopped(void)
381381
return (ppp_state == PPP_STATE_STOPPED);
382382
}
383383

384+
bool sm_ppp_is_running_on_cid(uint16_t cid)
385+
{
386+
if (!sm_ppp_is_stopped() && cid == ppp_pdn_cid) {
387+
LOG_ERR("Socket creation not allowed on PPP CID (%d) when PPP is not stopped (%d)", cid, ppp_state);
388+
return true;
389+
}
390+
return false;
391+
}
392+
384393
static int ppp_stop(enum ppp_reason reason)
385394
{
386395
if (ppp_state == PPP_STATE_STOPPED) {

app/src/sm_ppp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SM_PPP_
88

99
#include <stdbool.h>
10+
#include <zephyr/kernel.h>
1011

1112
/* Whether to forward CGEV notifications to the Serial Modem UART. */
1213
extern bool sm_fwd_cgev_notifs;
@@ -16,4 +17,6 @@ int sm_ppp_init(void);
1617

1718
bool sm_ppp_is_stopped(void);
1819

20+
bool sm_ppp_is_running_on_cid(uint16_t cid);
21+
1922
#endif

0 commit comments

Comments
 (0)