socket.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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/thread_info.h>
  72. #include <linux/wanrouter.h>
  73. #include <linux/if_bridge.h>
  74. #include <linux/if_frad.h>
  75. #include <linux/if_vlan.h>
  76. #include <linux/init.h>
  77. #include <linux/poll.h>
  78. #include <linux/cache.h>
  79. #include <linux/module.h>
  80. #include <linux/highmem.h>
  81. #include <linux/mount.h>
  82. #include <linux/security.h>
  83. #include <linux/syscalls.h>
  84. #include <linux/compat.h>
  85. #include <linux/kmod.h>
  86. #include <linux/audit.h>
  87. #include <linux/wireless.h>
  88. #include <linux/nsproxy.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/unistd.h>
  91. #include <net/compat.h>
  92. #include <net/wext.h>
  93. #include <net/sock.h>
  94. #include <linux/netfilter.h>
  95. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  96. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  97. unsigned long nr_segs, loff_t pos);
  98. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  99. unsigned long nr_segs, loff_t pos);
  100. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  101. static int sock_close(struct inode *inode, struct file *file);
  102. static unsigned int sock_poll(struct file *file,
  103. struct poll_table_struct *wait);
  104. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  105. #ifdef CONFIG_COMPAT
  106. static long compat_sock_ioctl(struct file *file,
  107. unsigned int cmd, unsigned long arg);
  108. #endif
  109. static int sock_fasync(int fd, struct file *filp, int on);
  110. static ssize_t sock_sendpage(struct file *file, struct page *page,
  111. int offset, size_t size, loff_t *ppos, int more);
  112. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  113. struct pipe_inode_info *pipe, size_t len,
  114. unsigned int flags);
  115. /*
  116. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  117. * in the operation structures but are done directly via the socketcall() multiplexor.
  118. */
  119. static const struct file_operations socket_file_ops = {
  120. .owner = THIS_MODULE,
  121. .llseek = no_llseek,
  122. .aio_read = sock_aio_read,
  123. .aio_write = sock_aio_write,
  124. .poll = sock_poll,
  125. .unlocked_ioctl = sock_ioctl,
  126. #ifdef CONFIG_COMPAT
  127. .compat_ioctl = compat_sock_ioctl,
  128. #endif
  129. .mmap = sock_mmap,
  130. .open = sock_no_open, /* special open code to disallow open via /proc */
  131. .release = sock_close,
  132. .fasync = sock_fasync,
  133. .sendpage = sock_sendpage,
  134. .splice_write = generic_splice_sendpage,
  135. .splice_read = sock_splice_read,
  136. };
  137. /*
  138. * The protocol list. Each protocol is registered in here.
  139. */
  140. static DEFINE_SPINLOCK(net_family_lock);
  141. static const struct net_proto_family *net_families[NPROTO] __read_mostly;
  142. /*
  143. * Statistics counters of the socket lists
  144. */
  145. static DEFINE_PER_CPU(int, sockets_in_use) = 0;
  146. /*
  147. * Support routines.
  148. * Move socket addresses back and forth across the kernel/user
  149. * divide and look after the messy bits.
  150. */
  151. #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
  152. 16 for IP, 16 for IPX,
  153. 24 for IPv6,
  154. about 80 for AX.25
  155. must be at least one bigger than
  156. the AF_UNIX size (see net/unix/af_unix.c
  157. :unix_mkname()).
  158. */
  159. /**
  160. * move_addr_to_kernel - copy a socket address into kernel space
  161. * @uaddr: Address in user space
  162. * @kaddr: Address in kernel space
  163. * @ulen: Length in user space
  164. *
  165. * The address is copied into kernel space. If the provided address is
  166. * too long an error code of -EINVAL is returned. If the copy gives
  167. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  168. */
  169. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  170. {
  171. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  172. return -EINVAL;
  173. if (ulen == 0)
  174. return 0;
  175. if (copy_from_user(kaddr, uaddr, ulen))
  176. return -EFAULT;
  177. return audit_sockaddr(ulen, kaddr);
  178. }
  179. /**
  180. * move_addr_to_user - copy an address to user space
  181. * @kaddr: kernel space address
  182. * @klen: length of address in kernel
  183. * @uaddr: user space address
  184. * @ulen: pointer to user length field
  185. *
  186. * The value pointed to by ulen on entry is the buffer length available.
  187. * This is overwritten with the buffer space used. -EINVAL is returned
  188. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  189. * is returned if either the buffer or the length field are not
  190. * accessible.
  191. * After copying the data up to the limit the user specifies, the true
  192. * length of the data is written over the length limit the user
  193. * specified. Zero is returned for a success.
  194. */
  195. int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
  196. int __user *ulen)
  197. {
  198. int err;
  199. int len;
  200. err = get_user(len, ulen);
  201. if (err)
  202. return err;
  203. if (len > klen)
  204. len = klen;
  205. if (len < 0 || len > sizeof(struct sockaddr_storage))
  206. return -EINVAL;
  207. if (len) {
  208. if (audit_sockaddr(klen, kaddr))
  209. return -ENOMEM;
  210. if (copy_to_user(uaddr, kaddr, len))
  211. return -EFAULT;
  212. }
  213. /*
  214. * "fromlen shall refer to the value before truncation.."
  215. * 1003.1g
  216. */
  217. return __put_user(klen, ulen);
  218. }
  219. #define SOCKFS_MAGIC 0x534F434B
  220. static struct kmem_cache *sock_inode_cachep __read_mostly;
  221. static struct inode *sock_alloc_inode(struct super_block *sb)
  222. {
  223. struct socket_alloc *ei;
  224. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  225. if (!ei)
  226. return NULL;
  227. init_waitqueue_head(&ei->socket.wait);
  228. ei->socket.fasync_list = NULL;
  229. ei->socket.state = SS_UNCONNECTED;
  230. ei->socket.flags = 0;
  231. ei->socket.ops = NULL;
  232. ei->socket.sk = NULL;
  233. ei->socket.file = NULL;
  234. return &ei->vfs_inode;
  235. }
  236. static void sock_destroy_inode(struct inode *inode)
  237. {
  238. kmem_cache_free(sock_inode_cachep,
  239. container_of(inode, struct socket_alloc, vfs_inode));
  240. }
  241. static void init_once(void *foo)
  242. {
  243. struct socket_alloc *ei = (struct socket_alloc *)foo;
  244. inode_init_once(&ei->vfs_inode);
  245. }
  246. static int init_inodecache(void)
  247. {
  248. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  249. sizeof(struct socket_alloc),
  250. 0,
  251. (SLAB_HWCACHE_ALIGN |
  252. SLAB_RECLAIM_ACCOUNT |
  253. SLAB_MEM_SPREAD),
  254. init_once);
  255. if (sock_inode_cachep == NULL)
  256. return -ENOMEM;
  257. return 0;
  258. }
  259. static struct super_operations sockfs_ops = {
  260. .alloc_inode = sock_alloc_inode,
  261. .destroy_inode =sock_destroy_inode,
  262. .statfs = simple_statfs,
  263. };
  264. static int sockfs_get_sb(struct file_system_type *fs_type,
  265. int flags, const char *dev_name, void *data,
  266. struct vfsmount *mnt)
  267. {
  268. return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
  269. mnt);
  270. }
  271. static struct vfsmount *sock_mnt __read_mostly;
  272. static struct file_system_type sock_fs_type = {
  273. .name = "sockfs",
  274. .get_sb = sockfs_get_sb,
  275. .kill_sb = kill_anon_super,
  276. };
  277. static int sockfs_delete_dentry(struct dentry *dentry)
  278. {
  279. /*
  280. * At creation time, we pretended this dentry was hashed
  281. * (by clearing DCACHE_UNHASHED bit in d_flags)
  282. * At delete time, we restore the truth : not hashed.
  283. * (so that dput() can proceed correctly)
  284. */
  285. dentry->d_flags |= DCACHE_UNHASHED;
  286. return 0;
  287. }
  288. /*
  289. * sockfs_dname() is called from d_path().
  290. */
  291. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  292. {
  293. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  294. dentry->d_inode->i_ino);
  295. }
  296. static struct dentry_operations sockfs_dentry_operations = {
  297. .d_delete = sockfs_delete_dentry,
  298. .d_dname = sockfs_dname,
  299. };
  300. /*
  301. * Obtains the first available file descriptor and sets it up for use.
  302. *
  303. * These functions create file structures and maps them to fd space
  304. * of the current process. On success it returns file descriptor
  305. * and file struct implicitly stored in sock->file.
  306. * Note that another thread may close file descriptor before we return
  307. * from this function. We use the fact that now we do not refer
  308. * to socket after mapping. If one day we will need it, this
  309. * function will increment ref. count on file by 1.
  310. *
  311. * In any case returned fd MAY BE not valid!
  312. * This race condition is unavoidable
  313. * with shared fd spaces, we cannot solve it inside kernel,
  314. * but we take care of internal coherence yet.
  315. */
  316. static int sock_alloc_fd(struct file **filep, int flags)
  317. {
  318. int fd;
  319. fd = get_unused_fd_flags(flags);
  320. if (likely(fd >= 0)) {
  321. struct file *file = get_empty_filp();
  322. *filep = file;
  323. if (unlikely(!file)) {
  324. put_unused_fd(fd);
  325. return -ENFILE;
  326. }
  327. } else
  328. *filep = NULL;
  329. return fd;
  330. }
  331. static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
  332. {
  333. struct dentry *dentry;
  334. struct qstr name = { .name = "" };
  335. dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
  336. if (unlikely(!dentry))
  337. return -ENOMEM;
  338. dentry->d_op = &sockfs_dentry_operations;
  339. /*
  340. * We dont want to push this dentry into global dentry hash table.
  341. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  342. * This permits a working /proc/$pid/fd/XXX on sockets
  343. */
  344. dentry->d_flags &= ~DCACHE_UNHASHED;
  345. d_instantiate(dentry, SOCK_INODE(sock));
  346. sock->file = file;
  347. init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
  348. &socket_file_ops);
  349. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  350. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  351. file->f_pos = 0;
  352. file->private_data = sock;
  353. return 0;
  354. }
  355. int sock_map_fd(struct socket *sock, int flags)
  356. {
  357. struct file *newfile;
  358. int fd = sock_alloc_fd(&newfile, flags);
  359. if (likely(fd >= 0)) {
  360. int err = sock_attach_fd(sock, newfile, flags);
  361. if (unlikely(err < 0)) {
  362. put_filp(newfile);
  363. put_unused_fd(fd);
  364. return err;
  365. }
  366. fd_install(fd, newfile);
  367. }
  368. return fd;
  369. }
  370. static struct socket *sock_from_file(struct file *file, int *err)
  371. {
  372. if (file->f_op == &socket_file_ops)
  373. return file->private_data; /* set in sock_map_fd */
  374. *err = -ENOTSOCK;
  375. return NULL;
  376. }
  377. /**
  378. * sockfd_lookup - Go from a file number to its socket slot
  379. * @fd: file handle
  380. * @err: pointer to an error code return
  381. *
  382. * The file handle passed in is locked and the socket it is bound
  383. * too is returned. If an error occurs the err pointer is overwritten
  384. * with a negative errno code and NULL is returned. The function checks
  385. * for both invalid handles and passing a handle which is not a socket.
  386. *
  387. * On a success the socket object pointer is returned.
  388. */
  389. struct socket *sockfd_lookup(int fd, int *err)
  390. {
  391. struct file *file;
  392. struct socket *sock;
  393. file = fget(fd);
  394. if (!file) {
  395. *err = -EBADF;
  396. return NULL;
  397. }
  398. sock = sock_from_file(file, err);
  399. if (!sock)
  400. fput(file);
  401. return sock;
  402. }
  403. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  404. {
  405. struct file *file;
  406. struct socket *sock;
  407. *err = -EBADF;
  408. file = fget_light(fd, fput_needed);
  409. if (file) {
  410. sock = sock_from_file(file, err);
  411. if (sock)
  412. return sock;
  413. fput_light(file, *fput_needed);
  414. }
  415. return NULL;
  416. }
  417. /**
  418. * sock_alloc - allocate a socket
  419. *
  420. * Allocate a new inode and socket object. The two are bound together
  421. * and initialised. The socket is then returned. If we are out of inodes
  422. * NULL is returned.
  423. */
  424. static struct socket *sock_alloc(void)
  425. {
  426. struct inode *inode;
  427. struct socket *sock;
  428. inode = new_inode(sock_mnt->mnt_sb);
  429. if (!inode)
  430. return NULL;
  431. sock = SOCKET_I(inode);
  432. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  433. inode->i_uid = current->fsuid;
  434. inode->i_gid = current->fsgid;
  435. get_cpu_var(sockets_in_use)++;
  436. put_cpu_var(sockets_in_use);
  437. return sock;
  438. }
  439. /*
  440. * In theory you can't get an open on this inode, but /proc provides
  441. * a back door. Remember to keep it shut otherwise you'll let the
  442. * creepy crawlies in.
  443. */
  444. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  445. {
  446. return -ENXIO;
  447. }
  448. const struct file_operations bad_sock_fops = {
  449. .owner = THIS_MODULE,
  450. .open = sock_no_open,
  451. };
  452. /**
  453. * sock_release - close a socket
  454. * @sock: socket to close
  455. *
  456. * The socket is released from the protocol stack if it has a release
  457. * callback, and the inode is then released if the socket is bound to
  458. * an inode not a file.
  459. */
  460. void sock_release(struct socket *sock)
  461. {
  462. if (sock->ops) {
  463. struct module *owner = sock->ops->owner;
  464. sock->ops->release(sock);
  465. sock->ops = NULL;
  466. module_put(owner);
  467. }
  468. if (sock->fasync_list)
  469. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  470. get_cpu_var(sockets_in_use)--;
  471. put_cpu_var(sockets_in_use);
  472. if (!sock->file) {
  473. iput(SOCK_INODE(sock));
  474. return;
  475. }
  476. sock->file = NULL;
  477. }
  478. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  479. struct msghdr *msg, size_t size)
  480. {
  481. struct sock_iocb *si = kiocb_to_siocb(iocb);
  482. int err;
  483. si->sock = sock;
  484. si->scm = NULL;
  485. si->msg = msg;
  486. si->size = size;
  487. err = security_socket_sendmsg(sock, msg, size);
  488. if (err)
  489. return err;
  490. return sock->ops->sendmsg(iocb, sock, msg, size);
  491. }
  492. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  493. {
  494. struct kiocb iocb;
  495. struct sock_iocb siocb;
  496. int ret;
  497. init_sync_kiocb(&iocb, NULL);
  498. iocb.private = &siocb;
  499. ret = __sock_sendmsg(&iocb, sock, msg, size);
  500. if (-EIOCBQUEUED == ret)
  501. ret = wait_on_sync_kiocb(&iocb);
  502. return ret;
  503. }
  504. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  505. struct kvec *vec, size_t num, size_t size)
  506. {
  507. mm_segment_t oldfs = get_fs();
  508. int result;
  509. set_fs(KERNEL_DS);
  510. /*
  511. * the following is safe, since for compiler definitions of kvec and
  512. * iovec are identical, yielding the same in-core layout and alignment
  513. */
  514. msg->msg_iov = (struct iovec *)vec;
  515. msg->msg_iovlen = num;
  516. result = sock_sendmsg(sock, msg, size);
  517. set_fs(oldfs);
  518. return result;
  519. }
  520. /*
  521. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  522. */
  523. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  524. struct sk_buff *skb)
  525. {
  526. ktime_t kt = skb->tstamp;
  527. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  528. struct timeval tv;
  529. /* Race occurred between timestamp enabling and packet
  530. receiving. Fill in the current time for now. */
  531. if (kt.tv64 == 0)
  532. kt = ktime_get_real();
  533. skb->tstamp = kt;
  534. tv = ktime_to_timeval(kt);
  535. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
  536. } else {
  537. struct timespec ts;
  538. /* Race occurred between timestamp enabling and packet
  539. receiving. Fill in the current time for now. */
  540. if (kt.tv64 == 0)
  541. kt = ktime_get_real();
  542. skb->tstamp = kt;
  543. ts = ktime_to_timespec(kt);
  544. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  548. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  549. struct msghdr *msg, size_t size, int flags)
  550. {
  551. int err;
  552. struct sock_iocb *si = kiocb_to_siocb(iocb);
  553. si->sock = sock;
  554. si->scm = NULL;
  555. si->msg = msg;
  556. si->size = size;
  557. si->flags = flags;
  558. err = security_socket_recvmsg(sock, msg, size, flags);
  559. if (err)
  560. return err;
  561. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  562. }
  563. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  564. size_t size, int flags)
  565. {
  566. struct kiocb iocb;
  567. struct sock_iocb siocb;
  568. int ret;
  569. init_sync_kiocb(&iocb, NULL);
  570. iocb.private = &siocb;
  571. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  572. if (-EIOCBQUEUED == ret)
  573. ret = wait_on_sync_kiocb(&iocb);
  574. return ret;
  575. }
  576. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  577. struct kvec *vec, size_t num, size_t size, int flags)
  578. {
  579. mm_segment_t oldfs = get_fs();
  580. int result;
  581. set_fs(KERNEL_DS);
  582. /*
  583. * the following is safe, since for compiler definitions of kvec and
  584. * iovec are identical, yielding the same in-core layout and alignment
  585. */
  586. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  587. result = sock_recvmsg(sock, msg, size, flags);
  588. set_fs(oldfs);
  589. return result;
  590. }
  591. static void sock_aio_dtor(struct kiocb *iocb)
  592. {
  593. kfree(iocb->private);
  594. }
  595. static ssize_t sock_sendpage(struct file *file, struct page *page,
  596. int offset, size_t size, loff_t *ppos, int more)
  597. {
  598. struct socket *sock;
  599. int flags;
  600. sock = file->private_data;
  601. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  602. if (more)
  603. flags |= MSG_MORE;
  604. return sock->ops->sendpage(sock, page, offset, size, flags);
  605. }
  606. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  607. struct pipe_inode_info *pipe, size_t len,
  608. unsigned int flags)
  609. {
  610. struct socket *sock = file->private_data;
  611. if (unlikely(!sock->ops->splice_read))
  612. return -EINVAL;
  613. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  614. }
  615. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  616. struct sock_iocb *siocb)
  617. {
  618. if (!is_sync_kiocb(iocb)) {
  619. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  620. if (!siocb)
  621. return NULL;
  622. iocb->ki_dtor = sock_aio_dtor;
  623. }
  624. siocb->kiocb = iocb;
  625. iocb->private = siocb;
  626. return siocb;
  627. }
  628. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  629. struct file *file, const struct iovec *iov,
  630. unsigned long nr_segs)
  631. {
  632. struct socket *sock = file->private_data;
  633. size_t size = 0;
  634. int i;
  635. for (i = 0; i < nr_segs; i++)
  636. size += iov[i].iov_len;
  637. msg->msg_name = NULL;
  638. msg->msg_namelen = 0;
  639. msg->msg_control = NULL;
  640. msg->msg_controllen = 0;
  641. msg->msg_iov = (struct iovec *)iov;
  642. msg->msg_iovlen = nr_segs;
  643. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  644. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  645. }
  646. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  647. unsigned long nr_segs, loff_t pos)
  648. {
  649. struct sock_iocb siocb, *x;
  650. if (pos != 0)
  651. return -ESPIPE;
  652. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  653. return 0;
  654. x = alloc_sock_iocb(iocb, &siocb);
  655. if (!x)
  656. return -ENOMEM;
  657. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  658. }
  659. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  660. struct file *file, const struct iovec *iov,
  661. unsigned long nr_segs)
  662. {
  663. struct socket *sock = file->private_data;
  664. size_t size = 0;
  665. int i;
  666. for (i = 0; i < nr_segs; i++)
  667. size += iov[i].iov_len;
  668. msg->msg_name = NULL;
  669. msg->msg_namelen = 0;
  670. msg->msg_control = NULL;
  671. msg->msg_controllen = 0;
  672. msg->msg_iov = (struct iovec *)iov;
  673. msg->msg_iovlen = nr_segs;
  674. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  675. if (sock->type == SOCK_SEQPACKET)
  676. msg->msg_flags |= MSG_EOR;
  677. return __sock_sendmsg(iocb, sock, msg, size);
  678. }
  679. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  680. unsigned long nr_segs, loff_t pos)
  681. {
  682. struct sock_iocb siocb, *x;
  683. if (pos != 0)
  684. return -ESPIPE;
  685. x = alloc_sock_iocb(iocb, &siocb);
  686. if (!x)
  687. return -ENOMEM;
  688. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  689. }
  690. /*
  691. * Atomic setting of ioctl hooks to avoid race
  692. * with module unload.
  693. */
  694. static DEFINE_MUTEX(br_ioctl_mutex);
  695. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
  696. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  697. {
  698. mutex_lock(&br_ioctl_mutex);
  699. br_ioctl_hook = hook;
  700. mutex_unlock(&br_ioctl_mutex);
  701. }
  702. EXPORT_SYMBOL(brioctl_set);
  703. static DEFINE_MUTEX(vlan_ioctl_mutex);
  704. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  705. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  706. {
  707. mutex_lock(&vlan_ioctl_mutex);
  708. vlan_ioctl_hook = hook;
  709. mutex_unlock(&vlan_ioctl_mutex);
  710. }
  711. EXPORT_SYMBOL(vlan_ioctl_set);
  712. static DEFINE_MUTEX(dlci_ioctl_mutex);
  713. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  714. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  715. {
  716. mutex_lock(&dlci_ioctl_mutex);
  717. dlci_ioctl_hook = hook;
  718. mutex_unlock(&dlci_ioctl_mutex);
  719. }
  720. EXPORT_SYMBOL(dlci_ioctl_set);
  721. /*
  722. * With an ioctl, arg may well be a user mode pointer, but we don't know
  723. * what to do with it - that's up to the protocol still.
  724. */
  725. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  726. {
  727. struct socket *sock;
  728. struct sock *sk;
  729. void __user *argp = (void __user *)arg;
  730. int pid, err;
  731. struct net *net;
  732. sock = file->private_data;
  733. sk = sock->sk;
  734. net = sock_net(sk);
  735. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  736. err = dev_ioctl(net, cmd, argp);
  737. } else
  738. #ifdef CONFIG_WIRELESS_EXT
  739. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  740. err = dev_ioctl(net, cmd, argp);
  741. } else
  742. #endif /* CONFIG_WIRELESS_EXT */
  743. switch (cmd) {
  744. case FIOSETOWN:
  745. case SIOCSPGRP:
  746. err = -EFAULT;
  747. if (get_user(pid, (int __user *)argp))
  748. break;
  749. err = f_setown(sock->file, pid, 1);
  750. break;
  751. case FIOGETOWN:
  752. case SIOCGPGRP:
  753. err = put_user(f_getown(sock->file),
  754. (int __user *)argp);
  755. break;
  756. case SIOCGIFBR:
  757. case SIOCSIFBR:
  758. case SIOCBRADDBR:
  759. case SIOCBRDELBR:
  760. err = -ENOPKG;
  761. if (!br_ioctl_hook)
  762. request_module("bridge");
  763. mutex_lock(&br_ioctl_mutex);
  764. if (br_ioctl_hook)
  765. err = br_ioctl_hook(net, cmd, argp);
  766. mutex_unlock(&br_ioctl_mutex);
  767. break;
  768. case SIOCGIFVLAN:
  769. case SIOCSIFVLAN:
  770. err = -ENOPKG;
  771. if (!vlan_ioctl_hook)
  772. request_module("8021q");
  773. mutex_lock(&vlan_ioctl_mutex);
  774. if (vlan_ioctl_hook)
  775. err = vlan_ioctl_hook(net, argp);
  776. mutex_unlock(&vlan_ioctl_mutex);
  777. break;
  778. case SIOCADDDLCI:
  779. case SIOCDELDLCI:
  780. err = -ENOPKG;
  781. if (!dlci_ioctl_hook)
  782. request_module("dlci");
  783. mutex_lock(&dlci_ioctl_mutex);
  784. if (dlci_ioctl_hook)
  785. err = dlci_ioctl_hook(cmd, argp);
  786. mutex_unlock(&dlci_ioctl_mutex);
  787. break;
  788. default:
  789. err = sock->ops->ioctl(sock, cmd, arg);
  790. /*
  791. * If this ioctl is unknown try to hand it down
  792. * to the NIC driver.
  793. */
  794. if (err == -ENOIOCTLCMD)
  795. err = dev_ioctl(net, cmd, argp);
  796. break;
  797. }
  798. return err;
  799. }
  800. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  801. {
  802. int err;
  803. struct socket *sock = NULL;
  804. err = security_socket_create(family, type, protocol, 1);
  805. if (err)
  806. goto out;
  807. sock = sock_alloc();
  808. if (!sock) {
  809. err = -ENOMEM;
  810. goto out;
  811. }
  812. sock->type = type;
  813. err = security_socket_post_create(sock, family, type, protocol, 1);
  814. if (err)
  815. goto out_release;
  816. out:
  817. *res = sock;
  818. return err;
  819. out_release:
  820. sock_release(sock);
  821. sock = NULL;
  822. goto out;
  823. }
  824. /* No kernel lock held - perfect */
  825. static unsigned int sock_poll(struct file *file, poll_table *wait)
  826. {
  827. struct socket *sock;
  828. /*
  829. * We can't return errors to poll, so it's either yes or no.
  830. */
  831. sock = file->private_data;
  832. return sock->ops->poll(file, sock, wait);
  833. }
  834. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  835. {
  836. struct socket *sock = file->private_data;
  837. return sock->ops->mmap(file, sock, vma);
  838. }
  839. static int sock_close(struct inode *inode, struct file *filp)
  840. {
  841. /*
  842. * It was possible the inode is NULL we were
  843. * closing an unfinished socket.
  844. */
  845. if (!inode) {
  846. printk(KERN_DEBUG "sock_close: NULL inode\n");
  847. return 0;
  848. }
  849. sock_release(SOCKET_I(inode));
  850. return 0;
  851. }
  852. /*
  853. * Update the socket async list
  854. *
  855. * Fasync_list locking strategy.
  856. *
  857. * 1. fasync_list is modified only under process context socket lock
  858. * i.e. under semaphore.
  859. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  860. * or under socket lock.
  861. * 3. fasync_list can be used from softirq context, so that
  862. * modification under socket lock have to be enhanced with
  863. * write_lock_bh(&sk->sk_callback_lock).
  864. * --ANK (990710)
  865. */
  866. static int sock_fasync(int fd, struct file *filp, int on)
  867. {
  868. struct fasync_struct *fa, *fna = NULL, **prev;
  869. struct socket *sock;
  870. struct sock *sk;
  871. if (on) {
  872. fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  873. if (fna == NULL)
  874. return -ENOMEM;
  875. }
  876. sock = filp->private_data;
  877. sk = sock->sk;
  878. if (sk == NULL) {
  879. kfree(fna);
  880. return -EINVAL;
  881. }
  882. lock_sock(sk);
  883. prev = &(sock->fasync_list);
  884. for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
  885. if (fa->fa_file == filp)
  886. break;
  887. if (on) {
  888. if (fa != NULL) {
  889. write_lock_bh(&sk->sk_callback_lock);
  890. fa->fa_fd = fd;
  891. write_unlock_bh(&sk->sk_callback_lock);
  892. kfree(fna);
  893. goto out;
  894. }
  895. fna->fa_file = filp;
  896. fna->fa_fd = fd;
  897. fna->magic = FASYNC_MAGIC;
  898. fna->fa_next = sock->fasync_list;
  899. write_lock_bh(&sk->sk_callback_lock);
  900. sock->fasync_list = fna;
  901. write_unlock_bh(&sk->sk_callback_lock);
  902. } else {
  903. if (fa != NULL) {
  904. write_lock_bh(&sk->sk_callback_lock);
  905. *prev = fa->fa_next;
  906. write_unlock_bh(&sk->sk_callback_lock);
  907. kfree(fa);
  908. }
  909. }
  910. out:
  911. release_sock(sock->sk);
  912. return 0;
  913. }
  914. /* This function may be called only under socket lock or callback_lock */
  915. int sock_wake_async(struct socket *sock, int how, int band)
  916. {
  917. if (!sock || !sock->fasync_list)
  918. return -1;
  919. switch (how) {
  920. case SOCK_WAKE_WAITD:
  921. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  922. break;
  923. goto call_kill;
  924. case SOCK_WAKE_SPACE:
  925. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  926. break;
  927. /* fall through */
  928. case SOCK_WAKE_IO:
  929. call_kill:
  930. __kill_fasync(sock->fasync_list, SIGIO, band);
  931. break;
  932. case SOCK_WAKE_URG:
  933. __kill_fasync(sock->fasync_list, SIGURG, band);
  934. }
  935. return 0;
  936. }
  937. static int __sock_create(struct net *net, int family, int type, int protocol,
  938. struct socket **res, int kern)
  939. {
  940. int err;
  941. struct socket *sock;
  942. const struct net_proto_family *pf;
  943. /*
  944. * Check protocol is in range
  945. */
  946. if (family < 0 || family >= NPROTO)
  947. return -EAFNOSUPPORT;
  948. if (type < 0 || type >= SOCK_MAX)
  949. return -EINVAL;
  950. /* Compatibility.
  951. This uglymoron is moved from INET layer to here to avoid
  952. deadlock in module load.
  953. */
  954. if (family == PF_INET && type == SOCK_PACKET) {
  955. static int warned;
  956. if (!warned) {
  957. warned = 1;
  958. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  959. current->comm);
  960. }
  961. family = PF_PACKET;
  962. }
  963. err = security_socket_create(family, type, protocol, kern);
  964. if (err)
  965. return err;
  966. /*
  967. * Allocate the socket and allow the family to set things up. if
  968. * the protocol is 0, the family is instructed to select an appropriate
  969. * default.
  970. */
  971. sock = sock_alloc();
  972. if (!sock) {
  973. if (net_ratelimit())
  974. printk(KERN_WARNING "socket: no more sockets\n");
  975. return -ENFILE; /* Not exactly a match, but its the
  976. closest posix thing */
  977. }
  978. sock->type = type;
  979. #ifdef CONFIG_MODULES
  980. /* Attempt to load a protocol module if the find failed.
  981. *
  982. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  983. * requested real, full-featured networking support upon configuration.
  984. * Otherwise module support will break!
  985. */
  986. if (net_families[family] == NULL)
  987. request_module("net-pf-%d", family);
  988. #endif
  989. rcu_read_lock();
  990. pf = rcu_dereference(net_families[family]);
  991. err = -EAFNOSUPPORT;
  992. if (!pf)
  993. goto out_release;
  994. /*
  995. * We will call the ->create function, that possibly is in a loadable
  996. * module, so we have to bump that loadable module refcnt first.
  997. */
  998. if (!try_module_get(pf->owner))
  999. goto out_release;
  1000. /* Now protected by module ref count */
  1001. rcu_read_unlock();
  1002. err = pf->create(net, sock, protocol);
  1003. if (err < 0)
  1004. goto out_module_put;
  1005. /*
  1006. * Now to bump the refcnt of the [loadable] module that owns this
  1007. * socket at sock_release time we decrement its refcnt.
  1008. */
  1009. if (!try_module_get(sock->ops->owner))
  1010. goto out_module_busy;
  1011. /*
  1012. * Now that we're done with the ->create function, the [loadable]
  1013. * module can have its refcnt decremented
  1014. */
  1015. module_put(pf->owner);
  1016. err = security_socket_post_create(sock, family, type, protocol, kern);
  1017. if (err)
  1018. goto out_sock_release;
  1019. *res = sock;
  1020. return 0;
  1021. out_module_busy:
  1022. err = -EAFNOSUPPORT;
  1023. out_module_put:
  1024. sock->ops = NULL;
  1025. module_put(pf->owner);
  1026. out_sock_release:
  1027. sock_release(sock);
  1028. return err;
  1029. out_release:
  1030. rcu_read_unlock();
  1031. goto out_sock_release;
  1032. }
  1033. int sock_create(int family, int type, int protocol, struct socket **res)
  1034. {
  1035. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1036. }
  1037. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1038. {
  1039. return __sock_create(&init_net, family, type, protocol, res, 1);
  1040. }
  1041. asmlinkage long sys_socket(int family, int type, int protocol)
  1042. {
  1043. int retval;
  1044. struct socket *sock;
  1045. int flags;
  1046. /* Check the SOCK_* constants for consistency. */
  1047. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1048. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1049. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1050. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1051. flags = type & ~SOCK_TYPE_MASK;
  1052. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1053. return -EINVAL;
  1054. type &= SOCK_TYPE_MASK;
  1055. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1056. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1057. retval = sock_create(family, type, protocol, &sock);
  1058. if (retval < 0)
  1059. goto out;
  1060. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1061. if (retval < 0)
  1062. goto out_release;
  1063. out:
  1064. /* It may be already another descriptor 8) Not kernel problem. */
  1065. return retval;
  1066. out_release:
  1067. sock_release(sock);
  1068. return retval;
  1069. }
  1070. /*
  1071. * Create a pair of connected sockets.
  1072. */
  1073. asmlinkage long sys_socketpair(int family, int type, int protocol,
  1074. int __user *usockvec)
  1075. {
  1076. struct socket *sock1, *sock2;
  1077. int fd1, fd2, err;
  1078. struct file *newfile1, *newfile2;
  1079. int flags;
  1080. flags = type & ~SOCK_TYPE_MASK;
  1081. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1082. return -EINVAL;
  1083. type &= SOCK_TYPE_MASK;
  1084. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1085. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1086. /*
  1087. * Obtain the first socket and check if the underlying protocol
  1088. * supports the socketpair call.
  1089. */
  1090. err = sock_create(family, type, protocol, &sock1);
  1091. if (err < 0)
  1092. goto out;
  1093. err = sock_create(family, type, protocol, &sock2);
  1094. if (err < 0)
  1095. goto out_release_1;
  1096. err = sock1->ops->socketpair(sock1, sock2);
  1097. if (err < 0)
  1098. goto out_release_both;
  1099. fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
  1100. if (unlikely(fd1 < 0)) {
  1101. err = fd1;
  1102. goto out_release_both;
  1103. }
  1104. fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
  1105. if (unlikely(fd2 < 0)) {
  1106. err = fd2;
  1107. put_filp(newfile1);
  1108. put_unused_fd(fd1);
  1109. goto out_release_both;
  1110. }
  1111. err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
  1112. if (unlikely(err < 0)) {
  1113. goto out_fd2;
  1114. }
  1115. err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
  1116. if (unlikely(err < 0)) {
  1117. fput(newfile1);
  1118. goto out_fd1;
  1119. }
  1120. err = audit_fd_pair(fd1, fd2);
  1121. if (err < 0) {
  1122. fput(newfile1);
  1123. fput(newfile2);
  1124. goto out_fd;
  1125. }
  1126. fd_install(fd1, newfile1);
  1127. fd_install(fd2, newfile2);
  1128. /* fd1 and fd2 may be already another descriptors.
  1129. * Not kernel problem.
  1130. */
  1131. err = put_user(fd1, &usockvec[0]);
  1132. if (!err)
  1133. err = put_user(fd2, &usockvec[1]);
  1134. if (!err)
  1135. return 0;
  1136. sys_close(fd2);
  1137. sys_close(fd1);
  1138. return err;
  1139. out_release_both:
  1140. sock_release(sock2);
  1141. out_release_1:
  1142. sock_release(sock1);
  1143. out:
  1144. return err;
  1145. out_fd2:
  1146. put_filp(newfile1);
  1147. sock_release(sock1);
  1148. out_fd1:
  1149. put_filp(newfile2);
  1150. sock_release(sock2);
  1151. out_fd:
  1152. put_unused_fd(fd1);
  1153. put_unused_fd(fd2);
  1154. goto out;
  1155. }
  1156. /*
  1157. * Bind a name to a socket. Nothing much to do here since it's
  1158. * the protocol's responsibility to handle the local address.
  1159. *
  1160. * We move the socket address to kernel space before we call
  1161. * the protocol layer (having also checked the address is ok).
  1162. */
  1163. asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1164. {
  1165. struct socket *sock;
  1166. struct sockaddr_storage address;
  1167. int err, fput_needed;
  1168. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1169. if (sock) {
  1170. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1171. if (err >= 0) {
  1172. err = security_socket_bind(sock,
  1173. (struct sockaddr *)&address,
  1174. addrlen);
  1175. if (!err)
  1176. err = sock->ops->bind(sock,
  1177. (struct sockaddr *)
  1178. &address, addrlen);
  1179. }
  1180. fput_light(sock->file, fput_needed);
  1181. }
  1182. return err;
  1183. }
  1184. /*
  1185. * Perform a listen. Basically, we allow the protocol to do anything
  1186. * necessary for a listen, and if that works, we mark the socket as
  1187. * ready for listening.
  1188. */
  1189. asmlinkage long sys_listen(int fd, int backlog)
  1190. {
  1191. struct socket *sock;
  1192. int err, fput_needed;
  1193. int somaxconn;
  1194. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1195. if (sock) {
  1196. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1197. if ((unsigned)backlog > somaxconn)
  1198. backlog = somaxconn;
  1199. err = security_socket_listen(sock, backlog);
  1200. if (!err)
  1201. err = sock->ops->listen(sock, backlog);
  1202. fput_light(sock->file, fput_needed);
  1203. }
  1204. return err;
  1205. }
  1206. /*
  1207. * For accept, we attempt to create a new socket, set up the link
  1208. * with the client, wake up the client, then return the new
  1209. * connected fd. We collect the address of the connector in kernel
  1210. * space and move it to user at the very end. This is unclean because
  1211. * we open the socket then return an error.
  1212. *
  1213. * 1003.1g adds the ability to recvmsg() to query connection pending
  1214. * status to recvmsg. We need to add that support in a way thats
  1215. * clean when we restucture accept also.
  1216. */
  1217. long do_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1218. int __user *upeer_addrlen, int flags)
  1219. {
  1220. struct socket *sock, *newsock;
  1221. struct file *newfile;
  1222. int err, len, newfd, fput_needed;
  1223. struct sockaddr_storage address;
  1224. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1225. return -EINVAL;
  1226. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1227. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1228. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1229. if (!sock)
  1230. goto out;
  1231. err = -ENFILE;
  1232. if (!(newsock = sock_alloc()))
  1233. goto out_put;
  1234. newsock->type = sock->type;
  1235. newsock->ops = sock->ops;
  1236. /*
  1237. * We don't need try_module_get here, as the listening socket (sock)
  1238. * has the protocol module (sock->ops->owner) held.
  1239. */
  1240. __module_get(newsock->ops->owner);
  1241. newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
  1242. if (unlikely(newfd < 0)) {
  1243. err = newfd;
  1244. sock_release(newsock);
  1245. goto out_put;
  1246. }
  1247. err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
  1248. if (err < 0)
  1249. goto out_fd_simple;
  1250. err = security_socket_accept(sock, newsock);
  1251. if (err)
  1252. goto out_fd;
  1253. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1254. if (err < 0)
  1255. goto out_fd;
  1256. if (upeer_sockaddr) {
  1257. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1258. &len, 2) < 0) {
  1259. err = -ECONNABORTED;
  1260. goto out_fd;
  1261. }
  1262. err = move_addr_to_user((struct sockaddr *)&address,
  1263. len, upeer_sockaddr, upeer_addrlen);
  1264. if (err < 0)
  1265. goto out_fd;
  1266. }
  1267. /* File flags are not inherited via accept() unlike another OSes. */
  1268. fd_install(newfd, newfile);
  1269. err = newfd;
  1270. security_socket_post_accept(sock, newsock);
  1271. out_put:
  1272. fput_light(sock->file, fput_needed);
  1273. out:
  1274. return err;
  1275. out_fd_simple:
  1276. sock_release(newsock);
  1277. put_filp(newfile);
  1278. put_unused_fd(newfd);
  1279. goto out_put;
  1280. out_fd:
  1281. fput(newfile);
  1282. put_unused_fd(newfd);
  1283. goto out_put;
  1284. }
  1285. #if 0
  1286. #ifdef HAVE_SET_RESTORE_SIGMASK
  1287. asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr,
  1288. int __user *upeer_addrlen,
  1289. const sigset_t __user *sigmask,
  1290. size_t sigsetsize, int flags)
  1291. {
  1292. sigset_t ksigmask, sigsaved;
  1293. int ret;
  1294. if (sigmask) {
  1295. /* XXX: Don't preclude handling different sized sigset_t's. */
  1296. if (sigsetsize != sizeof(sigset_t))
  1297. return -EINVAL;
  1298. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  1299. return -EFAULT;
  1300. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1301. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1302. }
  1303. ret = do_accept(fd, upeer_sockaddr, upeer_addrlen, flags);
  1304. if (ret < 0 && signal_pending(current)) {
  1305. /*
  1306. * Don't restore the signal mask yet. Let do_signal() deliver
  1307. * the signal on the way back to userspace, before the signal
  1308. * mask is restored.
  1309. */
  1310. if (sigmask) {
  1311. memcpy(&current->saved_sigmask, &sigsaved,
  1312. sizeof(sigsaved));
  1313. set_restore_sigmask();
  1314. }
  1315. } else if (sigmask)
  1316. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1317. return ret;
  1318. }
  1319. #else
  1320. asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr,
  1321. int __user *upeer_addrlen,
  1322. const sigset_t __user *sigmask,
  1323. size_t sigsetsize, int flags)
  1324. {
  1325. /* The platform does not support restoring the signal mask in the
  1326. * return path. So we do not allow using paccept() with a signal
  1327. * mask. */
  1328. if (sigmask)
  1329. return -EINVAL;
  1330. return do_accept(fd, upeer_sockaddr, upeer_addrlen, flags);
  1331. }
  1332. #endif
  1333. #endif
  1334. asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1335. int __user *upeer_addrlen)
  1336. {
  1337. return do_accept(fd, upeer_sockaddr, upeer_addrlen, 0);
  1338. }
  1339. /*
  1340. * Attempt to connect to a socket with the server address. The address
  1341. * is in user space so we verify it is OK and move it to kernel space.
  1342. *
  1343. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1344. * break bindings
  1345. *
  1346. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1347. * other SEQPACKET protocols that take time to connect() as it doesn't
  1348. * include the -EINPROGRESS status for such sockets.
  1349. */
  1350. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
  1351. int addrlen)
  1352. {
  1353. struct socket *sock;
  1354. struct sockaddr_storage address;
  1355. int err, fput_needed;
  1356. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1357. if (!sock)
  1358. goto out;
  1359. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1360. if (err < 0)
  1361. goto out_put;
  1362. err =
  1363. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1364. if (err)
  1365. goto out_put;
  1366. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1367. sock->file->f_flags);
  1368. out_put:
  1369. fput_light(sock->file, fput_needed);
  1370. out:
  1371. return err;
  1372. }
  1373. /*
  1374. * Get the local address ('name') of a socket object. Move the obtained
  1375. * name to user space.
  1376. */
  1377. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1378. int __user *usockaddr_len)
  1379. {
  1380. struct socket *sock;
  1381. struct sockaddr_storage address;
  1382. int len, err, fput_needed;
  1383. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1384. if (!sock)
  1385. goto out;
  1386. err = security_socket_getsockname(sock);
  1387. if (err)
  1388. goto out_put;
  1389. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1390. if (err)
  1391. goto out_put;
  1392. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1393. out_put:
  1394. fput_light(sock->file, fput_needed);
  1395. out:
  1396. return err;
  1397. }
  1398. /*
  1399. * Get the remote address ('name') of a socket object. Move the obtained
  1400. * name to user space.
  1401. */
  1402. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1403. int __user *usockaddr_len)
  1404. {
  1405. struct socket *sock;
  1406. struct sockaddr_storage address;
  1407. int len, err, fput_needed;
  1408. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1409. if (sock != NULL) {
  1410. err = security_socket_getpeername(sock);
  1411. if (err) {
  1412. fput_light(sock->file, fput_needed);
  1413. return err;
  1414. }
  1415. err =
  1416. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1417. 1);
  1418. if (!err)
  1419. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1420. usockaddr_len);
  1421. fput_light(sock->file, fput_needed);
  1422. }
  1423. return err;
  1424. }
  1425. /*
  1426. * Send a datagram to a given address. We move the address into kernel
  1427. * space and check the user space data area is readable before invoking
  1428. * the protocol.
  1429. */
  1430. asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
  1431. unsigned flags, struct sockaddr __user *addr,
  1432. int addr_len)
  1433. {
  1434. struct socket *sock;
  1435. struct sockaddr_storage address;
  1436. int err;
  1437. struct msghdr msg;
  1438. struct iovec iov;
  1439. int fput_needed;
  1440. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1441. if (!sock)
  1442. goto out;
  1443. iov.iov_base = buff;
  1444. iov.iov_len = len;
  1445. msg.msg_name = NULL;
  1446. msg.msg_iov = &iov;
  1447. msg.msg_iovlen = 1;
  1448. msg.msg_control = NULL;
  1449. msg.msg_controllen = 0;
  1450. msg.msg_namelen = 0;
  1451. if (addr) {
  1452. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1453. if (err < 0)
  1454. goto out_put;
  1455. msg.msg_name = (struct sockaddr *)&address;
  1456. msg.msg_namelen = addr_len;
  1457. }
  1458. if (sock->file->f_flags & O_NONBLOCK)
  1459. flags |= MSG_DONTWAIT;
  1460. msg.msg_flags = flags;
  1461. err = sock_sendmsg(sock, &msg, len);
  1462. out_put:
  1463. fput_light(sock->file, fput_needed);
  1464. out:
  1465. return err;
  1466. }
  1467. /*
  1468. * Send a datagram down a socket.
  1469. */
  1470. asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  1471. {
  1472. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1473. }
  1474. /*
  1475. * Receive a frame from the socket and optionally record the address of the
  1476. * sender. We verify the buffers are writable and if needed move the
  1477. * sender address from kernel to user space.
  1478. */
  1479. asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
  1480. unsigned flags, struct sockaddr __user *addr,
  1481. int __user *addr_len)
  1482. {
  1483. struct socket *sock;
  1484. struct iovec iov;
  1485. struct msghdr msg;
  1486. struct sockaddr_storage address;
  1487. int err, err2;
  1488. int fput_needed;
  1489. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1490. if (!sock)
  1491. goto out;
  1492. msg.msg_control = NULL;
  1493. msg.msg_controllen = 0;
  1494. msg.msg_iovlen = 1;
  1495. msg.msg_iov = &iov;
  1496. iov.iov_len = size;
  1497. iov.iov_base = ubuf;
  1498. msg.msg_name = (struct sockaddr *)&address;
  1499. msg.msg_namelen = sizeof(address);
  1500. if (sock->file->f_flags & O_NONBLOCK)
  1501. flags |= MSG_DONTWAIT;
  1502. err = sock_recvmsg(sock, &msg, size, flags);
  1503. if (err >= 0 && addr != NULL) {
  1504. err2 = move_addr_to_user((struct sockaddr *)&address,
  1505. msg.msg_namelen, addr, addr_len);
  1506. if (err2 < 0)
  1507. err = err2;
  1508. }
  1509. fput_light(sock->file, fput_needed);
  1510. out:
  1511. return err;
  1512. }
  1513. /*
  1514. * Receive a datagram from a socket.
  1515. */
  1516. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1517. unsigned flags)
  1518. {
  1519. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1520. }
  1521. /*
  1522. * Set a socket option. Because we don't know the option lengths we have
  1523. * to pass the user mode parameter for the protocols to sort out.
  1524. */
  1525. asmlinkage long sys_setsockopt(int fd, int level, int optname,
  1526. char __user *optval, int optlen)
  1527. {
  1528. int err, fput_needed;
  1529. struct socket *sock;
  1530. if (optlen < 0)
  1531. return -EINVAL;
  1532. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1533. if (sock != NULL) {
  1534. err = security_socket_setsockopt(sock, level, optname);
  1535. if (err)
  1536. goto out_put;
  1537. if (level == SOL_SOCKET)
  1538. err =
  1539. sock_setsockopt(sock, level, optname, optval,
  1540. optlen);
  1541. else
  1542. err =
  1543. sock->ops->setsockopt(sock, level, optname, optval,
  1544. optlen);
  1545. out_put:
  1546. fput_light(sock->file, fput_needed);
  1547. }
  1548. return err;
  1549. }
  1550. /*
  1551. * Get a socket option. Because we don't know the option lengths we have
  1552. * to pass a user mode parameter for the protocols to sort out.
  1553. */
  1554. asmlinkage long sys_getsockopt(int fd, int level, int optname,
  1555. char __user *optval, int __user *optlen)
  1556. {
  1557. int err, fput_needed;
  1558. struct socket *sock;
  1559. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1560. if (sock != NULL) {
  1561. err = security_socket_getsockopt(sock, level, optname);
  1562. if (err)
  1563. goto out_put;
  1564. if (level == SOL_SOCKET)
  1565. err =
  1566. sock_getsockopt(sock, level, optname, optval,
  1567. optlen);
  1568. else
  1569. err =
  1570. sock->ops->getsockopt(sock, level, optname, optval,
  1571. optlen);
  1572. out_put:
  1573. fput_light(sock->file, fput_needed);
  1574. }
  1575. return err;
  1576. }
  1577. /*
  1578. * Shutdown a socket.
  1579. */
  1580. asmlinkage long sys_shutdown(int fd, int how)
  1581. {
  1582. int err, fput_needed;
  1583. struct socket *sock;
  1584. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1585. if (sock != NULL) {
  1586. err = security_socket_shutdown(sock, how);
  1587. if (!err)
  1588. err = sock->ops->shutdown(sock, how);
  1589. fput_light(sock->file, fput_needed);
  1590. }
  1591. return err;
  1592. }
  1593. /* A couple of helpful macros for getting the address of the 32/64 bit
  1594. * fields which are the same type (int / unsigned) on our platforms.
  1595. */
  1596. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1597. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1598. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1599. /*
  1600. * BSD sendmsg interface
  1601. */
  1602. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1603. {
  1604. struct compat_msghdr __user *msg_compat =
  1605. (struct compat_msghdr __user *)msg;
  1606. struct socket *sock;
  1607. struct sockaddr_storage address;
  1608. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1609. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1610. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1611. /* 20 is size of ipv6_pktinfo */
  1612. unsigned char *ctl_buf = ctl;
  1613. struct msghdr msg_sys;
  1614. int err, ctl_len, iov_size, total_len;
  1615. int fput_needed;
  1616. err = -EFAULT;
  1617. if (MSG_CMSG_COMPAT & flags) {
  1618. if (get_compat_msghdr(&msg_sys, msg_compat))
  1619. return -EFAULT;
  1620. }
  1621. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1622. return -EFAULT;
  1623. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1624. if (!sock)
  1625. goto out;
  1626. /* do not move before msg_sys is valid */
  1627. err = -EMSGSIZE;
  1628. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1629. goto out_put;
  1630. /* Check whether to allocate the iovec area */
  1631. err = -ENOMEM;
  1632. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1633. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1634. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1635. if (!iov)
  1636. goto out_put;
  1637. }
  1638. /* This will also move the address data into kernel space */
  1639. if (MSG_CMSG_COMPAT & flags) {
  1640. err = verify_compat_iovec(&msg_sys, iov,
  1641. (struct sockaddr *)&address,
  1642. VERIFY_READ);
  1643. } else
  1644. err = verify_iovec(&msg_sys, iov,
  1645. (struct sockaddr *)&address,
  1646. VERIFY_READ);
  1647. if (err < 0)
  1648. goto out_freeiov;
  1649. total_len = err;
  1650. err = -ENOBUFS;
  1651. if (msg_sys.msg_controllen > INT_MAX)
  1652. goto out_freeiov;
  1653. ctl_len = msg_sys.msg_controllen;
  1654. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1655. err =
  1656. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1657. sizeof(ctl));
  1658. if (err)
  1659. goto out_freeiov;
  1660. ctl_buf = msg_sys.msg_control;
  1661. ctl_len = msg_sys.msg_controllen;
  1662. } else if (ctl_len) {
  1663. if (ctl_len > sizeof(ctl)) {
  1664. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1665. if (ctl_buf == NULL)
  1666. goto out_freeiov;
  1667. }
  1668. err = -EFAULT;
  1669. /*
  1670. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1671. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1672. * checking falls down on this.
  1673. */
  1674. if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
  1675. ctl_len))
  1676. goto out_freectl;
  1677. msg_sys.msg_control = ctl_buf;
  1678. }
  1679. msg_sys.msg_flags = flags;
  1680. if (sock->file->f_flags & O_NONBLOCK)
  1681. msg_sys.msg_flags |= MSG_DONTWAIT;
  1682. err = sock_sendmsg(sock, &msg_sys, total_len);
  1683. out_freectl:
  1684. if (ctl_buf != ctl)
  1685. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1686. out_freeiov:
  1687. if (iov != iovstack)
  1688. sock_kfree_s(sock->sk, iov, iov_size);
  1689. out_put:
  1690. fput_light(sock->file, fput_needed);
  1691. out:
  1692. return err;
  1693. }
  1694. /*
  1695. * BSD recvmsg interface
  1696. */
  1697. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
  1698. unsigned int flags)
  1699. {
  1700. struct compat_msghdr __user *msg_compat =
  1701. (struct compat_msghdr __user *)msg;
  1702. struct socket *sock;
  1703. struct iovec iovstack[UIO_FASTIOV];
  1704. struct iovec *iov = iovstack;
  1705. struct msghdr msg_sys;
  1706. unsigned long cmsg_ptr;
  1707. int err, iov_size, total_len, len;
  1708. int fput_needed;
  1709. /* kernel mode address */
  1710. struct sockaddr_storage addr;
  1711. /* user mode address pointers */
  1712. struct sockaddr __user *uaddr;
  1713. int __user *uaddr_len;
  1714. if (MSG_CMSG_COMPAT & flags) {
  1715. if (get_compat_msghdr(&msg_sys, msg_compat))
  1716. return -EFAULT;
  1717. }
  1718. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1719. return -EFAULT;
  1720. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1721. if (!sock)
  1722. goto out;
  1723. err = -EMSGSIZE;
  1724. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1725. goto out_put;
  1726. /* Check whether to allocate the iovec area */
  1727. err = -ENOMEM;
  1728. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1729. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1730. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1731. if (!iov)
  1732. goto out_put;
  1733. }
  1734. /*
  1735. * Save the user-mode address (verify_iovec will change the
  1736. * kernel msghdr to use the kernel address space)
  1737. */
  1738. uaddr = (__force void __user *)msg_sys.msg_name;
  1739. uaddr_len = COMPAT_NAMELEN(msg);
  1740. if (MSG_CMSG_COMPAT & flags) {
  1741. err = verify_compat_iovec(&msg_sys, iov,
  1742. (struct sockaddr *)&addr,
  1743. VERIFY_WRITE);
  1744. } else
  1745. err = verify_iovec(&msg_sys, iov,
  1746. (struct sockaddr *)&addr,
  1747. VERIFY_WRITE);
  1748. if (err < 0)
  1749. goto out_freeiov;
  1750. total_len = err;
  1751. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1752. msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1753. if (sock->file->f_flags & O_NONBLOCK)
  1754. flags |= MSG_DONTWAIT;
  1755. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1756. if (err < 0)
  1757. goto out_freeiov;
  1758. len = err;
  1759. if (uaddr != NULL) {
  1760. err = move_addr_to_user((struct sockaddr *)&addr,
  1761. msg_sys.msg_namelen, uaddr,
  1762. uaddr_len);
  1763. if (err < 0)
  1764. goto out_freeiov;
  1765. }
  1766. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1767. COMPAT_FLAGS(msg));
  1768. if (err)
  1769. goto out_freeiov;
  1770. if (MSG_CMSG_COMPAT & flags)
  1771. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1772. &msg_compat->msg_controllen);
  1773. else
  1774. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1775. &msg->msg_controllen);
  1776. if (err)
  1777. goto out_freeiov;
  1778. err = len;
  1779. out_freeiov:
  1780. if (iov != iovstack)
  1781. sock_kfree_s(sock->sk, iov, iov_size);
  1782. out_put:
  1783. fput_light(sock->file, fput_needed);
  1784. out:
  1785. return err;
  1786. }
  1787. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1788. /* Argument list sizes for sys_socketcall */
  1789. #define AL(x) ((x) * sizeof(unsigned long))
  1790. static const unsigned char nargs[19]={
  1791. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1792. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1793. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
  1794. AL(6)
  1795. };
  1796. #undef AL
  1797. /*
  1798. * System call vectors.
  1799. *
  1800. * Argument checking cleaned up. Saved 20% in size.
  1801. * This function doesn't need to set the kernel lock because
  1802. * it is set by the callees.
  1803. */
  1804. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1805. {
  1806. unsigned long a[6];
  1807. unsigned long a0, a1;
  1808. int err;
  1809. if (call < 1 || call > SYS_PACCEPT)
  1810. return -EINVAL;
  1811. /* copy_from_user should be SMP safe. */
  1812. if (copy_from_user(a, args, nargs[call]))
  1813. return -EFAULT;
  1814. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1815. if (err)
  1816. return err;
  1817. a0 = a[0];
  1818. a1 = a[1];
  1819. switch (call) {
  1820. case SYS_SOCKET:
  1821. err = sys_socket(a0, a1, a[2]);
  1822. break;
  1823. case SYS_BIND:
  1824. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1825. break;
  1826. case SYS_CONNECT:
  1827. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1828. break;
  1829. case SYS_LISTEN:
  1830. err = sys_listen(a0, a1);
  1831. break;
  1832. case SYS_ACCEPT:
  1833. err =
  1834. do_accept(a0, (struct sockaddr __user *)a1,
  1835. (int __user *)a[2], 0);
  1836. break;
  1837. case SYS_GETSOCKNAME:
  1838. err =
  1839. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1840. (int __user *)a[2]);
  1841. break;
  1842. case SYS_GETPEERNAME:
  1843. err =
  1844. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1845. (int __user *)a[2]);
  1846. break;
  1847. case SYS_SOCKETPAIR:
  1848. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1849. break;
  1850. case SYS_SEND:
  1851. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1852. break;
  1853. case SYS_SENDTO:
  1854. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1855. (struct sockaddr __user *)a[4], a[5]);
  1856. break;
  1857. case SYS_RECV:
  1858. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1859. break;
  1860. case SYS_RECVFROM:
  1861. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1862. (struct sockaddr __user *)a[4],
  1863. (int __user *)a[5]);
  1864. break;
  1865. case SYS_SHUTDOWN:
  1866. err = sys_shutdown(a0, a1);
  1867. break;
  1868. case SYS_SETSOCKOPT:
  1869. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1870. break;
  1871. case SYS_GETSOCKOPT:
  1872. err =
  1873. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1874. (int __user *)a[4]);
  1875. break;
  1876. case SYS_SENDMSG:
  1877. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1878. break;
  1879. case SYS_RECVMSG:
  1880. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1881. break;
  1882. case SYS_PACCEPT:
  1883. err =
  1884. sys_paccept(a0, (struct sockaddr __user *)a1,
  1885. (int __user *)a[2],
  1886. (const sigset_t __user *) a[3],
  1887. a[4], a[5]);
  1888. break;
  1889. default:
  1890. err = -EINVAL;
  1891. break;
  1892. }
  1893. return err;
  1894. }
  1895. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1896. /**
  1897. * sock_register - add a socket protocol handler
  1898. * @ops: description of protocol
  1899. *
  1900. * This function is called by a protocol handler that wants to
  1901. * advertise its address family, and have it linked into the
  1902. * socket interface. The value ops->family coresponds to the
  1903. * socket system call protocol family.
  1904. */
  1905. int sock_register(const struct net_proto_family *ops)
  1906. {
  1907. int err;
  1908. if (ops->family >= NPROTO) {
  1909. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1910. NPROTO);
  1911. return -ENOBUFS;
  1912. }
  1913. spin_lock(&net_family_lock);
  1914. if (net_families[ops->family])
  1915. err = -EEXIST;
  1916. else {
  1917. net_families[ops->family] = ops;
  1918. err = 0;
  1919. }
  1920. spin_unlock(&net_family_lock);
  1921. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1922. return err;
  1923. }
  1924. /**
  1925. * sock_unregister - remove a protocol handler
  1926. * @family: protocol family to remove
  1927. *
  1928. * This function is called by a protocol handler that wants to
  1929. * remove its address family, and have it unlinked from the
  1930. * new socket creation.
  1931. *
  1932. * If protocol handler is a module, then it can use module reference
  1933. * counts to protect against new references. If protocol handler is not
  1934. * a module then it needs to provide its own protection in
  1935. * the ops->create routine.
  1936. */
  1937. void sock_unregister(int family)
  1938. {
  1939. BUG_ON(family < 0 || family >= NPROTO);
  1940. spin_lock(&net_family_lock);
  1941. net_families[family] = NULL;
  1942. spin_unlock(&net_family_lock);
  1943. synchronize_rcu();
  1944. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1945. }
  1946. static int __init sock_init(void)
  1947. {
  1948. /*
  1949. * Initialize sock SLAB cache.
  1950. */
  1951. sk_init();
  1952. /*
  1953. * Initialize skbuff SLAB cache
  1954. */
  1955. skb_init();
  1956. /*
  1957. * Initialize the protocols module.
  1958. */
  1959. init_inodecache();
  1960. register_filesystem(&sock_fs_type);
  1961. sock_mnt = kern_mount(&sock_fs_type);
  1962. /* The real protocol initialization is performed in later initcalls.
  1963. */
  1964. #ifdef CONFIG_NETFILTER
  1965. netfilter_init();
  1966. #endif
  1967. return 0;
  1968. }
  1969. core_initcall(sock_init); /* early initcall */
  1970. #ifdef CONFIG_PROC_FS
  1971. void socket_seq_show(struct seq_file *seq)
  1972. {
  1973. int cpu;
  1974. int counter = 0;
  1975. for_each_possible_cpu(cpu)
  1976. counter += per_cpu(sockets_in_use, cpu);
  1977. /* It can be negative, by the way. 8) */
  1978. if (counter < 0)
  1979. counter = 0;
  1980. seq_printf(seq, "sockets: used %d\n", counter);
  1981. }
  1982. #endif /* CONFIG_PROC_FS */
  1983. #ifdef CONFIG_COMPAT
  1984. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1985. unsigned long arg)
  1986. {
  1987. struct socket *sock = file->private_data;
  1988. int ret = -ENOIOCTLCMD;
  1989. struct sock *sk;
  1990. struct net *net;
  1991. sk = sock->sk;
  1992. net = sock_net(sk);
  1993. if (sock->ops->compat_ioctl)
  1994. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1995. if (ret == -ENOIOCTLCMD &&
  1996. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  1997. ret = compat_wext_handle_ioctl(net, cmd, arg);
  1998. return ret;
  1999. }
  2000. #endif
  2001. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2002. {
  2003. return sock->ops->bind(sock, addr, addrlen);
  2004. }
  2005. int kernel_listen(struct socket *sock, int backlog)
  2006. {
  2007. return sock->ops->listen(sock, backlog);
  2008. }
  2009. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2010. {
  2011. struct sock *sk = sock->sk;
  2012. int err;
  2013. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2014. newsock);
  2015. if (err < 0)
  2016. goto done;
  2017. err = sock->ops->accept(sock, *newsock, flags);
  2018. if (err < 0) {
  2019. sock_release(*newsock);
  2020. *newsock = NULL;
  2021. goto done;
  2022. }
  2023. (*newsock)->ops = sock->ops;
  2024. done:
  2025. return err;
  2026. }
  2027. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2028. int flags)
  2029. {
  2030. return sock->ops->connect(sock, addr, addrlen, flags);
  2031. }
  2032. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2033. int *addrlen)
  2034. {
  2035. return sock->ops->getname(sock, addr, addrlen, 0);
  2036. }
  2037. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2038. int *addrlen)
  2039. {
  2040. return sock->ops->getname(sock, addr, addrlen, 1);
  2041. }
  2042. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2043. char *optval, int *optlen)
  2044. {
  2045. mm_segment_t oldfs = get_fs();
  2046. int err;
  2047. set_fs(KERNEL_DS);
  2048. if (level == SOL_SOCKET)
  2049. err = sock_getsockopt(sock, level, optname, optval, optlen);
  2050. else
  2051. err = sock->ops->getsockopt(sock, level, optname, optval,
  2052. optlen);
  2053. set_fs(oldfs);
  2054. return err;
  2055. }
  2056. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2057. char *optval, int optlen)
  2058. {
  2059. mm_segment_t oldfs = get_fs();
  2060. int err;
  2061. set_fs(KERNEL_DS);
  2062. if (level == SOL_SOCKET)
  2063. err = sock_setsockopt(sock, level, optname, optval, optlen);
  2064. else
  2065. err = sock->ops->setsockopt(sock, level, optname, optval,
  2066. optlen);
  2067. set_fs(oldfs);
  2068. return err;
  2069. }
  2070. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2071. size_t size, int flags)
  2072. {
  2073. if (sock->ops->sendpage)
  2074. return sock->ops->sendpage(sock, page, offset, size, flags);
  2075. return sock_no_sendpage(sock, page, offset, size, flags);
  2076. }
  2077. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2078. {
  2079. mm_segment_t oldfs = get_fs();
  2080. int err;
  2081. set_fs(KERNEL_DS);
  2082. err = sock->ops->ioctl(sock, cmd, arg);
  2083. set_fs(oldfs);
  2084. return err;
  2085. }
  2086. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2087. {
  2088. return sock->ops->shutdown(sock, how);
  2089. }
  2090. EXPORT_SYMBOL(sock_create);
  2091. EXPORT_SYMBOL(sock_create_kern);
  2092. EXPORT_SYMBOL(sock_create_lite);
  2093. EXPORT_SYMBOL(sock_map_fd);
  2094. EXPORT_SYMBOL(sock_recvmsg);
  2095. EXPORT_SYMBOL(sock_register);
  2096. EXPORT_SYMBOL(sock_release);
  2097. EXPORT_SYMBOL(sock_sendmsg);
  2098. EXPORT_SYMBOL(sock_unregister);
  2099. EXPORT_SYMBOL(sock_wake_async);
  2100. EXPORT_SYMBOL(sockfd_lookup);
  2101. EXPORT_SYMBOL(kernel_sendmsg);
  2102. EXPORT_SYMBOL(kernel_recvmsg);
  2103. EXPORT_SYMBOL(kernel_bind);
  2104. EXPORT_SYMBOL(kernel_listen);
  2105. EXPORT_SYMBOL(kernel_accept);
  2106. EXPORT_SYMBOL(kernel_connect);
  2107. EXPORT_SYMBOL(kernel_getsockname);
  2108. EXPORT_SYMBOL(kernel_getpeername);
  2109. EXPORT_SYMBOL(kernel_getsockopt);
  2110. EXPORT_SYMBOL(kernel_setsockopt);
  2111. EXPORT_SYMBOL(kernel_sendpage);
  2112. EXPORT_SYMBOL(kernel_sock_ioctl);
  2113. EXPORT_SYMBOL(kernel_sock_shutdown);