socket.c 84 KB

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