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