Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 17 | package org.onosproject.dhcprelay; |
| 18 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Multimap; |
Charles Chan | 70fdd49 | 2018-03-07 17:36:06 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Multimaps; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.common.collect.ImmutableSet; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 25 | import org.apache.felix.scr.annotations.Activate; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Component; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Modified; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Property; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.onlab.packet.BasePacket; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 34 | import org.onlab.packet.DHCP6; |
| 35 | import org.onlab.packet.IPv6; |
| 36 | import org.onlab.packet.Ethernet; |
| 37 | import org.onlab.packet.Ip6Address; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 38 | import org.onlab.packet.IpAddress; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 39 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 40 | import org.onlab.packet.MacAddress; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 41 | import org.onlab.packet.TpPort; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 42 | import org.onlab.packet.UDP; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 43 | import org.onlab.packet.VlanId; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 44 | import org.onlab.packet.dhcp.Dhcp6ClientDataOption; |
| 45 | import org.onlab.packet.dhcp.Dhcp6LeaseQueryOption; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 46 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
| 47 | import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 48 | import org.onlab.packet.dhcp.Dhcp6Option; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 49 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 50 | import org.onlab.packet.dhcp.Dhcp6IaTaOption; |
| 51 | import org.onlab.packet.dhcp.Dhcp6IaPdOption; |
| 52 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 53 | import org.onlab.packet.dhcp.Dhcp6IaPrefixOption; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 54 | import org.onlab.packet.dhcp.Dhcp6ClientIdOption; |
| 55 | import org.onlab.packet.dhcp.Dhcp6Duid; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 56 | import org.onlab.packet.DHCP6.MsgType; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 57 | import org.onlab.util.HexString; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 58 | import org.onlab.util.Tools; |
| 59 | import org.onosproject.cfg.ComponentConfigService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 60 | import org.onosproject.core.ApplicationId; |
| 61 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 62 | import org.onosproject.dhcprelay.api.DhcpHandler; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 63 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 64 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 65 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 66 | import org.onosproject.dhcprelay.store.DhcpRecord; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 67 | import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 68 | import org.onosproject.dhcprelay.store.DhcpRelayCounters; |
| 69 | import org.onosproject.dhcprelay.store.DhcpRelayCountersStore; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 70 | import org.onosproject.net.Device; |
| 71 | import org.onosproject.net.DeviceId; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 72 | import org.onosproject.routing.fpm.api.FpmRecord; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 73 | import org.onosproject.net.behaviour.Pipeliner; |
| 74 | import org.onosproject.net.device.DeviceService; |
| 75 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 76 | import org.onosproject.net.flow.TrafficSelector; |
| 77 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 78 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 79 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 80 | import org.onosproject.net.flowobjective.Objective; |
| 81 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 82 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 83 | import org.onosproject.net.host.HostProvider; |
| 84 | import org.onosproject.net.host.HostProviderRegistry; |
| 85 | import org.onosproject.net.host.HostProviderService; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 86 | import org.onosproject.net.host.HostService; |
| 87 | import org.onosproject.net.host.DefaultHostDescription; |
| 88 | import org.onosproject.net.host.HostDescription; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 89 | import org.onosproject.net.host.HostListener; |
| 90 | import org.onosproject.net.host.HostEvent; |
| 91 | import org.onosproject.net.intf.Interface; |
| 92 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 93 | import org.onosproject.net.packet.PacketPriority; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 94 | import org.onosproject.net.provider.ProviderId; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 95 | import org.onosproject.routeservice.Route; |
| 96 | import org.onosproject.routeservice.RouteStore; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 97 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 98 | import org.onosproject.net.ConnectPoint; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 99 | import org.onosproject.net.Host; |
| 100 | import org.onosproject.net.HostId; |
| 101 | import org.onosproject.net.HostLocation; |
| 102 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 103 | import org.onosproject.net.packet.OutboundPacket; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 104 | import org.onosproject.net.packet.PacketContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 105 | import org.onosproject.net.packet.PacketService; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 106 | import org.osgi.service.component.ComponentContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 107 | import org.slf4j.Logger; |
| 108 | import org.slf4j.LoggerFactory; |
| 109 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 110 | import org.onosproject.net.flow.TrafficTreatment; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 111 | import java.nio.ByteBuffer; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 112 | import java.util.ArrayList; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 113 | import java.util.Collection; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 114 | import java.util.Dictionary; |
| 115 | import java.util.List; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 116 | import java.util.Optional; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 117 | import java.util.Set; |
Charles Chan | 909cff8 | 2018-03-05 13:14:02 -0800 | [diff] [blame] | 118 | import java.util.concurrent.CopyOnWriteArrayList; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 119 | import java.util.concurrent.Executor; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 120 | import java.util.concurrent.atomic.AtomicInteger; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 121 | import static com.google.common.base.Preconditions.checkNotNull; |
| 122 | import static com.google.common.base.Preconditions.checkState; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 123 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 124 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 125 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
| 126 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 127 | import java.util.concurrent.Semaphore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 128 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 129 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 130 | @Component |
| 131 | @Service |
| 132 | @Property(name = "version", value = "6") |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 133 | public class Dhcp6HandlerImpl implements DhcpHandler, HostProvider { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 134 | public static final String DHCP_V6_RELAY_APP = "org.onosproject.Dhcp6HandlerImpl"; |
Saurav Das | 7c6dec1 | 2017-09-13 14:35:56 -0700 | [diff] [blame] | 135 | public static final ProviderId PROVIDER_ID = new ProviderId("dhcp6", DHCP_V6_RELAY_APP); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 136 | private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 137 | private String gCount = "global"; |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 138 | private static final String LQ_ROUTE_PROPERTY_NAME = "learnRouteFromLeasequery"; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 139 | private static final TrafficSelector CLIENT_SERVER_SELECTOR = DefaultTrafficSelector.builder() |
| 140 | .matchEthType(Ethernet.TYPE_IPV6) |
| 141 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 142 | .matchIPv6Src(IpPrefix.IPV6_LINK_LOCAL_PREFIX) |
| 143 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 144 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT)) |
| 145 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 146 | .build(); |
| 147 | private static final TrafficSelector SERVER_RELAY_SELECTOR = DefaultTrafficSelector.builder() |
| 148 | .matchEthType(Ethernet.TYPE_IPV6) |
| 149 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 150 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 151 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 152 | .build(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 153 | // lease query reply is from server to client (no relay in between) - so we need to |
| 154 | // catch that scenario also .. |
| 155 | private static final TrafficSelector LEASE_QUERY_RESPONSE_SELECTOR = DefaultTrafficSelector.builder() |
| 156 | .matchEthType(Ethernet.TYPE_IPV6) |
| 157 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 158 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 159 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT)) |
| 160 | .build(); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 161 | static final Set<TrafficSelector> DHCP_SELECTORS = ImmutableSet.of( |
| 162 | CLIENT_SERVER_SELECTOR, |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 163 | SERVER_RELAY_SELECTOR, |
| 164 | LEASE_QUERY_RESPONSE_SELECTOR |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 165 | ); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 166 | private static Logger log = LoggerFactory.getLogger(Dhcp6HandlerImpl.class); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 167 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 168 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 169 | protected DhcpRelayStore dhcpRelayStore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 170 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 172 | protected DhcpRelayCountersStore dhcpRelayCountersStore; |
| 173 | |
| 174 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 175 | protected PacketService packetService; |
| 176 | |
| 177 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 178 | protected RouteStore routeStore; |
| 179 | |
| 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 181 | protected InterfaceService interfaceService; |
| 182 | |
| 183 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 184 | protected HostService hostService; |
| 185 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 186 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 187 | protected HostProviderRegistry providerRegistry; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 188 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 189 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 190 | protected CoreService coreService; |
| 191 | |
| 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 193 | protected DhcpFpmPrefixStore dhcpFpmPrefixStore; |
| 194 | |
| 195 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 196 | protected DeviceService deviceService; |
| 197 | |
| 198 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 199 | protected FlowObjectiveService flowObjectiveService; |
| 200 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 201 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 202 | protected ComponentConfigService cfgService; |
| 203 | |
| 204 | @Property(name = Dhcp6HandlerImpl.LQ_ROUTE_PROPERTY_NAME, boolValue = false, |
| 205 | label = "Enable learning routing information from LQ") |
| 206 | private Boolean learnRouteFromLeasequery = Boolean.TRUE; |
| 207 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 208 | protected HostProviderService providerService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 209 | protected ApplicationId appId; |
Charles Chan | 70fdd49 | 2018-03-07 17:36:06 -0800 | [diff] [blame] | 210 | protected Multimap<DeviceId, VlanId> ignoredVlans = Multimaps.synchronizedMultimap(HashMultimap.create()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 211 | private InternalHostListener hostListener = new InternalHostListener(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 212 | private Boolean dhcpFpmEnabled = false; |
Charles Chan | 909cff8 | 2018-03-05 13:14:02 -0800 | [diff] [blame] | 213 | private List<DhcpServerInfo> defaultServerInfoList = new CopyOnWriteArrayList<>(); |
| 214 | private List<DhcpServerInfo> indirectServerInfoList = new CopyOnWriteArrayList<>(); |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 215 | |
| 216 | private Executor hostEventExecutor = newSingleThreadExecutor( |
| 217 | groupedThreads("dhcp6-event-host", "%d", log)); |
| 218 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 219 | private class IpAddressInfo { |
| 220 | Ip6Address ip6Address; |
| 221 | long prefTime; |
| 222 | } |
| 223 | private class PdPrefixInfo { |
| 224 | IpPrefix pdPrefix; |
| 225 | long prefTime; |
| 226 | } |
| 227 | protected int dhcp6PollInterval = 24 * 3600; // 24 hr period |
| 228 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 229 | // max 1 thread |
| 230 | static Semaphore recordSemaphore = new Semaphore(1); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 231 | |
| 232 | // CLIENT message types |
| 233 | public static final Set<Byte> MSG_TYPE_FROM_CLIENT = |
| 234 | ImmutableSet.of(DHCP6.MsgType.SOLICIT.value(), |
| 235 | DHCP6.MsgType.REQUEST.value(), |
| 236 | DHCP6.MsgType.REBIND.value(), |
| 237 | DHCP6.MsgType.RENEW.value(), |
| 238 | DHCP6.MsgType.RELEASE.value(), |
| 239 | DHCP6.MsgType.DECLINE.value(), |
| 240 | DHCP6.MsgType.CONFIRM.value(), |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 241 | DHCP6.MsgType.RELAY_FORW.value(), |
| 242 | DHCP6.MsgType.LEASEQUERY.value()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 243 | // SERVER message types |
| 244 | public static final Set<Byte> MSG_TYPE_FROM_SERVER = |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 245 | ImmutableSet.of(DHCP6.MsgType.RELAY_REPL.value(), |
| 246 | DHCP6.MsgType.LEASEQUERY_REPLY.value()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 247 | |
| 248 | @Activate |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 249 | protected void activate(ComponentContext context) { |
| 250 | cfgService.registerProperties(getClass()); |
| 251 | modified(context); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 252 | appId = coreService.registerApplication(DHCP_V6_RELAY_APP); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 253 | providerService = providerRegistry.register(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 254 | hostService.addListener(hostListener); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 257 | @Deactivate |
| 258 | protected void deactivate() { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 259 | cfgService.unregisterProperties(getClass(), false); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 260 | providerRegistry.unregister(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 261 | hostService.removeListener(hostListener); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 262 | defaultServerInfoList.forEach(this::stopMonitoringIps); |
| 263 | defaultServerInfoList.clear(); |
| 264 | indirectServerInfoList.forEach(this::stopMonitoringIps); |
| 265 | indirectServerInfoList.clear(); |
| 266 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 267 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 268 | @Modified |
| 269 | protected void modified(ComponentContext context) { |
| 270 | Dictionary<?, ?> properties = context.getProperties(); |
| 271 | Boolean flag; |
| 272 | flag = Tools.isPropertyEnabled(properties, Dhcp6HandlerImpl.LQ_ROUTE_PROPERTY_NAME); |
| 273 | if (flag != null) { |
| 274 | learnRouteFromLeasequery = flag; |
| 275 | log.info("Learning routes from DHCP leasequery is {}", |
| 276 | learnRouteFromLeasequery ? "enabled" : "disabled"); |
| 277 | } |
| 278 | } |
| 279 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 280 | private void stopMonitoringIps(DhcpServerInfo serverInfo) { |
| 281 | serverInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 282 | hostService.stopMonitoringIp(gatewayIp); |
| 283 | }); |
| 284 | serverInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 285 | hostService.stopMonitoringIp(serverIp); |
| 286 | }); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 289 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 290 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 291 | return defaultServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 295 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 296 | return indirectServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 300 | public void updateIgnoreVlanConfig(IgnoreDhcpConfig config) { |
| 301 | if (config == null) { |
| 302 | ignoredVlans.forEach(((deviceId, vlanId) -> { |
| 303 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 304 | })); |
| 305 | return; |
| 306 | } |
| 307 | config.ignoredVlans().forEach((deviceId, vlanId) -> { |
| 308 | if (ignoredVlans.get(deviceId).contains(vlanId)) { |
| 309 | // don't need to process if it already ignored |
| 310 | return; |
| 311 | } |
| 312 | processIgnoreVlanRule(deviceId, vlanId, ADD); |
| 313 | }); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 314 | ignoredVlans.forEach((deviceId, vlanId) -> { |
| 315 | if (!config.ignoredVlans().get(deviceId).contains(vlanId)) { |
| 316 | // not contains in new config, remove it |
| 317 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 318 | } |
| 319 | }); |
| 320 | } |
| 321 | |
| 322 | @Override |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 323 | public void removeIgnoreVlanState(IgnoreDhcpConfig config) { |
| 324 | if (config == null) { |
| 325 | ignoredVlans.clear(); |
| 326 | return; |
| 327 | } |
| 328 | config.ignoredVlans().forEach((deviceId, vlanId) -> { |
| 329 | ignoredVlans.remove(deviceId, vlanId); |
| 330 | }); |
| 331 | } |
| 332 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 333 | public DhcpRecord getDhcpRelayRecordFor(Ip6Address clientAddress) { |
| 334 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 335 | Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 336 | DhcpRecord dr = null; |
| 337 | for (DhcpRecord e:records) { |
| 338 | if (e.ip6Address().isPresent()) { |
| 339 | if (e.ip6Address().get().equals(clientAddress)) { |
| 340 | dr = e; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | return dr; |
| 346 | } |
| 347 | |
| 348 | public MacAddress findNextHopMacForIp6FromRelayStore(Ip6Address clientAddress, |
| 349 | MacAddress clientMacAddress, VlanId vlanID) { |
| 350 | |
| 351 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 352 | |
| 353 | if (dr != null) { |
| 354 | Optional<MacAddress> nextHopTempMac = dr.nextHopTemp(); |
| 355 | if (nextHopTempMac.isPresent()) { |
| 356 | log.info("findNextHopForIp6FromRelayStore " + clientAddress + " got mac " + nextHopTempMac.toString()); |
| 357 | return nextHopTempMac.get(); |
| 358 | } |
| 359 | } else { |
| 360 | log.warn("findNextHopMacForIp6FromRelayStore could NOT find next hop for " + clientAddress); |
| 361 | return null; |
| 362 | } |
| 363 | return null; |
| 364 | } |
| 365 | |
| 366 | public Ip6Address findNextHopIp6FromRelayStore(Ip6Address clientAddress) { |
| 367 | |
| 368 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 369 | if (dr != null) { |
| 370 | Optional<MacAddress> nextHopMac = dr.nextHop(); |
| 371 | if (nextHopMac.isPresent()) { |
| 372 | // find the local ip6 from the host store |
| 373 | HostId gwHostId = HostId.hostId(nextHopMac.get(), dr.vlanId()); |
| 374 | Host gwHost = hostService.getHost(gwHostId); |
| 375 | if (gwHost == null) { |
| 376 | log.warn("Can't find next hop host ID {}", gwHostId); |
| 377 | return null; |
| 378 | } |
| 379 | Ip6Address nextHopIp = gwHost.ipAddresses() |
| 380 | .stream() |
| 381 | .filter(IpAddress::isIp6) |
| 382 | .filter(IpAddress::isLinkLocal) |
| 383 | .map(IpAddress::getIp6Address) |
| 384 | .findFirst() |
| 385 | .orElse(null); |
| 386 | |
| 387 | log.info("findNextHopIp6FromRelayStore " + clientAddress + " got mac " + |
| 388 | nextHopMac.toString() + " ip6 " + nextHopIp); |
| 389 | return nextHopIp; |
| 390 | } |
| 391 | } else { |
| 392 | log.warn("findNextHopIp6FromRelayStore could NOT find next hop for " + clientAddress); |
| 393 | return null; |
| 394 | } |
| 395 | return null; |
| 396 | } |
| 397 | |
| 398 | private void setPotentialNextHopForIp6InRelayStore(Ip6Address clientAddress, |
| 399 | VlanId vlanId, MacAddress nh) { |
| 400 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 401 | if (dr != null) { |
| 402 | dr.nextHopTemp(nh); |
| 403 | log.debug("LQ6 potential NH mac " + nh.toString() + " UPDATED in RelayRecord client " + clientAddress); |
| 404 | } else { |
| 405 | log.warn("LQ6 potential NH mac" + nh.toString() + |
| 406 | " NOT FOUND in RelayRecord for client - LQ rejected" + clientAddress); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | public void handleLeaseQuery6ReplyMsg(PacketContext context, DHCP6 dhcp6Payload) { |
| 411 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 412 | log.info("Got LQV6-REPLY on port {}", inPort); |
| 413 | List<Dhcp6Option> lopt = dhcp6Payload.getOptions(); |
| 414 | log.info("Options list: {}", lopt); |
| 415 | // find out if this lease is known is |
| 416 | Dhcp6ClientDataOption clientDataOption = dhcp6Payload.getOptions() |
| 417 | .stream() |
| 418 | .filter(opt -> opt instanceof Dhcp6ClientDataOption) |
| 419 | .map(pld -> (Dhcp6ClientDataOption) pld) |
| 420 | .findFirst() |
| 421 | .orElse(null); |
| 422 | |
| 423 | if (clientDataOption == null) { |
| 424 | log.warn("clientDataOption option is not present, " + |
| 425 | "lease is UNKNOWN - not adding any new route..."); |
| 426 | } else { |
| 427 | Dhcp6IaAddressOption aiAddressOption = clientDataOption.getOptions() |
| 428 | .stream() |
| 429 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 430 | .map(pld -> (Dhcp6IaAddressOption) pld) |
| 431 | .findFirst() |
| 432 | .orElse(null); |
| 433 | |
| 434 | Dhcp6ClientIdOption clientIdOption = clientDataOption.getOptions() |
| 435 | .stream() |
| 436 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 437 | .map(pld -> (Dhcp6ClientIdOption) pld) |
| 438 | .findFirst() |
| 439 | .orElse(null); |
| 440 | |
| 441 | if (aiAddressOption == null) { |
| 442 | log.warn("clientDataOption from DHCP server does not " + |
| 443 | "contains Dhcp6IaAddressOption for the client - giving up..."); |
| 444 | } else { |
| 445 | Ip6Address clientAddress = aiAddressOption.getIp6Address(); |
| 446 | MacAddress clientMacAddress = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 447 | Ethernet packet = context.inPacket().parsed(); |
| 448 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 449 | MacAddress potentialNextHopMac = |
| 450 | findNextHopMacForIp6FromRelayStore(clientAddress, clientMacAddress, vlanId); |
| 451 | |
| 452 | if (potentialNextHopMac == null) { |
| 453 | log.warn("Can't find next hop host mac for client {} mac:{}/{}", |
| 454 | clientAddress, clientMacAddress, vlanId); |
| 455 | return; |
| 456 | } else { |
| 457 | log.info("Next hop mac for {}/{}/{} is {}", clientAddress, |
| 458 | clientMacAddress, vlanId, potentialNextHopMac.toString()); |
| 459 | } |
| 460 | // search the next hop in the hosts store |
| 461 | HostId gwHostId = HostId.hostId(potentialNextHopMac, vlanId); |
| 462 | Host gwHost = hostService.getHost(gwHostId); |
| 463 | if (gwHost == null) { |
| 464 | log.warn("Can't find next hop host ID {}", gwHostId); |
| 465 | return; |
| 466 | } |
| 467 | Ip6Address nextHopIp = gwHost.ipAddresses() |
| 468 | .stream() |
| 469 | .filter(IpAddress::isIp6) |
| 470 | .filter(IpAddress::isLinkLocal) |
| 471 | .map(IpAddress::getIp6Address) |
| 472 | .findFirst() |
| 473 | .orElse(null); |
| 474 | if (nextHopIp == null) { |
| 475 | log.warn("Can't find IP6 address of next hop {}", gwHost); |
| 476 | return; |
| 477 | } |
| 478 | log.info("client " + clientAddress + " is known !"); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 479 | Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 480 | log.debug("updating route of Client for indirectly connected."); |
| 481 | log.debug("client ip: " + clientAddress + ", next hop ip6: " + nextHopIp); |
| 482 | routeStore.updateRoute(routeForIP6); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 487 | @Override |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 488 | public void processDhcpPacket(PacketContext context, BasePacket payload) { |
| 489 | checkNotNull(payload, "DHCP6 payload can't be null"); |
| 490 | checkState(payload instanceof DHCP6, "Payload is not a DHCP6"); |
| 491 | DHCP6 dhcp6Payload = (DHCP6) payload; |
| 492 | Ethernet receivedPacket = context.inPacket().parsed(); |
| 493 | |
| 494 | if (!configured()) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 495 | log.warn("Missing DHCP6 relay server config. " + |
| 496 | "Abort packet processing dhcp6 payload {}", dhcp6Payload); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 499 | byte msgTypeVal = dhcp6Payload.getMsgType(); |
| 500 | MsgType msgType = DHCP6.MsgType.getType(msgTypeVal); |
| 501 | log.debug("msgType is {}", msgType); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 502 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 503 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Jonathan Hart | 8ca2bc0 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 504 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 505 | if (inPort == null) { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 506 | log.warn("incoming ConnectPoint is null"); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 507 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 508 | Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort); |
| 509 | //ignore the packets if dhcp client interface is not configured on onos. |
| 510 | if (receivingInterfaces.isEmpty()) { |
| 511 | log.warn("Virtual interface is not configured on {}", inPort); |
| 512 | return; |
| 513 | } |
| 514 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 515 | if (msgTypeVal == DHCP6.MsgType.LEASEQUERY.value()) { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 516 | List<InternalPacket> ethernetClientPackets = |
| 517 | learnRouteFromLeasequery ? |
| 518 | processLQ6PacketFromClient(context, receivedPacket, receivingInterfaces, dhcp6Payload) : |
| 519 | processDhcp6ForwardOnly(context, receivedPacket, receivingInterfaces, dhcp6Payload); |
| 520 | for (InternalPacket internalPacket : ethernetClientPackets) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 521 | forwardPacket(internalPacket); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 522 | } |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 523 | } else if (msgTypeVal == DHCP6.MsgType.LEASEQUERY_REPLY.value() && learnRouteFromLeasequery) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 524 | IPv6 clientIpv6 = (IPv6) receivedPacket.getPayload(); |
| 525 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 526 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 527 | Interface serverInterface = Dhcp6HandlerUtil.directlyConnected(clientDhcp6) ? |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 528 | getServerInterface() : getIndirectServerInterface(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 529 | InternalPacket ethernetPacketReply = |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 530 | Dhcp6HandlerUtil.processLQ6PacketFromServer( |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 531 | defaultServerInfoList, indirectServerInfoList, |
| 532 | serverInterface, interfaceService, |
| 533 | hostService, |
| 534 | context, receivedPacket, receivingInterfaces); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 535 | if (ethernetPacketReply != null) { |
| 536 | forwardPacket(ethernetPacketReply); |
| 537 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 538 | handleLeaseQuery6ReplyMsg(context, dhcp6Payload); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 539 | } else if (MSG_TYPE_FROM_CLIENT.contains(msgTypeVal)) { |
| 540 | List<InternalPacket> ethernetClientPacket = |
| 541 | processDhcp6PacketFromClient(context, receivedPacket, receivingInterfaces); |
| 542 | for (InternalPacket internalPacket : ethernetClientPacket) { |
| 543 | forwardPacket(internalPacket); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 544 | } |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 545 | } else if (MSG_TYPE_FROM_SERVER.contains(msgTypeVal)) { |
| 546 | log.debug("calling processDhcp6PacketFromServer with RELAY_REPL {}, {}", receivedPacket, dhcp6Payload); |
| 547 | InternalPacket ethernetPacketReply = |
| 548 | processDhcp6PacketFromServer(context, receivedPacket, receivingInterfaces); |
| 549 | if (ethernetPacketReply != null) { |
| 550 | forwardPacket(ethernetPacketReply); |
| 551 | } |
| 552 | } else { |
| 553 | log.warn("Not so fast, packet type {} not supported yet", msgTypeVal); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 557 | /** |
| 558 | * Checks if this app has been configured. |
| 559 | * |
| 560 | * @return true if all information we need have been initialized |
| 561 | */ |
| 562 | public boolean configured() { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 563 | return !defaultServerInfoList.isEmpty(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 566 | @Override |
| 567 | public ProviderId id() { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 568 | return PROVIDER_ID; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public void triggerProbe(Host host) { |
| 573 | // Do nothing here |
| 574 | } |
| 575 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 576 | //forward the packet to ConnectPoint where the DHCP server is attached. |
| 577 | private void forwardPacket(InternalPacket packet) { |
| 578 | //send Packetout to dhcp server connectpoint. |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 579 | if (packet.getDestLocation() != null) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 580 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 581 | .setOutput(packet.getDestLocation().port()).build(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 582 | OutboundPacket o = new DefaultOutboundPacket( |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 583 | packet.getDestLocation().deviceId(), t, ByteBuffer.wrap(packet.getPacket().serialize())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 584 | packetService.emit(o); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 585 | if (log.isTraceEnabled()) { |
| 586 | IPv6 ip6 = (IPv6) packet.getPacket().getPayload(); |
| 587 | UDP udp = (UDP) ip6.getPayload(); |
| 588 | DHCP6 dhcp6 = (DHCP6) udp.getPayload(); |
| 589 | log.trace("Relaying packet to destination {} eth: {} dhcp: {}", |
| 590 | packet.getDestLocation(), packet.getPacket(), dhcp6); |
| 591 | } |
| 592 | |
| 593 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 596 | /** |
| 597 | * extract from dhcp6 packet client ipv6 address of given by dhcp server. |
| 598 | * |
| 599 | * @param dhcp6 the dhcp6 packet |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 600 | * @return IpAddressInfo IpAddressInfo given by dhcp server, or null if not exists |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 601 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 602 | private IpAddressInfo extractIpAddress(DHCP6 dhcp6) { |
| 603 | IpAddressInfo ipInfo = new IpAddressInfo(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 604 | |
| 605 | log.debug("extractIpAddress enters dhcp6 {}.", dhcp6); |
| 606 | // Extract IPv6 address from IA NA ot IA TA option |
| 607 | Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions() |
| 608 | .stream() |
| 609 | .filter(opt -> opt instanceof Dhcp6IaNaOption) |
| 610 | .map(opt -> (Dhcp6IaNaOption) opt) |
| 611 | .findFirst(); |
| 612 | Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions() |
| 613 | .stream() |
| 614 | .filter(opt -> opt instanceof Dhcp6IaTaOption) |
| 615 | .map(opt -> (Dhcp6IaTaOption) opt) |
| 616 | .findFirst(); |
| 617 | Optional<Dhcp6IaAddressOption> iaAddressOption; |
| 618 | if (iaNaOption.isPresent()) { |
| 619 | log.debug("Found IPv6 address from iaNaOption {}", iaNaOption); |
| 620 | |
| 621 | iaAddressOption = iaNaOption.get().getOptions().stream() |
| 622 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 623 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 624 | .findFirst(); |
| 625 | } else if (iaTaOption.isPresent()) { |
| 626 | log.debug("Found IPv6 address from iaTaOption {}", iaTaOption); |
| 627 | |
| 628 | iaAddressOption = iaTaOption.get().getOptions().stream() |
| 629 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 630 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 631 | .findFirst(); |
| 632 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 633 | log.info("No IPv6 address found from iaTaOption {}", iaTaOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 634 | iaAddressOption = Optional.empty(); |
| 635 | } |
| 636 | if (iaAddressOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 637 | ipInfo.ip6Address = iaAddressOption.get().getIp6Address(); |
| 638 | ipInfo.prefTime = iaAddressOption.get().getPreferredLifetime(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 639 | log.debug("Found IPv6 address from iaAddressOption {}", iaAddressOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 640 | } else { |
| 641 | log.debug("Can't find IPv6 address from DHCPv6 {}", dhcp6); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 642 | return null; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 643 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 644 | return ipInfo; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 645 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 646 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 647 | /** |
| 648 | * extract from dhcp6 packet Prefix prefix provided by dhcp server. |
| 649 | * |
| 650 | * @param dhcp6 the dhcp6 payload |
| 651 | * @return IpPrefix Prefix Delegation prefix, or null if not exists. |
| 652 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 653 | private PdPrefixInfo extractPrefix(DHCP6 dhcp6) { |
| 654 | log.debug("extractPrefix enters {}", dhcp6); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 655 | |
| 656 | // extract prefix |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 657 | PdPrefixInfo pdPrefixInfo = new PdPrefixInfo(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 658 | |
| 659 | Ip6Address prefixAddress = null; |
| 660 | |
| 661 | // Extract IPv6 prefix from IA PD option |
| 662 | Optional<Dhcp6IaPdOption> iaPdOption = dhcp6.getOptions() |
| 663 | .stream() |
| 664 | .filter(opt -> opt instanceof Dhcp6IaPdOption) |
| 665 | .map(opt -> (Dhcp6IaPdOption) opt) |
| 666 | .findFirst(); |
| 667 | |
| 668 | Optional<Dhcp6IaPrefixOption> iaPrefixOption; |
| 669 | if (iaPdOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 670 | log.debug("IA_PD option found {}", iaPdOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 671 | |
| 672 | iaPrefixOption = iaPdOption.get().getOptions().stream() |
| 673 | .filter(opt -> opt instanceof Dhcp6IaPrefixOption) |
| 674 | .map(opt -> (Dhcp6IaPrefixOption) opt) |
| 675 | .findFirst(); |
| 676 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 677 | log.debug("IA_PD option NOT found"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 678 | |
| 679 | iaPrefixOption = Optional.empty(); |
| 680 | } |
| 681 | if (iaPrefixOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 682 | log.debug("IAPrefix Option within IA_PD option found {}", iaPrefixOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 683 | |
| 684 | prefixAddress = iaPrefixOption.get().getIp6Prefix(); |
| 685 | int prefixLen = (int) iaPrefixOption.get().getPrefixLength(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 686 | log.debug("Prefix length is {} bits", prefixLen); |
| 687 | pdPrefixInfo.pdPrefix = IpPrefix.valueOf(prefixAddress, prefixLen); |
| 688 | pdPrefixInfo.prefTime = iaPrefixOption.get().getPreferredLifetime(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 689 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 690 | log.debug("Can't find IPv6 prefix from DHCPv6 {}", dhcp6); |
| 691 | return null; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 692 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 693 | return pdPrefixInfo; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /** |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 697 | * extract from dhcp6 packet ClientIdOption. |
| 698 | * |
| 699 | * @param directConnFlag directly connected host |
| 700 | * @param dhcp6Payload the dhcp6 payload |
| 701 | * @return Dhcp6ClientIdOption clientIdOption, or null if not exists. |
| 702 | */ |
| 703 | private Dhcp6ClientIdOption extractClinedId(Boolean directConnFlag, DHCP6 dhcp6Payload) { |
| 704 | Dhcp6ClientIdOption clientIdOption; |
| 705 | |
| 706 | if (directConnFlag) { |
| 707 | clientIdOption = dhcp6Payload.getOptions() |
| 708 | .stream() |
| 709 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 710 | .map(opt -> (Dhcp6ClientIdOption) opt) |
| 711 | .findFirst() |
| 712 | .orElse(null); |
| 713 | } else { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 714 | DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Payload); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 715 | clientIdOption = leafDhcp.getOptions() |
| 716 | .stream() |
| 717 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 718 | .map(opt -> (Dhcp6ClientIdOption) opt) |
| 719 | .findFirst() |
| 720 | .orElse(null); |
| 721 | } |
| 722 | |
| 723 | return clientIdOption; |
| 724 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 725 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 726 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 727 | * remove host or route and update dhcp relay record attributes. |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 728 | * |
| 729 | * @param directConnFlag flag to show that packet is from directly connected client |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 730 | * @param location client side connect point |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 731 | * @param dhcp6Packet the dhcp6 payload |
| 732 | * @param clientPacket client's ethernet packet |
| 733 | * @param clientIpv6 client's Ipv6 packet |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 734 | * @param clientInterface client interfaces |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 735 | */ |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 736 | private void removeHostOrRoute(boolean directConnFlag, ConnectPoint location, |
| 737 | DHCP6 dhcp6Packet, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 738 | Ethernet clientPacket, IPv6 clientIpv6, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 739 | Interface clientInterface) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 740 | log.debug("removeHostOrRoute enters {}", dhcp6Packet); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 741 | VlanId vlanId = clientInterface.vlan(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 742 | MacAddress srcMac = clientPacket.getSourceMAC(); // could be gw or host |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 743 | MacAddress leafClientMac; |
| 744 | byte leafMsgType; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 745 | log.debug("client mac {} client vlan {}", HexString.toHexString(srcMac.toBytes(), ":"), vlanId); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 746 | |
| 747 | Dhcp6ClientIdOption clientIdOption = extractClinedId(directConnFlag, dhcp6Packet); |
| 748 | if (clientIdOption != null) { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 749 | if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || |
| 750 | (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) { |
| 751 | leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 752 | } else { |
| 753 | log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 754 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 757 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 758 | log.warn("CLIENTID option NOT found. Don't create DhcpRelay Record."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 759 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 763 | HostId leafHostId = HostId.hostId(leafClientMac, vlanId); |
| 764 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 765 | if (record == null) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 766 | record = new DhcpRecord(leafHostId); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 767 | } else { |
| 768 | record = record.clone(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 769 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 770 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 771 | Boolean isMsgRelease = Dhcp6HandlerUtil.isDhcp6Release(dhcp6Packet); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 772 | IpAddressInfo ipInfo; |
| 773 | PdPrefixInfo pdInfo = null; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 774 | if (directConnFlag) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 775 | // Add to host store if it is connected to network directly |
| 776 | ipInfo = extractIpAddress(dhcp6Packet); |
| 777 | if (ipInfo != null) { |
| 778 | if (isMsgRelease) { |
| 779 | HostId hostId = HostId.hostId(srcMac, vlanId); |
| 780 | log.debug("remove Host {} ip for directly connected.", hostId.toString()); |
| 781 | providerService.removeIpFromHost(hostId, ipInfo.ip6Address); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 782 | } |
| 783 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 784 | log.debug("ipAddress not found. Do not remove Host {} for directly connected.", |
| 785 | HostId.hostId(srcMac, vlanId).toString()); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 786 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 787 | leafMsgType = dhcp6Packet.getMsgType(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 788 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 789 | // Remove from route store if it is not connected to network directly |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 790 | // pick out the first link-local ip address |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 791 | IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 792 | if (nextHopIp == null) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 793 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 794 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 795 | return; |
| 796 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 797 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 798 | DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Packet); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 799 | ipInfo = extractIpAddress(leafDhcp); |
| 800 | if (ipInfo == null) { |
| 801 | log.debug("ip is null"); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 802 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 803 | if (isMsgRelease) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 804 | Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 805 | log.debug("removing route of 128 address for indirectly connected."); |
| 806 | log.debug("128 ip {}, nexthop {}", |
| 807 | HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"), |
| 808 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 809 | routeStore.removeRoute(routeForIP); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 810 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 811 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 812 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 813 | pdInfo = extractPrefix(leafDhcp); |
| 814 | if (pdInfo == null) { |
| 815 | log.debug("ipPrefix is null "); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 816 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 817 | if (isMsgRelease) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 818 | Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 819 | log.debug("removing route of PD for indirectly connected."); |
| 820 | log.debug("pd ip {}, nexthop {}", |
| 821 | HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"), |
| 822 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 823 | |
| 824 | routeStore.removeRoute(routeForPrefix); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 825 | if (this.dhcpFpmEnabled) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 826 | dhcpFpmPrefixStore.removeFpmRecord(pdInfo.pdPrefix); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 827 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 830 | leafMsgType = leafDhcp.getMsgType(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 831 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 832 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 833 | if (isMsgRelease) { |
| 834 | log.debug("DHCP6 RELEASE msg."); |
| 835 | if (record != null) { |
| 836 | if (ipInfo != null) { |
| 837 | log.debug("DhcpRelay Record ip6Address is set to null."); |
| 838 | record.ip6Address(null); |
| 839 | } |
| 840 | if (pdInfo != null) { |
| 841 | log.debug("DhcpRelay Record pdPrefix is set to null."); |
| 842 | } |
| 843 | |
| 844 | if (!record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) { |
| 845 | log.warn("IP6 address and IP6 PD both are null. Remove record."); |
| 846 | // do not remove a record. Let timer task handler it. |
| 847 | //dhcpRelayStore.removeDhcpRecord(HostId.hostId(leafClientMac, vlanId)); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
Ray Milkey | ffe1a33 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 852 | if (record != null) { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 853 | record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Ray Milkey | ffe1a33 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 854 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
| 855 | record.ip6Status(DHCP6.MsgType.getType(leafMsgType)); |
| 856 | record.setDirectlyConnected(directConnFlag); |
| 857 | if (!directConnFlag) { |
| 858 | // Update gateway mac address if the host is not directly connected |
| 859 | record.nextHop(srcMac); |
| 860 | } |
| 861 | record.updateLastSeen(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 862 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 863 | dhcpRelayStore.updateDhcpRecord(leafHostId, record); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 864 | // TODO Use AtomicInteger for the counters |
| 865 | try { |
| 866 | recordSemaphore.acquire(); |
| 867 | try { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 868 | dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 869 | } finally { |
| 870 | // calling release() after a successful acquire() |
| 871 | recordSemaphore.release(); |
| 872 | } |
| 873 | } catch (InterruptedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 874 | Thread.currentThread().interrupt(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 875 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /** |
| 879 | * add host or route and update dhcp relay record. |
| 880 | * |
| 881 | * @param directConnFlag flag to show that packet is from directly connected client |
| 882 | * @param location client side connect point |
| 883 | * @param dhcp6Relay the dhcp6 payload |
| 884 | * @param embeddedDhcp6 the dhcp6 payload within relay |
| 885 | * @param srcMac client gw/host macAddress |
| 886 | * @param clientInterface client interface |
| 887 | */ |
| 888 | private void addHostOrRoute(boolean directConnFlag, ConnectPoint location, DHCP6 dhcp6Relay, |
| 889 | DHCP6 embeddedDhcp6, MacAddress srcMac, Interface clientInterface) { |
| 890 | log.debug("addHostOrRoute entered."); |
| 891 | VlanId vlanId = clientInterface.vlan(); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 892 | Boolean isMsgReply = Dhcp6HandlerUtil.isDhcp6Reply(dhcp6Relay); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 893 | MacAddress leafClientMac; |
| 894 | Byte leafMsgType; |
| 895 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 896 | Dhcp6ClientIdOption clientIdOption = extractClinedId(directConnFlag, embeddedDhcp6); |
| 897 | if (clientIdOption != null) { |
| 898 | log.debug("CLIENTID option found {}", clientIdOption); |
| 899 | if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || |
| 900 | (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) { |
| 901 | leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 902 | } else { |
| 903 | log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 904 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 907 | } else { |
| 908 | log.warn("CLIENTID option NOT found. No DhcpRelay Record created."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 909 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 910 | return; |
| 911 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 912 | HostId leafHostId = HostId.hostId(leafClientMac, vlanId); |
| 913 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 914 | if (record == null) { |
| 915 | record = new DhcpRecord(HostId.hostId(leafClientMac, vlanId)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 916 | } else { |
| 917 | record = record.clone(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 918 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 919 | |
| 920 | IpAddressInfo ipInfo; |
| 921 | PdPrefixInfo pdInfo = null; |
| 922 | if (directConnFlag) { |
| 923 | // Add to host store if it connect to network directly |
| 924 | ipInfo = extractIpAddress(embeddedDhcp6); |
| 925 | if (ipInfo != null) { |
| 926 | if (isMsgReply) { |
| 927 | Set<IpAddress> ips = Sets.newHashSet(ipInfo.ip6Address); |
| 928 | HostId hostId = HostId.hostId(srcMac, vlanId); |
| 929 | Host host = hostService.getHost(hostId); |
| 930 | HostLocation hostLocation = new HostLocation(clientInterface.connectPoint(), |
| 931 | System.currentTimeMillis()); |
| 932 | Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation); |
| 933 | if (host != null) { |
| 934 | // Dual homing support: |
| 935 | // if host exists, use old locations and new location |
| 936 | hostLocations.addAll(host.locations()); |
| 937 | } |
| 938 | HostDescription desc = new DefaultHostDescription(srcMac, vlanId, hostLocations, ips, |
| 939 | false); |
| 940 | log.debug("adding Host for directly connected."); |
| 941 | log.debug("client mac {} client vlan {} hostlocation {}", |
| 942 | HexString.toHexString(srcMac.toBytes(), ":"), vlanId, hostLocation.toString()); |
| 943 | // Replace the ip when dhcp server give the host new ip address |
| 944 | providerService.hostDetected(hostId, desc, false); |
| 945 | } |
| 946 | } else { |
| 947 | log.warn("ipAddress not found. Do not add Host {} for directly connected.", |
| 948 | HostId.hostId(srcMac, vlanId).toString()); |
| 949 | } |
| 950 | leafMsgType = embeddedDhcp6.getMsgType(); |
| 951 | } else { |
| 952 | // Add to route store if it does not connect to network directly |
| 953 | // pick out the first link-local ip address |
| 954 | IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId); |
| 955 | if (nextHopIp == null) { |
| 956 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 957 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 958 | return; |
| 959 | } |
| 960 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 961 | DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(embeddedDhcp6); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 962 | ipInfo = extractIpAddress(leafDhcp); |
| 963 | if (ipInfo == null) { |
| 964 | log.debug("ip is null"); |
| 965 | } else { |
| 966 | if (isMsgReply) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 967 | Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 968 | log.debug("adding Route of 128 address for indirectly connected."); |
| 969 | routeStore.updateRoute(routeForIP); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | pdInfo = extractPrefix(leafDhcp); |
| 974 | if (pdInfo == null) { |
| 975 | log.debug("ipPrefix is null "); |
| 976 | } else { |
| 977 | if (isMsgReply) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 978 | Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 979 | log.debug("adding Route of PD for indirectly connected."); |
| 980 | routeStore.updateRoute(routeForPrefix); |
| 981 | if (this.dhcpFpmEnabled) { |
| 982 | FpmRecord fpmRecord = new FpmRecord(pdInfo.pdPrefix, nextHopIp, FpmRecord.Type.DHCP_RELAY); |
| 983 | dhcpFpmPrefixStore.addFpmRecord(pdInfo.pdPrefix, fpmRecord); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | leafMsgType = leafDhcp.getMsgType(); |
| 988 | } |
| 989 | if (leafMsgType == DHCP6.MsgType.RELEASE.value() || |
| 990 | (leafMsgType == DHCP6.MsgType.REPLY.value()) && ipInfo == null) { |
| 991 | log.warn("DHCP6 RELEASE/REPLY(null ip) from Server. MsgType {}", leafMsgType); |
| 992 | //return; |
| 993 | } |
| 994 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 995 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 996 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 997 | if (leafMsgType == DHCP6.MsgType.REPLY.value()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 998 | if (ipInfo != null) { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 999 | log.debug("IP6 address is being stored into dhcp-relay store."); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1000 | log.debug("Client IP6 address {}", HexString.toHexString(ipInfo.ip6Address.toOctets(), ":")); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1001 | record.ip6Address(ipInfo.ip6Address); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1002 | record.updateAddrPrefTime(ipInfo.prefTime); |
| 1003 | record.updateLastIp6Update(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1004 | } else { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1005 | log.debug("IP6 address is not returned from server. Maybe only PD is returned."); |
| 1006 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1007 | if (pdInfo != null) { |
| 1008 | log.debug("IP6 PD address {}", |
| 1009 | HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":")); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1010 | record.pdPrefix(pdInfo.pdPrefix); |
| 1011 | record.updatePdPrefTime(pdInfo.prefTime); |
| 1012 | record.updateLastPdUpdate(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1013 | } else { |
| 1014 | log.debug("IP6 PD address is not returned from server. Maybe only IPAddress is returned."); |
| 1015 | } |
| 1016 | } |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1017 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1018 | record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1019 | record.ip6Status(DHCP6.MsgType.getType(leafMsgType)); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1020 | record.setDirectlyConnected(directConnFlag); |
| 1021 | record.updateLastSeen(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1022 | dhcpRelayStore.updateDhcpRecord(leafHostId, record); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1023 | // TODO Use AtomicInteger for the counters |
| 1024 | try { |
| 1025 | recordSemaphore.acquire(); |
| 1026 | try { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1027 | dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1028 | } finally { |
| 1029 | // calling release() after a successful acquire() |
| 1030 | recordSemaphore.release(); |
| 1031 | } |
| 1032 | } catch (InterruptedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 1033 | Thread.currentThread().interrupt(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1034 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1037 | private List<InternalPacket> processDhcp6ForwardOnly(PacketContext context, |
| 1038 | Ethernet clientPacket, |
| 1039 | Set<Interface> clientInterfaces, |
| 1040 | DHCP6 dhcpPacket) { |
| 1041 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 1042 | log.trace("Got DHCPv6 on port {}", inPort); |
| 1043 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcpPacket); |
| 1044 | |
| 1045 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1046 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1047 | |
| 1048 | for (DhcpServerInfo dhcpServer : serverInfoList) { |
| 1049 | Ethernet newPacket = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context, |
| 1050 | clientPacket, clientInterfaces, dhcpServer, getServerInterface(dhcpServer)); |
| 1051 | log.trace("Built packet for server {} : {}", dhcpServer, newPacket); |
| 1052 | internalPackets.add(InternalPacket.internalPacket(newPacket, |
| 1053 | dhcpServer.getDhcpServerConnectPoint().get())); |
| 1054 | } |
| 1055 | |
| 1056 | return internalPackets; |
| 1057 | } |
| 1058 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1059 | private List<InternalPacket> processLQ6PacketFromClient(PacketContext context, |
| 1060 | Ethernet clientPacket, |
| 1061 | Set<Interface> clientInterfaces, |
| 1062 | DHCP6 dhcp6Payload) { |
| 1063 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 1064 | log.info("Got LQ-REQUEST V6 on port {}", inPort); |
| 1065 | List<Dhcp6Option> lopt = dhcp6Payload.getOptions(); |
| 1066 | log.info("Options list: {}", lopt); |
| 1067 | Dhcp6LeaseQueryOption lqoption = dhcp6Payload.getOptions() |
| 1068 | .stream() |
| 1069 | .filter(opt -> opt instanceof Dhcp6LeaseQueryOption) |
| 1070 | .map(pld -> (Dhcp6LeaseQueryOption) pld) |
| 1071 | .findFirst() |
| 1072 | .orElse(null); |
| 1073 | |
| 1074 | if (lqoption == null) { |
| 1075 | // Can't find dhcp payload |
| 1076 | log.warn("Can't find dhcp6 lease query message - aborting"); |
| 1077 | return null; |
| 1078 | } else { |
| 1079 | log.info("dhcp6 lqv6 options found: {}", lqoption); |
| 1080 | } |
| 1081 | log.warn("LQv6 for " + lqoption.linkAddress.toString() + " comes from " + inPort.toString()); |
| 1082 | Ethernet packet = context.inPacket().parsed(); |
| 1083 | Ip6Address clientAddress = lqoption.linkAddress; |
| 1084 | IPv6 ipv6Packet = (IPv6) packet.getPayload(); |
| 1085 | Ip6Address nextHopIp = findNextHopIp6FromRelayStore(clientAddress); |
| 1086 | |
| 1087 | // 1. only if there is a route to remove - remove it |
| 1088 | if (nextHopIp != null) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1089 | Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1090 | log.debug("Removing route of Client " + clientAddress + |
| 1091 | " for indirectly connected - next hop ip6 " + nextHopIp); |
| 1092 | routeStore.removeRoute(routeForIP6); |
| 1093 | } |
| 1094 | |
| 1095 | // 2. note the potential NH this packet came from in case it's a known lease |
| 1096 | // this NH will then be used to build the route |
| 1097 | MacAddress potentialNH = packet.getSourceMAC(); |
| 1098 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 1099 | setPotentialNextHopForIp6InRelayStore(clientAddress, vlanId, potentialNH); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1100 | // 3. route this LQ6 to all relevant servers |
| 1101 | IPv6 clientIpv6 = (IPv6) clientPacket.getPayload(); |
| 1102 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 1103 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
| 1104 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1105 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1106 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1107 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1108 | List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList); |
| 1109 | |
| 1110 | for (DhcpServerInfo serverInfo : copyServerInfoList) { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1111 | if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1112 | log.warn("Can't get server connect point, ignore"); |
| 1113 | continue; |
| 1114 | } |
| 1115 | DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList); |
| 1116 | if (newServerInfo == null) { |
| 1117 | log.warn("Can't get server interface with host info resolved, ignore"); |
| 1118 | continue; |
| 1119 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1120 | Interface serverInterface = getServerInterface(newServerInfo); |
| 1121 | if (serverInterface == null) { |
| 1122 | log.warn("Can't get server interface, ignore"); |
| 1123 | continue; |
| 1124 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1125 | Ethernet etherRouted = (Ethernet) clientPacket.clone(); |
| 1126 | MacAddress macFacingServer = serverInterface.mac(); |
| 1127 | if (macFacingServer == null) { |
| 1128 | log.warn("No MAC address for server Interface {}", serverInterface); |
| 1129 | return null; |
| 1130 | } |
| 1131 | etherRouted.setSourceMACAddress(macFacingServer); |
| 1132 | etherRouted.setDestinationMACAddress(newServerInfo.getDhcpConnectMac().get()); |
| 1133 | InternalPacket internalPacket = |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1134 | InternalPacket.internalPacket(etherRouted, |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1135 | serverInfo.getDhcpServerConnectPoint().get()); |
| 1136 | internalPackets.add(internalPacket); |
| 1137 | log.debug("Sending LQ to DHCP server {}", newServerInfo.getDhcpServerIp6()); |
| 1138 | } |
| 1139 | log.debug("num of client packets to send is{}", internalPackets.size()); |
| 1140 | |
| 1141 | return internalPackets; |
| 1142 | } |
| 1143 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1144 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1145 | * build the DHCP6 solicit/request packet with gatewayip. |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1146 | * |
| 1147 | * @param context packet context |
| 1148 | * @param clientPacket client ethernet packet |
| 1149 | * @param clientInterfaces set of client side interfaces |
| 1150 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1151 | private List<InternalPacket> processDhcp6PacketFromClient(PacketContext context, |
| 1152 | Ethernet clientPacket, |
| 1153 | Set<Interface> clientInterfaces) { |
Yi Tseng | 3bd57ac | 2017-11-29 14:39:18 -0800 | [diff] [blame] | 1154 | ConnectPoint receivedFrom = context.inPacket().receivedFrom(); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1155 | Ip6Address relayAgentIp = Dhcp6HandlerUtil.getRelayAgentIPv6Address(clientInterfaces); |
Yi Tseng | 3bd57ac | 2017-11-29 14:39:18 -0800 | [diff] [blame] | 1156 | MacAddress relayAgentMac = clientInterfaces.iterator().next().mac(); |
| 1157 | if (relayAgentIp == null || relayAgentMac == null) { |
| 1158 | log.warn("Missing DHCP relay agent interface Ipv6 addr config for " |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1159 | + "packet from client on port: {}. Aborting packet processing", |
| 1160 | clientInterfaces.iterator().next().connectPoint()); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1161 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Charles Chan | 239492c | 2018-01-22 13:27:28 -0800 | [diff] [blame] | 1162 | return Lists.newArrayList(); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1163 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1164 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1165 | IPv6 clientIpv6 = (IPv6) clientPacket.getPayload(); |
| 1166 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 1167 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
| 1168 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1169 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6); |
Charles Chan | a990ce9 | 2017-10-30 10:22:50 -0700 | [diff] [blame] | 1170 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1171 | ConnectPoint clientConnectionPoint = context.inPacket().receivedFrom(); |
| 1172 | VlanId vlanIdInUse = VlanId.vlanId(clientPacket.getVlanID()); |
| 1173 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1174 | .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1175 | .findFirst() |
| 1176 | .orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1177 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1178 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1179 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1180 | List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1181 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1182 | for (DhcpServerInfo serverInfo : copyServerInfoList) { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1183 | if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1184 | log.warn("Can't get server connect point, ignore"); |
| 1185 | continue; |
| 1186 | } |
| 1187 | DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList); |
| 1188 | if (newServerInfo == null) { |
| 1189 | log.warn("Can't get server interface with host info resolved, ignore"); |
| 1190 | continue; |
| 1191 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1192 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1193 | Interface serverInterface = getServerInterface(newServerInfo); |
| 1194 | if (serverInterface == null) { |
| 1195 | log.warn("Can't get server interface, ignore"); |
| 1196 | continue; |
| 1197 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1198 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1199 | Ethernet etherReply = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context, clientPacket, |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1200 | clientInterfaces, newServerInfo, serverInterface); |
| 1201 | removeHostOrRoute(directConnFlag, clientConnectionPoint, clientDhcp6, clientPacket, |
| 1202 | clientIpv6, clientInterface); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1203 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1204 | InternalPacket internalPacket = InternalPacket.internalPacket(etherReply, |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1205 | serverInfo.getDhcpServerConnectPoint().get()); |
| 1206 | internalPackets.add(internalPacket); |
| 1207 | } |
| 1208 | log.debug("num of client packets to send is{}", internalPackets.size()); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1209 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1210 | return internalPackets; |
| 1211 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1212 | |
| 1213 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1214 | * process the DHCP6 relay-reply packet from dhcp server. |
| 1215 | * |
| 1216 | * @param context packet context |
| 1217 | * @param receivedPacket server ethernet packet |
| 1218 | * @param recevingInterfaces set of server side interfaces |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1219 | * @return internalPacket toward client |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1220 | */ |
| 1221 | private InternalPacket processDhcp6PacketFromServer(PacketContext context, |
| 1222 | Ethernet receivedPacket, Set<Interface> recevingInterfaces) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1223 | // get dhcp6 header. |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 1224 | Ethernet etherReply = receivedPacket.duplicate(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1225 | IPv6 ipv6Packet = (IPv6) etherReply.getPayload(); |
| 1226 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
| 1227 | DHCP6 dhcp6Relay = (DHCP6) udpPacket.getPayload(); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1228 | Boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcp6Relay); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1229 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1230 | DHCP6 embeddedDhcp6 = dhcp6Relay.getOptions().stream() |
| 1231 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 1232 | .map(BasePacket::getPayload) |
| 1233 | .map(pld -> (DHCP6) pld) |
| 1234 | .findFirst() |
| 1235 | .orElse(null); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1236 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1237 | DhcpServerInfo foundServerInfo = findServerInfoFromServer(directConnFlag, inPort); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1238 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1239 | if (foundServerInfo == null) { |
| 1240 | log.warn("Cannot find server info"); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1241 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_INFO); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1242 | return null; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1243 | } else { |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1244 | if (Dhcp6HandlerUtil.isServerIpEmpty(foundServerInfo)) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1245 | log.warn("Cannot find server info's ipaddress"); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1246 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_IP6ADDR); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1247 | return null; |
| 1248 | } |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1251 | Dhcp6InterfaceIdOption interfaceIdOption = dhcp6Relay.getOptions().stream() |
| 1252 | .filter(opt -> opt instanceof Dhcp6InterfaceIdOption) |
| 1253 | .map(opt -> (Dhcp6InterfaceIdOption) opt) |
| 1254 | .findFirst() |
| 1255 | .orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1256 | if (interfaceIdOption == null) { |
| 1257 | log.warn("Interface Id option is not present, abort packet..."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1258 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.OPTION_MISSING_FAIL); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1259 | return null; |
| 1260 | } |
| 1261 | |
| 1262 | MacAddress peerMac = interfaceIdOption.getMacAddress(); |
| 1263 | String clientConnectionPointStr = new String(interfaceIdOption.getInPort()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1264 | ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1265 | VlanId vlanIdInUse = VlanId.vlanId(interfaceIdOption.getVlanId()); |
| 1266 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1267 | .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1268 | .findFirst().orElse(null); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1269 | if (clientInterface == null) { |
| 1270 | log.warn("Cannot get client interface for from packet, abort... vlan {}", vlanIdInUse.toString()); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1271 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_MATCHING_INTF); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1272 | return null; |
| 1273 | } |
Kalhee Kim | 44fbda1 | 2018-04-03 21:08:18 +0000 | [diff] [blame] | 1274 | etherReply.setVlanID(vlanIdInUse.toShort()); |
| 1275 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1276 | MacAddress relayAgentMac = clientInterface.mac(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1277 | if (relayAgentMac == null) { |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1278 | log.warn("Can not get client interface mac, abort packet.."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1279 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1280 | return null; |
| 1281 | } |
| 1282 | etherReply.setSourceMACAddress(relayAgentMac); |
| 1283 | |
| 1284 | // find destMac |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1285 | MacAddress clientMac; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1286 | Ip6Address peerAddress = Ip6Address.valueOf(dhcp6Relay.getPeerAddress()); |
| 1287 | Set<Host> clients = hostService.getHostsByIp(peerAddress); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1288 | if (clients.isEmpty()) { |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1289 | log.trace("There's no host found for this address {}", |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1290 | HexString.toHexString(dhcp6Relay.getPeerAddress(), ":")); |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1291 | log.trace("Let's look up interfaceId {}", HexString.toHexString(peerMac.toBytes(), ":")); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1292 | clientMac = peerMac; |
| 1293 | } else { |
| 1294 | clientMac = clients.iterator().next().mac(); |
| 1295 | if (clientMac == null) { |
| 1296 | log.warn("No client mac address found, abort packet..."); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1297 | dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1298 | return null; |
| 1299 | } |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1300 | log.trace("Client mac address found from getHostByIp"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1301 | } |
| 1302 | etherReply.setDestinationMACAddress(clientMac); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1303 | // ip header |
| 1304 | ipv6Packet.setSourceAddress(dhcp6Relay.getLinkAddress()); |
| 1305 | ipv6Packet.setDestinationAddress(dhcp6Relay.getPeerAddress()); |
| 1306 | // udp header |
| 1307 | udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 1308 | if (directConnFlag) { |
| 1309 | udpPacket.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT); |
| 1310 | } else { |
| 1311 | udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT); |
| 1312 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1313 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1314 | |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1315 | boolean hostOrRouteAllowed = learnRouteFromLeasequery || |
| 1316 | Dhcp6HandlerUtil.getDhcp6LeafMessageType(dhcp6Relay) != MsgType.LEASEQUERY_REPLY; |
| 1317 | log.debug("Can add host or route: {}", hostOrRouteAllowed); |
| 1318 | |
| 1319 | if (hostOrRouteAllowed) { |
| 1320 | // add host or route |
| 1321 | addHostOrRoute(directConnFlag, clientConnectionPoint, dhcp6Relay, embeddedDhcp6, |
| 1322 | clientMac, clientInterface); |
| 1323 | } |
| 1324 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1325 | udpPacket.setPayload(embeddedDhcp6); |
| 1326 | udpPacket.resetChecksum(); |
| 1327 | ipv6Packet.setPayload(udpPacket); |
| 1328 | etherReply.setPayload(ipv6Packet); |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1329 | return InternalPacket.internalPacket(etherReply, clientConnectionPoint); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1330 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1331 | |
| 1332 | @Override |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1333 | public void setDhcpFpmEnabled(Boolean enabled) { |
| 1334 | dhcpFpmEnabled = enabled; |
| 1335 | } |
| 1336 | |
| 1337 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1338 | public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1339 | log.debug("setDefaultDhcpServerConfigs is called."); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1340 | setDhcpServerConfigs(configs, defaultServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | @Override |
| 1344 | public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1345 | log.debug("setIndirectDhcpServerConfigs is called."); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1346 | setDhcpServerConfigs(configs, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1350 | log.debug("config size {}.", configs.size()); |
| 1351 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1352 | if (configs.size() == 0) { |
| 1353 | // no config to update |
| 1354 | return; |
| 1355 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1356 | // TODO: currently we pick up first DHCP server config. |
| 1357 | // Will use other server configs in the future for HA. |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1358 | Boolean isConfigValid = false; |
| 1359 | for (DhcpServerConfig serverConfig : configs) { |
| 1360 | if (serverConfig.getDhcpServerIp6().isPresent()) { |
| 1361 | isConfigValid = true; |
| 1362 | break; |
| 1363 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1364 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1365 | if (!isConfigValid) { |
| 1366 | log.warn("No IP V6 server address found."); |
| 1367 | return; // No IP V6 address found |
| 1368 | } |
| 1369 | for (DhcpServerInfo oldServerInfo : serverInfoList) { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1370 | // stop monitoring gateway or server |
| 1371 | oldServerInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 1372 | hostService.stopMonitoringIp(gatewayIp); |
| 1373 | }); |
| 1374 | oldServerInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 1375 | hostService.stopMonitoringIp(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1376 | cancelDhcpPacket(serverIp); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1377 | }); |
| 1378 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1379 | serverInfoList.clear(); |
| 1380 | for (DhcpServerConfig serverConfig : configs) { |
| 1381 | // Create new server info according to the config |
| 1382 | DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig, |
| 1383 | DhcpServerInfo.Version.DHCP_V6); |
| 1384 | checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(), |
| 1385 | "Connect point not exists"); |
| 1386 | checkState(newServerInfo.getDhcpServerIp6().isPresent(), |
| 1387 | "IP of DHCP server not exists"); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1388 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1389 | log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null)); |
| 1390 | log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp6().orElse(null)); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1391 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1392 | Ip6Address serverIp = newServerInfo.getDhcpServerIp6().get(); |
| 1393 | Ip6Address ipToProbe; |
| 1394 | if (newServerInfo.getDhcpGatewayIp6().isPresent()) { |
| 1395 | ipToProbe = newServerInfo.getDhcpGatewayIp6().get(); |
| 1396 | } else { |
| 1397 | ipToProbe = newServerInfo.getDhcpServerIp6().orElse(null); |
| 1398 | } |
| 1399 | String hostToProbe = newServerInfo.getDhcpGatewayIp6() |
| 1400 | .map(ip -> "gateway").orElse("server"); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1401 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1402 | log.warn("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
| 1403 | hostService.startMonitoringIp(ipToProbe); |
| 1404 | |
| 1405 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 1406 | if (!hosts.isEmpty()) { |
| 1407 | Host host = hosts.iterator().next(); |
| 1408 | newServerInfo.setDhcpConnectVlan(host.vlan()); |
| 1409 | newServerInfo.setDhcpConnectMac(host.mac()); |
| 1410 | log.warn("Host found host {}", host); |
| 1411 | |
| 1412 | } else { |
| 1413 | log.warn("No host found host ip {}", ipToProbe); |
| 1414 | } |
| 1415 | // Add new server info |
| 1416 | synchronized (this) { |
| 1417 | serverInfoList.add(newServerInfo); |
| 1418 | } |
| 1419 | if (!hosts.isEmpty()) { |
| 1420 | requestDhcpPacket(serverIp); |
| 1421 | } |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1422 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | class InternalHostListener implements HostListener { |
| 1426 | @Override |
| 1427 | public void event(HostEvent event) { |
| 1428 | switch (event.type()) { |
| 1429 | case HOST_ADDED: |
| 1430 | case HOST_UPDATED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 1431 | log.trace("Scheduled host event {}", event); |
| 1432 | hostEventExecutor.execute(() -> hostUpdated(event.subject())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1433 | break; |
| 1434 | case HOST_REMOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 1435 | log.trace("Scheduled host event {}", event); |
| 1436 | hostEventExecutor.execute(() -> hostRemoved(event.subject())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1437 | break; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1438 | default: |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1445 | * Handle host updated. |
| 1446 | * If the host is DHCP server or gateway, update connect mac and vlan. |
| 1447 | * |
| 1448 | * @param host the host |
| 1449 | */ |
| 1450 | private void hostUpdated(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1451 | hostUpdated(host, defaultServerInfoList); |
| 1452 | hostUpdated(host, indirectServerInfoList); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1455 | private void hostUpdated(Host host, List<DhcpServerInfo> serverInfoList) { |
| 1456 | DhcpServerInfo serverInfo; |
| 1457 | Ip6Address targetIp; |
| 1458 | if (!serverInfoList.isEmpty()) { |
| 1459 | serverInfo = serverInfoList.get(0); |
| 1460 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1461 | targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1462 | |
| 1463 | if (targetIp == null) { |
| 1464 | targetIp = serverIp; |
| 1465 | } |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1466 | if (targetIp != null) { |
| 1467 | if (host.ipAddresses().contains(targetIp)) { |
| 1468 | serverInfo.setDhcpConnectMac(host.mac()); |
| 1469 | serverInfo.setDhcpConnectVlan(host.vlan()); |
| 1470 | requestDhcpPacket(serverIp); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1475 | /** |
| 1476 | * Handle host removed. |
| 1477 | * If the host is DHCP server or gateway, unset connect mac and vlan. |
| 1478 | * |
| 1479 | * @param host the host |
| 1480 | */ |
| 1481 | private void hostRemoved(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1482 | hostRemoved(host, defaultServerInfoList); |
| 1483 | hostRemoved(host, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1486 | private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) { |
| 1487 | DhcpServerInfo serverInfo; |
| 1488 | Ip6Address targetIp; |
| 1489 | |
| 1490 | if (!serverInfoList.isEmpty()) { |
| 1491 | serverInfo = serverInfoList.get(0); |
| 1492 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1493 | targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1494 | |
| 1495 | if (targetIp == null) { |
| 1496 | targetIp = serverIp; |
| 1497 | } |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1498 | if (targetIp != null) { |
| 1499 | if (host.ipAddresses().contains(targetIp)) { |
| 1500 | serverInfo.setDhcpConnectVlan(null); |
| 1501 | serverInfo.setDhcpConnectMac(null); |
| 1502 | cancelDhcpPacket(serverIp); |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1508 | /** |
| 1509 | * Gets Interface facing to the server for default host. |
| 1510 | * |
| 1511 | * @return the Interface facing to the server; null if not found |
| 1512 | */ |
| 1513 | private Interface getServerInterface() { |
| 1514 | DhcpServerInfo serverInfo; |
| 1515 | ConnectPoint dhcpServerConnectPoint; |
| 1516 | VlanId dhcpConnectVlan; |
| 1517 | |
| 1518 | if (!defaultServerInfoList.isEmpty()) { |
| 1519 | serverInfo = defaultServerInfoList.get(0); |
| 1520 | dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1521 | dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1522 | } else { |
| 1523 | return null; |
| 1524 | } |
| 1525 | if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) { |
| 1526 | log.info("Default DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6()); |
| 1527 | return null; |
| 1528 | } |
| 1529 | return interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 1530 | .stream() |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1531 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan)) |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1532 | .findFirst() |
| 1533 | .orElse(null); |
| 1534 | } |
| 1535 | |
| 1536 | /** |
| 1537 | * Gets Interface facing to the server for indirect hosts. |
| 1538 | * Use default server Interface if indirect server not configured. |
| 1539 | * |
| 1540 | * @return the Interface facing to the server; null if not found |
| 1541 | */ |
| 1542 | private Interface getIndirectServerInterface() { |
| 1543 | DhcpServerInfo serverInfo; |
| 1544 | |
| 1545 | ConnectPoint indirectDhcpServerConnectPoint; |
| 1546 | VlanId indirectDhcpConnectVlan; |
| 1547 | |
| 1548 | if (!indirectServerInfoList.isEmpty()) { |
| 1549 | serverInfo = indirectServerInfoList.get(0); |
| 1550 | indirectDhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1551 | indirectDhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1552 | } else { |
| 1553 | return getServerInterface(); |
| 1554 | } |
| 1555 | if (indirectDhcpServerConnectPoint == null || indirectDhcpConnectVlan == null) { |
| 1556 | log.info("Indirect DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6()); |
| 1557 | return null; |
| 1558 | } |
| 1559 | return interfaceService.getInterfacesByPort(indirectDhcpServerConnectPoint) |
| 1560 | .stream() |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1561 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, indirectDhcpConnectVlan)) |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1562 | .findFirst() |
| 1563 | .orElse(null); |
| 1564 | } |
| 1565 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1566 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1567 | * Checks if serverInfo's host info (mac and vlan) is filled in; if not, fills in. |
| 1568 | * |
| 1569 | * @param serverInfo server information |
| 1570 | * @return newServerInfo if host info can be either found or filled in. |
| 1571 | */ |
| 1572 | private DhcpServerInfo getHostInfoForServerInfo(DhcpServerInfo serverInfo, List<DhcpServerInfo> sererInfoList) { |
| 1573 | DhcpServerInfo newServerInfo = null; |
| 1574 | MacAddress dhcpServerConnectMac = serverInfo.getDhcpConnectMac().orElse(null); |
| 1575 | VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1576 | ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1577 | |
| 1578 | if (dhcpServerConnectMac != null && dhcpConnectVlan != null) { |
| 1579 | newServerInfo = serverInfo; |
Charles Chan | 750c999 | 2018-02-26 10:44:49 -0800 | [diff] [blame] | 1580 | log.debug("DHCP server {} host info found. ConnectPt{} Mac {} vlan {}", serverInfo.getDhcpServerIp6(), |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1581 | dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan); |
| 1582 | } else { |
| 1583 | log.warn("DHCP server {} not resolve yet connectPt {} mac {} vlan {}", serverInfo.getDhcpServerIp6(), |
| 1584 | dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan); |
| 1585 | |
| 1586 | Ip6Address ipToProbe; |
| 1587 | if (serverInfo.getDhcpGatewayIp6().isPresent()) { |
| 1588 | ipToProbe = serverInfo.getDhcpGatewayIp6().get(); |
| 1589 | } else { |
| 1590 | ipToProbe = serverInfo.getDhcpServerIp6().orElse(null); |
| 1591 | } |
| 1592 | String hostToProbe = serverInfo.getDhcpGatewayIp6() |
| 1593 | .map(ip -> "gateway").orElse("server"); |
| 1594 | |
| 1595 | log.info("Dynamically probing to resolve {} IP {}", hostToProbe, ipToProbe); |
| 1596 | hostService.startMonitoringIp(ipToProbe); |
| 1597 | |
| 1598 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 1599 | if (!hosts.isEmpty()) { |
| 1600 | int serverInfoIndex = sererInfoList.indexOf(serverInfo); |
| 1601 | Host host = hosts.iterator().next(); |
| 1602 | serverInfo.setDhcpConnectVlan(host.vlan()); |
| 1603 | serverInfo.setDhcpConnectMac(host.mac()); |
| 1604 | // replace the serverInfo in the list |
| 1605 | sererInfoList.set(serverInfoIndex, serverInfo); |
| 1606 | newServerInfo = serverInfo; |
| 1607 | log.warn("Dynamically host found host {}", host); |
| 1608 | } else { |
| 1609 | log.warn("No host found host ip {} dynamically", ipToProbe); |
| 1610 | } |
| 1611 | } |
| 1612 | return newServerInfo; |
| 1613 | } |
| 1614 | |
| 1615 | /** |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1616 | * Gets Interface facing to the server for default host. |
| 1617 | * |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1618 | * @param serverInfo server information |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1619 | * @return the Interface facing to the server; null if not found |
| 1620 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1621 | private Interface getServerInterface(DhcpServerInfo serverInfo) { |
| 1622 | Interface serverInterface = null; |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 1623 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1624 | ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1625 | VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1626 | |
| 1627 | if (dhcpServerConnectPoint != null && dhcpConnectVlan != null) { |
| 1628 | serverInterface = interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 1629 | .stream() |
Taras Lemkin | 4178591 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 1630 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1631 | .findFirst() |
| 1632 | .orElse(null); |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 1633 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1634 | log.warn("DHCP server {} not resolve yet connectPoint {} vlan {}", serverInfo.getDhcpServerIp6(), |
| 1635 | dhcpServerConnectPoint, dhcpConnectVlan); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1636 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1637 | |
| 1638 | return serverInterface; |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1641 | private void requestDhcpPacket(Ip6Address serverIp) { |
| 1642 | requestServerDhcpPacket(serverIp); |
| 1643 | requestClientDhcpPacket(serverIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1644 | requestServerLQPacket(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | private void cancelDhcpPacket(Ip6Address serverIp) { |
| 1648 | cancelServerDhcpPacket(serverIp); |
| 1649 | cancelClientDhcpPacket(serverIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1650 | cancelServerLQPacket(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | private void cancelServerDhcpPacket(Ip6Address serverIp) { |
| 1654 | TrafficSelector serverSelector = |
| 1655 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1656 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1657 | .build(); |
| 1658 | packetService.cancelPackets(serverSelector, |
| 1659 | PacketPriority.CONTROL, |
| 1660 | appId); |
| 1661 | } |
| 1662 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1663 | private void cancelServerLQPacket(Ip6Address serverIp) { |
| 1664 | TrafficSelector serverSelector = |
| 1665 | DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR) |
| 1666 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1667 | .build(); |
| 1668 | packetService.cancelPackets(serverSelector, |
| 1669 | PacketPriority.CONTROL, |
| 1670 | appId); |
| 1671 | } |
| 1672 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1673 | private void requestServerDhcpPacket(Ip6Address serverIp) { |
| 1674 | TrafficSelector serverSelector = |
| 1675 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1676 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1677 | .build(); |
| 1678 | packetService.requestPackets(serverSelector, |
| 1679 | PacketPriority.CONTROL, |
| 1680 | appId); |
| 1681 | } |
| 1682 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1683 | private void requestServerLQPacket(Ip6Address serverIp) { |
| 1684 | TrafficSelector serverSelector = |
| 1685 | DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR) |
| 1686 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1687 | .build(); |
| 1688 | packetService.requestPackets(serverSelector, |
| 1689 | PacketPriority.CONTROL, |
| 1690 | appId); |
| 1691 | } |
| 1692 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1693 | private void cancelClientDhcpPacket(Ip6Address serverIp) { |
| 1694 | // Packet comes from relay |
| 1695 | TrafficSelector indirectClientSelector = |
| 1696 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1697 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1698 | .build(); |
| 1699 | packetService.cancelPackets(indirectClientSelector, |
| 1700 | PacketPriority.CONTROL, |
| 1701 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1702 | indirectClientSelector = |
| 1703 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1704 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1705 | .build(); |
| 1706 | packetService.cancelPackets(indirectClientSelector, |
| 1707 | PacketPriority.CONTROL, |
| 1708 | appId); |
| 1709 | indirectClientSelector = |
| 1710 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1711 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1712 | .build(); |
| 1713 | packetService.cancelPackets(indirectClientSelector, |
| 1714 | PacketPriority.CONTROL, |
| 1715 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1716 | |
| 1717 | // Packet comes from client |
| 1718 | packetService.cancelPackets(CLIENT_SERVER_SELECTOR, |
| 1719 | PacketPriority.CONTROL, |
| 1720 | appId); |
| 1721 | } |
| 1722 | |
| 1723 | private void requestClientDhcpPacket(Ip6Address serverIp) { |
| 1724 | // Packet comes from relay |
| 1725 | TrafficSelector indirectClientSelector = |
| 1726 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1727 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1728 | .build(); |
| 1729 | packetService.requestPackets(indirectClientSelector, |
| 1730 | PacketPriority.CONTROL, |
| 1731 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1732 | indirectClientSelector = |
| 1733 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1734 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1735 | .build(); |
| 1736 | packetService.requestPackets(indirectClientSelector, |
| 1737 | PacketPriority.CONTROL, |
| 1738 | appId); |
| 1739 | indirectClientSelector = |
| 1740 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1741 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1742 | .build(); |
| 1743 | packetService.requestPackets(indirectClientSelector, |
| 1744 | PacketPriority.CONTROL, |
| 1745 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1746 | |
| 1747 | // Packet comes from client |
| 1748 | packetService.requestPackets(CLIENT_SERVER_SELECTOR, |
| 1749 | PacketPriority.CONTROL, |
| 1750 | appId); |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * Process the ignore rules. |
| 1755 | * |
| 1756 | * @param deviceId the device id |
| 1757 | * @param vlanId the vlan to be ignored |
| 1758 | * @param op the operation, ADD to install; REMOVE to uninstall rules |
| 1759 | */ |
| 1760 | private void processIgnoreVlanRule(DeviceId deviceId, VlanId vlanId, Objective.Operation op) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1761 | AtomicInteger installedCount = new AtomicInteger(DHCP_SELECTORS.size()); |
| 1762 | DHCP_SELECTORS.forEach(trafficSelector -> { |
| 1763 | TrafficSelector selector = DefaultTrafficSelector.builder(trafficSelector) |
| 1764 | .matchVlanId(vlanId) |
| 1765 | .build(); |
| 1766 | |
| 1767 | ForwardingObjective.Builder builder = DefaultForwardingObjective.builder() |
| 1768 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1769 | .withSelector(selector) |
| 1770 | .withPriority(IGNORE_CONTROL_PRIORITY) |
Yi Tseng | dbabeed | 2017-11-29 10:49:20 -0800 | [diff] [blame] | 1771 | .withTreatment(DefaultTrafficTreatment.emptyTreatment()) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1772 | .fromApp(appId); |
| 1773 | |
| 1774 | |
| 1775 | ObjectiveContext objectiveContext = new ObjectiveContext() { |
| 1776 | @Override |
| 1777 | public void onSuccess(Objective objective) { |
| 1778 | log.info("Ignore rule {} (Vlan id {}, device {}, selector {})", |
| 1779 | op, vlanId, deviceId, selector); |
| 1780 | int countDown = installedCount.decrementAndGet(); |
| 1781 | if (countDown != 0) { |
| 1782 | return; |
| 1783 | } |
| 1784 | switch (op) { |
| 1785 | case ADD: |
| 1786 | ignoredVlans.put(deviceId, vlanId); |
| 1787 | break; |
| 1788 | case REMOVE: |
| 1789 | ignoredVlans.remove(deviceId, vlanId); |
| 1790 | break; |
| 1791 | default: |
| 1792 | log.warn("Unsupported objective operation {}", op); |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | @Override |
| 1798 | public void onError(Objective objective, ObjectiveError error) { |
| 1799 | log.warn("Can't {} ignore rule (vlan id {}, selector {}, device {}) due to {}", |
| 1800 | op, vlanId, selector, deviceId, error); |
| 1801 | } |
| 1802 | }; |
| 1803 | |
| 1804 | ForwardingObjective fwd; |
| 1805 | switch (op) { |
| 1806 | case ADD: |
| 1807 | fwd = builder.add(objectiveContext); |
| 1808 | break; |
| 1809 | case REMOVE: |
| 1810 | fwd = builder.remove(objectiveContext); |
| 1811 | break; |
| 1812 | default: |
| 1813 | log.warn("Unsupported objective operation {}", op); |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | Device device = deviceService.getDevice(deviceId); |
| 1818 | if (device == null || !device.is(Pipeliner.class)) { |
| 1819 | log.warn("Device {} is not available now, wait until device is available", deviceId); |
| 1820 | return; |
| 1821 | } |
| 1822 | flowObjectiveService.apply(deviceId, fwd); |
| 1823 | }); |
| 1824 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1825 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1826 | /** |
| 1827 | * Find first ipaddress for a given Host info i.e. mac and vlan. |
| 1828 | * |
| 1829 | * @param clientMac client mac |
| 1830 | * @param vlanId packet's vlan |
| 1831 | * @return next-hop link-local ipaddress for a given host |
| 1832 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1833 | private IpAddress getFirstIpByHost(Boolean directConnFlag, MacAddress clientMac, VlanId vlanId) { |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1834 | IpAddress nextHopIp; |
| 1835 | // pick out the first link-local ip address |
| 1836 | HostId gwHostId = HostId.hostId(clientMac, vlanId); |
| 1837 | Host gwHost = hostService.getHost(gwHostId); |
| 1838 | if (gwHost == null) { |
| 1839 | log.warn("Can't find gateway host for hostId {}", gwHostId); |
| 1840 | return null; |
| 1841 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1842 | if (directConnFlag) { |
| 1843 | nextHopIp = gwHost.ipAddresses() |
| 1844 | .stream() |
| 1845 | .filter(IpAddress::isIp6) |
| 1846 | .map(IpAddress::getIp6Address) |
| 1847 | .findFirst() |
| 1848 | .orElse(null); |
| 1849 | } else { |
| 1850 | nextHopIp = gwHost.ipAddresses() |
| 1851 | .stream() |
| 1852 | .filter(IpAddress::isIp6) |
| 1853 | .filter(ip6 -> ip6.isLinkLocal()) |
| 1854 | .map(IpAddress::getIp6Address) |
| 1855 | .findFirst() |
| 1856 | .orElse(null); |
| 1857 | } |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1858 | return nextHopIp; |
| 1859 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1860 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1861 | private List<DhcpServerInfo> findValidServerInfo(boolean directConnFlag) { |
| 1862 | List<DhcpServerInfo> validServerInfo; |
| 1863 | |
| 1864 | if (directConnFlag || indirectServerInfoList.isEmpty()) { |
| 1865 | validServerInfo = new ArrayList<DhcpServerInfo>(defaultServerInfoList); |
| 1866 | } else { |
| 1867 | validServerInfo = new ArrayList<DhcpServerInfo>(indirectServerInfoList); |
| 1868 | } |
| 1869 | return validServerInfo; |
| 1870 | } |
| 1871 | |
| 1872 | private DhcpServerInfo findServerInfoFromServer(boolean directConnFlag, ConnectPoint inPort) { |
| 1873 | List<DhcpServerInfo> validServerInfoList = findValidServerInfo(directConnFlag); |
| 1874 | DhcpServerInfo foundServerInfo = null; |
| 1875 | for (DhcpServerInfo serverInfo : validServerInfoList) { |
| 1876 | if (inPort.equals(serverInfo.getDhcpServerConnectPoint().get())) { |
| 1877 | foundServerInfo = serverInfo; |
Charles Chan | 750c999 | 2018-02-26 10:44:49 -0800 | [diff] [blame] | 1878 | log.debug("ServerInfo found for Rcv port {} Server Connect Point {} for {}", |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1879 | inPort, serverInfo.getDhcpServerConnectPoint(), directConnFlag ? "direct" : "indirect"); |
| 1880 | break; |
| 1881 | } else { |
| 1882 | log.warn("Rcv port {} not the same as Server Connect Point {} for {}", |
| 1883 | inPort, serverInfo.getDhcpServerConnectPoint(), directConnFlag ? "direct" : "indirect"); |
| 1884 | } |
| 1885 | } |
| 1886 | return foundServerInfo; |
| 1887 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1888 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1889 | /** |
| 1890 | * Set the dhcp6 lease expiry poll interval value. |
| 1891 | * |
| 1892 | * @param val poll interval value in seconds |
| 1893 | */ |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1894 | @Override |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1895 | public void setDhcp6PollInterval(int val) { |
| 1896 | dhcp6PollInterval = val; |
| 1897 | } |
| 1898 | |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1899 | /** |
| 1900 | * get the dhcp6 lease expiry poll interval value. |
| 1901 | * This is a private function |
| 1902 | * @return poll interval value in seconds |
| 1903 | */ |
| 1904 | private int getDhcp6PollInterval() { |
| 1905 | return dhcp6PollInterval; |
| 1906 | } |
| 1907 | |
| 1908 | /** |
| 1909 | * Find lease-expired ipaddresses and pd prefixes. |
| 1910 | * Removing host/route/fpm entries. |
| 1911 | */ |
| 1912 | public void timeTick() { |
| 1913 | long currentTime = System.currentTimeMillis(); |
| 1914 | Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords(); |
| 1915 | |
| 1916 | log.debug("timeTick called currenttime {} records num {} ", currentTime, records.size()); |
| 1917 | |
| 1918 | records.forEach(record -> { |
| 1919 | boolean addrOrPdRemoved = false; |
| 1920 | DHCP6.MsgType ip6Status = record.ip6Status().orElse(null); |
| 1921 | if (ip6Status == null) { |
| 1922 | log.debug("record is not valid v6 record."); |
| 1923 | return; |
| 1924 | } |
| 1925 | |
| 1926 | if ((currentTime - record.getLastIp6Update()) > |
| 1927 | ((record.addrPrefTime() + getDhcp6PollInterval() / 2) * 1000)) { |
| 1928 | // remove ipaddress from host/route table |
| 1929 | IpAddress ip = record.ip6Address().orElse(null); |
| 1930 | if (ip != null) { |
| 1931 | if (record.directlyConnected()) { |
| 1932 | providerService.removeIpFromHost(HostId.hostId(record.macAddress(), |
| 1933 | record.vlanId()), ip); |
| 1934 | } else { |
| 1935 | MacAddress gwMac = record.nextHop().orElse(null); |
| 1936 | if (gwMac == null) { |
| 1937 | log.warn("Can't find gateway mac address from record {} for ip6Addr", record); |
| 1938 | return; |
| 1939 | } |
| 1940 | IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(), |
| 1941 | gwMac, |
| 1942 | record.vlanId()); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1943 | Route route = new Route(Route.Source.DHCP, ip.toIpPrefix(), nextHopIp); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1944 | routeStore.removeRoute(route); |
| 1945 | } |
| 1946 | record.updateAddrPrefTime(0); |
| 1947 | record.ip6Address(null); |
| 1948 | addrOrPdRemoved = true; |
| 1949 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(), |
| 1950 | record.vlanId()), record); |
| 1951 | log.warn("IP6 address is set to null. delta {} lastUpdate {} addrPrefTime {}", |
| 1952 | (currentTime - record.getLastIp6Update()), record.getLastIp6Update(), |
| 1953 | record.addrPrefTime()); |
| 1954 | } |
| 1955 | } |
| 1956 | if ((currentTime - record.getLastPdUpdate()) > |
| 1957 | ((record.pdPrefTime() + getDhcp6PollInterval() / 2) * 1000)) { |
| 1958 | // remove PD from route/fpm table |
| 1959 | IpPrefix pdIpPrefix = record.pdPrefix().orElse(null); |
| 1960 | if (pdIpPrefix != null) { |
| 1961 | if (record.directlyConnected()) { |
| 1962 | providerService.removeIpFromHost(HostId.hostId(record.macAddress(), record.vlanId()), |
| 1963 | pdIpPrefix.address().getIp6Address()); |
| 1964 | } else { |
| 1965 | MacAddress gwMac = record.nextHop().orElse(null); |
| 1966 | if (gwMac == null) { |
| 1967 | log.warn("Can't find gateway mac address from record {} for PD prefix", record); |
| 1968 | return; |
| 1969 | } |
| 1970 | IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(), |
| 1971 | gwMac, |
| 1972 | record.vlanId()); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1973 | Route route = new Route(Route.Source.DHCP, pdIpPrefix, nextHopIp); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1974 | routeStore.removeRoute(route); |
| 1975 | if (this.dhcpFpmEnabled) { |
| 1976 | dhcpFpmPrefixStore.removeFpmRecord(pdIpPrefix); |
| 1977 | } |
| 1978 | } |
| 1979 | record.updatePdPrefTime(0); |
| 1980 | record.pdPrefix(null); |
| 1981 | addrOrPdRemoved = true; |
| 1982 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(), |
| 1983 | record.vlanId()), record); |
| 1984 | log.warn("PD prefix is set to null.delta {} pdPrefTime {}", |
| 1985 | (currentTime - record.getLastPdUpdate()), record.pdPrefTime()); |
| 1986 | } |
| 1987 | } |
| 1988 | if (addrOrPdRemoved && |
| 1989 | !record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) { |
| 1990 | log.warn("ip6Status {} IP6 address and IP6 PD both are null. Remove record.", ip6Status); |
| 1991 | dhcpRelayStore.removeDhcpRecord(HostId.hostId(record.macAddress(), record.vlanId())); |
| 1992 | } |
| 1993 | } |
| 1994 | ); |
| 1995 | } |
| 1996 | } |