inet.cc (9200:16de812c5f53) inet.cc (9955:5d8722ab804b)
1/*
1/*
2 * Copyright (c) 2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Gabe Black
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Gabe Black
43 * Geoffrey Blake
31 */
32
33#include <cstddef>
34#include <cstdio>
35#include <sstream>
36#include <string>
37
38#include "base/cprintf.hh"
39#include "base/inet.hh"
40#include "base/types.hh"
41
42using namespace std;
43namespace Net {
44
45EthAddr::EthAddr()
46{
47 memset(data, 0, ETH_ADDR_LEN);
48}
49
50EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
51{
52 *data = *ea;
53}
54
55EthAddr::EthAddr(const eth_addr &ea)
56{
57 *data = *ea.data;
58}
59
60EthAddr::EthAddr(const std::string &addr)
61{
62 parse(addr);
63}
64
65const EthAddr &
66EthAddr::operator=(const eth_addr &ea)
67{
68 *data = *ea.data;
69 return *this;
70}
71
72const EthAddr &
73EthAddr::operator=(const std::string &addr)
74{
75 parse(addr);
76 return *this;
77}
78
79void
80EthAddr::parse(const std::string &addr)
81{
82 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
83 // the sscanf function won't work.
84 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
85 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
86 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
87 memset(data, 0xff, ETH_ADDR_LEN);
88 return;
89 }
90
91 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
92 if (bytes[i] & ~0xff) {
93 memset(data, 0xff, ETH_ADDR_LEN);
94 return;
95 }
96
97 data[i] = bytes[i];
98 }
99}
100
101string
102EthAddr::string() const
103{
104 stringstream stream;
105 stream << *this;
106 return stream.str();
107}
108
109bool
110operator==(const EthAddr &left, const EthAddr &right)
111{
112 return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
113}
114
115ostream &
116operator<<(ostream &stream, const EthAddr &ea)
117{
118 const uint8_t *a = ea.addr();
119 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
120 return stream;
121}
122
123string
124IpAddress::string() const
125{
126 stringstream stream;
127 stream << *this;
128 return stream.str();
129}
130
131bool
132operator==(const IpAddress &left, const IpAddress &right)
133{
134 return left.ip() == right.ip();
135}
136
137ostream &
138operator<<(ostream &stream, const IpAddress &ia)
139{
140 uint32_t ip = ia.ip();
141 ccprintf(stream, "%x.%x.%x.%x",
142 (uint8_t)(ip >> 24), (uint8_t)(ip >> 16),
143 (uint8_t)(ip >> 8), (uint8_t)(ip >> 0));
144 return stream;
145}
146
147string
148IpNetmask::string() const
149{
150 stringstream stream;
151 stream << *this;
152 return stream.str();
153}
154
155bool
156operator==(const IpNetmask &left, const IpNetmask &right)
157{
158 return (left.ip() == right.ip()) &&
159 (left.netmask() == right.netmask());
160}
161
162ostream &
163operator<<(ostream &stream, const IpNetmask &in)
164{
165 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
166 return stream;
167}
168
169string
170IpWithPort::string() const
171{
172 stringstream stream;
173 stream << *this;
174 return stream.str();
175}
176
177bool
178operator==(const IpWithPort &left, const IpWithPort &right)
179{
180 return (left.ip() == right.ip()) && (left.port() == right.port());
181}
182
183ostream &
184operator<<(ostream &stream, const IpWithPort &iwp)
185{
186 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
187 return stream;
188}
189
190uint16_t
191cksum(const IpPtr &ptr)
192{
193 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
194 return ip_cksum_carry(sum);
195}
196
197uint16_t
198__tu_cksum(const IpPtr &ip)
199{
200 int tcplen = ip->len() - ip->hlen();
201 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
202 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
203 sum += htons(ip->ip_p + tcplen);
204 return ip_cksum_carry(sum);
205}
206
207uint16_t
44 */
45
46#include <cstddef>
47#include <cstdio>
48#include <sstream>
49#include <string>
50
51#include "base/cprintf.hh"
52#include "base/inet.hh"
53#include "base/types.hh"
54
55using namespace std;
56namespace Net {
57
58EthAddr::EthAddr()
59{
60 memset(data, 0, ETH_ADDR_LEN);
61}
62
63EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
64{
65 *data = *ea;
66}
67
68EthAddr::EthAddr(const eth_addr &ea)
69{
70 *data = *ea.data;
71}
72
73EthAddr::EthAddr(const std::string &addr)
74{
75 parse(addr);
76}
77
78const EthAddr &
79EthAddr::operator=(const eth_addr &ea)
80{
81 *data = *ea.data;
82 return *this;
83}
84
85const EthAddr &
86EthAddr::operator=(const std::string &addr)
87{
88 parse(addr);
89 return *this;
90}
91
92void
93EthAddr::parse(const std::string &addr)
94{
95 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
96 // the sscanf function won't work.
97 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
98 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
99 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
100 memset(data, 0xff, ETH_ADDR_LEN);
101 return;
102 }
103
104 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
105 if (bytes[i] & ~0xff) {
106 memset(data, 0xff, ETH_ADDR_LEN);
107 return;
108 }
109
110 data[i] = bytes[i];
111 }
112}
113
114string
115EthAddr::string() const
116{
117 stringstream stream;
118 stream << *this;
119 return stream.str();
120}
121
122bool
123operator==(const EthAddr &left, const EthAddr &right)
124{
125 return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
126}
127
128ostream &
129operator<<(ostream &stream, const EthAddr &ea)
130{
131 const uint8_t *a = ea.addr();
132 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
133 return stream;
134}
135
136string
137IpAddress::string() const
138{
139 stringstream stream;
140 stream << *this;
141 return stream.str();
142}
143
144bool
145operator==(const IpAddress &left, const IpAddress &right)
146{
147 return left.ip() == right.ip();
148}
149
150ostream &
151operator<<(ostream &stream, const IpAddress &ia)
152{
153 uint32_t ip = ia.ip();
154 ccprintf(stream, "%x.%x.%x.%x",
155 (uint8_t)(ip >> 24), (uint8_t)(ip >> 16),
156 (uint8_t)(ip >> 8), (uint8_t)(ip >> 0));
157 return stream;
158}
159
160string
161IpNetmask::string() const
162{
163 stringstream stream;
164 stream << *this;
165 return stream.str();
166}
167
168bool
169operator==(const IpNetmask &left, const IpNetmask &right)
170{
171 return (left.ip() == right.ip()) &&
172 (left.netmask() == right.netmask());
173}
174
175ostream &
176operator<<(ostream &stream, const IpNetmask &in)
177{
178 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
179 return stream;
180}
181
182string
183IpWithPort::string() const
184{
185 stringstream stream;
186 stream << *this;
187 return stream.str();
188}
189
190bool
191operator==(const IpWithPort &left, const IpWithPort &right)
192{
193 return (left.ip() == right.ip()) && (left.port() == right.port());
194}
195
196ostream &
197operator<<(ostream &stream, const IpWithPort &iwp)
198{
199 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
200 return stream;
201}
202
203uint16_t
204cksum(const IpPtr &ptr)
205{
206 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
207 return ip_cksum_carry(sum);
208}
209
210uint16_t
211__tu_cksum(const IpPtr &ip)
212{
213 int tcplen = ip->len() - ip->hlen();
214 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
215 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
216 sum += htons(ip->ip_p + tcplen);
217 return ip_cksum_carry(sum);
218}
219
220uint16_t
221__tu_cksum6(const Ip6Ptr &ip6)
222{
223 int tcplen = ip6->plen() - ip6->extensionLength();
224 int sum = ip_cksum_add(ip6->payload(), tcplen, 0);
225 sum = ip_cksum_add(ip6->src(), 32, sum);
226 sum += htons(ip6->proto() + tcplen);
227 return ip_cksum_carry(sum);
228}
229
230uint16_t
208cksum(const TcpPtr &tcp)
231cksum(const TcpPtr &tcp)
209{ return __tu_cksum(IpPtr(tcp.packet())); }
232{
233 if (IpPtr(tcp.packet())) {
234 return __tu_cksum(IpPtr(tcp.packet()));
235 } else if (Ip6Ptr(tcp.packet())) {
236 return __tu_cksum6(Ip6Ptr(tcp.packet()));
237 } else {
238 assert(0);
239 }
240 // Should never reach here
241 return 0;
242}
210
211uint16_t
212cksum(const UdpPtr &udp)
243
244uint16_t
245cksum(const UdpPtr &udp)
213{ return __tu_cksum(IpPtr(udp.packet())); }
246{
247 if (IpPtr(udp.packet())) {
248 return __tu_cksum(IpPtr(udp.packet()));
249 } else if (Ip6Ptr(udp.packet())) {
250 return __tu_cksum6(Ip6Ptr(udp.packet()));
251 } else {
252 assert(0);
253 }
254 return 0;
255}
214
215bool
216IpHdr::options(vector<const IpOpt *> &vec) const
217{
218 vec.clear();
219
220 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
221 int all = hlen() - sizeof(struct ip_hdr);
222 while (all > 0) {
223 const IpOpt *opt = (const IpOpt *)data;
224 int len = opt->len();
225 if (all < len)
226 return false;
227
228 vec.push_back(opt);
229 all -= len;
230 data += len;
231 }
232
233 return true;
234}
235
256
257bool
258IpHdr::options(vector<const IpOpt *> &vec) const
259{
260 vec.clear();
261
262 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
263 int all = hlen() - sizeof(struct ip_hdr);
264 while (all > 0) {
265 const IpOpt *opt = (const IpOpt *)data;
266 int len = opt->len();
267 if (all < len)
268 return false;
269
270 vec.push_back(opt);
271 all -= len;
272 data += len;
273 }
274
275 return true;
276}
277
278#define IP6_EXTENSION(nxt) (nxt == IP_PROTO_HOPOPTS) ? true : \
279 (nxt == IP_PROTO_ROUTING) ? true : \
280 (nxt == IP_PROTO_FRAGMENT) ? true : \
281 (nxt == IP_PROTO_AH) ? true : \
282 (nxt == IP_PROTO_ESP) ? true: \
283 (nxt == IP_PROTO_DSTOPTS) ? true : false
284
285/* Scan the IP6 header for all header extensions
286 * and return the number of headers found
287 */
288int
289Ip6Hdr::extensionLength() const
290{
291 const uint8_t *data = bytes() + IP6_HDR_LEN;
292 uint8_t nxt = ip6_nxt;
293 int len = 0;
294 int all = plen();
295
296 while (IP6_EXTENSION(nxt)) {
297 const Ip6Opt *ext = (const Ip6Opt *)data;
298 nxt = ext->nxt();
299 len += ext->len();
300 data += ext->len();
301 all -= ext->len();
302 assert(all >= 0);
303 }
304 return len;
305}
306
307/* Scan the IP6 header for a particular extension
308 * header type and return a pointer to it if it
309 * exists, otherwise return NULL
310 */
311const Ip6Opt*
312Ip6Hdr::getExt(uint8_t ext_type) const
313{
314 const uint8_t *data = bytes() + IP6_HDR_LEN;
315 uint8_t nxt = ip6_nxt;
316 Ip6Opt* opt = NULL;
317 int all = plen();
318
319 while (IP6_EXTENSION(nxt)) {
320 opt = (Ip6Opt *)data;
321 if (nxt == ext_type) {
322 break;
323 }
324 nxt = opt->nxt();
325 data += opt->len();
326 all -= opt->len();
327 opt = NULL;
328 assert(all >= 0);
329 }
330 return (const Ip6Opt*)opt;
331}
332
333/* Scan the IP6 header and any extension headers
334 * to find what type of Layer 4 header exists
335 * after this header
336 */
337uint8_t
338Ip6Hdr::proto() const
339{
340 const uint8_t *data = bytes() + IP6_HDR_LEN;
341 uint8_t nxt = ip6_nxt;
342 int all = plen();
343
344 while (IP6_EXTENSION(nxt)) {
345 const Ip6Opt *ext = (const Ip6Opt *)data;
346 nxt = ext->nxt();
347 data += ext->len();
348 all -= ext->len();
349 assert(all >= 0);
350 }
351 return nxt;
352}
353
236bool
237TcpHdr::options(vector<const TcpOpt *> &vec) const
238{
239 vec.clear();
240
241 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
242 int all = off() - sizeof(struct tcp_hdr);
243 while (all > 0) {
244 const TcpOpt *opt = (const TcpOpt *)data;
245 int len = opt->len();
246 if (all < len)
247 return false;
248
249 vec.push_back(opt);
250 all -= len;
251 data += len;
252 }
253
254 return true;
255}
256
257int
258hsplit(const EthPacketPtr &ptr)
259{
260 int split_point = 0;
261
262 IpPtr ip(ptr);
354bool
355TcpHdr::options(vector<const TcpOpt *> &vec) const
356{
357 vec.clear();
358
359 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
360 int all = off() - sizeof(struct tcp_hdr);
361 while (all > 0) {
362 const TcpOpt *opt = (const TcpOpt *)data;
363 int len = opt->len();
364 if (all < len)
365 return false;
366
367 vec.push_back(opt);
368 all -= len;
369 data += len;
370 }
371
372 return true;
373}
374
375int
376hsplit(const EthPacketPtr &ptr)
377{
378 int split_point = 0;
379
380 IpPtr ip(ptr);
381 Ip6Ptr ip6(ptr);
263 if (ip) {
264 split_point = ip.pstart();
265
266 TcpPtr tcp(ip);
267 if (tcp)
268 split_point = tcp.pstart();
269
270 UdpPtr udp(ip);
271 if (udp)
272 split_point = udp.pstart();
382 if (ip) {
383 split_point = ip.pstart();
384
385 TcpPtr tcp(ip);
386 if (tcp)
387 split_point = tcp.pstart();
388
389 UdpPtr udp(ip);
390 if (udp)
391 split_point = udp.pstart();
392 } else if (ip6) {
393 split_point = ip6.pstart();
394
395 TcpPtr tcp(ip6);
396 if (tcp)
397 split_point = tcp.pstart();
398 UdpPtr udp(ip6);
399 if (udp)
400 split_point = udp.pstart();
273 }
274 return split_point;
275}
276
277
278} // namespace Net
401 }
402 return split_point;
403}
404
405
406} // namespace Net