sock.c 72 KB

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