Skip to content

Commit

Permalink
babeld-auto-gw-mode: add new package
Browse files Browse the repository at this point in the history
  • Loading branch information
spiccinini committed Feb 12, 2021
1 parent 5398a91 commit 0be853f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/babeld-auto-gw-mode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (C) 2021 Santiago Piccinini <spiccinini@altermundi.net>
#
# This is free software, licensed under the GNU Affero General Public License v3.
#

include ../../libremesh.mk

define Package/$(PKG_NAME)
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Bebeld auto Internet gateway module.
MAINTAINER:=Santiago Piccinini <spiccinini@altermundi.net>
DEPENDS:=+libubus-lua +watchping +lime-proto-babeld
PKGARCH:=all
endef


define Package/$(PKG_NAME)/description
Watchping hooks to set babeld Internet automatic announcements.
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
9 changes: 9 additions & 0 deletions packages/babeld-auto-gw-mode/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# babeld-auto-gw-mode

By default babeld will redistribute all the routes installed even if they "don't work". For example
when the internet provider use DHCP and the service is not working but interface is up, the route is
installed but not working and babeld will anounce the non working route to the network.

This package provides a solucion using watchping hooks that adds routes with a special protocol number
(7) when the WAN port has a working internet access and removes this route when the internet connection
is not working as detected by watchping.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/lua

local uci = require('uci')

local uci = config.get_uci_cursor()

-- Redistribute default routes only if they are of protocol 7.
-- This routes are installed and removed using watchping.

uci:set("babeld", "default4", "proto", "7")
uci:set("babeld", "default6", "proto", "7")
uci:commit("babeld")
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/lua
--! Copyright (C) 2021 Santiago Piccinini <spiccinini@altermundi.net>
--!
--! This is free software, licensed under the GNU Affero General Public License v3.

local ubus = require "ubus"
local utils = require "lime.utils"

local NONWORKING_ROUTE_METRIC = 84831

local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end

local wan4_status = conn:call("network.interface.wan", "status", {})
local wan_ifc = wan4_status.device

--! Do our best to select only the "original" default route installed. Using grep because
--! using the dev argument removes the dev part of the ip route output.
local route = utils.unsafe_shell("ip route show default metric 0 | grep " .. wan_ifc):gsub("\n","")

--! replace the default route with one that has metric NONWORKING_ROUTE_METRIC
if route ~= '' then
os.execute("ip r add " .. route .. " metric " .. tonumber(NONWORKING_ROUTE_METRIC))
os.execute("ip r del " .. route)
end

--! remove the babel redistributable route
os.execute("ip r del default proto 7")
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/lua
--! Copyright (C) 2021 Santiago Piccinini <spiccinini@altermundi.net>
--!
--! This is free software, licensed under the GNU Affero General Public License v3.

local ubus = require "ubus"
local utils = require "lime.utils"

local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end

local wan4_status = conn:call("network.interface.wan", "status", {})
local wan_ifc = wan4_status.device

--! change the default route from the nonworking metric value to the working metric value
local nonworking_route = utils.unsafe_shell("ip route show default metric 84831 | grep " .. wan_ifc):gsub("\n","")
if nonworking_route ~= '' then
os.execute("ip r add " .. nonworking_route .. " metric 0")
os.execute("ip r del " .. nonworking_route .. " metric 84831")
end

--! install a route with proto value 7 that will be redistributed by babeld
for _, route4 in ipairs(wan4_status.route) do
if route4.target == "0.0.0.0" and route4.mask == 0 then
local r = ("ip r add default via " .. route4.nexthop .. " dev " .. wan4_status.device .. " proto 7 metric 10")
os.execute(r)
end
end

0 comments on commit 0be853f

Please sign in to comment.