├── .gitignore ├── AUTHOR ├── LICENSE ├── README.md ├── WARNING ├── sample ├── tcp │ ├── Makefile │ ├── ksocket.h │ ├── ksocket_tcp_cli_demo.c │ └── ksocket_tcp_srv_demo.c └── udp │ ├── Makefile │ ├── ksocket.h │ ├── ksocket_udp_cli_demo.c │ └── ksocket_udp_srv_demo.c └── src ├── Makefile ├── ksocket.c └── ksocket.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.cmd 3 | *.ko 4 | *.mod.o 5 | *.o 6 | *.symvers 7 | *.order 8 | .tmp_versions 9 | *.mod.c 10 | *.d 11 | -------------------------------------------------------------------------------- /AUTHOR: -------------------------------------------------------------------------------- 1 | Author 2 | 3 | song.xian-guang@hotmail.com 4 | 5 | (C)2007-2008, China 6 | 7 | 2017 Hardik Bagdi(hbagdi1@binghamton.edu) 8 | Changes for Compatibility with Linux 4.9 to use iov_iter 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | GNU GENERAL PUBLIC LICENSE 3 | Version 2, June 1991 4 | 5 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 6 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your 13 | freedom to share and change it. By contrast, the GNU General Public 14 | License is intended to guarantee your freedom to share and change free 15 | software--to make sure the software is free for all its users. This 16 | General Public License applies to most of the Free Software 17 | Foundation's software and to any other program whose authors commit to 18 | using it. (Some other Free Software Foundation software is covered by 19 | the GNU Library General Public License instead.) You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | this service if you wish), that you receive source code or can get it 26 | if you want it, that you can change the software or use pieces of it 27 | in new free programs; and that you know you can do these things. 28 | 29 | To protect your rights, we need to make restrictions that forbid 30 | anyone to deny you these rights or to ask you to surrender the rights. 31 | These restrictions translate to certain responsibilities for you if you 32 | distribute copies of the software, or if you modify it. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must give the recipients all the rights that 36 | you have. You must make sure that they, too, receive or can get the 37 | source code. And you must show them these terms so they know their 38 | rights. 39 | 40 | We protect your rights with two steps: (1) copyright the software, and 41 | (2) offer you this license which gives you legal permission to copy, 42 | distribute and/or modify the software. 43 | 44 | Also, for each author's protection and ours, we want to make certain 45 | that everyone understands that there is no warranty for this free 46 | software. If the software is modified by someone else and passed on, we 47 | want its recipients to know that what they have is not the original, so 48 | that any problems introduced by others will not reflect on the original 49 | authors' reputations. 50 | 51 | Finally, any free program is threatened constantly by software 52 | patents. We wish to avoid the danger that redistributors of a free 53 | program will individually obtain patent licenses, in effect making the 54 | program proprietary. To prevent this, we have made it clear that any 55 | patent must be licensed for everyone's free use or not licensed at all. 56 | 57 | The precise terms and conditions for copying, distribution and 58 | modification follow. 59 | 60 | GNU GENERAL PUBLIC LICENSE 61 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 62 | 63 | 0. This License applies to any program or other work which contains 64 | a notice placed by the copyright holder saying it may be distributed 65 | under the terms of this General Public License. The "Program", below, 66 | refers to any such program or work, and a "work based on the Program" 67 | means either the Program or any derivative work under copyright law: 68 | that is to say, a work containing the Program or a portion of it, 69 | either verbatim or with modifications and/or translated into another 70 | language. (Hereinafter, translation is included without limitation in 71 | the term "modification".) Each licensee is addressed as "you". 72 | 73 | Activities other than copying, distribution and modification are not 74 | covered by this License; they are outside its scope. The act of 75 | running the Program is not restricted, and the output from the Program 76 | is covered only if its contents constitute a work based on the 77 | Program (independent of having been made by running the Program). 78 | Whether that is true depends on what the Program does. 79 | 80 | 1. You may copy and distribute verbatim copies of the Program's 81 | source code as you receive it, in any medium, provided that you 82 | conspicuously and appropriately publish on each copy an appropriate 83 | copyright notice and disclaimer of warranty; keep intact all the 84 | notices that refer to this License and to the absence of any warranty; 85 | and give any other recipients of the Program a copy of this License 86 | along with the Program. 87 | 88 | You may charge a fee for the physical act of transferring a copy, and 89 | you may at your option offer warranty protection in exchange for a fee. 90 | 91 | 2. You may modify your copy or copies of the Program or any portion 92 | of it, thus forming a work based on the Program, and copy and 93 | distribute such modifications or work under the terms of Section 1 94 | above, provided that you also meet all of these conditions: 95 | 96 | a) You must cause the modified files to carry prominent notices 97 | stating that you changed the files and the date of any change. 98 | 99 | b) You must cause any work that you distribute or publish, that in 100 | whole or in part contains or is derived from the Program or any 101 | part thereof, to be licensed as a whole at no charge to all third 102 | parties under the terms of this License. 103 | 104 | c) If the modified program normally reads commands interactively 105 | when run, you must cause it, when started running for such 106 | interactive use in the most ordinary way, to print or display an 107 | announcement including an appropriate copyright notice and a 108 | notice that there is no warranty (or else, saying that you provide 109 | a warranty) and that users may redistribute the program under 110 | these conditions, and telling the user how to view a copy of this 111 | License. (Exception: if the Program itself is interactive but 112 | does not normally print such an announcement, your work based on 113 | the Program is not required to print an announcement.) 114 | 115 | These requirements apply to the modified work as a whole. If 116 | identifiable sections of that work are not derived from the Program, 117 | and can be reasonably considered independent and separate works in 118 | themselves, then this License, and its terms, do not apply to those 119 | sections when you distribute them as separate works. But when you 120 | distribute the same sections as part of a whole which is a work based 121 | on the Program, the distribution of the whole must be on the terms of 122 | this License, whose permissions for other licensees extend to the 123 | entire whole, and thus to each and every part regardless of who wrote it. 124 | 125 | Thus, it is not the intent of this section to claim rights or contest 126 | your rights to work written entirely by you; rather, the intent is to 127 | exercise the right to control the distribution of derivative or 128 | collective works based on the Program. 129 | 130 | In addition, mere aggregation of another work not based on the Program 131 | with the Program (or with a work based on the Program) on a volume of 132 | a storage or distribution medium does not bring the other work under 133 | the scope of this License. 134 | 135 | 3. You may copy and distribute the Program (or a work based on it, 136 | under Section 2) in object code or executable form under the terms of 137 | Sections 1 and 2 above provided that you also do one of the following: 138 | 139 | a) Accompany it with the complete corresponding machine-readable 140 | source code, which must be distributed under the terms of Sections 141 | 1 and 2 above on a medium customarily used for software interchange; or, 142 | 143 | b) Accompany it with a written offer, valid for at least three 144 | years, to give any third party, for a charge no more than your 145 | cost of physically performing source distribution, a complete 146 | machine-readable copy of the corresponding source code, to be 147 | distributed under the terms of Sections 1 and 2 above on a medium 148 | customarily used for software interchange; or, 149 | 150 | c) Accompany it with the information you received as to the offer 151 | to distribute corresponding source code. (This alternative is 152 | allowed only for noncommercial distribution and only if you 153 | received the program in object code or executable form with such 154 | an offer, in accord with Subsection b above.) 155 | 156 | The source code for a work means the preferred form of the work for 157 | making modifications to it. For an executable work, complete source 158 | code means all the source code for all modules it contains, plus any 159 | associated interface definition files, plus the scripts used to 160 | control compilation and installation of the executable. However, as a 161 | special exception, the source code distributed need not include 162 | anything that is normally distributed (in either source or binary 163 | form) with the major components (compiler, kernel, and so on) of the 164 | operating system on which the executable runs, unless that component 165 | itself accompanies the executable. 166 | 167 | If distribution of executable or object code is made by offering 168 | access to copy from a designated place, then offering equivalent 169 | access to copy the source code from the same place counts as 170 | distribution of the source code, even though third parties are not 171 | compelled to copy the source along with the object code. 172 | 173 | 4. You may not copy, modify, sublicense, or distribute the Program 174 | except as expressly provided under this License. Any attempt 175 | otherwise to copy, modify, sublicense or distribute the Program is 176 | void, and will automatically terminate your rights under this License. 177 | However, parties who have received copies, or rights, from you under 178 | this License will not have their licenses terminated so long as such 179 | parties remain in full compliance. 180 | 181 | 5. You are not required to accept this License, since you have not 182 | signed it. However, nothing else grants you permission to modify or 183 | distribute the Program or its derivative works. These actions are 184 | prohibited by law if you do not accept this License. Therefore, by 185 | modifying or distributing the Program (or any work based on the 186 | Program), you indicate your acceptance of this License to do so, and 187 | all its terms and conditions for copying, distributing or modifying 188 | the Program or works based on it. 189 | 190 | 6. Each time you redistribute the Program (or any work based on the 191 | Program), the recipient automatically receives a license from the 192 | original licensor to copy, distribute or modify the Program subject to 193 | these terms and conditions. You may not impose any further 194 | restrictions on the recipients' exercise of the rights granted herein. 195 | You are not responsible for enforcing compliance by third parties to 196 | this License. 197 | 198 | 7. If, as a consequence of a court judgment or allegation of patent 199 | infringement or for any other reason (not limited to patent issues), 200 | conditions are imposed on you (whether by court order, agreement or 201 | otherwise) that contradict the conditions of this License, they do not 202 | excuse you from the conditions of this License. If you cannot 203 | distribute so as to satisfy simultaneously your obligations under this 204 | License and any other pertinent obligations, then as a consequence you 205 | may not distribute the Program at all. For example, if a patent 206 | license would not permit royalty-free redistribution of the Program by 207 | all those who receive copies directly or indirectly through you, then 208 | the only way you could satisfy both it and this License would be to 209 | refrain entirely from distribution of the Program. 210 | 211 | If any portion of this section is held invalid or unenforceable under 212 | any particular circumstance, the balance of the section is intended to 213 | apply and the section as a whole is intended to apply in other 214 | circumstances. 215 | 216 | It is not the purpose of this section to induce you to infringe any 217 | patents or other property right claims or to contest validity of any 218 | such claims; this section has the sole purpose of protecting the 219 | integrity of the free software distribution system, which is 220 | implemented by public license practices. Many people have made 221 | generous contributions to the wide range of software distributed 222 | through that system in reliance on consistent application of that 223 | system; it is up to the author/donor to decide if he or she is willing 224 | to distribute software through any other system and a licensee cannot 225 | impose that choice. 226 | 227 | This section is intended to make thoroughly clear what is believed to 228 | be a consequence of the rest of this License. 229 | 230 | 8. If the distribution and/or use of the Program is restricted in 231 | certain countries either by patents or by copyrighted interfaces, the 232 | original copyright holder who places the Program under this License 233 | may add an explicit geographical distribution limitation excluding 234 | those countries, so that distribution is permitted only in or among 235 | countries not thus excluded. In such case, this License incorporates 236 | the limitation as if written in the body of this License. 237 | 238 | 9. The Free Software Foundation may publish revised and/or new versions 239 | of the General Public License from time to time. Such new versions will 240 | be similar in spirit to the present version, but may differ in detail to 241 | address new problems or concerns. 242 | 243 | Each version is given a distinguishing version number. If the Program 244 | specifies a version number of this License which applies to it and "any 245 | later version", you have the option of following the terms and conditions 246 | either of that version or of any later version published by the Free 247 | Software Foundation. If the Program does not specify a version number of 248 | this License, you may choose any version ever published by the Free Software 249 | Foundation. 250 | 251 | 10. If you wish to incorporate parts of the Program into other free 252 | programs whose distribution conditions are different, write to the author 253 | to ask for permission. For software which is copyrighted by the Free 254 | Software Foundation, write to the Free Software Foundation; we sometimes 255 | make exceptions for this. Our decision will be guided by the two goals 256 | of preserving the free status of all derivatives of our free software and 257 | of promoting the sharing and reuse of software generally. 258 | 259 | NO WARRANTY 260 | 261 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 262 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 263 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 264 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 265 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 266 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 267 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 268 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 269 | REPAIR OR CORRECTION. 270 | 271 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 272 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 273 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 274 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 275 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 276 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 277 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 278 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 279 | POSSIBILITY OF SUCH DAMAGES. 280 | 281 | END OF TERMS AND CONDITIONS 282 | 283 | How to Apply These Terms to Your New Programs 284 | 285 | If you develop a new program, and you want it to be of the greatest 286 | possible use to the public, the best way to achieve this is to make it 287 | free software which everyone can redistribute and change under these terms. 288 | 289 | To do so, attach the following notices to the program. It is safest 290 | to attach them to the start of each source file to most effectively 291 | convey the exclusion of warranty; and each file should have at least 292 | the "copyright" line and a pointer to where the full notice is found. 293 | 294 | 295 | Copyright (C) 296 | 297 | This program is free software; you can redistribute it and/or modify 298 | it under the terms of the GNU General Public License as published by 299 | the Free Software Foundation; either version 2 of the License, or 300 | (at your option) any later version. 301 | 302 | This program is distributed in the hope that it will be useful, 303 | but WITHOUT ANY WARRANTY; without even the implied warranty of 304 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 305 | GNU General Public License for more details. 306 | 307 | You should have received a copy of the GNU General Public License 308 | along with this program; if not, write to the Free Software 309 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 310 | 311 | 312 | Also add information on how to contact you by electronic and paper mail. 313 | 314 | If the program is interactive, make it output a short notice like this 315 | when it starts in an interactive mode: 316 | 317 | Gnomovision version 69, Copyright (C) year name of author 318 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 319 | This is free software, and you are welcome to redistribute it 320 | under certain conditions; type `show c' for details. 321 | 322 | The hypothetical commands `show w' and `show c' should show the appropriate 323 | parts of the General Public License. Of course, the commands you use may 324 | be called something other than `show w' and `show c'; they could even be 325 | mouse-clicks or menu items--whatever suits your program. 326 | 327 | You should also get your employer (if you work as a programmer) or your 328 | school, if any, to sign a "copyright disclaimer" for the program, if 329 | necessary. Here is a sample; alter the names: 330 | 331 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 332 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 333 | 334 | , 1 April 1989 335 | Ty Coon, President of Vice 336 | 337 | This General Public License does not permit incorporating your program into 338 | proprietary programs. If your program is a subroutine library, you may 339 | consider it more useful to permit linking proprietary applications with the 340 | library. If this is what you want to do, use the GNU Library General 341 | Public License instead of this License. 342 | 343 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ksocket API 2 | 3 | BSD-style socket API in kernel space for TCP/IP networking. 4 | Original ksocket for v2.6 was published at [http://ksocket.sourceforge.net/](http://ksocket.sourceforge.net/). 5 | This repository contains changes to make it compatible with new kernel versions. 6 | The ksocket API has been updated to incorporate the changes related to the [iov_iter](https://lwn.net/Articles/625077/) interface introduced in kernel v3.19+. 7 | 8 | ### Getting started 9 | ``` 10 | $ git clone https://github.com/hbagdi/ksocket.git 11 | $ cd ksocket/src 12 | $ make # make sure you have the kernel headers/tree installed 13 | $ sudo insmod ksocket.ko 14 | #now you can use the exported symbols from this kernel module 15 | ``` 16 | 17 | ### Sample 18 | Look at `sample` sub-directory for code-samples. 19 | ``` 20 | $ cd ksocket/sample/tcp/ 21 | $ make 22 | # two kernel modules are ksocket_tcp_cli_demo.ko(TCP client) and ksocket_tcp_srv_demo.ko(TCP server) 23 | $ sudo ksocket_tcp_srv_demo.ko 24 | $ sudo ksocket_tcp_cli_demo.ko 25 | $ dmesg 26 | ``` 27 | 28 | ### Support across kernel versions 29 | The original ksocket work was to support Linux 2.6, however support for v5.4.0 has been 30 | included. 31 | 32 | v5.4.0 support is a work on Linux ubuntu 5.4.0-66 x86-64, therefore there is no guarantee it works 33 | well on versions > 2.6 && < 5.3.x 34 | 35 | In order to keep the 2.6 version stable it was left without the 5.4.0 work, 36 | instead we now have two branches: v2.6 (master) and v5.4.0 37 | 38 | #### v2.6 original development 39 | https://github.com/hbagdi/ksocket 40 | 41 | #### v5.4.0 42 | https://github.com/hbagdi/ksocket/tree/linux-5.4.0 43 | 44 | ### Contributing/Reporting Bugs 45 | - Feel free to open Pull-Requests here for any enhancements/fixes. 46 | - Open an issue in the repository for any help or bugs. Make sure to mention Kernel version. 47 | 48 | ### Contact 49 | Email at hardikbagdi@gmail.com for any further help. 50 | -------------------------------------------------------------------------------- /WARNING: -------------------------------------------------------------------------------- 1 | IMPORTANT NOTICE: 2 | 3 | Use this code at your own risk. 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample/tcp/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # ksocket project 3 | # BSD-style socket APIs for kernel 2.6 developers 4 | # 5 | # @2007-2008, China 6 | # @song.xian-guang@hotmail.com (MSN Accounts) 7 | # 8 | # This code is licenced under the GPL 9 | # Feel free to contact me if any questions 10 | # 11 | # @2017 12 | # Hardik Bagdi (hbagdi1@binghamton.edu) 13 | # Changes for Compatibility with Linux 4.9 to use iov_iter 14 | 15 | .PHONY: default clean 16 | 17 | obj-m := ksocket_tcp_srv_demo.o ksocket_tcp_cli_demo.o 18 | #ksocket_tcp_srv-objs := ../ksocket.o 19 | KDIR := /lib/modules/$(shell uname -r)/build 20 | PWD := $(shell pwd) 21 | 22 | EXTRA_LDFLAGS := -I../../src 23 | 24 | ifeq ($(ADDRSAFE),true) 25 | EXTRA_CFLAGS += -DKSOCKET_ADDR_SAFE 26 | endif 27 | 28 | default: 29 | $(MAKE) -C $(KDIR) M=$(PWD) modules 30 | 31 | clean: 32 | rm -rf *.ko *.o *.mod.* .H* .tm* .*cmd Module.symvers modules.order 33 | 34 | -------------------------------------------------------------------------------- /sample/tcp/ksocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | 16 | #ifndef _ksocket_h_ 17 | #define _ksocket_h_ 18 | 19 | struct socket; 20 | struct sockaddr; 21 | struct in_addr; 22 | typedef struct socket * ksocket_t; 23 | 24 | /* BSD socket APIs prototype declaration */ 25 | extern ksocket_t ksocket(int domain, int type, int protocol); 26 | extern int kshutdown(ksocket_t socket, int how); 27 | extern int kclose(ksocket_t socket); 28 | 29 | extern int kbind(ksocket_t socket, struct sockaddr *address, int address_len); 30 | extern int klisten(ksocket_t socket, int backlog); 31 | extern int kconnect(ksocket_t socket, struct sockaddr *address, int address_len); 32 | extern ksocket_t kaccept(ksocket_t socket, struct sockaddr *address, int *address_len); 33 | 34 | extern ssize_t krecv(ksocket_t socket, void *buffer, size_t length, int flags); 35 | extern ssize_t ksend(ksocket_t socket, const void *buffer, size_t length, int flags); 36 | extern ssize_t krecvfrom(ksocket_t socket, void * buffer, size_t length, int flags, struct sockaddr * address, int * address_len); 37 | extern ssize_t ksendto(ksocket_t socket, void *message, size_t length, int flags, const struct sockaddr *dest_addr, int dest_len); 38 | 39 | extern int kgetsockname(ksocket_t socket, struct sockaddr *address, int *address_len); 40 | extern int kgetpeername(ksocket_t socket, struct sockaddr *address, int *address_len); 41 | extern int ksetsockopt(ksocket_t socket, int level, int optname, void *optval, int optlen); 42 | extern int kgetsockopt(ksocket_t socket, int level, int optname, void *optval, int *optlen); 43 | 44 | extern unsigned int inet_addr(char* ip); 45 | extern char *inet_ntoa(struct in_addr *in); /* DO NOT forget to kfree the return pointer */ 46 | 47 | #endif /* !_ksocket_h_ */ 48 | -------------------------------------------------------------------------------- /sample/tcp/ksocket_tcp_cli_demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project test sample - tcp client 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ksocket.h" 27 | 28 | #define BUF_SIZE 100 29 | 30 | int tcp_cli(void *arg) 31 | { 32 | ksocket_t sockfd_cli; 33 | struct sockaddr_in addr_srv; 34 | char buf[BUF_SIZE], *tmp; 35 | int addr_len; 36 | 37 | #ifdef KSOCKET_ADDR_SAFE 38 | mm_segment_t old_fs; 39 | old_fs = get_fs(); 40 | set_fs(KERNEL_DS); 41 | #endif 42 | 43 | memset(&addr_srv, 0, sizeof(addr_srv)); 44 | addr_srv.sin_family = AF_INET; 45 | addr_srv.sin_port = htons(4444); 46 | addr_srv.sin_addr.s_addr = inet_addr("127.0.0.1");; 47 | addr_len = sizeof(struct sockaddr_in); 48 | 49 | sockfd_cli = ksocket(AF_INET, SOCK_STREAM, 0); 50 | printk("sockfd_cli = 0x%p\n", sockfd_cli); 51 | if (sockfd_cli == NULL) 52 | { 53 | printk("socket failed\n"); 54 | return -1; 55 | } 56 | if (kconnect(sockfd_cli, (struct sockaddr*)&addr_srv, addr_len) < 0) 57 | { 58 | printk("connect failed\n"); 59 | return -1; 60 | } 61 | 62 | tmp = "quit"; 63 | printk("connected to : %s %d\n", tmp, ntohs(addr_srv.sin_port)); 64 | 65 | krecv(sockfd_cli, buf, 1024, 0); 66 | ksend(sockfd_cli, tmp, 4, 0); 67 | printk("got message : %s\n", buf); 68 | 69 | kclose(sockfd_cli); 70 | #ifdef KSOCKET_ADDR_SAFE 71 | set_fs(old_fs); 72 | #endif 73 | 74 | return 0; 75 | } 76 | 77 | static int ksocket_tcp_cli_init(void) 78 | { 79 | kthread_run(tcp_cli,NULL,"tcp_cli_kthread"); 80 | printk("ksocket tcp cli init ok\n"); 81 | return 0; 82 | } 83 | 84 | static void ksocket_tcp_cli_exit(void) 85 | { 86 | printk("ksocket tcp cli exit\n"); 87 | } 88 | 89 | module_init(ksocket_tcp_cli_init); 90 | module_exit(ksocket_tcp_cli_exit); 91 | 92 | MODULE_LICENSE("Dual BSD/GPL"); 93 | -------------------------------------------------------------------------------- /sample/tcp/ksocket_tcp_srv_demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project test sample - tcp server 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ksocket.h" 27 | 28 | #define BUF_SIZE 100 29 | 30 | static int port = 4444; 31 | module_param(port, int, 0444); 32 | 33 | int tcp_srv(void *arg) 34 | { 35 | ksocket_t sockfd_srv, sockfd_cli; 36 | struct sockaddr_in addr_srv; 37 | struct sockaddr_in addr_cli; 38 | char buf[BUF_SIZE], *tmp; 39 | int addr_len, len; 40 | 41 | #ifdef KSOCKET_ADDR_SAFE 42 | mm_segment_t old_fs; 43 | old_fs = get_fs(); 44 | set_fs(KERNEL_DS); 45 | #endif 46 | 47 | sockfd_srv = sockfd_cli = NULL; 48 | memset(&addr_cli, 0, sizeof(addr_cli)); 49 | memset(&addr_srv, 0, sizeof(addr_srv)); 50 | addr_srv.sin_family = AF_INET; 51 | addr_srv.sin_port = htons(port); 52 | addr_srv.sin_addr.s_addr = INADDR_ANY; 53 | addr_len = sizeof(struct sockaddr_in); 54 | 55 | sockfd_srv = ksocket(AF_INET, SOCK_STREAM, 0); 56 | printk("sockfd_srv = 0x%p\n", sockfd_srv); 57 | if (sockfd_srv == NULL) 58 | { 59 | printk("socket failed\n"); 60 | return -1; 61 | } 62 | if (kbind(sockfd_srv, (struct sockaddr *)&addr_srv, addr_len) < 0) 63 | { 64 | printk("bind failed\n"); 65 | return -1; 66 | } 67 | 68 | if (klisten(sockfd_srv, 10) < 0) 69 | { 70 | printk("listen failed\n"); 71 | return -1; 72 | } 73 | 74 | sockfd_cli = kaccept(sockfd_srv, (struct sockaddr *)&addr_cli, &addr_len); 75 | if (sockfd_cli == NULL) 76 | { 77 | printk("accept failed\n"); 78 | return -1; 79 | } 80 | else 81 | printk("sockfd_cli = 0x%p\n", sockfd_cli); 82 | 83 | tmp = inet_ntoa(&addr_cli.sin_addr); 84 | printk("got connected from : %s %d\n", tmp, ntohs(addr_cli.sin_port)); 85 | kfree(tmp); 86 | 87 | len = sprintf(buf, "%s", "Hello, welcome to ksocket tcp srv service\n"); 88 | ksend(sockfd_cli, buf, len, 0); 89 | 90 | while (1) 91 | { 92 | memset(buf, 0, sizeof(buf)); 93 | len = krecv(sockfd_cli, buf, sizeof(buf), 0); 94 | if (len > 0) 95 | { 96 | printk("got message : %s\n", buf); 97 | ksend(sockfd_cli, buf, len, 0); 98 | if (memcmp(buf, "quit", 4) == 0) 99 | break; 100 | } 101 | } 102 | 103 | kclose(sockfd_cli); 104 | kclose(sockfd_srv); 105 | #ifdef KSOCKET_ADDR_SAFE 106 | set_fs(old_fs); 107 | #endif 108 | 109 | return 0; 110 | } 111 | 112 | static int ksocket_tcp_srv_init(void) 113 | { 114 | kthread_run(tcp_srv, NULL, "tcp_srv_kthread"); 115 | 116 | printk("ksocket tcp srv init ok\n"); 117 | return 0; 118 | } 119 | 120 | static void ksocket_tcp_srv_exit(void) 121 | { 122 | printk("ksocket tcp srv exit\n"); 123 | } 124 | 125 | module_init(ksocket_tcp_srv_init); 126 | module_exit(ksocket_tcp_srv_exit); 127 | 128 | MODULE_LICENSE("Dual BSD/GPL"); 129 | -------------------------------------------------------------------------------- /sample/udp/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # ksocket project 3 | # BSD-style socket APIs for kernel 2.6 developers 4 | # 5 | # @2007-2008, China 6 | # @song.xian-guang@hotmail.com (MSN Accounts) 7 | # 8 | # This code is licenced under the GPL 9 | # Feel free to contact me if any questions 10 | # @2017 11 | # Hardik Bagdi (hbagdi1@binghamton.edu) 12 | # Changes for Compatibility with Linux 4.9 to use iov_iter 13 | # 14 | 15 | .PHONY: default clean 16 | 17 | obj-m := ksocket_udp_srv_demo.o ksocket_udp_cli_demo.o 18 | #ksocket_tcp_srv-objs := ../ksocket.o 19 | KDIR := /lib/modules/$(shell uname -r)/build 20 | PWD := $(shell pwd) 21 | 22 | EXTRA_LDFLAGS := -I../../src 23 | 24 | ifeq ($(ADDRSAFE),true) 25 | EXTRA_CFLAGS += -DKSOCKET_ADDR_SAFE 26 | endif 27 | 28 | default: 29 | $(MAKE) -C $(KDIR) M=$(PWD) modules 30 | 31 | clean: 32 | rm -rf *.ko *.o *.mod.* .H* .tm* .*cmd Module.symvers modules.order 33 | 34 | -------------------------------------------------------------------------------- /sample/udp/ksocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | * 15 | */ 16 | 17 | #ifndef _ksocket_h_ 18 | #define _ksocket_h_ 19 | 20 | struct socket; 21 | struct sockaddr; 22 | struct in_addr; 23 | typedef struct socket * ksocket_t; 24 | 25 | /* BSD socket APIs prototype declaration */ 26 | extern ksocket_t ksocket(int domain, int type, int protocol); 27 | extern int kshutdown(ksocket_t socket, int how); 28 | extern int kclose(ksocket_t socket); 29 | 30 | extern int kbind(ksocket_t socket, struct sockaddr *address, int address_len); 31 | extern int klisten(ksocket_t socket, int backlog); 32 | extern int kconnect(ksocket_t socket, struct sockaddr *address, int address_len); 33 | extern ksocket_t kaccept(ksocket_t socket, struct sockaddr *address, int *address_len); 34 | 35 | extern ssize_t krecv(ksocket_t socket, void *buffer, size_t length, int flags); 36 | extern ssize_t ksend(ksocket_t socket, const void *buffer, size_t length, int flags); 37 | extern ssize_t krecvfrom(ksocket_t socket, void * buffer, size_t length, int flags, struct sockaddr * address, int * address_len); 38 | extern ssize_t ksendto(ksocket_t socket, void *message, size_t length, int flags, const struct sockaddr *dest_addr, int dest_len); 39 | 40 | extern int kgetsockname(ksocket_t socket, struct sockaddr *address, int *address_len); 41 | extern int kgetpeername(ksocket_t socket, struct sockaddr *address, int *address_len); 42 | extern int ksetsockopt(ksocket_t socket, int level, int optname, void *optval, int optlen); 43 | extern int kgetsockopt(ksocket_t socket, int level, int optname, void *optval, int *optlen); 44 | 45 | extern unsigned int inet_addr(char* ip); 46 | extern char *inet_ntoa(struct in_addr *in); /* DO NOT forget to kfree the return pointer */ 47 | 48 | #endif /* !_ksocket_h_ */ 49 | -------------------------------------------------------------------------------- /sample/udp/ksocket_udp_cli_demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project test sample - udp client 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ksocket.h" 27 | 28 | #define BUF_SIZE 100 29 | 30 | int udp_cli(void *arg) 31 | { 32 | ksocket_t sockfd_cli; 33 | struct sockaddr_in addr_srv; 34 | char buf[BUF_SIZE], *tmp; 35 | int addr_len; 36 | 37 | #ifdef KSOCKET_ADDR_SAFE 38 | mm_segment_t old_fs; 39 | old_fs = get_fs(); 40 | set_fs(KERNEL_DS); 41 | #endif 42 | 43 | memset(&addr_srv, 0, sizeof(addr_srv)); 44 | addr_srv.sin_family = AF_INET; 45 | addr_srv.sin_port = htons(4444); 46 | addr_srv.sin_addr.s_addr = inet_addr("127.0.0.1");; 47 | addr_len = sizeof(struct sockaddr_in); 48 | 49 | sockfd_cli = ksocket(AF_INET, SOCK_DGRAM, 0); 50 | printk("sockfd_cli = 0x%p\n", sockfd_cli); 51 | if (sockfd_cli == NULL) 52 | { 53 | printk("socket failed\n"); 54 | return -1; 55 | } 56 | tmp = "quit"; 57 | ksendto(sockfd_cli, tmp, 4, 0,(struct sockaddr*)&addr_srv, sizeof(addr_srv)); 58 | krecvfrom(sockfd_cli, buf, 1024, 0,(struct sockaddr*)&addr_srv, &addr_len); 59 | printk("got message : %s\n", buf); 60 | kclose(sockfd_cli); 61 | #ifdef KSOCKET_ADDR_SAFE 62 | set_fs(old_fs); 63 | #endif 64 | 65 | return 0; 66 | } 67 | 68 | static int ksocket_udp_cli_init(void) 69 | { 70 | kthread_run(udp_cli,NULL,"tcp_cli_kthread"); 71 | printk("ksocket udp cli init ok\n"); 72 | return 0; 73 | } 74 | 75 | static void ksocket_udp_cli_exit(void) 76 | { 77 | printk("ksocket udp cli exit\n"); 78 | } 79 | 80 | module_init(ksocket_udp_cli_init); 81 | module_exit(ksocket_udp_cli_exit); 82 | 83 | MODULE_LICENSE("Dual BSD/GPL"); 84 | -------------------------------------------------------------------------------- /sample/udp/ksocket_udp_srv_demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project test sample - udp server 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ksocket.h" 27 | 28 | #define BUF_SIZE 100 29 | 30 | static int port = 4444; 31 | module_param(port, int, 0444); 32 | 33 | int udp_srv(void *arg) 34 | { 35 | ksocket_t sockfd_srv, sockfd_cli; 36 | struct sockaddr_in addr_srv; 37 | struct sockaddr_in addr_cli; 38 | char buf[BUF_SIZE]; 39 | int addr_len, len; 40 | 41 | #ifdef KSOCKET_ADDR_SAFE 42 | mm_segment_t old_fs; 43 | old_fs = get_fs(); 44 | set_fs(KERNEL_DS); 45 | #endif 46 | 47 | sockfd_srv = sockfd_cli = NULL; 48 | memset(&addr_cli, 0, sizeof(addr_cli)); 49 | memset(&addr_srv, 0, sizeof(addr_srv)); 50 | addr_srv.sin_family = AF_INET; 51 | addr_srv.sin_port = htons(port); 52 | addr_srv.sin_addr.s_addr = INADDR_ANY; 53 | addr_len = sizeof(struct sockaddr_in); 54 | 55 | sockfd_srv = ksocket(AF_INET, SOCK_DGRAM, 0); 56 | printk("sockfd_srv = 0x%p\n", sockfd_srv); 57 | if (sockfd_srv == NULL) 58 | { 59 | printk("socket failed\n"); 60 | return -1; 61 | } 62 | if (kbind(sockfd_srv, (struct sockaddr *)&addr_srv, addr_len) < 0) 63 | { 64 | printk("bind failed\n"); 65 | return -1; 66 | } 67 | 68 | while (1) 69 | { 70 | memset(buf, 0, sizeof(buf)); 71 | len = krecvfrom(sockfd_srv, buf, sizeof(buf), 0,(struct sockaddr*)&addr_cli,&addr_len); 72 | if (len > 0) 73 | { 74 | printk("got message : %s\n", buf); 75 | ksendto(sockfd_srv, buf, len, 0,(struct sockaddr*)&addr_cli,addr_len); 76 | if (memcmp(buf, "quit", 4) == 0) 77 | break; 78 | } 79 | } 80 | 81 | kclose(sockfd_srv); 82 | #ifdef KSOCKET_ADDR_SAFE 83 | set_fs(old_fs); 84 | #endif 85 | 86 | return 0; 87 | } 88 | 89 | static int ksocket_udp_srv_init(void) 90 | { 91 | kthread_run(udp_srv, NULL, "tcp_srv_kthread"); 92 | 93 | printk("ksocket udp srv init ok\n"); 94 | return 0; 95 | } 96 | 97 | static void ksocket_udp_srv_exit(void) 98 | { 99 | printk("ksocket udp srv exit\n"); 100 | } 101 | 102 | module_init(ksocket_udp_srv_init); 103 | module_exit(ksocket_udp_srv_exit); 104 | 105 | MODULE_LICENSE("Dual BSD/GPL"); 106 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | obj-m += ksocket.o 2 | 3 | all: 4 | make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules 5 | clean: 6 | make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean 7 | -------------------------------------------------------------------------------- /src/ksocket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | * 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "ksocket.h" 25 | //#include "nested.h" 26 | //#include "sxgdebug.h" 27 | 28 | #define KSOCKET_NAME "ksocket" 29 | #define KSOCKET_VERSION "0.0.2" 30 | #define KSOCKET_DESCPT "BSD-style socket APIs for kernel 2.6 developers" 31 | #define KSOCKET_AUTHOR "msn : song.xian-guang@hotmail.com\n"\ 32 | "blog: http://sxg.cublog.cn" 33 | #define KSOCKET_DATE "2008-05-15" 34 | 35 | MODULE_AUTHOR(KSOCKET_AUTHOR); 36 | MODULE_DESCRIPTION(KSOCKET_NAME"-"KSOCKET_VERSION"\n"KSOCKET_DESCPT); 37 | MODULE_LICENSE("Dual BSD/GPL"); 38 | 39 | /* 40 | static void (*origSk)(struct sock *sk, int bytes) = NULL; 41 | 42 | static void yh_sk_data_ready(struct sock *sk, int bytes) 43 | { 44 | if (origSk) { 45 | (*origSk)(sk, bytes); 46 | } 47 | 48 | wake_up_rcv(); 49 | return; 50 | } 51 | */ 52 | 53 | ksocket_t ksocket(int domain, int type, int protocol) 54 | { 55 | struct socket *sk = NULL; 56 | int ret = 0; 57 | 58 | ret = sock_create(domain, type, protocol, &sk); 59 | if (ret < 0) 60 | { 61 | printk(KERN_INFO "sock_create failed\n"); 62 | return NULL; 63 | } 64 | 65 | /* 66 | if (sk && sk->sk) { 67 | if (sk->sk->sk_data_ready) { 68 | origSk = sk->sk->sk_data_ready; 69 | sk->sk->sk_data_ready = yh_sk_data_ready; 70 | } else { 71 | printk(KERN_INFO "sk->sk->sk_data_ready is NULL\n"); 72 | } 73 | } else { 74 | printk(KERN_INFO "sk or sk->sk is NULL\n"); 75 | } 76 | */ 77 | 78 | printk("sock_create sk= 0x%p\n", sk); 79 | 80 | return sk; 81 | } 82 | 83 | int kbind(ksocket_t socket, struct sockaddr *address, int address_len) 84 | { 85 | struct socket *sk; 86 | int ret = 0; 87 | 88 | sk = (struct socket *)socket; 89 | ret = sk->ops->bind(sk, address, address_len); 90 | printk("kbind ret = %d\n", ret); 91 | 92 | return ret; 93 | } 94 | 95 | int klisten(ksocket_t socket, int backlog) 96 | { 97 | struct socket *sk; 98 | int ret; 99 | 100 | sk = (struct socket *)socket; 101 | 102 | if ((unsigned)backlog > SOMAXCONN) 103 | backlog = SOMAXCONN; 104 | 105 | ret = sk->ops->listen(sk, backlog); 106 | 107 | return ret; 108 | } 109 | 110 | int kconnect(ksocket_t socket, struct sockaddr *address, int address_len) 111 | { 112 | struct socket *sk; 113 | int ret; 114 | 115 | sk = (struct socket *)socket; 116 | ret = sk->ops->connect(sk, address, address_len, 0/*sk->file->f_flags*/); 117 | 118 | return ret; 119 | } 120 | 121 | ksocket_t kaccept(ksocket_t socket, struct sockaddr *address, int *address_len) 122 | { 123 | struct socket *sk; 124 | struct socket *new_sk = NULL; 125 | int ret; 126 | 127 | sk = (struct socket *)socket; 128 | 129 | printk("family = %d, type = %d, protocol = %d\n", 130 | sk->sk->sk_family, sk->type, sk->sk->sk_protocol); 131 | //new_sk = sock_alloc(); 132 | //sock_alloc() is not exported, so i use sock_create() instead 133 | ret = sock_create(sk->sk->sk_family, sk->type, sk->sk->sk_protocol, &new_sk); 134 | if (ret < 0) 135 | return NULL; 136 | if (!new_sk) 137 | return NULL; 138 | 139 | new_sk->type = sk->type; 140 | new_sk->ops = sk->ops; 141 | 142 | ret = sk->ops->accept(sk, new_sk, 0 /*sk->file->f_flags*/); 143 | if (ret < 0) 144 | goto error_kaccept; 145 | 146 | if (address) 147 | { 148 | ret = new_sk->ops->getname(new_sk, address, address_len, 2); 149 | if (ret < 0) 150 | goto error_kaccept; 151 | } 152 | 153 | return new_sk; 154 | 155 | error_kaccept: 156 | sock_release(new_sk); 157 | return NULL; 158 | } 159 | 160 | ssize_t krecv(ksocket_t socket, void *buffer, size_t length, int flags) 161 | { 162 | struct socket *sk; 163 | struct msghdr msg; 164 | struct iovec iov; 165 | int ret; 166 | #ifndef KSOCKET_ADDR_SAFE 167 | mm_segment_t old_fs; 168 | #endif 169 | 170 | memset(&msg,0,sizeof(msg)); 171 | sk = (struct socket *)socket; 172 | 173 | iov.iov_base = (void *)buffer; 174 | iov.iov_len = (__kernel_size_t)length; 175 | 176 | //type 177 | msg.msg_iter.type = READ; 178 | //address 179 | msg.msg_name = NULL; 180 | msg.msg_namelen = 0; 181 | //msg_iter 182 | msg.msg_iter.iov = &iov; 183 | msg.msg_iter.iov_offset = 0; 184 | msg.msg_iter.count = iov.iov_len; 185 | msg.msg_iter.nr_segs = 1; 186 | //control 187 | msg.msg_control = NULL; 188 | msg.msg_controllen = 0; 189 | 190 | /* 191 | * msg.msg_iov->iov_base is declared as follows: 192 | * void __user *iov_base; 193 | * which means there is an user space pointer in 'msg' 194 | * use set_fs(KERNEL_DS) to make the pointer safe to kernel space 195 | */ 196 | #ifndef KSOCKET_ADDR_SAFE 197 | old_fs = get_fs(); 198 | set_fs(KERNEL_DS); 199 | #endif 200 | //hardik 201 | ret = sock_recvmsg(sk, &msg, flags); 202 | #ifndef KSOCKET_ADDR_SAFE 203 | set_fs(old_fs); 204 | #endif 205 | if (ret < 0) 206 | goto out_krecv; 207 | //ret = msg.msg_iov.iov_len;//? 208 | 209 | out_krecv: 210 | return ret; 211 | 212 | } 213 | 214 | ssize_t ksend(ksocket_t socket, const void *buffer, size_t length, int flags) 215 | { 216 | struct socket *sk; 217 | struct msghdr msg; 218 | struct iovec iov; 219 | int len; 220 | #ifndef KSOCKET_ADDR_SAFE 221 | mm_segment_t old_fs; 222 | #endif 223 | 224 | sk = (struct socket *)socket; 225 | 226 | iov.iov_base = (void *)buffer; 227 | iov.iov_len = (__kernel_size_t)length; 228 | 229 | //type 230 | msg.msg_iter.type = READ; 231 | //address 232 | msg.msg_name = NULL; 233 | msg.msg_namelen = 0; 234 | //msg_iter 235 | msg.msg_iter.iov = &iov; 236 | msg.msg_iter.iov_offset = 0; 237 | msg.msg_iter.count = iov.iov_len; 238 | msg.msg_iter.nr_segs = 1; 239 | //control 240 | msg.msg_control = NULL; 241 | msg.msg_controllen = 0; 242 | 243 | msg.msg_flags = flags; 244 | 245 | #ifndef KSOCKET_ADDR_SAFE 246 | old_fs = get_fs(); 247 | set_fs(KERNEL_DS); 248 | #endif 249 | //hardik 250 | len = sock_sendmsg(sk, &msg);//? 251 | #ifndef KSOCKET_ADDR_SAFE 252 | set_fs(old_fs); 253 | #endif 254 | 255 | return len;//len ? 256 | } 257 | 258 | int kshutdown(ksocket_t socket, int how) 259 | { 260 | struct socket *sk; 261 | int ret = 0; 262 | 263 | sk = (struct socket *)socket; 264 | if (sk) 265 | ret = sk->ops->shutdown(sk, how); 266 | 267 | return ret; 268 | } 269 | 270 | //TODO: ? 271 | int kclose(ksocket_t socket) 272 | { 273 | struct socket *sk; 274 | int ret; 275 | 276 | sk = (struct socket *)socket; 277 | ret = sk->ops->release(sk); 278 | 279 | if (sk) 280 | sock_release(sk); 281 | 282 | return ret; 283 | } 284 | 285 | ssize_t krecvfrom(ksocket_t socket, void * buffer, size_t length, 286 | int flags, struct sockaddr * address, 287 | int * address_len) 288 | { 289 | struct socket *sk; 290 | struct msghdr msg; 291 | struct iovec iov; 292 | int len; 293 | #ifndef KSOCKET_ADDR_SAFE 294 | mm_segment_t old_fs; 295 | #endif 296 | 297 | sk = (struct socket *)socket; 298 | 299 | iov.iov_base = (void *)buffer; 300 | iov.iov_len = (__kernel_size_t)length; 301 | 302 | //type 303 | msg.msg_iter.type = READ; 304 | //address 305 | msg.msg_name = address; 306 | msg.msg_namelen = 128; 307 | //msg_iter 308 | msg.msg_iter.iov = &iov; 309 | msg.msg_iter.iov_offset = 0; 310 | msg.msg_iter.count = iov.iov_len; 311 | msg.msg_iter.nr_segs = 1; 312 | //control 313 | msg.msg_control = NULL; 314 | msg.msg_controllen = 0; 315 | 316 | #ifndef KSOCKET_ADDR_SAFE 317 | old_fs = get_fs(); 318 | set_fs(KERNEL_DS); 319 | #endif 320 | //hardik 321 | len = sock_recvmsg(sk, &msg, flags); 322 | #ifndef KSOCKET_ADDR_SAFE 323 | set_fs(old_fs); 324 | #endif 325 | 326 | if (address) 327 | { 328 | *address_len = msg.msg_namelen; 329 | } 330 | 331 | return len; 332 | } 333 | 334 | ssize_t ksendto(ksocket_t socket, void *message, size_t length, 335 | int flags, const struct sockaddr *dest_addr, 336 | int dest_len) 337 | { 338 | struct socket *sk; 339 | struct msghdr msg; 340 | struct iovec iov; 341 | int len; 342 | #ifndef KSOCKET_ADDR_SAFE 343 | mm_segment_t old_fs; 344 | #endif 345 | 346 | sk = (struct socket *)socket; 347 | 348 | iov.iov_base = (void *)message; 349 | iov.iov_len = (__kernel_size_t)length; 350 | 351 | //type 352 | msg.msg_iter.type = READ; 353 | //msg_iter 354 | msg.msg_iter.iov = &iov; 355 | msg.msg_iter.iov_offset = 0; 356 | msg.msg_iter.count = iov.iov_len; 357 | msg.msg_iter.nr_segs = 1; 358 | //control 359 | msg.msg_control = NULL; 360 | msg.msg_controllen = 0; 361 | 362 | msg.msg_flags = flags; 363 | if (dest_addr) 364 | { 365 | msg.msg_name = (void *)dest_addr; 366 | msg.msg_namelen = dest_len; 367 | } 368 | 369 | #ifndef KSOCKET_ADDR_SAFE 370 | old_fs = get_fs(); 371 | set_fs(KERNEL_DS); 372 | #endif 373 | //hardik 374 | len = sock_sendmsg(sk, &msg);//? 375 | #ifndef KSOCKET_ADDR_SAFE 376 | set_fs(old_fs); 377 | #endif 378 | 379 | return len;//len ? 380 | } 381 | 382 | int kgetsockname(ksocket_t socket, struct sockaddr *address, int *address_len) 383 | { 384 | struct socket *sk; 385 | int ret; 386 | 387 | sk = (struct socket *)socket; 388 | ret = sk->ops->getname(sk, address, address_len, 0); 389 | 390 | return ret; 391 | } 392 | 393 | int kgetpeername(ksocket_t socket, struct sockaddr *address, int *address_len) 394 | { 395 | struct socket *sk; 396 | int ret; 397 | 398 | sk = (struct socket *)socket; 399 | ret = sk->ops->getname(sk, address, address_len, 1); 400 | 401 | return ret; 402 | } 403 | 404 | int ksetsockopt(ksocket_t socket, int level, int optname, void *optval, int optlen) 405 | { 406 | struct socket *sk; 407 | int ret; 408 | #ifndef KSOCKET_ADDR_SAFE 409 | mm_segment_t old_fs; 410 | #endif 411 | 412 | sk = (struct socket *)socket; 413 | 414 | #ifndef KSOCKET_ADDR_SAFE 415 | old_fs = get_fs(); 416 | set_fs(KERNEL_DS); 417 | #endif 418 | 419 | if (level == SOL_SOCKET) 420 | ret = sock_setsockopt(sk, level, optname, optval, optlen); 421 | else 422 | ret = sk->ops->setsockopt(sk, level, optname, optval, optlen); 423 | 424 | #ifndef KSOCKET_ADDR_SAFE 425 | set_fs(old_fs); 426 | #endif 427 | 428 | return ret; 429 | } 430 | 431 | int kgetsockopt(ksocket_t socket, int level, int optname, void *optval, int *optlen) 432 | { 433 | /* struct socket *sk; 434 | int ret; 435 | mm_segment_t old_fs; 436 | 437 | sk = (struct socket *)socket; 438 | 439 | old_fs = get_fs(); 440 | set_fs(KERNEL_DS); 441 | 442 | if (level == SOL_SOCKET) 443 | ret = sock_getsockopt(sk, level, optname, optval, optlen); 444 | else 445 | ret = sk->ops->getsockopt(sk, level, optname, optval, optlen); 446 | 447 | set_fs(old_fs); 448 | 449 | return ret; 450 | */ 451 | return -ENOSYS; 452 | } 453 | 454 | 455 | //helper functions 456 | unsigned int inet_addr(char* ip) 457 | { 458 | int a, b, c, d; 459 | char addr[4]; 460 | 461 | sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d); 462 | addr[0] = a; 463 | addr[1] = b; 464 | addr[2] = c; 465 | addr[3] = d; 466 | 467 | return *(unsigned int *)addr; 468 | } 469 | 470 | char *inet_ntoa(struct in_addr *in) 471 | { 472 | char* str_ip = NULL; 473 | u_int32_t int_ip = 0; 474 | 475 | str_ip = kmalloc(16 * sizeof(char), GFP_KERNEL); 476 | if (!str_ip) 477 | return NULL; 478 | else 479 | memset(str_ip, 0, 16); 480 | 481 | int_ip = in->s_addr; 482 | 483 | sprintf(str_ip, "%d.%d.%d.%d", (int_ip ) & 0xFF, 484 | (int_ip >> 8 ) & 0xFF, 485 | (int_ip >> 16) & 0xFF, 486 | (int_ip >> 24) & 0xFF); 487 | return str_ip; 488 | } 489 | 490 | //module init and cleanup procedure 491 | static int ksocket_init(void) 492 | { 493 | printk("%s version %s\n%s\n%s\n", 494 | KSOCKET_NAME, KSOCKET_VERSION, 495 | KSOCKET_DESCPT, KSOCKET_AUTHOR); 496 | 497 | return 0; 498 | } 499 | 500 | static void ksocket_exit(void) 501 | { 502 | printk("ksocket exit\n"); 503 | } 504 | 505 | module_init(ksocket_init); 506 | module_exit(ksocket_exit); 507 | 508 | EXPORT_SYMBOL(ksocket); 509 | EXPORT_SYMBOL(kbind); 510 | EXPORT_SYMBOL(klisten); 511 | EXPORT_SYMBOL(kconnect); 512 | EXPORT_SYMBOL(kaccept); 513 | EXPORT_SYMBOL(krecv); 514 | EXPORT_SYMBOL(ksend); 515 | EXPORT_SYMBOL(kshutdown); 516 | EXPORT_SYMBOL(kclose); 517 | EXPORT_SYMBOL(krecvfrom); 518 | EXPORT_SYMBOL(ksendto); 519 | EXPORT_SYMBOL(kgetsockname); 520 | EXPORT_SYMBOL(kgetpeername); 521 | EXPORT_SYMBOL(ksetsockopt); 522 | EXPORT_SYMBOL(kgetsockopt); 523 | EXPORT_SYMBOL(inet_addr); 524 | EXPORT_SYMBOL(inet_ntoa); 525 | -------------------------------------------------------------------------------- /src/ksocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ksocket project 3 | * BSD-style socket APIs for kernel 2.6 developers 4 | * 5 | * @2007-2008, China 6 | * @song.xian-guang@hotmail.com (MSN Accounts) 7 | * 8 | * This code is licenced under the GPL 9 | * Feel free to contact me if any questions 10 | * 11 | * @2017 12 | * Hardik Bagdi (hbagdi1@binghamton.edu) 13 | * Changes for Compatibility with Linux 4.9 to use iov_iter 14 | */ 15 | 16 | #ifndef _ksocket_h_ 17 | #define _ksocket_h_ 18 | 19 | struct socket; 20 | struct sockaddr; 21 | struct in_addr; 22 | typedef struct socket * ksocket_t; 23 | 24 | /* BSD socket APIs prototype declaration */ 25 | ksocket_t ksocket(int domain, int type, int protocol); 26 | int kshutdown(ksocket_t socket, int how); 27 | int kclose(ksocket_t socket); 28 | 29 | int kbind(ksocket_t socket, struct sockaddr *address, int address_len); 30 | int klisten(ksocket_t socket, int backlog); 31 | int kconnect(ksocket_t socket, struct sockaddr *address, int address_len); 32 | ksocket_t kaccept(ksocket_t socket, struct sockaddr *address, int *address_len); 33 | 34 | ssize_t krecv(ksocket_t socket, void *buffer, size_t length, int flags); 35 | ssize_t ksend(ksocket_t socket, const void *buffer, size_t length, int flags); 36 | ssize_t krecvfrom(ksocket_t socket, void * buffer, size_t length, int flags, struct sockaddr * address, int * address_len); 37 | ssize_t ksendto(ksocket_t socket, void *message, size_t length, int flags, const struct sockaddr *dest_addr, int dest_len); 38 | 39 | int kgetsockname(ksocket_t socket, struct sockaddr *address, int *address_len); 40 | int kgetpeername(ksocket_t socket, struct sockaddr *address, int *address_len); 41 | int ksetsockopt(ksocket_t socket, int level, int optname, void *optval, int optlen); 42 | int kgetsockopt(ksocket_t socket, int level, int optname, void *optval, int *optlen); 43 | 44 | unsigned int inet_addr(char* ip); 45 | char *inet_ntoa(struct in_addr *in); /* DO NOT forget to kfree the return pointer */ 46 | 47 | #endif /* !_ksocket_h_ */ 48 | --------------------------------------------------------------------------------