alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 16 | package org.onosproject.igmp; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Property; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onlab.packet.IGMP; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 39 | import org.onosproject.net.mcast.McastRoute; |
| 40 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.packet.InboundPacket; |
| 42 | import org.onosproject.net.packet.PacketContext; |
| 43 | import org.onosproject.net.packet.PacketPriority; |
| 44 | import org.onosproject.net.packet.PacketProcessor; |
| 45 | import org.onosproject.net.packet.PacketService; |
| 46 | import org.slf4j.Logger; |
| 47 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 48 | import java.util.Optional; |
| 49 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Internet Group Management Protocol. |
| 52 | */ |
| 53 | @Component(immediate = true) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 54 | public class IgmpSnoop { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 57 | private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4"; |
| 58 | |
| 59 | @Property(name = "multicastAddress", |
| 60 | label = "Define the multicast base raneg to listen to") |
| 61 | private String multicastAddress = DEFAULT_MCAST_ADDR; |
| 62 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected PacketService packetService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected CoreService coreService; |
| 68 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected NetworkConfigRegistry networkConfig; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected MulticastRouteService multicastService; |
| 74 | |
| 75 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 76 | private static ApplicationId appId; |
| 77 | |
| 78 | @Activate |
| 79 | public void activate() { |
| 80 | appId = coreService.registerApplication("org.onosproject.igmp"); |
| 81 | |
| 82 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 83 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 84 | networkConfig.getSubjects(DeviceId.class, IgmpDeviceConfig.class).forEach( |
| 85 | subject -> { |
| 86 | IgmpDeviceConfig config = networkConfig.getConfig(subject, |
| 87 | IgmpDeviceConfig.class); |
| 88 | if (config != null) { |
| 89 | IgmpDeviceData data = config.getDevice(); |
| 90 | submitPacketRequests(data.deviceId()); |
| 91 | } |
| 92 | } |
| 93 | ); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 94 | |
| 95 | log.info("Started"); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
| 100 | packetService.removeProcessor(processor); |
| 101 | processor = null; |
| 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 105 | private void submitPacketRequests(DeviceId deviceId) { |
| 106 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 107 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 108 | selector.matchIPProtocol(IPv4.PROTOCOL_IGMP); |
| 109 | packetService.requestPackets(selector.build(), |
| 110 | PacketPriority.REACTIVE, |
| 111 | appId, |
| 112 | Optional.of(deviceId)); |
| 113 | |
| 114 | } |
| 115 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Packet processor responsible for handling IGMP packets. |
| 118 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 119 | private class IgmpPacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public void process(PacketContext context) { |
| 123 | // Stop processing if the packet has been handled, since we |
| 124 | // can't do any more to it. |
| 125 | if (context.isHandled()) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | InboundPacket pkt = context.inPacket(); |
| 130 | Ethernet ethPkt = pkt.parsed(); |
| 131 | if (ethPkt == null) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * IPv6 MLD packets are handled by ICMP6. We'll only deal |
| 137 | * with IPv4. |
| 138 | */ |
| 139 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 144 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
| 145 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 146 | log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr, |
| 147 | context.inPacket().receivedFrom()); |
| 148 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 149 | |
| 150 | if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 151 | log.debug("IGMP Picked up a non IGMP packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 155 | IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 156 | if (!mcast.contains(gaddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 157 | log.debug("IGMP Picked up a non multicast packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (mcast.contains(saddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 162 | log.debug("IGMP Picked up a packet with a multicast source address."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 163 | return; |
| 164 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 165 | |
| 166 | IGMP igmp = (IGMP) ip.getPayload(); |
| 167 | switch (igmp.getIgmpType()) { |
| 168 | |
| 169 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
| 170 | IGMPProcessMembership.processMembership(igmp, pkt.receivedFrom()); |
| 171 | break; |
| 172 | |
| 173 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 174 | processQuery(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 178 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 179 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
| 180 | log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: " + |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 181 | igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 182 | break; |
| 183 | |
| 184 | default: |
| 185 | log.debug("Unkown IGMP message type: " + igmp.getIgmpType()); |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 190 | |
| 191 | private void processQuery(IGMP pkt, ConnectPoint location) { |
| 192 | pkt.getGroups().forEach(group -> group.getSources().forEach(src -> { |
| 193 | |
| 194 | McastRoute route = new McastRoute(src, |
| 195 | group.getGaddr(), |
| 196 | McastRoute.Type.IGMP); |
| 197 | multicastService.add(route); |
| 198 | multicastService.addSink(route, location); |
| 199 | |
| 200 | })); |
| 201 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 202 | } |