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 #include "nx_ipv6.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _nxd_ip_raw_packet_source_send PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Yuxin Zhou, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function sends a raw IP packet using specified interface IP */
46 /* address as source. */
47 /* */
48 /* INPUT */
49 /* */
50 /* ip_ptr Pointer to IP control block */
51 /* packet_ptr Pointer to packet to send */
52 /* destination_ip Destination IP address */
53 /* address_index Index of IPv4 or IPv6 address */
54 /* to use as the source address*/
55 /* protocol Value for the protocol field */
56 /* ttl Value for ttl or hop limit */
57 /* tos Value for tos or traffic */
58 /* class and flow label */
59 /* */
60 /* OUTPUT */
61 /* */
62 /* status Completion status */
63 /* */
64 /* CALLS */
65 /* */
66 /* nx_ip_packet_send Core IP packet send service */
67 /* nx_ip_route_find Find a suitable outgoing */
68 /* interface. */
69 /* tx_mutex_get Get protection mutex */
70 /* tx_mutex_put Put protection mutex */
71 /* _nxd_ipv6_raw_packet_send_internal IPv6 raw packet send */
72 /* */
73 /* CALLED BY */
74 /* */
75 /* Application */
76 /* */
77 /* RELEASE HISTORY */
78 /* */
79 /* DATE NAME DESCRIPTION */
80 /* */
81 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
82 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
83 /* resulting in version 6.1 */
84 /* */
85 /**************************************************************************/
_nxd_ip_raw_packet_source_send(NX_IP * ip_ptr,NX_PACKET * packet_ptr,NXD_ADDRESS * destination_ip,UINT address_index,ULONG protocol,UINT ttl,ULONG tos)86 UINT _nxd_ip_raw_packet_source_send(NX_IP *ip_ptr,
87 NX_PACKET *packet_ptr,
88 NXD_ADDRESS *destination_ip,
89 UINT address_index,
90 ULONG protocol,
91 UINT ttl,
92 ULONG tos)
93 {
94
95 #ifndef NX_DISABLE_IPV4
96 ULONG next_hop_address = NX_NULL;
97 #endif /* NX_DISABLE_IPV4 */
98 UINT status = NX_SUCCESS;
99
100 #ifdef NX_DISABLE_IPV4
101 NX_PARAMETER_NOT_USED(ttl);
102 NX_PARAMETER_NOT_USED(tos);
103 #endif /* NX_DISABLE_IPV4 */
104
105 /* Add debug information. */
106 NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
107
108 /* Get mutex protection. */
109 tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
110
111 #ifndef NX_DISABLE_IPV4
112
113 /* Store address information into the packet structure. */
114 if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V4)
115 {
116 packet_ptr -> nx_packet_address.nx_packet_interface_ptr = &(ip_ptr -> nx_ip_interface[address_index]);
117
118 /* Figure out a suitable outgoing interface. */
119 /* Since next_hop_address is validated in _nx_ip_packet_send, there is no need to check the value here. */
120 _nx_ip_route_find(ip_ptr, destination_ip -> nxd_ip_address.v4, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr, &next_hop_address);
121
122 /* If trace is enabled, insert this event into the trace buffer. */
123 NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_RAW_PACKET_SEND, ip_ptr, packet_ptr, destination_ip, 0, NX_TRACE_IP_EVENTS, 0, 0);
124
125 /* Yes, raw packet sending and receiving is enabled send packet! */
126 /*lint -e{644} suppress variable might not be initialized, since "next_hop_address" was initialized in _nx_ip_route_find. */
127 _nx_ip_packet_send(ip_ptr, packet_ptr, destination_ip -> nxd_ip_address.v4, (tos & 0xFF) << 16, (ttl & 0xFF), protocol << 16, NX_FRAGMENT_OKAY, next_hop_address);
128 }
129 #endif /* NX_DISABLE_IPV4 */
130
131 #ifdef FEATURE_NX_IPV6
132 if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V6)
133 {
134 packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr = &(ip_ptr -> nx_ipv6_address[address_index]);
135
136 status = _nxd_ipv6_raw_packet_send_internal(ip_ptr, packet_ptr, destination_ip, protocol);
137 }
138 #endif /* FEATURE_NX_IPV6 */
139
140 /* Release mutex protection. */
141 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
142
143 /* Return a status! */
144 return(status);
145 }
146
147