inet.hh revision 5484
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; } 1431114SN/A}; 1441114SN/A 1451114SN/A/* 1461114SN/A * IP Stuff 1471114SN/A */ 1481114SN/Astruct IpOpt; 1491114SN/Astruct IpHdr : public ip_hdr 1501078SN/A{ 1511078SN/A uint8_t version() const { return ip_v; } 1521092SN/A uint8_t hlen() const { return ip_hl * 4; } 1531078SN/A uint8_t tos() const { return ip_tos; } 1541078SN/A uint16_t len() const { return ntohs(ip_len); } 1551078SN/A uint16_t id() const { return ntohs(ip_id); } 1561078SN/A uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 1571078SN/A uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 1581078SN/A uint8_t ttl() const { return ip_ttl; } 1591078SN/A uint8_t proto() const { return ip_p; } 1601092SN/A uint16_t sum() const { return ip_sum; } 1611078SN/A uint32_t src() const { return ntohl(ip_src); } 1621078SN/A uint32_t dst() const { return ntohl(ip_dst); } 1631078SN/A 1641092SN/A void sum(uint16_t sum) { ip_sum = sum; } 1651078SN/A 1661114SN/A bool options(std::vector<const IpOpt *> &vec) const; 1671079SN/A 1681079SN/A int size() const { return hlen(); } 1691079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 1701079SN/A const uint8_t *payload() const { return bytes() + size(); } 1711079SN/A uint8_t *bytes() { return (uint8_t *)this; } 1721079SN/A uint8_t *payload() { return bytes() + size(); } 1731078SN/A}; 1741078SN/A 1751114SN/Aclass IpPtr 1761114SN/A{ 1771114SN/A protected: 1781114SN/A friend class TcpPtr; 1791114SN/A friend class UdpPtr; 1802566SN/A EthPacketPtr p; 1811114SN/A 1822566SN/A void set(const EthPacketPtr &ptr) 1831114SN/A { 1845484Snate@binkert.org p = 0; 1855484Snate@binkert.org 1865484Snate@binkert.org if (ptr) { 1875484Snate@binkert.org EthHdr *eth = (EthHdr *)ptr->data; 1885484Snate@binkert.org if (eth->type() == ETH_TYPE_IP) 1895484Snate@binkert.org p = ptr; 1905484Snate@binkert.org } 1911114SN/A } 1921114SN/A 1931114SN/A public: 1945484Snate@binkert.org IpPtr() : p(0) {} 1955484Snate@binkert.org IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); } 1965484Snate@binkert.org IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); } 1971114SN/A IpPtr(const IpPtr &ptr) : p(ptr.p) { } 1981114SN/A 1995484Snate@binkert.org IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); } 2005484Snate@binkert.org IpHdr *operator->() { return get(); } 2015484Snate@binkert.org IpHdr &operator*() { return *get(); } 2021114SN/A 2035484Snate@binkert.org const IpHdr *get() const 2045484Snate@binkert.org { return (const IpHdr *)(p->data + sizeof(eth_hdr)); } 2055484Snate@binkert.org const IpHdr *operator->() const { return get(); } 2065484Snate@binkert.org const IpHdr &operator*() const { return *get(); } 2071114SN/A 2082566SN/A const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } 2091114SN/A const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } 2101114SN/A const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } 2111114SN/A 2122566SN/A const EthPacketPtr packet() const { return p; } 2132566SN/A EthPacketPtr packet() { return p; } 2141114SN/A bool operator!() const { return !p; } 2151114SN/A operator bool() const { return p; } 2161114SN/A}; 2171114SN/A 2181114SN/Auint16_t cksum(const IpPtr &ptr); 2191114SN/A 2201114SN/Astruct IpOpt : public ip_opt 2211114SN/A{ 2221114SN/A uint8_t type() const { return opt_type; } 2231114SN/A uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } 2241114SN/A uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } 2251114SN/A uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } 2261114SN/A uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 2271114SN/A 2281114SN/A bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } 2291114SN/A bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } 2301114SN/A bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } 2311114SN/A 2321114SN/A const uint8_t *data() const { return opt_data.data8; } 2331114SN/A void sec(ip_opt_data_sec &sec) const; 2341114SN/A void lsrr(ip_opt_data_rr &rr) const; 2351114SN/A void ssrr(ip_opt_data_rr &rr) const; 2361114SN/A void ts(ip_opt_data_ts &ts) const; 2371114SN/A uint16_t satid() const { return ntohs(opt_data.satid); } 2381114SN/A uint16_t mtup() const { return ntohs(opt_data.mtu); } 2391114SN/A uint16_t mtur() const { return ntohs(opt_data.mtu); } 2401114SN/A void tr(ip_opt_data_tr &tr) const; 2411114SN/A const uint32_t *addext() const { return &opt_data.addext[0]; } 2421114SN/A uint16_t rtralt() const { return ntohs(opt_data.rtralt); } 2431114SN/A void sdb(std::vector<uint32_t> &vec) const; 2441114SN/A}; 2451114SN/A 2461114SN/A/* 2471114SN/A * TCP Stuff 2481114SN/A */ 2491114SN/Astruct TcpOpt; 2501114SN/Astruct TcpHdr : public tcp_hdr 2511078SN/A{ 2521078SN/A uint16_t sport() const { return ntohs(th_sport); } 2531078SN/A uint16_t dport() const { return ntohs(th_dport); } 2541078SN/A uint32_t seq() const { return ntohl(th_seq); } 2551078SN/A uint32_t ack() const { return ntohl(th_ack); } 2561078SN/A uint8_t off() const { return th_off; } 2571078SN/A uint8_t flags() const { return th_flags & 0x3f; } 2581078SN/A uint16_t win() const { return ntohs(th_win); } 2591092SN/A uint16_t sum() const { return th_sum; } 2601078SN/A uint16_t urp() const { return ntohs(th_urp); } 2611078SN/A 2621092SN/A void sum(uint16_t sum) { th_sum = sum; } 2631078SN/A 2641114SN/A bool options(std::vector<const TcpOpt *> &vec) const; 2651114SN/A 2661079SN/A int size() const { return off(); } 2671079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 2681079SN/A const uint8_t *payload() const { return bytes() + size(); } 2691079SN/A uint8_t *bytes() { return (uint8_t *)this; } 2701079SN/A uint8_t *payload() { return bytes() + size(); } 2711078SN/A}; 2721078SN/A 2731114SN/Aclass TcpPtr 2741114SN/A{ 2751114SN/A protected: 2762566SN/A EthPacketPtr p; 2771114SN/A int off; 2781114SN/A 2792566SN/A void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; } 2801114SN/A void set(const IpPtr &ptr) 2811114SN/A { 2825484Snate@binkert.org if (ptr && ptr->proto() == IP_PROTO_TCP) 2831114SN/A set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 2841114SN/A else 2851114SN/A set(0, 0); 2861114SN/A } 2871114SN/A 2881114SN/A public: 2895484Snate@binkert.org TcpPtr() : p(0), off(0) {} 2905484Snate@binkert.org TcpPtr(const IpPtr &ptr) : p(0), off(0) { set(ptr); } 2911114SN/A TcpPtr(const TcpPtr &ptr) : p(ptr.p), off(ptr.off) {} 2921114SN/A 2935484Snate@binkert.org TcpHdr *get() { return (TcpHdr *)(p->data + off); } 2945484Snate@binkert.org TcpHdr *operator->() { return get(); } 2955484Snate@binkert.org TcpHdr &operator*() { return *get(); } 2961114SN/A 2975484Snate@binkert.org const TcpHdr *get() const { return (const TcpHdr *)(p->data + off); } 2985484Snate@binkert.org const TcpHdr *operator->() const { return get(); } 2995484Snate@binkert.org const TcpHdr &operator*() const { return *get(); } 3001114SN/A 3011114SN/A const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; } 3021114SN/A const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t.off); return *this; } 3031114SN/A 3042566SN/A const EthPacketPtr packet() const { return p; } 3052566SN/A EthPacketPtr packet() { return p; } 3061114SN/A bool operator!() const { return !p; } 3071114SN/A operator bool() const { return p; } 3081114SN/A}; 3091114SN/A 3101114SN/Auint16_t cksum(const TcpPtr &ptr); 3111114SN/A 3121114SN/Atypedef Range<uint16_t> SackRange; 3131114SN/A 3141114SN/Astruct TcpOpt : public tcp_opt 3151114SN/A{ 3161114SN/A uint8_t type() const { return opt_type; } 3171114SN/A uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } 3181114SN/A 3191114SN/A bool isopt(int opt) const { return type() == opt; } 3201114SN/A 3211114SN/A const uint8_t *data() const { return opt_data.data8; } 3221114SN/A 3231114SN/A uint16_t mss() const { return ntohs(opt_data.mss); } 3241114SN/A uint8_t wscale() const { return opt_data.wscale; } 3251114SN/A bool sack(std::vector<SackRange> &vec) const; 3261114SN/A uint32_t echo() const { return ntohl(opt_data.echo); } 3271114SN/A uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } 3281114SN/A uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } 3291114SN/A uint32_t cc() const { return ntohl(opt_data.cc); } 3301114SN/A uint8_t cksum() const{ return opt_data.cksum; } 3311114SN/A const uint8_t *md5() const { return opt_data.md5; } 3321114SN/A 3331114SN/A int size() const { return len(); } 3341114SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 3351114SN/A const uint8_t *payload() const { return bytes() + size(); } 3361114SN/A uint8_t *bytes() { return (uint8_t *)this; } 3371114SN/A uint8_t *payload() { return bytes() + size(); } 3381114SN/A}; 3391114SN/A 3401114SN/A/* 3411114SN/A * UDP Stuff 3421114SN/A */ 3431114SN/Astruct UdpHdr : public udp_hdr 3441078SN/A{ 3451078SN/A uint16_t sport() const { return ntohs(uh_sport); } 3461078SN/A uint16_t dport() const { return ntohs(uh_dport); } 3471078SN/A uint16_t len() const { return ntohs(uh_ulen); } 3481171SN/A uint16_t sum() const { return uh_sum; } 3491078SN/A 3501171SN/A void sum(uint16_t sum) { uh_sum = sum; } 3511078SN/A 3521114SN/A int size() const { return sizeof(udp_hdr); } 3531079SN/A const uint8_t *bytes() const { return (const uint8_t *)this; } 3541079SN/A const uint8_t *payload() const { return bytes() + size(); } 3551079SN/A uint8_t *bytes() { return (uint8_t *)this; } 3561079SN/A uint8_t *payload() { return bytes() + size(); } 3571078SN/A}; 3581078SN/A 3591114SN/Aclass UdpPtr 3601114SN/A{ 3611114SN/A protected: 3622566SN/A EthPacketPtr p; 3631114SN/A int off; 3641114SN/A 3652566SN/A void set(const EthPacketPtr &ptr, int offset) { p = ptr; off = offset; } 3661114SN/A void set(const IpPtr &ptr) 3671114SN/A { 3685484Snate@binkert.org if (ptr && ptr->proto() == IP_PROTO_UDP) 3691114SN/A set(ptr.p, sizeof(eth_hdr) + ptr->hlen()); 3701114SN/A else 3711114SN/A set(0, 0); 3721114SN/A } 3731114SN/A 3741114SN/A public: 3755484Snate@binkert.org UdpPtr() : p(0), off(0) {} 3765484Snate@binkert.org UdpPtr(const IpPtr &ptr) : p(0), off(0) { set(ptr); } 3771114SN/A UdpPtr(const UdpPtr &ptr) : p(ptr.p), off(ptr.off) {} 3781114SN/A 3795484Snate@binkert.org UdpHdr *get() { return (UdpHdr *)(p->data + off); } 3805484Snate@binkert.org UdpHdr *operator->() { return get(); } 3815484Snate@binkert.org UdpHdr &operator*() { return *get(); } 3821114SN/A 3835484Snate@binkert.org const UdpHdr *get() const { return (const UdpHdr *)(p->data + off); } 3845484Snate@binkert.org const UdpHdr *operator->() const { return get(); } 3855484Snate@binkert.org const UdpHdr &operator*() const { return *get(); } 3861114SN/A 3871114SN/A const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; } 3881114SN/A const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t.off); return *this; } 3891114SN/A 3902566SN/A const EthPacketPtr packet() const { return p; } 3912566SN/A EthPacketPtr packet() { return p; } 3921114SN/A bool operator!() const { return !p; } 3931114SN/A operator bool() const { return p; } 3941114SN/A}; 3951114SN/A 3961114SN/Auint16_t cksum(const UdpPtr &ptr); 3971114SN/A 3981114SN/A/* namespace Net */ } 3991114SN/A 4001078SN/A#endif // __BASE_INET_HH__ 401