1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2004
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <common.h>
8 #include <command.h>
9 #include <env.h>
10 #include <log.h>
11 #include <stdio_dev.h>
12 #include <net.h>
13 
14 #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE
15 #define CONFIG_NETCONSOLE_BUFFER_SIZE 512
16 #endif
17 
18 static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE];
19 static int input_size; /* char count in input buffer */
20 static int input_offset; /* offset to valid chars in input buffer */
21 static int input_recursion;
22 static int output_recursion;
23 static int net_timeout;
24 static uchar nc_ether[6]; /* server enet address */
25 static struct in_addr nc_ip; /* server ip */
26 static short nc_out_port; /* target output port */
27 static short nc_in_port; /* source input port */
28 static const char *output_packet; /* used by first send udp */
29 static int output_packet_len;
30 /*
31  * Start with a default last protocol.
32  * We are only interested in NETCONS or not.
33  */
34 enum proto_t net_loop_last_protocol = BOOTP;
35 
nc_wait_arp_handler(uchar * pkt,unsigned dest,struct in_addr sip,unsigned src,unsigned len)36 static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
37 				 struct in_addr sip, unsigned src,
38 				 unsigned len)
39 {
40 	net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
41 }
42 
nc_handler(uchar * pkt,unsigned dest,struct in_addr sip,unsigned src,unsigned len)43 static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip,
44 		       unsigned src, unsigned len)
45 {
46 	if (input_size)
47 		net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
48 }
49 
nc_timeout_handler(void)50 static void nc_timeout_handler(void)
51 {
52 	net_set_state(NETLOOP_SUCCESS);
53 }
54 
is_broadcast(struct in_addr ip)55 static int is_broadcast(struct in_addr ip)
56 {
57 	static struct in_addr netmask;
58 	static struct in_addr our_ip;
59 	static int env_changed_id;
60 	int env_id = env_get_id();
61 
62 	/* update only when the environment has changed */
63 	if (env_changed_id != env_id) {
64 		netmask = env_get_ip("netmask");
65 		our_ip = env_get_ip("ipaddr");
66 
67 		env_changed_id = env_id;
68 	}
69 
70 	return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */
71 		((netmask.s_addr & our_ip.s_addr) ==
72 		 (netmask.s_addr & ip.s_addr) && /* on the same net and */
73 		 (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */
74 }
75 
refresh_settings_from_env(void)76 static int refresh_settings_from_env(void)
77 {
78 	const char *p;
79 	static int env_changed_id;
80 	int env_id = env_get_id();
81 
82 	/* update only when the environment has changed */
83 	if (env_changed_id != env_id) {
84 		if (env_get("ncip")) {
85 			nc_ip = env_get_ip("ncip");
86 			if (!nc_ip.s_addr)
87 				return -1;	/* ncip is 0.0.0.0 */
88 			p = strchr(env_get("ncip"), ':');
89 			if (p != NULL) {
90 				nc_out_port = simple_strtoul(p + 1, NULL, 10);
91 				nc_in_port = nc_out_port;
92 			}
93 		} else {
94 			nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */
95 		}
96 
97 		p = env_get("ncoutport");
98 		if (p != NULL)
99 			nc_out_port = simple_strtoul(p, NULL, 10);
100 		p = env_get("ncinport");
101 		if (p != NULL)
102 			nc_in_port = simple_strtoul(p, NULL, 10);
103 
104 		if (is_broadcast(nc_ip))
105 			/* broadcast MAC address */
106 			memset(nc_ether, 0xff, sizeof(nc_ether));
107 		else
108 			/* force arp request */
109 			memset(nc_ether, 0, sizeof(nc_ether));
110 	}
111 	return 0;
112 }
113 
114 /**
115  * Called from net_loop in net/net.c before each packet
116  */
nc_start(void)117 void nc_start(void)
118 {
119 	refresh_settings_from_env();
120 	if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) {
121 		/* going to check for input packet */
122 		net_set_udp_handler(nc_handler);
123 		net_set_timeout_handler(net_timeout, nc_timeout_handler);
124 	} else {
125 		/* send arp request */
126 		uchar *pkt;
127 		net_set_arp_handler(nc_wait_arp_handler);
128 		pkt = (uchar *)net_tx_packet + net_eth_hdr_size() +
129 			IP_UDP_HDR_SIZE;
130 		memcpy(pkt, output_packet, output_packet_len);
131 		net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port,
132 				    output_packet_len);
133 	}
134 }
135 
nc_input_packet(uchar * pkt,struct in_addr src_ip,unsigned dest_port,unsigned src_port,unsigned len)136 int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
137 	unsigned src_port, unsigned len)
138 {
139 	int end, chunk;
140 
141 	if (dest_port != nc_in_port || !len)
142 		return 0; /* not for us */
143 
144 	if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip))
145 		return 0; /* not from our client */
146 
147 	debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
148 
149 	if (input_size == sizeof(input_buffer))
150 		return 1; /* no space */
151 	if (len > sizeof(input_buffer) - input_size)
152 		len = sizeof(input_buffer) - input_size;
153 
154 	end = input_offset + input_size;
155 	if (end >= sizeof(input_buffer))
156 		end -= sizeof(input_buffer);
157 
158 	chunk = len;
159 	/* Check if packet will wrap in input_buffer */
160 	if (end + len >= sizeof(input_buffer)) {
161 		chunk = sizeof(input_buffer) - end;
162 		/* Copy the second part of the pkt to start of input_buffer */
163 		memcpy(input_buffer, pkt + chunk, len - chunk);
164 	}
165 	/* Copy first (or only) part of pkt after end of current valid input*/
166 	memcpy(input_buffer + end, pkt, chunk);
167 
168 	input_size += len;
169 
170 	return 1;
171 }
172 
nc_send_packet(const char * buf,int len)173 static void nc_send_packet(const char *buf, int len)
174 {
175 #ifdef CONFIG_DM_ETH
176 	struct udevice *eth;
177 #else
178 	struct eth_device *eth;
179 #endif
180 	int inited = 0;
181 	uchar *pkt;
182 	uchar *ether;
183 	struct in_addr ip;
184 
185 	debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
186 
187 	eth = eth_get_dev();
188 	if (eth == NULL)
189 		return;
190 
191 	if (!memcmp(nc_ether, net_null_ethaddr, 6)) {
192 		if (eth_is_active(eth))
193 			return;	/* inside net loop */
194 		output_packet = buf;
195 		output_packet_len = len;
196 		input_recursion = 1;
197 		net_loop(NETCONS); /* wait for arp reply and send packet */
198 		input_recursion = 0;
199 		output_packet_len = 0;
200 		return;
201 	}
202 
203 	if (!eth_is_active(eth)) {
204 		if (eth_is_on_demand_init()) {
205 			if (eth_init() < 0)
206 				return;
207 			eth_set_last_protocol(NETCONS);
208 		} else {
209 			eth_init_state_only();
210 		}
211 
212 		inited = 1;
213 	}
214 	pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
215 	memcpy(pkt, buf, len);
216 	ether = nc_ether;
217 	ip = nc_ip;
218 	net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len);
219 
220 	if (inited) {
221 		if (eth_is_on_demand_init())
222 			eth_halt();
223 		else
224 			eth_halt_state_only();
225 	}
226 }
227 
nc_stdio_start(struct stdio_dev * dev)228 static int nc_stdio_start(struct stdio_dev *dev)
229 {
230 	int retval;
231 
232 	nc_out_port = 6666; /* default port */
233 	nc_in_port = nc_out_port;
234 
235 	retval = refresh_settings_from_env();
236 	if (retval != 0)
237 		return retval;
238 
239 	/*
240 	 * Initialize the static IP settings and buffer pointers
241 	 * incase we call net_send_udp_packet before net_loop
242 	 */
243 	net_init();
244 
245 	return 0;
246 }
247 
nc_stdio_putc(struct stdio_dev * dev,char c)248 static void nc_stdio_putc(struct stdio_dev *dev, char c)
249 {
250 	if (output_recursion)
251 		return;
252 	output_recursion = 1;
253 
254 	nc_send_packet(&c, 1);
255 
256 	output_recursion = 0;
257 }
258 
nc_stdio_puts(struct stdio_dev * dev,const char * s)259 static void nc_stdio_puts(struct stdio_dev *dev, const char *s)
260 {
261 	int len;
262 
263 	if (output_recursion)
264 		return;
265 	output_recursion = 1;
266 
267 	len = strlen(s);
268 	while (len) {
269 		int send_len = min(len, (int)sizeof(input_buffer));
270 		nc_send_packet(s, send_len);
271 		len -= send_len;
272 		s += send_len;
273 	}
274 
275 	output_recursion = 0;
276 }
277 
nc_stdio_getc(struct stdio_dev * dev)278 static int nc_stdio_getc(struct stdio_dev *dev)
279 {
280 	uchar c;
281 
282 	input_recursion = 1;
283 
284 	net_timeout = 0;	/* no timeout */
285 	while (!input_size)
286 		net_loop(NETCONS);
287 
288 	input_recursion = 0;
289 
290 	c = input_buffer[input_offset++];
291 
292 	if (input_offset >= sizeof(input_buffer))
293 		input_offset -= sizeof(input_buffer);
294 	input_size--;
295 
296 	return c;
297 }
298 
nc_stdio_tstc(struct stdio_dev * dev)299 static int nc_stdio_tstc(struct stdio_dev *dev)
300 {
301 #ifdef CONFIG_DM_ETH
302 	struct udevice *eth;
303 #else
304 	struct eth_device *eth;
305 #endif
306 
307 	if (input_recursion)
308 		return 0;
309 
310 	if (input_size)
311 		return 1;
312 
313 	eth = eth_get_dev();
314 	if (eth_is_active(eth))
315 		return 0;	/* inside net loop */
316 
317 	input_recursion = 1;
318 
319 	net_timeout = 1;
320 	net_loop(NETCONS);	/* kind of poll */
321 
322 	input_recursion = 0;
323 
324 	return input_size != 0;
325 }
326 
drv_nc_init(void)327 int drv_nc_init(void)
328 {
329 	struct stdio_dev dev;
330 	int rc;
331 
332 	memset(&dev, 0, sizeof(dev));
333 
334 	strcpy(dev.name, "nc");
335 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
336 	dev.start = nc_stdio_start;
337 	dev.putc = nc_stdio_putc;
338 	dev.puts = nc_stdio_puts;
339 	dev.getc = nc_stdio_getc;
340 	dev.tstc = nc_stdio_tstc;
341 
342 	rc = stdio_register(&dev);
343 
344 	return (rc == 0) ? 1 : rc;
345 }
346