blob: d715c0647c958784a402c3c3135e931bde4ca999 [file] [log] [blame]
Yi Tseng51301292017-07-28 13:02:59 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.dhcprelay;
19
Yi Tseng7da339e2017-10-23 19:39:39 -070020import com.google.common.collect.HashMultimap;
21import com.google.common.collect.ImmutableSet;
Yi Tseng2fe8f3f2017-09-07 16:22:51 -070022import com.google.common.collect.Lists;
Yi Tseng7da339e2017-10-23 19:39:39 -070023import com.google.common.collect.Multimap;
Yi Tseng51301292017-07-28 13:02:59 -070024import com.google.common.collect.Sets;
Yi Tsenge72fbb52017-08-02 15:03:31 -070025import org.apache.felix.scr.annotations.Activate;
Yi Tseng51301292017-07-28 13:02:59 -070026import org.apache.felix.scr.annotations.Component;
Yi Tsenge72fbb52017-08-02 15:03:31 -070027import org.apache.felix.scr.annotations.Deactivate;
Yi Tseng51301292017-07-28 13:02:59 -070028import org.apache.felix.scr.annotations.Property;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32import org.onlab.packet.BasePacket;
33import org.onlab.packet.DHCP;
34import org.onlab.packet.Ethernet;
35import org.onlab.packet.IPv4;
36import org.onlab.packet.Ip4Address;
37import org.onlab.packet.IpAddress;
38import org.onlab.packet.MacAddress;
Yi Tseng7da339e2017-10-23 19:39:39 -070039import org.onlab.packet.TpPort;
Yi Tseng51301292017-07-28 13:02:59 -070040import org.onlab.packet.UDP;
41import org.onlab.packet.VlanId;
42import org.onlab.packet.dhcp.CircuitId;
43import org.onlab.packet.dhcp.DhcpOption;
44import org.onlab.packet.dhcp.DhcpRelayAgentOption;
Yi Tseng7da339e2017-10-23 19:39:39 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
Yi Tseng51301292017-07-28 13:02:59 -070047import org.onosproject.dhcprelay.api.DhcpHandler;
Yi Tseng2fe8f3f2017-09-07 16:22:51 -070048import org.onosproject.dhcprelay.api.DhcpServerInfo;
Yi Tsenge72fbb52017-08-02 15:03:31 -070049import org.onosproject.dhcprelay.config.DhcpServerConfig;
Yi Tseng7da339e2017-10-23 19:39:39 -070050import org.onosproject.dhcprelay.config.IgnoreDhcpConfig;
Yi Tseng51301292017-07-28 13:02:59 -070051import org.onosproject.dhcprelay.store.DhcpRecord;
52import org.onosproject.dhcprelay.store.DhcpRelayStore;
Yi Tseng7da339e2017-10-23 19:39:39 -070053import org.onosproject.net.Device;
54import org.onosproject.net.DeviceId;
55import org.onosproject.net.behaviour.Pipeliner;
56import org.onosproject.net.device.DeviceService;
57import org.onosproject.net.flow.DefaultTrafficSelector;
58import org.onosproject.net.flow.TrafficSelector;
59import org.onosproject.net.flowobjective.DefaultForwardingObjective;
60import org.onosproject.net.flowobjective.FlowObjectiveService;
61import org.onosproject.net.flowobjective.ForwardingObjective;
62import org.onosproject.net.flowobjective.Objective;
63import org.onosproject.net.flowobjective.ObjectiveContext;
64import org.onosproject.net.flowobjective.ObjectiveError;
Yi Tsenge72fbb52017-08-02 15:03:31 -070065import org.onosproject.net.host.HostEvent;
66import org.onosproject.net.host.HostListener;
Yi Tseng4b013202017-09-08 17:22:51 -070067import org.onosproject.net.host.HostProvider;
68import org.onosproject.net.host.HostProviderRegistry;
69import org.onosproject.net.host.HostProviderService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070070import org.onosproject.net.intf.Interface;
71import org.onosproject.net.intf.InterfaceService;
Yi Tseng7da339e2017-10-23 19:39:39 -070072import org.onosproject.net.packet.PacketPriority;
Yi Tseng4b013202017-09-08 17:22:51 -070073import org.onosproject.net.provider.ProviderId;
Ray Milkey69ec8712017-08-08 13:00:43 -070074import org.onosproject.routeservice.Route;
75import org.onosproject.routeservice.RouteStore;
Yi Tseng51301292017-07-28 13:02:59 -070076import org.onosproject.net.ConnectPoint;
77import org.onosproject.net.Host;
78import org.onosproject.net.HostId;
79import org.onosproject.net.HostLocation;
80import org.onosproject.net.flow.DefaultTrafficTreatment;
81import org.onosproject.net.flow.TrafficTreatment;
82import org.onosproject.net.host.DefaultHostDescription;
83import org.onosproject.net.host.HostDescription;
84import org.onosproject.net.host.HostService;
Yi Tseng51301292017-07-28 13:02:59 -070085import org.onosproject.net.host.InterfaceIpAddress;
86import org.onosproject.net.packet.DefaultOutboundPacket;
87import org.onosproject.net.packet.OutboundPacket;
88import org.onosproject.net.packet.PacketContext;
89import org.onosproject.net.packet.PacketService;
90import org.slf4j.Logger;
91import org.slf4j.LoggerFactory;
92
93import java.nio.ByteBuffer;
Yi Tsengdcef2c22017-08-05 20:34:06 -070094import java.util.Collection;
Yi Tseng51301292017-07-28 13:02:59 -070095import java.util.Collections;
96import java.util.List;
97import java.util.Optional;
98import java.util.Set;
Yi Tseng7da339e2017-10-23 19:39:39 -070099import java.util.concurrent.atomic.AtomicInteger;
Yi Tseng51301292017-07-28 13:02:59 -0700100import java.util.stream.Collectors;
101
102import static com.google.common.base.Preconditions.checkNotNull;
103import static com.google.common.base.Preconditions.checkState;
104import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_CircuitID;
105import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_END;
106import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
107import static org.onlab.packet.MacAddress.valueOf;
108import static org.onlab.packet.dhcp.DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID;
Yi Tseng7da339e2017-10-23 19:39:39 -0700109import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
110import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;
Yi Tseng51301292017-07-28 13:02:59 -0700111
112@Component
113@Service
114@Property(name = "version", value = "4")
Yi Tseng4b013202017-09-08 17:22:51 -0700115public class Dhcp4HandlerImpl implements DhcpHandler, HostProvider {
Charles Chan75edab72017-09-12 17:09:32 -0700116 public static final String DHCP_V4_RELAY_APP = "org.onosproject.Dhcp4HandlerImpl";
117 public static final ProviderId PROVIDER_ID = new ProviderId("dhcp4", DHCP_V4_RELAY_APP);
Yi Tseng7da339e2017-10-23 19:39:39 -0700118 private static final String BROADCAST_IP = "255.255.255.255";
119 private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000;
120
121 private static final TrafficSelector CLIENT_SERVER_SELECTOR = DefaultTrafficSelector.builder()
122 .matchEthType(Ethernet.TYPE_IPV4)
123 .matchIPProtocol(IPv4.PROTOCOL_UDP)
124 .matchIPSrc(Ip4Address.ZERO.toIpPrefix())
125 .matchIPDst(Ip4Address.valueOf(BROADCAST_IP).toIpPrefix())
126 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT))
127 .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
128 .build();
129 private static final TrafficSelector SERVER_RELAY_SELECTOR = DefaultTrafficSelector.builder()
130 .matchEthType(Ethernet.TYPE_IPV4)
131 .matchIPProtocol(IPv4.PROTOCOL_UDP)
132 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
133 .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
134 .build();
135 static final Set<TrafficSelector> DHCP_SELECTORS = ImmutableSet.of(
136 CLIENT_SERVER_SELECTOR,
137 SERVER_RELAY_SELECTOR
138 );
Yi Tseng51301292017-07-28 13:02:59 -0700139 private static Logger log = LoggerFactory.getLogger(Dhcp4HandlerImpl.class);
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected DhcpRelayStore dhcpRelayStore;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
145 protected PacketService packetService;
146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng51301292017-07-28 13:02:59 -0700148 protected RouteStore routeStore;
149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
151 protected InterfaceService interfaceService;
152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
154 protected HostService hostService;
155
Yi Tseng4b013202017-09-08 17:22:51 -0700156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 protected HostProviderRegistry providerRegistry;
158
Yi Tseng7da339e2017-10-23 19:39:39 -0700159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 protected CoreService coreService;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
163 protected DeviceService deviceService;
164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
166 protected FlowObjectiveService flowObjectiveService;
167
Yi Tseng4b013202017-09-08 17:22:51 -0700168 protected HostProviderService providerService;
Yi Tseng7da339e2017-10-23 19:39:39 -0700169 protected ApplicationId appId;
170 protected Multimap<DeviceId, VlanId> ignoredVlans = HashMultimap.create();
Yi Tsenge72fbb52017-08-02 15:03:31 -0700171 private InternalHostListener hostListener = new InternalHostListener();
172
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700173 private List<DhcpServerInfo> defaultServerInfoList = Lists.newArrayList();
174 private List<DhcpServerInfo> indirectServerInfoList = Lists.newArrayList();
Yi Tseng4ec727d2017-08-31 11:21:00 -0700175
Yi Tsenge72fbb52017-08-02 15:03:31 -0700176 @Activate
177 protected void activate() {
Yi Tseng7da339e2017-10-23 19:39:39 -0700178 appId = coreService.registerApplication(DHCP_V4_RELAY_APP);
Yi Tsenge72fbb52017-08-02 15:03:31 -0700179 hostService.addListener(hostListener);
Yi Tseng4b013202017-09-08 17:22:51 -0700180 providerService = providerRegistry.register(this);
Yi Tsenge72fbb52017-08-02 15:03:31 -0700181 }
182
183 @Deactivate
184 protected void deactivate() {
Yi Tseng4b013202017-09-08 17:22:51 -0700185 providerRegistry.unregister(this);
186 hostService.removeListener(hostListener);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700187 defaultServerInfoList.forEach(this::stopMonitoringIps);
188 defaultServerInfoList.clear();
189 indirectServerInfoList.forEach(this::stopMonitoringIps);
190 indirectServerInfoList.clear();
Yi Tsenge72fbb52017-08-02 15:03:31 -0700191 }
192
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700193 private void stopMonitoringIps(DhcpServerInfo serverInfo) {
194 serverInfo.getDhcpGatewayIp4().ifPresent(gatewayIp -> {
195 hostService.stopMonitoringIp(gatewayIp);
196 });
197 serverInfo.getDhcpServerIp4().ifPresent(serverIp -> {
198 hostService.stopMonitoringIp(serverIp);
199 });
Yi Tseng51301292017-07-28 13:02:59 -0700200 }
201
202 @Override
Yi Tsenge72fbb52017-08-02 15:03:31 -0700203 public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700204 setDhcpServerConfigs(configs, defaultServerInfoList);
205 }
206
207 @Override
208 public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) {
209 setDhcpServerConfigs(configs, indirectServerInfoList);
210 }
211
212 @Override
213 public List<DhcpServerInfo> getDefaultDhcpServerInfoList() {
214 return defaultServerInfoList;
215 }
216
217 @Override
218 public List<DhcpServerInfo> getIndirectDhcpServerInfoList() {
219 return indirectServerInfoList;
220 }
221
Yi Tseng7da339e2017-10-23 19:39:39 -0700222 @Override
223 public void updateIgnoreVlanConfig(IgnoreDhcpConfig config) {
224 if (config == null) {
225 ignoredVlans.forEach(((deviceId, vlanId) -> {
226 processIgnoreVlanRule(deviceId, vlanId, REMOVE);
227 }));
228 return;
229 }
230 config.ignoredVlans().forEach((deviceId, vlanId) -> {
231 if (ignoredVlans.get(deviceId).contains(vlanId)) {
232 // don't need to process if it already ignored
233 return;
234 }
235 processIgnoreVlanRule(deviceId, vlanId, ADD);
236 });
237
238 ignoredVlans.forEach((deviceId, vlanId) -> {
239 if (!config.ignoredVlans().get(deviceId).contains(vlanId)) {
240 // not contains in new config, remove it
241 processIgnoreVlanRule(deviceId, vlanId, REMOVE);
242 }
243 });
244 }
245
Saurav Dasba7eb1a2017-12-13 16:19:35 -0800246 @Override
247 public void removeIgnoreVlanState(IgnoreDhcpConfig config) {
248 if (config == null) {
249 ignoredVlans.clear();
250 return;
251 }
252 config.ignoredVlans().forEach((deviceId, vlanId) -> {
253 ignoredVlans.remove(deviceId, vlanId);
254 });
255 }
256
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700257 public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) {
Yi Tsenge72fbb52017-08-02 15:03:31 -0700258 if (configs.size() == 0) {
259 // no config to update
260 return;
261 }
262
263 // TODO: currently we pick up first DHCP server config.
264 // Will use other server configs in the future for HA.
265 DhcpServerConfig serverConfig = configs.iterator().next();
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700266
Yi Tsengaefbb002017-09-08 16:23:32 -0700267 if (!serverConfig.getDhcpServerIp4().isPresent()) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700268 // not a DHCPv4 config
Yi Tsenge72fbb52017-08-02 15:03:31 -0700269 return;
270 }
271
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700272 if (!serverInfoList.isEmpty()) {
273 // remove old server info
274 DhcpServerInfo oldServerInfo = serverInfoList.remove(0);
Yi Tsenge72fbb52017-08-02 15:03:31 -0700275
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700276 // stop monitoring gateway or server
277 oldServerInfo.getDhcpGatewayIp4().ifPresent(gatewayIp -> {
278 hostService.stopMonitoringIp(gatewayIp);
279 });
280 oldServerInfo.getDhcpServerIp4().ifPresent(serverIp -> {
281 hostService.stopMonitoringIp(serverIp);
Yi Tseng7da339e2017-10-23 19:39:39 -0700282 cancelDhcpPacket(serverIp);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700283 });
Yi Tsenge72fbb52017-08-02 15:03:31 -0700284 }
Yi Tseng4fa05832017-08-17 13:08:31 -0700285
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700286 // Create new server info according to the config
287 DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig,
288 DhcpServerInfo.Version.DHCP_V4);
289 checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(),
Yi Tseng4ec727d2017-08-31 11:21:00 -0700290 "Connect point not exists");
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700291 checkState(newServerInfo.getDhcpServerIp4().isPresent(),
Yi Tseng4ec727d2017-08-31 11:21:00 -0700292 "IP of DHCP server not exists");
Yi Tseng4ec727d2017-08-31 11:21:00 -0700293
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700294 log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null));
295 log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp4().orElse(null));
296
Yi Tseng7da339e2017-10-23 19:39:39 -0700297 Ip4Address serverIp = newServerInfo.getDhcpServerIp4().get();
298 Ip4Address ipToProbe;
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700299 if (newServerInfo.getDhcpGatewayIp4().isPresent()) {
300 ipToProbe = newServerInfo.getDhcpGatewayIp4().get();
301 } else {
302 ipToProbe = newServerInfo.getDhcpServerIp4().orElse(null);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700303 }
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700304 String hostToProbe = newServerInfo.getDhcpGatewayIp4()
305 .map(ip -> "gateway").orElse("server");
Yi Tseng4ec727d2017-08-31 11:21:00 -0700306
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700307 log.debug("Probing to resolve {} IP {}", hostToProbe, ipToProbe);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700308 hostService.startMonitoringIp(ipToProbe);
309
310 Set<Host> hosts = hostService.getHostsByIp(ipToProbe);
311 if (!hosts.isEmpty()) {
312 Host host = hosts.iterator().next();
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700313 newServerInfo.setDhcpConnectVlan(host.vlan());
314 newServerInfo.setDhcpConnectMac(host.mac());
Yi Tseng4ec727d2017-08-31 11:21:00 -0700315 }
316
Yi Tseng053682b2017-11-09 13:54:12 -0800317 // Add new server info
318 synchronized (this) {
319 serverInfoList.clear();
320 serverInfoList.add(0, newServerInfo);
321 }
322
Yi Tseng7da339e2017-10-23 19:39:39 -0700323 requestDhcpPacket(serverIp);
Yi Tsenge72fbb52017-08-02 15:03:31 -0700324 }
325
Yi Tseng4fa05832017-08-17 13:08:31 -0700326 @Override
Yi Tseng51301292017-07-28 13:02:59 -0700327 public void processDhcpPacket(PacketContext context, BasePacket payload) {
328 checkNotNull(payload, "DHCP payload can't be null");
329 checkState(payload instanceof DHCP, "Payload is not a DHCP");
330 DHCP dhcpPayload = (DHCP) payload;
331 if (!configured()) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700332 log.warn("Missing default DHCP relay server config. Abort packet processing");
Yi Tseng51301292017-07-28 13:02:59 -0700333 return;
334 }
335
336 ConnectPoint inPort = context.inPacket().receivedFrom();
Yi Tseng51301292017-07-28 13:02:59 -0700337 checkNotNull(dhcpPayload, "Can't find DHCP payload");
338 Ethernet packet = context.inPacket().parsed();
339 DHCP.MsgType incomingPacketType = dhcpPayload.getOptions().stream()
340 .filter(dhcpOption -> dhcpOption.getCode() == OptionCode_MessageType.getValue())
341 .map(DhcpOption::getData)
342 .map(data -> DHCP.MsgType.getType(data[0]))
343 .findFirst()
344 .orElse(null);
345 checkNotNull(incomingPacketType, "Can't get message type from DHCP payload {}", dhcpPayload);
346 switch (incomingPacketType) {
347 case DHCPDISCOVER:
Yi Tsengdcef2c22017-08-05 20:34:06 -0700348 // Add the gateway IP as virtual interface IP for server to understand
Yi Tseng51301292017-07-28 13:02:59 -0700349 // the lease to be assigned and forward the packet to dhcp server.
350 Ethernet ethernetPacketDiscover =
Yi Tsengdcef2c22017-08-05 20:34:06 -0700351 processDhcpPacketFromClient(context, packet);
Yi Tseng51301292017-07-28 13:02:59 -0700352 if (ethernetPacketDiscover != null) {
353 writeRequestDhcpRecord(inPort, packet, dhcpPayload);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700354 handleDhcpDiscoverAndRequest(ethernetPacketDiscover, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -0700355 }
356 break;
357 case DHCPOFFER:
358 //reply to dhcp client.
359 Ethernet ethernetPacketOffer = processDhcpPacketFromServer(packet);
360 if (ethernetPacketOffer != null) {
361 writeResponseDhcpRecord(ethernetPacketOffer, dhcpPayload);
Yi Tsengdcef2c22017-08-05 20:34:06 -0700362 sendResponseToClient(ethernetPacketOffer, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -0700363 }
364 break;
365 case DHCPREQUEST:
366 // add the gateway ip as virtual interface ip for server to understand
367 // the lease to be assigned and forward the packet to dhcp server.
368 Ethernet ethernetPacketRequest =
Yi Tsengdcef2c22017-08-05 20:34:06 -0700369 processDhcpPacketFromClient(context, packet);
Yi Tseng51301292017-07-28 13:02:59 -0700370 if (ethernetPacketRequest != null) {
371 writeRequestDhcpRecord(inPort, packet, dhcpPayload);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700372 handleDhcpDiscoverAndRequest(ethernetPacketRequest, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -0700373 }
374 break;
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400375 case DHCPDECLINE:
376 break;
Yi Tseng51301292017-07-28 13:02:59 -0700377 case DHCPACK:
378 // reply to dhcp client.
379 Ethernet ethernetPacketAck = processDhcpPacketFromServer(packet);
380 if (ethernetPacketAck != null) {
381 writeResponseDhcpRecord(ethernetPacketAck, dhcpPayload);
382 handleDhcpAck(ethernetPacketAck, dhcpPayload);
Yi Tsengdcef2c22017-08-05 20:34:06 -0700383 sendResponseToClient(ethernetPacketAck, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -0700384 }
385 break;
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400386 case DHCPNAK:
387 break;
Yi Tseng51301292017-07-28 13:02:59 -0700388 case DHCPRELEASE:
389 // TODO: release the ip address from client
390 break;
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400391 case DHCPINFORM:
392 break;
393 case DHCPFORCERENEW:
394 break;
395 case DHCPLEASEQUERY:
396 handleLeaseQueryMsg(context, packet, dhcpPayload);
397 break;
398 case DHCPLEASEACTIVE:
399 handleLeaseQueryActivateMsg(packet, dhcpPayload);
400 break;
401 case DHCPLEASEUNASSIGNED:
402 case DHCPLEASEUNKNOWN:
403 handleLeaseQueryUnknown(packet, dhcpPayload);
404 break;
Yi Tseng51301292017-07-28 13:02:59 -0700405 default:
406 break;
407 }
408 }
409
410 /**
411 * Checks if this app has been configured.
412 *
413 * @return true if all information we need have been initialized
414 */
Yi Tseng4ec727d2017-08-31 11:21:00 -0700415 private boolean configured() {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700416 return !defaultServerInfoList.isEmpty();
Yi Tseng51301292017-07-28 13:02:59 -0700417 }
418
419 /**
Yi Tsengdcef2c22017-08-05 20:34:06 -0700420 * Returns the first interface ip from interface.
Yi Tseng51301292017-07-28 13:02:59 -0700421 *
Yi Tsengdcef2c22017-08-05 20:34:06 -0700422 * @param iface interface of one connect point
Yi Tseng51301292017-07-28 13:02:59 -0700423 * @return the first interface IP; null if not exists an IP address in
424 * these interfaces
425 */
Yi Tseng4fa05832017-08-17 13:08:31 -0700426 private Ip4Address getFirstIpFromInterface(Interface iface) {
Yi Tsengdcef2c22017-08-05 20:34:06 -0700427 checkNotNull(iface, "Interface can't be null");
428 return iface.ipAddressesList().stream()
Yi Tseng51301292017-07-28 13:02:59 -0700429 .map(InterfaceIpAddress::ipAddress)
430 .filter(IpAddress::isIp4)
431 .map(IpAddress::getIp4Address)
432 .findFirst()
433 .orElse(null);
434 }
435
436 /**
Yi Tseng4ec727d2017-08-31 11:21:00 -0700437 * Gets Interface facing to the server for default host.
Yi Tsengdcef2c22017-08-05 20:34:06 -0700438 *
439 * @return the Interface facing to the server; null if not found
440 */
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700441 private Interface getDefaultServerInterface() {
442 return getServerInterface(defaultServerInfoList);
Yi Tsengdcef2c22017-08-05 20:34:06 -0700443 }
444
445 /**
Yi Tseng4ec727d2017-08-31 11:21:00 -0700446 * Gets Interface facing to the server for indirect hosts.
447 * Use default server Interface if indirect server not configured.
448 *
449 * @return the Interface facing to the server; null if not found
450 */
451 private Interface getIndirectServerInterface() {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700452 return getServerInterface(indirectServerInfoList);
453 }
454
455 private Interface getServerInterface(List<DhcpServerInfo> serverInfos) {
Yi Tsengdc510082017-11-29 14:39:18 -0800456 return serverInfos.stream()
Yi Tseng4ec727d2017-08-31 11:21:00 -0700457 .findFirst()
Yi Tsengdc510082017-11-29 14:39:18 -0800458 .map(serverInfo -> {
459 ConnectPoint dhcpServerConnectPoint =
460 serverInfo.getDhcpServerConnectPoint().orElse(null);
461 VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
462 if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) {
463 return null;
464 }
465 return interfaceService.getInterfacesByPort(dhcpServerConnectPoint)
466 .stream()
467 .filter(iface -> interfaceContainsVlan(iface, dhcpConnectVlan))
468 .findFirst()
469 .orElse(null);
470 })
Yi Tseng4ec727d2017-08-31 11:21:00 -0700471 .orElse(null);
472 }
473
474 /**
475 * Determind if an Interface contains a vlan id.
476 *
477 * @param iface the Interface
478 * @param vlanId the vlan id
479 * @return true if the Interface contains the vlan id
480 */
481 private boolean interfaceContainsVlan(Interface iface, VlanId vlanId) {
Yi Tseng58e74312017-09-30 11:35:42 +0800482 if (vlanId.equals(VlanId.NONE)) {
483 // untagged packet, check if vlan untagged or vlan native is not NONE
484 return !iface.vlanUntagged().equals(VlanId.NONE) ||
485 !iface.vlanNative().equals(VlanId.NONE);
486 }
487 // tagged packet, check if the interface contains the vlan
488 return iface.vlanTagged().contains(vlanId);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700489 }
490
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400491 private void handleLeaseQueryActivateMsg(Ethernet packet, DHCP dhcpPayload) {
492 log.debug("LQ: Got DHCPLEASEACTIVE packet!");
493
494 // TODO: release the ip address from client
495 MacAddress clientMacAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
496 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
497 HostId hostId = HostId.hostId(clientMacAddress, vlanId);
498 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
499
500 if (record == null) {
501 log.warn("Can't find record for host {} when processing DHCPLEASEACTIVE", hostId);
502 return;
503 }
504
505 // need to update routes
506 log.debug("Lease Query for Client results in DHCPLEASEACTIVE - route needs to be modified");
507 // get current route
508 // find the ip of that client with the DhcpRelay store
509
510 Ip4Address clientIP = record.ip4Address().orElse(null);
511 log.debug("LQ: IP of host is " + clientIP.getIp4Address());
512
513 MacAddress nextHopMac = record.nextHop().orElse(null);
514 log.debug("LQ: MAC of resulting *OLD* NH for that host is " + nextHopMac.toString());
515
516 // find the new NH by looking at the src MAC of the dhcp request
517 // from the LQ store
518 MacAddress newNextHopMac = record.nextHopTemp().orElse(null);
519 log.debug("LQ: MAC of resulting *NEW* NH for that host is " + newNextHopMac.toString());
520
521 log.debug("LQ: updating dhcp relay record with new NH");
522 record.nextHop(newNextHopMac);
523
524 // find the next hop IP from its mac
525 HostId gwHostId = HostId.hostId(newNextHopMac, vlanId);
526 Host gwHost = hostService.getHost(gwHostId);
527
528 if (gwHost == null) {
529 log.warn("Can't find gateway for new NH host " + gwHostId);
530 return;
531 }
532
533 Ip4Address nextHopIp = gwHost.ipAddresses()
534 .stream()
535 .filter(IpAddress::isIp4)
536 .map(IpAddress::getIp4Address)
537 .findFirst()
538 .orElse(null);
539
540 if (nextHopIp == null) {
541 log.warn("Can't find IP address of gateway " + gwHost);
542 return;
543 }
544
545 log.debug("LQ: *NEW* NH IP for host is " + nextHopIp.getIp4Address());
546 Route route = new Route(Route.Source.STATIC, clientIP.toIpPrefix(), nextHopIp);
547 routeStore.updateRoute(route);
548
549 // and forward to client
550 Ethernet ethernetPacket = processLeaseQueryFromServer(packet);
551 if (ethernetPacket != null) {
552 sendResponseToClient(ethernetPacket, dhcpPayload);
553 }
554 }
555
556 /**
557 *
558 */
559 private void handleLeaseQueryMsg(PacketContext context, Ethernet packet, DHCP dhcpPayload) {
560 log.debug("LQ: Got DHCPLEASEQUERY packet!");
561 MacAddress clientMacAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
562 log.debug("LQ: got DHCPLEASEQUERY with MAC " + clientMacAddress.toString());
563 // add the client mac (hostid) of this request to a store (the entry will be removed with
564 // the reply sent to the originator)
565 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
566 HostId hId = HostId.hostId(clientMacAddress, vlanId);
567 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hId).orElse(null);
568 if (record != null) {
569 //new NH is to be taken from src mac of LQ packet
570 MacAddress newNextHop = packet.getSourceMAC();
571 record.nextHopTemp(newNextHop);
572 record.ip4Status(dhcpPayload.getPacketType());
573 record.updateLastSeen();
574
575 // do a basic routing of the packet (this is unicast routing
576 // not a relay operation like for other broadcast dhcp packets
577 Ethernet ethernetPacketLQ = processLeaseQueryFromAgent(context, packet);
578 // and forward to server
579 handleDhcpDiscoverAndRequest(ethernetPacketLQ, dhcpPayload);
580 } else {
581 log.warn("LQ: Error! - DHCP relay record for that client not found - ignoring LQ!");
582 }
583 }
584
585 private void handleLeaseQueryUnknown(Ethernet packet, DHCP dhcpPayload) {
586 log.debug("Lease Query for Client results in DHCPLEASEUNASSIGNED or " +
587 "DHCPLEASEUNKNOWN - removing route & forwarding reply to originator");
588 MacAddress clientMacAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
589 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
590 HostId hostId = HostId.hostId(clientMacAddress, vlanId);
591 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
592
593 if (record == null) {
594 log.warn("Can't find record for host {} when handling LQ UNKNOWN/UNASSIGNED message", hostId);
595 return;
596 }
597
598 Ip4Address clientIP = record.ip4Address().orElse(null);
599 log.debug("LQ: IP of host is " + clientIP.getIp4Address());
600
601 // find the new NH by looking at the src MAC of the dhcp request
602 // from the LQ store
603 MacAddress nextHopMac = record.nextHop().orElse(null);
604 log.debug("LQ: MAC of resulting *Existing* NH for that route is " + nextHopMac.toString());
605
606 // find the next hop IP from its mac
607 HostId gwHostId = HostId.hostId(nextHopMac, vlanId);
608 Host gwHost = hostService.getHost(gwHostId);
609
610 if (gwHost == null) {
611 log.warn("Can't find gateway for new NH host " + gwHostId);
612 return;
613 }
614
615 Ip4Address nextHopIp = gwHost.ipAddresses()
616 .stream()
617 .filter(IpAddress::isIp4)
618 .map(IpAddress::getIp4Address)
619 .findFirst()
620 .orElse(null);
621
622 if (nextHopIp == null) {
623 log.warn("Can't find IP address of gateway {}", gwHost);
624 return;
625 }
626
627 log.debug("LQ: *Existing* NH IP for host is " + nextHopIp.getIp4Address() + " removing route for it");
628 Route route = new Route(Route.Source.STATIC, clientIP.toIpPrefix(), nextHopIp);
629 routeStore.removeRoute(route);
630
631 // remove from temp store
632 dhcpRelayStore.removeDhcpRecord(hostId);
633
634 // and forward to client
635 Ethernet ethernetPacket = processLeaseQueryFromServer(packet);
636 if (ethernetPacket != null) {
637 sendResponseToClient(ethernetPacket, dhcpPayload);
638 }
639 }
640
Yi Tseng4ec727d2017-08-31 11:21:00 -0700641 /**
Yi Tseng51301292017-07-28 13:02:59 -0700642 * Build the DHCP discover/request packet with gateway IP(unicast packet).
643 *
644 * @param context the packet context
645 * @param ethernetPacket the ethernet payload to process
Yi Tseng51301292017-07-28 13:02:59 -0700646 * @return processed packet
647 */
648 private Ethernet processDhcpPacketFromClient(PacketContext context,
Yi Tsengdcef2c22017-08-05 20:34:06 -0700649 Ethernet ethernetPacket) {
Yi Tsengc44dc2e2017-11-03 16:27:32 -0700650 ConnectPoint receivedFrom = context.inPacket().receivedFrom();
651 DeviceId receivedFromDevice = receivedFrom.deviceId();
652
Yi Tseng4ec727d2017-08-31 11:21:00 -0700653 // get dhcp header.
654 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
655 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
656 UDP udpPacket = (UDP) ipv4Packet.getPayload();
657 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
658
Yi Tsengc44dc2e2017-11-03 16:27:32 -0700659 // TODO: refactor
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700660 VlanId dhcpConnectVlan = null;
661 MacAddress dhcpConnectMac = null;
662 Ip4Address dhcpServerIp = null;
663 Ip4Address relayAgentIp = null;
664
665 VlanId indirectDhcpConnectVlan = null;
666 MacAddress indirectDhcpConnectMac = null;
667 Ip4Address indirectDhcpServerIp = null;
668 Ip4Address indirectRelayAgentIp = null;
669
670 if (!defaultServerInfoList.isEmpty()) {
671 DhcpServerInfo serverInfo = defaultServerInfoList.get(0);
672 dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
673 dhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null);
674 dhcpServerIp = serverInfo.getDhcpServerIp4().orElse(null);
Yi Tsengc44dc2e2017-11-03 16:27:32 -0700675 relayAgentIp = serverInfo.getRelayAgentIp4(receivedFromDevice).orElse(null);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700676 }
677
678 if (!indirectServerInfoList.isEmpty()) {
679 DhcpServerInfo indirectServerInfo = indirectServerInfoList.get(0);
680 indirectDhcpConnectVlan = indirectServerInfo.getDhcpConnectVlan().orElse(null);
681 indirectDhcpConnectMac = indirectServerInfo.getDhcpConnectMac().orElse(null);
682 indirectDhcpServerIp = indirectServerInfo.getDhcpServerIp4().orElse(null);
Yi Tsengc44dc2e2017-11-03 16:27:32 -0700683 indirectRelayAgentIp = indirectServerInfo.getRelayAgentIp4(receivedFromDevice).orElse(null);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700684 }
685
Yi Tsengdcef2c22017-08-05 20:34:06 -0700686 Ip4Address clientInterfaceIp =
687 interfaceService.getInterfacesByPort(context.inPacket().receivedFrom())
688 .stream()
689 .map(Interface::ipAddressesList)
690 .flatMap(Collection::stream)
691 .map(InterfaceIpAddress::ipAddress)
692 .filter(IpAddress::isIp4)
693 .map(IpAddress::getIp4Address)
694 .findFirst()
695 .orElse(null);
696 if (clientInterfaceIp == null) {
697 log.warn("Can't find interface IP for client interface for port {}",
698 context.inPacket().receivedFrom());
699 return null;
700 }
Yi Tseng4ec727d2017-08-31 11:21:00 -0700701 boolean isDirectlyConnected = directlyConnected(dhcpPacket);
Yi Tsengdc510082017-11-29 14:39:18 -0800702 Interface serverInterface;
703 if (isDirectlyConnected) {
704 serverInterface = getDefaultServerInterface();
705 } else {
706 serverInterface = getIndirectServerInterface();
707 if (serverInterface == null) {
708 // Indirect server interface not found, use default server interface
709 serverInterface = getDefaultServerInterface();
710 }
711 }
Yi Tsengdcef2c22017-08-05 20:34:06 -0700712 if (serverInterface == null) {
Yi Tseng4ec727d2017-08-31 11:21:00 -0700713 log.warn("Can't get {} server interface, ignore", isDirectlyConnected ? "direct" : "indirect");
Yi Tsengdcef2c22017-08-05 20:34:06 -0700714 return null;
715 }
Yi Tseng4fa05832017-08-17 13:08:31 -0700716 Ip4Address ipFacingServer = getFirstIpFromInterface(serverInterface);
717 MacAddress macFacingServer = serverInterface.mac();
718 if (ipFacingServer == null || macFacingServer == null) {
Yi Tsengdcef2c22017-08-05 20:34:06 -0700719 log.warn("No IP address for server Interface {}", serverInterface);
Yi Tseng51301292017-07-28 13:02:59 -0700720 return null;
721 }
722 if (dhcpConnectMac == null) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -0700723 log.warn("DHCP Server/Gateway IP not yet resolved .. Aborting DHCP "
Yi Tseng51301292017-07-28 13:02:59 -0700724 + "packet processing from client on port: {}",
Yi Tsengdcef2c22017-08-05 20:34:06 -0700725 context.inPacket().receivedFrom());
Yi Tseng51301292017-07-28 13:02:59 -0700726 return null;
727 }
Yi Tseng4ec727d2017-08-31 11:21:00 -0700728
Yi Tseng4fa05832017-08-17 13:08:31 -0700729 etherReply.setSourceMACAddress(macFacingServer);
Yi Tseng4fa05832017-08-17 13:08:31 -0700730 ipv4Packet.setSourceAddress(ipFacingServer.toInt());
Yi Tseng51301292017-07-28 13:02:59 -0700731
Yi Tseng4ec727d2017-08-31 11:21:00 -0700732 if (isDirectlyConnected) {
Charles Chanb5d24822017-10-10 16:53:32 -0400733 etherReply.setDestinationMACAddress(dhcpConnectMac);
734 etherReply.setVlanID(dhcpConnectVlan.toShort());
735 ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
736
Yi Tseng51301292017-07-28 13:02:59 -0700737 ConnectPoint inPort = context.inPacket().receivedFrom();
738 VlanId vlanId = VlanId.vlanId(ethernetPacket.getVlanID());
739 // add connected in port and vlan
740 CircuitId cid = new CircuitId(inPort.toString(), vlanId);
741 byte[] circuitId = cid.serialize();
742 DhcpOption circuitIdSubOpt = new DhcpOption();
743 circuitIdSubOpt
744 .setCode(CIRCUIT_ID.getValue())
745 .setLength((byte) circuitId.length)
746 .setData(circuitId);
747
748 DhcpRelayAgentOption newRelayAgentOpt = new DhcpRelayAgentOption();
749 newRelayAgentOpt.setCode(OptionCode_CircuitID.getValue());
750 newRelayAgentOpt.addSubOption(circuitIdSubOpt);
751
Charles Chanb5d24822017-10-10 16:53:32 -0400752 // Removes END option first
Yi Tseng51301292017-07-28 13:02:59 -0700753 List<DhcpOption> options = dhcpPacket.getOptions().stream()
754 .filter(opt -> opt.getCode() != OptionCode_END.getValue())
755 .collect(Collectors.toList());
756
757 // push relay agent option
758 options.add(newRelayAgentOpt);
759
760 // make sure option 255(End) is the last option
761 DhcpOption endOption = new DhcpOption();
762 endOption.setCode(OptionCode_END.getValue());
763 options.add(endOption);
764
765 dhcpPacket.setOptions(options);
Yi Tsengdcef2c22017-08-05 20:34:06 -0700766
Charles Chanb5d24822017-10-10 16:53:32 -0400767 // Sets relay agent IP
768 int effectiveRelayAgentIp = relayAgentIp != null ?
769 relayAgentIp.toInt() : clientInterfaceIp.toInt();
770 dhcpPacket.setGatewayIPAddress(effectiveRelayAgentIp);
771 } else {
772 if (indirectDhcpServerIp != null) {
773 // Use indirect server config for indirect packets if configured
774 etherReply.setDestinationMACAddress(indirectDhcpConnectMac);
775 etherReply.setVlanID(indirectDhcpConnectVlan.toShort());
776 ipv4Packet.setDestinationAddress(indirectDhcpServerIp.toInt());
Yi Tseng51301292017-07-28 13:02:59 -0700777
Charles Chanb5d24822017-10-10 16:53:32 -0400778 // Set giaddr if indirect relay agent IP is configured
779 if (indirectRelayAgentIp != null) {
780 dhcpPacket.setGatewayIPAddress(indirectRelayAgentIp.toInt());
781 }
782 } else {
783 // Otherwise, use default server config for indirect packets
784 etherReply.setDestinationMACAddress(dhcpConnectMac);
785 etherReply.setVlanID(dhcpConnectVlan.toShort());
786 ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
Yi Tseng4ec727d2017-08-31 11:21:00 -0700787
Charles Chanb5d24822017-10-10 16:53:32 -0400788 // Set giaddr if direct relay agent IP is configured
789 if (relayAgentIp != null) {
790 dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt());
791 }
Yi Tseng4ec727d2017-08-31 11:21:00 -0700792 }
Yi Tseng4fa05832017-08-17 13:08:31 -0700793 }
794
Yi Tsengf41681e2017-10-03 09:58:19 -0700795 // Remove broadcast flag
796 dhcpPacket.setFlags((short) 0);
797
Yi Tseng51301292017-07-28 13:02:59 -0700798 udpPacket.setPayload(dhcpPacket);
Yi Tseng4ec727d2017-08-31 11:21:00 -0700799 // As a DHCP relay, the source port should be server port( instead
800 // of client port.
Yi Tsengdcef2c22017-08-05 20:34:06 -0700801 udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
Yi Tseng51301292017-07-28 13:02:59 -0700802 udpPacket.setDestinationPort(UDP.DHCP_SERVER_PORT);
803 ipv4Packet.setPayload(udpPacket);
Charles Chan7edf7642017-10-09 11:07:25 -0400804 ipv4Packet.setTtl((byte) 64);
Yi Tseng51301292017-07-28 13:02:59 -0700805 etherReply.setPayload(ipv4Packet);
806 return etherReply;
807 }
808
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400809
810 /**
811 * Do a basic routing for a packet from client (used for LQ processing).
812 *
813 * @param context the packet context
814 * @param ethernetPacket the ethernet payload to process
815 * @return processed packet
816 */
817 private Ethernet processLeaseQueryFromAgent(PacketContext context,
818 Ethernet ethernetPacket) {
819 // get dhcp header.
820 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
821 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
822 UDP udpPacket = (UDP) ipv4Packet.getPayload();
823 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
824
825 VlanId dhcpConnectVlan = null;
826 MacAddress dhcpConnectMac = null;
827 Ip4Address dhcpServerIp = null;
828
829 VlanId indirectDhcpConnectVlan = null;
830 MacAddress indirectDhcpConnectMac = null;
831 Ip4Address indirectDhcpServerIp = null;
832
833 if (!defaultServerInfoList.isEmpty()) {
834 DhcpServerInfo serverInfo = defaultServerInfoList.get(0);
835 dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
836 dhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null);
837 dhcpServerIp = serverInfo.getDhcpServerIp4().orElse(null);
838 }
839
840 if (!indirectServerInfoList.isEmpty()) {
841 DhcpServerInfo indirectServerInfo = indirectServerInfoList.get(0);
842 indirectDhcpConnectVlan = indirectServerInfo.getDhcpConnectVlan().orElse(null);
843 indirectDhcpConnectMac = indirectServerInfo.getDhcpConnectMac().orElse(null);
844 indirectDhcpServerIp = indirectServerInfo.getDhcpServerIp4().orElse(null);
845 }
846
847 Ip4Address clientInterfaceIp =
848 interfaceService.getInterfacesByPort(context.inPacket().receivedFrom())
849 .stream()
850 .map(Interface::ipAddressesList)
851 .flatMap(Collection::stream)
852 .map(InterfaceIpAddress::ipAddress)
853 .filter(IpAddress::isIp4)
854 .map(IpAddress::getIp4Address)
855 .findFirst()
856 .orElse(null);
857 if (clientInterfaceIp == null) {
858 log.warn("Can't find interface IP for client interface for port {}",
859 context.inPacket().receivedFrom());
860 return null;
861 }
862 boolean isDirectlyConnected = directlyConnected(dhcpPacket);
Yi Tsengdc510082017-11-29 14:39:18 -0800863 Interface serverInterface;
864 if (isDirectlyConnected) {
865 serverInterface = getDefaultServerInterface();
866 } else {
867 serverInterface = getIndirectServerInterface();
868 if (serverInterface == null) {
869 // Indirect server interface not found, use default server interface
870 serverInterface = getDefaultServerInterface();
871 }
872 }
Charles Chan2f9aa2c2017-10-08 23:53:36 -0400873 if (serverInterface == null) {
874 log.warn("Can't get {} server interface, ignore", isDirectlyConnected ? "direct" : "indirect");
875 return null;
876 }
877 Ip4Address ipFacingServer = getFirstIpFromInterface(serverInterface);
878 MacAddress macFacingServer = serverInterface.mac();
879 if (ipFacingServer == null || macFacingServer == null) {
880 log.warn("No IP address for server Interface {}", serverInterface);
881 return null;
882 }
883 if (dhcpConnectMac == null) {
884 log.warn("DHCP server/gateway not yet resolved .. Aborting DHCP "
885 + "packet processing from client on port: {}",
886 context.inPacket().receivedFrom());
887 return null;
888 }
889
890 etherReply.setSourceMACAddress(macFacingServer);
891 etherReply.setDestinationMACAddress(dhcpConnectMac);
892 etherReply.setVlanID(dhcpConnectVlan.toShort());
893 ipv4Packet.setSourceAddress(ipFacingServer.toInt());
894 ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
895
896 if (indirectDhcpServerIp != null) {
897 // Indirect case, replace destination to indirect dhcp server if exist
898 etherReply.setDestinationMACAddress(indirectDhcpConnectMac);
899 etherReply.setVlanID(indirectDhcpConnectVlan.toShort());
900 ipv4Packet.setDestinationAddress(indirectDhcpServerIp.toInt());
901 }
902
903 udpPacket.setPayload(dhcpPacket);
904 // As a DHCP relay, the source port should be server port( instead
905 // of client port.
906 udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
907 udpPacket.setDestinationPort(UDP.DHCP_SERVER_PORT);
908 ipv4Packet.setPayload(udpPacket);
909 etherReply.setPayload(ipv4Packet);
910 return etherReply;
911 }
912
913
Yi Tseng51301292017-07-28 13:02:59 -0700914 /**
915 * Writes DHCP record to the store according to the request DHCP packet (Discover, Request).
916 *
917 * @param location the location which DHCP packet comes from
918 * @param ethernet the DHCP packet
919 * @param dhcpPayload the DHCP payload
920 */
921 private void writeRequestDhcpRecord(ConnectPoint location,
922 Ethernet ethernet,
923 DHCP dhcpPayload) {
924 VlanId vlanId = VlanId.vlanId(ethernet.getVlanID());
925 MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
926 HostId hostId = HostId.hostId(macAddress, vlanId);
927 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
928 if (record == null) {
929 record = new DhcpRecord(HostId.hostId(macAddress, vlanId));
930 } else {
931 record = record.clone();
932 }
933 record.addLocation(new HostLocation(location, System.currentTimeMillis()));
934 record.ip4Status(dhcpPayload.getPacketType());
935 record.setDirectlyConnected(directlyConnected(dhcpPayload));
936 if (!directlyConnected(dhcpPayload)) {
937 // Update gateway mac address if the host is not directly connected
938 record.nextHop(ethernet.getSourceMAC());
939 }
940 record.updateLastSeen();
941 dhcpRelayStore.updateDhcpRecord(HostId.hostId(macAddress, vlanId), record);
942 }
943
944 /**
945 * Writes DHCP record to the store according to the response DHCP packet (Offer, Ack).
946 *
947 * @param ethernet the DHCP packet
948 * @param dhcpPayload the DHCP payload
949 */
950 private void writeResponseDhcpRecord(Ethernet ethernet,
951 DHCP dhcpPayload) {
Yi Tsengdcef2c22017-08-05 20:34:06 -0700952 Optional<Interface> outInterface = getClientInterface(ethernet, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -0700953 if (!outInterface.isPresent()) {
954 log.warn("Failed to determine where to send {}", dhcpPayload.getPacketType());
955 return;
956 }
957
958 Interface outIface = outInterface.get();
959 ConnectPoint location = outIface.connectPoint();
Yi Tseng4ec727d2017-08-31 11:21:00 -0700960 VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
Yi Tsengdcef2c22017-08-05 20:34:06 -0700961 if (vlanId == null) {
962 vlanId = outIface.vlan();
963 }
Yi Tseng51301292017-07-28 13:02:59 -0700964 MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
965 HostId hostId = HostId.hostId(macAddress, vlanId);
966 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
967 if (record == null) {
968 record = new DhcpRecord(HostId.hostId(macAddress, vlanId));
969 } else {
970 record = record.clone();
971 }
972 record.addLocation(new HostLocation(location, System.currentTimeMillis()));
973 if (dhcpPayload.getPacketType() == DHCP.MsgType.DHCPACK) {
974 record.ip4Address(Ip4Address.valueOf(dhcpPayload.getYourIPAddress()));
975 }
976 record.ip4Status(dhcpPayload.getPacketType());
977 record.setDirectlyConnected(directlyConnected(dhcpPayload));
978 record.updateLastSeen();
979 dhcpRelayStore.updateDhcpRecord(HostId.hostId(macAddress, vlanId), record);
980 }
981
982 /**
983 * Build the DHCP offer/ack with proper client port.
984 *
985 * @param ethernetPacket the original packet comes from server
986 * @return new packet which will send to the client
987 */
988 private Ethernet processDhcpPacketFromServer(Ethernet ethernetPacket) {
989 // get dhcp header.
990 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
991 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
992 UDP udpPacket = (UDP) ipv4Packet.getPayload();
993 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
994
995 // determine the vlanId of the client host - note that this vlan id
996 // could be different from the vlan in the packet from the server
Yi Tsengdcef2c22017-08-05 20:34:06 -0700997 Interface clientInterface = getClientInterface(ethernetPacket, dhcpPayload).orElse(null);
Yi Tseng51301292017-07-28 13:02:59 -0700998
Yi Tsengdcef2c22017-08-05 20:34:06 -0700999 if (clientInterface == null) {
Yi Tseng51301292017-07-28 13:02:59 -07001000 log.warn("Cannot find the interface for the DHCP {}", dhcpPayload);
1001 return null;
1002 }
Yi Tsengdcef2c22017-08-05 20:34:06 -07001003 VlanId vlanId;
1004 if (clientInterface.vlanTagged().isEmpty()) {
1005 vlanId = clientInterface.vlan();
1006 } else {
1007 // might be multiple vlan in same interface
Yi Tseng4ec727d2017-08-31 11:21:00 -07001008 vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
Yi Tsengdcef2c22017-08-05 20:34:06 -07001009 }
1010 if (vlanId == null) {
1011 vlanId = VlanId.NONE;
1012 }
1013 etherReply.setVlanID(vlanId.toShort());
1014 etherReply.setSourceMACAddress(clientInterface.mac());
Yi Tseng51301292017-07-28 13:02:59 -07001015
Yi Tsengdcef2c22017-08-05 20:34:06 -07001016 if (!directlyConnected(dhcpPayload)) {
1017 // if client is indirectly connected, try use next hop mac address
1018 MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
1019 HostId hostId = HostId.hostId(macAddress, vlanId);
1020 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
1021 if (record != null) {
1022 // if next hop can be found, use mac address of next hop
1023 record.nextHop().ifPresent(etherReply::setDestinationMACAddress);
1024 } else {
1025 // otherwise, discard the packet
1026 log.warn("Can't find record for host id {}, discard packet", hostId);
1027 return null;
1028 }
Yi Tsengc03fa242017-08-17 17:43:38 -07001029 } else {
1030 etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
Yi Tsengdcef2c22017-08-05 20:34:06 -07001031 }
1032
Yi Tseng51301292017-07-28 13:02:59 -07001033 // we leave the srcMac from the original packet
Yi Tseng51301292017-07-28 13:02:59 -07001034 // figure out the relay agent IP corresponding to the original request
Yi Tseng4fa05832017-08-17 13:08:31 -07001035 Ip4Address ipFacingClient = getFirstIpFromInterface(clientInterface);
1036 if (ipFacingClient == null) {
Yi Tseng51301292017-07-28 13:02:59 -07001037 log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. "
1038 + "Aborting relay for dhcp packet from server {}",
Yi Tsengdcef2c22017-08-05 20:34:06 -07001039 etherReply.getDestinationMAC(), clientInterface.vlan(),
Yi Tseng51301292017-07-28 13:02:59 -07001040 ethernetPacket);
1041 return null;
1042 }
1043 // SRC_IP: relay agent IP
1044 // DST_IP: offered IP
Yi Tseng4fa05832017-08-17 13:08:31 -07001045 ipv4Packet.setSourceAddress(ipFacingClient.toInt());
Yi Tseng51301292017-07-28 13:02:59 -07001046 ipv4Packet.setDestinationAddress(dhcpPayload.getYourIPAddress());
1047 udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
1048 if (directlyConnected(dhcpPayload)) {
1049 udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
1050 } else {
1051 // forward to another dhcp relay
Yi Tseng72b599a2017-09-14 13:24:21 -07001052 // FIXME: Currently we assume the DHCP comes from a L2 relay with
1053 // Option 82, this might not work if DHCP message comes from
1054 // L3 relay.
1055 udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
Yi Tseng51301292017-07-28 13:02:59 -07001056 }
1057
1058 udpPacket.setPayload(dhcpPayload);
1059 ipv4Packet.setPayload(udpPacket);
1060 etherReply.setPayload(ipv4Packet);
1061 return etherReply;
1062 }
1063
Yi Tsengdcef2c22017-08-05 20:34:06 -07001064 /**
Charles Chan2f9aa2c2017-10-08 23:53:36 -04001065 * Build the DHCP offer/ack with proper client port.
1066 *
1067 * @param ethernetPacket the original packet comes from server
1068 * @return new packet which will send to the client
1069 */
1070 private Ethernet processLeaseQueryFromServer(Ethernet ethernetPacket) {
1071 // get dhcp header.
1072 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
1073 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
1074 UDP udpPacket = (UDP) ipv4Packet.getPayload();
1075 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
1076
1077 // determine the vlanId of the client host - note that this vlan id
1078 // could be different from the vlan in the packet from the server
1079 Interface clientInterface = getClientInterface(ethernetPacket, dhcpPayload).orElse(null);
1080
1081 if (clientInterface == null) {
1082 log.warn("Cannot find the interface for the DHCP {}", dhcpPayload);
1083 return null;
1084 }
1085 VlanId vlanId;
1086 if (clientInterface.vlanTagged().isEmpty()) {
1087 vlanId = clientInterface.vlan();
1088 } else {
1089 // might be multiple vlan in same interface
1090 vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
1091 }
1092 if (vlanId == null) {
1093 vlanId = VlanId.NONE;
1094 }
1095 etherReply.setVlanID(vlanId.toShort());
1096 etherReply.setSourceMACAddress(clientInterface.mac());
1097
1098 if (!directlyConnected(dhcpPayload)) {
1099 // if client is indirectly connected, try use next hop mac address
1100 MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
1101 HostId hostId = HostId.hostId(macAddress, vlanId);
1102 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
1103 if (record != null) {
1104 // if next hop can be found, use mac address of next hop
1105 record.nextHop().ifPresent(etherReply::setDestinationMACAddress);
1106 } else {
1107 // otherwise, discard the packet
1108 log.warn("Can't find record for host id {}, discard packet", hostId);
1109 return null;
1110 }
1111 } else {
1112 etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
1113 }
1114
1115 // default is client port
1116 udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
1117 udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
1118
1119 udpPacket.setPayload(dhcpPayload);
1120 ipv4Packet.setPayload(udpPacket);
1121 etherReply.setPayload(ipv4Packet);
1122 return etherReply;
1123 }
1124 /**
Yi Tsengdcef2c22017-08-05 20:34:06 -07001125 * Extracts VLAN ID from relay agent option.
1126 *
1127 * @param dhcpPayload the DHCP payload
1128 * @return VLAN ID from DHCP payload; null if not exists
1129 */
Yi Tseng4ec727d2017-08-31 11:21:00 -07001130 private VlanId getVlanIdFromRelayAgentOption(DHCP dhcpPayload) {
Yi Tsengdcef2c22017-08-05 20:34:06 -07001131 DhcpRelayAgentOption option = (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID);
1132 if (option == null) {
1133 return null;
1134 }
1135 DhcpOption circuitIdSubOption = option.getSubOption(CIRCUIT_ID.getValue());
1136 if (circuitIdSubOption == null) {
1137 return null;
1138 }
1139 try {
1140 CircuitId circuitId = CircuitId.deserialize(circuitIdSubOption.getData());
1141 return circuitId.vlanId();
1142 } catch (IllegalArgumentException e) {
1143 // can't deserialize the circuit ID
1144 return null;
1145 }
1146 }
1147
1148 /**
1149 * Removes DHCP relay agent information option (option 82) from DHCP payload.
1150 * Also reset giaddr to 0
1151 *
1152 * @param ethPacket the Ethernet packet to be processed
1153 * @return Ethernet packet processed
1154 */
1155 private Ethernet removeRelayAgentOption(Ethernet ethPacket) {
1156 Ethernet ethernet = (Ethernet) ethPacket.clone();
1157 IPv4 ipv4 = (IPv4) ethernet.getPayload();
1158 UDP udp = (UDP) ipv4.getPayload();
1159 DHCP dhcpPayload = (DHCP) udp.getPayload();
1160
1161 // removes relay agent information option
1162 List<DhcpOption> options = dhcpPayload.getOptions();
1163 options = options.stream()
1164 .filter(option -> option.getCode() != OptionCode_CircuitID.getValue())
1165 .collect(Collectors.toList());
1166 dhcpPayload.setOptions(options);
1167 dhcpPayload.setGatewayIPAddress(0);
1168
1169 udp.setPayload(dhcpPayload);
1170 ipv4.setPayload(udp);
1171 ethernet.setPayload(ipv4);
1172 return ethernet;
1173 }
1174
Yi Tseng51301292017-07-28 13:02:59 -07001175
1176 /**
1177 * Check if the host is directly connected to the network or not.
1178 *
1179 * @param dhcpPayload the dhcp payload
1180 * @return true if the host is directly connected to the network; false otherwise
1181 */
1182 private boolean directlyConnected(DHCP dhcpPayload) {
Yi Tseng2cf59912017-08-24 14:47:34 -07001183 DhcpRelayAgentOption relayAgentOption =
1184 (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID);
Yi Tseng51301292017-07-28 13:02:59 -07001185
1186 // Doesn't contains relay option
1187 if (relayAgentOption == null) {
1188 return true;
1189 }
1190
Yi Tseng2cf59912017-08-24 14:47:34 -07001191 // check circuit id, if circuit id is invalid, we say it is an indirect host
1192 DhcpOption circuitIdOpt = relayAgentOption.getSubOption(CIRCUIT_ID.getValue());
Yi Tseng51301292017-07-28 13:02:59 -07001193
Yi Tseng2cf59912017-08-24 14:47:34 -07001194 try {
1195 CircuitId.deserialize(circuitIdOpt.getData());
Yi Tseng51301292017-07-28 13:02:59 -07001196 return true;
Yi Tseng2cf59912017-08-24 14:47:34 -07001197 } catch (Exception e) {
1198 // invalid circuit id
1199 return false;
Yi Tseng51301292017-07-28 13:02:59 -07001200 }
Yi Tseng51301292017-07-28 13:02:59 -07001201 }
1202
1203
1204 /**
1205 * Send the DHCP ack to the requester host.
1206 * Modify Host or Route store according to the type of DHCP.
1207 *
1208 * @param ethernetPacketAck the packet
1209 * @param dhcpPayload the DHCP data
1210 */
1211 private void handleDhcpAck(Ethernet ethernetPacketAck, DHCP dhcpPayload) {
Yi Tsengdcef2c22017-08-05 20:34:06 -07001212 Optional<Interface> outInterface = getClientInterface(ethernetPacketAck, dhcpPayload);
Yi Tseng51301292017-07-28 13:02:59 -07001213 if (!outInterface.isPresent()) {
1214 log.warn("Can't find output interface for dhcp: {}", dhcpPayload);
1215 return;
1216 }
1217
1218 Interface outIface = outInterface.get();
1219 HostLocation hostLocation = new HostLocation(outIface.connectPoint(), System.currentTimeMillis());
1220 MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
Yi Tseng4ec727d2017-08-31 11:21:00 -07001221 VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
Yi Tsengdcef2c22017-08-05 20:34:06 -07001222 if (vlanId == null) {
1223 vlanId = outIface.vlan();
1224 }
Yi Tseng51301292017-07-28 13:02:59 -07001225 HostId hostId = HostId.hostId(macAddress, vlanId);
1226 Ip4Address ip = Ip4Address.valueOf(dhcpPayload.getYourIPAddress());
1227
1228 if (directlyConnected(dhcpPayload)) {
1229 // Add to host store if it connect to network directly
1230 Set<IpAddress> ips = Sets.newHashSet(ip);
Yi Tseng4b013202017-09-08 17:22:51 -07001231 Host host = hostService.getHost(hostId);
Yi Tseng51301292017-07-28 13:02:59 -07001232
Yi Tseng4b013202017-09-08 17:22:51 -07001233 Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation);
1234 if (host != null) {
1235 // Dual homing support:
1236 // if host exists, use old locations and new location
1237 hostLocations.addAll(host.locations());
1238 }
1239 HostDescription desc = new DefaultHostDescription(macAddress, vlanId,
1240 hostLocations, ips, false);
1241 // Add IP address when dhcp server give the host new ip address
1242 providerService.hostDetected(hostId, desc, false);
Yi Tseng51301292017-07-28 13:02:59 -07001243 } else {
1244 // Add to route store if it does not connect to network directly
1245 // Get gateway host IP according to host mac address
Yi Tsengdcef2c22017-08-05 20:34:06 -07001246 // TODO: remove relay store here
Yi Tseng51301292017-07-28 13:02:59 -07001247 DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
1248
1249 if (record == null) {
1250 log.warn("Can't find DHCP record of host {}", hostId);
1251 return;
1252 }
1253
1254 MacAddress gwMac = record.nextHop().orElse(null);
1255 if (gwMac == null) {
1256 log.warn("Can't find gateway mac address from record {}", record);
1257 return;
1258 }
1259
1260 HostId gwHostId = HostId.hostId(gwMac, record.vlanId());
1261 Host gwHost = hostService.getHost(gwHostId);
1262
1263 if (gwHost == null) {
1264 log.warn("Can't find gateway host {}", gwHostId);
1265 return;
1266 }
1267
1268 Ip4Address nextHopIp = gwHost.ipAddresses()
1269 .stream()
1270 .filter(IpAddress::isIp4)
1271 .map(IpAddress::getIp4Address)
1272 .findFirst()
1273 .orElse(null);
1274
1275 if (nextHopIp == null) {
1276 log.warn("Can't find IP address of gateway {}", gwHost);
1277 return;
1278 }
1279
1280 Route route = new Route(Route.Source.STATIC, ip.toIpPrefix(), nextHopIp);
1281 routeStore.updateRoute(route);
1282 }
Yi Tseng51301292017-07-28 13:02:59 -07001283 }
1284
1285 /**
1286 * forward the packet to ConnectPoint where the DHCP server is attached.
1287 *
1288 * @param packet the packet
1289 */
Yi Tseng4ec727d2017-08-31 11:21:00 -07001290 private void handleDhcpDiscoverAndRequest(Ethernet packet, DHCP dhcpPayload) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001291 boolean direct = directlyConnected(dhcpPayload);
1292 DhcpServerInfo serverInfo = defaultServerInfoList.get(0);
1293 if (!direct && !indirectServerInfoList.isEmpty()) {
1294 serverInfo = indirectServerInfoList.get(0);
Yi Tseng4ec727d2017-08-31 11:21:00 -07001295 }
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001296 ConnectPoint portToFotward = serverInfo.getDhcpServerConnectPoint().orElse(null);
Yi Tseng51301292017-07-28 13:02:59 -07001297 // send packet to dhcp server connect point.
Yi Tseng4ec727d2017-08-31 11:21:00 -07001298 if (portToFotward != null) {
Yi Tseng51301292017-07-28 13:02:59 -07001299 TrafficTreatment t = DefaultTrafficTreatment.builder()
Yi Tseng4ec727d2017-08-31 11:21:00 -07001300 .setOutput(portToFotward.port()).build();
Yi Tseng51301292017-07-28 13:02:59 -07001301 OutboundPacket o = new DefaultOutboundPacket(
Yi Tseng4ec727d2017-08-31 11:21:00 -07001302 portToFotward.deviceId(), t, ByteBuffer.wrap(packet.serialize()));
Yi Tseng51301292017-07-28 13:02:59 -07001303 if (log.isTraceEnabled()) {
1304 log.trace("Relaying packet to dhcp server {}", packet);
1305 }
1306 packetService.emit(o);
1307 } else {
1308 log.warn("Can't find DHCP server connect point, abort.");
1309 }
1310 }
1311
1312
1313 /**
1314 * Gets output interface of a dhcp packet.
1315 * If option 82 exists in the dhcp packet and the option was sent by
Yi Tseng4ec727d2017-08-31 11:21:00 -07001316 * ONOS (circuit format is correct), use the connect
Yi Tseng51301292017-07-28 13:02:59 -07001317 * point and vlan id from circuit id; otherwise, find host by destination
1318 * address and use vlan id from sender (dhcp server).
1319 *
1320 * @param ethPacket the ethernet packet
1321 * @param dhcpPayload the dhcp packet
1322 * @return an interface represent the output port and vlan; empty value
1323 * if the host or circuit id not found
1324 */
Yi Tsengdcef2c22017-08-05 20:34:06 -07001325 private Optional<Interface> getClientInterface(Ethernet ethPacket, DHCP dhcpPayload) {
Yi Tseng51301292017-07-28 13:02:59 -07001326 VlanId originalPacketVlanId = VlanId.vlanId(ethPacket.getVlanID());
Yi Tseng51301292017-07-28 13:02:59 -07001327 DhcpRelayAgentOption option = (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID);
1328
Yi Tseng4ec727d2017-08-31 11:21:00 -07001329 DhcpOption circuitIdSubOption = option.getSubOption(CIRCUIT_ID.getValue());
1330 try {
1331 CircuitId circuitId = CircuitId.deserialize(circuitIdSubOption.getData());
1332 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(circuitId.connectPoint());
1333 VlanId vlanId = circuitId.vlanId();
1334 return interfaceService.getInterfacesByPort(connectPoint)
1335 .stream()
1336 .filter(iface -> interfaceContainsVlan(iface, vlanId))
1337 .findFirst();
1338 } catch (IllegalArgumentException ex) {
1339 // invalid circuit format, didn't sent by ONOS
1340 log.debug("Invalid circuit {}, use information from dhcp payload",
1341 circuitIdSubOption.getData());
Yi Tseng51301292017-07-28 13:02:59 -07001342 }
1343
1344 // Use Vlan Id from DHCP server if DHCP relay circuit id was not
1345 // sent by ONOS or circuit Id can't be parsed
Yi Tsengdcef2c22017-08-05 20:34:06 -07001346 // TODO: remove relay store from this method
Yi Tseng51301292017-07-28 13:02:59 -07001347 MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress());
1348 Optional<DhcpRecord> dhcpRecord = dhcpRelayStore.getDhcpRecord(HostId.hostId(dstMac, originalPacketVlanId));
Yi Tsengdcef2c22017-08-05 20:34:06 -07001349 ConnectPoint clientConnectPoint = dhcpRecord
Yi Tseng51301292017-07-28 13:02:59 -07001350 .map(DhcpRecord::locations)
1351 .orElse(Collections.emptySet())
1352 .stream()
1353 .reduce((hl1, hl2) -> {
Yi Tsengdcef2c22017-08-05 20:34:06 -07001354 // find latest host connect point
Yi Tseng51301292017-07-28 13:02:59 -07001355 if (hl1 == null || hl2 == null) {
1356 return hl1 == null ? hl2 : hl1;
1357 }
1358 return hl1.time() > hl2.time() ? hl1 : hl2;
1359 })
Yi Tsengdcef2c22017-08-05 20:34:06 -07001360 .orElse(null);
Yi Tseng51301292017-07-28 13:02:59 -07001361
Yi Tsengdcef2c22017-08-05 20:34:06 -07001362 if (clientConnectPoint != null) {
1363 return interfaceService.getInterfacesByPort(clientConnectPoint)
1364 .stream()
Yi Tseng4ec727d2017-08-31 11:21:00 -07001365 .filter(iface -> interfaceContainsVlan(iface, originalPacketVlanId))
Yi Tsengdcef2c22017-08-05 20:34:06 -07001366 .findFirst();
1367 }
1368 return Optional.empty();
Yi Tseng51301292017-07-28 13:02:59 -07001369 }
1370
1371 /**
1372 * Send the response DHCP to the requester host.
1373 *
1374 * @param ethPacket the packet
1375 * @param dhcpPayload the DHCP data
1376 */
1377 private void sendResponseToClient(Ethernet ethPacket, DHCP dhcpPayload) {
Yi Tsengdcef2c22017-08-05 20:34:06 -07001378 Optional<Interface> outInterface = getClientInterface(ethPacket, dhcpPayload);
1379 if (directlyConnected(dhcpPayload)) {
1380 ethPacket = removeRelayAgentOption(ethPacket);
1381 }
1382 if (!outInterface.isPresent()) {
1383 log.warn("Can't find output interface for client, ignore");
1384 return;
1385 }
1386 Interface outIface = outInterface.get();
1387 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
1388 .setOutput(outIface.connectPoint().port())
1389 .build();
1390 OutboundPacket o = new DefaultOutboundPacket(
1391 outIface.connectPoint().deviceId(),
1392 treatment,
1393 ByteBuffer.wrap(ethPacket.serialize()));
1394 if (log.isTraceEnabled()) {
1395 log.trace("Relaying packet to DHCP client {} via {}, vlan {}",
1396 ethPacket,
1397 outIface.connectPoint(),
1398 outIface.vlan());
1399 }
1400 packetService.emit(o);
Yi Tseng51301292017-07-28 13:02:59 -07001401 }
Yi Tsenge72fbb52017-08-02 15:03:31 -07001402
Yi Tseng4b013202017-09-08 17:22:51 -07001403 @Override
1404 public void triggerProbe(Host host) {
1405 // Do nothing here
1406 }
1407
1408 @Override
1409 public ProviderId id() {
Charles Chan75edab72017-09-12 17:09:32 -07001410 return PROVIDER_ID;
Yi Tseng4b013202017-09-08 17:22:51 -07001411 }
1412
Yi Tsenge72fbb52017-08-02 15:03:31 -07001413 class InternalHostListener implements HostListener {
1414 @Override
1415 public void event(HostEvent event) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001416 if (!configured()) {
1417 return;
1418 }
Yi Tsenge72fbb52017-08-02 15:03:31 -07001419 switch (event.type()) {
1420 case HOST_ADDED:
1421 case HOST_UPDATED:
1422 hostUpdated(event.subject());
1423 break;
1424 case HOST_REMOVED:
1425 hostRemoved(event.subject());
1426 break;
Yi Tsenge72fbb52017-08-02 15:03:31 -07001427 default:
1428 break;
1429 }
1430 }
1431 }
1432
1433 /**
Yi Tsenge72fbb52017-08-02 15:03:31 -07001434 * Handle host updated.
1435 * If the host is DHCP server or gateway, update connect mac and vlan.
1436 *
1437 * @param host the host
1438 */
1439 private void hostUpdated(Host host) {
Yi Tseng7da339e2017-10-23 19:39:39 -07001440 hostUpdated(host, defaultServerInfoList);
1441 hostUpdated(host, indirectServerInfoList);
Yi Tsenge72fbb52017-08-02 15:03:31 -07001442 }
1443
Yi Tseng7da339e2017-10-23 19:39:39 -07001444 private void hostUpdated(Host host, List<DhcpServerInfo> srverInfoList) {
1445 DhcpServerInfo serverInfo;
1446 Ip4Address targetIp;
1447 if (!srverInfoList.isEmpty()) {
1448 serverInfo = srverInfoList.get(0);
1449 targetIp = serverInfo.getDhcpGatewayIp4().orElse(null);
1450 Ip4Address serverIp = serverInfo.getDhcpServerIp4().orElse(null);
1451
1452 if (targetIp == null) {
1453 targetIp = serverIp;
1454 }
1455
1456 if (targetIp != null) {
1457 if (host.ipAddresses().contains(targetIp)) {
1458 serverInfo.setDhcpConnectMac(host.mac());
1459 serverInfo.setDhcpConnectVlan(host.vlan());
1460 requestDhcpPacket(serverIp);
1461 }
1462 }
1463 }
1464 }
1465
1466
Yi Tsenge72fbb52017-08-02 15:03:31 -07001467 /**
1468 * Handle host removed.
1469 * If the host is DHCP server or gateway, unset connect mac and vlan.
1470 *
1471 * @param host the host
1472 */
1473 private void hostRemoved(Host host) {
Yi Tseng7da339e2017-10-23 19:39:39 -07001474 hostRemoved(host, defaultServerInfoList);
1475 hostRemoved(host, indirectServerInfoList);
1476 }
1477
1478 private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001479 DhcpServerInfo serverInfo;
Yi Tseng7da339e2017-10-23 19:39:39 -07001480 Ip4Address targetIp;
1481 if (!serverInfoList.isEmpty()) {
1482 serverInfo = serverInfoList.get(0);
1483 Ip4Address serverIp = serverInfo.getDhcpServerIp4().orElse(null);
1484 targetIp = serverInfo.getDhcpGatewayIp4().orElse(null);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001485
Yi Tseng7da339e2017-10-23 19:39:39 -07001486 if (targetIp == null) {
1487 targetIp = serverIp;
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001488 }
Yi Tseng7da339e2017-10-23 19:39:39 -07001489
1490 if (targetIp != null) {
1491 if (host.ipAddresses().contains(targetIp)) {
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001492 serverInfo.setDhcpConnectVlan(null);
1493 serverInfo.setDhcpConnectMac(null);
Yi Tseng7da339e2017-10-23 19:39:39 -07001494 cancelDhcpPacket(serverIp);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001495 }
Yi Tsenge72fbb52017-08-02 15:03:31 -07001496 }
Yi Tsenge72fbb52017-08-02 15:03:31 -07001497 }
Yi Tseng7da339e2017-10-23 19:39:39 -07001498 }
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001499
Yi Tseng7da339e2017-10-23 19:39:39 -07001500 private void requestDhcpPacket(Ip4Address serverIp) {
1501 requestServerDhcpPacket(serverIp);
1502 requestClientDhcpPacket(serverIp);
1503 }
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001504
Yi Tseng7da339e2017-10-23 19:39:39 -07001505 private void cancelDhcpPacket(Ip4Address serverIp) {
1506 cancelServerDhcpPacket(serverIp);
1507 cancelClientDhcpPacket(serverIp);
1508 }
1509
1510 private void cancelServerDhcpPacket(Ip4Address serverIp) {
1511 TrafficSelector serverSelector =
1512 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1513 .matchIPSrc(serverIp.toIpPrefix())
1514 .build();
1515 packetService.cancelPackets(serverSelector,
1516 PacketPriority.CONTROL,
1517 appId);
1518 }
1519
1520 private void requestServerDhcpPacket(Ip4Address serverIp) {
1521 TrafficSelector serverSelector =
1522 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1523 .matchIPSrc(serverIp.toIpPrefix())
1524 .build();
1525 packetService.requestPackets(serverSelector,
1526 PacketPriority.CONTROL,
1527 appId);
1528 }
1529
1530 private void cancelClientDhcpPacket(Ip4Address serverIp) {
1531 // Packet comes from relay
1532 TrafficSelector indirectClientSelector =
1533 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1534 .matchIPDst(serverIp.toIpPrefix())
1535 .build();
1536 packetService.cancelPackets(indirectClientSelector,
1537 PacketPriority.CONTROL,
1538 appId);
1539
1540 // Packet comes from client
1541 packetService.cancelPackets(CLIENT_SERVER_SELECTOR,
1542 PacketPriority.CONTROL,
1543 appId);
1544 }
1545
1546 private void requestClientDhcpPacket(Ip4Address serverIp) {
1547 // Packet comes from relay
1548 TrafficSelector indirectClientSelector =
1549 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1550 .matchIPDst(serverIp.toIpPrefix())
1551 .build();
1552 packetService.requestPackets(indirectClientSelector,
1553 PacketPriority.CONTROL,
1554 appId);
1555
1556 // Packet comes from client
1557 packetService.requestPackets(CLIENT_SERVER_SELECTOR,
1558 PacketPriority.CONTROL,
1559 appId);
1560 }
1561
1562 /**
1563 * Process the ignore rules.
1564 *
1565 * @param deviceId the device id
1566 * @param vlanId the vlan to be ignored
1567 * @param op the operation, ADD to install; REMOVE to uninstall rules
1568 */
1569 private void processIgnoreVlanRule(DeviceId deviceId, VlanId vlanId, Objective.Operation op) {
Yi Tseng7da339e2017-10-23 19:39:39 -07001570 AtomicInteger installedCount = new AtomicInteger(DHCP_SELECTORS.size());
1571 DHCP_SELECTORS.forEach(trafficSelector -> {
1572 TrafficSelector selector = DefaultTrafficSelector.builder(trafficSelector)
1573 .matchVlanId(vlanId)
1574 .build();
1575
1576 ForwardingObjective.Builder builder = DefaultForwardingObjective.builder()
1577 .withFlag(ForwardingObjective.Flag.VERSATILE)
1578 .withSelector(selector)
1579 .withPriority(IGNORE_CONTROL_PRIORITY)
Yi Tsengf29ffd72017-11-29 10:49:20 -08001580 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
Yi Tseng7da339e2017-10-23 19:39:39 -07001581 .fromApp(appId);
1582
1583
1584 ObjectiveContext objectiveContext = new ObjectiveContext() {
1585 @Override
1586 public void onSuccess(Objective objective) {
1587 log.info("Ignore rule {} (Vlan id {}, device {}, selector {})",
1588 op, vlanId, deviceId, selector);
1589 int countDown = installedCount.decrementAndGet();
1590 if (countDown != 0) {
1591 return;
1592 }
1593 switch (op) {
1594 case ADD:
1595 ignoredVlans.put(deviceId, vlanId);
1596 break;
1597 case REMOVE:
1598 ignoredVlans.remove(deviceId, vlanId);
1599 break;
1600 default:
1601 log.warn("Unsupported objective operation {}", op);
1602 break;
1603 }
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001604 }
Yi Tseng7da339e2017-10-23 19:39:39 -07001605
1606 @Override
1607 public void onError(Objective objective, ObjectiveError error) {
1608 log.warn("Can't {} ignore rule (vlan id {}, selector {}, device {}) due to {}",
1609 op, vlanId, selector, deviceId, error);
Yi Tseng2fe8f3f2017-09-07 16:22:51 -07001610 }
Yi Tseng7da339e2017-10-23 19:39:39 -07001611 };
1612
1613 ForwardingObjective fwd;
1614 switch (op) {
1615 case ADD:
1616 fwd = builder.add(objectiveContext);
1617 break;
1618 case REMOVE:
1619 fwd = builder.remove(objectiveContext);
1620 break;
1621 default:
1622 log.warn("Unsupported objective operation {}", op);
1623 return;
Yi Tseng4ec727d2017-08-31 11:21:00 -07001624 }
Yi Tseng7da339e2017-10-23 19:39:39 -07001625
1626 Device device = deviceService.getDevice(deviceId);
1627 if (device == null || !device.is(Pipeliner.class)) {
1628 log.warn("Device {} is not available now, wait until device is available", deviceId);
1629 return;
1630 }
1631 flowObjectiveService.apply(deviceId, fwd);
1632 });
Yi Tsenge72fbb52017-08-02 15:03:31 -07001633 }
Yi Tseng51301292017-07-28 13:02:59 -07001634}