sock.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Alan Cox, <A.Cox@swansea.ac.uk>
  14. *
  15. * Fixes:
  16. * Alan Cox : Numerous verify_area() problems
  17. * Alan Cox : Connecting on a connecting socket
  18. * now returns an error for tcp.
  19. * Alan Cox : sock->protocol is set correctly.
  20. * and is not sometimes left as 0.
  21. * Alan Cox : connect handles icmp errors on a
  22. * connect properly. Unfortunately there
  23. * is a restart syscall nasty there. I
  24. * can't match BSD without hacking the C
  25. * library. Ideas urgently sought!
  26. * Alan Cox : Disallow bind() to addresses that are
  27. * not ours - especially broadcast ones!!
  28. * Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
  29. * Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
  30. * instead they leave that for the DESTROY timer.
  31. * Alan Cox : Clean up error flag in accept
  32. * Alan Cox : TCP ack handling is buggy, the DESTROY timer
  33. * was buggy. Put a remove_sock() in the handler
  34. * for memory when we hit 0. Also altered the timer
  35. * code. The ACK stuff can wait and needs major
  36. * TCP layer surgery.
  37. * Alan Cox : Fixed TCP ack bug, removed remove sock
  38. * and fixed timer/inet_bh race.
  39. * Alan Cox : Added zapped flag for TCP
  40. * Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
  41. * Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  42. * Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
  43. * Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
  44. * Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
  45. * Rick Sladkey : Relaxed UDP rules for matching packets.
  46. * C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
  47. * Pauline Middelink : identd support
  48. * Alan Cox : Fixed connect() taking signals I think.
  49. * Alan Cox : SO_LINGER supported
  50. * Alan Cox : Error reporting fixes
  51. * Anonymous : inet_create tidied up (sk->reuse setting)
  52. * Alan Cox : inet sockets don't set sk->type!
  53. * Alan Cox : Split socket option code
  54. * Alan Cox : Callbacks
  55. * Alan Cox : Nagle flag for Charles & Johannes stuff
  56. * Alex : Removed restriction on inet fioctl
  57. * Alan Cox : Splitting INET from NET core
  58. * Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
  59. * Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
  60. * Alan Cox : Split IP from generic code
  61. * Alan Cox : New kfree_skbmem()
  62. * Alan Cox : Make SO_DEBUG superuser only.
  63. * Alan Cox : Allow anyone to clear SO_DEBUG
  64. * (compatibility fix)
  65. * Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
  66. * Alan Cox : Allocator for a socket is settable.
  67. * Alan Cox : SO_ERROR includes soft errors.
  68. * Alan Cox : Allow NULL arguments on some SO_ opts
  69. * Alan Cox : Generic socket allocation to make hooks
  70. * easier (suggested by Craig Metz).
  71. * Michael Pall : SO_ERROR returns positive errno again
  72. * Steve Whitehouse: Added default destructor to free
  73. * protocol private data.
  74. * Steve Whitehouse: Added various other default routines
  75. * common to several socket families.
  76. * Chris Evans : Call suser() check last on F_SETOWN
  77. * Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
  78. * Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
  79. * Andi Kleen : Fix write_space callback
  80. * Chris Evans : Security fixes - signedness again
  81. * Arnaldo C. Melo : cleanups, use skb_queue_purge
  82. *
  83. * To Fix:
  84. *
  85. *
  86. * This program is free software; you can redistribute it and/or
  87. * modify it under the terms of the GNU General Public License
  88. * as published by the Free Software Foundation; either version
  89. * 2 of the License, or (at your option) any later version.
  90. */
  91. #include <linux/capability.h>
  92. #include <linux/errno.h>
  93. #include <linux/types.h>
  94. #include <linux/socket.h>
  95. #include <linux/in.h>
  96. #include <linux/kernel.h>
  97. #include <linux/module.h>
  98. #include <linux/proc_fs.h>
  99. #include <linux/seq_file.h>
  100. #include <linux/sched.h>
  101. #include <linux/timer.h>
  102. #include <linux/string.h>
  103. #include <linux/sockios.h>
  104. #include <linux/net.h>
  105. #include <linux/mm.h>
  106. #include <linux/slab.h>
  107. #include <linux/interrupt.h>
  108. #include <linux/poll.h>
  109. #include <linux/tcp.h>
  110. #include <linux/init.h>
  111. #include <linux/highmem.h>
  112. #include <asm/uaccess.h>
  113. #include <asm/system.h>
  114. #include <linux/netdevice.h>
  115. #include <net/protocol.h>
  116. #include <linux/skbuff.h>
  117. #include <net/net_namespace.h>
  118. #include <net/request_sock.h>
  119. #include <net/sock.h>
  120. #include <linux/net_tstamp.h>
  121. #include <net/xfrm.h>
  122. #include <linux/ipsec.h>
  123. #include <net/cls_cgroup.h>
  124. #include <linux/filter.h>
  125. #ifdef CONFIG_INET
  126. #include <net/tcp.h>
  127. #endif
  128. /*
  129. * Each address family might have different locking rules, so we have
  130. * one slock key per address family:
  131. */
  132. static struct lock_class_key af_family_keys[AF_MAX];
  133. static struct lock_class_key af_family_slock_keys[AF_MAX];
  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 *const 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-AF_RDS" , "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-AF_CAN" ,
  150. "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" ,
  151. "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" ,
  152. "sk_lock-AF_IEEE802154",
  153. "sk_lock-AF_MAX"
  154. };
  155. static const char *const af_family_slock_key_strings[AF_MAX+1] = {
  156. "slock-AF_UNSPEC", "slock-AF_UNIX" , "slock-AF_INET" ,
  157. "slock-AF_AX25" , "slock-AF_IPX" , "slock-AF_APPLETALK",
  158. "slock-AF_NETROM", "slock-AF_BRIDGE" , "slock-AF_ATMPVC" ,
  159. "slock-AF_X25" , "slock-AF_INET6" , "slock-AF_ROSE" ,
  160. "slock-AF_DECnet", "slock-AF_NETBEUI" , "slock-AF_SECURITY" ,
  161. "slock-AF_KEY" , "slock-AF_NETLINK" , "slock-AF_PACKET" ,
  162. "slock-AF_ASH" , "slock-AF_ECONET" , "slock-AF_ATMSVC" ,
  163. "slock-AF_RDS" , "slock-AF_SNA" , "slock-AF_IRDA" ,
  164. "slock-AF_PPPOX" , "slock-AF_WANPIPE" , "slock-AF_LLC" ,
  165. "slock-27" , "slock-28" , "slock-AF_CAN" ,
  166. "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" ,
  167. "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" ,
  168. "slock-AF_IEEE802154",
  169. "slock-AF_MAX"
  170. };
  171. static const char *const af_family_clock_key_strings[AF_MAX+1] = {
  172. "clock-AF_UNSPEC", "clock-AF_UNIX" , "clock-AF_INET" ,
  173. "clock-AF_AX25" , "clock-AF_IPX" , "clock-AF_APPLETALK",
  174. "clock-AF_NETROM", "clock-AF_BRIDGE" , "clock-AF_ATMPVC" ,
  175. "clock-AF_X25" , "clock-AF_INET6" , "clock-AF_ROSE" ,
  176. "clock-AF_DECnet", "clock-AF_NETBEUI" , "clock-AF_SECURITY" ,
  177. "clock-AF_KEY" , "clock-AF_NETLINK" , "clock-AF_PACKET" ,
  178. "clock-AF_ASH" , "clock-AF_ECONET" , "clock-AF_ATMSVC" ,
  179. "clock-AF_RDS" , "clock-AF_SNA" , "clock-AF_IRDA" ,
  180. "clock-AF_PPPOX" , "clock-AF_WANPIPE" , "clock-AF_LLC" ,
  181. "clock-27" , "clock-28" , "clock-AF_CAN" ,
  182. "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" ,
  183. "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" ,
  184. "clock-AF_IEEE802154",
  185. "clock-AF_MAX"
  186. };
  187. /*
  188. * sk_callback_lock locking rules are per-address-family,
  189. * so split the lock classes by using a per-AF key:
  190. */
  191. static struct lock_class_key af_callback_keys[AF_MAX];
  192. /* Take into consideration the size of the struct sk_buff overhead in the
  193. * determination of these values, since that is non-constant across
  194. * platforms. This makes socket queueing behavior and performance
  195. * not depend upon such differences.
  196. */
  197. #define _SK_MEM_PACKETS 256
  198. #define _SK_MEM_OVERHEAD (sizeof(struct sk_buff) + 256)
  199. #define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  200. #define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  201. /* Run time adjustable parameters. */
  202. __u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
  203. __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
  204. __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
  205. __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
  206. /* Maximal space eaten by iovec or ancilliary data plus some space */
  207. int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
  208. EXPORT_SYMBOL(sysctl_optmem_max);
  209. #if defined(CONFIG_CGROUPS) && !defined(CONFIG_NET_CLS_CGROUP)
  210. int net_cls_subsys_id = -1;
  211. EXPORT_SYMBOL_GPL(net_cls_subsys_id);
  212. #endif
  213. static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
  214. {
  215. struct timeval tv;
  216. if (optlen < sizeof(tv))
  217. return -EINVAL;
  218. if (copy_from_user(&tv, optval, sizeof(tv)))
  219. return -EFAULT;
  220. if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
  221. return -EDOM;
  222. if (tv.tv_sec < 0) {
  223. static int warned __read_mostly;
  224. *timeo_p = 0;
  225. if (warned < 10 && net_ratelimit()) {
  226. warned++;
  227. printk(KERN_INFO "sock_set_timeout: `%s' (pid %d) "
  228. "tries to set negative timeout\n",
  229. current->comm, task_pid_nr(current));
  230. }
  231. return 0;
  232. }
  233. *timeo_p = MAX_SCHEDULE_TIMEOUT;
  234. if (tv.tv_sec == 0 && tv.tv_usec == 0)
  235. return 0;
  236. if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
  237. *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
  238. return 0;
  239. }
  240. static void sock_warn_obsolete_bsdism(const char *name)
  241. {
  242. static int warned;
  243. static char warncomm[TASK_COMM_LEN];
  244. if (strcmp(warncomm, current->comm) && warned < 5) {
  245. strcpy(warncomm, current->comm);
  246. printk(KERN_WARNING "process `%s' is using obsolete "
  247. "%s SO_BSDCOMPAT\n", warncomm, name);
  248. warned++;
  249. }
  250. }
  251. static void sock_disable_timestamp(struct sock *sk, int flag)
  252. {
  253. if (sock_flag(sk, flag)) {
  254. sock_reset_flag(sk, flag);
  255. if (!sock_flag(sk, SOCK_TIMESTAMP) &&
  256. !sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE)) {
  257. net_disable_timestamp();
  258. }
  259. }
  260. }
  261. int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  262. {
  263. int err;
  264. int skb_len;
  265. unsigned long flags;
  266. struct sk_buff_head *list = &sk->sk_receive_queue;
  267. /* Cast sk->rcvbuf to unsigned... It's pointless, but reduces
  268. number of warnings when compiling with -W --ANK
  269. */
  270. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  271. (unsigned)sk->sk_rcvbuf) {
  272. atomic_inc(&sk->sk_drops);
  273. return -ENOMEM;
  274. }
  275. err = sk_filter(sk, skb);
  276. if (err)
  277. return err;
  278. if (!sk_rmem_schedule(sk, skb->truesize)) {
  279. atomic_inc(&sk->sk_drops);
  280. return -ENOBUFS;
  281. }
  282. skb->dev = NULL;
  283. skb_set_owner_r(skb, sk);
  284. /* Cache the SKB length before we tack it onto the receive
  285. * queue. Once it is added it no longer belongs to us and
  286. * may be freed by other threads of control pulling packets
  287. * from the queue.
  288. */
  289. skb_len = skb->len;
  290. /* we escape from rcu protected region, make sure we dont leak
  291. * a norefcounted dst
  292. */
  293. skb_dst_force(skb);
  294. spin_lock_irqsave(&list->lock, flags);
  295. skb->dropcount = atomic_read(&sk->sk_drops);
  296. __skb_queue_tail(list, skb);
  297. spin_unlock_irqrestore(&list->lock, flags);
  298. if (!sock_flag(sk, SOCK_DEAD))
  299. sk->sk_data_ready(sk, skb_len);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(sock_queue_rcv_skb);
  303. int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested)
  304. {
  305. int rc = NET_RX_SUCCESS;
  306. if (sk_filter(sk, skb))
  307. goto discard_and_relse;
  308. skb->dev = NULL;
  309. if (sk_rcvqueues_full(sk, skb)) {
  310. atomic_inc(&sk->sk_drops);
  311. goto discard_and_relse;
  312. }
  313. if (nested)
  314. bh_lock_sock_nested(sk);
  315. else
  316. bh_lock_sock(sk);
  317. if (!sock_owned_by_user(sk)) {
  318. /*
  319. * trylock + unlock semantics:
  320. */
  321. mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
  322. rc = sk_backlog_rcv(sk, skb);
  323. mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
  324. } else if (sk_add_backlog(sk, skb)) {
  325. bh_unlock_sock(sk);
  326. atomic_inc(&sk->sk_drops);
  327. goto discard_and_relse;
  328. }
  329. bh_unlock_sock(sk);
  330. out:
  331. sock_put(sk);
  332. return rc;
  333. discard_and_relse:
  334. kfree_skb(skb);
  335. goto out;
  336. }
  337. EXPORT_SYMBOL(sk_receive_skb);
  338. void sk_reset_txq(struct sock *sk)
  339. {
  340. sk_tx_queue_clear(sk);
  341. }
  342. EXPORT_SYMBOL(sk_reset_txq);
  343. struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
  344. {
  345. struct dst_entry *dst = __sk_dst_get(sk);
  346. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  347. sk_tx_queue_clear(sk);
  348. rcu_assign_pointer(sk->sk_dst_cache, NULL);
  349. dst_release(dst);
  350. return NULL;
  351. }
  352. return dst;
  353. }
  354. EXPORT_SYMBOL(__sk_dst_check);
  355. struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
  356. {
  357. struct dst_entry *dst = sk_dst_get(sk);
  358. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  359. sk_dst_reset(sk);
  360. dst_release(dst);
  361. return NULL;
  362. }
  363. return dst;
  364. }
  365. EXPORT_SYMBOL(sk_dst_check);
  366. static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen)
  367. {
  368. int ret = -ENOPROTOOPT;
  369. #ifdef CONFIG_NETDEVICES
  370. struct net *net = sock_net(sk);
  371. char devname[IFNAMSIZ];
  372. int index;
  373. /* Sorry... */
  374. ret = -EPERM;
  375. if (!capable(CAP_NET_RAW))
  376. goto out;
  377. ret = -EINVAL;
  378. if (optlen < 0)
  379. goto out;
  380. /* Bind this socket to a particular device like "eth0",
  381. * as specified in the passed interface name. If the
  382. * name is "" or the option length is zero the socket
  383. * is not bound.
  384. */
  385. if (optlen > IFNAMSIZ - 1)
  386. optlen = IFNAMSIZ - 1;
  387. memset(devname, 0, sizeof(devname));
  388. ret = -EFAULT;
  389. if (copy_from_user(devname, optval, optlen))
  390. goto out;
  391. index = 0;
  392. if (devname[0] != '\0') {
  393. struct net_device *dev;
  394. rcu_read_lock();
  395. dev = dev_get_by_name_rcu(net, devname);
  396. if (dev)
  397. index = dev->ifindex;
  398. rcu_read_unlock();
  399. ret = -ENODEV;
  400. if (!dev)
  401. goto out;
  402. }
  403. lock_sock(sk);
  404. sk->sk_bound_dev_if = index;
  405. sk_dst_reset(sk);
  406. release_sock(sk);
  407. ret = 0;
  408. out:
  409. #endif
  410. return ret;
  411. }
  412. static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
  413. {
  414. if (valbool)
  415. sock_set_flag(sk, bit);
  416. else
  417. sock_reset_flag(sk, bit);
  418. }
  419. /*
  420. * This is meant for all protocols to use and covers goings on
  421. * at the socket level. Everything here is generic.
  422. */
  423. int sock_setsockopt(struct socket *sock, int level, int optname,
  424. char __user *optval, unsigned int optlen)
  425. {
  426. struct sock *sk = sock->sk;
  427. int val;
  428. int valbool;
  429. struct linger ling;
  430. int ret = 0;
  431. /*
  432. * Options without arguments
  433. */
  434. if (optname == SO_BINDTODEVICE)
  435. return sock_bindtodevice(sk, optval, optlen);
  436. if (optlen < sizeof(int))
  437. return -EINVAL;
  438. if (get_user(val, (int __user *)optval))
  439. return -EFAULT;
  440. valbool = val ? 1 : 0;
  441. lock_sock(sk);
  442. switch (optname) {
  443. case SO_DEBUG:
  444. if (val && !capable(CAP_NET_ADMIN))
  445. ret = -EACCES;
  446. else
  447. sock_valbool_flag(sk, SOCK_DBG, valbool);
  448. break;
  449. case SO_REUSEADDR:
  450. sk->sk_reuse = valbool;
  451. break;
  452. case SO_TYPE:
  453. case SO_PROTOCOL:
  454. case SO_DOMAIN:
  455. case SO_ERROR:
  456. ret = -ENOPROTOOPT;
  457. break;
  458. case SO_DONTROUTE:
  459. sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
  460. break;
  461. case SO_BROADCAST:
  462. sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
  463. break;
  464. case SO_SNDBUF:
  465. /* Don't error on this BSD doesn't and if you think
  466. about it this is right. Otherwise apps have to
  467. play 'guess the biggest size' games. RCVBUF/SNDBUF
  468. are treated in BSD as hints */
  469. if (val > sysctl_wmem_max)
  470. val = sysctl_wmem_max;
  471. set_sndbuf:
  472. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  473. if ((val * 2) < SOCK_MIN_SNDBUF)
  474. sk->sk_sndbuf = SOCK_MIN_SNDBUF;
  475. else
  476. sk->sk_sndbuf = val * 2;
  477. /*
  478. * Wake up sending tasks if we
  479. * upped the value.
  480. */
  481. sk->sk_write_space(sk);
  482. break;
  483. case SO_SNDBUFFORCE:
  484. if (!capable(CAP_NET_ADMIN)) {
  485. ret = -EPERM;
  486. break;
  487. }
  488. goto set_sndbuf;
  489. case SO_RCVBUF:
  490. /* Don't error on this BSD doesn't and if you think
  491. about it this is right. Otherwise apps have to
  492. play 'guess the biggest size' games. RCVBUF/SNDBUF
  493. are treated in BSD as hints */
  494. if (val > sysctl_rmem_max)
  495. val = sysctl_rmem_max;
  496. set_rcvbuf:
  497. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  498. /*
  499. * We double it on the way in to account for
  500. * "struct sk_buff" etc. overhead. Applications
  501. * assume that the SO_RCVBUF setting they make will
  502. * allow that much actual data to be received on that
  503. * socket.
  504. *
  505. * Applications are unaware that "struct sk_buff" and
  506. * other overheads allocate from the receive buffer
  507. * during socket buffer allocation.
  508. *
  509. * And after considering the possible alternatives,
  510. * returning the value we actually used in getsockopt
  511. * is the most desirable behavior.
  512. */
  513. if ((val * 2) < SOCK_MIN_RCVBUF)
  514. sk->sk_rcvbuf = SOCK_MIN_RCVBUF;
  515. else
  516. sk->sk_rcvbuf = val * 2;
  517. break;
  518. case SO_RCVBUFFORCE:
  519. if (!capable(CAP_NET_ADMIN)) {
  520. ret = -EPERM;
  521. break;
  522. }
  523. goto set_rcvbuf;
  524. case SO_KEEPALIVE:
  525. #ifdef CONFIG_INET
  526. if (sk->sk_protocol == IPPROTO_TCP)
  527. tcp_set_keepalive(sk, valbool);
  528. #endif
  529. sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
  530. break;
  531. case SO_OOBINLINE:
  532. sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
  533. break;
  534. case SO_NO_CHECK:
  535. sk->sk_no_check = valbool;
  536. break;
  537. case SO_PRIORITY:
  538. if ((val >= 0 && val <= 6) || capable(CAP_NET_ADMIN))
  539. sk->sk_priority = val;
  540. else
  541. ret = -EPERM;
  542. break;
  543. case SO_LINGER:
  544. if (optlen < sizeof(ling)) {
  545. ret = -EINVAL; /* 1003.1g */
  546. break;
  547. }
  548. if (copy_from_user(&ling, optval, sizeof(ling))) {
  549. ret = -EFAULT;
  550. break;
  551. }
  552. if (!ling.l_onoff)
  553. sock_reset_flag(sk, SOCK_LINGER);
  554. else {
  555. #if (BITS_PER_LONG == 32)
  556. if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
  557. sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
  558. else
  559. #endif
  560. sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
  561. sock_set_flag(sk, SOCK_LINGER);
  562. }
  563. break;
  564. case SO_BSDCOMPAT:
  565. sock_warn_obsolete_bsdism("setsockopt");
  566. break;
  567. case SO_PASSCRED:
  568. if (valbool)
  569. set_bit(SOCK_PASSCRED, &sock->flags);
  570. else
  571. clear_bit(SOCK_PASSCRED, &sock->flags);
  572. break;
  573. case SO_TIMESTAMP:
  574. case SO_TIMESTAMPNS:
  575. if (valbool) {
  576. if (optname == SO_TIMESTAMP)
  577. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  578. else
  579. sock_set_flag(sk, SOCK_RCVTSTAMPNS);
  580. sock_set_flag(sk, SOCK_RCVTSTAMP);
  581. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  582. } else {
  583. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  584. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  585. }
  586. break;
  587. case SO_TIMESTAMPING:
  588. if (val & ~SOF_TIMESTAMPING_MASK) {
  589. ret = -EINVAL;
  590. break;
  591. }
  592. sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE,
  593. val & SOF_TIMESTAMPING_TX_HARDWARE);
  594. sock_valbool_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE,
  595. val & SOF_TIMESTAMPING_TX_SOFTWARE);
  596. sock_valbool_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE,
  597. val & SOF_TIMESTAMPING_RX_HARDWARE);
  598. if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
  599. sock_enable_timestamp(sk,
  600. SOCK_TIMESTAMPING_RX_SOFTWARE);
  601. else
  602. sock_disable_timestamp(sk,
  603. SOCK_TIMESTAMPING_RX_SOFTWARE);
  604. sock_valbool_flag(sk, SOCK_TIMESTAMPING_SOFTWARE,
  605. val & SOF_TIMESTAMPING_SOFTWARE);
  606. sock_valbool_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE,
  607. val & SOF_TIMESTAMPING_SYS_HARDWARE);
  608. sock_valbool_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE,
  609. val & SOF_TIMESTAMPING_RAW_HARDWARE);
  610. break;
  611. case SO_RCVLOWAT:
  612. if (val < 0)
  613. val = INT_MAX;
  614. sk->sk_rcvlowat = val ? : 1;
  615. break;
  616. case SO_RCVTIMEO:
  617. ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
  618. break;
  619. case SO_SNDTIMEO:
  620. ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
  621. break;
  622. case SO_ATTACH_FILTER:
  623. ret = -EINVAL;
  624. if (optlen == sizeof(struct sock_fprog)) {
  625. struct sock_fprog fprog;
  626. ret = -EFAULT;
  627. if (copy_from_user(&fprog, optval, sizeof(fprog)))
  628. break;
  629. ret = sk_attach_filter(&fprog, sk);
  630. }
  631. break;
  632. case SO_DETACH_FILTER:
  633. ret = sk_detach_filter(sk);
  634. break;
  635. case SO_PASSSEC:
  636. if (valbool)
  637. set_bit(SOCK_PASSSEC, &sock->flags);
  638. else
  639. clear_bit(SOCK_PASSSEC, &sock->flags);
  640. break;
  641. case SO_MARK:
  642. if (!capable(CAP_NET_ADMIN))
  643. ret = -EPERM;
  644. else
  645. sk->sk_mark = val;
  646. break;
  647. /* We implement the SO_SNDLOWAT etc to
  648. not be settable (1003.1g 5.3) */
  649. case SO_RXQ_OVFL:
  650. if (valbool)
  651. sock_set_flag(sk, SOCK_RXQ_OVFL);
  652. else
  653. sock_reset_flag(sk, SOCK_RXQ_OVFL);
  654. break;
  655. default:
  656. ret = -ENOPROTOOPT;
  657. break;
  658. }
  659. release_sock(sk);
  660. return ret;
  661. }
  662. EXPORT_SYMBOL(sock_setsockopt);
  663. int sock_getsockopt(struct socket *sock, int level, int optname,
  664. char __user *optval, int __user *optlen)
  665. {
  666. struct sock *sk = sock->sk;
  667. union {
  668. int val;
  669. struct linger ling;
  670. struct timeval tm;
  671. } v;
  672. int lv = sizeof(int);
  673. int len;
  674. if (get_user(len, optlen))
  675. return -EFAULT;
  676. if (len < 0)
  677. return -EINVAL;
  678. memset(&v, 0, sizeof(v));
  679. switch (optname) {
  680. case SO_DEBUG:
  681. v.val = sock_flag(sk, SOCK_DBG);
  682. break;
  683. case SO_DONTROUTE:
  684. v.val = sock_flag(sk, SOCK_LOCALROUTE);
  685. break;
  686. case SO_BROADCAST:
  687. v.val = !!sock_flag(sk, SOCK_BROADCAST);
  688. break;
  689. case SO_SNDBUF:
  690. v.val = sk->sk_sndbuf;
  691. break;
  692. case SO_RCVBUF:
  693. v.val = sk->sk_rcvbuf;
  694. break;
  695. case SO_REUSEADDR:
  696. v.val = sk->sk_reuse;
  697. break;
  698. case SO_KEEPALIVE:
  699. v.val = !!sock_flag(sk, SOCK_KEEPOPEN);
  700. break;
  701. case SO_TYPE:
  702. v.val = sk->sk_type;
  703. break;
  704. case SO_PROTOCOL:
  705. v.val = sk->sk_protocol;
  706. break;
  707. case SO_DOMAIN:
  708. v.val = sk->sk_family;
  709. break;
  710. case SO_ERROR:
  711. v.val = -sock_error(sk);
  712. if (v.val == 0)
  713. v.val = xchg(&sk->sk_err_soft, 0);
  714. break;
  715. case SO_OOBINLINE:
  716. v.val = !!sock_flag(sk, SOCK_URGINLINE);
  717. break;
  718. case SO_NO_CHECK:
  719. v.val = sk->sk_no_check;
  720. break;
  721. case SO_PRIORITY:
  722. v.val = sk->sk_priority;
  723. break;
  724. case SO_LINGER:
  725. lv = sizeof(v.ling);
  726. v.ling.l_onoff = !!sock_flag(sk, SOCK_LINGER);
  727. v.ling.l_linger = sk->sk_lingertime / HZ;
  728. break;
  729. case SO_BSDCOMPAT:
  730. sock_warn_obsolete_bsdism("getsockopt");
  731. break;
  732. case SO_TIMESTAMP:
  733. v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
  734. !sock_flag(sk, SOCK_RCVTSTAMPNS);
  735. break;
  736. case SO_TIMESTAMPNS:
  737. v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
  738. break;
  739. case SO_TIMESTAMPING:
  740. v.val = 0;
  741. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  742. v.val |= SOF_TIMESTAMPING_TX_HARDWARE;
  743. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  744. v.val |= SOF_TIMESTAMPING_TX_SOFTWARE;
  745. if (sock_flag(sk, SOCK_TIMESTAMPING_RX_HARDWARE))
  746. v.val |= SOF_TIMESTAMPING_RX_HARDWARE;
  747. if (sock_flag(sk, SOCK_TIMESTAMPING_RX_SOFTWARE))
  748. v.val |= SOF_TIMESTAMPING_RX_SOFTWARE;
  749. if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE))
  750. v.val |= SOF_TIMESTAMPING_SOFTWARE;
  751. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE))
  752. v.val |= SOF_TIMESTAMPING_SYS_HARDWARE;
  753. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE))
  754. v.val |= SOF_TIMESTAMPING_RAW_HARDWARE;
  755. break;
  756. case SO_RCVTIMEO:
  757. lv = sizeof(struct timeval);
  758. if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
  759. v.tm.tv_sec = 0;
  760. v.tm.tv_usec = 0;
  761. } else {
  762. v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
  763. v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * 1000000) / HZ;
  764. }
  765. break;
  766. case SO_SNDTIMEO:
  767. lv = sizeof(struct timeval);
  768. if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
  769. v.tm.tv_sec = 0;
  770. v.tm.tv_usec = 0;
  771. } else {
  772. v.tm.tv_sec = sk->sk_sndtimeo / HZ;
  773. v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * 1000000) / HZ;
  774. }
  775. break;
  776. case SO_RCVLOWAT:
  777. v.val = sk->sk_rcvlowat;
  778. break;
  779. case SO_SNDLOWAT:
  780. v.val = 1;
  781. break;
  782. case SO_PASSCRED:
  783. v.val = test_bit(SOCK_PASSCRED, &sock->flags) ? 1 : 0;
  784. break;
  785. case SO_PEERCRED:
  786. if (len > sizeof(sk->sk_peercred))
  787. len = sizeof(sk->sk_peercred);
  788. if (copy_to_user(optval, &sk->sk_peercred, len))
  789. return -EFAULT;
  790. goto lenout;
  791. case SO_PEERNAME:
  792. {
  793. char address[128];
  794. if (sock->ops->getname(sock, (struct sockaddr *)address, &lv, 2))
  795. return -ENOTCONN;
  796. if (lv < len)
  797. return -EINVAL;
  798. if (copy_to_user(optval, address, len))
  799. return -EFAULT;
  800. goto lenout;
  801. }
  802. /* Dubious BSD thing... Probably nobody even uses it, but
  803. * the UNIX standard wants it for whatever reason... -DaveM
  804. */
  805. case SO_ACCEPTCONN:
  806. v.val = sk->sk_state == TCP_LISTEN;
  807. break;
  808. case SO_PASSSEC:
  809. v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0;
  810. break;
  811. case SO_PEERSEC:
  812. return security_socket_getpeersec_stream(sock, optval, optlen, len);
  813. case SO_MARK:
  814. v.val = sk->sk_mark;
  815. break;
  816. case SO_RXQ_OVFL:
  817. v.val = !!sock_flag(sk, SOCK_RXQ_OVFL);
  818. break;
  819. default:
  820. return -ENOPROTOOPT;
  821. }
  822. if (len > lv)
  823. len = lv;
  824. if (copy_to_user(optval, &v, len))
  825. return -EFAULT;
  826. lenout:
  827. if (put_user(len, optlen))
  828. return -EFAULT;
  829. return 0;
  830. }
  831. /*
  832. * Initialize an sk_lock.
  833. *
  834. * (We also register the sk_lock with the lock validator.)
  835. */
  836. static inline void sock_lock_init(struct sock *sk)
  837. {
  838. sock_lock_init_class_and_name(sk,
  839. af_family_slock_key_strings[sk->sk_family],
  840. af_family_slock_keys + sk->sk_family,
  841. af_family_key_strings[sk->sk_family],
  842. af_family_keys + sk->sk_family);
  843. }
  844. /*
  845. * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
  846. * even temporarly, because of RCU lookups. sk_node should also be left as is.
  847. */
  848. static void sock_copy(struct sock *nsk, const struct sock *osk)
  849. {
  850. #ifdef CONFIG_SECURITY_NETWORK
  851. void *sptr = nsk->sk_security;
  852. #endif
  853. BUILD_BUG_ON(offsetof(struct sock, sk_copy_start) !=
  854. sizeof(osk->sk_node) + sizeof(osk->sk_refcnt) +
  855. sizeof(osk->sk_tx_queue_mapping));
  856. memcpy(&nsk->sk_copy_start, &osk->sk_copy_start,
  857. osk->sk_prot->obj_size - offsetof(struct sock, sk_copy_start));
  858. #ifdef CONFIG_SECURITY_NETWORK
  859. nsk->sk_security = sptr;
  860. security_sk_clone(osk, nsk);
  861. #endif
  862. }
  863. static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
  864. int family)
  865. {
  866. struct sock *sk;
  867. struct kmem_cache *slab;
  868. slab = prot->slab;
  869. if (slab != NULL) {
  870. sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
  871. if (!sk)
  872. return sk;
  873. if (priority & __GFP_ZERO) {
  874. /*
  875. * caches using SLAB_DESTROY_BY_RCU should let
  876. * sk_node.next un-modified. Special care is taken
  877. * when initializing object to zero.
  878. */
  879. if (offsetof(struct sock, sk_node.next) != 0)
  880. memset(sk, 0, offsetof(struct sock, sk_node.next));
  881. memset(&sk->sk_node.pprev, 0,
  882. prot->obj_size - offsetof(struct sock,
  883. sk_node.pprev));
  884. }
  885. }
  886. else
  887. sk = kmalloc(prot->obj_size, priority);
  888. if (sk != NULL) {
  889. kmemcheck_annotate_bitfield(sk, flags);
  890. if (security_sk_alloc(sk, family, priority))
  891. goto out_free;
  892. if (!try_module_get(prot->owner))
  893. goto out_free_sec;
  894. sk_tx_queue_clear(sk);
  895. }
  896. return sk;
  897. out_free_sec:
  898. security_sk_free(sk);
  899. out_free:
  900. if (slab != NULL)
  901. kmem_cache_free(slab, sk);
  902. else
  903. kfree(sk);
  904. return NULL;
  905. }
  906. static void sk_prot_free(struct proto *prot, struct sock *sk)
  907. {
  908. struct kmem_cache *slab;
  909. struct module *owner;
  910. owner = prot->owner;
  911. slab = prot->slab;
  912. security_sk_free(sk);
  913. if (slab != NULL)
  914. kmem_cache_free(slab, sk);
  915. else
  916. kfree(sk);
  917. module_put(owner);
  918. }
  919. #ifdef CONFIG_CGROUPS
  920. void sock_update_classid(struct sock *sk)
  921. {
  922. u32 classid = task_cls_classid(current);
  923. if (classid && classid != sk->sk_classid)
  924. sk->sk_classid = classid;
  925. }
  926. EXPORT_SYMBOL(sock_update_classid);
  927. #endif
  928. /**
  929. * sk_alloc - All socket objects are allocated here
  930. * @net: the applicable net namespace
  931. * @family: protocol family
  932. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  933. * @prot: struct proto associated with this new sock instance
  934. */
  935. struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
  936. struct proto *prot)
  937. {
  938. struct sock *sk;
  939. sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
  940. if (sk) {
  941. sk->sk_family = family;
  942. /*
  943. * See comment in struct sock definition to understand
  944. * why we need sk_prot_creator -acme
  945. */
  946. sk->sk_prot = sk->sk_prot_creator = prot;
  947. sock_lock_init(sk);
  948. sock_net_set(sk, get_net(net));
  949. atomic_set(&sk->sk_wmem_alloc, 1);
  950. sock_update_classid(sk);
  951. }
  952. return sk;
  953. }
  954. EXPORT_SYMBOL(sk_alloc);
  955. static void __sk_free(struct sock *sk)
  956. {
  957. struct sk_filter *filter;
  958. if (sk->sk_destruct)
  959. sk->sk_destruct(sk);
  960. filter = rcu_dereference_check(sk->sk_filter,
  961. atomic_read(&sk->sk_wmem_alloc) == 0);
  962. if (filter) {
  963. sk_filter_uncharge(sk, filter);
  964. rcu_assign_pointer(sk->sk_filter, NULL);
  965. }
  966. sock_disable_timestamp(sk, SOCK_TIMESTAMP);
  967. sock_disable_timestamp(sk, SOCK_TIMESTAMPING_RX_SOFTWARE);
  968. if (atomic_read(&sk->sk_omem_alloc))
  969. printk(KERN_DEBUG "%s: optmem leakage (%d bytes) detected.\n",
  970. __func__, atomic_read(&sk->sk_omem_alloc));
  971. put_net(sock_net(sk));
  972. sk_prot_free(sk->sk_prot_creator, sk);
  973. }
  974. void sk_free(struct sock *sk)
  975. {
  976. /*
  977. * We substract one from sk_wmem_alloc and can know if
  978. * some packets are still in some tx queue.
  979. * If not null, sock_wfree() will call __sk_free(sk) later
  980. */
  981. if (atomic_dec_and_test(&sk->sk_wmem_alloc))
  982. __sk_free(sk);
  983. }
  984. EXPORT_SYMBOL(sk_free);
  985. /*
  986. * Last sock_put should drop referrence to sk->sk_net. It has already
  987. * been dropped in sk_change_net. Taking referrence to stopping namespace
  988. * is not an option.
  989. * Take referrence to a socket to remove it from hash _alive_ and after that
  990. * destroy it in the context of init_net.
  991. */
  992. void sk_release_kernel(struct sock *sk)
  993. {
  994. if (sk == NULL || sk->sk_socket == NULL)
  995. return;
  996. sock_hold(sk);
  997. sock_release(sk->sk_socket);
  998. release_net(sock_net(sk));
  999. sock_net_set(sk, get_net(&init_net));
  1000. sock_put(sk);
  1001. }
  1002. EXPORT_SYMBOL(sk_release_kernel);
  1003. struct sock *sk_clone(const struct sock *sk, const gfp_t priority)
  1004. {
  1005. struct sock *newsk;
  1006. newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
  1007. if (newsk != NULL) {
  1008. struct sk_filter *filter;
  1009. sock_copy(newsk, sk);
  1010. /* SANITY */
  1011. get_net(sock_net(newsk));
  1012. sk_node_init(&newsk->sk_node);
  1013. sock_lock_init(newsk);
  1014. bh_lock_sock(newsk);
  1015. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  1016. newsk->sk_backlog.len = 0;
  1017. atomic_set(&newsk->sk_rmem_alloc, 0);
  1018. /*
  1019. * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
  1020. */
  1021. atomic_set(&newsk->sk_wmem_alloc, 1);
  1022. atomic_set(&newsk->sk_omem_alloc, 0);
  1023. skb_queue_head_init(&newsk->sk_receive_queue);
  1024. skb_queue_head_init(&newsk->sk_write_queue);
  1025. #ifdef CONFIG_NET_DMA
  1026. skb_queue_head_init(&newsk->sk_async_wait_queue);
  1027. #endif
  1028. spin_lock_init(&newsk->sk_dst_lock);
  1029. rwlock_init(&newsk->sk_callback_lock);
  1030. lockdep_set_class_and_name(&newsk->sk_callback_lock,
  1031. af_callback_keys + newsk->sk_family,
  1032. af_family_clock_key_strings[newsk->sk_family]);
  1033. newsk->sk_dst_cache = NULL;
  1034. newsk->sk_wmem_queued = 0;
  1035. newsk->sk_forward_alloc = 0;
  1036. newsk->sk_send_head = NULL;
  1037. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  1038. sock_reset_flag(newsk, SOCK_DONE);
  1039. skb_queue_head_init(&newsk->sk_error_queue);
  1040. filter = newsk->sk_filter;
  1041. if (filter != NULL)
  1042. sk_filter_charge(newsk, filter);
  1043. if (unlikely(xfrm_sk_clone_policy(newsk))) {
  1044. /* It is still raw copy of parent, so invalidate
  1045. * destructor and make plain sk_free() */
  1046. newsk->sk_destruct = NULL;
  1047. sk_free(newsk);
  1048. newsk = NULL;
  1049. goto out;
  1050. }
  1051. newsk->sk_err = 0;
  1052. newsk->sk_priority = 0;
  1053. /*
  1054. * Before updating sk_refcnt, we must commit prior changes to memory
  1055. * (Documentation/RCU/rculist_nulls.txt for details)
  1056. */
  1057. smp_wmb();
  1058. atomic_set(&newsk->sk_refcnt, 2);
  1059. /*
  1060. * Increment the counter in the same struct proto as the master
  1061. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  1062. * is the same as sk->sk_prot->socks, as this field was copied
  1063. * with memcpy).
  1064. *
  1065. * This _changes_ the previous behaviour, where
  1066. * tcp_create_openreq_child always was incrementing the
  1067. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  1068. * to be taken into account in all callers. -acme
  1069. */
  1070. sk_refcnt_debug_inc(newsk);
  1071. sk_set_socket(newsk, NULL);
  1072. newsk->sk_wq = NULL;
  1073. if (newsk->sk_prot->sockets_allocated)
  1074. percpu_counter_inc(newsk->sk_prot->sockets_allocated);
  1075. if (sock_flag(newsk, SOCK_TIMESTAMP) ||
  1076. sock_flag(newsk, SOCK_TIMESTAMPING_RX_SOFTWARE))
  1077. net_enable_timestamp();
  1078. }
  1079. out:
  1080. return newsk;
  1081. }
  1082. EXPORT_SYMBOL_GPL(sk_clone);
  1083. void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
  1084. {
  1085. __sk_dst_set(sk, dst);
  1086. sk->sk_route_caps = dst->dev->features;
  1087. if (sk->sk_route_caps & NETIF_F_GSO)
  1088. sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
  1089. sk->sk_route_caps &= ~sk->sk_route_nocaps;
  1090. if (sk_can_gso(sk)) {
  1091. if (dst->header_len) {
  1092. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  1093. } else {
  1094. sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1095. sk->sk_gso_max_size = dst->dev->gso_max_size;
  1096. }
  1097. }
  1098. }
  1099. EXPORT_SYMBOL_GPL(sk_setup_caps);
  1100. void __init sk_init(void)
  1101. {
  1102. if (totalram_pages <= 4096) {
  1103. sysctl_wmem_max = 32767;
  1104. sysctl_rmem_max = 32767;
  1105. sysctl_wmem_default = 32767;
  1106. sysctl_rmem_default = 32767;
  1107. } else if (totalram_pages >= 131072) {
  1108. sysctl_wmem_max = 131071;
  1109. sysctl_rmem_max = 131071;
  1110. }
  1111. }
  1112. /*
  1113. * Simple resource managers for sockets.
  1114. */
  1115. /*
  1116. * Write buffer destructor automatically called from kfree_skb.
  1117. */
  1118. void sock_wfree(struct sk_buff *skb)
  1119. {
  1120. struct sock *sk = skb->sk;
  1121. unsigned int len = skb->truesize;
  1122. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
  1123. /*
  1124. * Keep a reference on sk_wmem_alloc, this will be released
  1125. * after sk_write_space() call
  1126. */
  1127. atomic_sub(len - 1, &sk->sk_wmem_alloc);
  1128. sk->sk_write_space(sk);
  1129. len = 1;
  1130. }
  1131. /*
  1132. * if sk_wmem_alloc reaches 0, we must finish what sk_free()
  1133. * could not do because of in-flight packets
  1134. */
  1135. if (atomic_sub_and_test(len, &sk->sk_wmem_alloc))
  1136. __sk_free(sk);
  1137. }
  1138. EXPORT_SYMBOL(sock_wfree);
  1139. /*
  1140. * Read buffer destructor automatically called from kfree_skb.
  1141. */
  1142. void sock_rfree(struct sk_buff *skb)
  1143. {
  1144. struct sock *sk = skb->sk;
  1145. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  1146. sk_mem_uncharge(skb->sk, skb->truesize);
  1147. }
  1148. EXPORT_SYMBOL(sock_rfree);
  1149. int sock_i_uid(struct sock *sk)
  1150. {
  1151. int uid;
  1152. read_lock(&sk->sk_callback_lock);
  1153. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : 0;
  1154. read_unlock(&sk->sk_callback_lock);
  1155. return uid;
  1156. }
  1157. EXPORT_SYMBOL(sock_i_uid);
  1158. unsigned long sock_i_ino(struct sock *sk)
  1159. {
  1160. unsigned long ino;
  1161. read_lock(&sk->sk_callback_lock);
  1162. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  1163. read_unlock(&sk->sk_callback_lock);
  1164. return ino;
  1165. }
  1166. EXPORT_SYMBOL(sock_i_ino);
  1167. /*
  1168. * Allocate a skb from the socket's send buffer.
  1169. */
  1170. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  1171. gfp_t priority)
  1172. {
  1173. if (force || atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  1174. struct sk_buff *skb = alloc_skb(size, priority);
  1175. if (skb) {
  1176. skb_set_owner_w(skb, sk);
  1177. return skb;
  1178. }
  1179. }
  1180. return NULL;
  1181. }
  1182. EXPORT_SYMBOL(sock_wmalloc);
  1183. /*
  1184. * Allocate a skb from the socket's receive buffer.
  1185. */
  1186. struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force,
  1187. gfp_t priority)
  1188. {
  1189. if (force || atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  1190. struct sk_buff *skb = alloc_skb(size, priority);
  1191. if (skb) {
  1192. skb_set_owner_r(skb, sk);
  1193. return skb;
  1194. }
  1195. }
  1196. return NULL;
  1197. }
  1198. /*
  1199. * Allocate a memory block from the socket's option memory buffer.
  1200. */
  1201. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
  1202. {
  1203. if ((unsigned)size <= sysctl_optmem_max &&
  1204. atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
  1205. void *mem;
  1206. /* First do the add, to avoid the race if kmalloc
  1207. * might sleep.
  1208. */
  1209. atomic_add(size, &sk->sk_omem_alloc);
  1210. mem = kmalloc(size, priority);
  1211. if (mem)
  1212. return mem;
  1213. atomic_sub(size, &sk->sk_omem_alloc);
  1214. }
  1215. return NULL;
  1216. }
  1217. EXPORT_SYMBOL(sock_kmalloc);
  1218. /*
  1219. * Free an option memory block.
  1220. */
  1221. void sock_kfree_s(struct sock *sk, void *mem, int size)
  1222. {
  1223. kfree(mem);
  1224. atomic_sub(size, &sk->sk_omem_alloc);
  1225. }
  1226. EXPORT_SYMBOL(sock_kfree_s);
  1227. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  1228. I think, these locks should be removed for datagram sockets.
  1229. */
  1230. static long sock_wait_for_wmem(struct sock *sk, long timeo)
  1231. {
  1232. DEFINE_WAIT(wait);
  1233. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1234. for (;;) {
  1235. if (!timeo)
  1236. break;
  1237. if (signal_pending(current))
  1238. break;
  1239. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1240. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1241. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
  1242. break;
  1243. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1244. break;
  1245. if (sk->sk_err)
  1246. break;
  1247. timeo = schedule_timeout(timeo);
  1248. }
  1249. finish_wait(sk_sleep(sk), &wait);
  1250. return timeo;
  1251. }
  1252. /*
  1253. * Generic send/receive buffer handlers
  1254. */
  1255. struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
  1256. unsigned long data_len, int noblock,
  1257. int *errcode)
  1258. {
  1259. struct sk_buff *skb;
  1260. gfp_t gfp_mask;
  1261. long timeo;
  1262. int err;
  1263. gfp_mask = sk->sk_allocation;
  1264. if (gfp_mask & __GFP_WAIT)
  1265. gfp_mask |= __GFP_REPEAT;
  1266. timeo = sock_sndtimeo(sk, noblock);
  1267. while (1) {
  1268. err = sock_error(sk);
  1269. if (err != 0)
  1270. goto failure;
  1271. err = -EPIPE;
  1272. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1273. goto failure;
  1274. if (atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  1275. skb = alloc_skb(header_len, gfp_mask);
  1276. if (skb) {
  1277. int npages;
  1278. int i;
  1279. /* No pages, we're done... */
  1280. if (!data_len)
  1281. break;
  1282. npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  1283. skb->truesize += data_len;
  1284. skb_shinfo(skb)->nr_frags = npages;
  1285. for (i = 0; i < npages; i++) {
  1286. struct page *page;
  1287. skb_frag_t *frag;
  1288. page = alloc_pages(sk->sk_allocation, 0);
  1289. if (!page) {
  1290. err = -ENOBUFS;
  1291. skb_shinfo(skb)->nr_frags = i;
  1292. kfree_skb(skb);
  1293. goto failure;
  1294. }
  1295. frag = &skb_shinfo(skb)->frags[i];
  1296. frag->page = page;
  1297. frag->page_offset = 0;
  1298. frag->size = (data_len >= PAGE_SIZE ?
  1299. PAGE_SIZE :
  1300. data_len);
  1301. data_len -= PAGE_SIZE;
  1302. }
  1303. /* Full success... */
  1304. break;
  1305. }
  1306. err = -ENOBUFS;
  1307. goto failure;
  1308. }
  1309. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1310. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1311. err = -EAGAIN;
  1312. if (!timeo)
  1313. goto failure;
  1314. if (signal_pending(current))
  1315. goto interrupted;
  1316. timeo = sock_wait_for_wmem(sk, timeo);
  1317. }
  1318. skb_set_owner_w(skb, sk);
  1319. return skb;
  1320. interrupted:
  1321. err = sock_intr_errno(timeo);
  1322. failure:
  1323. *errcode = err;
  1324. return NULL;
  1325. }
  1326. EXPORT_SYMBOL(sock_alloc_send_pskb);
  1327. struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
  1328. int noblock, int *errcode)
  1329. {
  1330. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode);
  1331. }
  1332. EXPORT_SYMBOL(sock_alloc_send_skb);
  1333. static void __lock_sock(struct sock *sk)
  1334. {
  1335. DEFINE_WAIT(wait);
  1336. for (;;) {
  1337. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  1338. TASK_UNINTERRUPTIBLE);
  1339. spin_unlock_bh(&sk->sk_lock.slock);
  1340. schedule();
  1341. spin_lock_bh(&sk->sk_lock.slock);
  1342. if (!sock_owned_by_user(sk))
  1343. break;
  1344. }
  1345. finish_wait(&sk->sk_lock.wq, &wait);
  1346. }
  1347. static void __release_sock(struct sock *sk)
  1348. {
  1349. struct sk_buff *skb = sk->sk_backlog.head;
  1350. do {
  1351. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  1352. bh_unlock_sock(sk);
  1353. do {
  1354. struct sk_buff *next = skb->next;
  1355. WARN_ON_ONCE(skb_dst_is_noref(skb));
  1356. skb->next = NULL;
  1357. sk_backlog_rcv(sk, skb);
  1358. /*
  1359. * We are in process context here with softirqs
  1360. * disabled, use cond_resched_softirq() to preempt.
  1361. * This is safe to do because we've taken the backlog
  1362. * queue private:
  1363. */
  1364. cond_resched_softirq();
  1365. skb = next;
  1366. } while (skb != NULL);
  1367. bh_lock_sock(sk);
  1368. } while ((skb = sk->sk_backlog.head) != NULL);
  1369. /*
  1370. * Doing the zeroing here guarantee we can not loop forever
  1371. * while a wild producer attempts to flood us.
  1372. */
  1373. sk->sk_backlog.len = 0;
  1374. }
  1375. /**
  1376. * sk_wait_data - wait for data to arrive at sk_receive_queue
  1377. * @sk: sock to wait on
  1378. * @timeo: for how long
  1379. *
  1380. * Now socket state including sk->sk_err is changed only under lock,
  1381. * hence we may omit checks after joining wait queue.
  1382. * We check receive queue before schedule() only as optimization;
  1383. * it is very likely that release_sock() added new data.
  1384. */
  1385. int sk_wait_data(struct sock *sk, long *timeo)
  1386. {
  1387. int rc;
  1388. DEFINE_WAIT(wait);
  1389. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1390. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1391. rc = sk_wait_event(sk, timeo, !skb_queue_empty(&sk->sk_receive_queue));
  1392. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1393. finish_wait(sk_sleep(sk), &wait);
  1394. return rc;
  1395. }
  1396. EXPORT_SYMBOL(sk_wait_data);
  1397. /**
  1398. * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
  1399. * @sk: socket
  1400. * @size: memory size to allocate
  1401. * @kind: allocation type
  1402. *
  1403. * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
  1404. * rmem allocation. This function assumes that protocols which have
  1405. * memory_pressure use sk_wmem_queued as write buffer accounting.
  1406. */
  1407. int __sk_mem_schedule(struct sock *sk, int size, int kind)
  1408. {
  1409. struct proto *prot = sk->sk_prot;
  1410. int amt = sk_mem_pages(size);
  1411. int allocated;
  1412. sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
  1413. allocated = atomic_add_return(amt, prot->memory_allocated);
  1414. /* Under limit. */
  1415. if (allocated <= prot->sysctl_mem[0]) {
  1416. if (prot->memory_pressure && *prot->memory_pressure)
  1417. *prot->memory_pressure = 0;
  1418. return 1;
  1419. }
  1420. /* Under pressure. */
  1421. if (allocated > prot->sysctl_mem[1])
  1422. if (prot->enter_memory_pressure)
  1423. prot->enter_memory_pressure(sk);
  1424. /* Over hard limit. */
  1425. if (allocated > prot->sysctl_mem[2])
  1426. goto suppress_allocation;
  1427. /* guarantee minimum buffer size under pressure */
  1428. if (kind == SK_MEM_RECV) {
  1429. if (atomic_read(&sk->sk_rmem_alloc) < prot->sysctl_rmem[0])
  1430. return 1;
  1431. } else { /* SK_MEM_SEND */
  1432. if (sk->sk_type == SOCK_STREAM) {
  1433. if (sk->sk_wmem_queued < prot->sysctl_wmem[0])
  1434. return 1;
  1435. } else if (atomic_read(&sk->sk_wmem_alloc) <
  1436. prot->sysctl_wmem[0])
  1437. return 1;
  1438. }
  1439. if (prot->memory_pressure) {
  1440. int alloc;
  1441. if (!*prot->memory_pressure)
  1442. return 1;
  1443. alloc = percpu_counter_read_positive(prot->sockets_allocated);
  1444. if (prot->sysctl_mem[2] > alloc *
  1445. sk_mem_pages(sk->sk_wmem_queued +
  1446. atomic_read(&sk->sk_rmem_alloc) +
  1447. sk->sk_forward_alloc))
  1448. return 1;
  1449. }
  1450. suppress_allocation:
  1451. if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
  1452. sk_stream_moderate_sndbuf(sk);
  1453. /* Fail only if socket is _under_ its sndbuf.
  1454. * In this case we cannot block, so that we have to fail.
  1455. */
  1456. if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
  1457. return 1;
  1458. }
  1459. /* Alas. Undo changes. */
  1460. sk->sk_forward_alloc -= amt * SK_MEM_QUANTUM;
  1461. atomic_sub(amt, prot->memory_allocated);
  1462. return 0;
  1463. }
  1464. EXPORT_SYMBOL(__sk_mem_schedule);
  1465. /**
  1466. * __sk_reclaim - reclaim memory_allocated
  1467. * @sk: socket
  1468. */
  1469. void __sk_mem_reclaim(struct sock *sk)
  1470. {
  1471. struct proto *prot = sk->sk_prot;
  1472. atomic_sub(sk->sk_forward_alloc >> SK_MEM_QUANTUM_SHIFT,
  1473. prot->memory_allocated);
  1474. sk->sk_forward_alloc &= SK_MEM_QUANTUM - 1;
  1475. if (prot->memory_pressure && *prot->memory_pressure &&
  1476. (atomic_read(prot->memory_allocated) < prot->sysctl_mem[0]))
  1477. *prot->memory_pressure = 0;
  1478. }
  1479. EXPORT_SYMBOL(__sk_mem_reclaim);
  1480. /*
  1481. * Set of default routines for initialising struct proto_ops when
  1482. * the protocol does not support a particular function. In certain
  1483. * cases where it makes no sense for a protocol to have a "do nothing"
  1484. * function, some default processing is provided.
  1485. */
  1486. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  1487. {
  1488. return -EOPNOTSUPP;
  1489. }
  1490. EXPORT_SYMBOL(sock_no_bind);
  1491. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  1492. int len, int flags)
  1493. {
  1494. return -EOPNOTSUPP;
  1495. }
  1496. EXPORT_SYMBOL(sock_no_connect);
  1497. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  1498. {
  1499. return -EOPNOTSUPP;
  1500. }
  1501. EXPORT_SYMBOL(sock_no_socketpair);
  1502. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags)
  1503. {
  1504. return -EOPNOTSUPP;
  1505. }
  1506. EXPORT_SYMBOL(sock_no_accept);
  1507. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  1508. int *len, int peer)
  1509. {
  1510. return -EOPNOTSUPP;
  1511. }
  1512. EXPORT_SYMBOL(sock_no_getname);
  1513. unsigned int sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
  1514. {
  1515. return 0;
  1516. }
  1517. EXPORT_SYMBOL(sock_no_poll);
  1518. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1519. {
  1520. return -EOPNOTSUPP;
  1521. }
  1522. EXPORT_SYMBOL(sock_no_ioctl);
  1523. int sock_no_listen(struct socket *sock, int backlog)
  1524. {
  1525. return -EOPNOTSUPP;
  1526. }
  1527. EXPORT_SYMBOL(sock_no_listen);
  1528. int sock_no_shutdown(struct socket *sock, int how)
  1529. {
  1530. return -EOPNOTSUPP;
  1531. }
  1532. EXPORT_SYMBOL(sock_no_shutdown);
  1533. int sock_no_setsockopt(struct socket *sock, int level, int optname,
  1534. char __user *optval, unsigned int optlen)
  1535. {
  1536. return -EOPNOTSUPP;
  1537. }
  1538. EXPORT_SYMBOL(sock_no_setsockopt);
  1539. int sock_no_getsockopt(struct socket *sock, int level, int optname,
  1540. char __user *optval, int __user *optlen)
  1541. {
  1542. return -EOPNOTSUPP;
  1543. }
  1544. EXPORT_SYMBOL(sock_no_getsockopt);
  1545. int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  1546. size_t len)
  1547. {
  1548. return -EOPNOTSUPP;
  1549. }
  1550. EXPORT_SYMBOL(sock_no_sendmsg);
  1551. int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
  1552. size_t len, int flags)
  1553. {
  1554. return -EOPNOTSUPP;
  1555. }
  1556. EXPORT_SYMBOL(sock_no_recvmsg);
  1557. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  1558. {
  1559. /* Mirror missing mmap method error code */
  1560. return -ENODEV;
  1561. }
  1562. EXPORT_SYMBOL(sock_no_mmap);
  1563. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  1564. {
  1565. ssize_t res;
  1566. struct msghdr msg = {.msg_flags = flags};
  1567. struct kvec iov;
  1568. char *kaddr = kmap(page);
  1569. iov.iov_base = kaddr + offset;
  1570. iov.iov_len = size;
  1571. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  1572. kunmap(page);
  1573. return res;
  1574. }
  1575. EXPORT_SYMBOL(sock_no_sendpage);
  1576. /*
  1577. * Default Socket Callbacks
  1578. */
  1579. static void sock_def_wakeup(struct sock *sk)
  1580. {
  1581. struct socket_wq *wq;
  1582. rcu_read_lock();
  1583. wq = rcu_dereference(sk->sk_wq);
  1584. if (wq_has_sleeper(wq))
  1585. wake_up_interruptible_all(&wq->wait);
  1586. rcu_read_unlock();
  1587. }
  1588. static void sock_def_error_report(struct sock *sk)
  1589. {
  1590. struct socket_wq *wq;
  1591. rcu_read_lock();
  1592. wq = rcu_dereference(sk->sk_wq);
  1593. if (wq_has_sleeper(wq))
  1594. wake_up_interruptible_poll(&wq->wait, POLLERR);
  1595. sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
  1596. rcu_read_unlock();
  1597. }
  1598. static void sock_def_readable(struct sock *sk, int len)
  1599. {
  1600. struct socket_wq *wq;
  1601. rcu_read_lock();
  1602. wq = rcu_dereference(sk->sk_wq);
  1603. if (wq_has_sleeper(wq))
  1604. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1605. POLLRDNORM | POLLRDBAND);
  1606. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  1607. rcu_read_unlock();
  1608. }
  1609. static void sock_def_write_space(struct sock *sk)
  1610. {
  1611. struct socket_wq *wq;
  1612. rcu_read_lock();
  1613. /* Do not wake up a writer until he can make "significant"
  1614. * progress. --DaveM
  1615. */
  1616. if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
  1617. wq = rcu_dereference(sk->sk_wq);
  1618. if (wq_has_sleeper(wq))
  1619. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1620. POLLWRNORM | POLLWRBAND);
  1621. /* Should agree with poll, otherwise some programs break */
  1622. if (sock_writeable(sk))
  1623. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  1624. }
  1625. rcu_read_unlock();
  1626. }
  1627. static void sock_def_destruct(struct sock *sk)
  1628. {
  1629. kfree(sk->sk_protinfo);
  1630. }
  1631. void sk_send_sigurg(struct sock *sk)
  1632. {
  1633. if (sk->sk_socket && sk->sk_socket->file)
  1634. if (send_sigurg(&sk->sk_socket->file->f_owner))
  1635. sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
  1636. }
  1637. EXPORT_SYMBOL(sk_send_sigurg);
  1638. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  1639. unsigned long expires)
  1640. {
  1641. if (!mod_timer(timer, expires))
  1642. sock_hold(sk);
  1643. }
  1644. EXPORT_SYMBOL(sk_reset_timer);
  1645. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  1646. {
  1647. if (timer_pending(timer) && del_timer(timer))
  1648. __sock_put(sk);
  1649. }
  1650. EXPORT_SYMBOL(sk_stop_timer);
  1651. void sock_init_data(struct socket *sock, struct sock *sk)
  1652. {
  1653. skb_queue_head_init(&sk->sk_receive_queue);
  1654. skb_queue_head_init(&sk->sk_write_queue);
  1655. skb_queue_head_init(&sk->sk_error_queue);
  1656. #ifdef CONFIG_NET_DMA
  1657. skb_queue_head_init(&sk->sk_async_wait_queue);
  1658. #endif
  1659. sk->sk_send_head = NULL;
  1660. init_timer(&sk->sk_timer);
  1661. sk->sk_allocation = GFP_KERNEL;
  1662. sk->sk_rcvbuf = sysctl_rmem_default;
  1663. sk->sk_sndbuf = sysctl_wmem_default;
  1664. sk->sk_state = TCP_CLOSE;
  1665. sk_set_socket(sk, sock);
  1666. sock_set_flag(sk, SOCK_ZAPPED);
  1667. if (sock) {
  1668. sk->sk_type = sock->type;
  1669. sk->sk_wq = sock->wq;
  1670. sock->sk = sk;
  1671. } else
  1672. sk->sk_wq = NULL;
  1673. spin_lock_init(&sk->sk_dst_lock);
  1674. rwlock_init(&sk->sk_callback_lock);
  1675. lockdep_set_class_and_name(&sk->sk_callback_lock,
  1676. af_callback_keys + sk->sk_family,
  1677. af_family_clock_key_strings[sk->sk_family]);
  1678. sk->sk_state_change = sock_def_wakeup;
  1679. sk->sk_data_ready = sock_def_readable;
  1680. sk->sk_write_space = sock_def_write_space;
  1681. sk->sk_error_report = sock_def_error_report;
  1682. sk->sk_destruct = sock_def_destruct;
  1683. sk->sk_sndmsg_page = NULL;
  1684. sk->sk_sndmsg_off = 0;
  1685. sk->sk_peercred.pid = 0;
  1686. sk->sk_peercred.uid = -1;
  1687. sk->sk_peercred.gid = -1;
  1688. sk->sk_write_pending = 0;
  1689. sk->sk_rcvlowat = 1;
  1690. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  1691. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  1692. sk->sk_stamp = ktime_set(-1L, 0);
  1693. /*
  1694. * Before updating sk_refcnt, we must commit prior changes to memory
  1695. * (Documentation/RCU/rculist_nulls.txt for details)
  1696. */
  1697. smp_wmb();
  1698. atomic_set(&sk->sk_refcnt, 1);
  1699. atomic_set(&sk->sk_drops, 0);
  1700. }
  1701. EXPORT_SYMBOL(sock_init_data);
  1702. void lock_sock_nested(struct sock *sk, int subclass)
  1703. {
  1704. might_sleep();
  1705. spin_lock_bh(&sk->sk_lock.slock);
  1706. if (sk->sk_lock.owned)
  1707. __lock_sock(sk);
  1708. sk->sk_lock.owned = 1;
  1709. spin_unlock(&sk->sk_lock.slock);
  1710. /*
  1711. * The sk_lock has mutex_lock() semantics here:
  1712. */
  1713. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  1714. local_bh_enable();
  1715. }
  1716. EXPORT_SYMBOL(lock_sock_nested);
  1717. void release_sock(struct sock *sk)
  1718. {
  1719. /*
  1720. * The sk_lock has mutex_unlock() semantics:
  1721. */
  1722. mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
  1723. spin_lock_bh(&sk->sk_lock.slock);
  1724. if (sk->sk_backlog.tail)
  1725. __release_sock(sk);
  1726. sk->sk_lock.owned = 0;
  1727. if (waitqueue_active(&sk->sk_lock.wq))
  1728. wake_up(&sk->sk_lock.wq);
  1729. spin_unlock_bh(&sk->sk_lock.slock);
  1730. }
  1731. EXPORT_SYMBOL(release_sock);
  1732. /**
  1733. * lock_sock_fast - fast version of lock_sock
  1734. * @sk: socket
  1735. *
  1736. * This version should be used for very small section, where process wont block
  1737. * return false if fast path is taken
  1738. * sk_lock.slock locked, owned = 0, BH disabled
  1739. * return true if slow path is taken
  1740. * sk_lock.slock unlocked, owned = 1, BH enabled
  1741. */
  1742. bool lock_sock_fast(struct sock *sk)
  1743. {
  1744. might_sleep();
  1745. spin_lock_bh(&sk->sk_lock.slock);
  1746. if (!sk->sk_lock.owned)
  1747. /*
  1748. * Note : We must disable BH
  1749. */
  1750. return false;
  1751. __lock_sock(sk);
  1752. sk->sk_lock.owned = 1;
  1753. spin_unlock(&sk->sk_lock.slock);
  1754. /*
  1755. * The sk_lock has mutex_lock() semantics here:
  1756. */
  1757. mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
  1758. local_bh_enable();
  1759. return true;
  1760. }
  1761. EXPORT_SYMBOL(lock_sock_fast);
  1762. int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  1763. {
  1764. struct timeval tv;
  1765. if (!sock_flag(sk, SOCK_TIMESTAMP))
  1766. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  1767. tv = ktime_to_timeval(sk->sk_stamp);
  1768. if (tv.tv_sec == -1)
  1769. return -ENOENT;
  1770. if (tv.tv_sec == 0) {
  1771. sk->sk_stamp = ktime_get_real();
  1772. tv = ktime_to_timeval(sk->sk_stamp);
  1773. }
  1774. return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
  1775. }
  1776. EXPORT_SYMBOL(sock_get_timestamp);
  1777. int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  1778. {
  1779. struct timespec ts;
  1780. if (!sock_flag(sk, SOCK_TIMESTAMP))
  1781. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  1782. ts = ktime_to_timespec(sk->sk_stamp);
  1783. if (ts.tv_sec == -1)
  1784. return -ENOENT;
  1785. if (ts.tv_sec == 0) {
  1786. sk->sk_stamp = ktime_get_real();
  1787. ts = ktime_to_timespec(sk->sk_stamp);
  1788. }
  1789. return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
  1790. }
  1791. EXPORT_SYMBOL(sock_get_timestampns);
  1792. void sock_enable_timestamp(struct sock *sk, int flag)
  1793. {
  1794. if (!sock_flag(sk, flag)) {
  1795. sock_set_flag(sk, flag);
  1796. /*
  1797. * we just set one of the two flags which require net
  1798. * time stamping, but time stamping might have been on
  1799. * already because of the other one
  1800. */
  1801. if (!sock_flag(sk,
  1802. flag == SOCK_TIMESTAMP ?
  1803. SOCK_TIMESTAMPING_RX_SOFTWARE :
  1804. SOCK_TIMESTAMP))
  1805. net_enable_timestamp();
  1806. }
  1807. }
  1808. /*
  1809. * Get a socket option on an socket.
  1810. *
  1811. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  1812. * asynchronous errors should be reported by getsockopt. We assume
  1813. * this means if you specify SO_ERROR (otherwise whats the point of it).
  1814. */
  1815. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  1816. char __user *optval, int __user *optlen)
  1817. {
  1818. struct sock *sk = sock->sk;
  1819. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1820. }
  1821. EXPORT_SYMBOL(sock_common_getsockopt);
  1822. #ifdef CONFIG_COMPAT
  1823. int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
  1824. char __user *optval, int __user *optlen)
  1825. {
  1826. struct sock *sk = sock->sk;
  1827. if (sk->sk_prot->compat_getsockopt != NULL)
  1828. return sk->sk_prot->compat_getsockopt(sk, level, optname,
  1829. optval, optlen);
  1830. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  1831. }
  1832. EXPORT_SYMBOL(compat_sock_common_getsockopt);
  1833. #endif
  1834. int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
  1835. struct msghdr *msg, size_t size, int flags)
  1836. {
  1837. struct sock *sk = sock->sk;
  1838. int addr_len = 0;
  1839. int err;
  1840. err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
  1841. flags & ~MSG_DONTWAIT, &addr_len);
  1842. if (err >= 0)
  1843. msg->msg_namelen = addr_len;
  1844. return err;
  1845. }
  1846. EXPORT_SYMBOL(sock_common_recvmsg);
  1847. /*
  1848. * Set socket options on an inet socket.
  1849. */
  1850. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  1851. char __user *optval, unsigned int optlen)
  1852. {
  1853. struct sock *sk = sock->sk;
  1854. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1855. }
  1856. EXPORT_SYMBOL(sock_common_setsockopt);
  1857. #ifdef CONFIG_COMPAT
  1858. int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
  1859. char __user *optval, unsigned int optlen)
  1860. {
  1861. struct sock *sk = sock->sk;
  1862. if (sk->sk_prot->compat_setsockopt != NULL)
  1863. return sk->sk_prot->compat_setsockopt(sk, level, optname,
  1864. optval, optlen);
  1865. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  1866. }
  1867. EXPORT_SYMBOL(compat_sock_common_setsockopt);
  1868. #endif
  1869. void sk_common_release(struct sock *sk)
  1870. {
  1871. if (sk->sk_prot->destroy)
  1872. sk->sk_prot->destroy(sk);
  1873. /*
  1874. * Observation: when sock_common_release is called, processes have
  1875. * no access to socket. But net still has.
  1876. * Step one, detach it from networking:
  1877. *
  1878. * A. Remove from hash tables.
  1879. */
  1880. sk->sk_prot->unhash(sk);
  1881. /*
  1882. * In this point socket cannot receive new packets, but it is possible
  1883. * that some packets are in flight because some CPU runs receiver and
  1884. * did hash table lookup before we unhashed socket. They will achieve
  1885. * receive queue and will be purged by socket destructor.
  1886. *
  1887. * Also we still have packets pending on receive queue and probably,
  1888. * our own packets waiting in device queues. sock_destroy will drain
  1889. * receive queue, but transmitted packets will delay socket destruction
  1890. * until the last reference will be released.
  1891. */
  1892. sock_orphan(sk);
  1893. xfrm_sk_free_policy(sk);
  1894. sk_refcnt_debug_release(sk);
  1895. sock_put(sk);
  1896. }
  1897. EXPORT_SYMBOL(sk_common_release);
  1898. static DEFINE_RWLOCK(proto_list_lock);
  1899. static LIST_HEAD(proto_list);
  1900. #ifdef CONFIG_PROC_FS
  1901. #define PROTO_INUSE_NR 64 /* should be enough for the first time */
  1902. struct prot_inuse {
  1903. int val[PROTO_INUSE_NR];
  1904. };
  1905. static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
  1906. #ifdef CONFIG_NET_NS
  1907. void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
  1908. {
  1909. int cpu = smp_processor_id();
  1910. per_cpu_ptr(net->core.inuse, cpu)->val[prot->inuse_idx] += val;
  1911. }
  1912. EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
  1913. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  1914. {
  1915. int cpu, idx = prot->inuse_idx;
  1916. int res = 0;
  1917. for_each_possible_cpu(cpu)
  1918. res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
  1919. return res >= 0 ? res : 0;
  1920. }
  1921. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  1922. static int __net_init sock_inuse_init_net(struct net *net)
  1923. {
  1924. net->core.inuse = alloc_percpu(struct prot_inuse);
  1925. return net->core.inuse ? 0 : -ENOMEM;
  1926. }
  1927. static void __net_exit sock_inuse_exit_net(struct net *net)
  1928. {
  1929. free_percpu(net->core.inuse);
  1930. }
  1931. static struct pernet_operations net_inuse_ops = {
  1932. .init = sock_inuse_init_net,
  1933. .exit = sock_inuse_exit_net,
  1934. };
  1935. static __init int net_inuse_init(void)
  1936. {
  1937. if (register_pernet_subsys(&net_inuse_ops))
  1938. panic("Cannot initialize net inuse counters");
  1939. return 0;
  1940. }
  1941. core_initcall(net_inuse_init);
  1942. #else
  1943. static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
  1944. void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
  1945. {
  1946. __get_cpu_var(prot_inuse).val[prot->inuse_idx] += val;
  1947. }
  1948. EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
  1949. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  1950. {
  1951. int cpu, idx = prot->inuse_idx;
  1952. int res = 0;
  1953. for_each_possible_cpu(cpu)
  1954. res += per_cpu(prot_inuse, cpu).val[idx];
  1955. return res >= 0 ? res : 0;
  1956. }
  1957. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  1958. #endif
  1959. static void assign_proto_idx(struct proto *prot)
  1960. {
  1961. prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
  1962. if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
  1963. printk(KERN_ERR "PROTO_INUSE_NR exhausted\n");
  1964. return;
  1965. }
  1966. set_bit(prot->inuse_idx, proto_inuse_idx);
  1967. }
  1968. static void release_proto_idx(struct proto *prot)
  1969. {
  1970. if (prot->inuse_idx != PROTO_INUSE_NR - 1)
  1971. clear_bit(prot->inuse_idx, proto_inuse_idx);
  1972. }
  1973. #else
  1974. static inline void assign_proto_idx(struct proto *prot)
  1975. {
  1976. }
  1977. static inline void release_proto_idx(struct proto *prot)
  1978. {
  1979. }
  1980. #endif
  1981. int proto_register(struct proto *prot, int alloc_slab)
  1982. {
  1983. if (alloc_slab) {
  1984. prot->slab = kmem_cache_create(prot->name, prot->obj_size, 0,
  1985. SLAB_HWCACHE_ALIGN | prot->slab_flags,
  1986. NULL);
  1987. if (prot->slab == NULL) {
  1988. printk(KERN_CRIT "%s: Can't create sock SLAB cache!\n",
  1989. prot->name);
  1990. goto out;
  1991. }
  1992. if (prot->rsk_prot != NULL) {
  1993. prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name);
  1994. if (prot->rsk_prot->slab_name == NULL)
  1995. goto out_free_sock_slab;
  1996. prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
  1997. prot->rsk_prot->obj_size, 0,
  1998. SLAB_HWCACHE_ALIGN, NULL);
  1999. if (prot->rsk_prot->slab == NULL) {
  2000. printk(KERN_CRIT "%s: Can't create request sock SLAB cache!\n",
  2001. prot->name);
  2002. goto out_free_request_sock_slab_name;
  2003. }
  2004. }
  2005. if (prot->twsk_prot != NULL) {
  2006. prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
  2007. if (prot->twsk_prot->twsk_slab_name == NULL)
  2008. goto out_free_request_sock_slab;
  2009. prot->twsk_prot->twsk_slab =
  2010. kmem_cache_create(prot->twsk_prot->twsk_slab_name,
  2011. prot->twsk_prot->twsk_obj_size,
  2012. 0,
  2013. SLAB_HWCACHE_ALIGN |
  2014. prot->slab_flags,
  2015. NULL);
  2016. if (prot->twsk_prot->twsk_slab == NULL)
  2017. goto out_free_timewait_sock_slab_name;
  2018. }
  2019. }
  2020. write_lock(&proto_list_lock);
  2021. list_add(&prot->node, &proto_list);
  2022. assign_proto_idx(prot);
  2023. write_unlock(&proto_list_lock);
  2024. return 0;
  2025. out_free_timewait_sock_slab_name:
  2026. kfree(prot->twsk_prot->twsk_slab_name);
  2027. out_free_request_sock_slab:
  2028. if (prot->rsk_prot && prot->rsk_prot->slab) {
  2029. kmem_cache_destroy(prot->rsk_prot->slab);
  2030. prot->rsk_prot->slab = NULL;
  2031. }
  2032. out_free_request_sock_slab_name:
  2033. if (prot->rsk_prot)
  2034. kfree(prot->rsk_prot->slab_name);
  2035. out_free_sock_slab:
  2036. kmem_cache_destroy(prot->slab);
  2037. prot->slab = NULL;
  2038. out:
  2039. return -ENOBUFS;
  2040. }
  2041. EXPORT_SYMBOL(proto_register);
  2042. void proto_unregister(struct proto *prot)
  2043. {
  2044. write_lock(&proto_list_lock);
  2045. release_proto_idx(prot);
  2046. list_del(&prot->node);
  2047. write_unlock(&proto_list_lock);
  2048. if (prot->slab != NULL) {
  2049. kmem_cache_destroy(prot->slab);
  2050. prot->slab = NULL;
  2051. }
  2052. if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) {
  2053. kmem_cache_destroy(prot->rsk_prot->slab);
  2054. kfree(prot->rsk_prot->slab_name);
  2055. prot->rsk_prot->slab = NULL;
  2056. }
  2057. if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
  2058. kmem_cache_destroy(prot->twsk_prot->twsk_slab);
  2059. kfree(prot->twsk_prot->twsk_slab_name);
  2060. prot->twsk_prot->twsk_slab = NULL;
  2061. }
  2062. }
  2063. EXPORT_SYMBOL(proto_unregister);
  2064. #ifdef CONFIG_PROC_FS
  2065. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  2066. __acquires(proto_list_lock)
  2067. {
  2068. read_lock(&proto_list_lock);
  2069. return seq_list_start_head(&proto_list, *pos);
  2070. }
  2071. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2072. {
  2073. return seq_list_next(v, &proto_list, pos);
  2074. }
  2075. static void proto_seq_stop(struct seq_file *seq, void *v)
  2076. __releases(proto_list_lock)
  2077. {
  2078. read_unlock(&proto_list_lock);
  2079. }
  2080. static char proto_method_implemented(const void *method)
  2081. {
  2082. return method == NULL ? 'n' : 'y';
  2083. }
  2084. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  2085. {
  2086. seq_printf(seq, "%-9s %4u %6d %6d %-3s %6u %-3s %-10s "
  2087. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  2088. proto->name,
  2089. proto->obj_size,
  2090. sock_prot_inuse_get(seq_file_net(seq), proto),
  2091. proto->memory_allocated != NULL ? atomic_read(proto->memory_allocated) : -1,
  2092. proto->memory_pressure != NULL ? *proto->memory_pressure ? "yes" : "no" : "NI",
  2093. proto->max_header,
  2094. proto->slab == NULL ? "no" : "yes",
  2095. module_name(proto->owner),
  2096. proto_method_implemented(proto->close),
  2097. proto_method_implemented(proto->connect),
  2098. proto_method_implemented(proto->disconnect),
  2099. proto_method_implemented(proto->accept),
  2100. proto_method_implemented(proto->ioctl),
  2101. proto_method_implemented(proto->init),
  2102. proto_method_implemented(proto->destroy),
  2103. proto_method_implemented(proto->shutdown),
  2104. proto_method_implemented(proto->setsockopt),
  2105. proto_method_implemented(proto->getsockopt),
  2106. proto_method_implemented(proto->sendmsg),
  2107. proto_method_implemented(proto->recvmsg),
  2108. proto_method_implemented(proto->sendpage),
  2109. proto_method_implemented(proto->bind),
  2110. proto_method_implemented(proto->backlog_rcv),
  2111. proto_method_implemented(proto->hash),
  2112. proto_method_implemented(proto->unhash),
  2113. proto_method_implemented(proto->get_port),
  2114. proto_method_implemented(proto->enter_memory_pressure));
  2115. }
  2116. static int proto_seq_show(struct seq_file *seq, void *v)
  2117. {
  2118. if (v == &proto_list)
  2119. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  2120. "protocol",
  2121. "size",
  2122. "sockets",
  2123. "memory",
  2124. "press",
  2125. "maxhdr",
  2126. "slab",
  2127. "module",
  2128. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  2129. else
  2130. proto_seq_printf(seq, list_entry(v, struct proto, node));
  2131. return 0;
  2132. }
  2133. static const struct seq_operations proto_seq_ops = {
  2134. .start = proto_seq_start,
  2135. .next = proto_seq_next,
  2136. .stop = proto_seq_stop,
  2137. .show = proto_seq_show,
  2138. };
  2139. static int proto_seq_open(struct inode *inode, struct file *file)
  2140. {
  2141. return seq_open_net(inode, file, &proto_seq_ops,
  2142. sizeof(struct seq_net_private));
  2143. }
  2144. static const struct file_operations proto_seq_fops = {
  2145. .owner = THIS_MODULE,
  2146. .open = proto_seq_open,
  2147. .read = seq_read,
  2148. .llseek = seq_lseek,
  2149. .release = seq_release_net,
  2150. };
  2151. static __net_init int proto_init_net(struct net *net)
  2152. {
  2153. if (!proc_net_fops_create(net, "protocols", S_IRUGO, &proto_seq_fops))
  2154. return -ENOMEM;
  2155. return 0;
  2156. }
  2157. static __net_exit void proto_exit_net(struct net *net)
  2158. {
  2159. proc_net_remove(net, "protocols");
  2160. }
  2161. static __net_initdata struct pernet_operations proto_net_ops = {
  2162. .init = proto_init_net,
  2163. .exit = proto_exit_net,
  2164. };
  2165. static int __init proto_init(void)
  2166. {
  2167. return register_pernet_subsys(&proto_net_ops);
  2168. }
  2169. subsys_initcall(proto_init);
  2170. #endif /* PROC_FS */