sock.c 59 KB

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