socket.c 81 KB

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