void send_arp(struct link_int *l, u_char * device) { int n; u_char *buf; buf = (u_char *) malloc(ARP_H + ETH_H); if (!buf) { perror("no packet memory"); exit(EXIT_FAILURE); } memset(buf, 0, ARP_H + ETH_H); build_ethernet(enet_dst, enet_src, ETHERTYPE_ARP, NULL, 0, buf); build_arp(ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REQUEST, enet_src, (void *)&ip_dst, enet_dst, (void *)&ip_dst, NULL, 0, buf + ETH_H); n = write_link_layer(l, device, buf, ARP_H + ETH_H); printf("Wrote %d byte ARP packet through linktype %d\n", n, l->linktype); }
/* Send ARP Reply packet */ void inject_arp_reply(char *eth_src, char *ip_src, char *eth_target, char *ip_target) { packet_t *p = NULL; ether_t *ether = NULL; arp_t *arp; p = packet_new_empty(); ether = build_ethernet(eth_src, eth_target, ETHER_TYPE_ARP); packet_append_header(p, PROTO_NAME_ETHER, (void *)ether, ETHER_HDR_LEN); arp = build_arp_ethip(ARP_OP_REPLY, eth_src, ip_src, eth_target, ip_target); packet_append_header(p, PROTO_NAME_ARP, (void *)arp, sizeof(arp_t) + sizeof(arp_ethip_t)); inject(p); packet_free(p); free(ether); free(arp); }
/* Send custom ARP message */ void inject_arp(char *eth_src, char *eth_dst, _uint16 opcode, char *sha, char *spa, char *tha, char *tpa) { packet_t *p = NULL; ether_t *ether = NULL; arp_t *arp; p = packet_new_empty(); ether = build_ethernet(eth_src, eth_dst, ETHER_TYPE_ARP); packet_append_header(p, PROTO_NAME_ETHER, (void *)ether, ETHER_HDR_LEN); arp = build_arp_ethip(opcode, sha, spa, tha, tpa); packet_append_header(p, PROTO_NAME_ARP, (void *)arp, sizeof(arp_t) + sizeof(arp_ethip_t)); inject(p); packet_free(p); free(ether); free(arp); }
int main(int argc, char *argv[]) { libnet_t *l; char *device = "eth0"; char errbuf[LIBNET_ERRBUF_SIZE]; libnet_ptag_t ipo_ptag = 0; libnet_ptag_t ip_ptag = 0; libnet_ptag_t eth_ptag = 0; int ip_len = 0; l = libnet_init( LIBNET_LINK, device, errbuf); assert(l); printf("Packet: options=4, payload=0\n"); ip_len = 20 + 4 + 0; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 4); ip_ptag = build_ipv4(l, ip_ptag, 0, 24); eth_ptag = build_ethernet(l, eth_ptag); assert_lengths(l, 24, 6, 0); ipo_ptag = ip_ptag = eth_ptag = 0; libnet_clear_packet(l); printf("Packet: options=3, payload=1\n"); ip_len = 20 + 4 + 1; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 3); ip_ptag = build_ipv4(l, ip_ptag, 1, 25); eth_ptag = build_ethernet(l, eth_ptag); assert_lengths(l, 25, 6, 1); ipo_ptag = ip_ptag = eth_ptag = 0; libnet_clear_packet(l); printf("Packet: options=3, payload=1\n"); ip_len = 20 + 4 + 1; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 3); ip_ptag = build_ipv4(l, ip_ptag, 1, ip_len); eth_ptag = build_ethernet(l, eth_ptag); assert_lengths(l, 25, 6, 1); printf("... modify -> options=40\n"); ip_len = 20 + 40 + 1; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 40); assert_lengths(l, ip_len, 15, 1); printf("... modify -> options=0\n"); ip_len = 20 + 0 + 1; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 0); assert_lengths(l, ip_len, 5, 1); printf("... modify -> options=5\n"); ip_len = 20 + 8 + 1; /* ip + options + payload */ ipo_ptag = build_ipo(l, ipo_ptag, 5); assert_lengths(l, ip_len, 7, 1); printf("... modify -> ip_payload=5\n"); ip_len = 20 + 8 + 5; /* ip + options + payload */ ip_ptag = build_ipv4(l, ip_ptag, 5, ip_len); assert_lengths(l, ip_len, 7, 1); ipo_ptag = ip_ptag = eth_ptag = 0; libnet_clear_packet(l); return (EXIT_SUCCESS); }