1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Transmission Control Protocol (TCP)                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_tcp.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_mss_option_get                              PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function searches for the Maximum Segment Size (MSS) option.   */
45 /*    If found, first check the option length, if option length is not    */
46 /*    valid, it returns NX_FALSE to the caller, else it set the mss value */
47 /*    and returns NX_TRUE to the caller. Otherwise, NX_TRUE is returned.  */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    option_ptr                            Pointer to option area        */
52 /*    option_area_size                      Size of option area           */
53 /*    mss                                   Max segment size              */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    NX_FALSE                              TCP option is invalid         */
58 /*    NX_TRUE                               TCP option is valid           */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _nx_tcp_packet_process                TCP packet processing         */
67 /*    _nx_tcp_server_socket_relisten        Socket relisten processing    */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_nx_tcp_mss_option_get(UCHAR * option_ptr,ULONG option_area_size,ULONG * mss)78 UINT  _nx_tcp_mss_option_get(UCHAR *option_ptr, ULONG option_area_size, ULONG *mss)
79 {
80 
81 ULONG option_length;
82 
83     /* Initialize the value.  */
84     *mss = 0;
85 
86     /* Loop through the option area looking for the MSS.  */
87     while (option_area_size >= 4)
88     {
89 
90         /* Is the current character the MSS type?  */
91         if (*option_ptr == NX_TCP_MSS_KIND)
92         {
93 
94             /* Yes, we found it!  */
95 
96             /* Move the pointer forward by one.  */
97             option_ptr++;
98 
99             /* Check the option length, if option length is not equal to 4, return NX_FALSE.  */
100             if (*option_ptr++ != 4)
101             {
102                 return(NX_FALSE);
103             }
104 
105             /* Build the mss size.  */
106             *mss = (ULONG)*option_ptr++;
107 
108             /* Get the LSB of the MSS.  */
109             *mss = (*mss << 8) | (ULONG)*option_ptr;
110 
111             /* Finished, get out of the loop!  */
112             break;
113         }
114 
115         /* Otherwise, process relative to the option type.  */
116 
117         /* Check for end of list.  */
118         if (*option_ptr == NX_TCP_EOL_KIND)
119         {
120 
121             /* Yes, end of list, get out!  */
122             break;
123         }
124 
125         /* Check for NOP.  */
126         if (*option_ptr++ == NX_TCP_NOP_KIND)
127         {
128 
129             /* One character option!  */
130             option_area_size--;
131         }
132         else
133         {
134 
135             /* Derive the option length.  */
136             option_length =  ((ULONG)*option_ptr);
137 
138             /* Return when option length is invalid. */
139             if (option_length == 0)
140             {
141                 return(NX_FALSE);
142             }
143 
144             /* Move the option pointer forward.  */
145             option_ptr =  option_ptr + (option_length - 1);
146 
147             /* Determine if this is greater than the option area size.  */
148             if (option_length > option_area_size)
149             {
150                 return(NX_FALSE);
151             }
152             else
153             {
154                 option_area_size =  option_area_size - option_length;
155             }
156         }
157     }
158 
159     /* Return.  */
160     return(NX_TRUE);
161 }
162 
163