First, the basic knowledge of the network:
IP routing table acquired on the device side
[[email protected]/] # Route–n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
Default 192.168.1.1 0.0.0.0 UG 0 0 0 apcli0
Default 192.168.2.254 0.0.0.0 UG 0 0 0 ra0
10.8.164.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 apcli0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 ra0
1 route Command parameters
[Email protected] ~]# route [-nee]
[[email protected] ~]# route add [-net|-host] [domain or host] netmask [mask] [Gw|dev]
[[email protected] ~]# route del [-net|-host] [domain or host] netmask [mask] [Gw|dev]
Parameters of the observation:
-N: Do not use the communication protocol or host name to directly use IP or port number;
-ee: Use more detailed information to display
Add (add) and delete (del) route related parameters:
-net: Indicates that the following route is a domain;
-host: Represents a route that is followed by a connection to a single host;
Netmask: Related to the domain, you can set the netmask to determine the size of the domain;
Gw:gateway, followed by the IP value oh, unlike Dev;
Dev: If you only want to specify that a network card is connected, use this setting, followed by eth0, etc.
Format: route-n
Format:/sbin/route-n
Used to print the routing table, plus the-n parameter is to print the IP address without printing the host name in the output information.
2 Example
Example 1: Displaying the current route
Command:
Route
Route-n
Example 2: Add a gateway/set up a gateway
Command: Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0
Example 3: Masking a route
Command: Route add-net 224.0.0.0 netmask 240.0.0.0 Reject
Example 4: Delete a route record
Command:
Route del-net 224.0.0.0 netmask 240.0.0.0
Route del-net 224.0.0.0 netmask 240.0.0.0 Reject
Example 5: Delete and add set default gateway
Command:
Route del default GW 192.168.120.240
Route add default GW 192.168.120.240
3 Related structures and definitions
struct Rtentry
{
unsigned long rt_pad1;
struct SOCKADDR rt_dst;
struct SOCKADDR rt_gateway;
struct SOCKADDR rt_genmask;
unsigned short rt_flags;
Short rt_pad2;
unsigned long rt_pad3;
void *rt_pad4;
Short rt_metric;
Char *rt_dev;
unsigned long rt_mtu;
#ifndef __kernel__
#define RT_MSS RT_MTU
#endif
unsigned long Rt_window;
unsigned short Rt_irtt;
};
#define RTF_UP 0x0001
#define Rtf_gateway 0x0002
#define Rtf_host 0x0004
#define Rtf_reinstate 0x0008
#define RTF_DYNAMIC 0x0010
#define Rtf_modified 0x0020
#define RTF_MTU 0x0040
#define RTF_MSS RTF_MTU
#define Rtf_window 0x0080
#define RTF_IRTT 0x0100
#define Rtf_reject 0x0200
4 control of the underlying kernel
int ioctl (int fd, int request, .../* void *arg */) detailed
The third argument is always a pointer, but the type of the pointer depends on the request parameter. We can divide network-related requests into 6 categories:
Socket interface Operation
File operations
Interface operation
ARP Cache operation
Routing table Operations
Flow System
The following table lists the request parameters for the network-related IOCTL request and the data type that the ARG address must point to:
|
request |
|
|
set connect Port |
siocatmark siocspgrp siocgpgrp |
Gets the process ID or process group ID of the socket interface |
int int int |
Text Thing |
Fionbio Fioasync Fionread Fiosetown Fiogetown |
Set/clear non-blocking I/O flag Set/clear signal-driven asynchronous I/O flag Gets the number of bytes in the receive buffer Set the process ID or process group ID of the file Gets the process ID or process group ID of the file |
Int Int Int Int Int |
Pick up Mouth |
siocgifconf siocsifaddr siocgifaddr siocsifflags siocgifflags siocsifdstaddr siocgifdstaddr siocgifbrdaddr siocsifbrdaddr siocgifnetmask siocsifnetmask siocgifmetric siocsifmetric siocgifmtu siocxxx |
get a list of all interfaces Set interface address Get interface address set interface flag Get interface flag set point to address Get broadcast address Set broadcast address get subnet mask set subnet mask Gets the measure of the interface Gets the interface MTU |
struct ifconf struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq struct ifreq |
ARP |
siocsarp siocgarp siocdarp |
Create/modify ARP table entry get ARP table entry delete arp table entry |
struct arpreq struct arpreq struct arpreq |
Lu By |
Siocaddrt Siocdelrt |
Add path Delete Path |
struct Rtentry struct Rtentry |
Flow |
I_xxx |
|
|
|
For routing operations, the function that completes the IOCTL is
int ip_rt_ioctl (unsigned int cmd, void *arg), this function determines whether to create a route table entry or delete a route table entry, depending on whether the incoming CMD is SIOCADDRT or SIOCDELRT.
Linux Network route