#
# OCNバーチャルコネクトでインターネット接続
#

#
# ひかり電話契約あり
# 固定IP1契約 の場合
#

ip route default gateway tunnel 1
ipv6 route default gateway dhcp lan2
ipv6 prefix 1 dhcp-prefix@lan2::/64
ipv6 source address selection rule lifetime
ip lan1 address 192.168.100.1/24
ipv6 lan1 address dhcp-prefix@lan2::1/64
ipv6 lan1 rtadv send 1 o_flag=on
ipv6 lan1 dhcp service server
ip lan2 address dhcp
ipv6 lan2 address dhcp
ipv6 lan2 prefix change log on
ipv6 lan2 secure filter in 101000 101001 101002 101003
ipv6 lan2 secure filter out 101099 dynamic 101080 101081 101082 101083 101084 101085 101098 101099
ipv6 lan2 dhcp service client
ngn type lan2 ntt
tunnel select 1
 tunnel encapsulation map-e
 tunnel map-e type ocn
 ip tunnel mtu 1460
 ip tunnel secure filter in 400003 400020 400021 400022 400023 400024 400025 400030 400032
 ip tunnel secure filter out 400013 400020 400021 400022 400023 400024 400025 400026 400027 400099 dynamic 400080 400081 400082 400083 400084 400085 400098 400099
 ip tunnel nat descriptor 20000
 tunnel enable 1
ip filter 400000 reject 10.0.0.0/8 * * * *
ip filter 400001 reject 172.16.0.0/12 * * * *
ip filter 400002 reject 192.168.0.0/16 * * * *
ip filter 400003 reject 192.168.100.0/24 * * * *
ip filter 400010 reject * 10.0.0.0/8 * * *
ip filter 400011 reject * 172.16.0.0/12 * * *
ip filter 400012 reject * 192.168.0.0/16 * * *
ip filter 400013 reject * 192.168.100.0/24 * * *
ip filter 400020 reject * * udp,tcp 135 *
ip filter 400021 reject * * udp,tcp * 135
ip filter 400022 reject * * udp,tcp netbios_ns-netbios_ssn *
ip filter 400023 reject * * udp,tcp * netbios_ns-netbios_ssn
ip filter 400024 reject * * udp,tcp 445 *
ip filter 400025 reject * * udp,tcp * 445
ip filter 400026 restrict * * tcpfin * www,21,nntp
ip filter 400027 restrict * * tcprst * www,21,nntp
ip filter 400030 pass * 192.168.100.0/24 icmp * *
ip filter 400031 pass * 192.168.100.0/24 established * *
ip filter 400032 pass * 192.168.100.0/24 tcp * ident
ip filter 400033 pass * 192.168.100.0/24 tcp ftpdata *
ip filter 400034 pass * 192.168.100.0/24 tcp,udp * domain
ip filter 400035 pass * 192.168.100.0/24 udp domain *
ip filter 400036 pass * 192.168.100.0/24 udp * ntp
ip filter 400037 pass * 192.168.100.0/24 udp ntp *
ip filter 400099 pass * * * * *
ip filter 500000 restrict * * * * *
ip filter dynamic 400080 * * ftp
ip filter dynamic 400081 * * domain
ip filter dynamic 400082 * * www
ip filter dynamic 400083 * * smtp
ip filter dynamic 400084 * * pop3
ip filter dynamic 400085 * * submission
ip filter dynamic 400098 * * tcp
ip filter dynamic 400099 * * udp
nat descriptor type 20000 masquerade
nat descriptor address outer 20000 map-e
ipv6 filter 101000 pass * * icmp6 * *
ipv6 filter 101001 pass * * tcp * ident
ipv6 filter 101002 pass * * udp * 546
ipv6 filter 101003 pass * * 4
ipv6 filter 101099 pass * * * * *
ipv6 filter dynamic 101080 * * ftp
ipv6 filter dynamic 101081 * * domain
ipv6 filter dynamic 101082 * * www
ipv6 filter dynamic 101083 * * smtp
ipv6 filter dynamic 101084 * * pop3
ipv6 filter dynamic 101085 * * submission
ipv6 filter dynamic 101098 * * tcp
ipv6 filter dynamic 101099 * * udp
telnetd host lan
dhcp service server
dhcp server rfc2131 compliant except remain-silent
dhcp scope 1 192.168.100.2-192.168.100.191/24
dhcp client release linkdown on
dns host lan1
dns service fallback on
dns server dhcp lan2
dns server select 500000 dhcp lan2 any .
dns private address spoof on
schedule at 1 startup * lua emfs:/ocn_map_e.lua
sip use on
statistics traffic on
embedded file ocn_map_e.lua <<EOF
SERVER_URL = "(アドレス解決システム URL)"
USERNAME = "(認証用ID)"
PASSWORD = "(認証用パスワード)"
WAN_IF = "LAN2"

LOG_PTN1 = WAN_IF .. ": link up"
LOG_PTN2 = "%[OCN MAP%-E%] hostname="
PTN = /(Add IPv6 prefix|LAN2: link up|hostname=)/
LOG_LEVEL = "info"
LOG_PFX = "[OCN MAP-E]"

function logger(msg)
  rt.syslog(LOG_LEVEL, string.format("%s %s", LOG_PFX, msg))
end
function retry_timer(t, c)
  return (t * 2 * c)
end
local rtn, str, count, log, sleep_time, hostname, tmp
local req_t = {}
local res_t
math.randomseed(os.time())
req_t.method = "GET"

req_t.auth_type = "basic"
req_t.auth_name = USERNAME
req_t.auth_pass = PASSWORD
rtn, str = rt.syslogwatch(LOG_PTN2, 1, 30)
if (rtn > 0) then
  tmp, hostname = string.split(str[1], "=")
end
while true do
  count = 1
  while hostname do
    req_t.url = string.format("%s?hostname=%s", SERVER_URL, hostname)
    res_t = rt.httprequest(req_t)
    if res_t.rtn1 then
      logger("Notified IPv6 address to the address resolution server.")
      if res_t.code == 200 then
        if string.match(res_t.body, "good") or string.match(res_t.body, "nochg") or string.match(res_t.body, "nohost") then
          log = string.format("Succeeded to notify IPv6 address to the address resolution server. (code=%d)", res_t.code)
          logger(log)
          break
        end
      end
      log = string.format("Failed to notify IPv6 address to the address resolution server. (code=%d)", res_t.code)
      logger(log)
    end
    sleep_time = retry_timer(60, count)
    if (sleep_time > 3600) then
      sleep_time = 3600
    else
      count = count * 2
    end
    rt.sleep(sleep_time)
  end
  sleep_time = math.random(43200, 86400)
  rtn, str = rt.syslogwatch(PTN, 1, sleep_time)
  if (rtn > 0) and string.match(str[1], LOG_PTN2) then
    tmp, hostname = string.split(str[1], "=")
  elseif (rtn > 0) and string.match(str[1], LOG_PTN1) then
    rt.sleep(5)
  end
end
EOF