title: UDP packet does not arrive if not pinged beforehand
body: <p>I have a code to send a UDP datagram packet from a linux equip (server) to another (client). I have an issue when do this secuence:</p>
  
  <ol>
  <li>reboot server.</li>
  <li>run the program to send the packet and it is not received by the client.</li>
  </ol>
  
  <p>if then I do a ping to the client ip, and run again the program, now the packet is received by the cliente</p>
  
  <p>This is the code:</p>
  
  <pre><code>  int main (int argc, char **argv){
    int i, status, datalen, frame_length, sd, bytes;
    char *interface, *target, *src_ip, *dst_ip;
    struct ip6_hdr iphdr;
    struct udphdr udphdr;
    uint8_t *data, *src_mac, *dst_mac, *ether_frame;
    struct addrinfo hints, *res;
    struct sockaddr_in6 *ipv6;
    struct sockaddr_ll device;
    struct ifreq ifr;
    void *tmp;
  
    // Allocate memory for various arrays.
    src_mac = allocate_ustrmem (6);
    dst_mac = allocate_ustrmem (6);
    data = allocate_ustrmem (IP_MAXPACKET);
    ether_frame = allocate_ustrmem (IP_MAXPACKET);
    interface = allocate_strmem (INET6_ADDRSTRLEN);
    target = allocate_strmem (INET6_ADDRSTRLEN);
    src_ip = allocate_strmem (INET6_ADDRSTRLEN);
    dst_ip = allocate_strmem (INET6_ADDRSTRLEN);
  
    // Interface to send packet through.
    strcpy (interface, "usb0");
  
    // Submit request for a socket descriptor to look up interface.
    if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) &lt; 0) {
      perror ("socket() failed to get socket descriptor for using ioctl() ");
      exit (EXIT_FAILURE);
    }
  
    // Use ioctl() to look up interface name and get its MAC address.
    memset (&amp;ifr, 0, sizeof (ifr));
    snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
    if (ioctl (sd, SIOCGIFHWADDR, &amp;ifr) &lt; 0) {
      perror ("ioctl() failed to get source MAC address ");
      return (EXIT_FAILURE);
    }
    close (sd);
  
    // Copy source MAC address.
    memcpy (src_mac, ifr.ifr_hwaddr.sa_data, 6 * sizeof (uint8_t));
  
    // Report source MAC address to stdout.
    printf ("MAC address for interface %s is ", interface);
    for (i=0; i&lt;5; i++) {
      printf ("%02x:", src_mac[i]);
    }
    printf ("%02x\n", src_mac[5]);
  
    // Find interface index from interface name and store index in
    // struct sockaddr_ll device, which will be used as an argument of sendto().
    memset (&amp;device, 0, sizeof (device));
    if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
      perror ("if_nametoindex() failed to obtain interface index ");
      exit (EXIT_FAILURE);
    }
    printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);
  
    // Set destination MAC address
    dst_mac[0] = 0x00;
    dst_mac[1] = 0x11;
    dst_mac[2] = 0x7d;
    dst_mac[3] = 0x30;
    dst_mac[4] = 0x7f;
    dst_mac[5] = 0xd0;
  
    // Source IPv6 address
    strcpy (src_ip, "fe80::11:7dff:fe30:8013");
  
    // Destination URL or IPv6 address
    strcpy (target, "fe80:0000:0000:0000:0211:7d00:0030:7fd0");
  
    // Fill out hints for getaddrinfo().
    memset (&amp;hints, 0, sizeof (hints));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = hints.ai_flags | AI_CANONNAME;
  
    // Resolve target using getaddrinfo().
    if ((status = getaddrinfo (target, NULL, &amp;hints, &amp;res)) != 0) {
      fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
      exit (EXIT_FAILURE);
    }
    ipv6 = (struct sockaddr_in6 *) res-&gt;ai_addr;
    tmp = &amp;(ipv6-&gt;sin6_addr);
    if (inet_ntop (AF_INET6, tmp, dst_ip, INET6_ADDRSTRLEN) == NULL) {
      status = errno;
      fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
      exit (EXIT_FAILURE);
    }
    freeaddrinfo (res);
  
    // Fill out sockaddr_ll.
    device.sll_family = AF_PACKET;
    memcpy (device.sll_addr, src_mac, 6 * sizeof (uint8_t));
    device.sll_halen = 6;
  
    // UDP data
    datalen = 4;
    data[0] = 'T';
    data[1] = 'E';
    data[2] = 'S';
    data[3] = 'T';
  
    // IPv6 header
  
    // IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
    iphdr.ip6_flow = htonl ((6 &lt;&lt; 28) | (0 &lt;&lt; 20) | 0);
  
    // Payload length (16 bits): UDP header + UDP data
    iphdr.ip6_plen = htons (UDP_HDRLEN + datalen);
  
    // Next header (8 bits): 17 for UDP
    iphdr.ip6_nxt = IPPROTO_UDP;
  
    // Hop limit (8 bits): default to maximum value
    iphdr.ip6_hops = 255;
  
    // Source IPv6 address (128 bits)
    if ((status = inet_pton (AF_INET6, src_ip, &amp;(iphdr.ip6_src))) != 1) {
      fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
      exit (EXIT_FAILURE);
    }
  
    // Destination IPv6 address (128 bits)
    if ((status = inet_pton (AF_INET6, dst_ip, &amp;(iphdr.ip6_dst))) != 1) {
      fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
      exit (EXIT_FAILURE);
    }
  
    // UDP header
  
    // Source port number (16 bits): pick a number
    udphdr.source = htons (61616);
  
    // Destination port number (16 bits): pick a number
    udphdr.dest = htons (61616);
  
    // Length of UDP datagram (16 bits): UDP header + UDP data
    udphdr.len = htons (UDP_HDRLEN + datalen);
  
    // UDP checksum (16 bits)
    udphdr.check = udp6_checksum (iphdr, udphdr, data, datalen);
  
    // Fill out ethernet frame header.
  
    // Ethernet frame length = ethernet header (MAC + MAC + ethernet type) + ethernet data (IP header + UDP header + UDP data)
    frame_length = 6 + 6 + 2 + IP6_HDRLEN + UDP_HDRLEN + datalen;
  
    // Destination and Source MAC addresses
    memcpy (ether_frame, dst_mac, 6 * sizeof (uint8_t));
    memcpy (ether_frame + 6, src_mac, 6 * sizeof (uint8_t));
  
    // Next is ethernet type code (ETH_P_IPV6 for IPv6).
    // http://www.iana.org/assignments/ethernet-numbers
    ether_frame[12] = ETH_P_IPV6 / 256;
    ether_frame[13] = ETH_P_IPV6 % 256;
  
    // Next is ethernet frame data (IPv6 header + UDP header + UDP data).
  
    // IPv6 header
    memcpy (ether_frame + ETH_HDRLEN, &amp;iphdr, IP6_HDRLEN * sizeof (uint8_t));
  
    // UDP header
    memcpy (ether_frame + ETH_HDRLEN + IP6_HDRLEN, &amp;udphdr, UDP_HDRLEN * sizeof (uint8_t));
  
    // UDP data
    memcpy (ether_frame + ETH_HDRLEN + IP6_HDRLEN + UDP_HDRLEN, data, datalen * sizeof (uint8_t));
  
    // Submit request for a raw socket descriptor.
    if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) &lt; 0) {
      perror ("socket() failed ");
      exit (EXIT_FAILURE);
    }
  
    // Send ethernet frame to socket.
    if ((bytes = sendto (sd, ether_frame, frame_length, 0, (struct sockaddr *) &amp;device, sizeof (device))) &lt;= 0) {
      perror ("sendto() failed");
      exit (EXIT_FAILURE);
    }
  
    // Close socket descriptor.
    close (sd);
  
    // Free allocated memory.
    free (src_mac);
    free (dst_mac);
    free (data);
    free (ether_frame);
    free (interface);
    free (target);
    free (src_ip);
    free (dst_ip);
  
    return (EXIT_SUCCESS);
  }
  </code></pre>