blob: cfef0f13a29ce28ce287e240c24a316245cc3de7 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
18import org.onlab.packet.ARP;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.IpAddress;
Pier Ventreb6a7f342016-11-26 21:05:22 -080022import org.onlab.packet.IpPrefix;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.MacAddress;
Charles Chanf4586112015-11-09 16:37:23 -080024import org.onlab.packet.VlanId;
Ray Milkey2a31aeb2017-08-03 16:28:24 -070025import org.onosproject.net.neighbour.NeighbourMessageContext;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
Pier Ventreb6a7f342016-11-26 21:05:22 -080028import org.onosproject.net.host.HostService;
Charles Chan319d1a22015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Charles Chandebfea32016-10-24 14:52:01 -070030import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
sangho80f11cb2015-04-01 13:05:26 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Saurav Dasc28b3432015-10-30 17:45:38 -070034import java.util.Set;
Pier Ventreb6a7f342016-11-26 21:05:22 -080035import java.util.stream.Collectors;
Saurav Dasc28b3432015-10-30 17:45:38 -070036
Ray Milkey2a31aeb2017-08-03 16:28:24 -070037import static org.onosproject.net.neighbour.NeighbourMessageType.REQUEST;
sangho80f11cb2015-04-01 13:05:26 -070038
Charles Chanb7f75ac2016-01-11 18:28:54 -080039/**
40 * Handler of ARP packets that responses or forwards ARP packets that
41 * are sent to the controller.
42 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080043public class ArpHandler extends SegmentRoutingNeighbourHandler {
sangho80f11cb2015-04-01 13:05:26 -070044
45 private static Logger log = LoggerFactory.getLogger(ArpHandler.class);
46
sangho80f11cb2015-04-01 13:05:26 -070047 /**
48 * Creates an ArpHandler object.
49 *
50 * @param srManager SegmentRoutingManager object
51 */
52 public ArpHandler(SegmentRoutingManager srManager) {
Pier Ventreb6b81d52016-12-02 08:16:05 -080053 super(srManager);
sangho80f11cb2015-04-01 13:05:26 -070054 }
55
56 /**
57 * Processes incoming ARP packets.
Charles Chanf4586112015-11-09 16:37:23 -080058 *
sangho80f11cb2015-04-01 13:05:26 -070059 * If it is an ARP request to router itself or known hosts,
60 * then it sends ARP response.
61 * If it is an ARP request to unknown hosts in its own subnet,
62 * then it flood the ARP request to the ports.
63 * If it is an ARP response, then set a flow rule for the host
64 * and forward any IP packets to the host in the packet buffer to the host.
Charles Chanf4586112015-11-09 16:37:23 -080065 * <p>
66 * Note: We handles all ARP packet in, even for those ARP packets between
67 * hosts in the same subnet.
68 * For an ARP packet with broadcast destination MAC,
69 * some switches pipelines will send it to the controller due to table miss,
Saurav Das7b1b4882016-02-05 13:15:20 -080070 * other switches will flood the packets directly in the data plane without
Charles Chanf4586112015-11-09 16:37:23 -080071 * packet in.
72 * We can deal with both cases.
sangho80f11cb2015-04-01 13:05:26 -070073 *
Pier Ventreb6a7f342016-11-26 21:05:22 -080074 * @param pkt incoming ARP packet and context information
75 * @param hostService the host service
sangho80f11cb2015-04-01 13:05:26 -070076 */
Pier Ventreb6a7f342016-11-26 21:05:22 -080077 public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) {
Charles Chancbdc9be2016-10-17 18:03:37 -070078
Charles Chandebfea32016-10-24 14:52:01 -070079 SegmentRoutingAppConfig appConfig = srManager.cfgService
80 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Pier Ventreb6a7f342016-11-26 21:05:22 -080081 if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) {
Charles Chandebfea32016-10-24 14:52:01 -070082 // Ignore ARP packets come from suppressed ports
Pier Ventre1a655962016-11-28 16:48:06 -080083 pkt.drop();
Charles Chandebfea32016-10-24 14:52:01 -070084 return;
85 }
86
Pier Ventreb6a7f342016-11-26 21:05:22 -080087 if (!validateArpSpa(pkt)) {
Charles Chanee891c52016-11-08 16:32:13 -080088 log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.",
Pier Ventreb6a7f342016-11-26 21:05:22 -080089 pkt.inPort(), pkt.sender().getIp4Address());
Pier Ventre1a655962016-11-28 16:48:06 -080090 pkt.drop();
Charles Chancbdc9be2016-10-17 18:03:37 -070091 return;
92 }
93
Pier Ventreb6a7f342016-11-26 21:05:22 -080094 if (pkt.type() == REQUEST) {
95 handleArpRequest(pkt, hostService);
sangho80f11cb2015-04-01 13:05:26 -070096 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -080097 handleArpReply(pkt, hostService);
sangho80f11cb2015-04-01 13:05:26 -070098 }
99 }
100
Pier Ventreb6a7f342016-11-26 21:05:22 -0800101 private void handleArpRequest(NeighbourMessageContext pkt, HostService hostService) {
Charles Chanf4586112015-11-09 16:37:23 -0800102 // ARP request for router. Send ARP reply.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800103 if (isArpForRouter(pkt)) {
104 MacAddress targetMac = config.getRouterMacForAGatewayIp(pkt.target().getIp4Address());
Charles Chan76adba22018-03-30 12:11:34 -0700105 if (targetMac == null) {
106 log.warn("Router MAC of {} is not configured. Cannot handle ARP request from {}",
107 pkt.inPort().deviceId(), pkt.sender());
108 return;
109 }
Charles Chan1e544ca2018-06-29 14:28:39 -0700110 sendResponse(pkt, targetMac, hostService, true);
Charles Chanbbc8d902015-10-15 10:48:13 -0700111 } else {
Charles Chanb3016ed2017-02-27 15:50:43 -0800112 // NOTE: Ignore ARP packets except those target for the router
113 // We will reconsider enabling this when we have host learning support
114 /*
Pier Ventreb6a7f342016-11-26 21:05:22 -0800115 Set<Host> hosts = hostService.getHostsByIp(pkt.target());
116 if (hosts.size() > 1) {
117 log.warn("More than one host with the same ip {}", pkt.target());
118 }
119 Host targetHost = hosts.stream().findFirst().orElse(null);
Charles Chanf4586112015-11-09 16:37:23 -0800120 // ARP request for known hosts. Send proxy ARP reply on behalf of the target.
Charles Chanbbc8d902015-10-15 10:48:13 -0700121 if (targetHost != null) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800122 pkt.forward(targetHost.location());
Charles Chanf4586112015-11-09 16:37:23 -0800123 // ARP request for unknown host in the subnet. Flood in the subnet.
124 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800125 flood(pkt);
Charles Chanf4586112015-11-09 16:37:23 -0800126 }
Charles Chanb3016ed2017-02-27 15:50:43 -0800127 */
Charles Chanf4586112015-11-09 16:37:23 -0800128 }
129 }
sangho80f11cb2015-04-01 13:05:26 -0700130
Pier Ventreb6a7f342016-11-26 21:05:22 -0800131 private void handleArpReply(NeighbourMessageContext pkt, HostService hostService) {
Charles Chanf4586112015-11-09 16:37:23 -0800132 // ARP reply for router. Process all pending IP packets.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800133 if (isArpForRouter(pkt)) {
134 Ip4Address hostIpAddress = pkt.sender().getIp4Address();
135 srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress);
Charles Chanf4586112015-11-09 16:37:23 -0800136 } else {
Charles Chanb3016ed2017-02-27 15:50:43 -0800137 // NOTE: Ignore ARP packets except those target for the router
138 // We will reconsider enabling this when we have host learning support
139 /*
Pier Ventreb6a7f342016-11-26 21:05:22 -0800140 HostId targetHostId = HostId.hostId(pkt.dstMac(), pkt.vlan());
141 Host targetHost = hostService.getHost(targetHostId);
Charles Chanf4586112015-11-09 16:37:23 -0800142 // ARP reply for known hosts. Forward to the host.
143 if (targetHost != null) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800144 pkt.forward(targetHost.location());
Charles Chanf4586112015-11-09 16:37:23 -0800145 // ARP reply for unknown host, Flood in the subnet.
146 } else {
147 // Don't flood to non-edge ports
Charles Chan10b0fb72017-02-02 16:20:42 -0800148 if (pkt.vlan().equals(SegmentRoutingManager.INTERNAL_VLAN)) {
Charles Chanf4586112015-11-09 16:37:23 -0800149 return;
150 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800151 flood(pkt);
Charles Chanbbc8d902015-10-15 10:48:13 -0700152 }
Charles Chanb3016ed2017-02-27 15:50:43 -0800153 */
sangho80f11cb2015-04-01 13:05:26 -0700154 }
155 }
156
Charles Chancbdc9be2016-10-17 18:03:37 -0700157 /**
158 * Check if the source protocol address of an ARP packet belongs to the same
159 * subnet configured on the port it is seen.
160 *
Pier Ventreb6a7f342016-11-26 21:05:22 -0800161 * @param pkt ARP packet and context information
Charles Chancbdc9be2016-10-17 18:03:37 -0700162 * @return true if the source protocol address belongs to the configured subnet
163 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800164 private boolean validateArpSpa(NeighbourMessageContext pkt) {
165 Ip4Address spa = pkt.sender().getIp4Address();
166 Set<IpPrefix> subnet = config.getPortSubnets(pkt.inPort().deviceId(), pkt.inPort().port())
167 .stream()
168 .filter(ipPrefix -> ipPrefix.isIp4() && ipPrefix.contains(spa))
169 .collect(Collectors.toSet());
170 return !subnet.isEmpty();
Charles Chancbdc9be2016-10-17 18:03:37 -0700171 }
172
sangho80f11cb2015-04-01 13:05:26 -0700173
Pier Ventreb6a7f342016-11-26 21:05:22 -0800174 private boolean isArpForRouter(NeighbourMessageContext pkt) {
175 Ip4Address targetProtocolAddress = pkt.target().getIp4Address();
176 Set<IpAddress> gatewayIpAddresses = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800177 try {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800178 if (targetProtocolAddress.equals(config.getRouterIpv4(pkt.inPort().deviceId()))) {
sangho80f11cb2015-04-01 13:05:26 -0700179 return true;
180 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800181 gatewayIpAddresses = config.getPortIPs(pkt.inPort().deviceId());
Saurav Das2d94d312015-11-24 23:21:05 -0800182 } catch (DeviceConfigNotFoundException e) {
183 log.warn(e.getMessage() + " Aborting check for router IP in processing arp");
184 }
185 if (gatewayIpAddresses != null &&
186 gatewayIpAddresses.contains(targetProtocolAddress)) {
187 return true;
sangho80f11cb2015-04-01 13:05:26 -0700188 }
189 return false;
190 }
191
sangho80f11cb2015-04-01 13:05:26 -0700192 /**
193 * Sends an APR request for the target IP address to all ports except in-port.
194 *
195 * @param deviceId Switch device ID
196 * @param targetAddress target IP address for ARP
197 * @param inPort in-port
198 */
199 public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800200 byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH];
201 byte[] senderIpAddress = new byte[Ip4Address.BYTE_LENGTH];
202 /*
203 * Retrieves device info.
204 */
Pier Luigi6a83c4a2017-01-29 12:38:48 -0800205 if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) {
206 log.warn("Aborting sendArpRequest, we cannot get all the information needed");
207 return;
208 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800209 /*
210 * Creates the request.
211 */
212 Ethernet arpRequest = ARP.buildArpRequest(
213 senderMacAddress,
214 senderIpAddress,
215 targetAddress.toOctets(),
216 VlanId.NO_VID
Charles Chanf4586112015-11-09 16:37:23 -0800217 );
Pier Ventreb6b81d52016-12-02 08:16:05 -0800218 flood(arpRequest, inPort, targetAddress);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800219 }
220
sangho80f11cb2015-04-01 13:05:26 -0700221}