Annotation of libaitio/example/bpf.c, revision 1.1.2.6
1.1.2.1 misho 1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <fcntl.h>
4: #include <unistd.h>
5: #include <string.h>
6: #include <errno.h>
7: #include <poll.h>
8: #include <getopt.h>
9: #include <pthread.h>
10: #include <assert.h>
11: #include <sys/types.h>
12: #include <sys/param.h>
13: #include <sys/limits.h>
14: #include <sys/socket.h>
15: #include <sys/ioctl.h>
16: #include <sys/mman.h>
17: #include <net/if.h>
18: #include <net/bpf.h>
19: #include <machine/atomic.h>
20:
21: #ifdef __FreeBSD__
22: #include <net/ethernet.h>
23: #endif
24: #ifdef __OpenBSD__
25: #include <net/ethertypes.h>
26: #include <netinet/in_systm.h>
27: #endif
28: #include <net/if_arp.h>
29: #include <netinet/in.h>
30: #include <netinet/ip.h>
31: #include <netinet/ip6.h>
32: #include <netinet/udp.h>
33: #include <netinet/tcp.h>
34: #include <netinet/ip_icmp.h>
35: #include <arpa/inet.h>
36: #ifdef __OpenBSD__
37: #include <netinet/if_ether.h>
38: #endif
39:
1.1.2.2 misho 40: #include <aitio.h>
41:
1.1.2.1 misho 42: #ifndef roundup
43: #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
44: #endif
45:
46: #define time_spec_sub(vvp, uvp) \
47: do { \
48: (vvp)->tv_sec -= (uvp)->tv_sec; \
49: (vvp)->tv_nsec -= (uvp)->tv_nsec; \
50: if ((vvp)->tv_nsec < 0) { \
51: (vvp)->tv_sec--; \
52: (vvp)->tv_nsec += 1000000000; \
53: } \
54: } while (0)
55:
56:
57: int Verbose, flg;
58:
59:
60: static void *
61: ShowPkt(void *buffer)
62: {
63: char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 };
64: struct bpf_hdr *bpf = buffer;
65: #ifdef __FreeBSD__
66: struct bpf_zbuf_header *bzh = buffer;
67: struct icmphdr *icmp;
68: #endif
69: #ifdef __OpenBSD__
70: struct icmp *icmp;
71: #endif
72: struct ether_header *eth;
73: struct ip *ip;
74: struct ip6_hdr *ipv6;
75: struct arphdr *arp;
76: struct udphdr *udp;
77: struct tcphdr *tcp;
78:
79: assert(buffer);
80:
81: #ifdef __FreeBSD__
82: snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", flg ? bzh->bzh_kernel_len : bpf->bh_datalen);
83: strlcat(szShow, szLine, USHRT_MAX);
84: eth = (struct ether_header *) (buffer + (flg ? bzh->bzh_kernel_len : bpf->bh_hdrlen));
85: #else
86: snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", bpf->bh_datalen);
87: strlcat(szShow, szLine, USHRT_MAX);
88: eth = (struct ether_header *) (buffer + bpf->bh_hdrlen);
89: #endif
90:
91: switch (ntohs(eth->ether_type)) {
92: case ETHERTYPE_ARP:
93: strlcpy(szWrk, "(ARP)", sizeof szWrk);
94: break;
95: case ETHERTYPE_IP:
96: strlcpy(szWrk, "(IP)", sizeof szWrk);
97: break;
98: case ETHERTYPE_IPV6:
99: strlcpy(szWrk, "(IPv6)", sizeof szWrk);
100: break;
101: default:
102: memset(szWrk, 0, sizeof szWrk);
103: }
104: snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1],
105: eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
106: snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr);
107: strlcat(szShow, szLine, USHRT_MAX);
108: snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1],
109: eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]);
110: snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr);
111: strlcat(szShow, szLine, USHRT_MAX);
112:
113: switch (ntohs(eth->ether_type)) {
114: case ETHERTYPE_ARP:
115: arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
116: strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX);
117:
118: switch (ntohs(arp->ar_op)) {
119: case ARPOP_REQUEST:
120: strlcpy(szStr, "Request", sizeof szStr);
121: break;
122: case ARPOP_REPLY:
123: strlcpy(szStr, "Reply", sizeof szStr);
124: break;
125: case ARPOP_REVREQUEST:
126: strlcpy(szStr, "RevRequest", sizeof szStr);
127: break;
128: case ARPOP_REVREPLY:
129: strlcpy(szStr, "RevReply", sizeof szStr);
130: break;
131: case ARPOP_INVREQUEST:
132: strlcpy(szStr, "InvRequest", sizeof szStr);
133: break;
134: case ARPOP_INVREPLY:
135: strlcpy(szStr, "InvReply", sizeof szStr);
136: break;
137: default:
138: strlcpy(szStr, "*Unknown*", sizeof szStr);
139: }
140: snprintf(szLine, BUFSIZ, "\tAddr_fmt: %d, Proto_fmt: %d, Addr_len: %d, Proto_len: %d, %s\n",
141: ntohs(arp->ar_hrd), ntohs(arp->ar_pro), arp->ar_hln, arp->ar_pln, szStr);
142: strlcat(szShow, szLine, USHRT_MAX);
143: break;
144: case ETHERTYPE_IP:
145: ip = (struct ip*) (((caddr_t) eth) + ETHER_HDR_LEN);
146: strlcat(szShow, "\t>>> IP ...\n", USHRT_MAX);
147:
148: snprintf(szStr, BUFSIZ, "Frag_off: %d", ntohs(ip->ip_off) & IP_OFFMASK);
149: if (ntohs(ip->ip_off) & IP_RF)
150: strlcat(szStr, "|RF", sizeof szStr);
151: if (ntohs(ip->ip_off) & IP_DF)
152: strlcat(szStr, "|DF", sizeof szStr);
153: if (ntohs(ip->ip_off) & IP_MF)
154: strlcat(szStr, "|MF", sizeof szStr);
155: snprintf(szLine, BUFSIZ, "\tTOS: %02x, TTL: %d, ID: %04x, Cksum: %04x, Len: %d, %s\n",
156: ip->ip_tos, ip->ip_ttl, ntohs(ip->ip_id), ntohs(ip->ip_sum),
157: ntohs(ip->ip_len), szStr);
158: strlcat(szShow, szLine, USHRT_MAX);
159: snprintf(szLine, BUFSIZ, "\tProto: %d, Src_addr: %s, Dst_addr: %s\n",
160: ip->ip_p, inet_ntop(AF_INET, &ip->ip_src, szStr, BUFSIZ),
161: inet_ntop(AF_INET, &ip->ip_dst, szWrk, BUFSIZ));
162: strlcat(szShow, szLine, USHRT_MAX);
163:
164: Proto = ip->ip_p;
165: break;
166: case ETHERTYPE_IPV6:
167: ipv6 = (struct ip6_hdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
168: strlcat(szShow, "\t>>> IPv6 ...\n", USHRT_MAX);
169:
170: snprintf(szLine, BUFSIZ, "\tHLim: %d, Payload_len: %d, Flow: %u\n", ipv6->ip6_hlim,
171: htons(ipv6->ip6_plen), htonl(ipv6->ip6_flow) & IPV6_FLOWLABEL_MASK);
172: strlcat(szShow, szLine, USHRT_MAX);
173: inet_ntop(AF_INET6, &ipv6->ip6_src, szStr, BUFSIZ);
174: inet_ntop(AF_INET6, &ipv6->ip6_dst, szWrk, BUFSIZ);
175: snprintf(szLine, BUFSIZ, "\tNext: %d, Src_addr: %s, Dst_addr: %s\n", ipv6->ip6_nxt, szStr, szWrk);
176: strlcat(szShow, szLine, USHRT_MAX);
177:
178: Proto = ipv6->ip6_nxt;
179: break;
180: }
181:
182: if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6))
183: switch (Proto) {
184: case IPPROTO_TCP:
185: strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX);
186: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
187: tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
188: else
189: tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip));
190:
191: snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n",
192: ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack);
193: strlcat(szShow, szLine, USHRT_MAX);
194: snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->th_off >> 4);
195: if (tcp->th_flags & TH_FIN)
196: strlcat(szWrk, "|FIN", sizeof szWrk);
197: if (tcp->th_flags & TH_SYN)
198: strlcat(szWrk, "|SYN", sizeof szWrk);
199: if (tcp->th_flags & TH_RST)
200: strlcat(szWrk, "|RST", sizeof szWrk);
201: if (tcp->th_flags & TH_PUSH)
202: strlcat(szWrk, "|PUSH", sizeof szWrk);
203: if (tcp->th_flags & TH_ACK)
204: strlcat(szWrk, "|ACK", sizeof szWrk);
205: if (tcp->th_flags & TH_URG)
206: strlcat(szWrk, "|URG", sizeof szWrk);
207: if (tcp->th_flags & TH_ECE)
208: strlcat(szWrk, "|ECE", sizeof szWrk);
209: if (tcp->th_flags & TH_CWR)
210: strlcat(szWrk, "|CWR", sizeof szWrk);
211: snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n",
212: szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum));
213: strlcat(szShow, szLine, USHRT_MAX);
214: break;
215: case IPPROTO_UDP:
216: strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX);
217: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
218: udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
219: else
220: udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip));
221:
222: snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n",
223: ntohs(udp->uh_sport), ntohs(udp->uh_dport),
224: ntohs(udp->uh_ulen), ntohs(udp->uh_sum));
225: strlcat(szShow, szLine, USHRT_MAX);
226: break;
227: case IPPROTO_ICMP:
228: strlcat(szShow, "\t\t>>> ICMP ...\n", USHRT_MAX);
229: #ifdef __FreeBSD__
230: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
231: icmp = (struct icmphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
232: else
233: icmp = (struct icmphdr*) (((caddr_t) ip) + sizeof(struct ip));
234: #endif
235: #ifdef __OpenBSD__
236: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
237: icmp = (struct icmp*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
238: else
239: icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip));
240: #endif
241:
242: snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n",
243: icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum));
244: strlcat(szShow, szLine, USHRT_MAX);
245: break;
246: }
247:
248: printf("%s===\n", szShow);
249: pthread_exit(NULL);
250: }
251:
252: // ----------------------
253:
254: int
255: main(int argc, char **argv)
256: {
257: u_int n, count = (u_int) -1;
258: register int i;
259: int dev, fd, ret, siz = 0;
260: char szStr[BUFSIZ], szMap[MAXPATHLEN] = { 0 }, *buffer = NULL;
261: struct ifreq ifr;
262: struct pollfd pfd = { 0 };
263: pthread_t tid;
264: char ch, mode = 'R';
265: struct timespec ts_start, ts_end;
1.1.2.3 misho 266: void *bz = NULL;
1.1.2.1 misho 267:
268: while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1)
269: switch (ch) {
270: case 'w':
271: mode = 'W';
272: break;
273: case 's':
274: siz = strtol(optarg, NULL, 0);
275: break;
276: case 'p':
277: count = strtol(optarg, NULL, 0);
278: break;
279: case 'f':
280: strlcpy(szMap, optarg, sizeof szMap);
281: break;
282: case 'z':
283: flg++;
284: break;
285: case 'v':
286: Verbose++;
287: break;
288: case 'h':
289: default:
290: return 1;
291: }
292: argc -= optind;
293: argv += optind;
294: if (argc < 1)
295: return 1;
296:
297: if (**argv == '/')
298: strlcpy(szStr, strrchr(*argv, '/') + 1, sizeof szStr);
299: else
300: strlcpy(szStr, *argv, sizeof szStr);
301:
1.1.2.4 misho 302: #ifdef __FreeBSD__
303: dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, &bz);
304: if (dev == -1)
305: dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, NULL);
1.1.2.1 misho 306: #else
1.1.2.4 misho 307: dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, NULL);
1.1.2.1 misho 308: #endif
309: if (dev == -1) {
310: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
311: return 1;
312: } else
313: printf("dev=%d(%s)\n", dev, szStr);
314:
1.1.2.6 ! misho 315: if (ioctl(dev, BIOCGBLEN, &siz) == -1) {
1.1.2.1 misho 316: perror("ioctl(BIOCGBLEN)");
1.1.2.5 misho 317: io_etherClose(dev, &bz);
1.1.2.1 misho 318: return 1;
319: } else
1.1.2.6 ! misho 320: printf("BPF buffer len=%d\n", siz);
1.1.2.1 misho 321:
1.1.2.6 ! misho 322: if (*szMap) {
! 323: fd = open(szMap, O_RDWR);
! 324: if (fd == -1) {
! 325: perror("open(map)");
1.1.2.5 misho 326: io_etherClose(dev, &bz);
1.1.2.1 misho 327: return 1;
328: }
1.1.2.6 ! misho 329: buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
! 330: close(fd);
! 331: } else
! 332: buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
! 333: if (buffer == MAP_FAILED) {
! 334: perror("mmap()");
! 335: io_etherClose(dev, &bz);
! 336: return 1;
1.1.2.1 misho 337: }
338:
339: pfd.fd = dev;
340: assert(!clock_gettime(CLOCK_REALTIME, &ts_start));
341: if (mode == 'R') {
342: pfd.events = POLLIN;
343: for (i = 0; i < count; i++) {
344: if (poll(&pfd, 1, -1) == -1)
345: break;
346:
1.1.2.6 ! misho 347: ret = io_etherRecv(dev, buffer, siz, bz);
! 348: if (ret == -1)
! 349: printf("%d) io_etherRecv(%d) #%d - %s\n", i, ret,
! 350: io_GetErrno(), io_GetError());
! 351: if (Verbose) {
! 352: printf("%d) +readed %d bytes\n", i, ret);
! 353: pthread_create(&tid, NULL, ShowPkt, buffer);
! 354: pthread_join(tid, NULL);
1.1.2.1 misho 355: }
356: }
357: } else {
358: pfd.events = POLLOUT;
359: for (i = 0; i < count; i++) {
360: if (poll(&pfd, 1, -1) == -1)
361: break;
362:
1.1.2.5 misho 363: ret = io_etherSend(dev, buffer, siz);
1.1.2.1 misho 364: if (ret == -1)
1.1.2.5 misho 365: printf("io_etherSend(%d) #%d - %s\n", ret, io_GetErrno(), io_GetError());
1.1.2.1 misho 366: if (Verbose)
367: printf("+writed %d bytes\n", ret);
368: }
369: }
370: assert(!clock_gettime(CLOCK_REALTIME, &ts_end));
371:
1.1.2.6 ! misho 372: munmap(buffer, siz);
1.1.2.1 misho 373: io_etherClose(dev, &bz);
374:
375: time_spec_sub(&ts_end, &ts_start);
376: printf("%d.%09lu for %d iterations\n", ts_end.tv_sec, ts_end.tv_nsec, i);
377: ts_end.tv_sec *= 1000000000 / i;
378: printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i);
379: return 0;
380: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>