sock.c 45 KB

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