sock.c 67 KB

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