File: gre.cpp

package info (click to toggle)
ostinato 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,840 kB
  • sloc: cpp: 46,226; makefile: 8
file content (471 lines) | stat: -rw-r--r-- 12,744 bytes parent folder | download | duplicates (2)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Copyright (C) 2021 Srivats P.

This file is part of "Ostinato"

This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

#include "gre.h"

GreProtocol::GreProtocol(StreamBase *stream, AbstractProtocol *parent)
    : AbstractProtocol(stream, parent)
{
}

GreProtocol::~GreProtocol()
{
}

AbstractProtocol* GreProtocol::createInstance(StreamBase *stream,
    AbstractProtocol *parent)
{
    return new GreProtocol(stream, parent);
}

quint32 GreProtocol::protocolNumber() const
{
    return OstProto::Protocol::kGreFieldNumber;
}

void GreProtocol::protoDataCopyInto(OstProto::Protocol &protocol) const
{
    protocol.MutableExtension(OstProto::gre)->CopyFrom(data);
    protocol.mutable_protocol_id()->set_id(protocolNumber());
}

void GreProtocol::protoDataCopyFrom(const OstProto::Protocol &protocol)
{
    if (protocol.protocol_id().id() == protocolNumber() &&
            protocol.HasExtension(OstProto::gre))
        data.MergeFrom(protocol.GetExtension(OstProto::gre));
}

QString GreProtocol::name() const
{
    return QString("General Routing Encapsulation Protocol");
}

QString GreProtocol::shortName() const
{
    return QString("GRE");
}

AbstractProtocol::ProtocolIdType GreProtocol::protocolIdType() const
{
    return ProtocolIdEth;
}

quint32 GreProtocol::protocolId(ProtocolIdType type) const
{
    switch(type)
    {
        case ProtocolIdIp: return 47;
        default:break;
    }

    return AbstractProtocol::protocolId(type);
}

int GreProtocol::fieldCount() const
{
    return gre_fieldCount;
}

AbstractProtocol::FieldFlags GreProtocol::fieldFlags(int index) const
{
    AbstractProtocol::FieldFlags flags;

    flags = AbstractProtocol::fieldFlags(index);

    switch (index) {
    case gre_checksum:
        flags |= CksumField;
        break;

    case gre_isOverrideChecksum:
        flags &= ~FrameField;
        flags |= MetaField;
        break;
    }

    return flags;
}

QVariant GreProtocol::fieldData(int index, FieldAttrib attrib,
        int streamIndex) const
{
    switch (index)
    {
        case gre_flags:
        {
            switch(attrib)
            {
                case FieldName:
                    return QString("Flags");
                case FieldValue:
                    return data.flags();
                case FieldTextValue:
                {
                    QString fstr;
                    fstr.append("Cksum:");
                    fstr.append(data.flags() & GRE_FLAG_CKSUM ? "Y" : "N");
                    fstr.append(" Key:");
                    fstr.append(data.flags() & GRE_FLAG_KEY ? "Y" : "N");
                    fstr.append(" Seq:");
                    fstr.append(data.flags() & GRE_FLAG_SEQ ? "Y" : "N");
                    return fstr;
                }
                case FieldFrameValue:
                    return QByteArray(1, char(data.flags()));
                case FieldBitSize:
                    return 4;
                default:
                    break;
            }
            break;
        }
        case gre_rsvd0:
        {
            switch(attrib)
            {
                case FieldName:
                    return QString("Reserved0");
                case FieldValue:
                    return data.rsvd0();
                case FieldTextValue:
                    return QString("%1").arg(data.rsvd0());
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(2);
                    qToBigEndian(quint16(data.rsvd0()), (uchar*)fv.data());
                    return fv;
                }
                case FieldBitSize:
                    return 9;
                default:
                    break;
            }
            break;
        }

        case gre_version:
        {
            switch(attrib)
            {
                case FieldName:
                    return QString("Version");
                case FieldValue:
                    return data.version();
                case FieldFrameValue:
                    return QByteArray(1, char(data.version()));
                case FieldTextValue:
                    return QString("%1").arg(data.version());
                case FieldBitSize:
                    return 3;
                default:
                    break;
            }
            break;
        }
        case gre_protocol:
        {
            quint16 protocol = payloadProtocolId(ProtocolIdEth);

            switch(attrib)
            {
                case FieldName:
                    return QString("Protocol");
                case FieldValue:
                    return protocol;
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(2);
                    qToBigEndian(protocol, (uchar*) fv.data());
                    return fv;
                }
                case FieldTextValue:
                    return  QString("0x%1").arg(
                        protocol, 4, BASE_HEX, QChar('0'));;
                default:
                    break;
            }
            break;
        }
        case gre_checksum:
        {
            if (attrib == FieldName)
                return QString("Checksum");

            if ((data.flags() & GRE_FLAG_CKSUM) == 0)
            {
                if (attrib == FieldTextValue)
                    return QObject::tr("<not-included>");
                else
                    return QVariant();
            }

            if (attrib == FieldBitSize)
                return 16;

            quint16 cksum;
            if (data.is_override_checksum()) {
                cksum = data.checksum();
            } else {
                quint32 sum = 0;
                cksum = protocolFrameCksum(streamIndex, CksumIp);
                sum += (quint16) ~cksum;
                cksum = protocolFramePayloadCksum(streamIndex, CksumIp);
                sum += (quint16) ~cksum;

                while (sum >> 16)
                    sum = (sum & 0xFFFF) + (sum >> 16);

                cksum = (~sum) & 0xFFFF;
            }

            switch(attrib)
            {
                case FieldValue:
                    return cksum;
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(2);
                    qToBigEndian(cksum, (uchar*) fv.data());
                    return fv;
                }
                case FieldTextValue:
                    return  QString("0x%1").arg(
                        cksum, 4, BASE_HEX, QChar('0'));;
                default:
                    break;
            }
            break;
        }

        case gre_rsvd1:
        {
            if (attrib == FieldName)
                return QString("Reserved1");

            if ((data.flags() & GRE_FLAG_CKSUM) == 0)
            {
                if (attrib == FieldTextValue)
                    return QObject::tr("<not-included>");
                else
                    return QVariant();
            }

            switch(attrib)
            {
                case FieldValue:
                    return data.rsvd1();
                case FieldTextValue:
                    return QString("%1").arg(data.rsvd1());
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(2);
                    qToBigEndian((quint16) data.rsvd1(), (uchar*) fv.data());
                    return fv;
                }
                default:
                    break;
            }
            break;
        }

        case gre_key:
        {
            if (attrib == FieldName)
                return QString("Key");

            if ((data.flags() & GRE_FLAG_KEY) == 0)
            {
                if (attrib == FieldTextValue)
                    return QObject::tr("<not-included>");
                else
                    return QVariant();
            }

            switch(attrib)
            {
                case FieldValue:
                    return data.key();
                case FieldTextValue:
                    return QString("0x%1").arg(data.key(), 8, BASE_HEX, QChar('0'));
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(4);
                    qToBigEndian((quint32) data.key(), (uchar*) fv.data());
                    return fv;
                }
                default:
                    break;
            }
            break;
        }

        case gre_sequence:
        {
            if (attrib == FieldName)
                return QString("Sequence Number");

            if ((data.flags() & GRE_FLAG_SEQ) == 0)
            {
                if (attrib == FieldTextValue)
                    return QObject::tr("<not-included>");
                else
                    return QVariant();
            }

            switch(attrib)
            {
                case FieldValue:
                    return data.sequence_num();
                case FieldTextValue:
                    return QString("%1").arg(data.sequence_num());
                case FieldFrameValue:
                {
                    QByteArray fv;
                    fv.resize(4);
                    qToBigEndian((quint32) data.sequence_num(), (uchar*) fv.data());
                    return fv;
                }
                default:
                    break;
            }
            break;
        }

        // Meta fields
        case gre_isOverrideChecksum:
        {
            switch(attrib)
            {
                case FieldValue:
                    return data.is_override_checksum();
                default:
                    break;
            }
            break;
        }

        default:
            qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
                index);
            break;
    }

    return AbstractProtocol::fieldData(index, attrib, streamIndex);
}

bool GreProtocol::setFieldData(int index, const QVariant &value,
        FieldAttrib attrib)
{
    bool isOk = false;

    if (attrib != FieldValue)
        goto _exit;

    switch (index)
    {
        case gre_flags:
        {
            uint flags = value.toUInt(&isOk);
            if (isOk)
                data.set_flags(flags);
            break;
        }
        case gre_rsvd0:
        {
            uint rsvd0 = value.toUInt(&isOk);
            if (isOk)
                data.set_rsvd0(rsvd0);
            break;
        }
        case gre_version:
        {
            uint ver = value.toUInt(&isOk);
            if (isOk)
                data.set_version(ver);
            break;
        }
        case gre_protocol:
        {
            uint proto = value.toUInt(&isOk);
            if (isOk)
                data.set_protocol_type(proto);
            break;
        }
        case gre_checksum:
        {
            uint csum = value.toUInt(&isOk);
            if (isOk)
                data.set_checksum(csum);
            break;
        }
        case gre_isOverrideChecksum:
        {
            data.set_is_override_checksum(value.toBool());
            break;
        }
        case gre_rsvd1:
        {
            uint rsvd1 = value.toUInt(&isOk);
            if (isOk)
                data.set_rsvd1(rsvd1);
            break;
        }
        case gre_key:
        {
            uint key = value.toUInt(&isOk);
            if (isOk)
                data.set_key(key);
            break;
        }
        case gre_sequence:
        {
            uint seq = value.toUInt(&isOk);
            if (isOk)
                data.set_sequence_num(seq);
            break;
        }
        default:
            qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
                index);
            break;
    }

_exit:
    return isOk;
}

int GreProtocol::protocolFrameSize(int /*streamIndex*/) const
{
    int size = 4; // mandatory fields - flags, rsvd0, version, protocol

    if (data.flags() & GRE_FLAG_CKSUM)
        size += 4;
    if (data.flags() & GRE_FLAG_KEY)
        size += 4;
    if (data.flags() & GRE_FLAG_SEQ)
        size += 4;

    return size;
}