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 cmMemset ((U8 *)&f1Params.sockFd, -1, sizeof(CmInetFd));
107 cmMemset ((U8 *)&f1Params.lstnSockFd, -1, sizeof(CmInetFd));
108 fillDestNetAddr(&f1Params.destIpNetAddr, &sctpCfg.duIpAddr);
110 /* Set polling to FALSE */
111 pollingState = FALSE;
117 /*******************************************************************
119 * @brief Fills the address List of the source Ip Address
123 * Function : fillAddrLst
126 * Fills the address List of source Ip Address
128 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
129 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
130 * @return ROK - success
133 ******************************************************************/
135 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
138 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
139 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
144 /******************************************************************************
146 * @brief Fills the address List of the source Ip Address
150 * Function : fillDestNetAddr
153 * Fills the address List of destinatoion Ip Address
155 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
156 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
157 * @return ROK - success
160 *******************************************************************************/
161 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
163 /* Filling destination address */
164 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
165 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
169 /******************************************************************************
171 * @brief Eastablishes the Assoc Req for the received interface type
175 * Function : sctpStartReq
178 * Eastablishes the Assoc Req for the received interface type
180 * @params[in] DuSctpDestCb *paramPtr
181 * @return ROK - success
184 *******************************************************************************/
189 socket_type = CM_INET_STREAM;
190 fillAddrLst(&f1Params.localAddrLst, &sctpCfg.cuIpAddr);
192 if((ret = cmInetSocket(socket_type, &f1Params.lstnSockFd, IPPROTO_SCTP) != ROK))
194 DU_LOG("\nSCTP : Socket[%d] coudnt open for listening", f1Params.lstnSockFd.fd);
196 else if((ret = cmInetSctpBindx(&f1Params.lstnSockFd, &f1Params.localAddrLst, f1Params.srcPort)) != ROK)
198 DU_LOG("\nSCTP: Binding failed at CU");
200 else if((ret = sctpAccept(&f1Params.lstnSockFd, &f1Params.peerAddr, &f1Params.sockFd)) != ROK)
202 DU_LOG("\nSCTP: Unable to accept the connection at CU");
204 else if(sctpSockPoll() != ROK)
206 DU_LOG("\nSCTP: Polling failed to start at CU");
210 /*******************************************************************
212 * @brief Sets socket options as per requirement
216 * Function : sctpSetSockOpts
219 * Sets socket options as per requirement
222 * @return ROK - success
225 * ****************************************************************/
226 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
229 CmSctpEvent sctpEvent;
231 sctpEvent.dataIoEvent = TRUE;
232 sctpEvent.associationEvent = TRUE;
233 sctpEvent.addressEvent = TRUE;
234 sctpEvent.sendFailureEvent = TRUE;
235 sctpEvent.peerErrorEvent = TRUE;
236 sctpEvent.shutdownEvent = TRUE;
237 sctpEvent.partialDeliveryEvent = TRUE;
238 sctpEvent.adaptationLayerEvent = TRUE;
240 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent)) != ROK)
248 /*******************************************************************
250 * @brief Initiates connection with peer SCTP
254 * Function : sctpAccept
257 * Establishes SCTP connection with peer.
258 * Here, DU-SCTP will initate connection towards CU-SCTP
261 * @return ROK - success
264 * ****************************************************************/
265 S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
268 ret = cmInetListen(lstnSock_Fd, 1);
271 DU_LOG("\nSCTP : Listening on socket failed");
272 cmInetClose(lstnSock_Fd);
276 DU_LOG("\nSCTP : Connecting");
280 ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
287 DU_LOG("\nSCTP : Failed to accept connection");
293 sctpSetSockOpts(sock_Fd);
297 DU_LOG("\nSCTP : Connection established");
302 /*******************************************************************
304 * @brief Handles an SCTP notification message
308 * Function : sctpNtfyHdlr
311 * Handles an SCTP notification message
313 * @params[in] Notify message
315 * @return ROK - success
318 * ****************************************************************/
319 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
321 switch(ntfy->header.nType)
323 case CM_INET_SCTP_ASSOC_CHANGE :
324 DU_LOG("\nSCTP : Assoc change notification received");
325 switch(ntfy->u.assocChange.state)
327 case CM_INET_SCTP_COMM_UP:
328 DU_LOG("Event : COMMUNICATION UP");
331 case CM_INET_SCTP_COMM_LOST:
332 DU_LOG("Event : COMMUNICATION LOST");
335 case CM_INET_SCTP_RESTART:
336 DU_LOG("Event : SCTP RESTART");
339 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
340 DU_LOG("Event : SHUTDOWN COMPLETE");
343 case CM_INET_SCTP_CANT_STR_ASSOC:
344 DU_LOG("Event : CANT START ASSOC");
348 DU_LOG("\nInvalid event");
352 case CM_INET_SCTP_PEER_ADDR_CHANGE :
353 DU_LOG("\nSCTP : Peer Address Change notificarion received");
354 /* Need to add handler */
356 case CM_INET_SCTP_REMOTE_ERROR :
357 DU_LOG("\nSCTP : Remote Error notification received");
359 case CM_INET_SCTP_SEND_FAILED :
360 DU_LOG("\nSCTP : Send Failed notification received\n");
362 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
363 DU_LOG("\nSCTP : Shutdown Event notification received\n");
367 case CM_INET_SCTP_ADAPTATION_INDICATION :
368 DU_LOG("\nSCTP : Adaptation Indication received\n");
370 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
371 DU_LOG("\nSCTP : Partial Delivery Event received\n");
374 DU_LOG("\nSCTP : Invalid notification type\n");
380 }/* End of sctpNtfyHdlr */
382 /*******************************************************************
384 * @brief Receives message on the socket
388 * Function : sctpSockPoll
391 * Receives message on the socket
394 * @return ROK - success
397 * ****************************************************************/
405 CmInetAddr egtpFromAddr;
406 CmInetMemInfo memInfo;
407 sctpSockPollParams f1PollParams;
409 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
411 egtpFromAddr.port = egtpCb.dstCb.dstPort;
412 egtpFromAddr.address = egtpCb.dstCb.dstIp;
414 if(f1Params.sockFd.blocking)
423 timeoutPtr = &timeout;
425 memInfo.region = CU_APP_MEM_REG;
426 memInfo.pool = CU_POOL;
428 CM_INET_FD_ZERO(&f1PollParams.readFd);
432 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeoutPtr, &memInfo)) != ROK)
434 DU_LOG("\nSCTP : Failed to RecvMsg for F1 at CU\n");
437 /* Receiving EGTP data */
439 ret = cmInetRecvMsg(&(egtpCb.recvTptSrvr.sockFd), &egtpFromAddr, &memInfo, &egtpBuf, &egtpBufLen, CM_INET_NO_FLAG);
440 if(ret == ROK && egtpBuf != NULLP)
442 DU_LOG("\nEGTP : Received message \n");
443 SPrntMsg(egtpBuf, 0 ,0);
444 cuEgtpHdlRecvMsg(egtpBuf);
449 }/* End of sctpSockPoll() */
451 /*******************************************************************
453 * @brief checks for valid readFd and process the InetSctpRecvMsg
458 * Function : processPolling
461 * checks for valid readFd and process the InetSctpRecvMsg
464 * @params[in] Params required for polling
465 * @params[in] SockFd for file descriptor
466 * @params[in] timeoutPtr indicates the timeout value
467 * @params[in] MemInfo indicates memory region
469 * @return ROK - success
472 * ****************************************************************/
474 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo)
477 CM_INET_FD_SET(sockFd, &pollParams->readFd);
478 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFds);
479 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
481 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
482 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
483 if(connUp & (ret != ROK))
485 f1Params.bReadFdSet = RFAILED;
489 if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
491 ret = sctpNtfyHdlr(&pollParams->ntfy);
494 DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
497 else if(connUp & (pollParams->port == f1Params.destPort))
499 F1APMsgHdlr(pollParams->mBuf);
500 SPutMsg(pollParams->mBuf);
504 SPutMsg(pollParams->mBuf);
509 }/* End of sctpSockPoll() */
511 /*******************************************************************
513 * @brief Send message on SCTP socket
517 * Function : sctpSend
520 * Send message on SCTP socket
523 * @return ROK - success
526 * ****************************************************************/
527 S16 sctpSend(Buffer *mBuf)
530 MsgLen len; /* number of actually sent octets */
531 CmInetMemInfo memInfo;
533 memInfo.region = CU_APP_MEM_REG;
534 memInfo.pool = CU_POOL;
536 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
538 if(ret != ROK && ret != RWOULDBLOCK)
540 DU_LOG("\nSCTP : Send message failed");
545 } /* End of sctpSend */
547 /**********************************************************************
549 **********************************************************************/