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