sock.c 50 KB

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