sock.c 39 KB

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