查看: 84|回复: 0

[Mikrotik Ros] Mikrotik RouterOS ipv6-npt脚本

[复制链接]

124

主题

0

回帖

508

积分

管理员

积分
508
发表于 2026-8-20 21:30:40 | 显示全部楼层 |阅读模式
  1. #!rsc by RouterOS
  2. #
  3. # Set up NPTv6.
  4. #
  5. # GUA prefix is reserved by allocating a network from $argWanPool
  6. # on $argLoopbackInt. Once GUA network is available, its address-prefix
  7. # is used to set up mangling rules to perform network prefix translation
  8. # with addresses from $argUlaPool.
  9. #
  10. # Since mangling is performed after connection tracking, all mangled packets
  11. # continue processing with connection-state=invalid. Thus raw rules are added
  12. # to mark these packets for notrack. Adjust your filter rules accordingly.
  13. #
  14. # Optional arg*Extra can be used to further narrow matching criteria.
  15. # E.g. to perform translation only when the packet is routed through WAN:
  16. #
  17. #   :global argSnptMangleExtra {"out-interface-list"="WAN"}
  18. #
  19. #
  20. # Arguments:
  21. #   argLoopbackInt (str): name of the loopback interface
  22. #   argWanPool (str): name of the WAN pool
  23. #   argUlaPool (str): name of the ULA pool
  24. #   argManagedID (str): regex-escaped unique ID of the managed objects
  25. #   [argSnptMangleExtra] (array): Optional extra properties for the SNPT mangle rule
  26. #   [argDnptMangleExtra] (array): Optional extra properties for the DNPT mangle rule
  27. #   [argSnptRawExtra] (array): Optional extra properties for the SNPT raw rule
  28. #   [argDnptRawExtra] (array): Optional extra properties for the DNPT raw rule
  29. #
  30. #
  31. # Affects:
  32. #   /ipv6/address
  33. #   /ipv6/firewall/mangle
  34. #   /ipv6/firewall/raw
  35. #
  36. #
  37. # Requirements:
  38. #   - mod/ipv6-functions

  39. :global GlobalFunctionsReady;
  40. :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }


  41. :local SetupRules do={
  42.     :global argLoopbackInt
  43.     :global argWanPool
  44.     :global argManagedID
  45.     :global argSnptMangleExtra
  46.     :global argDnptMangleExtra
  47.     :global argSnptRawExtra
  48.     :global argDnptRawExtra

  49.     :global WaitIP6Address
  50.     :global SetIfExistsElseAdd
  51.     :global SetIfExistsElseAddUnlessEqual

  52.     $SetIfExistsElseAddUnlessEqual /ipv6/address\
  53.         ({"comment~"$argManagedID\\\$""})\
  54.         ({\
  55.             interface="$argLoopbackInt";\
  56.             advertise=false;\
  57.             "from-pool"="$argWanPool"\
  58.         })\
  59.         ({\
  60.             interface=$argLoopbackInt;\
  61.             advertise="no";\
  62.             "from-pool"=$argWanPool;\
  63.             comment=""Managed: NPTv6 / $argManagedID""\
  64.         })
  65.     :local varGuaPrefix [$WaitIP6Address $argLoopbackInt $2 ("$argManagedID\$")]

  66.     $SetIfExistsElseAdd /ipv6/firewall/mangle\
  67.         ({"comment~"snpt-$argManagedID\\\$""})\
  68.         ($argSnptMangleExtra , {\
  69.             chain="postrouting";\
  70.             action="snpt";\
  71.             "src-address"=$1;\
  72.             "src-prefix"=$1;\
  73.             "dst-prefix"=$varGuaPrefix;\
  74.             comment=""Managed: NPTv6 / snpt-$argManagedID""\
  75.         })

  76.     $SetIfExistsElseAdd /ipv6/firewall/mangle\
  77.         ({"comment~"dnpt-$argManagedID\\\$""})\
  78.         ($argDnptMangleExtra , {\
  79.             chain="prerouting";\
  80.             action="dnpt";\
  81.             "dst-address"=$varGuaPrefix;\
  82.             "src-prefix"=$varGuaPrefix;\
  83.             "dst-prefix"=$1;\
  84.             comment=""Managed: NPTv6 / dnpt-$argManagedID""\
  85.         })

  86.     $SetIfExistsElseAdd /ipv6/firewall/raw\
  87.         ({"comment~"snpt-$argManagedID\\\$""})\
  88.         ($argSnptRawExtra , {\
  89.             chain="prerouting";\
  90.             action="notrack";\
  91.             "src-address"=$1;\
  92.             comment=""Managed: NPTv6 / snpt-$argManagedID""\
  93.         })

  94.     $SetIfExistsElseAdd /ipv6/firewall/raw\
  95.         ({"comment~"dnpt-$argManagedID\\\$""})\
  96.         ($argDnptRawExtra , {\
  97.             chain="prerouting";\
  98.             action="notrack";\
  99.             "dst-address"=$varGuaPrefix;\
  100.             comment=""Managed: NPTv6 / dnpt-$argManagedID""\
  101.         })
  102. }

  103. :local TearDown do={
  104.     :global argManagedID
  105.     /ipv6/address/remove [find comment~"$argManagedID\$"]
  106.     /ipv6/firewall/mangle/remove [find comment~"$argManagedID\$"]
  107.     /ipv6/firewall/raw/remove [find comment~"$argManagedID\$"]
  108. }

  109. :global LogPrint
  110. :global AssertNotEmpty

  111. :global argLoopbackInt
  112. :global argWanPool
  113. :global argUlaPool
  114. :global argManagedID

  115. $AssertNotEmpty "argLoopbackInt" $argLoopbackInt
  116. $AssertNotEmpty "argWanPool" $argWanPool
  117. $AssertNotEmpty "argUlaPool" $argUlaPool
  118. $AssertNotEmpty "argManagedID" $argManagedID


  119. /ipv6/pool
  120. :local varWanPrefix [get value-name=prefix $argWanPool]
  121. :local varUlaPrefix [get value-name=prefix $argUlaPool]

  122. :do {
  123.     $SetupRules $varUlaPrefix $varWanPrefix
  124.     $LogPrint info $0 ("Add NPT: $varUlaPrefix <-> $varWanPrefix")
  125. } on-error={
  126.     $LogPrint warning $0 ("Failed to update NPTv6, retrying from scratch")
  127.     :do {
  128.         $TearDown
  129.         $SetupRules $varUlaPrefix $varWanPrefix
  130.     } on-error={
  131.         $TearDown
  132.         $LogPrint error $0 ("Failed to set up NPTv6")
  133.         :error "fatal error in ipv6-npt.rsc"
  134.     }
  135. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|蜀ICP备17024538号-6

在本版发帖
关注公众号
QQ客服返回顶部