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