socket.c 51 KB

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