PERFORCE change 141883 for review
Ryan French
rfrench at FreeBSD.org
Tue May 20 03:33:06 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=141883
Change 141883 by rfrench at rfrench_mpls on 2008/05/20 03:32:12
MPLS-Needle is now working as planned. It will read in the config file, extract the information from the file for the headers, and send the packets to an available BPF device. I will include a readme file on how the config file is structured at a later date. All in all it should be pretty simple to see from the included one thou.
Submitted by: Ryan French
Affected files ...
.. //depot/projects/soc2008/rfrench_mpls/mpls-needle/mpls-needle.c#3 edit
.. //depot/projects/soc2008/rfrench_mpls/mpls-needle/mpls-needle.conf#2 edit
Differences ...
==== //depot/projects/soc2008/rfrench_mpls/mpls-needle/mpls-needle.c#3 (text+ko) ====
@@ -58,10 +58,9 @@
char line[128];
uint32_t i;
- char dev[16];
- size_t len = 16;
+ int fd;
+ size_t len;
- uint8_t *buf;
ssize_t wb;
//datagram data
@@ -71,11 +70,9 @@
if (argc < 5)
{
fprintf(stderr,"incorrect usage of program - refer to documentation\n");
- return 1;
+ return -1;
}
- dl_bpf_open(&if_name);
-
for(i=1; i<argc; i++)
{
if(strcmp(argv[i], "-i") == 0)
@@ -93,26 +90,32 @@
printf("if_name = %s\n", if_name);
printf("config_name = %s\n", config_name);
+ if((fd = dl_bpf_open(if_name)) == -1)
+ {
+ fprintf(stderr, "unable to open bpf device on interface %s\n", if_name);
+ return -1;
+ }
+
FILE *config = fopen(config_name, "r");
if(config == NULL)
{
fprintf(stderr, "unable to open config file '%s'\n", config_name);
- return 2;
+ return -1;
}
while(!(feof(config)))
{
fgets(line, 128, config);
- build_packets(line, datagram, if_name);
- if((wb = write(dev, datagram, len)) < (ssize_t)len)
+ build_packets(line, datagram, if_name, &len);
+ if((wb = write(fd, datagram, len)) < (ssize_t)len)
{
if(wb == -1)
{
- fprintf(stderr, "%d bytes failed", len);
+ fprintf(stderr, "%d bytes failed\n", len);
}
else
{
- printf("%d bytes sent of %d total", wb, len);
+ printf("%d bytes sent of %d total\n", wb, len);
}
return -1;
@@ -121,6 +124,8 @@
//close config file
fclose(config);
+ //close the bpf device
+ close(fd);
return 0;
}
@@ -279,7 +284,7 @@
return 0;
}
-int build_packets(char *line, uint8_t *datagram, char *if_name)
+int build_packets(char *line, uint8_t *datagram, char *if_name, size_t *len)
{
uint32_t if_index;
@@ -306,6 +311,7 @@
struct ether_header *eth = (struct ether_header *)(datagram);
struct ip *iph = (struct ip *)(datagram + sizeof(uint32_t) + sizeof(struct ether_header));
struct icmphdr *icmph = (struct icmphdr *)(datagram + sizeof(struct ip) + sizeof(uint32_t) + sizeof(struct ether_header));
+ uint32_t mplsh;
//zero out the buffer
memset(datagram, 0, 1500);
@@ -372,26 +378,28 @@
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = ip4_tos;
- iph->ip_len = (sizeof(struct ip) + sizeof(struct icmphdr));
+ iph->ip_len = htons((iph->ip_hl<<2) + 8 + 20);
iph->ip_id = htonl (54321);
iph->ip_off = 0;
iph->ip_ttl = ip4_ttl;
iph->ip_p = 1;
iph->ip_sum = 0;
- iph->ip_sum = in_cksum((unsigned short *)iph, sizeof(iph));
iph->ip_src.s_addr = inet_addr(inet_ntoa(ip4_src));
iph->ip_dst.s_addr = inet_addr(inet_ntoa(ip4_dst));
+ iph->ip_sum = in_cksum((unsigned short *)iph, iph->ip_hl<<2);
//set values in icmp header
icmph->icmp_type = icmp_type;
icmph->icmp_code = icmp_code;
icmph->icmp_cksum = 0;
- icmph->icmp_cksum = in_cksum((unsigned short *)icmph, sizeof(icmph));
+ icmph->icmp_cksum = htons(in_cksum((unsigned short *)icmph, sizeof(icmph)));
//set values in mpls header
- uint32_t mplsh = htonl(mpls_label << 12 | mpls_qos << 9 | mpls_bos_flag << 8 | mpls_ttl);
+ mplsh = htonl(mpls_label << 12 | mpls_qos << 9 | mpls_bos_flag << 8 | mpls_ttl);
memcpy(&datagram[14], &mplsh, 4);
+ *len = (14 + (sizeof(mplsh)) + (iph->ip_hl<<2) + 8 + 20);
+
printf("\nbuilt packets values\n");
printf("dst_mac = %s\n", ether_ntoa(&dst_mac));
@@ -468,7 +476,7 @@
do
{
- snprintf(dev, len, "/dev/bpf%d\n", i);
+ snprintf(dev, len, "/dev/bpf%d", i);
if((fd = open(dev, O_RDWR)) == -1)
{
if(errno == EBUSY)
@@ -513,4 +521,3 @@
return fd;
}
-
==== //depot/projects/soc2008/rfrench_mpls/mpls-needle/mpls-needle.conf#2 (text+ko) ====
@@ -1,1 +1,3 @@
-dst_mac 00:16:d3:fc:c0:b8 ip4 10.1.60.134/10.1.60.134/2/0 icmp 0/0 mpls 1/1/1/3 +dst_mac 00:16:d3:fc:c0:b8 ip4 10.1.18.149/10.1.18.162/2/0 icmp 0/0 mpls 55/1/1/3
+dst_mac 00:16:d3:fc:c0:b8 ip4 10.1.18.149/10.1.18.162/2/0 icmp 0/0 mpls 44/1/1/3
+dst_mac 00:16:d3:fc:c0:b8 ip4 10.1.18.149/10.1.18.162/2/0 icmp 0/0 mpls 66/1/1/3
More information about the p4-projects
mailing list