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 * Steve Reinhardt 31 * 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 * Steve Reinhardt 43 * Gabe Black
|
| 44 * Geoffrey Blake
|
32 */ 33 34#ifndef __BASE_INET_HH__ 35#define __BASE_INET_HH__ 36 37#include <iosfwd> 38#include <string> 39#include <utility> 40#include <vector> 41 42#include "base/types.hh" 43#include "dev/etherpkt.hh" 44#include "dnet/os.h" 45#include "dnet/eth.h" 46#include "dnet/ip.h" 47#include "dnet/ip6.h" 48#include "dnet/addr.h" 49#include "dnet/arp.h" 50#include "dnet/icmp.h" 51#include "dnet/tcp.h" 52#include "dnet/udp.h" 53#include "dnet/intf.h" 54#include "dnet/route.h" 55#include "dnet/fw.h" 56#include "dnet/blob.h" 57#include "dnet/rand.h" 58 59namespace Net { 60 61/* 62 * Ethernet Stuff 63 */ 64struct EthAddr : protected eth_addr 65{ 66 protected: 67 void parse(const std::string &addr); 68 69 public: 70 EthAddr(); 71 EthAddr(const uint8_t ea[ETH_ADDR_LEN]); 72 EthAddr(const eth_addr &ea); 73 EthAddr(const std::string &addr); 74 const EthAddr &operator=(const eth_addr &ea); 75 const EthAddr &operator=(const std::string &addr); 76 77 int size() const { return sizeof(eth_addr); } 78 79 const uint8_t *bytes() const { return &data[0]; } 80 uint8_t *bytes() { return &data[0]; } 81 82 const uint8_t *addr() const { return &data[0]; } 83 bool unicast() const { return data[0] == 0x00; } 84 bool multicast() const { return data[0] == 0x01; } 85 bool broadcast() const { return data[0] == 0xff; } 86 std::string string() const; 87 88 operator uint64_t() const 89 { 90 uint64_t reg = 0; 91 reg |= ((uint64_t)data[0]) << 40; 92 reg |= ((uint64_t)data[1]) << 32; 93 reg |= ((uint64_t)data[2]) << 24; 94 reg |= ((uint64_t)data[3]) << 16; 95 reg |= ((uint64_t)data[4]) << 8; 96 reg |= ((uint64_t)data[5]) << 0; 97 return reg; 98 } 99 100}; 101 102std::ostream &operator<<(std::ostream &stream, const EthAddr &ea); 103bool operator==(const EthAddr &left, const EthAddr &right); 104 105struct EthHdr : public eth_hdr 106{
| 45 */ 46 47#ifndef __BASE_INET_HH__ 48#define __BASE_INET_HH__ 49 50#include <iosfwd> 51#include <string> 52#include <utility> 53#include <vector> 54 55#include "base/types.hh" 56#include "dev/etherpkt.hh" 57#include "dnet/os.h" 58#include "dnet/eth.h" 59#include "dnet/ip.h" 60#include "dnet/ip6.h" 61#include "dnet/addr.h" 62#include "dnet/arp.h" 63#include "dnet/icmp.h" 64#include "dnet/tcp.h" 65#include "dnet/udp.h" 66#include "dnet/intf.h" 67#include "dnet/route.h" 68#include "dnet/fw.h" 69#include "dnet/blob.h" 70#include "dnet/rand.h" 71 72namespace Net { 73 74/* 75 * Ethernet Stuff 76 */ 77struct EthAddr : protected eth_addr 78{ 79 protected: 80 void parse(const std::string &addr); 81 82 public: 83 EthAddr(); 84 EthAddr(const uint8_t ea[ETH_ADDR_LEN]); 85 EthAddr(const eth_addr &ea); 86 EthAddr(const std::string &addr); 87 const EthAddr &operator=(const eth_addr &ea); 88 const EthAddr &operator=(const std::string &addr); 89 90 int size() const { return sizeof(eth_addr); } 91 92 const uint8_t *bytes() const { return &data[0]; } 93 uint8_t *bytes() { return &data[0]; } 94 95 const uint8_t *addr() const { return &data[0]; } 96 bool unicast() const { return data[0] == 0x00; } 97 bool multicast() const { return data[0] == 0x01; } 98 bool broadcast() const { return data[0] == 0xff; } 99 std::string string() const; 100 101 operator uint64_t() const 102 { 103 uint64_t reg = 0; 104 reg |= ((uint64_t)data[0]) << 40; 105 reg |= ((uint64_t)data[1]) << 32; 106 reg |= ((uint64_t)data[2]) << 24; 107 reg |= ((uint64_t)data[3]) << 16; 108 reg |= ((uint64_t)data[4]) << 8; 109 reg |= ((uint64_t)data[5]) << 0; 110 return reg; 111 } 112 113}; 114 115std::ostream &operator<<(std::ostream &stream, const EthAddr &ea); 116bool operator==(const EthAddr &left, const EthAddr &right); 117 118struct EthHdr : public eth_hdr 119{
|
107 uint16_t type() const { return ntohs(eth_type); }
| 120 bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); } 121 uint16_t type() const { 122 if (!isVlan()) 123 return ntohs(eth_type); 124 else 125 // L3 type is now 16 bytes into the hdr with 802.1Q 126 // instead of 12. dnet/eth.h only supports 802.1 127 return ntohs(*((uint16_t*)(((uint8_t *)this) + 16))); 128 } 129 uint16_t vlanId() const { 130 if (isVlan()) 131 return ntohs(*((uint16_t*)(((uint8_t *)this) + 14))); 132 else 133 return 0x0000; 134 } 135
|
108 const EthAddr &src() const { return *(EthAddr *)ð_src; } 109 const EthAddr &dst() const { return *(EthAddr *)ð_dst; } 110
| 136 const EthAddr &src() const { return *(EthAddr *)ð_src; } 137 const EthAddr &dst() const { return *(EthAddr *)ð_dst; } 138
|
111 int size() const { return sizeof(eth_hdr); }
| 139 int size() const { 140 if (!isVlan()) 141 return sizeof(eth_hdr); 142 else 143 return (sizeof(eth_hdr)+4); 144 }
|
112 113 const uint8_t *bytes() const { return (const uint8_t *)this; } 114 const uint8_t *payload() const { return bytes() + size(); } 115 uint8_t *bytes() { return (uint8_t *)this; } 116 uint8_t *payload() { return bytes() + size(); } 117}; 118 119class EthPtr 120{ 121 protected: 122 friend class IpPtr;
| 145 146 const uint8_t *bytes() const { return (const uint8_t *)this; } 147 const uint8_t *payload() const { return bytes() + size(); } 148 uint8_t *bytes() { return (uint8_t *)this; } 149 uint8_t *payload() { return bytes() + size(); } 150}; 151 152class EthPtr 153{ 154 protected: 155 friend class IpPtr;
|
| 156 friend class Ip6Ptr;
|
123 EthPacketPtr p; 124 125 public: 126 EthPtr() {} 127 EthPtr(const EthPacketPtr &ptr) : p(ptr) { } 128 129 EthHdr *operator->() { return (EthHdr *)p->data; } 130 EthHdr &operator*() { return *(EthHdr *)p->data; } 131 operator EthHdr *() { return (EthHdr *)p->data; } 132 133 const EthHdr *operator->() const { return (const EthHdr *)p->data; } 134 const EthHdr &operator*() const { return *(const EthHdr *)p->data; } 135 operator const EthHdr *() const { return (const EthHdr *)p->data; } 136 137 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; } 138 139 const EthPacketPtr packet() const { return p; } 140 EthPacketPtr packet() { return p; } 141 bool operator!() const { return !p; } 142 operator bool() const { return p; } 143 int off() const { return 0; } 144 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } 145}; 146 147/* 148 * IP Stuff 149 */ 150struct IpAddress 151{ 152 protected: 153 uint32_t _ip; 154 155 public: 156 IpAddress() : _ip(0) 157 {} 158 IpAddress(const uint32_t __ip) : _ip(__ip) 159 {} 160 161 uint32_t ip() const { return _ip; } 162 163 std::string string() const; 164}; 165 166std::ostream &operator<<(std::ostream &stream, const IpAddress &ia); 167bool operator==(const IpAddress &left, const IpAddress &right); 168 169struct IpNetmask : public IpAddress 170{ 171 protected: 172 uint8_t _netmask; 173 174 public: 175 IpNetmask() : IpAddress(), _netmask(0) 176 {} 177 IpNetmask(const uint32_t __ip, const uint8_t __netmask) : 178 IpAddress(__ip), _netmask(__netmask) 179 {} 180 181 uint8_t netmask() const { return _netmask; } 182 183 std::string string() const; 184}; 185 186std::ostream &operator<<(std::ostream &stream, const IpNetmask &in); 187bool operator==(const IpNetmask &left, const IpNetmask &right); 188 189struct IpWithPort : public IpAddress 190{ 191 protected: 192 uint16_t _port; 193 194 public: 195 IpWithPort() : IpAddress(), _port(0) 196 {} 197 IpWithPort(const uint32_t __ip, const uint16_t __port) : 198 IpAddress(__ip), _port(__port) 199 {} 200 201 uint8_t port() const { return _port; } 202 203 std::string string() const; 204}; 205 206std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp); 207bool operator==(const IpWithPort &left, const IpWithPort &right); 208 209struct IpOpt; 210struct IpHdr : public ip_hdr 211{ 212 uint8_t version() const { return ip_v; } 213 uint8_t hlen() const { return ip_hl * 4; } 214 uint8_t tos() const { return ip_tos; } 215 uint16_t len() const { return ntohs(ip_len); } 216 uint16_t id() const { return ntohs(ip_id); } 217 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 218 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 219 uint8_t ttl() const { return ip_ttl; } 220 uint8_t proto() const { return ip_p; } 221 uint16_t sum() const { return ip_sum; } 222 uint32_t src() const { return ntohl(ip_src); } 223 uint32_t dst() const { return ntohl(ip_dst); } 224 225 void sum(uint16_t sum) { ip_sum = sum; } 226 void id(uint16_t _id) { ip_id = htons(_id); } 227 void len(uint16_t _len) { ip_len = htons(_len); } 228
| 157 EthPacketPtr p; 158 159 public: 160 EthPtr() {} 161 EthPtr(const EthPacketPtr &ptr) : p(ptr) { } 162 163 EthHdr *operator->() { return (EthHdr *)p->data; } 164 EthHdr &operator*() { return *(EthHdr *)p->data; } 165 operator EthHdr *() { return (EthHdr *)p->data; } 166 167 const EthHdr *operator->() const { return (const EthHdr *)p->data; } 168 const EthHdr &operator*() const { return *(const EthHdr *)p->data; } 169 operator const EthHdr *() const { return (const EthHdr *)p->data; } 170 171 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; } 172 173 const EthPacketPtr packet() const { return p; } 174 EthPacketPtr packet() { return p; } 175 bool operator!() const { return !p; } 176 operator bool() const { return p; } 177 int off() const { return 0; } 178 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } 179}; 180 181/* 182 * IP Stuff 183 */ 184struct IpAddress 185{ 186 protected: 187 uint32_t _ip; 188 189 public: 190 IpAddress() : _ip(0) 191 {} 192 IpAddress(const uint32_t __ip) : _ip(__ip) 193 {} 194 195 uint32_t ip() const { return _ip; } 196 197 std::string string() const; 198}; 199 200std::ostream &operator<<(std::ostream &stream, const IpAddress &ia); 201bool operator==(const IpAddress &left, const IpAddress &right); 202 203struct IpNetmask : public IpAddress 204{ 205 protected: 206 uint8_t _netmask; 207 208 public: 209 IpNetmask() : IpAddress(), _netmask(0) 210 {} 211 IpNetmask(const uint32_t __ip, const uint8_t __netmask) : 212 IpAddress(__ip), _netmask(__netmask) 213 {} 214 215 uint8_t netmask() const { return _netmask; } 216 217 std::string string() const; 218}; 219 220std::ostream &operator<<(std::ostream &stream, const IpNetmask &in); 221bool operator==(const IpNetmask &left, const IpNetmask &right); 222 223struct IpWithPort : public IpAddress 224{ 225 protected: 226 uint16_t _port; 227 228 public: 229 IpWithPort() : IpAddress(), _port(0) 230 {} 231 IpWithPort(const uint32_t __ip, const uint16_t __port) : 232 IpAddress(__ip), _port(__port) 233 {} 234 235 uint8_t port() const { return _port; } 236 237 std::string string() const; 238}; 239 240std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp); 241bool operator==(const IpWithPort &left, const IpWithPort &right); 242 243struct IpOpt; 244struct IpHdr : public ip_hdr 245{ 246 uint8_t version() const { return ip_v; } 247 uint8_t hlen() const { return ip_hl * 4; } 248 uint8_t tos() const { return ip_tos; } 249 uint16_t len() const { return ntohs(ip_len); } 250 uint16_t id() const { return ntohs(ip_id); } 251 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 252 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 253 uint8_t ttl() const { return ip_ttl; } 254 uint8_t proto() const { return ip_p; } 255 uint16_t sum() const { return ip_sum; } 256 uint32_t src() const { return ntohl(ip_src); } 257 uint32_t dst() const { return ntohl(ip_dst); } 258 259 void sum(uint16_t sum) { ip_sum = sum; } 260 void id(uint16_t _id) { ip_id = htons(_id); } 261 void len(uint16_t _len) { ip_len = htons(_len); } 262
|
229
| |
230 bool options(std::vector<const IpOpt *> &vec) const; 231 232 int size() const { return hlen(); } 233 const uint8_t *bytes() const { return (const uint8_t *)this; } 234 const uint8_t *payload() const { return bytes() + size(); } 235 uint8_t *bytes() { return (uint8_t *)this; } 236 uint8_t *payload() { return bytes() + size(); } 237}; 238 239class IpPtr 240{ 241 protected: 242 friend class TcpPtr; 243 friend class UdpPtr; 244 EthPacketPtr p;
| 263 bool options(std::vector<const IpOpt *> &vec) const; 264 265 int size() const { return hlen(); } 266 const uint8_t *bytes() const { return (const uint8_t *)this; } 267 const uint8_t *payload() const { return bytes() + size(); } 268 uint8_t *bytes() { return (uint8_t *)this; } 269 uint8_t *payload() { return bytes() + size(); } 270}; 271 272class IpPtr 273{ 274 protected: 275 friend class TcpPtr; 276 friend class UdpPtr; 277 EthPacketPtr p;
|
| 278 bool eth_hdr_vlan;
|
245 246 void set(const EthPacketPtr &ptr) 247 { 248 p = 0;
| 279 280 void set(const EthPacketPtr &ptr) 281 { 282 p = 0;
|
| 283 eth_hdr_vlan = false;
|
249 250 if (ptr) { 251 EthHdr *eth = (EthHdr *)ptr->data; 252 if (eth->type() == ETH_TYPE_IP) 253 p = ptr;
| 284 285 if (ptr) { 286 EthHdr *eth = (EthHdr *)ptr->data; 287 if (eth->type() == ETH_TYPE_IP) 288 p = ptr;
|
| 289 if (eth->isVlan()) 290 eth_hdr_vlan = true;
|
254 } 255 } 256 257 public:
| 291 } 292 } 293 294 public:
|
258 IpPtr() : p(0) {} 259 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); } 260 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); } 261 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
| 295 IpPtr() : p(0), eth_hdr_vlan(false) {} 296 IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); } 297 IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); } 298 IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
|
262
| 299
|
263 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
| 300 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) + 301 ((eth_hdr_vlan) ? 4 : 0)); }
|
264 IpHdr *operator->() { return get(); } 265 IpHdr &operator*() { return *get(); } 266 267 const IpHdr *get() const
| 302 IpHdr *operator->() { return get(); } 303 IpHdr &operator*() { return *get(); } 304 305 const IpHdr *get() const
|
268 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
| 306 { return (const IpHdr *)(p->data + sizeof(eth_hdr) + 307 ((eth_hdr_vlan) ? 4 : 0)); }
|
269 const IpHdr *operator->() const { return get(); } 270 const IpHdr &operator*() const { return *get(); } 271 272 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } 273 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 274 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 275 276 const EthPacketPtr packet() const { return p; } 277 EthPacketPtr packet() { return p; } 278 bool operator!() const { return !p; } 279 operator bool() const { return p; }
| 308 const IpHdr *operator->() const { return get(); } 309 const IpHdr &operator*() const { return *get(); } 310 311 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } 312 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 313 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 314 315 const EthPacketPtr packet() const { return p; } 316 EthPacketPtr packet() { return p; } 317 bool operator!() const { return !p; } 318 operator bool() const { return p; }
|
280 int off() const { return sizeof(eth_hdr); } 281 int pstart() const { return off() + get()->size(); }
| 319 int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); } 320 int pstart() const { return (off() + get()->size()); }
|
282}; 283 284uint16_t cksum(const IpPtr &ptr); 285 286struct IpOpt : public ip_opt 287{ 288 uint8_t type() const { return opt_type; } 289 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 290 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 291 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 292 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 293 294 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 295 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 296 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 297 298 const uint8_t *data() const { return opt_data.data8; } 299 void sec(ip_opt_data_sec &sec) const; 300 void lsrr(ip_opt_data_rr &rr) const; 301 void ssrr(ip_opt_data_rr &rr) const; 302 void ts(ip_opt_data_ts &ts) const; 303 uint16_t satid() const { return ntohs(opt_data.satid); } 304 uint16_t mtup() const { return ntohs(opt_data.mtu); } 305 uint16_t mtur() const { return ntohs(opt_data.mtu); } 306 void tr(ip_opt_data_tr &tr) const; 307 const uint32_t *addext() const { return &opt_data.addext[0]; } 308 uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 309 void sdb(std::vector<uint32_t> &vec) const; 310}; 311 312/*
| 321}; 322 323uint16_t cksum(const IpPtr &ptr); 324 325struct IpOpt : public ip_opt 326{ 327 uint8_t type() const { return opt_type; } 328 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 329 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 330 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 331 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 332 333 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 334 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 335 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 336 337 const uint8_t *data() const { return opt_data.data8; } 338 void sec(ip_opt_data_sec &sec) const; 339 void lsrr(ip_opt_data_rr &rr) const; 340 void ssrr(ip_opt_data_rr &rr) const; 341 void ts(ip_opt_data_ts &ts) const; 342 uint16_t satid() const { return ntohs(opt_data.satid); } 343 uint16_t mtup() const { return ntohs(opt_data.mtu); } 344 uint16_t mtur() const { return ntohs(opt_data.mtu); } 345 void tr(ip_opt_data_tr &tr) const; 346 const uint32_t *addext() const { return &opt_data.addext[0]; } 347 uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 348 void sdb(std::vector<uint32_t> &vec) const; 349}; 350 351/*
|
| 352 * Ip6 Classes 353 */ 354struct Ip6Opt; 355struct Ip6Hdr : public ip6_hdr 356{ 357 uint8_t version() const { return ip6_vfc; } 358 uint32_t flow() const { return ntohl(ip6_flow); } 359 uint16_t plen() const { return ntohs(ip6_plen); } 360 uint16_t hlen() const { return IP6_HDR_LEN; } 361 uint8_t nxt() const { return ip6_nxt; } 362 uint8_t hlim() const { return ip6_hlim; } 363 364 const uint8_t* src() const { return ip6_src.data; } 365 const uint8_t* dst() const { return ip6_dst.data; } 366 367 int extensionLength() const; 368 const Ip6Opt* getExt(uint8_t ext) const; 369 const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); } 370 const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); } 371 const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); } 372 uint8_t proto() const; 373 374 void plen(uint16_t _plen) { ip6_plen = htons(_plen); } 375 376 int size() const { return IP6_HDR_LEN + extensionLength(); } 377 const uint8_t *bytes() const { return (const uint8_t *)this; } 378 const uint8_t *payload() const { return bytes() + IP6_HDR_LEN 379 + extensionLength(); } 380 uint8_t *bytes() { return (uint8_t *)this; } 381 uint8_t *payload() { return bytes() + IP6_HDR_LEN 382 + extensionLength(); } 383}; 384 385class Ip6Ptr 386{ 387 protected: 388 friend class TcpPtr; 389 friend class UdpPtr; 390 EthPacketPtr p; 391 bool eth_hdr_vlan; 392 393 void set(const EthPacketPtr &ptr) 394 { 395 p = 0; 396 eth_hdr_vlan = false; 397 398 if (ptr) { 399 EthHdr *eth = (EthHdr *)ptr->data; 400 if (eth->type() == ETH_TYPE_IPV6) 401 p = ptr; 402 if (eth->isVlan()) 403 eth_hdr_vlan = true; 404 } 405 } 406 407 public: 408 Ip6Ptr() : p(0), eth_hdr_vlan(false) {} 409 Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); } 410 Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); } 411 Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { } 412 413 Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr) 414 + ((eth_hdr_vlan) ? 4 : 0)); } 415 Ip6Hdr *operator->() { return get(); } 416 Ip6Hdr &operator*() { return *get(); } 417 418 const Ip6Hdr *get() const 419 { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr) 420 + ((eth_hdr_vlan) ? 4 : 0)); } 421 const Ip6Hdr *operator->() const { return get(); } 422 const Ip6Hdr &operator*() const { return *get(); } 423 424 const Ip6Ptr &operator=(const EthPacketPtr &ptr) 425 { set(ptr); return *this; } 426 const Ip6Ptr &operator=(const EthPtr &ptr) 427 { set(ptr.p); return *this; } 428 const Ip6Ptr &operator=(const Ip6Ptr &ptr) 429 { p = ptr.p; return *this; } 430 431 const EthPacketPtr packet() const { return p; } 432 EthPacketPtr packet() { return p; } 433 bool operator!() const { return !p; } 434 operator bool() const { return p; } 435 int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); } 436 int pstart() const { return off() + get()->size(); } 437}; 438 439// Dnet supplied ipv6 opt header is incomplete and 440// newer NIC card filters expect a more robust 441// ipv6 header option declaration. 442struct ip6_opt_fragment { 443 uint16_t offlg; 444 uint32_t ident; 445}; 446 447struct ip6_opt_routing_type2 { 448 uint8_t type; 449 uint8_t segleft; 450 uint32_t reserved; 451 ip6_addr_t addr; 452}; 453 454#define HOME_ADDRESS_OPTION 0xC9 455struct ip6_opt_dstopts { 456 uint8_t type; 457 uint8_t length; 458 ip6_addr_t addr; 459} __attribute__((packed)); 460 461struct ip6_opt_hdr 462{ 463 uint8_t ext_nxt; 464 uint8_t ext_len; 465 union { 466 struct ip6_opt_fragment fragment; 467 struct ip6_opt_routing_type2 rtType2; 468 struct ip6_opt_dstopts dstOpts; 469 } ext_data; 470} __attribute__((packed)); 471 472struct Ip6Opt : public ip6_opt_hdr 473{ 474 uint8_t nxt() const { return ext_nxt; } 475 uint8_t extlen() const { return ext_len; } 476 uint8_t len() const { return extlen() + 8; } 477 478 // Supporting the types of header extensions likely to be encountered: 479 // fragment, routing type 2 and dstopts. 480 481 // Routing type 2 482 uint8_t rtType2Type() const { return ext_data.rtType2.type; } 483 uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; } 484 const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; } 485 486 // Fragment 487 uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); } 488 uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); } 489 490 // Dst Options/Home Address Option 491 uint8_t dstOptType() const { return ext_data.dstOpts.type; } 492 uint8_t dstOptLength() const { return ext_data.dstOpts.length; } 493 const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; } 494}; 495 496 497/*
|
313 * TCP Stuff 314 */ 315struct TcpOpt; 316struct TcpHdr : public tcp_hdr 317{ 318 uint16_t sport() const { return ntohs(th_sport); } 319 uint16_t dport() const { return ntohs(th_dport); } 320 uint32_t seq() const { return ntohl(th_seq); } 321 uint32_t ack() const { return ntohl(th_ack); }
| 498 * TCP Stuff 499 */ 500struct TcpOpt; 501struct TcpHdr : public tcp_hdr 502{ 503 uint16_t sport() const { return ntohs(th_sport); } 504 uint16_t dport() const { return ntohs(th_dport); } 505 uint32_t seq() const { return ntohl(th_seq); } 506 uint32_t ack() const { return ntohl(th_ack); }
|
322 uint8_t off() const { return th_off; }
| 507 uint8_t off() const { return th_off*4; }
|
323 uint8_t flags() const { return th_flags & 0x3f; } 324 uint16_t win() const { return ntohs(th_win); } 325 uint16_t sum() const { return th_sum; } 326 uint16_t urp() const { return ntohs(th_urp); } 327 328 void sum(uint16_t sum) { th_sum = sum; } 329 void seq(uint32_t _seq) { th_seq = htonl(_seq); } 330 void flags(uint8_t _flags) { th_flags = _flags; } 331 332 bool options(std::vector<const TcpOpt *> &vec) const; 333 334 int size() const { return off(); } 335 const uint8_t *bytes() const { return (const uint8_t *)this; } 336 const uint8_t *payload() const { return bytes() + size(); } 337 uint8_t *bytes() { return (uint8_t *)this; } 338 uint8_t *payload() { return bytes() + size(); } 339}; 340 341class TcpPtr 342{ 343 protected: 344 EthPacketPtr p; 345 int _off; 346 347 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 348 void set(const IpPtr &ptr) 349 { 350 if (ptr && ptr->proto() == IP_PROTO_TCP)
| 508 uint8_t flags() const { return th_flags & 0x3f; } 509 uint16_t win() const { return ntohs(th_win); } 510 uint16_t sum() const { return th_sum; } 511 uint16_t urp() const { return ntohs(th_urp); } 512 513 void sum(uint16_t sum) { th_sum = sum; } 514 void seq(uint32_t _seq) { th_seq = htonl(_seq); } 515 void flags(uint8_t _flags) { th_flags = _flags; } 516 517 bool options(std::vector<const TcpOpt *> &vec) const; 518 519 int size() const { return off(); } 520 const uint8_t *bytes() const { return (const uint8_t *)this; } 521 const uint8_t *payload() const { return bytes() + size(); } 522 uint8_t *bytes() { return (uint8_t *)this; } 523 uint8_t *payload() { return bytes() + size(); } 524}; 525 526class TcpPtr 527{ 528 protected: 529 EthPacketPtr p; 530 int _off; 531 532 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 533 void set(const IpPtr &ptr) 534 { 535 if (ptr && ptr->proto() == IP_PROTO_TCP)
|
351 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
| 536 set(ptr.p, ptr.pstart());
|
352 else 353 set(0, 0); 354 }
| 537 else 538 set(0, 0); 539 }
|
| 540 void set(const Ip6Ptr &ptr) 541 { 542 if (ptr && ptr->proto() == IP_PROTO_TCP) 543 set(ptr.p, ptr.pstart()); 544 else 545 set(0, 0); 546 }
|
355 356 public: 357 TcpPtr() : p(0), _off(0) {} 358 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
| 547 548 public: 549 TcpPtr() : p(0), _off(0) {} 550 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
|
| 551 TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
|
359 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 360 361 TcpHdr *get() { return (TcpHdr *)(p->data + _off); } 362 TcpHdr *operator->() { return get(); } 363 TcpHdr &operator*() { return *get(); } 364 365 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); } 366 const TcpHdr *operator->() const { return get(); } 367 const TcpHdr &operator*() const { return *get(); } 368
| 552 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 553 554 TcpHdr *get() { return (TcpHdr *)(p->data + _off); } 555 TcpHdr *operator->() { return get(); } 556 TcpHdr &operator*() { return *get(); } 557 558 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); } 559 const TcpHdr *operator->() const { return get(); } 560 const TcpHdr &operator*() const { return *get(); } 561
|
369 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; } 370 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; }
| 562 const TcpPtr &operator=(const IpPtr &i) 563 { set(i); return *this; } 564 const TcpPtr &operator=(const TcpPtr &t) 565 { set(t.p, t._off); return *this; }
|
371 372 const EthPacketPtr packet() const { return p; } 373 EthPacketPtr packet() { return p; } 374 bool operator!() const { return !p; } 375 operator bool() const { return p; } 376 int off() const { return _off; } 377 int pstart() const { return off() + get()->size(); } 378}; 379 380uint16_t cksum(const TcpPtr &ptr); 381 382struct TcpOpt : public tcp_opt 383{ 384 uint8_t type() const { return opt_type; } 385 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 386 387 bool isopt(int opt) const { return type() == opt; } 388 389 const uint8_t *data() const { return opt_data.data8; } 390 391 uint16_t mss() const { return ntohs(opt_data.mss); } 392 uint8_t wscale() const { return opt_data.wscale; } 393 uint32_t echo() const { return ntohl(opt_data.echo); } 394 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 395 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 396 uint32_t cc() const { return ntohl(opt_data.cc); } 397 uint8_t cksum() const{ return opt_data.cksum; } 398 const uint8_t *md5() const { return opt_data.md5; } 399 400 int size() const { return len(); } 401 const uint8_t *bytes() const { return (const uint8_t *)this; } 402 const uint8_t *payload() const { return bytes() + size(); } 403 uint8_t *bytes() { return (uint8_t *)this; } 404 uint8_t *payload() { return bytes() + size(); } 405}; 406 407/* 408 * UDP Stuff 409 */ 410struct UdpHdr : public udp_hdr 411{ 412 uint16_t sport() const { return ntohs(uh_sport); } 413 uint16_t dport() const { return ntohs(uh_dport); } 414 uint16_t len() const { return ntohs(uh_ulen); } 415 uint16_t sum() const { return uh_sum; } 416 417 void sum(uint16_t sum) { uh_sum = sum; } 418 void len(uint16_t _len) { uh_ulen = htons(_len); } 419 420 int size() const { return sizeof(udp_hdr); } 421 const uint8_t *bytes() const { return (const uint8_t *)this; } 422 const uint8_t *payload() const { return bytes() + size(); } 423 uint8_t *bytes() { return (uint8_t *)this; } 424 uint8_t *payload() { return bytes() + size(); } 425}; 426 427class UdpPtr 428{ 429 protected: 430 EthPacketPtr p; 431 int _off; 432 433 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 434 void set(const IpPtr &ptr) 435 { 436 if (ptr && ptr->proto() == IP_PROTO_UDP)
| 566 567 const EthPacketPtr packet() const { return p; } 568 EthPacketPtr packet() { return p; } 569 bool operator!() const { return !p; } 570 operator bool() const { return p; } 571 int off() const { return _off; } 572 int pstart() const { return off() + get()->size(); } 573}; 574 575uint16_t cksum(const TcpPtr &ptr); 576 577struct TcpOpt : public tcp_opt 578{ 579 uint8_t type() const { return opt_type; } 580 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 581 582 bool isopt(int opt) const { return type() == opt; } 583 584 const uint8_t *data() const { return opt_data.data8; } 585 586 uint16_t mss() const { return ntohs(opt_data.mss); } 587 uint8_t wscale() const { return opt_data.wscale; } 588 uint32_t echo() const { return ntohl(opt_data.echo); } 589 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 590 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 591 uint32_t cc() const { return ntohl(opt_data.cc); } 592 uint8_t cksum() const{ return opt_data.cksum; } 593 const uint8_t *md5() const { return opt_data.md5; } 594 595 int size() const { return len(); } 596 const uint8_t *bytes() const { return (const uint8_t *)this; } 597 const uint8_t *payload() const { return bytes() + size(); } 598 uint8_t *bytes() { return (uint8_t *)this; } 599 uint8_t *payload() { return bytes() + size(); } 600}; 601 602/* 603 * UDP Stuff 604 */ 605struct UdpHdr : public udp_hdr 606{ 607 uint16_t sport() const { return ntohs(uh_sport); } 608 uint16_t dport() const { return ntohs(uh_dport); } 609 uint16_t len() const { return ntohs(uh_ulen); } 610 uint16_t sum() const { return uh_sum; } 611 612 void sum(uint16_t sum) { uh_sum = sum; } 613 void len(uint16_t _len) { uh_ulen = htons(_len); } 614 615 int size() const { return sizeof(udp_hdr); } 616 const uint8_t *bytes() const { return (const uint8_t *)this; } 617 const uint8_t *payload() const { return bytes() + size(); } 618 uint8_t *bytes() { return (uint8_t *)this; } 619 uint8_t *payload() { return bytes() + size(); } 620}; 621 622class UdpPtr 623{ 624 protected: 625 EthPacketPtr p; 626 int _off; 627 628 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 629 void set(const IpPtr &ptr) 630 { 631 if (ptr && ptr->proto() == IP_PROTO_UDP)
|
437 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
| 632 set(ptr.p, ptr.pstart());
|
438 else 439 set(0, 0); 440 }
| 633 else 634 set(0, 0); 635 }
|
| 636 void set(const Ip6Ptr &ptr) 637 { 638 if (ptr && ptr->proto() == IP_PROTO_UDP) 639 set(ptr.p, ptr.pstart()); 640 else 641 set(0, 0); 642 }
|
441 442 public: 443 UdpPtr() : p(0), _off(0) {} 444 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
| 643 644 public: 645 UdpPtr() : p(0), _off(0) {} 646 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
|
| 647 UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
|
445 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 446 447 UdpHdr *get() { return (UdpHdr *)(p->data + _off); } 448 UdpHdr *operator->() { return get(); } 449 UdpHdr &operator*() { return *get(); } 450 451 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); } 452 const UdpHdr *operator->() const { return get(); } 453 const UdpHdr &operator*() const { return *get(); } 454 455 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
| 648 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 649 650 UdpHdr *get() { return (UdpHdr *)(p->data + _off); } 651 UdpHdr *operator->() { return get(); } 652 UdpHdr &operator*() { return *get(); } 653 654 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); } 655 const UdpHdr *operator->() const { return get(); } 656 const UdpHdr &operator*() const { return *get(); } 657 658 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
|
456 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; }
| 659 const UdpPtr &operator=(const UdpPtr &t) 660 { set(t.p, t._off); return *this; }
|
457 458 const EthPacketPtr packet() const { return p; } 459 EthPacketPtr packet() { return p; } 460 bool operator!() const { return !p; } 461 operator bool() const { return p; } 462 int off() const { return _off; } 463 int pstart() const { return off() + get()->size(); } 464}; 465
| 661 662 const EthPacketPtr packet() const { return p; } 663 EthPacketPtr packet() { return p; } 664 bool operator!() const { return !p; } 665 operator bool() const { return p; } 666 int off() const { return _off; } 667 int pstart() const { return off() + get()->size(); } 668}; 669
|
| 670uint16_t __tu_cksum6(const Ip6Ptr &ip6);
|
466uint16_t __tu_cksum(const IpPtr &ip); 467uint16_t cksum(const UdpPtr &ptr); 468 469int hsplit(const EthPacketPtr &ptr); 470 471} // namespace Net 472 473#endif // __BASE_INET_HH__
| 671uint16_t __tu_cksum(const IpPtr &ip); 672uint16_t cksum(const UdpPtr &ptr); 673 674int hsplit(const EthPacketPtr &ptr); 675 676} // namespace Net 677 678#endif // __BASE_INET_HH__
|