sock.c 45 KB

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