sock.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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_stream(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. #ifdef CONFIG_COMPAT
  1184. int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
  1185. char __user *optval, int __user *optlen)
  1186. {
  1187. struct sock *sk = sock->sk;
  1188. if (sk->sk_prot->compat_setsockopt != NULL)
  1189. return sk->sk_prot->compat_getsockopt(sk, level, optname,
  1190. optval, optlen);
  1191. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1192. }
  1193. EXPORT_SYMBOL(compat_sock_common_getsockopt);
  1194. #endif
  1195. int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
  1196. struct msghdr *msg, size_t size, int flags)
  1197. {
  1198. struct sock *sk = sock->sk;
  1199. int addr_len = 0;
  1200. int err;
  1201. err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
  1202. flags & ~MSG_DONTWAIT, &addr_len);
  1203. if (err >= 0)
  1204. msg->msg_namelen = addr_len;
  1205. return err;
  1206. }
  1207. EXPORT_SYMBOL(sock_common_recvmsg);
  1208. /*
  1209. * Set socket options on an inet socket.
  1210. */
  1211. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  1212. char __user *optval, int optlen)
  1213. {
  1214. struct sock *sk = sock->sk;
  1215. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1216. }
  1217. EXPORT_SYMBOL(sock_common_setsockopt);
  1218. #ifdef CONFIG_COMPAT
  1219. int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
  1220. char __user *optval, int optlen)
  1221. {
  1222. struct sock *sk = sock->sk;
  1223. if (sk->sk_prot->compat_setsockopt != NULL)
  1224. return sk->sk_prot->compat_setsockopt(sk, level, optname,
  1225. optval, optlen);
  1226. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1227. }
  1228. EXPORT_SYMBOL(compat_sock_common_setsockopt);
  1229. #endif
  1230. void sk_common_release(struct sock *sk)
  1231. {
  1232. if (sk->sk_prot->destroy)
  1233. sk->sk_prot->destroy(sk);
  1234. /*
  1235. * Observation: when sock_common_release is called, processes have
  1236. * no access to socket. But net still has.
  1237. * Step one, detach it from networking:
  1238. *
  1239. * A. Remove from hash tables.
  1240. */
  1241. sk->sk_prot->unhash(sk);
  1242. /*
  1243. * In this point socket cannot receive new packets, but it is possible
  1244. * that some packets are in flight because some CPU runs receiver and
  1245. * did hash table lookup before we unhashed socket. They will achieve
  1246. * receive queue and will be purged by socket destructor.
  1247. *
  1248. * Also we still have packets pending on receive queue and probably,
  1249. * our own packets waiting in device queues. sock_destroy will drain
  1250. * receive queue, but transmitted packets will delay socket destruction
  1251. * until the last reference will be released.
  1252. */
  1253. sock_orphan(sk);
  1254. xfrm_sk_free_policy(sk);
  1255. sk_refcnt_debug_release(sk);
  1256. sock_put(sk);
  1257. }
  1258. EXPORT_SYMBOL(sk_common_release);
  1259. static DEFINE_RWLOCK(proto_list_lock);
  1260. static LIST_HEAD(proto_list);
  1261. int proto_register(struct proto *prot, int alloc_slab)
  1262. {
  1263. char *request_sock_slab_name = NULL;
  1264. char *timewait_sock_slab_name;
  1265. int rc = -ENOBUFS;
  1266. if (alloc_slab) {
  1267. prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
  1268. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1269. if (prot->slab == NULL) {
  1270. printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
  1271. prot->name);
  1272. goto out;
  1273. }
  1274. if (prot->rsk_prot != NULL) {
  1275. static const char mask[] = "request_sock_%s";
  1276. request_sock_slab_name = kmalloc(strlen(prot->name) + sizeof(mask) - 1, GFP_KERNEL);
  1277. if (request_sock_slab_name == NULL)
  1278. goto out_free_sock_slab;
  1279. sprintf(request_sock_slab_name, mask, prot->name);
  1280. prot->rsk_prot->slab = kmem_cache_create(request_sock_slab_name,
  1281. prot->rsk_prot->obj_size, 0,
  1282. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1283. if (prot->rsk_prot->slab == NULL) {
  1284. printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
  1285. prot->name);
  1286. goto out_free_request_sock_slab_name;
  1287. }
  1288. }
  1289. if (prot->twsk_prot != NULL) {
  1290. static const char mask[] = "tw_sock_%s";
  1291. timewait_sock_slab_name = kmalloc(strlen(prot->name) + sizeof(mask) - 1, GFP_KERNEL);
  1292. if (timewait_sock_slab_name == NULL)
  1293. goto out_free_request_sock_slab;
  1294. sprintf(timewait_sock_slab_name, mask, prot->name);
  1295. prot->twsk_prot->twsk_slab =
  1296. kmem_cache_create(timewait_sock_slab_name,
  1297. prot->twsk_prot->twsk_obj_size,
  1298. 0, SLAB_HWCACHE_ALIGN,
  1299. NULL, NULL);
  1300. if (prot->twsk_prot->twsk_slab == NULL)
  1301. goto out_free_timewait_sock_slab_name;
  1302. }
  1303. }
  1304. write_lock(&proto_list_lock);
  1305. list_add(&prot->node, &proto_list);
  1306. write_unlock(&proto_list_lock);
  1307. rc = 0;
  1308. out:
  1309. return rc;
  1310. out_free_timewait_sock_slab_name:
  1311. kfree(timewait_sock_slab_name);
  1312. out_free_request_sock_slab:
  1313. if (prot->rsk_prot && prot->rsk_prot->slab) {
  1314. kmem_cache_destroy(prot->rsk_prot->slab);
  1315. prot->rsk_prot->slab = NULL;
  1316. }
  1317. out_free_request_sock_slab_name:
  1318. kfree(request_sock_slab_name);
  1319. out_free_sock_slab:
  1320. kmem_cache_destroy(prot->slab);
  1321. prot->slab = NULL;
  1322. goto out;
  1323. }
  1324. EXPORT_SYMBOL(proto_register);
  1325. void proto_unregister(struct proto *prot)
  1326. {
  1327. write_lock(&proto_list_lock);
  1328. list_del(&prot->node);
  1329. write_unlock(&proto_list_lock);
  1330. if (prot->slab != NULL) {
  1331. kmem_cache_destroy(prot->slab);
  1332. prot->slab = NULL;
  1333. }
  1334. if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
  1335. const char *name = kmem_cache_name(prot->rsk_prot->slab);
  1336. kmem_cache_destroy(prot->rsk_prot->slab);
  1337. kfree(name);
  1338. prot->rsk_prot->slab = NULL;
  1339. }
  1340. if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
  1341. const char *name = kmem_cache_name(prot->twsk_prot->twsk_slab);
  1342. kmem_cache_destroy(prot->twsk_prot->twsk_slab);
  1343. kfree(name);
  1344. prot->twsk_prot->twsk_slab = NULL;
  1345. }
  1346. }
  1347. EXPORT_SYMBOL(proto_unregister);
  1348. #ifdef CONFIG_PROC_FS
  1349. static inline struct proto *__proto_head(void)
  1350. {
  1351. return list_entry(proto_list.next, struct proto, node);
  1352. }
  1353. static inline struct proto *proto_head(void)
  1354. {
  1355. return list_empty(&proto_list) ? NULL : __proto_head();
  1356. }
  1357. static inline struct proto *proto_next(struct proto *proto)
  1358. {
  1359. return proto->node.next == &proto_list ? NULL :
  1360. list_entry(proto->node.next, struct proto, node);
  1361. }
  1362. static inline struct proto *proto_get_idx(loff_t pos)
  1363. {
  1364. struct proto *proto;
  1365. loff_t i = 0;
  1366. list_for_each_entry(proto, &proto_list, node)
  1367. if (i++ == pos)
  1368. goto out;
  1369. proto = NULL;
  1370. out:
  1371. return proto;
  1372. }
  1373. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  1374. {
  1375. read_lock(&proto_list_lock);
  1376. return *pos ? proto_get_idx(*pos - 1) : SEQ_START_TOKEN;
  1377. }
  1378. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1379. {
  1380. ++*pos;
  1381. return v == SEQ_START_TOKEN ? proto_head() : proto_next(v);
  1382. }
  1383. static void proto_seq_stop(struct seq_file *seq, void *v)
  1384. {
  1385. read_unlock(&proto_list_lock);
  1386. }
  1387. static char proto_method_implemented(const void *method)
  1388. {
  1389. return method == NULL ? 'n' : 'y';
  1390. }
  1391. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  1392. {
  1393. seq_printf(seq, "%-9s %4u %6d %6d %-3s %6u %-3s %-10s "
  1394. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  1395. proto->name,
  1396. proto->obj_size,
  1397. proto->sockets_allocated != NULL ? atomic_read(proto->sockets_allocated) : -1,
  1398. proto->memory_allocated != NULL ? atomic_read(proto->memory_allocated) : -1,
  1399. proto->memory_pressure != NULL ? *proto->memory_pressure ? "yes" : "no" : "NI",
  1400. proto->max_header,
  1401. proto->slab == NULL ? "no" : "yes",
  1402. module_name(proto->owner),
  1403. proto_method_implemented(proto->close),
  1404. proto_method_implemented(proto->connect),
  1405. proto_method_implemented(proto->disconnect),
  1406. proto_method_implemented(proto->accept),
  1407. proto_method_implemented(proto->ioctl),
  1408. proto_method_implemented(proto->init),
  1409. proto_method_implemented(proto->destroy),
  1410. proto_method_implemented(proto->shutdown),
  1411. proto_method_implemented(proto->setsockopt),
  1412. proto_method_implemented(proto->getsockopt),
  1413. proto_method_implemented(proto->sendmsg),
  1414. proto_method_implemented(proto->recvmsg),
  1415. proto_method_implemented(proto->sendpage),
  1416. proto_method_implemented(proto->bind),
  1417. proto_method_implemented(proto->backlog_rcv),
  1418. proto_method_implemented(proto->hash),
  1419. proto_method_implemented(proto->unhash),
  1420. proto_method_implemented(proto->get_port),
  1421. proto_method_implemented(proto->enter_memory_pressure));
  1422. }
  1423. static int proto_seq_show(struct seq_file *seq, void *v)
  1424. {
  1425. if (v == SEQ_START_TOKEN)
  1426. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  1427. "protocol",
  1428. "size",
  1429. "sockets",
  1430. "memory",
  1431. "press",
  1432. "maxhdr",
  1433. "slab",
  1434. "module",
  1435. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  1436. else
  1437. proto_seq_printf(seq, v);
  1438. return 0;
  1439. }
  1440. static struct seq_operations proto_seq_ops = {
  1441. .start = proto_seq_start,
  1442. .next = proto_seq_next,
  1443. .stop = proto_seq_stop,
  1444. .show = proto_seq_show,
  1445. };
  1446. static int proto_seq_open(struct inode *inode, struct file *file)
  1447. {
  1448. return seq_open(file, &proto_seq_ops);
  1449. }
  1450. static struct file_operations proto_seq_fops = {
  1451. .owner = THIS_MODULE,
  1452. .open = proto_seq_open,
  1453. .read = seq_read,
  1454. .llseek = seq_lseek,
  1455. .release = seq_release,
  1456. };
  1457. static int __init proto_init(void)
  1458. {
  1459. /* register /proc/net/protocols */
  1460. return proc_net_fops_create("protocols", S_IRUGO, &proto_seq_fops) == NULL ? -ENOBUFS : 0;
  1461. }
  1462. subsys_initcall(proto_init);
  1463. #endif /* PROC_FS */
  1464. EXPORT_SYMBOL(sk_alloc);
  1465. EXPORT_SYMBOL(sk_free);
  1466. EXPORT_SYMBOL(sk_send_sigurg);
  1467. EXPORT_SYMBOL(sock_alloc_send_skb);
  1468. EXPORT_SYMBOL(sock_init_data);
  1469. EXPORT_SYMBOL(sock_kfree_s);
  1470. EXPORT_SYMBOL(sock_kmalloc);
  1471. EXPORT_SYMBOL(sock_no_accept);
  1472. EXPORT_SYMBOL(sock_no_bind);
  1473. EXPORT_SYMBOL(sock_no_connect);
  1474. EXPORT_SYMBOL(sock_no_getname);
  1475. EXPORT_SYMBOL(sock_no_getsockopt);
  1476. EXPORT_SYMBOL(sock_no_ioctl);
  1477. EXPORT_SYMBOL(sock_no_listen);
  1478. EXPORT_SYMBOL(sock_no_mmap);
  1479. EXPORT_SYMBOL(sock_no_poll);
  1480. EXPORT_SYMBOL(sock_no_recvmsg);
  1481. EXPORT_SYMBOL(sock_no_sendmsg);
  1482. EXPORT_SYMBOL(sock_no_sendpage);
  1483. EXPORT_SYMBOL(sock_no_setsockopt);
  1484. EXPORT_SYMBOL(sock_no_shutdown);
  1485. EXPORT_SYMBOL(sock_no_socketpair);
  1486. EXPORT_SYMBOL(sock_rfree);
  1487. EXPORT_SYMBOL(sock_setsockopt);
  1488. EXPORT_SYMBOL(sock_wfree);
  1489. EXPORT_SYMBOL(sock_wmalloc);
  1490. EXPORT_SYMBOL(sock_i_uid);
  1491. EXPORT_SYMBOL(sock_i_ino);
  1492. EXPORT_SYMBOL(sysctl_optmem_max);
  1493. #ifdef CONFIG_SYSCTL
  1494. EXPORT_SYMBOL(sysctl_rmem_max);
  1495. EXPORT_SYMBOL(sysctl_wmem_max);
  1496. #endif