af_inet.c 49 KB

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