sock.c 49 KB

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