socket.c 55 KB

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