sock.c 54 KB

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