dv.c 11 KB

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