socket.c 79 KB

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