dv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Generic frame diversion
  7. *
  8. * Authors:
  9. * Benoit LOCHER: initial integration within the kernel with support for ethernet
  10. * Dave Miller: improvement on the code (correctness, performance and source files)
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/socket.h>
  20. #include <linux/in.h>
  21. #include <linux/inet.h>
  22. #include <linux/ip.h>
  23. #include <linux/udp.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <net/dst.h>
  30. #include <net/arp.h>
  31. #include <net/sock.h>
  32. #include <net/ipv6.h>
  33. #include <net/ip.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/system.h>
  36. #include <asm/checksum.h>
  37. #include <linux/divert.h>
  38. #include <linux/sockios.h>
  39. const char sysctl_divert_version[32]="0.46"; /* Current version */
  40. static int __init dv_init(void)
  41. {
  42. return 0;
  43. }
  44. module_init(dv_init);
  45. /*
  46. * Allocate a divert_blk for a device. This must be an ethernet nic.
  47. */
  48. int alloc_divert_blk(struct net_device *dev)
  49. {
  50. int alloc_size = (sizeof(struct divert_blk) + 3) & ~3;
  51. dev->divert = NULL;
  52. if (dev->type == ARPHRD_ETHER) {
  53. dev->divert = (struct divert_blk *)
  54. kmalloc(alloc_size, GFP_KERNEL);
  55. if (dev->divert == NULL) {
  56. printk(KERN_INFO "divert: unable to allocate divert_blk for %s\n",
  57. dev->name);
  58. return -ENOMEM;
  59. }
  60. memset(dev->divert, 0, sizeof(struct divert_blk));
  61. dev_hold(dev);
  62. }
  63. return 0;
  64. }
  65. /*
  66. * Free a divert_blk allocated by the above function, if it was
  67. * allocated on that device.
  68. */
  69. void free_divert_blk(struct net_device *dev)
  70. {
  71. if (dev->divert) {
  72. kfree(dev->divert);
  73. dev->divert=NULL;
  74. dev_put(dev);
  75. }
  76. }
  77. /*
  78. * Adds a tcp/udp (source or dest) port to an array
  79. */
  80. static int add_port(u16 ports[], u16 port)
  81. {
  82. int i;
  83. if (port == 0)
  84. return -EINVAL;
  85. /* Storing directly in network format for performance,
  86. * thanks Dave :)
  87. */
  88. port = htons(port);
  89. for (i = 0; i < MAX_DIVERT_PORTS; i++) {
  90. if (ports[i] == port)
  91. return -EALREADY;
  92. }
  93. for (i = 0; i < MAX_DIVERT_PORTS; i++) {
  94. if (ports[i] == 0) {
  95. ports[i] = port;
  96. return 0;
  97. }
  98. }
  99. return -ENOBUFS;
  100. }
  101. /*
  102. * Removes a port from an array tcp/udp (source or dest)
  103. */
  104. static int remove_port(u16 ports[], u16 port)
  105. {
  106. int i;
  107. if (port == 0)
  108. return -EINVAL;
  109. /* Storing directly in network format for performance,
  110. * thanks Dave !
  111. */
  112. port = htons(port);
  113. for (i = 0; i < MAX_DIVERT_PORTS; i++) {
  114. if (ports[i] == port) {
  115. ports[i] = 0;
  116. return 0;
  117. }
  118. }
  119. return -EINVAL;
  120. }
  121. /* Some basic sanity checks on the arguments passed to divert_ioctl() */
  122. static int check_args(struct divert_cf *div_cf, struct net_device **dev)
  123. {
  124. char devname[32];
  125. int ret;
  126. if (dev == NULL)
  127. return -EFAULT;
  128. /* GETVERSION: all other args are unused */
  129. if (div_cf->cmd == DIVCMD_GETVERSION)
  130. return 0;
  131. /* Network device index should reasonably be between 0 and 1000 :) */
  132. if (div_cf->dev_index < 0 || div_cf->dev_index > 1000)
  133. return -EINVAL;
  134. /* Let's try to find the ifname */
  135. sprintf(devname, "eth%d", div_cf->dev_index);
  136. *dev = dev_get_by_name(devname);
  137. /* dev should NOT be null */
  138. if (*dev == NULL)
  139. return -EINVAL;
  140. ret = 0;
  141. /* user issuing the ioctl must be a super one :) */
  142. if (!capable(CAP_SYS_ADMIN)) {
  143. ret = -EPERM;
  144. goto out;
  145. }
  146. /* Device must have a divert_blk member NOT null */
  147. if ((*dev)->divert == NULL)
  148. ret = -EINVAL;
  149. out:
  150. dev_put(*dev);
  151. return ret;
  152. }
  153. /*
  154. * control function of the diverter
  155. */
  156. #if 0
  157. #define DVDBG(a) \
  158. printk(KERN_DEBUG "divert_ioctl() line %d %s\n", __LINE__, (a))
  159. #else
  160. #define DVDBG(a)
  161. #endif
  162. int divert_ioctl(unsigned int cmd, struct divert_cf __user *arg)
  163. {
  164. struct divert_cf div_cf;
  165. struct divert_blk *div_blk;
  166. struct net_device *dev;
  167. int ret;
  168. switch (cmd) {
  169. case SIOCGIFDIVERT:
  170. DVDBG("SIOCGIFDIVERT, copy_from_user");
  171. if (copy_from_user(&div_cf, arg, sizeof(struct divert_cf)))
  172. return -EFAULT;
  173. DVDBG("before check_args");
  174. ret = check_args(&div_cf, &dev);
  175. if (ret)
  176. return ret;
  177. DVDBG("after checkargs");
  178. div_blk = dev->divert;
  179. DVDBG("befre switch()");
  180. switch (div_cf.cmd) {
  181. case DIVCMD_GETSTATUS:
  182. /* Now, just give the user the raw divert block
  183. * for him to play with :)
  184. */
  185. if (copy_to_user(div_cf.arg1.ptr, dev->divert,
  186. sizeof(struct divert_blk)))
  187. return -EFAULT;
  188. break;
  189. case DIVCMD_GETVERSION:
  190. DVDBG("GETVERSION: checking ptr");
  191. if (div_cf.arg1.ptr == NULL)
  192. return -EINVAL;
  193. DVDBG("GETVERSION: copying data to userland");
  194. if (copy_to_user(div_cf.arg1.ptr,
  195. sysctl_divert_version, 32))
  196. return -EFAULT;
  197. DVDBG("GETVERSION: data copied");
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. break;
  203. case SIOCSIFDIVERT:
  204. if (copy_from_user(&div_cf, arg, sizeof(struct divert_cf)))
  205. return -EFAULT;
  206. ret = check_args(&div_cf, &dev);
  207. if (ret)
  208. return ret;
  209. div_blk = dev->divert;
  210. switch(div_cf.cmd) {
  211. case DIVCMD_RESET:
  212. div_blk->divert = 0;
  213. div_blk->protos = DIVERT_PROTO_NONE;
  214. memset(div_blk->tcp_dst, 0,
  215. MAX_DIVERT_PORTS * sizeof(u16));
  216. memset(div_blk->tcp_src, 0,
  217. MAX_DIVERT_PORTS * sizeof(u16));
  218. memset(div_blk->udp_dst, 0,
  219. MAX_DIVERT_PORTS * sizeof(u16));
  220. memset(div_blk->udp_src, 0,
  221. MAX_DIVERT_PORTS * sizeof(u16));
  222. return 0;
  223. case DIVCMD_DIVERT:
  224. switch(div_cf.arg1.int32) {
  225. case DIVARG1_ENABLE:
  226. if (div_blk->divert)
  227. return -EALREADY;
  228. div_blk->divert = 1;
  229. break;
  230. case DIVARG1_DISABLE:
  231. if (!div_blk->divert)
  232. return -EALREADY;
  233. div_blk->divert = 0;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. break;
  239. case DIVCMD_IP:
  240. switch(div_cf.arg1.int32) {
  241. case DIVARG1_ENABLE:
  242. if (div_blk->protos & DIVERT_PROTO_IP)
  243. return -EALREADY;
  244. div_blk->protos |= DIVERT_PROTO_IP;
  245. break;
  246. case DIVARG1_DISABLE:
  247. if (!(div_blk->protos & DIVERT_PROTO_IP))
  248. return -EALREADY;
  249. div_blk->protos &= ~DIVERT_PROTO_IP;
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. break;
  255. case DIVCMD_TCP:
  256. switch(div_cf.arg1.int32) {
  257. case DIVARG1_ENABLE:
  258. if (div_blk->protos & DIVERT_PROTO_TCP)
  259. return -EALREADY;
  260. div_blk->protos |= DIVERT_PROTO_TCP;
  261. break;
  262. case DIVARG1_DISABLE:
  263. if (!(div_blk->protos & DIVERT_PROTO_TCP))
  264. return -EALREADY;
  265. div_blk->protos &= ~DIVERT_PROTO_TCP;
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. break;
  271. case DIVCMD_TCPDST:
  272. switch(div_cf.arg1.int32) {
  273. case DIVARG1_ADD:
  274. return add_port(div_blk->tcp_dst,
  275. div_cf.arg2.uint16);
  276. case DIVARG1_REMOVE:
  277. return remove_port(div_blk->tcp_dst,
  278. div_cf.arg2.uint16);
  279. default:
  280. return -EINVAL;
  281. }
  282. break;
  283. case DIVCMD_TCPSRC:
  284. switch(div_cf.arg1.int32) {
  285. case DIVARG1_ADD:
  286. return add_port(div_blk->tcp_src,
  287. div_cf.arg2.uint16);
  288. case DIVARG1_REMOVE:
  289. return remove_port(div_blk->tcp_src,
  290. div_cf.arg2.uint16);
  291. default:
  292. return -EINVAL;
  293. }
  294. break;
  295. case DIVCMD_UDP:
  296. switch(div_cf.arg1.int32) {
  297. case DIVARG1_ENABLE:
  298. if (div_blk->protos & DIVERT_PROTO_UDP)
  299. return -EALREADY;
  300. div_blk->protos |= DIVERT_PROTO_UDP;
  301. break;
  302. case DIVARG1_DISABLE:
  303. if (!(div_blk->protos & DIVERT_PROTO_UDP))
  304. return -EALREADY;
  305. div_blk->protos &= ~DIVERT_PROTO_UDP;
  306. break;
  307. default:
  308. return -EINVAL;
  309. }
  310. break;
  311. case DIVCMD_UDPDST:
  312. switch(div_cf.arg1.int32) {
  313. case DIVARG1_ADD:
  314. return add_port(div_blk->udp_dst,
  315. div_cf.arg2.uint16);
  316. case DIVARG1_REMOVE:
  317. return remove_port(div_blk->udp_dst,
  318. div_cf.arg2.uint16);
  319. default:
  320. return -EINVAL;
  321. }
  322. break;
  323. case DIVCMD_UDPSRC:
  324. switch(div_cf.arg1.int32) {
  325. case DIVARG1_ADD:
  326. return add_port(div_blk->udp_src,
  327. div_cf.arg2.uint16);
  328. case DIVARG1_REMOVE:
  329. return remove_port(div_blk->udp_src,
  330. div_cf.arg2.uint16);
  331. default:
  332. return -EINVAL;
  333. }
  334. break;
  335. case DIVCMD_ICMP:
  336. switch(div_cf.arg1.int32) {
  337. case DIVARG1_ENABLE:
  338. if (div_blk->protos & DIVERT_PROTO_ICMP)
  339. return -EALREADY;
  340. div_blk->protos |= DIVERT_PROTO_ICMP;
  341. break;
  342. case DIVARG1_DISABLE:
  343. if (!(div_blk->protos & DIVERT_PROTO_ICMP))
  344. return -EALREADY;
  345. div_blk->protos &= ~DIVERT_PROTO_ICMP;
  346. break;
  347. default:
  348. return -EINVAL;
  349. }
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. /*
  361. * Check if packet should have its dest mac address set to the box itself
  362. * for diversion
  363. */
  364. #define ETH_DIVERT_FRAME(skb) \
  365. memcpy(eth_hdr(skb), skb->dev->dev_addr, ETH_ALEN); \
  366. skb->pkt_type=PACKET_HOST
  367. void divert_frame(struct sk_buff *skb)
  368. {
  369. struct ethhdr *eth = eth_hdr(skb);
  370. struct iphdr *iph;
  371. struct tcphdr *tcph;
  372. struct udphdr *udph;
  373. struct divert_blk *divert = skb->dev->divert;
  374. int i, src, dst;
  375. unsigned char *skb_data_end = skb->data + skb->len;
  376. /* Packet is already aimed at us, return */
  377. if (!memcmp(eth, skb->dev->dev_addr, ETH_ALEN))
  378. return;
  379. /* proto is not IP, do nothing */
  380. if (eth->h_proto != htons(ETH_P_IP))
  381. return;
  382. /* Divert all IP frames ? */
  383. if (divert->protos & DIVERT_PROTO_IP) {
  384. ETH_DIVERT_FRAME(skb);
  385. return;
  386. }
  387. /* Check for possible (maliciously) malformed IP frame (thanks Dave) */
  388. iph = (struct iphdr *) skb->data;
  389. if (((iph->ihl<<2)+(unsigned char*)(iph)) >= skb_data_end) {
  390. printk(KERN_INFO "divert: malformed IP packet !\n");
  391. return;
  392. }
  393. switch (iph->protocol) {
  394. /* Divert all ICMP frames ? */
  395. case IPPROTO_ICMP:
  396. if (divert->protos & DIVERT_PROTO_ICMP) {
  397. ETH_DIVERT_FRAME(skb);
  398. return;
  399. }
  400. break;
  401. /* Divert all TCP frames ? */
  402. case IPPROTO_TCP:
  403. if (divert->protos & DIVERT_PROTO_TCP) {
  404. ETH_DIVERT_FRAME(skb);
  405. return;
  406. }
  407. /* Check for possible (maliciously) malformed IP
  408. * frame (thanx Dave)
  409. */
  410. tcph = (struct tcphdr *)
  411. (((unsigned char *)iph) + (iph->ihl<<2));
  412. if (((unsigned char *)(tcph+1)) >= skb_data_end) {
  413. printk(KERN_INFO "divert: malformed TCP packet !\n");
  414. return;
  415. }
  416. /* Divert some tcp dst/src ports only ?*/
  417. for (i = 0; i < MAX_DIVERT_PORTS; i++) {
  418. dst = divert->tcp_dst[i];
  419. src = divert->tcp_src[i];
  420. if ((dst && dst == tcph->dest) ||
  421. (src && src == tcph->source)) {
  422. ETH_DIVERT_FRAME(skb);
  423. return;
  424. }
  425. }
  426. break;
  427. /* Divert all UDP frames ? */
  428. case IPPROTO_UDP:
  429. if (divert->protos & DIVERT_PROTO_UDP) {
  430. ETH_DIVERT_FRAME(skb);
  431. return;
  432. }
  433. /* Check for possible (maliciously) malformed IP
  434. * packet (thanks Dave)
  435. */
  436. udph = (struct udphdr *)
  437. (((unsigned char *)iph) + (iph->ihl<<2));
  438. if (((unsigned char *)(udph+1)) >= skb_data_end) {
  439. printk(KERN_INFO
  440. "divert: malformed UDP packet !\n");
  441. return;
  442. }
  443. /* Divert some udp dst/src ports only ? */
  444. for (i = 0; i < MAX_DIVERT_PORTS; i++) {
  445. dst = divert->udp_dst[i];
  446. src = divert->udp_src[i];
  447. if ((dst && dst == udph->dest) ||
  448. (src && src == udph->source)) {
  449. ETH_DIVERT_FRAME(skb);
  450. return;
  451. }
  452. }
  453. break;
  454. }
  455. }