1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains all SCTP related functionality */
22 #include "ric_e2ap_msg_hdl.h"
23 #include "ric_stub_sctp.h"
25 CuSctpDestCb ricParams;
28 /**************************************************************************
29 * @brief Function to configure the Sctp Params during config Request
33 * Function : sctpCfgReq
36 * This function configures SCTP Params during the config Request
38 * @return ROK - success
41 ***************************************************************************/
46 sctpCfg = ricCfgParams.sctpParams;
49 ricParams.destPort = sctpCfg.duPort;
50 ricParams.srcPort = sctpCfg.ricPort;
51 ricParams.bReadFdSet = ROK;
52 memset(&ricParams.sockFd, -1, sizeof(CmInetFd));
53 memset(&ricParams.lstnSockFd, -1, sizeof(CmInetFd));
54 fillDestNetAddr(&ricParams.destIpNetAddr, &sctpCfg.duIpAddr);
59 /*******************************************************************
61 * @brief Fills the address List of the source Ip Address
65 * Function : fillAddrLst
68 * Fills the address List of source Ip Address
70 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
71 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
72 * @return ROK - success
75 ******************************************************************/
77 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
80 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
81 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
86 /******************************************************************************
88 * @brief Fills the address List of the source Ip Address
92 * Function : fillDestNetAddr
95 * Fills the address List of destinatoion Ip Address
97 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
98 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
99 * @return ROK - success
102 *******************************************************************************/
103 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
105 /* Filling destination address */
106 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
107 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
111 /******************************************************************************
113 * @brief Eastablishes the Assoc Req for the received interface type
117 * Function : sctpStartReq
120 * Eastablishes the Assoc Req for the received interface type
122 * @params[in] DuSctpDestCb *paramPtr
123 * @return ROK - success
126 *******************************************************************************/
131 socket_type = CM_INET_STREAM;
132 fillAddrLst(&ricParams.localAddrLst, &sctpCfg.ricIpAddr);
134 if((ret = cmInetSocket(socket_type, &ricParams.lstnSockFd, IPPROTO_SCTP) != ROK))
136 DU_LOG("\nSCTP : Socket[%d] coudnt open for listening", ricParams.lstnSockFd.fd);
138 else if((ret = cmInetSctpBindx(&ricParams.lstnSockFd, &ricParams.localAddrLst, ricParams.srcPort)) != ROK)
140 DU_LOG("\nSCTP: Binding failed at RIC");
142 else if((ret = sctpAccept(&ricParams.lstnSockFd, &ricParams.peerAddr, &ricParams.sockFd)) != ROK)
144 DU_LOG("\nSCTP: Unable to accept the connection at RIC");
146 else if(sctpSockPoll() != ROK)
148 DU_LOG("\nSCTP: Polling failed to start at RIC");
152 /*******************************************************************
154 * @brief Sets socket options as per requirement
158 * Function : sctpSetSockOpts
161 * Sets socket options as per requirement
164 * @return ROK - success
167 * ****************************************************************/
168 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
171 CmSctpEvent sctpEvent;
173 sctpEvent.dataIoEvent = TRUE;
174 sctpEvent.associationEvent = TRUE;
175 sctpEvent.addressEvent = TRUE;
176 sctpEvent.sendFailureEvent = TRUE;
177 sctpEvent.peerErrorEvent = TRUE;
178 sctpEvent.shutdownEvent = TRUE;
179 sctpEvent.partialDeliveryEvent = TRUE;
180 sctpEvent.adaptationLayerEvent = TRUE;
182 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent)) != ROK)
190 /*******************************************************************
192 * @brief Initiates connection with peer SCTP
196 * Function : sctpAccept
199 * Establishes SCTP connection with peer.
200 * Here, DU-SCTP will initate connection towards RIC-SCTP
203 * @return ROK - success
206 * ****************************************************************/
207 S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
211 ret = cmInetListen(lstnSock_Fd, 1);
214 DU_LOG("\nSCTP : Listening on socket failed");
215 cmInetClose(lstnSock_Fd);
219 DU_LOG("\nSCTP : Connecting");
223 ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
230 DU_LOG("\nSCTP : Failed to accept connection");
236 sctpSetSockOpts(sock_Fd);
240 DU_LOG("\nSCTP : Connection established");
245 /*******************************************************************
247 * @brief Handles an SCTP notification message
251 * Function : sctpNtfyHdlr
254 * Handles an SCTP notification message
256 * @params[in] Notify message
258 * @return ROK - success
261 * ****************************************************************/
262 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
264 switch(ntfy->header.nType)
266 case CM_INET_SCTP_ASSOC_CHANGE :
267 DU_LOG("\nSCTP : Assoc change notification received");
268 switch(ntfy->u.assocChange.state)
270 case CM_INET_SCTP_COMM_UP:
271 DU_LOG("Event : COMMUNICATION UP");
274 case CM_INET_SCTP_COMM_LOST:
275 DU_LOG("Event : COMMUNICATION LOST");
278 case CM_INET_SCTP_RESTART:
279 DU_LOG("Event : SCTP RESTART");
282 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
283 DU_LOG("Event : SHUTDOWN COMPLETE");
286 case CM_INET_SCTP_CANT_STR_ASSOC:
287 DU_LOG("Event : CANT START ASSOC");
291 DU_LOG("\nInvalid event");
295 case CM_INET_SCTP_PEER_ADDR_CHANGE :
296 DU_LOG("\nSCTP : Peer Address Change notificarion received");
297 /* Need to add handler */
299 case CM_INET_SCTP_REMOTE_ERROR :
300 DU_LOG("\nSCTP : Remote Error notification received");
302 case CM_INET_SCTP_SEND_FAILED :
303 DU_LOG("\nSCTP : Send Failed notification received\n");
305 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
306 DU_LOG("\nSCTP : Shutdown Event notification received\n");
310 case CM_INET_SCTP_ADAPTATION_INDICATION :
311 DU_LOG("\nSCTP : Adaptation Indication received\n");
313 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
314 DU_LOG("\nSCTP : Partial Delivery Event received\n");
317 DU_LOG("\nSCTP : Invalid notification type\n");
323 }/* End of sctpNtfyHdlr */
325 /*******************************************************************
327 * @brief Receives message on the socket
331 * Function : sctpSockPoll
334 * Receives message on the socket
337 * @return ROK - success
340 * ****************************************************************/
346 CmInetMemInfo memInfo;
347 sctpSockPollParams e2PollParams;
349 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
351 if(ricParams.sockFd.blocking)
360 timeoutPtr = &timeout;
363 memInfo.region = RIC_APP_MEM_REG;
364 memInfo.pool = RIC_POOL;
366 CM_INET_FD_ZERO(&e2PollParams.readFd);
368 DU_LOG("\nSCTP : Polling started at RIC\n");
371 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeoutPtr, &memInfo)) != ROK)
373 DU_LOG("\nSCTP : Failed to RecvMsg for E2 at RIC \n");
377 }/* End of sctpSockPoll() */
379 /*******************************************************************
381 * @brief checks for valid readFd and process the InetSctpRecvMsg
386 * Function : processPolling
389 * checks for valid readFd and process the InetSctpRecvMsg
392 * @params[in] Params required for polling
393 * @params[in] SockFd for file descriptor
394 * @params[in] timeoutPtr indicates the timeout value
395 * @params[in] MemInfo indicates memory region
397 * @return ROK - success
400 * ****************************************************************/
403 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo)
406 CM_INET_FD_SET(sockFd, &pollParams->readFd);
407 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
408 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
410 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
411 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
412 if(connUp & (ret != ROK))
414 ricParams.bReadFdSet = RFAILED;
418 if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
420 ret = sctpNtfyHdlr(&pollParams->ntfy);
423 DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
426 else if(connUp & (pollParams->port == ricParams.destPort))
428 E2APMsgHdlr(pollParams->mBuf);
429 SPutMsg(pollParams->mBuf);
433 SPutMsg(pollParams->mBuf);
438 }/* End of sctpSockPoll() */
440 /*******************************************************************
442 * @brief Send message on SCTP socket
446 * Function : sctpSend
449 * Send message on SCTP socket
452 * @return ROK - success
455 * ****************************************************************/
456 S16 sctpSend(Buffer *mBuf)
459 MsgLen len; /* number of actually sent octets */
460 CmInetMemInfo memInfo;
462 memInfo.region = RIC_APP_MEM_REG;
463 memInfo.pool = RIC_POOL;
465 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
467 if(ret != ROK && ret != RWOULDBLOCK)
469 DU_LOG("\nSCTP : Send message failed");
474 } /* End of sctpSend */
475 /**********************************************************************
477 **********************************************************************/