i'm trying send custom ipv6 header through raw socket in c linux.
succeded in ipv4 using ip_hdrincl socket option, however, there's not equivalent ipv6.
found workaround here suggesting use socket(af_inet6, sock_raw, ipproto_raw) have same effect enabling ip_hdrincl socket option.
socket created , i'm not getting error until use sendto function modified header.
i setup socket this:
static int socketfd = 0; static struct sockaddr_in6 remote; int main() { socketfd = socket (pf_inet6, sock_raw, ipproto_raw); if (socketfd < 0) { printf ("an error ocurred while creating socket.\n"); exit (2); } remote.sin6_family = af_inet6; remote.sin6_port = htons (25000); if (inet_pton (af_inet6, "fd00:c0de::70d6:4ab9:115d:8cda", &(remote.sin6_addr)) != 1) { close (socketfd); printf ("unable parse ipv6 address.\n"); exit (2); } /*more code */ ... return 0; } and then, have callback function should send custom ipv6 packets sendto fails returning einval.
static void sendpacket () { char buffer[bufsiz]; const size_t len = sizeof(struct ip6_hdr) + sizeof(struct udp_hdr); struct ip6_hdr *ip6 = (struct ip6_hdr*) (buffer); struct udp_hdr *udp = (struct udp_hdr *) (buffer + sizeof(struct ip6_hdr)); memset (buffer, 0, bufsiz); ip6->ip6_ctlun.ip6_un2_vfc = 0x60; ip6->ip6_dst = remote.sin6_addr; ip6->ip6_flow = 60; ip6->ip6_hops = 64; ip6->ip6_nxt = 17; ip6->ip6_plen = sizeof(struct udp_hdr); if (inet_pton (af_inet6, "fd00:c0de::62a4:4cff:1234:5678", &(ip6->ip6_src)) != 1) { printf ("error while parsing spoofed ipv6 address.\n"); return; } // fabricate udp header. source port number, redundant udp->uh_sport = htons (21000); // destination port number udp->uh_dport = remote.sin6_port; udp->uh_ulen = htons (sizeof(struct udp_hdr)); if (sendto (socketfd, buffer, len, 0, (struct sockaddr *) &remote, sizeof(remote)) < 0) { perror ("sendto"); printf ("error while sending packet.\n"); } } i've debugged program , values in ip6_hdr struct seem correct , ones in struct udp_hdr. missing something?
ok, found solution. struggling this, way solved using pcap library (followed this example). must use af_packets, found complete example uses raw sockets accomplish in link.
Comments
Post a Comment