sock.c 74 KB

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