sock.c 38 KB

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