sock.c 56 KB

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