|
17 | 17 | #include "sm_at_host.h" |
18 | 18 | #include "sm_at_socket.h" |
19 | 19 | #include "sm_sockopt.h" |
| 20 | +#include "sm_ppp.h" |
20 | 21 |
|
21 | 22 | LOG_MODULE_REGISTER(sm_sock, CONFIG_SM_LOG_LEVEL); |
22 | 23 |
|
@@ -155,7 +156,7 @@ static int bind_to_pdn(struct sm_socket *sock) |
155 | 156 | ret = nrf_setsockopt(sock->fd, NRF_SOL_SOCKET, NRF_SO_BINDTOPDN, &cid_int, |
156 | 157 | sizeof(int)); |
157 | 158 | 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); |
159 | 160 | ret = -errno; |
160 | 161 | } |
161 | 162 | } |
@@ -1104,6 +1105,37 @@ static int socket_datamode_callback(uint8_t op, const uint8_t *data, int len, ui |
1104 | 1105 | return ret; |
1105 | 1106 | } |
1106 | 1107 |
|
| 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 | + |
1107 | 1139 | SM_AT_CMD_CUSTOM(xsocket, "AT#XSOCKET", handle_at_socket); |
1108 | 1140 | static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser *parser, |
1109 | 1141 | uint32_t param_count) |
@@ -1145,6 +1177,10 @@ static int handle_at_socket(enum at_parser_cmd_type cmd_type, struct at_parser * |
1145 | 1177 | goto error; |
1146 | 1178 | } |
1147 | 1179 | } |
| 1180 | + if (!cid_validity_raw_socket_check(sock->cid, sock->type)) { |
| 1181 | + err = -EINVAL; |
| 1182 | + goto error; |
| 1183 | + } |
1148 | 1184 | err = do_socket_open(sock); |
1149 | 1185 | if (err) { |
1150 | 1186 | 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, |
1255 | 1291 | goto error; |
1256 | 1292 | } |
1257 | 1293 | } |
| 1294 | + if (!cid_validity_raw_socket_check(sock->cid, sock->type)) { |
| 1295 | + err = -EINVAL; |
| 1296 | + goto error; |
| 1297 | + } |
1258 | 1298 | err = do_secure_socket_open(sock, peer_verify); |
1259 | 1299 | if (err) { |
1260 | 1300 | LOG_ERR("do_secure_socket_open() failed: %d", err); |
|
0 commit comments