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_interface_address_set                        PORTABLE C      */
36 /*                                                           6.1.11       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function sets the IP address and the network mask for the      */
44 /*    supplied IP instance.                                               */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP control block pointer      */
49 /*    interface_index                       IP Interface Index            */
50 /*    ip_address                            IP address                    */
51 /*    network_mask                          Network mask                  */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    tx_mutex_get                          Get protection mutex          */
60 /*    tx_mutex_put                          Put protection mutex          */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*  04-25-2022     Yuxin Zhou               Modified comment(s), and      */
74 /*                                            added internal ip address   */
75 /*                                            change notification,        */
76 /*                                            resulting in version 6.1.11 */
77 /*                                                                        */
78 /**************************************************************************/
_nx_ip_interface_address_set(NX_IP * ip_ptr,UINT interface_index,ULONG ip_address,ULONG network_mask)79 UINT  _nx_ip_interface_address_set(NX_IP *ip_ptr, UINT interface_index, ULONG ip_address, ULONG network_mask)
80 {
81 
82 #ifndef NX_DISABLE_IPV4
83 TX_INTERRUPT_SAVE_AREA
84 
85 VOID  (*address_change_notify)(NX_IP *, VOID *);
86 VOID *additional_info;
87 VOID  (*address_change_notify_internal)(NX_IP *, VOID *);
88 ULONG previous_ip_address;
89 ULONG previous_network_mask;
90 
91 
92     /* If trace is enabled, insert this event into the trace buffer.  */
93     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_ADDRESS_SET, ip_ptr, ip_address, network_mask, 0, NX_TRACE_IP_EVENTS, 0, 0);
94 
95 
96     /* Get mutex protection.  */
97     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
98 
99     /* Disable interrupts.  */
100     TX_DISABLE
101 
102     /* Save previous IP address and network mask.  */
103     previous_ip_address =    ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_address;
104     previous_network_mask =  ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network_mask;
105 
106     /* Pickup the current notification callback and additional information pointers.  */
107     address_change_notify =  ip_ptr -> nx_ip_address_change_notify;
108     additional_info =        ip_ptr -> nx_ip_address_change_notify_additional_info;
109 
110     /* Pickup the internal notification callback.  */
111     address_change_notify_internal = ip_ptr -> nx_ip_address_change_notify_internal;
112 
113     /* Setup the IP address and the network mask. */
114     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_address      =  ip_address;
115     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network_mask =  network_mask;
116     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network      =  ip_address & network_mask;
117 
118     /* Ensure the RARP function is disabled.  */
119     ip_ptr -> nx_ip_rarp_periodic_update =  NX_NULL;
120     ip_ptr -> nx_ip_rarp_queue_process =    NX_NULL;
121 
122     /* Restore interrupts.  */
123     TX_RESTORE
124 
125     /* Release mutex protection.  */
126     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
127 
128     /* Determine if the application should be notified of the IP address and/or
129        network mask change.  */
130     if ((address_change_notify) &&
131         ((ip_address != previous_ip_address) || (network_mask != previous_network_mask)))
132     {
133 
134         /* Yes, call the application's IP address change notify function.  */
135         (address_change_notify)(ip_ptr, additional_info);
136     }
137 
138     /* Determine if the internal application should be notified of the IP address and/or
139        network mask change.  */
140     if ((address_change_notify_internal) &&
141         ((ip_address != previous_ip_address) || (network_mask != previous_network_mask)))
142     {
143 
144         /* Yes, call the application's IP address change notify function.  */
145         (address_change_notify_internal)(ip_ptr, NX_NULL);
146     }
147 
148     /* Initialize the ARP defend timeout.  */
149     ip_ptr -> nx_ip_interface[interface_index].nx_interface_arp_defend_timeout = 0;
150 
151     /* Return completion status.  */
152     return(NX_SUCCESS);
153 #else /* NX_DISABLE_IPV4  */
154     NX_PARAMETER_NOT_USED(ip_ptr);
155     NX_PARAMETER_NOT_USED(interface_index);
156     NX_PARAMETER_NOT_USED(ip_address);
157     NX_PARAMETER_NOT_USED(network_mask);
158 
159     return(NX_NOT_SUPPORTED);
160 #endif /* !NX_DISABLE_IPV4  */
161 }
162 
163