summaryrefslogtreecommitdiff
path: root/babeld/kernel_zebra.c
diff options
context:
space:
mode:
Diffstat (limited to 'babeld/kernel_zebra.c')
-rw-r--r--babeld/kernel_zebra.c461
1 files changed, 461 insertions, 0 deletions
diff --git a/babeld/kernel_zebra.c b/babeld/kernel_zebra.c
new file mode 100644
index 00000000..0fecb527
--- /dev/null
+++ b/babeld/kernel_zebra.c
@@ -0,0 +1,461 @@
+/*
+ * This file is free software: you may copy, redistribute and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * This file incorporates work covered by the following copyright and
+ * permission notice:
+ *
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#include <zebra.h>
+#include "prefix.h"
+#include "zclient.h"
+#include "kernel.h"
+#include "privs.h"
+#include "command.h"
+#include "vty.h"
+#include "memory.h"
+#include "thread.h"
+
+#include "util.h"
+#include "babel_interface.h"
+#include "babel_zebra.h"
+
+
+static int
+kernel_route_add_v4(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex, unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric);
+static int
+kernel_route_add_v6(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex, unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric);
+static int
+kernel_route_delete_v4(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex,
+ unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric);
+static int
+kernel_route_delete_v6(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex,
+ unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric);
+
+
+int export_table = -1, import_table = -1; /* just for compatibility */
+
+int
+kernel_setup(int setup)
+{
+ return 0;
+}
+
+/* get a connection with zebra client, at all costs */
+int
+kernel_setup_socket(int setup)
+{
+ return -1;
+}
+
+int
+kernel_setup_interface(int setup, struct interface *interface)
+{
+ return 1;
+}
+
+int
+kernel_interface_operational(struct interface *interface)
+{
+ return if_is_operative(interface);
+}
+
+int
+kernel_interface_ipv4(struct interface *interface, unsigned char *addr_r)
+{
+ assert(0); /* function not used */
+ return -1;
+}
+
+int
+kernel_interface_mtu(struct interface *interface)
+{
+ return MIN(interface->mtu, interface->mtu6);
+}
+
+int
+kernel_interface_wireless(struct interface *interface)
+{
+ return 0;
+}
+
+extern int
+zapi_ipv6_route (u_char cmd, struct zclient *zclient,
+ struct prefix_ipv6 *p, struct zapi_ipv6 *api);
+
+int
+kernel_route(int operation, const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex, unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric)
+{
+ int rc;
+ int added;
+ int ipv4;
+
+ /* Check that the protocol family is consistent. */
+ if(plen >= 96 && v4mapped(pref)) {
+ if(!v4mapped(gate)) {
+ errno = EINVAL;
+ return -1;
+ }
+ ipv4 = 1;
+ } else {
+ if(v4mapped(gate)) {
+ errno = EINVAL;
+ return -1;
+ }
+ ipv4 = 0;
+ }
+
+ switch (operation) {
+ case ROUTE_ADD:
+ return ipv4 ?
+ kernel_route_add_v4(pref, plen, gate, ifindex, metric,
+ newgate, newifindex, newmetric):
+ kernel_route_add_v6(pref, plen, gate, ifindex, metric,
+ newgate, newifindex, newmetric);
+ break;
+ case ROUTE_FLUSH:
+ return ipv4 ?
+ kernel_route_delete_v4(pref, plen, gate, ifindex, metric,
+ newgate, newifindex, newmetric):
+ kernel_route_delete_v6(pref, plen, gate, ifindex, metric,
+ newgate, newifindex, newmetric);
+ break;
+ case ROUTE_MODIFY:
+ if(newmetric == metric && memcmp(newgate, gate, 16) == 0 &&
+ newifindex == ifindex)
+ return 0;
+ /* It is better to add the new route before removing the old
+ one, to avoid losing packets. However, if the old and new
+ priorities are equal, this only works if the kernel supports
+ ECMP. So we first try the "right" order, and fall back on
+ the "wrong" order if it fails with EEXIST. */
+ rc = ipv4 ?
+ kernel_route_add_v4(pref, plen,
+ newgate, newifindex, newmetric,
+ NULL, 0, 0):
+ kernel_route_add_v6(pref, plen,
+ newgate, newifindex, newmetric,
+ NULL, 0, 0);
+ if(rc < 0) {
+ if(errno != EEXIST)
+ return rc;
+ added = 0;
+ } else {
+ added = 1;
+ }
+
+ if (ipv4) {
+ kernel_route_delete_v4(pref, plen,
+ gate, ifindex, metric,
+ NULL, 0, 0);
+ } else {
+ kernel_route_delete_v6(pref, plen,
+ gate, ifindex, metric,
+ NULL, 0, 0);
+ }
+
+ if(!added) {
+ rc = ipv4 ?
+ kernel_route_add_v4(pref, plen,
+ newgate, newifindex, newmetric,
+ NULL, 0, 0):
+ kernel_route_add_v6(pref, plen,
+ newgate, newifindex, newmetric,
+ NULL, 0, 0);
+ if(rc < 0) {
+ if(errno == EEXIST)
+ rc = 1;
+ /* In principle, we should try to re-install the flushed
+ route on failure to preserve. However, this should
+ hopefully not matter much in practice. */
+ }
+ }
+
+ return rc;
+ break;
+ default:
+ zlog_err("this should never appens (false value - kernel_route)");
+ assert(0);
+ exit(1);
+ break;
+ }
+}
+
+static int
+kernel_route_add_v4(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex, unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric)
+{
+ unsigned int tmp_ifindex = ifindex; /* (for typing) */
+ struct zapi_ipv4 api; /* quagga's communication system */
+ struct prefix_ipv4 quagga_prefix; /* quagga's prefix */
+ struct in_addr babel_prefix_addr; /* babeld's prefix addr */
+ struct in_addr nexthop; /* next router to go */
+ struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */
+
+ /* convert to be comprehensive by quagga */
+ /* convert given addresses */
+ uchar_to_inaddr(&babel_prefix_addr, pref);
+ uchar_to_inaddr(&nexthop, gate);
+
+ /* make prefix structure */
+ memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+ quagga_prefix.family = AF_INET;
+ IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+ quagga_prefix.prefixlen = plen - 96; /* our plen is for v4mapped's addr */
+ apply_mask_ipv4(&quagga_prefix);
+
+ api.type = ZEBRA_ROUTE_BABEL;
+ api.flags = 0;
+ api.message = 0;
+ SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+ api.nexthop_num = 1;
+ api.nexthop = &nexthop_pointer;
+ SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
+ api.ifindex_num = 1;
+ api.ifindex = &tmp_ifindex;
+ SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
+ api.metric = metric;
+
+ debugf(BABEL_DEBUG_ROUTE, "adding route (ipv4) to zebra");
+ return zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient,
+ &quagga_prefix, &api);
+}
+
+static int
+kernel_route_add_v6(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex, unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric)
+{
+ unsigned int tmp_ifindex = ifindex; /* (for typing) */
+ struct zapi_ipv6 api; /* quagga's communication system */
+ struct prefix_ipv6 quagga_prefix; /* quagga's prefix */
+ struct in6_addr babel_prefix_addr; /* babeld's prefix addr */
+ struct in6_addr nexthop; /* next router to go */
+ struct in6_addr *nexthop_pointer = &nexthop;
+
+ /* convert to be comprehensive by quagga */
+ /* convert given addresses */
+ uchar_to_in6addr(&babel_prefix_addr, pref);
+ uchar_to_in6addr(&nexthop, gate);
+
+ /* make prefix structure */
+ memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+ quagga_prefix.family = AF_INET6;
+ IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+ quagga_prefix.prefixlen = plen;
+ apply_mask_ipv6(&quagga_prefix);
+
+ api.type = ZEBRA_ROUTE_BABEL;
+ api.flags = 0;
+ api.message = 0;
+ SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+ api.nexthop_num = 1;
+ api.nexthop = &nexthop_pointer;
+ SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
+ api.ifindex_num = 1;
+ api.ifindex = &tmp_ifindex;
+ SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
+ api.metric = metric;
+
+ debugf(BABEL_DEBUG_ROUTE, "adding route (ipv6) to zebra");
+ return zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient,
+ &quagga_prefix, &api);
+}
+
+static int
+kernel_route_delete_v4(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex,
+ unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric)
+{
+ unsigned int tmp_ifindex = ifindex; /* (for typing) */
+ struct zapi_ipv4 api; /* quagga's communication system */
+ struct prefix_ipv4 quagga_prefix; /* quagga's prefix */
+ struct in_addr babel_prefix_addr; /* babeld's prefix addr */
+ struct in_addr nexthop; /* next router to go */
+ struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */
+
+ /* convert to be comprehensive by quagga */
+ /* convert given addresses */
+ uchar_to_inaddr(&babel_prefix_addr, pref);
+ uchar_to_inaddr(&nexthop, gate);
+
+ /* make prefix structure */
+ memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+ quagga_prefix.family = AF_INET;
+ IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+ quagga_prefix.prefixlen = plen - 96;
+ apply_mask_ipv4(&quagga_prefix);
+
+ api.type = ZEBRA_ROUTE_BABEL;
+ api.flags = 0;
+ api.message = 0;
+ SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+ api.nexthop_num = 1;
+ api.nexthop = &nexthop_pointer;
+ SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
+ api.ifindex_num = 1;
+ api.ifindex = &tmp_ifindex;
+ SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
+ api.metric = metric;
+
+ debugf(BABEL_DEBUG_ROUTE, "removing route (ipv4) to zebra");
+ return zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
+ &quagga_prefix, &api);
+}
+
+static int
+kernel_route_delete_v6(const unsigned char *pref, unsigned short plen,
+ const unsigned char *gate, int ifindex,
+ unsigned int metric,
+ const unsigned char *newgate, int newifindex,
+ unsigned int newmetric)
+{
+ unsigned int tmp_ifindex = ifindex; /* (for typing) */
+ struct zapi_ipv6 api; /* quagga's communication system */
+ struct prefix_ipv6 quagga_prefix; /* quagga's prefix */
+ struct in6_addr babel_prefix_addr; /* babeld's prefix addr */
+ struct in6_addr nexthop; /* next router to go */
+ struct in6_addr *nexthop_pointer = &nexthop;
+
+ /* convert to be comprehensive by quagga */
+ /* convert given addresses */
+ uchar_to_in6addr(&babel_prefix_addr, pref);
+ uchar_to_in6addr(&nexthop, gate);
+
+ /* make prefix structure */
+ memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+ quagga_prefix.family = AF_INET6;
+ IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+ quagga_prefix.prefixlen = plen;
+ apply_mask_ipv6(&quagga_prefix);
+
+ api.type = ZEBRA_ROUTE_BABEL;
+ api.flags = 0;
+ api.message = 0;
+ SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+ api.nexthop_num = 1;
+ api.nexthop = &nexthop_pointer;
+ SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
+ api.ifindex_num = 1;
+ api.ifindex = &tmp_ifindex;
+
+ debugf(BABEL_DEBUG_ROUTE, "removing route (ipv6) to zebra");
+ return zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
+ &quagga_prefix, &api);
+}
+
+int
+kernel_routes(struct kernel_route *routes, int maxroutes)
+{
+ fprintf(stderr, "\tkernel_routes --- not implemented\n");
+ return 0;
+}
+
+int
+kernel_callback(int (*fn)(int, void*), void *closure)
+{
+ struct thread thread;
+ fprintf(stderr, "\tkernel_callback\n");
+ /* do a little work on threads */
+ if (thread_fetch(master, &thread) != NULL) {
+ thread_call (&thread);
+ }
+ return 0;
+}
+
+int
+kernel_addresses(struct interface *interface, int ll,
+ struct kernel_route *routes, int maxroutes)
+{
+ fprintf(stderr, "\tkernel_addresses --- not implemented\n");
+ return 0;
+}
+
+int
+if_eui64(char *ifname, int ifindex, unsigned char *eui)
+{
+ struct interface *ifp = if_lookup_by_index(ifindex);
+ if (ifp == NULL) {
+ return -1;
+ }
+#ifdef HAVE_STRUCT_SOCKADDR_DL
+ u_char len = ifp->sdl.sdl_alen;
+ char *tmp = ifp->sdl.sdl_data + ifp->sdl.sdl_nlen;
+#else
+ u_char len = (u_char) ifp->hw_addr_len;
+ char *tmp = (void*) ifp->hw_addr;
+#endif
+ if (len == 8) {
+ memcpy(eui, tmp, 8);
+ eui[0] ^= 2;
+ } else if (len == 6) {
+ memcpy(eui, tmp, 3);
+ eui[3] = 0xFF;
+ eui[4] = 0xFE;
+ memcpy(eui+5, tmp+3, 3);
+ } else if (len > 8) {
+ memcpy(eui, tmp, 8);
+ } else if (len > 0){
+ memset(eui, 0, 8 - len);
+ memcpy(eui + 8 - len, tmp, len);
+ } else {
+ return -1;
+ }
+ return 0;
+}