Internet Protocol Family

Source: Internet
Author: User

Linux in 2.6.32 version can support 37 protocol families, each protocol family is represented by a net_proto_family structure instance, at the time of system initialization, the protocol family corresponding to each protocol family is subscript, call Sock_register () to register the structure to the global array net_ Families[nproto]. There is also an address family concept, address family is identified by the address family constants, so far, the Protocol family constant and address family constants are one by one corresponding, and the same value

/include/linux/sock.h

/* Supported address families. */#define Af_unspec0#define af_unix1/* Unix domain sockets */#define AF_LOCAL1/* POSIX name for af_unix*/#define AF_INET2/ * Internet IP Protocol */#define af_ax253/* amateur Radio ax.25 */#define af_ipx4/* Novell IPX */#define af_appletalk5/* A Ppletalk DDP */#define af_netrom6/* amateur Radio net/rom * * #define AF_BRIDGE7/* Multiprotocol Bridge */#define AF_ATMPVC8 /* ATM pvcs*/#define AF_X259/* Reserved for X. */#define AF_INET610/* IP version 6*/#define AF_ROSE11/* Amateur Radio X. plp*/#define af_decnet12/* Reserved for DECnet project*/#define af_netbeui13/* Reserved for 802.2LLC project*/# Define af_security14/* Security callback pseudo AF */#define AF_KEY15/* Pf_key KEY Management API */#define Af_netli Nk16#define af_routeaf_netlink/* Alias to emulate 4.4BSD */#define af_packet17/* Packet family*/#define af_ash18/* ash*/# Define af_econet19/* Acorn econet*/#define af_atmsvc20/* ATM svcs*/#define af_rds21/* RDS sockets */#define AF_SNA22/* Linux SNA Project (nutters!) */#define af_irda23/* IRDA sockets*/#define af_pppox24/* pppox sockets*/#define Af_wanpip e25/* wanpipe API Sockets */#define af_llc26/* Linux llc*/#define af_can29/* Controller Area Network */#define Af_tip c30/* TIPC sockets*/#define af_bluetooth31/* Bluetooth sockets */#define af_iucv32/* IUCV sockets*/#define af_rxrpc33/* R XRPC sockets */#define AF_ISDN34/* MISDN Sockets * * #define AF_PHONET35/* phonet sockets*/#define af_ieee80215436/* IEEE80 2154 sockets*/#define af_max37/* for now. *//* Protocol families, same as address families. */#define Pf_unspecaf_unspec#define pf_unixaf_unix#define pf_localaf_local#define pf_inetaf_inet#define PF_AX25AF_ Ax25#define Pf_ipxaf_ipx#define Pf_appletalkaf_appletalk#definepf_netromaf_netrom#define PF_BRIDGEAF_BRIDGE# Define Pf_atmpvcaf_atmpvc#define pf_x25af_x25#define pf_inet6af_inet6#define pf_roseaf_rose#define PF_DECnetAF_ Decnet#define Pf_netbeuiaf_netbeui#define Pf_securityaf_security#define Pf_keyaf_key#defIne pf_netlinkaf_netlink#define pf_routeaf_route#define pf_packetaf_packet#define PF_ASHAF_ASH#define PF_ECONETAF_ Econet#define pf_atmsvcaf_atmsvc#define pf_rdsaf_rds#define pf_snaaf_sna#define PF_IRDAAF_IRDA#define PF_PPPOXAF_ Pppox#define pf_wanpipeaf_wanpipe#define pf_llcaf_llc#define pf_canaf_can#define PF_TIPCAF_TIPC#define PF_ Bluetoothaf_bluetooth#define pf_iucvaf_iucv#define pf_rxrpcaf_rxrpc#define pf_isdnaf_isdn#define PF_PHONETAF_ Phonet#define Pf_ieee802154af_ieee802154#define Pf_maxaf_max

corresponding to the different protocol families, the structure and implementation of the transport layer are greatly different, so their respective sets of interface creation function will be very different, and through the net_proto_family structure shielding these differences, so that each protocol at the time of initialization, can be unified with Sock_register ( ) is registered in the Net_families array. So actually the net_proto_family structure provides an interface between the protocol family and the set interface creation

net_proto_family


struct net_proto_family {intfamily;int (*create) (struct net *net, struct socket *sock, int protocol); struct Module*owner;} ;/** *sock_register-add a socket protocol handler * @ops: Description of protocol * *this function is called by a Protoco L handler that wants to *advertise its address family, and has it linked into the *socket interface. The value ops->family coresponds to the *socket system call protocol family. */int sock_register (const struct net_proto_family *ops) {int err;if (ops->family >= nproto) {PRINTK (Kern_crit) Protocol%d >= Nproto (%d) \ n ", ops->family,       Nproto); return-enobufs;} Spin_lock (&net_family_lock); if (net_families[ops->family]) Err =-eexist;else {net_families[ops->family] = Ops;err = 0;} Spin_unlock (&net_family_lock);p rintk (kern_info "net:registered protocol family%d\n", ops->family); return err ;}

INET_PROTOSWThe INET_PROTOSW structure is used every time a socket is created, and this structure works only at the socket Layer

/* This is used-register socket interfaces for IP protocols.  */struct inet_protosw {struct List_head list;        /* These the form the lookup key.  */unsigned short type;   /* The 2nd argument to socket (2). */unsigned Short Protocol; /* This is the L4 protocol number.  */struct Proto *prot;const struct proto_ops *ops;  int              capability;/* which (if any) capability      does * We need to use this socket      * interface?                                      */char             No_check;   /* checksum on Rcv/xmit/none? */unsigned char flags;      /* See inet_protosw_* below.  */};
type

The type of the identity socket interface, there are three types of sock_stream, Sock_dgram, and Sock_raw for the Internet Protocol family, and the second parameter of the socket () that creates the socket function with the application layer exactly corresponds to the value of type

Protocol

Identifies the four-layer protocol number in the Protocol family, and the values in the Internet Protocol family include IPPROTO_TCP, IPPROTO_UDP, etc.

Prot

Socket interface Network layer interface. TCP is Tcp_prot; UDP is Udp_prot; The original socket interface is Raw_prot.

Ops

Socket interface Transport Layer interface. TCP is inet_stream_ops; UDP is inet_dgram_ops; The original socket interface is inet_sockraw_ops.

Capability

When greater than zero, you need to verify that the process that is currently creating the socket interface has this capability.

Flags

Inet_protosw_reuse identifies if the port can be reused

Inet_protosw_permanent identifies that this protocol cannot be replaced or uninstalled

INET_PROTOSW_ICSK identity is not a continuous set of interfaces

INET_REGISTER_PROTOSW () registers the INET_PROTOSW instance in the INET_SW hash list, INET_UNREGISTER_PROTOSW () can unregister the specified INET_PROTOSW instance from the INETSW hash list, but note that the inet_ that is identified as Inet_protosw_permanent PROTOSW instances cannot be overloaded or unregistered.

void Inet_register_protosw (struct inet_protosw *p) {struct List_head *lh;struct inet_protosw *answer;int protocol = P >protocol;struct list_head *last_perm;spin_lock_bh (&inetsw_lock); if (P->type >= SOCK_MAX) goto Out_ illegal;/* If We is trying to override a permanent protocol, bail. */answer = Null;last_perm = &inetsw[p->type];list_for_each (LH, &inetsw[p->type]) {answer = List_entry (LH , struct INET_PROTOSW, list);/* Check only the Non-wild match. */if (Inet_protosw_permanent & Answer->flags) {if (protocol = = Answer->protocol) Break;last_perm = LH;} Answer = NULL;}  if (answer) Goto out_permanent;/* ADD The new entry after the last permanent entry if any, so it * The new entry does not Override a permanent entry when matched with * a wild-card protocol.  But it's allowed to override any existing * non-permanent entry. This means if we remove this entry, the * system automatically returns to the old behavior. */list_add_rcu (&p->list, last_pERM); Out:spin_unlock_bh (&inetsw_lock); RETURN;OUT_PERMANENT:PRINTK (Kern_err "attempt to override Permanent Protocol%d.\n ", protocol); Goto OUT;OUT_ILLEGAL:PRINTK (kern_err" ignoring attempt to register invalid socket t Ype%d.\n ", p->type); goto out;}
Net_protocol

Net_protocol is a very important structure that defines the transport layer protocols that are supported in the protocol family and the message reception routines of the transport layer. This structure is the bridge between the network layer and the transport layer, and when the network data packet flows from the network layer to the transport layer, the Transport layer protocol datagram receive handler function in this structure is called.

/* This is used to register protocols. */struct net_protocol {int (*handler) (struct sk_buff *skb), Void (*err_handler) (struct Sk_buff *skb, u32 info); Int (*gso_ Send_check) (struct sk_buff *skb), struct sk_buff       * (*gso_segment) (struct sk_buff *skb,       int features); struct SK_ Buff      * * (*gro_receive) (struct sk_buff **head,       struct sk_buff *skb); int (*gro_complete) (struct sk_buff *skb); unsigned intno_policy:1,netns_ok:1;};
Int (*handler) (struct Sk_buff *skb)

The Transport Layer Protocol datagram receives the processing function, when the network layer receives the IP datagram, according to the IP datagram indicated the Transport layer protocol, calls the corresponding transport layer Net_protocol structure The routine receives the message.

void (*err_handler) (struct Sk_buff *skb, u32 info)
After receiving the error message in the ICMP module, the error message is parsed and the exception handler function Err_handler () of the corresponding transport layer is called according to the original IP header in the error message.

Int (*gso_send_check) (struct sk_buff *skb);

struct Sk_buff * (*gso_segment) (struct sk_buff *skb, int features);

GSO is a feature of the network Device support transport layer

When the GSO datagram is output to a network device, if the network device does not support GSO, the transport layer is required to re-calculate the GSO segmentation and checksum for the output datagram. Therefore, the network layer is required to provide interfaces to the device layer, to access the GSO segmentation and checksum of the transport layer, to segment and perform checksums on the output datagrams.

unsigned int no_policy

Identifies whether a policy route is being routed while TCP and UDP are not policy-routed by default

The kernel defines 4 Net_protocol structure instances-------Icmp_protocol,udp_protocol,tcp_protocol,igmp_protocol for the Internet Protocol, respectively, for ICMP, UDP, TCP, IGMP protocol one by one corresponds. When the Internet protocol family is initialized, call Inet_add_protocol () to register them in the Net_protocol struct pointer array Inet_protos[max_inet_protos], and during system operation, Any time a kernel module can be loaded/unloaded, call the function Inet_add_protocol/inet_del_protocol Register the Net_protocol struct instance in the inet_protos[] array, or remove it from it.


initialization of the Internet Protocol family

The initialization function of the Internet Protocol Family is inet_init (), and the Inet_init () is added to the initialization list of the kernel via Fs_initcall (inet_init), ensuring that this function is called at system startup

static int __init inet_init (void) {struct Sk_buff *dummy_skb;struct inet_protosw *q;struct list_head *r;int rc =-EINVAL; build_bug_on (sizeof inet_skb_parm) > sizeof (DUMMY_SKB-&GT;CB)), rc = Proto_register (&tcp_prot, 1); if (RC Goto OUT;RC = Proto_register (&udp_prot, 1), if (rc) goto OUT_UNREGISTER_TCP_PROTO;RC = Proto_register (&raw_prot , 1); if (RC) goto out_unregister_udp_proto;/* *tell SOCKET that is we are alive ... */(void) Sock_register (&inet_family_ OPS); #ifdef config_sysctlip_static_sysctl_init (); #endif/* *add all the base protocols. */if (Inet_add_protocol (&icmp_protocol, ipproto_icmp) < 0) PRINTK (kern_crit "inet_init:cannot" Add ICMP protocol\ N "), if (Inet_add_protocol (&udp_protocol, IPPROTO_UDP) < 0) PRINTK (kern_crit" inet_init:cannot add UDP protocol\n ") if (Inet_add_protocol (&tcp_protocol, ipproto_tcp) < 0) PRINTK (kern_crit" inet_init:cannot Add TCP protocol\n " ); #ifdef config_ip_multicastif (Inet_add_protocol (&igmp_protocol, IPPROTO_IGMP) < 0) PRINTK (kern_crit "inet_init:cannot add IGMP protocol\n"), #endif/* Register the Socket-side information for Inet_c Reate. */for (R = &inetsw[0]; R < &inetsw[SOCK_MAX]; ++r) Init_list_head (R); for (q = inetsw_array; Q < &inetsw_a Rray[inetsw_array_len]; ++Q) INET_REGISTER_PROTOSW (q);/* *set the ARP module up */arp_init ();/* *set the IP module up */ip_init (); Tcp_v4_init ();/* Setup TCP Slab cache for open requests. */tcp_init ();/* Setup UDP Memory threshold */udp_init ();/* Add udp-lite (RFC 3828) */udplite4_register ();/* *set the ICMP  Layer Up */if (icmp_init () < 0) Panic ("Failed to create the ICMP control socket.\n");/* *initialise the multicast router */#if defined (config_ip_mroute) if (Ip_mr_init ()) PRINTK (kern_crit "inet_init:cannot init IPv4 mroute\n"); #endif/* * Initialise per-cpu IPv4 MIBs */if (Init_ipv4_mibs ()) PRINTK (kern_crit "inet_init:cannot init IPv4 mibs\n"); ipv4_proc_ Init (); Ipfrag_init ();d ev_add_pack (&ip_packet_type); rc = 0;out:return Rc;out_unregiSter_udp_proto:proto_unregister (&udp_prot); Out_unregister_tcp_proto:proto_unregister (&tcp_prot); goto Out;}



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.