socket.c 54 KB

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