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 "cu_f1ap_msg_hdl.h"
23 #include "cu_stub_sctp.h"
24 #include "cu_stub_egtp.h"
26 CuSctpDestCb f1Params;
27 /**************************************************************************
28 * @brief Task Initiation callback function.
32 * Function : sctpActvInit
35 * This function is supplied as one of parameters during SCTP's
36 * task registration. SSI will invoke this function once, after
37 * it creates and attaches this TAPA Task to a system task.
39 * @param[in] Ent entity, the entity ID of this task.
40 * @param[in] Inst inst, the instance ID of this task.
41 * @param[in] Region region, the region ID registered for memory
43 * @param[in] Reason reason.
44 * @return ROK - success
46 ***************************************************************************/
49 DU_LOG("\n\nSCTP : Initializing");
53 sctpCfg = cuCfgParams.sctpParams;
58 /**************************************************************************
59 * @brief Task Activation callback function.
63 * Function : sctpActvTsk
66 * This function handles all SCTP messages received
67 * This API is registered with SSI during the
68 * Task Registration of DU APP.
70 * @param[in] Pst *pst, Post structure of the primitive.
71 * @param[in] Buffer *mBuf, Packed primitive parameters in the
73 * @return ROK - success
76 ***************************************************************************/
77 S16 sctpActvTsk(Pst *pst, Buffer *mBuf)
84 /**************************************************************************
85 * @brief Function to configure the Sctp Params during config Request
89 * Function : duSctpCfgReq
92 * This function configures SCTP Params during the config Request
94 * @return ROK - success
97 ***************************************************************************/
103 f1Params.destPort = sctpCfg.duPort;
104 f1Params.srcPort = sctpCfg.cuPort;
105 f1Params.bReadFdSet = ROK;
106 memset(&f1Params.sockFd, -1, sizeof(CmInetFd));
107 memset(&f1Params.lstnSockFd, -1, sizeof(CmInetFd));
108 fillDestNetAddr(&f1Params.destIpNetAddr, &sctpCfg.duIpAddr);
114 /*******************************************************************
116 * @brief Fills the address List of the source Ip Address
120 * Function : fillAddrLst
123 * Fills the address List of source Ip Address
125 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
126 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
127 * @return ROK - success
130 ******************************************************************/
132 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
135 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
136 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
141 /******************************************************************************
143 * @brief Fills the address List of the source Ip Address
147 * Function : fillDestNetAddr
150 * Fills the address List of destinatoion Ip Address
152 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
153 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
154 * @return ROK - success
157 *******************************************************************************/
158 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
160 /* Filling destination address */
161 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
162 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
166 /******************************************************************************
168 * @brief Eastablishes the Assoc Req for the received interface type
172 * Function : sctpStartReq
175 * Eastablishes the Assoc Req for the received interface type
177 * @params[in] DuSctpDestCb *paramPtr
178 * @return ROK - success
181 *******************************************************************************/
186 socket_type = CM_INET_STREAM;
187 fillAddrLst(&f1Params.localAddrLst, &sctpCfg.cuIpAddr);
189 if((ret = cmInetSocket(socket_type, &f1Params.lstnSockFd, IPPROTO_SCTP) != ROK))
191 DU_LOG("\nSCTP : Socket[%d] coudnt open for listening", f1Params.lstnSockFd.fd);
193 else if((ret = cmInetSctpBindx(&f1Params.lstnSockFd, &f1Params.localAddrLst, f1Params.srcPort)) != ROK)
195 DU_LOG("\nSCTP: Binding failed at CU");
197 else if((ret = sctpAccept(&f1Params.lstnSockFd, &f1Params.peerAddr, &f1Params.sockFd)) != ROK)
199 DU_LOG("\nSCTP: Unable to accept the connection at CU");
201 else if(sctpSockPoll() != ROK)
203 DU_LOG("\nSCTP: Polling failed to start at CU");
207 /*******************************************************************
209 * @brief Sets socket options as per requirement
213 * Function : sctpSetSockOpts
216 * Sets socket options as per requirement
219 * @return ROK - success
222 * ****************************************************************/
223 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
226 CmSctpEvent sctpEvent;
228 sctpEvent.dataIoEvent = TRUE;
229 sctpEvent.associationEvent = TRUE;
230 sctpEvent.addressEvent = TRUE;
231 sctpEvent.sendFailureEvent = TRUE;
232 sctpEvent.peerErrorEvent = TRUE;
233 sctpEvent.shutdownEvent = TRUE;
234 sctpEvent.partialDeliveryEvent = TRUE;
235 sctpEvent.adaptationLayerEvent = TRUE;
237 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent)) != ROK)
245 /*******************************************************************
247 * @brief Initiates connection with peer SCTP
251 * Function : sctpAccept
254 * Establishes SCTP connection with peer.
255 * Here, DU-SCTP will initate connection towards CU-SCTP
258 * @return ROK - success
261 * ****************************************************************/
262 S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
266 ret = cmInetListen(lstnSock_Fd, 1);
269 DU_LOG("\nSCTP : Listening on socket failed");
270 cmInetClose(lstnSock_Fd);
274 DU_LOG("\nSCTP : Connecting");
278 ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
285 DU_LOG("\nSCTP : Failed to accept connection");
291 sctpSetSockOpts(sock_Fd);
295 DU_LOG("\nSCTP : Connection established");
300 /*******************************************************************
302 * @brief Handles an SCTP notification message
306 * Function : sctpNtfyHdlr
309 * Handles an SCTP notification message
311 * @params[in] Notify message
313 * @return ROK - success
316 * ****************************************************************/
317 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
319 switch(ntfy->header.nType)
321 case CM_INET_SCTP_ASSOC_CHANGE :
322 DU_LOG("\nSCTP : Assoc change notification received");
323 switch(ntfy->u.assocChange.state)
325 case CM_INET_SCTP_COMM_UP:
326 DU_LOG("Event : COMMUNICATION UP");
329 case CM_INET_SCTP_COMM_LOST:
330 DU_LOG("Event : COMMUNICATION LOST");
333 case CM_INET_SCTP_RESTART:
334 DU_LOG("Event : SCTP RESTART");
337 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
338 DU_LOG("Event : SHUTDOWN COMPLETE");
341 case CM_INET_SCTP_CANT_STR_ASSOC:
342 DU_LOG("Event : CANT START ASSOC");
346 DU_LOG("\nInvalid event");
350 case CM_INET_SCTP_PEER_ADDR_CHANGE :
351 DU_LOG("\nSCTP : Peer Address Change notificarion received");
352 /* Need to add handler */
354 case CM_INET_SCTP_REMOTE_ERROR :
355 DU_LOG("\nSCTP : Remote Error notification received");
357 case CM_INET_SCTP_SEND_FAILED :
358 DU_LOG("\nSCTP : Send Failed notification received\n");
360 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
361 DU_LOG("\nSCTP : Shutdown Event notification received\n");
365 case CM_INET_SCTP_ADAPTATION_INDICATION :
366 DU_LOG("\nSCTP : Adaptation Indication received\n");
368 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
369 DU_LOG("\nSCTP : Partial Delivery Event received\n");
372 DU_LOG("\nSCTP : Invalid notification type\n");
378 }/* End of sctpNtfyHdlr */
380 /*******************************************************************
382 * @brief Receives message on the socket
386 * Function : sctpSockPoll
389 * Receives message on the socket
392 * @return ROK - success
395 * ****************************************************************/
403 CmInetAddr egtpFromAddr;
404 CmInetMemInfo memInfo;
405 sctpSockPollParams f1PollParams;
407 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
409 egtpFromAddr.port = egtpCb.dstCb.dstPort;
410 egtpFromAddr.address = egtpCb.dstCb.dstIp;
412 if(f1Params.sockFd.blocking)
421 timeoutPtr = &timeout;
423 memInfo.region = CU_APP_MEM_REG;
424 memInfo.pool = CU_POOL;
426 CM_INET_FD_ZERO(&f1PollParams.readFd);
430 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeoutPtr, &memInfo)) != ROK)
432 DU_LOG("\nSCTP : Failed to RecvMsg for F1 at CU\n");
435 /* Receiving EGTP data */
437 ret = cmInetRecvMsg(&(egtpCb.recvTptSrvr.sockFd), &egtpFromAddr, &memInfo, &egtpBuf, &egtpBufLen, CM_INET_NO_FLAG);
438 if(ret == ROK && egtpBuf != NULLP)
440 DU_LOG("\nEGTP : Received message \n");
441 SPrntMsg(egtpBuf, 0 ,0);
442 cuEgtpHdlRecvMsg(egtpBuf);
447 }/* End of sctpSockPoll() */
449 /*******************************************************************
451 * @brief checks for valid readFd and process the InetSctpRecvMsg
456 * Function : processPolling
459 * checks for valid readFd and process the InetSctpRecvMsg
462 * @params[in] Params required for polling
463 * @params[in] SockFd for file descriptor
464 * @params[in] timeoutPtr indicates the timeout value
465 * @params[in] MemInfo indicates memory region
467 * @return ROK - success
470 * ****************************************************************/
472 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo)
475 CM_INET_FD_SET(sockFd, &pollParams->readFd);
476 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
477 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
479 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
480 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
481 if(connUp & (ret != ROK))
483 f1Params.bReadFdSet = RFAILED;
487 if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
489 ret = sctpNtfyHdlr(&pollParams->ntfy);
492 DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
495 else if(connUp & (pollParams->port == f1Params.destPort))
497 F1APMsgHdlr(pollParams->mBuf);
498 SPutMsg(pollParams->mBuf);
502 SPutMsg(pollParams->mBuf);
507 }/* End of sctpSockPoll() */
509 /*******************************************************************
511 * @brief Send message on SCTP socket
515 * Function : sctpSend
518 * Send message on SCTP socket
521 * @return ROK - success
524 * ****************************************************************/
525 S16 sctpSend(Buffer *mBuf)
528 MsgLen len; /* number of actually sent octets */
529 CmInetMemInfo memInfo;
531 memInfo.region = CU_APP_MEM_REG;
532 memInfo.pool = CU_POOL;
534 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
536 if(ret != ROK && ret != RWOULDBLOCK)
538 DU_LOG("\nSCTP : Send message failed");
543 } /* End of sctpSend */
545 /**********************************************************************
547 **********************************************************************/