sock.c 37 KB

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