sock.c 45 KB

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