sock.c 74 KB

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