sock.c 49 KB

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