sock.c 67 KB

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