socket.c 49 KB

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