sock.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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/config.h>
  95. #include <linux/errno.h>
  96. #include <linux/types.h>
  97. #include <linux/socket.h>
  98. #include <linux/in.h>
  99. #include <linux/kernel.h>
  100. #include <linux/module.h>
  101. #include <linux/proc_fs.h>
  102. #include <linux/seq_file.h>
  103. #include <linux/sched.h>
  104. #include <linux/timer.h>
  105. #include <linux/string.h>
  106. #include <linux/sockios.h>
  107. #include <linux/net.h>
  108. #include <linux/mm.h>
  109. #include <linux/slab.h>
  110. #include <linux/interrupt.h>
  111. #include <linux/poll.h>
  112. #include <linux/tcp.h>
  113. #include <linux/init.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/request_sock.h>
  120. #include <net/sock.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. /* Take into consideration the size of the struct sk_buff overhead in the
  128. * determination of these values, since that is non-constant across
  129. * platforms. This makes socket queueing behavior and performance
  130. * not depend upon such differences.
  131. */
  132. #define _SK_MEM_PACKETS 256
  133. #define _SK_MEM_OVERHEAD (sizeof(struct sk_buff) + 256)
  134. #define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  135. #define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  136. /* Run time adjustable parameters. */
  137. __u32 sysctl_wmem_max = SK_WMEM_MAX;
  138. __u32 sysctl_rmem_max = SK_RMEM_MAX;
  139. __u32 sysctl_wmem_default = SK_WMEM_MAX;
  140. __u32 sysctl_rmem_default = SK_RMEM_MAX;
  141. /* Maximal space eaten by iovec or ancilliary data plus some space */
  142. int sysctl_optmem_max = sizeof(unsigned long)*(2*UIO_MAXIOV + 512);
  143. static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
  144. {
  145. struct timeval tv;
  146. if (optlen < sizeof(tv))
  147. return -EINVAL;
  148. if (copy_from_user(&tv, optval, sizeof(tv)))
  149. return -EFAULT;
  150. *timeo_p = MAX_SCHEDULE_TIMEOUT;
  151. if (tv.tv_sec == 0 && tv.tv_usec == 0)
  152. return 0;
  153. if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
  154. *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
  155. return 0;
  156. }
  157. static void sock_warn_obsolete_bsdism(const char *name)
  158. {
  159. static int warned;
  160. static char warncomm[TASK_COMM_LEN];
  161. if (strcmp(warncomm, current->comm) && warned < 5) {
  162. strcpy(warncomm, current->comm);
  163. printk(KERN_WARNING "process `%s' is using obsolete "
  164. "%s SO_BSDCOMPAT\n", warncomm, name);
  165. warned++;
  166. }
  167. }
  168. static void sock_disable_timestamp(struct sock *sk)
  169. {
  170. if (sock_flag(sk, SOCK_TIMESTAMP)) {
  171. sock_reset_flag(sk, SOCK_TIMESTAMP);
  172. net_disable_timestamp();
  173. }
  174. }
  175. /*
  176. * This is meant for all protocols to use and covers goings on
  177. * at the socket level. Everything here is generic.
  178. */
  179. int sock_setsockopt(struct socket *sock, int level, int optname,
  180. char __user *optval, int optlen)
  181. {
  182. struct sock *sk=sock->sk;
  183. struct sk_filter *filter;
  184. int val;
  185. int valbool;
  186. struct linger ling;
  187. int ret = 0;
  188. /*
  189. * Options without arguments
  190. */
  191. #ifdef SO_DONTLINGER /* Compatibility item... */
  192. if (optname == SO_DONTLINGER) {
  193. lock_sock(sk);
  194. sock_reset_flag(sk, SOCK_LINGER);
  195. release_sock(sk);
  196. return 0;
  197. }
  198. #endif
  199. if(optlen<sizeof(int))
  200. return(-EINVAL);
  201. if (get_user(val, (int __user *)optval))
  202. return -EFAULT;
  203. valbool = val?1:0;
  204. lock_sock(sk);
  205. switch(optname)
  206. {
  207. case SO_DEBUG:
  208. if(val && !capable(CAP_NET_ADMIN))
  209. {
  210. ret = -EACCES;
  211. }
  212. else if (valbool)
  213. sock_set_flag(sk, SOCK_DBG);
  214. else
  215. sock_reset_flag(sk, SOCK_DBG);
  216. break;
  217. case SO_REUSEADDR:
  218. sk->sk_reuse = valbool;
  219. break;
  220. case SO_TYPE:
  221. case SO_ERROR:
  222. ret = -ENOPROTOOPT;
  223. break;
  224. case SO_DONTROUTE:
  225. if (valbool)
  226. sock_set_flag(sk, SOCK_LOCALROUTE);
  227. else
  228. sock_reset_flag(sk, SOCK_LOCALROUTE);
  229. break;
  230. case SO_BROADCAST:
  231. sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
  232. break;
  233. case SO_SNDBUF:
  234. /* Don't error on this BSD doesn't and if you think
  235. about it this is right. Otherwise apps have to
  236. play 'guess the biggest size' games. RCVBUF/SNDBUF
  237. are treated in BSD as hints */
  238. if (val > sysctl_wmem_max)
  239. val = sysctl_wmem_max;
  240. set_sndbuf:
  241. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  242. if ((val * 2) < SOCK_MIN_SNDBUF)
  243. sk->sk_sndbuf = SOCK_MIN_SNDBUF;
  244. else
  245. sk->sk_sndbuf = val * 2;
  246. /*
  247. * Wake up sending tasks if we
  248. * upped the value.
  249. */
  250. sk->sk_write_space(sk);
  251. break;
  252. case SO_SNDBUFFORCE:
  253. if (!capable(CAP_NET_ADMIN)) {
  254. ret = -EPERM;
  255. break;
  256. }
  257. goto set_sndbuf;
  258. case SO_RCVBUF:
  259. /* Don't error on this BSD doesn't and if you think
  260. about it this is right. Otherwise apps have to
  261. play 'guess the biggest size' games. RCVBUF/SNDBUF
  262. are treated in BSD as hints */
  263. if (val > sysctl_rmem_max)
  264. val = sysctl_rmem_max;
  265. set_rcvbuf:
  266. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  267. /* FIXME: is this lower bound the right one? */
  268. if ((val * 2) < SOCK_MIN_RCVBUF)
  269. sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
  270. else
  271. sk->sk_rcvbuf = val * 2;
  272. break;
  273. case SO_RCVBUFFORCE:
  274. if (!capable(CAP_NET_ADMIN)) {
  275. ret = -EPERM;
  276. break;
  277. }
  278. goto set_rcvbuf;
  279. case SO_KEEPALIVE:
  280. #ifdef CONFIG_INET
  281. if (sk->sk_protocol == IPPROTO_TCP)
  282. tcp_set_keepalive(sk, valbool);
  283. #endif
  284. sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
  285. break;
  286. case SO_OOBINLINE:
  287. sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
  288. break;
  289. case SO_NO_CHECK:
  290. sk->sk_no_check = valbool;
  291. break;
  292. case SO_PRIORITY:
  293. if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
  294. sk->sk_priority = val;
  295. else
  296. ret = -EPERM;
  297. break;
  298. case SO_LINGER:
  299. if(optlen<sizeof(ling)) {
  300. ret = -EINVAL; /* 1003.1g */
  301. break;
  302. }
  303. if (copy_from_user(&ling,optval,sizeof(ling))) {
  304. ret = -EFAULT;
  305. break;
  306. }
  307. if (!ling.l_onoff)
  308. sock_reset_flag(sk, SOCK_LINGER);
  309. else {
  310. #if (BITS_PER_LONG == 32)
  311. if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
  312. sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
  313. else
  314. #endif
  315. sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
  316. sock_set_flag(sk, SOCK_LINGER);
  317. }
  318. break;
  319. case SO_BSDCOMPAT:
  320. sock_warn_obsolete_bsdism("setsockopt");
  321. break;
  322. case SO_PASSCRED:
  323. if (valbool)
  324. set_bit(SOCK_PASSCRED, &sock->flags);
  325. else
  326. clear_bit(SOCK_PASSCRED, &sock->flags);
  327. break;
  328. case SO_TIMESTAMP:
  329. if (valbool) {
  330. sock_set_flag(sk, SOCK_RCVTSTAMP);
  331. sock_enable_timestamp(sk);
  332. } else
  333. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  334. break;
  335. case SO_RCVLOWAT:
  336. if (val < 0)
  337. val = INT_MAX;
  338. sk->sk_rcvlowat = val ? : 1;
  339. break;
  340. case SO_RCVTIMEO:
  341. ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
  342. break;
  343. case SO_SNDTIMEO:
  344. ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
  345. break;
  346. #ifdef CONFIG_NETDEVICES
  347. case SO_BINDTODEVICE:
  348. {
  349. char devname[IFNAMSIZ];
  350. /* Sorry... */
  351. if (!capable(CAP_NET_RAW)) {
  352. ret = -EPERM;
  353. break;
  354. }
  355. /* Bind this socket to a particular device like "eth0",
  356. * as specified in the passed interface name. If the
  357. * name is "" or the option length is zero the socket
  358. * is not bound.
  359. */
  360. if (!valbool) {
  361. sk->sk_bound_dev_if = 0;
  362. } else {
  363. if (optlen > IFNAMSIZ - 1)
  364. optlen = IFNAMSIZ - 1;
  365. memset(devname, 0, sizeof(devname));
  366. if (copy_from_user(devname, optval, optlen)) {
  367. ret = -EFAULT;
  368. break;
  369. }
  370. /* Remove any cached route for this socket. */
  371. sk_dst_reset(sk);
  372. if (devname[0] == '\0') {
  373. sk->sk_bound_dev_if = 0;
  374. } else {
  375. struct net_device *dev = dev_get_by_name(devname);
  376. if (!dev) {
  377. ret = -ENODEV;
  378. break;
  379. }
  380. sk->sk_bound_dev_if = dev->ifindex;
  381. dev_put(dev);
  382. }
  383. }
  384. break;
  385. }
  386. #endif
  387. case SO_ATTACH_FILTER:
  388. ret = -EINVAL;
  389. if (optlen == sizeof(struct sock_fprog)) {
  390. struct sock_fprog fprog;
  391. ret = -EFAULT;
  392. if (copy_from_user(&fprog, optval, sizeof(fprog)))
  393. break;
  394. ret = sk_attach_filter(&fprog, sk);
  395. }
  396. break;
  397. case SO_DETACH_FILTER:
  398. spin_lock_bh(&sk->sk_lock.slock);
  399. filter = sk->sk_filter;
  400. if (filter) {
  401. sk->sk_filter = NULL;
  402. spin_unlock_bh(&sk->sk_lock.slock);
  403. sk_filter_release(sk, filter);
  404. break;
  405. }
  406. spin_unlock_bh(&sk->sk_lock.slock);
  407. ret = -ENONET;
  408. break;
  409. /* We implement the SO_SNDLOWAT etc to
  410. not be settable (1003.1g 5.3) */
  411. default:
  412. ret = -ENOPROTOOPT;
  413. break;
  414. }
  415. release_sock(sk);
  416. return ret;
  417. }
  418. int sock_getsockopt(struct socket *sock, int level, int optname,
  419. char __user *optval, int __user *optlen)
  420. {
  421. struct sock *sk = sock->sk;
  422. union
  423. {
  424. int val;
  425. struct linger ling;
  426. struct timeval tm;
  427. } v;
  428. unsigned int lv = sizeof(int);
  429. int len;
  430. if(get_user(len,optlen))
  431. return -EFAULT;
  432. if(len < 0)
  433. return -EINVAL;
  434. switch(optname)
  435. {
  436. case SO_DEBUG:
  437. v.val = sock_flag(sk, SOCK_DBG);
  438. break;
  439. case SO_DONTROUTE:
  440. v.val = sock_flag(sk, SOCK_LOCALROUTE);
  441. break;
  442. case SO_BROADCAST:
  443. v.val = !!sock_flag(sk, SOCK_BROADCAST);
  444. break;
  445. case SO_SNDBUF:
  446. v.val = sk->sk_sndbuf;
  447. break;
  448. case SO_RCVBUF:
  449. v.val = sk->sk_rcvbuf;
  450. break;
  451. case SO_REUSEADDR:
  452. v.val = sk->sk_reuse;
  453. break;
  454. case SO_KEEPALIVE:
  455. v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
  456. break;
  457. case SO_TYPE:
  458. v.val = sk->sk_type;
  459. break;
  460. case SO_ERROR:
  461. v.val = -sock_error(sk);
  462. if(v.val==0)
  463. v.val = xchg(&sk->sk_err_soft, 0);
  464. break;
  465. case SO_OOBINLINE:
  466. v.val = !!sock_flag(sk, SOCK_URGINLINE);
  467. break;
  468. case SO_NO_CHECK:
  469. v.val = sk->sk_no_check;
  470. break;
  471. case SO_PRIORITY:
  472. v.val = sk->sk_priority;
  473. break;
  474. case SO_LINGER:
  475. lv = sizeof(v.ling);
  476. v.ling.l_onoff = !!sock_flag(sk, SOCK_LINGER);
  477. v.ling.l_linger = sk->sk_lingertime / HZ;
  478. break;
  479. case SO_BSDCOMPAT:
  480. sock_warn_obsolete_bsdism("getsockopt");
  481. break;
  482. case SO_TIMESTAMP:
  483. v.val = sock_flag(sk, SOCK_RCVTSTAMP);
  484. break;
  485. case SO_RCVTIMEO:
  486. lv=sizeof(struct timeval);
  487. if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
  488. v.tm.tv_sec = 0;
  489. v.tm.tv_usec = 0;
  490. } else {
  491. v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
  492. v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
  493. }
  494. break;
  495. case SO_SNDTIMEO:
  496. lv=sizeof(struct timeval);
  497. if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
  498. v.tm.tv_sec = 0;
  499. v.tm.tv_usec = 0;
  500. } else {
  501. v.tm.tv_sec = sk->sk_sndtimeo / HZ;
  502. v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
  503. }
  504. break;
  505. case SO_RCVLOWAT:
  506. v.val = sk->sk_rcvlowat;
  507. break;
  508. case SO_SNDLOWAT:
  509. v.val=1;
  510. break;
  511. case SO_PASSCRED:
  512. v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
  513. break;
  514. case SO_PEERCRED:
  515. if (len > sizeof(sk->sk_peercred))
  516. len = sizeof(sk->sk_peercred);
  517. if (copy_to_user(optval, &sk->sk_peercred, len))
  518. return -EFAULT;
  519. goto lenout;
  520. case SO_PEERNAME:
  521. {
  522. char address[128];
  523. if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
  524. return -ENOTCONN;
  525. if (lv < len)
  526. return -EINVAL;
  527. if (copy_to_user(optval, address, len))
  528. return -EFAULT;
  529. goto lenout;
  530. }
  531. /* Dubious BSD thing... Probably nobody even uses it, but
  532. * the UNIX standard wants it for whatever reason... -DaveM
  533. */
  534. case SO_ACCEPTCONN:
  535. v.val = sk->sk_state == TCP_LISTEN;
  536. break;
  537. case SO_PEERSEC:
  538. return security_socket_getpeersec_stream(sock, optval, optlen, len);
  539. default:
  540. return(-ENOPROTOOPT);
  541. }
  542. if (len > lv)
  543. len = lv;
  544. if (copy_to_user(optval, &v, len))
  545. return -EFAULT;
  546. lenout:
  547. if (put_user(len, optlen))
  548. return -EFAULT;
  549. return 0;
  550. }
  551. /**
  552. * sk_alloc - All socket objects are allocated here
  553. * @family: protocol family
  554. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  555. * @prot: struct proto associated with this new sock instance
  556. * @zero_it: if we should zero the newly allocated sock
  557. */
  558. struct sock *sk_alloc(int family, gfp_t priority,
  559. struct proto *prot, int zero_it)
  560. {
  561. struct sock *sk = NULL;
  562. kmem_cache_t *slab = prot->slab;
  563. if (slab != NULL)
  564. sk = kmem_cache_alloc(slab, priority);
  565. else
  566. sk = kmalloc(prot->obj_size, priority);
  567. if (sk) {
  568. if (zero_it) {
  569. memset(sk, 0, prot->obj_size);
  570. sk->sk_family = family;
  571. /*
  572. * See comment in struct sock definition to understand
  573. * why we need sk_prot_creator -acme
  574. */
  575. sk->sk_prot = sk->sk_prot_creator = prot;
  576. sock_lock_init(sk);
  577. }
  578. if (security_sk_alloc(sk, family, priority))
  579. goto out_free;
  580. if (!try_module_get(prot->owner))
  581. goto out_free;
  582. }
  583. return sk;
  584. out_free:
  585. if (slab != NULL)
  586. kmem_cache_free(slab, sk);
  587. else
  588. kfree(sk);
  589. return NULL;
  590. }
  591. void sk_free(struct sock *sk)
  592. {
  593. struct sk_filter *filter;
  594. struct module *owner = sk->sk_prot_creator->owner;
  595. if (sk->sk_destruct)
  596. sk->sk_destruct(sk);
  597. filter = sk->sk_filter;
  598. if (filter) {
  599. sk_filter_release(sk, filter);
  600. sk->sk_filter = NULL;
  601. }
  602. sock_disable_timestamp(sk);
  603. if (atomic_read(&sk->sk_omem_alloc))
  604. printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
  605. __FUNCTION__, atomic_read(&sk->sk_omem_alloc));
  606. security_sk_free(sk);
  607. if (sk->sk_prot_creator->slab != NULL)
  608. kmem_cache_free(sk->sk_prot_creator->slab, sk);
  609. else
  610. kfree(sk);
  611. module_put(owner);
  612. }
  613. struct sock *sk_clone(const struct sock *sk, const gfp_t priority)
  614. {
  615. struct sock *newsk = sk_alloc(sk->sk_family, priority, sk->sk_prot, 0);
  616. if (newsk != NULL) {
  617. struct sk_filter *filter;
  618. memcpy(newsk, sk, sk->sk_prot->obj_size);
  619. /* SANITY */
  620. sk_node_init(&newsk->sk_node);
  621. sock_lock_init(newsk);
  622. bh_lock_sock(newsk);
  623. atomic_set(&newsk->sk_rmem_alloc, 0);
  624. atomic_set(&newsk->sk_wmem_alloc, 0);
  625. atomic_set(&newsk->sk_omem_alloc, 0);
  626. skb_queue_head_init(&newsk->sk_receive_queue);
  627. skb_queue_head_init(&newsk->sk_write_queue);
  628. rwlock_init(&newsk->sk_dst_lock);
  629. rwlock_init(&newsk->sk_callback_lock);
  630. newsk->sk_dst_cache = NULL;
  631. newsk->sk_wmem_queued = 0;
  632. newsk->sk_forward_alloc = 0;
  633. newsk->sk_send_head = NULL;
  634. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  635. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  636. sock_reset_flag(newsk, SOCK_DONE);
  637. skb_queue_head_init(&newsk->sk_error_queue);
  638. filter = newsk->sk_filter;
  639. if (filter != NULL)
  640. sk_filter_charge(newsk, filter);
  641. if (unlikely(xfrm_sk_clone_policy(newsk))) {
  642. /* It is still raw copy of parent, so invalidate
  643. * destructor and make plain sk_free() */
  644. newsk->sk_destruct = NULL;
  645. sk_free(newsk);
  646. newsk = NULL;
  647. goto out;
  648. }
  649. newsk->sk_err = 0;
  650. newsk->sk_priority = 0;
  651. atomic_set(&newsk->sk_refcnt, 2);
  652. /*
  653. * Increment the counter in the same struct proto as the master
  654. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  655. * is the same as sk->sk_prot->socks, as this field was copied
  656. * with memcpy).
  657. *
  658. * This _changes_ the previous behaviour, where
  659. * tcp_create_openreq_child always was incrementing the
  660. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  661. * to be taken into account in all callers. -acme
  662. */
  663. sk_refcnt_debug_inc(newsk);
  664. newsk->sk_socket = NULL;
  665. newsk->sk_sleep = NULL;
  666. if (newsk->sk_prot->sockets_allocated)
  667. atomic_inc(newsk->sk_prot->sockets_allocated);
  668. }
  669. out:
  670. return newsk;
  671. }
  672. EXPORT_SYMBOL_GPL(sk_clone);
  673. void __init sk_init(void)
  674. {
  675. if (num_physpages <= 4096) {
  676. sysctl_wmem_max = 32767;
  677. sysctl_rmem_max = 32767;
  678. sysctl_wmem_default = 32767;
  679. sysctl_rmem_default = 32767;
  680. } else if (num_physpages >= 131072) {
  681. sysctl_wmem_max = 131071;
  682. sysctl_rmem_max = 131071;
  683. }
  684. }
  685. /*
  686. * Simple resource managers for sockets.
  687. */
  688. /*
  689. * Write buffer destructor automatically called from kfree_skb.
  690. */
  691. void sock_wfree(struct sk_buff *skb)
  692. {
  693. struct sock *sk = skb->sk;
  694. /* In case it might be waiting for more memory. */
  695. atomic_sub(skb->truesize, &sk->sk_wmem_alloc);
  696. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE))
  697. sk->sk_write_space(sk);
  698. sock_put(sk);
  699. }
  700. /*
  701. * Read buffer destructor automatically called from kfree_skb.
  702. */
  703. void sock_rfree(struct sk_buff *skb)
  704. {
  705. struct sock *sk = skb->sk;
  706. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  707. }
  708. int sock_i_uid(struct sock *sk)
  709. {
  710. int uid;
  711. read_lock(&sk->sk_callback_lock);
  712. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
  713. read_unlock(&sk->sk_callback_lock);
  714. return uid;
  715. }
  716. unsigned long sock_i_ino(struct sock *sk)
  717. {
  718. unsigned long ino;
  719. read_lock(&sk->sk_callback_lock);
  720. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  721. read_unlock(&sk->sk_callback_lock);
  722. return ino;
  723. }
  724. /*
  725. * Allocate a skb from the socket's send buffer.
  726. */
  727. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  728. gfp_t priority)
  729. {
  730. if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  731. struct sk_buff * skb = alloc_skb(size, priority);
  732. if (skb) {
  733. skb_set_owner_w(skb, sk);
  734. return skb;
  735. }
  736. }
  737. return NULL;
  738. }
  739. /*
  740. * Allocate a skb from the socket's receive buffer.
  741. */
  742. struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
  743. gfp_t priority)
  744. {
  745. if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  746. struct sk_buff *skb = alloc_skb(size, priority);
  747. if (skb) {
  748. skb_set_owner_r(skb, sk);
  749. return skb;
  750. }
  751. }
  752. return NULL;
  753. }
  754. /*
  755. * Allocate a memory block from the socket's option memory buffer.
  756. */
  757. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
  758. {
  759. if ((unsigned)size <= sysctl_optmem_max &&
  760. atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
  761. void *mem;
  762. /* First do the add, to avoid the race if kmalloc
  763. * might sleep.
  764. */
  765. atomic_add(size, &sk->sk_omem_alloc);
  766. mem = kmalloc(size, priority);
  767. if (mem)
  768. return mem;
  769. atomic_sub(size, &sk->sk_omem_alloc);
  770. }
  771. return NULL;
  772. }
  773. /*
  774. * Free an option memory block.
  775. */
  776. void sock_kfree_s(struct sock *sk, void *mem, int size)
  777. {
  778. kfree(mem);
  779. atomic_sub(size, &sk->sk_omem_alloc);
  780. }
  781. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  782. I think, these locks should be removed for datagram sockets.
  783. */
  784. static long sock_wait_for_wmem(struct sock * sk, long timeo)
  785. {
  786. DEFINE_WAIT(wait);
  787. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  788. for (;;) {
  789. if (!timeo)
  790. break;
  791. if (signal_pending(current))
  792. break;
  793. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  794. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  795. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
  796. break;
  797. if (sk->sk_shutdown & SEND_SHUTDOWN)
  798. break;
  799. if (sk->sk_err)
  800. break;
  801. timeo = schedule_timeout(timeo);
  802. }
  803. finish_wait(sk->sk_sleep, &wait);
  804. return timeo;
  805. }
  806. /*
  807. * Generic send/receive buffer handlers
  808. */
  809. static struct sk_buff *sock_alloc_send_pskb(struct sock *sk,
  810. unsigned long header_len,
  811. unsigned long data_len,
  812. int noblock, int *errcode)
  813. {
  814. struct sk_buff *skb;
  815. gfp_t gfp_mask;
  816. long timeo;
  817. int err;
  818. gfp_mask = sk->sk_allocation;
  819. if (gfp_mask & __GFP_WAIT)
  820. gfp_mask |= __GFP_REPEAT;
  821. timeo = sock_sndtimeo(sk, noblock);
  822. while (1) {
  823. err = sock_error(sk);
  824. if (err != 0)
  825. goto failure;
  826. err = -EPIPE;
  827. if (sk->sk_shutdown & SEND_SHUTDOWN)
  828. goto failure;
  829. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  830. skb = alloc_skb(header_len, sk->sk_allocation);
  831. if (skb) {
  832. int npages;
  833. int i;
  834. /* No pages, we're done... */
  835. if (!data_len)
  836. break;
  837. npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  838. skb->truesize += data_len;
  839. skb_shinfo(skb)->nr_frags = npages;
  840. for (i = 0; i < npages; i++) {
  841. struct page *page;
  842. skb_frag_t *frag;
  843. page = alloc_pages(sk->sk_allocation, 0);
  844. if (!page) {
  845. err = -ENOBUFS;
  846. skb_shinfo(skb)->nr_frags = i;
  847. kfree_skb(skb);
  848. goto failure;
  849. }
  850. frag = &skb_shinfo(skb)->frags[i];
  851. frag->page = page;
  852. frag->page_offset = 0;
  853. frag->size = (data_len >= PAGE_SIZE ?
  854. PAGE_SIZE :
  855. data_len);
  856. data_len -= PAGE_SIZE;
  857. }
  858. /* Full success... */
  859. break;
  860. }
  861. err = -ENOBUFS;
  862. goto failure;
  863. }
  864. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  865. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  866. err = -EAGAIN;
  867. if (!timeo)
  868. goto failure;
  869. if (signal_pending(current))
  870. goto interrupted;
  871. timeo = sock_wait_for_wmem(sk, timeo);
  872. }
  873. skb_set_owner_w(skb, sk);
  874. return skb;
  875. interrupted:
  876. err = sock_intr_errno(timeo);
  877. failure:
  878. *errcode = err;
  879. return NULL;
  880. }
  881. struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
  882. int noblock, int *errcode)
  883. {
  884. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
  885. }
  886. static void __lock_sock(struct sock *sk)
  887. {
  888. DEFINE_WAIT(wait);
  889. for(;;) {
  890. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  891. TASK_UNINTERRUPTIBLE);
  892. spin_unlock_bh(&sk->sk_lock.slock);
  893. schedule();
  894. spin_lock_bh(&sk->sk_lock.slock);
  895. if(!sock_owned_by_user(sk))
  896. break;
  897. }
  898. finish_wait(&sk->sk_lock.wq, &wait);
  899. }
  900. static void __release_sock(struct sock *sk)
  901. {
  902. struct sk_buff *skb = sk->sk_backlog.head;
  903. do {
  904. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  905. bh_unlock_sock(sk);
  906. do {
  907. struct sk_buff *next = skb->next;
  908. skb->next = NULL;
  909. sk->sk_backlog_rcv(sk, skb);
  910. /*
  911. * We are in process context here with softirqs
  912. * disabled, use cond_resched_softirq() to preempt.
  913. * This is safe to do because we've taken the backlog
  914. * queue private:
  915. */
  916. cond_resched_softirq();
  917. skb = next;
  918. } while (skb != NULL);
  919. bh_lock_sock(sk);
  920. } while((skb = sk->sk_backlog.head) != NULL);
  921. }
  922. /**
  923. * sk_wait_data - wait for data to arrive at sk_receive_queue
  924. * @sk: sock to wait on
  925. * @timeo: for how long
  926. *
  927. * Now socket state including sk->sk_err is changed only under lock,
  928. * hence we may omit checks after joining wait queue.
  929. * We check receive queue before schedule() only as optimization;
  930. * it is very likely that release_sock() added new data.
  931. */
  932. int sk_wait_data(struct sock *sk, long *timeo)
  933. {
  934. int rc;
  935. DEFINE_WAIT(wait);
  936. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  937. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  938. rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
  939. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  940. finish_wait(sk->sk_sleep, &wait);
  941. return rc;
  942. }
  943. EXPORT_SYMBOL(sk_wait_data);
  944. /*
  945. * Set of default routines for initialising struct proto_ops when
  946. * the protocol does not support a particular function. In certain
  947. * cases where it makes no sense for a protocol to have a "do nothing"
  948. * function, some default processing is provided.
  949. */
  950. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  951. {
  952. return -EOPNOTSUPP;
  953. }
  954. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  955. int len, int flags)
  956. {
  957. return -EOPNOTSUPP;
  958. }
  959. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  960. {
  961. return -EOPNOTSUPP;
  962. }
  963. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
  964. {
  965. return -EOPNOTSUPP;
  966. }
  967. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  968. int *len, int peer)
  969. {
  970. return -EOPNOTSUPP;
  971. }
  972. unsigned int sock_no_poll(struct file * file, struct socket *sock, poll_table *pt)
  973. {
  974. return 0;
  975. }
  976. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  977. {
  978. return -EOPNOTSUPP;
  979. }
  980. int sock_no_listen(struct socket *sock, int backlog)
  981. {
  982. return -EOPNOTSUPP;
  983. }
  984. int sock_no_shutdown(struct socket *sock, int how)
  985. {
  986. return -EOPNOTSUPP;
  987. }
  988. int sock_no_setsockopt(struct socket *sock, int level, int optname,
  989. char __user *optval, int optlen)
  990. {
  991. return -EOPNOTSUPP;
  992. }
  993. int sock_no_getsockopt(struct socket *sock, int level, int optname,
  994. char __user *optval, int __user *optlen)
  995. {
  996. return -EOPNOTSUPP;
  997. }
  998. int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  999. size_t len)
  1000. {
  1001. return -EOPNOTSUPP;
  1002. }
  1003. int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  1004. size_t len, int flags)
  1005. {
  1006. return -EOPNOTSUPP;
  1007. }
  1008. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  1009. {
  1010. /* Mirror missing mmap method error code */
  1011. return -ENODEV;
  1012. }
  1013. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  1014. {
  1015. ssize_t res;
  1016. struct msghdr msg = {.msg_flags = flags};
  1017. struct kvec iov;
  1018. char *kaddr = kmap(page);
  1019. iov.iov_base = kaddr + offset;
  1020. iov.iov_len = size;
  1021. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  1022. kunmap(page);
  1023. return res;
  1024. }
  1025. /*
  1026. * Default Socket Callbacks
  1027. */
  1028. static void sock_def_wakeup(struct sock *sk)
  1029. {
  1030. read_lock(&sk->sk_callback_lock);
  1031. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  1032. wake_up_interruptible_all(sk->sk_sleep);
  1033. read_unlock(&sk->sk_callback_lock);
  1034. }
  1035. static void sock_def_error_report(struct sock *sk)
  1036. {
  1037. read_lock(&sk->sk_callback_lock);
  1038. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  1039. wake_up_interruptible(sk->sk_sleep);
  1040. sk_wake_async(sk,0,POLL_ERR);
  1041. read_unlock(&sk->sk_callback_lock);
  1042. }
  1043. static void sock_def_readable(struct sock *sk, int len)
  1044. {
  1045. read_lock(&sk->sk_callback_lock);
  1046. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  1047. wake_up_interruptible(sk->sk_sleep);
  1048. sk_wake_async(sk,1,POLL_IN);
  1049. read_unlock(&sk->sk_callback_lock);
  1050. }
  1051. static void sock_def_write_space(struct sock *sk)
  1052. {
  1053. read_lock(&sk->sk_callback_lock);
  1054. /* Do not wake up a writer until he can make "significant"
  1055. * progress. --DaveM
  1056. */
  1057. if((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
  1058. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  1059. wake_up_interruptible(sk->sk_sleep);
  1060. /* Should agree with poll, otherwise some programs break */
  1061. if (sock_writeable(sk))
  1062. sk_wake_async(sk, 2, POLL_OUT);
  1063. }
  1064. read_unlock(&sk->sk_callback_lock);
  1065. }
  1066. static void sock_def_destruct(struct sock *sk)
  1067. {
  1068. kfree(sk->sk_protinfo);
  1069. }
  1070. void sk_send_sigurg(struct sock *sk)
  1071. {
  1072. if (sk->sk_socket && sk->sk_socket->file)
  1073. if (send_sigurg(&sk->sk_socket->file->f_owner))
  1074. sk_wake_async(sk, 3, POLL_PRI);
  1075. }
  1076. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  1077. unsigned long expires)
  1078. {
  1079. if (!mod_timer(timer, expires))
  1080. sock_hold(sk);
  1081. }
  1082. EXPORT_SYMBOL(sk_reset_timer);
  1083. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  1084. {
  1085. if (timer_pending(timer) && del_timer(timer))
  1086. __sock_put(sk);
  1087. }
  1088. EXPORT_SYMBOL(sk_stop_timer);
  1089. void sock_init_data(struct socket *sock, struct sock *sk)
  1090. {
  1091. skb_queue_head_init(&sk->sk_receive_queue);
  1092. skb_queue_head_init(&sk->sk_write_queue);
  1093. skb_queue_head_init(&sk->sk_error_queue);
  1094. sk->sk_send_head = NULL;
  1095. init_timer(&sk->sk_timer);
  1096. sk->sk_allocation = GFP_KERNEL;
  1097. sk->sk_rcvbuf = sysctl_rmem_default;
  1098. sk->sk_sndbuf = sysctl_wmem_default;
  1099. sk->sk_state = TCP_CLOSE;
  1100. sk->sk_socket = sock;
  1101. sock_set_flag(sk, SOCK_ZAPPED);
  1102. if(sock)
  1103. {
  1104. sk->sk_type = sock->type;
  1105. sk->sk_sleep = &sock->wait;
  1106. sock->sk = sk;
  1107. } else
  1108. sk->sk_sleep = NULL;
  1109. rwlock_init(&sk->sk_dst_lock);
  1110. rwlock_init(&sk->sk_callback_lock);
  1111. sk->sk_state_change = sock_def_wakeup;
  1112. sk->sk_data_ready = sock_def_readable;
  1113. sk->sk_write_space = sock_def_write_space;
  1114. sk->sk_error_report = sock_def_error_report;
  1115. sk->sk_destruct = sock_def_destruct;
  1116. sk->sk_sndmsg_page = NULL;
  1117. sk->sk_sndmsg_off = 0;
  1118. sk->sk_peercred.pid = 0;
  1119. sk->sk_peercred.uid = -1;
  1120. sk->sk_peercred.gid = -1;
  1121. sk->sk_write_pending = 0;
  1122. sk->sk_rcvlowat = 1;
  1123. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  1124. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  1125. sk->sk_stamp.tv_sec = -1L;
  1126. sk->sk_stamp.tv_usec = -1L;
  1127. atomic_set(&sk->sk_refcnt, 1);
  1128. }
  1129. void fastcall lock_sock(struct sock *sk)
  1130. {
  1131. might_sleep();
  1132. spin_lock_bh(&(sk->sk_lock.slock));
  1133. if (sk->sk_lock.owner)
  1134. __lock_sock(sk);
  1135. sk->sk_lock.owner = (void *)1;
  1136. spin_unlock_bh(&(sk->sk_lock.slock));
  1137. }
  1138. EXPORT_SYMBOL(lock_sock);
  1139. void fastcall release_sock(struct sock *sk)
  1140. {
  1141. spin_lock_bh(&(sk->sk_lock.slock));
  1142. if (sk->sk_backlog.tail)
  1143. __release_sock(sk);
  1144. sk->sk_lock.owner = NULL;
  1145. if (waitqueue_active(&(sk->sk_lock.wq)))
  1146. wake_up(&(sk->sk_lock.wq));
  1147. spin_unlock_bh(&(sk->sk_lock.slock));
  1148. }
  1149. EXPORT_SYMBOL(release_sock);
  1150. int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  1151. {
  1152. if (!sock_flag(sk, SOCK_TIMESTAMP))
  1153. sock_enable_timestamp(sk);
  1154. if (sk->sk_stamp.tv_sec == -1)
  1155. return -ENOENT;
  1156. if (sk->sk_stamp.tv_sec == 0)
  1157. do_gettimeofday(&sk->sk_stamp);
  1158. return copy_to_user(userstamp, &sk->sk_stamp, sizeof(struct timeval)) ?
  1159. -EFAULT : 0;
  1160. }
  1161. EXPORT_SYMBOL(sock_get_timestamp);
  1162. void sock_enable_timestamp(struct sock *sk)
  1163. {
  1164. if (!sock_flag(sk, SOCK_TIMESTAMP)) {
  1165. sock_set_flag(sk, SOCK_TIMESTAMP);
  1166. net_enable_timestamp();
  1167. }
  1168. }
  1169. EXPORT_SYMBOL(sock_enable_timestamp);
  1170. /*
  1171. * Get a socket option on an socket.
  1172. *
  1173. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  1174. * asynchronous errors should be reported by getsockopt. We assume
  1175. * this means if you specify SO_ERROR (otherwise whats the point of it).
  1176. */
  1177. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  1178. char __user *optval, int __user *optlen)
  1179. {
  1180. struct sock *sk = sock->sk;
  1181. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1182. }
  1183. EXPORT_SYMBOL(sock_common_getsockopt);
  1184. #ifdef CONFIG_COMPAT
  1185. int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
  1186. char __user *optval, int __user *optlen)
  1187. {
  1188. struct sock *sk = sock->sk;
  1189. if (sk->sk_prot->compat_setsockopt != NULL)
  1190. return sk->sk_prot->compat_getsockopt(sk, level, optname,
  1191. optval, optlen);
  1192. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1193. }
  1194. EXPORT_SYMBOL(compat_sock_common_getsockopt);
  1195. #endif
  1196. int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
  1197. struct msghdr *msg, size_t size, int flags)
  1198. {
  1199. struct sock *sk = sock->sk;
  1200. int addr_len = 0;
  1201. int err;
  1202. err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
  1203. flags & ~MSG_DONTWAIT, &addr_len);
  1204. if (err >= 0)
  1205. msg->msg_namelen = addr_len;
  1206. return err;
  1207. }
  1208. EXPORT_SYMBOL(sock_common_recvmsg);
  1209. /*
  1210. * Set socket options on an inet socket.
  1211. */
  1212. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  1213. char __user *optval, int optlen)
  1214. {
  1215. struct sock *sk = sock->sk;
  1216. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1217. }
  1218. EXPORT_SYMBOL(sock_common_setsockopt);
  1219. #ifdef CONFIG_COMPAT
  1220. int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
  1221. char __user *optval, int optlen)
  1222. {
  1223. struct sock *sk = sock->sk;
  1224. if (sk->sk_prot->compat_setsockopt != NULL)
  1225. return sk->sk_prot->compat_setsockopt(sk, level, optname,
  1226. optval, optlen);
  1227. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1228. }
  1229. EXPORT_SYMBOL(compat_sock_common_setsockopt);
  1230. #endif
  1231. void sk_common_release(struct sock *sk)
  1232. {
  1233. if (sk->sk_prot->destroy)
  1234. sk->sk_prot->destroy(sk);
  1235. /*
  1236. * Observation: when sock_common_release is called, processes have
  1237. * no access to socket. But net still has.
  1238. * Step one, detach it from networking:
  1239. *
  1240. * A. Remove from hash tables.
  1241. */
  1242. sk->sk_prot->unhash(sk);
  1243. /*
  1244. * In this point socket cannot receive new packets, but it is possible
  1245. * that some packets are in flight because some CPU runs receiver and
  1246. * did hash table lookup before we unhashed socket. They will achieve
  1247. * receive queue and will be purged by socket destructor.
  1248. *
  1249. * Also we still have packets pending on receive queue and probably,
  1250. * our own packets waiting in device queues. sock_destroy will drain
  1251. * receive queue, but transmitted packets will delay socket destruction
  1252. * until the last reference will be released.
  1253. */
  1254. sock_orphan(sk);
  1255. xfrm_sk_free_policy(sk);
  1256. sk_refcnt_debug_release(sk);
  1257. sock_put(sk);
  1258. }
  1259. EXPORT_SYMBOL(sk_common_release);
  1260. static DEFINE_RWLOCK(proto_list_lock);
  1261. static LIST_HEAD(proto_list);
  1262. int proto_register(struct proto *prot, int alloc_slab)
  1263. {
  1264. char *request_sock_slab_name = NULL;
  1265. char *timewait_sock_slab_name;
  1266. int rc = -ENOBUFS;
  1267. if (alloc_slab) {
  1268. prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
  1269. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1270. if (prot->slab == NULL) {
  1271. printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
  1272. prot->name);
  1273. goto out;
  1274. }
  1275. if (prot->rsk_prot != NULL) {
  1276. static const char mask[] = "request_sock_%s";
  1277. request_sock_slab_name = kmalloc(strlen(prot->name) + sizeof(mask) - 1, GFP_KERNEL);
  1278. if (request_sock_slab_name == NULL)
  1279. goto out_free_sock_slab;
  1280. sprintf(request_sock_slab_name, mask, prot->name);
  1281. prot->rsk_prot->slab = kmem_cache_create(request_sock_slab_name,
  1282. prot->rsk_prot->obj_size, 0,
  1283. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1284. if (prot->rsk_prot->slab == NULL) {
  1285. printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
  1286. prot->name);
  1287. goto out_free_request_sock_slab_name;
  1288. }
  1289. }
  1290. if (prot->twsk_prot != NULL) {
  1291. static const char mask[] = "tw_sock_%s";
  1292. timewait_sock_slab_name = kmalloc(strlen(prot->name) + sizeof(mask) - 1, GFP_KERNEL);
  1293. if (timewait_sock_slab_name == NULL)
  1294. goto out_free_request_sock_slab;
  1295. sprintf(timewait_sock_slab_name, mask, prot->name);
  1296. prot->twsk_prot->twsk_slab =
  1297. kmem_cache_create(timewait_sock_slab_name,
  1298. prot->twsk_prot->twsk_obj_size,
  1299. 0, SLAB_HWCACHE_ALIGN,
  1300. NULL, NULL);
  1301. if (prot->twsk_prot->twsk_slab == NULL)
  1302. goto out_free_timewait_sock_slab_name;
  1303. }
  1304. }
  1305. write_lock(&proto_list_lock);
  1306. list_add(&prot->node, &proto_list);
  1307. write_unlock(&proto_list_lock);
  1308. rc = 0;
  1309. out:
  1310. return rc;
  1311. out_free_timewait_sock_slab_name:
  1312. kfree(timewait_sock_slab_name);
  1313. out_free_request_sock_slab:
  1314. if (prot->rsk_prot && prot->rsk_prot->slab) {
  1315. kmem_cache_destroy(prot->rsk_prot->slab);
  1316. prot->rsk_prot->slab = NULL;
  1317. }
  1318. out_free_request_sock_slab_name:
  1319. kfree(request_sock_slab_name);
  1320. out_free_sock_slab:
  1321. kmem_cache_destroy(prot->slab);
  1322. prot->slab = NULL;
  1323. goto out;
  1324. }
  1325. EXPORT_SYMBOL(proto_register);
  1326. void proto_unregister(struct proto *prot)
  1327. {
  1328. write_lock(&proto_list_lock);
  1329. list_del(&prot->node);
  1330. write_unlock(&proto_list_lock);
  1331. if (prot->slab != NULL) {
  1332. kmem_cache_destroy(prot->slab);
  1333. prot->slab = NULL;
  1334. }
  1335. if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
  1336. const char *name = kmem_cache_name(prot->rsk_prot->slab);
  1337. kmem_cache_destroy(prot->rsk_prot->slab);
  1338. kfree(name);
  1339. prot->rsk_prot->slab = NULL;
  1340. }
  1341. if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
  1342. const char *name = kmem_cache_name(prot->twsk_prot->twsk_slab);
  1343. kmem_cache_destroy(prot->twsk_prot->twsk_slab);
  1344. kfree(name);
  1345. prot->twsk_prot->twsk_slab = NULL;
  1346. }
  1347. }
  1348. EXPORT_SYMBOL(proto_unregister);
  1349. #ifdef CONFIG_PROC_FS
  1350. static inline struct proto *__proto_head(void)
  1351. {
  1352. return list_entry(proto_list.next, struct proto, node);
  1353. }
  1354. static inline struct proto *proto_head(void)
  1355. {
  1356. return list_empty(&proto_list) ? NULL : __proto_head();
  1357. }
  1358. static inline struct proto *proto_next(struct proto *proto)
  1359. {
  1360. return proto->node.next == &proto_list ? NULL :
  1361. list_entry(proto->node.next, struct proto, node);
  1362. }
  1363. static inline struct proto *proto_get_idx(loff_t pos)
  1364. {
  1365. struct proto *proto;
  1366. loff_t i = 0;
  1367. list_for_each_entry(proto, &proto_list, node)
  1368. if (i++ == pos)
  1369. goto out;
  1370. proto = NULL;
  1371. out:
  1372. return proto;
  1373. }
  1374. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  1375. {
  1376. read_lock(&proto_list_lock);
  1377. return *pos ? proto_get_idx(*pos - 1) : SEQ_START_TOKEN;
  1378. }
  1379. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1380. {
  1381. ++*pos;
  1382. return v == SEQ_START_TOKEN ? proto_head() : proto_next(v);
  1383. }
  1384. static void proto_seq_stop(struct seq_file *seq, void *v)
  1385. {
  1386. read_unlock(&proto_list_lock);
  1387. }
  1388. static char proto_method_implemented(const void *method)
  1389. {
  1390. return method == NULL ? 'n' : 'y';
  1391. }
  1392. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  1393. {
  1394. seq_printf(seq, "%-9s %4u %6d %6d %-3s %6u %-3s %-10s "
  1395. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  1396. proto->name,
  1397. proto->obj_size,
  1398. proto->sockets_allocated != NULL ? atomic_read(proto->sockets_allocated) : -1,
  1399. proto->memory_allocated != NULL ? atomic_read(proto->memory_allocated) : -1,
  1400. proto->memory_pressure != NULL ? *proto->memory_pressure ? "yes" : "no" : "NI",
  1401. proto->max_header,
  1402. proto->slab == NULL ? "no" : "yes",
  1403. module_name(proto->owner),
  1404. proto_method_implemented(proto->close),
  1405. proto_method_implemented(proto->connect),
  1406. proto_method_implemented(proto->disconnect),
  1407. proto_method_implemented(proto->accept),
  1408. proto_method_implemented(proto->ioctl),
  1409. proto_method_implemented(proto->init),
  1410. proto_method_implemented(proto->destroy),
  1411. proto_method_implemented(proto->shutdown),
  1412. proto_method_implemented(proto->setsockopt),
  1413. proto_method_implemented(proto->getsockopt),
  1414. proto_method_implemented(proto->sendmsg),
  1415. proto_method_implemented(proto->recvmsg),
  1416. proto_method_implemented(proto->sendpage),
  1417. proto_method_implemented(proto->bind),
  1418. proto_method_implemented(proto->backlog_rcv),
  1419. proto_method_implemented(proto->hash),
  1420. proto_method_implemented(proto->unhash),
  1421. proto_method_implemented(proto->get_port),
  1422. proto_method_implemented(proto->enter_memory_pressure));
  1423. }
  1424. static int proto_seq_show(struct seq_file *seq, void *v)
  1425. {
  1426. if (v == SEQ_START_TOKEN)
  1427. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  1428. "protocol",
  1429. "size",
  1430. "sockets",
  1431. "memory",
  1432. "press",
  1433. "maxhdr",
  1434. "slab",
  1435. "module",
  1436. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  1437. else
  1438. proto_seq_printf(seq, v);
  1439. return 0;
  1440. }
  1441. static struct seq_operations proto_seq_ops = {
  1442. .start = proto_seq_start,
  1443. .next = proto_seq_next,
  1444. .stop = proto_seq_stop,
  1445. .show = proto_seq_show,
  1446. };
  1447. static int proto_seq_open(struct inode *inode, struct file *file)
  1448. {
  1449. return seq_open(file, &proto_seq_ops);
  1450. }
  1451. static struct file_operations proto_seq_fops = {
  1452. .owner = THIS_MODULE,
  1453. .open = proto_seq_open,
  1454. .read = seq_read,
  1455. .llseek = seq_lseek,
  1456. .release = seq_release,
  1457. };
  1458. static int __init proto_init(void)
  1459. {
  1460. /* register /proc/net/protocols */
  1461. return proc_net_fops_create("protocols", S_IRUGO, &proto_seq_fops) == NULL ? -ENOBUFS : 0;
  1462. }
  1463. subsys_initcall(proto_init);
  1464. #endif /* PROC_FS */
  1465. EXPORT_SYMBOL(sk_alloc);
  1466. EXPORT_SYMBOL(sk_free);
  1467. EXPORT_SYMBOL(sk_send_sigurg);
  1468. EXPORT_SYMBOL(sock_alloc_send_skb);
  1469. EXPORT_SYMBOL(sock_init_data);
  1470. EXPORT_SYMBOL(sock_kfree_s);
  1471. EXPORT_SYMBOL(sock_kmalloc);
  1472. EXPORT_SYMBOL(sock_no_accept);
  1473. EXPORT_SYMBOL(sock_no_bind);
  1474. EXPORT_SYMBOL(sock_no_connect);
  1475. EXPORT_SYMBOL(sock_no_getname);
  1476. EXPORT_SYMBOL(sock_no_getsockopt);
  1477. EXPORT_SYMBOL(sock_no_ioctl);
  1478. EXPORT_SYMBOL(sock_no_listen);
  1479. EXPORT_SYMBOL(sock_no_mmap);
  1480. EXPORT_SYMBOL(sock_no_poll);
  1481. EXPORT_SYMBOL(sock_no_recvmsg);
  1482. EXPORT_SYMBOL(sock_no_sendmsg);
  1483. EXPORT_SYMBOL(sock_no_sendpage);
  1484. EXPORT_SYMBOL(sock_no_setsockopt);
  1485. EXPORT_SYMBOL(sock_no_shutdown);
  1486. EXPORT_SYMBOL(sock_no_socketpair);
  1487. EXPORT_SYMBOL(sock_rfree);
  1488. EXPORT_SYMBOL(sock_setsockopt);
  1489. EXPORT_SYMBOL(sock_wfree);
  1490. EXPORT_SYMBOL(sock_wmalloc);
  1491. EXPORT_SYMBOL(sock_i_uid);
  1492. EXPORT_SYMBOL(sock_i_ino);
  1493. EXPORT_SYMBOL(sysctl_optmem_max);
  1494. #ifdef CONFIG_SYSCTL
  1495. EXPORT_SYMBOL(sysctl_rmem_max);
  1496. EXPORT_SYMBOL(sysctl_wmem_max);
  1497. #endif