af_inet.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * PF_INET protocol family socket handler.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Florian La Roche, <flla@stud.uni-sb.de>
  11. * Alan Cox, <A.Cox@swansea.ac.uk>
  12. *
  13. * Changes (see also sock.c)
  14. *
  15. * piggy,
  16. * Karl Knutson : Socket protocol table
  17. * A.N.Kuznetsov : Socket death error in accept().
  18. * John Richardson : Fix non blocking error in connect()
  19. * so sockets that fail to connect
  20. * don't return -EINPROGRESS.
  21. * Alan Cox : Asynchronous I/O support
  22. * Alan Cox : Keep correct socket pointer on sock
  23. * structures
  24. * when accept() ed
  25. * Alan Cox : Semantics of SO_LINGER aren't state
  26. * moved to close when you look carefully.
  27. * With this fixed and the accept bug fixed
  28. * some RPC stuff seems happier.
  29. * Niibe Yutaka : 4.4BSD style write async I/O
  30. * Alan Cox,
  31. * Tony Gale : Fixed reuse semantics.
  32. * Alan Cox : bind() shouldn't abort existing but dead
  33. * sockets. Stops FTP netin:.. I hope.
  34. * Alan Cox : bind() works correctly for RAW sockets.
  35. * Note that FreeBSD at least was broken
  36. * in this respect so be careful with
  37. * compatibility tests...
  38. * Alan Cox : routing cache support
  39. * Alan Cox : memzero the socket structure for
  40. * compactness.
  41. * Matt Day : nonblock connect error handler
  42. * Alan Cox : Allow large numbers of pending sockets
  43. * (eg for big web sites), but only if
  44. * specifically application requested.
  45. * Alan Cox : New buffering throughout IP. Used
  46. * dumbly.
  47. * Alan Cox : New buffering now used smartly.
  48. * Alan Cox : BSD rather than common sense
  49. * interpretation of listen.
  50. * Germano Caronni : Assorted small races.
  51. * Alan Cox : sendmsg/recvmsg basic support.
  52. * Alan Cox : Only sendmsg/recvmsg now supported.
  53. * Alan Cox : Locked down bind (see security list).
  54. * Alan Cox : Loosened bind a little.
  55. * Mike McLagan : ADD/DEL DLCI Ioctls
  56. * Willy Konynenberg : Transparent proxying support.
  57. * David S. Miller : New socket lookup architecture.
  58. * Some other random speedups.
  59. * Cyrus Durgin : Cleaned up file for kmod hacks.
  60. * Andi Kleen : Fix inet_stream_connect TCP race.
  61. *
  62. * This program is free software; you can redistribute it and/or
  63. * modify it under the terms of the GNU General Public License
  64. * as published by the Free Software Foundation; either version
  65. * 2 of the License, or (at your option) any later version.
  66. */
  67. #define pr_fmt(fmt) "IPv4: " fmt
  68. #include <linux/err.h>
  69. #include <linux/errno.h>
  70. #include <linux/types.h>
  71. #include <linux/socket.h>
  72. #include <linux/in.h>
  73. #include <linux/kernel.h>
  74. #include <linux/kmod.h>
  75. #include <linux/sched.h>
  76. #include <linux/timer.h>
  77. #include <linux/string.h>
  78. #include <linux/sockios.h>
  79. #include <linux/net.h>
  80. #include <linux/capability.h>
  81. #include <linux/fcntl.h>
  82. #include <linux/mm.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/stat.h>
  85. #include <linux/init.h>
  86. #include <linux/poll.h>
  87. #include <linux/netfilter_ipv4.h>
  88. #include <linux/random.h>
  89. #include <linux/slab.h>
  90. #include <asm/uaccess.h>
  91. #include <linux/inet.h>
  92. #include <linux/igmp.h>
  93. #include <linux/inetdevice.h>
  94. #include <linux/netdevice.h>
  95. #include <net/checksum.h>
  96. #include <net/ip.h>
  97. #include <net/protocol.h>
  98. #include <net/arp.h>
  99. #include <net/route.h>
  100. #include <net/ip_fib.h>
  101. #include <net/inet_connection_sock.h>
  102. #include <net/tcp.h>
  103. #include <net/udp.h>
  104. #include <net/udplite.h>
  105. #include <net/ping.h>
  106. #include <linux/skbuff.h>
  107. #include <net/sock.h>
  108. #include <net/raw.h>
  109. #include <net/icmp.h>
  110. #include <net/inet_common.h>
  111. #include <net/ip_tunnels.h>
  112. #include <net/xfrm.h>
  113. #include <net/net_namespace.h>
  114. #include <net/secure_seq.h>
  115. #ifdef CONFIG_IP_MROUTE
  116. #include <linux/mroute.h>
  117. #endif
  118. #include <net/l3mdev.h>
  119. /* The inetsw table contains everything that inet_create needs to
  120. * build a new socket.
  121. */
  122. static struct list_head inetsw[SOCK_MAX];
  123. static DEFINE_SPINLOCK(inetsw_lock);
  124. /* New destruction routine */
  125. void inet_sock_destruct(struct sock *sk)
  126. {
  127. struct inet_sock *inet = inet_sk(sk);
  128. __skb_queue_purge(&sk->sk_receive_queue);
  129. __skb_queue_purge(&sk->sk_error_queue);
  130. sk_mem_reclaim(sk);
  131. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  132. pr_err("Attempt to release TCP socket in state %d %p\n",
  133. sk->sk_state, sk);
  134. return;
  135. }
  136. if (!sock_flag(sk, SOCK_DEAD)) {
  137. pr_err("Attempt to release alive inet socket %p\n", sk);
  138. return;
  139. }
  140. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  141. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  142. WARN_ON(sk->sk_wmem_queued);
  143. WARN_ON(sk->sk_forward_alloc);
  144. kfree(rcu_dereference_protected(inet->inet_opt, 1));
  145. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  146. dst_release(sk->sk_rx_dst);
  147. sk_refcnt_debug_dec(sk);
  148. }
  149. EXPORT_SYMBOL(inet_sock_destruct);
  150. /*
  151. * The routines beyond this point handle the behaviour of an AF_INET
  152. * socket object. Mostly it punts to the subprotocols of IP to do
  153. * the work.
  154. */
  155. /*
  156. * Automatically bind an unbound socket.
  157. */
  158. static int inet_autobind(struct sock *sk)
  159. {
  160. struct inet_sock *inet;
  161. /* We may need to bind the socket. */
  162. lock_sock(sk);
  163. inet = inet_sk(sk);
  164. if (!inet->inet_num) {
  165. if (sk->sk_prot->get_port(sk, 0)) {
  166. release_sock(sk);
  167. return -EAGAIN;
  168. }
  169. inet->inet_sport = htons(inet->inet_num);
  170. }
  171. release_sock(sk);
  172. return 0;
  173. }
  174. /*
  175. * Move a socket into listening state.
  176. */
  177. int inet_listen(struct socket *sock, int backlog)
  178. {
  179. struct sock *sk = sock->sk;
  180. unsigned char old_state;
  181. int err;
  182. lock_sock(sk);
  183. err = -EINVAL;
  184. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  185. goto out;
  186. old_state = sk->sk_state;
  187. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  188. goto out;
  189. /* Really, if the socket is already in listen state
  190. * we can only allow the backlog to be adjusted.
  191. */
  192. if (old_state != TCP_LISTEN) {
  193. /* Enable TFO w/o requiring TCP_FASTOPEN socket option.
  194. * Note that only TCP sockets (SOCK_STREAM) will reach here.
  195. * Also fastopen backlog may already been set via the option
  196. * because the socket was in TCP_LISTEN state previously but
  197. * was shutdown() rather than close().
  198. */
  199. if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
  200. (sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
  201. !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
  202. fastopen_queue_tune(sk, backlog);
  203. tcp_fastopen_init_key_once(true);
  204. }
  205. err = inet_csk_listen_start(sk, backlog);
  206. if (err)
  207. goto out;
  208. }
  209. sk->sk_max_ack_backlog = backlog;
  210. err = 0;
  211. out:
  212. release_sock(sk);
  213. return err;
  214. }
  215. EXPORT_SYMBOL(inet_listen);
  216. /*
  217. * Create an inet socket.
  218. */
  219. static int inet_create(struct net *net, struct socket *sock, int protocol,
  220. int kern)
  221. {
  222. struct sock *sk;
  223. struct inet_protosw *answer;
  224. struct inet_sock *inet;
  225. struct proto *answer_prot;
  226. unsigned char answer_flags;
  227. int try_loading_module = 0;
  228. int err;
  229. if (protocol < 0 || protocol >= IPPROTO_MAX)
  230. return -EINVAL;
  231. sock->state = SS_UNCONNECTED;
  232. /* Look for the requested type/protocol pair. */
  233. lookup_protocol:
  234. err = -ESOCKTNOSUPPORT;
  235. rcu_read_lock();
  236. list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
  237. err = 0;
  238. /* Check the non-wild match. */
  239. if (protocol == answer->protocol) {
  240. if (protocol != IPPROTO_IP)
  241. break;
  242. } else {
  243. /* Check for the two wild cases. */
  244. if (IPPROTO_IP == protocol) {
  245. protocol = answer->protocol;
  246. break;
  247. }
  248. if (IPPROTO_IP == answer->protocol)
  249. break;
  250. }
  251. err = -EPROTONOSUPPORT;
  252. }
  253. if (unlikely(err)) {
  254. if (try_loading_module < 2) {
  255. rcu_read_unlock();
  256. /*
  257. * Be more specific, e.g. net-pf-2-proto-132-type-1
  258. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  259. */
  260. if (++try_loading_module == 1)
  261. request_module("net-pf-%d-proto-%d-type-%d",
  262. PF_INET, protocol, sock->type);
  263. /*
  264. * Fall back to generic, e.g. net-pf-2-proto-132
  265. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  266. */
  267. else
  268. request_module("net-pf-%d-proto-%d",
  269. PF_INET, protocol);
  270. goto lookup_protocol;
  271. } else
  272. goto out_rcu_unlock;
  273. }
  274. err = -EPERM;
  275. if (sock->type == SOCK_RAW && !kern &&
  276. !ns_capable(net->user_ns, CAP_NET_RAW))
  277. goto out_rcu_unlock;
  278. sock->ops = answer->ops;
  279. answer_prot = answer->prot;
  280. answer_flags = answer->flags;
  281. rcu_read_unlock();
  282. WARN_ON(!answer_prot->slab);
  283. err = -ENOBUFS;
  284. sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
  285. if (!sk)
  286. goto out;
  287. err = 0;
  288. if (INET_PROTOSW_REUSE & answer_flags)
  289. sk->sk_reuse = SK_CAN_REUSE;
  290. inet = inet_sk(sk);
  291. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  292. inet->nodefrag = 0;
  293. if (SOCK_RAW == sock->type) {
  294. inet->inet_num = protocol;
  295. if (IPPROTO_RAW == protocol)
  296. inet->hdrincl = 1;
  297. }
  298. if (net->ipv4.sysctl_ip_no_pmtu_disc)
  299. inet->pmtudisc = IP_PMTUDISC_DONT;
  300. else
  301. inet->pmtudisc = IP_PMTUDISC_WANT;
  302. inet->inet_id = 0;
  303. sock_init_data(sock, sk);
  304. sk->sk_destruct = inet_sock_destruct;
  305. sk->sk_protocol = protocol;
  306. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  307. inet->uc_ttl = -1;
  308. inet->mc_loop = 1;
  309. inet->mc_ttl = 1;
  310. inet->mc_all = 1;
  311. inet->mc_index = 0;
  312. inet->mc_list = NULL;
  313. inet->rcv_tos = 0;
  314. sk_refcnt_debug_inc(sk);
  315. if (inet->inet_num) {
  316. /* It assumes that any protocol which allows
  317. * the user to assign a number at socket
  318. * creation time automatically
  319. * shares.
  320. */
  321. inet->inet_sport = htons(inet->inet_num);
  322. /* Add to protocol hash chains. */
  323. err = sk->sk_prot->hash(sk);
  324. if (err) {
  325. sk_common_release(sk);
  326. goto out;
  327. }
  328. }
  329. if (sk->sk_prot->init) {
  330. err = sk->sk_prot->init(sk);
  331. if (err)
  332. sk_common_release(sk);
  333. }
  334. out:
  335. return err;
  336. out_rcu_unlock:
  337. rcu_read_unlock();
  338. goto out;
  339. }
  340. /*
  341. * The peer socket should always be NULL (or else). When we call this
  342. * function we are destroying the object and from then on nobody
  343. * should refer to it.
  344. */
  345. int inet_release(struct socket *sock)
  346. {
  347. struct sock *sk = sock->sk;
  348. if (sk) {
  349. long timeout;
  350. /* Applications forget to leave groups before exiting */
  351. ip_mc_drop_socket(sk);
  352. /* If linger is set, we don't return until the close
  353. * is complete. Otherwise we return immediately. The
  354. * actually closing is done the same either way.
  355. *
  356. * If the close is due to the process exiting, we never
  357. * linger..
  358. */
  359. timeout = 0;
  360. if (sock_flag(sk, SOCK_LINGER) &&
  361. !(current->flags & PF_EXITING))
  362. timeout = sk->sk_lingertime;
  363. sock->sk = NULL;
  364. sk->sk_prot->close(sk, timeout);
  365. }
  366. return 0;
  367. }
  368. EXPORT_SYMBOL(inet_release);
  369. int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  370. {
  371. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  372. struct sock *sk = sock->sk;
  373. struct inet_sock *inet = inet_sk(sk);
  374. struct net *net = sock_net(sk);
  375. unsigned short snum;
  376. int chk_addr_ret;
  377. u32 tb_id = RT_TABLE_LOCAL;
  378. int err;
  379. /* If the socket has its own bind function then use it. (RAW) */
  380. if (sk->sk_prot->bind) {
  381. err = sk->sk_prot->bind(sk, uaddr, addr_len);
  382. goto out;
  383. }
  384. err = -EINVAL;
  385. if (addr_len < sizeof(struct sockaddr_in))
  386. goto out;
  387. if (addr->sin_family != AF_INET) {
  388. /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
  389. * only if s_addr is INADDR_ANY.
  390. */
  391. err = -EAFNOSUPPORT;
  392. if (addr->sin_family != AF_UNSPEC ||
  393. addr->sin_addr.s_addr != htonl(INADDR_ANY))
  394. goto out;
  395. }
  396. tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
  397. chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
  398. /* Not specified by any standard per-se, however it breaks too
  399. * many applications when removed. It is unfortunate since
  400. * allowing applications to make a non-local bind solves
  401. * several problems with systems using dynamic addressing.
  402. * (ie. your servers still start up even if your ISDN link
  403. * is temporarily down)
  404. */
  405. err = -EADDRNOTAVAIL;
  406. if (!net->ipv4.sysctl_ip_nonlocal_bind &&
  407. !(inet->freebind || inet->transparent) &&
  408. addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
  409. chk_addr_ret != RTN_LOCAL &&
  410. chk_addr_ret != RTN_MULTICAST &&
  411. chk_addr_ret != RTN_BROADCAST)
  412. goto out;
  413. snum = ntohs(addr->sin_port);
  414. err = -EACCES;
  415. if (snum && snum < PROT_SOCK &&
  416. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  417. goto out;
  418. /* We keep a pair of addresses. rcv_saddr is the one
  419. * used by hash lookups, and saddr is used for transmit.
  420. *
  421. * In the BSD API these are the same except where it
  422. * would be illegal to use them (multicast/broadcast) in
  423. * which case the sending device address is used.
  424. */
  425. lock_sock(sk);
  426. /* Check these errors (active socket, double bind). */
  427. err = -EINVAL;
  428. if (sk->sk_state != TCP_CLOSE || inet->inet_num)
  429. goto out_release_sock;
  430. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  431. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  432. inet->inet_saddr = 0; /* Use device */
  433. /* Make sure we are allowed to bind here. */
  434. if ((snum || !inet->bind_address_no_port) &&
  435. sk->sk_prot->get_port(sk, snum)) {
  436. inet->inet_saddr = inet->inet_rcv_saddr = 0;
  437. err = -EADDRINUSE;
  438. goto out_release_sock;
  439. }
  440. if (inet->inet_rcv_saddr)
  441. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  442. if (snum)
  443. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  444. inet->inet_sport = htons(inet->inet_num);
  445. inet->inet_daddr = 0;
  446. inet->inet_dport = 0;
  447. sk_dst_reset(sk);
  448. err = 0;
  449. out_release_sock:
  450. release_sock(sk);
  451. out:
  452. return err;
  453. }
  454. EXPORT_SYMBOL(inet_bind);
  455. int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
  456. int addr_len, int flags)
  457. {
  458. struct sock *sk = sock->sk;
  459. if (addr_len < sizeof(uaddr->sa_family))
  460. return -EINVAL;
  461. if (uaddr->sa_family == AF_UNSPEC)
  462. return sk->sk_prot->disconnect(sk, flags);
  463. if (!inet_sk(sk)->inet_num && inet_autobind(sk))
  464. return -EAGAIN;
  465. return sk->sk_prot->connect(sk, uaddr, addr_len);
  466. }
  467. EXPORT_SYMBOL(inet_dgram_connect);
  468. static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
  469. {
  470. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  471. add_wait_queue(sk_sleep(sk), &wait);
  472. sk->sk_write_pending += writebias;
  473. /* Basic assumption: if someone sets sk->sk_err, he _must_
  474. * change state of the socket from TCP_SYN_*.
  475. * Connect() does not allow to get error notifications
  476. * without closing the socket.
  477. */
  478. while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  479. release_sock(sk);
  480. timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  481. lock_sock(sk);
  482. if (signal_pending(current) || !timeo)
  483. break;
  484. }
  485. remove_wait_queue(sk_sleep(sk), &wait);
  486. sk->sk_write_pending -= writebias;
  487. return timeo;
  488. }
  489. /*
  490. * Connect to a remote host. There is regrettably still a little
  491. * TCP 'magic' in here.
  492. */
  493. int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  494. int addr_len, int flags)
  495. {
  496. struct sock *sk = sock->sk;
  497. int err;
  498. long timeo;
  499. if (addr_len < sizeof(uaddr->sa_family))
  500. return -EINVAL;
  501. if (uaddr->sa_family == AF_UNSPEC) {
  502. err = sk->sk_prot->disconnect(sk, flags);
  503. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  504. goto out;
  505. }
  506. switch (sock->state) {
  507. default:
  508. err = -EINVAL;
  509. goto out;
  510. case SS_CONNECTED:
  511. err = -EISCONN;
  512. goto out;
  513. case SS_CONNECTING:
  514. err = -EALREADY;
  515. /* Fall out of switch with err, set for this state */
  516. break;
  517. case SS_UNCONNECTED:
  518. err = -EISCONN;
  519. if (sk->sk_state != TCP_CLOSE)
  520. goto out;
  521. err = sk->sk_prot->connect(sk, uaddr, addr_len);
  522. if (err < 0)
  523. goto out;
  524. sock->state = SS_CONNECTING;
  525. /* Just entered SS_CONNECTING state; the only
  526. * difference is that return value in non-blocking
  527. * case is EINPROGRESS, rather than EALREADY.
  528. */
  529. err = -EINPROGRESS;
  530. break;
  531. }
  532. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  533. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  534. int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
  535. tcp_sk(sk)->fastopen_req &&
  536. tcp_sk(sk)->fastopen_req->data ? 1 : 0;
  537. /* Error code is set above */
  538. if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
  539. goto out;
  540. err = sock_intr_errno(timeo);
  541. if (signal_pending(current))
  542. goto out;
  543. }
  544. /* Connection was closed by RST, timeout, ICMP error
  545. * or another process disconnected us.
  546. */
  547. if (sk->sk_state == TCP_CLOSE)
  548. goto sock_error;
  549. /* sk->sk_err may be not zero now, if RECVERR was ordered by user
  550. * and error was received after socket entered established state.
  551. * Hence, it is handled normally after connect() return successfully.
  552. */
  553. sock->state = SS_CONNECTED;
  554. err = 0;
  555. out:
  556. return err;
  557. sock_error:
  558. err = sock_error(sk) ? : -ECONNABORTED;
  559. sock->state = SS_UNCONNECTED;
  560. if (sk->sk_prot->disconnect(sk, flags))
  561. sock->state = SS_DISCONNECTING;
  562. goto out;
  563. }
  564. EXPORT_SYMBOL(__inet_stream_connect);
  565. int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  566. int addr_len, int flags)
  567. {
  568. int err;
  569. lock_sock(sock->sk);
  570. err = __inet_stream_connect(sock, uaddr, addr_len, flags);
  571. release_sock(sock->sk);
  572. return err;
  573. }
  574. EXPORT_SYMBOL(inet_stream_connect);
  575. /*
  576. * Accept a pending connection. The TCP layer now gives BSD semantics.
  577. */
  578. int inet_accept(struct socket *sock, struct socket *newsock, int flags)
  579. {
  580. struct sock *sk1 = sock->sk;
  581. int err = -EINVAL;
  582. struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
  583. if (!sk2)
  584. goto do_err;
  585. lock_sock(sk2);
  586. sock_rps_record_flow(sk2);
  587. WARN_ON(!((1 << sk2->sk_state) &
  588. (TCPF_ESTABLISHED | TCPF_SYN_RECV |
  589. TCPF_CLOSE_WAIT | TCPF_CLOSE)));
  590. sock_graft(sk2, newsock);
  591. newsock->state = SS_CONNECTED;
  592. err = 0;
  593. release_sock(sk2);
  594. do_err:
  595. return err;
  596. }
  597. EXPORT_SYMBOL(inet_accept);
  598. /*
  599. * This does both peername and sockname.
  600. */
  601. int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  602. int *uaddr_len, int peer)
  603. {
  604. struct sock *sk = sock->sk;
  605. struct inet_sock *inet = inet_sk(sk);
  606. DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
  607. sin->sin_family = AF_INET;
  608. if (peer) {
  609. if (!inet->inet_dport ||
  610. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  611. peer == 1))
  612. return -ENOTCONN;
  613. sin->sin_port = inet->inet_dport;
  614. sin->sin_addr.s_addr = inet->inet_daddr;
  615. } else {
  616. __be32 addr = inet->inet_rcv_saddr;
  617. if (!addr)
  618. addr = inet->inet_saddr;
  619. sin->sin_port = inet->inet_sport;
  620. sin->sin_addr.s_addr = addr;
  621. }
  622. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  623. *uaddr_len = sizeof(*sin);
  624. return 0;
  625. }
  626. EXPORT_SYMBOL(inet_getname);
  627. int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  628. {
  629. struct sock *sk = sock->sk;
  630. sock_rps_record_flow(sk);
  631. /* We may need to bind the socket. */
  632. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  633. inet_autobind(sk))
  634. return -EAGAIN;
  635. return sk->sk_prot->sendmsg(sk, msg, size);
  636. }
  637. EXPORT_SYMBOL(inet_sendmsg);
  638. ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
  639. size_t size, int flags)
  640. {
  641. struct sock *sk = sock->sk;
  642. sock_rps_record_flow(sk);
  643. /* We may need to bind the socket. */
  644. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  645. inet_autobind(sk))
  646. return -EAGAIN;
  647. if (sk->sk_prot->sendpage)
  648. return sk->sk_prot->sendpage(sk, page, offset, size, flags);
  649. return sock_no_sendpage(sock, page, offset, size, flags);
  650. }
  651. EXPORT_SYMBOL(inet_sendpage);
  652. int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  653. int flags)
  654. {
  655. struct sock *sk = sock->sk;
  656. int addr_len = 0;
  657. int err;
  658. sock_rps_record_flow(sk);
  659. err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
  660. flags & ~MSG_DONTWAIT, &addr_len);
  661. if (err >= 0)
  662. msg->msg_namelen = addr_len;
  663. return err;
  664. }
  665. EXPORT_SYMBOL(inet_recvmsg);
  666. int inet_shutdown(struct socket *sock, int how)
  667. {
  668. struct sock *sk = sock->sk;
  669. int err = 0;
  670. /* This should really check to make sure
  671. * the socket is a TCP socket. (WHY AC...)
  672. */
  673. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  674. 1->2 bit 2 snds.
  675. 2->3 */
  676. if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
  677. return -EINVAL;
  678. lock_sock(sk);
  679. if (sock->state == SS_CONNECTING) {
  680. if ((1 << sk->sk_state) &
  681. (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
  682. sock->state = SS_DISCONNECTING;
  683. else
  684. sock->state = SS_CONNECTED;
  685. }
  686. switch (sk->sk_state) {
  687. case TCP_CLOSE:
  688. err = -ENOTCONN;
  689. /* Hack to wake up other listeners, who can poll for
  690. POLLHUP, even on eg. unconnected UDP sockets -- RR */
  691. default:
  692. sk->sk_shutdown |= how;
  693. if (sk->sk_prot->shutdown)
  694. sk->sk_prot->shutdown(sk, how);
  695. break;
  696. /* Remaining two branches are temporary solution for missing
  697. * close() in multithreaded environment. It is _not_ a good idea,
  698. * but we have no choice until close() is repaired at VFS level.
  699. */
  700. case TCP_LISTEN:
  701. if (!(how & RCV_SHUTDOWN))
  702. break;
  703. /* Fall through */
  704. case TCP_SYN_SENT:
  705. err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
  706. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  707. break;
  708. }
  709. /* Wake up anyone sleeping in poll. */
  710. sk->sk_state_change(sk);
  711. release_sock(sk);
  712. return err;
  713. }
  714. EXPORT_SYMBOL(inet_shutdown);
  715. /*
  716. * ioctl() calls you can issue on an INET socket. Most of these are
  717. * device configuration and stuff and very rarely used. Some ioctls
  718. * pass on to the socket itself.
  719. *
  720. * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  721. * loads the devconfigure module does its configuring and unloads it.
  722. * There's a good 20K of config code hanging around the kernel.
  723. */
  724. int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  725. {
  726. struct sock *sk = sock->sk;
  727. int err = 0;
  728. struct net *net = sock_net(sk);
  729. switch (cmd) {
  730. case SIOCGSTAMP:
  731. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  732. break;
  733. case SIOCGSTAMPNS:
  734. err = sock_get_timestampns(sk, (struct timespec __user *)arg);
  735. break;
  736. case SIOCADDRT:
  737. case SIOCDELRT:
  738. case SIOCRTMSG:
  739. err = ip_rt_ioctl(net, cmd, (void __user *)arg);
  740. break;
  741. case SIOCDARP:
  742. case SIOCGARP:
  743. case SIOCSARP:
  744. err = arp_ioctl(net, cmd, (void __user *)arg);
  745. break;
  746. case SIOCGIFADDR:
  747. case SIOCSIFADDR:
  748. case SIOCGIFBRDADDR:
  749. case SIOCSIFBRDADDR:
  750. case SIOCGIFNETMASK:
  751. case SIOCSIFNETMASK:
  752. case SIOCGIFDSTADDR:
  753. case SIOCSIFDSTADDR:
  754. case SIOCSIFPFLAGS:
  755. case SIOCGIFPFLAGS:
  756. case SIOCSIFFLAGS:
  757. err = devinet_ioctl(net, cmd, (void __user *)arg);
  758. break;
  759. default:
  760. if (sk->sk_prot->ioctl)
  761. err = sk->sk_prot->ioctl(sk, cmd, arg);
  762. else
  763. err = -ENOIOCTLCMD;
  764. break;
  765. }
  766. return err;
  767. }
  768. EXPORT_SYMBOL(inet_ioctl);
  769. #ifdef CONFIG_COMPAT
  770. static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  771. {
  772. struct sock *sk = sock->sk;
  773. int err = -ENOIOCTLCMD;
  774. if (sk->sk_prot->compat_ioctl)
  775. err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
  776. return err;
  777. }
  778. #endif
  779. const struct proto_ops inet_stream_ops = {
  780. .family = PF_INET,
  781. .owner = THIS_MODULE,
  782. .release = inet_release,
  783. .bind = inet_bind,
  784. .connect = inet_stream_connect,
  785. .socketpair = sock_no_socketpair,
  786. .accept = inet_accept,
  787. .getname = inet_getname,
  788. .poll = tcp_poll,
  789. .ioctl = inet_ioctl,
  790. .listen = inet_listen,
  791. .shutdown = inet_shutdown,
  792. .setsockopt = sock_common_setsockopt,
  793. .getsockopt = sock_common_getsockopt,
  794. .sendmsg = inet_sendmsg,
  795. .recvmsg = inet_recvmsg,
  796. .mmap = sock_no_mmap,
  797. .sendpage = inet_sendpage,
  798. .splice_read = tcp_splice_read,
  799. .read_sock = tcp_read_sock,
  800. .peek_len = tcp_peek_len,
  801. #ifdef CONFIG_COMPAT
  802. .compat_setsockopt = compat_sock_common_setsockopt,
  803. .compat_getsockopt = compat_sock_common_getsockopt,
  804. .compat_ioctl = inet_compat_ioctl,
  805. #endif
  806. };
  807. EXPORT_SYMBOL(inet_stream_ops);
  808. const struct proto_ops inet_dgram_ops = {
  809. .family = PF_INET,
  810. .owner = THIS_MODULE,
  811. .release = inet_release,
  812. .bind = inet_bind,
  813. .connect = inet_dgram_connect,
  814. .socketpair = sock_no_socketpair,
  815. .accept = sock_no_accept,
  816. .getname = inet_getname,
  817. .poll = udp_poll,
  818. .ioctl = inet_ioctl,
  819. .listen = sock_no_listen,
  820. .shutdown = inet_shutdown,
  821. .setsockopt = sock_common_setsockopt,
  822. .getsockopt = sock_common_getsockopt,
  823. .sendmsg = inet_sendmsg,
  824. .recvmsg = inet_recvmsg,
  825. .mmap = sock_no_mmap,
  826. .sendpage = inet_sendpage,
  827. .set_peek_off = sk_set_peek_off,
  828. #ifdef CONFIG_COMPAT
  829. .compat_setsockopt = compat_sock_common_setsockopt,
  830. .compat_getsockopt = compat_sock_common_getsockopt,
  831. .compat_ioctl = inet_compat_ioctl,
  832. #endif
  833. };
  834. EXPORT_SYMBOL(inet_dgram_ops);
  835. /*
  836. * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
  837. * udp_poll
  838. */
  839. static const struct proto_ops inet_sockraw_ops = {
  840. .family = PF_INET,
  841. .owner = THIS_MODULE,
  842. .release = inet_release,
  843. .bind = inet_bind,
  844. .connect = inet_dgram_connect,
  845. .socketpair = sock_no_socketpair,
  846. .accept = sock_no_accept,
  847. .getname = inet_getname,
  848. .poll = datagram_poll,
  849. .ioctl = inet_ioctl,
  850. .listen = sock_no_listen,
  851. .shutdown = inet_shutdown,
  852. .setsockopt = sock_common_setsockopt,
  853. .getsockopt = sock_common_getsockopt,
  854. .sendmsg = inet_sendmsg,
  855. .recvmsg = inet_recvmsg,
  856. .mmap = sock_no_mmap,
  857. .sendpage = inet_sendpage,
  858. #ifdef CONFIG_COMPAT
  859. .compat_setsockopt = compat_sock_common_setsockopt,
  860. .compat_getsockopt = compat_sock_common_getsockopt,
  861. .compat_ioctl = inet_compat_ioctl,
  862. #endif
  863. };
  864. static const struct net_proto_family inet_family_ops = {
  865. .family = PF_INET,
  866. .create = inet_create,
  867. .owner = THIS_MODULE,
  868. };
  869. /* Upon startup we insert all the elements in inetsw_array[] into
  870. * the linked list inetsw.
  871. */
  872. static struct inet_protosw inetsw_array[] =
  873. {
  874. {
  875. .type = SOCK_STREAM,
  876. .protocol = IPPROTO_TCP,
  877. .prot = &tcp_prot,
  878. .ops = &inet_stream_ops,
  879. .flags = INET_PROTOSW_PERMANENT |
  880. INET_PROTOSW_ICSK,
  881. },
  882. {
  883. .type = SOCK_DGRAM,
  884. .protocol = IPPROTO_UDP,
  885. .prot = &udp_prot,
  886. .ops = &inet_dgram_ops,
  887. .flags = INET_PROTOSW_PERMANENT,
  888. },
  889. {
  890. .type = SOCK_DGRAM,
  891. .protocol = IPPROTO_ICMP,
  892. .prot = &ping_prot,
  893. .ops = &inet_sockraw_ops,
  894. .flags = INET_PROTOSW_REUSE,
  895. },
  896. {
  897. .type = SOCK_RAW,
  898. .protocol = IPPROTO_IP, /* wild card */
  899. .prot = &raw_prot,
  900. .ops = &inet_sockraw_ops,
  901. .flags = INET_PROTOSW_REUSE,
  902. }
  903. };
  904. #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
  905. void inet_register_protosw(struct inet_protosw *p)
  906. {
  907. struct list_head *lh;
  908. struct inet_protosw *answer;
  909. int protocol = p->protocol;
  910. struct list_head *last_perm;
  911. spin_lock_bh(&inetsw_lock);
  912. if (p->type >= SOCK_MAX)
  913. goto out_illegal;
  914. /* If we are trying to override a permanent protocol, bail. */
  915. last_perm = &inetsw[p->type];
  916. list_for_each(lh, &inetsw[p->type]) {
  917. answer = list_entry(lh, struct inet_protosw, list);
  918. /* Check only the non-wild match. */
  919. if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
  920. break;
  921. if (protocol == answer->protocol)
  922. goto out_permanent;
  923. last_perm = lh;
  924. }
  925. /* Add the new entry after the last permanent entry if any, so that
  926. * the new entry does not override a permanent entry when matched with
  927. * a wild-card protocol. But it is allowed to override any existing
  928. * non-permanent entry. This means that when we remove this entry, the
  929. * system automatically returns to the old behavior.
  930. */
  931. list_add_rcu(&p->list, last_perm);
  932. out:
  933. spin_unlock_bh(&inetsw_lock);
  934. return;
  935. out_permanent:
  936. pr_err("Attempt to override permanent protocol %d\n", protocol);
  937. goto out;
  938. out_illegal:
  939. pr_err("Ignoring attempt to register invalid socket type %d\n",
  940. p->type);
  941. goto out;
  942. }
  943. EXPORT_SYMBOL(inet_register_protosw);
  944. void inet_unregister_protosw(struct inet_protosw *p)
  945. {
  946. if (INET_PROTOSW_PERMANENT & p->flags) {
  947. pr_err("Attempt to unregister permanent protocol %d\n",
  948. p->protocol);
  949. } else {
  950. spin_lock_bh(&inetsw_lock);
  951. list_del_rcu(&p->list);
  952. spin_unlock_bh(&inetsw_lock);
  953. synchronize_net();
  954. }
  955. }
  956. EXPORT_SYMBOL(inet_unregister_protosw);
  957. static int inet_sk_reselect_saddr(struct sock *sk)
  958. {
  959. struct inet_sock *inet = inet_sk(sk);
  960. __be32 old_saddr = inet->inet_saddr;
  961. __be32 daddr = inet->inet_daddr;
  962. struct flowi4 *fl4;
  963. struct rtable *rt;
  964. __be32 new_saddr;
  965. struct ip_options_rcu *inet_opt;
  966. inet_opt = rcu_dereference_protected(inet->inet_opt,
  967. lockdep_sock_is_held(sk));
  968. if (inet_opt && inet_opt->opt.srr)
  969. daddr = inet_opt->opt.faddr;
  970. /* Query new route. */
  971. fl4 = &inet->cork.fl.u.ip4;
  972. rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
  973. sk->sk_bound_dev_if, sk->sk_protocol,
  974. inet->inet_sport, inet->inet_dport, sk);
  975. if (IS_ERR(rt))
  976. return PTR_ERR(rt);
  977. sk_setup_caps(sk, &rt->dst);
  978. new_saddr = fl4->saddr;
  979. if (new_saddr == old_saddr)
  980. return 0;
  981. if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) {
  982. pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
  983. __func__, &old_saddr, &new_saddr);
  984. }
  985. inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
  986. /*
  987. * XXX The only one ugly spot where we need to
  988. * XXX really change the sockets identity after
  989. * XXX it has entered the hashes. -DaveM
  990. *
  991. * Besides that, it does not check for connection
  992. * uniqueness. Wait for troubles.
  993. */
  994. return __sk_prot_rehash(sk);
  995. }
  996. int inet_sk_rebuild_header(struct sock *sk)
  997. {
  998. struct inet_sock *inet = inet_sk(sk);
  999. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  1000. __be32 daddr;
  1001. struct ip_options_rcu *inet_opt;
  1002. struct flowi4 *fl4;
  1003. int err;
  1004. /* Route is OK, nothing to do. */
  1005. if (rt)
  1006. return 0;
  1007. /* Reroute. */
  1008. rcu_read_lock();
  1009. inet_opt = rcu_dereference(inet->inet_opt);
  1010. daddr = inet->inet_daddr;
  1011. if (inet_opt && inet_opt->opt.srr)
  1012. daddr = inet_opt->opt.faddr;
  1013. rcu_read_unlock();
  1014. fl4 = &inet->cork.fl.u.ip4;
  1015. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
  1016. inet->inet_dport, inet->inet_sport,
  1017. sk->sk_protocol, RT_CONN_FLAGS(sk),
  1018. sk->sk_bound_dev_if);
  1019. if (!IS_ERR(rt)) {
  1020. err = 0;
  1021. sk_setup_caps(sk, &rt->dst);
  1022. } else {
  1023. err = PTR_ERR(rt);
  1024. /* Routing failed... */
  1025. sk->sk_route_caps = 0;
  1026. /*
  1027. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  1028. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  1029. */
  1030. if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr ||
  1031. sk->sk_state != TCP_SYN_SENT ||
  1032. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  1033. (err = inet_sk_reselect_saddr(sk)) != 0)
  1034. sk->sk_err_soft = -err;
  1035. }
  1036. return err;
  1037. }
  1038. EXPORT_SYMBOL(inet_sk_rebuild_header);
  1039. struct sk_buff *inet_gso_segment(struct sk_buff *skb,
  1040. netdev_features_t features)
  1041. {
  1042. bool udpfrag = false, fixedid = false, gso_partial, encap;
  1043. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1044. const struct net_offload *ops;
  1045. unsigned int offset = 0;
  1046. struct iphdr *iph;
  1047. int proto, tot_len;
  1048. int nhoff;
  1049. int ihl;
  1050. int id;
  1051. skb_reset_network_header(skb);
  1052. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  1053. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1054. goto out;
  1055. iph = ip_hdr(skb);
  1056. ihl = iph->ihl * 4;
  1057. if (ihl < sizeof(*iph))
  1058. goto out;
  1059. id = ntohs(iph->id);
  1060. proto = iph->protocol;
  1061. /* Warning: after this point, iph might be no longer valid */
  1062. if (unlikely(!pskb_may_pull(skb, ihl)))
  1063. goto out;
  1064. __skb_pull(skb, ihl);
  1065. encap = SKB_GSO_CB(skb)->encap_level > 0;
  1066. if (encap)
  1067. features &= skb->dev->hw_enc_features;
  1068. SKB_GSO_CB(skb)->encap_level += ihl;
  1069. skb_reset_transport_header(skb);
  1070. segs = ERR_PTR(-EPROTONOSUPPORT);
  1071. if (!skb->encapsulation || encap) {
  1072. udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  1073. fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
  1074. /* fixed ID is invalid if DF bit is not set */
  1075. if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF)))
  1076. goto out;
  1077. }
  1078. ops = rcu_dereference(inet_offloads[proto]);
  1079. if (likely(ops && ops->callbacks.gso_segment))
  1080. segs = ops->callbacks.gso_segment(skb, features);
  1081. if (IS_ERR_OR_NULL(segs))
  1082. goto out;
  1083. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  1084. skb = segs;
  1085. do {
  1086. iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
  1087. if (udpfrag) {
  1088. iph->frag_off = htons(offset >> 3);
  1089. if (skb->next)
  1090. iph->frag_off |= htons(IP_MF);
  1091. offset += skb->len - nhoff - ihl;
  1092. tot_len = skb->len - nhoff;
  1093. } else if (skb_is_gso(skb)) {
  1094. if (!fixedid) {
  1095. iph->id = htons(id);
  1096. id += skb_shinfo(skb)->gso_segs;
  1097. }
  1098. if (gso_partial)
  1099. tot_len = skb_shinfo(skb)->gso_size +
  1100. SKB_GSO_CB(skb)->data_offset +
  1101. skb->head - (unsigned char *)iph;
  1102. else
  1103. tot_len = skb->len - nhoff;
  1104. } else {
  1105. if (!fixedid)
  1106. iph->id = htons(id++);
  1107. tot_len = skb->len - nhoff;
  1108. }
  1109. iph->tot_len = htons(tot_len);
  1110. ip_send_check(iph);
  1111. if (encap)
  1112. skb_reset_inner_headers(skb);
  1113. skb->network_header = (u8 *)iph - skb->head;
  1114. } while ((skb = skb->next));
  1115. out:
  1116. return segs;
  1117. }
  1118. EXPORT_SYMBOL(inet_gso_segment);
  1119. struct sk_buff **inet_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  1120. {
  1121. const struct net_offload *ops;
  1122. struct sk_buff **pp = NULL;
  1123. struct sk_buff *p;
  1124. const struct iphdr *iph;
  1125. unsigned int hlen;
  1126. unsigned int off;
  1127. unsigned int id;
  1128. int flush = 1;
  1129. int proto;
  1130. off = skb_gro_offset(skb);
  1131. hlen = off + sizeof(*iph);
  1132. iph = skb_gro_header_fast(skb, off);
  1133. if (skb_gro_header_hard(skb, hlen)) {
  1134. iph = skb_gro_header_slow(skb, hlen, off);
  1135. if (unlikely(!iph))
  1136. goto out;
  1137. }
  1138. proto = iph->protocol;
  1139. rcu_read_lock();
  1140. ops = rcu_dereference(inet_offloads[proto]);
  1141. if (!ops || !ops->callbacks.gro_receive)
  1142. goto out_unlock;
  1143. if (*(u8 *)iph != 0x45)
  1144. goto out_unlock;
  1145. if (unlikely(ip_fast_csum((u8 *)iph, 5)))
  1146. goto out_unlock;
  1147. id = ntohl(*(__be32 *)&iph->id);
  1148. flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  1149. id >>= 16;
  1150. for (p = *head; p; p = p->next) {
  1151. struct iphdr *iph2;
  1152. u16 flush_id;
  1153. if (!NAPI_GRO_CB(p)->same_flow)
  1154. continue;
  1155. iph2 = (struct iphdr *)(p->data + off);
  1156. /* The above works because, with the exception of the top
  1157. * (inner most) layer, we only aggregate pkts with the same
  1158. * hdr length so all the hdrs we'll need to verify will start
  1159. * at the same offset.
  1160. */
  1161. if ((iph->protocol ^ iph2->protocol) |
  1162. ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
  1163. ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
  1164. NAPI_GRO_CB(p)->same_flow = 0;
  1165. continue;
  1166. }
  1167. /* All fields must match except length and checksum. */
  1168. NAPI_GRO_CB(p)->flush |=
  1169. (iph->ttl ^ iph2->ttl) |
  1170. (iph->tos ^ iph2->tos) |
  1171. ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
  1172. NAPI_GRO_CB(p)->flush |= flush;
  1173. /* We need to store of the IP ID check to be included later
  1174. * when we can verify that this packet does in fact belong
  1175. * to a given flow.
  1176. */
  1177. flush_id = (u16)(id - ntohs(iph2->id));
  1178. /* This bit of code makes it much easier for us to identify
  1179. * the cases where we are doing atomic vs non-atomic IP ID
  1180. * checks. Specifically an atomic check can return IP ID
  1181. * values 0 - 0xFFFF, while a non-atomic check can only
  1182. * return 0 or 0xFFFF.
  1183. */
  1184. if (!NAPI_GRO_CB(p)->is_atomic ||
  1185. !(iph->frag_off & htons(IP_DF))) {
  1186. flush_id ^= NAPI_GRO_CB(p)->count;
  1187. flush_id = flush_id ? 0xFFFF : 0;
  1188. }
  1189. /* If the previous IP ID value was based on an atomic
  1190. * datagram we can overwrite the value and ignore it.
  1191. */
  1192. if (NAPI_GRO_CB(skb)->is_atomic)
  1193. NAPI_GRO_CB(p)->flush_id = flush_id;
  1194. else
  1195. NAPI_GRO_CB(p)->flush_id |= flush_id;
  1196. }
  1197. NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF));
  1198. NAPI_GRO_CB(skb)->flush |= flush;
  1199. skb_set_network_header(skb, off);
  1200. /* The above will be needed by the transport layer if there is one
  1201. * immediately following this IP hdr.
  1202. */
  1203. /* Note : No need to call skb_gro_postpull_rcsum() here,
  1204. * as we already checked checksum over ipv4 header was 0
  1205. */
  1206. skb_gro_pull(skb, sizeof(*iph));
  1207. skb_set_transport_header(skb, skb_gro_offset(skb));
  1208. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  1209. out_unlock:
  1210. rcu_read_unlock();
  1211. out:
  1212. NAPI_GRO_CB(skb)->flush |= flush;
  1213. return pp;
  1214. }
  1215. EXPORT_SYMBOL(inet_gro_receive);
  1216. static struct sk_buff **ipip_gro_receive(struct sk_buff **head,
  1217. struct sk_buff *skb)
  1218. {
  1219. if (NAPI_GRO_CB(skb)->encap_mark) {
  1220. NAPI_GRO_CB(skb)->flush = 1;
  1221. return NULL;
  1222. }
  1223. NAPI_GRO_CB(skb)->encap_mark = 1;
  1224. return inet_gro_receive(head, skb);
  1225. }
  1226. #define SECONDS_PER_DAY 86400
  1227. /* inet_current_timestamp - Return IP network timestamp
  1228. *
  1229. * Return milliseconds since midnight in network byte order.
  1230. */
  1231. __be32 inet_current_timestamp(void)
  1232. {
  1233. u32 secs;
  1234. u32 msecs;
  1235. struct timespec64 ts;
  1236. ktime_get_real_ts64(&ts);
  1237. /* Get secs since midnight. */
  1238. (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
  1239. /* Convert to msecs. */
  1240. msecs = secs * MSEC_PER_SEC;
  1241. /* Convert nsec to msec. */
  1242. msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
  1243. /* Convert to network byte order. */
  1244. return htonl(msecs);
  1245. }
  1246. EXPORT_SYMBOL(inet_current_timestamp);
  1247. int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
  1248. {
  1249. if (sk->sk_family == AF_INET)
  1250. return ip_recv_error(sk, msg, len, addr_len);
  1251. #if IS_ENABLED(CONFIG_IPV6)
  1252. if (sk->sk_family == AF_INET6)
  1253. return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
  1254. #endif
  1255. return -EINVAL;
  1256. }
  1257. int inet_gro_complete(struct sk_buff *skb, int nhoff)
  1258. {
  1259. __be16 newlen = htons(skb->len - nhoff);
  1260. struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
  1261. const struct net_offload *ops;
  1262. int proto = iph->protocol;
  1263. int err = -ENOSYS;
  1264. if (skb->encapsulation) {
  1265. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
  1266. skb_set_inner_network_header(skb, nhoff);
  1267. }
  1268. csum_replace2(&iph->check, iph->tot_len, newlen);
  1269. iph->tot_len = newlen;
  1270. rcu_read_lock();
  1271. ops = rcu_dereference(inet_offloads[proto]);
  1272. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  1273. goto out_unlock;
  1274. /* Only need to add sizeof(*iph) to get to the next hdr below
  1275. * because any hdr with option will have been flushed in
  1276. * inet_gro_receive().
  1277. */
  1278. err = ops->callbacks.gro_complete(skb, nhoff + sizeof(*iph));
  1279. out_unlock:
  1280. rcu_read_unlock();
  1281. return err;
  1282. }
  1283. EXPORT_SYMBOL(inet_gro_complete);
  1284. static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
  1285. {
  1286. skb->encapsulation = 1;
  1287. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  1288. return inet_gro_complete(skb, nhoff);
  1289. }
  1290. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1291. unsigned short type, unsigned char protocol,
  1292. struct net *net)
  1293. {
  1294. struct socket *sock;
  1295. int rc = sock_create_kern(net, family, type, protocol, &sock);
  1296. if (rc == 0) {
  1297. *sk = sock->sk;
  1298. (*sk)->sk_allocation = GFP_ATOMIC;
  1299. /*
  1300. * Unhash it so that IP input processing does not even see it,
  1301. * we do not wish this socket to see incoming packets.
  1302. */
  1303. (*sk)->sk_prot->unhash(*sk);
  1304. }
  1305. return rc;
  1306. }
  1307. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1308. u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
  1309. {
  1310. return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
  1311. }
  1312. EXPORT_SYMBOL_GPL(snmp_get_cpu_field);
  1313. unsigned long snmp_fold_field(void __percpu *mib, int offt)
  1314. {
  1315. unsigned long res = 0;
  1316. int i;
  1317. for_each_possible_cpu(i)
  1318. res += snmp_get_cpu_field(mib, i, offt);
  1319. return res;
  1320. }
  1321. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1322. #if BITS_PER_LONG==32
  1323. u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
  1324. size_t syncp_offset)
  1325. {
  1326. void *bhptr;
  1327. struct u64_stats_sync *syncp;
  1328. u64 v;
  1329. unsigned int start;
  1330. bhptr = per_cpu_ptr(mib, cpu);
  1331. syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
  1332. do {
  1333. start = u64_stats_fetch_begin_irq(syncp);
  1334. v = *(((u64 *)bhptr) + offt);
  1335. } while (u64_stats_fetch_retry_irq(syncp, start));
  1336. return v;
  1337. }
  1338. EXPORT_SYMBOL_GPL(snmp_get_cpu_field64);
  1339. u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
  1340. {
  1341. u64 res = 0;
  1342. int cpu;
  1343. for_each_possible_cpu(cpu) {
  1344. res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
  1345. }
  1346. return res;
  1347. }
  1348. EXPORT_SYMBOL_GPL(snmp_fold_field64);
  1349. #endif
  1350. #ifdef CONFIG_IP_MULTICAST
  1351. static const struct net_protocol igmp_protocol = {
  1352. .handler = igmp_rcv,
  1353. .netns_ok = 1,
  1354. };
  1355. #endif
  1356. static const struct net_protocol tcp_protocol = {
  1357. .early_demux = tcp_v4_early_demux,
  1358. .handler = tcp_v4_rcv,
  1359. .err_handler = tcp_v4_err,
  1360. .no_policy = 1,
  1361. .netns_ok = 1,
  1362. .icmp_strict_tag_validation = 1,
  1363. };
  1364. static const struct net_protocol udp_protocol = {
  1365. .early_demux = udp_v4_early_demux,
  1366. .handler = udp_rcv,
  1367. .err_handler = udp_err,
  1368. .no_policy = 1,
  1369. .netns_ok = 1,
  1370. };
  1371. static const struct net_protocol icmp_protocol = {
  1372. .handler = icmp_rcv,
  1373. .err_handler = icmp_err,
  1374. .no_policy = 1,
  1375. .netns_ok = 1,
  1376. };
  1377. static __net_init int ipv4_mib_init_net(struct net *net)
  1378. {
  1379. int i;
  1380. net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
  1381. if (!net->mib.tcp_statistics)
  1382. goto err_tcp_mib;
  1383. net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
  1384. if (!net->mib.ip_statistics)
  1385. goto err_ip_mib;
  1386. for_each_possible_cpu(i) {
  1387. struct ipstats_mib *af_inet_stats;
  1388. af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
  1389. u64_stats_init(&af_inet_stats->syncp);
  1390. }
  1391. net->mib.net_statistics = alloc_percpu(struct linux_mib);
  1392. if (!net->mib.net_statistics)
  1393. goto err_net_mib;
  1394. net->mib.udp_statistics = alloc_percpu(struct udp_mib);
  1395. if (!net->mib.udp_statistics)
  1396. goto err_udp_mib;
  1397. net->mib.udplite_statistics = alloc_percpu(struct udp_mib);
  1398. if (!net->mib.udplite_statistics)
  1399. goto err_udplite_mib;
  1400. net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
  1401. if (!net->mib.icmp_statistics)
  1402. goto err_icmp_mib;
  1403. net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
  1404. GFP_KERNEL);
  1405. if (!net->mib.icmpmsg_statistics)
  1406. goto err_icmpmsg_mib;
  1407. tcp_mib_init(net);
  1408. return 0;
  1409. err_icmpmsg_mib:
  1410. free_percpu(net->mib.icmp_statistics);
  1411. err_icmp_mib:
  1412. free_percpu(net->mib.udplite_statistics);
  1413. err_udplite_mib:
  1414. free_percpu(net->mib.udp_statistics);
  1415. err_udp_mib:
  1416. free_percpu(net->mib.net_statistics);
  1417. err_net_mib:
  1418. free_percpu(net->mib.ip_statistics);
  1419. err_ip_mib:
  1420. free_percpu(net->mib.tcp_statistics);
  1421. err_tcp_mib:
  1422. return -ENOMEM;
  1423. }
  1424. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1425. {
  1426. kfree(net->mib.icmpmsg_statistics);
  1427. free_percpu(net->mib.icmp_statistics);
  1428. free_percpu(net->mib.udplite_statistics);
  1429. free_percpu(net->mib.udp_statistics);
  1430. free_percpu(net->mib.net_statistics);
  1431. free_percpu(net->mib.ip_statistics);
  1432. free_percpu(net->mib.tcp_statistics);
  1433. }
  1434. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1435. .init = ipv4_mib_init_net,
  1436. .exit = ipv4_mib_exit_net,
  1437. };
  1438. static int __init init_ipv4_mibs(void)
  1439. {
  1440. return register_pernet_subsys(&ipv4_mib_ops);
  1441. }
  1442. static __net_init int inet_init_net(struct net *net)
  1443. {
  1444. /*
  1445. * Set defaults for local port range
  1446. */
  1447. seqlock_init(&net->ipv4.ip_local_ports.lock);
  1448. net->ipv4.ip_local_ports.range[0] = 32768;
  1449. net->ipv4.ip_local_ports.range[1] = 60999;
  1450. seqlock_init(&net->ipv4.ping_group_range.lock);
  1451. /*
  1452. * Sane defaults - nobody may create ping sockets.
  1453. * Boot scripts should set this to distro-specific group.
  1454. */
  1455. net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
  1456. net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
  1457. /* Default values for sysctl-controlled parameters.
  1458. * We set them here, in case sysctl is not compiled.
  1459. */
  1460. net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
  1461. net->ipv4.sysctl_ip_dynaddr = 0;
  1462. net->ipv4.sysctl_ip_early_demux = 1;
  1463. /* Some igmp sysctl, whose values are always used */
  1464. net->ipv4.sysctl_igmp_max_memberships = 20;
  1465. net->ipv4.sysctl_igmp_max_msf = 10;
  1466. /* IGMP reports for link-local multicast groups are enabled by default */
  1467. net->ipv4.sysctl_igmp_llm_reports = 1;
  1468. net->ipv4.sysctl_igmp_qrv = 2;
  1469. return 0;
  1470. }
  1471. static __net_exit void inet_exit_net(struct net *net)
  1472. {
  1473. }
  1474. static __net_initdata struct pernet_operations af_inet_ops = {
  1475. .init = inet_init_net,
  1476. .exit = inet_exit_net,
  1477. };
  1478. static int __init init_inet_pernet_ops(void)
  1479. {
  1480. return register_pernet_subsys(&af_inet_ops);
  1481. }
  1482. static int ipv4_proc_init(void);
  1483. /*
  1484. * IP protocol layer initialiser
  1485. */
  1486. static struct packet_offload ip_packet_offload __read_mostly = {
  1487. .type = cpu_to_be16(ETH_P_IP),
  1488. .callbacks = {
  1489. .gso_segment = inet_gso_segment,
  1490. .gro_receive = inet_gro_receive,
  1491. .gro_complete = inet_gro_complete,
  1492. },
  1493. };
  1494. static const struct net_offload ipip_offload = {
  1495. .callbacks = {
  1496. .gso_segment = inet_gso_segment,
  1497. .gro_receive = ipip_gro_receive,
  1498. .gro_complete = ipip_gro_complete,
  1499. },
  1500. };
  1501. static int __init ipv4_offload_init(void)
  1502. {
  1503. /*
  1504. * Add offloads
  1505. */
  1506. if (udpv4_offload_init() < 0)
  1507. pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
  1508. if (tcpv4_offload_init() < 0)
  1509. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  1510. dev_add_offload(&ip_packet_offload);
  1511. inet_add_offload(&ipip_offload, IPPROTO_IPIP);
  1512. return 0;
  1513. }
  1514. fs_initcall(ipv4_offload_init);
  1515. static struct packet_type ip_packet_type __read_mostly = {
  1516. .type = cpu_to_be16(ETH_P_IP),
  1517. .func = ip_rcv,
  1518. };
  1519. static int __init inet_init(void)
  1520. {
  1521. struct inet_protosw *q;
  1522. struct list_head *r;
  1523. int rc = -EINVAL;
  1524. sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
  1525. rc = proto_register(&tcp_prot, 1);
  1526. if (rc)
  1527. goto out;
  1528. rc = proto_register(&udp_prot, 1);
  1529. if (rc)
  1530. goto out_unregister_tcp_proto;
  1531. rc = proto_register(&raw_prot, 1);
  1532. if (rc)
  1533. goto out_unregister_udp_proto;
  1534. rc = proto_register(&ping_prot, 1);
  1535. if (rc)
  1536. goto out_unregister_raw_proto;
  1537. /*
  1538. * Tell SOCKET that we are alive...
  1539. */
  1540. (void)sock_register(&inet_family_ops);
  1541. #ifdef CONFIG_SYSCTL
  1542. ip_static_sysctl_init();
  1543. #endif
  1544. /*
  1545. * Add all the base protocols.
  1546. */
  1547. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1548. pr_crit("%s: Cannot add ICMP protocol\n", __func__);
  1549. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1550. pr_crit("%s: Cannot add UDP protocol\n", __func__);
  1551. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1552. pr_crit("%s: Cannot add TCP protocol\n", __func__);
  1553. #ifdef CONFIG_IP_MULTICAST
  1554. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1555. pr_crit("%s: Cannot add IGMP protocol\n", __func__);
  1556. #endif
  1557. /* Register the socket-side information for inet_create. */
  1558. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1559. INIT_LIST_HEAD(r);
  1560. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1561. inet_register_protosw(q);
  1562. /*
  1563. * Set the ARP module up
  1564. */
  1565. arp_init();
  1566. /*
  1567. * Set the IP module up
  1568. */
  1569. ip_init();
  1570. tcp_v4_init();
  1571. /* Setup TCP slab cache for open requests. */
  1572. tcp_init();
  1573. /* Setup UDP memory threshold */
  1574. udp_init();
  1575. /* Add UDP-Lite (RFC 3828) */
  1576. udplite4_register();
  1577. ping_init();
  1578. /*
  1579. * Set the ICMP layer up
  1580. */
  1581. if (icmp_init() < 0)
  1582. panic("Failed to create the ICMP control socket.\n");
  1583. /*
  1584. * Initialise the multicast router
  1585. */
  1586. #if defined(CONFIG_IP_MROUTE)
  1587. if (ip_mr_init())
  1588. pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
  1589. #endif
  1590. if (init_inet_pernet_ops())
  1591. pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
  1592. /*
  1593. * Initialise per-cpu ipv4 mibs
  1594. */
  1595. if (init_ipv4_mibs())
  1596. pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
  1597. ipv4_proc_init();
  1598. ipfrag_init();
  1599. dev_add_pack(&ip_packet_type);
  1600. ip_tunnel_core_init();
  1601. rc = 0;
  1602. out:
  1603. return rc;
  1604. out_unregister_raw_proto:
  1605. proto_unregister(&raw_prot);
  1606. out_unregister_udp_proto:
  1607. proto_unregister(&udp_prot);
  1608. out_unregister_tcp_proto:
  1609. proto_unregister(&tcp_prot);
  1610. goto out;
  1611. }
  1612. fs_initcall(inet_init);
  1613. /* ------------------------------------------------------------------------ */
  1614. #ifdef CONFIG_PROC_FS
  1615. static int __init ipv4_proc_init(void)
  1616. {
  1617. int rc = 0;
  1618. if (raw_proc_init())
  1619. goto out_raw;
  1620. if (tcp4_proc_init())
  1621. goto out_tcp;
  1622. if (udp4_proc_init())
  1623. goto out_udp;
  1624. if (ping_proc_init())
  1625. goto out_ping;
  1626. if (ip_misc_proc_init())
  1627. goto out_misc;
  1628. out:
  1629. return rc;
  1630. out_misc:
  1631. ping_proc_exit();
  1632. out_ping:
  1633. udp4_proc_exit();
  1634. out_udp:
  1635. tcp4_proc_exit();
  1636. out_tcp:
  1637. raw_proc_exit();
  1638. out_raw:
  1639. rc = -ENOMEM;
  1640. goto out;
  1641. }
  1642. #else /* CONFIG_PROC_FS */
  1643. static int __init ipv4_proc_init(void)
  1644. {
  1645. return 0;
  1646. }
  1647. #endif /* CONFIG_PROC_FS */