2 * Copyright (c) 2013 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #ifndef __BASE_INET_HH__
43 #define __BASE_INET_HH__
50 #include "base/types.hh"
51 #include "dev/net/etherpkt.hh"
56 #include "dnet/addr.h"
58 #include "dnet/icmp.h"
61 #include "dnet/intf.h"
62 #include "dnet/route.h"
64 #include "dnet/blob.h"
65 #include "dnet/rand.h"
72 struct EthAddr : protected eth_addr
75 void parse(const std::string &addr);
83 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
84 EthAddr(const eth_addr &ea);
85 EthAddr(const std::string &addr);
86 const EthAddr &operator=(const eth_addr &ea);
87 const EthAddr &operator=(const std::string &addr);
88 /** @} */ // end of api_inet
93 int size() const { return sizeof(eth_addr); }
100 const uint8_t *bytes() const { return &data[0]; }
101 uint8_t *bytes() { return &data[0]; }
102 /** @} */ // end of api_inet
108 const uint8_t *addr() const { return &data[0]; }
109 bool unicast() const { return !(data[0] & 0x01); }
110 bool multicast() const { return !unicast() && !broadcast(); }
111 bool broadcast() const
113 bool isBroadcast = true;
114 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
115 isBroadcast = isBroadcast && data[i] == 0xff;
120 /** @} */ // end of api_inet
125 std::string string() const;
130 operator uint64_t() const
133 reg |= ((uint64_t)data[0]) << 40;
134 reg |= ((uint64_t)data[1]) << 32;
135 reg |= ((uint64_t)data[2]) << 24;
136 reg |= ((uint64_t)data[3]) << 16;
137 reg |= ((uint64_t)data[4]) << 8;
138 reg |= ((uint64_t)data[5]) << 0;
148 std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
149 bool operator==(const EthAddr &left, const EthAddr &right);
150 /** @} */ // end of api_inet
152 struct EthHdr : public eth_hdr
154 bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); }
155 uint16_t type() const {
157 return ntohs(eth_type);
159 // L3 type is now 16 bytes into the hdr with 802.1Q
160 // instead of 12. dnet/eth.h only supports 802.1
161 return ntohs(*((uint16_t*)(((uint8_t *)this) + 16)));
163 uint16_t vlanId() const {
165 return ntohs(*((uint16_t*)(((uint8_t *)this) + 14)));
170 const EthAddr &src() const { return *(EthAddr *)ð_src; }
171 const EthAddr &dst() const { return *(EthAddr *)ð_dst; }
175 return sizeof(eth_hdr);
177 return (sizeof(eth_hdr)+4);
180 const uint8_t *bytes() const { return (const uint8_t *)this; }
181 const uint8_t *payload() const { return bytes() + size(); }
182 uint8_t *bytes() { return (uint8_t *)this; }
183 uint8_t *payload() { return bytes() + size(); }
199 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
200 /** @} */ // end of api_inet
202 EthHdr *operator->() { return (EthHdr *)p->data; }
203 EthHdr &operator*() { return *(EthHdr *)p->data; }
204 operator EthHdr *() { return (EthHdr *)p->data; }
206 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
207 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
208 operator const EthHdr *() const { return (const EthHdr *)p->data; }
213 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
219 const EthPacketPtr packet() const { return p; }
220 EthPacketPtr packet() { return p; }
221 bool operator!() const { return !p; }
222 operator bool() const { return (p != nullptr); }
223 int off() const { return 0; }
224 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
225 /** @} */ // end of api_inet
243 IpAddress(const uint32_t __ip) : _ip(__ip)
245 /** @} */ // end of api_net
250 uint32_t ip() const { return _ip; }
255 std::string string() const;
262 std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
263 bool operator==(const IpAddress &left, const IpAddress &right);
264 /** @} */ // end of api_inet
266 struct IpNetmask : public IpAddress
272 IpNetmask() : IpAddress(), _netmask(0)
274 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
275 IpAddress(__ip), _netmask(__netmask)
281 uint8_t netmask() const { return _netmask; }
283 std::string string() const;
290 std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
291 bool operator==(const IpNetmask &left, const IpNetmask &right);
292 /** @} */ // end of api_inet
294 struct IpWithPort : public IpAddress
300 IpWithPort() : IpAddress(), _port(0)
302 IpWithPort(const uint32_t __ip, const uint16_t __port) :
303 IpAddress(__ip), _port(__port)
309 uint8_t port() const { return _port; }
311 std::string string() const;
318 std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
319 bool operator==(const IpWithPort &left, const IpWithPort &right);
320 /** @} */ // end of api_inet
323 struct IpHdr : public ip_hdr
325 uint8_t version() const { return ip_v; }
326 uint8_t hlen() const { return ip_hl * 4; }
327 uint8_t tos() const { return ip_tos; }
328 uint16_t len() const { return ntohs(ip_len); }
329 uint16_t id() const { return ntohs(ip_id); }
330 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
331 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
332 uint8_t ttl() const { return ip_ttl; }
333 uint8_t proto() const { return ip_p; }
334 uint16_t sum() const { return ip_sum; }
335 uint32_t src() const { return ntohl(ip_src); }
336 uint32_t dst() const { return ntohl(ip_dst); }
338 void sum(uint16_t sum) { ip_sum = sum; }
339 void id(uint16_t _id) { ip_id = htons(_id); }
340 void len(uint16_t _len) { ip_len = htons(_len); }
342 bool options(std::vector<const IpOpt *> &vec) const;
344 int size() const { return hlen(); }
345 const uint8_t *bytes() const { return (const uint8_t *)this; }
346 const uint8_t *payload() const { return bytes() + size(); }
347 uint8_t *bytes() { return (uint8_t *)this; }
348 uint8_t *payload() { return bytes() + size(); }
359 void set(const EthPacketPtr &ptr)
362 eth_hdr_vlan = false;
365 EthHdr *eth = (EthHdr *)ptr->data;
366 if (eth->type() == ETH_TYPE_IP)
378 IpPtr() : p(0), eth_hdr_vlan(false) {}
379 IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
380 IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
381 IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
382 /** @} */ // end of api_inet
384 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) +
385 ((eth_hdr_vlan) ? 4 : 0)); }
386 IpHdr *operator->() { return get(); }
387 IpHdr &operator*() { return *get(); }
393 const IpHdr *get() const
394 { return (const IpHdr *)(p->data + sizeof(eth_hdr) +
395 ((eth_hdr_vlan) ? 4 : 0)); }
396 const IpHdr *operator->() const { return get(); }
397 const IpHdr &operator*() const { return *get(); }
398 /** @} */ // end of api_inet
400 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
401 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
402 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
408 const EthPacketPtr packet() const { return p; }
409 EthPacketPtr packet() { return p; }
410 bool operator!() const { return !p; }
411 operator bool() const { return (p != nullptr); }
412 int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
413 int pstart() const { return (off() + get()->size()); }
414 /** @} */ // end of api_inet
420 uint16_t cksum(const IpPtr &ptr);
422 struct IpOpt : public ip_opt
424 uint8_t type() const { return opt_type; }
425 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
426 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
427 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
428 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
430 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
431 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
432 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
434 const uint8_t *data() const { return opt_data.data8; }
435 void sec(ip_opt_data_sec &sec) const;
436 void lsrr(ip_opt_data_rr &rr) const;
437 void ssrr(ip_opt_data_rr &rr) const;
438 void ts(ip_opt_data_ts &ts) const;
439 uint16_t satid() const { return ntohs(opt_data.satid); }
440 uint16_t mtup() const { return ntohs(opt_data.mtu); }
441 uint16_t mtur() const { return ntohs(opt_data.mtu); }
442 void tr(ip_opt_data_tr &tr) const;
443 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
444 void sdb(std::vector<uint32_t> &vec) const;
451 struct Ip6Hdr : public ip6_hdr
453 uint8_t version() const { return ip6_vfc; }
454 uint32_t flow() const { return ntohl(ip6_flow); }
455 uint16_t plen() const { return ntohs(ip6_plen); }
456 uint16_t hlen() const { return IP6_HDR_LEN; }
457 uint8_t nxt() const { return ip6_nxt; }
458 uint8_t hlim() const { return ip6_hlim; }
460 const uint8_t* src() const { return ip6_src.data; }
461 const uint8_t* dst() const { return ip6_dst.data; }
463 int extensionLength() const;
464 const Ip6Opt* getExt(uint8_t ext) const;
465 const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); }
466 const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); }
467 const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); }
468 uint8_t proto() const;
470 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
472 int size() const { return IP6_HDR_LEN + extensionLength(); }
473 const uint8_t *bytes() const { return (const uint8_t *)this; }
474 const uint8_t *payload() const { return bytes() + IP6_HDR_LEN
475 + extensionLength(); }
476 uint8_t *bytes() { return (uint8_t *)this; }
477 uint8_t *payload() { return bytes() + IP6_HDR_LEN
478 + extensionLength(); }
489 void set(const EthPacketPtr &ptr)
492 eth_hdr_vlan = false;
495 EthHdr *eth = (EthHdr *)ptr->data;
496 if (eth->type() == ETH_TYPE_IPV6)
508 Ip6Ptr() : p(0), eth_hdr_vlan(false) {}
509 Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
510 Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
511 Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
512 /** @} */ // end of api_inet
514 Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr)
515 + ((eth_hdr_vlan) ? 4 : 0)); }
516 Ip6Hdr *operator->() { return get(); }
517 Ip6Hdr &operator*() { return *get(); }
519 const Ip6Hdr *get() const
520 { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr)
521 + ((eth_hdr_vlan) ? 4 : 0)); }
522 const Ip6Hdr *operator->() const { return get(); }
523 const Ip6Hdr &operator*() const { return *get(); }
529 const Ip6Ptr &operator=(const EthPacketPtr &ptr)
530 { set(ptr); return *this; }
531 const Ip6Ptr &operator=(const EthPtr &ptr)
532 { set(ptr.p); return *this; }
533 const Ip6Ptr &operator=(const Ip6Ptr &ptr)
534 { p = ptr.p; return *this; }
535 /** @} */ // end of api_inet
541 const EthPacketPtr packet() const { return p; }
542 EthPacketPtr packet() { return p; }
543 bool operator!() const { return !p; }
544 operator bool() const { return (p != nullptr); }
545 int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
546 int pstart() const { return off() + get()->size(); }
547 /** @} */ // end of api_inet
550 // Dnet supplied ipv6 opt header is incomplete and
551 // newer NIC card filters expect a more robust
552 // ipv6 header option declaration.
553 struct ip6_opt_fragment {
558 struct ip6_opt_routing_type2 {
565 #define HOME_ADDRESS_OPTION 0xC9
566 struct M5_ATTR_PACKED ip6_opt_dstopts {
572 struct M5_ATTR_PACKED ip6_opt_hdr
577 struct ip6_opt_fragment fragment;
578 struct ip6_opt_routing_type2 rtType2;
579 struct ip6_opt_dstopts dstOpts;
583 struct Ip6Opt : public ip6_opt_hdr
585 uint8_t nxt() const { return ext_nxt; }
586 uint8_t extlen() const { return ext_len; }
587 uint8_t len() const { return extlen() + 8; }
589 // Supporting the types of header extensions likely to be encountered:
590 // fragment, routing type 2 and dstopts.
593 uint8_t rtType2Type() const { return ext_data.rtType2.type; }
594 uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; }
595 const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; }
598 uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); }
599 uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); }
601 // Dst Options/Home Address Option
602 uint8_t dstOptType() const { return ext_data.dstOpts.type; }
603 uint8_t dstOptLength() const { return ext_data.dstOpts.length; }
604 const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; }
612 struct TcpHdr : public tcp_hdr
614 uint16_t sport() const { return ntohs(th_sport); }
615 uint16_t dport() const { return ntohs(th_dport); }
616 uint32_t seq() const { return ntohl(th_seq); }
617 uint32_t ack() const { return ntohl(th_ack); }
618 uint8_t off() const { return th_off*4; }
619 uint8_t flags() const { return th_flags & 0x3f; }
620 uint16_t win() const { return ntohs(th_win); }
621 uint16_t sum() const { return th_sum; }
622 uint16_t urp() const { return ntohs(th_urp); }
624 void sum(uint16_t sum) { th_sum = sum; }
625 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
626 void flags(uint8_t _flags) { th_flags = _flags; }
628 bool options(std::vector<const TcpOpt *> &vec) const;
630 int size() const { return off(); }
631 const uint8_t *bytes() const { return (const uint8_t *)this; }
632 const uint8_t *payload() const { return bytes() + size(); }
633 uint8_t *bytes() { return (uint8_t *)this; }
634 uint8_t *payload() { return bytes() + size(); }
643 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
644 void set(const IpPtr &ptr)
646 if (ptr && ptr->proto() == IP_PROTO_TCP)
647 set(ptr.p, ptr.pstart());
651 void set(const Ip6Ptr &ptr)
653 if (ptr && ptr->proto() == IP_PROTO_TCP)
654 set(ptr.p, ptr.pstart());
664 TcpPtr() : p(0), _off(0) {}
665 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
666 TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
667 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
668 /** @} */ // end of api_inet
670 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
671 TcpHdr *operator->() { return get(); }
672 TcpHdr &operator*() { return *get(); }
674 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
675 const TcpHdr *operator->() const { return get(); }
676 const TcpHdr &operator*() const { return *get(); }
682 const TcpPtr &operator=(const IpPtr &i)
683 { set(i); return *this; }
684 const TcpPtr &operator=(const TcpPtr &t)
685 { set(t.p, t._off); return *this; }
686 /** @} */ // end of api_inet
692 const EthPacketPtr packet() const { return p; }
693 EthPacketPtr packet() { return p; }
694 bool operator!() const { return !p; }
695 operator bool() const { return (p != nullptr); }
696 int off() const { return _off; }
697 int pstart() const { return off() + get()->size(); }
698 /** @} */ // end of api_inet
704 uint16_t cksum(const TcpPtr &ptr);
706 struct TcpOpt : public tcp_opt
708 uint8_t type() const { return opt_type; }
709 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
711 bool isopt(int opt) const { return type() == opt; }
713 const uint8_t *data() const { return opt_data.data8; }
715 uint16_t mss() const { return ntohs(opt_data.mss); }
716 uint8_t wscale() const { return opt_data.wscale; }
717 uint32_t echo() const { return ntohl(opt_data.echo); }
718 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
719 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
720 uint32_t cc() const { return ntohl(opt_data.cc); }
721 uint8_t cksum() const{ return opt_data.cksum; }
722 const uint8_t *md5() const { return opt_data.md5; }
724 int size() const { return len(); }
725 const uint8_t *bytes() const { return (const uint8_t *)this; }
726 const uint8_t *payload() const { return bytes() + size(); }
727 uint8_t *bytes() { return (uint8_t *)this; }
728 uint8_t *payload() { return bytes() + size(); }
734 struct UdpHdr : public udp_hdr
736 uint16_t sport() const { return ntohs(uh_sport); }
737 uint16_t dport() const { return ntohs(uh_dport); }
738 uint16_t len() const { return ntohs(uh_ulen); }
739 uint16_t sum() const { return uh_sum; }
741 void sum(uint16_t sum) { uh_sum = sum; }
742 void len(uint16_t _len) { uh_ulen = htons(_len); }
744 int size() const { return sizeof(udp_hdr); }
745 const uint8_t *bytes() const { return (const uint8_t *)this; }
746 const uint8_t *payload() const { return bytes() + size(); }
747 uint8_t *bytes() { return (uint8_t *)this; }
748 uint8_t *payload() { return bytes() + size(); }
757 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
758 void set(const IpPtr &ptr)
760 if (ptr && ptr->proto() == IP_PROTO_UDP)
761 set(ptr.p, ptr.pstart());
765 void set(const Ip6Ptr &ptr)
767 if (ptr && ptr->proto() == IP_PROTO_UDP)
768 set(ptr.p, ptr.pstart());
777 UdpPtr() : p(0), _off(0) {}
778 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
779 UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
780 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
781 /** @} */ // end of api_inet
783 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
784 UdpHdr *operator->() { return get(); }
785 UdpHdr &operator*() { return *get(); }
787 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
788 const UdpHdr *operator->() const { return get(); }
789 const UdpHdr &operator*() const { return *get(); }
795 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
796 const UdpPtr &operator=(const UdpPtr &t)
797 { set(t.p, t._off); return *this; }
798 /** @} */ // end of api_inet
804 const EthPacketPtr packet() const { return p; }
805 EthPacketPtr packet() { return p; }
806 bool operator!() const { return !p; }
807 operator bool() const { return (p != nullptr); }
808 int off() const { return _off; }
809 int pstart() const { return off() + get()->size(); }
810 /** @} */ // end of api_inet
817 uint16_t __tu_cksum6(const Ip6Ptr &ip6);
818 uint16_t __tu_cksum(const IpPtr &ip);
819 uint16_t cksum(const UdpPtr &ptr);
820 /** @} */ // end of api_inet
825 int hsplit(const EthPacketPtr &ptr);
829 #endif // __BASE_INET_HH__