2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
32 #ifndef __BASE_INET_HH__
33 #define __BASE_INET_HH__
40 #include "base/range.hh"
41 #include "dev/etherpkt.hh"
42 #include "sim/host.hh"
48 #include "dnet/addr.h"
50 #include "dnet/icmp.h"
53 #include "dnet/intf.h"
54 #include "dnet/route.h"
56 #include "dnet/blob.h"
57 #include "dnet/rand.h"
64 struct EthAddr : protected eth_addr
67 void parse(const std::string &addr);
71 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
72 EthAddr(const eth_addr &ea);
73 EthAddr(const std::string &addr);
74 const EthAddr &operator=(const eth_addr &ea);
75 const EthAddr &operator=(const std::string &addr);
77 int size() const { return sizeof(eth_addr); }
79 const uint8_t *bytes() const { return &data[0]; }
80 uint8_t *bytes() { return &data[0]; }
82 const uint8_t *addr() const { return &data[0]; }
83 bool unicast() const { return data[0] == 0x00; }
84 bool multicast() const { return data[0] == 0x01; }
85 bool broadcast() const { return data[0] == 0xff; }
86 std::string string() const;
88 operator uint64_t() const
91 reg |= ((uint64_t)data[0]) << 40;
92 reg |= ((uint64_t)data[1]) << 32;
93 reg |= ((uint64_t)data[2]) << 24;
94 reg |= ((uint64_t)data[3]) << 16;
95 reg |= ((uint64_t)data[4]) << 8;
96 reg |= ((uint64_t)data[5]) << 0;
102 std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
103 bool operator==(const EthAddr &left, const EthAddr &right);
105 struct EthHdr : public eth_hdr
107 uint16_t type() const { return ntohs(eth_type); }
108 const EthAddr &src() const { return *(EthAddr *)ð_src; }
109 const EthAddr &dst() const { return *(EthAddr *)ð_dst; }
111 int size() const { return sizeof(eth_hdr); }
113 const uint8_t *bytes() const { return (const uint8_t *)this; }
114 const uint8_t *payload() const { return bytes() + size(); }
115 uint8_t *bytes() { return (uint8_t *)this; }
116 uint8_t *payload() { return bytes() + size(); }
127 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
129 EthHdr *operator->() { return (EthHdr *)p->data; }
130 EthHdr &operator*() { return *(EthHdr *)p->data; }
131 operator EthHdr *() { return (EthHdr *)p->data; }
133 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
134 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
135 operator const EthHdr *() const { return (const EthHdr *)p->data; }
137 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
139 const EthPacketPtr packet() const { return p; }
140 EthPacketPtr packet() { return p; }
141 bool operator!() const { return !p; }
142 operator bool() const { return p; }
143 int off() const { return 0; }
144 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
151 struct IpHdr : public ip_hdr
153 uint8_t version() const { return ip_v; }
154 uint8_t hlen() const { return ip_hl * 4; }
155 uint8_t tos() const { return ip_tos; }
156 uint16_t len() const { return ntohs(ip_len); }
157 uint16_t id() const { return ntohs(ip_id); }
158 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
159 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
160 uint8_t ttl() const { return ip_ttl; }
161 uint8_t proto() const { return ip_p; }
162 uint16_t sum() const { return ip_sum; }
163 uint32_t src() const { return ntohl(ip_src); }
164 uint32_t dst() const { return ntohl(ip_dst); }
166 void sum(uint16_t sum) { ip_sum = sum; }
167 void id(uint16_t _id) { ip_id = htons(_id); }
168 void len(uint16_t _len) { ip_len = htons(_len); }
171 bool options(std::vector<const IpOpt *> &vec) const;
173 int size() const { return hlen(); }
174 const uint8_t *bytes() const { return (const uint8_t *)this; }
175 const uint8_t *payload() const { return bytes() + size(); }
176 uint8_t *bytes() { return (uint8_t *)this; }
177 uint8_t *payload() { return bytes() + size(); }
187 void set(const EthPacketPtr &ptr)
192 EthHdr *eth = (EthHdr *)ptr->data;
193 if (eth->type() == ETH_TYPE_IP)
200 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); }
201 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); }
202 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
204 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
205 IpHdr *operator->() { return get(); }
206 IpHdr &operator*() { return *get(); }
208 const IpHdr *get() const
209 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
210 const IpHdr *operator->() const { return get(); }
211 const IpHdr &operator*() const { return *get(); }
213 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
214 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
215 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
217 const EthPacketPtr packet() const { return p; }
218 EthPacketPtr packet() { return p; }
219 bool operator!() const { return !p; }
220 operator bool() const { return p; }
221 int off() const { return sizeof(eth_hdr); }
222 int pstart() const { return off() + get()->size(); }
225 uint16_t cksum(const IpPtr &ptr);
227 struct IpOpt : public ip_opt
229 uint8_t type() const { return opt_type; }
230 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
231 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
232 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
233 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
235 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
236 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
237 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
239 const uint8_t *data() const { return opt_data.data8; }
240 void sec(ip_opt_data_sec &sec) const;
241 void lsrr(ip_opt_data_rr &rr) const;
242 void ssrr(ip_opt_data_rr &rr) const;
243 void ts(ip_opt_data_ts &ts) const;
244 uint16_t satid() const { return ntohs(opt_data.satid); }
245 uint16_t mtup() const { return ntohs(opt_data.mtu); }
246 uint16_t mtur() const { return ntohs(opt_data.mtu); }
247 void tr(ip_opt_data_tr &tr) const;
248 const uint32_t *addext() const { return &opt_data.addext[0]; }
249 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
250 void sdb(std::vector<uint32_t> &vec) const;
257 struct TcpHdr : public tcp_hdr
259 uint16_t sport() const { return ntohs(th_sport); }
260 uint16_t dport() const { return ntohs(th_dport); }
261 uint32_t seq() const { return ntohl(th_seq); }
262 uint32_t ack() const { return ntohl(th_ack); }
263 uint8_t off() const { return th_off; }
264 uint8_t flags() const { return th_flags & 0x3f; }
265 uint16_t win() const { return ntohs(th_win); }
266 uint16_t sum() const { return th_sum; }
267 uint16_t urp() const { return ntohs(th_urp); }
269 void sum(uint16_t sum) { th_sum = sum; }
270 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
271 void flags(uint8_t _flags) { th_flags = _flags; }
273 bool options(std::vector<const TcpOpt *> &vec) const;
275 int size() const { return off(); }
276 const uint8_t *bytes() const { return (const uint8_t *)this; }
277 const uint8_t *payload() const { return bytes() + size(); }
278 uint8_t *bytes() { return (uint8_t *)this; }
279 uint8_t *payload() { return bytes() + size(); }
288 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
289 void set(const IpPtr &ptr)
291 if (ptr && ptr->proto() == IP_PROTO_TCP)
292 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
298 TcpPtr() : p(0), _off(0) {}
299 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
300 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
302 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
303 TcpHdr *operator->() { return get(); }
304 TcpHdr &operator*() { return *get(); }
306 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
307 const TcpHdr *operator->() const { return get(); }
308 const TcpHdr &operator*() const { return *get(); }
310 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
311 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; }
313 const EthPacketPtr packet() const { return p; }
314 EthPacketPtr packet() { return p; }
315 bool operator!() const { return !p; }
316 operator bool() const { return p; }
317 int off() const { return _off; }
318 int pstart() const { return off() + get()->size(); }
321 uint16_t cksum(const TcpPtr &ptr);
323 typedef Range<uint16_t> SackRange;
325 struct TcpOpt : public tcp_opt
327 uint8_t type() const { return opt_type; }
328 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
330 bool isopt(int opt) const { return type() == opt; }
332 const uint8_t *data() const { return opt_data.data8; }
334 uint16_t mss() const { return ntohs(opt_data.mss); }
335 uint8_t wscale() const { return opt_data.wscale; }
336 bool sack(std::vector<SackRange> &vec) const;
337 uint32_t echo() const { return ntohl(opt_data.echo); }
338 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
339 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
340 uint32_t cc() const { return ntohl(opt_data.cc); }
341 uint8_t cksum() const{ return opt_data.cksum; }
342 const uint8_t *md5() const { return opt_data.md5; }
344 int size() const { return len(); }
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(); }
354 struct UdpHdr : public udp_hdr
356 uint16_t sport() const { return ntohs(uh_sport); }
357 uint16_t dport() const { return ntohs(uh_dport); }
358 uint16_t len() const { return ntohs(uh_ulen); }
359 uint16_t sum() const { return uh_sum; }
361 void sum(uint16_t sum) { uh_sum = sum; }
362 void len(uint16_t _len) { uh_ulen = htons(_len); }
364 int size() const { return sizeof(udp_hdr); }
365 const uint8_t *bytes() const { return (const uint8_t *)this; }
366 const uint8_t *payload() const { return bytes() + size(); }
367 uint8_t *bytes() { return (uint8_t *)this; }
368 uint8_t *payload() { return bytes() + size(); }
377 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
378 void set(const IpPtr &ptr)
380 if (ptr && ptr->proto() == IP_PROTO_UDP)
381 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
387 UdpPtr() : p(0), _off(0) {}
388 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
389 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
391 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
392 UdpHdr *operator->() { return get(); }
393 UdpHdr &operator*() { return *get(); }
395 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
396 const UdpHdr *operator->() const { return get(); }
397 const UdpHdr &operator*() const { return *get(); }
399 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
400 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; }
402 const EthPacketPtr packet() const { return p; }
403 EthPacketPtr packet() { return p; }
404 bool operator!() const { return !p; }
405 operator bool() const { return p; }
406 int off() const { return _off; }
407 int pstart() const { return off() + get()->size(); }
410 uint16_t cksum(const UdpPtr &ptr);
412 int hsplit(const EthPacketPtr &ptr);
414 /* namespace Net */ }
416 #endif // __BASE_INET_HH__