socket.c 81 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. 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. EXPORT_SYMBOL(sock_from_file);
  362. /**
  363. * sockfd_lookup - Go from a file number to its socket slot
  364. * @fd: file handle
  365. * @err: pointer to an error code return
  366. *
  367. * The file handle passed in is locked and the socket it is bound
  368. * too is returned. If an error occurs the err pointer is overwritten
  369. * with a negative errno code and NULL is returned. The function checks
  370. * for both invalid handles and passing a handle which is not a socket.
  371. *
  372. * On a success the socket object pointer is returned.
  373. */
  374. struct socket *sockfd_lookup(int fd, int *err)
  375. {
  376. struct file *file;
  377. struct socket *sock;
  378. file = fget(fd);
  379. if (!file) {
  380. *err = -EBADF;
  381. return NULL;
  382. }
  383. sock = sock_from_file(file, err);
  384. if (!sock)
  385. fput(file);
  386. return sock;
  387. }
  388. EXPORT_SYMBOL(sockfd_lookup);
  389. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  390. {
  391. struct file *file;
  392. struct socket *sock;
  393. *err = -EBADF;
  394. file = fget_light(fd, fput_needed);
  395. if (file) {
  396. sock = sock_from_file(file, err);
  397. if (sock)
  398. return sock;
  399. fput_light(file, *fput_needed);
  400. }
  401. return NULL;
  402. }
  403. /**
  404. * sock_alloc - allocate a socket
  405. *
  406. * Allocate a new inode and socket object. The two are bound together
  407. * and initialised. The socket is then returned. If we are out of inodes
  408. * NULL is returned.
  409. */
  410. static struct socket *sock_alloc(void)
  411. {
  412. struct inode *inode;
  413. struct socket *sock;
  414. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  415. if (!inode)
  416. return NULL;
  417. sock = SOCKET_I(inode);
  418. kmemcheck_annotate_bitfield(sock, type);
  419. inode->i_ino = get_next_ino();
  420. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  421. inode->i_uid = current_fsuid();
  422. inode->i_gid = current_fsgid();
  423. this_cpu_add(sockets_in_use, 1);
  424. return sock;
  425. }
  426. /*
  427. * In theory you can't get an open on this inode, but /proc provides
  428. * a back door. Remember to keep it shut otherwise you'll let the
  429. * creepy crawlies in.
  430. */
  431. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  432. {
  433. return -ENXIO;
  434. }
  435. const struct file_operations bad_sock_fops = {
  436. .owner = THIS_MODULE,
  437. .open = sock_no_open,
  438. .llseek = noop_llseek,
  439. };
  440. /**
  441. * sock_release - close a socket
  442. * @sock: socket to close
  443. *
  444. * The socket is released from the protocol stack if it has a release
  445. * callback, and the inode is then released if the socket is bound to
  446. * an inode not a file.
  447. */
  448. void sock_release(struct socket *sock)
  449. {
  450. if (sock->ops) {
  451. struct module *owner = sock->ops->owner;
  452. sock->ops->release(sock);
  453. sock->ops = NULL;
  454. module_put(owner);
  455. }
  456. if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
  457. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  458. if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
  459. return;
  460. this_cpu_sub(sockets_in_use, 1);
  461. if (!sock->file) {
  462. iput(SOCK_INODE(sock));
  463. return;
  464. }
  465. sock->file = NULL;
  466. }
  467. EXPORT_SYMBOL(sock_release);
  468. int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
  469. {
  470. *tx_flags = 0;
  471. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
  472. *tx_flags |= SKBTX_HW_TSTAMP;
  473. if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
  474. *tx_flags |= SKBTX_SW_TSTAMP;
  475. if (sock_flag(sk, SOCK_WIFI_STATUS))
  476. *tx_flags |= SKBTX_WIFI_STATUS;
  477. return 0;
  478. }
  479. EXPORT_SYMBOL(sock_tx_timestamp);
  480. static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
  481. struct msghdr *msg, size_t size)
  482. {
  483. struct sock_iocb *si = kiocb_to_siocb(iocb);
  484. sock_update_classid(sock->sk);
  485. si->sock = sock;
  486. si->scm = NULL;
  487. si->msg = msg;
  488. si->size = size;
  489. return sock->ops->sendmsg(iocb, sock, msg, size);
  490. }
  491. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  492. struct msghdr *msg, size_t size)
  493. {
  494. int err = security_socket_sendmsg(sock, msg, size);
  495. return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
  496. }
  497. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  498. {
  499. struct kiocb iocb;
  500. struct sock_iocb siocb;
  501. int ret;
  502. init_sync_kiocb(&iocb, NULL);
  503. iocb.private = &siocb;
  504. ret = __sock_sendmsg(&iocb, sock, msg, size);
  505. if (-EIOCBQUEUED == ret)
  506. ret = wait_on_sync_kiocb(&iocb);
  507. return ret;
  508. }
  509. EXPORT_SYMBOL(sock_sendmsg);
  510. static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
  511. {
  512. struct kiocb iocb;
  513. struct sock_iocb siocb;
  514. int ret;
  515. init_sync_kiocb(&iocb, NULL);
  516. iocb.private = &siocb;
  517. ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
  518. if (-EIOCBQUEUED == ret)
  519. ret = wait_on_sync_kiocb(&iocb);
  520. return ret;
  521. }
  522. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  523. struct kvec *vec, size_t num, size_t size)
  524. {
  525. mm_segment_t oldfs = get_fs();
  526. int result;
  527. set_fs(KERNEL_DS);
  528. /*
  529. * the following is safe, since for compiler definitions of kvec and
  530. * iovec are identical, yielding the same in-core layout and alignment
  531. */
  532. msg->msg_iov = (struct iovec *)vec;
  533. msg->msg_iovlen = num;
  534. result = sock_sendmsg(sock, msg, size);
  535. set_fs(oldfs);
  536. return result;
  537. }
  538. EXPORT_SYMBOL(kernel_sendmsg);
  539. static int ktime2ts(ktime_t kt, struct timespec *ts)
  540. {
  541. if (kt.tv64) {
  542. *ts = ktime_to_timespec(kt);
  543. return 1;
  544. } else {
  545. return 0;
  546. }
  547. }
  548. /*
  549. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  550. */
  551. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  552. struct sk_buff *skb)
  553. {
  554. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  555. struct timespec ts[3];
  556. int empty = 1;
  557. struct skb_shared_hwtstamps *shhwtstamps =
  558. skb_hwtstamps(skb);
  559. /* Race occurred between timestamp enabling and packet
  560. receiving. Fill in the current time for now. */
  561. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  562. __net_timestamp(skb);
  563. if (need_software_tstamp) {
  564. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  565. struct timeval tv;
  566. skb_get_timestamp(skb, &tv);
  567. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  568. sizeof(tv), &tv);
  569. } else {
  570. skb_get_timestampns(skb, &ts[0]);
  571. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  572. sizeof(ts[0]), &ts[0]);
  573. }
  574. }
  575. memset(ts, 0, sizeof(ts));
  576. if (skb->tstamp.tv64 &&
  577. sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
  578. skb_get_timestampns(skb, ts + 0);
  579. empty = 0;
  580. }
  581. if (shhwtstamps) {
  582. if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
  583. ktime2ts(shhwtstamps->syststamp, ts + 1))
  584. empty = 0;
  585. if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
  586. ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  587. empty = 0;
  588. }
  589. if (!empty)
  590. put_cmsg(msg, SOL_SOCKET,
  591. SCM_TIMESTAMPING, sizeof(ts), &ts);
  592. }
  593. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  594. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  595. struct sk_buff *skb)
  596. {
  597. int ack;
  598. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  599. return;
  600. if (!skb->wifi_acked_valid)
  601. return;
  602. ack = skb->wifi_acked;
  603. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  604. }
  605. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  606. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  607. struct sk_buff *skb)
  608. {
  609. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
  610. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  611. sizeof(__u32), &skb->dropcount);
  612. }
  613. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  614. struct sk_buff *skb)
  615. {
  616. sock_recv_timestamp(msg, sk, skb);
  617. sock_recv_drops(msg, sk, skb);
  618. }
  619. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  620. static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
  621. struct msghdr *msg, size_t size, int flags)
  622. {
  623. struct sock_iocb *si = kiocb_to_siocb(iocb);
  624. sock_update_classid(sock->sk);
  625. si->sock = sock;
  626. si->scm = NULL;
  627. si->msg = msg;
  628. si->size = size;
  629. si->flags = flags;
  630. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  631. }
  632. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  633. struct msghdr *msg, size_t size, int flags)
  634. {
  635. int err = security_socket_recvmsg(sock, msg, size, flags);
  636. return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
  637. }
  638. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  639. size_t size, int flags)
  640. {
  641. struct kiocb iocb;
  642. struct sock_iocb siocb;
  643. int ret;
  644. init_sync_kiocb(&iocb, NULL);
  645. iocb.private = &siocb;
  646. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  647. if (-EIOCBQUEUED == ret)
  648. ret = wait_on_sync_kiocb(&iocb);
  649. return ret;
  650. }
  651. EXPORT_SYMBOL(sock_recvmsg);
  652. static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  653. size_t size, int flags)
  654. {
  655. struct kiocb iocb;
  656. struct sock_iocb siocb;
  657. int ret;
  658. init_sync_kiocb(&iocb, NULL);
  659. iocb.private = &siocb;
  660. ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
  661. if (-EIOCBQUEUED == ret)
  662. ret = wait_on_sync_kiocb(&iocb);
  663. return ret;
  664. }
  665. /**
  666. * kernel_recvmsg - Receive a message from a socket (kernel space)
  667. * @sock: The socket to receive the message from
  668. * @msg: Received message
  669. * @vec: Input s/g array for message data
  670. * @num: Size of input s/g array
  671. * @size: Number of bytes to read
  672. * @flags: Message flags (MSG_DONTWAIT, etc...)
  673. *
  674. * On return the msg structure contains the scatter/gather array passed in the
  675. * vec argument. The array is modified so that it consists of the unfilled
  676. * portion of the original array.
  677. *
  678. * The returned value is the total number of bytes received, or an error.
  679. */
  680. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  681. struct kvec *vec, size_t num, size_t size, int flags)
  682. {
  683. mm_segment_t oldfs = get_fs();
  684. int result;
  685. set_fs(KERNEL_DS);
  686. /*
  687. * the following is safe, since for compiler definitions of kvec and
  688. * iovec are identical, yielding the same in-core layout and alignment
  689. */
  690. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  691. result = sock_recvmsg(sock, msg, size, flags);
  692. set_fs(oldfs);
  693. return result;
  694. }
  695. EXPORT_SYMBOL(kernel_recvmsg);
  696. static void sock_aio_dtor(struct kiocb *iocb)
  697. {
  698. kfree(iocb->private);
  699. }
  700. static ssize_t sock_sendpage(struct file *file, struct page *page,
  701. int offset, size_t size, loff_t *ppos, int more)
  702. {
  703. struct socket *sock;
  704. int flags;
  705. sock = file->private_data;
  706. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  707. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  708. flags |= more;
  709. return kernel_sendpage(sock, page, offset, size, flags);
  710. }
  711. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  712. struct pipe_inode_info *pipe, size_t len,
  713. unsigned int flags)
  714. {
  715. struct socket *sock = file->private_data;
  716. if (unlikely(!sock->ops->splice_read))
  717. return -EINVAL;
  718. sock_update_classid(sock->sk);
  719. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  720. }
  721. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  722. struct sock_iocb *siocb)
  723. {
  724. if (!is_sync_kiocb(iocb)) {
  725. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  726. if (!siocb)
  727. return NULL;
  728. iocb->ki_dtor = sock_aio_dtor;
  729. }
  730. siocb->kiocb = iocb;
  731. iocb->private = siocb;
  732. return siocb;
  733. }
  734. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  735. struct file *file, const struct iovec *iov,
  736. unsigned long nr_segs)
  737. {
  738. struct socket *sock = file->private_data;
  739. size_t size = 0;
  740. int i;
  741. for (i = 0; i < nr_segs; i++)
  742. size += iov[i].iov_len;
  743. msg->msg_name = NULL;
  744. msg->msg_namelen = 0;
  745. msg->msg_control = NULL;
  746. msg->msg_controllen = 0;
  747. msg->msg_iov = (struct iovec *)iov;
  748. msg->msg_iovlen = nr_segs;
  749. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  750. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  751. }
  752. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  753. unsigned long nr_segs, loff_t pos)
  754. {
  755. struct sock_iocb siocb, *x;
  756. if (pos != 0)
  757. return -ESPIPE;
  758. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  759. return 0;
  760. x = alloc_sock_iocb(iocb, &siocb);
  761. if (!x)
  762. return -ENOMEM;
  763. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  764. }
  765. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  766. struct file *file, const struct iovec *iov,
  767. unsigned long nr_segs)
  768. {
  769. struct socket *sock = file->private_data;
  770. size_t size = 0;
  771. int i;
  772. for (i = 0; i < nr_segs; i++)
  773. size += iov[i].iov_len;
  774. msg->msg_name = NULL;
  775. msg->msg_namelen = 0;
  776. msg->msg_control = NULL;
  777. msg->msg_controllen = 0;
  778. msg->msg_iov = (struct iovec *)iov;
  779. msg->msg_iovlen = nr_segs;
  780. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  781. if (sock->type == SOCK_SEQPACKET)
  782. msg->msg_flags |= MSG_EOR;
  783. return __sock_sendmsg(iocb, sock, msg, size);
  784. }
  785. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  786. unsigned long nr_segs, loff_t pos)
  787. {
  788. struct sock_iocb siocb, *x;
  789. if (pos != 0)
  790. return -ESPIPE;
  791. x = alloc_sock_iocb(iocb, &siocb);
  792. if (!x)
  793. return -ENOMEM;
  794. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  795. }
  796. /*
  797. * Atomic setting of ioctl hooks to avoid race
  798. * with module unload.
  799. */
  800. static DEFINE_MUTEX(br_ioctl_mutex);
  801. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  802. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  803. {
  804. mutex_lock(&br_ioctl_mutex);
  805. br_ioctl_hook = hook;
  806. mutex_unlock(&br_ioctl_mutex);
  807. }
  808. EXPORT_SYMBOL(brioctl_set);
  809. static DEFINE_MUTEX(vlan_ioctl_mutex);
  810. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  811. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  812. {
  813. mutex_lock(&vlan_ioctl_mutex);
  814. vlan_ioctl_hook = hook;
  815. mutex_unlock(&vlan_ioctl_mutex);
  816. }
  817. EXPORT_SYMBOL(vlan_ioctl_set);
  818. static DEFINE_MUTEX(dlci_ioctl_mutex);
  819. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  820. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  821. {
  822. mutex_lock(&dlci_ioctl_mutex);
  823. dlci_ioctl_hook = hook;
  824. mutex_unlock(&dlci_ioctl_mutex);
  825. }
  826. EXPORT_SYMBOL(dlci_ioctl_set);
  827. static long sock_do_ioctl(struct net *net, struct socket *sock,
  828. unsigned int cmd, unsigned long arg)
  829. {
  830. int err;
  831. void __user *argp = (void __user *)arg;
  832. err = sock->ops->ioctl(sock, cmd, arg);
  833. /*
  834. * If this ioctl is unknown try to hand it down
  835. * to the NIC driver.
  836. */
  837. if (err == -ENOIOCTLCMD)
  838. err = dev_ioctl(net, cmd, argp);
  839. return err;
  840. }
  841. /*
  842. * With an ioctl, arg may well be a user mode pointer, but we don't know
  843. * what to do with it - that's up to the protocol still.
  844. */
  845. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  846. {
  847. struct socket *sock;
  848. struct sock *sk;
  849. void __user *argp = (void __user *)arg;
  850. int pid, err;
  851. struct net *net;
  852. sock = file->private_data;
  853. sk = sock->sk;
  854. net = sock_net(sk);
  855. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  856. err = dev_ioctl(net, cmd, argp);
  857. } else
  858. #ifdef CONFIG_WEXT_CORE
  859. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  860. err = dev_ioctl(net, cmd, argp);
  861. } else
  862. #endif
  863. switch (cmd) {
  864. case FIOSETOWN:
  865. case SIOCSPGRP:
  866. err = -EFAULT;
  867. if (get_user(pid, (int __user *)argp))
  868. break;
  869. err = f_setown(sock->file, pid, 1);
  870. break;
  871. case FIOGETOWN:
  872. case SIOCGPGRP:
  873. err = put_user(f_getown(sock->file),
  874. (int __user *)argp);
  875. break;
  876. case SIOCGIFBR:
  877. case SIOCSIFBR:
  878. case SIOCBRADDBR:
  879. case SIOCBRDELBR:
  880. err = -ENOPKG;
  881. if (!br_ioctl_hook)
  882. request_module("bridge");
  883. mutex_lock(&br_ioctl_mutex);
  884. if (br_ioctl_hook)
  885. err = br_ioctl_hook(net, cmd, argp);
  886. mutex_unlock(&br_ioctl_mutex);
  887. break;
  888. case SIOCGIFVLAN:
  889. case SIOCSIFVLAN:
  890. err = -ENOPKG;
  891. if (!vlan_ioctl_hook)
  892. request_module("8021q");
  893. mutex_lock(&vlan_ioctl_mutex);
  894. if (vlan_ioctl_hook)
  895. err = vlan_ioctl_hook(net, argp);
  896. mutex_unlock(&vlan_ioctl_mutex);
  897. break;
  898. case SIOCADDDLCI:
  899. case SIOCDELDLCI:
  900. err = -ENOPKG;
  901. if (!dlci_ioctl_hook)
  902. request_module("dlci");
  903. mutex_lock(&dlci_ioctl_mutex);
  904. if (dlci_ioctl_hook)
  905. err = dlci_ioctl_hook(cmd, argp);
  906. mutex_unlock(&dlci_ioctl_mutex);
  907. break;
  908. default:
  909. err = sock_do_ioctl(net, sock, cmd, arg);
  910. break;
  911. }
  912. return err;
  913. }
  914. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  915. {
  916. int err;
  917. struct socket *sock = NULL;
  918. err = security_socket_create(family, type, protocol, 1);
  919. if (err)
  920. goto out;
  921. sock = sock_alloc();
  922. if (!sock) {
  923. err = -ENOMEM;
  924. goto out;
  925. }
  926. sock->type = type;
  927. err = security_socket_post_create(sock, family, type, protocol, 1);
  928. if (err)
  929. goto out_release;
  930. out:
  931. *res = sock;
  932. return err;
  933. out_release:
  934. sock_release(sock);
  935. sock = NULL;
  936. goto out;
  937. }
  938. EXPORT_SYMBOL(sock_create_lite);
  939. /* No kernel lock held - perfect */
  940. static unsigned int sock_poll(struct file *file, poll_table *wait)
  941. {
  942. struct socket *sock;
  943. /*
  944. * We can't return errors to poll, so it's either yes or no.
  945. */
  946. sock = file->private_data;
  947. return sock->ops->poll(file, sock, wait);
  948. }
  949. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  950. {
  951. struct socket *sock = file->private_data;
  952. return sock->ops->mmap(file, sock, vma);
  953. }
  954. static int sock_close(struct inode *inode, struct file *filp)
  955. {
  956. /*
  957. * It was possible the inode is NULL we were
  958. * closing an unfinished socket.
  959. */
  960. if (!inode) {
  961. printk(KERN_DEBUG "sock_close: NULL inode\n");
  962. return 0;
  963. }
  964. sock_release(SOCKET_I(inode));
  965. return 0;
  966. }
  967. /*
  968. * Update the socket async list
  969. *
  970. * Fasync_list locking strategy.
  971. *
  972. * 1. fasync_list is modified only under process context socket lock
  973. * i.e. under semaphore.
  974. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  975. * or under socket lock
  976. */
  977. static int sock_fasync(int fd, struct file *filp, int on)
  978. {
  979. struct socket *sock = filp->private_data;
  980. struct sock *sk = sock->sk;
  981. struct socket_wq *wq;
  982. if (sk == NULL)
  983. return -EINVAL;
  984. lock_sock(sk);
  985. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  986. fasync_helper(fd, filp, on, &wq->fasync_list);
  987. if (!wq->fasync_list)
  988. sock_reset_flag(sk, SOCK_FASYNC);
  989. else
  990. sock_set_flag(sk, SOCK_FASYNC);
  991. release_sock(sk);
  992. return 0;
  993. }
  994. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  995. int sock_wake_async(struct socket *sock, int how, int band)
  996. {
  997. struct socket_wq *wq;
  998. if (!sock)
  999. return -1;
  1000. rcu_read_lock();
  1001. wq = rcu_dereference(sock->wq);
  1002. if (!wq || !wq->fasync_list) {
  1003. rcu_read_unlock();
  1004. return -1;
  1005. }
  1006. switch (how) {
  1007. case SOCK_WAKE_WAITD:
  1008. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  1009. break;
  1010. goto call_kill;
  1011. case SOCK_WAKE_SPACE:
  1012. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  1013. break;
  1014. /* fall through */
  1015. case SOCK_WAKE_IO:
  1016. call_kill:
  1017. kill_fasync(&wq->fasync_list, SIGIO, band);
  1018. break;
  1019. case SOCK_WAKE_URG:
  1020. kill_fasync(&wq->fasync_list, SIGURG, band);
  1021. }
  1022. rcu_read_unlock();
  1023. return 0;
  1024. }
  1025. EXPORT_SYMBOL(sock_wake_async);
  1026. int __sock_create(struct net *net, int family, int type, int protocol,
  1027. struct socket **res, int kern)
  1028. {
  1029. int err;
  1030. struct socket *sock;
  1031. const struct net_proto_family *pf;
  1032. /*
  1033. * Check protocol is in range
  1034. */
  1035. if (family < 0 || family >= NPROTO)
  1036. return -EAFNOSUPPORT;
  1037. if (type < 0 || type >= SOCK_MAX)
  1038. return -EINVAL;
  1039. /* Compatibility.
  1040. This uglymoron is moved from INET layer to here to avoid
  1041. deadlock in module load.
  1042. */
  1043. if (family == PF_INET && type == SOCK_PACKET) {
  1044. static int warned;
  1045. if (!warned) {
  1046. warned = 1;
  1047. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1048. current->comm);
  1049. }
  1050. family = PF_PACKET;
  1051. }
  1052. err = security_socket_create(family, type, protocol, kern);
  1053. if (err)
  1054. return err;
  1055. /*
  1056. * Allocate the socket and allow the family to set things up. if
  1057. * the protocol is 0, the family is instructed to select an appropriate
  1058. * default.
  1059. */
  1060. sock = sock_alloc();
  1061. if (!sock) {
  1062. net_warn_ratelimited("socket: no more sockets\n");
  1063. return -ENFILE; /* Not exactly a match, but its the
  1064. closest posix thing */
  1065. }
  1066. sock->type = type;
  1067. #ifdef CONFIG_MODULES
  1068. /* Attempt to load a protocol module if the find failed.
  1069. *
  1070. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1071. * requested real, full-featured networking support upon configuration.
  1072. * Otherwise module support will break!
  1073. */
  1074. if (rcu_access_pointer(net_families[family]) == NULL)
  1075. request_module("net-pf-%d", family);
  1076. #endif
  1077. rcu_read_lock();
  1078. pf = rcu_dereference(net_families[family]);
  1079. err = -EAFNOSUPPORT;
  1080. if (!pf)
  1081. goto out_release;
  1082. /*
  1083. * We will call the ->create function, that possibly is in a loadable
  1084. * module, so we have to bump that loadable module refcnt first.
  1085. */
  1086. if (!try_module_get(pf->owner))
  1087. goto out_release;
  1088. /* Now protected by module ref count */
  1089. rcu_read_unlock();
  1090. err = pf->create(net, sock, protocol, kern);
  1091. if (err < 0)
  1092. goto out_module_put;
  1093. /*
  1094. * Now to bump the refcnt of the [loadable] module that owns this
  1095. * socket at sock_release time we decrement its refcnt.
  1096. */
  1097. if (!try_module_get(sock->ops->owner))
  1098. goto out_module_busy;
  1099. /*
  1100. * Now that we're done with the ->create function, the [loadable]
  1101. * module can have its refcnt decremented
  1102. */
  1103. module_put(pf->owner);
  1104. err = security_socket_post_create(sock, family, type, protocol, kern);
  1105. if (err)
  1106. goto out_sock_release;
  1107. *res = sock;
  1108. return 0;
  1109. out_module_busy:
  1110. err = -EAFNOSUPPORT;
  1111. out_module_put:
  1112. sock->ops = NULL;
  1113. module_put(pf->owner);
  1114. out_sock_release:
  1115. sock_release(sock);
  1116. return err;
  1117. out_release:
  1118. rcu_read_unlock();
  1119. goto out_sock_release;
  1120. }
  1121. EXPORT_SYMBOL(__sock_create);
  1122. int sock_create(int family, int type, int protocol, struct socket **res)
  1123. {
  1124. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1125. }
  1126. EXPORT_SYMBOL(sock_create);
  1127. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1128. {
  1129. return __sock_create(&init_net, family, type, protocol, res, 1);
  1130. }
  1131. EXPORT_SYMBOL(sock_create_kern);
  1132. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1133. {
  1134. int retval;
  1135. struct socket *sock;
  1136. int flags;
  1137. /* Check the SOCK_* constants for consistency. */
  1138. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1139. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1140. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1141. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1142. flags = type & ~SOCK_TYPE_MASK;
  1143. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1144. return -EINVAL;
  1145. type &= SOCK_TYPE_MASK;
  1146. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1147. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1148. retval = sock_create(family, type, protocol, &sock);
  1149. if (retval < 0)
  1150. goto out;
  1151. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1152. if (retval < 0)
  1153. goto out_release;
  1154. out:
  1155. /* It may be already another descriptor 8) Not kernel problem. */
  1156. return retval;
  1157. out_release:
  1158. sock_release(sock);
  1159. return retval;
  1160. }
  1161. /*
  1162. * Create a pair of connected sockets.
  1163. */
  1164. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1165. int __user *, usockvec)
  1166. {
  1167. struct socket *sock1, *sock2;
  1168. int fd1, fd2, err;
  1169. struct file *newfile1, *newfile2;
  1170. int flags;
  1171. flags = type & ~SOCK_TYPE_MASK;
  1172. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1173. return -EINVAL;
  1174. type &= SOCK_TYPE_MASK;
  1175. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1176. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1177. /*
  1178. * Obtain the first socket and check if the underlying protocol
  1179. * supports the socketpair call.
  1180. */
  1181. err = sock_create(family, type, protocol, &sock1);
  1182. if (err < 0)
  1183. goto out;
  1184. err = sock_create(family, type, protocol, &sock2);
  1185. if (err < 0)
  1186. goto out_release_1;
  1187. err = sock1->ops->socketpair(sock1, sock2);
  1188. if (err < 0)
  1189. goto out_release_both;
  1190. fd1 = sock_alloc_file(sock1, &newfile1, flags);
  1191. if (unlikely(fd1 < 0)) {
  1192. err = fd1;
  1193. goto out_release_both;
  1194. }
  1195. fd2 = sock_alloc_file(sock2, &newfile2, flags);
  1196. if (unlikely(fd2 < 0)) {
  1197. err = fd2;
  1198. fput(newfile1);
  1199. put_unused_fd(fd1);
  1200. sock_release(sock2);
  1201. goto out;
  1202. }
  1203. audit_fd_pair(fd1, fd2);
  1204. fd_install(fd1, newfile1);
  1205. fd_install(fd2, newfile2);
  1206. /* fd1 and fd2 may be already another descriptors.
  1207. * Not kernel problem.
  1208. */
  1209. err = put_user(fd1, &usockvec[0]);
  1210. if (!err)
  1211. err = put_user(fd2, &usockvec[1]);
  1212. if (!err)
  1213. return 0;
  1214. sys_close(fd2);
  1215. sys_close(fd1);
  1216. return err;
  1217. out_release_both:
  1218. sock_release(sock2);
  1219. out_release_1:
  1220. sock_release(sock1);
  1221. out:
  1222. return err;
  1223. }
  1224. /*
  1225. * Bind a name to a socket. Nothing much to do here since it's
  1226. * the protocol's responsibility to handle the local address.
  1227. *
  1228. * We move the socket address to kernel space before we call
  1229. * the protocol layer (having also checked the address is ok).
  1230. */
  1231. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1232. {
  1233. struct socket *sock;
  1234. struct sockaddr_storage address;
  1235. int err, fput_needed;
  1236. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1237. if (sock) {
  1238. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1239. if (err >= 0) {
  1240. err = security_socket_bind(sock,
  1241. (struct sockaddr *)&address,
  1242. addrlen);
  1243. if (!err)
  1244. err = sock->ops->bind(sock,
  1245. (struct sockaddr *)
  1246. &address, addrlen);
  1247. }
  1248. fput_light(sock->file, fput_needed);
  1249. }
  1250. return err;
  1251. }
  1252. /*
  1253. * Perform a listen. Basically, we allow the protocol to do anything
  1254. * necessary for a listen, and if that works, we mark the socket as
  1255. * ready for listening.
  1256. */
  1257. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1258. {
  1259. struct socket *sock;
  1260. int err, fput_needed;
  1261. int somaxconn;
  1262. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1263. if (sock) {
  1264. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1265. if ((unsigned int)backlog > somaxconn)
  1266. backlog = somaxconn;
  1267. err = security_socket_listen(sock, backlog);
  1268. if (!err)
  1269. err = sock->ops->listen(sock, backlog);
  1270. fput_light(sock->file, fput_needed);
  1271. }
  1272. return err;
  1273. }
  1274. /*
  1275. * For accept, we attempt to create a new socket, set up the link
  1276. * with the client, wake up the client, then return the new
  1277. * connected fd. We collect the address of the connector in kernel
  1278. * space and move it to user at the very end. This is unclean because
  1279. * we open the socket then return an error.
  1280. *
  1281. * 1003.1g adds the ability to recvmsg() to query connection pending
  1282. * status to recvmsg. We need to add that support in a way thats
  1283. * clean when we restucture accept also.
  1284. */
  1285. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1286. int __user *, upeer_addrlen, int, flags)
  1287. {
  1288. struct socket *sock, *newsock;
  1289. struct file *newfile;
  1290. int err, len, newfd, fput_needed;
  1291. struct sockaddr_storage address;
  1292. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1293. return -EINVAL;
  1294. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1295. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1296. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1297. if (!sock)
  1298. goto out;
  1299. err = -ENFILE;
  1300. newsock = sock_alloc();
  1301. if (!newsock)
  1302. goto out_put;
  1303. newsock->type = sock->type;
  1304. newsock->ops = sock->ops;
  1305. /*
  1306. * We don't need try_module_get here, as the listening socket (sock)
  1307. * has the protocol module (sock->ops->owner) held.
  1308. */
  1309. __module_get(newsock->ops->owner);
  1310. newfd = sock_alloc_file(newsock, &newfile, flags);
  1311. if (unlikely(newfd < 0)) {
  1312. err = newfd;
  1313. sock_release(newsock);
  1314. goto out_put;
  1315. }
  1316. err = security_socket_accept(sock, newsock);
  1317. if (err)
  1318. goto out_fd;
  1319. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1320. if (err < 0)
  1321. goto out_fd;
  1322. if (upeer_sockaddr) {
  1323. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1324. &len, 2) < 0) {
  1325. err = -ECONNABORTED;
  1326. goto out_fd;
  1327. }
  1328. err = move_addr_to_user(&address,
  1329. len, upeer_sockaddr, upeer_addrlen);
  1330. if (err < 0)
  1331. goto out_fd;
  1332. }
  1333. /* File flags are not inherited via accept() unlike another OSes. */
  1334. fd_install(newfd, newfile);
  1335. err = newfd;
  1336. out_put:
  1337. fput_light(sock->file, fput_needed);
  1338. out:
  1339. return err;
  1340. out_fd:
  1341. fput(newfile);
  1342. put_unused_fd(newfd);
  1343. goto out_put;
  1344. }
  1345. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1346. int __user *, upeer_addrlen)
  1347. {
  1348. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1349. }
  1350. /*
  1351. * Attempt to connect to a socket with the server address. The address
  1352. * is in user space so we verify it is OK and move it to kernel space.
  1353. *
  1354. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1355. * break bindings
  1356. *
  1357. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1358. * other SEQPACKET protocols that take time to connect() as it doesn't
  1359. * include the -EINPROGRESS status for such sockets.
  1360. */
  1361. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1362. int, addrlen)
  1363. {
  1364. struct socket *sock;
  1365. struct sockaddr_storage address;
  1366. int err, fput_needed;
  1367. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1368. if (!sock)
  1369. goto out;
  1370. err = move_addr_to_kernel(uservaddr, addrlen, &address);
  1371. if (err < 0)
  1372. goto out_put;
  1373. err =
  1374. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1375. if (err)
  1376. goto out_put;
  1377. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1378. sock->file->f_flags);
  1379. out_put:
  1380. fput_light(sock->file, fput_needed);
  1381. out:
  1382. return err;
  1383. }
  1384. /*
  1385. * Get the local address ('name') of a socket object. Move the obtained
  1386. * name to user space.
  1387. */
  1388. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1389. int __user *, usockaddr_len)
  1390. {
  1391. struct socket *sock;
  1392. struct sockaddr_storage address;
  1393. int len, err, fput_needed;
  1394. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1395. if (!sock)
  1396. goto out;
  1397. err = security_socket_getsockname(sock);
  1398. if (err)
  1399. goto out_put;
  1400. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1401. if (err)
  1402. goto out_put;
  1403. err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
  1404. out_put:
  1405. fput_light(sock->file, fput_needed);
  1406. out:
  1407. return err;
  1408. }
  1409. /*
  1410. * Get the remote address ('name') of a socket object. Move the obtained
  1411. * name to user space.
  1412. */
  1413. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1414. int __user *, usockaddr_len)
  1415. {
  1416. struct socket *sock;
  1417. struct sockaddr_storage address;
  1418. int len, err, fput_needed;
  1419. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1420. if (sock != NULL) {
  1421. err = security_socket_getpeername(sock);
  1422. if (err) {
  1423. fput_light(sock->file, fput_needed);
  1424. return err;
  1425. }
  1426. err =
  1427. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1428. 1);
  1429. if (!err)
  1430. err = move_addr_to_user(&address, len, usockaddr,
  1431. usockaddr_len);
  1432. fput_light(sock->file, fput_needed);
  1433. }
  1434. return err;
  1435. }
  1436. /*
  1437. * Send a datagram to a given address. We move the address into kernel
  1438. * space and check the user space data area is readable before invoking
  1439. * the protocol.
  1440. */
  1441. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1442. unsigned int, flags, struct sockaddr __user *, addr,
  1443. int, addr_len)
  1444. {
  1445. struct socket *sock;
  1446. struct sockaddr_storage address;
  1447. int err;
  1448. struct msghdr msg;
  1449. struct iovec iov;
  1450. int fput_needed;
  1451. if (len > INT_MAX)
  1452. len = INT_MAX;
  1453. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1454. if (!sock)
  1455. goto out;
  1456. iov.iov_base = buff;
  1457. iov.iov_len = len;
  1458. msg.msg_name = NULL;
  1459. msg.msg_iov = &iov;
  1460. msg.msg_iovlen = 1;
  1461. msg.msg_control = NULL;
  1462. msg.msg_controllen = 0;
  1463. msg.msg_namelen = 0;
  1464. if (addr) {
  1465. err = move_addr_to_kernel(addr, addr_len, &address);
  1466. if (err < 0)
  1467. goto out_put;
  1468. msg.msg_name = (struct sockaddr *)&address;
  1469. msg.msg_namelen = addr_len;
  1470. }
  1471. if (sock->file->f_flags & O_NONBLOCK)
  1472. flags |= MSG_DONTWAIT;
  1473. msg.msg_flags = flags;
  1474. err = sock_sendmsg(sock, &msg, len);
  1475. out_put:
  1476. fput_light(sock->file, fput_needed);
  1477. out:
  1478. return err;
  1479. }
  1480. /*
  1481. * Send a datagram down a socket.
  1482. */
  1483. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1484. unsigned int, flags)
  1485. {
  1486. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1487. }
  1488. /*
  1489. * Receive a frame from the socket and optionally record the address of the
  1490. * sender. We verify the buffers are writable and if needed move the
  1491. * sender address from kernel to user space.
  1492. */
  1493. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1494. unsigned int, flags, struct sockaddr __user *, addr,
  1495. int __user *, addr_len)
  1496. {
  1497. struct socket *sock;
  1498. struct iovec iov;
  1499. struct msghdr msg;
  1500. struct sockaddr_storage address;
  1501. int err, err2;
  1502. int fput_needed;
  1503. if (size > INT_MAX)
  1504. size = INT_MAX;
  1505. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1506. if (!sock)
  1507. goto out;
  1508. msg.msg_control = NULL;
  1509. msg.msg_controllen = 0;
  1510. msg.msg_iovlen = 1;
  1511. msg.msg_iov = &iov;
  1512. iov.iov_len = size;
  1513. iov.iov_base = ubuf;
  1514. msg.msg_name = (struct sockaddr *)&address;
  1515. msg.msg_namelen = sizeof(address);
  1516. if (sock->file->f_flags & O_NONBLOCK)
  1517. flags |= MSG_DONTWAIT;
  1518. err = sock_recvmsg(sock, &msg, size, flags);
  1519. if (err >= 0 && addr != NULL) {
  1520. err2 = move_addr_to_user(&address,
  1521. msg.msg_namelen, addr, addr_len);
  1522. if (err2 < 0)
  1523. err = err2;
  1524. }
  1525. fput_light(sock->file, fput_needed);
  1526. out:
  1527. return err;
  1528. }
  1529. /*
  1530. * Receive a datagram from a socket.
  1531. */
  1532. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1533. unsigned int flags)
  1534. {
  1535. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1536. }
  1537. /*
  1538. * Set a socket option. Because we don't know the option lengths we have
  1539. * to pass the user mode parameter for the protocols to sort out.
  1540. */
  1541. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1542. char __user *, optval, int, optlen)
  1543. {
  1544. int err, fput_needed;
  1545. struct socket *sock;
  1546. if (optlen < 0)
  1547. return -EINVAL;
  1548. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1549. if (sock != NULL) {
  1550. err = security_socket_setsockopt(sock, level, optname);
  1551. if (err)
  1552. goto out_put;
  1553. if (level == SOL_SOCKET)
  1554. err =
  1555. sock_setsockopt(sock, level, optname, optval,
  1556. optlen);
  1557. else
  1558. err =
  1559. sock->ops->setsockopt(sock, level, optname, optval,
  1560. optlen);
  1561. out_put:
  1562. fput_light(sock->file, fput_needed);
  1563. }
  1564. return err;
  1565. }
  1566. /*
  1567. * Get a socket option. Because we don't know the option lengths we have
  1568. * to pass a user mode parameter for the protocols to sort out.
  1569. */
  1570. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1571. char __user *, optval, int __user *, optlen)
  1572. {
  1573. int err, fput_needed;
  1574. struct socket *sock;
  1575. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1576. if (sock != NULL) {
  1577. err = security_socket_getsockopt(sock, level, optname);
  1578. if (err)
  1579. goto out_put;
  1580. if (level == SOL_SOCKET)
  1581. err =
  1582. sock_getsockopt(sock, level, optname, optval,
  1583. optlen);
  1584. else
  1585. err =
  1586. sock->ops->getsockopt(sock, level, optname, optval,
  1587. optlen);
  1588. out_put:
  1589. fput_light(sock->file, fput_needed);
  1590. }
  1591. return err;
  1592. }
  1593. /*
  1594. * Shutdown a socket.
  1595. */
  1596. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1597. {
  1598. int err, fput_needed;
  1599. struct socket *sock;
  1600. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1601. if (sock != NULL) {
  1602. err = security_socket_shutdown(sock, how);
  1603. if (!err)
  1604. err = sock->ops->shutdown(sock, how);
  1605. fput_light(sock->file, fput_needed);
  1606. }
  1607. return err;
  1608. }
  1609. /* A couple of helpful macros for getting the address of the 32/64 bit
  1610. * fields which are the same type (int / unsigned) on our platforms.
  1611. */
  1612. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1613. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1614. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1615. struct used_address {
  1616. struct sockaddr_storage name;
  1617. unsigned int name_len;
  1618. };
  1619. static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
  1620. struct msghdr *msg_sys, unsigned int flags,
  1621. struct used_address *used_address)
  1622. {
  1623. struct compat_msghdr __user *msg_compat =
  1624. (struct compat_msghdr __user *)msg;
  1625. struct sockaddr_storage address;
  1626. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1627. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1628. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1629. /* 20 is size of ipv6_pktinfo */
  1630. unsigned char *ctl_buf = ctl;
  1631. int err, ctl_len, total_len;
  1632. err = -EFAULT;
  1633. if (MSG_CMSG_COMPAT & flags) {
  1634. if (get_compat_msghdr(msg_sys, msg_compat))
  1635. return -EFAULT;
  1636. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1637. return -EFAULT;
  1638. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1639. err = -EMSGSIZE;
  1640. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1641. goto out;
  1642. err = -ENOMEM;
  1643. iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
  1644. GFP_KERNEL);
  1645. if (!iov)
  1646. goto out;
  1647. }
  1648. /* This will also move the address data into kernel space */
  1649. if (MSG_CMSG_COMPAT & flags) {
  1650. err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
  1651. } else
  1652. err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
  1653. if (err < 0)
  1654. goto out_freeiov;
  1655. total_len = err;
  1656. err = -ENOBUFS;
  1657. if (msg_sys->msg_controllen > INT_MAX)
  1658. goto out_freeiov;
  1659. ctl_len = msg_sys->msg_controllen;
  1660. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1661. err =
  1662. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1663. sizeof(ctl));
  1664. if (err)
  1665. goto out_freeiov;
  1666. ctl_buf = msg_sys->msg_control;
  1667. ctl_len = msg_sys->msg_controllen;
  1668. } else if (ctl_len) {
  1669. if (ctl_len > sizeof(ctl)) {
  1670. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1671. if (ctl_buf == NULL)
  1672. goto out_freeiov;
  1673. }
  1674. err = -EFAULT;
  1675. /*
  1676. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1677. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1678. * checking falls down on this.
  1679. */
  1680. if (copy_from_user(ctl_buf,
  1681. (void __user __force *)msg_sys->msg_control,
  1682. ctl_len))
  1683. goto out_freectl;
  1684. msg_sys->msg_control = ctl_buf;
  1685. }
  1686. msg_sys->msg_flags = flags;
  1687. if (sock->file->f_flags & O_NONBLOCK)
  1688. msg_sys->msg_flags |= MSG_DONTWAIT;
  1689. /*
  1690. * If this is sendmmsg() and current destination address is same as
  1691. * previously succeeded address, omit asking LSM's decision.
  1692. * used_address->name_len is initialized to UINT_MAX so that the first
  1693. * destination address never matches.
  1694. */
  1695. if (used_address && msg_sys->msg_name &&
  1696. used_address->name_len == msg_sys->msg_namelen &&
  1697. !memcmp(&used_address->name, msg_sys->msg_name,
  1698. used_address->name_len)) {
  1699. err = sock_sendmsg_nosec(sock, msg_sys, total_len);
  1700. goto out_freectl;
  1701. }
  1702. err = sock_sendmsg(sock, msg_sys, total_len);
  1703. /*
  1704. * If this is sendmmsg() and sending to current destination address was
  1705. * successful, remember it.
  1706. */
  1707. if (used_address && err >= 0) {
  1708. used_address->name_len = msg_sys->msg_namelen;
  1709. if (msg_sys->msg_name)
  1710. memcpy(&used_address->name, msg_sys->msg_name,
  1711. used_address->name_len);
  1712. }
  1713. out_freectl:
  1714. if (ctl_buf != ctl)
  1715. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1716. out_freeiov:
  1717. if (iov != iovstack)
  1718. kfree(iov);
  1719. out:
  1720. return err;
  1721. }
  1722. /*
  1723. * BSD sendmsg interface
  1724. */
  1725. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
  1726. {
  1727. int fput_needed, err;
  1728. struct msghdr msg_sys;
  1729. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1730. if (!sock)
  1731. goto out;
  1732. err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1733. fput_light(sock->file, fput_needed);
  1734. out:
  1735. return err;
  1736. }
  1737. /*
  1738. * Linux sendmmsg interface
  1739. */
  1740. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1741. unsigned int flags)
  1742. {
  1743. int fput_needed, err, datagrams;
  1744. struct socket *sock;
  1745. struct mmsghdr __user *entry;
  1746. struct compat_mmsghdr __user *compat_entry;
  1747. struct msghdr msg_sys;
  1748. struct used_address used_address;
  1749. if (vlen > UIO_MAXIOV)
  1750. vlen = UIO_MAXIOV;
  1751. datagrams = 0;
  1752. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1753. if (!sock)
  1754. return err;
  1755. used_address.name_len = UINT_MAX;
  1756. entry = mmsg;
  1757. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1758. err = 0;
  1759. while (datagrams < vlen) {
  1760. if (MSG_CMSG_COMPAT & flags) {
  1761. err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
  1762. &msg_sys, flags, &used_address);
  1763. if (err < 0)
  1764. break;
  1765. err = __put_user(err, &compat_entry->msg_len);
  1766. ++compat_entry;
  1767. } else {
  1768. err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
  1769. &msg_sys, flags, &used_address);
  1770. if (err < 0)
  1771. break;
  1772. err = put_user(err, &entry->msg_len);
  1773. ++entry;
  1774. }
  1775. if (err)
  1776. break;
  1777. ++datagrams;
  1778. }
  1779. fput_light(sock->file, fput_needed);
  1780. /* We only return an error if no datagrams were able to be sent */
  1781. if (datagrams != 0)
  1782. return datagrams;
  1783. return err;
  1784. }
  1785. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1786. unsigned int, vlen, unsigned int, flags)
  1787. {
  1788. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1789. }
  1790. static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1791. struct msghdr *msg_sys, unsigned int flags, int nosec)
  1792. {
  1793. struct compat_msghdr __user *msg_compat =
  1794. (struct compat_msghdr __user *)msg;
  1795. struct iovec iovstack[UIO_FASTIOV];
  1796. struct iovec *iov = iovstack;
  1797. unsigned long cmsg_ptr;
  1798. int err, total_len, len;
  1799. /* kernel mode address */
  1800. struct sockaddr_storage addr;
  1801. /* user mode address pointers */
  1802. struct sockaddr __user *uaddr;
  1803. int __user *uaddr_len;
  1804. if (MSG_CMSG_COMPAT & flags) {
  1805. if (get_compat_msghdr(msg_sys, msg_compat))
  1806. return -EFAULT;
  1807. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1808. return -EFAULT;
  1809. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1810. err = -EMSGSIZE;
  1811. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1812. goto out;
  1813. err = -ENOMEM;
  1814. iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
  1815. GFP_KERNEL);
  1816. if (!iov)
  1817. goto out;
  1818. }
  1819. /*
  1820. * Save the user-mode address (verify_iovec will change the
  1821. * kernel msghdr to use the kernel address space)
  1822. */
  1823. uaddr = (__force void __user *)msg_sys->msg_name;
  1824. uaddr_len = COMPAT_NAMELEN(msg);
  1825. if (MSG_CMSG_COMPAT & flags) {
  1826. err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1827. } else
  1828. err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
  1829. if (err < 0)
  1830. goto out_freeiov;
  1831. total_len = err;
  1832. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1833. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1834. if (sock->file->f_flags & O_NONBLOCK)
  1835. flags |= MSG_DONTWAIT;
  1836. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1837. total_len, flags);
  1838. if (err < 0)
  1839. goto out_freeiov;
  1840. len = err;
  1841. if (uaddr != NULL) {
  1842. err = move_addr_to_user(&addr,
  1843. msg_sys->msg_namelen, uaddr,
  1844. uaddr_len);
  1845. if (err < 0)
  1846. goto out_freeiov;
  1847. }
  1848. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1849. COMPAT_FLAGS(msg));
  1850. if (err)
  1851. goto out_freeiov;
  1852. if (MSG_CMSG_COMPAT & flags)
  1853. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1854. &msg_compat->msg_controllen);
  1855. else
  1856. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1857. &msg->msg_controllen);
  1858. if (err)
  1859. goto out_freeiov;
  1860. err = len;
  1861. out_freeiov:
  1862. if (iov != iovstack)
  1863. kfree(iov);
  1864. out:
  1865. return err;
  1866. }
  1867. /*
  1868. * BSD recvmsg interface
  1869. */
  1870. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1871. unsigned int, flags)
  1872. {
  1873. int fput_needed, err;
  1874. struct msghdr msg_sys;
  1875. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1876. if (!sock)
  1877. goto out;
  1878. err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1879. fput_light(sock->file, fput_needed);
  1880. out:
  1881. return err;
  1882. }
  1883. /*
  1884. * Linux recvmmsg interface
  1885. */
  1886. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1887. unsigned int flags, struct timespec *timeout)
  1888. {
  1889. int fput_needed, err, datagrams;
  1890. struct socket *sock;
  1891. struct mmsghdr __user *entry;
  1892. struct compat_mmsghdr __user *compat_entry;
  1893. struct msghdr msg_sys;
  1894. struct timespec end_time;
  1895. if (timeout &&
  1896. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1897. timeout->tv_nsec))
  1898. return -EINVAL;
  1899. datagrams = 0;
  1900. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1901. if (!sock)
  1902. return err;
  1903. err = sock_error(sock->sk);
  1904. if (err)
  1905. goto out_put;
  1906. entry = mmsg;
  1907. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1908. while (datagrams < vlen) {
  1909. /*
  1910. * No need to ask LSM for more than the first datagram.
  1911. */
  1912. if (MSG_CMSG_COMPAT & flags) {
  1913. err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  1914. &msg_sys, flags & ~MSG_WAITFORONE,
  1915. datagrams);
  1916. if (err < 0)
  1917. break;
  1918. err = __put_user(err, &compat_entry->msg_len);
  1919. ++compat_entry;
  1920. } else {
  1921. err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
  1922. &msg_sys, flags & ~MSG_WAITFORONE,
  1923. datagrams);
  1924. if (err < 0)
  1925. break;
  1926. err = put_user(err, &entry->msg_len);
  1927. ++entry;
  1928. }
  1929. if (err)
  1930. break;
  1931. ++datagrams;
  1932. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1933. if (flags & MSG_WAITFORONE)
  1934. flags |= MSG_DONTWAIT;
  1935. if (timeout) {
  1936. ktime_get_ts(timeout);
  1937. *timeout = timespec_sub(end_time, *timeout);
  1938. if (timeout->tv_sec < 0) {
  1939. timeout->tv_sec = timeout->tv_nsec = 0;
  1940. break;
  1941. }
  1942. /* Timeout, return less than vlen datagrams */
  1943. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1944. break;
  1945. }
  1946. /* Out of band data, return right away */
  1947. if (msg_sys.msg_flags & MSG_OOB)
  1948. break;
  1949. }
  1950. out_put:
  1951. fput_light(sock->file, fput_needed);
  1952. if (err == 0)
  1953. return datagrams;
  1954. if (datagrams != 0) {
  1955. /*
  1956. * We may return less entries than requested (vlen) if the
  1957. * sock is non block and there aren't enough datagrams...
  1958. */
  1959. if (err != -EAGAIN) {
  1960. /*
  1961. * ... or if recvmsg returns an error after we
  1962. * received some datagrams, where we record the
  1963. * error to return on the next call or if the
  1964. * app asks about it using getsockopt(SO_ERROR).
  1965. */
  1966. sock->sk->sk_err = -err;
  1967. }
  1968. return datagrams;
  1969. }
  1970. return err;
  1971. }
  1972. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1973. unsigned int, vlen, unsigned int, flags,
  1974. struct timespec __user *, timeout)
  1975. {
  1976. int datagrams;
  1977. struct timespec timeout_sys;
  1978. if (!timeout)
  1979. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1980. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1981. return -EFAULT;
  1982. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1983. if (datagrams > 0 &&
  1984. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1985. datagrams = -EFAULT;
  1986. return datagrams;
  1987. }
  1988. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1989. /* Argument list sizes for sys_socketcall */
  1990. #define AL(x) ((x) * sizeof(unsigned long))
  1991. static const unsigned char nargs[21] = {
  1992. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1993. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1994. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1995. AL(4), AL(5), AL(4)
  1996. };
  1997. #undef AL
  1998. /*
  1999. * System call vectors.
  2000. *
  2001. * Argument checking cleaned up. Saved 20% in size.
  2002. * This function doesn't need to set the kernel lock because
  2003. * it is set by the callees.
  2004. */
  2005. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2006. {
  2007. unsigned long a[6];
  2008. unsigned long a0, a1;
  2009. int err;
  2010. unsigned int len;
  2011. if (call < 1 || call > SYS_SENDMMSG)
  2012. return -EINVAL;
  2013. len = nargs[call];
  2014. if (len > sizeof(a))
  2015. return -EINVAL;
  2016. /* copy_from_user should be SMP safe. */
  2017. if (copy_from_user(a, args, len))
  2018. return -EFAULT;
  2019. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2020. a0 = a[0];
  2021. a1 = a[1];
  2022. switch (call) {
  2023. case SYS_SOCKET:
  2024. err = sys_socket(a0, a1, a[2]);
  2025. break;
  2026. case SYS_BIND:
  2027. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2028. break;
  2029. case SYS_CONNECT:
  2030. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2031. break;
  2032. case SYS_LISTEN:
  2033. err = sys_listen(a0, a1);
  2034. break;
  2035. case SYS_ACCEPT:
  2036. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2037. (int __user *)a[2], 0);
  2038. break;
  2039. case SYS_GETSOCKNAME:
  2040. err =
  2041. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2042. (int __user *)a[2]);
  2043. break;
  2044. case SYS_GETPEERNAME:
  2045. err =
  2046. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2047. (int __user *)a[2]);
  2048. break;
  2049. case SYS_SOCKETPAIR:
  2050. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2051. break;
  2052. case SYS_SEND:
  2053. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2054. break;
  2055. case SYS_SENDTO:
  2056. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2057. (struct sockaddr __user *)a[4], a[5]);
  2058. break;
  2059. case SYS_RECV:
  2060. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2061. break;
  2062. case SYS_RECVFROM:
  2063. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2064. (struct sockaddr __user *)a[4],
  2065. (int __user *)a[5]);
  2066. break;
  2067. case SYS_SHUTDOWN:
  2068. err = sys_shutdown(a0, a1);
  2069. break;
  2070. case SYS_SETSOCKOPT:
  2071. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2072. break;
  2073. case SYS_GETSOCKOPT:
  2074. err =
  2075. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2076. (int __user *)a[4]);
  2077. break;
  2078. case SYS_SENDMSG:
  2079. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  2080. break;
  2081. case SYS_SENDMMSG:
  2082. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2083. break;
  2084. case SYS_RECVMSG:
  2085. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  2086. break;
  2087. case SYS_RECVMMSG:
  2088. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2089. (struct timespec __user *)a[4]);
  2090. break;
  2091. case SYS_ACCEPT4:
  2092. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2093. (int __user *)a[2], a[3]);
  2094. break;
  2095. default:
  2096. err = -EINVAL;
  2097. break;
  2098. }
  2099. return err;
  2100. }
  2101. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2102. /**
  2103. * sock_register - add a socket protocol handler
  2104. * @ops: description of protocol
  2105. *
  2106. * This function is called by a protocol handler that wants to
  2107. * advertise its address family, and have it linked into the
  2108. * socket interface. The value ops->family coresponds to the
  2109. * socket system call protocol family.
  2110. */
  2111. int sock_register(const struct net_proto_family *ops)
  2112. {
  2113. int err;
  2114. if (ops->family >= NPROTO) {
  2115. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2116. NPROTO);
  2117. return -ENOBUFS;
  2118. }
  2119. spin_lock(&net_family_lock);
  2120. if (rcu_dereference_protected(net_families[ops->family],
  2121. lockdep_is_held(&net_family_lock)))
  2122. err = -EEXIST;
  2123. else {
  2124. rcu_assign_pointer(net_families[ops->family], ops);
  2125. err = 0;
  2126. }
  2127. spin_unlock(&net_family_lock);
  2128. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2129. return err;
  2130. }
  2131. EXPORT_SYMBOL(sock_register);
  2132. /**
  2133. * sock_unregister - remove a protocol handler
  2134. * @family: protocol family to remove
  2135. *
  2136. * This function is called by a protocol handler that wants to
  2137. * remove its address family, and have it unlinked from the
  2138. * new socket creation.
  2139. *
  2140. * If protocol handler is a module, then it can use module reference
  2141. * counts to protect against new references. If protocol handler is not
  2142. * a module then it needs to provide its own protection in
  2143. * the ops->create routine.
  2144. */
  2145. void sock_unregister(int family)
  2146. {
  2147. BUG_ON(family < 0 || family >= NPROTO);
  2148. spin_lock(&net_family_lock);
  2149. RCU_INIT_POINTER(net_families[family], NULL);
  2150. spin_unlock(&net_family_lock);
  2151. synchronize_rcu();
  2152. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2153. }
  2154. EXPORT_SYMBOL(sock_unregister);
  2155. static int __init sock_init(void)
  2156. {
  2157. int err;
  2158. /*
  2159. * Initialize the network sysctl infrastructure.
  2160. */
  2161. err = net_sysctl_init();
  2162. if (err)
  2163. goto out;
  2164. /*
  2165. * Initialize sock SLAB cache.
  2166. */
  2167. sk_init();
  2168. /*
  2169. * Initialize skbuff SLAB cache
  2170. */
  2171. skb_init();
  2172. /*
  2173. * Initialize the protocols module.
  2174. */
  2175. init_inodecache();
  2176. err = register_filesystem(&sock_fs_type);
  2177. if (err)
  2178. goto out_fs;
  2179. sock_mnt = kern_mount(&sock_fs_type);
  2180. if (IS_ERR(sock_mnt)) {
  2181. err = PTR_ERR(sock_mnt);
  2182. goto out_mount;
  2183. }
  2184. /* The real protocol initialization is performed in later initcalls.
  2185. */
  2186. #ifdef CONFIG_NETFILTER
  2187. netfilter_init();
  2188. #endif
  2189. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2190. skb_timestamping_init();
  2191. #endif
  2192. out:
  2193. return err;
  2194. out_mount:
  2195. unregister_filesystem(&sock_fs_type);
  2196. out_fs:
  2197. goto out;
  2198. }
  2199. core_initcall(sock_init); /* early initcall */
  2200. #ifdef CONFIG_PROC_FS
  2201. void socket_seq_show(struct seq_file *seq)
  2202. {
  2203. int cpu;
  2204. int counter = 0;
  2205. for_each_possible_cpu(cpu)
  2206. counter += per_cpu(sockets_in_use, cpu);
  2207. /* It can be negative, by the way. 8) */
  2208. if (counter < 0)
  2209. counter = 0;
  2210. seq_printf(seq, "sockets: used %d\n", counter);
  2211. }
  2212. #endif /* CONFIG_PROC_FS */
  2213. #ifdef CONFIG_COMPAT
  2214. static int do_siocgstamp(struct net *net, struct socket *sock,
  2215. unsigned int cmd, void __user *up)
  2216. {
  2217. mm_segment_t old_fs = get_fs();
  2218. struct timeval ktv;
  2219. int err;
  2220. set_fs(KERNEL_DS);
  2221. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2222. set_fs(old_fs);
  2223. if (!err)
  2224. err = compat_put_timeval(up, &ktv);
  2225. return err;
  2226. }
  2227. static int do_siocgstampns(struct net *net, struct socket *sock,
  2228. unsigned int cmd, void __user *up)
  2229. {
  2230. mm_segment_t old_fs = get_fs();
  2231. struct timespec kts;
  2232. int err;
  2233. set_fs(KERNEL_DS);
  2234. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2235. set_fs(old_fs);
  2236. if (!err)
  2237. err = compat_put_timespec(up, &kts);
  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 int 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);