这篇文章主要分析inet_init()函数的实现过程:
1796 static int __init inet_init(void)
1797 {
1798 struct inet_protosw *q;
1799 struct list_head *r;
1800 int rc = -EINVAL;
1801
1802 sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
1803
1804 rc = proto_register(&tcp_prot, 1);
1805 if (rc)
1806 goto out;
1807
1808 rc = proto_register(&udp_prot, 1);
1809 if (rc)
1810 goto out_unregister_tcp_proto;
1811
1812 rc = proto_register(&raw_prot, 1);
1813 if (rc)
1814 goto out_unregister_udp_proto;
1815
1816 rc = proto_register(&ping_prot, 1);
1817 if (rc)
1818 goto out_unregister_raw_proto;
1820 /*
1821 * Tell SOCKET that we are alive...
1822 */
1823
1824 (void)sock_register(&inet_family_ops);
1825
1826 #ifdef CONFIG_SYSCTL
1827 ip_static_sysctl_init();
1828 #endif
1829
1830 /*
1831 * Add all the base protocols.
1832 */
1833
1834 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1835 pr_crit("%s: Cannot add ICMP protocol\n", __func__);
1836 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1837 pr_crit("%s: Cannot add UDP protocol\n", __func__);
1838 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1839 pr_crit("%s: Cannot add TCP protocol\n", __func__);
1840 #ifdef CONFIG_IP_MULTICAST
1841 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1842 pr_crit("%s: Cannot add IGMP protocol\n", __func__);
1843 #endif
1844
1845 /* Register the socket-side information for inet_create. */
1846 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1847 INIT_LIST_HEAD(r);
1848
1849 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1850 inet_register_protosw(q);
1852 /*
1853 * Set the ARP module up
1854 */
1855
1856 arp_init();
1857
1858 /*
1859 * Set the IP module up
1860 */
1861
1862 ip_init();
1863
1864 /* Setup TCP slab cache for open requests. */
1865 tcp_init();
1866
1867 /* Setup UDP memory threshold */
1868 udp_init();
1869
1870 /* Add UDP-Lite (RFC 3828) */
1871 udplite4_register();
1872
1873 ping_init();
1874
1875 /*
1876 * Set the ICMP layer up
1877 */
1878
1879 if (icmp_init() < 0)
1880 panic("Failed to create the ICMP control socket.\n");
1881
1882 /*
1883 * Initialise the multicast router
1884 */
1885 #if defined(CONFIG_IP_MROUTE)
1886 if (ip_mr_init())
1887 pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
1888 #endif
1889
1890 if (init_inet_pernet_ops())
1891 pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
1892 /*
1893 * Initialise per-cpu ipv4 mibs
1894 */
1895
1896 if (init_ipv4_mibs())
1897 pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
1898
1899 ipv4_proc_init();
1900
1901 ipfrag_init();
1902
1903 dev_add_pack(&ip_packet_type);
1904
1905 ip_tunnel_core_init();
1906
1907 rc = 0;
1908 out:
1909 return rc;
1910 out_unregister_raw_proto:
1911 proto_unregister(&raw_prot);
1912 out_unregister_udp_proto:
1913 proto_unregister(&udp_prot);
1914 out_unregister_tcp_proto:
1915 proto_unreg