summaryrefslogtreecommitdiffstats
path: root/src/common/Network/Packet/IPv6Header.h
blob: 05653bdd453d19fc90e43f7c18b11b1706fb0740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Copyright (c) 2015-2016 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _IPV6_HEADER_H_
#define _IPV6_HEADER_H_

#include "PacketHeaderBase.h"

#define IPV6_16b_ADDR_GROUPS 8
#define IPV6_16b_ADDR_GROUPS_MSB 6
#define IPV6_HDR_LEN 40
static uint16_t default_ipv6[8] = { 0xDEAD, 0xBEEF, 0xDEAD, 0xBEEF,
                                    0xDEAD, 0xBEEF, 0xDEAD, 0xBEEF };

class IPv6Header
{
/*
 *      IPV6 extension headers
 */
    enum ext_headers_e {
        IPPROTO_HOPOPTS   = 0,       /* IPv6 hop-by-hop options      */
        IPPROTO_ROUTING   = 43,      /* IPv6 routing header          */
        IPPROTO_FRAGMENT  = 44,      /* IPv6 fragmentation header    */
        IPPROTO_ENCAP_SEC = 50,      /* IPv6 encapsulation security paylod header   */
        IPPROTO_AUTH      = 51,      /* IPv6 authentication header   */
        IPPROTO_ICMPV6    = 58,      /* ICMPv6                       */
        IPPROTO_NONE      = 59,      /* IPv6 no next header          */
        IPPROTO_DSTOPTS   = 60,      /* IPv6 destination options     */
        IPPROTO_MH        = 135,     /* IPv6 mobility header         */
    };

public:
    IPv6Header()
    {
        setDestIpv6(default_ipv6);
        setSourceIpv6(default_ipv6);
        setTrafficClass(0xDD);
    };

    IPv6Header  (uint16_t *argSource, uint16_t *argDestinaction, uint8_t  argTrafficClass)
    {
        setDestIpv6(argDestinaction);
        setSourceIpv6(argSource);
        setTrafficClass(argTrafficClass);
    };

    enum
    {
        DefaultSize = 40
    };

public:
    inline uint8_t  getVersion();
    inline void     setVersion(uint8_t);
    inline uint8_t  getHeaderLength() {return IPV6_HDR_LEN;}
    inline uint16_t getTrafficClass();
    inline void     setTrafficClass(uint16_t);
    inline uint32_t getFlowLabel();
    inline void     setFlowLabel(uint32_t);
    inline uint16_t getPayloadLen();
    inline void     setPayloadLen(uint16_t);
    inline uint8_t  getNextHdr();
    inline void     setNextHdr(uint8_t);
    inline uint8_t  getHopLimit();
    inline void     setHopLimit(uint8_t);
    inline void     getSourceIpv6(uint16_t *);
    inline void     setSourceIpv6(uint16_t *);
    inline void     getDestIpv6(uint16_t *);
    inline void     setDestIpv6(uint16_t *);
    uint8_t         getl4Proto(uint8_t *pkt, uint16_t pkt_len, uint8_t *&p_l4);
    inline void     updateTrafficClass(uint8_t newclass);
    inline void     updatePayloadLength(uint16_t newlen);
    inline void     updateIpv6Src(uint16_t *ipsrc);
    inline void     updateIpv6Dst(uint16_t *ipdst);
    inline void     updateMSBIpv6Src(uint16_t *ipsrc);
    inline void     updateMSBIpv6Dst(uint16_t *ipdst);
    inline void     updateLSBIpv6Src(uint32_t ipsrc);
    inline void     updateLSBIpv6Dst(uint32_t ipdst);
    inline void     swapSrcDest();

////////////////////////////////////////////////////////////////////////////////////////
// Common Header Interface
////////////////////////////////////////////////////////////////////////////////////////

public:
    inline  uint8_t*  getPointer          (){return (uint8_t*)this;}
    inline  uint32_t  getSize             (){return  getHeaderLength();}

public:
    uint32_t myVer_TrafficClass_FlowLabel;
    uint16_t myPayloadLen;
    uint8_t  myNextHdr;
    uint8_t  myHopLimit;
    uint16_t mySource[IPV6_16b_ADDR_GROUPS];
    uint16_t myDestination[IPV6_16b_ADDR_GROUPS];
    uint32_t myOption[1];
};


class IPv6PseudoHeader
{
public:
  uint32_t m_source_ip;
  uint32_t m_dest_ip;
  uint8_t  m_zero;
  uint8_t  m_protocol;
  uint16_t m_length;

public:
    inline uint8_t* getPointer(){return (uint8_t*)this;}
    inline uint32_t getSize();
    inline uint16_t inetChecksum();
};


/////////////////////////////////////////////
// inline functions implementation
/////////////////////////////////////////////

#include <common/BigEndianBitManipulation.h>

//--------------------------------
inline uint8_t IPv6Header::getVersion()
{
    return getMaskBit32(PKT_NTOHL(myVer_TrafficClass_FlowLabel), 0, 3);
}

inline void  IPv6Header::setVersion(uint8_t argVersion)
{
    uint32_t myVer = PKT_HTONL(myVer_TrafficClass_FlowLabel);
    setMaskBit32(myVer, 0, 3, argVersion);
    myVer_TrafficClass_FlowLabel = PKT_NTOHL(myVer);
}

//--------------------------------
inline uint16_t IPv6Header::getTrafficClass()
{
    return  getMaskBit32(PKT_NTOHL(myVer_TrafficClass_FlowLabel),  4, 11);
}

inline void 
IPv6Header::setTrafficClass(uint16_t argTrafficClass)
{
    uint32_t myTrafficClass = PKT_HTONL(myVer_TrafficClass_FlowLabel);
    setMaskBit32(myTrafficClass, 4, 11, argTrafficClass);
    myVer_TrafficClass_FlowLabel = PKT_NTOHL(myTrafficClass);
}

//--------------------------------
inline uint32_t IPv6Header::getFlowLabel()
{
    return  getMaskBit32(PKT_NTOHL(myVer_TrafficClass_FlowLabel), 12, 31);
}

inline void  IPv6Header::setFlowLabel(uint32_t argFlowLabel)
{
    uint32_t myFlowLabel = PKT_HTONL(myVer_TrafficClass_FlowLabel);
    setMaskBit32(myFlowLabel, 12, 31, argFlowLabel);
    myVer_TrafficClass_FlowLabel = PKT_NTOHL(myFlowLabel);
}

//--------------------------------
inline uint16_t IPv6Header::getPayloadLen()
{
    return  PKT_NTOHS(myPayloadLen);
}

inline void  IPv6Header::setPayloadLen(uint16_t argPayloadLen)
{
    myPayloadLen = PKT_HTONS(argPayloadLen);
}

//--------------------------------
inline uint8_t IPv6Header::getNextHdr()
{
    return  myNextHdr;
}

inline void  IPv6Header::setNextHdr(uint8_t argNextHdr)
{
    myNextHdr = argNextHdr;
}

//--------------------------------
inline uint8_t IPv6Header::getHopLimit()
{
    return  myHopLimit;
}

inline void  IPv6Header::setHopLimit(uint8_t argHopLimit)
{
    myHopLimit = argHopLimit;
}

//--------------------------------
inline  void IPv6Header::getSourceIpv6(uint16_t *argSourceAddress)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        argSourceAddress[i] = PKT_NTOHS(mySource[i]);
    }
}

inline void IPv6Header::setSourceIpv6(uint16_t *argSourceAddress)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        mySource[i] = PKT_HTONS(argSourceAddress[i]);
    }
}

//--------------------------------
inline  void IPv6Header::getDestIpv6(uint16_t *argDestAddress)
{
    uint8_t i;
    for (i=0; i<IPV6_16b_ADDR_GROUPS; i++) {
        argDestAddress[i] = PKT_NTOHS(myDestination[i]);
    }
}

inline void IPv6Header::setDestIpv6(uint16_t *argDestAddress)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        myDestination[i] = PKT_HTONS(argDestAddress[i]);
    }
}

//--------------------------------
inline void IPv6Header::updateIpv6Src(uint16_t *ipsrc)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        mySource[i] = PKT_HTONS(ipsrc[i]);
    }
}

inline void IPv6Header::updateIpv6Dst(uint16_t *ipdst)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        myDestination[i] = PKT_HTONS(ipdst[i]);
    }
}

//--------------------------------
inline void IPv6Header::updateMSBIpv6Src(uint16_t *ipsrc)
{
    uint8_t i;
    for (i=0; i<IPV6_16b_ADDR_GROUPS_MSB; i++) {
        mySource[i] = PKT_HTONS(ipsrc[i]);
    }
}

inline void IPv6Header::updateMSBIpv6Dst(uint16_t *ipdst)
{
    uint8_t i;
    for (i = 0; i < IPV6_16b_ADDR_GROUPS_MSB; i++) {
        myDestination[i] = PKT_HTONS(ipdst[i]);
    }
}

//--------------------------------
inline void IPv6Header::updateLSBIpv6Src(uint32_t ipsrc)
{
     uint32_t *lsb = (uint32_t *)&mySource[6];
     *lsb = PKT_HTONL(ipsrc);
}

inline void IPv6Header::updateLSBIpv6Dst(uint32_t ipdst)
{
     uint32_t *lsb = (uint32_t *)&myDestination[6];
     *lsb = PKT_HTONL(ipdst);
}

//--------------------------------
inline void IPv6Header::swapSrcDest()
{
    uint8_t i;
    uint16_t tmp[IPV6_16b_ADDR_GROUPS];
    for (i = 0; i < IPV6_16b_ADDR_GROUPS; i++) {
        tmp[i] = myDestination[i];
        myDestination[i] = mySource[i];
        mySource[i] = tmp[i];
    }
}

#endif