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