Internet協議族

來源:互聯網
上載者:User

標籤:網路

      Linux在2.6.32版本可支援37種協議族,每個協議族用一個net_proto_family結構執行個體來表示,在系統初始化時,以各協議族對應的協議族為下標,調用sock_register()將結構註冊到全域數組net_families[NPROTO]。此外還有一個地址族的概念,地址族用地址族常量來標識,到目前為止,協議族常量和地址族常量是一一對應的,且值相同

/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/* AppleTalk DDP */#define AF_NETROM6/* Amateur Radio NET/ROM */#define AF_BRIDGE7/* Multiprotocol bridge */#define AF_ATMPVC8/* ATM PVCs*/#define AF_X259/* Reserved for X.25 project */#define AF_INET610/* IP version 6*/#define AF_ROSE11/* Amateur Radio X.25 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_NETLINK16#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_WANPIPE25/* Wanpipe API Sockets */#define AF_LLC26/* Linux LLC*/#define AF_CAN29/* Controller Area Network      */#define AF_TIPC30/* TIPC sockets*/#define AF_BLUETOOTH31/* Bluetooth sockets */#define AF_IUCV32/* IUCV sockets*/#define AF_RXRPC33/* RxRPC sockets */#define AF_ISDN34/* mISDN sockets */#define AF_PHONET35/* Phonet sockets*/#define AF_IEEE80215436/* IEEE802154 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

對應不同的協議族,其傳輸層的結構和實現有著巨大的差異,因此其各自的套介面建立函數也會有很大區別,而通過net_proto_family結構屏蔽了這些區別,使得各個協議在初始化的時候,可以統一用sock_register()註冊到net_families數組中。因此實際上net_proto_family結構提供了一個協議族到套介面建立之間的介面

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 protocol handler that wants to *advertise its address family, and have 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);printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);return err;}

inet_protoswinet_protosw結構在每次建立套介面時會用到,此結構也只在套介面層起作用

/* This is used to register socket interfaces for IP protocols.  */struct inet_protosw {struct list_head list;        /* These two fields form the lookup key.  */unsigned short type;   /* This is 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 do      * we need to use this socket      * interface?                                      */char             no_check;   /* checksum on rcv/xmit/none? */unsigned char flags;      /* See INET_PROTOSW_* below.  */};
type

標識套介面的類型,對於Internet協議族共有三種類型SOCK_STREAM、SOCK_DGRAM和SOCK_RAW,與應用程式層建立套介面函數socket()的第二個參數type取值恰好對應

protocol

標識協議族中四層協議號,Internet協議族中的值包括IPPROTO_TCP、IPPROTO_UDP等

prot

套介面網路層介面。TCP為tcp_prot;UDP為udp_prot;原始套介面raw_prot。

ops

套介面傳輸層介面。TCP為inet_stream_ops;UDP為inet_dgram_ops;原始套介面inet_sockraw_ops。

capability

當大於零時,需要檢驗當前建立套介面的進程是否有這種能力。

flags

INET_PROTOSW_REUSE 標識連接埠是否能被重用

INET_PROTOSW_PERMANENT 標識此協議不能被替換或卸載

INET_PROTOSW_ICSK 標識是不是連續的套介面

inet_register_protosw()將inet_protosw執行個體註冊到inet_sw散列表中,而inet_unregister_protosw()可以將指定的inet_protosw執行個體從inetsw散列表中登出,但是注意:被標識為INET_PROTOSW_PERMANENT的inet_protosw執行個體不能重載或登出。

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 are 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 that * the new entry does not override a permanent entry when matched with * a wild-card protocol. But it is allowed to override any existing * non-permanent entry.  This means that when 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 type %d.\n",       p->type);goto out;}
net_protocol

net_protocol是一個非常重要的結構,定義了協議族中支援的傳輸層協議以及傳輸層的報文接收常式。此結構是網路層和傳輸層之前的橋樑,當網路資料報文從網路層流向傳輸層時,會調用此結構中的傳輸層協議資料報接收處理函數。

/* 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)

傳輸層協議資料報接收處理函數,當網路層接收IP資料報之後,根據IP資料報所指示傳輸層協議,調用對應傳輸層net_protocol結構的該常式接收報文。

void (*err_handler)(struct sk_buff *skb, u32 info)
在ICMP模組中接收到差錯報文後,會解析差錯報文,並根據差錯報文中原始的IP首部,調用對應傳輸層的異常處理函數err_handler()。

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

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

GSO是網路裝置支援傳輸層的一個功能

當GSO資料報輸出時到達網路裝置,如果網路裝置不支援GSO的情況,則需要傳輸層對輸出的資料報重新進行GSO分段和校正和的計算。因此需要網路層提供介面給裝置層,能夠訪問到傳輸層的GSO分段和校正和的計算功能,對輸出的資料報進行分段和執行校正和

unsigned int no_policy

標識在路由時是否進行策略路由,TCP和UDP預設不進行策略路由

核心中為Internet協議定義了4個net_protocol結構執行個體-------icmp_protocol,udp_protocol,tcp_protocol,igmp_protocol,分別於ICMP、UDP、TCP、IGMP協議一一對應。在Internet協議族初始化時,調用inet_add_protocol()將它們註冊到net_protocol結構指標數組inet_protos[MAX_INET_PROTOS]中,在系統運行過程中,隨時可以用核心模組載入/卸載的方式,調用函數inet_add_protocol/inet_del_protocol將net_protocol結構執行個體註冊到inet_protos[]數組中,或從中刪除。


Internet協議族的初始化

Internet協議族的初始化函數為inet_init(),通過fs_initcall(inet_init),將inet_init()加到核心的初始化列表中,保證了此函數會在系統啟動時被調用

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(struct inet_skb_parm) > sizeof(dummy_skb->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 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_create. */for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)INIT_LIST_HEAD(r);for (q = inetsw_array; q < &inetsw_array[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();dev_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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.