sock.c 59 KB

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