Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is free software: you may copy, redistribute and/or modify it |
| 3 | * under the terms of the GNU General Public License as published by the |
| 4 | * Free Software Foundation, either version 2 of the License, or (at your |
| 5 | * option) any later version. |
| 6 | * |
| 7 | * This file is distributed in the hope that it will be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | * General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | * |
| 15 | * This file incorporates work covered by the following copyright and |
| 16 | * permission notice: |
| 17 | * |
| 18 | Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek |
| 19 | |
| 20 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 21 | of this software and associated documentation files (the "Software"), to deal |
| 22 | in the Software without restriction, including without limitation the rights |
| 23 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 24 | copies of the Software, and to permit persons to whom the Software is |
| 25 | furnished to do so, subject to the following conditions: |
| 26 | |
| 27 | The above copyright notice and this permission notice shall be included in |
| 28 | all copies or substantial portions of the Software. |
| 29 | |
| 30 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 36 | THE SOFTWARE. |
| 37 | */ |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <netinet/in.h> |
| 42 | #include <netdb.h> |
| 43 | #include <arpa/inet.h> |
| 44 | |
| 45 | #include <zebra.h> |
| 46 | #include "prefix.h" |
| 47 | #include "zclient.h" |
| 48 | #include "kernel.h" |
| 49 | #include "privs.h" |
| 50 | #include "command.h" |
| 51 | #include "vty.h" |
| 52 | #include "memory.h" |
| 53 | #include "thread.h" |
| 54 | |
| 55 | #include "util.h" |
| 56 | #include "babel_interface.h" |
| 57 | #include "babel_zebra.h" |
| 58 | |
| 59 | |
| 60 | static int |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 61 | kernel_route_v4(int add, const unsigned char *pref, unsigned short plen, |
| 62 | const unsigned char *gate, int ifindex, |
| 63 | unsigned int metric); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 64 | static int |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 65 | kernel_route_v6(int add, const unsigned char *pref, unsigned short plen, |
| 66 | const unsigned char *gate, int ifindex, |
| 67 | unsigned int metric); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 68 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 69 | int |
| 70 | kernel_interface_operational(struct interface *interface) |
| 71 | { |
| 72 | return if_is_operative(interface); |
| 73 | } |
| 74 | |
| 75 | int |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 76 | kernel_interface_mtu(struct interface *interface) |
| 77 | { |
| 78 | return MIN(interface->mtu, interface->mtu6); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | kernel_interface_wireless(struct interface *interface) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 87 | int |
| 88 | kernel_route(int operation, const unsigned char *pref, unsigned short plen, |
| 89 | const unsigned char *gate, int ifindex, unsigned int metric, |
| 90 | const unsigned char *newgate, int newifindex, |
| 91 | unsigned int newmetric) |
| 92 | { |
| 93 | int rc; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 94 | int ipv4; |
| 95 | |
| 96 | /* Check that the protocol family is consistent. */ |
| 97 | if(plen >= 96 && v4mapped(pref)) { |
| 98 | if(!v4mapped(gate)) { |
| 99 | errno = EINVAL; |
| 100 | return -1; |
| 101 | } |
| 102 | ipv4 = 1; |
| 103 | } else { |
| 104 | if(v4mapped(gate)) { |
| 105 | errno = EINVAL; |
| 106 | return -1; |
| 107 | } |
| 108 | ipv4 = 0; |
| 109 | } |
| 110 | |
| 111 | switch (operation) { |
| 112 | case ROUTE_ADD: |
| 113 | return ipv4 ? |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 114 | kernel_route_v4(1, pref, plen, gate, ifindex, metric): |
| 115 | kernel_route_v6(1, pref, plen, gate, ifindex, metric); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 116 | break; |
| 117 | case ROUTE_FLUSH: |
| 118 | return ipv4 ? |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 119 | kernel_route_v4(0, pref, plen, gate, ifindex, metric): |
| 120 | kernel_route_v6(0, pref, plen, gate, ifindex, metric); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 121 | break; |
| 122 | case ROUTE_MODIFY: |
| 123 | if(newmetric == metric && memcmp(newgate, gate, 16) == 0 && |
| 124 | newifindex == ifindex) |
| 125 | return 0; |
Matthieu Boutier | ec8d8d5 | 2012-01-20 15:32:16 +0100 | [diff] [blame] | 126 | debugf(BABEL_DEBUG_ROUTE, "Modify route: delete old; add new."); |
Matthieu Boutier | 53b2195 | 2012-01-31 17:09:55 +0100 | [diff] [blame] | 127 | rc = ipv4 ? |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 128 | kernel_route_v4(0, pref, plen, gate, ifindex, metric): |
| 129 | kernel_route_v6(0, pref, plen, gate, ifindex, metric); |
Matthieu Boutier | 53b2195 | 2012-01-31 17:09:55 +0100 | [diff] [blame] | 130 | |
| 131 | if (rc < 0) |
| 132 | return -1; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 133 | |
Matthieu Boutier | ec8d8d5 | 2012-01-20 15:32:16 +0100 | [diff] [blame] | 134 | rc = ipv4 ? |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 135 | kernel_route_v4(1, pref, plen, newgate, newifindex, newmetric): |
| 136 | kernel_route_v6(1, pref, plen, newgate, newifindex, newmetric); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 137 | |
| 138 | return rc; |
| 139 | break; |
| 140 | default: |
| 141 | zlog_err("this should never appens (false value - kernel_route)"); |
| 142 | assert(0); |
| 143 | exit(1); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 149 | kernel_route_v4(int add, |
| 150 | const unsigned char *pref, unsigned short plen, |
| 151 | const unsigned char *gate, int ifindex, unsigned int metric) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 152 | { |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 153 | struct zapi_ipv4 api; /* quagga's communication system */ |
| 154 | struct prefix_ipv4 quagga_prefix; /* quagga's prefix */ |
| 155 | struct in_addr babel_prefix_addr; /* babeld's prefix addr */ |
| 156 | struct in_addr nexthop; /* next router to go */ |
| 157 | struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */ |
| 158 | |
Matthieu Boutier | 53b2195 | 2012-01-31 17:09:55 +0100 | [diff] [blame] | 159 | /* convert to be understandable by quagga */ |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 160 | /* convert given addresses */ |
| 161 | uchar_to_inaddr(&babel_prefix_addr, pref); |
| 162 | uchar_to_inaddr(&nexthop, gate); |
| 163 | |
| 164 | /* make prefix structure */ |
| 165 | memset (&quagga_prefix, 0, sizeof(quagga_prefix)); |
| 166 | quagga_prefix.family = AF_INET; |
| 167 | IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr); |
| 168 | quagga_prefix.prefixlen = plen - 96; /* our plen is for v4mapped's addr */ |
| 169 | apply_mask_ipv4(&quagga_prefix); |
| 170 | |
| 171 | api.type = ZEBRA_ROUTE_BABEL; |
| 172 | api.flags = 0; |
| 173 | api.message = 0; |
Denis Ovsienko | a19a3bf | 2012-01-21 23:16:00 +0400 | [diff] [blame] | 174 | api.safi = SAFI_UNICAST; |
Juliusz Chroboczek | b6475ec | 2012-02-09 12:29:10 +0100 | [diff] [blame] | 175 | |
| 176 | /* Unlike the native Linux and BSD interfaces, Quagga doesn't like |
| 177 | there to be both and IPv4 nexthop and an ifindex. Omit the |
| 178 | ifindex, and assume that the connected prefixes be set up |
| 179 | correctly. */ |
| 180 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 181 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
Juliusz Chroboczek | b6475ec | 2012-02-09 12:29:10 +0100 | [diff] [blame] | 182 | api.ifindex_num = 0; |
Juliusz Chroboczek | d70ab9d | 2012-02-09 17:23:09 +0100 | [diff] [blame] | 183 | if(metric >= KERNEL_INFINITY) { |
| 184 | api.flags = ZEBRA_FLAG_BLACKHOLE; |
| 185 | api.nexthop_num = 0; |
| 186 | } else { |
| 187 | api.nexthop_num = 1; |
| 188 | api.nexthop = &nexthop_pointer; |
| 189 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); |
| 190 | api.metric = metric; |
| 191 | } |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 192 | |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 193 | debugf(BABEL_DEBUG_ROUTE, "%s route (ipv4) to zebra", |
| 194 | add ? "adding" : "removing" ); |
| 195 | return zapi_ipv4_route (add ? ZEBRA_IPV4_ROUTE_ADD : |
| 196 | ZEBRA_IPV4_ROUTE_DELETE, |
| 197 | zclient, &quagga_prefix, &api); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 201 | kernel_route_v6(int add, const unsigned char *pref, unsigned short plen, |
| 202 | const unsigned char *gate, int ifindex, unsigned int metric) |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 203 | { |
| 204 | unsigned int tmp_ifindex = ifindex; /* (for typing) */ |
| 205 | struct zapi_ipv6 api; /* quagga's communication system */ |
| 206 | struct prefix_ipv6 quagga_prefix; /* quagga's prefix */ |
| 207 | struct in6_addr babel_prefix_addr; /* babeld's prefix addr */ |
| 208 | struct in6_addr nexthop; /* next router to go */ |
| 209 | struct in6_addr *nexthop_pointer = &nexthop; |
| 210 | |
Matthieu Boutier | 53b2195 | 2012-01-31 17:09:55 +0100 | [diff] [blame] | 211 | /* convert to be understandable by quagga */ |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 212 | /* convert given addresses */ |
| 213 | uchar_to_in6addr(&babel_prefix_addr, pref); |
| 214 | uchar_to_in6addr(&nexthop, gate); |
| 215 | |
| 216 | /* make prefix structure */ |
| 217 | memset (&quagga_prefix, 0, sizeof(quagga_prefix)); |
| 218 | quagga_prefix.family = AF_INET6; |
| 219 | IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr); |
| 220 | quagga_prefix.prefixlen = plen; |
| 221 | apply_mask_ipv6(&quagga_prefix); |
| 222 | |
| 223 | api.type = ZEBRA_ROUTE_BABEL; |
| 224 | api.flags = 0; |
| 225 | api.message = 0; |
Denis Ovsienko | a19a3bf | 2012-01-21 23:16:00 +0400 | [diff] [blame] | 226 | api.safi = SAFI_UNICAST; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 227 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
Juliusz Chroboczek | d70ab9d | 2012-02-09 17:23:09 +0100 | [diff] [blame] | 228 | if(metric >= KERNEL_INFINITY) { |
Juliusz Chroboczek | b63b448 | 2012-02-14 11:17:32 +0100 | [diff] [blame] | 229 | api.flags = ZEBRA_FLAG_BLACKHOLE; |
Juliusz Chroboczek | d70ab9d | 2012-02-09 17:23:09 +0100 | [diff] [blame] | 230 | api.nexthop_num = 0; |
| 231 | api.ifindex_num = 0; |
| 232 | } else { |
| 233 | api.nexthop_num = 1; |
| 234 | api.nexthop = &nexthop_pointer; |
| 235 | SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX); |
| 236 | api.ifindex_num = 1; |
| 237 | api.ifindex = &tmp_ifindex; |
| 238 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); |
| 239 | api.metric = metric; |
| 240 | } |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 241 | |
Juliusz Chroboczek | 5ca7986 | 2012-02-14 11:22:29 +0100 | [diff] [blame] | 242 | debugf(BABEL_DEBUG_ROUTE, "%s route (ipv6) to zebra", |
| 243 | add ? "adding" : "removing" ); |
| 244 | return zapi_ipv6_route (add ? ZEBRA_IPV6_ROUTE_ADD : |
| 245 | ZEBRA_IPV6_ROUTE_DELETE, |
| 246 | zclient, &quagga_prefix, &api); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 250 | if_eui64(char *ifname, int ifindex, unsigned char *eui) |
| 251 | { |
| 252 | struct interface *ifp = if_lookup_by_index(ifindex); |
| 253 | if (ifp == NULL) { |
| 254 | return -1; |
| 255 | } |
| 256 | #ifdef HAVE_STRUCT_SOCKADDR_DL |
| 257 | u_char len = ifp->sdl.sdl_alen; |
| 258 | char *tmp = ifp->sdl.sdl_data + ifp->sdl.sdl_nlen; |
| 259 | #else |
| 260 | u_char len = (u_char) ifp->hw_addr_len; |
| 261 | char *tmp = (void*) ifp->hw_addr; |
| 262 | #endif |
| 263 | if (len == 8) { |
| 264 | memcpy(eui, tmp, 8); |
| 265 | eui[0] ^= 2; |
| 266 | } else if (len == 6) { |
| 267 | memcpy(eui, tmp, 3); |
| 268 | eui[3] = 0xFF; |
| 269 | eui[4] = 0xFE; |
| 270 | memcpy(eui+5, tmp+3, 3); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 271 | } else { |
| 272 | return -1; |
| 273 | } |
| 274 | return 0; |
| 275 | } |