inet.hh revision 5761:94d56a48d7e3
1/* 2 * Copyright (c) 2002-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 */ 31 32#ifndef __BASE_INET_HH__ 33#define __BASE_INET_HH__ 34 35#include <iosfwd> 36#include <string> 37#include <utility> 38#include <vector> 39 40#include "base/range.hh" 41#include "dev/etherpkt.hh" 42#include "sim/host.hh" 43 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{ 107 uint16_t type() const { return ntohs(eth_type); } 108 const EthAddr &src() const { return *(EthAddr *)ð_src; } 109 const EthAddr &dst() const { return *(EthAddr *)ð_dst; } 110 111 int size() const { return sizeof(eth_hdr); } 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; 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}; 144 145/* 146 * IP Stuff 147 */ 148struct IpOpt; 149struct IpHdr : public ip_hdr 150{ 151 uint8_t version() const { return ip_v; } 152 uint8_t hlen() const { return ip_hl * 4; } 153 uint8_t tos() const { return ip_tos; } 154 uint16_t len() const { return ntohs(ip_len); } 155 uint16_t id() const { return ntohs(ip_id); } 156 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 157 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 158 uint8_t ttl() const { return ip_ttl; } 159 uint8_t proto() const { return ip_p; } 160 uint16_t sum() const { return ip_sum; } 161 uint32_t src() const { return ntohl(ip_src); } 162 uint32_t dst() const { return ntohl(ip_dst); } 163 164 void sum(uint16_t sum) { ip_sum = sum; } 165 void id(uint16_t _id) { ip_id = htons(_id); } 166 void len(uint16_t _len) { ip_len = htons(_len); } 167 168 169 bool options(std::vector<const IpOpt *> &vec) const; 170 171 int size() const { return hlen(); } 172 const uint8_t *bytes() const { return (const uint8_t *)this; } 173 const uint8_t *payload() const { return bytes() + size(); } 174 uint8_t *bytes() { return (uint8_t *)this; } 175 uint8_t *payload() { return bytes() + size(); } 176}; 177 178class IpPtr 179{ 180 protected: 181 friend class TcpPtr; 182 friend class UdpPtr; 183 EthPacketPtr p; 184 185 void set(const EthPacketPtr &ptr) 186 { 187 p = 0; 188 189 if (ptr) { 190 EthHdr *eth = (EthHdr *)ptr->data; 191 if (eth->type() == ETH_TYPE_IP) 192 p = ptr; 193 } 194 } 195 196 public: 197 IpPtr() : p(0) {} 198 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); } 199 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); } 200 IpPtr(const IpPtr &ptr) : p(ptr.p) { } 201 202 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); } 203 IpHdr *operator->() { return get(); } 204 IpHdr &operator*() { return *get(); } 205 206 const IpHdr *get() const 207 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); } 208 const IpHdr *operator->() const { return get(); } 209 const IpHdr &operator*() const { return *get(); } 210 211 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } 212 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 213 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 214 215 const EthPacketPtr packet() const { return p; } 216 EthPacketPtr packet() { return p; } 217 bool operator!() const { return !p; } 218 operator bool() const { return p; } 219}; 220 221uint16_t cksum(const IpPtr &ptr); 222 223struct IpOpt : public ip_opt 224{ 225 uint8_t type() const { return opt_type; } 226 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 227 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 228 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 229 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 230 231 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 232 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 233 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 234 235 const uint8_t *data() const { return opt_data.data8; } 236 void sec(ip_opt_data_sec &sec) const; 237 void lsrr(ip_opt_data_rr &rr) const; 238 void ssrr(ip_opt_data_rr &rr) const; 239 void ts(ip_opt_data_ts &ts) const; 240 uint16_t satid() const { return ntohs(opt_data.satid); } 241 uint16_t mtup() const { return ntohs(opt_data.mtu); } 242 uint16_t mtur() const { return ntohs(opt_data.mtu); } 243 void tr(ip_opt_data_tr &tr) const; 244 const uint32_t *addext() const { return &opt_data.addext[0]; } 245 uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 246 void sdb(std::vector<uint32_t> &vec) const; 247}; 248 249/* 250 * TCP Stuff 251 */ 252struct TcpOpt; 253struct TcpHdr : public tcp_hdr 254{ 255 uint16_t sport() const { return ntohs(th_sport); } 256 uint16_t dport() const { return ntohs(th_dport); } 257 uint32_t seq() const { return ntohl(th_seq); } 258 uint32_t ack() const { return ntohl(th_ack); } 259 uint8_t off() const { return th_off; } 260 uint8_t flags() const { return th_flags & 0x3f; } 261 uint16_t win() const { return ntohs(th_win); } 262 uint16_t sum() const { return th_sum; } 263 uint16_t urp() const { return ntohs(th_urp); } 264 265 void sum(uint16_t sum) { th_sum = sum; } 266 void seq(uint32_t _seq) { th_seq = htonl(_seq); } 267 void flags(uint8_t _flags) { th_flags = _flags; } 268 269 bool options(std::vector<const TcpOpt *> &vec) const; 270 271 int size() const { return off(); } 272 const uint8_t *bytes() const { return (const uint8_t *)this; } 273 const uint8_t *payload() const { return bytes() + size(); } 274 uint8_t *bytes() { return (uint8_t *)this; } 275 uint8_t *payload() { return bytes() + size(); } 276}; 277 278class TcpPtr 279{ 280 protected: 281 EthPacketPtr p; 282 int off; 283 284 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; } 285 void set(const IpPtr &ptr) 286 { 287 if (ptr && ptr->proto() == IP_PROTO_TCP) 288 set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 289 else 290 set(0, 0); 291 } 292 293 public: 294 TcpPtr() : p(0), off(0) {} 295 TcpPtr(const IpPtr &ptr) : p(0), off(0) { set(ptr); } 296 TcpPtr(const TcpPtr &ptr) : p(ptr.p), off(ptr.off) {} 297 298 TcpHdr *get() { return (TcpHdr *)(p->data + off); } 299 TcpHdr *operator->() { return get(); } 300 TcpHdr &operator*() { return *get(); } 301 302 const TcpHdr *get() const { return (const TcpHdr *)(p->data + off); } 303 const TcpHdr *operator->() const { return get(); } 304 const TcpHdr &operator*() const { return *get(); } 305 306 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; } 307 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t.off); return *this; } 308 309 const EthPacketPtr packet() const { return p; } 310 EthPacketPtr packet() { return p; } 311 bool operator!() const { return !p; } 312 operator bool() const { return p; } 313}; 314 315uint16_t cksum(const TcpPtr &ptr); 316 317typedef Range<uint16_t> SackRange; 318 319struct TcpOpt : public tcp_opt 320{ 321 uint8_t type() const { return opt_type; } 322 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 323 324 bool isopt(int opt) const { return type() == opt; } 325 326 const uint8_t *data() const { return opt_data.data8; } 327 328 uint16_t mss() const { return ntohs(opt_data.mss); } 329 uint8_t wscale() const { return opt_data.wscale; } 330 bool sack(std::vector<SackRange> &vec) const; 331 uint32_t echo() const { return ntohl(opt_data.echo); } 332 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 333 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 334 uint32_t cc() const { return ntohl(opt_data.cc); } 335 uint8_t cksum() const{ return opt_data.cksum; } 336 const uint8_t *md5() const { return opt_data.md5; } 337 338 int size() const { return len(); } 339 const uint8_t *bytes() const { return (const uint8_t *)this; } 340 const uint8_t *payload() const { return bytes() + size(); } 341 uint8_t *bytes() { return (uint8_t *)this; } 342 uint8_t *payload() { return bytes() + size(); } 343}; 344 345/* 346 * UDP Stuff 347 */ 348struct UdpHdr : public udp_hdr 349{ 350 uint16_t sport() const { return ntohs(uh_sport); } 351 uint16_t dport() const { return ntohs(uh_dport); } 352 uint16_t len() const { return ntohs(uh_ulen); } 353 uint16_t sum() const { return uh_sum; } 354 355 void sum(uint16_t sum) { uh_sum = sum; } 356 void len(uint16_t _len) { uh_ulen = htons(_len); } 357 358 int size() const { return sizeof(udp_hdr); } 359 const uint8_t *bytes() const { return (const uint8_t *)this; } 360 const uint8_t *payload() const { return bytes() + size(); } 361 uint8_t *bytes() { return (uint8_t *)this; } 362 uint8_t *payload() { return bytes() + size(); } 363}; 364 365class UdpPtr 366{ 367 protected: 368 EthPacketPtr p; 369 int off; 370 371 void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; } 372 void set(const IpPtr &ptr) 373 { 374 if (ptr && ptr->proto() == IP_PROTO_UDP) 375 set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 376 else 377 set(0, 0); 378 } 379 380 public: 381 UdpPtr() : p(0), off(0) {} 382 UdpPtr(const IpPtr &ptr) : p(0), off(0) { set(ptr); } 383 UdpPtr(const UdpPtr &ptr) : p(ptr.p), off(ptr.off) {} 384 385 UdpHdr *get() { return (UdpHdr *)(p->data + off); } 386 UdpHdr *operator->() { return get(); } 387 UdpHdr &operator*() { return *get(); } 388 389 const UdpHdr *get() const { return (const UdpHdr *)(p->data + off); } 390 const UdpHdr *operator->() const { return get(); } 391 const UdpHdr &operator*() const { return *get(); } 392 393 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; } 394 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t.off); return *this; } 395 396 const EthPacketPtr packet() const { return p; } 397 EthPacketPtr packet() { return p; } 398 bool operator!() const { return !p; } 399 operator bool() const { return p; } 400}; 401 402uint16_t cksum(const UdpPtr &ptr); 403 404/* namespace Net */ } 405 406#endif // __BASE_INET_HH__ 407