1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.controller.admin.network", package.seeall)
8 local uci = require("luci.model.uci").cursor()
12 local has_switch = false
14 uci:foreach("network", "switch",
21 entry({"admin", "network", "switch"}, view("network/switch"), _("Switch"), 20)
25 local has_wifi = false
27 uci:foreach("wireless", "wifi-device",
34 page = entry({"admin", "network", "wireless"}, view("network/wireless"), _('Wireless'), 15)
39 page = entry({"admin", "network", "iface_down"}, post("iface_down"), nil)
42 page = entry({"admin", "network", "network"}, view("network/interfaces"), _("Interfaces"), 10)
47 if nixio.fs.access("/etc/config/dhcp") then
48 page = node("admin", "network", "dhcp")
49 page.target = view("network/dhcp")
50 page.title = _("DHCP and DNS")
53 page = node("admin", "network", "hosts")
54 page.target = view("network/hosts")
55 page.title = _("Hostnames")
59 page = node("admin", "network", "routes")
60 page.target = view("network/routes")
61 page.title = _("Static Routes")
64 page = node("admin", "network", "diagnostics")
65 page.target = template("admin_network/diagnostics")
66 page.title = _("Diagnostics")
69 page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
72 page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
75 page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
78 page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
81 page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
86 local function addr2dev(addr, src)
87 local ip = require "luci.ip"
88 local route = ip.route(addr, src)
89 if not src and route and route.src then
90 route = ip.route(addr, route.src:string())
92 return route and route.dev
95 function iface_down(iface, force)
96 local netmd = require "luci.model.network".init()
97 local peer = luci.http.getenv("REMOTE_ADDR")
98 local serv = luci.http.getenv("SERVER_ADDR")
100 if force ~= "force" and serv and peer then
101 local dev = addr2dev(peer, serv)
103 local nets = netmd:get_networks()
107 for _, net in ipairs(nets) do
108 if net:contains_interface(dev) then
114 if outnet:name() == iface then
115 luci.http.status(409, "Is inbound interface")
119 local peeraddr = outnet:get("peeraddr")
120 for _, ai in ipairs(peeraddr and nixio.getaddrinfo(peeraddr) or {}) do
121 local peerdev = addr2dev(ai.address)
122 for _, net in ipairs(peerdev and nets or {}) do
123 if net:contains_interface(peerdev) and net:name() == iface then
124 luci.http.status(409, "Is inbound interface")
132 if netmd:get_network(iface) then
133 luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null"
134 % luci.util.shellquote(iface))
135 luci.http.status(200, "Shut down")
139 luci.http.status(404, "No such interface")
142 function diag_command(cmd, addr)
143 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
144 luci.http.prepare_content("text/plain")
146 local util = io.popen(cmd % luci.util.shellquote(addr))
149 local ln = util:read("*l")
150 if not ln then break end
152 luci.http.write("\n")
161 luci.http.status(500, "Bad address")
164 function diag_ping(addr)
165 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
168 function diag_traceroute(addr)
169 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
172 function diag_nslookup(addr)
173 diag_command("nslookup %s 2>&1", addr)
176 function diag_ping6(addr)
177 diag_command("ping6 -c 5 %s 2>&1", addr)
180 function diag_traceroute6(addr)
181 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)