sock.c 50 KB

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