socket.c 55 KB

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