1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 Ramon Fried <rfried.dev@gmail.com>
4 */
5
6 #include <env.h>
7 #include <net.h>
8 #include <net/pcap.h>
9 #include <time.h>
10 #include <linux/errno.h>
11 #include <asm/io.h>
12
13 #define LINKTYPE_ETHERNET 1
14
15 static bool initialized;
16 static bool running;
17 static bool buffer_full;
18 static void *buf;
19 static unsigned int max_size;
20 static unsigned int pos;
21
22 static unsigned long incoming_count;
23 static unsigned long outgoing_count;
24
25 struct pcap_header {
26 u32 magic;
27 u16 version_major;
28 u16 version_minor;
29 s32 thiszone;
30 u32 sigfigs;
31 u32 snaplen;
32 u32 network;
33 };
34
35 struct pcap_packet_header {
36 u32 ts_sec;
37 u32 ts_usec;
38 u32 incl_len;
39 u32 orig_len;
40 };
41
42 static struct pcap_header file_header = {
43 .magic = 0xa1b2c3d4,
44 .version_major = 2,
45 .version_minor = 4,
46 .snaplen = 65535,
47 .network = LINKTYPE_ETHERNET,
48 };
49
pcap_init(phys_addr_t paddr,unsigned long size)50 int pcap_init(phys_addr_t paddr, unsigned long size)
51 {
52 buf = map_physmem(paddr, size, 0);
53 if (!buf) {
54 printf("Failed mapping PCAP memory\n");
55 return -ENOMEM;
56 }
57
58 printf("PCAP capture initialized: addr: 0x%lx max length: %lu\n",
59 (unsigned long)buf, size);
60
61 memcpy(buf, &file_header, sizeof(file_header));
62 pos = sizeof(file_header);
63 max_size = size;
64 initialized = true;
65 running = false;
66 buffer_full = false;
67 incoming_count = 0;
68 outgoing_count = 0;
69 return 0;
70 }
71
pcap_start_stop(bool start)72 int pcap_start_stop(bool start)
73 {
74 if (!initialized) {
75 printf("error: pcap was not initialized\n");
76 return -ENODEV;
77 }
78
79 running = start;
80
81 return 0;
82 }
83
pcap_clear(void)84 int pcap_clear(void)
85 {
86 if (!initialized) {
87 printf("error: pcap was not initialized\n");
88 return -ENODEV;
89 }
90
91 pos = sizeof(file_header);
92 incoming_count = 0;
93 outgoing_count = 0;
94 buffer_full = false;
95
96 printf("pcap capture cleared\n");
97 return 0;
98 }
99
pcap_post(const void * packet,size_t len,bool outgoing)100 int pcap_post(const void *packet, size_t len, bool outgoing)
101 {
102 struct pcap_packet_header header;
103 u64 cur_time = timer_get_us();
104
105 if (!initialized || !running || !buf)
106 return -ENODEV;
107
108 if (buffer_full)
109 return -ENOMEM;
110
111 if ((pos + len + sizeof(header)) >= max_size) {
112 buffer_full = true;
113 printf("\n!!! Buffer is full, consider increasing buffer size !!!\n");
114 return -ENOMEM;
115 }
116
117 header.ts_sec = cur_time / 1000000;
118 header.ts_usec = cur_time % 1000000;
119 header.incl_len = len;
120 header.orig_len = len;
121
122 memcpy(buf + pos, &header, sizeof(header));
123 pos += sizeof(header);
124 memcpy(buf + pos, packet, len);
125 pos += len;
126
127 if (outgoing)
128 outgoing_count++;
129 else
130 incoming_count++;
131
132 env_set_hex("pcapsize", pos);
133
134 return 0;
135 }
136
pcap_print_status(void)137 int pcap_print_status(void)
138 {
139 if (!initialized) {
140 printf("pcap was not initialized\n");
141 return -ENODEV;
142 }
143 printf("PCAP status:\n");
144 printf("\tInitialized addr: 0x%lx\tmax length: %u\n",
145 (unsigned long)buf, max_size);
146 printf("\tStatus: %s.\t file size: %u\n", running ? "Active" : "Idle",
147 pos);
148 printf("\tIncoming packets: %lu Outgoing packets: %lu\n",
149 incoming_count, outgoing_count);
150
151 return 0;
152 }
153
pcap_active(void)154 bool pcap_active(void)
155 {
156 return running;
157 }
158