sock.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. kmem_cache_free(slab, sk);
  558. sk = NULL;
  559. } else
  560. __module_get(prot->owner);
  561. }
  562. return sk;
  563. }
  564. void sk_free(struct sock *sk)
  565. {
  566. struct sk_filter *filter;
  567. struct module *owner = sk->sk_prot->owner;
  568. if (sk->sk_destruct)
  569. sk->sk_destruct(sk);
  570. filter = sk->sk_filter;
  571. if (filter) {
  572. sk_filter_release(sk, filter);
  573. sk->sk_filter = NULL;
  574. }
  575. sock_disable_timestamp(sk);
  576. if (atomic_read(&sk->sk_omem_alloc))
  577. printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
  578. __FUNCTION__, atomic_read(&sk->sk_omem_alloc));
  579. security_sk_free(sk);
  580. if (sk->sk_prot->slab != NULL)
  581. kmem_cache_free(sk->sk_prot->slab, sk);
  582. else
  583. kfree(sk);
  584. module_put(owner);
  585. }
  586. void __init sk_init(void)
  587. {
  588. if (num_physpages <= 4096) {
  589. sysctl_wmem_max = 32767;
  590. sysctl_rmem_max = 32767;
  591. sysctl_wmem_default = 32767;
  592. sysctl_rmem_default = 32767;
  593. } else if (num_physpages >= 131072) {
  594. sysctl_wmem_max = 131071;
  595. sysctl_rmem_max = 131071;
  596. }
  597. }
  598. /*
  599. * Simple resource managers for sockets.
  600. */
  601. /*
  602. * Write buffer destructor automatically called from kfree_skb.
  603. */
  604. void sock_wfree(struct sk_buff *skb)
  605. {
  606. struct sock *sk = skb->sk;
  607. /* In case it might be waiting for more memory. */
  608. atomic_sub(skb->truesize, &sk->sk_wmem_alloc);
  609. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE))
  610. sk->sk_write_space(sk);
  611. sock_put(sk);
  612. }
  613. /*
  614. * Read buffer destructor automatically called from kfree_skb.
  615. */
  616. void sock_rfree(struct sk_buff *skb)
  617. {
  618. struct sock *sk = skb->sk;
  619. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  620. }
  621. int sock_i_uid(struct sock *sk)
  622. {
  623. int uid;
  624. read_lock(&sk->sk_callback_lock);
  625. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
  626. read_unlock(&sk->sk_callback_lock);
  627. return uid;
  628. }
  629. unsigned long sock_i_ino(struct sock *sk)
  630. {
  631. unsigned long ino;
  632. read_lock(&sk->sk_callback_lock);
  633. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  634. read_unlock(&sk->sk_callback_lock);
  635. return ino;
  636. }
  637. /*
  638. * Allocate a skb from the socket's send buffer.
  639. */
  640. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
  641. {
  642. if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  643. struct sk_buff * skb = alloc_skb(size, priority);
  644. if (skb) {
  645. skb_set_owner_w(skb, sk);
  646. return skb;
  647. }
  648. }
  649. return NULL;
  650. }
  651. /*
  652. * Allocate a skb from the socket's receive buffer.
  653. */
  654. struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
  655. {
  656. if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  657. struct sk_buff *skb = alloc_skb(size, priority);
  658. if (skb) {
  659. skb_set_owner_r(skb, sk);
  660. return skb;
  661. }
  662. }
  663. return NULL;
  664. }
  665. /*
  666. * Allocate a memory block from the socket's option memory buffer.
  667. */
  668. void *sock_kmalloc(struct sock *sk, int size, int priority)
  669. {
  670. if ((unsigned)size <= sysctl_optmem_max &&
  671. atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
  672. void *mem;
  673. /* First do the add, to avoid the race if kmalloc
  674. * might sleep.
  675. */
  676. atomic_add(size, &sk->sk_omem_alloc);
  677. mem = kmalloc(size, priority);
  678. if (mem)
  679. return mem;
  680. atomic_sub(size, &sk->sk_omem_alloc);
  681. }
  682. return NULL;
  683. }
  684. /*
  685. * Free an option memory block.
  686. */
  687. void sock_kfree_s(struct sock *sk, void *mem, int size)
  688. {
  689. kfree(mem);
  690. atomic_sub(size, &sk->sk_omem_alloc);
  691. }
  692. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  693. I think, these locks should be removed for datagram sockets.
  694. */
  695. static long sock_wait_for_wmem(struct sock * sk, long timeo)
  696. {
  697. DEFINE_WAIT(wait);
  698. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  699. for (;;) {
  700. if (!timeo)
  701. break;
  702. if (signal_pending(current))
  703. break;
  704. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  705. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  706. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
  707. break;
  708. if (sk->sk_shutdown & SEND_SHUTDOWN)
  709. break;
  710. if (sk->sk_err)
  711. break;
  712. timeo = schedule_timeout(timeo);
  713. }
  714. finish_wait(sk->sk_sleep, &wait);
  715. return timeo;
  716. }
  717. /*
  718. * Generic send/receive buffer handlers
  719. */
  720. static struct sk_buff *sock_alloc_send_pskb(struct sock *sk,
  721. unsigned long header_len,
  722. unsigned long data_len,
  723. int noblock, int *errcode)
  724. {
  725. struct sk_buff *skb;
  726. unsigned int gfp_mask;
  727. long timeo;
  728. int err;
  729. gfp_mask = sk->sk_allocation;
  730. if (gfp_mask & __GFP_WAIT)
  731. gfp_mask |= __GFP_REPEAT;
  732. timeo = sock_sndtimeo(sk, noblock);
  733. while (1) {
  734. err = sock_error(sk);
  735. if (err != 0)
  736. goto failure;
  737. err = -EPIPE;
  738. if (sk->sk_shutdown & SEND_SHUTDOWN)
  739. goto failure;
  740. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  741. skb = alloc_skb(header_len, sk->sk_allocation);
  742. if (skb) {
  743. int npages;
  744. int i;
  745. /* No pages, we're done... */
  746. if (!data_len)
  747. break;
  748. npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  749. skb->truesize += data_len;
  750. skb_shinfo(skb)->nr_frags = npages;
  751. for (i = 0; i < npages; i++) {
  752. struct page *page;
  753. skb_frag_t *frag;
  754. page = alloc_pages(sk->sk_allocation, 0);
  755. if (!page) {
  756. err = -ENOBUFS;
  757. skb_shinfo(skb)->nr_frags = i;
  758. kfree_skb(skb);
  759. goto failure;
  760. }
  761. frag = &skb_shinfo(skb)->frags[i];
  762. frag->page = page;
  763. frag->page_offset = 0;
  764. frag->size = (data_len >= PAGE_SIZE ?
  765. PAGE_SIZE :
  766. data_len);
  767. data_len -= PAGE_SIZE;
  768. }
  769. /* Full success... */
  770. break;
  771. }
  772. err = -ENOBUFS;
  773. goto failure;
  774. }
  775. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  776. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  777. err = -EAGAIN;
  778. if (!timeo)
  779. goto failure;
  780. if (signal_pending(current))
  781. goto interrupted;
  782. timeo = sock_wait_for_wmem(sk, timeo);
  783. }
  784. skb_set_owner_w(skb, sk);
  785. return skb;
  786. interrupted:
  787. err = sock_intr_errno(timeo);
  788. failure:
  789. *errcode = err;
  790. return NULL;
  791. }
  792. struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
  793. int noblock, int *errcode)
  794. {
  795. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
  796. }
  797. static void __lock_sock(struct sock *sk)
  798. {
  799. DEFINE_WAIT(wait);
  800. for(;;) {
  801. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  802. TASK_UNINTERRUPTIBLE);
  803. spin_unlock_bh(&sk->sk_lock.slock);
  804. schedule();
  805. spin_lock_bh(&sk->sk_lock.slock);
  806. if(!sock_owned_by_user(sk))
  807. break;
  808. }
  809. finish_wait(&sk->sk_lock.wq, &wait);
  810. }
  811. static void __release_sock(struct sock *sk)
  812. {
  813. struct sk_buff *skb = sk->sk_backlog.head;
  814. do {
  815. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  816. bh_unlock_sock(sk);
  817. do {
  818. struct sk_buff *next = skb->next;
  819. skb->next = NULL;
  820. sk->sk_backlog_rcv(sk, skb);
  821. /*
  822. * We are in process context here with softirqs
  823. * disabled, use cond_resched_softirq() to preempt.
  824. * This is safe to do because we've taken the backlog
  825. * queue private:
  826. */
  827. cond_resched_softirq();
  828. skb = next;
  829. } while (skb != NULL);
  830. bh_lock_sock(sk);
  831. } while((skb = sk->sk_backlog.head) != NULL);
  832. }
  833. /**
  834. * sk_wait_data - wait for data to arrive at sk_receive_queue
  835. * sk - sock to wait on
  836. * timeo - for how long
  837. *
  838. * Now socket state including sk->sk_err is changed only under lock,
  839. * hence we may omit checks after joining wait queue.
  840. * We check receive queue before schedule() only as optimization;
  841. * it is very likely that release_sock() added new data.
  842. */
  843. int sk_wait_data(struct sock *sk, long *timeo)
  844. {
  845. int rc;
  846. DEFINE_WAIT(wait);
  847. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  848. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  849. rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
  850. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  851. finish_wait(sk->sk_sleep, &wait);
  852. return rc;
  853. }
  854. EXPORT_SYMBOL(sk_wait_data);
  855. /*
  856. * Set of default routines for initialising struct proto_ops when
  857. * the protocol does not support a particular function. In certain
  858. * cases where it makes no sense for a protocol to have a "do nothing"
  859. * function, some default processing is provided.
  860. */
  861. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  862. {
  863. return -EOPNOTSUPP;
  864. }
  865. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  866. int len, int flags)
  867. {
  868. return -EOPNOTSUPP;
  869. }
  870. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  871. {
  872. return -EOPNOTSUPP;
  873. }
  874. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
  875. {
  876. return -EOPNOTSUPP;
  877. }
  878. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  879. int *len, int peer)
  880. {
  881. return -EOPNOTSUPP;
  882. }
  883. unsigned int sock_no_poll(struct file * file, struct socket *sock, poll_table *pt)
  884. {
  885. return 0;
  886. }
  887. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  888. {
  889. return -EOPNOTSUPP;
  890. }
  891. int sock_no_listen(struct socket *sock, int backlog)
  892. {
  893. return -EOPNOTSUPP;
  894. }
  895. int sock_no_shutdown(struct socket *sock, int how)
  896. {
  897. return -EOPNOTSUPP;
  898. }
  899. int sock_no_setsockopt(struct socket *sock, int level, int optname,
  900. char __user *optval, int optlen)
  901. {
  902. return -EOPNOTSUPP;
  903. }
  904. int sock_no_getsockopt(struct socket *sock, int level, int optname,
  905. char __user *optval, int __user *optlen)
  906. {
  907. return -EOPNOTSUPP;
  908. }
  909. int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  910. size_t len)
  911. {
  912. return -EOPNOTSUPP;
  913. }
  914. int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  915. size_t len, int flags)
  916. {
  917. return -EOPNOTSUPP;
  918. }
  919. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  920. {
  921. /* Mirror missing mmap method error code */
  922. return -ENODEV;
  923. }
  924. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  925. {
  926. ssize_t res;
  927. struct msghdr msg = {.msg_flags = flags};
  928. struct kvec iov;
  929. char *kaddr = kmap(page);
  930. iov.iov_base = kaddr + offset;
  931. iov.iov_len = size;
  932. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  933. kunmap(page);
  934. return res;
  935. }
  936. /*
  937. * Default Socket Callbacks
  938. */
  939. static void sock_def_wakeup(struct sock *sk)
  940. {
  941. read_lock(&sk->sk_callback_lock);
  942. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  943. wake_up_interruptible_all(sk->sk_sleep);
  944. read_unlock(&sk->sk_callback_lock);
  945. }
  946. static void sock_def_error_report(struct sock *sk)
  947. {
  948. read_lock(&sk->sk_callback_lock);
  949. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  950. wake_up_interruptible(sk->sk_sleep);
  951. sk_wake_async(sk,0,POLL_ERR);
  952. read_unlock(&sk->sk_callback_lock);
  953. }
  954. static void sock_def_readable(struct sock *sk, int len)
  955. {
  956. read_lock(&sk->sk_callback_lock);
  957. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  958. wake_up_interruptible(sk->sk_sleep);
  959. sk_wake_async(sk,1,POLL_IN);
  960. read_unlock(&sk->sk_callback_lock);
  961. }
  962. static void sock_def_write_space(struct sock *sk)
  963. {
  964. read_lock(&sk->sk_callback_lock);
  965. /* Do not wake up a writer until he can make "significant"
  966. * progress. --DaveM
  967. */
  968. if((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
  969. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  970. wake_up_interruptible(sk->sk_sleep);
  971. /* Should agree with poll, otherwise some programs break */
  972. if (sock_writeable(sk))
  973. sk_wake_async(sk, 2, POLL_OUT);
  974. }
  975. read_unlock(&sk->sk_callback_lock);
  976. }
  977. static void sock_def_destruct(struct sock *sk)
  978. {
  979. if (sk->sk_protinfo)
  980. kfree(sk->sk_protinfo);
  981. }
  982. void sk_send_sigurg(struct sock *sk)
  983. {
  984. if (sk->sk_socket && sk->sk_socket->file)
  985. if (send_sigurg(&sk->sk_socket->file->f_owner))
  986. sk_wake_async(sk, 3, POLL_PRI);
  987. }
  988. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  989. unsigned long expires)
  990. {
  991. if (!mod_timer(timer, expires))
  992. sock_hold(sk);
  993. }
  994. EXPORT_SYMBOL(sk_reset_timer);
  995. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  996. {
  997. if (timer_pending(timer) && del_timer(timer))
  998. __sock_put(sk);
  999. }
  1000. EXPORT_SYMBOL(sk_stop_timer);
  1001. void sock_init_data(struct socket *sock, struct sock *sk)
  1002. {
  1003. skb_queue_head_init(&sk->sk_receive_queue);
  1004. skb_queue_head_init(&sk->sk_write_queue);
  1005. skb_queue_head_init(&sk->sk_error_queue);
  1006. sk->sk_send_head = NULL;
  1007. init_timer(&sk->sk_timer);
  1008. sk->sk_allocation = GFP_KERNEL;
  1009. sk->sk_rcvbuf = sysctl_rmem_default;
  1010. sk->sk_sndbuf = sysctl_wmem_default;
  1011. sk->sk_state = TCP_CLOSE;
  1012. sk->sk_socket = sock;
  1013. sock_set_flag(sk, SOCK_ZAPPED);
  1014. if(sock)
  1015. {
  1016. sk->sk_type = sock->type;
  1017. sk->sk_sleep = &sock->wait;
  1018. sock->sk = sk;
  1019. } else
  1020. sk->sk_sleep = NULL;
  1021. rwlock_init(&sk->sk_dst_lock);
  1022. rwlock_init(&sk->sk_callback_lock);
  1023. sk->sk_state_change = sock_def_wakeup;
  1024. sk->sk_data_ready = sock_def_readable;
  1025. sk->sk_write_space = sock_def_write_space;
  1026. sk->sk_error_report = sock_def_error_report;
  1027. sk->sk_destruct = sock_def_destruct;
  1028. sk->sk_sndmsg_page = NULL;
  1029. sk->sk_sndmsg_off = 0;
  1030. sk->sk_peercred.pid = 0;
  1031. sk->sk_peercred.uid = -1;
  1032. sk->sk_peercred.gid = -1;
  1033. sk->sk_write_pending = 0;
  1034. sk->sk_rcvlowat = 1;
  1035. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  1036. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  1037. sk->sk_stamp.tv_sec = -1L;
  1038. sk->sk_stamp.tv_usec = -1L;
  1039. atomic_set(&sk->sk_refcnt, 1);
  1040. }
  1041. void fastcall lock_sock(struct sock *sk)
  1042. {
  1043. might_sleep();
  1044. spin_lock_bh(&(sk->sk_lock.slock));
  1045. if (sk->sk_lock.owner)
  1046. __lock_sock(sk);
  1047. sk->sk_lock.owner = (void *)1;
  1048. spin_unlock_bh(&(sk->sk_lock.slock));
  1049. }
  1050. EXPORT_SYMBOL(lock_sock);
  1051. void fastcall release_sock(struct sock *sk)
  1052. {
  1053. spin_lock_bh(&(sk->sk_lock.slock));
  1054. if (sk->sk_backlog.tail)
  1055. __release_sock(sk);
  1056. sk->sk_lock.owner = NULL;
  1057. if (waitqueue_active(&(sk->sk_lock.wq)))
  1058. wake_up(&(sk->sk_lock.wq));
  1059. spin_unlock_bh(&(sk->sk_lock.slock));
  1060. }
  1061. EXPORT_SYMBOL(release_sock);
  1062. int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  1063. {
  1064. if (!sock_flag(sk, SOCK_TIMESTAMP))
  1065. sock_enable_timestamp(sk);
  1066. if (sk->sk_stamp.tv_sec == -1)
  1067. return -ENOENT;
  1068. if (sk->sk_stamp.tv_sec == 0)
  1069. do_gettimeofday(&sk->sk_stamp);
  1070. return copy_to_user(userstamp, &sk->sk_stamp, sizeof(struct timeval)) ?
  1071. -EFAULT : 0;
  1072. }
  1073. EXPORT_SYMBOL(sock_get_timestamp);
  1074. void sock_enable_timestamp(struct sock *sk)
  1075. {
  1076. if (!sock_flag(sk, SOCK_TIMESTAMP)) {
  1077. sock_set_flag(sk, SOCK_TIMESTAMP);
  1078. net_enable_timestamp();
  1079. }
  1080. }
  1081. EXPORT_SYMBOL(sock_enable_timestamp);
  1082. /*
  1083. * Get a socket option on an socket.
  1084. *
  1085. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  1086. * asynchronous errors should be reported by getsockopt. We assume
  1087. * this means if you specify SO_ERROR (otherwise whats the point of it).
  1088. */
  1089. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  1090. char __user *optval, int __user *optlen)
  1091. {
  1092. struct sock *sk = sock->sk;
  1093. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1094. }
  1095. EXPORT_SYMBOL(sock_common_getsockopt);
  1096. int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
  1097. struct msghdr *msg, size_t size, int flags)
  1098. {
  1099. struct sock *sk = sock->sk;
  1100. int addr_len = 0;
  1101. int err;
  1102. err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
  1103. flags & ~MSG_DONTWAIT, &addr_len);
  1104. if (err >= 0)
  1105. msg->msg_namelen = addr_len;
  1106. return err;
  1107. }
  1108. EXPORT_SYMBOL(sock_common_recvmsg);
  1109. /*
  1110. * Set socket options on an inet socket.
  1111. */
  1112. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  1113. char __user *optval, int optlen)
  1114. {
  1115. struct sock *sk = sock->sk;
  1116. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1117. }
  1118. EXPORT_SYMBOL(sock_common_setsockopt);
  1119. void sk_common_release(struct sock *sk)
  1120. {
  1121. if (sk->sk_prot->destroy)
  1122. sk->sk_prot->destroy(sk);
  1123. /*
  1124. * Observation: when sock_common_release is called, processes have
  1125. * no access to socket. But net still has.
  1126. * Step one, detach it from networking:
  1127. *
  1128. * A. Remove from hash tables.
  1129. */
  1130. sk->sk_prot->unhash(sk);
  1131. /*
  1132. * In this point socket cannot receive new packets, but it is possible
  1133. * that some packets are in flight because some CPU runs receiver and
  1134. * did hash table lookup before we unhashed socket. They will achieve
  1135. * receive queue and will be purged by socket destructor.
  1136. *
  1137. * Also we still have packets pending on receive queue and probably,
  1138. * our own packets waiting in device queues. sock_destroy will drain
  1139. * receive queue, but transmitted packets will delay socket destruction
  1140. * until the last reference will be released.
  1141. */
  1142. sock_orphan(sk);
  1143. xfrm_sk_free_policy(sk);
  1144. #ifdef INET_REFCNT_DEBUG
  1145. if (atomic_read(&sk->sk_refcnt) != 1)
  1146. printk(KERN_DEBUG "Destruction of the socket %p delayed, c=%d\n",
  1147. sk, atomic_read(&sk->sk_refcnt));
  1148. #endif
  1149. sock_put(sk);
  1150. }
  1151. EXPORT_SYMBOL(sk_common_release);
  1152. static DEFINE_RWLOCK(proto_list_lock);
  1153. static LIST_HEAD(proto_list);
  1154. int proto_register(struct proto *prot, int alloc_slab)
  1155. {
  1156. int rc = -ENOBUFS;
  1157. if (alloc_slab) {
  1158. prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
  1159. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1160. if (prot->slab == NULL) {
  1161. printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
  1162. prot->name);
  1163. goto out;
  1164. }
  1165. }
  1166. write_lock(&proto_list_lock);
  1167. list_add(&prot->node, &proto_list);
  1168. write_unlock(&proto_list_lock);
  1169. rc = 0;
  1170. out:
  1171. return rc;
  1172. }
  1173. EXPORT_SYMBOL(proto_register);
  1174. void proto_unregister(struct proto *prot)
  1175. {
  1176. write_lock(&proto_list_lock);
  1177. if (prot->slab != NULL) {
  1178. kmem_cache_destroy(prot->slab);
  1179. prot->slab = NULL;
  1180. }
  1181. list_del(&prot->node);
  1182. write_unlock(&proto_list_lock);
  1183. }
  1184. EXPORT_SYMBOL(proto_unregister);
  1185. #ifdef CONFIG_PROC_FS
  1186. static inline struct proto *__proto_head(void)
  1187. {
  1188. return list_entry(proto_list.next, struct proto, node);
  1189. }
  1190. static inline struct proto *proto_head(void)
  1191. {
  1192. return list_empty(&proto_list) ? NULL : __proto_head();
  1193. }
  1194. static inline struct proto *proto_next(struct proto *proto)
  1195. {
  1196. return proto->node.next == &proto_list ? NULL :
  1197. list_entry(proto->node.next, struct proto, node);
  1198. }
  1199. static inline struct proto *proto_get_idx(loff_t pos)
  1200. {
  1201. struct proto *proto;
  1202. loff_t i = 0;
  1203. list_for_each_entry(proto, &proto_list, node)
  1204. if (i++ == pos)
  1205. goto out;
  1206. proto = NULL;
  1207. out:
  1208. return proto;
  1209. }
  1210. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  1211. {
  1212. read_lock(&proto_list_lock);
  1213. return *pos ? proto_get_idx(*pos - 1) : SEQ_START_TOKEN;
  1214. }
  1215. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1216. {
  1217. ++*pos;
  1218. return v == SEQ_START_TOKEN ? proto_head() : proto_next(v);
  1219. }
  1220. static void proto_seq_stop(struct seq_file *seq, void *v)
  1221. {
  1222. read_unlock(&proto_list_lock);
  1223. }
  1224. static char proto_method_implemented(const void *method)
  1225. {
  1226. return method == NULL ? 'n' : 'y';
  1227. }
  1228. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  1229. {
  1230. seq_printf(seq, "%-9s %4u %6d %6d %-3s %6u %-3s %-10s "
  1231. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  1232. proto->name,
  1233. proto->obj_size,
  1234. proto->sockets_allocated != NULL ? atomic_read(proto->sockets_allocated) : -1,
  1235. proto->memory_allocated != NULL ? atomic_read(proto->memory_allocated) : -1,
  1236. proto->memory_pressure != NULL ? *proto->memory_pressure ? "yes" : "no" : "NI",
  1237. proto->max_header,
  1238. proto->slab == NULL ? "no" : "yes",
  1239. module_name(proto->owner),
  1240. proto_method_implemented(proto->close),
  1241. proto_method_implemented(proto->connect),
  1242. proto_method_implemented(proto->disconnect),
  1243. proto_method_implemented(proto->accept),
  1244. proto_method_implemented(proto->ioctl),
  1245. proto_method_implemented(proto->init),
  1246. proto_method_implemented(proto->destroy),
  1247. proto_method_implemented(proto->shutdown),
  1248. proto_method_implemented(proto->setsockopt),
  1249. proto_method_implemented(proto->getsockopt),
  1250. proto_method_implemented(proto->sendmsg),
  1251. proto_method_implemented(proto->recvmsg),
  1252. proto_method_implemented(proto->sendpage),
  1253. proto_method_implemented(proto->bind),
  1254. proto_method_implemented(proto->backlog_rcv),
  1255. proto_method_implemented(proto->hash),
  1256. proto_method_implemented(proto->unhash),
  1257. proto_method_implemented(proto->get_port),
  1258. proto_method_implemented(proto->enter_memory_pressure));
  1259. }
  1260. static int proto_seq_show(struct seq_file *seq, void *v)
  1261. {
  1262. if (v == SEQ_START_TOKEN)
  1263. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  1264. "protocol",
  1265. "size",
  1266. "sockets",
  1267. "memory",
  1268. "press",
  1269. "maxhdr",
  1270. "slab",
  1271. "module",
  1272. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  1273. else
  1274. proto_seq_printf(seq, v);
  1275. return 0;
  1276. }
  1277. static struct seq_operations proto_seq_ops = {
  1278. .start = proto_seq_start,
  1279. .next = proto_seq_next,
  1280. .stop = proto_seq_stop,
  1281. .show = proto_seq_show,
  1282. };
  1283. static int proto_seq_open(struct inode *inode, struct file *file)
  1284. {
  1285. return seq_open(file, &proto_seq_ops);
  1286. }
  1287. static struct file_operations proto_seq_fops = {
  1288. .owner = THIS_MODULE,
  1289. .open = proto_seq_open,
  1290. .read = seq_read,
  1291. .llseek = seq_lseek,
  1292. .release = seq_release,
  1293. };
  1294. static int __init proto_init(void)
  1295. {
  1296. /* register /proc/net/protocols */
  1297. return proc_net_fops_create("protocols", S_IRUGO, &proto_seq_fops) == NULL ? -ENOBUFS : 0;
  1298. }
  1299. subsys_initcall(proto_init);
  1300. #endif /* PROC_FS */
  1301. EXPORT_SYMBOL(sk_alloc);
  1302. EXPORT_SYMBOL(sk_free);
  1303. EXPORT_SYMBOL(sk_send_sigurg);
  1304. EXPORT_SYMBOL(sock_alloc_send_skb);
  1305. EXPORT_SYMBOL(sock_init_data);
  1306. EXPORT_SYMBOL(sock_kfree_s);
  1307. EXPORT_SYMBOL(sock_kmalloc);
  1308. EXPORT_SYMBOL(sock_no_accept);
  1309. EXPORT_SYMBOL(sock_no_bind);
  1310. EXPORT_SYMBOL(sock_no_connect);
  1311. EXPORT_SYMBOL(sock_no_getname);
  1312. EXPORT_SYMBOL(sock_no_getsockopt);
  1313. EXPORT_SYMBOL(sock_no_ioctl);
  1314. EXPORT_SYMBOL(sock_no_listen);
  1315. EXPORT_SYMBOL(sock_no_mmap);
  1316. EXPORT_SYMBOL(sock_no_poll);
  1317. EXPORT_SYMBOL(sock_no_recvmsg);
  1318. EXPORT_SYMBOL(sock_no_sendmsg);
  1319. EXPORT_SYMBOL(sock_no_sendpage);
  1320. EXPORT_SYMBOL(sock_no_setsockopt);
  1321. EXPORT_SYMBOL(sock_no_shutdown);
  1322. EXPORT_SYMBOL(sock_no_socketpair);
  1323. EXPORT_SYMBOL(sock_rfree);
  1324. EXPORT_SYMBOL(sock_setsockopt);
  1325. EXPORT_SYMBOL(sock_wfree);
  1326. EXPORT_SYMBOL(sock_wmalloc);
  1327. EXPORT_SYMBOL(sock_i_uid);
  1328. EXPORT_SYMBOL(sock_i_ino);
  1329. #ifdef CONFIG_SYSCTL
  1330. EXPORT_SYMBOL(sysctl_optmem_max);
  1331. EXPORT_SYMBOL(sysctl_rmem_max);
  1332. EXPORT_SYMBOL(sysctl_wmem_max);
  1333. #endif