slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 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 | package org.onosproject.artemis.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.json.JSONArray; |
| 20 | import org.json.JSONException; |
| 21 | import org.json.JSONObject; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onosproject.artemis.impl.bgpspeakers.BgpSpeakers; |
| 25 | import org.onosproject.artemis.impl.bgpspeakers.QuaggaBgpSpeakers; |
| 26 | import org.onosproject.routing.bgp.BgpInfoService; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | /** |
| 34 | * Timertask class which detects and mitigates BGP hijacks. |
| 35 | */ |
| 36 | class Deaggregator extends java.util.TimerTask { |
| 37 | |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | private Set<ArtemisConfig.ArtemisPrefixes> prefixes = Sets.newHashSet(); |
| 40 | private Set<BgpSpeakers> bgpSpeakers = Sets.newHashSet(); |
| 41 | |
| 42 | Deaggregator(BgpInfoService bgpInfoService) { |
| 43 | super(); |
| 44 | // deaggregator must know the type of the connected BGP speakers and the BGP info. |
| 45 | // for this example we only have one Quagga BGP speaker. |
| 46 | bgpSpeakers.add(new QuaggaBgpSpeakers(bgpInfoService)); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void run() { |
| 51 | ArrayList<JSONObject> messagesArray = DataHandler.getInstance().getData(); |
| 52 | // log.info("Messages size: " + messagesArray.size()); |
| 53 | |
| 54 | // Example of BGP Update message: |
| 55 | // { |
| 56 | // "path":[65001, 65002, 65004], (origin being last) |
| 57 | // "prefix":"12.0.0.0/8", |
| 58 | // } |
| 59 | |
| 60 | prefixes.forEach(prefix -> { |
| 61 | IpPrefix monitoredPrefix = prefix.prefix(); |
| 62 | |
| 63 | // for each update message in memory check for hijack |
| 64 | for (JSONObject tmp : messagesArray) { |
| 65 | IpPrefix receivedPrefix = null; |
| 66 | try { |
| 67 | receivedPrefix = IpPrefix.valueOf(tmp.getString("prefix")); |
| 68 | } catch (JSONException e) { |
| 69 | log.warn("JSONException: " + e.getMessage()); |
| 70 | e.printStackTrace(); |
| 71 | } |
| 72 | if (receivedPrefix == null) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | // check if the announced network address is inside our subnet |
| 77 | if (monitoredPrefix.contains(receivedPrefix)) { |
| 78 | JSONArray path = null; |
| 79 | try { |
| 80 | path = tmp.getJSONArray("path"); |
| 81 | } catch (JSONException e) { |
| 82 | log.warn("JSONException: " + e.getMessage()); |
| 83 | e.printStackTrace(); |
| 84 | } |
| 85 | if (path == null) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | int state = prefix.checkPath(path); |
| 90 | if (state >= 100) { |
| 91 | log.warn("BGP Hijack detected of type " + |
| 92 | (state - 100) + "\n" + tmp.toString()); |
| 93 | DataHandler.Serializer.writeHijack(tmp); |
| 94 | // can only de-aggregate /23 subnets and higher |
| 95 | int cidr = receivedPrefix.prefixLength(); |
| 96 | if (receivedPrefix.prefixLength() < 24) { |
| 97 | byte[] octets = receivedPrefix.address().toOctets(); |
| 98 | int byteGroup = (cidr + 1) / 8, |
| 99 | bitPos = 8 - (cidr + 1) % 8; |
| 100 | |
| 101 | octets[byteGroup] = (byte) (octets[byteGroup] & ~(1 << bitPos)); |
| 102 | String low = IpPrefix.valueOf(IpAddress.Version.INET, octets, cidr + 1).toString(); |
| 103 | octets[byteGroup] = (byte) (octets[byteGroup] | (1 << bitPos)); |
| 104 | String high = IpPrefix.valueOf(IpAddress.Version.INET, octets, cidr + 1).toString(); |
| 105 | |
| 106 | String[] prefixes = {low, high}; |
| 107 | bgpSpeakers.forEach(bgpSpeakers -> bgpSpeakers.announceSubPrefixes(prefixes)); |
| 108 | } else { |
| 109 | log.warn("Cannot announce smaller prefix than /24"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | public Set<ArtemisConfig.ArtemisPrefixes> getPrefixes() { |
| 118 | return prefixes; |
| 119 | } |
| 120 | |
| 121 | public void setPrefixes(Set<ArtemisConfig.ArtemisPrefixes> prefixes) { |
| 122 | this.prefixes = prefixes; |
| 123 | } |
| 124 | } |