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