Implement router and packet filtering firewall in Linux

Source: Internet
Author: User
Article Title: Use Linux to implement routers and packet filtering firewalls. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Implement router and packet filtering firewall in Linux
  
Router and firewall]
Vro is a widely used device between IP segments. There are many ready-made products on the market. In applications, we often connect routers across the wan and LAN. most router products are designed based on this need. However, with the expansion of the user's IP network, we need a router that can address multiple Ethernet networks. the traditional router products occasionally have several dual-Ethernet network interfaces, however, such a product is especially expensive, and if it supports Fast Ethernet applications, the price will be astronomical. Layer-3 switches can implement such a function, however, layer-3 switches are not cheap either.
When a router is working, it queries its route table based on the destination IP address of the packet received by a port, and then decides to forward the packet to the corresponding port. There are several route tables for a vro: one is a route based on the IP address of each port of the vro and the subnet mask. This route is called a "fixed route "; the second is the system administrator's route to a subnet that needs to pass through a next-level router. this is called a static route "; in the network environment, let each vro broadcast its route information so that the routers can learn from each other. the learned route is called "dynamic routing ". The vro also forwards packets whose destination address is not in its route table to a preset IP address ". In the process of route matching, there are generally such priorities: fixed routes> static routes> Dynamic routes> default routes.
The router will only view the destination address of the IP data packet. that is to say, in principle, the router will "accept all" and forward all data packets, unless it cannot be sent out. If you want the router to add a check when forwarding data packets, check the data packet source and the application layer service type required by the data packet, determine whether the data packet should be forwarded or processed according to the pre-designed rules, so that the router is no longer a router in a simple sense, it is a type of firewall-packet filtering firewall.
The packet filtering firewall can check the source, source port, destination address, destination port, and transmission layer protocol type of the data packet, and match a rule table according to the content of the checked item, when the rule table is defined, the operation defined in the rule table is executed. Generally, the rule table can define the following operations: ACCEPT (pass through), NAT (MASQ address translation), DENY (discard), and REJECT (REJECT, at the same time, the "unavailable" message is sent to the source end ).
Of course, you can also buy out-of-the-box products that support the packet filtering firewall on the market, but the price issue is still an important factor that we have to consider. In particular, most products have many license and performance restrictions.
The Linux operating system is born from an IP address. apart from the price advantages of Linux, it is more attractive to the built-in powerful network functions, in addition to various Internet application services, linux also provides complete router and firewall functions. The system cost and Function Ratio brought by it are quite attractive. why don't we try it?
[Preparations for Linux systems]
Compared with ready-made router products, we call such a router a "soft router". of course, the performance of the router made in this way is definitely not inferior to that of the "hardware router, we only need to understand that many vrouters of the "25 ×" series only use the 25 m clock speed Motorola 68030CPU (performance equivalent to Intel 80386.
The hardware configuration requirements for Linux system hosts used as "software routers" are not too high. it is used as a route selection between the three or five local networks. if there are hundreds of computers, it is enough to choose a host with a Pentium 133 or above. of course, the current market is not very expensive for Sai Yang 2, Pentium 4, or other CPUs. configure 64 m memory, if conditions are met, it doesn't matter if you can configure it to MB. the hard disk should not be too large, otherwise it will be a waste.
The key lies in the configuration of network adapters. Linux supports Ethernet network adapters that can be seen in most markets. The installation of PCI cards is relatively simple, as long as the kernel supports, the system correctly identifies the number of cards at startup. The installation of the ISA card is not complex. for details, refer to my previous article.
Www.nyfzns.net www.520cat.com
The following figure shows the network interfaces that I have actually used:
★Intel EtherExpress Pro PCI100M
★RealTek 8029/8139 PCI 10 M/100 M
★D-link series (driven by Tulip 2114X) PCI 10 M
★ACCTON EN1207 (using Tulip 2114X driver) PCI 100 M
★3COM full series (PCI/ISA 10 M/100 M)
★AMD PC-NET 32 PCI 10 M
★Most NE2000 compatible NICs, such as Topstar, Dlink, ACCTON, and SN2000
★HardLink PCMCIA laptop dedicated Nic 10 M
Of course, Linux supports far more than these NICs.
There are several methods to mount the NIC driver to Linux. The most common method is to use the modprobe system tool to mount a kernel module. for example, you can load a NE2000 compatible NIC driver as follows:
# Modprobe ne io = 0x300
In fact, most PCI NICs do not need to specify the IO address range, just like loading an ACCTON EN1207c Nic:
# Modprobe tulip
Compiling the NIC driver to the Linux kernel is the most efficient method.
# Make menuconfig
Replace the "# make config" mentioned in many old documents. in this way, a menu is displayed, and the driver of the specified Network adapter is selected under the "Network Device" option in the menu, after compilation, you will get a complete Linux kernel that supports the corresponding Nic.
For many systems that can only recognize one network card, write the second and later network card parameters in/etc/lilo. conf is the best practice. here we have four NICs installed in one system, one is the RealTek 8139 of PCI, and the other three are NE2000, in this way, the system will not automatically recognize the three ISA cards after the 8139 Nic. we need to go to/etc/lilo. add the following sentence to conf:
Append =-ether = 3, 0x240, eth1 ether = 11, 0x300, eth2 ether = 4, 0x260, eth3-
During the kernel compilation process, we can select "optimize as router not host" in the Network option, so that the system will be optimized based on the router instead of the host. at the same time, you can also select "Fast switching", "Forwarding between high speed interfaces", and "advanced router" as needed ". If we plan to implement the packet filtering firewall on this machine, select "firewalling", "IP: masquerading", and "ip masq.
For details about how to compile the Kernel, see "Linux Kernel Howto ". At http://howto.fhlfox.dhs.org/online....what is the problem? /A>
Note that you can check the system prompt or use the dmesg command to check whether the system recognizes and starts the corresponding NIC driver. in the example above, a Realtek8139 Nic exists, we can see this situation on the machines with three NE2000 NICs:
Rtl8139.c: v1.07 5/6/99 Donald Becker http://cesdis.gsfc.nasa.gov/linux/drivers/r
Tl8139.html
Eth0: RealTek RTL8139 Fast Ethernet at 0x6000, IRQ 9, 00: e0: 4c: dd: 5f: 85.
Ne. c: v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)
NE * 000 ethercard probe at 0x240: 00 40 05 1f 60 9a
Eth1: NE2000 found at 0x240, using IRQ 3.
NE * 000 ethercard probe at 0x300: 00 40 05 22 08 65
Eth2: NE2000 forund at 0x300, using IRQ 11.
NE * 000 ethercard probe at 0x260: 00 40 05 23 59 0f
Eth3: NE2000 forund at 0x260, using IRQ 4.
Then configure the IP address of each network port. of course, you can use the dialog box of the operating system to configure the parameters of the first Nic, but an experienced network administrator generally does not, because we cannot configure other NICs, we must use this command to configure the first Nic:
# Ifconfig eth0 10.3.3.3 netmask 255.255.255.0 broadcast 10.3.3.255
The second Eni is named eth1, and the others are eth2, eth3, and so on.
Due to the limitation of the computer architecture, there is only one PCI controller on a general PC motherboard, while a PCI controller can only support up to four PCI devices at the same time, of course, some dedicated servers may support more than two PCI controllers. The installation of the ISA Nic is limited by the system interrupt number. there are only 16 available IRQ numbers in a single CPU system, and there is not much hardware left after the hard disk, memory, keyboard, and display card, therefore, four cards are usually installed. of course, the dual-CPU system can have 256 interrupt numbers available, which is much more plentiful.
In addition, we also need to turn on the IP forwarding switch in Linux, which is very easy. execute this command:
Echo 1>/proc/sys/net/ipv4/ip_forward
Of course, these configuration commands should be put in/etc/rc. d/rc. local, so that you do not need to reconfigure them after each boot.
[Implement vro in Linux]
Correct configuration? Why does the cursor drag? After parameters such as the P address, the system automatically calculates the fixed route of the subnet connected to each port. In this example, we can see that:
You can run the "route-n" or "netstat-r-n" command to view the current route table of the system. In the example shown on the right, we can obtain the following route table:
# Route-n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.3.2.4 0.0.0.0 255.255.255.252 U 0 0 0 eth0
10.3.2.128 0.0.0.0 255.255.255.128 U 0 0 0 eth3
10.3.253.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
The Destination term indicates the Destination network, and its size can be calculated using the subnet mask (Genmask). The Flags term indicates whether the network segment is directly connected. "U" indicates direct availability, metric is a level-1 hop, Ref is a priority reference, and Use Interface is the connection port. The Gateway column specifies the address of the next-level router when other routers need to be forwarded.
You can also create or delete a static route by running the route command. the format is as follows:
# Route add-net destination subnet address netmask destination subnet mask gw next-level router address
Similarly
# Route del-net destination subnet address netmask destination subnet mask gw next-level router address
You can set static routes.
Use
# Route add default gw gateway address
Or
# Route add-net 0.0.0.0 netmask 0.0.0.0 gw gateway address
We add the route settings command to/etc/rc. d/rc. local so that it can automatically run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.