socket.c 82 KB

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