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);
79 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
80 EthAddr(const eth_addr &ea);
81 EthAddr(const std::string &addr);
82 const EthAddr &operator=(const eth_addr &ea);
83 const EthAddr &operator=(const std::string &addr);
85 int size() const { return sizeof(eth_addr); }
87 const uint8_t *bytes() const { return &data[0]; }
88 uint8_t *bytes() { return &data[0]; }
90 const uint8_t *addr() const { return &data[0]; }
91 bool unicast() const { return !(data[0] & 0x01); }
92 bool multicast() const { return !unicast() && !broadcast(); }
93 bool broadcast() const
95 bool isBroadcast = true;
96 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
97 isBroadcast = isBroadcast && data[i] == 0xff;
103 std::string string() const;
105 operator uint64_t() const
108 reg |= ((uint64_t)data[0]) << 40;
109 reg |= ((uint64_t)data[1]) << 32;
110 reg |= ((uint64_t)data[2]) << 24;
111 reg |= ((uint64_t)data[3]) << 16;
112 reg |= ((uint64_t)data[4]) << 8;
113 reg |= ((uint64_t)data[5]) << 0;
119 std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
120 bool operator==(const EthAddr &left, const EthAddr &right);
122 struct EthHdr : public eth_hdr
124 bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); }
125 uint16_t type() const {
127 return ntohs(eth_type);
129 // L3 type is now 16 bytes into the hdr with 802.1Q
130 // instead of 12. dnet/eth.h only supports 802.1
131 return ntohs(*((uint16_t*)(((uint8_t *)this) + 16)));
133 uint16_t vlanId() const {
135 return ntohs(*((uint16_t*)(((uint8_t *)this) + 14)));
140 const EthAddr &src() const { return *(EthAddr *)ð_src; }
141 const EthAddr &dst() const { return *(EthAddr *)ð_dst; }
145 return sizeof(eth_hdr);
147 return (sizeof(eth_hdr)+4);
150 const uint8_t *bytes() const { return (const uint8_t *)this; }
151 const uint8_t *payload() const { return bytes() + size(); }
152 uint8_t *bytes() { return (uint8_t *)this; }
153 uint8_t *payload() { return bytes() + size(); }
165 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
167 EthHdr *operator->() { return (EthHdr *)p->data; }
168 EthHdr &operator*() { return *(EthHdr *)p->data; }
169 operator EthHdr *() { return (EthHdr *)p->data; }
171 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
172 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
173 operator const EthHdr *() const { return (const EthHdr *)p->data; }
175 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
177 const EthPacketPtr packet() const { return p; }
178 EthPacketPtr packet() { return p; }
179 bool operator!() const { return !p; }
180 operator bool() const { return (p != nullptr); }
181 int off() const { return 0; }
182 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
196 IpAddress(const uint32_t __ip) : _ip(__ip)
199 uint32_t ip() const { return _ip; }
201 std::string string() const;
204 std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
205 bool operator==(const IpAddress &left, const IpAddress &right);
207 struct IpNetmask : public IpAddress
213 IpNetmask() : IpAddress(), _netmask(0)
215 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
216 IpAddress(__ip), _netmask(__netmask)
219 uint8_t netmask() const { return _netmask; }
221 std::string string() const;
224 std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
225 bool operator==(const IpNetmask &left, const IpNetmask &right);
227 struct IpWithPort : public IpAddress
233 IpWithPort() : IpAddress(), _port(0)
235 IpWithPort(const uint32_t __ip, const uint16_t __port) :
236 IpAddress(__ip), _port(__port)
239 uint8_t port() const { return _port; }
241 std::string string() const;
244 std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
245 bool operator==(const IpWithPort &left, const IpWithPort &right);
248 struct IpHdr : public ip_hdr
250 uint8_t version() const { return ip_v; }
251 uint8_t hlen() const { return ip_hl * 4; }
252 uint8_t tos() const { return ip_tos; }
253 uint16_t len() const { return ntohs(ip_len); }
254 uint16_t id() const { return ntohs(ip_id); }
255 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
256 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
257 uint8_t ttl() const { return ip_ttl; }
258 uint8_t proto() const { return ip_p; }
259 uint16_t sum() const { return ip_sum; }
260 uint32_t src() const { return ntohl(ip_src); }
261 uint32_t dst() const { return ntohl(ip_dst); }
263 void sum(uint16_t sum) { ip_sum = sum; }
264 void id(uint16_t _id) { ip_id = htons(_id); }
265 void len(uint16_t _len) { ip_len = htons(_len); }
267 bool options(std::vector<const IpOpt *> &vec) const;
269 int size() const { return hlen(); }
270 const uint8_t *bytes() const { return (const uint8_t *)this; }
271 const uint8_t *payload() const { return bytes() + size(); }
272 uint8_t *bytes() { return (uint8_t *)this; }
273 uint8_t *payload() { return bytes() + size(); }
284 void set(const EthPacketPtr &ptr)
287 eth_hdr_vlan = false;
290 EthHdr *eth = (EthHdr *)ptr->data;
291 if (eth->type() == ETH_TYPE_IP)
299 IpPtr() : p(0), eth_hdr_vlan(false) {}
300 IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
301 IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
302 IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
304 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) +
305 ((eth_hdr_vlan) ? 4 : 0)); }
306 IpHdr *operator->() { return get(); }
307 IpHdr &operator*() { return *get(); }
309 const IpHdr *get() const
310 { return (const IpHdr *)(p->data + sizeof(eth_hdr) +
311 ((eth_hdr_vlan) ? 4 : 0)); }
312 const IpHdr *operator->() const { return get(); }
313 const IpHdr &operator*() const { return *get(); }
315 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
316 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
317 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
319 const EthPacketPtr packet() const { return p; }
320 EthPacketPtr packet() { return p; }
321 bool operator!() const { return !p; }
322 operator bool() const { return (p != nullptr); }
323 int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
324 int pstart() const { return (off() + get()->size()); }
327 uint16_t cksum(const IpPtr &ptr);
329 struct IpOpt : public ip_opt
331 uint8_t type() const { return opt_type; }
332 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
333 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
334 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
335 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
337 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
338 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
339 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
341 const uint8_t *data() const { return opt_data.data8; }
342 void sec(ip_opt_data_sec &sec) const;
343 void lsrr(ip_opt_data_rr &rr) const;
344 void ssrr(ip_opt_data_rr &rr) const;
345 void ts(ip_opt_data_ts &ts) const;
346 uint16_t satid() const { return ntohs(opt_data.satid); }
347 uint16_t mtup() const { return ntohs(opt_data.mtu); }
348 uint16_t mtur() const { return ntohs(opt_data.mtu); }
349 void tr(ip_opt_data_tr &tr) const;
350 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
351 void sdb(std::vector<uint32_t> &vec) const;
358 struct Ip6Hdr : public ip6_hdr
360 uint8_t version() const { return ip6_vfc; }
361 uint32_t flow() const { return ntohl(ip6_flow); }
362 uint16_t plen() const { return ntohs(ip6_plen); }
363 uint16_t hlen() const { return IP6_HDR_LEN; }
364 uint8_t nxt() const { return ip6_nxt; }
365 uint8_t hlim() const { return ip6_hlim; }
367 const uint8_t* src() const { return ip6_src.data; }
368 const uint8_t* dst() const { return ip6_dst.data; }
370 int extensionLength() const;
371 const Ip6Opt* getExt(uint8_t ext) const;
372 const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); }
373 const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); }
374 const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); }
375 uint8_t proto() const;
377 void plen(uint16_t _plen) { ip6_plen = htons(_plen); }
379 int size() const { return IP6_HDR_LEN + extensionLength(); }
380 const uint8_t *bytes() const { return (const uint8_t *)this; }
381 const uint8_t *payload() const { return bytes() + IP6_HDR_LEN
382 + extensionLength(); }
383 uint8_t *bytes() { return (uint8_t *)this; }
384 uint8_t *payload() { return bytes() + IP6_HDR_LEN
385 + extensionLength(); }
396 void set(const EthPacketPtr &ptr)
399 eth_hdr_vlan = false;
402 EthHdr *eth = (EthHdr *)ptr->data;
403 if (eth->type() == ETH_TYPE_IPV6)
411 Ip6Ptr() : p(0), eth_hdr_vlan(false) {}
412 Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); }
413 Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); }
414 Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { }
416 Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr)
417 + ((eth_hdr_vlan) ? 4 : 0)); }
418 Ip6Hdr *operator->() { return get(); }
419 Ip6Hdr &operator*() { return *get(); }
421 const Ip6Hdr *get() const
422 { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr)
423 + ((eth_hdr_vlan) ? 4 : 0)); }
424 const Ip6Hdr *operator->() const { return get(); }
425 const Ip6Hdr &operator*() const { return *get(); }
427 const Ip6Ptr &operator=(const EthPacketPtr &ptr)
428 { set(ptr); return *this; }
429 const Ip6Ptr &operator=(const EthPtr &ptr)
430 { set(ptr.p); return *this; }
431 const Ip6Ptr &operator=(const Ip6Ptr &ptr)
432 { p = ptr.p; return *this; }
434 const EthPacketPtr packet() const { return p; }
435 EthPacketPtr packet() { return p; }
436 bool operator!() const { return !p; }
437 operator bool() const { return (p != nullptr); }
438 int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
439 int pstart() const { return off() + get()->size(); }
442 // Dnet supplied ipv6 opt header is incomplete and
443 // newer NIC card filters expect a more robust
444 // ipv6 header option declaration.
445 struct ip6_opt_fragment {
450 struct ip6_opt_routing_type2 {
457 #define HOME_ADDRESS_OPTION 0xC9
458 struct ip6_opt_dstopts {
462 } __attribute__((packed));
469 struct ip6_opt_fragment fragment;
470 struct ip6_opt_routing_type2 rtType2;
471 struct ip6_opt_dstopts dstOpts;
473 } __attribute__((packed));
475 struct Ip6Opt : public ip6_opt_hdr
477 uint8_t nxt() const { return ext_nxt; }
478 uint8_t extlen() const { return ext_len; }
479 uint8_t len() const { return extlen() + 8; }
481 // Supporting the types of header extensions likely to be encountered:
482 // fragment, routing type 2 and dstopts.
485 uint8_t rtType2Type() const { return ext_data.rtType2.type; }
486 uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; }
487 const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; }
490 uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); }
491 uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); }
493 // Dst Options/Home Address Option
494 uint8_t dstOptType() const { return ext_data.dstOpts.type; }
495 uint8_t dstOptLength() const { return ext_data.dstOpts.length; }
496 const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; }
504 struct TcpHdr : public tcp_hdr
506 uint16_t sport() const { return ntohs(th_sport); }
507 uint16_t dport() const { return ntohs(th_dport); }
508 uint32_t seq() const { return ntohl(th_seq); }
509 uint32_t ack() const { return ntohl(th_ack); }
510 uint8_t off() const { return th_off*4; }
511 uint8_t flags() const { return th_flags & 0x3f; }
512 uint16_t win() const { return ntohs(th_win); }
513 uint16_t sum() const { return th_sum; }
514 uint16_t urp() const { return ntohs(th_urp); }
516 void sum(uint16_t sum) { th_sum = sum; }
517 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
518 void flags(uint8_t _flags) { th_flags = _flags; }
520 bool options(std::vector<const TcpOpt *> &vec) const;
522 int size() const { return off(); }
523 const uint8_t *bytes() const { return (const uint8_t *)this; }
524 const uint8_t *payload() const { return bytes() + size(); }
525 uint8_t *bytes() { return (uint8_t *)this; }
526 uint8_t *payload() { return bytes() + size(); }
535 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
536 void set(const IpPtr &ptr)
538 if (ptr && ptr->proto() == IP_PROTO_TCP)
539 set(ptr.p, ptr.pstart());
543 void set(const Ip6Ptr &ptr)
545 if (ptr && ptr->proto() == IP_PROTO_TCP)
546 set(ptr.p, ptr.pstart());
552 TcpPtr() : p(0), _off(0) {}
553 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
554 TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
555 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
557 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
558 TcpHdr *operator->() { return get(); }
559 TcpHdr &operator*() { return *get(); }
561 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
562 const TcpHdr *operator->() const { return get(); }
563 const TcpHdr &operator*() const { return *get(); }
565 const TcpPtr &operator=(const IpPtr &i)
566 { set(i); return *this; }
567 const TcpPtr &operator=(const TcpPtr &t)
568 { set(t.p, t._off); return *this; }
570 const EthPacketPtr packet() const { return p; }
571 EthPacketPtr packet() { return p; }
572 bool operator!() const { return !p; }
573 operator bool() const { return (p != nullptr); }
574 int off() const { return _off; }
575 int pstart() const { return off() + get()->size(); }
578 uint16_t cksum(const TcpPtr &ptr);
580 struct TcpOpt : public tcp_opt
582 uint8_t type() const { return opt_type; }
583 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
585 bool isopt(int opt) const { return type() == opt; }
587 const uint8_t *data() const { return opt_data.data8; }
589 uint16_t mss() const { return ntohs(opt_data.mss); }
590 uint8_t wscale() const { return opt_data.wscale; }
591 uint32_t echo() const { return ntohl(opt_data.echo); }
592 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
593 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
594 uint32_t cc() const { return ntohl(opt_data.cc); }
595 uint8_t cksum() const{ return opt_data.cksum; }
596 const uint8_t *md5() const { return opt_data.md5; }
598 int size() const { return len(); }
599 const uint8_t *bytes() const { return (const uint8_t *)this; }
600 const uint8_t *payload() const { return bytes() + size(); }
601 uint8_t *bytes() { return (uint8_t *)this; }
602 uint8_t *payload() { return bytes() + size(); }
608 struct UdpHdr : public udp_hdr
610 uint16_t sport() const { return ntohs(uh_sport); }
611 uint16_t dport() const { return ntohs(uh_dport); }
612 uint16_t len() const { return ntohs(uh_ulen); }
613 uint16_t sum() const { return uh_sum; }
615 void sum(uint16_t sum) { uh_sum = sum; }
616 void len(uint16_t _len) { uh_ulen = htons(_len); }
618 int size() const { return sizeof(udp_hdr); }
619 const uint8_t *bytes() const { return (const uint8_t *)this; }
620 const uint8_t *payload() const { return bytes() + size(); }
621 uint8_t *bytes() { return (uint8_t *)this; }
622 uint8_t *payload() { return bytes() + size(); }
631 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
632 void set(const IpPtr &ptr)
634 if (ptr && ptr->proto() == IP_PROTO_UDP)
635 set(ptr.p, ptr.pstart());
639 void set(const Ip6Ptr &ptr)
641 if (ptr && ptr->proto() == IP_PROTO_UDP)
642 set(ptr.p, ptr.pstart());
648 UdpPtr() : p(0), _off(0) {}
649 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
650 UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); }
651 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
653 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
654 UdpHdr *operator->() { return get(); }
655 UdpHdr &operator*() { return *get(); }
657 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
658 const UdpHdr *operator->() const { return get(); }
659 const UdpHdr &operator*() const { return *get(); }
661 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
662 const UdpPtr &operator=(const UdpPtr &t)
663 { set(t.p, t._off); return *this; }
665 const EthPacketPtr packet() const { return p; }
666 EthPacketPtr packet() { return p; }
667 bool operator!() const { return !p; }
668 operator bool() const { return (p != nullptr); }
669 int off() const { return _off; }
670 int pstart() const { return off() + get()->size(); }
673 uint16_t __tu_cksum6(const Ip6Ptr &ip6);
674 uint16_t __tu_cksum(const IpPtr &ip);
675 uint16_t cksum(const UdpPtr &ptr);
677 int hsplit(const EthPacketPtr &ptr);
681 #endif // __BASE_INET_HH__