inet.hh revision 10469
11689SN/A/*
28707Sandreas.hansson@arm.com * Copyright (c) 2013 ARM Limited
37849SAli.Saidi@ARM.com * All rights reserved
47849SAli.Saidi@ARM.com *
57849SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
67849SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
77849SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
87849SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
97849SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
107849SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
117849SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
127849SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
137849SAli.Saidi@ARM.com *
142329SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
151689SN/A * Copyright (c) 2010 Advanced Micro Devices, Inc.
161689SN/A * All rights reserved.
171689SN/A *
181689SN/A * Redistribution and use in source and binary forms, with or without
191689SN/A * modification, are permitted provided that the following conditions are
201689SN/A * met: redistributions of source code must retain the above copyright
211689SN/A * notice, this list of conditions and the following disclaimer;
221689SN/A * redistributions in binary form must reproduce the above copyright
231689SN/A * notice, this list of conditions and the following disclaimer in the
241689SN/A * documentation and/or other materials provided with the distribution;
251689SN/A * neither the name of the copyright holders nor the names of its
261689SN/A * contributors may be used to endorse or promote products derived from
271689SN/A * this software without specific prior written permission.
281689SN/A *
291689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
301689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
311689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
321689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
331689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
341689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
351689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
361689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
371689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
381689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392665Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412756Sksewell@umich.edu * Authors: Nathan Binkert
421689SN/A *          Steve Reinhardt
431689SN/A *          Gabe Black
442292SN/A *          Geoffrey Blake
452292SN/A */
461060SN/A
479020Sgblack@eecs.umich.edu#ifndef __BASE_INET_HH__
482669Sktlim@umich.edu#define __BASE_INET_HH__
491461SN/A
506658Snate@binkert.org#include <iosfwd>
511060SN/A#include <string>
528229Snate@binkert.org#include <utility>
537849SAli.Saidi@ARM.com#include <vector>
543348Sbinkertn@umich.edu
552669Sktlim@umich.edu#include "base/types.hh"
561461SN/A#include "dev/etherpkt.hh"
571060SN/A#include "dnet/os.h"
588737Skoansin.tan@gmail.com#include "dnet/eth.h"
595529Snate@binkert.org#include "dnet/ip.h"
601060SN/A#include "dnet/ip6.h"
612329SN/A#include "dnet/addr.h"
622329SN/A#include "dnet/arp.h"
632329SN/A#include "dnet/icmp.h"
642329SN/A#include "dnet/tcp.h"
652348SN/A#include "dnet/udp.h"
662329SN/A#include "dnet/intf.h"
671060SN/A#include "dnet/route.h"
681060SN/A#include "dnet/fw.h"
692292SN/A#include "dnet/blob.h"
701060SN/A#include "dnet/rand.h"
711060SN/A
721060SN/Anamespace Net {
731061SN/A
741060SN/A/*
751061SN/A * Ethernet Stuff
762733Sktlim@umich.edu */
771060SN/Astruct EthAddr : protected eth_addr
782292SN/A{
791061SN/A  protected:
801061SN/A    void parse(const std::string &addr);
811061SN/A
821060SN/A  public:
831060SN/A    EthAddr();
842107SN/A    EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
852292SN/A    EthAddr(const eth_addr &ea);
862632Sstever@eecs.umich.edu    EthAddr(const std::string &addr);
877849SAli.Saidi@ARM.com    const EthAddr &operator=(const eth_addr &ea);
887849SAli.Saidi@ARM.com    const EthAddr &operator=(const std::string &addr);
897849SAli.Saidi@ARM.com
907849SAli.Saidi@ARM.com    int size() const { return sizeof(eth_addr); }
917849SAli.Saidi@ARM.com
927849SAli.Saidi@ARM.com    const uint8_t *bytes() const { return &data[0]; }
937849SAli.Saidi@ARM.com    uint8_t *bytes() { return &data[0]; }
947849SAli.Saidi@ARM.com
957849SAli.Saidi@ARM.com    const uint8_t *addr() const { return &data[0]; }
967849SAli.Saidi@ARM.com    bool unicast() const { return !(data[0] & 0x01); }
977849SAli.Saidi@ARM.com    bool multicast() const { return !unicast() && !broadcast(); }
987944SGiacomo.Gabrielli@arm.com    bool broadcast() const
997944SGiacomo.Gabrielli@arm.com    {
1007944SGiacomo.Gabrielli@arm.com        bool isBroadcast = true;
1017944SGiacomo.Gabrielli@arm.com        for (int i = 0; i < ETH_ADDR_LEN; ++i) {
1027849SAli.Saidi@ARM.com            isBroadcast = isBroadcast && data[i] == 0xff;
1037849SAli.Saidi@ARM.com        }
1047849SAli.Saidi@ARM.com
1057849SAli.Saidi@ARM.com        return isBroadcast;
1067849SAli.Saidi@ARM.com    }
1077849SAli.Saidi@ARM.com
1087849SAli.Saidi@ARM.com    std::string string() const;
1097849SAli.Saidi@ARM.com
1102935Sksewell@umich.edu    operator uint64_t() const
1118462Sgeoffrey.blake@arm.com    {
1128462Sgeoffrey.blake@arm.com        uint64_t reg = 0;
1138462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[0]) << 40;
1148462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[1]) << 32;
1158462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[2]) << 24;
1168462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[3]) << 16;
1178462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[4]) << 8;
1188462Sgeoffrey.blake@arm.com        reg |= ((uint64_t)data[5]) << 0;
1198462Sgeoffrey.blake@arm.com        return reg;
1208462Sgeoffrey.blake@arm.com    }
1218462Sgeoffrey.blake@arm.com
1228462Sgeoffrey.blake@arm.com};
1238462Sgeoffrey.blake@arm.com
1248462Sgeoffrey.blake@arm.comstd::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
1258462Sgeoffrey.blake@arm.combool operator==(const EthAddr &left, const EthAddr &right);
1268462Sgeoffrey.blake@arm.com
1278462Sgeoffrey.blake@arm.comstruct EthHdr : public eth_hdr
1288462Sgeoffrey.blake@arm.com{
1298462Sgeoffrey.blake@arm.com    bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); }
1308462Sgeoffrey.blake@arm.com    uint16_t type() const {
1318462Sgeoffrey.blake@arm.com        if (!isVlan())
1328462Sgeoffrey.blake@arm.com            return ntohs(eth_type);
1338462Sgeoffrey.blake@arm.com        else
1348462Sgeoffrey.blake@arm.com            // L3 type is now 16 bytes into the hdr with 802.1Q
1358462Sgeoffrey.blake@arm.com            // instead of 12.  dnet/eth.h only supports 802.1
1368462Sgeoffrey.blake@arm.com            return ntohs(*((uint16_t*)(((uint8_t *)this) + 16)));
1378462Sgeoffrey.blake@arm.com    }
1388462Sgeoffrey.blake@arm.com    uint16_t vlanId() const {
1398462Sgeoffrey.blake@arm.com        if (isVlan())
1408462Sgeoffrey.blake@arm.com            return ntohs(*((uint16_t*)(((uint8_t *)this) + 14)));
1418462Sgeoffrey.blake@arm.com        else
1428462Sgeoffrey.blake@arm.com            return 0x0000;
1438462Sgeoffrey.blake@arm.com    }
1448462Sgeoffrey.blake@arm.com
1458462Sgeoffrey.blake@arm.com    const EthAddr &src() const { return *(EthAddr *)&eth_src; }
1468462Sgeoffrey.blake@arm.com    const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
1478462Sgeoffrey.blake@arm.com
1488462Sgeoffrey.blake@arm.com    int size() const {
1498462Sgeoffrey.blake@arm.com        if (!isVlan())
1501060SN/A            return sizeof(eth_hdr);
1512329SN/A        else
1522329SN/A            return (sizeof(eth_hdr)+4);
1532292SN/A    }
1542292SN/A
1552292SN/A    const uint8_t *bytes() const { return (const uint8_t *)this; }
1562292SN/A    const uint8_t *payload() const { return bytes() + size(); }
1572292SN/A    uint8_t *bytes() { return (uint8_t *)this; }
1582292SN/A    uint8_t *payload() { return bytes() + size(); }
1592292SN/A};
1602292SN/A
1611060SN/Aclass EthPtr
1621060SN/A{
1631060SN/A  protected:
1641060SN/A    friend class IpPtr;
1652292SN/A    friend class Ip6Ptr;
1662292SN/A    EthPacketPtr p;
1672292SN/A
1682307SN/A  public:
1697849SAli.Saidi@ARM.com    EthPtr() {}
1702669Sktlim@umich.edu    EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
1712696Sktlim@umich.edu
1728460SAli.Saidi@ARM.com    EthHdr *operator->() { return (EthHdr *)p->data; }
1738460SAli.Saidi@ARM.com    EthHdr &operator*() { return *(EthHdr *)p->data; }
1741060SN/A    operator EthHdr *() { return (EthHdr *)p->data; }
1751060SN/A
1762292SN/A    const EthHdr *operator->() const { return (const EthHdr *)p->data; }
1772292SN/A    const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
1782292SN/A    operator const EthHdr *() const { return (const EthHdr *)p->data; }
1792292SN/A
1802292SN/A    const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
1812292SN/A
1822292SN/A    const EthPacketPtr packet() const { return p; }
1832292SN/A    EthPacketPtr packet() { return p; }
1841060SN/A    bool operator!() const { return !p; }
1852292SN/A    operator bool() const { return (p != nullptr); }
1862292SN/A    int off() const { return 0; }
1872292SN/A    int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
1882292SN/A};
1892292SN/A
1902292SN/A/*
1912292SN/A * IP Stuff
1922292SN/A */
1932292SN/Astruct IpAddress
1942292SN/A{
1952292SN/A  protected:
1966221Snate@binkert.org    uint32_t _ip;
1971060SN/A
1981060SN/A  public:
1992292SN/A    IpAddress() : _ip(0)
2005529Snate@binkert.org    {}
2011684SN/A    IpAddress(const uint32_t __ip) : _ip(__ip)
2022292SN/A    {}
2032292SN/A
2041684SN/A    uint32_t ip() const { return _ip; }
2052292SN/A
2061062SN/A    std::string string() const;
2071062SN/A};
2082292SN/A
2091060SN/Astd::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
2101060SN/Abool operator==(const IpAddress &left, const IpAddress &right);
2112292SN/A
2126221Snate@binkert.orgstruct IpNetmask : public IpAddress
2132292SN/A{
2142292SN/A  protected:
2151060SN/A    uint8_t _netmask;
2161060SN/A
2172292SN/A  public:
2182292SN/A    IpNetmask() : IpAddress(), _netmask(0)
2192292SN/A    {}
2204302Sktlim@umich.edu    IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
2214302Sktlim@umich.edu        IpAddress(__ip), _netmask(__netmask)
2224302Sktlim@umich.edu    {}
2238707Sandreas.hansson@arm.com
2248707Sandreas.hansson@arm.com    uint8_t netmask() const { return _netmask; }
2258707Sandreas.hansson@arm.com
2262292SN/A    std::string string() const;
2272669Sktlim@umich.edu};
2282292SN/A
2292843Sktlim@umich.edustd::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
2302863Sktlim@umich.edubool operator==(const IpNetmask &left, const IpNetmask &right);
2312843Sktlim@umich.edu
2322843Sktlim@umich.edustruct IpWithPort : public IpAddress
2332843Sktlim@umich.edu{
2342843Sktlim@umich.edu  protected:
2352843Sktlim@umich.edu    uint16_t _port;
2362307SN/A
2372307SN/A  public:
2382348SN/A    IpWithPort() : IpAddress(), _port(0)
2392307SN/A    {}
2402307SN/A    IpWithPort(const uint32_t __ip, const uint16_t __port) :
2412348SN/A        IpAddress(__ip), _port(__port)
2422307SN/A    {}
2432307SN/A
2442348SN/A    uint8_t port() const { return _port; }
2452292SN/A
2461060SN/A    std::string string() const;
2471061SN/A};
2482329SN/A
2492329SN/Astd::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
2502292SN/Abool operator==(const IpWithPort &left, const IpWithPort &right);
2512292SN/A
2522292SN/Astruct IpOpt;
2532329SN/Astruct IpHdr : public ip_hdr
2542329SN/A{
2552292SN/A    uint8_t  version() const { return ip_v; }
2562292SN/A    uint8_t  hlen() const { return ip_hl * 4; }
2572292SN/A    uint8_t  tos() const { return ip_tos; }
2581061SN/A    uint16_t len() const { return ntohs(ip_len); }
2591061SN/A    uint16_t id() const { return ntohs(ip_id); }
2601061SN/A    uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
2611763SN/A    uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
2621061SN/A    uint8_t  ttl() const { return ip_ttl; }
2631061SN/A    uint8_t  proto() const { return ip_p; }
2642935Sksewell@umich.edu    uint16_t sum() const { return ip_sum; }
2651061SN/A    uint32_t src() const { return ntohl(ip_src); }
2661061SN/A    uint32_t dst() const { return ntohl(ip_dst); }
2677720Sgblack@eecs.umich.edu
2681062SN/A    void sum(uint16_t sum) { ip_sum = sum; }
2691062SN/A    void id(uint16_t _id) { ip_id = htons(_id); }
2701062SN/A    void len(uint16_t _len) { ip_len = htons(_len); }
2711062SN/A
2721062SN/A    bool options(std::vector<const IpOpt *> &vec) const;
2737764Sgblack@eecs.umich.edu
2742292SN/A    int size() const { return hlen(); }
2752292SN/A    const uint8_t *bytes() const { return (const uint8_t *)this; }
2762292SN/A    const uint8_t *payload() const { return bytes() + size(); }
2777764Sgblack@eecs.umich.edu    uint8_t *bytes() { return (uint8_t *)this; }
2781062SN/A    uint8_t *payload() { return bytes() + size(); }
2791062SN/A};
2807849SAli.Saidi@ARM.com
2817849SAli.Saidi@ARM.comclass IpPtr
2821062SN/A{
2837847Sminkyu.jeong@arm.com  protected:
2847847Sminkyu.jeong@arm.com    friend class TcpPtr;
2857847Sminkyu.jeong@arm.com    friend class UdpPtr;
2867847Sminkyu.jeong@arm.com    EthPacketPtr p;
2877847Sminkyu.jeong@arm.com    bool eth_hdr_vlan;
2887847Sminkyu.jeong@arm.com
2897847Sminkyu.jeong@arm.com    void set(const EthPacketPtr &ptr)
2907847Sminkyu.jeong@arm.com    {
2917847Sminkyu.jeong@arm.com        p = 0;
2922292SN/A        eth_hdr_vlan = false;
2938503Sgblack@eecs.umich.edu
2948503Sgblack@eecs.umich.edu        if (ptr) {
2951684SN/A            EthHdr *eth = (EthHdr *)ptr->data;
2962292SN/A            if (eth->type() == ETH_TYPE_IP)
2972292SN/A                p = ptr;
2982292SN/A            if (eth->isVlan())
2997720Sgblack@eecs.umich.edu                eth_hdr_vlan = true;
3008503Sgblack@eecs.umich.edu        }
3018503Sgblack@eecs.umich.edu    }
3022292SN/A
3032292SN/A  public:
3046221Snate@binkert.org    IpPtr() : p(0), eth_hdr_vlan(false) {}
3052292SN/A    IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
3062292SN/A    IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
3072292SN/A    IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
3082292SN/A
3091684SN/A    IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) +
3101684SN/A                                   ((eth_hdr_vlan) ? 4 : 0)); }
3112292SN/A    IpHdr *operator->() { return get(); }
3122292SN/A    IpHdr &operator*() { return *get(); }
3132292SN/A
3142292SN/A    const IpHdr *get() const
3158503Sgblack@eecs.umich.edu    { return (const IpHdr *)(p->data + sizeof(eth_hdr) +
3168503Sgblack@eecs.umich.edu                            ((eth_hdr_vlan) ? 4 : 0)); }
3171684SN/A    const IpHdr *operator->() const { return get(); }
3182292SN/A    const IpHdr &operator*() const { return *get(); }
3192292SN/A
3202292SN/A    const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
3211684SN/A    const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
3221684SN/A    const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
3232292SN/A
3242292SN/A    const EthPacketPtr packet() const { return p; }
3252292SN/A    EthPacketPtr packet() { return p; }
3266221Snate@binkert.org    bool operator!() const { return !p; }
3271684SN/A    operator bool() const { return (p != nullptr); }
3282292SN/A    int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
3292292SN/A    int pstart() const { return (off() + get()->size()); }
3302292SN/A};
3312292SN/A
3322292SN/Auint16_t cksum(const IpPtr &ptr);
3332292SN/A
3342292SN/Astruct IpOpt : public ip_opt
3352292SN/A{
3361062SN/A    uint8_t type() const { return opt_type; }
3371062SN/A    uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
3381062SN/A    uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
3391062SN/A    uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
3401061SN/A    uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
3418541Sgblack@eecs.umich.edu
3429023Sgblack@eecs.umich.edu    bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
3438541Sgblack@eecs.umich.edu    bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
3441060SN/A    bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
3457764Sgblack@eecs.umich.edu
3467764Sgblack@eecs.umich.edu    const uint8_t *data() const { return opt_data.data8; }
3477764Sgblack@eecs.umich.edu    void sec(ip_opt_data_sec &sec) const;
3487764Sgblack@eecs.umich.edu    void lsrr(ip_opt_data_rr &rr) const;
3492292SN/A    void ssrr(ip_opt_data_rr &rr) const;
3506221Snate@binkert.org    void ts(ip_opt_data_ts &ts) const;
3512292SN/A    uint16_t satid() const { return ntohs(opt_data.satid); }
3522292SN/A    uint16_t mtup() const { return ntohs(opt_data.mtu); }
3536221Snate@binkert.org    uint16_t mtur() const { return ntohs(opt_data.mtu); }
3542292SN/A    void tr(ip_opt_data_tr &tr) const;
3552292SN/A    const uint32_t *addext() const { return &opt_data.addext[0]; }
3566221Snate@binkert.org    uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
3572292SN/A    void sdb(std::vector<uint32_t> &vec) const;
3582292SN/A};
3596221Snate@binkert.org
3602292SN/A/*
3616221Snate@binkert.org * Ip6 Classes
3626221Snate@binkert.org */
3636221Snate@binkert.orgstruct Ip6Opt;
3642292SN/Astruct Ip6Hdr : public ip6_hdr
3658462Sgeoffrey.blake@arm.com{
3668462Sgeoffrey.blake@arm.com    uint8_t version() const { return ip6_vfc; }
3678462Sgeoffrey.blake@arm.com    uint32_t flow() const { return ntohl(ip6_flow); }
3688462Sgeoffrey.blake@arm.com    uint16_t plen() const { return ntohs(ip6_plen); }
3698462Sgeoffrey.blake@arm.com    uint16_t hlen() const { return IP6_HDR_LEN; }
3708462Sgeoffrey.blake@arm.com    uint8_t nxt() const { return ip6_nxt; }
3712292SN/A    uint8_t hlim() const { return ip6_hlim; }
3722733Sktlim@umich.edu
3732733Sktlim@umich.edu    const uint8_t* src() const { return ip6_src.data; }
3741060SN/A    const uint8_t* dst() const { return ip6_dst.data; }
3751060SN/A
3761060SN/A    int extensionLength() const;
3771060SN/A    const Ip6Opt* getExt(uint8_t ext) const;
3781060SN/A    const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); }
3791060SN/A    const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); }
3801060SN/A    const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); }
3811060SN/A    uint8_t proto() const;
3821060SN/A
3831060SN/A    void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
3841060SN/A
3851060SN/A    int size() const { return IP6_HDR_LEN + extensionLength(); }
3861060SN/A    const uint8_t *bytes() const { return (const uint8_t *)this; }
3871060SN/A    const uint8_t *payload() const { return bytes() + IP6_HDR_LEN
3881060SN/A                                            + extensionLength(); }
3891060SN/A    uint8_t *bytes() { return (uint8_t *)this; }
3901060SN/A    uint8_t *payload() { return bytes() + IP6_HDR_LEN
3911060SN/A                                + extensionLength(); }
3921060SN/A};
3931060SN/A
3941060SN/Aclass Ip6Ptr
3951060SN/A{
3961060SN/A  protected:
3971061SN/A    friend class TcpPtr;
3981061SN/A    friend class UdpPtr;
3991061SN/A    EthPacketPtr p;
4007720Sgblack@eecs.umich.edu    bool eth_hdr_vlan;
4012292SN/A
4027764Sgblack@eecs.umich.edu    void set(const EthPacketPtr &ptr)
4037764Sgblack@eecs.umich.edu    {
4047764Sgblack@eecs.umich.edu        p = 0;
4057764Sgblack@eecs.umich.edu        eth_hdr_vlan = false;
4068314Sgeoffrey.blake@arm.com
4078314Sgeoffrey.blake@arm.com        if (ptr) {
4088314Sgeoffrey.blake@arm.com            EthHdr *eth = (EthHdr *)ptr->data;
4092678Sktlim@umich.edu            if (eth->type() == ETH_TYPE_IPV6)
4102678Sktlim@umich.edu                p = ptr;
4112292SN/A            if (eth->isVlan())
4122292SN/A                eth_hdr_vlan = true;
4132292SN/A        }
4142292SN/A    }
4152292SN/A
4162292SN/A  public:
4172292SN/A    Ip6Ptr() : p(0), eth_hdr_vlan(false) {}
4182292SN/A    Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
4192292SN/A    Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
4202292SN/A    Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
4212292SN/A
4222292SN/A    Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr)
4232292SN/A                                      + ((eth_hdr_vlan) ? 4 : 0)); }
4242292SN/A    Ip6Hdr *operator->() { return get(); }
4252292SN/A    Ip6Hdr &operator*() { return *get(); }
4262292SN/A
4272292SN/A    const Ip6Hdr *get() const
4282292SN/A    { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr)
4292292SN/A                              + ((eth_hdr_vlan) ? 4 : 0)); }
4301060SN/A    const Ip6Hdr *operator->() const { return get(); }
4311060SN/A    const Ip6Hdr &operator*() const { return *get(); }
4321060SN/A
4331060SN/A    const Ip6Ptr &operator=(const EthPacketPtr &ptr)
4341060SN/A    { set(ptr); return *this; }
4351060SN/A    const Ip6Ptr &operator=(const EthPtr &ptr)
4361060SN/A    { set(ptr.p); return *this; }
4371060SN/A    const Ip6Ptr &operator=(const Ip6Ptr &ptr)
4381060SN/A    { p = ptr.p; return *this; }
4391060SN/A
4401060SN/A    const EthPacketPtr packet() const { return p; }
4411060SN/A    EthPacketPtr packet() { return p; }
4421060SN/A    bool operator!() const { return !p; }
4431060SN/A    operator bool() const { return (p != nullptr); }
4441060SN/A    int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
4451060SN/A    int pstart() const { return off() + get()->size(); }
4462696Sktlim@umich.edu};
4472696Sktlim@umich.edu
4482696Sktlim@umich.edu// Dnet supplied ipv6 opt header is incomplete and
4492696Sktlim@umich.edu// newer NIC card filters expect a more robust
4502696Sktlim@umich.edu// ipv6 header option declaration.
4512696Sktlim@umich.edustruct ip6_opt_fragment {
4522696Sktlim@umich.edu    uint16_t offlg;
4536221Snate@binkert.org    uint32_t ident;
4542696Sktlim@umich.edu};
4551060SN/A
4561062SN/Astruct ip6_opt_routing_type2 {
4571060SN/A    uint8_t type;
4581060SN/A    uint8_t segleft;
4591062SN/A    uint32_t reserved;
4601060SN/A    ip6_addr_t addr;
4611062SN/A};
4622292SN/A
4631060SN/A#define HOME_ADDRESS_OPTION 0xC9
4642893Sktlim@umich.edustruct ip6_opt_dstopts {
4652893Sktlim@umich.edu    uint8_t type;
4662893Sktlim@umich.edu    uint8_t length;
4672906Sktlim@umich.edu    ip6_addr_t addr;
4682906Sktlim@umich.edu} __attribute__((packed));
4692906Sktlim@umich.edu
4701060SN/Astruct ip6_opt_hdr
4711060SN/A{
4721060SN/A    uint8_t ext_nxt;
4731060SN/A    uint8_t ext_len;
4742292SN/A    union {
4751062SN/A        struct ip6_opt_fragment fragment;
4762292SN/A        struct ip6_opt_routing_type2 rtType2;
4776221Snate@binkert.org        struct ip6_opt_dstopts dstOpts;
4782292SN/A    } ext_data;
4792292SN/A} __attribute__((packed));
4806221Snate@binkert.org
4812292SN/Astruct Ip6Opt : public ip6_opt_hdr
4822292SN/A{
4836221Snate@binkert.org    uint8_t nxt() const { return ext_nxt; }
4842292SN/A    uint8_t extlen() const { return ext_len; }
4852292SN/A    uint8_t len() const { return extlen() + 8; }
4866221Snate@binkert.org
4872292SN/A    // Supporting the types of header extensions likely to be encountered:
4882348SN/A    // fragment, routing type 2 and dstopts.
4892348SN/A
4902348SN/A    // Routing type 2
4912292SN/A    uint8_t  rtType2Type() const { return ext_data.rtType2.type; }
4922292SN/A    uint8_t  rtType2SegLft() const { return ext_data.rtType2.segleft; }
4932843Sktlim@umich.edu    const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; }
4942843Sktlim@umich.edu
4952843Sktlim@umich.edu    // Fragment
4962348SN/A    uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); }
4972307SN/A    uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); }
4982307SN/A
4998462Sgeoffrey.blake@arm.com    // Dst Options/Home Address Option
5008462Sgeoffrey.blake@arm.com    uint8_t dstOptType() const { return ext_data.dstOpts.type; }
5018462Sgeoffrey.blake@arm.com    uint8_t dstOptLength() const { return ext_data.dstOpts.length; }
5028462Sgeoffrey.blake@arm.com    const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; }
5038462Sgeoffrey.blake@arm.com};
5048462Sgeoffrey.blake@arm.com
5052292SN/A
5062292SN/A/*
5075999Snate@binkert.org * TCP Stuff
5082292SN/A */
5095999Snate@binkert.orgstruct TcpOpt;
5102727Sktlim@umich.edustruct TcpHdr : public tcp_hdr
5115999Snate@binkert.org{
5122292SN/A    uint16_t sport() const { return ntohs(th_sport); }
5135999Snate@binkert.org    uint16_t dport() const { return ntohs(th_dport); }
5142292SN/A    uint32_t seq() const { return ntohl(th_seq); }
5155999Snate@binkert.org    uint32_t ack() const { return ntohl(th_ack); }
5162292SN/A    uint8_t  off() const { return th_off*4; }
5175999Snate@binkert.org    uint8_t  flags() const { return th_flags & 0x3f; }
5187849SAli.Saidi@ARM.com    uint16_t win() const { return ntohs(th_win); }
5197849SAli.Saidi@ARM.com    uint16_t sum() const { return th_sum; }
5202292SN/A    uint16_t urp() const { return ntohs(th_urp); }
5212292SN/A
5222292SN/A    void sum(uint16_t sum) { th_sum = sum; }
5235999Snate@binkert.org    void seq(uint32_t _seq) { th_seq = htonl(_seq); }
5242348SN/A    void flags(uint8_t _flags) { th_flags  = _flags; }
5255999Snate@binkert.org
5262348SN/A    bool options(std::vector<const TcpOpt *> &vec) const;
5275999Snate@binkert.org
5288462Sgeoffrey.blake@arm.com    int size() const { return off(); }
5298462Sgeoffrey.blake@arm.com    const uint8_t *bytes() const { return (const uint8_t *)this; }
5308462Sgeoffrey.blake@arm.com    const uint8_t *payload() const { return bytes() + size(); }
5318462Sgeoffrey.blake@arm.com    uint8_t *bytes() { return (uint8_t *)this; }
5328462Sgeoffrey.blake@arm.com    uint8_t *payload() { return bytes() + size(); }
5338462Sgeoffrey.blake@arm.com};
5348462Sgeoffrey.blake@arm.com
5358462Sgeoffrey.blake@arm.comclass TcpPtr
5368462Sgeoffrey.blake@arm.com{
5378462Sgeoffrey.blake@arm.com  protected:
5382292SN/A    EthPacketPtr p;
5395999Snate@binkert.org    int _off;
5402348SN/A
5412348SN/A    void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
5422348SN/A    void set(const IpPtr &ptr)
5435999Snate@binkert.org    {
5448064SAli.Saidi@ARM.com        if (ptr && ptr->proto() == IP_PROTO_TCP)
5458064SAli.Saidi@ARM.com            set(ptr.p, ptr.pstart());
5468064SAli.Saidi@ARM.com        else
5478064SAli.Saidi@ARM.com            set(0, 0);
5482292SN/A    }
5495999Snate@binkert.org    void set(const Ip6Ptr &ptr)
5502348SN/A    {
5512292SN/A        if (ptr && ptr->proto() == IP_PROTO_TCP)
5522348SN/A            set(ptr.p, ptr.pstart());
5532292SN/A        else
5542348SN/A            set(0, 0);
5552292SN/A    }
5561060SN/A
5571060SN/A  public:
5582292SN/A    TcpPtr() : p(0), _off(0) {}
559    TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
560    TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
561    TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
562
563    TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
564    TcpHdr *operator->() { return get(); }
565    TcpHdr &operator*() { return *get(); }
566
567    const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
568    const TcpHdr *operator->() const { return get(); }
569    const TcpHdr &operator*() const { return *get(); }
570
571    const TcpPtr &operator=(const IpPtr &i)
572    { set(i); return *this; }
573    const TcpPtr &operator=(const TcpPtr &t)
574    { set(t.p, t._off); return *this; }
575
576    const EthPacketPtr packet() const { return p; }
577    EthPacketPtr packet() { return p; }
578    bool operator!() const { return !p; }
579    operator bool() const { return (p != nullptr); }
580    int off() const { return _off; }
581    int pstart() const { return off() + get()->size(); }
582};
583
584uint16_t cksum(const TcpPtr &ptr);
585
586struct TcpOpt : public tcp_opt
587{
588    uint8_t type() const { return opt_type; }
589    uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
590
591    bool isopt(int opt) const { return type() == opt; }
592
593    const uint8_t *data() const { return opt_data.data8; }
594
595    uint16_t mss() const { return ntohs(opt_data.mss); }
596    uint8_t wscale() const { return opt_data.wscale; }
597    uint32_t echo() const { return ntohl(opt_data.echo); }
598    uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
599    uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
600    uint32_t cc() const { return ntohl(opt_data.cc); }
601    uint8_t cksum() const{ return opt_data.cksum; }
602    const uint8_t *md5() const { return opt_data.md5; }
603
604    int size() const { return len(); }
605    const uint8_t *bytes() const { return (const uint8_t *)this; }
606    const uint8_t *payload() const { return bytes() + size(); }
607    uint8_t *bytes() { return (uint8_t *)this; }
608    uint8_t *payload() { return bytes() + size(); }
609};
610
611/*
612 * UDP Stuff
613 */
614struct UdpHdr : public udp_hdr
615{
616    uint16_t sport() const { return ntohs(uh_sport); }
617    uint16_t dport() const { return ntohs(uh_dport); }
618    uint16_t len() const { return ntohs(uh_ulen); }
619    uint16_t sum() const { return uh_sum; }
620
621    void sum(uint16_t sum) { uh_sum = sum; }
622    void len(uint16_t _len) { uh_ulen = htons(_len); }
623
624    int size() const { return sizeof(udp_hdr); }
625    const uint8_t *bytes() const { return (const uint8_t *)this; }
626    const uint8_t *payload() const { return bytes() + size(); }
627    uint8_t *bytes() { return (uint8_t *)this; }
628    uint8_t *payload() { return bytes() + size(); }
629};
630
631class UdpPtr
632{
633  protected:
634    EthPacketPtr p;
635    int _off;
636
637    void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
638    void set(const IpPtr &ptr)
639    {
640        if (ptr && ptr->proto() == IP_PROTO_UDP)
641            set(ptr.p, ptr.pstart());
642        else
643            set(0, 0);
644    }
645    void set(const Ip6Ptr &ptr)
646    {
647        if (ptr && ptr->proto() == IP_PROTO_UDP)
648            set(ptr.p, ptr.pstart());
649        else
650            set(0, 0);
651    }
652
653  public:
654    UdpPtr() : p(0), _off(0) {}
655    UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
656    UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
657    UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
658
659    UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
660    UdpHdr *operator->() { return get(); }
661    UdpHdr &operator*() { return *get(); }
662
663    const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
664    const UdpHdr *operator->() const { return get(); }
665    const UdpHdr &operator*() const { return *get(); }
666
667    const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
668    const UdpPtr &operator=(const UdpPtr &t)
669    { set(t.p, t._off); return *this; }
670
671    const EthPacketPtr packet() const { return p; }
672    EthPacketPtr packet() { return p; }
673    bool operator!() const { return !p; }
674    operator bool() const { return (p != nullptr); }
675    int off() const { return _off; }
676    int pstart() const { return off() + get()->size(); }
677};
678
679uint16_t __tu_cksum6(const Ip6Ptr &ip6);
680uint16_t __tu_cksum(const IpPtr &ip);
681uint16_t cksum(const UdpPtr &ptr);
682
683int hsplit(const EthPacketPtr &ptr);
684
685} // namespace Net
686
687#endif // __BASE_INET_HH__
688