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 // Assigns MPLS tag at position i (starting at zero!) with values:
25 // m ... total number of tags (important to set BoS in last tag)
26 // Label ... label value
27 // Exp ... EXP field (typically CoS)
28 // TTL ... Time To Live
30 // NOTE: Two usage possibilities!
32 // 1.) When called from for-loop to add all tags the total size mpls_s
33 // is updated continuously and the BoS is set in the last tag.
34 // Therefore set m = total number of tags!
36 // 2.) But when changing a particular tag within an existing MPLS stack
37 // the total number of tags does not change, therefore use m=0.
39 // RETURN VALUE: 0 upon success, 1 upon failure
41 int mops_mpls(struct mops
*mp
, int i
, int m
, u_int32_t Label
, u_int8_t Exp
, u_int8_t TTL
)
45 if ((m
) && (i
>=m
)) return 1; // label index greater than number of labels!
46 if (Label
> 1048575) return 1;
47 if (Exp
> 7) return 1;
49 // Create binary tag: Label(20) EXP(3) BoS(1) TTL(8)
51 ptr
= (u_int8_t
*) &Label
;
52 mp
->mpls
[4*i
+0] = *(ptr
+2);
53 mp
->mpls
[4*i
+1] = *(ptr
+1);
54 mp
->mpls
[4*i
+2] = *(ptr
+0);
56 mp
->mpls
[4*i
+2] |= Exp
;
57 mp
->mpls
[4*i
+3] = TTL
;
59 if ((m
) && (i
==(m
-1))) // reached last tag!
61 mp
->mpls
[4*i
+2] |= 0x01; // set BoS in last tag
64 if ( (mp
->eth_type
!= 0x8847) && (mp
->eth_type
!= 0x8848) )
66 mp
->eth_type_backup
= mp
->eth_type
;
68 mp
->eth_type
= 0x8847;
74 // Remove MPLS tags from packet mp
76 // j indicates which tag to be removed (1..n)
77 // j=0 means: remove all tags!
79 // RETURN VALUE: 1 upon failure, 0 upon success
80 int mops_mpls_remove (struct mops
*mp
, int j
)
85 if (j
==0) // remove all tags
91 mp
->eth_type
= mp
->eth_type_backup
; // restore original ethertype
99 if (j
>k
) return 1; // The packet only consists of k tag(s)
101 if (k
==1) // only delete the single tag
105 mp
->eth_type
= mp
->eth_type_backup
; // restore original ethertype
109 // if we got here we have more than one tag:
111 if (j
==k
) // remove last tag (of several)
117 // remove some non-ending tag: 0, 1, 2, 3
118 a
= (j
-1)*4; // target
119 b
= j
*4; // source (what should be copied)
120 memcpy(&mp
->mpls
[a
], &mp
->mpls
[b
], (k
-j
)*4);
126 // Set BOS in tag k where k=1..n
127 int mops_mpls_bos (struct mops
*mp
, int k
)
131 n
= mp
->mpls_s
/4; // n = total number of tags
134 mp
->mpls
[(k
-1)*4+2] |= 0x01;
139 // Unset BOS in tag k where k=1..n
140 int mops_mpls_nobos (struct mops
*mp
, int k
)
144 n
= mp
->mpls_s
/4; // n = total number of tags
147 mp
->mpls
[(k
-1)*4+2] &= 0xfe;