sock.c 67 KB

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