socket.c 84 KB

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