inet.hh revision 5782
12SN/A/* 21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 321078SN/A#ifndef __BASE_INET_HH__ 331078SN/A#define __BASE_INET_HH__ 341078SN/A 351114SN/A#include <iosfwd> 361078SN/A#include <string> 371114SN/A#include <utility> 381114SN/A#include <vector> 391114SN/A 401114SN/A#include "base/range.hh" 411114SN/A#include "dev/etherpkt.hh" 421114SN/A#include "sim/host.hh" 431078SN/A 441078SN/A#include "dnet/os.h" 451078SN/A#include "dnet/eth.h" 461078SN/A#include "dnet/ip.h" 471078SN/A#include "dnet/ip6.h" 481078SN/A#include "dnet/addr.h" 491078SN/A#include "dnet/arp.h" 501078SN/A#include "dnet/icmp.h" 511078SN/A#include "dnet/tcp.h" 521078SN/A#include "dnet/udp.h" 531078SN/A#include "dnet/intf.h" 541078SN/A#include "dnet/route.h" 551078SN/A#include "dnet/fw.h" 561078SN/A#include "dnet/blob.h" 571078SN/A#include "dnet/rand.h" 582SN/A 591114SN/Anamespace Net { 602SN/A 611114SN/A/* 621114SN/A * Ethernet Stuff 631114SN/A */ 641114SN/Astruct EthAddr : protected eth_addr 651114SN/A{ 661114SN/A protected: 671114SN/A void parse(const std::string &addr); 681078SN/A 691114SN/A public: 701114SN/A EthAddr(); 711114SN/A EthAddr(const uint8_t ea[ETH_ADDR_LEN]); 721114SN/A EthAddr(const eth_addr &ea); 731114SN/A EthAddr(const std::string &addr); 741114SN/A const EthAddr &operator=(const eth_addr &ea); 751114SN/A const EthAddr &operator=(const std::string &addr); 761079SN/A 771114SN/A int size() const { return sizeof(eth_addr); } 781114SN/A 791114SN/A const uint8_t *bytes() const { return &data[0]; } 801114SN/A uint8_t *bytes() { return &data[0]; } 811114SN/A 821114SN/A const uint8_t *addr() const { return &data[0]; } 831114SN/A bool unicast() const { return data[0] == 0x00; } 841114SN/A bool multicast() const { return data[0] == 0x01; } 851114SN/A bool broadcast() const { return data[0] == 0xff; } 861114SN/A std::string string() const; 871137SN/A 881137SN/A operator uint64_t() const 891137SN/A { 901137SN/A uint64_t reg = 0; 911137SN/A reg |= ((uint64_t)data[0]) << 40; 921137SN/A reg |= ((uint64_t)data[1]) << 32; 931137SN/A reg |= ((uint64_t)data[2]) << 24; 941137SN/A reg |= ((uint64_t)data[3]) << 16; 951137SN/A reg |= ((uint64_t)data[4]) << 8; 961137SN/A reg |= ((uint64_t)data[5]) << 0; 971137SN/A return reg; 981137SN/A } 991137SN/A 1001114SN/A}; 1011114SN/A 1021114SN/Astd::ostream &operator<<(std::ostream &stream, const EthAddr &ea); 1031114SN/Abool operator==(const EthAddr &left, const EthAddr &right); 1041114SN/A 1051114SN/Astruct EthHdr : public eth_hdr 1061078SN/A{ 1071078SN/A uint16_t type() const { return ntohs(eth_type); } 1081114SN/A const EthAddr &src() const { return *(EthAddr *)ð_src; } 1091114SN/A const EthAddr &dst() const { return *(EthAddr *)ð_dst; } 1101079SN/A 1111114SN/A int size() const { return sizeof(eth_hdr); } 1121079SN/A 1131079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 1141079SN/A const uint8_t *payload() const { return bytes() + size(); } 1151079SN/A uint8_t *bytes() { return (uint8_t *)this; } 1161079SN/A uint8_t *payload() { return bytes() + size(); } 1171078SN/A}; 1181078SN/A 1191114SN/Aclass EthPtr 1201114SN/A{ 1211114SN/A protected: 1221114SN/A friend class IpPtr; 1232566SN/A EthPacketPtr p; 1241114SN/A 1251114SN/A public: 1261114SN/A EthPtr() {} 1272566SN/A EthPtr(const EthPacketPtr &ptr) : p(ptr) { } 1281114SN/A 1291114SN/A EthHdr *operator->() { return (EthHdr *)p->data; } 1301114SN/A EthHdr &operator*() { return *(EthHdr *)p->data; } 1311114SN/A operator EthHdr *() { return (EthHdr *)p->data; } 1321114SN/A 1331114SN/A const EthHdr *operator->() const { return (const EthHdr *)p->data; } 1341114SN/A const EthHdr &operator*() const { return *(const EthHdr *)p->data; } 1351114SN/A operator const EthHdr *() const { return (const EthHdr *)p->data; } 1361114SN/A 1372566SN/A const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; } 1381114SN/A 1392566SN/A const EthPacketPtr packet() const { return p; } 1402566SN/A EthPacketPtr packet() { return p; } 1411114SN/A bool operator!() const { return !p; } 1421114SN/A operator bool() const { return p; } 1435782Ssaidi@eecs.umich.edu int off() const { return 0; } 1445782Ssaidi@eecs.umich.edu int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } 1451114SN/A}; 1461114SN/A 1471114SN/A/* 1481114SN/A * IP Stuff 1491114SN/A */ 1501114SN/Astruct IpOpt; 1511114SN/Astruct IpHdr : public ip_hdr 1521078SN/A{ 1531078SN/A uint8_t version() const { return ip_v; } 1541092SN/A uint8_t hlen() const { return ip_hl * 4; } 1551078SN/A uint8_t tos() const { return ip_tos; } 1561078SN/A uint16_t len() const { return ntohs(ip_len); } 1571078SN/A uint16_t id() const { return ntohs(ip_id); } 1581078SN/A uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 1591078SN/A uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 1601078SN/A uint8_t ttl() const { return ip_ttl; } 1611078SN/A uint8_t proto() const { return ip_p; } 1621092SN/A uint16_t sum() const { return ip_sum; } 1631078SN/A uint32_t src() const { return ntohl(ip_src); } 1641078SN/A uint32_t dst() const { return ntohl(ip_dst); } 1651078SN/A 1661092SN/A void sum(uint16_t sum) { ip_sum = sum; } 1675761Ssaidi@eecs.umich.edu void id(uint16_t _id) { ip_id = htons(_id); } 1685761Ssaidi@eecs.umich.edu void len(uint16_t _len) { ip_len = htons(_len); } 1695761Ssaidi@eecs.umich.edu 1701078SN/A 1711114SN/A bool options(std::vector<const IpOpt *> &vec) const; 1721079SN/A 1731079SN/A int size() const { return hlen(); } 1741079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 1751079SN/A const uint8_t *payload() const { return bytes() + size(); } 1761079SN/A uint8_t *bytes() { return (uint8_t *)this; } 1771079SN/A uint8_t *payload() { return bytes() + size(); } 1781078SN/A}; 1791078SN/A 1801114SN/Aclass IpPtr 1811114SN/A{ 1821114SN/A protected: 1831114SN/A friend class TcpPtr; 1841114SN/A friend class UdpPtr; 1852566SN/A EthPacketPtr p; 1861114SN/A 1872566SN/A void set(const EthPacketPtr &ptr) 1881114SN/A { 1895484Snate@binkert.org p = 0; 1905484Snate@binkert.org 1915484Snate@binkert.org if (ptr) { 1925484Snate@binkert.org EthHdr *eth = (EthHdr *)ptr->data; 1935484Snate@binkert.org if (eth->type() == ETH_TYPE_IP) 1945484Snate@binkert.org p = ptr; 1955484Snate@binkert.org } 1961114SN/A } 1971114SN/A 1981114SN/A public: 1995484Snate@binkert.org IpPtr() : p(0) {} 2005484Snate@binkert.org IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); } 2015484Snate@binkert.org IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); } 2021114SN/A IpPtr(const IpPtr &ptr) : p(ptr.p) { } 2031114SN/A 2045484Snate@binkert.org IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); } 2055484Snate@binkert.org IpHdr *operator->() { return get(); } 2065484Snate@binkert.org IpHdr &operator*() { return *get(); } 2071114SN/A 2085484Snate@binkert.org const IpHdr *get() const 2095484Snate@binkert.org { return (const IpHdr *)(p->data + sizeof(eth_hdr)); } 2105484Snate@binkert.org const IpHdr *operator->() const { return get(); } 2115484Snate@binkert.org const IpHdr &operator*() const { return *get(); } 2121114SN/A 2132566SN/A const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } 2141114SN/A const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 2151114SN/A const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 2161114SN/A 2172566SN/A const EthPacketPtr packet() const { return p; } 2182566SN/A EthPacketPtr packet() { return p; } 2191114SN/A bool operator!() const { return !p; } 2201114SN/A operator bool() const { return p; } 2215782Ssaidi@eecs.umich.edu int off() const { return sizeof(eth_hdr); } 2225782Ssaidi@eecs.umich.edu int pstart() const { return off() + get()->size(); } 2231114SN/A}; 2241114SN/A 2251114SN/Auint16_t cksum(const IpPtr &ptr); 2261114SN/A 2271114SN/Astruct IpOpt : public ip_opt 2281114SN/A{ 2291114SN/A uint8_t type() const { return opt_type; } 2301114SN/A uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 2311114SN/A uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 2321114SN/A uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 2331114SN/A uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 2341114SN/A 2351114SN/A bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 2361114SN/A bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 2371114SN/A bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 2381114SN/A 2391114SN/A const uint8_t *data() const { return opt_data.data8; } 2401114SN/A void sec(ip_opt_data_sec &sec) const; 2411114SN/A void lsrr(ip_opt_data_rr &rr) const; 2421114SN/A void ssrr(ip_opt_data_rr &rr) const; 2431114SN/A void ts(ip_opt_data_ts &ts) const; 2441114SN/A uint16_t satid() const { return ntohs(opt_data.satid); } 2451114SN/A uint16_t mtup() const { return ntohs(opt_data.mtu); } 2461114SN/A uint16_t mtur() const { return ntohs(opt_data.mtu); } 2471114SN/A void tr(ip_opt_data_tr &tr) const; 2481114SN/A const uint32_t *addext() const { return &opt_data.addext[0]; } 2491114SN/A uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 2501114SN/A void sdb(std::vector<uint32_t> &vec) const; 2511114SN/A}; 2521114SN/A 2531114SN/A/* 2541114SN/A * TCP Stuff 2551114SN/A */ 2561114SN/Astruct TcpOpt; 2571114SN/Astruct TcpHdr : public tcp_hdr 2581078SN/A{ 2591078SN/A uint16_t sport() const { return ntohs(th_sport); } 2601078SN/A uint16_t dport() const { return ntohs(th_dport); } 2611078SN/A uint32_t seq() const { return ntohl(th_seq); } 2621078SN/A uint32_t ack() const { return ntohl(th_ack); } 2631078SN/A uint8_t off() const { return th_off; } 2641078SN/A uint8_t flags() const { return th_flags & 0x3f; } 2651078SN/A uint16_t win() const { return ntohs(th_win); } 2661092SN/A uint16_t sum() const { return th_sum; } 2671078SN/A uint16_t urp() const { return ntohs(th_urp); } 2681078SN/A 2691092SN/A void sum(uint16_t sum) { th_sum = sum; } 2705761Ssaidi@eecs.umich.edu void seq(uint32_t _seq) { th_seq = htonl(_seq); } 2715761Ssaidi@eecs.umich.edu void flags(uint8_t _flags) { th_flags = _flags; } 2721078SN/A 2731114SN/A bool options(std::vector<const TcpOpt *> &vec) const; 2741114SN/A 2751079SN/A int size() const { return off(); } 2761079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 2771079SN/A const uint8_t *payload() const { return bytes() + size(); } 2781079SN/A uint8_t *bytes() { return (uint8_t *)this; } 2791079SN/A uint8_t *payload() { return bytes() + size(); } 2801078SN/A}; 2811078SN/A 2821114SN/Aclass TcpPtr 2831114SN/A{ 2841114SN/A protected: 2852566SN/A EthPacketPtr p; 2865782Ssaidi@eecs.umich.edu int _off; 2871114SN/A 2885782Ssaidi@eecs.umich.edu void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 2891114SN/A void set(const IpPtr &ptr) 2901114SN/A { 2915484Snate@binkert.org if (ptr && ptr->proto() == IP_PROTO_TCP) 2921114SN/A set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 2931114SN/A else 2941114SN/A set(0, 0); 2951114SN/A } 2961114SN/A 2971114SN/A public: 2985782Ssaidi@eecs.umich.edu TcpPtr() : p(0), _off(0) {} 2995782Ssaidi@eecs.umich.edu TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); } 3005782Ssaidi@eecs.umich.edu TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 3011114SN/A 3025782Ssaidi@eecs.umich.edu TcpHdr *get() { return (TcpHdr *)(p->data + _off); } 3035484Snate@binkert.org TcpHdr *operator->() { return get(); } 3045484Snate@binkert.org TcpHdr &operator*() { return *get(); } 3051114SN/A 3065782Ssaidi@eecs.umich.edu const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); } 3075484Snate@binkert.org const TcpHdr *operator->() const { return get(); } 3085484Snate@binkert.org const TcpHdr &operator*() const { return *get(); } 3091114SN/A 3101114SN/A const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; } 3115782Ssaidi@eecs.umich.edu const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; } 3121114SN/A 3132566SN/A const EthPacketPtr packet() const { return p; } 3142566SN/A EthPacketPtr packet() { return p; } 3151114SN/A bool operator!() const { return !p; } 3161114SN/A operator bool() const { return p; } 3175782Ssaidi@eecs.umich.edu int off() const { return _off; } 3185782Ssaidi@eecs.umich.edu int pstart() const { return off() + get()->size(); } 3191114SN/A}; 3201114SN/A 3211114SN/Auint16_t cksum(const TcpPtr &ptr); 3221114SN/A 3231114SN/Atypedef Range<uint16_t> SackRange; 3241114SN/A 3251114SN/Astruct TcpOpt : public tcp_opt 3261114SN/A{ 3271114SN/A uint8_t type() const { return opt_type; } 3281114SN/A uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 3291114SN/A 3301114SN/A bool isopt(int opt) const { return type() == opt; } 3311114SN/A 3321114SN/A const uint8_t *data() const { return opt_data.data8; } 3331114SN/A 3341114SN/A uint16_t mss() const { return ntohs(opt_data.mss); } 3351114SN/A uint8_t wscale() const { return opt_data.wscale; } 3361114SN/A bool sack(std::vector<SackRange> &vec) const; 3371114SN/A uint32_t echo() const { return ntohl(opt_data.echo); } 3381114SN/A uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 3391114SN/A uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 3401114SN/A uint32_t cc() const { return ntohl(opt_data.cc); } 3411114SN/A uint8_t cksum() const{ return opt_data.cksum; } 3421114SN/A const uint8_t *md5() const { return opt_data.md5; } 3431114SN/A 3441114SN/A int size() const { return len(); } 3451114SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 3461114SN/A const uint8_t *payload() const { return bytes() + size(); } 3471114SN/A uint8_t *bytes() { return (uint8_t *)this; } 3481114SN/A uint8_t *payload() { return bytes() + size(); } 3491114SN/A}; 3501114SN/A 3511114SN/A/* 3521114SN/A * UDP Stuff 3531114SN/A */ 3541114SN/Astruct UdpHdr : public udp_hdr 3551078SN/A{ 3561078SN/A uint16_t sport() const { return ntohs(uh_sport); } 3571078SN/A uint16_t dport() const { return ntohs(uh_dport); } 3581078SN/A uint16_t len() const { return ntohs(uh_ulen); } 3591171SN/A uint16_t sum() const { return uh_sum; } 3601078SN/A 3611171SN/A void sum(uint16_t sum) { uh_sum = sum; } 3625761Ssaidi@eecs.umich.edu void len(uint16_t _len) { uh_ulen = htons(_len); } 3631078SN/A 3641114SN/A int size() const { return sizeof(udp_hdr); } 3651079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 3661079SN/A const uint8_t *payload() const { return bytes() + size(); } 3671079SN/A uint8_t *bytes() { return (uint8_t *)this; } 3681079SN/A uint8_t *payload() { return bytes() + size(); } 3691078SN/A}; 3701078SN/A 3711114SN/Aclass UdpPtr 3721114SN/A{ 3731114SN/A protected: 3742566SN/A EthPacketPtr p; 3755782Ssaidi@eecs.umich.edu int _off; 3761114SN/A 3775782Ssaidi@eecs.umich.edu void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } 3781114SN/A void set(const IpPtr &ptr) 3791114SN/A { 3805484Snate@binkert.org if (ptr && ptr->proto() == IP_PROTO_UDP) 3811114SN/A set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 3821114SN/A else 3831114SN/A set(0, 0); 3841114SN/A } 3851114SN/A 3861114SN/A public: 3875782Ssaidi@eecs.umich.edu UdpPtr() : p(0), _off(0) {} 3885782Ssaidi@eecs.umich.edu UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); } 3895782Ssaidi@eecs.umich.edu UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {} 3901114SN/A 3915782Ssaidi@eecs.umich.edu UdpHdr *get() { return (UdpHdr *)(p->data + _off); } 3925484Snate@binkert.org UdpHdr *operator->() { return get(); } 3935484Snate@binkert.org UdpHdr &operator*() { return *get(); } 3941114SN/A 3955782Ssaidi@eecs.umich.edu const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); } 3965484Snate@binkert.org const UdpHdr *operator->() const { return get(); } 3975484Snate@binkert.org const UdpHdr &operator*() const { return *get(); } 3981114SN/A 3991114SN/A const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; } 4005782Ssaidi@eecs.umich.edu const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; } 4011114SN/A 4022566SN/A const EthPacketPtr packet() const { return p; } 4032566SN/A EthPacketPtr packet() { return p; } 4041114SN/A bool operator!() const { return !p; } 4051114SN/A operator bool() const { return p; } 4065782Ssaidi@eecs.umich.edu int off() const { return _off; } 4075782Ssaidi@eecs.umich.edu int pstart() const { return off() + get()->size(); } 4081114SN/A}; 4091114SN/A 4101114SN/Auint16_t cksum(const UdpPtr &ptr); 4111114SN/A 4125782Ssaidi@eecs.umich.eduint hsplit(const EthPacketPtr &ptr); 4135782Ssaidi@eecs.umich.edu 4141114SN/A/* namespace Net */ } 4151114SN/A 4161078SN/A#endif // __BASE_INET_HH__ 417