978851173f448ae1720de5f17dc06534d459ec90
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPUpdateMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Streams;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPError;
22 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
23 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
24 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
25 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser;
26 import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
28 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
30 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
34 import org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes;
35 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
36 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
37 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
38 import org.opendaylight.protocol.util.ByteBufWriteUtil;
39 import org.opendaylight.protocol.util.Ipv4Util;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.TablesKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
58 import org.opendaylight.yangtools.yang.binding.Notification;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
64  *
65  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
66  */
67 public final class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
68
69     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
70
71     public static final int TYPE = 2;
72
73     private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
74
75     private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
76
77     private final AttributeRegistry attrReg;
78
79     private final NlriRegistry nlriReg;
80
81     public BGPUpdateMessageParser(final AttributeRegistry attrReg, final NlriRegistry nlriReg) {
82         this.attrReg = requireNonNull(attrReg);
83         this.nlriReg = requireNonNull(nlriReg);
84     }
85
86     @Override
87     public void serializeMessage(final Notification message, final ByteBuf bytes) {
88         Preconditions.checkArgument(message instanceof Update, "Message needs to be of type Update");
89         final Update update = (Update) message;
90
91         final ByteBuf messageBody = Unpooled.buffer();
92         final List<WithdrawnRoutes> withdrawnRoutes = update.getWithdrawnRoutes();
93         if (withdrawnRoutes != null) {
94             final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
95             withdrawnRoutes.forEach(withdrawnRoute -> writePathIdPrefix(withdrawnRoutesBuf, withdrawnRoute.getPathId(),
96                     withdrawnRoute.getPrefix()));
97             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
98             messageBody.writeBytes(withdrawnRoutesBuf);
99         } else {
100             messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
101         }
102         if (update.getAttributes() != null) {
103             final ByteBuf pathAttributesBuf = Unpooled.buffer();
104             this.attrReg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
105             messageBody.writeShort(pathAttributesBuf.writerIndex());
106             messageBody.writeBytes(pathAttributesBuf);
107         } else {
108             messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
109         }
110         final List<Nlri> nlris = update.getNlri();
111         if (nlris != null) {
112             nlris.forEach(nlri -> writePathIdPrefix(messageBody, nlri.getPathId(), nlri.getPrefix()));
113         }
114         MessageUtil.formatMessage(TYPE, messageBody, bytes);
115     }
116
117     private static void writePathIdPrefix(final ByteBuf byteBuf, final PathId pathId, final Ipv4Prefix ipv4Prefix) {
118         PathIdUtil.writePathId(pathId, byteBuf);
119         ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix, byteBuf);
120     }
121
122     /**
123      * Parse Update message from buffer. Calls {@link #checkMandatoryAttributesPresence(Update, RevisedErrorHandling)}
124      * to check for presence of mandatory attributes.
125      *
126      * @param buffer Encoded BGP message in ByteBuf
127      * @param messageLength Length of the BGP message
128      * @param constraint Peer specific constraints
129      * @return Parsed Update message body
130      */
131     @Override
132     public Update parseMessageBody(final ByteBuf buffer, final int messageLength,
133             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
134         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
135                 "Buffer cannot be null or empty.");
136
137         final UpdateBuilder builder = new UpdateBuilder();
138         final boolean isMultiPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
139                 new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
140         final RevisedErrorHandling errorHandling = RevisedErrorHandling.from(constraint);
141
142         final int withdrawnRoutesLength = buffer.readUnsignedShort();
143         if (withdrawnRoutesLength > 0) {
144             final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
145             final ByteBuf withdrawnRoutesBuffer = buffer.readBytes(withdrawnRoutesLength);
146             while (withdrawnRoutesBuffer.isReadable()) {
147                 final WithdrawnRoutesBuilder withdrawnRoutesBuilder = new WithdrawnRoutesBuilder();
148                 if (isMultiPathSupported) {
149                     withdrawnRoutesBuilder.setPathId(PathIdUtil.readPathId(withdrawnRoutesBuffer));
150                 }
151                 withdrawnRoutesBuilder.setPrefix(readPrefix(withdrawnRoutesBuffer, errorHandling, "Withdrawn Routes"));
152                 withdrawnRoutes.add(withdrawnRoutesBuilder.build());
153             }
154             withdrawnRoutesBuffer.release();
155             builder.setWithdrawnRoutes(withdrawnRoutes);
156         }
157         final int totalPathAttrLength = buffer.readUnsignedShort();
158         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
159             return builder.build();
160         }
161
162         Optional<BGPTreatAsWithdrawException> withdrawCauseOpt;
163         if (totalPathAttrLength > 0) {
164             final ParsedAttributes attributes = parseAttributes(buffer, totalPathAttrLength, constraint);
165             builder.setAttributes(attributes.getAttributes());
166             withdrawCauseOpt = attributes.getWithdrawCause();
167         } else {
168             withdrawCauseOpt = Optional.empty();
169         }
170
171         final List<Nlri> nlri = new ArrayList<>();
172         while (buffer.isReadable()) {
173             final NlriBuilder nlriBuilder = new NlriBuilder();
174             if (isMultiPathSupported) {
175                 nlriBuilder.setPathId(PathIdUtil.readPathId(buffer));
176             }
177             nlriBuilder.setPrefix(readPrefix(buffer, errorHandling, "NLRI"));
178             nlri.add(nlriBuilder.build());
179         }
180         if (!nlri.isEmpty()) {
181             builder.setNlri(nlri);
182         }
183
184         try {
185             checkMandatoryAttributesPresence(builder.build(), errorHandling);
186         } catch (BGPTreatAsWithdrawException e) {
187             LOG.debug("Well-known mandatory attributes missing", e);
188             if (withdrawCauseOpt.isPresent()) {
189                 final BGPTreatAsWithdrawException exception = withdrawCauseOpt.get();
190                 exception.addSuppressed(e);
191                 withdrawCauseOpt = Optional.of(exception);
192             } else {
193                 withdrawCauseOpt = Optional.of(e);
194             }
195         }
196
197         Update msg = builder.build();
198         if (withdrawCauseOpt.isPresent()) {
199             // Attempt to apply treat-as-withdraw
200             msg = withdrawUpdate(msg, errorHandling, withdrawCauseOpt.get());
201         }
202
203         LOG.debug("BGP Update message was parsed {}.", msg);
204         return msg;
205     }
206
207     @SuppressWarnings("checkstyle:illegalCatch")
208     private ParsedAttributes parseAttributes(final ByteBuf buffer, final int totalPathAttrLength,
209             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
210         try {
211             return attrReg.parseAttributes(buffer.readSlice(totalPathAttrLength), constraint);
212         } catch (final RuntimeException | BGPParsingException e) {
213             // Catch everything else and turn it into a BGPDocumentedException
214             throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
215         }
216     }
217
218     private static Ipv4Prefix readPrefix(final ByteBuf buf, final RevisedErrorHandling errorHandling,
219             final String fieldName) throws BGPDocumentedException {
220         final int prefixLength = buf.readUnsignedByte();
221         if (errorHandling != RevisedErrorHandling.NONE) {
222             // https://tools.ietf.org/html/rfc7606#section-5.3
223             if (prefixLength > 32) {
224                 throw new BGPDocumentedException(fieldName + " length " + prefixLength + " exceeds 32 bytes",
225                     BGPError.ATTR_LENGTH_ERROR);
226             }
227             if (prefixLength > buf.readableBytes() * 8) {
228                 throw new BGPDocumentedException(fieldName + " length " + prefixLength
229                     + " exceeds unconsumed field space", BGPError.ATTR_LENGTH_ERROR);
230             }
231         }
232
233         return Ipv4Util.prefixForByteBuf(buf, prefixLength);
234     }
235
236     /**
237      * Check for presence of well known mandatory path attributes ORIGIN, AS_PATH and NEXT_HOP in Update message.
238      *
239      * @param message Update message
240      * @param errorHandling Error handling type
241      */
242     private static void checkMandatoryAttributesPresence(final Update message,
243             final RevisedErrorHandling errorHandling) throws BGPDocumentedException, BGPTreatAsWithdrawException {
244         requireNonNull(message, "Update message cannot be null");
245
246         final Attributes attrs = message.getAttributes();
247         if (message.getNlri() != null && (attrs == null || attrs.getCNextHop() == null)) {
248             throw reportMissingAttribute(errorHandling, "NEXT_HOP", NextHopAttributeParser.TYPE);
249         }
250
251         if (MessageUtil.isAnyNlriPresent(message)) {
252             if (attrs == null || attrs.getOrigin() == null) {
253                 throw reportMissingAttribute(errorHandling, "ORIGIN", OriginAttributeParser.TYPE);
254             }
255             if (attrs.getAsPath() == null) {
256                 throw reportMissingAttribute(errorHandling, "AS_PATH", AsPathAttributeParser.TYPE);
257             }
258         }
259     }
260
261     private static BGPDocumentedException reportMissingAttribute(final RevisedErrorHandling errorHandling,
262             final String attrName, final int attrType) throws BGPDocumentedException, BGPTreatAsWithdrawException {
263         return errorHandling.reportError(BGPError.WELL_KNOWN_ATTR_MISSING, new byte[] { (byte) attrType },
264             "Well known mandatory attribute missing: %s", attrName);
265     }
266
267     private Update withdrawUpdate(final Update parsed, final RevisedErrorHandling errorHandling,
268             final BGPTreatAsWithdrawException withdrawCause) throws BGPDocumentedException {
269         if (errorHandling == RevisedErrorHandling.NONE) {
270             throw new BGPDocumentedException(withdrawCause);
271         }
272
273         // TODO: additional checks as per RFC7606 section 5.2
274
275         LOG.debug("Converting BGP Update message {} to withdraw", parsed, withdrawCause);
276         final UpdateBuilder builder = new UpdateBuilder();
277
278         final List<Nlri> nlris = parsed.getNlri();
279         final List<WithdrawnRoutes> withdrawn;
280         if (nlris != null && !nlris.isEmpty()) {
281             withdrawn = Streams.concat(parsed.nonnullWithdrawnRoutes().stream(),
282                 nlris.stream().map(nlri -> new WithdrawnRoutesBuilder(nlri).build()))
283                     .collect(ImmutableList.toImmutableList());
284         } else {
285             withdrawn = parsed.getWithdrawnRoutes();
286         }
287         builder.setWithdrawnRoutes(withdrawn);
288
289         final Attributes attributes = parsed.getAttributes();
290         if (attributes != null) {
291             builder.setAttributes(withdrawAttributes(attributes, withdrawCause));
292         }
293
294         return builder.build();
295     }
296
297     private Attributes withdrawAttributes(final Attributes parsed,
298             final BGPTreatAsWithdrawException withdrawCause) throws BGPDocumentedException {
299         final AttributesBuilder builder = new AttributesBuilder();
300         final MpReachNlri mpReachNlri = getMpReach(parsed);
301         if (mpReachNlri == null) {
302             // No MP_REACH attribute, just reuse MP_UNREACH if it is present.
303             builder.addAugmentation(Attributes2.class, parsed.augmentation(Attributes2.class));
304             return builder.build();
305         }
306
307         final MpUnreachNlri unreachNlri = getMpUnreach(parsed);
308         if (unreachNlri != null) {
309             final TablesKey reachKey = new TablesKey(mpReachNlri.getAfi(), mpReachNlri.getSafi());
310             final TablesKey unreachKey = new TablesKey(unreachNlri.getAfi(), unreachNlri.getSafi());
311             if (!reachKey.equals(unreachKey)) {
312                 LOG.warn("Unexpected mismatch between MP_REACH ({}) and MP_UNREACH ({})", reachKey, unreachKey,
313                     withdrawCause);
314                 throw new BGPDocumentedException(withdrawCause);
315             }
316         }
317
318         final MpUnreachNlri converted = this.nlriReg.convertMpReachToMpUnReach(mpReachNlri, unreachNlri)
319                 .orElseThrow(() -> {
320                     LOG.warn("Could not convert attributes {} to withdraw attributes", parsed, withdrawCause);
321                     return new BGPDocumentedException(withdrawCause);
322                 });
323
324         builder.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(converted).build());
325         return builder.build();
326     }
327
328     private static MpReachNlri getMpReach(final Attributes attrs) {
329         final Attributes1 reachAttr = attrs.augmentation(Attributes1.class);
330         return reachAttr == null ? null : reachAttr.getMpReachNlri();
331     }
332
333     private static MpUnreachNlri getMpUnreach(final Attributes attrs) {
334         final Attributes2 unreachAttr = attrs.augmentation(Attributes2.class);
335         return unreachAttr == null ? null : unreachAttr.getMpUnreachNlri();
336     }
337 }