socket.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  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. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  102. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  103. unsigned long nr_segs, loff_t pos);
  104. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  105. unsigned long nr_segs, loff_t pos);
  106. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  107. static int sock_close(struct inode *inode, struct file *file);
  108. static unsigned int sock_poll(struct file *file,
  109. struct poll_table_struct *wait);
  110. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  111. #ifdef CONFIG_COMPAT
  112. static long compat_sock_ioctl(struct file *file,
  113. unsigned int cmd, unsigned long arg);
  114. #endif
  115. static int sock_fasync(int fd, struct file *filp, int on);
  116. static ssize_t sock_sendpage(struct file *file, struct page *page,
  117. int offset, size_t size, loff_t *ppos, int more);
  118. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  119. struct pipe_inode_info *pipe, size_t len,
  120. unsigned int flags);
  121. /*
  122. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  123. * in the operation structures but are done directly via the socketcall() multiplexor.
  124. */
  125. static const struct file_operations socket_file_ops = {
  126. .owner = THIS_MODULE,
  127. .llseek = no_llseek,
  128. .aio_read = sock_aio_read,
  129. .aio_write = sock_aio_write,
  130. .poll = sock_poll,
  131. .unlocked_ioctl = sock_ioctl,
  132. #ifdef CONFIG_COMPAT
  133. .compat_ioctl = compat_sock_ioctl,
  134. #endif
  135. .mmap = sock_mmap,
  136. .open = sock_no_open, /* special open code to disallow open via /proc */
  137. .release = sock_close,
  138. .fasync = sock_fasync,
  139. .sendpage = sock_sendpage,
  140. .splice_write = generic_splice_sendpage,
  141. .splice_read = sock_splice_read,
  142. };
  143. /*
  144. * The protocol list. Each protocol is registered in here.
  145. */
  146. static DEFINE_SPINLOCK(net_family_lock);
  147. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  148. /*
  149. * Statistics counters of the socket lists
  150. */
  151. static DEFINE_PER_CPU(int, sockets_in_use);
  152. /*
  153. * Support routines.
  154. * Move socket addresses back and forth across the kernel/user
  155. * divide and look after the messy bits.
  156. */
  157. /**
  158. * move_addr_to_kernel - copy a socket address into kernel space
  159. * @uaddr: Address in user space
  160. * @kaddr: Address in kernel space
  161. * @ulen: Length in user space
  162. *
  163. * The address is copied into kernel space. If the provided address is
  164. * too long an error code of -EINVAL is returned. If the copy gives
  165. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  166. */
  167. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  168. {
  169. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  170. return -EINVAL;
  171. if (ulen == 0)
  172. return 0;
  173. if (copy_from_user(kaddr, uaddr, ulen))
  174. return -EFAULT;
  175. return audit_sockaddr(ulen, kaddr);
  176. }
  177. /**
  178. * move_addr_to_user - copy an address to user space
  179. * @kaddr: kernel space address
  180. * @klen: length of address in kernel
  181. * @uaddr: user space address
  182. * @ulen: pointer to user length field
  183. *
  184. * The value pointed to by ulen on entry is the buffer length available.
  185. * This is overwritten with the buffer space used. -EINVAL is returned
  186. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  187. * is returned if either the buffer or the length field are not
  188. * accessible.
  189. * After copying the data up to the limit the user specifies, the true
  190. * length of the data is written over the length limit the user
  191. * specified. Zero is returned for a success.
  192. */
  193. static int move_addr_to_user(struct sockaddr *kaddr, int klen,
  194. void __user *uaddr, int __user *ulen)
  195. {
  196. int err;
  197. int len;
  198. err = get_user(len, ulen);
  199. if (err)
  200. return err;
  201. if (len > klen)
  202. len = klen;
  203. if (len < 0 || len > sizeof(struct sockaddr_storage))
  204. return -EINVAL;
  205. if (len) {
  206. if (audit_sockaddr(klen, kaddr))
  207. return -ENOMEM;
  208. if (copy_to_user(uaddr, kaddr, len))
  209. return -EFAULT;
  210. }
  211. /*
  212. * "fromlen shall refer to the value before truncation.."
  213. * 1003.1g
  214. */
  215. return __put_user(klen, ulen);
  216. }
  217. static struct kmem_cache *sock_inode_cachep __read_mostly;
  218. static struct inode *sock_alloc_inode(struct super_block *sb)
  219. {
  220. struct socket_alloc *ei;
  221. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  222. if (!ei)
  223. return NULL;
  224. ei->socket.wq = kmalloc(sizeof(struct socket_wq), GFP_KERNEL);
  225. if (!ei->socket.wq) {
  226. kmem_cache_free(sock_inode_cachep, ei);
  227. return NULL;
  228. }
  229. init_waitqueue_head(&ei->socket.wq->wait);
  230. ei->socket.wq->fasync_list = NULL;
  231. ei->socket.state = SS_UNCONNECTED;
  232. ei->socket.flags = 0;
  233. ei->socket.ops = NULL;
  234. ei->socket.sk = NULL;
  235. ei->socket.file = NULL;
  236. return &ei->vfs_inode;
  237. }
  238. static void wq_free_rcu(struct rcu_head *head)
  239. {
  240. struct socket_wq *wq = container_of(head, struct socket_wq, rcu);
  241. kfree(wq);
  242. }
  243. static void sock_destroy_inode(struct inode *inode)
  244. {
  245. struct socket_alloc *ei;
  246. ei = container_of(inode, struct socket_alloc, vfs_inode);
  247. call_rcu(&ei->socket.wq->rcu, wq_free_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 (sock->wq->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. if (sk == NULL)
  951. return -EINVAL;
  952. lock_sock(sk);
  953. fasync_helper(fd, filp, on, &sock->wq->fasync_list);
  954. if (!sock->wq->fasync_list)
  955. sock_reset_flag(sk, SOCK_FASYNC);
  956. else
  957. sock_set_flag(sk, SOCK_FASYNC);
  958. release_sock(sk);
  959. return 0;
  960. }
  961. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  962. int sock_wake_async(struct socket *sock, int how, int band)
  963. {
  964. struct socket_wq *wq;
  965. if (!sock)
  966. return -1;
  967. rcu_read_lock();
  968. wq = rcu_dereference(sock->wq);
  969. if (!wq || !wq->fasync_list) {
  970. rcu_read_unlock();
  971. return -1;
  972. }
  973. switch (how) {
  974. case SOCK_WAKE_WAITD:
  975. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  976. break;
  977. goto call_kill;
  978. case SOCK_WAKE_SPACE:
  979. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  980. break;
  981. /* fall through */
  982. case SOCK_WAKE_IO:
  983. call_kill:
  984. kill_fasync(&wq->fasync_list, SIGIO, band);
  985. break;
  986. case SOCK_WAKE_URG:
  987. kill_fasync(&wq->fasync_list, SIGURG, band);
  988. }
  989. rcu_read_unlock();
  990. return 0;
  991. }
  992. EXPORT_SYMBOL(sock_wake_async);
  993. int __sock_create(struct net *net, int family, int type, int protocol,
  994. struct socket **res, int kern)
  995. {
  996. int err;
  997. struct socket *sock;
  998. const struct net_proto_family *pf;
  999. /*
  1000. * Check protocol is in range
  1001. */
  1002. if (family < 0 || family >= NPROTO)
  1003. return -EAFNOSUPPORT;
  1004. if (type < 0 || type >= SOCK_MAX)
  1005. return -EINVAL;
  1006. /* Compatibility.
  1007. This uglymoron is moved from INET layer to here to avoid
  1008. deadlock in module load.
  1009. */
  1010. if (family == PF_INET && type == SOCK_PACKET) {
  1011. static int warned;
  1012. if (!warned) {
  1013. warned = 1;
  1014. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1015. current->comm);
  1016. }
  1017. family = PF_PACKET;
  1018. }
  1019. err = security_socket_create(family, type, protocol, kern);
  1020. if (err)
  1021. return err;
  1022. /*
  1023. * Allocate the socket and allow the family to set things up. if
  1024. * the protocol is 0, the family is instructed to select an appropriate
  1025. * default.
  1026. */
  1027. sock = sock_alloc();
  1028. if (!sock) {
  1029. if (net_ratelimit())
  1030. printk(KERN_WARNING "socket: no more sockets\n");
  1031. return -ENFILE; /* Not exactly a match, but its the
  1032. closest posix thing */
  1033. }
  1034. sock->type = type;
  1035. #ifdef CONFIG_MODULES
  1036. /* Attempt to load a protocol module if the find failed.
  1037. *
  1038. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1039. * requested real, full-featured networking support upon configuration.
  1040. * Otherwise module support will break!
  1041. */
  1042. if (rcu_access_pointer(net_families[family]) == NULL)
  1043. request_module("net-pf-%d", family);
  1044. #endif
  1045. rcu_read_lock();
  1046. pf = rcu_dereference(net_families[family]);
  1047. err = -EAFNOSUPPORT;
  1048. if (!pf)
  1049. goto out_release;
  1050. /*
  1051. * We will call the ->create function, that possibly is in a loadable
  1052. * module, so we have to bump that loadable module refcnt first.
  1053. */
  1054. if (!try_module_get(pf->owner))
  1055. goto out_release;
  1056. /* Now protected by module ref count */
  1057. rcu_read_unlock();
  1058. err = pf->create(net, sock, protocol, kern);
  1059. if (err < 0)
  1060. goto out_module_put;
  1061. /*
  1062. * Now to bump the refcnt of the [loadable] module that owns this
  1063. * socket at sock_release time we decrement its refcnt.
  1064. */
  1065. if (!try_module_get(sock->ops->owner))
  1066. goto out_module_busy;
  1067. /*
  1068. * Now that we're done with the ->create function, the [loadable]
  1069. * module can have its refcnt decremented
  1070. */
  1071. module_put(pf->owner);
  1072. err = security_socket_post_create(sock, family, type, protocol, kern);
  1073. if (err)
  1074. goto out_sock_release;
  1075. *res = sock;
  1076. return 0;
  1077. out_module_busy:
  1078. err = -EAFNOSUPPORT;
  1079. out_module_put:
  1080. sock->ops = NULL;
  1081. module_put(pf->owner);
  1082. out_sock_release:
  1083. sock_release(sock);
  1084. return err;
  1085. out_release:
  1086. rcu_read_unlock();
  1087. goto out_sock_release;
  1088. }
  1089. EXPORT_SYMBOL(__sock_create);
  1090. int sock_create(int family, int type, int protocol, struct socket **res)
  1091. {
  1092. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1093. }
  1094. EXPORT_SYMBOL(sock_create);
  1095. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1096. {
  1097. return __sock_create(&init_net, family, type, protocol, res, 1);
  1098. }
  1099. EXPORT_SYMBOL(sock_create_kern);
  1100. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1101. {
  1102. int retval;
  1103. struct socket *sock;
  1104. int flags;
  1105. /* Check the SOCK_* constants for consistency. */
  1106. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1107. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1108. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1109. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1110. flags = type & ~SOCK_TYPE_MASK;
  1111. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1112. return -EINVAL;
  1113. type &= SOCK_TYPE_MASK;
  1114. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1115. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1116. retval = sock_create(family, type, protocol, &sock);
  1117. if (retval < 0)
  1118. goto out;
  1119. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1120. if (retval < 0)
  1121. goto out_release;
  1122. out:
  1123. /* It may be already another descriptor 8) Not kernel problem. */
  1124. return retval;
  1125. out_release:
  1126. sock_release(sock);
  1127. return retval;
  1128. }
  1129. /*
  1130. * Create a pair of connected sockets.
  1131. */
  1132. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1133. int __user *, usockvec)
  1134. {
  1135. struct socket *sock1, *sock2;
  1136. int fd1, fd2, err;
  1137. struct file *newfile1, *newfile2;
  1138. int flags;
  1139. flags = type & ~SOCK_TYPE_MASK;
  1140. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1141. return -EINVAL;
  1142. type &= SOCK_TYPE_MASK;
  1143. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1144. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1145. /*
  1146. * Obtain the first socket and check if the underlying protocol
  1147. * supports the socketpair call.
  1148. */
  1149. err = sock_create(family, type, protocol, &sock1);
  1150. if (err < 0)
  1151. goto out;
  1152. err = sock_create(family, type, protocol, &sock2);
  1153. if (err < 0)
  1154. goto out_release_1;
  1155. err = sock1->ops->socketpair(sock1, sock2);
  1156. if (err < 0)
  1157. goto out_release_both;
  1158. fd1 = sock_alloc_file(sock1, &newfile1, flags);
  1159. if (unlikely(fd1 < 0)) {
  1160. err = fd1;
  1161. goto out_release_both;
  1162. }
  1163. fd2 = sock_alloc_file(sock2, &newfile2, flags);
  1164. if (unlikely(fd2 < 0)) {
  1165. err = fd2;
  1166. fput(newfile1);
  1167. put_unused_fd(fd1);
  1168. sock_release(sock2);
  1169. goto out;
  1170. }
  1171. audit_fd_pair(fd1, fd2);
  1172. fd_install(fd1, newfile1);
  1173. fd_install(fd2, newfile2);
  1174. /* fd1 and fd2 may be already another descriptors.
  1175. * Not kernel problem.
  1176. */
  1177. err = put_user(fd1, &usockvec[0]);
  1178. if (!err)
  1179. err = put_user(fd2, &usockvec[1]);
  1180. if (!err)
  1181. return 0;
  1182. sys_close(fd2);
  1183. sys_close(fd1);
  1184. return err;
  1185. out_release_both:
  1186. sock_release(sock2);
  1187. out_release_1:
  1188. sock_release(sock1);
  1189. out:
  1190. return err;
  1191. }
  1192. /*
  1193. * Bind a name to a socket. Nothing much to do here since it's
  1194. * the protocol's responsibility to handle the local address.
  1195. *
  1196. * We move the socket address to kernel space before we call
  1197. * the protocol layer (having also checked the address is ok).
  1198. */
  1199. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1200. {
  1201. struct socket *sock;
  1202. struct sockaddr_storage address;
  1203. int err, fput_needed;
  1204. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1205. if (sock) {
  1206. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1207. if (err >= 0) {
  1208. err = security_socket_bind(sock,
  1209. (struct sockaddr *)&address,
  1210. addrlen);
  1211. if (!err)
  1212. err = sock->ops->bind(sock,
  1213. (struct sockaddr *)
  1214. &address, addrlen);
  1215. }
  1216. fput_light(sock->file, fput_needed);
  1217. }
  1218. return err;
  1219. }
  1220. /*
  1221. * Perform a listen. Basically, we allow the protocol to do anything
  1222. * necessary for a listen, and if that works, we mark the socket as
  1223. * ready for listening.
  1224. */
  1225. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1226. {
  1227. struct socket *sock;
  1228. int err, fput_needed;
  1229. int somaxconn;
  1230. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1231. if (sock) {
  1232. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1233. if ((unsigned)backlog > somaxconn)
  1234. backlog = somaxconn;
  1235. err = security_socket_listen(sock, backlog);
  1236. if (!err)
  1237. err = sock->ops->listen(sock, backlog);
  1238. fput_light(sock->file, fput_needed);
  1239. }
  1240. return err;
  1241. }
  1242. /*
  1243. * For accept, we attempt to create a new socket, set up the link
  1244. * with the client, wake up the client, then return the new
  1245. * connected fd. We collect the address of the connector in kernel
  1246. * space and move it to user at the very end. This is unclean because
  1247. * we open the socket then return an error.
  1248. *
  1249. * 1003.1g adds the ability to recvmsg() to query connection pending
  1250. * status to recvmsg. We need to add that support in a way thats
  1251. * clean when we restucture accept also.
  1252. */
  1253. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1254. int __user *, upeer_addrlen, int, flags)
  1255. {
  1256. struct socket *sock, *newsock;
  1257. struct file *newfile;
  1258. int err, len, newfd, fput_needed;
  1259. struct sockaddr_storage address;
  1260. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1261. return -EINVAL;
  1262. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1263. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1264. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1265. if (!sock)
  1266. goto out;
  1267. err = -ENFILE;
  1268. newsock = sock_alloc();
  1269. if (!newsock)
  1270. goto out_put;
  1271. newsock->type = sock->type;
  1272. newsock->ops = sock->ops;
  1273. /*
  1274. * We don't need try_module_get here, as the listening socket (sock)
  1275. * has the protocol module (sock->ops->owner) held.
  1276. */
  1277. __module_get(newsock->ops->owner);
  1278. newfd = sock_alloc_file(newsock, &newfile, flags);
  1279. if (unlikely(newfd < 0)) {
  1280. err = newfd;
  1281. sock_release(newsock);
  1282. goto out_put;
  1283. }
  1284. err = security_socket_accept(sock, newsock);
  1285. if (err)
  1286. goto out_fd;
  1287. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1288. if (err < 0)
  1289. goto out_fd;
  1290. if (upeer_sockaddr) {
  1291. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1292. &len, 2) < 0) {
  1293. err = -ECONNABORTED;
  1294. goto out_fd;
  1295. }
  1296. err = move_addr_to_user((struct sockaddr *)&address,
  1297. len, upeer_sockaddr, upeer_addrlen);
  1298. if (err < 0)
  1299. goto out_fd;
  1300. }
  1301. /* File flags are not inherited via accept() unlike another OSes. */
  1302. fd_install(newfd, newfile);
  1303. err = newfd;
  1304. out_put:
  1305. fput_light(sock->file, fput_needed);
  1306. out:
  1307. return err;
  1308. out_fd:
  1309. fput(newfile);
  1310. put_unused_fd(newfd);
  1311. goto out_put;
  1312. }
  1313. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1314. int __user *, upeer_addrlen)
  1315. {
  1316. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1317. }
  1318. /*
  1319. * Attempt to connect to a socket with the server address. The address
  1320. * is in user space so we verify it is OK and move it to kernel space.
  1321. *
  1322. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1323. * break bindings
  1324. *
  1325. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1326. * other SEQPACKET protocols that take time to connect() as it doesn't
  1327. * include the -EINPROGRESS status for such sockets.
  1328. */
  1329. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1330. int, addrlen)
  1331. {
  1332. struct socket *sock;
  1333. struct sockaddr_storage address;
  1334. int err, fput_needed;
  1335. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1336. if (!sock)
  1337. goto out;
  1338. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1339. if (err < 0)
  1340. goto out_put;
  1341. err =
  1342. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1343. if (err)
  1344. goto out_put;
  1345. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1346. sock->file->f_flags);
  1347. out_put:
  1348. fput_light(sock->file, fput_needed);
  1349. out:
  1350. return err;
  1351. }
  1352. /*
  1353. * Get the local address ('name') of a socket object. Move the obtained
  1354. * name to user space.
  1355. */
  1356. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1357. int __user *, usockaddr_len)
  1358. {
  1359. struct socket *sock;
  1360. struct sockaddr_storage address;
  1361. int len, err, fput_needed;
  1362. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1363. if (!sock)
  1364. goto out;
  1365. err = security_socket_getsockname(sock);
  1366. if (err)
  1367. goto out_put;
  1368. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1369. if (err)
  1370. goto out_put;
  1371. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1372. out_put:
  1373. fput_light(sock->file, fput_needed);
  1374. out:
  1375. return err;
  1376. }
  1377. /*
  1378. * Get the remote address ('name') of a socket object. Move the obtained
  1379. * name to user space.
  1380. */
  1381. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1382. int __user *, usockaddr_len)
  1383. {
  1384. struct socket *sock;
  1385. struct sockaddr_storage address;
  1386. int len, err, fput_needed;
  1387. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1388. if (sock != NULL) {
  1389. err = security_socket_getpeername(sock);
  1390. if (err) {
  1391. fput_light(sock->file, fput_needed);
  1392. return err;
  1393. }
  1394. err =
  1395. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1396. 1);
  1397. if (!err)
  1398. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1399. usockaddr_len);
  1400. fput_light(sock->file, fput_needed);
  1401. }
  1402. return err;
  1403. }
  1404. /*
  1405. * Send a datagram to a given address. We move the address into kernel
  1406. * space and check the user space data area is readable before invoking
  1407. * the protocol.
  1408. */
  1409. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1410. unsigned, flags, struct sockaddr __user *, addr,
  1411. int, addr_len)
  1412. {
  1413. struct socket *sock;
  1414. struct sockaddr_storage address;
  1415. int err;
  1416. struct msghdr msg;
  1417. struct iovec iov;
  1418. int fput_needed;
  1419. if (len > INT_MAX)
  1420. len = INT_MAX;
  1421. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1422. if (!sock)
  1423. goto out;
  1424. iov.iov_base = buff;
  1425. iov.iov_len = len;
  1426. msg.msg_name = NULL;
  1427. msg.msg_iov = &iov;
  1428. msg.msg_iovlen = 1;
  1429. msg.msg_control = NULL;
  1430. msg.msg_controllen = 0;
  1431. msg.msg_namelen = 0;
  1432. if (addr) {
  1433. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1434. if (err < 0)
  1435. goto out_put;
  1436. msg.msg_name = (struct sockaddr *)&address;
  1437. msg.msg_namelen = addr_len;
  1438. }
  1439. if (sock->file->f_flags & O_NONBLOCK)
  1440. flags |= MSG_DONTWAIT;
  1441. msg.msg_flags = flags;
  1442. err = sock_sendmsg(sock, &msg, len);
  1443. out_put:
  1444. fput_light(sock->file, fput_needed);
  1445. out:
  1446. return err;
  1447. }
  1448. /*
  1449. * Send a datagram down a socket.
  1450. */
  1451. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1452. unsigned, flags)
  1453. {
  1454. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1455. }
  1456. /*
  1457. * Receive a frame from the socket and optionally record the address of the
  1458. * sender. We verify the buffers are writable and if needed move the
  1459. * sender address from kernel to user space.
  1460. */
  1461. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1462. unsigned, flags, struct sockaddr __user *, addr,
  1463. int __user *, addr_len)
  1464. {
  1465. struct socket *sock;
  1466. struct iovec iov;
  1467. struct msghdr msg;
  1468. struct sockaddr_storage address;
  1469. int err, err2;
  1470. int fput_needed;
  1471. if (size > INT_MAX)
  1472. size = INT_MAX;
  1473. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1474. if (!sock)
  1475. goto out;
  1476. msg.msg_control = NULL;
  1477. msg.msg_controllen = 0;
  1478. msg.msg_iovlen = 1;
  1479. msg.msg_iov = &iov;
  1480. iov.iov_len = size;
  1481. iov.iov_base = ubuf;
  1482. msg.msg_name = (struct sockaddr *)&address;
  1483. msg.msg_namelen = sizeof(address);
  1484. if (sock->file->f_flags & O_NONBLOCK)
  1485. flags |= MSG_DONTWAIT;
  1486. err = sock_recvmsg(sock, &msg, size, flags);
  1487. if (err >= 0 && addr != NULL) {
  1488. err2 = move_addr_to_user((struct sockaddr *)&address,
  1489. msg.msg_namelen, addr, addr_len);
  1490. if (err2 < 0)
  1491. err = err2;
  1492. }
  1493. fput_light(sock->file, fput_needed);
  1494. out:
  1495. return err;
  1496. }
  1497. /*
  1498. * Receive a datagram from a socket.
  1499. */
  1500. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1501. unsigned flags)
  1502. {
  1503. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1504. }
  1505. /*
  1506. * Set a socket option. Because we don't know the option lengths we have
  1507. * to pass the user mode parameter for the protocols to sort out.
  1508. */
  1509. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1510. char __user *, optval, int, optlen)
  1511. {
  1512. int err, fput_needed;
  1513. struct socket *sock;
  1514. if (optlen < 0)
  1515. return -EINVAL;
  1516. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1517. if (sock != NULL) {
  1518. err = security_socket_setsockopt(sock, level, optname);
  1519. if (err)
  1520. goto out_put;
  1521. if (level == SOL_SOCKET)
  1522. err =
  1523. sock_setsockopt(sock, level, optname, optval,
  1524. optlen);
  1525. else
  1526. err =
  1527. sock->ops->setsockopt(sock, level, optname, optval,
  1528. optlen);
  1529. out_put:
  1530. fput_light(sock->file, fput_needed);
  1531. }
  1532. return err;
  1533. }
  1534. /*
  1535. * Get a socket option. Because we don't know the option lengths we have
  1536. * to pass a user mode parameter for the protocols to sort out.
  1537. */
  1538. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1539. char __user *, optval, int __user *, optlen)
  1540. {
  1541. int err, fput_needed;
  1542. struct socket *sock;
  1543. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1544. if (sock != NULL) {
  1545. err = security_socket_getsockopt(sock, level, optname);
  1546. if (err)
  1547. goto out_put;
  1548. if (level == SOL_SOCKET)
  1549. err =
  1550. sock_getsockopt(sock, level, optname, optval,
  1551. optlen);
  1552. else
  1553. err =
  1554. sock->ops->getsockopt(sock, level, optname, optval,
  1555. optlen);
  1556. out_put:
  1557. fput_light(sock->file, fput_needed);
  1558. }
  1559. return err;
  1560. }
  1561. /*
  1562. * Shutdown a socket.
  1563. */
  1564. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1565. {
  1566. int err, fput_needed;
  1567. struct socket *sock;
  1568. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1569. if (sock != NULL) {
  1570. err = security_socket_shutdown(sock, how);
  1571. if (!err)
  1572. err = sock->ops->shutdown(sock, how);
  1573. fput_light(sock->file, fput_needed);
  1574. }
  1575. return err;
  1576. }
  1577. /* A couple of helpful macros for getting the address of the 32/64 bit
  1578. * fields which are the same type (int / unsigned) on our platforms.
  1579. */
  1580. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1581. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1582. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1583. /*
  1584. * BSD sendmsg interface
  1585. */
  1586. SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
  1587. {
  1588. struct compat_msghdr __user *msg_compat =
  1589. (struct compat_msghdr __user *)msg;
  1590. struct socket *sock;
  1591. struct sockaddr_storage address;
  1592. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1593. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1594. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1595. /* 20 is size of ipv6_pktinfo */
  1596. unsigned char *ctl_buf = ctl;
  1597. struct msghdr msg_sys;
  1598. int err, ctl_len, iov_size, total_len;
  1599. int fput_needed;
  1600. err = -EFAULT;
  1601. if (MSG_CMSG_COMPAT & flags) {
  1602. if (get_compat_msghdr(&msg_sys, msg_compat))
  1603. return -EFAULT;
  1604. } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1605. return -EFAULT;
  1606. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1607. if (!sock)
  1608. goto out;
  1609. /* do not move before msg_sys is valid */
  1610. err = -EMSGSIZE;
  1611. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1612. goto out_put;
  1613. /* Check whether to allocate the iovec area */
  1614. err = -ENOMEM;
  1615. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1616. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1617. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1618. if (!iov)
  1619. goto out_put;
  1620. }
  1621. /* This will also move the address data into kernel space */
  1622. if (MSG_CMSG_COMPAT & flags) {
  1623. err = verify_compat_iovec(&msg_sys, iov,
  1624. (struct sockaddr *)&address,
  1625. VERIFY_READ);
  1626. } else
  1627. err = verify_iovec(&msg_sys, iov,
  1628. (struct sockaddr *)&address,
  1629. VERIFY_READ);
  1630. if (err < 0)
  1631. goto out_freeiov;
  1632. total_len = err;
  1633. err = -ENOBUFS;
  1634. if (msg_sys.msg_controllen > INT_MAX)
  1635. goto out_freeiov;
  1636. ctl_len = msg_sys.msg_controllen;
  1637. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1638. err =
  1639. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1640. sizeof(ctl));
  1641. if (err)
  1642. goto out_freeiov;
  1643. ctl_buf = msg_sys.msg_control;
  1644. ctl_len = msg_sys.msg_controllen;
  1645. } else if (ctl_len) {
  1646. if (ctl_len > sizeof(ctl)) {
  1647. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1648. if (ctl_buf == NULL)
  1649. goto out_freeiov;
  1650. }
  1651. err = -EFAULT;
  1652. /*
  1653. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1654. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1655. * checking falls down on this.
  1656. */
  1657. if (copy_from_user(ctl_buf,
  1658. (void __user __force *)msg_sys.msg_control,
  1659. ctl_len))
  1660. goto out_freectl;
  1661. msg_sys.msg_control = ctl_buf;
  1662. }
  1663. msg_sys.msg_flags = flags;
  1664. if (sock->file->f_flags & O_NONBLOCK)
  1665. msg_sys.msg_flags |= MSG_DONTWAIT;
  1666. err = sock_sendmsg(sock, &msg_sys, total_len);
  1667. out_freectl:
  1668. if (ctl_buf != ctl)
  1669. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1670. out_freeiov:
  1671. if (iov != iovstack)
  1672. sock_kfree_s(sock->sk, iov, iov_size);
  1673. out_put:
  1674. fput_light(sock->file, fput_needed);
  1675. out:
  1676. return err;
  1677. }
  1678. static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
  1679. struct msghdr *msg_sys, unsigned flags, int nosec)
  1680. {
  1681. struct compat_msghdr __user *msg_compat =
  1682. (struct compat_msghdr __user *)msg;
  1683. struct iovec iovstack[UIO_FASTIOV];
  1684. struct iovec *iov = iovstack;
  1685. unsigned long cmsg_ptr;
  1686. int err, iov_size, total_len, len;
  1687. /* kernel mode address */
  1688. struct sockaddr_storage addr;
  1689. /* user mode address pointers */
  1690. struct sockaddr __user *uaddr;
  1691. int __user *uaddr_len;
  1692. if (MSG_CMSG_COMPAT & flags) {
  1693. if (get_compat_msghdr(msg_sys, msg_compat))
  1694. return -EFAULT;
  1695. } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
  1696. return -EFAULT;
  1697. err = -EMSGSIZE;
  1698. if (msg_sys->msg_iovlen > UIO_MAXIOV)
  1699. goto out;
  1700. /* Check whether to allocate the iovec area */
  1701. err = -ENOMEM;
  1702. iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
  1703. if (msg_sys->msg_iovlen > UIO_FASTIOV) {
  1704. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1705. if (!iov)
  1706. goto out;
  1707. }
  1708. /*
  1709. * Save the user-mode address (verify_iovec will change the
  1710. * kernel msghdr to use the kernel address space)
  1711. */
  1712. uaddr = (__force void __user *)msg_sys->msg_name;
  1713. uaddr_len = COMPAT_NAMELEN(msg);
  1714. if (MSG_CMSG_COMPAT & flags) {
  1715. err = verify_compat_iovec(msg_sys, iov,
  1716. (struct sockaddr *)&addr,
  1717. VERIFY_WRITE);
  1718. } else
  1719. err = verify_iovec(msg_sys, iov,
  1720. (struct sockaddr *)&addr,
  1721. VERIFY_WRITE);
  1722. if (err < 0)
  1723. goto out_freeiov;
  1724. total_len = err;
  1725. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1726. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1727. if (sock->file->f_flags & O_NONBLOCK)
  1728. flags |= MSG_DONTWAIT;
  1729. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1730. total_len, flags);
  1731. if (err < 0)
  1732. goto out_freeiov;
  1733. len = err;
  1734. if (uaddr != NULL) {
  1735. err = move_addr_to_user((struct sockaddr *)&addr,
  1736. msg_sys->msg_namelen, uaddr,
  1737. uaddr_len);
  1738. if (err < 0)
  1739. goto out_freeiov;
  1740. }
  1741. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1742. COMPAT_FLAGS(msg));
  1743. if (err)
  1744. goto out_freeiov;
  1745. if (MSG_CMSG_COMPAT & flags)
  1746. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1747. &msg_compat->msg_controllen);
  1748. else
  1749. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1750. &msg->msg_controllen);
  1751. if (err)
  1752. goto out_freeiov;
  1753. err = len;
  1754. out_freeiov:
  1755. if (iov != iovstack)
  1756. sock_kfree_s(sock->sk, iov, iov_size);
  1757. out:
  1758. return err;
  1759. }
  1760. /*
  1761. * BSD recvmsg interface
  1762. */
  1763. SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
  1764. unsigned int, flags)
  1765. {
  1766. int fput_needed, err;
  1767. struct msghdr msg_sys;
  1768. struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1769. if (!sock)
  1770. goto out;
  1771. err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1772. fput_light(sock->file, fput_needed);
  1773. out:
  1774. return err;
  1775. }
  1776. /*
  1777. * Linux recvmmsg interface
  1778. */
  1779. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1780. unsigned int flags, struct timespec *timeout)
  1781. {
  1782. int fput_needed, err, datagrams;
  1783. struct socket *sock;
  1784. struct mmsghdr __user *entry;
  1785. struct compat_mmsghdr __user *compat_entry;
  1786. struct msghdr msg_sys;
  1787. struct timespec end_time;
  1788. if (timeout &&
  1789. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1790. timeout->tv_nsec))
  1791. return -EINVAL;
  1792. datagrams = 0;
  1793. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1794. if (!sock)
  1795. return err;
  1796. err = sock_error(sock->sk);
  1797. if (err)
  1798. goto out_put;
  1799. entry = mmsg;
  1800. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1801. while (datagrams < vlen) {
  1802. /*
  1803. * No need to ask LSM for more than the first datagram.
  1804. */
  1805. if (MSG_CMSG_COMPAT & flags) {
  1806. err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
  1807. &msg_sys, flags, datagrams);
  1808. if (err < 0)
  1809. break;
  1810. err = __put_user(err, &compat_entry->msg_len);
  1811. ++compat_entry;
  1812. } else {
  1813. err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
  1814. &msg_sys, flags, datagrams);
  1815. if (err < 0)
  1816. break;
  1817. err = put_user(err, &entry->msg_len);
  1818. ++entry;
  1819. }
  1820. if (err)
  1821. break;
  1822. ++datagrams;
  1823. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1824. if (flags & MSG_WAITFORONE)
  1825. flags |= MSG_DONTWAIT;
  1826. if (timeout) {
  1827. ktime_get_ts(timeout);
  1828. *timeout = timespec_sub(end_time, *timeout);
  1829. if (timeout->tv_sec < 0) {
  1830. timeout->tv_sec = timeout->tv_nsec = 0;
  1831. break;
  1832. }
  1833. /* Timeout, return less than vlen datagrams */
  1834. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1835. break;
  1836. }
  1837. /* Out of band data, return right away */
  1838. if (msg_sys.msg_flags & MSG_OOB)
  1839. break;
  1840. }
  1841. out_put:
  1842. fput_light(sock->file, fput_needed);
  1843. if (err == 0)
  1844. return datagrams;
  1845. if (datagrams != 0) {
  1846. /*
  1847. * We may return less entries than requested (vlen) if the
  1848. * sock is non block and there aren't enough datagrams...
  1849. */
  1850. if (err != -EAGAIN) {
  1851. /*
  1852. * ... or if recvmsg returns an error after we
  1853. * received some datagrams, where we record the
  1854. * error to return on the next call or if the
  1855. * app asks about it using getsockopt(SO_ERROR).
  1856. */
  1857. sock->sk->sk_err = -err;
  1858. }
  1859. return datagrams;
  1860. }
  1861. return err;
  1862. }
  1863. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1864. unsigned int, vlen, unsigned int, flags,
  1865. struct timespec __user *, timeout)
  1866. {
  1867. int datagrams;
  1868. struct timespec timeout_sys;
  1869. if (!timeout)
  1870. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1871. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1872. return -EFAULT;
  1873. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1874. if (datagrams > 0 &&
  1875. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1876. datagrams = -EFAULT;
  1877. return datagrams;
  1878. }
  1879. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1880. /* Argument list sizes for sys_socketcall */
  1881. #define AL(x) ((x) * sizeof(unsigned long))
  1882. static const unsigned char nargs[20] = {
  1883. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1884. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1885. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1886. AL(4), AL(5)
  1887. };
  1888. #undef AL
  1889. /*
  1890. * System call vectors.
  1891. *
  1892. * Argument checking cleaned up. Saved 20% in size.
  1893. * This function doesn't need to set the kernel lock because
  1894. * it is set by the callees.
  1895. */
  1896. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  1897. {
  1898. unsigned long a[6];
  1899. unsigned long a0, a1;
  1900. int err;
  1901. unsigned int len;
  1902. if (call < 1 || call > SYS_RECVMMSG)
  1903. return -EINVAL;
  1904. len = nargs[call];
  1905. if (len > sizeof(a))
  1906. return -EINVAL;
  1907. /* copy_from_user should be SMP safe. */
  1908. if (copy_from_user(a, args, len))
  1909. return -EFAULT;
  1910. audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1911. a0 = a[0];
  1912. a1 = a[1];
  1913. switch (call) {
  1914. case SYS_SOCKET:
  1915. err = sys_socket(a0, a1, a[2]);
  1916. break;
  1917. case SYS_BIND:
  1918. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1919. break;
  1920. case SYS_CONNECT:
  1921. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1922. break;
  1923. case SYS_LISTEN:
  1924. err = sys_listen(a0, a1);
  1925. break;
  1926. case SYS_ACCEPT:
  1927. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1928. (int __user *)a[2], 0);
  1929. break;
  1930. case SYS_GETSOCKNAME:
  1931. err =
  1932. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1933. (int __user *)a[2]);
  1934. break;
  1935. case SYS_GETPEERNAME:
  1936. err =
  1937. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1938. (int __user *)a[2]);
  1939. break;
  1940. case SYS_SOCKETPAIR:
  1941. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1942. break;
  1943. case SYS_SEND:
  1944. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1945. break;
  1946. case SYS_SENDTO:
  1947. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1948. (struct sockaddr __user *)a[4], a[5]);
  1949. break;
  1950. case SYS_RECV:
  1951. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1952. break;
  1953. case SYS_RECVFROM:
  1954. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1955. (struct sockaddr __user *)a[4],
  1956. (int __user *)a[5]);
  1957. break;
  1958. case SYS_SHUTDOWN:
  1959. err = sys_shutdown(a0, a1);
  1960. break;
  1961. case SYS_SETSOCKOPT:
  1962. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1963. break;
  1964. case SYS_GETSOCKOPT:
  1965. err =
  1966. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1967. (int __user *)a[4]);
  1968. break;
  1969. case SYS_SENDMSG:
  1970. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1971. break;
  1972. case SYS_RECVMSG:
  1973. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1974. break;
  1975. case SYS_RECVMMSG:
  1976. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  1977. (struct timespec __user *)a[4]);
  1978. break;
  1979. case SYS_ACCEPT4:
  1980. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1981. (int __user *)a[2], a[3]);
  1982. break;
  1983. default:
  1984. err = -EINVAL;
  1985. break;
  1986. }
  1987. return err;
  1988. }
  1989. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1990. /**
  1991. * sock_register - add a socket protocol handler
  1992. * @ops: description of protocol
  1993. *
  1994. * This function is called by a protocol handler that wants to
  1995. * advertise its address family, and have it linked into the
  1996. * socket interface. The value ops->family coresponds to the
  1997. * socket system call protocol family.
  1998. */
  1999. int sock_register(const struct net_proto_family *ops)
  2000. {
  2001. int err;
  2002. if (ops->family >= NPROTO) {
  2003. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  2004. NPROTO);
  2005. return -ENOBUFS;
  2006. }
  2007. spin_lock(&net_family_lock);
  2008. if (rcu_dereference_protected(net_families[ops->family],
  2009. lockdep_is_held(&net_family_lock)))
  2010. err = -EEXIST;
  2011. else {
  2012. rcu_assign_pointer(net_families[ops->family], ops);
  2013. err = 0;
  2014. }
  2015. spin_unlock(&net_family_lock);
  2016. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  2017. return err;
  2018. }
  2019. EXPORT_SYMBOL(sock_register);
  2020. /**
  2021. * sock_unregister - remove a protocol handler
  2022. * @family: protocol family to remove
  2023. *
  2024. * This function is called by a protocol handler that wants to
  2025. * remove its address family, and have it unlinked from the
  2026. * new socket creation.
  2027. *
  2028. * If protocol handler is a module, then it can use module reference
  2029. * counts to protect against new references. If protocol handler is not
  2030. * a module then it needs to provide its own protection in
  2031. * the ops->create routine.
  2032. */
  2033. void sock_unregister(int family)
  2034. {
  2035. BUG_ON(family < 0 || family >= NPROTO);
  2036. spin_lock(&net_family_lock);
  2037. rcu_assign_pointer(net_families[family], NULL);
  2038. spin_unlock(&net_family_lock);
  2039. synchronize_rcu();
  2040. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  2041. }
  2042. EXPORT_SYMBOL(sock_unregister);
  2043. static int __init sock_init(void)
  2044. {
  2045. int err;
  2046. /*
  2047. * Initialize sock SLAB cache.
  2048. */
  2049. sk_init();
  2050. /*
  2051. * Initialize skbuff SLAB cache
  2052. */
  2053. skb_init();
  2054. /*
  2055. * Initialize the protocols module.
  2056. */
  2057. init_inodecache();
  2058. err = register_filesystem(&sock_fs_type);
  2059. if (err)
  2060. goto out_fs;
  2061. sock_mnt = kern_mount(&sock_fs_type);
  2062. if (IS_ERR(sock_mnt)) {
  2063. err = PTR_ERR(sock_mnt);
  2064. goto out_mount;
  2065. }
  2066. /* The real protocol initialization is performed in later initcalls.
  2067. */
  2068. #ifdef CONFIG_NETFILTER
  2069. netfilter_init();
  2070. #endif
  2071. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  2072. skb_timestamping_init();
  2073. #endif
  2074. out:
  2075. return err;
  2076. out_mount:
  2077. unregister_filesystem(&sock_fs_type);
  2078. out_fs:
  2079. goto out;
  2080. }
  2081. core_initcall(sock_init); /* early initcall */
  2082. #ifdef CONFIG_PROC_FS
  2083. void socket_seq_show(struct seq_file *seq)
  2084. {
  2085. int cpu;
  2086. int counter = 0;
  2087. for_each_possible_cpu(cpu)
  2088. counter += per_cpu(sockets_in_use, cpu);
  2089. /* It can be negative, by the way. 8) */
  2090. if (counter < 0)
  2091. counter = 0;
  2092. seq_printf(seq, "sockets: used %d\n", counter);
  2093. }
  2094. #endif /* CONFIG_PROC_FS */
  2095. #ifdef CONFIG_COMPAT
  2096. static int do_siocgstamp(struct net *net, struct socket *sock,
  2097. unsigned int cmd, struct compat_timeval __user *up)
  2098. {
  2099. mm_segment_t old_fs = get_fs();
  2100. struct timeval ktv;
  2101. int err;
  2102. set_fs(KERNEL_DS);
  2103. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2104. set_fs(old_fs);
  2105. if (!err) {
  2106. err = put_user(ktv.tv_sec, &up->tv_sec);
  2107. err |= __put_user(ktv.tv_usec, &up->tv_usec);
  2108. }
  2109. return err;
  2110. }
  2111. static int do_siocgstampns(struct net *net, struct socket *sock,
  2112. unsigned int cmd, struct compat_timespec __user *up)
  2113. {
  2114. mm_segment_t old_fs = get_fs();
  2115. struct timespec kts;
  2116. int err;
  2117. set_fs(KERNEL_DS);
  2118. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2119. set_fs(old_fs);
  2120. if (!err) {
  2121. err = put_user(kts.tv_sec, &up->tv_sec);
  2122. err |= __put_user(kts.tv_nsec, &up->tv_nsec);
  2123. }
  2124. return err;
  2125. }
  2126. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2127. {
  2128. struct ifreq __user *uifr;
  2129. int err;
  2130. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2131. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2132. return -EFAULT;
  2133. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2134. if (err)
  2135. return err;
  2136. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2137. return -EFAULT;
  2138. return 0;
  2139. }
  2140. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2141. {
  2142. struct compat_ifconf ifc32;
  2143. struct ifconf ifc;
  2144. struct ifconf __user *uifc;
  2145. struct compat_ifreq __user *ifr32;
  2146. struct ifreq __user *ifr;
  2147. unsigned int i, j;
  2148. int err;
  2149. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2150. return -EFAULT;
  2151. if (ifc32.ifcbuf == 0) {
  2152. ifc32.ifc_len = 0;
  2153. ifc.ifc_len = 0;
  2154. ifc.ifc_req = NULL;
  2155. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2156. } else {
  2157. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2158. sizeof(struct ifreq);
  2159. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2160. ifc.ifc_len = len;
  2161. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2162. ifr32 = compat_ptr(ifc32.ifcbuf);
  2163. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2164. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2165. return -EFAULT;
  2166. ifr++;
  2167. ifr32++;
  2168. }
  2169. }
  2170. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2171. return -EFAULT;
  2172. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2173. if (err)
  2174. return err;
  2175. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2176. return -EFAULT;
  2177. ifr = ifc.ifc_req;
  2178. ifr32 = compat_ptr(ifc32.ifcbuf);
  2179. for (i = 0, j = 0;
  2180. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2181. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2182. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2183. return -EFAULT;
  2184. ifr32++;
  2185. ifr++;
  2186. }
  2187. if (ifc32.ifcbuf == 0) {
  2188. /* Translate from 64-bit structure multiple to
  2189. * a 32-bit one.
  2190. */
  2191. i = ifc.ifc_len;
  2192. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2193. ifc32.ifc_len = i;
  2194. } else {
  2195. ifc32.ifc_len = i;
  2196. }
  2197. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2198. return -EFAULT;
  2199. return 0;
  2200. }
  2201. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2202. {
  2203. struct ifreq __user *ifr;
  2204. u32 data;
  2205. void __user *datap;
  2206. ifr = compat_alloc_user_space(sizeof(*ifr));
  2207. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2208. return -EFAULT;
  2209. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2210. return -EFAULT;
  2211. datap = compat_ptr(data);
  2212. if (put_user(datap, &ifr->ifr_ifru.ifru_data))
  2213. return -EFAULT;
  2214. return dev_ioctl(net, SIOCETHTOOL, ifr);
  2215. }
  2216. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2217. {
  2218. void __user *uptr;
  2219. compat_uptr_t uptr32;
  2220. struct ifreq __user *uifr;
  2221. uifr = compat_alloc_user_space(sizeof(*uifr));
  2222. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2223. return -EFAULT;
  2224. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2225. return -EFAULT;
  2226. uptr = compat_ptr(uptr32);
  2227. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2228. return -EFAULT;
  2229. return dev_ioctl(net, SIOCWANDEV, uifr);
  2230. }
  2231. static int bond_ioctl(struct net *net, unsigned int cmd,
  2232. struct compat_ifreq __user *ifr32)
  2233. {
  2234. struct ifreq kifr;
  2235. struct ifreq __user *uifr;
  2236. mm_segment_t old_fs;
  2237. int err;
  2238. u32 data;
  2239. void __user *datap;
  2240. switch (cmd) {
  2241. case SIOCBONDENSLAVE:
  2242. case SIOCBONDRELEASE:
  2243. case SIOCBONDSETHWADDR:
  2244. case SIOCBONDCHANGEACTIVE:
  2245. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2246. return -EFAULT;
  2247. old_fs = get_fs();
  2248. set_fs(KERNEL_DS);
  2249. err = dev_ioctl(net, cmd, &kifr);
  2250. set_fs(old_fs);
  2251. return err;
  2252. case SIOCBONDSLAVEINFOQUERY:
  2253. case SIOCBONDINFOQUERY:
  2254. uifr = compat_alloc_user_space(sizeof(*uifr));
  2255. if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2256. return -EFAULT;
  2257. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2258. return -EFAULT;
  2259. datap = compat_ptr(data);
  2260. if (put_user(datap, &uifr->ifr_ifru.ifru_data))
  2261. return -EFAULT;
  2262. return dev_ioctl(net, cmd, uifr);
  2263. default:
  2264. return -EINVAL;
  2265. }
  2266. }
  2267. static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
  2268. struct compat_ifreq __user *u_ifreq32)
  2269. {
  2270. struct ifreq __user *u_ifreq64;
  2271. char tmp_buf[IFNAMSIZ];
  2272. void __user *data64;
  2273. u32 data32;
  2274. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2275. IFNAMSIZ))
  2276. return -EFAULT;
  2277. if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2278. return -EFAULT;
  2279. data64 = compat_ptr(data32);
  2280. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2281. /* Don't check these user accesses, just let that get trapped
  2282. * in the ioctl handler instead.
  2283. */
  2284. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2285. IFNAMSIZ))
  2286. return -EFAULT;
  2287. if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2288. return -EFAULT;
  2289. return dev_ioctl(net, cmd, u_ifreq64);
  2290. }
  2291. static int dev_ifsioc(struct net *net, struct socket *sock,
  2292. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2293. {
  2294. struct ifreq __user *uifr;
  2295. int err;
  2296. uifr = compat_alloc_user_space(sizeof(*uifr));
  2297. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2298. return -EFAULT;
  2299. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2300. if (!err) {
  2301. switch (cmd) {
  2302. case SIOCGIFFLAGS:
  2303. case SIOCGIFMETRIC:
  2304. case SIOCGIFMTU:
  2305. case SIOCGIFMEM:
  2306. case SIOCGIFHWADDR:
  2307. case SIOCGIFINDEX:
  2308. case SIOCGIFADDR:
  2309. case SIOCGIFBRDADDR:
  2310. case SIOCGIFDSTADDR:
  2311. case SIOCGIFNETMASK:
  2312. case SIOCGIFPFLAGS:
  2313. case SIOCGIFTXQLEN:
  2314. case SIOCGMIIPHY:
  2315. case SIOCGMIIREG:
  2316. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2317. err = -EFAULT;
  2318. break;
  2319. }
  2320. }
  2321. return err;
  2322. }
  2323. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2324. struct compat_ifreq __user *uifr32)
  2325. {
  2326. struct ifreq ifr;
  2327. struct compat_ifmap __user *uifmap32;
  2328. mm_segment_t old_fs;
  2329. int err;
  2330. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2331. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2332. err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2333. err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2334. err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2335. err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2336. err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2337. err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
  2338. if (err)
  2339. return -EFAULT;
  2340. old_fs = get_fs();
  2341. set_fs(KERNEL_DS);
  2342. err = dev_ioctl(net, cmd, (void __user *)&ifr);
  2343. set_fs(old_fs);
  2344. if (cmd == SIOCGIFMAP && !err) {
  2345. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2346. err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2347. err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2348. err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2349. err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2350. err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2351. err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
  2352. if (err)
  2353. err = -EFAULT;
  2354. }
  2355. return err;
  2356. }
  2357. static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
  2358. {
  2359. void __user *uptr;
  2360. compat_uptr_t uptr32;
  2361. struct ifreq __user *uifr;
  2362. uifr = compat_alloc_user_space(sizeof(*uifr));
  2363. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2364. return -EFAULT;
  2365. if (get_user(uptr32, &uifr32->ifr_data))
  2366. return -EFAULT;
  2367. uptr = compat_ptr(uptr32);
  2368. if (put_user(uptr, &uifr->ifr_data))
  2369. return -EFAULT;
  2370. return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
  2371. }
  2372. struct rtentry32 {
  2373. u32 rt_pad1;
  2374. struct sockaddr rt_dst; /* target address */
  2375. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2376. struct sockaddr rt_genmask; /* target network mask (IP) */
  2377. unsigned short rt_flags;
  2378. short rt_pad2;
  2379. u32 rt_pad3;
  2380. unsigned char rt_tos;
  2381. unsigned char rt_class;
  2382. short rt_pad4;
  2383. short rt_metric; /* +1 for binary compatibility! */
  2384. /* char * */ u32 rt_dev; /* forcing the device at add */
  2385. u32 rt_mtu; /* per route MTU/Window */
  2386. u32 rt_window; /* Window clamping */
  2387. unsigned short rt_irtt; /* Initial RTT */
  2388. };
  2389. struct in6_rtmsg32 {
  2390. struct in6_addr rtmsg_dst;
  2391. struct in6_addr rtmsg_src;
  2392. struct in6_addr rtmsg_gateway;
  2393. u32 rtmsg_type;
  2394. u16 rtmsg_dst_len;
  2395. u16 rtmsg_src_len;
  2396. u32 rtmsg_metric;
  2397. u32 rtmsg_info;
  2398. u32 rtmsg_flags;
  2399. s32 rtmsg_ifindex;
  2400. };
  2401. static int routing_ioctl(struct net *net, struct socket *sock,
  2402. unsigned int cmd, void __user *argp)
  2403. {
  2404. int ret;
  2405. void *r = NULL;
  2406. struct in6_rtmsg r6;
  2407. struct rtentry r4;
  2408. char devname[16];
  2409. u32 rtdev;
  2410. mm_segment_t old_fs = get_fs();
  2411. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2412. struct in6_rtmsg32 __user *ur6 = argp;
  2413. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2414. 3 * sizeof(struct in6_addr));
  2415. ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2416. ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2417. ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2418. ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2419. ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2420. ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2421. ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2422. r = (void *) &r6;
  2423. } else { /* ipv4 */
  2424. struct rtentry32 __user *ur4 = argp;
  2425. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2426. 3 * sizeof(struct sockaddr));
  2427. ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
  2428. ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
  2429. ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2430. ret |= __get_user(r4.rt_window, &(ur4->rt_window));
  2431. ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2432. ret |= __get_user(rtdev, &(ur4->rt_dev));
  2433. if (rtdev) {
  2434. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2435. r4.rt_dev = devname; devname[15] = 0;
  2436. } else
  2437. r4.rt_dev = NULL;
  2438. r = (void *) &r4;
  2439. }
  2440. if (ret) {
  2441. ret = -EFAULT;
  2442. goto out;
  2443. }
  2444. set_fs(KERNEL_DS);
  2445. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2446. set_fs(old_fs);
  2447. out:
  2448. return ret;
  2449. }
  2450. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2451. * for some operations; this forces use of the newer bridge-utils that
  2452. * use compatiable ioctls
  2453. */
  2454. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2455. {
  2456. compat_ulong_t tmp;
  2457. if (get_user(tmp, argp))
  2458. return -EFAULT;
  2459. if (tmp == BRCTL_GET_VERSION)
  2460. return BRCTL_VERSION + 1;
  2461. return -EINVAL;
  2462. }
  2463. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2464. unsigned int cmd, unsigned long arg)
  2465. {
  2466. void __user *argp = compat_ptr(arg);
  2467. struct sock *sk = sock->sk;
  2468. struct net *net = sock_net(sk);
  2469. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2470. return siocdevprivate_ioctl(net, cmd, argp);
  2471. switch (cmd) {
  2472. case SIOCSIFBR:
  2473. case SIOCGIFBR:
  2474. return old_bridge_ioctl(argp);
  2475. case SIOCGIFNAME:
  2476. return dev_ifname32(net, argp);
  2477. case SIOCGIFCONF:
  2478. return dev_ifconf(net, argp);
  2479. case SIOCETHTOOL:
  2480. return ethtool_ioctl(net, argp);
  2481. case SIOCWANDEV:
  2482. return compat_siocwandev(net, argp);
  2483. case SIOCGIFMAP:
  2484. case SIOCSIFMAP:
  2485. return compat_sioc_ifmap(net, cmd, argp);
  2486. case SIOCBONDENSLAVE:
  2487. case SIOCBONDRELEASE:
  2488. case SIOCBONDSETHWADDR:
  2489. case SIOCBONDSLAVEINFOQUERY:
  2490. case SIOCBONDINFOQUERY:
  2491. case SIOCBONDCHANGEACTIVE:
  2492. return bond_ioctl(net, cmd, argp);
  2493. case SIOCADDRT:
  2494. case SIOCDELRT:
  2495. return routing_ioctl(net, sock, cmd, argp);
  2496. case SIOCGSTAMP:
  2497. return do_siocgstamp(net, sock, cmd, argp);
  2498. case SIOCGSTAMPNS:
  2499. return do_siocgstampns(net, sock, cmd, argp);
  2500. case SIOCSHWTSTAMP:
  2501. return compat_siocshwtstamp(net, argp);
  2502. case FIOSETOWN:
  2503. case SIOCSPGRP:
  2504. case FIOGETOWN:
  2505. case SIOCGPGRP:
  2506. case SIOCBRADDBR:
  2507. case SIOCBRDELBR:
  2508. case SIOCGIFVLAN:
  2509. case SIOCSIFVLAN:
  2510. case SIOCADDDLCI:
  2511. case SIOCDELDLCI:
  2512. return sock_ioctl(file, cmd, arg);
  2513. case SIOCGIFFLAGS:
  2514. case SIOCSIFFLAGS:
  2515. case SIOCGIFMETRIC:
  2516. case SIOCSIFMETRIC:
  2517. case SIOCGIFMTU:
  2518. case SIOCSIFMTU:
  2519. case SIOCGIFMEM:
  2520. case SIOCSIFMEM:
  2521. case SIOCGIFHWADDR:
  2522. case SIOCSIFHWADDR:
  2523. case SIOCADDMULTI:
  2524. case SIOCDELMULTI:
  2525. case SIOCGIFINDEX:
  2526. case SIOCGIFADDR:
  2527. case SIOCSIFADDR:
  2528. case SIOCSIFHWBROADCAST:
  2529. case SIOCDIFADDR:
  2530. case SIOCGIFBRDADDR:
  2531. case SIOCSIFBRDADDR:
  2532. case SIOCGIFDSTADDR:
  2533. case SIOCSIFDSTADDR:
  2534. case SIOCGIFNETMASK:
  2535. case SIOCSIFNETMASK:
  2536. case SIOCSIFPFLAGS:
  2537. case SIOCGIFPFLAGS:
  2538. case SIOCGIFTXQLEN:
  2539. case SIOCSIFTXQLEN:
  2540. case SIOCBRADDIF:
  2541. case SIOCBRDELIF:
  2542. case SIOCSIFNAME:
  2543. case SIOCGMIIPHY:
  2544. case SIOCGMIIREG:
  2545. case SIOCSMIIREG:
  2546. return dev_ifsioc(net, sock, cmd, argp);
  2547. case SIOCSARP:
  2548. case SIOCGARP:
  2549. case SIOCDARP:
  2550. case SIOCATMARK:
  2551. return sock_do_ioctl(net, sock, cmd, arg);
  2552. }
  2553. /* Prevent warning from compat_sys_ioctl, these always
  2554. * result in -EINVAL in the native case anyway. */
  2555. switch (cmd) {
  2556. case SIOCRTMSG:
  2557. case SIOCGIFCOUNT:
  2558. case SIOCSRARP:
  2559. case SIOCGRARP:
  2560. case SIOCDRARP:
  2561. case SIOCSIFLINK:
  2562. case SIOCGIFSLAVE:
  2563. case SIOCSIFSLAVE:
  2564. return -EINVAL;
  2565. }
  2566. return -ENOIOCTLCMD;
  2567. }
  2568. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  2569. unsigned long arg)
  2570. {
  2571. struct socket *sock = file->private_data;
  2572. int ret = -ENOIOCTLCMD;
  2573. struct sock *sk;
  2574. struct net *net;
  2575. sk = sock->sk;
  2576. net = sock_net(sk);
  2577. if (sock->ops->compat_ioctl)
  2578. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2579. if (ret == -ENOIOCTLCMD &&
  2580. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2581. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2582. if (ret == -ENOIOCTLCMD)
  2583. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2584. return ret;
  2585. }
  2586. #endif
  2587. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2588. {
  2589. return sock->ops->bind(sock, addr, addrlen);
  2590. }
  2591. EXPORT_SYMBOL(kernel_bind);
  2592. int kernel_listen(struct socket *sock, int backlog)
  2593. {
  2594. return sock->ops->listen(sock, backlog);
  2595. }
  2596. EXPORT_SYMBOL(kernel_listen);
  2597. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2598. {
  2599. struct sock *sk = sock->sk;
  2600. int err;
  2601. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2602. newsock);
  2603. if (err < 0)
  2604. goto done;
  2605. err = sock->ops->accept(sock, *newsock, flags);
  2606. if (err < 0) {
  2607. sock_release(*newsock);
  2608. *newsock = NULL;
  2609. goto done;
  2610. }
  2611. (*newsock)->ops = sock->ops;
  2612. __module_get((*newsock)->ops->owner);
  2613. done:
  2614. return err;
  2615. }
  2616. EXPORT_SYMBOL(kernel_accept);
  2617. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2618. int flags)
  2619. {
  2620. return sock->ops->connect(sock, addr, addrlen, flags);
  2621. }
  2622. EXPORT_SYMBOL(kernel_connect);
  2623. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2624. int *addrlen)
  2625. {
  2626. return sock->ops->getname(sock, addr, addrlen, 0);
  2627. }
  2628. EXPORT_SYMBOL(kernel_getsockname);
  2629. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2630. int *addrlen)
  2631. {
  2632. return sock->ops->getname(sock, addr, addrlen, 1);
  2633. }
  2634. EXPORT_SYMBOL(kernel_getpeername);
  2635. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2636. char *optval, int *optlen)
  2637. {
  2638. mm_segment_t oldfs = get_fs();
  2639. char __user *uoptval;
  2640. int __user *uoptlen;
  2641. int err;
  2642. uoptval = (char __user __force *) optval;
  2643. uoptlen = (int __user __force *) optlen;
  2644. set_fs(KERNEL_DS);
  2645. if (level == SOL_SOCKET)
  2646. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2647. else
  2648. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2649. uoptlen);
  2650. set_fs(oldfs);
  2651. return err;
  2652. }
  2653. EXPORT_SYMBOL(kernel_getsockopt);
  2654. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2655. char *optval, unsigned int optlen)
  2656. {
  2657. mm_segment_t oldfs = get_fs();
  2658. char __user *uoptval;
  2659. int err;
  2660. uoptval = (char __user __force *) optval;
  2661. set_fs(KERNEL_DS);
  2662. if (level == SOL_SOCKET)
  2663. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  2664. else
  2665. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  2666. optlen);
  2667. set_fs(oldfs);
  2668. return err;
  2669. }
  2670. EXPORT_SYMBOL(kernel_setsockopt);
  2671. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2672. size_t size, int flags)
  2673. {
  2674. sock_update_classid(sock->sk);
  2675. if (sock->ops->sendpage)
  2676. return sock->ops->sendpage(sock, page, offset, size, flags);
  2677. return sock_no_sendpage(sock, page, offset, size, flags);
  2678. }
  2679. EXPORT_SYMBOL(kernel_sendpage);
  2680. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2681. {
  2682. mm_segment_t oldfs = get_fs();
  2683. int err;
  2684. set_fs(KERNEL_DS);
  2685. err = sock->ops->ioctl(sock, cmd, arg);
  2686. set_fs(oldfs);
  2687. return err;
  2688. }
  2689. EXPORT_SYMBOL(kernel_sock_ioctl);
  2690. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2691. {
  2692. return sock->ops->shutdown(sock, how);
  2693. }
  2694. EXPORT_SYMBOL(kernel_sock_shutdown);