1 /******************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
18 #include <sys/socket.h>
19 #include <netlib/ethernet.h>
20 #include <netlib/ipv6.h>
21 #include <netlib/icmpv6.h>
22 #include <netlib/ndp.h>
23 #include <netlib/udp.h>
28 #define dprintf(_x ...) do { printf(_x); } while (0)
30 #define dprintf(_x ...)
33 /****************************** PROTOTYPES *******************************/
34 int8_t ip6addr_add (struct ip6addr_list_entry *new_address);
35 static void ipv6_init(int fd);
36 static int ip6_is_multicast (ip6_addr_t * ip);
38 /****************************** LOCAL VARIABLES **************************/
40 /* Own IPv6 address */
41 static struct ip6addr_list_entry *own_ip6;
43 /* Null IPv6 address */
44 static ip6_addr_t null_ip6;
46 /* helper variables */
47 static uint8_t null_mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
50 /****************************** IMPLEMENTATION ***************************/
53 * IPv6: Set the own IPv6 address.
55 * @param fd Socket descriptor
56 * @param _own_ip client IPv6 address (e.g. ::1)
59 set_ipv6_address (int fd, ip6_addr_t *_own_ip6)
61 own_ip6 = malloc (sizeof(struct ip6addr_list_entry));
63 /* If no address was passed as a parameter generate a link-local
64 * address from our MAC address.*/
66 memcpy(&(own_ip6->addr.addr),
67 ip6_create_ll_address(get_mac_address()),
70 memcpy (&(own_ip6->addr.addr), _own_ip6, 16);
72 /* Add to our list of IPv6 addresses */
73 ip6addr_add (own_ip6);
79 * IPv6: Get pointer to own IPv6 address.
81 * @return pointer to client IPv6 address (e.g. ::1)
84 get_ipv6_address (void)
86 return (ip6_addr_t *) &(own_ip6->addr);
90 * IPv6: Search for IPv6 address in list
92 * @return 0 - IPv6 address is not in list
93 * 1 - IPv6 address is in list
96 find_ip6addr (ip6_addr_t *ip)
98 struct ip6addr_list_entry *n = NULL;
103 for (n = first_ip6; n != NULL ; n=n->next)
104 if (ip6_cmp (&(n->addr), ip))
105 return 1; /* IPv6 address is in our list*/
107 return 0; /* not one of our IPv6 addresses*/
111 * NET: Handles IPv6-packets
113 * @param fd - Socket descriptor
114 * @param ip6_packet - Pointer to IPv6 header
115 * @param packetsize - Size of Ipv6 packet
116 * @return ERROR - -1 if packet is too small or unknown protocol
117 * return value of handle_udp
123 handle_ipv6 (int fd, uint8_t * ip6_packet, int32_t packetsize)
126 struct ip6hdr *ip6 = NULL;
127 ip6 = (struct ip6hdr *) ip6_packet;
129 /* Only handle packets which are for us */
130 if (! find_ip6addr(&(ip6->dst)))
133 if (packetsize < sizeof(struct ip6hdr))
134 return -1; // packet is too small
138 return handle_udp (fd, ip6_packet + sizeof (struct ip6hdr),
141 return handle_icmpv6 (fd, (struct ethhdr *) ip6_packet - sizeof(struct ethhdr),
145 return -1; // unknown protocol
149 * NET: Creates IPv6-packet. Places IPv6-header in a packet and fills it
150 * with corresponding information.
152 * Use this function with similar functions for other network layers
153 * (fill_ethhdr, fill_udphdr, fill_dnshdr, fill_btphdr).
155 * @param packet Points to the place where IPv6-header must be placed.
156 * @param packetsize Size of payload (i.e. excluding ethhdr and ip6hdr)
157 * @param ip_proto Type of the next level protocol (e.g. UDP).
158 * @param ip6_src Sender IPv6 address
159 * @param ip6_dst Receiver IPv6 address
168 fill_ip6hdr (uint8_t * packet, uint16_t packetsize,
169 uint8_t ip_proto, ip6_addr_t *ip6_src, ip6_addr_t *ip6_dst)
172 struct ip6hdr * ip6h = (struct ip6hdr *) packet;
174 ip6h->ver_tc_fl = 6 << 28; // set version to 6
175 ip6h->pl = packetsize; // IPv6 payload size
178 memcpy (&(ip6h->src), ip6_src, IPV6_ADDR_LENGTH);
179 memcpy (&(ip6h->dst), ip6_dst, IPV6_ADDR_LENGTH);
183 * NET: For a given MAC calculates EUI64-Identifier.
184 * See RFC 4291 "IP Version 6 Addressing Architecture"
188 mac2eui64 (const uint8_t *mac)
193 memcpy (eui64id, mac, 3);
194 memcpy (eui64id + 5, mac + 3, 3);
198 memcpy(&retid, eui64id, 8);
203 * NET: create link-local IPv6 address
205 * @param own_mac MAC of NIC
206 * @return ll_addr pointer to newly created link-local address
209 ip6_create_ll_address (const uint8_t *own_mac)
213 ll_addr = malloc (sizeof (struct ip6addr_list_entry));
214 memset (ll_addr, 0, IPV6_ADDR_LENGTH);
215 ll_addr->part.prefix |= IPV6_LL_PREFIX;
216 ll_addr->part.interface_id |= mac2eui64((uint8_t *) own_mac);
222 * NET: check if we already have an address with the same prefix.
223 * @param struct ip6_addr_list_entry *ip6
224 * @return true or false
227 unknown_prefix (ip6_addr_t *ip)
229 struct ip6addr_list_entry *node;
231 for( node = first_ip6; node != NULL; node=node->next )
232 if( node->addr.part.prefix == ip->part.prefix )
233 return 0; /* address is one of ours */
235 return 1; /* prefix not yet in our list */
239 * NET: Create empty element for prefix list and return a pointer to it;
240 * @return NULL - malloc failed
241 * ! NULL - pointer to new prefix_info
244 ip6_create_prefix_info ()
246 struct prefix_info *prfx_info;
248 prfx_info = malloc (sizeof(struct prefix_info));
256 * NET: create a new IPv6 address with a given network prefix
257 * and add it to our IPv6 address list
259 * @param ip6_addr prefix (as received in RA)
260 * @return NULL - pointer to new ip6addr_list entry
263 ip6_prefix2addr (ip6_addr_t prefix)
265 struct ip6addr_list_entry *new_address;
266 uint64_t interface_id;
268 new_address = malloc (sizeof(struct ip6addr_list_entry));
272 /* fill new addr struct */
273 /* extract prefix from Router Advertisement */
274 memcpy (&(new_address->addr.part.prefix), &prefix, 8 );
276 /* interface id is generated from MAC address */
277 interface_id = mac2eui64 (get_mac_address());
278 memcpy (&(new_address->addr.part.interface_id), &interface_id, 8);
284 * NET: add new IPv6 adress to list
286 * @param ip6_addr *new_address
287 * @return 0 - passed pointer = NULL;
291 ip6addr_add (struct ip6addr_list_entry *new_address)
293 struct ip6addr_list_entry *solicited_node;
296 if (new_address == NULL)
299 /* Don't add the same address twice */
300 if (find_ip6addr (&(new_address->addr)))
303 /* If address is a unicast address, we also have to process packets
304 * for its solicited-node multicast address.
305 * See RFC 2373 - IP Version 6 Adressing Architecture */
306 if (! ip6_is_multicast(&(new_address->addr))) {
309 solicited_node = malloc(sizeof(struct ip6addr_list_entry));
310 if (! solicited_node)
313 solicited_node->addr.part.prefix = IPV6_SOLIC_NODE_PREFIX;
314 solicited_node->addr.part.interface_id = IPV6_SOLIC_NODE_IFACE_ID;
315 solicited_node->addr.addr[13] = new_address->addr.addr[13];
316 solicited_node->addr.addr[14] = new_address->addr.addr[14];
317 solicited_node->addr.addr[15] = new_address->addr.addr[15];
318 ip6addr_add (solicited_node);
321 if (NULL == first_ip6)
322 first_ip6 = new_address;
323 last_ip6->next = new_address;
324 last_ip6 = new_address;
325 last_ip6->next = NULL;
327 return 1; /* no error */
331 * NET: Initialize IPv6
333 * @param fd socket fd
340 send_ip = &send_ipv6;
342 /* Address configuration parameters */
343 ip6_state.managed_mode = 0;
345 /* Null IPv6 address */
346 null_ip6.part.prefix = 0;
347 null_ip6.part.interface_id = 0;
349 /* Multicast addresses */
350 all_nodes_ll.addr.part.prefix = 0xff02000000000000;
351 all_nodes_ll.addr.part.interface_id = 1;
352 all_dhcpv6_ll.addr.part.prefix = 0xff02000000000000ULL;
353 all_dhcpv6_ll.addr.part.interface_id = 0x10002ULL;
354 all_routers_ll.addr.part.prefix = 0xff02000000000000;
355 all_routers_ll.addr.part.interface_id = 2;
357 ip6addr_add(&all_nodes_ll);
362 last_router = first_router;
364 /* Init Neighbour cache */
365 first_neighbor = NULL;
366 last_neighbor = first_neighbor;
368 send_router_solicitation (fd);
369 for(i=0; i < 4 && !is_ra_received(); i++) {
370 set_timer(TICKS_SEC);
373 if (is_ra_received())
375 } while (get_timer() > 0);
380 * NET: compare IPv6 adresses
382 * @param ip6_addr ip_1
383 * @param ip6_addr ip_2
386 ip6_cmp (ip6_addr_t *ip_1, ip6_addr_t *ip_2)
388 return ((int8_t) !memcmp( &(ip_1->addr[0]), &(ip_2->addr[0]),
393 * NET: Calculate checksum over IPv6 header and upper-layer protocol
394 * (e.g. UDP or ICMPv6)
396 * @param *ip - pointer to IPv6 address
397 * @return true or false
400 ip6_is_multicast (ip6_addr_t * ip)
403 return ! memcmp(&ip->addr[0], &mc, 1);
407 * NET: Generate multicast MAC address from IPv6 address
408 * (e.g. UDP or ICMPv6)
410 * @param *ip - pointer to IPv6 address
411 * @return pointer to Multicast MAC address
414 ip6_to_multicast_mac (ip6_addr_t * ip)
418 mc_mac = malloc(ETH_ALEN);
424 memcpy (mc_mac+2, (uint8_t *) &(ip->addr)+12, 4);
430 * NET: calculate checksum over IPv6 header and upper-layer protocol
431 * (e.g. UDP or ICMPv6)
433 * @param struct ip6hdr *ip6h - pointer to IPv6 header
434 * @param unsigned short *packet - pointer to header of upper-layer
436 * @param int words - number of words (as in 2 bytes)
437 * starting from *packet
440 static unsigned short
441 ip6_checksum (struct ip6hdr *ip6h, unsigned short *packet, int words)
444 unsigned long checksum;
445 struct ip6hdr pseudo_ip6h;
446 unsigned short *pip6h;
448 memcpy (&pseudo_ip6h, ip6h, sizeof(struct ip6hdr));
449 pseudo_ip6h.hl = ip6h->nh;
450 pseudo_ip6h.ver_tc_fl = 0;
452 pip6h = (unsigned short *) &pseudo_ip6h;
454 for (checksum = 0; words > 0; words--) {
455 checksum += *packet++;
459 for (i = 0; i < 20; i++) {
460 checksum += *pip6h++;
463 checksum = (checksum >> 16) + (checksum & 0xffff);
464 checksum += (checksum >> 16);
470 * NET: Handles IPv6-packets
472 * @param fd socket fd
473 * @param ip6_packet Pointer to IPv6 header in packet
474 * @param packetsize Size of IPv6 packet
475 * @return -1 == ERRROR
476 * return of handle_udp() or handle_icmp6()
482 send_ipv6 (int fd, void* buffer, int len)
487 struct icmp6hdr *icmp6h;
489 uint8_t *mac_addr, mac[6];
493 ip6h = (struct ip6hdr *) buffer;
494 udph = (struct udphdr *) ((uint8_t *) ip6h + sizeof (struct ip6hdr));
495 icmp6h = (struct icmp6hdr *) ((uint8_t *) ip6h + sizeof (struct ip6hdr));
497 memcpy(&ip_dst, &ip6h->dst, 16);
499 if(len + sizeof(struct ethhdr) > 1500)
502 if ( ip6_cmp (&ip6h->src, &null_ip6))
503 memcpy (&(ip6h->src), get_ipv6_address(), IPV6_ADDR_LENGTH);
505 if (ip6h->nh == 17) {//UDP
506 udph->uh_sum = ip6_checksum (ip6h, (unsigned short *) udph ,
508 /* As per RFC 768, if the computed checksum is zero,
509 * it is transmitted as all ones (the equivalent in
510 * one's complement arithmetic).
512 if (udph->uh_sum == 0)
513 udph->uh_sum = ~udph->uh_sum;
515 else if (ip6h->nh == 0x3a) //ICMPv6
516 icmp6h->checksum = ip6_checksum (ip6h,
517 (unsigned short *) icmp6h,
520 n = find_neighbor (&ip_dst);
522 // If packet is a neighbor solicitation
523 if (icmp6h->type == ICMPV6_NEIGHBOUR_SOLICITATION) {
524 mac_addr = ip6_to_multicast_mac (&ip_dst);
525 fill_ethhdr( buffer-sizeof(struct ethhdr), htons(ETHERTYPE_IPv6),
530 // If address is a multicast address, create a proper mac address
531 else if (ip6_is_multicast (&ip_dst)) {
532 mac_addr = ip6_to_multicast_mac (&ip_dst);
535 // Check if the MAC address is already cached
537 if (memcmp(n->mac, null_mac, ETH_ALEN) != 0)
538 memcpy (mac_addr, &(n->mac), ETH_ALEN); /* found it */
541 n = malloc(sizeof(struct neighbor));
542 memcpy(&(n->ip.addr[0]), &ip_dst, 16);
543 n->status = NB_PROBE;
548 if (! memcmp (mac_addr, &null_mac, 6)) {
549 if (n->eth_len == 0) {
550 send_neighbour_solicitation (fd, &ip_dst);
552 // Store the packet until we know the MAC address
553 memset(n->eth_frame, 0, 1500);
554 fill_ethhdr (n->eth_frame,
555 htons(ETHERTYPE_IPv6),
558 memcpy (&(n->eth_frame[sizeof(struct ethhdr)]),
561 set_timer(TICKS_SEC);
564 } while (get_timer() > 0);
569 fill_ethhdr (n->eth_frame, htons(ETHERTYPE_IPv6), get_mac_address(),
571 memcpy (&(n->eth_frame[sizeof(struct ethhdr)]), buffer, len);
572 return send_ether (fd, n->eth_frame, len + sizeof(struct ethhdr));
576 check_colons(const char *str)
582 dprintf("str : %s\n",str);
583 pch = strchr(str, ':');
586 pch = strchr(pch+1, ':');
590 col--; /* Its part of double colon */
595 dprintf("The number of col : %d \n",col);
596 dprintf("The number of dcol : %d \n",dcol);
598 if((dcol > 1) || /* Cannot have 2 "::" */
599 ((dcol == 1) && (col > 5)) || /* Too many ':'s */
600 ((dcol == 0) && (col != 7)) ) { /* Too few ':'s */
601 dprintf(" exiting for check_colons \n");
609 ipv6str_to_bytes(const char *str, char *ip)
614 uint32_t cnt = 0, len;
616 dprintf("str : %s \n",str);
619 if (cnt > 15 || !isxdigit(*str)){
622 if ((pos = strchr(str, ':')) != NULL) {
623 len = (int16_t) (pos - str);
624 dprintf("\t len is : %d \n",len);
627 strncpy(block, str, len);
629 dprintf("\t str : %s \n",str);
630 dprintf("\t block : %s \n",block);
633 strncpy(block, str, 4);
635 dprintf("\t str : %s \n",str);
636 dprintf("\t block : %s \n",block);
637 str += strlen(block);
639 res = strtol(block, NULL, 16);
640 dprintf("\t res : %x \n",res);
641 if ((res > 0xFFFF) || (res < 0))
643 ip[cnt++] = (res & 0xFF00) >> 8;
644 ip[cnt++] = (res & 0x00FF);
650 dprintf("cnt : %d\n",cnt);
654 int str_to_ipv6(const char *str, uint8_t *ip)
659 char tmp[30], buf[16];
663 if(!check_colons(str))
666 if ((ptr = strstr(str, "::")) != NULL) {
667 /* Handle the ::1 IPv6 loopback */
668 if(!strcmp(str,"::1")) {
673 dprintf(" len : %d \n",len);
674 if (len >= sizeof(tmp))
676 strncpy(tmp, str, len);
680 i = ipv6str_to_bytes(ptr, buf);
684 #if defined(ARGS_DEBUG)
686 dprintf("=========== bottom part i : %d \n",i);
688 dprintf("%02x \t",buf[j]);
691 /* Copy the bottom part i.e bytes following "::" */
692 memcpy(ip+(16-i), buf, i);
694 k = ipv6str_to_bytes(tmp, buf);
698 #if defined(ARGS_DEBUG)
699 dprintf("=========== top part k : %d \n",k);
701 printf("%02x \t",buf[j]);
704 /* Copy the top part i.e bytes before "::" */
706 #if defined(ARGS_DEBUG)
709 dprintf("%02x \t",ip[j]);
713 i = ipv6str_to_bytes(str, (char *)ip);
718 void ipv6_to_str(const uint8_t *ip, char *str)
721 uint8_t byte_even, byte_odd;
722 char *consec_zero, *strptr;
725 for (i = 0; i < 16; i+=2) {
729 sprintf(str, "%s%x%02x", str, byte_even, byte_odd);
731 sprintf(str, "%s%x", str, byte_odd);
739 consec_zero = strstr(strptr, "0:0:");
741 len = consec_zero - strptr;
744 else if (strptr[len-1] == ':')
747 strptr = consec_zero + 2;
749 } while (consec_zero);
751 len = consec_zero - str;
757 strptr = consec_zero + 4;
759 if (!strncmp(strptr, "0:", 2))
765 if (!strcmp(str, "::0"))