Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.proxyarp; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 17 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Kunihiro Ishiguro | 9322445 | 2015-02-13 00:40:08 +0900 | [diff] [blame] | 26 | import org.onlab.packet.ICMP6; |
alshabib | 78baaf2 | 2015-02-18 18:55:45 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IPv6; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 28 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 33 | import org.onosproject.net.packet.InboundPacket; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 35 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.packet.PacketProcessor; |
| 37 | import org.onosproject.net.packet.PacketService; |
| 38 | import org.onosproject.net.proxyarp.ProxyArpService; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 39 | import org.osgi.service.component.ComponentContext; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 42 | import java.util.Dictionary; |
| 43 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 44 | import static com.google.common.base.Strings.isNullOrEmpty; |
alshabib | 78baaf2 | 2015-02-18 18:55:45 -0800 | [diff] [blame] | 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Sample reactive proxy arp application. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | public class ProxyArp { |
| 52 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected PacketService packetService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected ProxyArpService proxyArpService; |
| 60 | |
| 61 | private ProxyArpProcessor processor = new ProxyArpProcessor(); |
| 62 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected CoreService coreService; |
| 65 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected ComponentConfigService cfgService; |
| 68 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 69 | private ApplicationId appId; |
| 70 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 71 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 72 | label = "Enable IPv6 Neighbor Discovery; default is false") |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 73 | protected boolean ipv6NeighborDiscovery = false; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 74 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 75 | @Activate |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 76 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 77 | cfgService.registerProperties(getClass()); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 78 | appId = coreService.registerApplication("org.onosproject.proxyarp"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 79 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 80 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 81 | readComponentConfiguration(context); |
| 82 | requestPackests(); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 83 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 84 | log.info("Started with Application ID {}", appId.id()); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | // TODO revoke all packet requests when deactivate |
| 90 | cfgService.unregisterProperties(getClass(), false); |
| 91 | packetService.removeProcessor(processor); |
| 92 | processor = null; |
| 93 | log.info("Stopped"); |
| 94 | } |
| 95 | |
| 96 | @Modified |
| 97 | public void modified(ComponentContext context) { |
| 98 | // TODO revoke unnecessary packet requests when config being modified |
| 99 | readComponentConfiguration(context); |
| 100 | requestPackests(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Request packet in via PacketService. |
| 105 | */ |
| 106 | private void requestPackests() { |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 107 | TrafficSelector.Builder selectorBuilder = |
| 108 | DefaultTrafficSelector.builder(); |
| 109 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
| 110 | packetService.requestPackets(selectorBuilder.build(), |
| 111 | PacketPriority.CONTROL, appId); |
| 112 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 113 | if (ipv6NeighborDiscovery) { |
| 114 | // IPv6 Neighbor Solicitation packet. |
| 115 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 116 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 117 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 118 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 119 | packetService.requestPackets(selectorBuilder.build(), |
| 120 | PacketPriority.CONTROL, appId); |
Kunihiro Ishiguro | 9322445 | 2015-02-13 00:40:08 +0900 | [diff] [blame] | 121 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 122 | // IPv6 Neighbor Advertisement packet. |
| 123 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 124 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 125 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 126 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 127 | packetService.requestPackets(selectorBuilder.build(), |
| 128 | PacketPriority.CONTROL, appId); |
| 129 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Extracts properties from the component configuration context. |
| 134 | * |
| 135 | * @param context the component context |
| 136 | */ |
| 137 | private void readComponentConfiguration(ComponentContext context) { |
| 138 | Dictionary<?, ?> properties = context.getProperties(); |
| 139 | Boolean flag; |
| 140 | |
| 141 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 142 | if (flag == null) { |
| 143 | log.info("IPv6 Neighbor Discovery is not configured, " + |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 144 | "using current value of {}", ipv6NeighborDiscovery); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 145 | } else { |
| 146 | ipv6NeighborDiscovery = flag; |
| 147 | log.info("Configured. IPv6 Neighbor Discovery is {}", |
| 148 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Check property name is defined and set to true. |
| 154 | * |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 155 | * @param properties properties to be looked up |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 156 | * @param propertyName the name of the property to look up |
| 157 | * @return value when the propertyName is defined or return null |
| 158 | */ |
| 159 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 160 | String propertyName) { |
| 161 | Boolean value = null; |
| 162 | try { |
| 163 | String s = (String) properties.get(propertyName); |
| 164 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 165 | } catch (ClassCastException e) { |
| 166 | // No propertyName defined. |
| 167 | value = null; |
| 168 | } |
| 169 | return value; |
| 170 | } |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Packet processor responsible for forwarding packets along their paths. |
| 174 | */ |
| 175 | private class ProxyArpProcessor implements PacketProcessor { |
| 176 | |
| 177 | @Override |
| 178 | public void process(PacketContext context) { |
| 179 | // Stop processing if the packet has been handled, since we |
| 180 | // can't do any more to it. |
| 181 | if (context.isHandled()) { |
| 182 | return; |
| 183 | } |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 184 | // If IPv6 NDP is disabled, don't handle IPv6 frames. |
| 185 | InboundPacket pkt = context.inPacket(); |
| 186 | Ethernet ethPkt = pkt.parsed(); |
| 187 | if (ethPkt == null) { |
| 188 | return; |
| 189 | } |
| 190 | if (!ipv6NeighborDiscovery && (ethPkt.getEtherType() == Ethernet.TYPE_IPV6)) { |
| 191 | return; |
| 192 | } |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 193 | //handle the arp packet. |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 194 | proxyArpService.handlePacket(context); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |