You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1433 lines
45 KiB

  1. /*
  2. * siit.c: the Stateless IP/ICMP Translator (SIIT) module for Linux.
  3. *
  4. *
  5. */
  6. #include <linux/version.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h> /* printk() */
  10. #include <linux/slab.h>
  11. #include <linux/errno.h> /* error codes */
  12. #include <linux/types.h> /* size_t */
  13. #include <linux/interrupt.h> /* mark_bh */
  14. #include <linux/random.h>
  15. #include <linux/in.h>
  16. #include <linux/netdevice.h> /* struct device, and other headers */
  17. #include <linux/etherdevice.h> /* eth_type_trans */
  18. #include <net/ip.h> /* struct iphdr */
  19. #include <net/icmp.h> /* struct icmphdr */
  20. #include <net/ipv6.h>
  21. #include <net/udp.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/in6.h>
  24. #include <linux/init.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/checksum.h>
  27. #include <net/ip6_checksum.h>
  28. #include <linux/in6.h>
  29. #include "siit.h"
  30. MODULE_AUTHOR("Dmitriy Moscalev, Grigory Klyuchnikov, Felix Fietkau");
  31. /*
  32. * If tos_ignore_flag != 0, we don't copy TOS and Traffic Class
  33. * from origin paket and set it to 0
  34. */
  35. int tos_ignore_flag = 0;
  36. #define siit_stats(_dev) (&(_dev)->stats)
  37. /*
  38. * The Utility stuff
  39. */
  40. #ifdef SIIT_DEBUG
  41. /* print dump bytes (data point data area sizeof len and message
  42. * before dump.
  43. */
  44. static int siit_print_dump(char *data, int len, char *message)
  45. {
  46. int i;
  47. int j = 0, k = 1;
  48. len = len > BUFF_SIZE ? BUFF_SIZE : len;
  49. printk("%s:\n", message);
  50. for (i=0; i < len; i++, k++) {
  51. if( i == len-1 || k == 16) {
  52. printk("%02x\n", (~(~0 << 8) & *(data+i)));
  53. j = 0;
  54. k = 0;
  55. }
  56. else if (j) {
  57. printk("%02x ", (~(~0 << 8) & *(data+i)));
  58. j--;
  59. }
  60. else {
  61. printk("%02x", (~(~0 << 8) & *(data+i)));
  62. j++;
  63. }
  64. }
  65. return 0;
  66. }
  67. #endif
  68. /*
  69. * Open and close
  70. */
  71. static int siit_open(struct net_device *dev)
  72. {
  73. netif_start_queue(dev);
  74. return 0;
  75. }
  76. static int siit_release(struct net_device *dev)
  77. {
  78. netif_stop_queue(dev); /* can't transmit any more */
  79. return 0;
  80. }
  81. /*
  82. * Translation IPv4 to IPv6 stuff
  83. *
  84. * ip4_ip6 (src, len, dst, include_flag)
  85. *
  86. * where
  87. * src - buffer with original IPv4 packet,
  88. * len - size of original packet,
  89. * dst - new buffer for IPv6 packet,
  90. * include_flag - if = 1, dst point to IPv4 packet that is ICMP error
  91. * included IP packet, else = 0
  92. */
  93. static int ip4_ip6(char *src, int len, char *dst, int include_flag)
  94. {
  95. struct iphdr *ih4 = (struct iphdr *) src; /* point to current IPv4 header struct */
  96. struct icmphdr *icmp_hdr; /* point to current ICMPv4 header struct */
  97. struct udphdr *udp_hdr; /* point to current IPv4 UDP header struct */
  98. struct ipv6hdr *ih6 = (struct ipv6hdr *) dst; /* point to current IPv6 header struct */
  99. struct frag_hdr *ih6_frag = (struct frag_hdr *)(dst+sizeof(struct ipv6hdr));
  100. /* point to current IPv6 fragment header struct */
  101. struct icmp6hdr *icmp6_hdr; /* point to current ICMPv6 header */
  102. int hdr_len = (int)(ih4->ihl * 4); /* IPv4 header length */
  103. int icmp_len; /* ICMPv4 packet length */
  104. int plen; /* payload length */
  105. unsigned int csum; /* need to calculate ICMPv6 and UDP checksum */
  106. int fl_csum = 0; /* flag to calculate UDP checksum */
  107. int icmperr = 1; /* flag to indicate ICMP error message and to need
  108. translate ICMP included IP packet */
  109. int fr_flag = 0; /* fragment flag, if = 0 - don't add
  110. fragment header */
  111. __u16 new_tot_len; /* need to calculate IPv6 total length */
  112. __u8 new_nexthdr; /* next header code */
  113. __u16 icmp_ptr = 0; /* Pointer field in ICMP_PARAMETERPROB */
  114. #ifdef SIIT_DEBUG /* print IPv4 header dump */
  115. siit_print_dump(src, hdr_len, "siit: ip4_ip6() (in) ip4 header dump");
  116. #endif
  117. /* If DF == 1 && MF == 0 && Fragment Offset == 0
  118. * or this packet is ICMP included IP packet
  119. * we don't need fragment header */
  120. if (ntohs(ih4->frag_off) == IP_DF || include_flag ) {
  121. /* not fragment and we need not to add Fragment
  122. * Header to IPv6 packet. */
  123. /* total length = total length from IPv4 packet */
  124. new_tot_len = ntohs(ih4->tot_len);
  125. if (ih4->protocol == IPPROTO_ICMP)
  126. new_nexthdr = NEXTHDR_ICMP;
  127. else
  128. new_nexthdr = ih4->protocol;
  129. }
  130. else {
  131. /* need to add Fragment Header */
  132. fr_flag = 1;
  133. /* total length = total length from IPv4 packet +
  134. length of Fragment Header */
  135. new_tot_len = ntohs(ih4->tot_len) + sizeof(struct frag_hdr);
  136. /* IPv6 Header NextHeader = NEXTHDR_FRAGMENT */
  137. new_nexthdr = NEXTHDR_FRAGMENT;
  138. /* Fragment Header NextHeader copy from IPv4 packet */
  139. if (ih4->protocol == IPPROTO_ICMP)
  140. ih6_frag->nexthdr = NEXTHDR_ICMP;
  141. else
  142. ih6_frag->nexthdr = ih4->protocol;
  143. /* copy frag offset from IPv4 packet */
  144. ih6_frag->frag_off = htons((ntohs(ih4->frag_off) & IP_OFFSET) << 3);
  145. /* copy MF flag from IPv4 packet */
  146. ih6_frag->frag_off = htons((ntohs(ih6_frag->frag_off) |
  147. ((ntohs(ih4->frag_off) & IP_MF) >> 13)));
  148. /* copy Identification field from IPv4 packet */
  149. ih6_frag->identification = htonl(ntohs(ih4->id));
  150. /* reserved field initialized to zero */
  151. ih6_frag->reserved = 0;
  152. }
  153. /* Form rest IPv6 fields */
  154. /*
  155. * At this point we need to add checking of unxpired source
  156. * route optin and if it is, send ICMPv4 "destination
  157. * unreacheble/source route failes" Type 3/Code 5 and
  158. * drop packet. (NOT RELEASED YET)
  159. */
  160. /* IP version = 6 */
  161. ih6->version = 6;
  162. if (tos_ignore_flag) {
  163. ih6->priority = 0;
  164. ih6->flow_lbl[0] = 0;
  165. } else {
  166. ih6->priority = (ih4->tos & 0xf0) >> 4;
  167. ih6->flow_lbl[0] = (ih4->tos & 0x0f) << 4;
  168. }
  169. ih6->flow_lbl[1] = 0;
  170. ih6->flow_lbl[2] = 0;
  171. /* Hop Limit = IPv4 TTL */
  172. ih6->hop_limit = ih4->ttl;
  173. /* Translate source destination addresses,
  174. for IPv6 host it's IPv4-translated IPv6 address,
  175. for IPv4 host it's IPv4-mapped IPv6 address
  176. !!WARNING!! Instead IPv4-mapped IPv6 addresses we use addreesses
  177. with unused prefix ::ffff:ffff:0:0/96, because KAME implementation
  178. doesn't support IPv4-mapped addresses in IPv6 packets and discard them.
  179. */
  180. if (include_flag) {
  181. /*
  182. It's ICMP included IP packet and there is a diffirence
  183. in src/dst addresses then src/dst in normal direction
  184. */
  185. /*
  186. Source address
  187. is IPv4-translated IPv6 address because packet traveled
  188. from IPv6 to IPv4 area
  189. */
  190. ih6->saddr.in6_u.u6_addr32[0] = 0;
  191. ih6->saddr.in6_u.u6_addr32[1] = 0;
  192. ih6->saddr.in6_u.u6_addr32[2] = htonl(TRANSLATED_PREFIX); /* to network order bytes */
  193. ih6->saddr.in6_u.u6_addr32[3] = ih4->saddr;
  194. /*
  195. Destination address
  196. is IPv4-mapped address (but it's not IPv4- mapped, we use
  197. prefix ::ffff:ffff:0:0/96
  198. */
  199. ih6->daddr.in6_u.u6_addr32[0] = 0;
  200. ih6->daddr.in6_u.u6_addr32[1] = 0;
  201. ih6->daddr.in6_u.u6_addr32[2] = htonl(MAPPED_PREFIX); /* to network order bytes */
  202. ih6->daddr.in6_u.u6_addr32[3] = ih4->daddr;
  203. }
  204. else {
  205. /*
  206. This is normal case (packet isn't included IP packet)
  207. Source address
  208. is IPv4-mapped address (but it's not IPv4- mapped, we use
  209. prefix ::ffff:ffff:0:0/96)
  210. */
  211. ih6->saddr.in6_u.u6_addr32[0] = 0;
  212. ih6->saddr.in6_u.u6_addr32[1] = 0;
  213. ih6->saddr.in6_u.u6_addr32[2] = htonl(MAPPED_PREFIX); /* to network order bytes */
  214. ih6->saddr.in6_u.u6_addr32[3] = ih4->saddr;
  215. /* Destination address
  216. is is IPv4-translated IPv6 address
  217. */
  218. ih6->daddr.in6_u.u6_addr32[0] = 0;
  219. ih6->daddr.in6_u.u6_addr32[1] = 0;
  220. ih6->daddr.in6_u.u6_addr32[2] = htonl(TRANSLATED_PREFIX); /* to network order bytes */
  221. ih6->daddr.in6_u.u6_addr32[3] = ih4->daddr;
  222. }
  223. /* Payload Length */
  224. plen = new_tot_len - hdr_len; /* Payload length = IPv4 total len - IPv4 header len */
  225. ih6->payload_len = htons(plen);
  226. /* Next Header */
  227. ih6->nexthdr = new_nexthdr; /* Next Header */
  228. /* Process ICMP protocols data */
  229. switch (ih4->protocol) {
  230. case IPPROTO_ICMP:
  231. if ( (ntohs(ih4->frag_off) & IP_OFFSET) != 0 || (ntohs(ih4->frag_off) & IP_MF) != 0 ) {
  232. PDEBUG("ip4_ip6(): don't translate ICMPv4 fragments - packet dropped.\n");
  233. return -1;
  234. }
  235. icmp_hdr = (struct icmphdr *) (src+hdr_len); /* point to ICMPv4 header */
  236. csum = 0;
  237. icmp_len = ntohs(ih4->tot_len) - hdr_len; /* ICMPv4 packet length */
  238. icmp6_hdr = (struct icmp6hdr *)(dst+sizeof(struct ipv6hdr)
  239. +fr_flag*sizeof(struct frag_hdr)); /* point to ICMPv6 header */
  240. if (include_flag) {
  241. /* ICMPv4 packet cannot be included in ICMPv4 Error message */
  242. /* !!! May be it's WRONG !!! ICMPv4 QUERY packet can be included
  243. in ICMPv4 Error message */
  244. PDEBUG("ip4_ip6(): It's included ICMPv4 in ICMPv4 Error message - packet dropped.\n");
  245. return -1;
  246. }
  247. /* Check ICMPv4 Type field */
  248. switch (icmp_hdr->type) {
  249. /* ICMP Error messages */
  250. /* Destination Unreachable (Type 3) */
  251. case ICMP_DEST_UNREACH:
  252. icmp6_hdr->icmp6_type = ICMPV6_DEST_UNREACH; /* to Type 1 */
  253. icmp6_hdr->icmp6_unused = 0;
  254. switch (icmp_hdr->code)
  255. {
  256. case ICMP_NET_UNREACH: /* Code 0 */
  257. case ICMP_HOST_UNREACH: /* Code 1 */
  258. case ICMP_SR_FAILED: /* Code 5 */
  259. case ICMP_NET_UNKNOWN: /* Code 6 */
  260. case ICMP_HOST_UNKNOWN: /* Code 7 */
  261. case ICMP_HOST_ISOLATED: /* Code 8 */
  262. case ICMP_NET_UNR_TOS: /* Code 11 */
  263. case ICMP_HOST_UNR_TOS: /* Code 12 */
  264. icmp6_hdr->icmp6_code = ICMPV6_NOROUTE; /* to Code 0 */
  265. break;
  266. case ICMP_PROT_UNREACH: /* Code 2 */
  267. icmp6_hdr->icmp6_type = ICMPV6_PARAMPROB; /* to Type 4 */
  268. icmp6_hdr->icmp6_code = ICMPV6_UNK_NEXTHDR; /* to Code 1 */
  269. /* Set pointer filed to 6, it's octet offset IPv6 Next Header field */
  270. icmp6_hdr->icmp6_pointer = htonl(6);
  271. break;
  272. case ICMP_PORT_UNREACH: /* Code 3 */
  273. icmp6_hdr->icmp6_code = ICMPV6_PORT_UNREACH; /* to Code 4 */
  274. break;
  275. case ICMP_FRAG_NEEDED: /* Code 4 */
  276. icmp6_hdr->icmp6_type = ICMPV6_PKT_TOOBIG; /* to Type 2 */
  277. icmp6_hdr->icmp6_code = 0;
  278. /* Correct MTU */
  279. if (icmp_hdr->un.frag.mtu == 0)
  280. /* we use minimum MTU for IPv4 PMTUv4 RFC1191, section 5;
  281. IPv6 implementation wouldn't accept Path MTU < 1280,
  282. but it records info correctly to always include
  283. a fragment header */
  284. icmp6_hdr->icmp6_mtu = htonl(576);
  285. else
  286. /* needs to adjusted for difference between IPv4/IPv6 headers
  287. * SIIT RFC2765, section 3.3,
  288. * we assume that difference is 20 bytes */
  289. icmp6_hdr->icmp6_mtu = htonl(ntohs(icmp_hdr->un.frag.mtu)+IP4_IP6_HDR_DIFF);
  290. break;
  291. case ICMP_NET_ANO: /* Code 9 */
  292. case ICMP_HOST_ANO: /* Code 10 */
  293. icmp6_hdr->icmp6_code = ICMPV6_ADM_PROHIBITED; /* to Code 1 */
  294. break;
  295. default: /* discard any other Code */
  296. PDEBUG("ip4_ip6(): Unknown ICMPv4 Type %d Code %d - packet dropped.\n",
  297. ICMP_DEST_UNREACH, icmp_hdr->code);
  298. return -1;
  299. }
  300. break;
  301. /* Time Exceeded (Type 11) */
  302. case ICMP_TIME_EXCEEDED:
  303. icmp6_hdr->icmp6_type = ICMPV6_TIME_EXCEED;
  304. icmp6_hdr->icmp6_code = icmp_hdr->code;
  305. break;
  306. /* Parameter Problem (Type 12) */
  307. case ICMP_PARAMETERPROB:
  308. icmp6_hdr->icmp6_type = ICMPV6_PARAMPROB;
  309. icmp6_hdr->icmp6_code = icmp_hdr->code;
  310. icmp_ptr = ntohs(icmp_hdr->un.echo.id) >> 8;
  311. switch (icmp_ptr) {
  312. case 0:
  313. icmp6_hdr->icmp6_pointer = 0; /* IPv4 Version -> IPv6 Version */
  314. break;
  315. case 2:
  316. icmp6_hdr->icmp6_pointer = __constant_htonl(4); /* IPv4 length -> IPv6 Payload Length */
  317. break;
  318. case 8:
  319. icmp6_hdr->icmp6_pointer = __constant_htonl(7); /* IPv4 TTL -> IPv6 Hop Limit */
  320. break;
  321. case 9:
  322. icmp6_hdr->icmp6_pointer = __constant_htonl(6); /* IPv4 Protocol -> IPv6 Next Header */
  323. break;
  324. case 12:
  325. icmp6_hdr->icmp6_pointer = __constant_htonl(8); /* IPv4 Src Addr -> IPv6 Src Addr */
  326. break;
  327. case 16:
  328. icmp6_hdr->icmp6_pointer = __constant_htonl(24); /* IPv4 Dst Addr -> IPv6 Dst Addr */
  329. break;
  330. default:
  331. icmp6_hdr->icmp6_pointer = 0xffffffff; /* set to all ones in any other cases */
  332. break;
  333. }
  334. break;
  335. case ICMP_ECHO:
  336. icmperr = 0;
  337. icmp6_hdr->icmp6_type = ICMPV6_ECHO_REQUEST;
  338. icmp6_hdr->icmp6_code = 0;
  339. /* Copy rest ICMP data to new IPv6 packet without changing */
  340. memcpy(((char *)icmp6_hdr)+4, ((char *)icmp_hdr)+4, len - hdr_len - 4);
  341. break;
  342. case ICMP_ECHOREPLY:
  343. icmperr = 0;
  344. icmp6_hdr->icmp6_type = ICMPV6_ECHO_REPLY;
  345. icmp6_hdr->icmp6_code = 0;
  346. /* Copy rest ICMP data to new IPv6 packet without changing */
  347. memcpy(((char *)icmp6_hdr)+4, ((char *)icmp_hdr)+4, len - hdr_len - 4);
  348. break;
  349. /* Discard any other ICMP messages */
  350. default:
  351. PDEBUG("ip4_ip6(): Unknown ICMPv4 packet Type %x - packet dropped.\n", icmp_hdr->type);
  352. return -1;
  353. }
  354. /* Now if it's ICMPv4 Error message we must translate included IP packet */
  355. if (icmperr) {
  356. /* Call our ip4_ip6() to translate included IP packet */
  357. if (ip4_ip6(src+hdr_len+sizeof(struct icmphdr), len - hdr_len - sizeof(struct icmphdr),
  358. dst+sizeof(struct ipv6hdr)+fr_flag*sizeof(struct frag_hdr)
  359. +sizeof(struct icmp6hdr), 1) == -1) {
  360. PDEBUG("ip4_ip6(): Uncorrect translation of ICMPv4 Error message - packet dropped.\n");
  361. return -1;
  362. }
  363. /* correct ICMPv6 packet length for diffirence between IPv4 and IPv6 headers
  364. in included IP packet
  365. */
  366. icmp_len += 20;
  367. /* and correct Payload length for diffirence between IPv4 and IPv6 headers */
  368. plen += 20;
  369. ih6->payload_len = htons(plen);
  370. }
  371. /* Calculate ICMPv6 checksum */
  372. icmp6_hdr->icmp6_cksum = 0;
  373. csum = 0;
  374. csum = csum_partial((u_char *)icmp6_hdr, icmp_len, csum);
  375. icmp6_hdr->icmp6_cksum = csum_ipv6_magic(&ih6->saddr, &ih6->daddr, icmp_len,
  376. IPPROTO_ICMPV6, csum);
  377. break;
  378. /* Process TCP protocols data */
  379. case IPPROTO_TCP:
  380. /* Copy TCP data to new IPv6 packet without changing */
  381. memcpy(dst+sizeof(struct ipv6hdr)+fr_flag*sizeof(struct frag_hdr),
  382. src+hdr_len, len - hdr_len);
  383. break;
  384. /* Process UDP protocols data */
  385. case IPPROTO_UDP:
  386. udp_hdr = (struct udphdr *)(src+hdr_len);
  387. if ((ntohs(ih4->frag_off) & IP_OFFSET) == 0) {
  388. if ((ntohs(ih4->frag_off) & IP_MF) != 0) {
  389. /* It's a first fragment */
  390. if (udp_hdr->check == 0) {
  391. /* System management event */
  392. printk("siit: First fragment of UDP with zero checksum - packet droped\n");
  393. printk("siit: addr: %x src port: %d dst port: %d\n",
  394. htonl(ih4->saddr), htons(udp_hdr->source), htons(udp_hdr->dest));
  395. return -1;
  396. }
  397. }
  398. else if (udp_hdr->check == 0)
  399. fl_csum = 1;
  400. }
  401. /* Copy UDP data to new IPv6 packet */
  402. udp_hdr = (struct udphdr *)(dst+sizeof(struct ipv6hdr)
  403. + fr_flag*sizeof(struct frag_hdr));
  404. memcpy((char *)udp_hdr, src+hdr_len, len - hdr_len);
  405. /* Calculate UDP checksum if UDP checksum in IPv4 packet was ZERO
  406. and if it isn't included IP packet
  407. */
  408. if (fl_csum && (!include_flag)) {
  409. udp_hdr->check = 0;
  410. csum = 0;
  411. csum = csum_partial((unsigned char *)udp_hdr, plen - fr_flag*sizeof(struct frag_hdr), csum);
  412. udp_hdr->check = csum_ipv6_magic(&ih6->saddr, &ih6->daddr, plen -
  413. fr_flag*sizeof(struct frag_hdr), IPPROTO_UDP, csum);
  414. }
  415. break;
  416. /* Discard packets with any other protocol */
  417. default:
  418. PDEBUG("ip4_ip6(): Unknown upper protocol - packet dropped.\n");
  419. return -1;
  420. }
  421. #ifdef SIIT_DEBUG
  422. siit_print_dump(dst, sizeof(struct ipv6hdr), "siit: ip4_ip6(): (out) ipv6 header dump");
  423. #endif
  424. return 0;
  425. }
  426. /*
  427. * Translation IPv6 to IPv4 stuff
  428. *
  429. * ip6_ip4(src, len, dst, include_flag)
  430. *
  431. * where
  432. * src - buffer with original IPv6 packet,
  433. * len - size of original packet,
  434. * dst - new buffer for IPv4 packet,
  435. * include_flag - if = 1, dst point to IPv6 packet that is ICMP error
  436. * included IP packet, else = 0
  437. *
  438. */
  439. static int ip6_ip4(char *src, int len, char *dst, int include_flag)
  440. {
  441. struct ipv6hdr *ip6_hdr; /* point to current IPv6 header struct */
  442. struct iphdr *ip_hdr; /* point to current IPv4 header struct */
  443. int opts_len = 0; /* to sum Option Headers length */
  444. int icmperr = 1; /* if = 1, indicate that packet is ICMP Error message, else = 0 */
  445. int ntot_len = 0; /* to calculate IPv6 Total Length field */
  446. int real_len;
  447. int len_delta;
  448. int ip6_payload_len;
  449. int inc_opts_len = 0; /* to sum Option Headers length in ICMP included IP packet */
  450. __u8 next_hdr; /* Next Header */
  451. #ifdef SIIT_DEBUG
  452. siit_print_dump(src, sizeof(struct ipv6hdr), "siit: ip6_ip4(): (in) ipv6 header dump");
  453. #endif
  454. if ( (len_delta = len - sizeof(struct ipv6hdr)) >= 0)
  455. {
  456. ip6_hdr = (struct ipv6hdr *)src;
  457. ip_hdr = (struct iphdr *)dst;
  458. real_len = sizeof(struct iphdr);
  459. /* Check validation of Saddr & Daddr? is a packet to fall under our translation? */
  460. if (include_flag) { /* It's ICMP included IP packet,
  461. about process include_flag see comment in ip4_ip6() */
  462. if (ip6_hdr->saddr.s6_addr32[2] != htonl(MAPPED_PREFIX)) {
  463. PDEBUG("ip6_ip4(): Included IP packet Src addr isn't mapped addr: %x%x%x%x, packet dropped.\n",
  464. ip6_hdr->saddr.s6_addr32[0], ip6_hdr->saddr.s6_addr32[1],
  465. ip6_hdr->saddr.s6_addr32[2], ip6_hdr->saddr.s6_addr32[3]);
  466. return -1;
  467. }
  468. if ( ip6_hdr->daddr.s6_addr32[2] != htonl(TRANSLATED_PREFIX)) {
  469. PDEBUG("ip6_ip4(): Included IP packet Dst addr isn't translated addr: %x%x%x%x, packet dropped.\n",
  470. ip6_hdr->daddr.s6_addr32[0], ip6_hdr->daddr.s6_addr32[1],
  471. ip6_hdr->daddr.s6_addr32[2], ip6_hdr->daddr.s6_addr32[3]);
  472. return -1;
  473. }
  474. }
  475. else { /* It's normal IP packet (not included in ICMP) */
  476. if (ip6_hdr->saddr.s6_addr32[2] != htonl(TRANSLATED_PREFIX)) {
  477. PDEBUG("ip6_ip4(): Src addr isn't translated addr: %x%x%x%x, packet dropped.\n",
  478. ip6_hdr->saddr.s6_addr32[0], ip6_hdr->saddr.s6_addr32[1],
  479. ip6_hdr->saddr.s6_addr32[2], ip6_hdr->saddr.s6_addr32[3]);
  480. return -1;
  481. }
  482. if ( ip6_hdr->daddr.s6_addr32[2] != htonl(MAPPED_PREFIX)) {
  483. PDEBUG("ip6_ip4(): Dst addr isn't mapped addr: %x%x%x%x, packet dropped.\n",
  484. ip6_hdr->daddr.s6_addr32[0], ip6_hdr->daddr.s6_addr32[1],
  485. ip6_hdr->daddr.s6_addr32[2], ip6_hdr->daddr.s6_addr32[3]);
  486. return -1;
  487. }
  488. }
  489. /* Set IPv4 Fragment Offset and ID to 0
  490. before process any Option Headers */
  491. ip_hdr->frag_off = 0;
  492. ip_hdr->id = 0;
  493. /*
  494. * We process only Fragment Header. Any other options headers
  495. * are ignored, i.e. there is no attempt to translate them.
  496. * However, the Total Length field and the Protocol field would
  497. * have to be adjusted to "skip" these extension headers.
  498. */
  499. next_hdr = ip6_hdr->nexthdr;
  500. /* Hop_by_Hop options header (ip6_hdr->nexthdr = 0). It must
  501. * appear only in IPv6 header's Next Header field.
  502. */
  503. if (next_hdr == NEXTHDR_HOP) {
  504. if ( (len_delta - sizeof(struct ipv6_opt_hdr)) >= 0)
  505. {
  506. struct ipv6_opt_hdr *ip6h =
  507. (struct ipv6_opt_hdr *)(src+sizeof(struct ipv6hdr) + opts_len);
  508. if ( (len_delta -= ip6h->hdrlen*8 + 8) >= 0)
  509. {
  510. opts_len += ip6h->hdrlen*8 + 8; /* See RFC 2460 page 11:
  511. Hdr Ext Len 8-bit unsigned integer. Length of the Hop-by-
  512. Hop Options header in 8-octet units, not
  513. including the first 8 octets.
  514. */
  515. next_hdr = ip6h->nexthdr;
  516. }
  517. else
  518. {
  519. PDEBUG("ip6_ip4(): hop_by_hop header error, packet droped");
  520. /* Generate ICMP Parameter Problem */
  521. return -1;
  522. }
  523. }
  524. }
  525. if (len_delta > 0)
  526. {
  527. while(next_hdr != NEXTHDR_ICMP && next_hdr != NEXTHDR_TCP
  528. && next_hdr != NEXTHDR_UDP)
  529. {
  530. /* Destination options header */
  531. if (next_hdr == NEXTHDR_DEST)
  532. {
  533. if ( (len_delta - sizeof(struct ipv6_opt_hdr)) >= 0)
  534. {
  535. struct ipv6_opt_hdr *ip6d =
  536. (struct ipv6_opt_hdr *)(src + sizeof(struct ipv6hdr) + opts_len);
  537. if ( (len_delta -= ip6d->hdrlen*8 + 8) >= 0)
  538. {
  539. opts_len += ip6d->hdrlen*8 + 8;
  540. next_hdr = ip6d->nexthdr;
  541. }
  542. }
  543. else
  544. {
  545. PDEBUG("ip6_ip4(): destination header error, packet droped");
  546. /* Generate ICMP Parameter Problem */
  547. return -1;
  548. }
  549. }
  550. /* Routing options header */
  551. else if (next_hdr == NEXTHDR_ROUTING)
  552. {
  553. if ( (len_delta - sizeof(struct ipv6_rt_hdr)) >= 0)
  554. {
  555. struct ipv6_rt_hdr *ip6rt =
  556. (struct ipv6_rt_hdr *)(src+sizeof(struct ipv6hdr) + opts_len);
  557. /* RFC 2765 SIIT, 4.1:
  558. If a routing header with a non-zero Segments Left field is present
  559. then the packet MUST NOT be translated, and an ICMPv6 "parameter
  560. problem/ erroneous header field encountered" (Type 4/Code 0) error
  561. message, with the Pointer field indicating the first byte of the
  562. Segments Left field, SHOULD be returned to the sender.
  563. */
  564. if (ip6rt->segments_left != 0) {
  565. /* Build ICMPv6 "Parameter Problem/Erroneous Header
  566. Field Encountered" & drop the packet */
  567. /* !!! We don't send ICMPv6 "Parameter Problem" !!! */
  568. PDEBUG("ip6_ip4(): routing header type != 0\n");
  569. return -1;
  570. }
  571. if ( (len_delta -= ip6rt->hdrlen*8 + 8) >= 0)
  572. {
  573. opts_len += ip6rt->hdrlen*8 + 8;
  574. next_hdr = ip6rt->nexthdr;
  575. }
  576. else
  577. {
  578. PDEBUG("ip6_ip4(): routing header error, packet droped");
  579. /* Generate ICMP Parameter Problem */
  580. return -1;
  581. }
  582. }
  583. }
  584. /* Fragment options header */
  585. else if (next_hdr == NEXTHDR_FRAGMENT)
  586. {
  587. if ( (len_delta -= sizeof(struct frag_hdr)) >= 0)
  588. {
  589. struct frag_hdr *ip6f =
  590. (struct frag_hdr *)(src+sizeof(struct ipv6hdr)+opts_len);
  591. opts_len += sizeof(struct frag_hdr); /* Frag Header Length = 8 */
  592. ip_hdr->id = htons(ntohl(ip6f->identification)); /* ID field */
  593. ip_hdr->frag_off = htons((ntohs(ip6f->frag_off) & IP6F_OFF_MASK) >> 3);
  594. /* fragment offset */
  595. ip_hdr->frag_off = htons(ntohs(ip_hdr->frag_off) |
  596. ((ntohs(ip6f->frag_off) & IP6F_MORE_FRAG) << 13));
  597. /* more fragments flag */
  598. next_hdr = ip6f->nexthdr;
  599. }
  600. else
  601. {
  602. PDEBUG("ip6_ip4(): fragment header error, packet droped");
  603. /* Generate ICMP Parameter Problem */
  604. return -1;
  605. }
  606. }
  607. /* No Next Header */
  608. else if (next_hdr == NEXTHDR_NONE)
  609. {
  610. /* RFC 2460 IPv6 Specification, 4.7
  611. 4.7 No Next Header
  612. The value 59 in the Next Header field of an IPv6 header or any
  613. extension header indicates that there is nothing following that
  614. header. If the Payload Length field of the IPv6 header indicates the
  615. presence of octets past the end of a header whose Next Header field
  616. contains 59, those octets must be ignored, and passed on unchanged if
  617. the packet is forwarded.
  618. */
  619. break;
  620. }
  621. else if (next_hdr == NEXTHDR_ESP || next_hdr == NEXTHDR_AUTH)
  622. {
  623. PDEBUG("ip6_ip4(): cannot translate AUTH or ESP extension header, packet dropped\n");
  624. return -1;
  625. }
  626. else if (next_hdr == NEXTHDR_IPV6)
  627. {
  628. PDEBUG("ip6_ip4(): cannot translate IPv6-IPv6 packet, packet dropped\n");
  629. return -1;
  630. }
  631. else if (next_hdr == 0)
  632. {
  633. /* As say RFC 2460 (IPv6 Spec) we should discard the packet and send an
  634. ICMP Parameter Problem message to the source of the packet, with an
  635. ICMP Code value of 1 ("unrecognized Next Header type encountered")
  636. and the ICMP Pointer field containing the offset of the unrecognized
  637. value within the original packet
  638. */
  639. /* NOT IMPLEMENTED */
  640. PDEBUG("ip6_ip4(): NEXTHDR in extension header = 0, packet dropped\n");
  641. return -1;
  642. }
  643. else
  644. {
  645. PDEBUG("ip6_ip4(): cannot translate extension header = %d, packet dropped\n", next_hdr);
  646. return -1;
  647. }
  648. }
  649. }
  650. }
  651. else
  652. {
  653. PDEBUG("ip6_ip4(): error packet len, packet dropped.\n");
  654. return -1;
  655. }
  656. /* Building ipv4 packet */
  657. ip_hdr->version = IPVERSION;
  658. ip_hdr->ihl = 5;
  659. /* TOS see comment about TOS in ip4_ip6() */
  660. if (tos_ignore_flag)
  661. ip_hdr->tos = 0;
  662. else {
  663. ip_hdr->tos = ip6_hdr->priority << 4;
  664. ip_hdr->tos = ip_hdr->tos | (ip6_hdr->flow_lbl[0] >> 4);
  665. }
  666. /* IPv4 Total Len = IPv6 Payload Len +
  667. IPv4 Header Len (without options) - Options Headers Len */
  668. ip6_payload_len = ntohs(ip6_hdr->payload_len);
  669. if (ip6_payload_len == 0)
  670. ntot_len = 0;
  671. else
  672. ntot_len = ip6_payload_len + IP4_IP6_HDR_DIFF - opts_len;
  673. ip_hdr->tot_len = htons(ntot_len);
  674. /* IPv4 TTL = IPv6 Hop Limit */
  675. ip_hdr->ttl = ip6_hdr->hop_limit;
  676. /* IPv4 Protocol = Next Header that will point to upper layer protocol */
  677. ip_hdr->protocol = next_hdr;
  678. /* IPv4 Src addr = last 4 bytes from IPv6 Src addr */
  679. ip_hdr->saddr = ip6_hdr->saddr.s6_addr32[3];
  680. /* IPv4 Dst addr = last 4 bytes from IPv6 Dst addr */
  681. ip_hdr->daddr = ip6_hdr->daddr.s6_addr32[3];
  682. /* Calculate IPv4 header checksum */
  683. ip_hdr->check = 0;
  684. ip_hdr->check = ip_fast_csum((unsigned char *)ip_hdr, ip_hdr->ihl);
  685. if (len_delta > 0)
  686. {
  687. /* PROCESS ICMP */
  688. if (next_hdr == NEXTHDR_ICMP)
  689. {
  690. struct icmp6hdr *icmp6_hdr;
  691. struct icmphdr *icmp_hdr;
  692. if ((len_delta -= sizeof(struct icmp6hdr)) >= 0)
  693. {
  694. icmp6_hdr = (struct icmp6hdr *)(src + sizeof(struct ipv6hdr) + opts_len);
  695. icmp_hdr = (struct icmphdr *)(dst + sizeof(struct iphdr));
  696. real_len += len_delta + sizeof(struct icmphdr);
  697. /* There is diffirent between ICMPv4/ICMPv6 protocol codes
  698. IPPROTO_ICMP = 1
  699. IPPROTO_ICMPV6 = 58 */
  700. ip_hdr->protocol = IPPROTO_ICMP;
  701. if (include_flag) {
  702. /* !!! Warnig !!! We discard ICMP packets with any ICMP as included
  703. in ICMP Error. But ICMP Error messages can include ICMP Query message
  704. */
  705. if (icmp6_hdr->icmp6_type != ICMPV6_ECHO_REQUEST)
  706. {
  707. PDEBUG("ip6_ip4(): included ICMPv6 in ICMPv6 Error message, packet dropped\n");
  708. return -1;
  709. }
  710. }
  711. /* Translate ICMPv6 to ICMPv4 */
  712. switch (icmp6_hdr->icmp6_type)
  713. {
  714. /* ICMP Error messages */
  715. /* Destination Unreachable (Type 1) */
  716. case ICMPV6_DEST_UNREACH: /* Type 1 */
  717. icmp_hdr->type = ICMP_DEST_UNREACH; /* to Type 3 */
  718. icmp_hdr->un.echo.id = 0;
  719. icmp_hdr->un.echo.sequence = 0;
  720. switch (icmp6_hdr->icmp6_code)
  721. {
  722. case ICMPV6_NOROUTE: /* Code 0 */
  723. case ICMPV6_NOT_NEIGHBOUR: /* Code 2 */
  724. case ICMPV6_ADDR_UNREACH: /* Code 3 */
  725. icmp_hdr->code = ICMP_HOST_UNREACH; /* To Code 1 */
  726. break;
  727. case ICMPV6_ADM_PROHIBITED: /* Code 1 */
  728. icmp_hdr->code = ICMP_HOST_ANO; /* To Code 10 */
  729. break;
  730. case ICMPV6_PORT_UNREACH: /* Code 4 */
  731. icmp_hdr->code = ICMP_PORT_UNREACH; /* To Code 3 */
  732. break;
  733. default: /* discard any other codes */
  734. PDEBUG("ip6_ip4(): Unknown ICMPv6 Type %d Code %d - packet dropped.\n",
  735. ICMPV6_DEST_UNREACH, icmp6_hdr->icmp6_code);
  736. return -1;
  737. }
  738. break;
  739. /* Packet Too Big (Type 2) */
  740. case ICMPV6_PKT_TOOBIG: /* Type 2 */
  741. icmp_hdr->type = ICMP_DEST_UNREACH; /* to Type 3 */
  742. icmp_hdr->code = ICMP_FRAG_NEEDED; /* to Code 4 */
  743. /* Change MTU, RFC 2765 (SIIT), 4.2:
  744. The MTU field needs to be adjusted for the difference between
  745. the IPv4 and IPv6 header sizes taking into account whether or
  746. not the packet in error includes a Fragment header.
  747. */
  748. /* !!! Don't implement !!! */
  749. icmp_hdr->un.frag.mtu = (__u16) icmp6_hdr->icmp6_mtu;
  750. break;
  751. /* Time Exceeded (Type 3) */
  752. case ICMPV6_TIME_EXCEED:
  753. icmp_hdr->type = ICMP_TIME_EXCEEDED; /* to Type 11 */
  754. icmp_hdr->code = icmp6_hdr->icmp6_code; /* Code unchanged */
  755. break;
  756. /* Parameter Problem (Type 4) */
  757. case ICMPV6_PARAMPROB:
  758. switch (icmp6_hdr->icmp6_code) {
  759. case ICMPV6_UNK_NEXTHDR: /* Code 1 */
  760. icmp_hdr->type = ICMP_DEST_UNREACH; /* to Type 3 */
  761. icmp_hdr->code = ICMP_PROT_UNREACH; /* to Code 2 */
  762. break;
  763. default: /* if Code != 1 */
  764. icmp_hdr->type = ICMP_PARAMETERPROB; /* to Type 12 */
  765. icmp_hdr->code = 0; /* to Code 0 */
  766. /* Update Pointer field
  767. RFC 2765 (SIIT), 4.2:
  768. The Pointer needs to be updated to point to the corresponding
  769. field in the translated include IP header.
  770. */
  771. switch (ntohl(icmp6_hdr->icmp6_pointer))
  772. {
  773. case 0: /* IPv6 Version -> IPv4 Version */
  774. icmp_hdr->un.echo.id = 0;
  775. break;
  776. case 4: /* IPv6 PayloadLength -> IPv4 Total Length */
  777. icmp_hdr->un.echo.id = 0x0002; /* 2 */
  778. break;
  779. case 6: /* IPv6 Next Header-> IPv4 Protocol */
  780. icmp_hdr->un.echo.id = 0x0009; /* 9 */
  781. break;
  782. case 7: /* IPv6 Hop Limit -> IPv4 TTL */
  783. icmp_hdr->un.echo.id = 0x0008; /* 8 */
  784. break;
  785. case 8: /* IPv6 Src addr -> IPv4 Src addr */
  786. icmp_hdr->un.echo.id = 0x000c; /* 12 */
  787. break;
  788. case 24: /* IPv6 Dst addr -> IPv4 Dst addr*/
  789. icmp_hdr->un.echo.id = 0x0010; /* 16 */
  790. break;
  791. default: /* set all ones in other cases */
  792. icmp_hdr->un.echo.id = 0xff;
  793. break;
  794. }
  795. break;
  796. }
  797. break;
  798. /* End of ICMP Error messages */
  799. /* Echo Request and Echo Reply (Type 128 and 129) */
  800. case ICMPV6_ECHO_REQUEST:
  801. icmperr = 0; /* not error ICMP message */
  802. icmp_hdr->type = ICMP_ECHO; /* to Type 8 */
  803. icmp_hdr->code = 0; /* to Code 0 */
  804. icmp_hdr->un.echo.id = icmp6_hdr->icmp6_identifier;
  805. icmp_hdr->un.echo.sequence = icmp6_hdr->icmp6_sequence;
  806. /* copy rest of ICMP data to result packet */
  807. if (len_delta > 0)
  808. memcpy(((char *)icmp_hdr) + sizeof(struct icmphdr),
  809. ((char *)icmp6_hdr) + sizeof(struct icmp6hdr), len_delta);
  810. break;
  811. case ICMPV6_ECHO_REPLY:
  812. icmperr = 0; /* not error ICMP message */
  813. icmp_hdr->type = ICMP_ECHOREPLY; /* to Type 0 */
  814. icmp_hdr->code = 0; /* to Code 0 */
  815. icmp_hdr->un.echo.id = icmp6_hdr->icmp6_identifier;
  816. icmp_hdr->un.echo.sequence = icmp6_hdr->icmp6_sequence;
  817. /* copy rest of ICMP data */
  818. if (len_delta > 0)
  819. memcpy(((char *)icmp_hdr) + sizeof(struct icmphdr),
  820. ((char *)icmp6_hdr) + sizeof(struct icmp6hdr), len_delta);
  821. break;
  822. default:
  823. /* Unknown error messages. Silently drop. */
  824. PDEBUG("ip6_ip4(): unknown ICMPv6 Type %d, packet dropped.\n", icmp6_hdr->icmp6_type);
  825. return -1;
  826. }
  827. if (icmperr)
  828. {
  829. /* If ICMP Error message, we translate IP included packet*/
  830. if (len_delta >= sizeof(struct ipv6hdr))
  831. {
  832. if((inc_opts_len = ip6_ip4((char *)icmp6_hdr + sizeof(struct icmp6hdr), len_delta,
  833. (char *)icmp_hdr + sizeof(struct icmphdr), 1)) == -1) {
  834. PDEBUG("ip6_ip4(): incorrect translation of ICMPv6 Error message, packet dropped\n");
  835. return -1;
  836. }
  837. /* correct IPv4 Total Len that = old Total Len
  838. - Options Headers Len in included IP packet
  839. - diffirence between IPv6 Header Len and IPv4 Header Len
  840. */
  841. if (ntot_len != 0)
  842. ip_hdr->tot_len = htons(ntot_len - inc_opts_len - IP4_IP6_HDR_DIFF);
  843. real_len = real_len - inc_opts_len - IP4_IP6_HDR_DIFF;
  844. }
  845. else if (len_delta > 0)
  846. {
  847. /* May be it need set 0x0 to rest area in result IPv4 packet,
  848. * but we copy rest data unchanged
  849. */
  850. memcpy(((char *)icmp_hdr) + sizeof(struct icmphdr),
  851. ((char *)icmp6_hdr) + sizeof(struct icmp6hdr), len_delta);
  852. }
  853. }
  854. /* Calculate IPv4 Header checksum */
  855. ip_hdr->check = 0;
  856. ip_hdr->check = ip_fast_csum((unsigned char *)ip_hdr, ip_hdr->ihl);
  857. /* Calculate ICMPv4 checksum */
  858. if (ntot_len != 0)
  859. {
  860. icmp_hdr->checksum = 0;
  861. icmp_hdr->checksum = ip_compute_csum((unsigned char *)icmp_hdr, ntohs(ip_hdr->tot_len)
  862. - sizeof(struct iphdr));
  863. }
  864. }
  865. else
  866. {
  867. PDEBUG("ip6_ip4(): error length ICMP packet, packet dropped.\n");
  868. return -1;
  869. }
  870. }
  871. /* PROCESS TCP and UDP (and rest data) */
  872. else {
  873. real_len += len_delta;
  874. /* we copy rest data to IPv4 packet without changing */
  875. memcpy(dst+sizeof(struct iphdr), src + sizeof(struct ipv6hdr) + opts_len, len_delta);
  876. }
  877. }
  878. if (include_flag) /* if it's included IP packet */
  879. return opts_len; /* return options headers length */
  880. else
  881. return real_len; /* result packet len */
  882. }
  883. /*
  884. * ip4_fragment(skb, len, hdr_len, dev, eth_h)
  885. * to fragment original IPv4 packet if result IPv6 packet will be > 1280
  886. */
  887. static int ip4_fragment(struct sk_buff *skb, int len, int hdr_len, struct net_device *dev, struct ethhdr *eth_h)
  888. {
  889. struct sk_buff *skb2 = NULL; /* pointer to new struct sk_buff for transleded packet */
  890. char buff[FRAG_BUFF_SIZE+hdr_len]; /* buffer to form new fragment packet */
  891. char *cur_ptr = skb->data+hdr_len; /* pointter to current packet data with len = frag_len */
  892. struct iphdr *ih4 = (struct iphdr *) skb->data;
  893. struct iphdr *new_ih4 = (struct iphdr *) buff; /* point to new IPv4 hdr */
  894. struct ethhdr *new_eth_h; /* point to ether hdr, need to set hard header data in fragment */
  895. int data_len = len - hdr_len; /* origin packet data len */
  896. int rest_len = data_len; /* rest data to fragment */
  897. int frag_len = 0; /* current fragment len */
  898. int last_frag = 0; /* last fragment flag, if = 1, it's last fragment */
  899. int flag_last_mf = 0;
  900. __u16 new_id = 0; /* to generate identification field */
  901. __u16 frag_offset = 0; /* fragment offset */
  902. unsigned int csum;
  903. unsigned short udp_len;
  904. #ifdef SIIT_DEBUG
  905. printk("siit: it's DF == 0 and result IPv6 packet will be > 1280\n");
  906. siit_print_dump(skb->data, hdr_len, "siit: (orig) ipv4_hdr dump");
  907. #endif
  908. if ((ntohs(ih4->frag_off) & IP_MF) == 0 )
  909. /* it's a case we'll clear MF flag in our last packet */
  910. flag_last_mf = 1;
  911. if (ih4->protocol == IPPROTO_UDP) {
  912. if ( (ntohs(ih4->frag_off) & IP_OFFSET) == 0) {
  913. struct udphdr *udp_hdr = (struct udphdr *)((char *)ih4 + hdr_len);
  914. if (!flag_last_mf) {
  915. if (udp_hdr->check == 0) {
  916. /* it's a first fragment with ZERO checksum and we drop packet */
  917. printk("siit: First fragment of UDP with zero checksum - packet droped\n");
  918. printk("siit: addr: %x src port: %d dst port: %d\n",
  919. htonl(ih4->saddr), htons(udp_hdr->source), htons(udp_hdr->dest));
  920. return -1;
  921. }
  922. }
  923. else if (udp_hdr->check == 0) {
  924. /* Calculate UDP checksum only if it's not fragment */
  925. udp_len = ntohs(udp_hdr->len);
  926. csum = 0;
  927. csum = csum_partial((unsigned char *)udp_hdr, udp_len, csum);
  928. udp_hdr->check = csum_tcpudp_magic(ih4->saddr, ih4->daddr, udp_len, IPPROTO_UDP, csum);
  929. }
  930. }
  931. }
  932. frag_offset = ntohs(ih4->frag_off) & IP_OFFSET;
  933. new_id = ih4->id;
  934. while(1) {
  935. if (rest_len <= FRAG_BUFF_SIZE) {
  936. /* it's last fragmen */
  937. frag_len = rest_len; /* rest data */
  938. last_frag = 1;
  939. }
  940. else
  941. frag_len = FRAG_BUFF_SIZE;
  942. /* copy IP header to buffer */
  943. memcpy(buff, skb->data, hdr_len);
  944. /* copy data to buffer with len = frag_len */
  945. memcpy(buff + hdr_len, cur_ptr, frag_len);
  946. /* set id field in new IPv4 header*/
  947. new_ih4->id = new_id;
  948. /* is it last fragmet */
  949. if(last_frag && flag_last_mf)
  950. /* clear MF flag */
  951. new_ih4->frag_off = htons(frag_offset & (~IP_MF));
  952. else
  953. /* set MF flag */
  954. new_ih4->frag_off = htons(frag_offset | IP_MF);
  955. /* change packet total length */
  956. new_ih4->tot_len = htons(frag_len+hdr_len);
  957. /* rebuild the header checksum (IP needs it) */
  958. new_ih4->check = 0;
  959. new_ih4->check = ip_fast_csum((unsigned char *)new_ih4,new_ih4->ihl);
  960. /* Allocate new sk_buff to compose translated packet */
  961. skb2 = dev_alloc_skb(frag_len+hdr_len+dev->hard_header_len+IP4_IP6_HDR_DIFF+IP6_FRAGMENT_SIZE);
  962. if (!skb2) {
  963. printk(KERN_DEBUG "%s: alloc_skb failure - packet dropped.\n", dev->name);
  964. dev_kfree_skb(skb2);
  965. return -1;
  966. }
  967. /* allocate skb->data portion for IP header len, fragment data len and ether header len
  968. * and copy to head ether header from origin skb
  969. */
  970. memcpy(skb_put(skb2, frag_len+hdr_len+dev->hard_header_len+IP4_IP6_HDR_DIFF+IP6_FRAGMENT_SIZE), (char *) eth_h,
  971. dev->hard_header_len);
  972. /* correct ether header data, ether protocol field to ETH_P_IPV6 */
  973. new_eth_h = (struct ethhdr *)skb2->data;
  974. new_eth_h->h_proto = htons(ETH_P_IPV6);
  975. /* reset the mac header */
  976. skb_reset_mac_header(skb2);
  977. /* pull ether header from new skb->data */
  978. skb_pull(skb2, dev->hard_header_len);
  979. /* set skb protocol to IPV6 */
  980. skb2->protocol = htons(ETH_P_IPV6);
  981. /* call translation function */
  982. if ( ip4_ip6(buff, frag_len+hdr_len, skb2->data, 0) == -1) {
  983. dev_kfree_skb(skb2);
  984. return -1;
  985. }
  986. /*
  987. * Set needed fields in new sk_buff
  988. */
  989. skb2->dev = dev;
  990. skb2->ip_summed = CHECKSUM_UNNECESSARY;
  991. skb2->pkt_type = PACKET_HOST;
  992. /* Add transmit statistic */
  993. siit_stats(dev)->tx_packets++;
  994. siit_stats(dev)->tx_bytes += skb2->len;
  995. /* send packet to upper layer */
  996. netif_rx(skb2);
  997. /* exit if it was last fragment */
  998. if (last_frag)
  999. break;
  1000. /* correct current data pointer */
  1001. cur_ptr += frag_len;
  1002. /* rest data len */
  1003. rest_len -= frag_len;
  1004. /* current fragment offset */
  1005. frag_offset = (frag_offset*8 + frag_len)/8;
  1006. }
  1007. return 0;
  1008. }
  1009. /*
  1010. * Transmit a packet (called by the kernel)
  1011. *
  1012. * siit_xmit(skb, dev)
  1013. *
  1014. * where
  1015. * skb - pointer to struct sk_buff with incomed packet
  1016. * dev - pointer to struct device on which packet revieved
  1017. *
  1018. * Statistic:
  1019. * for all incoming packes:
  1020. * stats.rx_bytes+=skb->len
  1021. * stats.rx_packets++
  1022. * for packets we can't transle:
  1023. * stats.tx_errors++
  1024. * device busy:
  1025. * stats.tx_errors++
  1026. * for packets we can't allocate sk_buff:
  1027. * stats.tx_dropped++
  1028. * for outgoing packes:
  1029. * stats.tx_packets++
  1030. * stats.tx_bytes+=skb2->len !!! But we don't set skb2->len !!!
  1031. */
  1032. static int siit_xmit(struct sk_buff *skb, struct net_device *dev)
  1033. {
  1034. struct sk_buff *skb2 = NULL;/* pointer to new struct sk_buff for transleded packet */
  1035. struct ethhdr *eth_h; /* pointer to incoming Ether header */
  1036. int len; /* original packets length */
  1037. int new_packet_len;
  1038. int skb_delta = 0; /* delta size for allocate new skb */
  1039. char new_packet_buff[2048];
  1040. /* Check pointer to sk_buff and device structs */
  1041. if (skb == NULL || dev == NULL)
  1042. return -EINVAL;
  1043. /* Add receive statistic */
  1044. siit_stats(dev)->rx_bytes += skb->len;
  1045. siit_stats(dev)->rx_packets++;
  1046. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,7,0)
  1047. netif_trans_update(dev);
  1048. #else
  1049. dev->trans_start = jiffies;
  1050. #endif
  1051. /* Upper layer (IP) protocol forms sk_buff for outgoing packet
  1052. * and sets IP header + Ether header too. IP layer sets outgoing
  1053. * device in sk_buff->dev.
  1054. * In function (from linux/net/core/dev.c) ther is a call to
  1055. * device transmit function (dev->hard_start_xmit):
  1056. *
  1057. * dev_queue_xmit(struct sk_buff *skb)
  1058. * {
  1059. * ...
  1060. * device *dev = skb->dev;
  1061. * ...
  1062. * dev->hard_start_xmit(skb, dev);
  1063. * ...
  1064. * }
  1065. * We save pointer to ether header in eth_h and skb_pull ether header
  1066. * from data field of skb_buff
  1067. */
  1068. eth_h = (struct ethhdr *)skb->data; /* point to incoming packet Ether Header */
  1069. #ifdef SIIT_DEBUG
  1070. siit_print_dump(skb->data, ETH_HLEN, "siit: eth_hdr dump");
  1071. #endif
  1072. /* Remove hardware header from origin sk_buff */
  1073. skb_pull(skb,dev->hard_header_len);
  1074. /*
  1075. * Process IPv4 paket
  1076. */
  1077. if (ntohs(skb->protocol) == ETH_P_IP) {
  1078. int hdr_len; /* IPv4 header length */
  1079. int data_len; /* IPv4 data length */
  1080. struct iphdr *ih4; /* pointer to IPv4 header */
  1081. struct icmphdr *icmp_hdr; /* point to current ICMPv4 header struct */
  1082. ih4 = (struct iphdr *)skb->data; /* point to incoming packet's IPv4 header */
  1083. /* Check IPv4 Total Length */
  1084. if (skb->len != ntohs(ih4->tot_len)) {
  1085. PDEBUG("siit_xmit(): Different skb_len %x and ip4 tot_len %x - packet dropped.\n",
  1086. skb->len, ih4->tot_len);
  1087. siit_stats(dev)->tx_errors++;
  1088. dev_kfree_skb(skb);
  1089. return 0;
  1090. }
  1091. len = skb->len; /* packet's total len */
  1092. hdr_len = (int)(ih4->ihl * 4); /* packet's header len */
  1093. data_len = len - hdr_len; /* packet's data len */
  1094. /* If DF == 0 */
  1095. if ( (ntohs(ih4->frag_off) & IP_DF) == 0 ) {
  1096. /* If result IPv6 packet will be > 1280
  1097. we need to fragment original IPv4 packet
  1098. */
  1099. if ( data_len > FRAG_BUFF_SIZE ) {
  1100. /* call function that fragment packet and translate to IPv6 each fragment
  1101. * and send to upper layer
  1102. */
  1103. if ( ip4_fragment(skb, len, hdr_len, dev, eth_h) == -1) {
  1104. siit_stats(dev)->tx_errors++;
  1105. }
  1106. /* Free incoming skb */
  1107. dev_kfree_skb(skb);
  1108. /* Device can accept a new packet */
  1109. return 0;
  1110. }
  1111. }
  1112. /* If DF == 1 && MF == 0 && Fragment Offset == 0
  1113. * we don't include fragment header
  1114. */
  1115. if ( ntohs(ih4->frag_off) == IP_DF )
  1116. skb_delta = IP4_IP6_HDR_DIFF; /* delta is +20 */
  1117. else
  1118. skb_delta = IP4_IP6_HDR_DIFF + IP6_FRAGMENT_SIZE; /* delta is +20 and +8 */
  1119. /* If it's ICMP, check is it included IP packet in it */
  1120. if ( ih4->protocol == IPPROTO_ICMP) {
  1121. icmp_hdr = (struct icmphdr *) (skb->data+hdr_len); /* point to ICMPv4 header */
  1122. if ( icmp_hdr->type != ICMP_ECHO && icmp_hdr->type != ICMP_ECHOREPLY) {
  1123. /*
  1124. * It's ICMP Error that has included IP packet
  1125. * we'll add only +20 because we don't include Fragment Header
  1126. * into translated included IP packet
  1127. */
  1128. skb_delta += IP4_IP6_HDR_DIFF;
  1129. }
  1130. }
  1131. /* Allocate new sk_buff to compose translated packet */
  1132. skb2 = dev_alloc_skb(len+dev->hard_header_len+skb_delta);
  1133. if (!skb2) {
  1134. printk(KERN_DEBUG "%s: alloc_skb failure - packet dropped.\n", dev->name);
  1135. dev_kfree_skb(skb);
  1136. siit_stats(dev)->rx_dropped++;
  1137. return 0;
  1138. }
  1139. /* allocate skb->data portion = IPv4 packet len + ether header len
  1140. * + skb_delta (max = two times (diffirence between IPv4 header and
  1141. * IPv6 header + Frag Header), second for included packet,
  1142. * and copy to head of skb->data ether header from origin skb
  1143. */
  1144. memcpy(skb_put(skb2, len+dev->hard_header_len+skb_delta), (char *)eth_h, dev->hard_header_len);
  1145. /* correct ether header data, ether protocol field to ETH_P_IPV6 */
  1146. eth_h = (struct ethhdr *)skb2->data;
  1147. eth_h->h_proto = htons(ETH_P_IPV6);
  1148. skb_reset_mac_header(skb2);
  1149. /* remove ether header from new skb->data,
  1150. * NOTE! data will rest, pointer to data and data len will change
  1151. */
  1152. skb_pull(skb2,dev->hard_header_len);
  1153. /* set skb protocol to IPV6 */
  1154. skb2->protocol = htons(ETH_P_IPV6);
  1155. /* call translation function */
  1156. if (ip4_ip6(skb->data, len, skb2->data, 0) == -1 ) {
  1157. dev_kfree_skb(skb);
  1158. dev_kfree_skb(skb2);
  1159. siit_stats(dev)->rx_errors++;
  1160. return 0;
  1161. }
  1162. }
  1163. /*
  1164. * IPv6 paket
  1165. */
  1166. else if (ntohs(skb->protocol) == ETH_P_IPV6) {
  1167. #ifdef SIIT_DEBUG
  1168. siit_print_dump(skb->data, sizeof(struct ipv6hdr), "siit: (in) ip6_hdr dump");
  1169. #endif
  1170. /* packet len = skb->data len*/
  1171. len = skb->len;
  1172. /* call translation function */
  1173. if ((new_packet_len = ip6_ip4(skb->data, len, new_packet_buff, 0)) == -1 )
  1174. {
  1175. PDEBUG("siit_xmit(): error translation ipv6->ipv4, packet dropped.\n");
  1176. siit_stats(dev)->rx_dropped++;
  1177. goto end;
  1178. }
  1179. /* Allocate new sk_buff to compose translated packet */
  1180. skb2 = dev_alloc_skb(new_packet_len + dev->hard_header_len);
  1181. if (!skb2) {
  1182. printk(KERN_DEBUG "%s: alloc_skb failure, packet dropped.\n", dev->name);
  1183. siit_stats(dev)->rx_dropped++;
  1184. goto end;
  1185. }
  1186. memcpy(skb_put(skb2, new_packet_len + dev->hard_header_len), (char *)eth_h, dev->hard_header_len);
  1187. eth_h = (struct ethhdr *)skb2->data;
  1188. eth_h->h_proto = htons(ETH_P_IP);
  1189. skb_reset_mac_header(skb2);
  1190. skb_pull(skb2, dev->hard_header_len);
  1191. memcpy(skb2->data, new_packet_buff, new_packet_len);
  1192. skb2->protocol = htons(ETH_P_IP);
  1193. }
  1194. else {
  1195. PDEBUG("siit_xmit(): unsupported protocol family %x, packet dropped.\n", skb->protocol);
  1196. goto end;
  1197. }
  1198. /*
  1199. * Set needed fields in new sk_buff
  1200. */
  1201. skb2->pkt_type = PACKET_HOST;
  1202. skb2->dev = dev;
  1203. skb2->ip_summed = CHECKSUM_UNNECESSARY;
  1204. /* Add transmit statistic */
  1205. siit_stats(dev)->tx_packets++;
  1206. siit_stats(dev)->tx_bytes += skb2->len;
  1207. /* Send packet to upper layer protocol */
  1208. netif_rx(skb2);
  1209. end:
  1210. dev_kfree_skb(skb);
  1211. return 0;
  1212. }
  1213. static bool header_ops_init = false;
  1214. static struct header_ops siit_header_ops ____cacheline_aligned;
  1215. static const struct net_device_ops siit_netdev_ops = {
  1216. .ndo_open = siit_open,
  1217. .ndo_stop = siit_release,
  1218. .ndo_start_xmit = siit_xmit,
  1219. };
  1220. /*
  1221. * The init function initialize of the SIIT device..
  1222. * It is invoked by register_netdev()
  1223. */
  1224. static void
  1225. siit_init(struct net_device *dev)
  1226. {
  1227. ether_setup(dev); /* assign some of the fields */
  1228. random_ether_addr(dev->dev_addr);
  1229. /*
  1230. * Assign device function.
  1231. */
  1232. dev->netdev_ops = &siit_netdev_ops;
  1233. dev->flags |= IFF_NOARP; /* ARP not used */
  1234. dev->tx_queue_len = 10;
  1235. if (!header_ops_init) {
  1236. memcpy(&siit_header_ops, dev->header_ops, sizeof(struct header_ops));
  1237. siit_header_ops.cache = NULL;
  1238. }
  1239. dev->header_ops = &siit_header_ops;
  1240. }
  1241. /*
  1242. * Finally, the module stuff
  1243. */
  1244. static struct net_device *siit_dev = NULL;
  1245. int init_module(void)
  1246. {
  1247. int res = -ENOMEM;
  1248. int priv_size;
  1249. priv_size = sizeof(struct header_ops);
  1250. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
  1251. siit_dev = alloc_netdev(priv_size, "siit%d", NET_NAME_UNKNOWN, siit_init);
  1252. #else
  1253. siit_dev = alloc_netdev(priv_size, "siit%d", siit_init);
  1254. #endif
  1255. if (!siit_dev)
  1256. goto err_alloc;
  1257. res = register_netdev(siit_dev);
  1258. if (res)
  1259. goto err_register;
  1260. return 0;
  1261. err_register:
  1262. free_netdev(siit_dev);
  1263. err_alloc:
  1264. printk(KERN_ERR "Error creating siit device: %d\n", res);
  1265. return res;
  1266. }
  1267. void cleanup_module(void)
  1268. {
  1269. unregister_netdev(siit_dev);
  1270. free_netdev(siit_dev);
  1271. }
  1272. MODULE_LICENSE("GPL");