Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [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 | |
| 17 | package org.onosproject.ra; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Service; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Deactivate; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Property; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.EthType; |
| 28 | import org.onlab.packet.Ethernet; |
| 29 | import org.onlab.packet.ICMP6; |
| 30 | import org.onlab.packet.IPv6; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 31 | import org.onlab.packet.Ip6Address; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 34 | import org.onlab.packet.ndp.NeighborDiscoveryOptions; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 35 | import org.onlab.packet.ndp.RouterAdvertisement; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.mastership.MastershipService; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 42 | import org.onosproject.net.MastershipRole; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigEvent; |
| 45 | import org.onosproject.net.config.NetworkConfigListener; |
| 46 | import org.onosproject.net.config.NetworkConfigRegistry; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.InterfaceConfig; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 48 | import org.onosproject.net.config.basics.SubjectFactories; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceEvent; |
| 50 | import org.onosproject.net.device.DeviceListener; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 51 | import org.onosproject.net.device.DeviceService; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 52 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.host.InterfaceIpAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 55 | import org.onosproject.net.intf.Interface; |
| 56 | import org.onosproject.net.intf.InterfaceEvent; |
| 57 | import org.onosproject.net.intf.InterfaceListener; |
| 58 | import org.onosproject.net.intf.InterfaceService; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 59 | import org.onosproject.net.packet.DefaultOutboundPacket; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 60 | import org.onosproject.net.packet.InboundPacket; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 61 | import org.onosproject.net.packet.OutboundPacket; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 62 | import org.onosproject.net.packet.PacketContext; |
| 63 | import org.onosproject.net.packet.PacketProcessor; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 64 | import org.onosproject.net.packet.PacketService; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 65 | import org.onosproject.ra.config.RouterAdvertisementDeviceConfig; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 66 | import org.osgi.service.component.ComponentContext; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | import org.slf4j.LoggerFactory; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 69 | |
| 70 | import javax.annotation.concurrent.GuardedBy; |
| 71 | import java.nio.ByteBuffer; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 72 | import java.util.AbstractMap; |
| 73 | import java.util.Arrays; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 74 | import java.util.Dictionary; |
| 75 | import java.util.LinkedHashMap; |
| 76 | import java.util.List; |
| 77 | import java.util.Map; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 78 | import java.util.Optional; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 79 | import java.util.Set; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 80 | import java.util.concurrent.CompletableFuture; |
| 81 | import java.util.concurrent.Executors; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 82 | import java.util.concurrent.ScheduledExecutorService; |
| 83 | import java.util.concurrent.ScheduledFuture; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 84 | import java.util.concurrent.TimeUnit; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 85 | import java.util.function.Function; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 86 | import java.util.stream.IntStream; |
| 87 | |
| 88 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 89 | import static org.onlab.util.Tools.get; |
| 90 | import static org.onlab.util.Tools.groupedThreads; |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 91 | import com.google.common.collect.ImmutableMap; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Manages IPv6 Router Advertisements. |
| 95 | */ |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 96 | @Service |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 97 | @Component(immediate = true) |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 98 | public class RouterAdvertisementManager implements RoutingAdvertisementService { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 99 | |
| 100 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 101 | private static final String PROP_RA_THREADS_POOL = "raPoolSize"; |
| 102 | private static final int DEFAULT_RA_THREADS_POOL_SIZE = 10; |
| 103 | private static final String PROP_RA_THREADS_DELAY = "raThreadDelay"; |
| 104 | private static final int DEFAULT_RA_THREADS_DELAY = 5; |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 105 | private static final String PROP_RA_FLAG_MBIT_STATUS = "raFlagMbitStatus"; |
| 106 | private static final boolean DEFAULT_RA_FLAG_MBIT_STATUS = false; |
| 107 | private static final String PROP_RA_FLAG_OBIT_STATUS = "raFlagObitStatus"; |
| 108 | private static final boolean DEFAULT_RA_FLAG_OBIT_STATUS = false; |
| 109 | private static final String PROP_RA_OPTION_PREFIX_STATUS = "raOptionPrefixStatus"; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 110 | private static final boolean DEFAULT_RA_OPTION_PREFIX_STATUS = false; |
| 111 | private static final String PROP_RA_GLOBAL_PREFIX_CONF_STATUS = "raGlobalPrefixConfStatus"; |
| 112 | private static final boolean DEFAULT_RA_GLOBAL_PREFIX_CONF_STATUS = true; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | PacketService packetService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected ComponentConfigService componentConfigService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | public InterfaceService interfaceService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | public MastershipService mastershipService; |
| 128 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 130 | protected NetworkConfigRegistry networkConfigRegistry; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 133 | protected DeviceService deviceService; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 134 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 135 | @Property(name = PROP_RA_THREADS_POOL, intValue = DEFAULT_RA_THREADS_POOL_SIZE, |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 136 | label = "Thread pool capacity") |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 137 | protected int raPoolSize = DEFAULT_RA_THREADS_POOL_SIZE; |
| 138 | |
| 139 | @Property(name = PROP_RA_THREADS_DELAY, intValue = DEFAULT_RA_THREADS_DELAY, |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 140 | label = "Thread delay in seconds") |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 141 | protected int raThreadDelay = DEFAULT_RA_THREADS_DELAY; |
| 142 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 143 | @Property(name = PROP_RA_FLAG_MBIT_STATUS, boolValue = DEFAULT_RA_FLAG_MBIT_STATUS, |
| 144 | label = "Turn M-bit flag on/off") |
| 145 | protected boolean raFlagMbitStatus = DEFAULT_RA_FLAG_MBIT_STATUS; |
| 146 | |
| 147 | @Property(name = PROP_RA_FLAG_OBIT_STATUS, boolValue = DEFAULT_RA_FLAG_OBIT_STATUS, |
| 148 | label = "Turn O-bit flag on/off") |
| 149 | protected boolean raFlagObitStatus = DEFAULT_RA_FLAG_OBIT_STATUS; |
| 150 | |
| 151 | @Property(name = PROP_RA_OPTION_PREFIX_STATUS, boolValue = DEFAULT_RA_OPTION_PREFIX_STATUS, |
| 152 | label = "Prefix option support needed or not") |
| 153 | protected boolean raOptionPrefixStatus = DEFAULT_RA_OPTION_PREFIX_STATUS; |
| 154 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 155 | @Property(name = PROP_RA_GLOBAL_PREFIX_CONF_STATUS, boolValue = DEFAULT_RA_GLOBAL_PREFIX_CONF_STATUS, |
| 156 | label = "Global prefix configuration support on/off") |
| 157 | protected boolean raGlobalConfigStatus = DEFAULT_RA_GLOBAL_PREFIX_CONF_STATUS; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 158 | |
| 159 | @GuardedBy(value = "this") |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 160 | private final Map<ConnectPoint, Map.Entry<ScheduledFuture<?>, List<InterfaceIpAddress>>> transmitters = |
| 161 | new LinkedHashMap<>(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 162 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 163 | @GuardedBy(value = "this") |
| 164 | private final Map<DeviceId, List<InterfaceIpAddress>> globalPrefixes = new LinkedHashMap<>(); |
| 165 | |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 166 | @Override |
| 167 | public ImmutableMap<DeviceId, List<InterfaceIpAddress>> getGlobalPrefixes() { |
| 168 | return ImmutableMap.copyOf(globalPrefixes); |
| 169 | } |
| 170 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 171 | private Function<Interface, Map.Entry<ConnectPoint, List<InterfaceIpAddress>>> prefixGenerator = |
| 172 | i -> { |
| 173 | Map.Entry<ConnectPoint, List<InterfaceIpAddress>> prefixEntry; |
| 174 | if (raGlobalConfigStatus && globalPrefixes.containsKey(i.connectPoint().deviceId())) { |
| 175 | prefixEntry = new AbstractMap.SimpleEntry<>(i.connectPoint(), |
| 176 | globalPrefixes.get(i.connectPoint().deviceId())); |
| 177 | } else { |
| 178 | prefixEntry = new AbstractMap.SimpleEntry<>(i.connectPoint(), i.ipAddressesList()); |
| 179 | } |
| 180 | return prefixEntry; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 181 | }; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 182 | |
| 183 | private ScheduledExecutorService executors = null; |
| 184 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 185 | private static final String APP_NAME = "org.onosproject.routeradvertisement"; |
| 186 | private ApplicationId appId; |
| 187 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 188 | private final ConfigFactory<DeviceId, RouterAdvertisementDeviceConfig> deviceConfigFactory = |
| 189 | new ConfigFactory<DeviceId, RouterAdvertisementDeviceConfig>( |
| 190 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 191 | RouterAdvertisementDeviceConfig.class, "routeradvertisement") { |
| 192 | @Override |
| 193 | public RouterAdvertisementDeviceConfig createConfig() { |
| 194 | |
| 195 | return new RouterAdvertisementDeviceConfig(); |
| 196 | } |
| 197 | }; |
| 198 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 199 | // Listener for handling dynamic interface modifications. |
| 200 | private class InternalInterfaceListener implements InterfaceListener { |
| 201 | @Override |
| 202 | public void event(InterfaceEvent event) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 203 | switch (event.type()) { |
| 204 | case INTERFACE_ADDED: |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 205 | case INTERFACE_UPDATED: |
| 206 | clearTxWorkers(); |
| 207 | loadGlobalPrefixConfig(); |
| 208 | setupTxWorkers(); |
| 209 | log.info("Configuration updated for {}", event.subject()); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 210 | break; |
| 211 | case INTERFACE_REMOVED: |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 212 | Interface i = event.subject(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 213 | if (mastershipService.getLocalRole(i.connectPoint().deviceId()) |
| 214 | == MastershipRole.MASTER) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 215 | deactivateRouterAdvertisement(i.connectPoint()); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 216 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 217 | break; |
| 218 | default: |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 222 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 223 | private final InterfaceListener interfaceListener = new InternalInterfaceListener(); |
| 224 | |
| 225 | // Enables RA threads on 'connectPoint' with configured IPv6s |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 226 | private synchronized void activateRouterAdvertisement(ConnectPoint connectPoint, |
| 227 | List<InterfaceIpAddress> addresses) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 228 | RAWorkerThread worker = new RAWorkerThread(connectPoint, addresses, raThreadDelay, null, null); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 229 | ScheduledFuture<?> handler = executors.scheduleAtFixedRate(worker, raThreadDelay, |
| 230 | raThreadDelay, TimeUnit.SECONDS); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 231 | transmitters.put(connectPoint, new AbstractMap.SimpleEntry<>(handler, addresses)); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Disables already activated RA threads on 'connectPoint' |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 235 | private synchronized List<InterfaceIpAddress> deactivateRouterAdvertisement(ConnectPoint connectPoint) { |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 236 | if (connectPoint != null) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 237 | Map.Entry<ScheduledFuture<?>, List<InterfaceIpAddress>> details = transmitters.get(connectPoint); |
| 238 | details.getKey().cancel(false); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 239 | transmitters.remove(connectPoint); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 240 | return details.getValue(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 241 | } |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 242 | return null; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 243 | } |
| 244 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 245 | private synchronized void setupThreadPool() { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 246 | executors = Executors.newScheduledThreadPool(raPoolSize, |
| 247 | groupedThreads("RouterAdvertisement", "event-%d", log)); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 248 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 249 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 250 | private synchronized void clearThreadPool() { |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 251 | executors.shutdown(); |
| 252 | } |
| 253 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 254 | // Start Tx threads for all configured interfaces. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 255 | private synchronized void setupTxWorkers() { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 256 | interfaceService.getInterfaces() |
| 257 | .stream() |
| 258 | .filter(i -> mastershipService.getLocalRole(i.connectPoint().deviceId()) |
| 259 | == MastershipRole.MASTER) |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 260 | .map(prefixGenerator::apply) |
| 261 | .filter(i -> i.getValue() |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 262 | .stream() |
| 263 | .anyMatch(ia -> ia.ipAddress().version().equals(IpAddress.Version.INET6))) |
| 264 | .forEach(j -> |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 265 | activateRouterAdvertisement(j.getKey(), j.getValue()) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 266 | ); |
| 267 | } |
| 268 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 269 | // Clear out Tx threads. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 270 | private synchronized void clearTxWorkers() { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 271 | transmitters.entrySet().stream().forEach(i -> i.getValue().getKey().cancel(false)); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 272 | transmitters.clear(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 273 | } |
| 274 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 275 | private synchronized void setupPoolAndTxWorkers() { |
| 276 | setupThreadPool(); |
| 277 | setupTxWorkers(); |
| 278 | } |
| 279 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 280 | private synchronized void clearPoolAndTxWorkers() { |
| 281 | clearTxWorkers(); |
| 282 | clearThreadPool(); |
| 283 | } |
| 284 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 285 | // Loading global prefixes for devices from network configuration |
| 286 | private synchronized void loadGlobalPrefixConfig() { |
| 287 | globalPrefixes.clear(); |
| 288 | Set<DeviceId> deviceSubjects = |
| 289 | networkConfigRegistry.getSubjects(DeviceId.class, RouterAdvertisementDeviceConfig.class); |
| 290 | deviceSubjects.forEach(subject -> { |
| 291 | RouterAdvertisementDeviceConfig config = |
| 292 | networkConfigRegistry.getConfig(subject, RouterAdvertisementDeviceConfig.class); |
| 293 | if (config != null) { |
| 294 | List<InterfaceIpAddress> ips = config.prefixes(); |
| 295 | globalPrefixes.put(subject, ips); |
| 296 | } |
| 297 | }); |
| 298 | } |
| 299 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 300 | // Handler for network configuration updates |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 301 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 302 | @Override |
| 303 | public void event(NetworkConfigEvent event) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 304 | if (event.configClass().equals(RouterAdvertisementDeviceConfig.class) |
| 305 | || event.configClass().equals(InterfaceConfig.class)) { |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 306 | switch (event.type()) { |
| 307 | case CONFIG_ADDED: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 308 | case CONFIG_UPDATED: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 309 | clearTxWorkers(); |
| 310 | loadGlobalPrefixConfig(); |
| 311 | setupTxWorkers(); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 312 | log.info("Configuration updated for {}", event.subject()); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 313 | break; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 314 | default: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 318 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 319 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 320 | private final InternalNetworkConfigListener networkConfigListener |
| 321 | = new InternalNetworkConfigListener(); |
| 322 | |
| 323 | // Handler for device updates |
| 324 | private class InternalDeviceListener implements DeviceListener { |
| 325 | |
| 326 | @Override |
| 327 | public void event(DeviceEvent event) { |
| 328 | switch (event.type()) { |
| 329 | case DEVICE_ADDED: |
| 330 | case PORT_UPDATED: |
| 331 | case PORT_ADDED: |
| 332 | case DEVICE_UPDATED: |
| 333 | case DEVICE_AVAILABILITY_CHANGED: |
| 334 | clearTxWorkers(); |
| 335 | setupTxWorkers(); |
| 336 | log.trace("Processed device event {} on {}", event.type(), event.subject()); |
| 337 | break; |
| 338 | default: |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | private final InternalDeviceListener internalDeviceListener = |
| 344 | new InternalDeviceListener(); |
| 345 | |
| 346 | // Processor for Solicited RA packets |
| 347 | private class InternalPacketProcessor implements PacketProcessor { |
| 348 | |
| 349 | @Override |
| 350 | public void process(PacketContext context) { |
| 351 | if (context.isHandled()) { |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | // Ensure packet is IPv6 Solicited RA |
| 356 | InboundPacket pkt = context.inPacket(); |
| 357 | Ethernet ethernet = pkt.parsed(); |
| 358 | if ((ethernet == null) || (ethernet.getEtherType() != Ethernet.TYPE_IPV6)) { |
| 359 | return; |
| 360 | } |
| 361 | IPv6 ipv6Packet = (IPv6) ethernet.getPayload(); |
| 362 | if (ipv6Packet.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 363 | return; |
| 364 | } |
| 365 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
| 366 | if (icmp6Packet.getIcmpType() != ICMP6.ROUTER_SOLICITATION) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // Start solicited-RA handling thread |
| 371 | SolicitedRAWorkerThread sraWorkerThread = new SolicitedRAWorkerThread(pkt); |
| 372 | executors.schedule(sraWorkerThread, 0, TimeUnit.SECONDS); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | InternalPacketProcessor processor = null; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 377 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 378 | @Activate |
| 379 | protected void activate(ComponentContext context) { |
| 380 | // Basic application registrations. |
| 381 | appId = coreService.registerApplication(APP_NAME); |
| 382 | componentConfigService.registerProperties(getClass()); |
| 383 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 384 | // Packet processor for handling Router Solicitations |
| 385 | processor = new InternalPacketProcessor(); |
| 386 | packetService.addProcessor(processor, PacketProcessor.director(3)); |
| 387 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 388 | // Setup global prefix loading components |
| 389 | networkConfigRegistry.addListener(networkConfigListener); |
| 390 | networkConfigRegistry.registerConfigFactory(deviceConfigFactory); |
| 391 | loadGlobalPrefixConfig(); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 392 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 393 | // Dynamic device updates handling |
| 394 | deviceService.addListener(internalDeviceListener); |
| 395 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 396 | // Setup pool and worker threads for existing interfaces |
| 397 | setupPoolAndTxWorkers(); |
| 398 | } |
| 399 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 400 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 401 | @Modified |
| 402 | protected void modified(ComponentContext context) { |
| 403 | int newRaPoolSize, newRaThreadDelay; |
| 404 | |
| 405 | // Loading configured properties. |
| 406 | if (context != null) { |
| 407 | Dictionary<?, ?> properties = context.getProperties(); |
| 408 | try { |
| 409 | // Handle change in pool size |
| 410 | String s = get(properties, PROP_RA_THREADS_POOL); |
| 411 | newRaPoolSize = isNullOrEmpty(s) ? |
| 412 | DEFAULT_RA_THREADS_POOL_SIZE : Integer.parseInt(s.trim()); |
| 413 | if (newRaPoolSize != raPoolSize) { |
| 414 | raPoolSize = newRaPoolSize; |
| 415 | clearPoolAndTxWorkers(); |
| 416 | setupPoolAndTxWorkers(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 417 | log.info("Thread pool size updated to {}", raPoolSize); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Handle change in thread delay |
| 421 | s = get(properties, PROP_RA_THREADS_DELAY); |
| 422 | newRaThreadDelay = isNullOrEmpty(s) ? |
| 423 | DEFAULT_RA_THREADS_DELAY : Integer.parseInt(s.trim()); |
| 424 | if (newRaThreadDelay != raThreadDelay) { |
| 425 | raThreadDelay = newRaThreadDelay; |
| 426 | clearTxWorkers(); |
| 427 | setupTxWorkers(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 428 | log.info("Thread delay updated to {}", raThreadDelay); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // Handle M-flag changes |
| 432 | s = get(properties, PROP_RA_FLAG_MBIT_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 433 | if (!isNullOrEmpty(s)) { |
| 434 | raFlagMbitStatus = Boolean.parseBoolean(s.trim()); |
| 435 | log.info("RA M-flag set {}", s); |
| 436 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 437 | |
| 438 | // Handle O-flag changes |
| 439 | s = get(properties, PROP_RA_FLAG_OBIT_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 440 | if (!isNullOrEmpty(s)) { |
| 441 | raFlagObitStatus = Boolean.parseBoolean(s.trim()); |
| 442 | log.info("RA O-flag set {}", s); |
| 443 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 444 | |
| 445 | // Handle prefix option configuration |
| 446 | s = get(properties, PROP_RA_OPTION_PREFIX_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 447 | if (!isNullOrEmpty(s)) { |
| 448 | raOptionPrefixStatus = Boolean.parseBoolean(s.trim()); |
| 449 | String status = raOptionPrefixStatus ? "enabled" : "disabled"; |
| 450 | log.info("RA prefix option {}", status); |
| 451 | } |
| 452 | |
| 453 | s = get(properties, PROP_RA_GLOBAL_PREFIX_CONF_STATUS); |
| 454 | if (!isNullOrEmpty(s)) { |
| 455 | raGlobalConfigStatus = Boolean.parseBoolean(s.trim()); |
| 456 | clearTxWorkers(); |
| 457 | setupTxWorkers(); |
| 458 | String status = raOptionPrefixStatus ? "enabled" : "disabled"; |
| 459 | log.info("RA global configuration file loading {}", status); |
| 460 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 461 | |
| 462 | } catch (NumberFormatException e) { |
| 463 | log.warn("Component configuration had invalid value, aborting changes loading.", e); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | @Deactivate |
| 469 | protected void deactivate() { |
| 470 | // Unregister resources. |
| 471 | componentConfigService.unregisterProperties(getClass(), false); |
| 472 | interfaceService.removeListener(interfaceListener); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 473 | networkConfigRegistry.removeListener(networkConfigListener); |
| 474 | networkConfigRegistry.unregisterConfigFactory(deviceConfigFactory); |
| 475 | packetService.removeProcessor(processor); |
| 476 | deviceService.removeListener(internalDeviceListener); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 477 | |
| 478 | // Clear pool & threads |
| 479 | clearPoolAndTxWorkers(); |
| 480 | } |
| 481 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 482 | // Worker thread for actually sending ICMPv6 RA packets. |
| 483 | private class RAWorkerThread implements Runnable { |
| 484 | |
| 485 | ConnectPoint connectPoint; |
| 486 | List<InterfaceIpAddress> ipAddresses; |
| 487 | int retransmitPeriod; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 488 | MacAddress solicitHostMac; |
| 489 | byte[] solicitHostAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 490 | |
| 491 | // Various fixed values in RA packet |
| 492 | public static final byte RA_HOP_LIMIT = (byte) 0xff; |
| 493 | public static final short RA_ROUTER_LIFETIME = (short) 1800; |
| 494 | public static final int RA_OPTIONS_BUFFER_SIZE = 500; |
| 495 | public static final int RA_OPTION_MTU_VALUE = 1500; |
| 496 | public static final int RA_OPTION_PREFIX_VALID_LIFETIME = 600; |
| 497 | public static final int RA_OPTION_PREFIX_PREFERRED_LIFETIME = 600; |
| 498 | public static final int RA_RETRANSMIT_CALIBRATION_PERIOD = 1; |
| 499 | |
| 500 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 501 | RAWorkerThread(ConnectPoint connectPoint, List<InterfaceIpAddress> ipAddresses, int period, |
| 502 | MacAddress macAddress, byte[] ipv6Address) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 503 | this.connectPoint = connectPoint; |
| 504 | this.ipAddresses = ipAddresses; |
| 505 | retransmitPeriod = period; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 506 | solicitHostMac = macAddress; |
| 507 | solicitHostAddress = ipv6Address; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | public void run() { |
| 511 | // Router Advertisement header filling. Please refer RFC-2461. |
| 512 | RouterAdvertisement ra = new RouterAdvertisement(); |
| 513 | ra.setCurrentHopLimit(RA_HOP_LIMIT); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 514 | ra.setMFlag((byte) (raFlagMbitStatus ? 0x01 : 0x00)); |
| 515 | ra.setOFlag((byte) (raFlagObitStatus ? 0x01 : 0x00)); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 516 | ra.setRouterLifetime(RA_ROUTER_LIFETIME); |
| 517 | ra.setReachableTime(0); |
| 518 | ra.setRetransmitTimer(retransmitPeriod + RA_RETRANSMIT_CALIBRATION_PERIOD); |
| 519 | |
| 520 | // Option : Source link-layer address. |
| 521 | byte[] optionBuffer = new byte[RA_OPTIONS_BUFFER_SIZE]; |
| 522 | ByteBuffer option = ByteBuffer.wrap(optionBuffer); |
| 523 | Optional<MacAddress> macAddress = interfaceService.getInterfacesByPort(connectPoint).stream() |
| 524 | .map(Interface::mac).findFirst(); |
| 525 | if (!macAddress.isPresent()) { |
| 526 | log.warn("Unable to retrieve interface {} MAC address. Terminating RA transmission.", connectPoint); |
| 527 | return; |
| 528 | } |
| 529 | option.put(macAddress.get().toBytes()); |
| 530 | ra.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, |
| 531 | Arrays.copyOfRange(option.array(), 0, option.position())); |
| 532 | |
| 533 | // Option : MTU. |
| 534 | option.rewind(); |
| 535 | option.putShort((short) 0); |
| 536 | option.putInt(RA_OPTION_MTU_VALUE); |
| 537 | ra.addOption(NeighborDiscoveryOptions.TYPE_MTU, |
| 538 | Arrays.copyOfRange(option.array(), 0, option.position())); |
| 539 | |
| 540 | // Option : Prefix information. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 541 | if (raOptionPrefixStatus) { |
| 542 | ipAddresses.stream() |
| 543 | .filter(i -> i.ipAddress().version().equals(IpAddress.Version.INET6)) |
| 544 | .forEach(i -> { |
| 545 | option.rewind(); |
| 546 | option.put((byte) i.subnetAddress().prefixLength()); |
| 547 | // Enable "onlink" option only. |
| 548 | option.put((byte) 0x80); |
| 549 | option.putInt(RA_OPTION_PREFIX_VALID_LIFETIME); |
| 550 | option.putInt(RA_OPTION_PREFIX_PREFERRED_LIFETIME); |
| 551 | // Clear reserved fields |
| 552 | option.putInt(0x00000000); |
| 553 | option.put(IpAddress.makeMaskedAddress(i.ipAddress(), |
| 554 | i.subnetAddress().prefixLength()).toOctets()); |
| 555 | ra.addOption(NeighborDiscoveryOptions.TYPE_PREFIX_INFORMATION, |
| 556 | Arrays.copyOfRange(option.array(), 0, option.position())); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 557 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 558 | }); |
| 559 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 560 | |
| 561 | // ICMPv6 header filling. |
| 562 | ICMP6 icmpv6 = new ICMP6(); |
| 563 | icmpv6.setIcmpType(ICMP6.ROUTER_ADVERTISEMENT); |
| 564 | icmpv6.setIcmpCode((byte) 0); |
| 565 | icmpv6.setPayload(ra); |
| 566 | |
| 567 | // IPv6 header filling. |
| 568 | byte[] ip6AllNodesAddress = Ip6Address.valueOf("ff02::1").toOctets(); |
| 569 | IPv6 ipv6 = new IPv6(); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 570 | ipv6.setDestinationAddress((solicitHostAddress == null) ? ip6AllNodesAddress : solicitHostAddress); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 571 | /* RA packet L2 source address created from port MAC address. |
| 572 | * Note : As per RFC-4861 RAs should be sent from link-local address. |
| 573 | */ |
| 574 | ipv6.setSourceAddress(IPv6.getLinkLocalAddress(macAddress.get().toBytes())); |
| 575 | ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6); |
| 576 | ipv6.setHopLimit(RA_HOP_LIMIT); |
| 577 | ipv6.setTrafficClass((byte) 0xe0); |
| 578 | ipv6.setPayload(icmpv6); |
| 579 | |
| 580 | // Ethernet header filling. |
| 581 | Ethernet ethernet = new Ethernet(); |
| 582 | |
| 583 | /* Ethernet IPv6 multicast address creation. |
| 584 | * Refer : RFC 2624 section 7. |
| 585 | */ |
| 586 | byte[] l2Ipv6MulticastAddress = MacAddress.IPV6_MULTICAST.toBytes(); |
| 587 | IntStream.range(1, 4).forEach(i -> l2Ipv6MulticastAddress[l2Ipv6MulticastAddress.length - i] = |
| 588 | ip6AllNodesAddress[ip6AllNodesAddress.length - i]); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 589 | // Provide unicast address for Solicit RA replays |
| 590 | ethernet.setDestinationMACAddress((solicitHostMac == null) ? |
| 591 | MacAddress.valueOf(l2Ipv6MulticastAddress) : solicitHostMac); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 592 | ethernet.setSourceMACAddress(macAddress.get().toBytes()); |
| 593 | ethernet.setEtherType(EthType.EtherType.IPV6.ethType().toShort()); |
| 594 | ethernet.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 595 | ethernet.setPayload(ipv6); |
| 596 | ethernet.setPad(false); |
| 597 | |
| 598 | // Flush out PACKET_OUT. |
| 599 | ByteBuffer stream = ByteBuffer.wrap(ethernet.serialize()); |
| 600 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 601 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 602 | treatment, stream); |
| 603 | packetService.emit(packet); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 604 | log.trace("Transmitted Unsolicited Router Advertisement on {}", connectPoint); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 605 | } |
| 606 | } |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 607 | |
| 608 | // Worker thread for processing IPv6 Solicited RA packets. |
| 609 | public class SolicitedRAWorkerThread implements Runnable { |
| 610 | |
| 611 | private InboundPacket packet; |
| 612 | |
| 613 | SolicitedRAWorkerThread(InboundPacket packet) { |
| 614 | this.packet = packet; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public void run() { |
| 619 | |
| 620 | // TODO : Validate Router Solicitation |
| 621 | |
| 622 | // Pause already running unsolicited RA threads in received connect point |
| 623 | ConnectPoint connectPoint = packet.receivedFrom(); |
| 624 | List<InterfaceIpAddress> addresses = deactivateRouterAdvertisement(connectPoint); |
| 625 | |
| 626 | /* Multicast RA(ie. Unsolicited RA) TX time is not preciously tracked so to make sure that |
| 627 | * Unicast RA(ie. Router Solicitation Response) is TXed before Mulicast RA |
| 628 | * logic adapted here is disable Mulicast RA, TX Unicast RA and then restore Multicast RA. |
| 629 | */ |
| 630 | log.trace("Processing Router Solicitations from {}", connectPoint); |
| 631 | try { |
| 632 | Ethernet ethernet = packet.parsed(); |
| 633 | IPv6 ipv6 = (IPv6) ethernet.getPayload(); |
| 634 | RAWorkerThread worker = new RAWorkerThread(connectPoint, |
| 635 | addresses, raThreadDelay, ethernet.getSourceMAC(), ipv6.getSourceAddress()); |
| 636 | // TODO : Estimate TX time as in RFC 4861, Section 6.2.6 and schedule TX based on it |
| 637 | CompletableFuture<Void> sraHandlerFuture = CompletableFuture.runAsync(worker, executors); |
| 638 | sraHandlerFuture.get(); |
| 639 | } catch (Exception e) { |
| 640 | log.error("Failed to respond to router solicitation. {}", e); |
| 641 | } finally { |
| 642 | activateRouterAdvertisement(connectPoint, addresses); |
| 643 | log.trace("Restored Unsolicited Router Advertisements on {}", connectPoint); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 648 | } |