socket.c 83 KB

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