sock.c 39 KB

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