Skip to content

Commit dbd1023

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 0f9ed37 commit dbd1023

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

app/src/sm_at_socket.c

Lines changed: 45 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,41 @@ 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+
#if defined(CONFIG_SM_PPP)
1116+
/* PPP is raw socket so new socket cannot be created on the same CID */
1117+
if (sm_ppp_is_running_on_cid(cid)) {
1118+
LOG_ERR("Socket creation not allowed on PPP PDN (%d) when PPP is not stopped", cid);
1119+
return false;
1120+
}
1121+
#endif
1122+
for (int i = 0; i < SM_MAX_SOCKET_COUNT; i++) {
1123+
if (socks[i].cid == cid) {
1124+
/* If the CID is used for RAW sockets*/
1125+
if (type == NRF_SOCK_RAW) {
1126+
LOG_ERR("Raw socket creation not allowed on PDN (%d) "
1127+
"which already has another socket", cid);
1128+
return false;
1129+
}
1130+
1131+
/* If the CID is used for a RAW socket */
1132+
if (socks[i].type == NRF_SOCK_RAW) {
1133+
LOG_ERR("Socket creation not allowed on PDN (%d) "
1134+
"which already has RAW socket", cid);
1135+
return false;
1136+
}
1137+
}
1138+
}
1139+
1140+
return true;
1141+
}
1142+
11071143
SM_AT_CMD_CUSTOM(xsocket, "AT#XSOCKET", handle_at_socket);
11081144
static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser *parser,
11091145
uint32_t param_count)
@@ -1145,6 +1181,10 @@ static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser *
11451181
goto error;
11461182
}
11471183
}
1184+
if (!cid_validity_raw_socket_check(sock->cid, sock->type)) {
1185+
err = -EFAULT;
1186+
goto error;
1187+
}
11481188
err = do_socket_open(sock);
11491189
if (err) {
11501190
LOG_ERR("do_socket_open() failed: %d", err);
@@ -1255,6 +1295,10 @@ static int handle_at_secure_socket(enum at_parser_cmd_type cmd_type,
12551295
goto error;
12561296
}
12571297
}
1298+
if (!cid_validity_raw_socket_check(sock->cid, sock->type)) {
1299+
err = -EINVAL;
1300+
goto error;
1301+
}
12581302
err = do_secure_socket_open(sock, peer_verify);
12591303
if (err) {
12601304
LOG_ERR("do_secure_socket_open() failed: %d", err);

app/src/sm_ppp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ 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+
return true;
388+
}
389+
return false;
390+
}
391+
384392
static int ppp_stop(enum ppp_reason reason)
385393
{
386394
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

doc/app/PPP_AT_commands.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ The set command allows you to start and stop PPP, and optionally define the PDN
3434
When a PPP start has been issued, the PPP connection is automatically activated and deactivated when the PDN connection requested for PPP is established and lost, respectively.
3535
This will continue until a PPP stop is issued by either the user by the ``AT#XPPP=0`` command or by the remote peer disconnecting the PPP using LCP termination.
3636

37+
.. note::
38+
39+
PPP cannot be started on a PDN connection that is already in use by another socket.
40+
The reason is that PPP is a raw socket, which intercepts all downlink data intended for other sockets on the same PDN, which disrupts normal socket operations.
41+
3742
Syntax
3843
~~~~~~
3944

doc/app/SOCKET_AT_commands.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Socket #XSOCKET
1717

1818
The ``#XSOCKET`` command allows you to open a socket and to check the socket handle.
1919

20+
.. note::
21+
22+
A socket cannot be created on a PDN connection that is already in use by a raw socket.
23+
A raw socket cannot be created on a PDN connection that is already in use by another socket.
24+
The reason is that a raw socket cannot keep its data separate from the data of another IP socket when both are operating on the same PDN.
25+
When a raw socket is active, it intercepts all downlink data intended for other sockets on the same PDN, which disrupts normal socket operations.
26+
2027
Set command
2128
-----------
2229

@@ -182,6 +189,12 @@ The ``#XSSOCKET`` command allows you to open a secure socket and to check the so
182189
.. note::
183190
TLS and DTLS servers are currently not supported.
184191

192+
.. note::
193+
194+
A secure socket cannot be created on a PDN connection that is already in use by a raw socket.
195+
The reason is that a raw socket cannot keep its data separate from the data of another IP socket when both are operating on the same PDN.
196+
When a raw socket is active, it intercepts all downlink data intended for other sockets on the same PDN, which disrupts normal socket operations.
197+
185198
Set command
186199
-----------
187200

0 commit comments

Comments
 (0)