sock.c 37 KB

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