MAC Clean-up [Issue-ID: ODUHIGH-212]
[o-du/l2.git] / src / cu_stub / cu_stub_egtp.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 *******************************************************************************/
18  
19 /* This file contains all EGTP related functionality */
20
21 #include "common_def.h"
22 #include "cu_stub_egtp.h"
23 #include "du_log.h"
24
25 /* Global variable declaration */
26 EgtpGlobalCb egtpCb;
27
28 /**************************************************************************
29  * @brief Task Initiation callback function. 
30  *
31  * @details
32  *
33  *     Function : egtpActvInit 
34  *    
35  *     Functionality:
36  *             This function is supplied as one of parameters during EGTP's 
37  *             task registration. SSI will invoke this function once, after
38  *             it creates and attaches this TAPA Task to a system task.
39  *     
40  * @param[in]  Ent entity, the entity ID of this task.     
41  * @param[in]  Inst inst, the instance ID of this task.
42  * @param[in]  Region region, the region ID registered for memory 
43  *              usage of this task.
44  * @param[in]  Reason reason.
45  * @return ROK     - success
46  *         RFAILED - failure
47  ***************************************************************************/
48 S16 egtpActvInit()
49 {
50   DU_LOG("\n\nEGTP : Initializing");
51   cmMemset ((U8 *)&egtpCb, 0, sizeof(EgtpGlobalCb));
52   protType = CM_INET_PROTO_UDP;
53   RETVALUE(ROK);
54 }
55
56
57 /**************************************************************************
58  * @brief Task Activation callback function. 
59  *
60  * @details
61  *
62  *      Function : egtpActvTsk 
63  * 
64  *      Functionality:
65  *           This function handles all EGTP messages received
66  *           This API is registered with SSI during the 
67  *           Task Registration of DU APP.
68  *     
69  * @param[in]  Pst     *pst, Post structure of the primitive.     
70  * @param[in]  Buffer *mBuf, Packed primitive parameters in the
71  *  buffer.
72  * @return ROK     - success
73  *         RFAILED - failure
74  *
75  ***************************************************************************/
76 S16 egtpInitReq()
77 {
78    S16 ret = ROK;
79    EgtpTnlEvt tnlEvt;
80
81    ret = cuEgtpCfgReq();
82    if(ret != ROK)
83    {
84       DU_LOG("\nEGTP : Configuration failed");
85       RETVALUE(ret);
86    }
87
88    ret = cuEgtpSrvOpenReq();
89    if(ret != ROK)
90    {
91        DU_LOG("\nEGTP : Transport server open request failed");
92        RETVALUE(ret);
93    }
94
95    tnlEvt.action = EGTP_TNL_MGMT_ADD;
96    tnlEvt.lclTeid = 10;
97    tnlEvt.remTeid = 1;
98    ret = cuEgtpTnlMgmtReq(tnlEvt);
99    if(ret != ROK)
100    {
101       DU_LOG("\n EGTP : Tunnel management request failed");
102       RETVALUE(RFAILED);
103    }
104
105    RETVALUE(ret);
106
107 } /* egtpInitReq */
108
109 /**************************************************************************
110  * @brief EGTP server configuration 
111  *
112  * @details
113  *
114  *      Function : egtpCfgReq
115  * 
116  *      Functionality:
117  *           This function handles EGTP configuration request.
118  *     
119  * @return ROK     - success
120  *         RFAILED - failure
121  *
122  * ***********************************************************************/
123 S16 cuEgtpCfgReq()
124 {
125    U8 ret;
126
127    cmMemcpy((U8 *)&egtpCb.egtpCfg, (U8 *)&cuCfgParams.egtpParams, (PTR)sizeof(EgtpParams));
128
129    egtpCb.recvTptSrvr.addr.address = CM_INET_NTOH_U32(egtpCb.egtpCfg.localIp.ipV4Addr);
130    egtpCb.recvTptSrvr.addr.port = EGTP_DFLT_PORT;
131
132    egtpCb.dstCb.dstIp = CM_INET_NTOH_U32(egtpCb.egtpCfg.destIp.ipV4Addr);
133    egtpCb.dstCb.dstPort = egtpCb.egtpCfg.destPort;
134    egtpCb.dstCb.sendTptSrvr.addr.address = CM_INET_NTOH_U32(egtpCb.egtpCfg.localIp.ipV4Addr);
135    egtpCb.dstCb.sendTptSrvr.addr.port = egtpCb.egtpCfg.localPort;
136    egtpCb.dstCb.numTunn = 0;
137
138    ret = cmHashListInit(&(egtpCb.dstCb.teIdLst), 1024, sizeof(EgtpTeIdCb), FALSE, CM_HASH_KEYTYPE_U32MOD, CU_APP_MEM_REG, CU_POOL);
139
140    if(ret != ROK)
141    {
142       DU_LOG("\nEGTP : TeId hash list initialization failed");
143       RETVALUE(RFAILED);
144    }
145    else
146    {
147       DU_LOG("\nEGTP : Configuration successful");
148    }
149
150    RETVALUE(ROK);
151 } /* cuEgtpCfgReq */
152
153 /**************************************************************************
154  * @brief EGTP server open request 
155  *
156  * @details
157  *
158  *      Function : egtpSrvOpenReq
159  * 
160  *      Functionality:
161  *           This function handles EGTP open server request.
162  *           It opens udp socket to receive/send msgs.
163  *     
164  * @param[in]  Pst *pst, post structure
165  * @return ROK     - success
166  *         RFAILED - failure
167  *
168  ***************************************************************************/
169
170 S16 cuEgtpSrvOpenReq(Pst *pst)
171 {
172
173    U8 ret;
174
175    DU_LOG("\nEGTP : Received open server request");
176  
177    sockType = CM_INET_DGRAM;
178    if(ret = (cmInetSocket(sockType, &(egtpCb.recvTptSrvr.sockFd), protType)) != ROK)
179    {
180       DU_LOG("\nEGTP : Failed to open UDP socket");
181       RETVALUE(RFAILED);
182    }
183
184    ret = cmInetBind(&(egtpCb.recvTptSrvr.sockFd), &(egtpCb.recvTptSrvr.addr));
185    if(ret != ROK)
186    {
187       DU_LOG("\nEGTP : Failed to bind socket");
188       RETVALUE(RFAILED);
189    }
190
191    if(ret = (cmInetSocket(sockType, &(egtpCb.dstCb.sendTptSrvr.sockFd), protType)) != ROK)
192    {  
193       DU_LOG("\nEGTP : Failed to open UDP socket");
194       RETVALUE(RFAILED);
195    }
196       
197    ret = cmInetBind(&(egtpCb.dstCb.sendTptSrvr.sockFd), &(egtpCb.dstCb.sendTptSrvr.addr));
198    if(ret != ROK)
199    {  
200       DU_LOG("\nEGTP : Failed to bind socket");
201       RETVALUE(RFAILED);
202    }
203        
204    /* TODO: set socket options */
205
206    DU_LOG("\nEGTP : Receiver socket[%d] and Sender socket[%d] open", egtpCb.recvTptSrvr.sockFd.fd, egtpCb.dstCb.sendTptSrvr.sockFd.fd);
207    RETVALUE(ROK);
208 } /* cuEgtpSrvOpenReq */
209
210
211 /**************************************************************************
212  * @brief EGTP tunnel management request
213  *
214  * @details
215  *
216  *      Function : cuEgtpTnlMgmtReq
217  * 
218  *      Functionality:
219  *           This function handles EGTP tunnel managament request
220  *     
221  * @param[in]  Pst *pst, post structure
222  *             Tunnel Eveny structure
223  * @return ROK     - success
224  *         RFAILED - failure
225  *
226  
227  * ***************************************************************************/
228 S16 cuEgtpTnlMgmtReq(EgtpTnlEvt tnlEvt)
229 {
230    S8 ret;
231
232    DU_LOG("\nEGTP : Received tunnel management request");
233    switch(tnlEvt.action)
234    {
235       case EGTP_TNL_MGMT_ADD:
236       {
237          ret = cuEgtpTnlAdd(tnlEvt);
238          break;
239       }
240       case EGTP_TNL_MGMT_MOD:
241       {
242          ret = cuEgtpTnlMod(tnlEvt);
243          break;
244       }
245       case EGTP_TNL_MGMT_DEL:
246       {
247          ret = cuEgtpTnlDel(tnlEvt);
248          break;
249       }
250       default:
251       {
252          DU_LOG("\nEGTP : Invalid tunnel management action[%d]", tnlEvt.action);
253          ret = RFAILED;
254       }
255    }
256
257    RETVALUE(ret);
258 }
259
260 /**************************************************************************
261  * @brief EGTP tunnel addition
262  *
263  * @details
264  *
265  *      Function : cuEgtpTnlAdd
266  * 
267  *      Functionality:
268  *           This function handles EGTP tunnel addition
269  *     
270  * @param[in]  Tunnel Event structure
271  * @return ROK     - success
272  *         RFAILED - failure
273  *
274  * ***************************************************************************/
275 S16 cuEgtpTnlAdd(EgtpTnlEvt tnlEvt)
276 {
277    S16   ret;
278    EgtpTeIdCb     *teidCb;
279    EgtpMsgHdr   preDefHdr; /* pre-define header for this tunnel */
280    
281
282    DU_LOG("\nEGTP : Tunnel addition : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
283
284    ret = SGetSBuf(CU_APP_MEM_REG, CU_POOL, (Data **)&teidCb, (Size)sizeof(EgtpTeIdCb));
285    if(ret != ROK)
286    {
287       DU_LOG("\nEGTP : Memory allocation failed");
288       RETVALUE(RFAILED);
289    }
290
291
292    cmMemset((U8 *)teidCb, 0, sizeof(EgtpTeIdCb));
293    teidCb->teId = tnlEvt.lclTeid;
294    teidCb->remTeId = tnlEvt.remTeid;
295
296    ret = cmHashListInsert(&(egtpCb.dstCb.teIdLst), (PTR)teidCb, (U8 *)&(teidCb->teId), sizeof(U32));
297    if(ret != ROK)
298    {
299       DU_LOG("\nEGTP : Failed to insert in hash list");
300       SPutSBuf(CU_APP_MEM_REG, CU_POOL, (Data *)teidCb, (Size)sizeof(EgtpTeIdCb));
301       RETVALUE(RFAILED);
302    }
303    egtpCb.dstCb.numTunn++;
304
305    /* Encoding pre-defined header */
306    cmMemset((U8*)&preDefHdr, 0, sizeof(EgtpMsgHdr));
307    preDefHdr.msgType = EGTPU_MSG_GPDU;
308    preDefHdr.teId = teidCb->remTeId;
309    preDefHdr.extHdr.pdcpNmb.pres = FALSE;
310    preDefHdr.extHdr.udpPort.pres = FALSE;
311    preDefHdr.nPdu.pres = FALSE;
312    
313    cuEgtpEncodeHdr((U8 *)teidCb->preEncodedHdr.hdr, &preDefHdr, &(teidCb->preEncodedHdr.cnt));
314
315 /*   SPutSBuf(CU_APP_MEM_REG, CU_POOL, (Data *)teidCb, (Size)sizeof(EgtpTeIdCb));*/
316
317    RETVALUE(ROK);
318 } /* cuEgtpTnlAdd */
319
320 /**************************************************************************
321  * @brief EGTP tunnel modification
322  *
323  * @details
324  *
325  *      Function : cuEgtpTnlMod
326  * 
327  *      Functionality:
328  *           This function handles EGTP tunnel modification
329  *     
330  * @param[in]  Tunnel Event structure
331  * @return ROK     - success
332  *         RFAILED - failure
333  * 
334  * ***************************************************************************/
335 S16 cuEgtpTnlMod(EgtpTnlEvt tnlEvt)
336 {
337 #if 0
338    S16   ret;
339    EgtpTeIdCb     *teidCb = NULLP;
340
341    printf("\nTunnel modification : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
342
343    cmHashListFind(&(egtpCb.dstCb.teIdLst), (U8 *)&(tnlEvt.teId), sizeof(U32), 0, (PTR *)&teidCb);
344    if(teidCb == NULLP)
345    {
346       printf("\nTunnel id not found");
347       RETVALUE(RFAILED);
348    }  
349    
350    teidCb->teId = tnlEvt.lclTeid;
351    teidCb->remTeId = tnlEvt.remTeid;
352 #endif
353    RETVALUE(ROK);
354 }
355
356 /**************************************************************************
357  * @brief EGTP tunnel deletion
358  *
359  * @details
360  *
361  *     Function : cuEgtpTnlDel
362  * 
363  *     Functionality:
364  *         This function handles EGTP tunnel deletion
365  *    
366  * @param[in]  Tunnel Event structure
367  * @return ROK     - success
368  *         RFAILED - failure
369  * 
370  * ***************************************************************************/
371 S16 cuEgtpTnlDel(EgtpTnlEvt tnlEvt)
372 {
373    EgtpTeIdCb     *teidCb = NULLP;
374
375    DU_LOG("\nEGTP : Tunnel deletion : Local Teid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
376    
377    cmHashListFind(&(egtpCb.dstCb.teIdLst), (U8 *)&(tnlEvt.lclTeid), sizeof(U32), 0, (PTR *)&teidCb);
378    if(teidCb == NULLP)
379    {
380       DU_LOG("\nEGTP : Tunnel id[%d] not configured", tnlEvt.lclTeid);
381       RETVALUE(RFAILED);
382    } 
383
384    cmHashListDelete(&(egtpCb.dstCb.teIdLst), (PTR)teidCb);
385    SPutSBuf(CU_APP_MEM_REG, CU_POOL, (Data *)teidCb, (Size)sizeof(EgtpTeIdCb));
386    egtpCb.dstCb.numTunn--;
387
388    RETVALUE(ROK);
389 } /* cuEgtpTnlDel */
390
391 /*******************************************************************
392  *
393  * @brief Encodes message header
394  *
395  * @details
396  *
397  *    Function : cuEgtpEncodeHdr
398  * 
399  *    Functionality:
400  *       Encodes EGTP message haeder
401  *
402  * @params[in] EGTP message
403  *             Message Buffer 
404  * @return ROK     - success
405  *         RFAILED - failure
406  *
407  * ****************************************************************/
408 S16 cuEgtpEncodeHdr(U8 *preEncodedHdr, EgtpMsgHdr *preDefHdr, U8 *hdrIdx)
409 {
410    U8         tmpByte = 0;                 /* Stores one byte of data for enc */
411    U8         cnt     = EGTP_MAX_HDR_LEN;  /* Stores the position */
412    Bool       extPres = FALSE;             /* Flag for indication of S, E or P presense flag */
413    U16        nwWord = 0;
414
415    /* Encoding header */
416    tmpByte |= EGTP_MASK_BIT6;   /* Setting 6th LSB of 1st byte as version */
417    tmpByte |= EGTP_MASK_BIT5;   /* Setting 5th LSB of 1st byte as protocol type */
418    
419    if(preDefHdr->extHdr.udpPort.pres || preDefHdr->extHdr.pdcpNmb.pres)
420    {
421       tmpByte |= EGTP_MASK_BIT3;  /* Setting 3rd LSB of 1st byte if Extension heaeder is present */
422    }
423     
424    if(preDefHdr->seqNum.pres)
425    {
426       tmpByte |= EGTP_MASK_BIT2;
427    }
428    
429    if(preDefHdr->nPdu.pres)
430    {
431       tmpByte |= EGTP_MASK_BIT1;
432    }
433    
434    if((tmpByte & EGTP_MASK_BIT1) || (tmpByte & EGTP_MASK_BIT2)||(tmpByte & EGTP_MASK_BIT3))
435    {
436       extPres = TRUE;
437    }
438    preEncodedHdr[--cnt] = tmpByte;
439    preEncodedHdr[--cnt] = preDefHdr->msgType;
440     
441    /* Encode Tunnel endpoint */
442    preEncodedHdr[--cnt] = 0;
443    preEncodedHdr[--cnt] = 0;
444    nwWord = (U16)(GetHiWord(preDefHdr->teId));
445    preEncodedHdr[--cnt] = (U8)(GetHiByte(nwWord));
446    preEncodedHdr[--cnt] = (U8)(GetLoByte(nwWord));
447    nwWord = (U16)(GetLoWord(preDefHdr->teId));
448    preEncodedHdr[--cnt] = (U8)(GetHiByte(nwWord));
449    preEncodedHdr[--cnt] = (U8)(GetLoByte(nwWord));
450     
451    /* Encode sequence number */
452    if(preDefHdr->seqNum.pres)
453    {
454       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->seqNum.val);
455       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->seqNum.val);
456    }
457    else if(extPres)
458    {
459       preEncodedHdr[--cnt] = 0;
460       preEncodedHdr[--cnt] = 0;
461    }
462
463    if(preDefHdr->nPdu.pres)
464    {
465       preEncodedHdr[--cnt] = preDefHdr->nPdu.val;
466    }
467    else if(extPres)
468    {
469       preEncodedHdr[--cnt] = 0;
470    }
471     
472    if(preDefHdr->extHdr.udpPort.pres)
473    {
474       preEncodedHdr[--cnt] = EGTP_EXT_HDR_UDP_TYPE;
475       preEncodedHdr[--cnt] = 1;
476       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->extHdr.udpPort.val);
477       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->extHdr.udpPort.val);
478    }
479    
480    if(preDefHdr->extHdr.pdcpNmb.pres)
481    {
482       preEncodedHdr[--cnt] = EGTP_EXT_HDR_PDCP_TYPE;
483       preEncodedHdr[--cnt] = 1;
484       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->extHdr.pdcpNmb.val);
485       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->extHdr.pdcpNmb.val);
486    }
487     
488    if(tmpByte & EGTP_MASK_BIT3)
489    {
490       preEncodedHdr[--cnt] = 0;
491    }
492    else if(extPres)
493    {
494       preEncodedHdr[--cnt] = 0;
495    } 
496    
497    *hdrIdx = cnt;
498    RETVALUE(ROK);
499
500 } /* egtpEncodeHdr */
501
502 S16 cuEgtpHdlRecvMsg(Buffer *mBuf)
503 {
504    /*Decoding of EGTP message header */
505    cuEgtpDecodeHdr(mBuf);
506
507    /* Start Pumping data from CU to DU */
508    RETVALUE(cuEgtpDatReq());
509
510 }
511
512 S16 cuEgtpDecodeHdr(Buffer *mBuf)
513 {
514    EgtpMsg  egtpMsg;
515    S16      retVal  = ROK;       /* Holds the return value */
516    U8       tmpByte[5];         /* Holds one byte of data after Dec */
517    U8       version = 0;         /* Holds the version type, decoded */
518    MsgLen   msgLen  = 0;         /* Holds the msgLen from the Hdr */
519    MsgLen   bufLen  = 0;         /* Holds the total buffer length */
520    U8       extHdrType = 0;       /* Holds the Extension hdr type */
521    U8       extHdrLen = 0;        /* Extension hdr length */
522    Bool     extPres = FALSE;      /* Flag for indication of S, E or P presense flag */
523
524    SFndLenMsg(mBuf, &bufLen);
525
526    /* Decode version */
527    SRemPreMsg(&tmpByte[0], mBuf);
528    version = tmpByte[0] >> 5;
529
530    /* Decode message type */
531    SRemPreMsg((Data*)&(egtpMsg.msgHdr.msgType), mBuf);
532
533    /* Decode message length */
534    SRemPreMsg(&tmpByte[1], mBuf);
535    SRemPreMsg(&tmpByte[2], mBuf);
536    msgLen = (tmpByte[1] << 8) | tmpByte[2];
537
538    /* Decode tunnel id */
539    SRemPreMsg(&tmpByte[1], mBuf);
540    SRemPreMsg(&tmpByte[2], mBuf);
541    SRemPreMsg(&tmpByte[3], mBuf);
542    SRemPreMsg(&tmpByte[4], mBuf);
543    egtpMsg.msgHdr.teId = (tmpByte[1] << 24) | (tmpByte[2] << 16) | (tmpByte[3] << 8) | tmpByte[4];
544
545    if((tmpByte[0] & EGTP_MASK_BIT1) || (tmpByte[0] & EGTP_MASK_BIT2)||(tmpByte[0] & EGTP_MASK_BIT3))
546    {
547       extPres = TRUE;
548    }
549
550    /* Decode sequence number */
551    if ( tmpByte[0] & EGTP_MASK_BIT2 )
552    {
553       egtpMsg.msgHdr.seqNum.pres = TRUE;
554       SRemPreMsg(&tmpByte[1], mBuf);
555       SRemPreMsg(&tmpByte[2], mBuf);
556       egtpMsg.msgHdr.seqNum.val = (tmpByte[1] << 8) | tmpByte[2];
557    }
558    else  if(extPres)
559    {
560       egtpMsg.msgHdr.seqNum.pres = 0;
561       SRemPreMsg(&tmpByte[1], mBuf);
562       SRemPreMsg(&tmpByte[2], mBuf);
563       egtpMsg.msgHdr.seqNum.val = (tmpByte[1] << 8) | tmpByte[2];
564    }
565
566    /* Decode N-PDU number */
567    if ( tmpByte[0] & EGTP_MASK_BIT1 )
568    {
569       egtpMsg.msgHdr.nPdu.pres = TRUE;
570       SRemPreMsg(&(egtpMsg.msgHdr.nPdu.val), mBuf);
571    }
572    else if(extPres)
573    {
574       egtpMsg.msgHdr.nPdu.pres = TRUE;
575       SRemPreMsg(&(egtpMsg.msgHdr.nPdu.val), mBuf);
576    }
577
578    if(extPres & EGTP_MASK_BIT1)
579    {
580       SRemPreMsg(&extHdrType, mBuf);
581       while( 0 != extHdrType)
582       {
583          switch (extHdrType)
584          {
585             case EGTP_EXT_HDR_UDP_TYPE:
586             {
587                SRemPreMsg(&extHdrLen, mBuf);
588                if(extHdrLen == 0x01)
589                {
590                   egtpMsg.msgHdr.extHdr.udpPort.pres = TRUE;
591                   SRemPreMsg(&tmpByte[1], mBuf);
592                   SRemPreMsg(&tmpByte[2], mBuf);
593                   egtpMsg.msgHdr.extHdr.udpPort.val = (tmpByte[1] << 8) | tmpByte[2];
594                }
595                break;
596             }
597
598             case EGTP_EXT_HDR_PDCP_TYPE:
599             {
600                SRemPreMsg(&extHdrLen, mBuf);
601                if(extHdrLen == 0x01)
602                {
603                   egtpMsg.msgHdr.extHdr.pdcpNmb.pres = TRUE;
604                   SRemPreMsg(&tmpByte[1], mBuf);
605                   SRemPreMsg(&tmpByte[2], mBuf);
606                   egtpMsg.msgHdr.extHdr.pdcpNmb.val = (tmpByte[1] << 8) | tmpByte[2];
607                }
608                break;
609             }
610          } /* End of switch */
611
612          SRemPreMsg(&extHdrType, mBuf);
613
614       } /* End of while */
615    } /* End of if(extPres & EGTP_MASK_BIT1) */
616    else if(extPres)
617    {
618       SRemPreMsg(&extHdrType, mBuf);
619    }
620
621    DU_LOG("\nEGTP : Message Buffer after decoding header ");
622    SPrntMsg(mBuf, 0, 0);
623
624    RETVALUE(ROK);
625      
626 } /* End of cuEgtpDecodeHdr */
627
628 S16 cuEgtpDatReq()
629 {
630    U8 cnt = 0;
631    EgtpMsg  egtpMsg;
632
633    /* Build Application message that is supposed to come from app to egtp */
634    BuildAppMsg(&egtpMsg);
635
636    /* Encode EGTP header to build final EGTP message */
637    BuildEgtpMsg(&egtpMsg);
638
639    /* Send Message to peer */
640    while(cnt < 1)
641    {
642       DU_LOG("\nEGTP : Sending message[%d]", cnt+1);
643       cuEgtpSendMsg(egtpMsg.msg);
644       cnt++;
645    }
646
647    SPutMsg(egtpMsg.msg);
648
649    RETVALUE(ROK);
650 }
651
652
653 S16 BuildAppMsg(EgtpMsg  *egtpMsg)
654 {
655    char data[30] = "This is EGTP data from CU";
656    int datSize = 30;
657  
658    Buffer   *mBuf;
659  
660    if(SGetMsg(CU_APP_MEM_REG, CU_POOL, &mBuf) == ROK)
661    {
662       if(SAddPstMsgMult((Data *)data, datSize, mBuf) != ROK)
663       {
664          DU_LOG("\nEGTP : SAddPstMsgMult failed");
665          SPutMsg(mBuf);
666          RETVALUE(RFAILED);
667       }
668    }
669    else
670    {
671        DU_LOG("\nEGTP : Failed to allocate memory");
672        RETVALUE(RFAILED);
673    }
674  
675    /* filling IPv4 header */
676    CmIpv4Hdr ipv4Hdr;
677    MsgLen    mLen;
678  
679    mLen = 0;
680    SFndLenMsg(mBuf, &mLen);
681
682    cmMemset((U8 *)&ipv4Hdr, 0, sizeof(CmIpv4Hdr));
683    ipv4Hdr.length = CM_IPV4_HDRLEN + mLen;
684    ipv4Hdr.hdrVer = 0x45;
685    ipv4Hdr.proto = 1;
686    ipv4Hdr.srcAddr = CM_INET_NTOH_U32(egtpCb.egtpCfg.localIp.ipV4Addr);
687    ipv4Hdr.destAddr = CM_INET_NTOH_U32(egtpCb.egtpCfg.destIp.ipV4Addr);
688  
689    /* Packing IPv4 header into buffer */
690    S16          ret, cnt, idx;
691    Data         revPkArray[CM_IPV4_HDRLEN];
692    Data         pkArray[CM_IPV4_HDRLEN];
693  
694    /* initialize locals */
695    cnt = 0;
696    cmMemset(revPkArray, 0, CM_IPV4_HDRLEN);
697    cmMemset(pkArray, 0, CM_IPV4_HDRLEN);
698
699    /* Pack Header Version */
700    pkArray[cnt++] = ipv4Hdr.hdrVer;
701  
702    /* Pack TOS */
703    pkArray[cnt++] = ipv4Hdr.tos;
704  
705    pkArray[cnt++] = (Data)GetHiByte(ipv4Hdr.length);
706    pkArray[cnt++] = (Data)GetLoByte(ipv4Hdr.length);
707  
708    /* Pack Id */
709    pkArray[cnt++] = (Data) GetHiByte(ipv4Hdr.id);
710    pkArray[cnt++] = (Data) GetLoByte(ipv4Hdr.id);
711  
712    /* Pack Offset */
713    pkArray[cnt++] = (Data)GetHiByte(ipv4Hdr.off);
714    pkArray[cnt++] = (Data)GetLoByte(ipv4Hdr.off);
715  
716    /* Pack TTL */
717    pkArray[cnt++] = ipv4Hdr.ttl;
718  
719    /* Pack Protocol */
720    pkArray[cnt++] = ipv4Hdr.proto;
721  
722    /* Pack Checksum */
723    pkArray[cnt++] = (Data)GetHiByte(ipv4Hdr.chkSum);
724    pkArray[cnt++] = (Data)GetLoByte(ipv4Hdr.chkSum);
725                                                         
726    /* Pack Source Address */
727    pkArray[cnt++] = (Data)GetHiByte(GetHiWord(ipv4Hdr.srcAddr));
728    pkArray[cnt++] = (Data)GetLoByte(GetHiWord(ipv4Hdr.srcAddr));
729    pkArray[cnt++] = (Data)GetHiByte(GetLoWord(ipv4Hdr.srcAddr));
730    pkArray[cnt++] = (Data)GetLoByte(GetLoWord(ipv4Hdr.srcAddr));
731  
732    /* Pack Destination Address */
733    pkArray[cnt++] = (Data)GetHiByte(GetHiWord(ipv4Hdr.destAddr));
734    pkArray[cnt++] = (Data)GetLoByte(GetHiWord(ipv4Hdr.destAddr));
735    pkArray[cnt++] = (Data)GetHiByte(GetLoWord(ipv4Hdr.destAddr));
736    pkArray[cnt++] = (Data)GetLoByte(GetLoWord(ipv4Hdr.destAddr));
737  
738    for (idx = 0;  idx < CM_IPV4_HDRLEN;  idx++)
739       revPkArray[idx] = pkArray[CM_IPV4_HDRLEN - idx -1];
740  
741    /* this function automatically reverses revPkArray */
742    ret = SAddPreMsgMult(revPkArray, (MsgLen)cnt, mBuf);
743  
744    egtpMsg->msgHdr.msgType = EGTPU_MSG_GPDU;
745    egtpMsg->msgHdr.nPdu.pres = FALSE;
746    egtpMsg->msgHdr.seqNum.pres = FALSE;
747    egtpMsg->msgHdr.extHdr.udpPort.pres = FALSE;
748    egtpMsg->msgHdr.extHdr.pdcpNmb.pres = FALSE;
749    egtpMsg->msgHdr.teId = 10;
750    egtpMsg->msg = mBuf;
751
752    RETVALUE(ROK);
753 }
754
755
756 S16 BuildEgtpMsg(EgtpMsg *egtpMsg)
757 {
758    EgtpTeIdCb   *teidCb = NULLP;
759    MsgLen tPduSize;
760    U8     hdrLen;
761    U32    msgLen;
762    EgtpMsgHdr   *msgHdr;
763  
764    cmHashListFind(&(egtpCb.dstCb.teIdLst), (U8 *)&(egtpMsg->msgHdr.teId), sizeof(U32), 0, (PTR *)&teidCb);
765    if(teidCb == NULLP)
766    {
767       DU_LOG("\nEGTP : Tunnel id[%d] not configured", egtpMsg->msgHdr.teId);
768       RETVALUE(LCM_REASON_INVALID_PAR_VAL);
769    }
770
771    msgHdr = &(egtpMsg->msgHdr);
772
773    hdrLen = teidCb->preEncodedHdr.cnt;
774
775    if(msgHdr->extHdr.pdcpNmb.pres)
776    {
777       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] |= EGTP_MASK_BIT3;
778       teidCb->preEncodedHdr.hdr[hdrLen] = EGTP_EXT_HDR_PDCP_TYPE;
779       teidCb->preEncodedHdr.hdr[--hdrLen] = 1;
780       teidCb->preEncodedHdr.hdr[--hdrLen] = GetHiByte(msgHdr->extHdr.pdcpNmb.val);
781       teidCb->preEncodedHdr.hdr[--hdrLen] = GetLoByte(msgHdr->extHdr.pdcpNmb.val);
782       teidCb->preEncodedHdr.hdr[--hdrLen] = 0;
783    }
784    else
785    {
786       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT3);
787    }
788  
789    SFndLenMsg(egtpMsg->msg, &tPduSize);
790
791    /*Adjust the header to fill the correct length*/
792    msgLen = tPduSize +  (EGTP_MAX_HDR_LEN - hdrLen) - 0x08;
793
794    /***********************************************
795     * Fill the length field of the message header *
796     ***********************************************/
797    teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 3] = (U8)GetHiByte(msgLen);
798    teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 4] = (U8)GetLoByte(msgLen);
799
800    /*Update the sequence number*/
801    if(egtpMsg->msgHdr.seqNum.pres)
802    {
803       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] |= (EGTP_MASK_BIT2);
804       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 9] = (U8)GetHiByte(egtpMsg->msgHdr.seqNum.val);
805       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 10] = (U8)GetLoByte(egtpMsg->msgHdr.seqNum.val);
806    }
807    else
808    {
809       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT2);
810    }
811
812    SAddPreMsgMult(&teidCb->preEncodedHdr.hdr[hdrLen], (EGTP_MAX_HDR_LEN - hdrLen), egtpMsg->msg);
813
814    DU_LOG("\nEGTP : Sending message buffer");
815    SPrntMsg(egtpMsg->msg, 0, 0);
816
817    RETVALUE(ROK);
818 }
819
820 S16 cuEgtpSendMsg(Buffer *mBuf)
821 {
822    S16            ret;
823    MsgLen         txLen;
824    CmInetMemInfo  info;
825    CmInetAddr     dstAddr;
826
827    info.region = CU_APP_MEM_REG;
828    info.pool = CU_POOL;
829  
830    dstAddr.port = EGTP_DFLT_PORT;
831    dstAddr.address = egtpCb.dstCb.dstIp;
832  
833    ret = cmInetSendMsg(&(egtpCb.dstCb.sendTptSrvr.sockFd), &dstAddr, &info, mBuf, &txLen, CM_INET_NO_FLAG);
834    if(ret != ROK && ret != RWOULDBLOCK)
835    {
836       DU_LOG("\nEGTP : Message send failure");
837       RETVALUE(RFAILED);
838    }
839    
840    DU_LOG("\nEGTP : Message Sent");
841  
842    RETVALUE(ROK);
843 }