Click
Loading...
Searching...
No Matches
ip.h
1/* -*- mode: c; c-basic-offset: 4 -*- */
2#ifndef CLICKNET_IP_H
3#define CLICKNET_IP_H
4/* get struct in_addr */
5#include <click/cxxprotect.h>
6CLICK_CXX_PROTECT
7#if CLICK_LINUXMODULE
8# include <net/checksum.h>
9# include <linux/in.h>
10#else
11# include <sys/types.h>
12#undef true
13# include <netinet/in.h>
14# include <netinet/ip.h>
15#define true linux_true
16#endif
17
18/*
19 * <clicknet/ip.h> -- IP header definitions, based on one of the BSDs.
20 *
21 * Relevant RFCs include:
22 * RFC791 Internet Protocol
23 * RFC3168 The Addition of Explicit Congestion Notification (ECN) to IP
24 */
25
26struct click_ip {
27#if CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN
28 unsigned ip_v : 4; /* 0 version == 4 */
29 unsigned ip_hl : 4; /* header length */
30#elif CLICK_BYTE_ORDER == CLICK_LITTLE_ENDIAN
31 unsigned ip_hl : 4; /* 0 header length */
32 unsigned ip_v : 4; /* version == 4 */
33#else
34# error "unknown byte order"
35#endif
36 uint8_t ip_tos; /* 1 type of service */
37#define IP_DSCPMASK 0xFC /* diffserv code point */
38#define IP_ECNMASK 0x03 /* ECN code point */
39#define IP_ECN_NOT_ECT 0x00 /* not ECN capable transport */
40#define IP_ECN_ECT1 0x01 /* ECN capable transport */
41#define IP_ECN_ECT2 0x02 /* ECN capable transport */
42#define IP_ECN_CE 0x03 /* ECN congestion exp'd */
43 uint16_t ip_len; /* 2-3 total length */
44 uint16_t ip_id; /* 4-5 identification */
45 uint16_t ip_off; /* 6-7 fragment offset field */
46#define IP_RF 0x8000 /* reserved fragment flag */
47#define IP_DF 0x4000 /* don't fragment flag */
48#define IP_MF 0x2000 /* more fragments flag */
49#if CLICK_LINUXMODULE
50#define IP_OFFMASK 0X1FFF /* mask for fragmenting bits */
51#endif
52 uint8_t ip_ttl; /* 8 time to live */
53 uint8_t ip_p; /* 9 protocol */
54 uint16_t ip_sum; /* 10-11 checksum */
55 struct in_addr ip_src; /* 12-15 source address */
56 struct in_addr ip_dst; /* 16-19 destination address */
57};
58
59/* ip_protocol */
60#define IP_PROTO_ICMP 1
61#define IP_PROTO_IGMP 2
62#define IP_PROTO_GGP 3
63#define IP_PROTO_IPIP 4
64#define IP_PROTO_ST 5
65#define IP_PROTO_TCP 6
66#define IP_PROTO_UCL 7
67#define IP_PROTO_EGP 8
68#define IP_PROTO_IGP 9
69#define IP_PROTO_BBN 10
70#define IP_PROTO_NVPII 11
71#define IP_PROTO_PUP 12
72#define IP_PROTO_ARGUS 13
73#define IP_PROTO_EMCON 14
74#define IP_PROTO_XNET 15
75#define IP_PROTO_CHAOS 16
76#define IP_PROTO_UDP 17
77#define IP_PROTO_MUX 18
78#define IP_PROTO_DCN 19
79#define IP_PROTO_HMP 20
80#define IP_PROTO_PRM 21
81#define IP_PROTO_XNS 22
82#define IP_PROTO_TRUNK1 23
83#define IP_PROTO_TRUNK2 24
84#define IP_PROTO_LEAF1 25
85#define IP_PROTO_LEAF2 26
86#define IP_PROTO_RDP 27
87#define IP_PROTO_IRTP 28
88#define IP_PROTO_ISOTP4 29
89#define IP_PROTO_NETBLT 30
90#define IP_PROTO_MFENSP 31
91#define IP_PROTO_MERIT 32
92#define IP_PROTO_DCCP 33
93#define IP_PROTO_RSVP 46
94#define IP_PROTO_GRE 47
95#define IP_PROTO_ICMP6 58
96#define IP_PROTO_CFTP 62
97#define IP_PROTO_SATNET 64
98#define IP_PROTO_MITSUBNET 65
99#define IP_PROTO_RVD 66
100#define IP_PROTO_IPPC 67
101#define IP_PROTO_SATMON 69
102#define IP_PROTO_IPCV 71
103#define IP_PROTO_BRSATMON 76
104#define IP_PROTO_WBMON 78
105#define IP_PROTO_WBEXPAK 79
106#define IP_PROTO_SCTP 132
107#define IP_PROTO_UDPLITE 136
108
109#define IP_PROTO_NONE 257
110#define IP_PROTO_TRANSP 258
111#define IP_PROTO_TCP_OR_UDP 256
112#define IP_PROTO_PAYLOAD 259
113
114#define IPOPT_EOL 0 /* end of option list */
115#define IPOPT_NOP 1 /* no operation */
116#define IPOPT_RR 7 /* record packet route */
117#define IPOPT_TS 68 /* timestamp */
118#define IPOPT_SECURITY 130 /* provide s,c,h,tcc */
119#define IPOPT_LSRR 131 /* loose source route */
120#define IPOPT_SATID 136 /* satnet id */
121#define IPOPT_SSRR 137 /* strict source route */
122#define IPOPT_RA 148 /* router alert */
123
124#define IP_ISFRAG(iph) (((iph)->ip_off & htons(IP_MF | IP_OFFMASK)) != 0)
125#define IP_FIRSTFRAG(iph) (((iph)->ip_off & htons(IP_OFFMASK)) == 0)
126
127
128/* checksum functions */
129
130#if !CLICK_LINUXMODULE
136uint16_t click_in_cksum(const unsigned char *x, int len);
137uint16_t click_in_cksum_pseudohdr_raw(uint32_t csum, uint32_t src, uint32_t dst, int proto, int packet_len);
138#else
139# define click_in_cksum(addr, len) \
140 ip_compute_csum((unsigned char *)(addr), (len))
141# define click_in_cksum_pseudohdr_raw(csum, src, dst, proto, transport_len) \
142 csum_tcpudp_magic((src), (dst), (transport_len), (proto), ~(csum) & 0xFFFF)
143#endif
144uint16_t click_in_cksum_pseudohdr_hard(uint32_t csum, const struct click_ip *iph, int packet_len);
145void click_update_zero_in_cksum_hard(uint16_t *csum, const unsigned char *addr, int len);
146
155static inline uint16_t
156click_in_cksum_pseudohdr(uint32_t data_csum, const struct click_ip *iph,
157 int transport_len)
158{
159 if (iph->ip_hl == 5)
160 return click_in_cksum_pseudohdr_raw(data_csum, iph->ip_src.s_addr, iph->ip_dst.s_addr, iph->ip_p, transport_len);
161 else
162 return click_in_cksum_pseudohdr_hard(data_csum, iph, transport_len);
163}
164
180static inline void
181click_update_in_cksum(uint16_t *csum, uint16_t old_hw, uint16_t new_hw)
182{
183 /* incrementally update IP checksum according to RFC1624:
184 new_sum = ~(~old_sum + ~old_halfword + new_halfword) */
185 uint32_t sum = (~*csum & 0xFFFF) + (~old_hw & 0xFFFF) + new_hw;
186 sum = (sum & 0xFFFF) + (sum >> 16);
187 *csum = ~(sum + (sum >> 16));
188}
189
199static inline void
200click_update_zero_in_cksum(uint16_t *csum, const unsigned char *x, int len)
201{
202 if (*csum == 0)
203 click_update_zero_in_cksum_hard(csum, x, len);
204}
205
206CLICK_CXX_UNPROTECT
207#include <click/cxxunprotect.h>
208#endif