| 1 |
/* |
| 2 |
* Copyright (c) 2006 Christophe Fillot. |
| 3 |
* E-mail: [email protected] |
| 4 |
* |
| 5 |
* net.h: Protocol Headers and Constants Definitions. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __NET_H__ |
| 9 |
#define __NET_H__ 1 |
| 10 |
|
| 11 |
#include "dynamips_common.h" |
| 12 |
|
| 13 |
#include <arpa/inet.h> |
| 14 |
#include <netdb.h> |
| 15 |
/* TODO missing in MinGW */ |
| 16 |
#include <sys/socket.h> |
| 17 |
#include <netinet/in.h> |
| 18 |
|
| 19 |
#define N_IP_ADDR_LEN 4 |
| 20 |
#define N_IP_ADDR_BITS 32 |
| 21 |
|
| 22 |
#define N_IPV6_ADDR_LEN 16 |
| 23 |
#define N_IPV6_ADDR_BITS 128 |
| 24 |
|
| 25 |
/* IPv4 Address definition */ |
| 26 |
typedef m_uint32_t n_ip_addr_t; |
| 27 |
|
| 28 |
/* IP Network definition */ |
| 29 |
typedef struct { |
| 30 |
n_ip_addr_t net_addr; |
| 31 |
n_ip_addr_t net_mask; |
| 32 |
}n_ip_network_t; |
| 33 |
|
| 34 |
/* IPv6 Address definition */ |
| 35 |
typedef struct { |
| 36 |
union { |
| 37 |
m_uint32_t u6_addr32[4]; |
| 38 |
m_uint16_t u6_addr16[8]; |
| 39 |
m_uint8_t u6_addr8[16]; |
| 40 |
}ip6; |
| 41 |
}n_ipv6_addr_t; |
| 42 |
|
| 43 |
/* IPv6 Network definition */ |
| 44 |
typedef struct { |
| 45 |
n_ipv6_addr_t net_addr; |
| 46 |
u_int net_mask; |
| 47 |
}n_ipv6_network_t; |
| 48 |
|
| 49 |
/* IP header minimum length */ |
| 50 |
#define N_IP_MIN_HLEN 5 |
| 51 |
|
| 52 |
/* IP: Common Protocols */ |
| 53 |
#define N_IP_PROTO_ICMP 1 |
| 54 |
#define N_IP_PROTO_IGMP 2 |
| 55 |
#define N_IP_PROTO_TCP 6 |
| 56 |
#define N_IP_PROTO_UDP 17 |
| 57 |
#define N_IP_PROTO_IPV6 41 |
| 58 |
#define N_IP_PROTO_GRE 47 |
| 59 |
#define N_IP_PROTO_ESP 50 |
| 60 |
#define N_IP_PROTO_AH 51 |
| 61 |
#define N_IP_PROTO_ICMPV6 58 |
| 62 |
#define N_IP_PROTO_EIGRP 88 |
| 63 |
#define N_IP_PROTO_OSPF 89 |
| 64 |
#define N_IP_PROTO_PIM 103 |
| 65 |
#define N_IP_PROTO_SCTP 132 |
| 66 |
#define N_IP_PROTO_MAX 256 |
| 67 |
|
| 68 |
#define N_IP_FLAG_DF 0x4000 |
| 69 |
#define N_IP_FLAG_MF 0x2000 |
| 70 |
#define N_IP_OFFMASK 0x1fff |
| 71 |
|
| 72 |
/* Maximum number of ports */ |
| 73 |
#define N_IP_PORT_MAX 65536 |
| 74 |
|
| 75 |
/* TCP: Header Flags */ |
| 76 |
#define N_TCP_FIN 0x01 |
| 77 |
#define N_TCP_SYN 0x02 |
| 78 |
#define N_TCP_RST 0x04 |
| 79 |
#define N_TCP_PUSH 0x08 |
| 80 |
#define N_TCP_ACK 0x10 |
| 81 |
#define N_TCP_URG 0x20 |
| 82 |
|
| 83 |
#define N_TCP_FLAGMASK 0x3F |
| 84 |
|
| 85 |
/* IPv6 Header Codes */ |
| 86 |
#define N_IPV6_PROTO_ICMP 58 |
| 87 |
#define N_IPV6_OPT_HOP_BY_HOP 0 /* Hop-by-Hop header */ |
| 88 |
#define N_IPV6_OPT_DST 60 /* Destination Options Header */ |
| 89 |
#define N_IPV6_OPT_ROUTE 43 /* Routing header */ |
| 90 |
#define N_IPV6_OPT_FRAG 44 /* Fragment Header */ |
| 91 |
#define N_IPV6_OPT_AH 51 /* Authentication Header */ |
| 92 |
#define N_IPV6_OPT_ESP 50 /* Encryption Security Payload */ |
| 93 |
#define N_IPV6_OPT_COMP 108 /* Payload Compression Protocol */ |
| 94 |
#define N_IPV6_OPT_END 59 /* No more headers */ |
| 95 |
|
| 96 |
/* Standard Ethernet MTU */ |
| 97 |
#define N_ETH_MTU 1500 |
| 98 |
|
| 99 |
/* Ethernet Constants */ |
| 100 |
#define N_ETH_ALEN 6 |
| 101 |
#define N_ETH_HLEN sizeof(n_eth_hdr_t) |
| 102 |
|
| 103 |
/* CRC Length */ |
| 104 |
#define N_ETH_CRC_LEN 4 |
| 105 |
|
| 106 |
/* Minimum size for ethernet payload */ |
| 107 |
#define N_ETH_MIN_DATA_LEN 46 |
| 108 |
#define N_ETH_MIN_FRAME_LEN (N_ETH_MIN_DATA_LEN + N_ETH_HLEN) |
| 109 |
|
| 110 |
#define N_ETH_PROTO_IP 0x0800 |
| 111 |
#define N_ETH_PROTO_IPV6 0x86DD |
| 112 |
#define N_ETH_PROTO_ARP 0x0806 |
| 113 |
#define N_ETH_PROTO_DOT1Q 0x8100 |
| 114 |
#define N_ETH_PROTO_DOT1Q_2 0x9100 |
| 115 |
#define N_ETH_PROTO_DOT1Q_3 0x9200 |
| 116 |
#define N_ETH_PROTO_DOT1Q_4 0x88A8 |
| 117 |
#define N_ETH_PROTO_MPLS 0x8847 |
| 118 |
#define N_ETH_PROTO_MPLS_MC 0x8848 |
| 119 |
#define N_ETH_PROTO_LOOP 0x9000 |
| 120 |
|
| 121 |
/* size needed for a string buffer */ |
| 122 |
#define N_ETH_SLEN (N_ETH_ALEN*3) |
| 123 |
|
| 124 |
/* ARP opcodes */ |
| 125 |
#define N_ARP_REQUEST 0x1 |
| 126 |
#define N_ARP_REPLY 0x2 |
| 127 |
|
| 128 |
/* Ethernet Address */ |
| 129 |
typedef struct { |
| 130 |
m_uint8_t eth_addr_byte[N_ETH_ALEN]; |
| 131 |
} __attribute__ ((__packed__)) n_eth_addr_t; |
| 132 |
|
| 133 |
/* Ethernet Header */ |
| 134 |
typedef struct { |
| 135 |
n_eth_addr_t daddr; /* destination eth addr */ |
| 136 |
n_eth_addr_t saddr; /* source ether addr */ |
| 137 |
m_uint16_t type; /* packet type ID field */ |
| 138 |
} __attribute__ ((__packed__)) n_eth_hdr_t; |
| 139 |
|
| 140 |
/* 802.1Q Ethernet Header */ |
| 141 |
typedef struct { |
| 142 |
n_eth_addr_t daddr; /* destination eth addr */ |
| 143 |
n_eth_addr_t saddr; /* source ether addr */ |
| 144 |
m_uint16_t type; /* packet type ID field (0x8100) */ |
| 145 |
m_uint16_t vlan_id; /* VLAN id + CoS */ |
| 146 |
} __attribute__ ((__packed__)) n_eth_dot1q_hdr_t; |
| 147 |
|
| 148 |
/* LLC header */ |
| 149 |
typedef struct { |
| 150 |
m_uint8_t dsap; |
| 151 |
m_uint8_t ssap; |
| 152 |
m_uint8_t ctrl; |
| 153 |
} __attribute__ ((__packed__)) n_eth_llc_hdr_t; |
| 154 |
|
| 155 |
/* SNAP header */ |
| 156 |
typedef struct { |
| 157 |
m_uint8_t oui[3]; |
| 158 |
m_uint16_t type; |
| 159 |
} __attribute__ ((__packed__)) n_eth_snap_hdr_t; |
| 160 |
|
| 161 |
/* Cisco ISL header */ |
| 162 |
typedef struct { |
| 163 |
m_uint16_t hsa1; /* High bits of source MAC address */ |
| 164 |
m_uint8_t hsa2; /* (in theory: 0x00-00-0c) */ |
| 165 |
m_uint16_t vlan; /* VLAN + BPDU */ |
| 166 |
m_uint16_t index; /* Index port of source */ |
| 167 |
m_uint16_t res; /* Reserved for TokenRing and FDDI */ |
| 168 |
} __attribute__ ((__packed__)) n_eth_isl_hdr_t; |
| 169 |
|
| 170 |
#define N_ISL_HDR_SIZE (sizeof(n_eth_llc_hdr_t) + sizeof(n_eth_isl_hdr_t)) |
| 171 |
|
| 172 |
/* Cisco SCP/RBCP header */ |
| 173 |
typedef struct { |
| 174 |
m_uint8_t sa; /* Source Address */ |
| 175 |
m_uint8_t da; /* Destination Address */ |
| 176 |
m_uint16_t len; /* Data Length */ |
| 177 |
m_uint8_t dsap; /* Destination Service Access Point */ |
| 178 |
m_uint8_t ssap; /* Source Service Access Point */ |
| 179 |
m_uint16_t opcode; /* Opcode */ |
| 180 |
m_uint16_t seqno; /* Sequence Number */ |
| 181 |
m_uint8_t flags; /* Flags: command/response */ |
| 182 |
m_uint8_t unk1; /* Unknown */ |
| 183 |
m_uint16_t unk2; /* Unknown */ |
| 184 |
m_uint16_t unk3; /* Unknown */ |
| 185 |
} __attribute__ ((__packed__)) n_scp_hdr_t; |
| 186 |
|
| 187 |
/* ----- ARP Header for the IPv4 protocol over Ethernet ------------------ */ |
| 188 |
typedef struct { |
| 189 |
m_uint16_t hw_type; /* Hardware type */ |
| 190 |
m_uint16_t proto_type; /* L3 protocol */ |
| 191 |
m_uint8_t hw_len; /* Length of hardware address */ |
| 192 |
m_uint8_t proto_len; /* Length of L3 address */ |
| 193 |
m_uint16_t opcode; /* ARP Opcode */ |
| 194 |
n_eth_addr_t eth_saddr; /* Source hardware address */ |
| 195 |
m_uint32_t ip_saddr; /* Source IP address */ |
| 196 |
n_eth_addr_t eth_daddr; /* Dest. hardware address */ |
| 197 |
m_uint32_t ip_daddr; /* Dest. IP address */ |
| 198 |
} __attribute__ ((__packed__)) n_arp_hdr_t; |
| 199 |
|
| 200 |
/* ----- IP Header ------------------------------------------------------- */ |
| 201 |
typedef struct { |
| 202 |
m_uint8_t ihl; |
| 203 |
m_uint8_t tos; |
| 204 |
m_uint16_t tot_len; |
| 205 |
m_uint16_t id; |
| 206 |
m_uint16_t frag_off; |
| 207 |
m_uint8_t ttl; |
| 208 |
m_uint8_t proto; |
| 209 |
m_uint16_t cksum; |
| 210 |
m_uint32_t saddr; |
| 211 |
m_uint32_t daddr; |
| 212 |
}n_ip_hdr_t; |
| 213 |
|
| 214 |
|
| 215 |
/* ----- UDP Header ------------------------------------------------------ */ |
| 216 |
typedef struct { |
| 217 |
m_uint16_t sport; |
| 218 |
m_uint16_t dport; |
| 219 |
m_uint16_t len; |
| 220 |
m_uint16_t cksum; |
| 221 |
}n_udp_hdr_t; |
| 222 |
|
| 223 |
/* ----- TCP Header ------------------------------------------------------ */ |
| 224 |
typedef struct { |
| 225 |
m_uint16_t sport; |
| 226 |
m_uint16_t dport; |
| 227 |
m_uint32_t seq; |
| 228 |
m_uint32_t ack_seq; |
| 229 |
m_uint8_t offset; |
| 230 |
m_uint8_t flags; |
| 231 |
m_uint16_t window; |
| 232 |
m_uint16_t cksum; |
| 233 |
m_uint16_t urg_ptr; |
| 234 |
}n_tcp_hdr_t; |
| 235 |
|
| 236 |
/* ----- Packet Context -------------------------------------------------- */ |
| 237 |
#define N_PKT_CTX_FLAG_ETHV2 0x0001 |
| 238 |
#define N_PKT_CTX_FLAG_VLAN 0x0002 |
| 239 |
#define N_PKT_CTX_FLAG_L3_ARP 0x0008 |
| 240 |
#define N_PKT_CTX_FLAG_L3_IP 0x0010 |
| 241 |
#define N_PKT_CTX_FLAG_L4_UDP 0x0020 |
| 242 |
#define N_PKT_CTX_FLAG_L4_TCP 0x0040 |
| 243 |
#define N_PKT_CTX_FLAG_L4_ICMP 0x0080 |
| 244 |
#define N_PKT_CTX_FLAG_IPH_OK 0x0100 |
| 245 |
#define N_PKT_CTX_FLAG_IP_FRAG 0x0200 |
| 246 |
|
| 247 |
typedef struct { |
| 248 |
/* full packet */ |
| 249 |
m_uint8_t *pkt; |
| 250 |
size_t pkt_len; |
| 251 |
|
| 252 |
/* Packet flags */ |
| 253 |
m_uint32_t flags; |
| 254 |
|
| 255 |
/* VLAN information */ |
| 256 |
m_uint16_t vlan_id; |
| 257 |
|
| 258 |
/* L4 protocol for IP */ |
| 259 |
u_int ip_l4_proto; |
| 260 |
|
| 261 |
/* L3 header */ |
| 262 |
union { |
| 263 |
n_arp_hdr_t *arp; |
| 264 |
n_ip_hdr_t *ip; |
| 265 |
void *l3; |
| 266 |
}; |
| 267 |
|
| 268 |
/* L4 header */ |
| 269 |
union { |
| 270 |
n_udp_hdr_t *udp; |
| 271 |
n_tcp_hdr_t *tcp; |
| 272 |
void *l4; |
| 273 |
}; |
| 274 |
}n_pkt_ctx_t; |
| 275 |
|
| 276 |
/* ----------------------------------------------------------------------- */ |
| 277 |
|
| 278 |
/* Check for a broadcast ethernet address */ |
| 279 |
static inline int eth_addr_is_bcast(n_eth_addr_t *addr) |
| 280 |
{ |
| 281 |
static const char *bcast_addr = "\xff\xff\xff\xff\xff\xff"; |
| 282 |
return(!memcmp(addr,bcast_addr,6)); |
| 283 |
} |
| 284 |
|
| 285 |
/* Check for a broadcast/multicast ethernet address */ |
| 286 |
static inline int eth_addr_is_mcast(n_eth_addr_t *addr) |
| 287 |
{ |
| 288 |
return(addr->eth_addr_byte[0] & 1); |
| 289 |
} |
| 290 |
|
| 291 |
/* Check for Cisco ISL destination address */ |
| 292 |
static inline int eth_addr_is_cisco_isl(n_eth_addr_t *addr) |
| 293 |
{ |
| 294 |
static const char *isl_addr = "\x01\x00\x0c\x00\x00"; |
| 295 |
return(!memcmp(addr,isl_addr,5)); /* only 40 bits to compare */ |
| 296 |
} |
| 297 |
|
| 298 |
/* Check for a SNAP header */ |
| 299 |
static inline int eth_llc_check_snap(n_eth_llc_hdr_t *llc_hdr) |
| 300 |
{ |
| 301 |
return((llc_hdr->dsap == 0xAA) && |
| 302 |
(llc_hdr->ssap == 0xAA) && |
| 303 |
(llc_hdr->ctrl == 0x03)); |
| 304 |
} |
| 305 |
|
| 306 |
/* Number of bits in a contiguous netmask */ |
| 307 |
static inline int ip_bits_mask(n_ip_addr_t mask) |
| 308 |
{ |
| 309 |
int prefix = 0; |
| 310 |
|
| 311 |
while(mask) { |
| 312 |
prefix++; |
| 313 |
mask = mask & (mask - 1); |
| 314 |
} |
| 315 |
return(prefix); |
| 316 |
} |
| 317 |
|
| 318 |
/* Initialize IPv6 masks */ |
| 319 |
void ipv6_init_masks(void); |
| 320 |
|
| 321 |
/* Convert an IPv4 address into a string */ |
| 322 |
char *n_ip_ntoa(char *buffer,n_ip_addr_t ip_addr); |
| 323 |
|
| 324 |
/* Convert in IPv6 address into a string */ |
| 325 |
char *n_ipv6_ntoa(char *buffer,n_ipv6_addr_t *ipv6_addr); |
| 326 |
|
| 327 |
/* Convert a string containing an IP address in binary */ |
| 328 |
int n_ip_aton(n_ip_addr_t *ip_addr,char *ip_str); |
| 329 |
|
| 330 |
/* Convert an IPv6 address from string into binary */ |
| 331 |
int n_ipv6_aton(n_ipv6_addr_t *ipv6_addr,char *ip_str); |
| 332 |
|
| 333 |
/* Parse an IPv4 CIDR prefix */ |
| 334 |
int ip_parse_cidr(char *token,n_ip_addr_t *net_addr,n_ip_addr_t *net_mask); |
| 335 |
|
| 336 |
/* Parse an IPv6 CIDR prefix */ |
| 337 |
int ipv6_parse_cidr(char *token,n_ipv6_addr_t *net_addr,u_int *net_mask); |
| 338 |
|
| 339 |
/* Parse a MAC address */ |
| 340 |
int parse_mac_addr(n_eth_addr_t *addr,char *str); |
| 341 |
|
| 342 |
/* Parse a board id */ |
| 343 |
int parse_board_id(m_uint8_t * buf,const char *id,int encode); |
| 344 |
|
| 345 |
/* Convert an Ethernet address into a string */ |
| 346 |
char *n_eth_ntoa(char *buffer,n_eth_addr_t *addr,int format); |
| 347 |
|
| 348 |
/* Create a new socket to connect to specified host */ |
| 349 |
int udp_connect(int local_port,char *remote_host,int remote_port); |
| 350 |
|
| 351 |
/* Listen on the specified port */ |
| 352 |
int ip_listen(char *ip_addr,int port,int sock_type,int max_fd,int fd_array[]); |
| 353 |
|
| 354 |
/* Listen on a TCP/UDP port - port is choosen in the specified rnaage */ |
| 355 |
int ip_listen_range(char *ip_addr,int port_start,int port_end,int *port, |
| 356 |
int sock_type); |
| 357 |
|
| 358 |
/* Create a socket UDP listening in a port of specified range */ |
| 359 |
int udp_listen_range(char *ip_addr,int port_start,int port_end,int *port); |
| 360 |
|
| 361 |
/* Connect an existing socket to connect to specified host */ |
| 362 |
int ip_connect_fd(int fd,char *remote_host,int remote_port); |
| 363 |
|
| 364 |
/* ISL rewrite */ |
| 365 |
void cisco_isl_rewrite(m_uint8_t *pkt,m_uint32_t tot_len); |
| 366 |
|
| 367 |
/* Verify checksum of an IP header */ |
| 368 |
int ip_verify_cksum(n_ip_hdr_t *hdr); |
| 369 |
|
| 370 |
/* Compute an IP checksum */ |
| 371 |
void ip_compute_cksum(n_ip_hdr_t *hdr); |
| 372 |
|
| 373 |
/* Compute TCP/UDP checksum */ |
| 374 |
m_uint16_t pkt_ctx_tcp_cksum(n_pkt_ctx_t *ctx,int ph); |
| 375 |
|
| 376 |
/* Analyze L4 for an IP packet */ |
| 377 |
int pkt_ctx_ip_analyze_l4(n_pkt_ctx_t *ctx); |
| 378 |
|
| 379 |
/* Analyze a packet */ |
| 380 |
int pkt_ctx_analyze(n_pkt_ctx_t *ctx,m_uint8_t *pkt,size_t pkt_len); |
| 381 |
|
| 382 |
/* Dump packet context */ |
| 383 |
void pkt_ctx_dump(n_pkt_ctx_t *ctx); |
| 384 |
|
| 385 |
#endif |