sock.c 42 KB

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