2 * Copyright (c) 2013 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
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.
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.
42 #include "base/inet.hh"
49 #include "base/cprintf.hh"
50 #include "base/logging.hh"
51 #include "base/types.hh"
58 memset(data
, 0, ETH_ADDR_LEN
);
61 EthAddr::EthAddr(const uint8_t ea
[ETH_ADDR_LEN
])
63 for (int i
= 0; i
< ETH_ADDR_LEN
; ++i
)
67 EthAddr::EthAddr(const eth_addr
&ea
)
69 for (int i
= 0; i
< ETH_ADDR_LEN
; ++i
)
73 EthAddr::EthAddr(const std::string
&addr
)
79 EthAddr::operator=(const eth_addr
&ea
)
86 EthAddr::operator=(const std::string
&addr
)
93 EthAddr::parse(const std::string
&addr
)
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
);
104 for (int i
= 0; i
< ETH_ADDR_LEN
; ++i
) {
105 if (bytes
[i
] & ~0xff) {
106 memset(data
, 0xff, ETH_ADDR_LEN
);
115 EthAddr::string() const
123 operator==(const EthAddr
&left
, const EthAddr
&right
)
125 return !memcmp(left
.bytes(), right
.bytes(), ETH_ADDR_LEN
);
129 operator<<(ostream
&stream
, const EthAddr
&ea
)
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]);
137 IpAddress::string() const
145 operator==(const IpAddress
&left
, const IpAddress
&right
)
147 return left
.ip() == right
.ip();
151 operator<<(ostream
&stream
, const IpAddress
&ia
)
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));
161 IpNetmask::string() const
169 operator==(const IpNetmask
&left
, const IpNetmask
&right
)
171 return (left
.ip() == right
.ip()) &&
172 (left
.netmask() == right
.netmask());
176 operator<<(ostream
&stream
, const IpNetmask
&in
)
178 ccprintf(stream
, "%s/%d", (const IpAddress
&)in
, in
.netmask());
183 IpWithPort::string() const
191 operator==(const IpWithPort
&left
, const IpWithPort
&right
)
193 return (left
.ip() == right
.ip()) && (left
.port() == right
.port());
197 operator<<(ostream
&stream
, const IpWithPort
&iwp
)
199 ccprintf(stream
, "%s:%d", (const IpAddress
&)iwp
, iwp
.port());
204 cksum(const IpPtr
&ptr
)
206 int sum
= ip_cksum_add(ptr
->bytes(), ptr
->hlen(), 0);
207 return ip_cksum_carry(sum
);
211 __tu_cksum(const IpPtr
&ip
)
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
);
221 __tu_cksum6(const Ip6Ptr
&ip6
)
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
);
231 cksum(const TcpPtr
&tcp
)
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()));
238 panic("Unrecognized IP packet format");
240 // Should never reach here
245 cksum(const UdpPtr
&udp
)
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()));
252 panic("Unrecognized IP packet format");
258 IpHdr::options(vector
<const IpOpt
*> &vec
) const
262 const uint8_t *data
= bytes() + sizeof(struct ip_hdr
);
263 int all
= hlen() - sizeof(struct ip_hdr
);
265 const IpOpt
*opt
= (const IpOpt
*)data
;
266 int len
= opt
->len();
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
285 /* Scan the IP6 header for all header extensions
286 * and return the number of headers found
289 Ip6Hdr::extensionLength() const
291 const uint8_t *data
= bytes() + IP6_HDR_LEN
;
292 uint8_t nxt
= ip6_nxt
;
296 while (IP6_EXTENSION(nxt
)) {
297 const Ip6Opt
*ext
= (const Ip6Opt
*)data
;
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
312 Ip6Hdr::getExt(uint8_t ext_type
) const
314 const uint8_t *data
= bytes() + IP6_HDR_LEN
;
315 uint8_t nxt
= ip6_nxt
;
319 while (IP6_EXTENSION(nxt
)) {
320 opt
= (Ip6Opt
*)data
;
321 if (nxt
== ext_type
) {
330 return (const Ip6Opt
*)opt
;
333 /* Scan the IP6 header and any extension headers
334 * to find what type of Layer 4 header exists
338 Ip6Hdr::proto() const
340 const uint8_t *data
= bytes() + IP6_HDR_LEN
;
341 uint8_t nxt
= ip6_nxt
;
344 while (IP6_EXTENSION(nxt
)) {
345 const Ip6Opt
*ext
= (const Ip6Opt
*)data
;
355 TcpHdr::options(vector
<const TcpOpt
*> &vec
) const
359 const uint8_t *data
= bytes() + sizeof(struct tcp_hdr
);
360 int all
= off() - sizeof(struct tcp_hdr
);
362 const TcpOpt
*opt
= (const TcpOpt
*)data
;
363 int len
= opt
->len();
376 hsplit(const EthPacketPtr
&ptr
)
383 split_point
= ip
.pstart();
387 split_point
= tcp
.pstart();
391 split_point
= udp
.pstart();
393 split_point
= ip6
.pstart();
397 split_point
= tcp
.pstart();
400 split_point
= udp
.pstart();