DhcpRelay.java
17.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.dhcprelay;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.packet.ARP;
import org.onlab.packet.DHCP;
import org.onlab.packet.DHCPOption;
import org.onlab.packet.DHCPPacketType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onlab.packet.TpPort;
import org.onlab.packet.UDP;
import org.onlab.packet.VlanId;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.incubator.net.intf.Interface;
import org.onosproject.incubator.net.intf.InterfaceService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.host.HostService;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.OutboundPacket;
import org.onosproject.net.packet.PacketContext;
import org.onosproject.net.packet.PacketPriority;
import org.onosproject.net.packet.PacketProcessor;
import org.onosproject.net.packet.PacketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableSet;
import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
import static org.onlab.packet.MacAddress.valueOf;
/**
* DHCP Relay Agent Application Component.
*/
@Component(immediate = true)
public class DhcpRelay {
public static final String DHCP_RELAY_APP = "org.onosproject.dhcp-relay";
private final Logger log = LoggerFactory.getLogger(getClass());
private final InternalConfigListener cfgListener = new InternalConfigListener();
private static Ip4Address relayAgentIP = null;
private static MacAddress relayAgentMAC = null;
private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f");
private final Set<ConfigFactory> factories = ImmutableSet.of(
new ConfigFactory<ApplicationId, DhcpRelayConfig>(APP_SUBJECT_FACTORY,
DhcpRelayConfig.class,
"dhcprelay") {
@Override
public DhcpRelayConfig createConfig() {
return new DhcpRelayConfig();
}
}
);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry cfgService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected PacketService packetService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected InterfaceService interfaceService;
private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor();
private ConnectPoint dhcpServerConnectPoint = null;
private Ip4Address dhcpServerIp = null;
private MacAddress dhcpServerMac = null;
private ApplicationId appId;
@Activate
protected void activate() {
//start the dhcp relay agent
appId = coreService.registerApplication(DHCP_RELAY_APP);
cfgService.addListener(cfgListener);
factories.forEach(cfgService::registerConfigFactory);
//update the dhcp server configuration.
updateConfig();
//add the packet services.
packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0));
requestPackets();
log.info("DHCP-RELAY Started");
log.info("started the apps dhcp relay");
}
@Deactivate
protected void deactivate() {
cfgService.removeListener(cfgListener);
factories.forEach(cfgService::unregisterConfigFactory);
packetService.removeProcessor(dhcpRelayPacketProcessor);
cancelPackets();
log.info("DHCP-RELAY Stopped");
}
private void updateConfig() {
DhcpRelayConfig cfg = cfgService.getConfig(appId, DhcpRelayConfig.class);
if (cfg == null) {
log.warn("Dhcp Server info not available");
return;
}
dhcpServerConnectPoint = cfg.getDhcpServerConnectPoint();
dhcpServerIp = cfg.getDhcpServerIp();
dhcpServerMac = cfg.getDhcpServermac();
log.info("dhcp server connect points are " + dhcpServerConnectPoint);
log.info("dhcp server ipaddress " + dhcpServerIp);
log.info("dhcp server mac address " + dhcpServerMac);
}
/**
* Request packet in via PacketService.
*/
private void requestPackets() {
TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchIPProtocol(IPv4.PROTOCOL_UDP)
.matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchIPProtocol(IPv4.PROTOCOL_UDP)
.matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
packetService.requestPackets(selectorClient.build(), PacketPriority.CONTROL, appId);
TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_ARP);
packetService.requestPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId);
}
/**
* Cancel requested packets in via packet service.
*/
private void cancelPackets() {
TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchIPProtocol(IPv4.PROTOCOL_UDP)
.matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchIPProtocol(IPv4.PROTOCOL_UDP)
.matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
packetService.cancelPackets(selectorClient.build(), PacketPriority.CONTROL, appId);
TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_ARP);
packetService.cancelPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId);
}
private class DhcpRelayPacketProcessor implements PacketProcessor {
@Override
public void process(PacketContext context) {
// process the packet and get the payload
Ethernet packet = context.inPacket().parsed();
if (packet == null) {
return;
}
if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
IPv4 ipv4Packet = (IPv4) packet.getPayload();
if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT ||
udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
//This packet is dhcp.
processDhcpPacket(context, dhcpPayload);
}
}
} else if (packet.getEtherType() == Ethernet.TYPE_ARP) {
ARP arpPacket = (ARP) packet.getPayload();
Set<Interface> serverInterfaces = interfaceService.
getInterfacesByPort(context.inPacket().receivedFrom());
//ignore the packets if dhcp server interface is not configured on onos.
if (serverInterfaces.isEmpty()) {
log.warn("server virtual interface not configured");
return;
}
if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
checkArpRequestFrmDhcpServ(serverInterfaces, arpPacket)) {
processArpPacket(context, packet);
}
}
}
//method to check the arp request is from dhcp server for default-gateway.
private boolean checkArpRequestFrmDhcpServ(Set<Interface> serverInterfaces, ARP arpPacket) {
if (Objects.equals(serverInterfaces.iterator().next().ipAddressesList().get(0).
ipAddress().getIp4Address(),
Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) {
return true;
}
return false;
}
//forward the packet to ConnectPoint where the DHCP server is attached.
private void forwardPacket(Ethernet packet) {
//send Packetout to dhcp server connectpoint.
if (dhcpServerConnectPoint != null) {
TrafficTreatment t = DefaultTrafficTreatment.builder()
.setOutput(dhcpServerConnectPoint.port()).build();
OutboundPacket o = new DefaultOutboundPacket(
dhcpServerConnectPoint.deviceId(), t, ByteBuffer.wrap(packet.serialize()));
packetService.emit(o);
}
}
/**
* Processes the ARP Payload and initiates a reply to the client.
*
* @param context context of the incoming message
* @param packet the ethernet payload
*/
private void processArpPacket(PacketContext context, Ethernet packet) {
ARP arpPacket = (ARP) packet.getPayload();
ARP arpReply = (ARP) arpPacket.clone();
arpReply.setOpCode(ARP.OP_REPLY);
arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
arpReply.setSenderHardwareAddress(myMAC.toBytes());
// Ethernet Frame.
Ethernet ethReply = new Ethernet();
ethReply.setSourceMACAddress(myMAC);
ethReply.setDestinationMACAddress(packet.getSourceMAC());
ethReply.setEtherType(Ethernet.TYPE_ARP);
ethReply.setVlanID(packet.getVlanID());
ethReply.setPayload(arpReply);
forwardPacket(ethReply);
}
//process the dhcp packet before sending to server
private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) {
Set<Interface> clientServerInterfaces = interfaceService.
getInterfacesByPort(context.inPacket().receivedFrom());
//ignore the packets if dhcp client interface is not configured on onos.
if (clientServerInterfaces.isEmpty()) {
log.warn("client virtual interface not configured");
return;
}
if (dhcpPayload == null) {
return;
}
Ethernet packet = context.inPacket().parsed();
DHCPPacketType incomingPacketType = null;
for (DHCPOption option : dhcpPayload.getOptions()) {
if (option.getCode() == OptionCode_MessageType.getValue()) {
byte[] data = option.getData();
incomingPacketType = DHCPPacketType.getType(data[0]);
}
}
switch (incomingPacketType) {
case DHCPDISCOVER:
//add the gatewayip as virtual interface ip for server to understand the lease to be assigned
//and forward the packet to dhcp server.
Ethernet ethernetPacketDiscover = processDhcpPacketFrmClient(context, packet, clientServerInterfaces);
forwardPacket(ethernetPacketDiscover);
break;
case DHCPOFFER:
//reply to dhcp client.
Ethernet ethernetPacketOffer = processDhcpPacketFrmServer(packet);
sendReply(ethernetPacketOffer, dhcpPayload);
break;
case DHCPREQUEST:
//add the gatewayip as virtual interface ip for server to understand the lease to be assigned
//and forward the packet to dhcp server.
Ethernet ethernetPacketRequest = processDhcpPacketFrmClient(context, packet, clientServerInterfaces);
forwardPacket(ethernetPacketRequest);
break;
case DHCPACK:
//reply to dhcp client.
Ethernet ethernetPacketAck = processDhcpPacketFrmServer(packet);
sendReply(ethernetPacketAck, dhcpPayload);
break;
default:
break;
}
}
//build the DHCP discover/request packet with gatewayip(unicast packet)
private Ethernet processDhcpPacketFrmClient(PacketContext context, Ethernet ethernetPacket,
Set<Interface> clientInterfaces) {
//assuming one interface per port for now.
relayAgentIP = clientInterfaces.iterator().next().ipAddressesList().get(0).
ipAddress().getIp4Address();
relayAgentMAC = clientInterfaces.iterator().next().mac();
// get dhcp header.
Ethernet etherReply = (Ethernet) ethernetPacket.clone();
etherReply.setSourceMACAddress(relayAgentMAC);
etherReply.setDestinationMACAddress(dhcpServerMac);
IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
ipv4Packet.setSourceAddress(relayAgentIP.toInt());
ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
dhcpPacket.setGatewayIPAddress(relayAgentIP.toInt());
udpPacket.setPayload(dhcpPacket);
ipv4Packet.setPayload(udpPacket);
etherReply.setPayload(ipv4Packet);
return etherReply;
}
//build the DHCP offer/ack with proper client port.
private Ethernet processDhcpPacketFrmServer(Ethernet ethernetPacket) {
// get dhcp header.
Ethernet etherReply = (Ethernet) ethernetPacket.clone();
IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
//set the ethernet frame.
etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
udpPacket.setPayload(dhcpPayload);
ipv4Packet.setPayload(udpPacket);
etherReply.setPayload(ipv4Packet);
return etherReply;
}
//send the response to the requestor host.
private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) {
MacAddress descMac = new MacAddress(dhcpPayload.getClientHardwareAddress());
Host host = hostService.getHost(HostId.hostId(descMac,
VlanId.vlanId(ethPacket.getVlanID())));
ConnectPoint dhcpRequestor = new ConnectPoint(host.location().elementId(),
host.location().port());
//send Packetout to requestor host.
if (dhcpRequestor != null) {
TrafficTreatment t = DefaultTrafficTreatment.builder()
.setOutput(dhcpRequestor.port()).build();
OutboundPacket o = new DefaultOutboundPacket(
dhcpRequestor.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize()));
packetService.emit(o);
}
}
}
/**
* Listener for network config events.
*/
private class InternalConfigListener implements NetworkConfigListener {
@Override
public void event(NetworkConfigEvent event) {
if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
event.configClass().equals(DhcpRelayConfig.class)) {
updateConfig();
log.info("Reconfigured");
}
}
}
}