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