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
|
/*
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.backend;
import android.net.TrafficStats;
import android.util.Log;
import com.lumaserv.bgp.BGPListener;
import com.lumaserv.bgp.BGPServer;
import com.lumaserv.bgp.BGPSession;
import com.lumaserv.bgp.BGPSessionConfiguration;
import com.lumaserv.bgp.protocol.AFI;
import com.lumaserv.bgp.protocol.BGPPacket;
import com.lumaserv.bgp.protocol.IPPrefix;
import com.lumaserv.bgp.protocol.SAFI;
import com.lumaserv.bgp.protocol.attribute.ASPathAttribute;
import com.lumaserv.bgp.protocol.attribute.MPReachableNLRIAttribute;
import com.lumaserv.bgp.protocol.attribute.NextHopAttribute;
import com.lumaserv.bgp.protocol.attribute.OriginAttribute;
import com.lumaserv.bgp.protocol.attribute.PathAttribute;
import com.lumaserv.bgp.protocol.attribute.TunnelEncapsAttribute;
import com.lumaserv.bgp.protocol.message.BGPUpdate;
import com.wireguard.config.InetEndpoint;
import com.wireguard.config.InetNetwork;
import com.wireguard.crypto.Key;
import com.wireguard.crypto.KeyFormatException;
import io.grpc.ManagedChannel;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.net.SocketFactory;
public class Bgp implements BGPListener {
private static final String TAG = "WireGuard/Bgp";
private static final String SESSION = "demosession";
private static final int MY_ASN = (int)4200000201L;
private static final int REMOTE_ASN = (int)4200000010L;
private static final String REMOTE_ADDR = "10.49.32.1";
private static final String REMOTE_ID = "10.49.160.1";
private static final String LOCAL_ID = "10.49.33.218";
private static final int PORT = 0;
private static final int STATS_TAG = 1; // FIXME
private ManagedChannel channel;
private Tunnel tunnel;
private int tunnelHandle;
private BGPServer server;
public Bgp(ManagedChannel channel, Tunnel tunnel, int tunnelHandle) {
this.channel = channel;
this.tunnel = tunnel;
this.tunnelHandle = tunnelHandle;
}
@Override
public void onOpen(BGPSession session) {
// DO WHAT YOU WANT
Log.i(TAG, "onOpen");
// BGPUpdate update;
// {
// List<IPPrefix> prefixes = new ArrayList<>(1);
// prefixes.add(new IPPrefix(new byte[]{10, 49, 124, 105}, 32));
// List<PathAttribute> attrs = new ArrayList<>();
// attrs.add(new OriginAttribute(OriginAttribute.Origin.IGP));
// attrs.add(new NextHopAttribute().setAddress(new byte[]{10, 49, 125, 105}));
// ASPathAttribute.Segment seg = new ASPathAttribute.Segment();
// seg.setType(ASPathAttribute.Segment.Type.SEQUENCE);
// seg.getAsns().add(MY_ASN);
// ASPathAttribute asPath = new ASPathAttribute(session);
// asPath.getSegments().add(seg);
// attrs.add(asPath);
// update = new BGPUpdate().setAttributes(attrs).setPrefixes(prefixes);
// }
// BGPUpdate update2;
// try {
// List<PathAttribute> attrs = new ArrayList<>();
// attrs.add(new OriginAttribute(OriginAttribute.Origin.IGP));
// ASPathAttribute.Segment seg = new ASPathAttribute.Segment();
// seg.setType(ASPathAttribute.Segment.Type.SEQUENCE);
// seg.getAsns().add(MY_ASN);
// ASPathAttribute asPath = new ASPathAttribute(session);
// List<IPPrefix> prefixes = new ArrayList<>(1);
// prefixes.add(new IPPrefix(new byte[]{0x20, 0x1, 0x04, 0x70, (byte)0xdf, (byte)0xae, 0x63, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x05}, 128));
// MPReachableNLRIAttribute mpr = new MPReachableNLRIAttribute();
// mpr.setAfi(AFI.IPV6).setSafi(SAFI.UNICAST).setNextHop(InetAddress.getByName("2001:470:dfae:6300::1:105")).setNlriPrefixes(prefixes);
// attrs.add(mpr);
// asPath.getSegments().add(seg);
// attrs.add(asPath);
// update2 = new BGPUpdate().setAttributes(attrs);
// } catch (UnknownHostException ex) {
// throw new RuntimeException(ex);
// }
// try {
// session.sendUpdate(update);
// session.sendUpdate(update2);
// } catch (IOException ex) {
// throw new RuntimeException(ex);
// }
}
@Override
public void onUpdate(BGPSession session, BGPUpdate update) {
// DO WHAT YOU WANT
Log.i(TAG, "onUpdate: " + update.getPrefixes() + ",-" + update.getWithdrawnPrefixes() + "," + update.getAttributes());
MPReachableNLRIAttribute mpr = null;
TunnelEncapsAttribute te = null;
for (PathAttribute attr: update.getAttributes()) {
if (attr instanceof TunnelEncapsAttribute) {
te = (TunnelEncapsAttribute)attr;
} else if (attr instanceof MPReachableNLRIAttribute) {
mpr = (MPReachableNLRIAttribute)attr;
}
}
if (te == null) {
return;
}
TunnelEncapsAttribute.WireGuard wg = null;
TunnelEncapsAttribute.Color col = null;
TunnelEncapsAttribute.EgressEndpoint ep = null;
TunnelEncapsAttribute.UDPDestinationPort port = null;
for (TunnelEncapsAttribute.Tunnel t: te.getTunnels()) {
if (t.getType() != 51820) {
continue;
}
for (TunnelEncapsAttribute.SubTlv st: t.getSubTlvs()) {
if (st instanceof TunnelEncapsAttribute.WireGuard) {
wg = (TunnelEncapsAttribute.WireGuard)st;
} else if (st instanceof TunnelEncapsAttribute.Color) {
col = (TunnelEncapsAttribute.Color)st;
} else if (st instanceof TunnelEncapsAttribute.EgressEndpoint) {
ep = (TunnelEncapsAttribute.EgressEndpoint)st;
} else if (st instanceof TunnelEncapsAttribute.UDPDestinationPort) {
port = (TunnelEncapsAttribute.UDPDestinationPort)st;
}
}
}
if (wg == null) {
return;
}
try {
Key publicKey = Key.fromBytes(wg.getPublicKey());
InetEndpoint endpoint = null;
if (ep != null && port != null) {
endpoint = InetEndpoint.fromAddress(ep.getAddress(), port.getPort());
}
tunnel.onEndpointChange(publicKey, endpoint);
List<InetNetwork> addNetworks = new ArrayList<>();
List<InetNetwork> removeNetworks = new ArrayList<>();
for (IPPrefix prefix: update.getPrefixes()) {
try {
addNetworks.add(new InetNetwork(InetAddress.getByAddress(prefix.getAddress()), prefix.getLength()));
} catch (UnknownHostException ignore) {
}
}
for (IPPrefix prefix: update.getWithdrawnPrefixes()) {
try {
removeNetworks.add(new InetNetwork(InetAddress.getByAddress(prefix.getAddress()), prefix.getLength()));
} catch (UnknownHostException ignore) {
}
}
if (mpr != null && (mpr.getAfi() == AFI.IPV6 || mpr.getAfi() == AFI.IPV4) && mpr.getSafi() == SAFI.UNICAST) {
for (IPPrefix prefix: mpr.getNlriPrefixes()) {
try {
addNetworks.add(new InetNetwork(InetAddress.getByAddress(prefix.getAddress()), prefix.getLength()));
} catch (UnknownHostException ignore) {
}
}
}
tunnel.onAllowedIpsChange(publicKey, addNetworks, removeNetworks);
} catch (KeyFormatException ex) {
Log.w(TAG, "Key.fromBytes " + ex);
}
}
@Override
public void onClose(BGPSession session) {
// NOT YET IMPLEMENTED
Log.i(TAG, "onClose");
}
public boolean startServer() {
stopServer();
try {
SocketFactory factory = new SocketFactory() {
private Socket taggedSocket(Socket sock) throws SocketException {
TrafficStats.tagSocket(sock);
return sock;
}
@Override
public Socket createSocket(String host, int port) throws IOException {
TrafficStats.setThreadStatsTag(STATS_TAG);
return taggedSocket(new Socket(host, port));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
TrafficStats.setThreadStatsTag(STATS_TAG);
return taggedSocket(new Socket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
TrafficStats.setThreadStatsTag(STATS_TAG);
return taggedSocket(new Socket(address, port, localAddress, localPort));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
TrafficStats.setThreadStatsTag(STATS_TAG);
return taggedSocket(new Socket(host, port, localHost, localPort));
}
};
BGPSessionConfiguration config =
new BGPSessionConfiguration(SESSION,
MY_ASN,
ip(LOCAL_ID),
REMOTE_ASN,
ip(REMOTE_ID),
null, // Remote address
factory,
this);
TrafficStats.setThreadStatsTag(STATS_TAG);
ServerSocket socket = new ServerSocket(PORT);
//TrafficStats.tagSocket(socket);
// Set
server = new BGPServer(socket);
server.getSessionConfigurations().add(config);
server.connect(config, REMOTE_ADDR);
return true;
} catch (IOException ex) {
return false;
}
}
public void stopServer() {
if (server != null) {
server.shutdown();
server = null;
}
}
private static byte[] ip(String s) throws UnknownHostException {
InetAddress addr = InetAddress.getByName(s);
byte[] data = addr.getAddress();
if (data.length != 4)
throw new UnknownHostException(s + ": Not an IPv4 address");
return data;
}
}
|