sock.c 49 KB

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