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 * |
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; --- 13 unchanged lines hidden (view full) --- 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 |
44 */ 45 46#include <cstddef> 47#include <cstdio> 48#include <sstream> 49#include <string> 50 51#include "base/cprintf.hh" --- 161 unchanged lines hidden (view full) --- 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 |
231cksum(const TcpPtr &tcp) |
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} |
243 244uint16_t 245cksum(const UdpPtr &udp) |
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} |
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); --- 6 unchanged lines hidden (view full) --- 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 |
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) { --- 11 unchanged lines hidden (view full) --- 373} 374 375int 376hsplit(const EthPacketPtr &ptr) 377{ 378 int split_point = 0; 379 380 IpPtr ip(ptr); |
381 Ip6Ptr ip6(ptr); |
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(); |
401 } 402 return split_point; 403} 404 405 406} // namespace Net |