1*6779Ssam /* if_imphost.c 4.12 82/05/11 */ 25712Ssam 35712Ssam #include "imp.h" 45712Ssam #if NIMP > 0 55712Ssam /* 65712Ssam * Host table manipulation routines. 75712Ssam * Only needed when shipping stuff through an IMP. 85712Ssam */ 95712Ssam 105712Ssam #include "../h/param.h" 115712Ssam #include "../h/mbuf.h" 125712Ssam #include "../net/in.h" 135712Ssam #include "../net/in_systm.h" 145712Ssam #include "../net/if_imp.h" 155860Sroot #include "../net/if_imphost.h" 165712Ssam 175712Ssam /* 185712Ssam * Head of host table hash chains. 195712Ssam */ 205860Sroot struct mbuf *hosts; 215712Ssam 225712Ssam /* 235712Ssam * Given an internet address 245712Ssam * return a host structure (if it exists). 255712Ssam */ 265712Ssam struct host * 275771Swnj hostlookup(addr) 285712Ssam struct in_addr addr; 295712Ssam { 305712Ssam register struct host *hp; 315712Ssam register struct mbuf *m; 325712Ssam register int hash = HOSTHASH(addr); 336589Ssam int s = splnet(); 345712Ssam 355771Swnj COUNT(HOSTLOOKUP); 365860Sroot for (m = hosts; m; m = m->m_next) { 375712Ssam hp = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 386589Ssam if (hp->h_addr.s_addr == addr.s_addr) { 396589Ssam hp->h_flags |= HF_INUSE; 406589Ssam goto found; 416589Ssam } 425712Ssam } 436589Ssam hp = 0; 446589Ssam found: 456589Ssam splx(s); 466589Ssam return (hp); 475712Ssam } 485712Ssam 495712Ssam /* 505712Ssam * Enter a reference to this host's internet 515712Ssam * address. If no host structure exists, create 525712Ssam * one and hook it into the host database. 535712Ssam */ 545712Ssam struct host * 555771Swnj hostenter(addr) 565712Ssam struct in_addr addr; 575712Ssam { 585860Sroot register struct mbuf *m, **mprev; 595860Sroot register struct host *hp, *hp0 = 0; 605712Ssam register int hash = HOSTHASH(addr); 616589Ssam int s = splnet(); 625712Ssam 635771Swnj COUNT(HOSTENTER); 645860Sroot mprev = &hosts; 655860Sroot while (m = *mprev) { 666209Swnj mprev = &m->m_next; 675712Ssam hp = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 686589Ssam if ((hp->h_flags & HF_INUSE) == 0) { 696589Ssam if (hp->h_addr.s_addr == addr.s_addr) 706589Ssam goto foundhost; 715860Sroot if (hp0 == 0) 725860Sroot hp0 = hp; 735860Sroot continue; 745860Sroot } 755712Ssam if (hp->h_addr.s_addr == addr.s_addr) 765712Ssam goto foundhost; 775712Ssam } 785712Ssam 795712Ssam /* 805712Ssam * No current host structure, make one. 815712Ssam * If our search ran off the end of the 825712Ssam * chain of mbuf's, allocate another. 835712Ssam */ 845860Sroot if (hp0 == 0) { 855712Ssam m = m_getclr(M_DONTWAIT); 866589Ssam if (m == 0) { 876589Ssam splx(s); 885712Ssam return (0); 896589Ssam } 905860Sroot *mprev = m; 915860Sroot m->m_off = MMINOFF; 925860Sroot hp0 = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 935712Ssam } 945860Sroot mtod(dtom(hp0), struct hmbuf *)->hm_count++; 955860Sroot hp = hp0; 965712Ssam hp->h_addr = addr; 976589Ssam hp->h_timer = 0; 986725Ssam hp->h_flags = 0; 995712Ssam 1005712Ssam foundhost: 1016589Ssam hp->h_flags |= HF_INUSE; 1026589Ssam splx(s); 1035712Ssam return (hp); 1045712Ssam } 1055712Ssam 1065712Ssam /* 1076589Ssam * Mark a host structure free and set it's 1086589Ssam * timer going. 1095712Ssam */ 1105860Sroot hostfree(hp) 1115860Sroot register struct host *hp; 1125712Ssam { 1136589Ssam int s = splnet(); 1146589Ssam 1155771Swnj COUNT(HOSTFREE); 1166589Ssam hp->h_flags &= ~HF_INUSE; 1176589Ssam hp->h_timer = HOSTTIMER; 1186589Ssam hp->h_rfnm = 0; 1196589Ssam splx(s); 1205712Ssam } 1215712Ssam 1225712Ssam /* 1235712Ssam * Reset a given network's host entries. 1245712Ssam */ 1255771Swnj hostreset(net) 1265712Ssam int net; 1275712Ssam { 1285712Ssam register struct mbuf *m; 1295712Ssam register struct host *hp, *lp; 1305860Sroot struct hmbuf *hm; 1316589Ssam int s = splnet(); 1325712Ssam 1335771Swnj COUNT(HOSTRESET); 1345860Sroot for (m = hosts; m; m = m->m_next) { 1355860Sroot hm = mtod(m, struct hmbuf *); 1365860Sroot hp = hm->hm_hosts; 1375712Ssam lp = hp + HPMBUF; 1386589Ssam while (hm->hm_count > 0 && hp < lp) { 1396089Sroot if (hp->h_addr.s_net == net) { 1406589Ssam hp->h_flags &= ~HF_INUSE; 1416145Ssam hostrelease(hp); 1426089Sroot } 1435712Ssam hp++; 1445712Ssam } 1455712Ssam } 1466589Ssam splx(s); 1475712Ssam } 1485712Ssam 1495712Ssam /* 1505712Ssam * Remove a host structure and release 1515712Ssam * any resources it's accumulated. 1526589Ssam * This routine is always called at splnet. 1535712Ssam */ 1545860Sroot hostrelease(hp) 1555712Ssam register struct host *hp; 1565712Ssam { 1575860Sroot register struct mbuf *m, **mprev, *mh = dtom(hp); 1585712Ssam 1595771Swnj COUNT(HOSTRELEASE); 1605712Ssam /* 1615712Ssam * Discard any packets left on the waiting q 1625712Ssam */ 1635771Swnj if (m = hp->h_q) { 1645868Sroot register struct mbuf *n; 1655868Sroot 1665868Sroot do { 1675868Sroot n = m->m_act; 1685868Sroot m_freem(m); 1695868Sroot m = n; 1705868Sroot } while (m != hp->h_q); 1715771Swnj hp->h_q = 0; 1725712Ssam } 1736725Ssam hp->h_flags = 0; 1745860Sroot if (--mtod(mh, struct hmbuf *)->hm_count) 1755712Ssam return; 1765860Sroot mprev = &hosts; 1775860Sroot while ((m = *mprev) != mh) 1785860Sroot mprev = &m->m_next; 1796589Ssam *mprev = m_free(mh); 1805712Ssam } 1815924Sroot 1825924Sroot /* 1835924Sroot * Remove a packet from the holding q. 1845924Sroot * The RFNM counter is also bumped. 1855924Sroot */ 1865924Sroot struct mbuf * 1875924Sroot hostdeque(hp) 1885924Sroot register struct host *hp; 1895924Sroot { 1905924Sroot register struct mbuf *m; 1915924Sroot 1925924Sroot hp->h_rfnm--; 1935924Sroot HOST_DEQUE(hp, m); 1945924Sroot if (m) 1955924Sroot return (m); 1965924Sroot if (hp->h_rfnm == 0) 1975924Sroot hostfree(hp); 1985924Sroot return (0); 1995924Sroot } 2006589Ssam 2016589Ssam /* 2026589Ssam * Host data base timer routine. 2036589Ssam * Decrement timers on structures which are 2046589Ssam * waiting to be deallocated. On expiration 2056589Ssam * release resources, possibly deallocating 2066589Ssam * mbuf associated with structure. 2076589Ssam */ 2086589Ssam hostslowtimo() 2096589Ssam { 2106589Ssam register struct mbuf *m; 2116589Ssam register struct host *hp, *lp; 2126589Ssam struct hmbuf *hm; 2136589Ssam int s = splnet(); 2146589Ssam 2156589Ssam COUNT(HOSTSLOWTIMO); 2166589Ssam for (m = hosts; m; m = m->m_next) { 2176589Ssam hm = mtod(m, struct hmbuf *); 2186589Ssam hp = hm->hm_hosts; 2196589Ssam lp = hp + HPMBUF; 220*6779Ssam for (; hm->hm_count > 0 && hp < lp; hp++) { 2216589Ssam if (hp->h_flags & HF_INUSE) 2226589Ssam continue; 2236589Ssam if (hp->h_timer && --hp->h_timer == 0) 2246589Ssam hostrelease(hp); 2256589Ssam } 2266589Ssam } 2276589Ssam splx(s); 2286589Ssam } 229