Skip to content

Commit 1c9556f

Browse files
committed
FIXUP: feat(tft): implement tfts
1 parent 04b92cc commit 1c9556f

File tree

6 files changed

+305
-81
lines changed

6 files changed

+305
-81
lines changed

src/main/java/com/sipgate/udpproxy/Application.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,20 @@ public static void main(final String[] args) {
4242
(byte) 0x00,(byte) 0x08,(byte) 0x00
4343
};
4444

45-
final var gtpv2 = GTPv2Payload.fromBytes(createSessionRequest);
45+
// 262034860127565_EOFINDER_OVERALL_summary-BO-DATA-20230915-142910-276.pcap frame 64
46+
byte [] createBearerRequest = {
47+
(byte) 0x48,(byte) 0x5f,(byte) 0x00,(byte) 0x82,(byte) 0x0b,(byte) 0x35,(byte) 0x32,(byte) 0xac,(byte) 0x4b,(byte) 0xbe,(byte) 0xfa,(byte) 0x00,(byte) 0x49,(byte) 0x00,(byte) 0x01,(byte) 0x00,
48+
(byte) 0x06,(byte) 0x5d,(byte) 0x00,(byte) 0x71,(byte) 0x00,(byte) 0x49,(byte) 0x00,(byte) 0x01,(byte) 0x00,(byte) 0x00,(byte) 0x50,(byte) 0x00,(byte) 0x16,(byte) 0x00,(byte) 0x7c,(byte) 0x01,
49+
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x32,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x32,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x32,(byte) 0x00,
50+
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x32,(byte) 0x54,(byte) 0x00,(byte) 0x39,(byte) 0x00,(byte) 0x22,(byte) 0x20,(byte) 0x20,(byte) 0x19,(byte) 0x21,(byte) 0xfd,(byte) 0x00,(byte) 0x02,
51+
(byte) 0x30,(byte) 0xba,(byte) 0xbe,(byte) 0x00,(byte) 0x01,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x01,(byte) 0x80,(byte) 0x30,(byte) 0x11,
52+
(byte) 0x51,(byte) 0x27,(byte) 0x10,(byte) 0x75,(byte) 0x30,(byte) 0x11,(byte) 0x21,(byte) 0x19,(byte) 0x21,(byte) 0xfd,(byte) 0x00,(byte) 0x02,(byte) 0x30,(byte) 0xba,(byte) 0xbe,(byte) 0x00,
53+
(byte) 0x01,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x01,(byte) 0x80,(byte) 0x30,(byte) 0x11,(byte) 0x51,(byte) 0x27,(byte) 0x10,(byte) 0x75,
54+
(byte) 0x30,(byte) 0x57,(byte) 0x00,(byte) 0x09,(byte) 0x01,(byte) 0x85,(byte) 0x39,(byte) 0xaf,(byte) 0x26,(byte) 0x1e,(byte) 0xd4,(byte) 0x17,(byte) 0x65,(byte) 0x75,(byte) 0x5e,(byte) 0x00,
55+
(byte) 0x04,(byte) 0x00,(byte) 0x39,(byte) 0xaf,(byte) 0x26,(byte) 0x1e
56+
};
57+
58+
final var gtpv2 = GTPv2Payload.fromBytes(createBearerRequest);
4659
System.out.println(gtpv2);
4760
//System.exit(new CommandLine(new UdpProxyCommand()).execute(args));
4861
}

src/main/java/com/sipgate/udpproxy/udp/payload/gtpv2/ie/EpsBearerLevelTrafficFlowTemplate.java

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.sipgate.udpproxy.udp.payload.gtpv2.ie;
22

33
import com.sipgate.udpproxy.udp.payload.gtpv2.ie.decoder.BitHelper;
4-
import com.sipgate.udpproxy.udp.payload.gtpv2.ie.decoder.IpV4V6;
4+
import com.sipgate.udpproxy.udp.payload.gtpv2.ie.tft.GenericPacketFilterComponent;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
58

69
public class EpsBearerLevelTrafficFlowTemplate extends InformationElement {
710
public EpsBearerLevelTrafficFlowTemplate(final byte type, final byte spare, final byte instance, final byte[] payload) {
@@ -35,6 +38,28 @@ public EpsBearerLevelTrafficFlowTemplate setNumberOfPacketFilters(final int numb
3538
return this;
3639
}
3740

41+
public List<PacketFilter> getPacketFilters() {
42+
final var packetFilters = new ArrayList<PacketFilter>();
43+
int offset = 1;
44+
for (int i = 0; i < getNumberOfPacketFilters(); i++) {
45+
final byte[] packetFilterRaw = new byte[payload[offset + 2] + 3];
46+
System.arraycopy(payload, offset, packetFilterRaw, 0, packetFilterRaw.length);
47+
final var packetFilter = new PacketFilter(packetFilterRaw);
48+
packetFilters.add(packetFilter);
49+
offset += packetFilter.getPacketFilterLength() + 3;
50+
}
51+
return packetFilters;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "EpsBearerLevelTrafficFlowTemplate{" +
57+
"tftOperationCode=" + getTftOperationCodeEnum() +
58+
", numberOfPacketFilters=" + getNumberOfPacketFilters() +
59+
", packetFilters=" + getPacketFilters() +
60+
'}';
61+
}
62+
3863
public enum TftOperationCode {
3964
IGNORE_THIS_IE(0),
4065
CREATE_NEW_TFT(1),
@@ -118,91 +143,41 @@ public PacketFilter setPacketFilterLength(final int packetFilterLength) {
118143
return this;
119144
}
120145

121-
public PacketFilterComponentType getPacketFilterComponentType() {
122-
return PacketFilterComponentType.fromCode(BitHelper.toInt(payload[3]));
123-
}
124-
125-
public PacketFilter setPacketFilterComponentType(final PacketFilterComponentType packetFilterComponentType) {
126-
payload[3] = (byte) packetFilterComponentType.getCode();
127-
// TODO: set length and re-init payload array based on length
128-
return this;
129-
}
130-
131-
public String getIpv4RemoteAddress() {
132-
if (getPacketFilterComponentType() != PacketFilterComponentType.IPV4_REMOTE_ADDRESS) {
133-
throw new IllegalStateException("Packet filter component type is not IPV4_REMOTE_ADDRESS");
134-
}
135-
136-
final byte[] ipv4Address = new byte[4];
137-
System.arraycopy(payload, 4, ipv4Address, 0, 4);
138-
139-
return IpV4V6.decodeV4(ipv4Address);
140-
}
141-
142-
public String getIpv4RemoteAddressMask() {
143-
if (getPacketFilterComponentType() != PacketFilterComponentType.IPV4_REMOTE_ADDRESS) {
144-
throw new IllegalStateException("Packet filter component type is not IPV4_REMOTE_ADDRESS");
146+
public List<GenericPacketFilterComponent> getPacketFilterComponents() {
147+
final var components = new ArrayList<GenericPacketFilterComponent>();
148+
int offset = 3;
149+
while (offset < payload.length) {
150+
final var componentType = GenericPacketFilterComponent.PacketFilterComponentType.fromCode(payload[offset]);
151+
if (componentType == null) {
152+
throw new IllegalArgumentException("Unknown packet filter component type: " + payload[offset]);
153+
}
154+
final var componentLength = componentType.getValueLength() + 1;
155+
final var componentPayload = new byte[componentLength];
156+
try {
157+
System.arraycopy(payload, offset, componentPayload, 0, componentLength);
158+
} catch (final ArrayIndexOutOfBoundsException e) {
159+
throw new IllegalArgumentException("Packet filter component length is invalid! Indicated length: " + componentLength + ", actual length: " + (payload.length - offset));
160+
}
161+
System.arraycopy(payload, offset, componentPayload, 0, componentLength);
162+
final var component = GenericPacketFilterComponent.fromBytes(componentPayload);
163+
components.add(component);
164+
offset += componentLength;
145165
}
146166

147-
final byte[] ipv4AddressMask = new byte[4];
148-
System.arraycopy(payload, 8, ipv4AddressMask, 0, 4);
149-
150-
return IpV4V6.decodeV4(ipv4AddressMask);
167+
return components;
151168
}
152169

153-
public PacketFilter setIpv4RemoteAddress(final String ipv4RemoteAddress) {
154-
if (getPacketFilterComponentType() != PacketFilterComponentType.IPV4_REMOTE_ADDRESS) {
155-
throw new IllegalStateException("Packet filter component type is not IPV4_REMOTE_ADDRESS");
156-
}
157-
158-
final byte[] ipv4Address = IpV4V6.encodeV4(ipv4RemoteAddress);
159-
System.arraycopy(ipv4Address, 0, payload, 4, 4);
160-
161-
return this;
170+
@Override
171+
public String toString() {
172+
return "PacketFilter{" +
173+
"packetFilterIdentifier=" + getPacketFilterIdentifier() +
174+
", packetFilterDirection=" + getPacketFilterDirection() +
175+
", packetFilterEvaluationPrecedence=" + getPacketFilterEvaluationPrecedence() +
176+
", packetFilterLength=" + getPacketFilterLength() +
177+
", packetFilterComponents=" + getPacketFilterComponents() +
178+
'}';
162179
}
163180

164-
public enum PacketFilterComponentType {
165-
IPV4_REMOTE_ADDRESS(16),
166-
IPV4_LOCAL_ADDRESS(17),
167-
IPV6_REMOTE_ADDRESS(32),
168-
IPV6_REMOTE_ADDRESS_PREFIX_LENGTH(33),
169-
IPV6_LOCAL_ADDRESS_PREFIX_LENGTH(35),
170-
PROTOCOL_IDENTIFIER_NEXT_HEADER(48),
171-
SINGLE_LOCAL_PORT(64),
172-
LOCAL_PORT_RANGE(65),
173-
SINGLE_REMOTE_PORT(80),
174-
REMOTE_PORT_RANGE(81),
175-
SECURITY_PARAMETER_INDEX(96),
176-
TYPE_OF_SERVICE_TRAFFIC_CLASS(112),
177-
FLOW_LABEL(128),
178-
DESTINATION_MAC_ADDRESS(129),
179-
SOURCE_MAC_ADDRESS(130),
180-
IEE_802_1Q_C_TAG_VID(131),
181-
IEE_802_1Q_S_TAG_VID(132),
182-
IEE_802_1Q_C_TAG_PCP(133),
183-
IEE_802_1Q_S_TAG_PCP(134),
184-
ETHERTYPE(135),
185-
;
186-
187-
private final int code;
188-
189-
PacketFilterComponentType(final int code) {
190-
this.code = code;
191-
}
192-
193-
public int getCode() {
194-
return code;
195-
}
196-
197-
public static PacketFilterComponentType fromCode(final int code) {
198-
for (final var packetFilterComponentType : values()) {
199-
if (packetFilterComponentType.getCode() == code) {
200-
return packetFilterComponentType;
201-
}
202-
}
203-
return null;
204-
}
205-
}
206181
}
207182

208183
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.sipgate.udpproxy.udp.payload.gtpv2.ie.tft;
2+
3+
public class GenericPacketFilterComponent {
4+
protected final byte[] payload;
5+
6+
public GenericPacketFilterComponent(final byte[] payload) {
7+
this.payload = payload;
8+
}
9+
10+
public byte[] getPayload() {
11+
return payload;
12+
}
13+
14+
public PacketFilterComponentType getPacketFilterComponentType() {
15+
return PacketFilterComponentType.fromCode(payload[0]);
16+
}
17+
18+
public enum PacketFilterComponentType {
19+
IPV4_REMOTE_ADDRESS(16, 8),
20+
IPV4_LOCAL_ADDRESS(17, 8),
21+
IPV6_REMOTE_ADDRESS(32, 32),
22+
IPV6_REMOTE_ADDRESS_PREFIX_LENGTH(33, 17),
23+
IPV6_LOCAL_ADDRESS_PREFIX_LENGTH(35, 17),
24+
PROTOCOL_IDENTIFIER_NEXT_HEADER(48, 1),
25+
SINGLE_LOCAL_PORT(64, 2),
26+
LOCAL_PORT_RANGE(65, 4),
27+
SINGLE_REMOTE_PORT(80, 2),
28+
REMOTE_PORT_RANGE(81, 4),
29+
SECURITY_PARAMETER_INDEX(96, 4),
30+
TYPE_OF_SERVICE_TRAFFIC_CLASS(112, 2),
31+
FLOW_LABEL(128, 3),
32+
DESTINATION_MAC_ADDRESS(129, 6),
33+
SOURCE_MAC_ADDRESS(130, 6),
34+
IEE_802_1Q_C_TAG_VID(131, 2),
35+
IEE_802_1Q_S_TAG_VID(132, 2),
36+
IEE_802_1Q_C_TAG_PCP(133, 1),
37+
IEE_802_1Q_S_TAG_PCP(134, 1),
38+
ETHERTYPE(135, 2),
39+
;
40+
41+
private final int code;
42+
private final int length;
43+
44+
PacketFilterComponentType(final int code, final int length) {
45+
this.code = code;
46+
this.length = length;
47+
}
48+
49+
public int getCode() {
50+
return code;
51+
}
52+
53+
public int getValueLength() {
54+
return length;
55+
}
56+
57+
public static PacketFilterComponentType fromCode(final int code) {
58+
for (final var packetFilterComponentType : values()) {
59+
if (packetFilterComponentType.getCode() == code) {
60+
return packetFilterComponentType;
61+
}
62+
}
63+
return null;
64+
}
65+
}
66+
67+
public static GenericPacketFilterComponent fromBytes(final byte[] rawBytes) {
68+
final var packetFilterComponentType = PacketFilterComponentType.fromCode(rawBytes[0]);
69+
if (packetFilterComponentType == null) {
70+
throw new IllegalArgumentException("Unknown packet filter component type: " + rawBytes[0]);
71+
}
72+
73+
switch (packetFilterComponentType) {
74+
case IPV4_REMOTE_ADDRESS, IPV4_LOCAL_ADDRESS:
75+
return new IPv4AddressComponent(rawBytes);
76+
case IPV6_REMOTE_ADDRESS:
77+
return new IPv6AddressComponent(rawBytes);
78+
case IPV6_REMOTE_ADDRESS_PREFIX_LENGTH, IPV6_LOCAL_ADDRESS_PREFIX_LENGTH:
79+
return new IPv6AddressPrefixComponent(rawBytes);
80+
default:
81+
return new GenericPacketFilterComponent(rawBytes);
82+
}
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return "GenericPacketFilterComponent{" +
88+
"packetFilterComponentType=" + getPacketFilterComponentType() +
89+
'}';
90+
}
91+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.sipgate.udpproxy.udp.payload.gtpv2.ie.tft;
2+
3+
import com.sipgate.udpproxy.udp.payload.gtpv2.ie.decoder.IpV4V6;
4+
5+
public class IPv4AddressComponent extends GenericPacketFilterComponent {
6+
public IPv4AddressComponent(final byte[] payload) {
7+
super(payload);
8+
}
9+
10+
public IPv4AddressComponent setPacketFilterComponentType(final PacketFilterComponentType packetFilterComponentType) {
11+
if (PacketFilterComponentType.IPV4_REMOTE_ADDRESS != packetFilterComponentType
12+
&& PacketFilterComponentType.IPV4_LOCAL_ADDRESS != packetFilterComponentType) {
13+
throw new IllegalArgumentException(
14+
"Packet filter component type must be IPV4_REMOTE_ADDRESS OR IPV4_LOCAL_ADDRESS"
15+
);
16+
}
17+
payload[0] = (byte) packetFilterComponentType.getCode();
18+
return this;
19+
}
20+
21+
public String getIpv4Address() {
22+
final byte[] ipv4Address = new byte[4];
23+
System.arraycopy(payload, 1, ipv4Address, 0, 4);
24+
25+
return IpV4V6.decodeV4(ipv4Address);
26+
}
27+
28+
public IPv4AddressComponent setIpv4RemoteAddress(final String ipv4RemoteAddress) {
29+
final byte[] ipv4Address = IpV4V6.encodeV4(ipv4RemoteAddress);
30+
System.arraycopy(ipv4Address, 0, payload, 1, 4);
31+
32+
return this;
33+
}
34+
35+
public String getIpv4Mask() {
36+
final byte[] ipv4Address = new byte[4];
37+
System.arraycopy(payload, 9, ipv4Address, 0, 4);
38+
39+
return IpV4V6.decodeV4(ipv4Address);
40+
}
41+
42+
public IPv4AddressComponent setIpv4Mask(final String ipv4RemoteAddress) {
43+
final byte[] ipv4Address = IpV4V6.encodeV4(ipv4RemoteAddress);
44+
System.arraycopy(ipv4Address, 0, payload, 9, 4);
45+
46+
return this;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "IPv4AddressComponent{" +
52+
"packetFilterComponentType=" + getPacketFilterComponentType() +
53+
", ipv4Address=" + getIpv4Address() +
54+
", ipv4Mask=" + getIpv4Mask() +
55+
'}';
56+
}
57+
58+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.sipgate.udpproxy.udp.payload.gtpv2.ie.tft;
2+
3+
import com.sipgate.udpproxy.udp.payload.gtpv2.ie.decoder.IpV4V6;
4+
5+
public class IPv6AddressComponent extends GenericPacketFilterComponent {
6+
public IPv6AddressComponent(final byte[] payload) {
7+
super(payload);
8+
}
9+
10+
public String getIpV6Address() {
11+
final byte[] ipv6Address = new byte[16];
12+
System.arraycopy(payload, 1, ipv6Address, 0, 16);
13+
14+
return IpV4V6.decodeV6(ipv6Address);
15+
}
16+
17+
public IPv6AddressComponent setIpV6Address(final String ipV6Address) {
18+
final byte[] ipv6Address = IpV4V6.encodeV6(ipV6Address);
19+
System.arraycopy(ipv6Address, 0, payload, 1, 16);
20+
21+
return this;
22+
}
23+
24+
public String getIpV6Mask() {
25+
final byte[] ipv6Address = new byte[16];
26+
System.arraycopy(payload, 17, ipv6Address, 0, 16);
27+
28+
return IpV4V6.decodeV6(ipv6Address);
29+
}
30+
31+
public IPv6AddressComponent setIpV6Mask(final String ipV6Mask) {
32+
final byte[] ipv6Address = IpV4V6.encodeV6(ipV6Mask);
33+
System.arraycopy(ipv6Address, 0, payload, 17, 16);
34+
35+
return this;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "IPv6AddressComponent{" +
41+
"packetFilterComponentType=" + getPacketFilterComponentType() +
42+
", ipV6Address=" + getIpV6Address() +
43+
", ipV6Mask=" + getIpV6Mask() +
44+
'}';
45+
}
46+
}

0 commit comments

Comments
 (0)