socket.c 54 KB

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