socket.c 80 KB

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