2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008-2010 Herbert Haas
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
23 // Remove 802.1Q tags from packet mp
25 // k indicates which tag to be removed (1..n)
26 // k=0 means: remove all tags!
28 // RETURN VALUE: 1 upon failure, 0 upon success
29 int mops_dot1Q_remove (struct mops
*mp
, int k
)
39 n
= mp
->dot1Q_s
/4; // n = total number of tags
42 if (k
==1) { // only delete the single tag
48 // we have more than one tag:
50 if (k
==n
) { // remove last tag (of several)
55 // remove some non-ending tag: 0, 1, 2, 3
56 a
= (k
-1)*4; // target
57 b
= k
*4; // source (what should be copied)
58 memcpy(&mp
->dot1Q
[a
], &mp
->dot1Q
[b
], (n
-k
)*4);
65 // Unset CFI in tag k where k=1..n
66 int mops_dot1Q_nocfi (struct mops
*mp
, int k
)
70 n
= mp
->dot1Q_s
/4; // n = total number of tags
73 mp
->dot1Q
[((k
-1)*4)+2] &=0xef; // unset CFI (0xef = 1110 1111)
78 // Set CFI in tag k where k=1..n
79 int mops_dot1Q_cfi (struct mops
*mp
, int k
)
83 n
= mp
->dot1Q_s
/4; // n = total number of tags
86 mp
->dot1Q
[((k
-1)*4)+2] |=0x10; // set CFI (0x10 = 0001 0000)
91 // Assign 802.1Q tag with
94 // i ... tag position (starting from zero!)
96 // m ... modification: 1 = dot1Q_s is not changed
99 // When called from for-loop to add all tags the total size dot1Q_s
100 // is updated continuously, therefore use m=1.
102 // But when changing a particular tag within an existing 802.1Q stack
103 // the total number of tags does not change, therefore use m=0.
105 // RETURN VALUE: 0 upon success, 1 upon failure
107 int mops_dot1Q (struct mops
*mp
, int i
, int m
, u_int16_t v
, u_int16_t c
)
111 if (i
>=MAX_MOPS_DOT1Q_TAGS
) return 1; // max number of tags, see definitions in mops.h
112 if ((v
>4095)||(c
>7)) return 1; // greater values do not make sense
114 // Format: 0x8100 CoS-CFI-VLAN
115 // where c=CoS, v=VLAN
117 mp
->dot1Q
[4*i
+0]= 0x81;
118 mp
->dot1Q
[4*i
+1]= 0x00;
119 ptr
= (u_int8_t
*) &v
;
120 mp
->dot1Q
[4*i
+3]=*ptr
;
121 mp
->dot1Q
[4*i
+2]=*(ptr
+1);
122 mp
->dot1Q
[4*i
+2]^= (c8
<< 5);
125 mp
->dot1Q_s
=4*(1+i
); // NOTE: dot1Q_s = current tag position + 1
126 if (mp
->dot1Q_s
) mp
->use_dot1Q
= 1;