1
0

af_inet.c 50 KB

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