Deleted Added
sdiff udiff text old ( 9200:16de812c5f53 ) new ( 9235:5aa4896ed55a )
full compact
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Steve Reinhardt
31 * Gabe Black
32 */
33
34#ifndef __BASE_INET_HH__
35#define __BASE_INET_HH__
36
37#include <iosfwd>
38#include <string>
39#include <utility>
40#include <vector>
41
42#include "base/range.hh"
43#include "base/types.hh"
44#include "dev/etherpkt.hh"
45#include "dnet/os.h"
46#include "dnet/eth.h"
47#include "dnet/ip.h"
48#include "dnet/ip6.h"
49#include "dnet/addr.h"
50#include "dnet/arp.h"
51#include "dnet/icmp.h"
52#include "dnet/tcp.h"
53#include "dnet/udp.h"
54#include "dnet/intf.h"
55#include "dnet/route.h"
56#include "dnet/fw.h"
57#include "dnet/blob.h"
58#include "dnet/rand.h"
59
60namespace Net {
61
62/*
63 * Ethernet Stuff
64 */
65struct EthAddr : protected eth_addr
66{
67 protected:
68 void parse(const std::string &addr);
69
70 public:
71 EthAddr();
72 EthAddr(const uint8_t ea[ETH_ADDR_LEN]);
73 EthAddr(const eth_addr &ea);
74 EthAddr(const std::string &addr);
75 const EthAddr &operator=(const eth_addr &ea);
76 const EthAddr &operator=(const std::string &addr);
77
78 int size() const { return sizeof(eth_addr); }
79
80 const uint8_t *bytes() const { return &data[0]; }
81 uint8_t *bytes() { return &data[0]; }
82
83 const uint8_t *addr() const { return &data[0]; }
84 bool unicast() const { return data[0] == 0x00; }
85 bool multicast() const { return data[0] == 0x01; }
86 bool broadcast() const { return data[0] == 0xff; }
87 std::string string() const;
88
89 operator uint64_t() const
90 {
91 uint64_t reg = 0;
92 reg |= ((uint64_t)data[0]) << 40;
93 reg |= ((uint64_t)data[1]) << 32;
94 reg |= ((uint64_t)data[2]) << 24;
95 reg |= ((uint64_t)data[3]) << 16;
96 reg |= ((uint64_t)data[4]) << 8;
97 reg |= ((uint64_t)data[5]) << 0;
98 return reg;
99 }
100
101};
102
103std::ostream &operator<<(std::ostream &stream, const EthAddr &ea);
104bool operator==(const EthAddr &left, const EthAddr &right);
105
106struct EthHdr : public eth_hdr
107{
108 uint16_t type() const { return ntohs(eth_type); }
109 const EthAddr &src() const { return *(EthAddr *)&eth_src; }
110 const EthAddr &dst() const { return *(EthAddr *)&eth_dst; }
111
112 int size() const { return sizeof(eth_hdr); }
113
114 const uint8_t *bytes() const { return (const uint8_t *)this; }
115 const uint8_t *payload() const { return bytes() + size(); }
116 uint8_t *bytes() { return (uint8_t *)this; }
117 uint8_t *payload() { return bytes() + size(); }
118};
119
120class EthPtr
121{
122 protected:
123 friend class IpPtr;
124 EthPacketPtr p;
125
126 public:
127 EthPtr() {}
128 EthPtr(const EthPacketPtr &ptr) : p(ptr) { }
129
130 EthHdr *operator->() { return (EthHdr *)p->data; }
131 EthHdr &operator*() { return *(EthHdr *)p->data; }
132 operator EthHdr *() { return (EthHdr *)p->data; }
133
134 const EthHdr *operator->() const { return (const EthHdr *)p->data; }
135 const EthHdr &operator*() const { return *(const EthHdr *)p->data; }
136 operator const EthHdr *() const { return (const EthHdr *)p->data; }
137
138 const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; }
139
140 const EthPacketPtr packet() const { return p; }
141 EthPacketPtr packet() { return p; }
142 bool operator!() const { return !p; }
143 operator bool() const { return p; }
144 int off() const { return 0; }
145 int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
146};
147
148/*
149 * IP Stuff
150 */
151struct IpAddress
152{
153 protected:
154 uint32_t _ip;
155
156 public:
157 IpAddress() : _ip(0)
158 {}
159 IpAddress(const uint32_t __ip) : _ip(__ip)
160 {}
161
162 uint32_t ip() const { return _ip; }
163
164 std::string string() const;
165};
166
167std::ostream &operator<<(std::ostream &stream, const IpAddress &ia);
168bool operator==(const IpAddress &left, const IpAddress &right);
169
170struct IpNetmask : public IpAddress
171{
172 protected:
173 uint8_t _netmask;
174
175 public:
176 IpNetmask() : IpAddress(), _netmask(0)
177 {}
178 IpNetmask(const uint32_t __ip, const uint8_t __netmask) :
179 IpAddress(__ip), _netmask(__netmask)
180 {}
181
182 uint8_t netmask() const { return _netmask; }
183
184 std::string string() const;
185};
186
187std::ostream &operator<<(std::ostream &stream, const IpNetmask &in);
188bool operator==(const IpNetmask &left, const IpNetmask &right);
189
190struct IpWithPort : public IpAddress
191{
192 protected:
193 uint16_t _port;
194
195 public:
196 IpWithPort() : IpAddress(), _port(0)
197 {}
198 IpWithPort(const uint32_t __ip, const uint16_t __port) :
199 IpAddress(__ip), _port(__port)
200 {}
201
202 uint8_t port() const { return _port; }
203
204 std::string string() const;
205};
206
207std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp);
208bool operator==(const IpWithPort &left, const IpWithPort &right);
209
210struct IpOpt;
211struct IpHdr : public ip_hdr
212{
213 uint8_t version() const { return ip_v; }
214 uint8_t hlen() const { return ip_hl * 4; }
215 uint8_t tos() const { return ip_tos; }
216 uint16_t len() const { return ntohs(ip_len); }
217 uint16_t id() const { return ntohs(ip_id); }
218 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
219 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
220 uint8_t ttl() const { return ip_ttl; }
221 uint8_t proto() const { return ip_p; }
222 uint16_t sum() const { return ip_sum; }
223 uint32_t src() const { return ntohl(ip_src); }
224 uint32_t dst() const { return ntohl(ip_dst); }
225
226 void sum(uint16_t sum) { ip_sum = sum; }
227 void id(uint16_t _id) { ip_id = htons(_id); }
228 void len(uint16_t _len) { ip_len = htons(_len); }
229
230
231 bool options(std::vector<const IpOpt *> &vec) const;
232
233 int size() const { return hlen(); }
234 const uint8_t *bytes() const { return (const uint8_t *)this; }
235 const uint8_t *payload() const { return bytes() + size(); }
236 uint8_t *bytes() { return (uint8_t *)this; }
237 uint8_t *payload() { return bytes() + size(); }
238};
239
240class IpPtr
241{
242 protected:
243 friend class TcpPtr;
244 friend class UdpPtr;
245 EthPacketPtr p;
246
247 void set(const EthPacketPtr &ptr)
248 {
249 p = 0;
250
251 if (ptr) {
252 EthHdr *eth = (EthHdr *)ptr->data;
253 if (eth->type() == ETH_TYPE_IP)
254 p = ptr;
255 }
256 }
257
258 public:
259 IpPtr() : p(0) {}
260 IpPtr(const EthPacketPtr &ptr) : p(0) { set(ptr); }
261 IpPtr(const EthPtr &ptr) : p(0) { set(ptr.p); }
262 IpPtr(const IpPtr &ptr) : p(ptr.p) { }
263
264 IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr)); }
265 IpHdr *operator->() { return get(); }
266 IpHdr &operator*() { return *get(); }
267
268 const IpHdr *get() const
269 { return (const IpHdr *)(p->data + sizeof(eth_hdr)); }
270 const IpHdr *operator->() const { return get(); }
271 const IpHdr &operator*() const { return *get(); }
272
273 const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; }
274 const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; }
275 const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; }
276
277 const EthPacketPtr packet() const { return p; }
278 EthPacketPtr packet() { return p; }
279 bool operator!() const { return !p; }
280 operator bool() const { return p; }
281 int off() const { return sizeof(eth_hdr); }
282 int pstart() const { return off() + get()->size(); }
283};
284
285uint16_t cksum(const IpPtr &ptr);
286
287struct IpOpt : public ip_opt
288{
289 uint8_t type() const { return opt_type; }
290 uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); }
291 uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); }
292 uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); }
293 uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
294
295 bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); }
296 bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); }
297 bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); }
298
299 const uint8_t *data() const { return opt_data.data8; }
300 void sec(ip_opt_data_sec &sec) const;
301 void lsrr(ip_opt_data_rr &rr) const;
302 void ssrr(ip_opt_data_rr &rr) const;
303 void ts(ip_opt_data_ts &ts) const;
304 uint16_t satid() const { return ntohs(opt_data.satid); }
305 uint16_t mtup() const { return ntohs(opt_data.mtu); }
306 uint16_t mtur() const { return ntohs(opt_data.mtu); }
307 void tr(ip_opt_data_tr &tr) const;
308 const uint32_t *addext() const { return &opt_data.addext[0]; }
309 uint16_t rtralt() const { return ntohs(opt_data.rtralt); }
310 void sdb(std::vector<uint32_t> &vec) const;
311};
312
313/*
314 * TCP Stuff
315 */
316struct TcpOpt;
317struct TcpHdr : public tcp_hdr
318{
319 uint16_t sport() const { return ntohs(th_sport); }
320 uint16_t dport() const { return ntohs(th_dport); }
321 uint32_t seq() const { return ntohl(th_seq); }
322 uint32_t ack() const { return ntohl(th_ack); }
323 uint8_t off() const { return th_off; }
324 uint8_t flags() const { return th_flags & 0x3f; }
325 uint16_t win() const { return ntohs(th_win); }
326 uint16_t sum() const { return th_sum; }
327 uint16_t urp() const { return ntohs(th_urp); }
328
329 void sum(uint16_t sum) { th_sum = sum; }
330 void seq(uint32_t _seq) { th_seq = htonl(_seq); }
331 void flags(uint8_t _flags) { th_flags = _flags; }
332
333 bool options(std::vector<const TcpOpt *> &vec) const;
334
335 int size() const { return off(); }
336 const uint8_t *bytes() const { return (const uint8_t *)this; }
337 const uint8_t *payload() const { return bytes() + size(); }
338 uint8_t *bytes() { return (uint8_t *)this; }
339 uint8_t *payload() { return bytes() + size(); }
340};
341
342class TcpPtr
343{
344 protected:
345 EthPacketPtr p;
346 int _off;
347
348 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
349 void set(const IpPtr &ptr)
350 {
351 if (ptr && ptr->proto() == IP_PROTO_TCP)
352 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
353 else
354 set(0, 0);
355 }
356
357 public:
358 TcpPtr() : p(0), _off(0) {}
359 TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
360 TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
361
362 TcpHdr *get() { return (TcpHdr *)(p->data + _off); }
363 TcpHdr *operator->() { return get(); }
364 TcpHdr &operator*() { return *get(); }
365
366 const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); }
367 const TcpHdr *operator->() const { return get(); }
368 const TcpHdr &operator*() const { return *get(); }
369
370 const TcpPtr &operator=(const IpPtr &i) { set(i); return *this; }
371 const TcpPtr &operator=(const TcpPtr &t) { set(t.p, t._off); return *this; }
372
373 const EthPacketPtr packet() const { return p; }
374 EthPacketPtr packet() { return p; }
375 bool operator!() const { return !p; }
376 operator bool() const { return p; }
377 int off() const { return _off; }
378 int pstart() const { return off() + get()->size(); }
379};
380
381uint16_t cksum(const TcpPtr &ptr);
382
383struct TcpOpt : public tcp_opt
384{
385 uint8_t type() const { return opt_type; }
386 uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; }
387
388 bool isopt(int opt) const { return type() == opt; }
389
390 const uint8_t *data() const { return opt_data.data8; }
391
392 uint16_t mss() const { return ntohs(opt_data.mss); }
393 uint8_t wscale() const { return opt_data.wscale; }
394 uint32_t echo() const { return ntohl(opt_data.echo); }
395 uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); }
396 uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); }
397 uint32_t cc() const { return ntohl(opt_data.cc); }
398 uint8_t cksum() const{ return opt_data.cksum; }
399 const uint8_t *md5() const { return opt_data.md5; }
400
401 int size() const { return len(); }
402 const uint8_t *bytes() const { return (const uint8_t *)this; }
403 const uint8_t *payload() const { return bytes() + size(); }
404 uint8_t *bytes() { return (uint8_t *)this; }
405 uint8_t *payload() { return bytes() + size(); }
406};
407
408/*
409 * UDP Stuff
410 */
411struct UdpHdr : public udp_hdr
412{
413 uint16_t sport() const { return ntohs(uh_sport); }
414 uint16_t dport() const { return ntohs(uh_dport); }
415 uint16_t len() const { return ntohs(uh_ulen); }
416 uint16_t sum() const { return uh_sum; }
417
418 void sum(uint16_t sum) { uh_sum = sum; }
419 void len(uint16_t _len) { uh_ulen = htons(_len); }
420
421 int size() const { return sizeof(udp_hdr); }
422 const uint8_t *bytes() const { return (const uint8_t *)this; }
423 const uint8_t *payload() const { return bytes() + size(); }
424 uint8_t *bytes() { return (uint8_t *)this; }
425 uint8_t *payload() { return bytes() + size(); }
426};
427
428class UdpPtr
429{
430 protected:
431 EthPacketPtr p;
432 int _off;
433
434 void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; }
435 void set(const IpPtr &ptr)
436 {
437 if (ptr && ptr->proto() == IP_PROTO_UDP)
438 set(ptr.p, sizeof(eth_hdr) + ptr->hlen());
439 else
440 set(0, 0);
441 }
442
443 public:
444 UdpPtr() : p(0), _off(0) {}
445 UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); }
446 UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {}
447
448 UdpHdr *get() { return (UdpHdr *)(p->data + _off); }
449 UdpHdr *operator->() { return get(); }
450 UdpHdr &operator*() { return *get(); }
451
452 const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); }
453 const UdpHdr *operator->() const { return get(); }
454 const UdpHdr &operator*() const { return *get(); }
455
456 const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; }
457 const UdpPtr &operator=(const UdpPtr &t) { set(t.p, t._off); return *this; }
458
459 const EthPacketPtr packet() const { return p; }
460 EthPacketPtr packet() { return p; }
461 bool operator!() const { return !p; }
462 operator bool() const { return p; }
463 int off() const { return _off; }
464 int pstart() const { return off() + get()->size(); }
465};
466
467uint16_t cksum(const UdpPtr &ptr);
468
469int hsplit(const EthPacketPtr &ptr);
470
471} // namespace Net
472
473#endif // __BASE_INET_HH__