socket.c 49 KB

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