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 /**   Internet Protocol (IP)                                              */
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_ip.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_ip_gateway_address_set                          PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function sets the IP gateway address that will be used for     */
44 /*    sending IP packets with addresses not in the local network.         */
45 /*                                                                        */
46 /*    Note 1: An input gateway address of zero is handled as an error.    */
47 /*    Use nx_ip_gateway_address_clear to remove an existing gateway.      */
48 /*                                                                        */
49 /*    Note 2: For a gateway address is non zero, the IP gateway address   */
50 /*    and gateway interface pointer must be non null, or this function    */
51 /*    will return an error status.                                        */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    ip_ptr                                IP control block pointer      */
56 /*    ip_address                            Gateway IP address            */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    status                                Completion status             */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    tx_mutex_get                          Obtain protection mutex       */
65 /*    tx_mutex_put                          Release protection mutex      */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
76 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_nx_ip_gateway_address_set(NX_IP * ip_ptr,ULONG ip_address)80 UINT  _nx_ip_gateway_address_set(NX_IP *ip_ptr, ULONG ip_address)
81 {
82 
83 #ifndef NX_DISABLE_IPV4
84 INT           i;
85 TX_INTERRUPT_SAVE_AREA
86 
87 NX_INTERFACE *ip_interface_ptr = NX_NULL;
88 
89     /* If trace is enabled, insert this event into the trace buffer.  */
90     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_GATEWAY_ADDRESS_SET, ip_ptr, ip_address, 0, 0, NX_TRACE_IP_EVENTS, 0, 0);
91 
92     /* Obtain the IP internal mutex so the Gateway IP address can be setup.  */
93     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
94 
95     /* Loop through all the interfaces to find the one for the input gateway address. */
96     for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++)
97     {
98 
99         /* Must be a valid interface. Match the network subnet of the interface and input address. */
100         if ((ip_ptr -> nx_ip_interface[i].nx_interface_valid) &&
101             ((ip_address & (ip_ptr -> nx_ip_interface[i].nx_interface_ip_network_mask)) ==
102              ip_ptr -> nx_ip_interface[i].nx_interface_ip_network))
103         {
104 
105             /* This is the interface for the gateway.  */
106             ip_interface_ptr = &(ip_ptr -> nx_ip_interface[i]);
107 
108             /* Break out of the search. */
109             break;
110         }
111     }
112 
113     /* Check if we found an interface. */
114     if (ip_interface_ptr == NX_NULL)
115     {
116 
117         /* None found. Unlock the mutex, and return the error status. */
118         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
119 
120         return(NX_IP_ADDRESS_ERROR);
121     }
122 
123     /* Disable interrupts.  */
124     TX_DISABLE
125 
126     /* Setup the Gateway IP address.  */
127     ip_ptr -> nx_ip_gateway_address =  ip_address;
128 
129     ip_ptr -> nx_ip_gateway_interface = ip_interface_ptr;
130 
131     /* Restore interrupts.  */
132     TX_RESTORE
133 
134     /* Release the protection mutex.  */
135     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
136 
137     /* Return completion status.  */
138     return(NX_SUCCESS);
139 #else /* NX_DISABLE_IPV4  */
140     NX_PARAMETER_NOT_USED(ip_ptr);
141     NX_PARAMETER_NOT_USED(ip_address);
142 
143     return(NX_NOT_SUPPORTED);
144 #endif /* !NX_DISABLE_IPV4  */
145 }
146 
147