comparison src/g23m-fad/udp/udp_kerf.c @ 174:90eb61ecd093

src/g23m-fad: initial import from TCS3.2/LoCosto
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2016 05:40:46 +0000
parents
children
comparison
equal deleted inserted replaced
173:bf64d785238a 174:90eb61ecd093
1 /*
2 +----------------------------------------------------------------------------
3 | Project :
4 | Modul :
5 +----------------------------------------------------------------------------
6 | Copyright 2002 Texas Instruments Berlin, AG
7 | All rights reserved.
8 |
9 | This file is confidential and a trade secret of Texas
10 | Instruments Berlin, AG
11 | The receipt of or possession of this file does not convey
12 | any rights to reproduce or disclose its contents or to
13 | manufacture, use, or sell anything it may describe, in
14 | whole, or in part, without the specific written consent of
15 | Texas Instruments Berlin, AG.
16 +----------------------------------------------------------------------------
17 | Purpose : This Modul defines the functions for processing
18 | of incomming primitives for the component
19 | Internet Protocol of the mobile station
20 +----------------------------------------------------------------------------
21 */
22
23 #define ENTITY_UDP
24
25 #include <string.h>
26 #include "typedefs.h"
27 #include "pcm.h"
28 #include "pconst.cdg"
29 #include "mconst.cdg"
30 #include "message.h"
31 #include "ccdapi.h"
32 #include "vsi.h"
33 #include "macdef.h" /* to get PFREE_DESC2 */
34 #include "custom.h"
35 #include "gsm.h"
36 #include "prim.h"
37 #include "cus_udp.h"
38 #include "cnf_udp.h"
39 #include "mon_udp.h"
40 #include "pei.h"
41 #include "tok.h"
42 #include "dti.h" /* to get dti lib */
43 #include "udp.h"
44 #include "ip_udp.h"
45
46 /*
47 +--------------------------------------------------------------------+
48 | PROJECT : WAP MODULE : udp_kerf.c |
49 | STATE : code ROUTINE : init_udp |
50 +--------------------------------------------------------------------+
51
52 PURPOSE : Init the global variables of UDP
53 */
54
55 void init_udp (void)
56 {
57 T_HILA * p_ul;
58 T_LOLA * p_dl;
59 T_KER * p_ker = & udp_data->ker;
60
61 /* Set global variables for every entity */
62
63 INIT_STATE (HILA, DOWN);
64 INIT_STATE (LOLA, DOWN);
65
66 p_ul = & udp_data->hila;
67 *p_ul->entity_name = 0;
68 p_ul->dti_state = DTI_CLOSED;
69 p_ul->dti_data_req = NULL;
70
71 p_dl = & udp_data->lola;
72 *p_dl->entity_name = 0;
73 p_dl->dti_state = DTI_CLOSED;
74 p_dl->dti_data_ind = NULL;
75 p_dl->drop_packet = FALSE;
76 p_dl->state_err = STATE_DL_NO_ERROR;
77 p_dl->src_addr = NO_ADDR;
78 p_dl->dst_addr = NO_ADDR;
79 p_dl->dst_port = NO_PORT;
80 p_dl->src_port = NO_PORT;
81
82 /* Downlink and uplink for one entity */
83
84 p_ker->send_icmp = FALSE;
85 p_ker->port_state = PORT_DOWN;
86 p_ker->dti_data_req_icmp = NULL;
87
88 p_ker->dst_addr = NO_ADDR;
89 p_ker->src_addr = NO_ADDR;
90 p_ker->src_port = NO_PORT;
91 p_ker->dst_port = NO_PORT;
92 }
93
94 /*
95 +--------------------------------------------------------------------+
96 | PROJECT : WAP MODULE : udp_kerf.c |
97 | STATE : code ROUTINE : udp_build_packet |
98 +--------------------------------------------------------------------+
99
100 PURPOSE : Build UDP and ICMP packets to send
101 */
102
103 void udp_build_packet (BOOL hila, UBYTE to_do)
104 {
105 T_KER * p_ker = & udp_data->ker;
106
107 if (hila) /* Build the packets for higher layer */
108 {
109 switch (to_do)
110 {
111 case B_NORMAL_PACKET:
112 {
113 T_desc_list2 * desc_list = & udp_data->hila.dti_data_req->desc_list2;
114 T_desc2 * desc = (T_desc2 *) desc_list->first;
115 USHORT udp_payload_len = desc_list->list_len;
116
117 if (desc == NULL)
118 {
119 TRACE_ERROR ("Hila's desc == NULL in udp_build_packet().");
120 return;
121 }
122 else
123 {
124 /* Make a new desc for the IP and UDP packet header */
125
126 USHORT headroom = LEN_IP_HEADER_B + LEN_UDP_HEADER_B;
127 UBYTE * ip_header;
128 T_desc2 * desc_new = M_ALLOC (offsetof (T_desc2, buffer) + headroom);
129
130 if (desc_new == NULL)
131 {
132 TRACE_ERROR ("Not enough memory in udp_build_packet().");
133 return;
134 }
135 desc_new->next = (ULONG) desc;
136 desc_new->offset = 0;
137 desc_new->len = headroom;
138 desc_new->size = headroom;
139 ip_header = desc_new->buffer;
140 {
141 register unsigned i;
142 for (i=0; i < headroom; i++)
143 ip_header[i] = '\0';/*lint !e661 !e662 (Warning -- access/creation of out-of-bounds pointer) */
144 }
145
146 /* Put the desc at the first place of the descs and
147 * build the UDP header */
148
149 desc_list->first = (ULONG) desc_new;
150 desc_list->list_len = (USHORT) (desc_new->len + desc_list->list_len);
151
152 /* Fill in IP header information */
153
154 SET_IP_VERSION (ip_header, IP_VERSION);
155 SET_IP_HEADER_LEN (ip_header, MIN_HEADER_LEN);
156 /*lint -e{661, 662} (Warning -- access/creation of out-of-bounds pointer) */
157 {
158 SET_IP_TOTAL_LEN (ip_header, desc_list->list_len);
159 SET_IP_PROT (ip_header, UDP_PROT);
160 SET_IP_SOURCE_ADDR (ip_header, p_ker->src_addr);
161 SET_IP_DEST_ADDR (ip_header, p_ker->dst_addr);
162
163 SET_UDP_SRC_PORT (ip_header, p_ker->src_port);
164 SET_UDP_DST_PORT (ip_header, p_ker->dst_port);
165 SET_UDP_LEN (ip_header, udp_payload_len + LEN_UDP_HEADER_B);
166 }
167 {
168 UBYTE pseudo_header [LEN_PSEUDO_HEADER_B];
169 ULONG overlay;
170 USHORT chksum;
171
172 /* Build the pseudoheader for to calculate the chksum */
173
174 SET_UDP_PSEUDO_H_SRC_ADDR (pseudo_header, p_ker->src_addr);
175 SET_UDP_PSEUDO_H_DEST_ADDR (pseudo_header, p_ker->dst_addr);
176 SET_UDP_PSEUDO_H_ZERO (pseudo_header, 0);
177 SET_UDP_PSEUDO_H_PROT (pseudo_header, UDP_PROT);
178 SET_UDP_PSEUDO_H_LEN (pseudo_header, udp_payload_len + LEN_UDP_HEADER_B);
179
180 /* Calculate the checksum for the pseudoheader */
181 overlay = partial_checksum (pseudo_header, LEN_PSEUDO_HEADER_B);
182
183 /* Calculate for the UDP header and the data */
184 RESET_UDP_CHKSUM (ip_header, LEN_IP_HEADER_B);/*lint !e661 !e662 (Warning -- access/creation of out-of-bounds pointer) */
185 chksum = desc_checksum (desc_list, LEN_IP_HEADER_B, overlay);
186 SET_UDP_CHKSUM (ip_header, LEN_IP_HEADER_B, chksum);/*lint !e661 !e662 (Warning -- access/creation of out-of-bounds pointer) */
187 }
188 }
189 }
190 break;
191
192 default:
193 break;
194 }
195 }
196 else /* Build the packets for lower layer */
197 {
198 T_LOLA * p_dl = & udp_data->lola;
199 T_desc_list2 * desc_list = & p_dl->dti_data_ind->desc_list2;
200 T_desc2 * desc = (T_desc2 *) desc_list->first;
201
202 if (desc == NULL)
203 {
204 TRACE_ERROR ("Lola's desc == NULL in udp_build_packet().");
205 return;
206 }
207 if (desc->len < LEN_IP_HEADER_B)
208 {
209 TRACE_ERROR (
210 "Lola's desc->len < LEN_IP_HEADER_B "
211 "in udp_build_packet()."
212 );
213 return;
214 }
215 if (GET_IP_HEADER_LEN_B (desc->buffer + desc->offset) < LEN_IP_HEADER_B)
216 {
217 TRACE_ERROR (
218 "Lola's GET_IP_HEADER_LEN_B() < LEN_IP_HEADER_B "
219 "in udp_build_packet()."
220 );
221 return;
222 }
223
224 switch (to_do)
225 {
226 case B_NORMAL_PACKET:
227 {
228 /* Build a new desc for IP addresses and port numbers */
229
230 T_desc2 * addr = M_ALLOC (
231 offsetof (T_desc2, buffer) + sizeof (T_SRC_DES)
232 );
233 if (addr == NULL) {
234 TRACE_ERROR ("Not enough memory in udp_build_packet().");
235 return;
236 }
237
238 addr->next = (ULONG) desc;
239 addr->offset = 0;
240 addr->len = sizeof (T_SRC_DES);
241 addr->size = sizeof (T_SRC_DES);
242
243 memcpy (((T_SRC_DES *) addr->buffer)->src_ip, & p_dl->src_addr, 4);
244 memcpy (((T_SRC_DES *) addr->buffer)->des_ip, & p_dl->dst_addr, 4);
245 memcpy (((T_SRC_DES *) addr->buffer)->src_port, & p_dl->src_port, 2);
246 memcpy (((T_SRC_DES *) addr->buffer)->des_port, & p_dl->dst_port, 2);
247
248 desc_list->first = (ULONG) addr;
249 desc_list->list_len = (USHORT) (addr->len + desc_list->list_len);
250
251 /* Filter out the given IP and UDP header */
252 {
253 register unsigned headroom =
254 GET_IP_HEADER_LEN_B (desc->buffer + desc->offset) + LEN_UDP_HEADER_B;
255 if (desc->len < headroom)
256 {
257 TRACE_ERROR ("Lola's IP packet is short in udp_build_packet().");
258 return;
259 }
260 memmove (
261 desc->buffer + desc->offset,
262 desc->buffer + desc->offset + headroom,
263 desc->len - headroom
264 );
265 desc->len = (USHORT) (desc->len - headroom);
266 desc_list->list_len = (USHORT) (desc_list->list_len - headroom);
267 }
268 }
269 break;
270
271 case B_NO_DEST_PORT:
272 {
273 /* We build the whole ICMP packet */
274
275 PPASS (p_dl->dti_data_ind, data_req, DTI2_DATA_REQ);
276
277 desc_list = & data_req->desc_list2;
278 desc = (T_desc2 *) desc_list->first;
279
280 build_icmp_with_payload (
281 data_req,
282 IDENTITY_0,
283 STANDARD_TTL,
284 GET_IP_DEST_ADDR (desc->buffer + desc->offset),
285 ICMP_TYP_DEST_URECHBL,
286 ICMP_CODE_NO_PORT
287 );
288
289 p_ker->dti_data_req_icmp = data_req;
290 }
291 break;
292
293 default:
294 break;
295 }
296 }
297 }
298
299 /*-------------------------------------------------------------------------*/
300