socket.c 85 KB

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