socket.c 84 KB

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