win-pvdrivers

view xennet/xennet_common.c @ 592:d1754b0e1ead

Start of support for PNP OID's
author James Harper <james.harper@bendigoit.com.au>
date Fri Jun 19 22:15:47 2009 +1000 (2009-06-19)
parents 2447875396d0
children 214866b0a8fd
line source
1 /*
2 PV Net Driver for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
4 Copyright (C) 2007 Andrew Grover <andy.grover@oracle.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
21 #include "xennet.h"
23 /*
24 Increase the header to a certain size
25 */
27 BOOLEAN
28 XenNet_BuildHeader(packet_info_t *pi, ULONG new_header_size)
29 {
30 ULONG bytes_remaining;
32 //FUNCTION_ENTER();
34 if (new_header_size <= pi->header_length)
35 {
36 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size <= pi->header_length\n"));
37 return TRUE;
38 }
40 if (new_header_size > ARRAY_SIZE(pi->header_data))
41 {
42 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size > ARRAY_SIZE(pi->header_data)\n"));
43 return FALSE;
44 }
46 if (new_header_size <= pi->first_buffer_length)
47 {
48 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " new_header_size <= pi->first_buffer_length\n"));
49 pi->header_length = new_header_size;
50 if (pi->header_length == pi->first_buffer_length)
51 {
52 NdisGetNextBuffer(pi->curr_buffer, &pi->curr_buffer);
53 pi->curr_mdl_offset = 0;
54 }
55 else
56 {
57 pi->curr_mdl_offset = (USHORT)new_header_size;
58 if (pi->curr_pb)
59 pi->curr_pb = pi->curr_pb->next;
60 }
62 return TRUE;
63 }
64 else if (pi->header != pi->header_data)
65 {
66 //KdPrint((__DRIVER_NAME " Switching to header_data\n"));
67 memcpy(pi->header_data, pi->header, pi->header_length);
68 pi->header = pi->header_data;
69 }
71 bytes_remaining = new_header_size - pi->header_length;
73 //KdPrint((__DRIVER_NAME " A bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
74 while (bytes_remaining && pi->curr_buffer)
75 {
76 ULONG copy_size;
78 ASSERT(pi->curr_buffer);
79 //KdPrint((__DRIVER_NAME " B bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
80 if (MmGetMdlByteCount(pi->curr_buffer))
81 {
82 PUCHAR src_addr;
83 src_addr = MmGetSystemAddressForMdlSafe(pi->curr_buffer, NormalPagePriority);
84 if (!src_addr)
85 return FALSE;
86 copy_size = min(bytes_remaining, MmGetMdlByteCount(pi->curr_buffer) - pi->curr_mdl_offset);
87 //KdPrint((__DRIVER_NAME " B copy_size = %d\n", copy_size));
88 memcpy(pi->header + pi->header_length,
89 src_addr + pi->curr_mdl_offset, copy_size);
90 pi->curr_mdl_offset = (USHORT)(pi->curr_mdl_offset + copy_size);
91 pi->header_length += copy_size;
92 bytes_remaining -= copy_size;
93 }
94 if (pi->curr_mdl_offset == MmGetMdlByteCount(pi->curr_buffer))
95 {
96 NdisGetNextBuffer(pi->curr_buffer, &pi->curr_buffer);
97 if (pi->curr_pb)
98 pi->curr_pb = pi->curr_pb->next;
99 pi->curr_mdl_offset = 0;
100 }
101 }
102 //KdPrint((__DRIVER_NAME " C bytes_remaining = %d, pi->curr_buffer = %p, pi->mdl_count = %d\n", bytes_remaining, pi->curr_buffer, pi->mdl_count));
103 if (bytes_remaining)
104 {
105 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " bytes_remaining\n"));
106 return FALSE;
107 }
108 //FUNCTION_EXIT();
109 return TRUE;
110 }
112 ULONG
113 XenNet_ParsePacketHeader(packet_info_t *pi)
114 {
115 //FUNCTION_ENTER();
117 ASSERT(pi->first_buffer);
119 NdisQueryBufferSafe(pi->first_buffer, (PVOID)&pi->header, &pi->first_buffer_length, NormalPagePriority);
120 pi->curr_buffer = pi->first_buffer;
122 pi->header_length = 0;
123 pi->curr_mdl_offset = 0;
125 if (!XenNet_BuildHeader(pi, (ULONG)XN_HDR_SIZE))
126 {
127 KdPrint((__DRIVER_NAME " packet too small (Ethernet Header)\n"));
128 return PARSE_TOO_SMALL;
129 }
131 switch (GET_NET_PUSHORT(&pi->header[12])) // L2 protocol field
132 {
133 case 0x0800:
134 //KdPrint((__DRIVER_NAME " IP\n"));
135 if (pi->header_length < (ULONG)(XN_HDR_SIZE + 20))
136 {
137 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + 20)))
138 {
139 KdPrint((__DRIVER_NAME " packet too small (IP Header)\n"));
140 return PARSE_TOO_SMALL;
141 }
142 }
143 pi->ip_version = (pi->header[XN_HDR_SIZE + 0] & 0xF0) >> 4;
144 if (pi->ip_version != 4)
145 {
146 KdPrint((__DRIVER_NAME " ip_version = %d\n", pi->ip_version));
147 return PARSE_UNKNOWN_TYPE;
148 }
149 pi->ip4_header_length = (pi->header[XN_HDR_SIZE + 0] & 0x0F) << 2;
150 if (pi->header_length < (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + 20))
151 {
152 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + 20)))
153 {
154 //KdPrint((__DRIVER_NAME " packet too small (IP Header + IP Options + TCP Header)\n"));
155 return PARSE_TOO_SMALL;
156 }
157 }
158 break;
159 default:
160 //KdPrint((__DRIVER_NAME " Not IP (%d)\n", GET_NET_PUSHORT(&pi->header[12])));
161 return PARSE_UNKNOWN_TYPE;
162 }
163 pi->ip_proto = pi->header[XN_HDR_SIZE + 9];
164 switch (pi->ip_proto)
165 {
166 case 6: // TCP
167 case 17: // UDP
168 break;
169 default:
170 //KdPrint((__DRIVER_NAME " Not TCP/UDP (%d)\n", pi->ip_proto));
171 return PARSE_UNKNOWN_TYPE;
172 }
173 pi->ip4_length = GET_NET_PUSHORT(&pi->header[XN_HDR_SIZE + 2]);
174 pi->tcp_header_length = (pi->header[XN_HDR_SIZE + pi->ip4_header_length + 12] & 0xf0) >> 2;
176 if (pi->header_length < (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + pi->tcp_header_length))
177 {
178 if (!XenNet_BuildHeader(pi, (ULONG)(XN_HDR_SIZE + pi->ip4_header_length + pi->tcp_header_length)))
179 {
180 //KdPrint((__DRIVER_NAME " packet too small (IP Header + IP Options + TCP Header + TCP Options)\n"));
181 return PARSE_TOO_SMALL;
182 }
183 }
185 pi->tcp_length = pi->ip4_length - pi->ip4_header_length - pi->tcp_header_length;
186 pi->tcp_remaining = pi->tcp_length;
187 pi->tcp_seq = GET_NET_PULONG(&pi->header[XN_HDR_SIZE + pi->ip4_header_length + 4]);
188 pi->tcp_has_options = (BOOLEAN)(pi->tcp_header_length > 20);
189 if (pi->mss > 0 && pi->tcp_length > pi->mss)
190 pi->split_required = TRUE;
192 //KdPrint((__DRIVER_NAME " ip4_length = %d\n", pi->ip4_length));
193 //KdPrint((__DRIVER_NAME " tcp_length = %d\n", pi->tcp_length));
194 //FUNCTION_EXIT();
196 return PARSE_OK;
197 }
199 VOID
200 XenNet_SumIpHeader(
201 PUCHAR header,
202 USHORT ip4_header_length
203 )
204 {
205 ULONG csum = 0;
206 USHORT i;
208 ASSERT(ip4_header_length > 12);
209 ASSERT(!(ip4_header_length & 1));
211 header[XN_HDR_SIZE + 10] = 0;
212 header[XN_HDR_SIZE + 11] = 0;
213 for (i = 0; i < ip4_header_length; i += 2)
214 {
215 csum += GET_NET_PUSHORT(&header[XN_HDR_SIZE + i]);
216 }
217 while (csum & 0xFFFF0000)
218 csum = (csum & 0xFFFF) + (csum >> 16);
219 csum = ~csum;
220 SET_NET_USHORT(&header[XN_HDR_SIZE + 10], (USHORT)csum);
221 }