#!/bin/sh # ===================================================================== # Projet : DARKWALL # Fichier : lib/ip6Tools # Auteur : Nicolas "DarkHack" Chapuis (c) 2026 # ================== DARKHACK ================== # [ darkweb.fr - system online ] # Powered by Perplexity, your AI assistant # https://www.perplexity.ai # ============================================== # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===================================================================== if ! type "shInclude" > /dev/null 2>&1; then racineSh="${racineSh:-/etc/darksh}"; . "$racineSh/lib/shTools"; shDebugEnable="1";fi rootDarkWall="${rootDarkWall:-/etc/darkwall}" [ ! -d "$rootDarkWall" ] && shDebug 0 "exit cause rootDarWall is not dir:$rootDarkWall" && exit 0 requiresCmd="" requiresCmd="printf timeout sleep kill cat echo sh rm mkdir grep md5sum" requiresCmd="${requiresCmd} ip ip6tables wc sed awk" shWhichCmd $requiresCmd >/dev/null shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) requiresCmd="" requiresCmd="brctl ifup ifdown dhclient" shWhichCmd $requiresCmd >/dev/null shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) $(shInclude $rootDarkWall/lib/ip6NetworkMaskTools) $(shInclude $rootDarkWall/lib/ipTools) # firewall6ClientConf firewall6ServerConf # ip6toolsGetMd5Rule "filter" $md5 ip6toolsGetMd5Rule() { local log="ip6toolsGetMd5Rule:" local table=""$1"" local md5=""$2"" if [ -z "$md5" ];then return 1;fi local ruleListMd5="md5" local theChaineMd5="" ip6toolsRuleCreate ""$table"" "$ruleListMd5" local ruleFound="$( $cmdIp6tables -w -t "$table" -nL "$ruleListMd5" | $cmdGrep "$md5" | $cmdCut -f2 -d_ )" if [ -z "$ruleFound" ];then local ruleCpt="$( $cmdIp6tables -w -t "$table" -nL "$ruleListMd5" | $cmdWc -l )" local ruleCpt=$(( ruleCpt - 2 )); local newRuleMd5=""$ruleListMd5"-"$ruleCpt"" ip6toolsRuleCreate ""$table"" "$newRuleMd5" # echo "$cmdIp6tables -w -t $table -A "$ruleListMd5" -j $newRuleMd5 -m comment --comment ${md5}_${ruleCpt}_" >> "/tmp/firewl.log" if ! iptoolsExecute "$cmdIp6tables -w -t $table -A "$ruleListMd5" -j $newRuleMd5 -m comment --comment ${md5}_${ruleCpt}_" ; then shDebugHidden 0 ""$log" add rule ($table) $rule" # else # shDebugHidden 1 ""$log" add rule ($table) $rule" fi local theChaineMd5="${newRuleMd5}" else local theChaineMd5="${ruleListMd5}-${ruleFound}" fi echo "${theChaineMd5}" } #ip6toolsGetGw "${eth}" "${net6}" ip6toolsGetGw() { ip6toolsGetGws "${1}" "${2}" | $cmdTr -s ' ' '\n' | $cmdHead -n 1 } #i6ptoolsGetGws "${eth}" "${net6s}" ip6toolsGetGws() { # if [ -n "${1}" ];then local eth="$( iptoolsGetHostEth "${1}" | $cmdCut -f1 -d\: )"; else local eth="";fi if [ -n "${1}" ];then local eth="$( $cmdEcho "${1}" | $cmdCut -f1 -d\: )"; else local eth="";fi if [ -n "${2}" ];then local net6s="${2}"; else local net6s="";fi if [ -z "${net6s}" ] && [ -n "${eth}" ];then local net6s="$( ip6toolsGetNets "${eth}" )" fi local gw6s="" if [ -z "$eth" ];then #ip -6 route | sed -e 's/.*nexthop/_/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' #ip -6 route | sed -e 's/.*nexthop/_/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' | awk '/default/ { print $3 }' | sort -u | uniq | tr -s '\n' ' ' #ip -6 route | sed -e 's/.*nexthop/#default/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' #ip -6 route | sed -e 's/.*nexthop/#default/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' | awk '/default via/ { print $3 }' | sort -u | uniq | tr -s '\n' ' ' local listGw6s="$( $cmdIp -6 route show | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdAwk '/default via/ { print $3 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" else local listGw6s="$( $cmdIp -6 route show dev "${eth}" | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdAwk '/default via/ { print $3 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" # if [ -z "$( $cmdEcho ${eth} | grep : )" ];then # local listGw6s="$( $cmdIp -6 route show dev "${eth}" | $cmdAwk '/default/ { print $3 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" # else # local listGws6="" # fi fi if [ -n "${listGw6s}" ];then for gw6 in ${listGw6s};do if [ -z "${gw6}" ];then continue;fi local isGood=0 if [ -z "${net6s}" ];then local isGood=1 else for net6 in ${net6s};do if [ -z "${net6}" ];then continue;fi if ip6IsInNetwork "${gw6}" "${net6}";then local isGood=1 fi done fi if [ 1 -eq $isGood ];then if [ -z "${gw6s}" ];then local gw6s="${gw6}"; else local gw6s="${gw6s} ${gw6}"; fi fi done fi $cmdEcho "${gw6s}" } #ip6toolsGetAllGw ip6toolsGetAllGw() { ip6toolsGetAllGws | $cmdTr -s ' ' '\n' | $cmdHead -n 1 } #i6ptoolsGetAllGws ip6toolsGetAllGws() { local gw6s="" #ip -6 route | sed -e 's/.*nexthop/_/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' #ip -6 route | sed -e 's/.*nexthop/_/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' | awk '/default/ { print $3 }' | sort -u | uniq | tr -s '\n' ' ' #ip -6 route | sed -e 's/.*nexthop/#default/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' #ip -6 route | sed -e 's/.*nexthop/#default/g' | tr -s '\n' '#' | sed -e 's/#_/_/g' | tr '#' '\n' | awk '/default via/ { print $3 }' | sort -u | uniq | tr -s '\n' ' ' local listGw6s="$( $cmdIp -6 route show | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdAwk '/default via/ { print $3 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" shDebug 1 "$cmdIp -6 route show | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdAwk '/default via/ { print $3 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' '" >&2 if [ -n "${listGw6s}" ];then for gw6 in ${listGw6s};do if [ -z "${gw6}" ];then continue;fi if [ -z "${gw6s}" ];then local gw6s="${gw6}"; else local gw6s="${gw6s} ${gw6}"; fi done fi $cmdEcho "${gw6s}" } #ip6ToolsGetEthForGw ${gwAddr} ip6ToolsGetEthForGw() { $cmdIp -6 route show | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdGrep " via ${1} " | $cmdAwk '/default via/ { print $5 }' #echo "$cmdIp -6 route show | $cmdSed -e 's/.*nexthop/#default/g' | $cmdTr -s '\n' '#' | $cmdSed -e 's/#_/_/g' | $cmdTr '#' '\n' | $cmdGrep " via ${1} " | $cmdAwk '/default via/ { print $5 }'" } #ip6toolsGetLLForGw "${gwAddr}" ip6toolsGetLLForGw() { ip6toolsGetLl "$( ip6ToolsGetEthForGw "${1}" )" } #ip6toolsGetIp "${eth}" "${net6}" ip6toolsGetIp() { ip6toolsGetIps ""${1}"" ""${2}"" | $cmdTr -s ' ' '\n' | $cmdHead -n 1 } #ip6toolsGetIps "${eth}" "${net6}" ip6toolsGetIps() { local eth="${1}" # local eth="$(iptoolsGetHostEth "${1}" )" local net6="${2}" if [ -z "${eth}" ];then local listIp6="$( $cmdIp -6 addr show | $cmdGrep "inet6" | $cmdGrep "scope host\|scope site\|scope link\|global" | $cmdAwk '{print $2}' | $cmdCut -d'/' -f1 | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" else if [ -z "$( $cmdEcho ${eth} | grep : )" ];then local listIp6="$( $cmdIp -6 addr show dev ${eth} | $cmdGrep "inet6" | $cmdGrep "scope host\|scope site\|scope link\|global" | $cmdAwk '{print $2}' | $cmdCut -d'/' -f1 | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" else local listIp6="" fi fi if [ -n "${listIp6}" ];then for ip6 in ${listIp6};do if [ -z "${ip6}" ];then continue ;fi local isGood=0 if [ -n "${net6}" ];then if ip6IsInNetwork "${ip6}" "${net6}";then local isGood=1 fi else local isGood=1 fi if [ 1 -eq $isGood ];then if [ -z "$ip6s" ];then local ip6s="${ip6}"; else local ip6s="${ip6s} ${ip6}"; fi fi done fi $cmdEcho "${ip6s}" } #ip6ToolsGetEthForIp ${ipAddr} ip6ToolsGetEthForIp() { ip6ToolsGetEthForIp $* | $cmdHead -n 1 } ip6ToolsGetEthsForIp() { local ipAddr="$1" for iface in $($cmdIp link | $cmdGrep "UP,LOWER_UP" | $cmdCut -f2 -d: | $cmdTr -d '[:blank:]' );do for anIp in $( ip6toolsGetIps $iface );do [ "$ipAddr" != "$anIp" ] && continue $cmdEcho $iface done done } #ip6toolsGetNet "${eth}" "${ip6}" ip6toolsGetNet() { ip6toolsGetNets "${1}" "${2}" | $cmdTr -s ' ' '\n' | $cmdHead -n 1 } #ip6toolsGetNets "${eth}" "${ip6}" ip6toolsGetNets() { # local eth="$( iptoolsGetHostEth "${1}" )" local eth="$( $cmdEcho ${1} | $cmdCut -f1 -d\: )" # local eth="${1}" if [ "${eth}" = "lo" ];then local eth="";fi local net6s="" local ip6="${2}" if [ -z "$eth" ];then local listNet6="$( $cmdIp -6 route show | $cmdGrep / | $cmdGrep -v default | $cmdSed -e 's/local/::1\/128/g' | $cmdAwk '{ print $1 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" else local listNet6="$( $cmdIp -6 route show dev ${eth} | $cmdGrep / | $cmdGrep -v default | $cmdSed -e 's/local/::1\/128/g' | $cmdAwk '{ print $1 }' | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" fi if [ -n "${listNet6}" ];then for net6 in ${listNet6};do if [ -z "${net6}" ];then continue ;fi #echo " ${net6}:${ip6} $( ip6IsInNetwork "${ip6}" "${net6}" )" >&2 if [ -z "${ip6}" ] || ip6IsInNetwork "${ip6}" "${net6}" ;then if [ -z "${net6s}" ];then local net6s="${net6}" else local net6s="${net6s} ${net6}" # if [ -z "${net6s}" ];then local nets="$( ip6CidrToNetwork "${net6}" )" # else local net6s="${net6s} $( ip6CidrToNetwork "${net6}" )" fi fi done fi if [ -z "${net6s}" ] || ip6IsInNetwork "${ip6}" "fe80::/10" ;then local net6s="fe80::/10";fi $cmdEcho ${net6s} } # ip6toolsGetLl "${eth}" ip6toolsGetLl() { ip6toolsGetLls "${1}" | $cmdTr -s ' ' '\n' | $cmdHead -n 1 } # ip6toolsGetLls "${eth}" ip6toolsGetLls() { local eth="$( $cmdEcho ${1} | $cmdCut -f1 -d\: )" # liste les LL sur l'interface ou sur tout le système if [ -z "${eth}" ]; then # toutes les link-local local listLl6="$($cmdIp -6 addr show | $cmdGrep 'inet6' | $cmdGrep 'scope link' | $cmdAwk '{print $2}' | $cmdCut -d'/' -f1 | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" else local listLl6="$( $cmdIp -6 addr show dev ${eth} | $cmdGrep 'inet6' | $cmdGrep 'scope link' | $cmdAwk '{print $2}' | $cmdCut -d'/' -f1 | $cmdSort -u | $cmdUniq | $cmdTr -s '\n' ' ' )" fi # agrégation (même pattern que GetIps) if [ -n "${listLl6}" ]; then for ll6 in ${listLl6}; do [ -z "${ll6}" ] && continue if [ -z "${ll6s}" ]; then local ll6s="${ll6}" else local ll6s="${ll6s} ${ll6}" fi done fi $cmdEcho "${ll6s}" } #ip6toolsGetNodeSollicite "${ip6}" #ip6toolsGetNodeSollicite() { # local ipv6="$1" # local snmc # # snmc=$( # $cmdPrintf '%s\n' "$ipv6" | $cmdSed 's/^::/0::/' | $cmdSed 's/::/:Z:/' | $cmdAwk -F: ' # { # n=0; zseen=0; p=0 # for(i=1;i<=NF;i++){ # if($i=="Z"){ zseen=1; p=n; continue } # if($i=="") continue # out[++n]=$i # } # if(zseen){ # m=8-n # # groupes après le :: → vers la fin # for(i=n;i>p;i--) tmp[i+m]=out[i] # # groupes avant le :: → restent en place # for(i=1;i<=p;i++) tmp[i]=out[i] # # zéros insérés au bon endroit # for(i=p+1;i<=p+m;i++) tmp[i]="0" # n=8 # for(i=1;i<=n;i++) out[i]=tmp[i] # } # # hex = "" # for(i=1;i<=8;i++){ # h = out[i] # len = length(h) # if(len < 4){ # pad = 4-len # for(j=1;j<=pad;j++) h = "0" h # } # hex = hex h # } # # tail = substr(hex, length(hex)-5, 6) # hi = substr(tail, 1, 2) # lo = substr(tail, 3, 4) # # printf "ff02::1:ff%s:%s\n", hi, lo # }' # ) # $cmdEcho "$snmc" #} ip6toolsGetNodeSollicite() { local log="ip6toolsGetNodeSollicite:" local ip6="$1" local snmc="" if ! ip6IsValid "$ip6" ;then shDebugHidden 1 ""$log" ip6 invalide:$ip6" return 1 fi # expansion fiable, gere :: peu importe sa position (delegue a ip6InHex) local ip6Full="$(ip6InHex "$ip6")" # les 2 derniers groupes portent les 24 bits qui nous interessent local g7="$( $cmdEcho "$ip6Full" | $cmdCut -d':' -f7 )" local g8="$( $cmdEcho "$ip6Full" | $cmdCut -d':' -f8 )" # zero-pad chaque groupe sur 4 hexa (meme logique que le reste de la lib) while [ "$( $cmdExpr length "$g7" )" -lt 4 ];do g7="0${g7}";done while [ "$( $cmdExpr length "$g8" )" -lt 4 ];do g8="0${g8}";done local tail="${g7}${g8}" tail="$( $cmdEcho "$tail" | $cmdExpr substr "$tail" 3 6 2>/dev/null || $cmdEcho "$tail" | $cmdCut -c3-8 )" local hi="$( $cmdEcho "$tail" | $cmdCut -c1-2 )" local lo="$( $cmdEcho "$tail" | $cmdCut -c3-6 )" snmc="$( $cmdPrintf 'ff02::1:ff%s:%s\n' "$hi" "$lo" | $cmdTr '[:upper:]' '[:lower:]' )" $cmdEcho "$snmc" } ip6toolsRuleCreate() { local log="ip6toolsRuleCreate:" local table="" local rule="" for param in $*;do if [ -z "$table" ];then local table="$param" else local rule="$rule $param" fi done if [ -z "$table" ] || [ -z "$rule" ];then shDebugHidden 1 ""$log" usage : cmd \$table \$rule but (table:$table,rule:$rule)" >&2 else if ip6toolsRuleExiste "$table" "$rule" ; then shDebugHidden 1 ""$log" rule $rule in $table exist yet" >&2 else if ! iptoolsExecute "$cmdIp6tables -w -t $table -N $rule" ; then shDebugHidden 0 ""$log" create rule $rule in $table" >&2 else # shDebugHidden 2 ""$log" create rule $rule in $table" >&2 return 0 fi fi fi return 1 } ip6toolsRuleAdd() { local log="ip6toolsRuleAdd:" local table="" local rule="" for param in $*;do if [ -z "$table" ];then local table="$param" else local rule="$rule $param" fi done if [ -z "$table" ] || [ -z "$rule" ] ; then shDebugHidden 1 ""$log" usage : cmd \$table \$rule but (table:$table,rule:$rule)" >&2 else local comment=" -m comment --comment $(echo "$rule" | tr -s " " | tr " " "_")" if ! iptoolsExecuteNoLog "$cmdIp6tables -w -t $table -C $rule $comment" ; then # shDebugHidden 2 ""$log" rule exist yet ($table) :$rule" >&2 # else if ! iptoolsExecute "$cmdIp6tables -w -t $table -A $rule $comment" ; then shDebugHidden 0 ""$log" add rule ($table) $rule" else # shDebugHidden 1 ""$log" add rule ($table) $rule" return 0 fi fi fi return 1 } ip6toolsRuleInsert() { local log="ip6toolsRuleInsert:" local table="" local destination="" local rule="" for param in $*;do if [ -z "$table" ];then local table="$param" elif [ -z "$destination" ];then local destination="$param" else local rule="$rule $param" fi done if [ -z "$table" ] || [ -z "$rule" ] ; then shDebugHidden 0 ""$log" usage : cmd \$table \$rule but (table:$table,rule:$rule)" >&2 else local comment=""$(echo "$rule" | tr -s " " | tr " " "_")"_" local commentRule="-m comment --comment ${comment}" local commentSearchRule="-m comment --comment ${comment}" local finalRule="${destination} ${commentRule} ${rule}" local finalSearchRule="${destination} ${commentSearchRule} ${rule}" shDebugHidden 1 ""$log" check rule before insert:$cmdIp6tables -w -t $table -C $finalSearchRule" >&2 if iptoolsExecuteNoLog "$cmdIp6tables -w -t $table -C $finalSearchRule" ; then shDebugHidden 1 ""$log" rule exist yet ($table/$destination) :$rule" >&2 else if ! iptoolsExecute "$cmdIp6tables -w -t $table -I $finalRule" ; then shDebugHidden 0 ""$log" add rule ($table/$destination) $rule" >&2 else shDebugHidden 2 ""$log" add rule ($table/$destination) $rule" >&2 return 0 fi fi fi return 1 } ip6toolsRuleDelete() { local log="ip6toolsRuleDelete:" local table="$1" local direction="$2" local rule="$3" # local table="" # local rule="" # for param in $*;do # if [ -z "$table" ];then # local table="$param" # else # if [ -z "$direction" ];then # local direction="$param" # else # local rule="$rule $param" # fi # fi # done if [ -z "$table" ] || [ -z "$direction" ] || [ -z "$rule" ];then shDebugHidden 2 ""$log" usage : cmd \$table \$direction \$rule but (table:$table,direction:$direction,rule:$rule)" >&2 else shDebugHidden 1 ""$log" table:$table,direction:$direction,rule:$rule" >&2 for readCondition in $($cmdIp6tables -w -t "$table" -S "$direction" 2>&1 | $cmdGrep "$rule" | $cmdGrep "\-A" | $cmdTr ' ' '¤');do # if [ "$rule" = "$( $cmdEcho $readCondition | $cmdTr '¤' ' ' | $cmdRev | $cmdCut -d' ' -f1 | $cmdRev)" ] ;then conditionExtracted="$( $cmdEcho $readCondition | $cmdTr '¤' ' ' | $cmdRev | $cmdCut -d' ' -f1- | $cmdRev | $cmdCut -d' ' -f2- | $cmdSed 's/\"//g')" shDebugHidden 1 ""$log" rule found to delete : $conditionExtracted" >&2 if ! iptoolsExecute "$cmdIp6tables -w -t $table -D $conditionExtracted" ; then shDebugHidden 0 ""$log" clean rule -D : $rule (-t $table -D $conditionExtracted)" >&2 fi # fi done if [ -n "$( $cmdIp6tables -w -t $table -nL $direction 2>&1 | $cmdGrep $rule )" ] && [ -n "$conditionExtracted" ]; then if ! iptoolsExecute "$cmdIp6tables -w -t $table -F $conditionExtracted" ; then shDebugHidden 0 ""$log" clean rule -F : $rule (-t $table -F $conditionExtracted)" >&2 fi if ! iptoolsExecute "$cmdIp6tables -w -t $table -X $conditionExtracted" ; then shDebugHidden 0 ""$log" clean rule -X : $rule (-t $table -X $conditionExtracted)" >&2 fi fi if [ -n "$($cmdIp6tables -w -t $table -nL $direction 2>&1 | grep $rule)" ] ; then if ! iptoolsExecute "$cmdIp6tables -w -t $table -F $rule" ; then shDebugHidden 0 ""$log" clean rule -F : $rule (-t $table -F $rule)" >&2 fi if ! iptoolsExecute "$cmdIp6tables -w -t $table -X $rule" ; then shDebugHidden 0 ""$log" clean rule -X : $rule (-t $table -X $rule)" >&2 else return 0 fi fi fi return 1 } ip6toolsRuleExiste() { local log="ip6toolsRuleExiste:" local table="" local rule="" for param in $*;do if [ -z "$table" ];then local table="$param" else local rule="$rule $param" fi done if [ -z "$table" ] || [ -z "$rule" ];then shDebugHidden 0 ""$log" usage : cmd \$table \$rule but (table:$table,rule:$rule)" >&2 else if [ -n "$( $cmdIp6tables -w -t $table -S $rule 2>/dev/null )" ] ;then return 0 fi fi return 1 }