sock.c 66 KB

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