sock.c 42 KB

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