IPv6-only broadband: 464XLAT city by city, a NAT64 pool next to CGNAT

A broadband city can swap its home routers for ones with a CLAT (ip6Go /
ip6Stop, from the city panel's Home broadband). Over 30 to 180 days an even
share of the homes moves each day, paid as the routers go in ($4 a home),
waiting while cash is short; starting needs only the first day's share. New
homes in a swapping or finished city get the new router at the same price.
The panel says how many addresses it frees and what it does to the monthly
IPv4 bill.

ip4Use splits homes into public, behind CGNAT and IPv6-only: the NAT64 pool
(ceil(IPv6-only homes / NAT64 ratio), 64/128/256, default 128) comes right
after business blocks, before dual-stack homes. NAT64 gateways cost the same
per Gbps of IPv4 as CGNAT boxes; NAT64 refunds sit under CGNAT's at the same
ratio and never reach zero. The IPv4 share of home traffic is now a curve:
0.55 on day 0, two points less a year, floor 0.2. Out-of-space advice names
the sharing left to stretch. Everything is under rule ip4; old saves load
unchanged (no field means dual stack).
This commit is contained in:
jvanvik 2026-11-07 07:01:34 +01:00
parent 3bc1641a46
commit 69328ce257
25 changed files with 812 additions and 54 deletions

View file

@ -14,8 +14,13 @@
so the first months fit with room for a few customers (gameplay rule F1). */
// IP4_BLK: the most blocks a network holds; buying and leasing stop there, and a save keeps that many.
const IP4_START=21,IP4_MIN=16,IP4_MAX=24,IP4_HOLD=730,IP4_NAT=[8,16,32,64],IP4_NAT0=16,IP4_BLK=256;
// Public addresses per Gbps of busy-hour broadband (a home each), and the part of it CGNAT still carries: the rest is IPv6.
const IP4_BB=10,IP4_V4=0.55,IP4_CGN=2;
// Public addresses per Gbps of busy-hour broadband (a home each), and the part of it CGNAT still carries on day 0: the rest
// is IPv6. The world moves on, two points a year to a floor that never goes (ip4V4).
const IP4_BB=10,IP4_V4=0.55,IP4_V4_YR=0.02,IP4_V4_MIN=0.2,IP4_CGN=2;
// IPv6-only homes (464XLAT, 34-ipv6-only.js) have no address of their own: their IPv4 shares a NAT64 pool, at a wider ratio
// than CGNAT can take. Only what still needs IPv4 notices (consoles, IPv4-only VPNs): a share of the bill comes back, well
// under CGNAT's at the same ratio, and shrinking as the world goes IPv6.
const IP4_N64=[64,128,256],IP4_N640=128,IP4_ANNOY64={64:0.006,128:0.01,256:0.016};
// The market: dollars per address, flat for half a year, then +3% a year on a slow four-year swing. Leases are by the
// month (a broker keeps a cut of what yours earn), sales pay 92% of the price after the broker's fee.
const IP4_PX=38,IP4_FREE=180,IP4_YR=0.03,IP4_IN=0.5,IP4_OUT=0.4,IP4_SELL=0.92;
@ -30,17 +35,22 @@ const ip4Sz=len=>2**(32-len),ip4LenOk=len=>Number.isInteger(len)&&len>=IP4_MIN&&
const ip4Cidr=n=>'/'+(32-Math.ceil(Math.log2(Math.max(1,n)))),ip4N=n=>Math.round(n).toLocaleString();
function ip4On(){return !!S&&!!S.ip4&&rule('ip4')===true&&(S.scn?scnRule('ip4'):S.mode==='normal');}
function ip4Need(b){const k=b&&IP4_BIZ[b.type];if(!k||!(b.bw>0))return 0;return Math.min(4096,2**Math.ceil(Math.log2(Math.max(k[1],b.bw*k[0]))));}
/* Who uses what. T: usable addresses (owned and leased in, without what is leased out); B: business customers; H: homes;
cg: homes behind CGNAT; over: the share of homes that do not fit even at the ratio; free: what a new customer can have
with every home behind CGNAT. */
function ip4Use(s=S){const I=s.ip4,r=I.nat;let own=0,inn=0,out=0,B=0,H=0;
/* Who uses what. T: usable addresses (owned and leased in, without what is leased out); B: business customers; H: homes,
of them H6 IPv6-only (Hd dual stack); p6: the NAT64 pool; cg: dual-stack homes behind CGNAT; over: the share of homes
that do not fit even at the ratios; need: the least the homes can live with; free: what a new customer can have with
every dual-stack home behind CGNAT. The NAT64 pool comes right after business customers: its homes gave their own
addresses up already and cannot fall back on anything, while dual-stack homes still can (behind CGNAT). */
function ip4Use(s=S){const I=s.ip4,r=I.nat,r6=I.n64||IP4_N640;let own=0,inn=0,out=0,B=0,H=0,H6=0;
for(const b of I.b){const n=ip4Sz(b.len);if(b.k==='in')inn+=n;else if(b.out)out+=n;else own+=n;}
for(const b of s.biz)B+=ip4Need(b);
for(const c in s.pops){const p=s.pops[c];if(p.bb)H+=p.bb.d*IP4_BB;}
H=Math.ceil(H);const T=own+inn,A=Math.max(0,T-B),min=Math.ceil(H/r);let cg=0,over=0;
if(A<H){if(A>=min)cg=Math.min(H,Math.ceil((H-A)*r/(r-1)));else{cg=H;over=min?1-A/min:0;}}
return {T,own,inn,out,B,H,cg,over,free:T-B-min,r};}
for(const c in s.pops){const p=s.pops[c];if(p.bb){const h=p.bb.d*IP4_BB;H+=h;if(p.bb.v6)H6+=h*p.bb.v6;}}
H=Math.ceil(H);H6=Math.min(H,Math.ceil(H6));const Hd=H-H6,T=own+inn,A=Math.max(0,T-B),p6=Math.ceil(H6/r6),min=Math.ceil(Hd/r),need=p6+min,A2=A-p6;let cg=0,over=0;
if(A2<Hd){if(A2>=min)cg=Math.min(Hd,Math.ceil((Hd-A2)*r/(r-1)));else{cg=Hd;over=need?1-A/need:0;}}
return {T,own,inn,out,B,H,H6,Hd,p6,cg,over,need,free:T-B-need,r,r6};}
const ip4Fits=o=>!ip4On()||ip4Need(o)<=Math.max(0,ip4Use().free);
// Which sharing is left to stretch when space runs out, for the advice: CGNAT (0, no IPv6-only homes), the NAT64 pool alone
// (1, every home IPv6-only) or both (2).
const ip4Pools=u=>!u.H6?0:u.Hd?2:1;
// A customer renewing at bw: does the bigger block it needs fit what is free?
const ip4GrowOk=(b,bw)=>!ip4On()||ip4Need({type:b.type,bw})-ip4Need(b)<=Math.max(0,ip4Use().free);
const ip4Full=()=>S.ip4.b.length>=IP4_BLK;
@ -54,11 +64,17 @@ function ip4Px(d=S.day){if(!rule('market')||d<=IP4_FREE)return IP4_PX;const y=(d
const ip4Cost=len=>Math.round(ip4Sz(len)*ip4Px()*(1-0.015*(IP4_MAX-len))/100)*100;
const ip4Value=len=>Math.round(ip4Cost(len)*IP4_SELL/100)*100;
const ip4InMo=len=>Math.round(ip4Sz(len)*IP4_IN*ip4Px()/IP4_PX),ip4OutMo=len=>Math.round(ip4Sz(len)*IP4_OUT*ip4Px()/IP4_PX);
// The month's IPv4 bill: leases in and out, CGNAT boxes, refunds (eye: what broadband earns a month).
function ip4Bill(eye){const u=ip4Use();let lin=0,lout=0;for(const b of S.ip4.b){if(b.k==='in')lin+=b.mo||0;else if(b.out)lout+=b.mo||0;}
const cgS=u.H?u.cg/u.H:0;return {u,lin,lout,box:u.cg/IP4_BB*IP4_V4*IP4_CGN,refund:eye*(cgS*IP4_ANNOY[u.r]+u.over*IP4_SQZ)};}
// The IPv4 share of home traffic on day d: 55% on day 0 (so nothing changes at the start), two points less a year, never
// under a fifth (old consoles, old VPNs, old everything).
const ip4V4=(d=S.day)=>Math.max(IP4_V4_MIN,IP4_V4-IP4_V4_YR*Math.max(0,d)/365.2425);
/* The month's IPv4 bill: leases in and out, CGNAT boxes and NAT64 gateways (the same price per Gbps of IPv4 they carry),
refunds (eye: what broadband earns a month). NAT64 refunds shrink with the IPv4 share: in an IPv6-only home only what
still needs IPv4 meets the NAT at all. s: the network as ip4Use() takes it (a city panel asks about one that might be). */
function ip4Bill(eye,s=S){const u=ip4Use(s),v4=ip4V4();let lin=0,lout=0;for(const b of s.ip4.b){if(b.k==='in')lin+=b.mo||0;else if(b.out)lout+=b.mo||0;}
const cgS=u.H?u.cg/u.H:0,v6S=u.H?u.H6/u.H:0;
return {u,lin,lout,v4,box:u.cg/IP4_BB*v4*IP4_CGN,box64:u.H6/IP4_BB*v4*IP4_CGN,refund:eye*(cgS*IP4_ANNOY[u.r]+v6S*IP4_ANNOY64[u.r6]*v4/IP4_V4+u.over*IP4_SQZ)};}
// computeMoney: only what is there, so a network with nothing to pay keeps its money rows as they were.
function ip4Money(C,V){if(!ip4On())return;const b=ip4Bill(V.eyeballs||0),c=Math.round(b.lin+b.box+b.refund);if(c>0)C.ip4=c;if(b.lout>0)V.ip4=b.lout;}
function ip4Money(C,V){if(!ip4On())return;const b=ip4Bill(V.eyeballs||0),c=Math.round(b.lin+b.box+b.box64+b.refund);if(c>0)C.ip4=c;if(b.lout>0)V.ip4=b.lout;}
/* A new network gets its /21 from the RIR. An older save gets what it already uses registered to it and a quarter more to
grow into (about a month for a big, fast-growing network), rounded up to /24s in real CIDR blocks (a /21 at the
least), so nothing changes on load; those blocks move like any other two years on.
@ -69,19 +85,22 @@ function ip4Grant(s){const d=s.day||0,b=[],add=len=>b.push({id:'rir'+b.length,le
need=Math.max(ip4Sz(IP4_START),Math.ceil(need*1.25/256)*256);
while(need>=ip4Sz(IP4_MIN)&&b.length<200){add(IP4_MIN);need-=ip4Sz(IP4_MIN);}
for(let len=IP4_MIN+1;len<=IP4_MAX;len++)if(need>=ip4Sz(len)){add(len);need-=ip4Sz(len);}
return {b,nat:IP4_NAT0,w:0};}
return {b,nat:IP4_NAT0,n64:IP4_N640,w:0};}
const ip4New=()=>ip4Grant({});
function ip4Fix(s){const I=s.ip4&&typeof s.ip4==='object'&&!Array.isArray(s.ip4)?s.ip4:null;if(!I){s.ip4=ip4Grant(s);return;}
const day=Math.max(0,num(s.day,0)),mo=v=>Math.round(Math.min(1e8,Math.max(0,num(v,0))));
const b=(Array.isArray(I.b)?I.b:[]).filter(x=>x&&typeof x==='object'&&ip4LenOk(x.len)&&['rir','buy','in'].includes(x.k)).slice(0,IP4_BLK).map(x=>{
const o={id:typeof x.id==='string'&&x.id&&x.id.length<=40?x.id:uid(),len:x.len,k:x.k,d:int(x.d,0,day,0)};
if(x.k==='in')o.mo=mo(x.mo);else if(x.out===true){o.out=true;o.mo=mo(x.mo);}return o;});
s.ip4={b,nat:IP4_NAT.includes(I.nat)?I.nat:IP4_NAT0,w:int(I.w,0,3,0)};}
s.ip4={b,nat:IP4_NAT.includes(I.nat)?I.nat:IP4_NAT0,n64:IP4_N64.includes(I.n64)?I.n64:IP4_N640,w:int(I.w,0,3,0)};}
/* Once a day: say so when homes go behind CGNAT (a percent of them at least), and when even CGNAT cannot fit them all
(w: what was said). Each rearms only with room to spare again, so homes hovering at the limit hear it once. */
function ip4Day(d){if(!ip4On())return;const I=S.ip4,u=ip4Use(),A=u.T-u.B,go=[{l:tr('ip4.section'),act:'ip4Open'}];
if(u.cg>=Math.max(1,u.H*0.01)){if(!(I.w&1)){I.w|=1;toast(tr('ip4.toast_cgnat',{p:pct(u.cg/u.H)}),'',go);}}else if(A>=u.H*1.05)I.w&=~1;
if(u.over>0){if(!(I.w&2)){I.w|=2;toast(tr('ip4.toast_out',{r:u.r}),'bad',go);sfx('err');}}else if(A>=Math.ceil(u.H/u.r)*1.1)I.w&=~2;}
function ip4Day(d){if(!ip4On())return;ip6Day(d);const I=S.ip4,u=ip4Use(),A=u.T-u.B,go=[{l:tr('ip4.section'),act:'ip4Open'}];
if(u.cg>=Math.max(1,u.H*0.01)){if(!(I.w&1)){I.w|=1;toast(tr('ip4.toast_cgnat',{p:pct(u.cg/u.H)}),'',go);}}else if(A-u.p6>=u.Hd*1.05)I.w&=~1;
if(u.over>0){if(!(I.w&2)){I.w|=2;const k=ip4Pools(u);toast(k===1?tr('ip4.toast_out_v6',{r:u.r6}):k?tr('ip4.toast_out_both',{r:u.r,r6:u.r6}):tr('ip4.toast_out',{r:u.r}),'bad',go);sfx('err');}}
else if(A>=u.need*1.1)I.w&=~2;}
// Your customers need the addresses: what to stretch or get instead (ip4Busy, the bank's block rows).
function ip4InUse(u=ip4Use()){const k=ip4Pools(u);return k===1?tr('ip4.in_use_v6'):k?tr('ip4.in_use_both'):tr('ip4.in_use');}
// Renewals that wanted a bigger block than is free (clGrow, [customer, bandwidth]): they stay as they are, and hear why.
function ip4News(l=[]){for(const [b,bw] of l.slice(0,3))toast(tr('ip4.no_grow',{name:esc(b.name),city:esc(clWhere(b)),bw:fmtG(bw)}),'',[{l:tr('ip4.section'),act:'ip4Open'}]);}
// The line under an offer that cannot be signed for want of addresses.