From 1d26c0a20d3c595d2846de31c2034e27595540b1 Mon Sep 17 00:00:00 2001 From: JanHolger Date: Tue, 6 Jul 2021 17:01:33 +0200 Subject: Initial commit --- src/main/java/com/lumaserv/bgp/BGPListener.java | 11 ++++ src/main/java/com/lumaserv/bgp/BGPServer.java | 70 ++++++++++++++++++++ src/main/java/com/lumaserv/bgp/BGPSession.java | 54 +++++++++++++++ .../com/lumaserv/bgp/BGPSessionConfiguration.java | 19 ++++++ .../java/com/lumaserv/bgp/protocol/BGPPacket.java | 76 ++++++++++++++++++++++ .../java/com/lumaserv/bgp/protocol/IPPrefix.java | 27 ++++++++ .../protocol/attribute/AS4AggregatorAttribute.java | 30 +++++++++ .../bgp/protocol/attribute/AS4PathAttribute.java | 45 +++++++++++++ .../bgp/protocol/attribute/ASPathAttribute.java | 40 ++++++++++++ .../protocol/attribute/ASPathLimitAttribute.java | 29 +++++++++ .../protocol/attribute/AggregatorAttribute.java | 27 ++++++++ .../attribute/AtomicAggregateAttribute.java | 17 +++++ .../protocol/attribute/CommunitiesAttribute.java | 41 ++++++++++++ .../protocol/attribute/DevelopmentAttribute.java | 24 +++++++ .../attribute/ExtendedCommuntiesAttribute.java | 33 ++++++++++ .../attribute/LargeCommunityAttribute.java | 56 ++++++++++++++++ .../bgp/protocol/attribute/NextHopAttribute.java | 20 ++++++ .../bgp/protocol/attribute/OriginAttribute.java | 24 +++++++ .../bgp/protocol/attribute/PathAttribute.java | 38 +++++++++++ .../bgp/protocol/attribute/UnknownAttribute.java | 23 +++++++ .../bgp/protocol/message/BGPNotification.java | 18 +++++ .../com/lumaserv/bgp/protocol/message/BGPOpen.java | 35 ++++++++++ .../lumaserv/bgp/protocol/message/BGPUpdate.java | 63 ++++++++++++++++++ 23 files changed, 820 insertions(+) create mode 100644 src/main/java/com/lumaserv/bgp/BGPListener.java create mode 100644 src/main/java/com/lumaserv/bgp/BGPServer.java create mode 100644 src/main/java/com/lumaserv/bgp/BGPSession.java create mode 100644 src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/AS4AggregatorAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/AS4PathAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/PathAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/message/BGPOpen.java create mode 100644 src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java (limited to 'src/main/java') diff --git a/src/main/java/com/lumaserv/bgp/BGPListener.java b/src/main/java/com/lumaserv/bgp/BGPListener.java new file mode 100644 index 0000000..54f4fe5 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/BGPListener.java @@ -0,0 +1,11 @@ +package com.lumaserv.bgp; + +import com.lumaserv.bgp.protocol.message.BGPUpdate; + +public interface BGPListener { + + void onOpen(BGPSession session); + void onUpdate(BGPSession session, BGPUpdate update); + void onClose(BGPSession session); + +} diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java new file mode 100644 index 0000000..2e960fe --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -0,0 +1,70 @@ +package com.lumaserv.bgp; + +import com.lumaserv.bgp.protocol.BGPPacket; +import com.lumaserv.bgp.protocol.message.BGPOpen; +import lombok.Getter; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +public class BGPServer implements Runnable { + + final ServerSocket serverSocket; + @Getter + final List sessionConfigurations = new ArrayList<>(); + + public BGPServer() throws IOException { + serverSocket = new ServerSocket(179); + } + + private static boolean checkEqual(byte[] a, byte[] b) { + if(a == b) + return true; + if(a == null || b == null) + return false; + if(a.length != b.length) + return false; + for(int i=0; i c.getRemoteAs() == request.getAsn() && checkEqual(c.getRemoteIdentifier(), request.getIdentifier())) + .findFirst() + .orElse(null); + if(config == null) + continue; + BGPOpen response = new BGPOpen() + .setAsn(config.getLocalAs()) + .setHoldTime(request.getHoldTime()) + .setVersion(request.getVersion()) + .setIdentifier(config.getLocalIdentifier()); + try { + socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response.build()).build()); + } catch (IOException e) { + e.printStackTrace(); + } + BGPSession session = new BGPSession(socket, config); + session.keepAlive(); + new Thread(session).start(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + +} diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java new file mode 100644 index 0000000..de1fc66 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -0,0 +1,54 @@ +package com.lumaserv.bgp; + +import com.lumaserv.bgp.protocol.BGPPacket; +import com.lumaserv.bgp.protocol.message.BGPUpdate; +import lombok.Getter; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class BGPSession implements Runnable { + + @Getter + final BGPSessionConfiguration configuration; + final InputStream inputStream; + final OutputStream outputStream; + + public BGPSession(Socket socket, BGPSessionConfiguration configuration) throws IOException { + this.configuration = configuration; + this.inputStream = socket.getInputStream(); + this.outputStream = socket.getOutputStream(); + } + + public void keepAlive() { + try { + outputStream.write(new BGPPacket().setType(BGPPacket.Type.KEEPALIVE).setMessage(new byte[0]).build()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void handle(BGPPacket packet) { + switch (packet.getType()) { + case KEEPALIVE: + keepAlive(); + break; + case UPDATE: { + configuration.getListener().onUpdate(this, new BGPUpdate(packet.getMessage())); + break; + } + } + } + + public void run() { + try { + while (true) + handle(BGPPacket.read(inputStream)); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java b/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java new file mode 100644 index 0000000..8102f81 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java @@ -0,0 +1,19 @@ +package com.lumaserv.bgp; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Setter +@Getter +public class BGPSessionConfiguration { + + String name; + int localAs; + byte[] localIdentifier; + int remoteAs; + byte[] remoteIdentifier; + BGPListener listener; + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java b/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java new file mode 100644 index 0000000..e7cf66b --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java @@ -0,0 +1,76 @@ +package com.lumaserv.bgp.protocol; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +@Setter +@Getter +@NoArgsConstructor +public class BGPPacket { + + Type type; + byte[] message; + + public BGPPacket(byte[] packet) { + type = Type.fromValue(packet[18]); + message = new byte[packet.length-19]; + System.arraycopy(packet, 19, message, 0, message.length); + } + + public byte[] build() { + byte[] packet = new byte[message.length + 19]; + Arrays.fill(packet, 0, 16, (byte) 0xFF); + packet[16] = (byte)(packet.length >> 8); + packet[17] = (byte)(packet.length & 0xFF); + packet[18] = type.getValue(); + System.arraycopy(message, 0, packet, 19, message.length); + return packet; + } + + public static BGPPacket read(InputStream stream) throws IOException { + byte[] packet = new byte[18]; + int value; + for(int i=0; i segments = new ArrayList<>(); + + public AS4PathAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + Segment segment = new Segment().setType(data[offset]); + byte length = data[offset + 1]; + for(int i=0; i asns = new ArrayList<>(); + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java new file mode 100644 index 0000000..64be45b --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java @@ -0,0 +1,40 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +public class ASPathAttribute implements PathAttribute { + + List segments = new ArrayList<>(); + + public ASPathAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + Segment segment = new Segment().setType(data[offset]); + byte length = data[offset + 1]; + for(int i=0; i asns = new ArrayList<>(); + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java new file mode 100644 index 0000000..732b018 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java @@ -0,0 +1,29 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class ASPathLimitAttribute implements PathAttribute { + + byte upperBound; + int as; + + public ASPathLimitAttribute(byte typeCode, byte[] data) { + upperBound = data[0]; + as = ((data[1] & 0xFF) << 24) | + ((data[2] & 0xFF) << 16) | + ((data[3] & 0xFF) << 8) | + (data[4] & 0xFF); + } + + public byte getTypeCode() { + return 21; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java new file mode 100644 index 0000000..8a3dfdb --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java @@ -0,0 +1,27 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class AggregatorAttribute implements PathAttribute { + + int as; + byte[] origin; + + public AggregatorAttribute(byte typeCode, byte[] data) { + as = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF); + origin = new byte[4]; + System.arraycopy(data, 2, origin, 0, origin.length); + } + + public byte getTypeCode() { + return 7; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java new file mode 100644 index 0000000..6f7e479 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java @@ -0,0 +1,17 @@ +package com.lumaserv.bgp.protocol.attribute; + +public class AtomicAggregateAttribute implements PathAttribute { + + public AtomicAggregateAttribute(byte typeCode, byte[] data) { + + } + + public byte getTypeCode() { + return 6; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java new file mode 100644 index 0000000..7c7f060 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java @@ -0,0 +1,41 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class CommunitiesAttribute implements PathAttribute { + + List communities = new ArrayList<>(); + + public CommunitiesAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + communities.add(new Community() + .setValueA(((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF)) + .setValueB(((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF)) + ); + offset += 4; + } + } + + public byte getTypeCode() { + return 8; + } + + public byte[] build() { + return new byte[0]; + } + + @Setter + @Getter + public static class Community { + int valueA; + int valueB; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java new file mode 100644 index 0000000..453f934 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java @@ -0,0 +1,24 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DevelopmentAttribute implements PathAttribute { + + byte[] data; + + public DevelopmentAttribute(byte typeCode, byte[] data) { + this.data = data; + } + + public byte[] build() { + return data; + } + + public byte getTypeCode() { + return (byte) 255; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java new file mode 100644 index 0000000..759ae60 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java @@ -0,0 +1,33 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ExtendedCommuntiesAttribute implements PathAttribute { + + byte type; + byte subType; + int as; + int an; + + public ExtendedCommuntiesAttribute(byte typeCode, byte[] data) { + type = data[0]; + subType = data[1]; + as = ((data[2] & 0xFF) << 24) | + ((data[3] & 0xFF) << 16) | + ((data[4] & 0xFF) << 8) | + (data[5] & 0xFF); + an = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF); + } + + public byte getTypeCode() { + return 16; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java new file mode 100644 index 0000000..921d5a5 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java @@ -0,0 +1,56 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class LargeCommunityAttribute implements PathAttribute { + + List communities = new ArrayList<>(); + + public LargeCommunityAttribute(byte typeCode, byte[] data) { + for(int i=0; i> 8); + message[2] = (byte) (asn & 0xFF); + message[3] = (byte) (holdTime >> 8); + message[4] = (byte) (holdTime & 0xFF); + System.arraycopy(identifier, 0, message, 5, identifier.length); + return message; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java new file mode 100644 index 0000000..fa23432 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java @@ -0,0 +1,63 @@ +package com.lumaserv.bgp.protocol.message; + +import com.lumaserv.bgp.protocol.IPPrefix; +import com.lumaserv.bgp.protocol.attribute.PathAttribute; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class BGPUpdate { + + List withdrawnPrefixes = new ArrayList<>(); + List attributes = new ArrayList<>(); + List prefixes = new ArrayList<>(); + + public BGPUpdate(byte[] message) { + int routesLength = ((message[0] & 0xFF) << 8) | (message[1] & 0xFF); + int offset = 2; + int offsetOffset = 2; + while ((offset - offsetOffset) < routesLength) { + IPPrefix prefix = new IPPrefix() + .setLength(message[offset]) + .setAddress(new byte[4]); + offset++; + int addressLen = (int) Math.ceil(prefix.getLength() / 8d); + System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen); + offset += addressLen; + withdrawnPrefixes.add(prefix); + } + int attributesLength = ((message[offset] & 0xFF) << 8) | (message[offset + 1] & 0xFF); + offset += 2; + offsetOffset = offset; + while ((offset - offsetOffset) < attributesLength) { + byte flags = message[offset]; + byte typeCode = message[offset + 1]; + int length = message[offset + 2] & 0xFF; + offset += 3; + if((flags & 0b0001_0000) > 0) { + length <<= 8; + length |= message[offset] & 0xFF; + offset++; + } + byte[] data = new byte[length]; + System.arraycopy(message, offset, data, 0, length); + offset += length; + attributes.add(PathAttribute.from(typeCode, data)); + } + while (offset < message.length) { + IPPrefix prefix = new IPPrefix() + .setLength(message[offset]) + .setAddress(new byte[4]); + offset++; + int addressLen = (int) Math.ceil(prefix.getLength() / 8d); + System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen); + offset += addressLen; + prefixes.add(prefix); + } + } + +} -- cgit v1.2.3