inet.hh revision 1114
1/* 2 * Copyright (c) 2002-2003 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 29#ifndef __BASE_INET_HH__ 30#define __BASE_INET_HH__ 31 32#include <iosfwd> 33#include <string> 34#include <utility> 35#include <vector> 36 37#include "base/range.hh" 38#include "dev/etherpkt.hh" 39#include "sim/host.hh" 40 41#include "dnet/os.h" 42#include "dnet/eth.h" 43#include "dnet/ip.h" 44#include "dnet/ip6.h" 45#include "dnet/addr.h" 46#include "dnet/arp.h" 47#include "dnet/icmp.h" 48#include "dnet/tcp.h" 49#include "dnet/udp.h" 50#include "dnet/intf.h" 51#include "dnet/route.h" 52#include "dnet/fw.h" 53#include "dnet/blob.h" 54#include "dnet/rand.h" 55 56namespace Net { 57 58/* 59 * Ethernet Stuff 60 */ 61struct EthAddr : protected eth_addr 62{ 63 protected: 64 void parse(const std::string &addr); 65 66 public: 67 EthAddr(); 68 EthAddr(const uint8_t ea[ETH_ADDR_LEN]); 69 EthAddr(const eth_addr &ea); 70 EthAddr(const std::string &addr); 71 const EthAddr &operator=(const eth_addr &ea); 72 const EthAddr &operator=(const std::string &addr); 73 74 int size() const { return sizeof(eth_addr); } 75 76 const uint8_t *bytes() const { return &data[0]; } 77 uint8_t *bytes() { return &data[0]; } 78 79 const uint8_t *addr() const { return &data[0]; } 80 bool unicast() const { return data[0] == 0x00; } 81 bool multicast() const { return data[0] == 0x01; } 82 bool broadcast() const { return data[0] == 0xff; } 83 std::string string() const; 84}; 85 86std::ostream &operator<<(std::ostream &stream, const EthAddr &ea); 87bool operator==(const EthAddr &left, const EthAddr &right); 88 89struct EthHdr : public eth_hdr 90{ 91 uint16_t type() const { return ntohs(eth_type); } 92 const EthAddr &src() const { return *(EthAddr *)ð_src; } 93 const EthAddr &dst() const { return *(EthAddr *)ð_dst; } 94 95 int size() const { return sizeof(eth_hdr); } 96 97 const uint8_t *bytes() const { return (const uint8_t *)this; } 98 const uint8_t *payload() const { return bytes() + size(); } 99 uint8_t *bytes() { return (uint8_t *)this; } 100 uint8_t *payload() { return bytes() + size(); } 101}; 102 103class EthPtr 104{ 105 protected: 106 friend class IpPtr; 107 PacketPtr p; 108 109 public: 110 EthPtr() {} 111 EthPtr(const PacketPtr &ptr) : p(ptr) { } 112 113 EthHdr *operator->() { return (EthHdr *)p->data; } 114 EthHdr &operator*() { return *(EthHdr *)p->data; } 115 operator EthHdr *() { return (EthHdr *)p->data; } 116 117 const EthHdr *operator->() const { return (const EthHdr *)p->data; } 118 const EthHdr &operator*() const { return *(const EthHdr *)p->data; } 119 operator const EthHdr *() const { return (const EthHdr *)p->data; } 120 121 const EthPtr &operator=(const PacketPtr &ptr) { p = ptr; return *this; } 122 123 const PacketPtr packet() const { return p; } 124 PacketPtr packet() { return p; } 125 bool operator!() const { return !p; } 126 operator bool() const { return p; } 127}; 128 129/* 130 * IP Stuff 131 */ 132struct IpOpt; 133struct IpHdr : public ip_hdr 134{ 135 uint8_t version() const { return ip_v; } 136 uint8_t hlen() const { return ip_hl * 4; } 137 uint8_t tos() const { return ip_tos; } 138 uint16_t len() const { return ntohs(ip_len); } 139 uint16_t id() const { return ntohs(ip_id); } 140 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 141 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 142 uint8_t ttl() const { return ip_ttl; } 143 uint8_t proto() const { return ip_p; } 144 uint16_t sum() const { return ip_sum; } 145 uint32_t src() const { return ntohl(ip_src); } 146 uint32_t dst() const { return ntohl(ip_dst); } 147 148 void sum(uint16_t sum) { ip_sum = sum; } 149 150 bool options(std::vector<const IpOpt *> &vec) const; 151 152 int size() const { return hlen(); } 153 const uint8_t *bytes() const { return (const uint8_t *)this; } 154 const uint8_t *payload() const { return bytes() + size(); } 155 uint8_t *bytes() { return (uint8_t *)this; } 156 uint8_t *payload() { return bytes() + size(); } 157}; 158 159class IpPtr 160{ 161 protected: 162 friend class TcpPtr; 163 friend class UdpPtr; 164 PacketPtr p; 165 166 const IpHdr *h() const 167 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); } 168 IpHdr *h() { return (IpHdr *)(p->data + sizeof(eth_hdr)); } 169 170 void set(const PacketPtr &ptr) 171 { 172 EthHdr *eth = (EthHdr *)ptr->data; 173 if (eth->type() == ETH_TYPE_IP) 174 p = ptr; 175 else 176 p = 0; 177 } 178 179 public: 180 IpPtr() {} 181 IpPtr(const PacketPtr &ptr) { set(ptr); } 182 IpPtr(const EthPtr &ptr) { set(ptr.p); } 183 IpPtr(const IpPtr &ptr) : p(ptr.p) { } 184 185 IpHdr *operator->() { return h(); } 186 IpHdr &operator*() { return *h(); } 187 operator IpHdr *() { return h(); } 188 189 const IpHdr *operator->() const { return h(); } 190 const IpHdr &operator*() const { return *h(); } 191 operator const IpHdr *() const { return h(); } 192 193 const IpPtr &operator=(const PacketPtr &ptr) { set(ptr); return *this; } 194 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 195 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 196 197 const PacketPtr packet() const { return p; } 198 PacketPtr packet() { return p; } 199 bool operator!() const { return !p; } 200 operator bool() const { return p; } 201 operator bool() { return p; } 202}; 203 204uint16_t cksum(const IpPtr &ptr); 205 206struct IpOpt : public ip_opt 207{ 208 uint8_t type() const { return opt_type; } 209 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 210 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 211 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 212 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 213 214 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 215 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 216 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 217 218 const uint8_t *data() const { return opt_data.data8; } 219 void sec(ip_opt_data_sec &sec) const; 220 void lsrr(ip_opt_data_rr &rr) const; 221 void ssrr(ip_opt_data_rr &rr) const; 222 void ts(ip_opt_data_ts &ts) const; 223 uint16_t satid() const { return ntohs(opt_data.satid); } 224 uint16_t mtup() const { return ntohs(opt_data.mtu); } 225 uint16_t mtur() const { return ntohs(opt_data.mtu); } 226 void tr(ip_opt_data_tr &tr) const; 227 const uint32_t *addext() const { return &opt_data.addext[0]; } 228 uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 229 void sdb(std::vector<uint32_t> &vec) const; 230}; 231 232/* 233 * TCP Stuff 234 */ 235struct TcpOpt; 236struct TcpHdr : public tcp_hdr 237{ 238 uint16_t sport() const { return ntohs(th_sport); } 239 uint16_t dport() const { return ntohs(th_dport); } 240 uint32_t seq() const { return ntohl(th_seq); } 241 uint32_t ack() const { return ntohl(th_ack); } 242 uint8_t off() const { return th_off; } 243 uint8_t flags() const { return th_flags & 0x3f; } 244 uint16_t win() const { return ntohs(th_win); } 245 uint16_t sum() const { return th_sum; } 246 uint16_t urp() const { return ntohs(th_urp); } 247 248 void sum(uint16_t sum) { th_sum = sum; } 249 250 bool options(std::vector<const TcpOpt *> &vec) const; 251 252 int size() const { return off(); } 253 const uint8_t *bytes() const { return (const uint8_t *)this; } 254 const uint8_t *payload() const { return bytes() + size(); } 255 uint8_t *bytes() { return (uint8_t *)this; } 256 uint8_t *payload() { return bytes() + size(); } 257}; 258 259class TcpPtr 260{ 261 protected: 262 PacketPtr p; 263 int off; 264 265 const TcpHdr *h() const { return (const TcpHdr *)(p->data + off); } 266 TcpHdr *h() { return (TcpHdr *)(p->data + off); } 267 268 void set(const PacketPtr &ptr, int offset) { p = ptr; off = offset; } 269 void set(const IpPtr &ptr) 270 { 271 if (ptr->proto() == IP_PROTO_TCP) 272 set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 273 else 274 set(0, 0); 275 } 276 277 public: 278 TcpPtr() {} 279 TcpPtr(const IpPtr &ptr) { set(ptr); } 280 TcpPtr(const TcpPtr &ptr) : p(ptr.p), off(ptr.off) {} 281 282 TcpHdr *operator->() { return h(); } 283 TcpHdr &operator*() { return *h(); } 284 operator TcpHdr *() { return h(); } 285 286 const TcpHdr *operator->() const { return h(); } 287 const TcpHdr &operator*() const { return *h(); } 288 operator const TcpHdr *() const { return h(); } 289 290 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; } 291 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t.off); return *this; } 292 293 const PacketPtr packet() const { return p; } 294 PacketPtr packet() { return p; } 295 bool operator!() const { return !p; } 296 operator bool() const { return p; } 297 operator bool() { return p; } 298}; 299 300uint16_t cksum(const TcpPtr &ptr); 301 302typedef Range<uint16_t> SackRange; 303 304struct TcpOpt : public tcp_opt 305{ 306 uint8_t type() const { return opt_type; } 307 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 308 309 bool isopt(int opt) const { return type() == opt; } 310 311 const uint8_t *data() const { return opt_data.data8; } 312 313 uint16_t mss() const { return ntohs(opt_data.mss); } 314 uint8_t wscale() const { return opt_data.wscale; } 315 bool sack(std::vector<SackRange> &vec) const; 316 uint32_t echo() const { return ntohl(opt_data.echo); } 317 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 318 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 319 uint32_t cc() const { return ntohl(opt_data.cc); } 320 uint8_t cksum() const{ return opt_data.cksum; } 321 const uint8_t *md5() const { return opt_data.md5; } 322 323 int size() const { return len(); } 324 const uint8_t *bytes() const { return (const uint8_t *)this; } 325 const uint8_t *payload() const { return bytes() + size(); } 326 uint8_t *bytes() { return (uint8_t *)this; } 327 uint8_t *payload() { return bytes() + size(); } 328}; 329 330/* 331 * UDP Stuff 332 */ 333struct UdpHdr : public udp_hdr 334{ 335 uint16_t sport() const { return ntohs(uh_sport); } 336 uint16_t dport() const { return ntohs(uh_dport); } 337 uint16_t len() const { return ntohs(uh_ulen); } 338 uint16_t sum() const { return ntohs(uh_sum); } 339 340 void sum(uint16_t sum) { uh_sum = htons(sum); } 341 342 int size() const { return sizeof(udp_hdr); } 343 const uint8_t *bytes() const { return (const uint8_t *)this; } 344 const uint8_t *payload() const { return bytes() + size(); } 345 uint8_t *bytes() { return (uint8_t *)this; } 346 uint8_t *payload() { return bytes() + size(); } 347}; 348 349class UdpPtr 350{ 351 protected: 352 PacketPtr p; 353 int off; 354 355 const UdpHdr *h() const { return (const UdpHdr *)(p->data + off); } 356 UdpHdr *h() { return (UdpHdr *)(p->data + off); } 357 358 void set(const PacketPtr &ptr, int offset) { p = ptr; off = offset; } 359 void set(const IpPtr &ptr) 360 { 361 if (ptr->proto() == IP_PROTO_UDP) 362 set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 363 else 364 set(0, 0); 365 } 366 367 public: 368 UdpPtr() {} 369 UdpPtr(const IpPtr &ptr) { set(ptr); } 370 UdpPtr(const UdpPtr &ptr) : p(ptr.p), off(ptr.off) {} 371 372 UdpHdr *operator->() { return h(); } 373 UdpHdr &operator*() { return *h(); } 374 operator UdpHdr *() { return h(); } 375 376 const UdpHdr *operator->() const { return h(); } 377 const UdpHdr &operator*() const { return *h(); } 378 operator const UdpHdr *() const { return h(); } 379 380 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; } 381 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t.off); return *this; } 382 383 const PacketPtr packet() const { return p; } 384 PacketPtr packet() { return p; } 385 bool operator!() const { return !p; } 386 operator bool() const { return p; } 387 operator bool() { return p; } 388}; 389 390uint16_t cksum(const UdpPtr &ptr); 391 392/* namespace Net */ } 393 394#endif // __BASE_INET_HH__ 395