Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / env / common / vera / niu_ippktgen / C / wrapper / pgLibnet.c
CommitLineData
86530b38
AT
1#if (HAVE_CONFIG_H)
2#include "../include/config.h"
3#endif
4
5#include "include/libnet.h"
6
7#include "include/pg.h"
8#include "include/packet.h"
9
10/* AJXXXX */
11libnet_t * libnet_init_pkt(int injection_type, char *device, char *err_buf);
12int libnet_write_pkt(libnet_t *l, u_int8_t **packet, u_int32_t *len,
13 u_int32_t align);
14void libnet_print_pkt(u_int8_t *pktbuf, u_int32_t pktlen);
15
16
17extern unsigned char * genPayload (int data_type, unsigned int seed,
18 int data_length);
19extern unsigned char * addPayload (unsigned char *p1, int l1,
20 unsigned char *p2, int l2) ;
21extern unsigned char * addCrc (unsigned char *p1, int l1, unsigned int crc) ;
22extern unsigned char * addL2Pad(unsigned char * pktbuf,int pktlen,int l2_pad_length) ;
23
24extern int libnet_gen_l4_packet(struct packet_desc *packet_vars, libnet_t *l);
25extern int libnet_gen_l3_packet(struct packet_desc *packet_vars, libnet_t *l);
26extern int libnet_gen_l2_packet(struct packet_desc *packet_vars, libnet_t *l);
27
28
29
30char * libnet_call(struct packet_desc *packet_vars, u_int32_t *total_len) {
31
32 int c;
33 libnet_t *l;
34 char errbuf[LIBNET_ERRBUF_SIZE];
35 /* AJXXXX */
36 struct libnet_stats ls;
37 u_int32_t pktlen=0;
38 u_int32_t pktalign=0;
39 u_int8_t *pktbuf=NULL;
40 u_int32_t crc;
41 int status;
42 int l2_length;
43 int payload_start_ptr;
44
45
46/*
47 * Initialize the library. Root priviledges are required.
48 */
49 /* AJXXXX */
50 l = libnet_init_pkt(
51 LIBNET_LINK, /* injection type */
52 NULL, /* network interface */
53 errbuf); /* error buffer */
54
55 if (l == NULL)
56 {
57 fprintf(stderr, "libnet_init() failed: %s", errbuf);
58 exit(EXIT_FAILURE);
59 }
60
61
62 // The guts of packet gen
63
64 if(packet_vars->l2_pkt_type==L2_RUNT) {
65 l2_length = packet_vars->payload.data_length;
66 if(l2_length>4) {
67 l2_length= l2_length - 4;
68 // generate payload
69 pktbuf = genPayload(packet_vars->payload.data_type,
70 packet_vars->payload.data_seed,
71 l2_length);
72 pktlen = l2_length;
73 crc = libnet_compute_crc(pktbuf,pktlen);
74 pktbuf = addCrc(pktbuf,pktlen,crc);
75 pktlen+=4;
76 *total_len = pktlen;
77 } else { /*NOCRC*/
78 // generate payload
79 pktbuf = genPayload(packet_vars->payload.data_type,
80 packet_vars->payload.data_seed,
81 l2_length);
82 pktlen = l2_length;
83 *total_len = pktlen;
84
85 }
86 } else {
87 status = libnet_gen_l4_packet(packet_vars, l);
88 if(status == -1) {
89 printf("ERROR libnet_gen_l4_packet failed \n");
90 return(NULL);
91 }
92
93
94 status = libnet_gen_l3_packet(packet_vars, l);
95 if(status == -1) {
96 printf("ERROR libnet_gen_l3_packet failed \n");
97 return(NULL);
98 }
99 status = libnet_gen_l2_packet(packet_vars, l);
100 if(status == -1) {
101 printf("ERROR libnet_gen_l2_packet failed \n");
102 return(NULL);
103 }
104
105 /* sbehera - need this for IP header alignment */
106 pktalign = 2;
107 c = libnet_write_pkt(l, &pktbuf, &pktlen, pktalign);
108 if (c == -1)
109 {
110 fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
111 return(NULL);
112 }
113 payload_start_ptr = packet_vars->l2_hdr+packet_vars->l3_hdr+ packet_vars->l4_hdr;
114
115 if( ( packet_vars->payload.error_code & PG_CHKSUM_ERR) ){
116 if (packet_vars->l4_pkt_type & L4_PKT_TYPE_TCP) {
117 printf("Libnet CheckSum - %x%x\n",pktbuf[payload_start_ptr-4],pktbuf[payload_start_ptr-3]);
118 pktbuf[payload_start_ptr-4] = pktbuf[payload_start_ptr-4]^1;
119 } else if(packet_vars->l4_pkt_type & L4_PKT_TYPE_UDP) {
120 printf("Libnet CheckSum - %x%x\n",pktbuf[payload_start_ptr-2],pktbuf[payload_start_ptr-1]);
121 pktbuf[payload_start_ptr-1] = pktbuf[payload_start_ptr-1]^1;
122 }
123 }
124
125
126 pktbuf = addL2Pad(pktbuf,pktlen,packet_vars->payload.l2_pad_length);
127 pktlen+=packet_vars->payload.l2_pad_length;
128
129 crc = libnet_compute_crc(pktbuf,pktlen);
130
131 printf(" Libnet CRC - %x \n",crc);
132 if(packet_vars->payload.error_code & PG_CRC_ERR) {
133 crc = crc^0x01010101;
134 printf("Libnet CRCError - %x \n",crc);
135 }
136
137 pktbuf = addCrc(pktbuf,pktlen,crc);
138 pktlen+=4;
139 *total_len = pktlen;
140 }
141
142
143#if 1 /* AJXXXX */
144 // c = libnet_write_pkt(l, &pktbuf, &pktlen, pktalign);
145 // crc = libnet_compute_crc(pktbuf,pktlen);
146 // pktbuf = addCrc(pktbuf,pktlen,crc);
147 // pktlen+=4;
148#endif
149
150 if (c == -1) {
151 fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
152 return(NULL);
153 } else {
154 fprintf(stderr, "Wrote %d byte packet; check the wire.\n", pktlen);
155#if 1 /* AJXXXX */
156 // libnet_print_pkt(pktbuf, pktlen);
157 return(pktbuf);
158#endif
159 }
160
161}