sock.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  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. * Generic socket support routines. Memory allocators, socket lock/release
  7. * handler for protocols to use and generic option handler.
  8. *
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Alan Cox, <A.Cox@swansea.ac.uk>
  14. *
  15. * Fixes:
  16. * Alan Cox : Numerous verify_area() problems
  17. * Alan Cox : Connecting on a connecting socket
  18. * now returns an error for tcp.
  19. * Alan Cox : sock->protocol is set correctly.
  20. * and is not sometimes left as 0.
  21. * Alan Cox : connect handles icmp errors on a
  22. * connect properly. Unfortunately there
  23. * is a restart syscall nasty there. I
  24. * can't match BSD without hacking the C
  25. * library. Ideas urgently sought!
  26. * Alan Cox : Disallow bind() to addresses that are
  27. * not ours - especially broadcast ones!!
  28. * Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
  29. * Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
  30. * instead they leave that for the DESTROY timer.
  31. * Alan Cox : Clean up error flag in accept
  32. * Alan Cox : TCP ack handling is buggy, the DESTROY timer
  33. * was buggy. Put a remove_sock() in the handler
  34. * for memory when we hit 0. Also altered the timer
  35. * code. The ACK stuff can wait and needs major
  36. * TCP layer surgery.
  37. * Alan Cox : Fixed TCP ack bug, removed remove sock
  38. * and fixed timer/inet_bh race.
  39. * Alan Cox : Added zapped flag for TCP
  40. * Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
  41. * Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  42. * Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
  43. * Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
  44. * Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
  45. * Rick Sladkey : Relaxed UDP rules for matching packets.
  46. * C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
  47. * Pauline Middelink : identd support
  48. * Alan Cox : Fixed connect() taking signals I think.
  49. * Alan Cox : SO_LINGER supported
  50. * Alan Cox : Error reporting fixes
  51. * Anonymous : inet_create tidied up (sk->reuse setting)
  52. * Alan Cox : inet sockets don't set sk->type!
  53. * Alan Cox : Split socket option code
  54. * Alan Cox : Callbacks
  55. * Alan Cox : Nagle flag for Charles & Johannes stuff
  56. * Alex : Removed restriction on inet fioctl
  57. * Alan Cox : Splitting INET from NET core
  58. * Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
  59. * Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
  60. * Alan Cox : Split IP from generic code
  61. * Alan Cox : New kfree_skbmem()
  62. * Alan Cox : Make SO_DEBUG superuser only.
  63. * Alan Cox : Allow anyone to clear SO_DEBUG
  64. * (compatibility fix)
  65. * Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
  66. * Alan Cox : Allocator for a socket is settable.
  67. * Alan Cox : SO_ERROR includes soft errors.
  68. * Alan Cox : Allow NULL arguments on some SO_ opts
  69. * Alan Cox : Generic socket allocation to make hooks
  70. * easier (suggested by Craig Metz).
  71. * Michael Pall : SO_ERROR returns positive errno again
  72. * Steve Whitehouse: Added default destructor to free
  73. * protocol private data.
  74. * Steve Whitehouse: Added various other default routines
  75. * common to several socket families.
  76. * Chris Evans : Call suser() check last on F_SETOWN
  77. * Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
  78. * Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
  79. * Andi Kleen : Fix write_space callback
  80. * Chris Evans : Security fixes - signedness again
  81. * Arnaldo C. Melo : cleanups, use skb_queue_purge
  82. *
  83. * To Fix:
  84. *
  85. *
  86. * This program is free software; you can redistribute it and/or
  87. * modify it under the terms of the GNU General Public License
  88. * as published by the Free Software Foundation; either version
  89. * 2 of the License, or (at your option) any later version.
  90. */
  91. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  92. #include <linux/capability.h>
  93. #include <linux/errno.h>
  94. #include <linux/errqueue.h>
  95. #include <linux/types.h>
  96. #include <linux/socket.h>
  97. #include <linux/in.h>
  98. #include <linux/kernel.h>
  99. #include <linux/module.h>
  100. #include <linux/proc_fs.h>
  101. #include <linux/seq_file.h>
  102. #include <linux/sched.h>
  103. #include <linux/timer.h>
  104. #include <linux/string.h>
  105. #include <linux/sockios.h>
  106. #include <linux/net.h>
  107. #include <linux/mm.h>
  108. #include <linux/slab.h>
  109. #include <linux/interrupt.h>
  110. #include <linux/poll.h>
  111. #include <linux/tcp.h>
  112. #include <linux/init.h>
  113. #include <linux/highmem.h>
  114. #include <linux/user_namespace.h>
  115. #include <linux/static_key.h>
  116. #include <linux/memcontrol.h>
  117. #include <linux/prefetch.h>
  118. #include <asm/uaccess.h>
  119. #include <linux/netdevice.h>
  120. #include <net/protocol.h>
  121. #include <linux/skbuff.h>
  122. #include <net/net_namespace.h>
  123. #include <net/request_sock.h>
  124. #include <net/sock.h>
  125. #include <linux/net_tstamp.h>
  126. #include <net/xfrm.h>
  127. #include <linux/ipsec.h>
  128. #include <net/cls_cgroup.h>
  129. #include <net/netprio_cgroup.h>
  130. #include <linux/sock_diag.h>
  131. #include <linux/filter.h>
  132. #include <trace/events/sock.h>
  133. #ifdef CONFIG_INET
  134. #include <net/tcp.h>
  135. #endif
  136. #include <net/busy_poll.h>
  137. static DEFINE_MUTEX(proto_list_mutex);
  138. static LIST_HEAD(proto_list);
  139. /**
  140. * sk_ns_capable - General socket capability test
  141. * @sk: Socket to use a capability on or through
  142. * @user_ns: The user namespace of the capability to use
  143. * @cap: The capability to use
  144. *
  145. * Test to see if the opener of the socket had when the socket was
  146. * created and the current process has the capability @cap in the user
  147. * namespace @user_ns.
  148. */
  149. bool sk_ns_capable(const struct sock *sk,
  150. struct user_namespace *user_ns, int cap)
  151. {
  152. return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
  153. ns_capable(user_ns, cap);
  154. }
  155. EXPORT_SYMBOL(sk_ns_capable);
  156. /**
  157. * sk_capable - Socket global capability test
  158. * @sk: Socket to use a capability on or through
  159. * @cap: The global capability to use
  160. *
  161. * Test to see if the opener of the socket had when the socket was
  162. * created and the current process has the capability @cap in all user
  163. * namespaces.
  164. */
  165. bool sk_capable(const struct sock *sk, int cap)
  166. {
  167. return sk_ns_capable(sk, &init_user_ns, cap);
  168. }
  169. EXPORT_SYMBOL(sk_capable);
  170. /**
  171. * sk_net_capable - Network namespace socket capability test
  172. * @sk: Socket to use a capability on or through
  173. * @cap: The capability to use
  174. *
  175. * Test to see if the opener of the socket had when the socket was created
  176. * and the current process has the capability @cap over the network namespace
  177. * the socket is a member of.
  178. */
  179. bool sk_net_capable(const struct sock *sk, int cap)
  180. {
  181. return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
  182. }
  183. EXPORT_SYMBOL(sk_net_capable);
  184. #ifdef CONFIG_MEMCG_KMEM
  185. int mem_cgroup_sockets_init(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
  186. {
  187. struct proto *proto;
  188. int ret = 0;
  189. mutex_lock(&proto_list_mutex);
  190. list_for_each_entry(proto, &proto_list, node) {
  191. if (proto->init_cgroup) {
  192. ret = proto->init_cgroup(memcg, ss);
  193. if (ret)
  194. goto out;
  195. }
  196. }
  197. mutex_unlock(&proto_list_mutex);
  198. return ret;
  199. out:
  200. list_for_each_entry_continue_reverse(proto, &proto_list, node)
  201. if (proto->destroy_cgroup)
  202. proto->destroy_cgroup(memcg);
  203. mutex_unlock(&proto_list_mutex);
  204. return ret;
  205. }
  206. void mem_cgroup_sockets_destroy(struct mem_cgroup *memcg)
  207. {
  208. struct proto *proto;
  209. mutex_lock(&proto_list_mutex);
  210. list_for_each_entry_reverse(proto, &proto_list, node)
  211. if (proto->destroy_cgroup)
  212. proto->destroy_cgroup(memcg);
  213. mutex_unlock(&proto_list_mutex);
  214. }
  215. #endif
  216. /*
  217. * Each address family might have different locking rules, so we have
  218. * one slock key per address family:
  219. */
  220. static struct lock_class_key af_family_keys[AF_MAX];
  221. static struct lock_class_key af_family_slock_keys[AF_MAX];
  222. #if defined(CONFIG_MEMCG_KMEM)
  223. struct static_key memcg_socket_limit_enabled;
  224. EXPORT_SYMBOL(memcg_socket_limit_enabled);
  225. #endif
  226. /*
  227. * Make lock validator output more readable. (we pre-construct these
  228. * strings build-time, so that runtime initialization of socket
  229. * locks is fast):
  230. */
  231. static const char *const af_family_key_strings[AF_MAX+1] = {
  232. "sk_lock-AF_UNSPEC", "sk_lock-AF_UNIX" , "sk_lock-AF_INET" ,
  233. "sk_lock-AF_AX25" , "sk_lock-AF_IPX" , "sk_lock-AF_APPLETALK",
  234. "sk_lock-AF_NETROM", "sk_lock-AF_BRIDGE" , "sk_lock-AF_ATMPVC" ,
  235. "sk_lock-AF_X25" , "sk_lock-AF_INET6" , "sk_lock-AF_ROSE" ,
  236. "sk_lock-AF_DECnet", "sk_lock-AF_NETBEUI" , "sk_lock-AF_SECURITY" ,
  237. "sk_lock-AF_KEY" , "sk_lock-AF_NETLINK" , "sk_lock-AF_PACKET" ,
  238. "sk_lock-AF_ASH" , "sk_lock-AF_ECONET" , "sk_lock-AF_ATMSVC" ,
  239. "sk_lock-AF_RDS" , "sk_lock-AF_SNA" , "sk_lock-AF_IRDA" ,
  240. "sk_lock-AF_PPPOX" , "sk_lock-AF_WANPIPE" , "sk_lock-AF_LLC" ,
  241. "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" ,
  242. "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
  243. "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
  244. "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG" ,
  245. "sk_lock-AF_NFC" , "sk_lock-AF_VSOCK" , "sk_lock-AF_MAX"
  246. };
  247. static const char *const af_family_slock_key_strings[AF_MAX+1] = {
  248. "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
  249. "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
  250. "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
  251. "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
  252. "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
  253. "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
  254. "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
  255. "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
  256. "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
  257. "slock-27" , "slock-28" , "slock-AF_CAN" ,
  258. "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
  259. "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
  260. "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG" ,
  261. "slock-AF_NFC" , "slock-AF_VSOCK" ,"slock-AF_MAX"
  262. };
  263. static const char *const af_family_clock_key_strings[AF_MAX+1] = {
  264. "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
  265. "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
  266. "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
  267. "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
  268. "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
  269. "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
  270. "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
  271. "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
  272. "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
  273. "clock-27" , "clock-28" , "clock-AF_CAN" ,
  274. "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
  275. "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
  276. "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG" ,
  277. "clock-AF_NFC" , "clock-AF_VSOCK" , "clock-AF_MAX"
  278. };
  279. /*
  280. * sk_callback_lock locking rules are per-address-family,
  281. * so split the lock classes by using a per-AF key:
  282. */
  283. static struct lock_class_key af_callback_keys[AF_MAX];
  284. /* Take into consideration the size of the struct sk_buff overhead in the
  285. * determination of these values, since that is non-constant across
  286. * platforms. This makes socket queueing behavior and performance
  287. * not depend upon such differences.
  288. */
  289. #define _SK_MEM_PACKETS 256
  290. #define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
  291. #define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  292. #define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  293. /* Run time adjustable parameters. */
  294. __u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
  295. EXPORT_SYMBOL(sysctl_wmem_max);
  296. __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
  297. EXPORT_SYMBOL(sysctl_rmem_max);
  298. __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
  299. __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
  300. /* Maximal space eaten by iovec or ancillary data plus some space */
  301. int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
  302. EXPORT_SYMBOL(sysctl_optmem_max);
  303. int sysctl_tstamp_allow_data __read_mostly = 1;
  304. struct static_key memalloc_socks = STATIC_KEY_INIT_FALSE;
  305. EXPORT_SYMBOL_GPL(memalloc_socks);
  306. /**
  307. * sk_set_memalloc - sets %SOCK_MEMALLOC
  308. * @sk: socket to set it on
  309. *
  310. * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
  311. * It's the responsibility of the admin to adjust min_free_kbytes
  312. * to meet the requirements
  313. */
  314. void sk_set_memalloc(struct sock *sk)
  315. {
  316. sock_set_flag(sk, SOCK_MEMALLOC);
  317. sk->sk_allocation |= __GFP_MEMALLOC;
  318. static_key_slow_inc(&memalloc_socks);
  319. }
  320. EXPORT_SYMBOL_GPL(sk_set_memalloc);
  321. void sk_clear_memalloc(struct sock *sk)
  322. {
  323. sock_reset_flag(sk, SOCK_MEMALLOC);
  324. sk->sk_allocation &= ~__GFP_MEMALLOC;
  325. static_key_slow_dec(&memalloc_socks);
  326. /*
  327. * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
  328. * progress of swapping. SOCK_MEMALLOC may be cleared while
  329. * it has rmem allocations due to the last swapfile being deactivated
  330. * but there is a risk that the socket is unusable due to exceeding
  331. * the rmem limits. Reclaim the reserves and obey rmem limits again.
  332. */
  333. sk_mem_reclaim(sk);
  334. }
  335. EXPORT_SYMBOL_GPL(sk_clear_memalloc);
  336. int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  337. {
  338. int ret;
  339. unsigned long pflags = current->flags;
  340. /* these should have been dropped before queueing */
  341. BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
  342. current->flags |= PF_MEMALLOC;
  343. ret = sk->sk_backlog_rcv(sk, skb);
  344. tsk_restore_flags(current, pflags, PF_MEMALLOC);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL(__sk_backlog_rcv);
  348. static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
  349. {
  350. struct timeval tv;
  351. if (optlen < sizeof(tv))
  352. return -EINVAL;
  353. if (copy_from_user(&tv, optval, sizeof(tv)))
  354. return -EFAULT;
  355. if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
  356. return -EDOM;
  357. if (tv.tv_sec < 0) {
  358. static int warned __read_mostly;
  359. *timeo_p = 0;
  360. if (warned < 10 && net_ratelimit()) {
  361. warned++;
  362. pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
  363. __func__, current->comm, task_pid_nr(current));
  364. }
  365. return 0;
  366. }
  367. *timeo_p = MAX_SCHEDULE_TIMEOUT;
  368. if (tv.tv_sec == 0 && tv.tv_usec == 0)
  369. return 0;
  370. if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
  371. *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
  372. return 0;
  373. }
  374. static void sock_warn_obsolete_bsdism(const char *name)
  375. {
  376. static int warned;
  377. static char warncomm[TASK_COMM_LEN];
  378. if (strcmp(warncomm, current->comm) && warned < 5) {
  379. strcpy(warncomm, current->comm);
  380. pr_warn("process `%s' is using obsolete %s SO_BSDCOMPAT\n",
  381. warncomm, name);
  382. warned++;
  383. }
  384. }
  385. #define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
  386. static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
  387. {
  388. if (sk->sk_flags & flags) {
  389. sk->sk_flags &= ~flags;
  390. if (!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
  391. net_disable_timestamp();
  392. }
  393. }
  394. int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  395. {
  396. int err;
  397. unsigned long flags;
  398. struct sk_buff_head *list = &sk->sk_receive_queue;
  399. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
  400. atomic_inc(&sk->sk_drops);
  401. trace_sock_rcvqueue_full(sk, skb);
  402. return -ENOMEM;
  403. }
  404. err = sk_filter(sk, skb);
  405. if (err)
  406. return err;
  407. if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
  408. atomic_inc(&sk->sk_drops);
  409. return -ENOBUFS;
  410. }
  411. skb->dev = NULL;
  412. skb_set_owner_r(skb, sk);
  413. /* we escape from rcu protected region, make sure we dont leak
  414. * a norefcounted dst
  415. */
  416. skb_dst_force(skb);
  417. spin_lock_irqsave(&list->lock, flags);
  418. sock_skb_set_dropcount(sk, skb);
  419. __skb_queue_tail(list, skb);
  420. spin_unlock_irqrestore(&list->lock, flags);
  421. if (!sock_flag(sk, SOCK_DEAD))
  422. sk->sk_data_ready(sk);
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(sock_queue_rcv_skb);
  426. int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
  427. {
  428. int rc = NET_RX_SUCCESS;
  429. if (sk_filter(sk, skb))
  430. goto discard_and_relse;
  431. skb->dev = NULL;
  432. if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
  433. atomic_inc(&sk->sk_drops);
  434. goto discard_and_relse;
  435. }
  436. if (nested)
  437. bh_lock_sock_nested(sk);
  438. else
  439. bh_lock_sock(sk);
  440. if (!sock_owned_by_user(sk)) {
  441. /*
  442. * trylock + unlock semantics:
  443. */
  444. mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
  445. rc = sk_backlog_rcv(sk, skb);
  446. mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
  447. } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
  448. bh_unlock_sock(sk);
  449. atomic_inc(&sk->sk_drops);
  450. goto discard_and_relse;
  451. }
  452. bh_unlock_sock(sk);
  453. out:
  454. sock_put(sk);
  455. return rc;
  456. discard_and_relse:
  457. kfree_skb(skb);
  458. goto out;
  459. }
  460. EXPORT_SYMBOL(sk_receive_skb);
  461. struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
  462. {
  463. struct dst_entry *dst = __sk_dst_get(sk);
  464. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  465. sk_tx_queue_clear(sk);
  466. RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
  467. dst_release(dst);
  468. return NULL;
  469. }
  470. return dst;
  471. }
  472. EXPORT_SYMBOL(__sk_dst_check);
  473. struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
  474. {
  475. struct dst_entry *dst = sk_dst_get(sk);
  476. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  477. sk_dst_reset(sk);
  478. dst_release(dst);
  479. return NULL;
  480. }
  481. return dst;
  482. }
  483. EXPORT_SYMBOL(sk_dst_check);
  484. static int sock_setbindtodevice(struct sock *sk, char __user *optval,
  485. int optlen)
  486. {
  487. int ret = -ENOPROTOOPT;
  488. #ifdef CONFIG_NETDEVICES
  489. struct net *net = sock_net(sk);
  490. char devname[IFNAMSIZ];
  491. int index;
  492. /* Sorry... */
  493. ret = -EPERM;
  494. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  495. goto out;
  496. ret = -EINVAL;
  497. if (optlen < 0)
  498. goto out;
  499. /* Bind this socket to a particular device like "eth0",
  500. * as specified in the passed interface name. If the
  501. * name is "" or the option length is zero the socket
  502. * is not bound.
  503. */
  504. if (optlen > IFNAMSIZ - 1)
  505. optlen = IFNAMSIZ - 1;
  506. memset(devname, 0, sizeof(devname));
  507. ret = -EFAULT;
  508. if (copy_from_user(devname, optval, optlen))
  509. goto out;
  510. index = 0;
  511. if (devname[0] != '\0') {
  512. struct net_device *dev;
  513. rcu_read_lock();
  514. dev = dev_get_by_name_rcu(net, devname);
  515. if (dev)
  516. index = dev->ifindex;
  517. rcu_read_unlock();
  518. ret = -ENODEV;
  519. if (!dev)
  520. goto out;
  521. }
  522. lock_sock(sk);
  523. sk->sk_bound_dev_if = index;
  524. sk_dst_reset(sk);
  525. release_sock(sk);
  526. ret = 0;
  527. out:
  528. #endif
  529. return ret;
  530. }
  531. static int sock_getbindtodevice(struct sock *sk, char __user *optval,
  532. int __user *optlen, int len)
  533. {
  534. int ret = -ENOPROTOOPT;
  535. #ifdef CONFIG_NETDEVICES
  536. struct net *net = sock_net(sk);
  537. char devname[IFNAMSIZ];
  538. if (sk->sk_bound_dev_if == 0) {
  539. len = 0;
  540. goto zero;
  541. }
  542. ret = -EINVAL;
  543. if (len < IFNAMSIZ)
  544. goto out;
  545. ret = netdev_get_name(net, devname, sk->sk_bound_dev_if);
  546. if (ret)
  547. goto out;
  548. len = strlen(devname) + 1;
  549. ret = -EFAULT;
  550. if (copy_to_user(optval, devname, len))
  551. goto out;
  552. zero:
  553. ret = -EFAULT;
  554. if (put_user(len, optlen))
  555. goto out;
  556. ret = 0;
  557. out:
  558. #endif
  559. return ret;
  560. }
  561. static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
  562. {
  563. if (valbool)
  564. sock_set_flag(sk, bit);
  565. else
  566. sock_reset_flag(sk, bit);
  567. }
  568. bool sk_mc_loop(struct sock *sk)
  569. {
  570. if (dev_recursion_level())
  571. return false;
  572. if (!sk)
  573. return true;
  574. switch (sk->sk_family) {
  575. case AF_INET:
  576. return inet_sk(sk)->mc_loop;
  577. #if IS_ENABLED(CONFIG_IPV6)
  578. case AF_INET6:
  579. return inet6_sk(sk)->mc_loop;
  580. #endif
  581. }
  582. WARN_ON(1);
  583. return true;
  584. }
  585. EXPORT_SYMBOL(sk_mc_loop);
  586. /*
  587. * This is meant for all protocols to use and covers goings on
  588. * at the socket level. Everything here is generic.
  589. */
  590. int sock_setsockopt(struct socket *sock, int level, int optname,
  591. char __user *optval, unsigned int optlen)
  592. {
  593. struct sock *sk = sock->sk;
  594. int val;
  595. int valbool;
  596. struct linger ling;
  597. int ret = 0;
  598. /*
  599. * Options without arguments
  600. */
  601. if (optname == SO_BINDTODEVICE)
  602. return sock_setbindtodevice(sk, optval, optlen);
  603. if (optlen < sizeof(int))
  604. return -EINVAL;
  605. if (get_user(val, (int __user *)optval))
  606. return -EFAULT;
  607. valbool = val ? 1 : 0;
  608. lock_sock(sk);
  609. switch (optname) {
  610. case SO_DEBUG:
  611. if (val && !capable(CAP_NET_ADMIN))
  612. ret = -EACCES;
  613. else
  614. sock_valbool_flag(sk, SOCK_DBG, valbool);
  615. break;
  616. case SO_REUSEADDR:
  617. sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
  618. break;
  619. case SO_REUSEPORT:
  620. sk->sk_reuseport = valbool;
  621. break;
  622. case SO_TYPE:
  623. case SO_PROTOCOL:
  624. case SO_DOMAIN:
  625. case SO_ERROR:
  626. ret = -ENOPROTOOPT;
  627. break;
  628. case SO_DONTROUTE:
  629. sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
  630. break;
  631. case SO_BROADCAST:
  632. sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
  633. break;
  634. case SO_SNDBUF:
  635. /* Don't error on this BSD doesn't and if you think
  636. * about it this is right. Otherwise apps have to
  637. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  638. * are treated in BSD as hints
  639. */
  640. val = min_t(u32, val, sysctl_wmem_max);
  641. set_sndbuf:
  642. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  643. sk->sk_sndbuf = max_t(u32, val * 2, SOCK_MIN_SNDBUF);
  644. /* Wake up sending tasks if we upped the value. */
  645. sk->sk_write_space(sk);
  646. break;
  647. case SO_SNDBUFFORCE:
  648. if (!capable(CAP_NET_ADMIN)) {
  649. ret = -EPERM;
  650. break;
  651. }
  652. goto set_sndbuf;
  653. case SO_RCVBUF:
  654. /* Don't error on this BSD doesn't and if you think
  655. * about it this is right. Otherwise apps have to
  656. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  657. * are treated in BSD as hints
  658. */
  659. val = min_t(u32, val, sysctl_rmem_max);
  660. set_rcvbuf:
  661. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  662. /*
  663. * We double it on the way in to account for
  664. * "struct sk_buff" etc. overhead. Applications
  665. * assume that the SO_RCVBUF setting they make will
  666. * allow that much actual data to be received on that
  667. * socket.
  668. *
  669. * Applications are unaware that "struct sk_buff" and
  670. * other overheads allocate from the receive buffer
  671. * during socket buffer allocation.
  672. *
  673. * And after considering the possible alternatives,
  674. * returning the value we actually used in getsockopt
  675. * is the most desirable behavior.
  676. */
  677. sk->sk_rcvbuf = max_t(u32, val * 2, SOCK_MIN_RCVBUF);
  678. break;
  679. case SO_RCVBUFFORCE:
  680. if (!capable(CAP_NET_ADMIN)) {
  681. ret = -EPERM;
  682. break;
  683. }
  684. goto set_rcvbuf;
  685. case SO_KEEPALIVE:
  686. #ifdef CONFIG_INET
  687. if (sk->sk_protocol == IPPROTO_TCP &&
  688. sk->sk_type == SOCK_STREAM)
  689. tcp_set_keepalive(sk, valbool);
  690. #endif
  691. sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
  692. break;
  693. case SO_OOBINLINE:
  694. sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
  695. break;
  696. case SO_NO_CHECK:
  697. sk->sk_no_check_tx = valbool;
  698. break;
  699. case SO_PRIORITY:
  700. if ((val >= 0 && val <= 6) ||
  701. ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  702. sk->sk_priority = val;
  703. else
  704. ret = -EPERM;
  705. break;
  706. case SO_LINGER:
  707. if (optlen < sizeof(ling)) {
  708. ret = -EINVAL; /* 1003.1g */
  709. break;
  710. }
  711. if (copy_from_user(&ling, optval, sizeof(ling))) {
  712. ret = -EFAULT;
  713. break;
  714. }
  715. if (!ling.l_onoff)
  716. sock_reset_flag(sk, SOCK_LINGER);
  717. else {
  718. #if (BITS_PER_LONG == 32)
  719. if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
  720. sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
  721. else
  722. #endif
  723. sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
  724. sock_set_flag(sk, SOCK_LINGER);
  725. }
  726. break;
  727. case SO_BSDCOMPAT:
  728. sock_warn_obsolete_bsdism("setsockopt");
  729. break;
  730. case SO_PASSCRED:
  731. if (valbool)
  732. set_bit(SOCK_PASSCRED, &sock->flags);
  733. else
  734. clear_bit(SOCK_PASSCRED, &sock->flags);
  735. break;
  736. case SO_TIMESTAMP:
  737. case SO_TIMESTAMPNS:
  738. if (valbool) {
  739. if (optname == SO_TIMESTAMP)
  740. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  741. else
  742. sock_set_flag(sk, SOCK_RCVTSTAMPNS);
  743. sock_set_flag(sk, SOCK_RCVTSTAMP);
  744. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  745. } else {
  746. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  747. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  748. }
  749. break;
  750. case SO_TIMESTAMPING:
  751. if (val & ~SOF_TIMESTAMPING_MASK) {
  752. ret = -EINVAL;
  753. break;
  754. }
  755. if (val & SOF_TIMESTAMPING_OPT_ID &&
  756. !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
  757. if (sk->sk_protocol == IPPROTO_TCP) {
  758. if (sk->sk_state != TCP_ESTABLISHED) {
  759. ret = -EINVAL;
  760. break;
  761. }
  762. sk->sk_tskey = tcp_sk(sk)->snd_una;
  763. } else {
  764. sk->sk_tskey = 0;
  765. }
  766. }
  767. sk->sk_tsflags = val;
  768. if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
  769. sock_enable_timestamp(sk,
  770. SOCK_TIMESTAMPING_RX_SOFTWARE);
  771. else
  772. sock_disable_timestamp(sk,
  773. (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
  774. break;
  775. case SO_RCVLOWAT:
  776. if (val < 0)
  777. val = INT_MAX;
  778. sk->sk_rcvlowat = val ? : 1;
  779. break;
  780. case SO_RCVTIMEO:
  781. ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
  782. break;
  783. case SO_SNDTIMEO:
  784. ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
  785. break;
  786. case SO_ATTACH_FILTER:
  787. ret = -EINVAL;
  788. if (optlen == sizeof(struct sock_fprog)) {
  789. struct sock_fprog fprog;
  790. ret = -EFAULT;
  791. if (copy_from_user(&fprog, optval, sizeof(fprog)))
  792. break;
  793. ret = sk_attach_filter(&fprog, sk);
  794. }
  795. break;
  796. case SO_ATTACH_BPF:
  797. ret = -EINVAL;
  798. if (optlen == sizeof(u32)) {
  799. u32 ufd;
  800. ret = -EFAULT;
  801. if (copy_from_user(&ufd, optval, sizeof(ufd)))
  802. break;
  803. ret = sk_attach_bpf(ufd, sk);
  804. }
  805. break;
  806. case SO_DETACH_FILTER:
  807. ret = sk_detach_filter(sk);
  808. break;
  809. case SO_LOCK_FILTER:
  810. if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
  811. ret = -EPERM;
  812. else
  813. sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
  814. break;
  815. case SO_PASSSEC:
  816. if (valbool)
  817. set_bit(SOCK_PASSSEC, &sock->flags);
  818. else
  819. clear_bit(SOCK_PASSSEC, &sock->flags);
  820. break;
  821. case SO_MARK:
  822. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  823. ret = -EPERM;
  824. else
  825. sk->sk_mark = val;
  826. break;
  827. case SO_RXQ_OVFL:
  828. sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
  829. break;
  830. case SO_WIFI_STATUS:
  831. sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
  832. break;
  833. case SO_PEEK_OFF:
  834. if (sock->ops->set_peek_off)
  835. ret = sock->ops->set_peek_off(sk, val);
  836. else
  837. ret = -EOPNOTSUPP;
  838. break;
  839. case SO_NOFCS:
  840. sock_valbool_flag(sk, SOCK_NOFCS, valbool);
  841. break;
  842. case SO_SELECT_ERR_QUEUE:
  843. sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
  844. break;
  845. #ifdef CONFIG_NET_RX_BUSY_POLL
  846. case SO_BUSY_POLL:
  847. /* allow unprivileged users to decrease the value */
  848. if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN))
  849. ret = -EPERM;
  850. else {
  851. if (val < 0)
  852. ret = -EINVAL;
  853. else
  854. sk->sk_ll_usec = val;
  855. }
  856. break;
  857. #endif
  858. case SO_MAX_PACING_RATE:
  859. sk->sk_max_pacing_rate = val;
  860. sk->sk_pacing_rate = min(sk->sk_pacing_rate,
  861. sk->sk_max_pacing_rate);
  862. break;
  863. default:
  864. ret = -ENOPROTOOPT;
  865. break;
  866. }
  867. release_sock(sk);
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(sock_setsockopt);
  871. static void cred_to_ucred(struct pid *pid, const struct cred *cred,
  872. struct ucred *ucred)
  873. {
  874. ucred->pid = pid_vnr(pid);
  875. ucred->uid = ucred->gid = -1;
  876. if (cred) {
  877. struct user_namespace *current_ns = current_user_ns();
  878. ucred->uid = from_kuid_munged(current_ns, cred->euid);
  879. ucred->gid = from_kgid_munged(current_ns, cred->egid);
  880. }
  881. }
  882. int sock_getsockopt(struct socket *sock, int level, int optname,
  883. char __user *optval, int __user *optlen)
  884. {
  885. struct sock *sk = sock->sk;
  886. union {
  887. int val;
  888. struct linger ling;
  889. struct timeval tm;
  890. } v;
  891. int lv = sizeof(int);
  892. int len;
  893. if (get_user(len, optlen))
  894. return -EFAULT;
  895. if (len < 0)
  896. return -EINVAL;
  897. memset(&v, 0, sizeof(v));
  898. switch (optname) {
  899. case SO_DEBUG:
  900. v.val = sock_flag(sk, SOCK_DBG);
  901. break;
  902. case SO_DONTROUTE:
  903. v.val = sock_flag(sk, SOCK_LOCALROUTE);
  904. break;
  905. case SO_BROADCAST:
  906. v.val = sock_flag(sk, SOCK_BROADCAST);
  907. break;
  908. case SO_SNDBUF:
  909. v.val = sk->sk_sndbuf;
  910. break;
  911. case SO_RCVBUF:
  912. v.val = sk->sk_rcvbuf;
  913. break;
  914. case SO_REUSEADDR:
  915. v.val = sk->sk_reuse;
  916. break;
  917. case SO_REUSEPORT:
  918. v.val = sk->sk_reuseport;
  919. break;
  920. case SO_KEEPALIVE:
  921. v.val = sock_flag(sk, SOCK_KEEPOPEN);
  922. break;
  923. case SO_TYPE:
  924. v.val = sk->sk_type;
  925. break;
  926. case SO_PROTOCOL:
  927. v.val = sk->sk_protocol;
  928. break;
  929. case SO_DOMAIN:
  930. v.val = sk->sk_family;
  931. break;
  932. case SO_ERROR:
  933. v.val = -sock_error(sk);
  934. if (v.val == 0)
  935. v.val = xchg(&sk->sk_err_soft, 0);
  936. break;
  937. case SO_OOBINLINE:
  938. v.val = sock_flag(sk, SOCK_URGINLINE);
  939. break;
  940. case SO_NO_CHECK:
  941. v.val = sk->sk_no_check_tx;
  942. break;
  943. case SO_PRIORITY:
  944. v.val = sk->sk_priority;
  945. break;
  946. case SO_LINGER:
  947. lv = sizeof(v.ling);
  948. v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
  949. v.ling.l_linger = sk->sk_lingertime / HZ;
  950. break;
  951. case SO_BSDCOMPAT:
  952. sock_warn_obsolete_bsdism("getsockopt");
  953. break;
  954. case SO_TIMESTAMP:
  955. v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
  956. !sock_flag(sk, SOCK_RCVTSTAMPNS);
  957. break;
  958. case SO_TIMESTAMPNS:
  959. v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
  960. break;
  961. case SO_TIMESTAMPING:
  962. v.val = sk->sk_tsflags;
  963. break;
  964. case SO_RCVTIMEO:
  965. lv = sizeof(struct timeval);
  966. if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
  967. v.tm.tv_sec = 0;
  968. v.tm.tv_usec = 0;
  969. } else {
  970. v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
  971. v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
  972. }
  973. break;
  974. case SO_SNDTIMEO:
  975. lv = sizeof(struct timeval);
  976. if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
  977. v.tm.tv_sec = 0;
  978. v.tm.tv_usec = 0;
  979. } else {
  980. v.tm.tv_sec = sk->sk_sndtimeo / HZ;
  981. v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
  982. }
  983. break;
  984. case SO_RCVLOWAT:
  985. v.val = sk->sk_rcvlowat;
  986. break;
  987. case SO_SNDLOWAT:
  988. v.val = 1;
  989. break;
  990. case SO_PASSCRED:
  991. v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
  992. break;
  993. case SO_PEERCRED:
  994. {
  995. struct ucred peercred;
  996. if (len > sizeof(peercred))
  997. len = sizeof(peercred);
  998. cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
  999. if (copy_to_user(optval, &peercred, len))
  1000. return -EFAULT;
  1001. goto lenout;
  1002. }
  1003. case SO_PEERNAME:
  1004. {
  1005. char address[128];
  1006. if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
  1007. return -ENOTCONN;
  1008. if (lv < len)
  1009. return -EINVAL;
  1010. if (copy_to_user(optval, address, len))
  1011. return -EFAULT;
  1012. goto lenout;
  1013. }
  1014. /* Dubious BSD thing... Probably nobody even uses it, but
  1015. * the UNIX standard wants it for whatever reason... -DaveM
  1016. */
  1017. case SO_ACCEPTCONN:
  1018. v.val = sk->sk_state == TCP_LISTEN;
  1019. break;
  1020. case SO_PASSSEC:
  1021. v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
  1022. break;
  1023. case SO_PEERSEC:
  1024. return security_socket_getpeersec_stream(sock, optval, optlen, len);
  1025. case SO_MARK:
  1026. v.val = sk->sk_mark;
  1027. break;
  1028. case SO_RXQ_OVFL:
  1029. v.val = sock_flag(sk, SOCK_RXQ_OVFL);
  1030. break;
  1031. case SO_WIFI_STATUS:
  1032. v.val = sock_flag(sk, SOCK_WIFI_STATUS);
  1033. break;
  1034. case SO_PEEK_OFF:
  1035. if (!sock->ops->set_peek_off)
  1036. return -EOPNOTSUPP;
  1037. v.val = sk->sk_peek_off;
  1038. break;
  1039. case SO_NOFCS:
  1040. v.val = sock_flag(sk, SOCK_NOFCS);
  1041. break;
  1042. case SO_BINDTODEVICE:
  1043. return sock_getbindtodevice(sk, optval, optlen, len);
  1044. case SO_GET_FILTER:
  1045. len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
  1046. if (len < 0)
  1047. return len;
  1048. goto lenout;
  1049. case SO_LOCK_FILTER:
  1050. v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
  1051. break;
  1052. case SO_BPF_EXTENSIONS:
  1053. v.val = bpf_tell_extensions();
  1054. break;
  1055. case SO_SELECT_ERR_QUEUE:
  1056. v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
  1057. break;
  1058. #ifdef CONFIG_NET_RX_BUSY_POLL
  1059. case SO_BUSY_POLL:
  1060. v.val = sk->sk_ll_usec;
  1061. break;
  1062. #endif
  1063. case SO_MAX_PACING_RATE:
  1064. v.val = sk->sk_max_pacing_rate;
  1065. break;
  1066. case SO_INCOMING_CPU:
  1067. v.val = sk->sk_incoming_cpu;
  1068. break;
  1069. default:
  1070. /* We implement the SO_SNDLOWAT etc to not be settable
  1071. * (1003.1g 7).
  1072. */
  1073. return -ENOPROTOOPT;
  1074. }
  1075. if (len > lv)
  1076. len = lv;
  1077. if (copy_to_user(optval, &v, len))
  1078. return -EFAULT;
  1079. lenout:
  1080. if (put_user(len, optlen))
  1081. return -EFAULT;
  1082. return 0;
  1083. }
  1084. /*
  1085. * Initialize an sk_lock.
  1086. *
  1087. * (We also register the sk_lock with the lock validator.)
  1088. */
  1089. static inline void sock_lock_init(struct sock *sk)
  1090. {
  1091. sock_lock_init_class_and_name(sk,
  1092. af_family_slock_key_strings[sk->sk_family],
  1093. af_family_slock_keys + sk->sk_family,
  1094. af_family_key_strings[sk->sk_family],
  1095. af_family_keys + sk->sk_family);
  1096. }
  1097. /*
  1098. * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
  1099. * even temporarly, because of RCU lookups. sk_node should also be left as is.
  1100. * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
  1101. */
  1102. static void sock_copy(struct sock *nsk, const struct sock *osk)
  1103. {
  1104. #ifdef CONFIG_SECURITY_NETWORK
  1105. void *sptr = nsk->sk_security;
  1106. #endif
  1107. memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
  1108. memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
  1109. osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
  1110. #ifdef CONFIG_SECURITY_NETWORK
  1111. nsk->sk_security = sptr;
  1112. security_sk_clone(osk, nsk);
  1113. #endif
  1114. }
  1115. void sk_prot_clear_portaddr_nulls(struct sock *sk, int size)
  1116. {
  1117. unsigned long nulls1, nulls2;
  1118. nulls1 = offsetof(struct sock, __sk_common.skc_node.next);
  1119. nulls2 = offsetof(struct sock, __sk_common.skc_portaddr_node.next);
  1120. if (nulls1 > nulls2)
  1121. swap(nulls1, nulls2);
  1122. if (nulls1 != 0)
  1123. memset((char *)sk, 0, nulls1);
  1124. memset((char *)sk + nulls1 + sizeof(void *), 0,
  1125. nulls2 - nulls1 - sizeof(void *));
  1126. memset((char *)sk + nulls2 + sizeof(void *), 0,
  1127. size - nulls2 - sizeof(void *));
  1128. }
  1129. EXPORT_SYMBOL(sk_prot_clear_portaddr_nulls);
  1130. static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
  1131. int family)
  1132. {
  1133. struct sock *sk;
  1134. struct kmem_cache *slab;
  1135. slab = prot->slab;
  1136. if (slab != NULL) {
  1137. sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
  1138. if (!sk)
  1139. return sk;
  1140. if (priority & __GFP_ZERO) {
  1141. if (prot->clear_sk)
  1142. prot->clear_sk(sk, prot->obj_size);
  1143. else
  1144. sk_prot_clear_nulls(sk, prot->obj_size);
  1145. }
  1146. } else
  1147. sk = kmalloc(prot->obj_size, priority);
  1148. if (sk != NULL) {
  1149. kmemcheck_annotate_bitfield(sk, flags);
  1150. if (security_sk_alloc(sk, family, priority))
  1151. goto out_free;
  1152. if (!try_module_get(prot->owner))
  1153. goto out_free_sec;
  1154. sk_tx_queue_clear(sk);
  1155. }
  1156. return sk;
  1157. out_free_sec:
  1158. security_sk_free(sk);
  1159. out_free:
  1160. if (slab != NULL)
  1161. kmem_cache_free(slab, sk);
  1162. else
  1163. kfree(sk);
  1164. return NULL;
  1165. }
  1166. static void sk_prot_free(struct proto *prot, struct sock *sk)
  1167. {
  1168. struct kmem_cache *slab;
  1169. struct module *owner;
  1170. owner = prot->owner;
  1171. slab = prot->slab;
  1172. security_sk_free(sk);
  1173. if (slab != NULL)
  1174. kmem_cache_free(slab, sk);
  1175. else
  1176. kfree(sk);
  1177. module_put(owner);
  1178. }
  1179. #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
  1180. void sock_update_netprioidx(struct sock *sk)
  1181. {
  1182. if (in_interrupt())
  1183. return;
  1184. sk->sk_cgrp_prioidx = task_netprioidx(current);
  1185. }
  1186. EXPORT_SYMBOL_GPL(sock_update_netprioidx);
  1187. #endif
  1188. /**
  1189. * sk_alloc - All socket objects are allocated here
  1190. * @net: the applicable net namespace
  1191. * @family: protocol family
  1192. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  1193. * @prot: struct proto associated with this new sock instance
  1194. * @kern: is this to be a kernel socket?
  1195. */
  1196. struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
  1197. struct proto *prot, int kern)
  1198. {
  1199. struct sock *sk;
  1200. sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
  1201. if (sk) {
  1202. sk->sk_family = family;
  1203. /*
  1204. * See comment in struct sock definition to understand
  1205. * why we need sk_prot_creator -acme
  1206. */
  1207. sk->sk_prot = sk->sk_prot_creator = prot;
  1208. sock_lock_init(sk);
  1209. sk->sk_net_refcnt = kern ? 0 : 1;
  1210. if (likely(sk->sk_net_refcnt))
  1211. get_net(net);
  1212. sock_net_set(sk, net);
  1213. atomic_set(&sk->sk_wmem_alloc, 1);
  1214. sock_update_classid(sk);
  1215. sock_update_netprioidx(sk);
  1216. }
  1217. return sk;
  1218. }
  1219. EXPORT_SYMBOL(sk_alloc);
  1220. void sk_destruct(struct sock *sk)
  1221. {
  1222. struct sk_filter *filter;
  1223. if (sk->sk_destruct)
  1224. sk->sk_destruct(sk);
  1225. filter = rcu_dereference_check(sk->sk_filter,
  1226. atomic_read(&sk->sk_wmem_alloc) == 0);
  1227. if (filter) {
  1228. sk_filter_uncharge(sk, filter);
  1229. RCU_INIT_POINTER(sk->sk_filter, NULL);
  1230. }
  1231. sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
  1232. if (atomic_read(&sk->sk_omem_alloc))
  1233. pr_debug("%s: optmem leakage (%d bytes) detected\n",
  1234. __func__, atomic_read(&sk->sk_omem_alloc));
  1235. if (sk->sk_peer_cred)
  1236. put_cred(sk->sk_peer_cred);
  1237. put_pid(sk->sk_peer_pid);
  1238. if (likely(sk->sk_net_refcnt))
  1239. put_net(sock_net(sk));
  1240. sk_prot_free(sk->sk_prot_creator, sk);
  1241. }
  1242. static void __sk_free(struct sock *sk)
  1243. {
  1244. if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
  1245. sock_diag_broadcast_destroy(sk);
  1246. else
  1247. sk_destruct(sk);
  1248. }
  1249. void sk_free(struct sock *sk)
  1250. {
  1251. /*
  1252. * We subtract one from sk_wmem_alloc and can know if
  1253. * some packets are still in some tx queue.
  1254. * If not null, sock_wfree() will call __sk_free(sk) later
  1255. */
  1256. if (atomic_dec_and_test(&sk->sk_wmem_alloc))
  1257. __sk_free(sk);
  1258. }
  1259. EXPORT_SYMBOL(sk_free);
  1260. static void sk_update_clone(const struct sock *sk, struct sock *newsk)
  1261. {
  1262. if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
  1263. sock_update_memcg(newsk);
  1264. }
  1265. /**
  1266. * sk_clone_lock - clone a socket, and lock its clone
  1267. * @sk: the socket to clone
  1268. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  1269. *
  1270. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  1271. */
  1272. struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
  1273. {
  1274. struct sock *newsk;
  1275. bool is_charged = true;
  1276. newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
  1277. if (newsk != NULL) {
  1278. struct sk_filter *filter;
  1279. sock_copy(newsk, sk);
  1280. /* SANITY */
  1281. get_net(sock_net(newsk));
  1282. sk_node_init(&newsk->sk_node);
  1283. sock_lock_init(newsk);
  1284. bh_lock_sock(newsk);
  1285. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  1286. newsk->sk_backlog.len = 0;
  1287. atomic_set(&newsk->sk_rmem_alloc, 0);
  1288. /*
  1289. * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
  1290. */
  1291. atomic_set(&newsk->sk_wmem_alloc, 1);
  1292. atomic_set(&newsk->sk_omem_alloc, 0);
  1293. skb_queue_head_init(&newsk->sk_receive_queue);
  1294. skb_queue_head_init(&newsk->sk_write_queue);
  1295. spin_lock_init(&newsk->sk_dst_lock);
  1296. rwlock_init(&newsk->sk_callback_lock);
  1297. lockdep_set_class_and_name(&newsk->sk_callback_lock,
  1298. af_callback_keys + newsk->sk_family,
  1299. af_family_clock_key_strings[newsk->sk_family]);
  1300. newsk->sk_dst_cache = NULL;
  1301. newsk->sk_wmem_queued = 0;
  1302. newsk->sk_forward_alloc = 0;
  1303. newsk->sk_send_head = NULL;
  1304. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  1305. sock_reset_flag(newsk, SOCK_DONE);
  1306. skb_queue_head_init(&newsk->sk_error_queue);
  1307. filter = rcu_dereference_protected(newsk->sk_filter, 1);
  1308. if (filter != NULL)
  1309. /* though it's an empty new sock, the charging may fail
  1310. * if sysctl_optmem_max was changed between creation of
  1311. * original socket and cloning
  1312. */
  1313. is_charged = sk_filter_charge(newsk, filter);
  1314. if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk))) {
  1315. /* It is still raw copy of parent, so invalidate
  1316. * destructor and make plain sk_free() */
  1317. newsk->sk_destruct = NULL;
  1318. bh_unlock_sock(newsk);
  1319. sk_free(newsk);
  1320. newsk = NULL;
  1321. goto out;
  1322. }
  1323. newsk->sk_err = 0;
  1324. newsk->sk_priority = 0;
  1325. newsk->sk_incoming_cpu = raw_smp_processor_id();
  1326. atomic64_set(&newsk->sk_cookie, 0);
  1327. /*
  1328. * Before updating sk_refcnt, we must commit prior changes to memory
  1329. * (Documentation/RCU/rculist_nulls.txt for details)
  1330. */
  1331. smp_wmb();
  1332. atomic_set(&newsk->sk_refcnt, 2);
  1333. /*
  1334. * Increment the counter in the same struct proto as the master
  1335. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  1336. * is the same as sk->sk_prot->socks, as this field was copied
  1337. * with memcpy).
  1338. *
  1339. * This _changes_ the previous behaviour, where
  1340. * tcp_create_openreq_child always was incrementing the
  1341. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  1342. * to be taken into account in all callers. -acme
  1343. */
  1344. sk_refcnt_debug_inc(newsk);
  1345. sk_set_socket(newsk, NULL);
  1346. newsk->sk_wq = NULL;
  1347. sk_update_clone(sk, newsk);
  1348. if (newsk->sk_prot->sockets_allocated)
  1349. sk_sockets_allocated_inc(newsk);
  1350. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  1351. net_enable_timestamp();
  1352. }
  1353. out:
  1354. return newsk;
  1355. }
  1356. EXPORT_SYMBOL_GPL(sk_clone_lock);
  1357. void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
  1358. {
  1359. u32 max_segs = 1;
  1360. __sk_dst_set(sk, dst);
  1361. sk->sk_route_caps = dst->dev->features;
  1362. if (sk->sk_route_caps & NETIF_F_GSO)
  1363. sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
  1364. sk->sk_route_caps &= ~sk->sk_route_nocaps;
  1365. if (sk_can_gso(sk)) {
  1366. if (dst->header_len) {
  1367. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  1368. } else {
  1369. sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1370. sk->sk_gso_max_size = dst->dev->gso_max_size;
  1371. max_segs = max_t(u32, dst->dev->gso_max_segs, 1);
  1372. }
  1373. }
  1374. sk->sk_gso_max_segs = max_segs;
  1375. }
  1376. EXPORT_SYMBOL_GPL(sk_setup_caps);
  1377. /*
  1378. * Simple resource managers for sockets.
  1379. */
  1380. /*
  1381. * Write buffer destructor automatically called from kfree_skb.
  1382. */
  1383. void sock_wfree(struct sk_buff *skb)
  1384. {
  1385. struct sock *sk = skb->sk;
  1386. unsigned int len = skb->truesize;
  1387. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
  1388. /*
  1389. * Keep a reference on sk_wmem_alloc, this will be released
  1390. * after sk_write_space() call
  1391. */
  1392. atomic_sub(len - 1, &sk->sk_wmem_alloc);
  1393. sk->sk_write_space(sk);
  1394. len = 1;
  1395. }
  1396. /*
  1397. * if sk_wmem_alloc reaches 0, we must finish what sk_free()
  1398. * could not do because of in-flight packets
  1399. */
  1400. if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
  1401. __sk_free(sk);
  1402. }
  1403. EXPORT_SYMBOL(sock_wfree);
  1404. void skb_orphan_partial(struct sk_buff *skb)
  1405. {
  1406. /* TCP stack sets skb->ooo_okay based on sk_wmem_alloc,
  1407. * so we do not completely orphan skb, but transfert all
  1408. * accounted bytes but one, to avoid unexpected reorders.
  1409. */
  1410. if (skb->destructor == sock_wfree
  1411. #ifdef CONFIG_INET
  1412. || skb->destructor == tcp_wfree
  1413. #endif
  1414. ) {
  1415. atomic_sub(skb->truesize - 1, &skb->sk->sk_wmem_alloc);
  1416. skb->truesize = 1;
  1417. } else {
  1418. skb_orphan(skb);
  1419. }
  1420. }
  1421. EXPORT_SYMBOL(skb_orphan_partial);
  1422. /*
  1423. * Read buffer destructor automatically called from kfree_skb.
  1424. */
  1425. void sock_rfree(struct sk_buff *skb)
  1426. {
  1427. struct sock *sk = skb->sk;
  1428. unsigned int len = skb->truesize;
  1429. atomic_sub(len, &sk->sk_rmem_alloc);
  1430. sk_mem_uncharge(sk, len);
  1431. }
  1432. EXPORT_SYMBOL(sock_rfree);
  1433. /*
  1434. * Buffer destructor for skbs that are not used directly in read or write
  1435. * path, e.g. for error handler skbs. Automatically called from kfree_skb.
  1436. */
  1437. void sock_efree(struct sk_buff *skb)
  1438. {
  1439. sock_put(skb->sk);
  1440. }
  1441. EXPORT_SYMBOL(sock_efree);
  1442. kuid_t sock_i_uid(struct sock *sk)
  1443. {
  1444. kuid_t uid;
  1445. read_lock_bh(&sk->sk_callback_lock);
  1446. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
  1447. read_unlock_bh(&sk->sk_callback_lock);
  1448. return uid;
  1449. }
  1450. EXPORT_SYMBOL(sock_i_uid);
  1451. unsigned long sock_i_ino(struct sock *sk)
  1452. {
  1453. unsigned long ino;
  1454. read_lock_bh(&sk->sk_callback_lock);
  1455. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  1456. read_unlock_bh(&sk->sk_callback_lock);
  1457. return ino;
  1458. }
  1459. EXPORT_SYMBOL(sock_i_ino);
  1460. /*
  1461. * Allocate a skb from the socket's send buffer.
  1462. */
  1463. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  1464. gfp_t priority)
  1465. {
  1466. if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  1467. struct sk_buff *skb = alloc_skb(size, priority);
  1468. if (skb) {
  1469. skb_set_owner_w(skb, sk);
  1470. return skb;
  1471. }
  1472. }
  1473. return NULL;
  1474. }
  1475. EXPORT_SYMBOL(sock_wmalloc);
  1476. /*
  1477. * Allocate a memory block from the socket's option memory buffer.
  1478. */
  1479. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
  1480. {
  1481. if ((unsigned int)size <= sysctl_optmem_max &&
  1482. atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
  1483. void *mem;
  1484. /* First do the add, to avoid the race if kmalloc
  1485. * might sleep.
  1486. */
  1487. atomic_add(size, &sk->sk_omem_alloc);
  1488. mem = kmalloc(size, priority);
  1489. if (mem)
  1490. return mem;
  1491. atomic_sub(size, &sk->sk_omem_alloc);
  1492. }
  1493. return NULL;
  1494. }
  1495. EXPORT_SYMBOL(sock_kmalloc);
  1496. /* Free an option memory block. Note, we actually want the inline
  1497. * here as this allows gcc to detect the nullify and fold away the
  1498. * condition entirely.
  1499. */
  1500. static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
  1501. const bool nullify)
  1502. {
  1503. if (WARN_ON_ONCE(!mem))
  1504. return;
  1505. if (nullify)
  1506. kzfree(mem);
  1507. else
  1508. kfree(mem);
  1509. atomic_sub(size, &sk->sk_omem_alloc);
  1510. }
  1511. void sock_kfree_s(struct sock *sk, void *mem, int size)
  1512. {
  1513. __sock_kfree_s(sk, mem, size, false);
  1514. }
  1515. EXPORT_SYMBOL(sock_kfree_s);
  1516. void sock_kzfree_s(struct sock *sk, void *mem, int size)
  1517. {
  1518. __sock_kfree_s(sk, mem, size, true);
  1519. }
  1520. EXPORT_SYMBOL(sock_kzfree_s);
  1521. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  1522. I think, these locks should be removed for datagram sockets.
  1523. */
  1524. static long sock_wait_for_wmem(struct sock *sk, long timeo)
  1525. {
  1526. DEFINE_WAIT(wait);
  1527. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1528. for (;;) {
  1529. if (!timeo)
  1530. break;
  1531. if (signal_pending(current))
  1532. break;
  1533. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1534. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1535. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
  1536. break;
  1537. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1538. break;
  1539. if (sk->sk_err)
  1540. break;
  1541. timeo = schedule_timeout(timeo);
  1542. }
  1543. finish_wait(sk_sleep(sk), &wait);
  1544. return timeo;
  1545. }
  1546. /*
  1547. * Generic send/receive buffer handlers
  1548. */
  1549. struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
  1550. unsigned long data_len, int noblock,
  1551. int *errcode, int max_page_order)
  1552. {
  1553. struct sk_buff *skb;
  1554. long timeo;
  1555. int err;
  1556. timeo = sock_sndtimeo(sk, noblock);
  1557. for (;;) {
  1558. err = sock_error(sk);
  1559. if (err != 0)
  1560. goto failure;
  1561. err = -EPIPE;
  1562. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1563. goto failure;
  1564. if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf)
  1565. break;
  1566. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1567. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1568. err = -EAGAIN;
  1569. if (!timeo)
  1570. goto failure;
  1571. if (signal_pending(current))
  1572. goto interrupted;
  1573. timeo = sock_wait_for_wmem(sk, timeo);
  1574. }
  1575. skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
  1576. errcode, sk->sk_allocation);
  1577. if (skb)
  1578. skb_set_owner_w(skb, sk);
  1579. return skb;
  1580. interrupted:
  1581. err = sock_intr_errno(timeo);
  1582. failure:
  1583. *errcode = err;
  1584. return NULL;
  1585. }
  1586. EXPORT_SYMBOL(sock_alloc_send_pskb);
  1587. struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
  1588. int noblock, int *errcode)
  1589. {
  1590. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
  1591. }
  1592. EXPORT_SYMBOL(sock_alloc_send_skb);
  1593. /* On 32bit arches, an skb frag is limited to 2^15 */
  1594. #define SKB_FRAG_PAGE_ORDER get_order(32768)
  1595. /**
  1596. * skb_page_frag_refill - check that a page_frag contains enough room
  1597. * @sz: minimum size of the fragment we want to get
  1598. * @pfrag: pointer to page_frag
  1599. * @gfp: priority for memory allocation
  1600. *
  1601. * Note: While this allocator tries to use high order pages, there is
  1602. * no guarantee that allocations succeed. Therefore, @sz MUST be
  1603. * less or equal than PAGE_SIZE.
  1604. */
  1605. bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
  1606. {
  1607. if (pfrag->page) {
  1608. if (atomic_read(&pfrag->page->_count) == 1) {
  1609. pfrag->offset = 0;
  1610. return true;
  1611. }
  1612. if (pfrag->offset + sz <= pfrag->size)
  1613. return true;
  1614. put_page(pfrag->page);
  1615. }
  1616. pfrag->offset = 0;
  1617. if (SKB_FRAG_PAGE_ORDER) {
  1618. pfrag->page = alloc_pages((gfp & ~__GFP_WAIT) | __GFP_COMP |
  1619. __GFP_NOWARN | __GFP_NORETRY,
  1620. SKB_FRAG_PAGE_ORDER);
  1621. if (likely(pfrag->page)) {
  1622. pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
  1623. return true;
  1624. }
  1625. }
  1626. pfrag->page = alloc_page(gfp);
  1627. if (likely(pfrag->page)) {
  1628. pfrag->size = PAGE_SIZE;
  1629. return true;
  1630. }
  1631. return false;
  1632. }
  1633. EXPORT_SYMBOL(skb_page_frag_refill);
  1634. bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
  1635. {
  1636. if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
  1637. return true;
  1638. sk_enter_memory_pressure(sk);
  1639. sk_stream_moderate_sndbuf(sk);
  1640. return false;
  1641. }
  1642. EXPORT_SYMBOL(sk_page_frag_refill);
  1643. static void __lock_sock(struct sock *sk)
  1644. __releases(&sk->sk_lock.slock)
  1645. __acquires(&sk->sk_lock.slock)
  1646. {
  1647. DEFINE_WAIT(wait);
  1648. for (;;) {
  1649. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  1650. TASK_UNINTERRUPTIBLE);
  1651. spin_unlock_bh(&sk->sk_lock.slock);
  1652. schedule();
  1653. spin_lock_bh(&sk->sk_lock.slock);
  1654. if (!sock_owned_by_user(sk))
  1655. break;
  1656. }
  1657. finish_wait(&sk->sk_lock.wq, &wait);
  1658. }
  1659. static void __release_sock(struct sock *sk)
  1660. __releases(&sk->sk_lock.slock)
  1661. __acquires(&sk->sk_lock.slock)
  1662. {
  1663. struct sk_buff *skb = sk->sk_backlog.head;
  1664. do {
  1665. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  1666. bh_unlock_sock(sk);
  1667. do {
  1668. struct sk_buff *next = skb->next;
  1669. prefetch(next);
  1670. WARN_ON_ONCE(skb_dst_is_noref(skb));
  1671. skb->next = NULL;
  1672. sk_backlog_rcv(sk, skb);
  1673. /*
  1674. * We are in process context here with softirqs
  1675. * disabled, use cond_resched_softirq() to preempt.
  1676. * This is safe to do because we've taken the backlog
  1677. * queue private:
  1678. */
  1679. cond_resched_softirq();
  1680. skb = next;
  1681. } while (skb != NULL);
  1682. bh_lock_sock(sk);
  1683. } while ((skb = sk->sk_backlog.head) != NULL);
  1684. /*
  1685. * Doing the zeroing here guarantee we can not loop forever
  1686. * while a wild producer attempts to flood us.
  1687. */
  1688. sk->sk_backlog.len = 0;
  1689. }
  1690. /**
  1691. * sk_wait_data - wait for data to arrive at sk_receive_queue
  1692. * @sk: sock to wait on
  1693. * @timeo: for how long
  1694. *
  1695. * Now socket state including sk->sk_err is changed only under lock,
  1696. * hence we may omit checks after joining wait queue.
  1697. * We check receive queue before schedule() only as optimization;
  1698. * it is very likely that release_sock() added new data.
  1699. */
  1700. int sk_wait_data(struct sock *sk, long *timeo)
  1701. {
  1702. int rc;
  1703. DEFINE_WAIT(wait);
  1704. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1705. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1706. rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
  1707. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1708. finish_wait(sk_sleep(sk), &wait);
  1709. return rc;
  1710. }
  1711. EXPORT_SYMBOL(sk_wait_data);
  1712. /**
  1713. * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
  1714. * @sk: socket
  1715. * @size: memory size to allocate
  1716. * @kind: allocation type
  1717. *
  1718. * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
  1719. * rmem allocation. This function assumes that protocols which have
  1720. * memory_pressure use sk_wmem_queued as write buffer accounting.
  1721. */
  1722. int __sk_mem_schedule(struct sock *sk, int size, int kind)
  1723. {
  1724. struct proto *prot = sk->sk_prot;
  1725. int amt = sk_mem_pages(size);
  1726. long allocated;
  1727. int parent_status = UNDER_LIMIT;
  1728. sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
  1729. allocated = sk_memory_allocated_add(sk, amt, &parent_status);
  1730. /* Under limit. */
  1731. if (parent_status == UNDER_LIMIT &&
  1732. allocated <= sk_prot_mem_limits(sk, 0)) {
  1733. sk_leave_memory_pressure(sk);
  1734. return 1;
  1735. }
  1736. /* Under pressure. (we or our parents) */
  1737. if ((parent_status > SOFT_LIMIT) ||
  1738. allocated > sk_prot_mem_limits(sk, 1))
  1739. sk_enter_memory_pressure(sk);
  1740. /* Over hard limit (we or our parents) */
  1741. if ((parent_status == OVER_LIMIT) ||
  1742. (allocated > sk_prot_mem_limits(sk, 2)))
  1743. goto suppress_allocation;
  1744. /* guarantee minimum buffer size under pressure */
  1745. if (kind == SK_MEM_RECV) {
  1746. if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
  1747. return 1;
  1748. } else { /* SK_MEM_SEND */
  1749. if (sk->sk_type == SOCK_STREAM) {
  1750. if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
  1751. return 1;
  1752. } else if (atomic_read(&sk->sk_wmem_alloc) <
  1753. prot->sysctl_wmem[0])
  1754. return 1;
  1755. }
  1756. if (sk_has_memory_pressure(sk)) {
  1757. int alloc;
  1758. if (!sk_under_memory_pressure(sk))
  1759. return 1;
  1760. alloc = sk_sockets_allocated_read_positive(sk);
  1761. if (sk_prot_mem_limits(sk, 2) > alloc *
  1762. sk_mem_pages(sk->sk_wmem_queued +
  1763. atomic_read(&sk->sk_rmem_alloc) +
  1764. sk->sk_forward_alloc))
  1765. return 1;
  1766. }
  1767. suppress_allocation:
  1768. if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
  1769. sk_stream_moderate_sndbuf(sk);
  1770. /* Fail only if socket is _under_ its sndbuf.
  1771. * In this case we cannot block, so that we have to fail.
  1772. */
  1773. if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
  1774. return 1;
  1775. }
  1776. trace_sock_exceed_buf_limit(sk, prot, allocated);
  1777. /* Alas. Undo changes. */
  1778. sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
  1779. sk_memory_allocated_sub(sk, amt);
  1780. return 0;
  1781. }
  1782. EXPORT_SYMBOL(__sk_mem_schedule);
  1783. /**
  1784. * __sk_reclaim - reclaim memory_allocated
  1785. * @sk: socket
  1786. * @amount: number of bytes (rounded down to a SK_MEM_QUANTUM multiple)
  1787. */
  1788. void __sk_mem_reclaim(struct sock *sk, int amount)
  1789. {
  1790. amount >>= SK_MEM_QUANTUM_SHIFT;
  1791. sk_memory_allocated_sub(sk, amount);
  1792. sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
  1793. if (sk_under_memory_pressure(sk) &&
  1794. (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
  1795. sk_leave_memory_pressure(sk);
  1796. }
  1797. EXPORT_SYMBOL(__sk_mem_reclaim);
  1798. /*
  1799. * Set of default routines for initialising struct proto_ops when
  1800. * the protocol does not support a particular function. In certain
  1801. * cases where it makes no sense for a protocol to have a "do nothing"
  1802. * function, some default processing is provided.
  1803. */
  1804. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  1805. {
  1806. return -EOPNOTSUPP;
  1807. }
  1808. EXPORT_SYMBOL(sock_no_bind);
  1809. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  1810. int len, int flags)
  1811. {
  1812. return -EOPNOTSUPP;
  1813. }
  1814. EXPORT_SYMBOL(sock_no_connect);
  1815. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  1816. {
  1817. return -EOPNOTSUPP;
  1818. }
  1819. EXPORT_SYMBOL(sock_no_socketpair);
  1820. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
  1821. {
  1822. return -EOPNOTSUPP;
  1823. }
  1824. EXPORT_SYMBOL(sock_no_accept);
  1825. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  1826. int *len, int peer)
  1827. {
  1828. return -EOPNOTSUPP;
  1829. }
  1830. EXPORT_SYMBOL(sock_no_getname);
  1831. unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
  1832. {
  1833. return 0;
  1834. }
  1835. EXPORT_SYMBOL(sock_no_poll);
  1836. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1837. {
  1838. return -EOPNOTSUPP;
  1839. }
  1840. EXPORT_SYMBOL(sock_no_ioctl);
  1841. int sock_no_listen(struct socket *sock, int backlog)
  1842. {
  1843. return -EOPNOTSUPP;
  1844. }
  1845. EXPORT_SYMBOL(sock_no_listen);
  1846. int sock_no_shutdown(struct socket *sock, int how)
  1847. {
  1848. return -EOPNOTSUPP;
  1849. }
  1850. EXPORT_SYMBOL(sock_no_shutdown);
  1851. int sock_no_setsockopt(struct socket *sock, int level, int optname,
  1852. char __user *optval, unsigned int optlen)
  1853. {
  1854. return -EOPNOTSUPP;
  1855. }
  1856. EXPORT_SYMBOL(sock_no_setsockopt);
  1857. int sock_no_getsockopt(struct socket *sock, int level, int optname,
  1858. char __user *optval, int __user *optlen)
  1859. {
  1860. return -EOPNOTSUPP;
  1861. }
  1862. EXPORT_SYMBOL(sock_no_getsockopt);
  1863. int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  1864. {
  1865. return -EOPNOTSUPP;
  1866. }
  1867. EXPORT_SYMBOL(sock_no_sendmsg);
  1868. int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
  1869. int flags)
  1870. {
  1871. return -EOPNOTSUPP;
  1872. }
  1873. EXPORT_SYMBOL(sock_no_recvmsg);
  1874. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  1875. {
  1876. /* Mirror missing mmap method error code */
  1877. return -ENODEV;
  1878. }
  1879. EXPORT_SYMBOL(sock_no_mmap);
  1880. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  1881. {
  1882. ssize_t res;
  1883. struct msghdr msg = {.msg_flags = flags};
  1884. struct kvec iov;
  1885. char *kaddr = kmap(page);
  1886. iov.iov_base = kaddr + offset;
  1887. iov.iov_len = size;
  1888. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  1889. kunmap(page);
  1890. return res;
  1891. }
  1892. EXPORT_SYMBOL(sock_no_sendpage);
  1893. /*
  1894. * Default Socket Callbacks
  1895. */
  1896. static void sock_def_wakeup(struct sock *sk)
  1897. {
  1898. struct socket_wq *wq;
  1899. rcu_read_lock();
  1900. wq = rcu_dereference(sk->sk_wq);
  1901. if (wq_has_sleeper(wq))
  1902. wake_up_interruptible_all(&wq->wait);
  1903. rcu_read_unlock();
  1904. }
  1905. static void sock_def_error_report(struct sock *sk)
  1906. {
  1907. struct socket_wq *wq;
  1908. rcu_read_lock();
  1909. wq = rcu_dereference(sk->sk_wq);
  1910. if (wq_has_sleeper(wq))
  1911. wake_up_interruptible_poll(&wq->wait, POLLERR);
  1912. sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
  1913. rcu_read_unlock();
  1914. }
  1915. static void sock_def_readable(struct sock *sk)
  1916. {
  1917. struct socket_wq *wq;
  1918. rcu_read_lock();
  1919. wq = rcu_dereference(sk->sk_wq);
  1920. if (wq_has_sleeper(wq))
  1921. wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
  1922. POLLRDNORM | POLLRDBAND);
  1923. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  1924. rcu_read_unlock();
  1925. }
  1926. static void sock_def_write_space(struct sock *sk)
  1927. {
  1928. struct socket_wq *wq;
  1929. rcu_read_lock();
  1930. /* Do not wake up a writer until he can make "significant"
  1931. * progress. --DaveM
  1932. */
  1933. if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
  1934. wq = rcu_dereference(sk->sk_wq);
  1935. if (wq_has_sleeper(wq))
  1936. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1937. POLLWRNORM | POLLWRBAND);
  1938. /* Should agree with poll, otherwise some programs break */
  1939. if (sock_writeable(sk))
  1940. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  1941. }
  1942. rcu_read_unlock();
  1943. }
  1944. static void sock_def_destruct(struct sock *sk)
  1945. {
  1946. }
  1947. void sk_send_sigurg(struct sock *sk)
  1948. {
  1949. if (sk->sk_socket && sk->sk_socket->file)
  1950. if (send_sigurg(&sk->sk_socket->file->f_owner))
  1951. sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
  1952. }
  1953. EXPORT_SYMBOL(sk_send_sigurg);
  1954. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  1955. unsigned long expires)
  1956. {
  1957. if (!mod_timer(timer, expires))
  1958. sock_hold(sk);
  1959. }
  1960. EXPORT_SYMBOL(sk_reset_timer);
  1961. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  1962. {
  1963. if (del_timer(timer))
  1964. __sock_put(sk);
  1965. }
  1966. EXPORT_SYMBOL(sk_stop_timer);
  1967. void sock_init_data(struct socket *sock, struct sock *sk)
  1968. {
  1969. skb_queue_head_init(&sk->sk_receive_queue);
  1970. skb_queue_head_init(&sk->sk_write_queue);
  1971. skb_queue_head_init(&sk->sk_error_queue);
  1972. sk->sk_send_head = NULL;
  1973. init_timer(&sk->sk_timer);
  1974. sk->sk_allocation = GFP_KERNEL;
  1975. sk->sk_rcvbuf = sysctl_rmem_default;
  1976. sk->sk_sndbuf = sysctl_wmem_default;
  1977. sk->sk_state = TCP_CLOSE;
  1978. sk_set_socket(sk, sock);
  1979. sock_set_flag(sk, SOCK_ZAPPED);
  1980. if (sock) {
  1981. sk->sk_type = sock->type;
  1982. sk->sk_wq = sock->wq;
  1983. sock->sk = sk;
  1984. } else
  1985. sk->sk_wq = NULL;
  1986. spin_lock_init(&sk->sk_dst_lock);
  1987. rwlock_init(&sk->sk_callback_lock);
  1988. lockdep_set_class_and_name(&sk->sk_callback_lock,
  1989. af_callback_keys + sk->sk_family,
  1990. af_family_clock_key_strings[sk->sk_family]);
  1991. sk->sk_state_change = sock_def_wakeup;
  1992. sk->sk_data_ready = sock_def_readable;
  1993. sk->sk_write_space = sock_def_write_space;
  1994. sk->sk_error_report = sock_def_error_report;
  1995. sk->sk_destruct = sock_def_destruct;
  1996. sk->sk_frag.page = NULL;
  1997. sk->sk_frag.offset = 0;
  1998. sk->sk_peek_off = -1;
  1999. sk->sk_peer_pid = NULL;
  2000. sk->sk_peer_cred = NULL;
  2001. sk->sk_write_pending = 0;
  2002. sk->sk_rcvlowat = 1;
  2003. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  2004. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  2005. sk->sk_stamp = ktime_set(-1L, 0);
  2006. #ifdef CONFIG_NET_RX_BUSY_POLL
  2007. sk->sk_napi_id = 0;
  2008. sk->sk_ll_usec = sysctl_net_busy_read;
  2009. #endif
  2010. sk->sk_max_pacing_rate = ~0U;
  2011. sk->sk_pacing_rate = ~0U;
  2012. /*
  2013. * Before updating sk_refcnt, we must commit prior changes to memory
  2014. * (Documentation/RCU/rculist_nulls.txt for details)
  2015. */
  2016. smp_wmb();
  2017. atomic_set(&sk->sk_refcnt, 1);
  2018. atomic_set(&sk->sk_drops, 0);
  2019. }
  2020. EXPORT_SYMBOL(sock_init_data);
  2021. void lock_sock_nested(struct sock *sk, int subclass)
  2022. {
  2023. might_sleep();
  2024. spin_lock_bh(&sk->sk_lock.slock);
  2025. if (sk->sk_lock.owned)
  2026. __lock_sock(sk);
  2027. sk->sk_lock.owned = 1;
  2028. spin_unlock(&sk->sk_lock.slock);
  2029. /*
  2030. * The sk_lock has mutex_lock() semantics here:
  2031. */
  2032. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  2033. local_bh_enable();
  2034. }
  2035. EXPORT_SYMBOL(lock_sock_nested);
  2036. void release_sock(struct sock *sk)
  2037. {
  2038. /*
  2039. * The sk_lock has mutex_unlock() semantics:
  2040. */
  2041. mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
  2042. spin_lock_bh(&sk->sk_lock.slock);
  2043. if (sk->sk_backlog.tail)
  2044. __release_sock(sk);
  2045. /* Warning : release_cb() might need to release sk ownership,
  2046. * ie call sock_release_ownership(sk) before us.
  2047. */
  2048. if (sk->sk_prot->release_cb)
  2049. sk->sk_prot->release_cb(sk);
  2050. sock_release_ownership(sk);
  2051. if (waitqueue_active(&sk->sk_lock.wq))
  2052. wake_up(&sk->sk_lock.wq);
  2053. spin_unlock_bh(&sk->sk_lock.slock);
  2054. }
  2055. EXPORT_SYMBOL(release_sock);
  2056. /**
  2057. * lock_sock_fast - fast version of lock_sock
  2058. * @sk: socket
  2059. *
  2060. * This version should be used for very small section, where process wont block
  2061. * return false if fast path is taken
  2062. * sk_lock.slock locked, owned = 0, BH disabled
  2063. * return true if slow path is taken
  2064. * sk_lock.slock unlocked, owned = 1, BH enabled
  2065. */
  2066. bool lock_sock_fast(struct sock *sk)
  2067. {
  2068. might_sleep();
  2069. spin_lock_bh(&sk->sk_lock.slock);
  2070. if (!sk->sk_lock.owned)
  2071. /*
  2072. * Note : We must disable BH
  2073. */
  2074. return false;
  2075. __lock_sock(sk);
  2076. sk->sk_lock.owned = 1;
  2077. spin_unlock(&sk->sk_lock.slock);
  2078. /*
  2079. * The sk_lock has mutex_lock() semantics here:
  2080. */
  2081. mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
  2082. local_bh_enable();
  2083. return true;
  2084. }
  2085. EXPORT_SYMBOL(lock_sock_fast);
  2086. int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  2087. {
  2088. struct timeval tv;
  2089. if (!sock_flag(sk, SOCK_TIMESTAMP))
  2090. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  2091. tv = ktime_to_timeval(sk->sk_stamp);
  2092. if (tv.tv_sec == -1)
  2093. return -ENOENT;
  2094. if (tv.tv_sec == 0) {
  2095. sk->sk_stamp = ktime_get_real();
  2096. tv = ktime_to_timeval(sk->sk_stamp);
  2097. }
  2098. return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
  2099. }
  2100. EXPORT_SYMBOL(sock_get_timestamp);
  2101. int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  2102. {
  2103. struct timespec ts;
  2104. if (!sock_flag(sk, SOCK_TIMESTAMP))
  2105. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  2106. ts = ktime_to_timespec(sk->sk_stamp);
  2107. if (ts.tv_sec == -1)
  2108. return -ENOENT;
  2109. if (ts.tv_sec == 0) {
  2110. sk->sk_stamp = ktime_get_real();
  2111. ts = ktime_to_timespec(sk->sk_stamp);
  2112. }
  2113. return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
  2114. }
  2115. EXPORT_SYMBOL(sock_get_timestampns);
  2116. void sock_enable_timestamp(struct sock *sk, int flag)
  2117. {
  2118. if (!sock_flag(sk, flag)) {
  2119. unsigned long previous_flags = sk->sk_flags;
  2120. sock_set_flag(sk, flag);
  2121. /*
  2122. * we just set one of the two flags which require net
  2123. * time stamping, but time stamping might have been on
  2124. * already because of the other one
  2125. */
  2126. if (!(previous_flags & SK_FLAGS_TIMESTAMP))
  2127. net_enable_timestamp();
  2128. }
  2129. }
  2130. int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
  2131. int level, int type)
  2132. {
  2133. struct sock_exterr_skb *serr;
  2134. struct sk_buff *skb;
  2135. int copied, err;
  2136. err = -EAGAIN;
  2137. skb = sock_dequeue_err_skb(sk);
  2138. if (skb == NULL)
  2139. goto out;
  2140. copied = skb->len;
  2141. if (copied > len) {
  2142. msg->msg_flags |= MSG_TRUNC;
  2143. copied = len;
  2144. }
  2145. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2146. if (err)
  2147. goto out_free_skb;
  2148. sock_recv_timestamp(msg, sk, skb);
  2149. serr = SKB_EXT_ERR(skb);
  2150. put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
  2151. msg->msg_flags |= MSG_ERRQUEUE;
  2152. err = copied;
  2153. out_free_skb:
  2154. kfree_skb(skb);
  2155. out:
  2156. return err;
  2157. }
  2158. EXPORT_SYMBOL(sock_recv_errqueue);
  2159. /*
  2160. * Get a socket option on an socket.
  2161. *
  2162. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  2163. * asynchronous errors should be reported by getsockopt. We assume
  2164. * this means if you specify SO_ERROR (otherwise whats the point of it).
  2165. */
  2166. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  2167. char __user *optval, int __user *optlen)
  2168. {
  2169. struct sock *sk = sock->sk;
  2170. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  2171. }
  2172. EXPORT_SYMBOL(sock_common_getsockopt);
  2173. #ifdef CONFIG_COMPAT
  2174. int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
  2175. char __user *optval, int __user *optlen)
  2176. {
  2177. struct sock *sk = sock->sk;
  2178. if (sk->sk_prot->compat_getsockopt != NULL)
  2179. return sk->sk_prot->compat_getsockopt(sk, level, optname,
  2180. optval, optlen);
  2181. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  2182. }
  2183. EXPORT_SYMBOL(compat_sock_common_getsockopt);
  2184. #endif
  2185. int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  2186. int flags)
  2187. {
  2188. struct sock *sk = sock->sk;
  2189. int addr_len = 0;
  2190. int err;
  2191. err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
  2192. flags & ~MSG_DONTWAIT, &addr_len);
  2193. if (err >= 0)
  2194. msg->msg_namelen = addr_len;
  2195. return err;
  2196. }
  2197. EXPORT_SYMBOL(sock_common_recvmsg);
  2198. /*
  2199. * Set socket options on an inet socket.
  2200. */
  2201. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  2202. char __user *optval, unsigned int optlen)
  2203. {
  2204. struct sock *sk = sock->sk;
  2205. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  2206. }
  2207. EXPORT_SYMBOL(sock_common_setsockopt);
  2208. #ifdef CONFIG_COMPAT
  2209. int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
  2210. char __user *optval, unsigned int optlen)
  2211. {
  2212. struct sock *sk = sock->sk;
  2213. if (sk->sk_prot->compat_setsockopt != NULL)
  2214. return sk->sk_prot->compat_setsockopt(sk, level, optname,
  2215. optval, optlen);
  2216. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  2217. }
  2218. EXPORT_SYMBOL(compat_sock_common_setsockopt);
  2219. #endif
  2220. void sk_common_release(struct sock *sk)
  2221. {
  2222. if (sk->sk_prot->destroy)
  2223. sk->sk_prot->destroy(sk);
  2224. /*
  2225. * Observation: when sock_common_release is called, processes have
  2226. * no access to socket. But net still has.
  2227. * Step one, detach it from networking:
  2228. *
  2229. * A. Remove from hash tables.
  2230. */
  2231. sk->sk_prot->unhash(sk);
  2232. /*
  2233. * In this point socket cannot receive new packets, but it is possible
  2234. * that some packets are in flight because some CPU runs receiver and
  2235. * did hash table lookup before we unhashed socket. They will achieve
  2236. * receive queue and will be purged by socket destructor.
  2237. *
  2238. * Also we still have packets pending on receive queue and probably,
  2239. * our own packets waiting in device queues. sock_destroy will drain
  2240. * receive queue, but transmitted packets will delay socket destruction
  2241. * until the last reference will be released.
  2242. */
  2243. sock_orphan(sk);
  2244. xfrm_sk_free_policy(sk);
  2245. sk_refcnt_debug_release(sk);
  2246. if (sk->sk_frag.page) {
  2247. put_page(sk->sk_frag.page);
  2248. sk->sk_frag.page = NULL;
  2249. }
  2250. sock_put(sk);
  2251. }
  2252. EXPORT_SYMBOL(sk_common_release);
  2253. #ifdef CONFIG_PROC_FS
  2254. #define PROTO_INUSE_NR 64 /* should be enough for the first time */
  2255. struct prot_inuse {
  2256. int val[PROTO_INUSE_NR];
  2257. };
  2258. static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
  2259. #ifdef CONFIG_NET_NS
  2260. void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
  2261. {
  2262. __this_cpu_add(net->core.inuse->val[prot->inuse_idx], val);
  2263. }
  2264. EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
  2265. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  2266. {
  2267. int cpu, idx = prot->inuse_idx;
  2268. int res = 0;
  2269. for_each_possible_cpu(cpu)
  2270. res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
  2271. return res >= 0 ? res : 0;
  2272. }
  2273. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  2274. static int __net_init sock_inuse_init_net(struct net *net)
  2275. {
  2276. net->core.inuse = alloc_percpu(struct prot_inuse);
  2277. return net->core.inuse ? 0 : -ENOMEM;
  2278. }
  2279. static void __net_exit sock_inuse_exit_net(struct net *net)
  2280. {
  2281. free_percpu(net->core.inuse);
  2282. }
  2283. static struct pernet_operations net_inuse_ops = {
  2284. .init = sock_inuse_init_net,
  2285. .exit = sock_inuse_exit_net,
  2286. };
  2287. static __init int net_inuse_init(void)
  2288. {
  2289. if (register_pernet_subsys(&net_inuse_ops))
  2290. panic("Cannot initialize net inuse counters");
  2291. return 0;
  2292. }
  2293. core_initcall(net_inuse_init);
  2294. #else
  2295. static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
  2296. void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
  2297. {
  2298. __this_cpu_add(prot_inuse.val[prot->inuse_idx], val);
  2299. }
  2300. EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
  2301. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  2302. {
  2303. int cpu, idx = prot->inuse_idx;
  2304. int res = 0;
  2305. for_each_possible_cpu(cpu)
  2306. res += per_cpu(prot_inuse, cpu).val[idx];
  2307. return res >= 0 ? res : 0;
  2308. }
  2309. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  2310. #endif
  2311. static void assign_proto_idx(struct proto *prot)
  2312. {
  2313. prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
  2314. if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
  2315. pr_err("PROTO_INUSE_NR exhausted\n");
  2316. return;
  2317. }
  2318. set_bit(prot->inuse_idx, proto_inuse_idx);
  2319. }
  2320. static void release_proto_idx(struct proto *prot)
  2321. {
  2322. if (prot->inuse_idx != PROTO_INUSE_NR - 1)
  2323. clear_bit(prot->inuse_idx, proto_inuse_idx);
  2324. }
  2325. #else
  2326. static inline void assign_proto_idx(struct proto *prot)
  2327. {
  2328. }
  2329. static inline void release_proto_idx(struct proto *prot)
  2330. {
  2331. }
  2332. #endif
  2333. static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
  2334. {
  2335. if (!rsk_prot)
  2336. return;
  2337. kfree(rsk_prot->slab_name);
  2338. rsk_prot->slab_name = NULL;
  2339. if (rsk_prot->slab) {
  2340. kmem_cache_destroy(rsk_prot->slab);
  2341. rsk_prot->slab = NULL;
  2342. }
  2343. }
  2344. static int req_prot_init(const struct proto *prot)
  2345. {
  2346. struct request_sock_ops *rsk_prot = prot->rsk_prot;
  2347. if (!rsk_prot)
  2348. return 0;
  2349. rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
  2350. prot->name);
  2351. if (!rsk_prot->slab_name)
  2352. return -ENOMEM;
  2353. rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
  2354. rsk_prot->obj_size, 0,
  2355. 0, NULL);
  2356. if (!rsk_prot->slab) {
  2357. pr_crit("%s: Can't create request sock SLAB cache!\n",
  2358. prot->name);
  2359. return -ENOMEM;
  2360. }
  2361. return 0;
  2362. }
  2363. int proto_register(struct proto *prot, int alloc_slab)
  2364. {
  2365. if (alloc_slab) {
  2366. prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
  2367. SLAB_HWCACHE_ALIGN | prot->slab_flags,
  2368. NULL);
  2369. if (prot->slab == NULL) {
  2370. pr_crit("%s: Can't create sock SLAB cache!\n",
  2371. prot->name);
  2372. goto out;
  2373. }
  2374. if (req_prot_init(prot))
  2375. goto out_free_request_sock_slab;
  2376. if (prot->twsk_prot != NULL) {
  2377. prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
  2378. if (prot->twsk_prot->twsk_slab_name == NULL)
  2379. goto out_free_request_sock_slab;
  2380. prot->twsk_prot->twsk_slab =
  2381. kmem_cache_create(prot->twsk_prot->twsk_slab_name,
  2382. prot->twsk_prot->twsk_obj_size,
  2383. 0,
  2384. prot->slab_flags,
  2385. NULL);
  2386. if (prot->twsk_prot->twsk_slab == NULL)
  2387. goto out_free_timewait_sock_slab_name;
  2388. }
  2389. }
  2390. mutex_lock(&proto_list_mutex);
  2391. list_add(&prot->node, &proto_list);
  2392. assign_proto_idx(prot);
  2393. mutex_unlock(&proto_list_mutex);
  2394. return 0;
  2395. out_free_timewait_sock_slab_name:
  2396. kfree(prot->twsk_prot->twsk_slab_name);
  2397. out_free_request_sock_slab:
  2398. req_prot_cleanup(prot->rsk_prot);
  2399. kmem_cache_destroy(prot->slab);
  2400. prot->slab = NULL;
  2401. out:
  2402. return -ENOBUFS;
  2403. }
  2404. EXPORT_SYMBOL(proto_register);
  2405. void proto_unregister(struct proto *prot)
  2406. {
  2407. mutex_lock(&proto_list_mutex);
  2408. release_proto_idx(prot);
  2409. list_del(&prot->node);
  2410. mutex_unlock(&proto_list_mutex);
  2411. if (prot->slab != NULL) {
  2412. kmem_cache_destroy(prot->slab);
  2413. prot->slab = NULL;
  2414. }
  2415. req_prot_cleanup(prot->rsk_prot);
  2416. if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
  2417. kmem_cache_destroy(prot->twsk_prot->twsk_slab);
  2418. kfree(prot->twsk_prot->twsk_slab_name);
  2419. prot->twsk_prot->twsk_slab = NULL;
  2420. }
  2421. }
  2422. EXPORT_SYMBOL(proto_unregister);
  2423. #ifdef CONFIG_PROC_FS
  2424. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  2425. __acquires(proto_list_mutex)
  2426. {
  2427. mutex_lock(&proto_list_mutex);
  2428. return seq_list_start_head(&proto_list, *pos);
  2429. }
  2430. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2431. {
  2432. return seq_list_next(v, &proto_list, pos);
  2433. }
  2434. static void proto_seq_stop(struct seq_file *seq, void *v)
  2435. __releases(proto_list_mutex)
  2436. {
  2437. mutex_unlock(&proto_list_mutex);
  2438. }
  2439. static char proto_method_implemented(const void *method)
  2440. {
  2441. return method == NULL ? 'n' : 'y';
  2442. }
  2443. static long sock_prot_memory_allocated(struct proto *proto)
  2444. {
  2445. return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
  2446. }
  2447. static char *sock_prot_memory_pressure(struct proto *proto)
  2448. {
  2449. return proto->memory_pressure != NULL ?
  2450. proto_memory_pressure(proto) ? "yes" : "no" : "NI";
  2451. }
  2452. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  2453. {
  2454. seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
  2455. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  2456. proto->name,
  2457. proto->obj_size,
  2458. sock_prot_inuse_get(seq_file_net(seq), proto),
  2459. sock_prot_memory_allocated(proto),
  2460. sock_prot_memory_pressure(proto),
  2461. proto->max_header,
  2462. proto->slab == NULL ? "no" : "yes",
  2463. module_name(proto->owner),
  2464. proto_method_implemented(proto->close),
  2465. proto_method_implemented(proto->connect),
  2466. proto_method_implemented(proto->disconnect),
  2467. proto_method_implemented(proto->accept),
  2468. proto_method_implemented(proto->ioctl),
  2469. proto_method_implemented(proto->init),
  2470. proto_method_implemented(proto->destroy),
  2471. proto_method_implemented(proto->shutdown),
  2472. proto_method_implemented(proto->setsockopt),
  2473. proto_method_implemented(proto->getsockopt),
  2474. proto_method_implemented(proto->sendmsg),
  2475. proto_method_implemented(proto->recvmsg),
  2476. proto_method_implemented(proto->sendpage),
  2477. proto_method_implemented(proto->bind),
  2478. proto_method_implemented(proto->backlog_rcv),
  2479. proto_method_implemented(proto->hash),
  2480. proto_method_implemented(proto->unhash),
  2481. proto_method_implemented(proto->get_port),
  2482. proto_method_implemented(proto->enter_memory_pressure));
  2483. }
  2484. static int proto_seq_show(struct seq_file *seq, void *v)
  2485. {
  2486. if (v == &proto_list)
  2487. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  2488. "protocol",
  2489. "size",
  2490. "sockets",
  2491. "memory",
  2492. "press",
  2493. "maxhdr",
  2494. "slab",
  2495. "module",
  2496. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  2497. else
  2498. proto_seq_printf(seq, list_entry(v, struct proto, node));
  2499. return 0;
  2500. }
  2501. static const struct seq_operations proto_seq_ops = {
  2502. .start = proto_seq_start,
  2503. .next = proto_seq_next,
  2504. .stop = proto_seq_stop,
  2505. .show = proto_seq_show,
  2506. };
  2507. static int proto_seq_open(struct inode *inode, struct file *file)
  2508. {
  2509. return seq_open_net(inode, file, &proto_seq_ops,
  2510. sizeof(struct seq_net_private));
  2511. }
  2512. static const struct file_operations proto_seq_fops = {
  2513. .owner = THIS_MODULE,
  2514. .open = proto_seq_open,
  2515. .read = seq_read,
  2516. .llseek = seq_lseek,
  2517. .release = seq_release_net,
  2518. };
  2519. static __net_init int proto_init_net(struct net *net)
  2520. {
  2521. if (!proc_create("protocols", S_IRUGO, net->proc_net, &proto_seq_fops))
  2522. return -ENOMEM;
  2523. return 0;
  2524. }
  2525. static __net_exit void proto_exit_net(struct net *net)
  2526. {
  2527. remove_proc_entry("protocols", net->proc_net);
  2528. }
  2529. static __net_initdata struct pernet_operations proto_net_ops = {
  2530. .init = proto_init_net,
  2531. .exit = proto_exit_net,
  2532. };
  2533. static int __init proto_init(void)
  2534. {
  2535. return register_pernet_subsys(&proto_net_ops);
  2536. }
  2537. subsys_initcall(proto_init);
  2538. #endif /* PROC_FS */