blob: 44e97f9cad15b20ce178708205003c743b82b1a2 [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() {
Jonathan Hartc4f681c2016-09-09 07:14:25 -0700108 return eth.getSourceMAC();
109 }
110
111 @Override
112 public MacAddress dstMac() {
113 return eth.getDestinationMAC();
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700114 }
115
116 @Override
117 public IpAddress target() {
118 return target;
119 }
120
121 @Override
122 public IpAddress sender() {
123 return sender;
124 }
125
126 @Override
127 public void proxy(ConnectPoint outPort) {
128 actions.proxy(this, outPort);
129 }
130
131 @Override
132 public void proxy(Interface outIntf) {
133 actions.proxy(this, outIntf);
134 }
135
136 @Override
137 public void reply(MacAddress targetMac) {
138 checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
139
140 actions.reply(this, targetMac);
141 }
142
143 @Override
144 public void flood() {
145 actions.flood(this);
146 }
147
148 @Override
149 public void drop() {
150 actions.drop(this);
151 }
152
153 /**
154 * Attempts to create a MessageContext for the given Ethernet frame. If the
155 * frame is a valid ARP or NDP request or response, a context will be
156 * created.
157 *
158 * @param eth input Ethernet frame
159 * @param inPort in port
160 * @return MessageContext if the packet was ARP or NDP, otherwise null
161 */
162 public static NeighbourMessageContext createContext(Ethernet eth,
163 ConnectPoint inPort,
164 NeighbourMessageActions actions) {
165 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
166 return createArpContext(eth, inPort, actions);
167 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
168 return createNdpContext(eth, inPort, actions);
169 }
170
171 return null;
172 }
173
174 /**
175 * Extracts context information from ARP packets.
176 *
177 * @param eth input Ethernet frame that is thought to be ARP
178 * @param inPort in port
179 * @return MessageContext object if the packet was a valid ARP packet,
180 * otherwise null
181 */
182 private static NeighbourMessageContext createArpContext(Ethernet eth,
183 ConnectPoint inPort,
184 NeighbourMessageActions actions) {
185 if (eth.getEtherType() != Ethernet.TYPE_ARP) {
186 return null;
187 }
188
189 ARP arp = (ARP) eth.getPayload();
190
191 IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
192 IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
193
194 NeighbourMessageType type;
195 if (arp.getOpCode() == ARP.OP_REQUEST) {
196 type = NeighbourMessageType.REQUEST;
197 } else if (arp.getOpCode() == ARP.OP_REPLY) {
198 type = NeighbourMessageType.REPLY;
199 } else {
200 return null;
201 }
202
203 return new DefaultNeighbourMessageContext(actions, eth, inPort,
204 NeighbourProtocol.ARP, type, target, sender);
205 }
206
207 /**
208 * Extracts context information from NDP packets.
209 *
210 * @param eth input Ethernet frame that is thought to be NDP
211 * @param inPort in port
212 * @return MessageContext object if the packet was a valid NDP packet,
213 * otherwise null
214 */
215 private static NeighbourMessageContext createNdpContext(Ethernet eth,
216 ConnectPoint inPort,
217 NeighbourMessageActions actions) {
218 if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
219 return null;
220 }
221 IPv6 ipv6 = (IPv6) eth.getPayload();
222
223 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
224 return null;
225 }
226 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
227
228 IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
229 IpAddress target = null;
230
231 NeighbourMessageType type;
232 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
233 type = NeighbourMessageType.REQUEST;
234 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
235 target = Ip6Address.valueOf(nsol.getTargetAddress());
236 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
237 type = NeighbourMessageType.REPLY;
238 } else {
239 return null;
240 }
241
242 return new DefaultNeighbourMessageContext(actions, eth, inPort,
243 NeighbourProtocol.NDP, type, target, sender);
244 }
245
246}