dv.c 11 KB

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