alias_db.c (62159) | alias_db.c (63899) |
---|---|
1/* -*- mode: c; tab-width: 8; c-basic-indent: 4; -*- 2 Alias_db.c encapsulates all data structures used for storing 3 packet aliasing data. Other parts of the aliasing software 4 access data through functions provided in this file. 5 6 Data storage is based on the notion of a "link", which is 7 established for ICMP echo/reply packets, UDP datagrams and 8 TCP stream connections. A link stores the original source --- 89 unchanged lines hidden (view full) --- 98 multiple external addresses rather than just a single 99 host address. 100 101 PacketAliasRedirectPort() and PacketAliasRedirectAddr() 102 added to the API. The first function is a more generalized 103 version of PacketAliasPermanentLink(). The second function 104 implements static network address translation. 105 | 1/* -*- mode: c; tab-width: 8; c-basic-indent: 4; -*- 2 Alias_db.c encapsulates all data structures used for storing 3 packet aliasing data. Other parts of the aliasing software 4 access data through functions provided in this file. 5 6 Data storage is based on the notion of a "link", which is 7 established for ICMP echo/reply packets, UDP datagrams and 8 TCP stream connections. A link stores the original source --- 89 unchanged lines hidden (view full) --- 98 multiple external addresses rather than just a single 99 host address. 100 101 PacketAliasRedirectPort() and PacketAliasRedirectAddr() 102 added to the API. The first function is a more generalized 103 version of PacketAliasPermanentLink(). The second function 104 implements static network address translation. 105 |
106 Version 3.2: July, 2000 (salander and satoh) 107 Added FindNewPortGroup to get contiguous range of port values. 108 109 Added QueryUdpTcpIn and QueryUdpTcpOut to look for an aliasing 110 link but not actually add one. 111 112 Added FindRtspOut, which is closely derived from FindUdpTcpOut, 113 except that the alias port (from FindNewPortGroup) is provided 114 as input. 115 |
|
106 See HISTORY file for additional revisions. 107 | 116 See HISTORY file for additional revisions. 117 |
108 $FreeBSD: head/sys/netinet/libalias/alias_db.c 62159 2000-06-27 14:56:07Z ru $ | 118 $FreeBSD: head/sys/netinet/libalias/alias_db.c 63899 2000-07-26 23:15:46Z archie $ |
109*/ 110 111 112/* System include files */ 113#include <errno.h> 114#include <stdlib.h> 115#include <stdio.h> 116#include <unistd.h> --- 378 unchanged lines hidden (view full) --- 495 IncrementalCleanup() - look for stale links in a single chain 496 DeleteLink() - remove link 497 AddLink() - add link 498 ReLink() - change link 499 500Link search: 501 FindLinkOut() - find link for outgoing packets 502 FindLinkIn() - find link for incoming packets | 119*/ 120 121 122/* System include files */ 123#include <errno.h> 124#include <stdlib.h> 125#include <stdio.h> 126#include <unistd.h> --- 378 unchanged lines hidden (view full) --- 505 IncrementalCleanup() - look for stale links in a single chain 506 DeleteLink() - remove link 507 AddLink() - add link 508 ReLink() - change link 509 510Link search: 511 FindLinkOut() - find link for outgoing packets 512 FindLinkIn() - find link for incoming packets |
513 514Port search: 515 FindNewPortGroup() - find an available group of ports |
|
503*/ 504 505/* Local prototypes */ 506static int GetNewPort(struct alias_link *, int); 507 508static u_short GetSocket(u_short, int *, int); 509 510static void CleanupAliasData(void); --- 15 unchanged lines hidden (view full) --- 526FindLinkOut(struct in_addr, struct in_addr, u_short, u_short, int, int); 527 528static struct alias_link * 529FindLinkIn(struct in_addr, struct in_addr, u_short, u_short, int, int); 530 531 532#define ALIAS_PORT_BASE 0x08000 533#define ALIAS_PORT_MASK 0x07fff | 516*/ 517 518/* Local prototypes */ 519static int GetNewPort(struct alias_link *, int); 520 521static u_short GetSocket(u_short, int *, int); 522 523static void CleanupAliasData(void); --- 15 unchanged lines hidden (view full) --- 539FindLinkOut(struct in_addr, struct in_addr, u_short, u_short, int, int); 540 541static struct alias_link * 542FindLinkIn(struct in_addr, struct in_addr, u_short, u_short, int, int); 543 544 545#define ALIAS_PORT_BASE 0x08000 546#define ALIAS_PORT_MASK 0x07fff |
547#define ALIAS_PORT_MASK_EVEN 0x07ffe |
|
534#define GET_NEW_PORT_MAX_ATTEMPTS 20 535 536#define GET_ALIAS_PORT -1 537#define GET_ALIAS_ID GET_ALIAS_PORT 538 | 548#define GET_NEW_PORT_MAX_ATTEMPTS 20 549 550#define GET_ALIAS_PORT -1 551#define GET_ALIAS_ID GET_ALIAS_PORT 552 |
553#define FIND_EVEN_ALIAS_BASE 1 554 |
|
539/* GetNewPort() allocates port numbers. Note that if a port number 540 is already in use, that does not mean that it cannot be used by 541 another link concurrently. This is because GetNewPort() looks for 542 unused triplets: (dest addr, dest port, alias port). */ 543 544static int 545GetNewPort(struct alias_link *link, int alias_port_param) 546{ --- 152 unchanged lines hidden (view full) --- 699 else 700 { 701 close(sock); 702 return(0); 703 } 704} 705 706 | 555/* GetNewPort() allocates port numbers. Note that if a port number 556 is already in use, that does not mean that it cannot be used by 557 another link concurrently. This is because GetNewPort() looks for 558 unused triplets: (dest addr, dest port, alias port). */ 559 560static int 561GetNewPort(struct alias_link *link, int alias_port_param) 562{ --- 152 unchanged lines hidden (view full) --- 715 else 716 { 717 close(sock); 718 return(0); 719 } 720} 721 722 |
723/* FindNewPortGroup() returns a base port number for an available 724 range of contiguous port numbers. Note that if a port number 725 is already in use, that does not mean that it cannot be used by 726 another link concurrently. This is because FindNewPortGroup() 727 looks for unused triplets: (dest addr, dest port, alias port). */ 728 729int 730FindNewPortGroup(struct in_addr dst_addr, 731 struct in_addr alias_addr, 732 u_short src_port, 733 u_short dst_port, 734 u_short port_count, 735 u_char proto, 736 u_char align) 737{ 738 int i, j; 739 int max_trials; 740 u_short port_sys; 741 int link_type; 742 743 /* 744 * Get link_type from protocol 745 */ 746 747 switch (proto) 748 { 749 case IPPROTO_UDP: 750 link_type = LINK_UDP; 751 break; 752 case IPPROTO_TCP: 753 link_type = LINK_TCP; 754 break; 755 default: 756 return (0); 757 break; 758 } 759 760 /* 761 * The aliasing port is automatically selected 762 * by one of two methods below: 763 */ 764 max_trials = GET_NEW_PORT_MAX_ATTEMPTS; 765 766 if (packetAliasMode & PKT_ALIAS_SAME_PORTS) { 767 /* 768 * When the ALIAS_SAME_PORTS option is 769 * chosen, the first try will be the 770 * actual source port. If this is already 771 * in use, the remainder of the trials 772 * will be random. 773 */ 774 port_sys = ntohs(src_port); 775 776 } else { 777 778 /* First trial and all subsequent are random. */ 779 if (align == FIND_EVEN_ALIAS_BASE) 780 port_sys = random() & ALIAS_PORT_MASK_EVEN; 781 else 782 port_sys = random() & ALIAS_PORT_MASK; 783 784 port_sys += ALIAS_PORT_BASE; 785 } 786 787/* Port number search */ 788 for (i = 0; i < max_trials; i++) { 789 790 struct alias_link *search_result; 791 792 for (j = 0; j < port_count; j++) 793 if (0 != (search_result = FindLinkIn(dst_addr, alias_addr, 794 dst_port, htons(port_sys + j), 795 link_type, 0))) 796 break; 797 798 /* Found a good range, return base */ 799 if (j == port_count) 800 return (htons(port_sys)); 801 802 /* Find a new base to try */ 803 if (align == FIND_EVEN_ALIAS_BASE) 804 port_sys = random() & ALIAS_PORT_MASK_EVEN; 805 else 806 port_sys = random() & ALIAS_PORT_MASK; 807 808 port_sys += ALIAS_PORT_BASE; 809 } 810 811#ifdef DEBUG 812 fprintf(stderr, "PacketAlias/FindNewPortGroup(): "); 813 fprintf(stderr, "could not find free port(s)\n"); 814#endif 815 816 return(0); 817} 818 |
|
707static void 708CleanupAliasData(void) 709{ 710 struct alias_link *link; 711 int i, icount; 712 713 icount = 0; 714 for (i=0; i<LINK_TABLE_OUT_SIZE; i++) --- 880 unchanged lines hidden (view full) --- 1595 call_id, NO_DEST_PORT, GET_ALIAS_PORT, 1596 LINK_PPTP); 1597 } 1598 1599 return(link); 1600} 1601 1602 | 819static void 820CleanupAliasData(void) 821{ 822 struct alias_link *link; 823 int i, icount; 824 825 icount = 0; 826 for (i=0; i<LINK_TABLE_OUT_SIZE; i++) --- 880 unchanged lines hidden (view full) --- 1707 call_id, NO_DEST_PORT, GET_ALIAS_PORT, 1708 LINK_PPTP); 1709 } 1710 1711 return(link); 1712} 1713 1714 |
1715struct alias_link * 1716QueryUdpTcpIn(struct in_addr dst_addr, 1717 struct in_addr alias_addr, 1718 u_short dst_port, 1719 u_short alias_port, 1720 u_char proto) 1721{ 1722 int link_type; 1723 struct alias_link *link; 1724 1725 switch (proto) 1726 { 1727 case IPPROTO_UDP: 1728 link_type = LINK_UDP; 1729 break; 1730 case IPPROTO_TCP: 1731 link_type = LINK_TCP; 1732 break; 1733 default: 1734 return NULL; 1735 break; 1736 } 1737 1738 link = FindLinkIn(dst_addr, alias_addr, 1739 dst_port, alias_port, 1740 link_type, 0); 1741 1742 return(link); 1743} 1744 1745 1746struct alias_link * 1747QueryUdpTcpOut(struct in_addr src_addr, 1748 struct in_addr dst_addr, 1749 u_short src_port, 1750 u_short dst_port, 1751 u_char proto) 1752{ 1753 int link_type; 1754 struct alias_link *link; 1755 1756 switch (proto) 1757 { 1758 case IPPROTO_UDP: 1759 link_type = LINK_UDP; 1760 break; 1761 case IPPROTO_TCP: 1762 link_type = LINK_TCP; 1763 break; 1764 default: 1765 return NULL; 1766 break; 1767 } 1768 1769 link = FindLinkOut(src_addr, dst_addr, 1770 src_port, dst_port, 1771 link_type, 0); 1772 1773 return(link); 1774} 1775 1776 1777struct alias_link * 1778FindRtspOut(struct in_addr src_addr, 1779 struct in_addr dst_addr, 1780 u_short src_port, 1781 u_short alias_port, 1782 u_char proto) 1783{ 1784 int link_type; 1785 struct alias_link *link; 1786 1787 switch (proto) 1788 { 1789 case IPPROTO_UDP: 1790 link_type = LINK_UDP; 1791 break; 1792 case IPPROTO_TCP: 1793 link_type = LINK_TCP; 1794 break; 1795 default: 1796 return NULL; 1797 break; 1798 } 1799 1800 link = FindLinkOut(src_addr, dst_addr, src_port, 0, link_type, 1); 1801 1802 if (link == NULL) 1803 { 1804 struct in_addr alias_addr; 1805 1806 alias_addr = FindAliasAddress(src_addr); 1807 link = AddLink(src_addr, dst_addr, alias_addr, 1808 src_port, 0, alias_port, 1809 link_type); 1810 } 1811 1812 return(link); 1813} 1814 1815 |
|
1603struct in_addr 1604FindOriginalAddress(struct in_addr alias_addr) 1605{ 1606 struct alias_link *link; 1607 1608 link = FindLinkIn(nullAddress, alias_addr, 1609 0, 0, LINK_ADDR, 0); 1610 if (link == NULL) --- 1003 unchanged lines hidden --- | 1816struct in_addr 1817FindOriginalAddress(struct in_addr alias_addr) 1818{ 1819 struct alias_link *link; 1820 1821 link = FindLinkIn(nullAddress, alias_addr, 1822 0, 0, LINK_ADDR, 0); 1823 if (link == NULL) --- 1003 unchanged lines hidden --- |