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