blob: e056f79449f6e96beffaea6d466aa7d30f156828 [file] [log] [blame]
Jonathan Hart9bdaaec2016-08-22 13:33:45 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.incubator.net.neighbour.impl;
18
19import com.google.common.annotations.Beta;
20import org.onlab.packet.ARP;
21import org.onlab.packet.Ethernet;
22import org.onlab.packet.ICMP6;
23import org.onlab.packet.IPv6;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip6Address;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onlab.packet.ndp.NeighborSolicitation;
30import org.onosproject.incubator.net.intf.Interface;
31import org.onosproject.incubator.net.neighbour.NeighbourMessageActions;
32import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
33import org.onosproject.incubator.net.neighbour.NeighbourMessageType;
34import org.onosproject.incubator.net.neighbour.NeighbourProtocol;
35import org.onosproject.net.ConnectPoint;
36
37import static com.google.common.base.Preconditions.checkState;
38
39/**
40 * Default implementation of a neighbour message context.
41 */
42@Beta
43public class DefaultNeighbourMessageContext implements NeighbourMessageContext {
44
45 private final NeighbourProtocol protocol;
46 private final NeighbourMessageType type;
47
48 private final IpAddress target;
49 private final IpAddress sender;
50
51 private final Ethernet eth;
52 private final ConnectPoint inPort;
53
54 private final NeighbourMessageActions actions;
55
56 /**
57 * Creates a new neighbour message context.
58 *
59 * @param actions actions
60 * @param eth ethernet frame
61 * @param inPort incoming port
62 * @param protocol message protocol
63 * @param type message type
64 * @param target target IP address
65 * @param sender sender IP address
66 */
67 DefaultNeighbourMessageContext(NeighbourMessageActions actions,
68 Ethernet eth, ConnectPoint inPort,
69 NeighbourProtocol protocol,
70 NeighbourMessageType type,
71 IpAddress target, IpAddress sender) {
72 this.actions = actions;
73 this.eth = eth;
74 this.inPort = inPort;
75 this.protocol = protocol;
76 this.type = type;
77 this.target = target;
78 this.sender = sender;
79 }
80
81 @Override
82 public ConnectPoint inPort() {
83 return inPort;
84 }
85
86 @Override
87 public Ethernet packet() {
88 return eth;
89 }
90
91 @Override
92 public NeighbourProtocol protocol() {
93 return protocol;
94 }
95
96 @Override
97 public NeighbourMessageType type() {
98 return type;
99 }
100
101 @Override
102 public VlanId vlan() {
103 return VlanId.vlanId(eth.getVlanID());
104 }
105
106 @Override
107 public MacAddress srcMac() {
108 return MacAddress.valueOf(eth.getSourceMACAddress());
109 }
110
111 @Override
112 public IpAddress target() {
113 return target;
114 }
115
116 @Override
117 public IpAddress sender() {
118 return sender;
119 }
120
121 @Override
122 public void proxy(ConnectPoint outPort) {
123 actions.proxy(this, outPort);
124 }
125
126 @Override
127 public void proxy(Interface outIntf) {
128 actions.proxy(this, outIntf);
129 }
130
131 @Override
132 public void reply(MacAddress targetMac) {
133 checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
134
135 actions.reply(this, targetMac);
136 }
137
138 @Override
139 public void flood() {
140 actions.flood(this);
141 }
142
143 @Override
144 public void drop() {
145 actions.drop(this);
146 }
147
148 /**
149 * Attempts to create a MessageContext for the given Ethernet frame. If the
150 * frame is a valid ARP or NDP request or response, a context will be
151 * created.
152 *
153 * @param eth input Ethernet frame
154 * @param inPort in port
155 * @return MessageContext if the packet was ARP or NDP, otherwise null
156 */
157 public static NeighbourMessageContext createContext(Ethernet eth,
158 ConnectPoint inPort,
159 NeighbourMessageActions actions) {
160 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
161 return createArpContext(eth, inPort, actions);
162 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
163 return createNdpContext(eth, inPort, actions);
164 }
165
166 return null;
167 }
168
169 /**
170 * Extracts context information from ARP packets.
171 *
172 * @param eth input Ethernet frame that is thought to be ARP
173 * @param inPort in port
174 * @return MessageContext object if the packet was a valid ARP packet,
175 * otherwise null
176 */
177 private static NeighbourMessageContext createArpContext(Ethernet eth,
178 ConnectPoint inPort,
179 NeighbourMessageActions actions) {
180 if (eth.getEtherType() != Ethernet.TYPE_ARP) {
181 return null;
182 }
183
184 ARP arp = (ARP) eth.getPayload();
185
186 IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
187 IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
188
189 NeighbourMessageType type;
190 if (arp.getOpCode() == ARP.OP_REQUEST) {
191 type = NeighbourMessageType.REQUEST;
192 } else if (arp.getOpCode() == ARP.OP_REPLY) {
193 type = NeighbourMessageType.REPLY;
194 } else {
195 return null;
196 }
197
198 return new DefaultNeighbourMessageContext(actions, eth, inPort,
199 NeighbourProtocol.ARP, type, target, sender);
200 }
201
202 /**
203 * Extracts context information from NDP packets.
204 *
205 * @param eth input Ethernet frame that is thought to be NDP
206 * @param inPort in port
207 * @return MessageContext object if the packet was a valid NDP packet,
208 * otherwise null
209 */
210 private static NeighbourMessageContext createNdpContext(Ethernet eth,
211 ConnectPoint inPort,
212 NeighbourMessageActions actions) {
213 if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
214 return null;
215 }
216 IPv6 ipv6 = (IPv6) eth.getPayload();
217
218 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
219 return null;
220 }
221 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
222
223 IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
224 IpAddress target = null;
225
226 NeighbourMessageType type;
227 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
228 type = NeighbourMessageType.REQUEST;
229 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
230 target = Ip6Address.valueOf(nsol.getTargetAddress());
231 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
232 type = NeighbourMessageType.REPLY;
233 } else {
234 return null;
235 }
236
237 return new DefaultNeighbourMessageContext(actions, eth, inPort,
238 NeighbourProtocol.NDP, type, target, sender);
239 }
240
241}