socket.c 85 KB

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