socket.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385
  1. /*
  2. * NET An implementation of the SOCKET network access protocol.
  3. *
  4. * Version: @(#)socket.c 1.1.93 18/02/95
  5. *
  6. * Authors: Orest Zborowski, <obz@Kodak.COM>
  7. * Ross Biro
  8. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  9. *
  10. * Fixes:
  11. * Anonymous : NOTSOCK/BADF cleanup. Error fix in
  12. * shutdown()
  13. * Alan Cox : verify_area() fixes
  14. * Alan Cox : Removed DDI
  15. * Jonathan Kamens : SOCK_DGRAM reconnect bug
  16. * Alan Cox : Moved a load of checks to the very
  17. * top level.
  18. * Alan Cox : Move address structures to/from user
  19. * mode above the protocol layers.
  20. * Rob Janssen : Allow 0 length sends.
  21. * Alan Cox : Asynchronous I/O support (cribbed from the
  22. * tty drivers).
  23. * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
  24. * Jeff Uphoff : Made max number of sockets command-line
  25. * configurable.
  26. * Matti Aarnio : Made the number of sockets dynamic,
  27. * to be allocated when needed, and mr.
  28. * Uphoff's max is used as max to be
  29. * allowed to allocate.
  30. * Linus : Argh. removed all the socket allocation
  31. * altogether: it's in the inode now.
  32. * Alan Cox : Made sock_alloc()/sock_release() public
  33. * for NetROM and future kernel nfsd type
  34. * stuff.
  35. * Alan Cox : sendmsg/recvmsg basics.
  36. * Tom Dyas : Export net symbols.
  37. * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
  38. * Alan Cox : Added thread locking to sys_* calls
  39. * for sockets. May have errors at the
  40. * moment.
  41. * Kevin Buhr : Fixed the dumb errors in the above.
  42. * Andi Kleen : Some small cleanups, optimizations,
  43. * and fixed a copy_from_user() bug.
  44. * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
  45. * Tigran Aivazian : Made listen(2) backlog sanity checks
  46. * protocol-independent
  47. *
  48. *
  49. * This program is free software; you can redistribute it and/or
  50. * modify it under the terms of the GNU General Public License
  51. * as published by the Free Software Foundation; either version
  52. * 2 of the License, or (at your option) any later version.
  53. *
  54. *
  55. * This module is effectively the top level interface to the BSD socket
  56. * paradigm.
  57. *
  58. * Based upon Swansea University Computer Society NET3.039
  59. */
  60. #include <linux/mm.h>
  61. #include <linux/socket.h>
  62. #include <linux/file.h>
  63. #include <linux/net.h>
  64. #include <linux/interrupt.h>
  65. #include <linux/thread_info.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/netdevice.h>
  68. #include <linux/proc_fs.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/mutex.h>
  71. #include <linux/wanrouter.h>
  72. #include <linux/if_bridge.h>
  73. #include <linux/if_frad.h>
  74. #include <linux/if_vlan.h>
  75. #include <linux/init.h>
  76. #include <linux/poll.h>
  77. #include <linux/cache.h>
  78. #include <linux/module.h>
  79. #include <linux/highmem.h>
  80. #include <linux/mount.h>
  81. #include <linux/security.h>
  82. #include <linux/syscalls.h>
  83. #include <linux/compat.h>
  84. #include <linux/kmod.h>
  85. #include <linux/audit.h>
  86. #include <linux/wireless.h>
  87. #include <linux/nsproxy.h>
  88. #include <linux/magic.h>
  89. #include <linux/slab.h>
  90. #include <asm/uaccess.h>
  91. #include <asm/unistd.h>
  92. #include <net/compat.h>
  93. #include <net/wext.h>
  94. #include <net/cls_cgroup.h>
  95. #include <net/sock.h>
  96. #include <linux/netfilter.h>
  97. #include <linux/if_tun.h>
  98. #include <linux/ipv6_route.h>
  99. #include <linux/route.h>
  100. #include <linux/sockios.h>
  101. #include <linux/atalk.h>
  102. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  103. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  104. unsigned long nr_segs, loff_t pos);
  105. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  106. unsigned long nr_segs, loff_t pos);
  107. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  108. static int sock_close(struct inode *inode, struct file *file);
  109. static unsigned int sock_poll(struct file *file,
  110. struct poll_table_struct *wait);
  111. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  112. #ifdef CONFIG_COMPAT
  113. static long compat_sock_ioctl(struct file *file,
  114. unsigned int cmd, unsigned long arg);
  115. #endif
  116. static int sock_fasync(int fd, struct file *filp, int on);
  117. static ssize_t sock_sendpage(struct file *file, struct page *page,
  118. int offset, size_t size, loff_t *ppos, int more);
  119. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  120. struct pipe_inode_info *pipe, size_t len,
  121. unsigned int flags);
  122. /*
  123. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  124. * in the operation structures but are done directly via the socketcall() multiplexor.
  125. */
  126. static const struct file_operations socket_file_ops = {
  127. .owner = THIS_MODULE,
  128. .llseek = no_llseek,
  129. .aio_read = sock_aio_read,
  130. .aio_write = sock_aio_write,
  131. .poll = sock_poll,
  132. .unlocked_ioctl = sock_ioctl,
  133. #ifdef CONFIG_COMPAT
  134. .compat_ioctl = compat_sock_ioctl,
  135. #endif
  136. .mmap = sock_mmap,
  137. .open = sock_no_open, /* special open code to disallow open via /proc */
  138. .release = sock_close,
  139. .fasync = sock_fasync,
  140. .sendpage = sock_sendpage,
  141. .splice_write = generic_splice_sendpage,
  142. .splice_read = sock_splice_read,
  143. };
  144. /*
  145. * The protocol list. Each protocol is registered in here.
  146. */
  147. static DEFINE_SPINLOCK(net_family_lock);
  148. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  149. /*
  150. * Statistics counters of the socket lists
  151. */
  152. static DEFINE_PER_CPU(int, sockets_in_use);
  153. /*
  154. * Support routines.
  155. * Move socket addresses back and forth across the kernel/user
  156. * divide and look after the messy bits.
  157. */
  158. /**
  159. * move_addr_to_kernel - copy a socket address into kernel space
  160. * @uaddr: Address in user space
  161. * @kaddr: Address in kernel space
  162. * @ulen: Length in user space
  163. *
  164. * The address is copied into kernel space. If the provided address is
  165. * too long an error code of -EINVAL is returned. If the copy gives
  166. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  167. */
  168. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  169. {
  170. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  171. return -EINVAL;
  172. if (ulen == 0)
  173. return 0;
  174. if (copy_from_user(kaddr, uaddr, ulen))
  175. return -EFAULT;
  176. return audit_sockaddr(ulen, kaddr);
  177. }
  178. /**
  179. * move_addr_to_user - copy an address to user space
  180. * @kaddr: kernel space address
  181. * @klen: length of address in kernel
  182. * @uaddr: user space address
  183. * @ulen: pointer to user length field
  184. *
  185. * The value pointed to by ulen on entry is the buffer length available.
  186. * This is overwritten with the buffer space used. -EINVAL is returned
  187. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  188. * is returned if either the buffer or the length field are not
  189. * accessible.
  190. * After copying the data up to the limit the user specifies, the true
  191. * length of the data is written over the length limit the user
  192. * specified. Zero is returned for a success.
  193. */
  194. static int move_addr_to_user(struct sockaddr *kaddr, int klen,
  195. void __user *uaddr, int __user *ulen)
  196. {
  197. int err;
  198. int len;
  199. err = get_user(len, ulen);
  200. if (err)
  201. return err;
  202. if (len > klen)
  203. len = klen;
  204. if (len < 0 || len > sizeof(struct sockaddr_storage))
  205. return -EINVAL;
  206. if (len) {
  207. if (audit_sockaddr(klen, kaddr))
  208. return -ENOMEM;
  209. if (copy_to_user(uaddr, kaddr, len))
  210. return -EFAULT;
  211. }
  212. /*
  213. * "fromlen shall refer to the value before truncation.."
  214. * 1003.1g
  215. */
  216. return __put_user(klen, ulen);
  217. }
  218. static struct kmem_cache *sock_inode_cachep __read_mostly;
  219. static struct inode *sock_alloc_inode(struct super_block *sb)
  220. {
  221. struct socket_alloc *ei;
  222. struct socket_wq *wq;
  223. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  224. if (!ei)
  225. return NULL;
  226. wq = kmalloc(sizeof(*wq), GFP_KERNEL);
  227. if (!wq) {
  228. kmem_cache_free(sock_inode_cachep, ei);
  229. return NULL;
  230. }
  231. init_waitqueue_head(&wq->wait);
  232. wq->fasync_list = NULL;
  233. RCU_INIT_POINTER(ei->socket.wq, wq);
  234. ei->socket.state = SS_UNCONNECTED;
  235. ei->socket.flags = 0;
  236. ei->socket.ops = NULL;
  237. ei->socket.sk = NULL;
  238. ei->socket.file = NULL;
  239. return &ei->vfs_inode;
  240. }
  241. static void sock_destroy_inode(struct inode *inode)
  242. {
  243. struct socket_alloc *ei;
  244. struct socket_wq *wq;
  245. ei = container_of(inode, struct socket_alloc, vfs_inode);
  246. wq = rcu_dereference_protected(ei->socket.wq, 1);
  247. kfree_rcu(wq, rcu);
  248. kmem_cache_free(sock_inode_cachep, ei);
  249. }
  250. static void init_once(void *foo)
  251. {
  252. struct socket_alloc *ei = (struct socket_alloc *)foo;
  253. inode_init_once(&ei->vfs_inode);
  254. }
  255. static int init_inodecache(void)
  256. {
  257. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  258. sizeof(struct socket_alloc),
  259. 0,
  260. (SLAB_HWCACHE_ALIGN |
  261. SLAB_RECLAIM_ACCOUNT |
  262. SLAB_MEM_SPREAD),
  263. init_once);
  264. if (sock_inode_cachep == NULL)
  265. return -ENOMEM;
  266. return 0;
  267. }
  268. static const struct super_operations sockfs_ops = {
  269. .alloc_inode = sock_alloc_inode,
  270. .destroy_inode = sock_destroy_inode,
  271. .statfs = simple_statfs,
  272. };
  273. /*
  274. * sockfs_dname() is called from d_path().
  275. */
  276. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  277. {
  278. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  279. dentry->d_inode->i_ino);
  280. }
  281. static const struct dentry_operations sockfs_dentry_operations = {
  282. .d_dname = sockfs_dname,
  283. };
  284. static struct dentry *sockfs_mount(struct file_system_type *fs_type,
  285. int flags, const char *dev_name, void *data)
  286. {
  287. return mount_pseudo(fs_type, "socket:", &sockfs_ops,
  288. &sockfs_dentry_operations, SOCKFS_MAGIC);
  289. }
  290. static struct vfsmount *sock_mnt __read_mostly;
  291. static struct file_system_type sock_fs_type = {
  292. .name = "sockfs",
  293. .mount = sockfs_mount,
  294. .kill_sb = kill_anon_super,
  295. };
  296. /*
  297. * Obtains the first available file descriptor and sets it up for use.
  298. *
  299. * These functions create file structures and maps them to fd space
  300. * of the current process. On success it returns file descriptor
  301. * and file struct implicitly stored in sock->file.
  302. * Note that another thread may close file descriptor before we return
  303. * from this function. We use the fact that now we do not refer
  304. * to socket after mapping. If one day we will need it, this
  305. * function will increment ref. count on file by 1.
  306. *
  307. * In any case returned fd MAY BE not valid!
  308. * This race condition is unavoidable
  309. * with shared fd spaces, we cannot solve it inside kernel,
  310. * but we take care of internal coherence yet.
  311. */
  312. static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
  313. {
  314. struct qstr name = { .name = "" };
  315. struct path path;
  316. struct file *file;
  317. int fd;
  318. fd = get_unused_fd_flags(flags);
  319. if (unlikely(fd < 0))
  320. return fd;
  321. path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
  322. if (unlikely(!path.dentry)) {
  323. put_unused_fd(fd);
  324. return -ENOMEM;
  325. }
  326. path.mnt = mntget(sock_mnt);
  327. d_instantiate(path.dentry, SOCK_INODE(sock));
  328. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  329. file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
  330. &socket_file_ops);
  331. if (unlikely(!file)) {
  332. /* drop dentry, keep inode */
  333. ihold(path.dentry->d_inode);
  334. path_put(&path);
  335. put_unused_fd(fd);
  336. return -ENFILE;
  337. }
  338. sock->file = file;
  339. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  340. file->f_pos = 0;
  341. file->private_data = sock;
  342. *f = file;
  343. return fd;
  344. }
  345. int sock_map_fd(struct socket *sock, int flags)
  346. {
  347. struct file *newfile;
  348. int fd = sock_alloc_file(sock, &newfile, flags);
  349. if (likely(fd >= 0))
  350. fd_install(fd, newfile);
  351. return fd;
  352. }
  353. EXPORT_SYMBOL(sock_map_fd);
  354. static struct socket *sock_from_file(struct file *file, int *err)
  355. {
  356. if (file->f_op == &socket_file_ops)
  357. return file->private_data; /* set in sock_map_fd */
  358. *err = -ENOTSOCK;
  359. return NULL;
  360. }
  361. /**
  362. * sockfd_lookup - Go from a file number to its socket slot
  363. * @fd: file handle
  364. * @err: pointer to an error code return
  365. *
  366. * The file handle passed in is locked and the socket it is bound
  367. * too is returned. If an error occurs the err pointer is overwritten
  368. * with a negative errno code and NULL is returned. The function checks
  369. * for both invalid handles and passing a handle which is not a socket.
  370. *
  371. * On a success the socket object pointer is returned.
  372. */
  373. struct socket *sockfd_lookup(int fd, int *err)
  374. {
  375. struct file *file;
  376. struct socket *sock;
  377. file = fget(fd);
  378. if (!file) {
  379. *err = -EBADF;
  380. return NULL;
  381. }
  382. sock = sock_from_file(file, err);
  383. if (!sock)
  384. fput(file);
  385. return sock;
  386. }
  387. EXPORT_SYMBOL(sockfd_lookup);
  388. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  389. {
  390. struct file *file;
  391. struct socket *sock;
  392. *err = -EBADF;
  393. file = fget_light(fd, fput_needed);
  394. if (file) {
  395. sock = sock_from_file(file, err);
  396. if (sock)
  397. return sock;
  398. fput_light(file, *fput_needed);
  399. }
  400. return NULL;
  401. }
  402. /**
  403. * sock_alloc - allocate a socket
  404. *
  405. * Allocate a new inode and socket object. The two are bound together
  406. * and initialised. The socket is then returned. If we are out of inodes
  407. * NULL is returned.
  408. */
  409. static struct socket *sock_alloc(void)
  410. {
  411. struct inode *inode;
  412. struct socket *sock;
  413. inode = new_inode(sock_mnt->mnt_sb);
  414. if (!inode)
  415. return NULL;
  416. sock = SOCKET_I(inode);
  417. kmemcheck_annotate_bitfield(sock, type);
  418. inode->i_ino = get_next_ino();
  419. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  420. inode->i_uid = current_fsuid();
  421. inode->i_gid = current_fsgid();
  422. percpu_add(sockets_in_use, 1);
  423. return sock;
  424. }
  425. /*
  426. * In theory you can't get an open on this inode, but /proc provides
  427. * a back door. Remember to keep it shut otherwise you'll let the
  428. * creepy crawlies in.
  429. */
  430. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  431. {
  432. return -ENXIO;
  433. }
  434. const struct file_operations bad_sock_fops = {
  435. .owner = THIS_MODULE,
  436. .open = sock_no_open,
  437. .llseek = noop_llseek,
  438. };
  439. /**
  440. * sock_release - close a socket
  441. * @sock: socket to close
  442. *
  443. * The socket is released from the protocol stack if it has a release
  444. * callback, and the inode is then released if the socket is bound to
  445. * an inode not a file.
  446. */
  447. void sock_release(struct socket *sock)
  448. {
  449. if (sock->ops) {
  450. struct module *owner = sock->ops->owner;
  451. sock->ops->release(sock);
  452. sock->ops = NULL;
  453. module_put(owner);
  454. }
  455. if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
  456. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  457. percpu_sub(sockets_in_use, 1);
  458. if (!sock->file) {
  459. iput(SOCK_INODE(sock));
  460. return;
  461. }
  462. sock->file = NULL;
  463. }
  464. EXPORT_SYMBOL(sock_release);
  465. int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
  466. {
  467. *tx_flags = 0;
  468. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  469. *tx_flags |= SKBTX_HW_TSTAMP;
  470. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  471. *tx_flags |= SKBTX_SW_TSTAMP;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(sock_tx_timestamp);
  475. static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
  476. struct msghdr *msg, size_t size)
  477. {
  478. struct sock_iocb *si = kiocb_to_siocb(iocb);
  479. sock_update_classid(sock->sk);
  480. si->sock = sock;
  481. si->scm = NULL;
  482. si->msg = msg;
  483. si->size = size;
  484. return sock->ops->sendmsg(iocb, sock, msg, size);
  485. }
  486. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  487. struct msghdr *msg, size_t size)
  488. {
  489. int err = security_socket_sendmsg(sock, msg, size);
  490. return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
  491. }
  492. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  493. {
  494. struct kiocb iocb;
  495. struct sock_iocb siocb;
  496. int ret;
  497. init_sync_kiocb(&iocb, NULL);
  498. iocb.private = &siocb;
  499. ret = __sock_sendmsg(&iocb, sock, msg, size);
  500. if (-EIOCBQUEUED == ret)
  501. ret = wait_on_sync_kiocb(&iocb);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(sock_sendmsg);
  505. int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
  506. {
  507. struct kiocb iocb;
  508. struct sock_iocb siocb;
  509. int ret;
  510. init_sync_kiocb(&iocb, NULL);
  511. iocb.private = &siocb;
  512. ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
  513. if (-EIOCBQUEUED == ret)
  514. ret = wait_on_sync_kiocb(&iocb);
  515. return ret;
  516. }
  517. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  518. struct kvec *vec, size_t num, size_t size)
  519. {
  520. mm_segment_t oldfs = get_fs();
  521. int result;
  522. set_fs(KERNEL_DS);
  523. /*
  524. * the following is safe, since for compiler definitions of kvec and
  525. * iovec are identical, yielding the same in-core layout and alignment
  526. */
  527. msg->msg_iov = (struct iovec *)vec;
  528. msg->msg_iovlen = num;
  529. result = sock_sendmsg(sock, msg, size);
  530. set_fs(oldfs);
  531. return result;
  532. }
  533. EXPORT_SYMBOL(kernel_sendmsg);
  534. static int ktime2ts(ktime_t kt, struct timespec *ts)
  535. {
  536. if (kt.tv64) {
  537. *ts = ktime_to_timespec(kt);
  538. return 1;
  539. } else {
  540. return 0;
  541. }
  542. }
  543. /*
  544. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  545. */
  546. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  547. struct sk_buff *skb)
  548. {
  549. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  550. struct timespec ts[3];
  551. int empty = 1;
  552. struct skb_shared_hwtstamps *shhwtstamps =
  553. skb_hwtstamps(skb);
  554. /* Race occurred between timestamp enabling and packet
  555. receiving. Fill in the current time for now. */
  556. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  557. __net_timestamp(skb);
  558. if (need_software_tstamp) {
  559. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  560. struct timeval tv;
  561. skb_get_timestamp(skb, &tv);
  562. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  563. sizeof(tv), &tv);
  564. } else {
  565. skb_get_timestampns(skb, &ts[0]);
  566. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  567. sizeof(ts[0]), &ts[0]);
  568. }
  569. }
  570. memset(ts, 0, sizeof(ts));
  571. if (skb->tstamp.tv64 &&
  572. sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
  573. skb_get_timestampns(skb, ts + 0);
  574. empty = 0;
  575. }
  576. if (shhwtstamps) {
  577. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  578. ktime2ts(shhwtstamps->syststamp, ts + 1))
  579. empty = 0;
  580. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  581. ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  582. empty = 0;
  583. }
  584. if (!empty)
  585. put_cmsg(msg, SOL_SOCKET,
  586. SCM_TIMESTAMPING, sizeof(ts), &ts);
  587. }
  588. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  589. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  590. struct sk_buff *skb)
  591. {
  592. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
  593. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  594. sizeof(__u32), &skb->dropcount);
  595. }
  596. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  597. struct sk_buff *skb)
  598. {
  599. sock_recv_timestamp(msg, sk, skb);
  600. sock_recv_drops(msg, sk, skb);
  601. }
  602. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  603. static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
  604. struct msghdr *msg, size_t size, int flags)
  605. {
  606. struct sock_iocb *si = kiocb_to_siocb(iocb);
  607. sock_update_classid(sock->sk);
  608. si->sock = sock;
  609. si->scm = NULL;
  610. si->msg = msg;
  611. si->size = size;
  612. si->flags = flags;
  613. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  614. }
  615. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  616. struct msghdr *msg, size_t size, int flags)
  617. {
  618. int err = security_socket_recvmsg(sock, msg, size, flags);
  619. return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
  620. }
  621. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  622. size_t size, int flags)
  623. {
  624. struct kiocb iocb;
  625. struct sock_iocb siocb;
  626. int ret;
  627. init_sync_kiocb(&iocb, NULL);
  628. iocb.private = &siocb;
  629. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  630. if (-EIOCBQUEUED == ret)
  631. ret = wait_on_sync_kiocb(&iocb);
  632. return ret;
  633. }
  634. EXPORT_SYMBOL(sock_recvmsg);
  635. static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  636. size_t size, int flags)
  637. {
  638. struct kiocb iocb;
  639. struct sock_iocb siocb;
  640. int ret;
  641. init_sync_kiocb(&iocb, NULL);
  642. iocb.private = &siocb;
  643. ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
  644. if (-EIOCBQUEUED == ret)
  645. ret = wait_on_sync_kiocb(&iocb);
  646. return ret;
  647. }
  648. /**
  649. * kernel_recvmsg - Receive a message from a socket (kernel space)
  650. * @sock: The socket to receive the message from
  651. * @msg: Received message
  652. * @vec: Input s/g array for message data
  653. * @num: Size of input s/g array
  654. * @size: Number of bytes to read
  655. * @flags: Message flags (MSG_DONTWAIT, etc...)
  656. *
  657. * On return the msg structure contains the scatter/gather array passed in the
  658. * vec argument. The array is modified so that it consists of the unfilled
  659. * portion of the original array.
  660. *
  661. * The returned value is the total number of bytes received, or an error.
  662. */
  663. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  664. struct kvec *vec, size_t num, size_t size, int flags)
  665. {
  666. mm_segment_t oldfs = get_fs();
  667. int result;
  668. set_fs(KERNEL_DS);
  669. /*
  670. * the following is safe, since for compiler definitions of kvec and
  671. * iovec are identical, yielding the same in-core layout and alignment
  672. */
  673. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  674. result = sock_recvmsg(sock, msg, size, flags);
  675. set_fs(oldfs);
  676. return result;
  677. }
  678. EXPORT_SYMBOL(kernel_recvmsg);
  679. static void sock_aio_dtor(struct kiocb *iocb)
  680. {
  681. kfree(iocb->private);
  682. }
  683. static ssize_t sock_sendpage(struct file *file, struct page *page,
  684. int offset, size_t size, loff_t *ppos, int more)
  685. {
  686. struct socket *sock;
  687. int flags;
  688. sock = file->private_data;
  689. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  690. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  691. flags |= more;
  692. return kernel_sendpage(sock, page, offset, size, flags);
  693. }
  694. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  695. struct pipe_inode_info *pipe, size_t len,
  696. unsigned int flags)
  697. {
  698. struct socket *sock = file->private_data;
  699. if (unlikely(!sock->ops->splice_read))
  700. return -EINVAL;
  701. sock_update_classid(sock->sk);
  702. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  703. }
  704. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  705. struct sock_iocb *siocb)
  706. {
  707. if (!is_sync_kiocb(iocb)) {
  708. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  709. if (!siocb)
  710. return NULL;
  711. iocb->ki_dtor = sock_aio_dtor;
  712. }
  713. siocb->kiocb = iocb;
  714. iocb->private = siocb;
  715. return siocb;
  716. }
  717. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  718. struct file *file, const struct iovec *iov,
  719. unsigned long nr_segs)
  720. {
  721. struct socket *sock = file->private_data;
  722. size_t size = 0;
  723. int i;
  724. for (i = 0; i < nr_segs; i++)
  725. size += iov[i].iov_len;
  726. msg->msg_name = NULL;
  727. msg->msg_namelen = 0;
  728. msg->msg_control = NULL;
  729. msg->msg_controllen = 0;
  730. msg->msg_iov = (struct iovec *)iov;
  731. msg->msg_iovlen = nr_segs;
  732. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  733. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  734. }
  735. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  736. unsigned long nr_segs, loff_t pos)
  737. {
  738. struct sock_iocb siocb, *x;
  739. if (pos != 0)
  740. return -ESPIPE;
  741. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  742. return 0;
  743. x = alloc_sock_iocb(iocb, &siocb);
  744. if (!x)
  745. return -ENOMEM;
  746. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  747. }
  748. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  749. struct file *file, const struct iovec *iov,
  750. unsigned long nr_segs)
  751. {
  752. struct socket *sock = file->private_data;
  753. size_t size = 0;
  754. int i;
  755. for (i = 0; i < nr_segs; i++)
  756. size += iov[i].iov_len;
  757. msg->msg_name = NULL;
  758. msg->msg_namelen = 0;
  759. msg->msg_control = NULL;
  760. msg->msg_controllen = 0;
  761. msg->msg_iov = (struct iovec *)iov;
  762. msg->msg_iovlen = nr_segs;
  763. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  764. if (sock->type == SOCK_SEQPACKET)
  765. msg->msg_flags |= MSG_EOR;
  766. return __sock_sendmsg(iocb, sock, msg, size);
  767. }
  768. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  769. unsigned long nr_segs, loff_t pos)
  770. {
  771. struct sock_iocb siocb, *x;
  772. if (pos != 0)
  773. return -ESPIPE;
  774. x = alloc_sock_iocb(iocb, &siocb);
  775. if (!x)
  776. return -ENOMEM;
  777. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  778. }
  779. /*
  780. * Atomic setting of ioctl hooks to avoid race
  781. * with module unload.
  782. */
  783. static DEFINE_MUTEX(br_ioctl_mutex);
  784. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  785. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  786. {
  787. mutex_lock(&br_ioctl_mutex);
  788. br_ioctl_hook = hook;
  789. mutex_unlock(&br_ioctl_mutex);
  790. }
  791. EXPORT_SYMBOL(brioctl_set);
  792. static DEFINE_MUTEX(vlan_ioctl_mutex);
  793. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  794. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  795. {
  796. mutex_lock(&vlan_ioctl_mutex);
  797. vlan_ioctl_hook = hook;
  798. mutex_unlock(&vlan_ioctl_mutex);
  799. }
  800. EXPORT_SYMBOL(vlan_ioctl_set);
  801. static DEFINE_MUTEX(dlci_ioctl_mutex);
  802. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  803. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  804. {
  805. mutex_lock(&dlci_ioctl_mutex);
  806. dlci_ioctl_hook = hook;
  807. mutex_unlock(&dlci_ioctl_mutex);
  808. }
  809. EXPORT_SYMBOL(dlci_ioctl_set);
  810. static long sock_do_ioctl(struct net *net, struct socket *sock,
  811. unsigned int cmd, unsigned long arg)
  812. {
  813. int err;
  814. void __user *argp = (void __user *)arg;
  815. err = sock->ops->ioctl(sock, cmd, arg);
  816. /*
  817. * If this ioctl is unknown try to hand it down
  818. * to the NIC driver.
  819. */
  820. if (err == -ENOIOCTLCMD)
  821. err = dev_ioctl(net, cmd, argp);
  822. return err;
  823. }
  824. /*
  825. * With an ioctl, arg may well be a user mode pointer, but we don't know
  826. * what to do with it - that's up to the protocol still.
  827. */
  828. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  829. {
  830. struct socket *sock;
  831. struct sock *sk;
  832. void __user *argp = (void __user *)arg;
  833. int pid, err;
  834. struct net *net;
  835. sock = file->private_data;
  836. sk = sock->sk;
  837. net = sock_net(sk);
  838. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  839. err = dev_ioctl(net, cmd, argp);
  840. } else
  841. #ifdef CONFIG_WEXT_CORE
  842. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  843. err = dev_ioctl(net, cmd, argp);
  844. } else
  845. #endif
  846. switch (cmd) {
  847. case FIOSETOWN:
  848. case SIOCSPGRP:
  849. err = -EFAULT;
  850. if (get_user(pid, (int __user *)argp))
  851. break;
  852. err = f_setown(sock->file, pid, 1);
  853. break;
  854. case FIOGETOWN:
  855. case SIOCGPGRP:
  856. err = put_user(f_getown(sock->file),
  857. (int __user *)argp);
  858. break;
  859. case SIOCGIFBR:
  860. case SIOCSIFBR:
  861. case SIOCBRADDBR:
  862. case SIOCBRDELBR:
  863. err = -ENOPKG;
  864. if (!br_ioctl_hook)
  865. request_module("bridge");
  866. mutex_lock(&br_ioctl_mutex);
  867. if (br_ioctl_hook)
  868. err = br_ioctl_hook(net, cmd, argp);
  869. mutex_unlock(&br_ioctl_mutex);
  870. break;
  871. case SIOCGIFVLAN:
  872. case SIOCSIFVLAN:
  873. err = -ENOPKG;
  874. if (!vlan_ioctl_hook)
  875. request_module("8021q");
  876. mutex_lock(&vlan_ioctl_mutex);
  877. if (vlan_ioctl_hook)
  878. err = vlan_ioctl_hook(net, argp);
  879. mutex_unlock(&vlan_ioctl_mutex);
  880. break;
  881. case SIOCADDDLCI:
  882. case SIOCDELDLCI:
  883. err = -ENOPKG;
  884. if (!dlci_ioctl_hook)
  885. request_module("dlci");
  886. mutex_lock(&dlci_ioctl_mutex);
  887. if (dlci_ioctl_hook)
  888. err = dlci_ioctl_hook(cmd, argp);
  889. mutex_unlock(&dlci_ioctl_mutex);
  890. break;
  891. default:
  892. err = sock_do_ioctl(net, sock, cmd, arg);
  893. break;
  894. }
  895. return err;
  896. }
  897. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  898. {
  899. int err;
  900. struct socket *sock = NULL;
  901. err = security_socket_create(family, type, protocol, 1);
  902. if (err)
  903. goto out;
  904. sock = sock_alloc();
  905. if (!sock) {
  906. err = -ENOMEM;
  907. goto out;
  908. }
  909. sock->type = type;
  910. err = security_socket_post_create(sock, family, type, protocol, 1);
  911. if (err)
  912. goto out_release;
  913. out:
  914. *res = sock;
  915. return err;
  916. out_release:
  917. sock_release(sock);
  918. sock = NULL;
  919. goto out;
  920. }
  921. EXPORT_SYMBOL(sock_create_lite);
  922. /* No kernel lock held - perfect */
  923. static unsigned int sock_poll(struct file *file, poll_table *wait)
  924. {
  925. struct socket *sock;
  926. /*
  927. * We can't return errors to poll, so it's either yes or no.
  928. */
  929. sock = file->private_data;
  930. return sock->ops->poll(file, sock, wait);
  931. }
  932. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  933. {
  934. struct socket *sock = file->private_data;
  935. return sock->ops->mmap(file, sock, vma);
  936. }
  937. static int sock_close(struct inode *inode, struct file *filp)
  938. {
  939. /*
  940. * It was possible the inode is NULL we were
  941. * closing an unfinished socket.
  942. */
  943. if (!inode) {
  944. printk(KERN_DEBUG "sock_close: NULL inode\n");
  945. return 0;
  946. }
  947. sock_release(SOCKET_I(inode));
  948. return 0;
  949. }
  950. /*
  951. * Update the socket async list
  952. *
  953. * Fasync_list locking strategy.
  954. *
  955. * 1. fasync_list is modified only under process context socket lock
  956. * i.e. under semaphore.
  957. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  958. * or under socket lock
  959. */
  960. static int sock_fasync(int fd, struct file *filp, int on)
  961. {
  962. struct socket *sock = filp->private_data;
  963. struct sock *sk = sock->sk;
  964. struct socket_wq *wq;
  965. if (sk == NULL)
  966. return -EINVAL;
  967. lock_sock(sk);
  968. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  969. fasync_helper(fd, filp, on, &wq->fasync_list);
  970. if (!wq->fasync_list)
  971. sock_reset_flag(sk, SOCK_FASYNC);
  972. else
  973. sock_set_flag(sk, SOCK_FASYNC);
  974. release_sock(sk);
  975. return 0;
  976. }
  977. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  978. int sock_wake_async(struct socket *sock, int how, int band)
  979. {
  980. struct socket_wq *wq;
  981. if (!sock)
  982. return -1;
  983. rcu_read_lock();
  984. wq = rcu_dereference(sock->wq);
  985. if (!wq || !wq->fasync_list) {
  986. rcu_read_unlock();
  987. return -1;
  988. }
  989. switch (how) {
  990. case SOCK_WAKE_WAITD:
  991. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  992. break;
  993. goto call_kill;
  994. case SOCK_WAKE_SPACE:
  995. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  996. break;
  997. /* fall through */
  998. case SOCK_WAKE_IO:
  999. call_kill:
  1000. kill_fasync(&wq->fasync_list, SIGIO, band);
  1001. break;
  1002. case SOCK_WAKE_URG:
  1003. kill_fasync(&wq->fasync_list, SIGURG, band);
  1004. }
  1005. rcu_read_unlock();
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(sock_wake_async);
  1009. int __sock_create(struct net *net, int family, int type, int protocol,
  1010. struct socket **res, int kern)
  1011. {
  1012. int err;
  1013. struct socket *sock;
  1014. const struct net_proto_family *pf;
  1015. /*
  1016. * Check protocol is in range
  1017. */
  1018. if (family < 0 || family >= NPROTO)
  1019. return -EAFNOSUPPORT;
  1020. if (type < 0 || type >= SOCK_MAX)
  1021. return -EINVAL;
  1022. /* Compatibility.
  1023. This uglymoron is moved from INET layer to here to avoid
  1024. deadlock in module load.
  1025. */
  1026. if (family == PF_INET && type == SOCK_PACKET) {
  1027. static int warned;
  1028. if (!warned) {
  1029. warned = 1;
  1030. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1031. current->comm);
  1032. }
  1033. family = PF_PACKET;
  1034. }
  1035. err = security_socket_create(family, type, protocol, kern);
  1036. if (err)
  1037. return err;
  1038. /*
  1039. * Allocate the socket and allow the family to set things up. if
  1040. * the protocol is 0, the family is instructed to select an appropriate
  1041. * default.
  1042. */
  1043. sock = sock_alloc();
  1044. if (!sock) {
  1045. if (net_ratelimit())
  1046. printk(KERN_WARNING "socket: no more sockets\n");
  1047. return -ENFILE; /* Not exactly a match, but its the
  1048. closest posix thing */
  1049. }
  1050. sock->type = type;
  1051. #ifdef CONFIG_MODULES
  1052. /* Attempt to load a protocol module if the find failed.
  1053. *
  1054. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1055. * requested real, full-featured networking support upon configuration.
  1056. * Otherwise module support will break!
  1057. */
  1058. if (rcu_access_pointer(net_families[family]) == NULL)
  1059. request_module("net-pf-%d", family);
  1060. #endif
  1061. rcu_read_lock();
  1062. pf = rcu_dereference(net_families[family]);
  1063. err = -EAFNOSUPPORT;
  1064. if (!pf)
  1065. goto out_release;
  1066. /*
  1067. * We will call the ->create function, that possibly is in a loadable
  1068. * module, so we have to bump that loadable module refcnt first.
  1069. */
  1070. if (!try_module_get(pf->owner))
  1071. goto out_release;
  1072. /* Now protected by module ref count */
  1073. rcu_read_unlock();
  1074. err = pf->create(net, sock, protocol, kern);
  1075. if (err < 0)
  1076. goto out_module_put;
  1077. /*
  1078. * Now to bump the refcnt of the [loadable] module that owns this
  1079. * socket at sock_release time we decrement its refcnt.
  1080. */
  1081. if (!try_module_get(sock->ops->owner))
  1082. goto out_module_busy;
  1083. /*
  1084. * Now that we're done with the ->create function, the [loadable]
  1085. * module can have its refcnt decremented
  1086. */
  1087. module_put(pf->owner);
  1088. err = security_socket_post_create(sock, family, type, protocol, kern);
  1089. if (err)
  1090. goto out_sock_release;
  1091. *res = sock;
  1092. return 0;
  1093. out_module_busy:
  1094. err = -EAFNOSUPPORT;
  1095. out_module_put:
  1096. sock->ops = NULL;
  1097. module_put(pf->owner);
  1098. out_sock_release:
  1099. sock_release(sock);
  1100. return err;
  1101. out_release:
  1102. rcu_read_unlock();
  1103. goto out_sock_release;
  1104. }
  1105. EXPORT_SYMBOL(__sock_create);
  1106. int sock_create(int family, int type, int protocol, struct socket **res)
  1107. {
  1108. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1109. }
  1110. EXPORT_SYMBOL(sock_create);
  1111. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1112. {
  1113. return __sock_create(&init_net, family, type, protocol, res, 1);
  1114. }
  1115. EXPORT_SYMBOL(sock_create_kern);
  1116. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1117. {
  1118. int retval;
  1119. struct socket *sock;
  1120. int flags;
  1121. /* Check the SOCK_* constants for consistency. */
  1122. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1123. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1124. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1125. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1126. flags = type & ~SOCK_TYPE_MASK;
  1127. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1128. return -EINVAL;
  1129. type &= SOCK_TYPE_MASK;
  1130. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1131. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1132. retval = sock_create(family, type, protocol, &sock);
  1133. if (retval < 0)
  1134. goto out;
  1135. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1136. if (retval < 0)
  1137. goto out_release;
  1138. out:
  1139. /* It may be already another descriptor 8) Not kernel problem. */
  1140. return retval;
  1141. out_release:
  1142. sock_release(sock);
  1143. return retval;
  1144. }
  1145. /*
  1146. * Create a pair of connected sockets.
  1147. */
  1148. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1149. int __user *, usockvec)
  1150. {
  1151. struct socket *sock1, *sock2;
  1152. int fd1, fd2, err;
  1153. struct file *newfile1, *newfile2;
  1154. int flags;
  1155. flags = type & ~SOCK_TYPE_MASK;
  1156. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1157. return -EINVAL;
  1158. type &= SOCK_TYPE_MASK;
  1159. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1160. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1161. /*
  1162. * Obtain the first socket and check if the underlying protocol
  1163. * supports the socketpair call.
  1164. */
  1165. err = sock_create(family, type, protocol, &sock1);
  1166. if (err < 0)
  1167. goto out;
  1168. err = sock_create(family, type, protocol, &sock2);
  1169. if (err < 0)
  1170. goto out_release_1;
  1171. err = sock1->ops->socketpair(sock1, sock2);
  1172. if (err < 0)
  1173. goto out_release_both;
  1174. fd1 = sock_alloc_file(sock1, &newfile1, flags);
  1175. if (unlikely(fd1 < 0)) {
  1176. err = fd1;
  1177. goto out_release_both;
  1178. }
  1179. fd2 = sock_alloc_file(sock2, &newfile2, flags);
  1180. if (unlikely(fd2 < 0)) {
  1181. err = fd2;
  1182. fput(newfile1);
  1183. put_unused_fd(fd1);
  1184. sock_release(sock2);
  1185. goto out;
  1186. }
  1187. audit_fd_pair(fd1, fd2);
  1188. fd_install(fd1, newfile1);
  1189. fd_install(fd2, newfile2);
  1190. /* fd1 and fd2 may be already another descriptors.
  1191. * Not kernel problem.
  1192. */
  1193. err = put_user(fd1, &usockvec[0]);
  1194. if (!err)
  1195. err = put_user(fd2, &usockvec[1]);
  1196. if (!err)
  1197. return 0;
  1198. sys_close(fd2);
  1199. sys_close(fd1);
  1200. return err;
  1201. out_release_both:
  1202. sock_release(sock2);
  1203. out_release_1:
  1204. sock_release(sock1);
  1205. out:
  1206. return err;
  1207. }
  1208. /*
  1209. * Bind a name to a socket. Nothing much to do here since it's
  1210. * the protocol's responsibility to handle the local address.
  1211. *
  1212. * We move the socket address to kernel space before we call
  1213. * the protocol layer (having also checked the address is ok).
  1214. */
  1215. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1216. {
  1217. struct socket *sock;
  1218. struct sockaddr_storage address;
  1219. int err, fput_needed;
  1220. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1221. if (sock) {
  1222. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1223. if (err >= 0) {
  1224. err = security_socket_bind(sock,
  1225. (struct sockaddr *)&address,
  1226. addrlen);
  1227. if (!err)
  1228. err = sock->ops->bind(sock,
  1229. (struct sockaddr *)
  1230. &address, addrlen);
  1231. }
  1232. fput_light(sock->file, fput_needed);
  1233. }
  1234. return err;
  1235. }
  1236. /*
  1237. * Perform a listen. Basically, we allow the protocol to do anything
  1238. * necessary for a listen, and if that works, we mark the socket as
  1239. * ready for listening.
  1240. */
  1241. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1242. {
  1243. struct socket *sock;
  1244. int err, fput_needed;
  1245. int somaxconn;
  1246. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1247. if (sock) {
  1248. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1249. if ((unsigned)backlog > somaxconn)
  1250. backlog = somaxconn;
  1251. err = security_socket_listen(sock, backlog);
  1252. if (!err)
  1253. err = sock->ops->listen(sock, backlog);
  1254. fput_light(sock->file, fput_needed);
  1255. }
  1256. return err;
  1257. }
  1258. /*
  1259. * For accept, we attempt to create a new socket, set up the link
  1260. * with the client, wake up the client, then return the new
  1261. * connected fd. We collect the address of the connector in kernel
  1262. * space and move it to user at the very end. This is unclean because
  1263. * we open the socket then return an error.
  1264. *
  1265. * 1003.1g adds the ability to recvmsg() to query connection pending
  1266. * status to recvmsg. We need to add that support in a way thats
  1267. * clean when we restucture accept also.
  1268. */
  1269. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1270. int __user *, upeer_addrlen, int, flags)
  1271. {
  1272. struct socket *sock, *newsock;
  1273. struct file *newfile;
  1274. int err, len, newfd, fput_needed;
  1275. struct sockaddr_storage address;
  1276. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1277. return -EINVAL;
  1278. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1279. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1280. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1281. if (!sock)
  1282. goto out;
  1283. err = -ENFILE;
  1284. newsock = sock_alloc();
  1285. if (!newsock)
  1286. goto out_put;
  1287. newsock->type = sock->type;
  1288. newsock->ops = sock->ops;
  1289. /*
  1290. * We don't need try_module_get here, as the listening socket (sock)
  1291. * has the protocol module (sock->ops->owner) held.
  1292. */
  1293. __module_get(newsock->ops->owner);
  1294. newfd = sock_alloc_file(newsock, &newfile, flags);
  1295. if (unlikely(newfd < 0)) {
  1296. err = newfd;
  1297. sock_release(newsock);
  1298. goto out_put;
  1299. }
  1300. err = security_socket_accept(sock, newsock);
  1301. if (err)
  1302. goto out_fd;
  1303. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1304. if (err < 0)
  1305. goto out_fd;
  1306. if (upeer_sockaddr) {
  1307. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1308. &len, 2) < 0) {
  1309. err = -ECONNABORTED;
  1310. goto out_fd;
  1311. }
  1312. err = move_addr_to_user((struct sockaddr *)&address,
  1313. len, upeer_sockaddr, upeer_addrlen);
  1314. if (err < 0)
  1315. goto out_fd;
  1316. }
  1317. /* File flags are not inherited via accept() unlike another OSes. */
  1318. fd_install(newfd, newfile);
  1319. err = newfd;
  1320. out_put:
  1321. fput_light(sock->file, fput_needed);
  1322. out:
  1323. return err;
  1324. out_fd:
  1325. fput(newfile);
  1326. put_unused_fd(newfd);
  1327. goto out_put;
  1328. }
  1329. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1330. int __user *, upeer_addrlen)
  1331. {
  1332. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1333. }
  1334. /*
  1335. * Attempt to connect to a socket with the server address. The address
  1336. * is in user space so we verify it is OK and move it to kernel space.
  1337. *
  1338. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1339. * break bindings
  1340. *
  1341. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1342. * other SEQPACKET protocols that take time to connect() as it doesn't
  1343. * include the -EINPROGRESS status for such sockets.
  1344. */
  1345. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1346. int, addrlen)
  1347. {
  1348. struct socket *sock;
  1349. struct sockaddr_storage address;
  1350. int err, fput_needed;
  1351. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1352. if (!sock)
  1353. goto out;
  1354. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1355. if (err < 0)
  1356. goto out_put;
  1357. err =
  1358. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1359. if (err)
  1360. goto out_put;
  1361. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1362. sock->file->f_flags);
  1363. out_put:
  1364. fput_light(sock->file, fput_needed);
  1365. out:
  1366. return err;
  1367. }
  1368. /*
  1369. * Get the local address ('name') of a socket object. Move the obtained
  1370. * name to user space.
  1371. */
  1372. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1373. int __user *, usockaddr_len)
  1374. {
  1375. struct socket *sock;
  1376. struct sockaddr_storage address;
  1377. int len, err, fput_needed;
  1378. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1379. if (!sock)
  1380. goto out;
  1381. err = security_socket_getsockname(sock);
  1382. if (err)
  1383. goto out_put;
  1384. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1385. if (err)
  1386. goto out_put;
  1387. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1388. out_put:
  1389. fput_light(sock->file, fput_needed);
  1390. out:
  1391. return err;
  1392. }
  1393. /*
  1394. * Get the remote address ('name') of a socket object. Move the obtained
  1395. * name to user space.
  1396. */
  1397. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1398. int __user *, usockaddr_len)
  1399. {
  1400. struct socket *sock;
  1401. struct sockaddr_storage address;
  1402. int len, err, fput_needed;
  1403. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1404. if (sock != NULL) {
  1405. err = security_socket_getpeername(sock);
  1406. if (err) {
  1407. fput_light(sock->file, fput_needed);
  1408. return err;
  1409. }
  1410. err =
  1411. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1412. 1);
  1413. if (!err)
  1414. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1415. usockaddr_len);
  1416. fput_light(sock->file, fput_needed);
  1417. }
  1418. return err;
  1419. }
  1420. /*
  1421. * Send a datagram to a given address. We move the address into kernel
  1422. * space and check the user space data area is readable before invoking
  1423. * the protocol.
  1424. */
  1425. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1426. unsigned, flags, struct sockaddr __user *, addr,
  1427. int, addr_len)
  1428. {
  1429. struct socket *sock;
  1430. struct sockaddr_storage address;
  1431. int err;
  1432. struct msghdr msg;
  1433. struct iovec iov;
  1434. int fput_needed;
  1435. if (len > INT_MAX)
  1436. len = INT_MAX;
  1437. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1438. if (!sock)
  1439. goto out;
  1440. iov.iov_base = buff;
  1441. iov.iov_len = len;
  1442. msg.msg_name = NULL;
  1443. msg.msg_iov = &iov;
  1444. msg.msg_iovlen = 1;
  1445. msg.msg_control = NULL;
  1446. msg.msg_controllen = 0;
  1447. msg.msg_namelen = 0;
  1448. if (addr) {
  1449. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1450. if (err < 0)
  1451. goto out_put;
  1452. msg.msg_name = (struct sockaddr *)&address;
  1453. msg.msg_namelen = addr_len;
  1454. }
  1455. if (sock->file->f_flags & O_NONBLOCK)
  1456. flags |= MSG_DONTWAIT;
  1457. msg.msg_flags = flags;
  1458. err = sock_sendmsg(sock, &msg, len);
  1459. out_put:
  1460. fput_light(sock->file, fput_needed);
  1461. out:
  1462. return err;
  1463. }
  1464. /*
  1465. * Send a datagram down a socket.
  1466. */
  1467. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1468. unsigned, flags)
  1469. {
  1470. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1471. }
  1472. /*
  1473. * Receive a frame from the socket and optionally record the address of the
  1474. * sender. We verify the buffers are writable and if needed move the
  1475. * sender address from kernel to user space.
  1476. */
  1477. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1478. unsigned, flags, struct sockaddr __user *, addr,
  1479. int __user *, addr_len)
  1480. {
  1481. struct socket *sock;
  1482. struct iovec iov;
  1483. struct msghdr msg;
  1484. struct sockaddr_storage address;
  1485. int err, err2;
  1486. int fput_needed;
  1487. if (size > INT_MAX)
  1488. size = INT_MAX;
  1489. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1490. if (!sock)
  1491. goto out;
  1492. msg.msg_control = NULL;
  1493. msg.msg_controllen = 0;
  1494. msg.msg_iovlen = 1;
  1495. msg.msg_iov = &iov;
  1496. iov.iov_len = size;
  1497. iov.iov_base = ubuf;
  1498. msg.msg_name = (struct sockaddr *)&address;
  1499. msg.msg_namelen = sizeof(address);
  1500. if (sock->file->f_flags & O_NONBLOCK)
  1501. flags |= MSG_DONTWAIT;
  1502. err = sock_recvmsg(sock, &msg, size, flags);
  1503. if (err >= 0 && addr != NULL) {
  1504. err2 = move_addr_to_user((struct sockaddr *)&address,
  1505. msg.msg_namelen, addr, addr_len);
  1506. if (err2 < 0)
  1507. err = err2;
  1508. }
  1509. fput_light(sock->file, fput_needed);
  1510. out:
  1511. return err;
  1512. }
  1513. /*
  1514. * Receive a datagram from a socket.
  1515. */
  1516. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1517. unsigned flags)
  1518. {
  1519. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1520. }
  1521. /*
  1522. * Set a socket option. Because we don't know the option lengths we have
  1523. * to pass the user mode parameter for the protocols to sort out.
  1524. */
  1525. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1526. char __user *, optval, int, optlen)
  1527. {
  1528. int err, fput_needed;
  1529. struct socket *sock;
  1530. if (optlen < 0)
  1531. return -EINVAL;
  1532. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1533. if (sock != NULL) {
  1534. err = security_socket_setsockopt(sock, level, optname);
  1535. if (err)
  1536. goto out_put;
  1537. if (level == SOL_SOCKET)
  1538. err =
  1539. sock_setsockopt(sock, level, optname, optval,
  1540. optlen);
  1541. else
  1542. err =
  1543. sock->ops->setsockopt(sock, level, optname, optval,
  1544. optlen);
  1545. out_put:
  1546. fput_light(sock->file, fput_needed);
  1547. }
  1548. return err;
  1549. }
  1550. /*
  1551. * Get a socket option. Because we don't know the option lengths we have
  1552. * to pass a user mode parameter for the protocols to sort out.
  1553. */
  1554. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1555. char __user *, optval, int __user *, optlen)
  1556. {
  1557. int err, fput_needed;
  1558. struct socket *sock;
  1559. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1560. if (sock != NULL) {
  1561. err = security_socket_getsockopt(sock, level, optname);
  1562. if (err)
  1563. goto out_put;
  1564. if (level == SOL_SOCKET)
  1565. err =
  1566. sock_getsockopt(sock, level, optname, optval,
  1567. optlen);
  1568. else
  1569. err =
  1570. sock->ops->getsockopt(sock, level, optname, optval,
  1571. optlen);
  1572. out_put:
  1573. fput_light(sock->file, fput_needed);
  1574. }
  1575. return err;
  1576. }
  1577. /*
  1578. * Shutdown a socket.
  1579. */
  1580. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1581. {
  1582. int err, fput_needed;
  1583. struct socket *sock;
  1584. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1585. if (sock != NULL) {
  1586. err = security_socket_shutdown(sock, how);
  1587. if (!err)
  1588. err = sock->ops->shutdown(sock, how);
  1589. fput_light(sock->file, fput_needed);
  1590. }
  1591. return err;
  1592. }
  1593. /* A couple of helpful macros for getting the address of the 32/64 bit
  1594. * fields which are the same type (int / unsigned) on our platforms.
  1595. */
  1596. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1597. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1598. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1599. struct used_address {
  1600. struct sockaddr_storage name;
  1601. unsigned int name_len;
  1602. };
  1603. static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1604. struct msghdr *msg_sys, unsigned flags,
  1605. struct used_address *used_address)
  1606. {
  1607. struct compat_msghdr __user *msg_compat =
  1608. (struct compat_msghdr __user *)msg;
  1609. struct sockaddr_storage address;
  1610. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1611. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1612. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1613. /* 20 is size of ipv6_pktinfo */
  1614. unsigned char *ctl_buf = ctl;
  1615. int err, ctl_len, iov_size, total_len;
  1616. err = -EFAULT;
  1617. if (MSG_CMSG_COMPAT & flags) {
  1618. if (get_compat_msghdr(msg_sys, msg_compat))
  1619. return -EFAULT;
  1620. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1621. return -EFAULT;
  1622. /* do not move before msg_sys is valid */
  1623. err = -EMSGSIZE;
  1624. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1625. goto out;
  1626. /* Check whether to allocate the iovec area */
  1627. err = -ENOMEM;
  1628. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1629. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1630. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1631. if (!iov)
  1632. goto out;
  1633. }
  1634. /* This will also move the address data into kernel space */
  1635. if (MSG_CMSG_COMPAT & flags) {
  1636. err = verify_compat_iovec(msg_sys, iov,
  1637. (struct sockaddr *)&address,
  1638. VERIFY_READ);
  1639. } else
  1640. err = verify_iovec(msg_sys, iov,
  1641. (struct sockaddr *)&address,
  1642. VERIFY_READ);
  1643. if (err < 0)
  1644. goto out_freeiov;
  1645. total_len = err;
  1646. err = -ENOBUFS;
  1647. if (msg_sys->msg_controllen > INT_MAX)
  1648. goto out_freeiov;
  1649. ctl_len = msg_sys->msg_controllen;
  1650. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1651. err =
  1652. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1653. sizeof(ctl));
  1654. if (err)
  1655. goto out_freeiov;
  1656. ctl_buf = msg_sys->msg_control;
  1657. ctl_len = msg_sys->msg_controllen;
  1658. } else if (ctl_len) {
  1659. if (ctl_len > sizeof(ctl)) {
  1660. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1661. if (ctl_buf == NULL)
  1662. goto out_freeiov;
  1663. }
  1664. err = -EFAULT;
  1665. /*
  1666. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1667. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1668. * checking falls down on this.
  1669. */
  1670. if (copy_from_user(ctl_buf,
  1671. (void __user __force *)msg_sys->msg_control,
  1672. ctl_len))
  1673. goto out_freectl;
  1674. msg_sys->msg_control = ctl_buf;
  1675. }
  1676. msg_sys->msg_flags = flags;
  1677. if (sock->file->f_flags & O_NONBLOCK)
  1678. msg_sys->msg_flags |= MSG_DONTWAIT;
  1679. /*
  1680. * If this is sendmmsg() and current destination address is same as
  1681. * previously succeeded address, omit asking LSM's decision.
  1682. * used_address->name_len is initialized to UINT_MAX so that the first
  1683. * destination address never matches.
  1684. */
  1685. if (used_address && msg_sys->msg_name &&
  1686. used_address->name_len == msg_sys->msg_namelen &&
  1687. !memcmp(&used_address->name, msg_sys->msg_name,
  1688. used_address->name_len)) {
  1689. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1690. goto out_freectl;
  1691. }
  1692. err = sock_sendmsg(sock, msg_sys, total_len);
  1693. /*
  1694. * If this is sendmmsg() and sending to current destination address was
  1695. * successful, remember it.
  1696. */
  1697. if (used_address && err >= 0) {
  1698. used_address->name_len = msg_sys->msg_namelen;
  1699. if (msg_sys->msg_name)
  1700. memcpy(&used_address->name, msg_sys->msg_name,
  1701. used_address->name_len);
  1702. }
  1703. out_freectl:
  1704. if (ctl_buf != ctl)
  1705. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1706. out_freeiov:
  1707. if (iov != iovstack)
  1708. sock_kfree_s(sock->sk, iov, iov_size);
  1709. out:
  1710. return err;
  1711. }
  1712. /*
  1713. * BSD sendmsg interface
  1714. */
  1715. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
  1716. {
  1717. int fput_needed, err;
  1718. struct msghdr msg_sys;
  1719. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1720. if (!sock)
  1721. goto out;
  1722. err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1723. fput_light(sock->file, fput_needed);
  1724. out:
  1725. return err;
  1726. }
  1727. /*
  1728. * Linux sendmmsg interface
  1729. */
  1730. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1731. unsigned int flags)
  1732. {
  1733. int fput_needed, err, datagrams;
  1734. struct socket *sock;
  1735. struct mmsghdr __user *entry;
  1736. struct compat_mmsghdr __user *compat_entry;
  1737. struct msghdr msg_sys;
  1738. struct used_address used_address;
  1739. if (vlen > UIO_MAXIOV)
  1740. vlen = UIO_MAXIOV;
  1741. datagrams = 0;
  1742. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1743. if (!sock)
  1744. return err;
  1745. used_address.name_len = UINT_MAX;
  1746. entry = mmsg;
  1747. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1748. err = 0;
  1749. while (datagrams < vlen) {
  1750. if (MSG_CMSG_COMPAT & flags) {
  1751. err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1752. &msg_sys, flags, &used_address);
  1753. if (err < 0)
  1754. break;
  1755. err = __put_user(err, &compat_entry->msg_len);
  1756. ++compat_entry;
  1757. } else {
  1758. err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
  1759. &msg_sys, flags, &used_address);
  1760. if (err < 0)
  1761. break;
  1762. err = put_user(err, &entry->msg_len);
  1763. ++entry;
  1764. }
  1765. if (err)
  1766. break;
  1767. ++datagrams;
  1768. }
  1769. fput_light(sock->file, fput_needed);
  1770. /* We only return an error if no datagrams were able to be sent */
  1771. if (datagrams != 0)
  1772. return datagrams;
  1773. return err;
  1774. }
  1775. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1776. unsigned int, vlen, unsigned int, flags)
  1777. {
  1778. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1779. }
  1780. static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1781. struct msghdr *msg_sys, unsigned flags, int nosec)
  1782. {
  1783. struct compat_msghdr __user *msg_compat =
  1784. (struct compat_msghdr __user *)msg;
  1785. struct iovec iovstack[UIO_FASTIOV];
  1786. struct iovec *iov = iovstack;
  1787. unsigned long cmsg_ptr;
  1788. int err, iov_size, total_len, len;
  1789. /* kernel mode address */
  1790. struct sockaddr_storage addr;
  1791. /* user mode address pointers */
  1792. struct sockaddr __user *uaddr;
  1793. int __user *uaddr_len;
  1794. if (MSG_CMSG_COMPAT & flags) {
  1795. if (get_compat_msghdr(msg_sys, msg_compat))
  1796. return -EFAULT;
  1797. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1798. return -EFAULT;
  1799. err = -EMSGSIZE;
  1800. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1801. goto out;
  1802. /* Check whether to allocate the iovec area */
  1803. err = -ENOMEM;
  1804. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1805. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1806. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1807. if (!iov)
  1808. goto out;
  1809. }
  1810. /*
  1811. * Save the user-mode address (verify_iovec will change the
  1812. * kernel msghdr to use the kernel address space)
  1813. */
  1814. uaddr = (__force void __user *)msg_sys->msg_name;
  1815. uaddr_len = COMPAT_NAMELEN(msg);
  1816. if (MSG_CMSG_COMPAT & flags) {
  1817. err = verify_compat_iovec(msg_sys, iov,
  1818. (struct sockaddr *)&addr,
  1819. VERIFY_WRITE);
  1820. } else
  1821. err = verify_iovec(msg_sys, iov,
  1822. (struct sockaddr *)&addr,
  1823. VERIFY_WRITE);
  1824. if (err < 0)
  1825. goto out_freeiov;
  1826. total_len = err;
  1827. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1828. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1829. if (sock->file->f_flags & O_NONBLOCK)
  1830. flags |= MSG_DONTWAIT;
  1831. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1832. total_len, flags);
  1833. if (err < 0)
  1834. goto out_freeiov;
  1835. len = err;
  1836. if (uaddr != NULL) {
  1837. err = move_addr_to_user((struct sockaddr *)&addr,
  1838. msg_sys->msg_namelen, uaddr,
  1839. uaddr_len);
  1840. if (err < 0)
  1841. goto out_freeiov;
  1842. }
  1843. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1844. COMPAT_FLAGS(msg));
  1845. if (err)
  1846. goto out_freeiov;
  1847. if (MSG_CMSG_COMPAT & flags)
  1848. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1849. &msg_compat->msg_controllen);
  1850. else
  1851. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1852. &msg->msg_controllen);
  1853. if (err)
  1854. goto out_freeiov;
  1855. err = len;
  1856. out_freeiov:
  1857. if (iov != iovstack)
  1858. sock_kfree_s(sock->sk, iov, iov_size);
  1859. out:
  1860. return err;
  1861. }
  1862. /*
  1863. * BSD recvmsg interface
  1864. */
  1865. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1866. unsigned int, flags)
  1867. {
  1868. int fput_needed, err;
  1869. struct msghdr msg_sys;
  1870. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1871. if (!sock)
  1872. goto out;
  1873. err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1874. fput_light(sock->file, fput_needed);
  1875. out:
  1876. return err;
  1877. }
  1878. /*
  1879. * Linux recvmmsg interface
  1880. */
  1881. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1882. unsigned int flags, struct timespec *timeout)
  1883. {
  1884. int fput_needed, err, datagrams;
  1885. struct socket *sock;
  1886. struct mmsghdr __user *entry;
  1887. struct compat_mmsghdr __user *compat_entry;
  1888. struct msghdr msg_sys;
  1889. struct timespec end_time;
  1890. if (timeout &&
  1891. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1892. timeout->tv_nsec))
  1893. return -EINVAL;
  1894. datagrams = 0;
  1895. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1896. if (!sock)
  1897. return err;
  1898. err = sock_error(sock->sk);
  1899. if (err)
  1900. goto out_put;
  1901. entry = mmsg;
  1902. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1903. while (datagrams < vlen) {
  1904. /*
  1905. * No need to ask LSM for more than the first datagram.
  1906. */
  1907. if (MSG_CMSG_COMPAT & flags) {
  1908. err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  1909. &msg_sys, flags & ~MSG_WAITFORONE,
  1910. datagrams);
  1911. if (err < 0)
  1912. break;
  1913. err = __put_user(err, &compat_entry->msg_len);
  1914. ++compat_entry;
  1915. } else {
  1916. err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
  1917. &msg_sys, flags & ~MSG_WAITFORONE,
  1918. datagrams);
  1919. if (err < 0)
  1920. break;
  1921. err = put_user(err, &entry->msg_len);
  1922. ++entry;
  1923. }
  1924. if (err)
  1925. break;
  1926. ++datagrams;
  1927. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1928. if (flags & MSG_WAITFORONE)
  1929. flags |= MSG_DONTWAIT;
  1930. if (timeout) {
  1931. ktime_get_ts(timeout);
  1932. *timeout = timespec_sub(end_time, *timeout);
  1933. if (timeout->tv_sec < 0) {
  1934. timeout->tv_sec = timeout->tv_nsec = 0;
  1935. break;
  1936. }
  1937. /* Timeout, return less than vlen datagrams */
  1938. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1939. break;
  1940. }
  1941. /* Out of band data, return right away */
  1942. if (msg_sys.msg_flags & MSG_OOB)
  1943. break;
  1944. }
  1945. out_put:
  1946. fput_light(sock->file, fput_needed);
  1947. if (err == 0)
  1948. return datagrams;
  1949. if (datagrams != 0) {
  1950. /*
  1951. * We may return less entries than requested (vlen) if the
  1952. * sock is non block and there aren't enough datagrams...
  1953. */
  1954. if (err != -EAGAIN) {
  1955. /*
  1956. * ... or if recvmsg returns an error after we
  1957. * received some datagrams, where we record the
  1958. * error to return on the next call or if the
  1959. * app asks about it using getsockopt(SO_ERROR).
  1960. */
  1961. sock->sk->sk_err = -err;
  1962. }
  1963. return datagrams;
  1964. }
  1965. return err;
  1966. }
  1967. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1968. unsigned int, vlen, unsigned int, flags,
  1969. struct timespec __user *, timeout)
  1970. {
  1971. int datagrams;
  1972. struct timespec timeout_sys;
  1973. if (!timeout)
  1974. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1975. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1976. return -EFAULT;
  1977. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1978. if (datagrams > 0 &&
  1979. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1980. datagrams = -EFAULT;
  1981. return datagrams;
  1982. }
  1983. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1984. /* Argument list sizes for sys_socketcall */
  1985. #define AL(x) ((x) * sizeof(unsigned long))
  1986. static const unsigned char nargs[21] = {
  1987. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1988. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1989. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1990. AL(4), AL(5), AL(4)
  1991. };
  1992. #undef AL
  1993. /*
  1994. * System call vectors.
  1995. *
  1996. * Argument checking cleaned up. Saved 20% in size.
  1997. * This function doesn't need to set the kernel lock because
  1998. * it is set by the callees.
  1999. */
  2000. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2001. {
  2002. unsigned long a[6];
  2003. unsigned long a0, a1;
  2004. int err;
  2005. unsigned int len;
  2006. if (call < 1 || call > SYS_SENDMMSG)
  2007. return -EINVAL;
  2008. len = nargs[call];
  2009. if (len > sizeof(a))
  2010. return -EINVAL;
  2011. /* copy_from_user should be SMP safe. */
  2012. if (copy_from_user(a, args, len))
  2013. return -EFAULT;
  2014. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2015. a0 = a[0];
  2016. a1 = a[1];
  2017. switch (call) {
  2018. case SYS_SOCKET:
  2019. err = sys_socket(a0, a1, a[2]);
  2020. break;
  2021. case SYS_BIND:
  2022. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2023. break;
  2024. case SYS_CONNECT:
  2025. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2026. break;
  2027. case SYS_LISTEN:
  2028. err = sys_listen(a0, a1);
  2029. break;
  2030. case SYS_ACCEPT:
  2031. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2032. (int __user *)a[2], 0);
  2033. break;
  2034. case SYS_GETSOCKNAME:
  2035. err =
  2036. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2037. (int __user *)a[2]);
  2038. break;
  2039. case SYS_GETPEERNAME:
  2040. err =
  2041. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2042. (int __user *)a[2]);
  2043. break;
  2044. case SYS_SOCKETPAIR:
  2045. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2046. break;
  2047. case SYS_SEND:
  2048. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2049. break;
  2050. case SYS_SENDTO:
  2051. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2052. (struct sockaddr __user *)a[4], a[5]);
  2053. break;
  2054. case SYS_RECV:
  2055. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2056. break;
  2057. case SYS_RECVFROM:
  2058. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2059. (struct sockaddr __user *)a[4],
  2060. (int __user *)a[5]);
  2061. break;
  2062. case SYS_SHUTDOWN:
  2063. err = sys_shutdown(a0, a1);
  2064. break;
  2065. case SYS_SETSOCKOPT:
  2066. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2067. break;
  2068. case SYS_GETSOCKOPT:
  2069. err =
  2070. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2071. (int __user *)a[4]);
  2072. break;
  2073. case SYS_SENDMSG:
  2074. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2075. break;
  2076. case SYS_SENDMMSG:
  2077. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2078. break;
  2079. case SYS_RECVMSG:
  2080. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2081. break;
  2082. case SYS_RECVMMSG:
  2083. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2084. (struct timespec __user *)a[4]);
  2085. break;
  2086. case SYS_ACCEPT4:
  2087. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2088. (int __user *)a[2], a[3]);
  2089. break;
  2090. default:
  2091. err = -EINVAL;
  2092. break;
  2093. }
  2094. return err;
  2095. }
  2096. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2097. /**
  2098. * sock_register - add a socket protocol handler
  2099. * @ops: description of protocol
  2100. *
  2101. * This function is called by a protocol handler that wants to
  2102. * advertise its address family, and have it linked into the
  2103. * socket interface. The value ops->family coresponds to the
  2104. * socket system call protocol family.
  2105. */
  2106. int sock_register(const struct net_proto_family *ops)
  2107. {
  2108. int err;
  2109. if (ops->family >= NPROTO) {
  2110. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2111. NPROTO);
  2112. return -ENOBUFS;
  2113. }
  2114. spin_lock(&net_family_lock);
  2115. if (rcu_dereference_protected(net_families[ops->family],
  2116. lockdep_is_held(&net_family_lock)))
  2117. err = -EEXIST;
  2118. else {
  2119. rcu_assign_pointer(net_families[ops->family], ops);
  2120. err = 0;
  2121. }
  2122. spin_unlock(&net_family_lock);
  2123. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2124. return err;
  2125. }
  2126. EXPORT_SYMBOL(sock_register);
  2127. /**
  2128. * sock_unregister - remove a protocol handler
  2129. * @family: protocol family to remove
  2130. *
  2131. * This function is called by a protocol handler that wants to
  2132. * remove its address family, and have it unlinked from the
  2133. * new socket creation.
  2134. *
  2135. * If protocol handler is a module, then it can use module reference
  2136. * counts to protect against new references. If protocol handler is not
  2137. * a module then it needs to provide its own protection in
  2138. * the ops->create routine.
  2139. */
  2140. void sock_unregister(int family)
  2141. {
  2142. BUG_ON(family < 0 || family >= NPROTO);
  2143. spin_lock(&net_family_lock);
  2144. rcu_assign_pointer(net_families[family], NULL);
  2145. spin_unlock(&net_family_lock);
  2146. synchronize_rcu();
  2147. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2148. }
  2149. EXPORT_SYMBOL(sock_unregister);
  2150. static int __init sock_init(void)
  2151. {
  2152. int err;
  2153. /*
  2154. * Initialize sock SLAB cache.
  2155. */
  2156. sk_init();
  2157. /*
  2158. * Initialize skbuff SLAB cache
  2159. */
  2160. skb_init();
  2161. /*
  2162. * Initialize the protocols module.
  2163. */
  2164. init_inodecache();
  2165. err = register_filesystem(&sock_fs_type);
  2166. if (err)
  2167. goto out_fs;
  2168. sock_mnt = kern_mount(&sock_fs_type);
  2169. if (IS_ERR(sock_mnt)) {
  2170. err = PTR_ERR(sock_mnt);
  2171. goto out_mount;
  2172. }
  2173. /* The real protocol initialization is performed in later initcalls.
  2174. */
  2175. #ifdef CONFIG_NETFILTER
  2176. netfilter_init();
  2177. #endif
  2178. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2179. skb_timestamping_init();
  2180. #endif
  2181. out:
  2182. return err;
  2183. out_mount:
  2184. unregister_filesystem(&sock_fs_type);
  2185. out_fs:
  2186. goto out;
  2187. }
  2188. core_initcall(sock_init); /* early initcall */
  2189. #ifdef CONFIG_PROC_FS
  2190. void socket_seq_show(struct seq_file *seq)
  2191. {
  2192. int cpu;
  2193. int counter = 0;
  2194. for_each_possible_cpu(cpu)
  2195. counter += per_cpu(sockets_in_use, cpu);
  2196. /* It can be negative, by the way. 8) */
  2197. if (counter < 0)
  2198. counter = 0;
  2199. seq_printf(seq, "sockets: used %d\n", counter);
  2200. }
  2201. #endif /* CONFIG_PROC_FS */
  2202. #ifdef CONFIG_COMPAT
  2203. static int do_siocgstamp(struct net *net, struct socket *sock,
  2204. unsigned int cmd, struct compat_timeval __user *up)
  2205. {
  2206. mm_segment_t old_fs = get_fs();
  2207. struct timeval ktv;
  2208. int err;
  2209. set_fs(KERNEL_DS);
  2210. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2211. set_fs(old_fs);
  2212. if (!err) {
  2213. err = put_user(ktv.tv_sec, &up->tv_sec);
  2214. err |= __put_user(ktv.tv_usec, &up->tv_usec);
  2215. }
  2216. return err;
  2217. }
  2218. static int do_siocgstampns(struct net *net, struct socket *sock,
  2219. unsigned int cmd, struct compat_timespec __user *up)
  2220. {
  2221. mm_segment_t old_fs = get_fs();
  2222. struct timespec kts;
  2223. int err;
  2224. set_fs(KERNEL_DS);
  2225. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2226. set_fs(old_fs);
  2227. if (!err) {
  2228. err = put_user(kts.tv_sec, &up->tv_sec);
  2229. err |= __put_user(kts.tv_nsec, &up->tv_nsec);
  2230. }
  2231. return err;
  2232. }
  2233. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2234. {
  2235. struct ifreq __user *uifr;
  2236. int err;
  2237. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2238. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2239. return -EFAULT;
  2240. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2241. if (err)
  2242. return err;
  2243. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2244. return -EFAULT;
  2245. return 0;
  2246. }
  2247. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2248. {
  2249. struct compat_ifconf ifc32;
  2250. struct ifconf ifc;
  2251. struct ifconf __user *uifc;
  2252. struct compat_ifreq __user *ifr32;
  2253. struct ifreq __user *ifr;
  2254. unsigned int i, j;
  2255. int err;
  2256. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2257. return -EFAULT;
  2258. if (ifc32.ifcbuf == 0) {
  2259. ifc32.ifc_len = 0;
  2260. ifc.ifc_len = 0;
  2261. ifc.ifc_req = NULL;
  2262. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2263. } else {
  2264. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2265. sizeof(struct ifreq);
  2266. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2267. ifc.ifc_len = len;
  2268. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2269. ifr32 = compat_ptr(ifc32.ifcbuf);
  2270. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2271. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2272. return -EFAULT;
  2273. ifr++;
  2274. ifr32++;
  2275. }
  2276. }
  2277. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2278. return -EFAULT;
  2279. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2280. if (err)
  2281. return err;
  2282. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2283. return -EFAULT;
  2284. ifr = ifc.ifc_req;
  2285. ifr32 = compat_ptr(ifc32.ifcbuf);
  2286. for (i = 0, j = 0;
  2287. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2288. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2289. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2290. return -EFAULT;
  2291. ifr32++;
  2292. ifr++;
  2293. }
  2294. if (ifc32.ifcbuf == 0) {
  2295. /* Translate from 64-bit structure multiple to
  2296. * a 32-bit one.
  2297. */
  2298. i = ifc.ifc_len;
  2299. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2300. ifc32.ifc_len = i;
  2301. } else {
  2302. ifc32.ifc_len = i;
  2303. }
  2304. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2305. return -EFAULT;
  2306. return 0;
  2307. }
  2308. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2309. {
  2310. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2311. bool convert_in = false, convert_out = false;
  2312. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2313. struct ethtool_rxnfc __user *rxnfc;
  2314. struct ifreq __user *ifr;
  2315. u32 rule_cnt = 0, actual_rule_cnt;
  2316. u32 ethcmd;
  2317. u32 data;
  2318. int ret;
  2319. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2320. return -EFAULT;
  2321. compat_rxnfc = compat_ptr(data);
  2322. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2323. return -EFAULT;
  2324. /* Most ethtool structures are defined without padding.
  2325. * Unfortunately struct ethtool_rxnfc is an exception.
  2326. */
  2327. switch (ethcmd) {
  2328. default:
  2329. break;
  2330. case ETHTOOL_GRXCLSRLALL:
  2331. /* Buffer size is variable */
  2332. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2333. return -EFAULT;
  2334. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2335. return -ENOMEM;
  2336. buf_size += rule_cnt * sizeof(u32);
  2337. /* fall through */
  2338. case ETHTOOL_GRXRINGS:
  2339. case ETHTOOL_GRXCLSRLCNT:
  2340. case ETHTOOL_GRXCLSRULE:
  2341. convert_out = true;
  2342. /* fall through */
  2343. case ETHTOOL_SRXCLSRLDEL:
  2344. case ETHTOOL_SRXCLSRLINS:
  2345. buf_size += sizeof(struct ethtool_rxnfc);
  2346. convert_in = true;
  2347. break;
  2348. }
  2349. ifr = compat_alloc_user_space(buf_size);
  2350. rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2351. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2352. return -EFAULT;
  2353. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2354. &ifr->ifr_ifru.ifru_data))
  2355. return -EFAULT;
  2356. if (convert_in) {
  2357. /* We expect there to be holes between fs.m_ext and
  2358. * fs.ring_cookie and at the end of fs, but nowhere else.
  2359. */
  2360. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2361. sizeof(compat_rxnfc->fs.m_ext) !=
  2362. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2363. sizeof(rxnfc->fs.m_ext));
  2364. BUILD_BUG_ON(
  2365. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2366. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2367. offsetof(struct ethtool_rxnfc, fs.location) -
  2368. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2369. if (copy_in_user(rxnfc, compat_rxnfc,
  2370. (void *)(&rxnfc->fs.m_ext + 1) -
  2371. (void *)rxnfc) ||
  2372. copy_in_user(&rxnfc->fs.ring_cookie,
  2373. &compat_rxnfc->fs.ring_cookie,
  2374. (void *)(&rxnfc->fs.location + 1) -
  2375. (void *)&rxnfc->fs.ring_cookie) ||
  2376. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2377. sizeof(rxnfc->rule_cnt)))
  2378. return -EFAULT;
  2379. }
  2380. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2381. if (ret)
  2382. return ret;
  2383. if (convert_out) {
  2384. if (copy_in_user(compat_rxnfc, rxnfc,
  2385. (const void *)(&rxnfc->fs.m_ext + 1) -
  2386. (const void *)rxnfc) ||
  2387. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2388. &rxnfc->fs.ring_cookie,
  2389. (const void *)(&rxnfc->fs.location + 1) -
  2390. (const void *)&rxnfc->fs.ring_cookie) ||
  2391. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2392. sizeof(rxnfc->rule_cnt)))
  2393. return -EFAULT;
  2394. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2395. /* As an optimisation, we only copy the actual
  2396. * number of rules that the underlying
  2397. * function returned. Since Mallory might
  2398. * change the rule count in user memory, we
  2399. * check that it is less than the rule count
  2400. * originally given (as the user buffer size),
  2401. * which has been range-checked.
  2402. */
  2403. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2404. return -EFAULT;
  2405. if (actual_rule_cnt < rule_cnt)
  2406. rule_cnt = actual_rule_cnt;
  2407. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2408. &rxnfc->rule_locs[0],
  2409. rule_cnt * sizeof(u32)))
  2410. return -EFAULT;
  2411. }
  2412. }
  2413. return 0;
  2414. }
  2415. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2416. {
  2417. void __user *uptr;
  2418. compat_uptr_t uptr32;
  2419. struct ifreq __user *uifr;
  2420. uifr = compat_alloc_user_space(sizeof(*uifr));
  2421. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2422. return -EFAULT;
  2423. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2424. return -EFAULT;
  2425. uptr = compat_ptr(uptr32);
  2426. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2427. return -EFAULT;
  2428. return dev_ioctl(net, SIOCWANDEV, uifr);
  2429. }
  2430. static int bond_ioctl(struct net *net, unsigned int cmd,
  2431. struct compat_ifreq __user *ifr32)
  2432. {
  2433. struct ifreq kifr;
  2434. struct ifreq __user *uifr;
  2435. mm_segment_t old_fs;
  2436. int err;
  2437. u32 data;
  2438. void __user *datap;
  2439. switch (cmd) {
  2440. case SIOCBONDENSLAVE:
  2441. case SIOCBONDRELEASE:
  2442. case SIOCBONDSETHWADDR:
  2443. case SIOCBONDCHANGEACTIVE:
  2444. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2445. return -EFAULT;
  2446. old_fs = get_fs();
  2447. set_fs(KERNEL_DS);
  2448. err = dev_ioctl(net, cmd,
  2449. (struct ifreq __user __force *) &kifr);
  2450. set_fs(old_fs);
  2451. return err;
  2452. case SIOCBONDSLAVEINFOQUERY:
  2453. case SIOCBONDINFOQUERY:
  2454. uifr = compat_alloc_user_space(sizeof(*uifr));
  2455. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2456. return -EFAULT;
  2457. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2458. return -EFAULT;
  2459. datap = compat_ptr(data);
  2460. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2461. return -EFAULT;
  2462. return dev_ioctl(net, cmd, uifr);
  2463. default:
  2464. return -EINVAL;
  2465. }
  2466. }
  2467. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2468. struct compat_ifreq __user *u_ifreq32)
  2469. {
  2470. struct ifreq __user *u_ifreq64;
  2471. char tmp_buf[IFNAMSIZ];
  2472. void __user *data64;
  2473. u32 data32;
  2474. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2475. IFNAMSIZ))
  2476. return -EFAULT;
  2477. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2478. return -EFAULT;
  2479. data64 = compat_ptr(data32);
  2480. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2481. /* Don't check these user accesses, just let that get trapped
  2482. * in the ioctl handler instead.
  2483. */
  2484. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2485. IFNAMSIZ))
  2486. return -EFAULT;
  2487. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2488. return -EFAULT;
  2489. return dev_ioctl(net, cmd, u_ifreq64);
  2490. }
  2491. static int dev_ifsioc(struct net *net, struct socket *sock,
  2492. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2493. {
  2494. struct ifreq __user *uifr;
  2495. int err;
  2496. uifr = compat_alloc_user_space(sizeof(*uifr));
  2497. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2498. return -EFAULT;
  2499. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2500. if (!err) {
  2501. switch (cmd) {
  2502. case SIOCGIFFLAGS:
  2503. case SIOCGIFMETRIC:
  2504. case SIOCGIFMTU:
  2505. case SIOCGIFMEM:
  2506. case SIOCGIFHWADDR:
  2507. case SIOCGIFINDEX:
  2508. case SIOCGIFADDR:
  2509. case SIOCGIFBRDADDR:
  2510. case SIOCGIFDSTADDR:
  2511. case SIOCGIFNETMASK:
  2512. case SIOCGIFPFLAGS:
  2513. case SIOCGIFTXQLEN:
  2514. case SIOCGMIIPHY:
  2515. case SIOCGMIIREG:
  2516. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2517. err = -EFAULT;
  2518. break;
  2519. }
  2520. }
  2521. return err;
  2522. }
  2523. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2524. struct compat_ifreq __user *uifr32)
  2525. {
  2526. struct ifreq ifr;
  2527. struct compat_ifmap __user *uifmap32;
  2528. mm_segment_t old_fs;
  2529. int err;
  2530. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2531. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2532. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2533. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2534. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2535. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2536. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2537. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2538. if (err)
  2539. return -EFAULT;
  2540. old_fs = get_fs();
  2541. set_fs(KERNEL_DS);
  2542. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2543. set_fs(old_fs);
  2544. if (cmd == SIOCGIFMAP && !err) {
  2545. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2546. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2547. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2548. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2549. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2550. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2551. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2552. if (err)
  2553. err = -EFAULT;
  2554. }
  2555. return err;
  2556. }
  2557. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2558. {
  2559. void __user *uptr;
  2560. compat_uptr_t uptr32;
  2561. struct ifreq __user *uifr;
  2562. uifr = compat_alloc_user_space(sizeof(*uifr));
  2563. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2564. return -EFAULT;
  2565. if (get_user(uptr32, &uifr32->ifr_data))
  2566. return -EFAULT;
  2567. uptr = compat_ptr(uptr32);
  2568. if (put_user(uptr, &uifr->ifr_data))
  2569. return -EFAULT;
  2570. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2571. }
  2572. struct rtentry32 {
  2573. u32 rt_pad1;
  2574. struct sockaddr rt_dst; /* target address */
  2575. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2576. struct sockaddr rt_genmask; /* target network mask (IP) */
  2577. unsigned short rt_flags;
  2578. short rt_pad2;
  2579. u32 rt_pad3;
  2580. unsigned char rt_tos;
  2581. unsigned char rt_class;
  2582. short rt_pad4;
  2583. short rt_metric; /* +1 for binary compatibility! */
  2584. /* char * */ u32 rt_dev; /* forcing the device at add */
  2585. u32 rt_mtu; /* per route MTU/Window */
  2586. u32 rt_window; /* Window clamping */
  2587. unsigned short rt_irtt; /* Initial RTT */
  2588. };
  2589. struct in6_rtmsg32 {
  2590. struct in6_addr rtmsg_dst;
  2591. struct in6_addr rtmsg_src;
  2592. struct in6_addr rtmsg_gateway;
  2593. u32 rtmsg_type;
  2594. u16 rtmsg_dst_len;
  2595. u16 rtmsg_src_len;
  2596. u32 rtmsg_metric;
  2597. u32 rtmsg_info;
  2598. u32 rtmsg_flags;
  2599. s32 rtmsg_ifindex;
  2600. };
  2601. static int routing_ioctl(struct net *net, struct socket *sock,
  2602. unsigned int cmd, void __user *argp)
  2603. {
  2604. int ret;
  2605. void *r = NULL;
  2606. struct in6_rtmsg r6;
  2607. struct rtentry r4;
  2608. char devname[16];
  2609. u32 rtdev;
  2610. mm_segment_t old_fs = get_fs();
  2611. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2612. struct in6_rtmsg32 __user *ur6 = argp;
  2613. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2614. 3 * sizeof(struct in6_addr));
  2615. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2616. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2617. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2618. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2619. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2620. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2621. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2622. r = (void *) &r6;
  2623. } else { /* ipv4 */
  2624. struct rtentry32 __user *ur4 = argp;
  2625. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2626. 3 * sizeof(struct sockaddr));
  2627. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2628. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2629. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2630. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2631. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2632. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2633. if (rtdev) {
  2634. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2635. r4.rt_dev = (char __user __force *)devname;
  2636. devname[15] = 0;
  2637. } else
  2638. r4.rt_dev = NULL;
  2639. r = (void *) &r4;
  2640. }
  2641. if (ret) {
  2642. ret = -EFAULT;
  2643. goto out;
  2644. }
  2645. set_fs(KERNEL_DS);
  2646. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2647. set_fs(old_fs);
  2648. out:
  2649. return ret;
  2650. }
  2651. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2652. * for some operations; this forces use of the newer bridge-utils that
  2653. * use compatible ioctls
  2654. */
  2655. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2656. {
  2657. compat_ulong_t tmp;
  2658. if (get_user(tmp, argp))
  2659. return -EFAULT;
  2660. if (tmp == BRCTL_GET_VERSION)
  2661. return BRCTL_VERSION + 1;
  2662. return -EINVAL;
  2663. }
  2664. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2665. unsigned int cmd, unsigned long arg)
  2666. {
  2667. void __user *argp = compat_ptr(arg);
  2668. struct sock *sk = sock->sk;
  2669. struct net *net = sock_net(sk);
  2670. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2671. return siocdevprivate_ioctl(net, cmd, argp);
  2672. switch (cmd) {
  2673. case SIOCSIFBR:
  2674. case SIOCGIFBR:
  2675. return old_bridge_ioctl(argp);
  2676. case SIOCGIFNAME:
  2677. return dev_ifname32(net, argp);
  2678. case SIOCGIFCONF:
  2679. return dev_ifconf(net, argp);
  2680. case SIOCETHTOOL:
  2681. return ethtool_ioctl(net, argp);
  2682. case SIOCWANDEV:
  2683. return compat_siocwandev(net, argp);
  2684. case SIOCGIFMAP:
  2685. case SIOCSIFMAP:
  2686. return compat_sioc_ifmap(net, cmd, argp);
  2687. case SIOCBONDENSLAVE:
  2688. case SIOCBONDRELEASE:
  2689. case SIOCBONDSETHWADDR:
  2690. case SIOCBONDSLAVEINFOQUERY:
  2691. case SIOCBONDINFOQUERY:
  2692. case SIOCBONDCHANGEACTIVE:
  2693. return bond_ioctl(net, cmd, argp);
  2694. case SIOCADDRT:
  2695. case SIOCDELRT:
  2696. return routing_ioctl(net, sock, cmd, argp);
  2697. case SIOCGSTAMP:
  2698. return do_siocgstamp(net, sock, cmd, argp);
  2699. case SIOCGSTAMPNS:
  2700. return do_siocgstampns(net, sock, cmd, argp);
  2701. case SIOCSHWTSTAMP:
  2702. return compat_siocshwtstamp(net, argp);
  2703. case FIOSETOWN:
  2704. case SIOCSPGRP:
  2705. case FIOGETOWN:
  2706. case SIOCGPGRP:
  2707. case SIOCBRADDBR:
  2708. case SIOCBRDELBR:
  2709. case SIOCGIFVLAN:
  2710. case SIOCSIFVLAN:
  2711. case SIOCADDDLCI:
  2712. case SIOCDELDLCI:
  2713. return sock_ioctl(file, cmd, arg);
  2714. case SIOCGIFFLAGS:
  2715. case SIOCSIFFLAGS:
  2716. case SIOCGIFMETRIC:
  2717. case SIOCSIFMETRIC:
  2718. case SIOCGIFMTU:
  2719. case SIOCSIFMTU:
  2720. case SIOCGIFMEM:
  2721. case SIOCSIFMEM:
  2722. case SIOCGIFHWADDR:
  2723. case SIOCSIFHWADDR:
  2724. case SIOCADDMULTI:
  2725. case SIOCDELMULTI:
  2726. case SIOCGIFINDEX:
  2727. case SIOCGIFADDR:
  2728. case SIOCSIFADDR:
  2729. case SIOCSIFHWBROADCAST:
  2730. case SIOCDIFADDR:
  2731. case SIOCGIFBRDADDR:
  2732. case SIOCSIFBRDADDR:
  2733. case SIOCGIFDSTADDR:
  2734. case SIOCSIFDSTADDR:
  2735. case SIOCGIFNETMASK:
  2736. case SIOCSIFNETMASK:
  2737. case SIOCSIFPFLAGS:
  2738. case SIOCGIFPFLAGS:
  2739. case SIOCGIFTXQLEN:
  2740. case SIOCSIFTXQLEN:
  2741. case SIOCBRADDIF:
  2742. case SIOCBRDELIF:
  2743. case SIOCSIFNAME:
  2744. case SIOCGMIIPHY:
  2745. case SIOCGMIIREG:
  2746. case SIOCSMIIREG:
  2747. return dev_ifsioc(net, sock, cmd, argp);
  2748. case SIOCSARP:
  2749. case SIOCGARP:
  2750. case SIOCDARP:
  2751. case SIOCATMARK:
  2752. return sock_do_ioctl(net, sock, cmd, arg);
  2753. }
  2754. /* Prevent warning from compat_sys_ioctl, these always
  2755. * result in -EINVAL in the native case anyway. */
  2756. switch (cmd) {
  2757. case SIOCRTMSG:
  2758. case SIOCGIFCOUNT:
  2759. case SIOCSRARP:
  2760. case SIOCGRARP:
  2761. case SIOCDRARP:
  2762. case SIOCSIFLINK:
  2763. case SIOCGIFSLAVE:
  2764. case SIOCSIFSLAVE:
  2765. return -EINVAL;
  2766. }
  2767. return -ENOIOCTLCMD;
  2768. }
  2769. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  2770. unsigned long arg)
  2771. {
  2772. struct socket *sock = file->private_data;
  2773. int ret = -ENOIOCTLCMD;
  2774. struct sock *sk;
  2775. struct net *net;
  2776. sk = sock->sk;
  2777. net = sock_net(sk);
  2778. if (sock->ops->compat_ioctl)
  2779. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2780. if (ret == -ENOIOCTLCMD &&
  2781. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2782. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2783. if (ret == -ENOIOCTLCMD)
  2784. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2785. return ret;
  2786. }
  2787. #endif
  2788. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2789. {
  2790. return sock->ops->bind(sock, addr, addrlen);
  2791. }
  2792. EXPORT_SYMBOL(kernel_bind);
  2793. int kernel_listen(struct socket *sock, int backlog)
  2794. {
  2795. return sock->ops->listen(sock, backlog);
  2796. }
  2797. EXPORT_SYMBOL(kernel_listen);
  2798. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2799. {
  2800. struct sock *sk = sock->sk;
  2801. int err;
  2802. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2803. newsock);
  2804. if (err < 0)
  2805. goto done;
  2806. err = sock->ops->accept(sock, *newsock, flags);
  2807. if (err < 0) {
  2808. sock_release(*newsock);
  2809. *newsock = NULL;
  2810. goto done;
  2811. }
  2812. (*newsock)->ops = sock->ops;
  2813. __module_get((*newsock)->ops->owner);
  2814. done:
  2815. return err;
  2816. }
  2817. EXPORT_SYMBOL(kernel_accept);
  2818. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2819. int flags)
  2820. {
  2821. return sock->ops->connect(sock, addr, addrlen, flags);
  2822. }
  2823. EXPORT_SYMBOL(kernel_connect);
  2824. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2825. int *addrlen)
  2826. {
  2827. return sock->ops->getname(sock, addr, addrlen, 0);
  2828. }
  2829. EXPORT_SYMBOL(kernel_getsockname);
  2830. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2831. int *addrlen)
  2832. {
  2833. return sock->ops->getname(sock, addr, addrlen, 1);
  2834. }
  2835. EXPORT_SYMBOL(kernel_getpeername);
  2836. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2837. char *optval, int *optlen)
  2838. {
  2839. mm_segment_t oldfs = get_fs();
  2840. char __user *uoptval;
  2841. int __user *uoptlen;
  2842. int err;
  2843. uoptval = (char __user __force *) optval;
  2844. uoptlen = (int __user __force *) optlen;
  2845. set_fs(KERNEL_DS);
  2846. if (level == SOL_SOCKET)
  2847. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2848. else
  2849. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2850. uoptlen);
  2851. set_fs(oldfs);
  2852. return err;
  2853. }
  2854. EXPORT_SYMBOL(kernel_getsockopt);
  2855. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2856. char *optval, unsigned int optlen)
  2857. {
  2858. mm_segment_t oldfs = get_fs();
  2859. char __user *uoptval;
  2860. int err;
  2861. uoptval = (char __user __force *) optval;
  2862. set_fs(KERNEL_DS);
  2863. if (level == SOL_SOCKET)
  2864. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  2865. else
  2866. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  2867. optlen);
  2868. set_fs(oldfs);
  2869. return err;
  2870. }
  2871. EXPORT_SYMBOL(kernel_setsockopt);
  2872. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2873. size_t size, int flags)
  2874. {
  2875. sock_update_classid(sock->sk);
  2876. if (sock->ops->sendpage)
  2877. return sock->ops->sendpage(sock, page, offset, size, flags);
  2878. return sock_no_sendpage(sock, page, offset, size, flags);
  2879. }
  2880. EXPORT_SYMBOL(kernel_sendpage);
  2881. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2882. {
  2883. mm_segment_t oldfs = get_fs();
  2884. int err;
  2885. set_fs(KERNEL_DS);
  2886. err = sock->ops->ioctl(sock, cmd, arg);
  2887. set_fs(oldfs);
  2888. return err;
  2889. }
  2890. EXPORT_SYMBOL(kernel_sock_ioctl);
  2891. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2892. {
  2893. return sock->ops->shutdown(sock, how);
  2894. }
  2895. EXPORT_SYMBOL(kernel_sock_shutdown);