1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *	LiMon Monitor (LiMon) - Network.
4  *
5  *	Copyright 1994 - 2000 Neil Russell.
6  *
7  * History
8  *	9/16/00	  bor  adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
9  */
10 
11 #ifndef __NET_LEGACY_H__
12 #define __NET_LEGACY_H__
13 
14 #include <linux/types.h>
15 #include <asm/byteorder.h>	/* for nton* / ntoh* stuff */
16 #include <log.h>
17 #include <time.h>
18 #include <linux/if_ether.h>
19 #include <linux/string.h>
20 
21 struct bd_info;
22 struct cmd_tbl;
23 struct udevice;
24 
25 #define DEBUG_LL_STATE 0	/* Link local state machine changes */
26 #define DEBUG_DEV_PKT 0		/* Packets or info directed to the device */
27 #define DEBUG_NET_PKT 0		/* Packets on info on the network at large */
28 #define DEBUG_INT_STATE 0	/* Internal network state changes */
29 
30 /* ARP hardware address length */
31 #define ARP_HLEN 6
32 /*
33  * The size of a MAC address in string form, each digit requires two chars
34  * and five separator characters to form '00:00:00:00:00:00'.
35  */
36 #define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1)
37 
38 /**
39  * An incoming packet handler.
40  * @param pkt    pointer to the application packet
41  * @param dport  destination UDP port
42  * @param sip    source IP address
43  * @param sport  source UDP port
44  * @param len    packet length
45  */
46 typedef void rxhand_f(uchar *pkt, unsigned int dport,
47 		      struct in_addr sip, unsigned int sport,
48 		      unsigned int len);
49 
50 /**
51  * An incoming ICMP packet handler.
52  * @param type	ICMP type
53  * @param code	ICMP code
54  * @param dport	destination UDP port
55  * @param sip	source IP address
56  * @param sport	source UDP port
57  * @param pkt	pointer to the ICMP packet data
58  * @param len	packet length
59  */
60 typedef void rxhand_icmp_f(unsigned type, unsigned int code, unsigned int dport,
61 			   struct in_addr sip, unsigned int sport, uchar *pkt,
62 			   unsigned int len);
63 
64 /*
65  *	A timeout handler.  Called after time interval has expired.
66  */
67 typedef void	thand_f(void);
68 
69 /*
70  * The devname can be either an exact name given by the driver or device tree
71  * or it can be an alias of the form "eth%d"
72  */
73 struct udevice *eth_get_dev_by_name(const char *devname);
74 int eth_init_state_only(void); /* Set active state */
75 void eth_halt_state_only(void); /* Set passive state */
76 
77 /**
78  * eth_env_set_enetaddr_by_index() - set the MAC address environment variable
79  *
80  * This sets up an environment variable with the given MAC address (@enetaddr).
81  * The environment variable to be set is defined by <@base_name><@index>addr.
82  * If @index is 0 it is omitted. For common Ethernet this means ethaddr,
83  * eth1addr, etc.
84  *
85  * @base_name:  Base name for variable, typically "eth"
86  * @index:      Index of interface being updated (>=0)
87  * @enetaddr:   Pointer to MAC address to put into the variable
88  * Return: 0 if OK, other value on error
89  */
90 int eth_env_set_enetaddr_by_index(const char *base_name, int index,
91 				  uchar *enetaddr);
92 
93 /*
94  * Get the hardware address for an ethernet interface .
95  * Args:
96  *	base_name - base name for device (normally "eth")
97  *	index - device index number (0 for first)
98  *	enetaddr - returns 6 byte hardware address
99  * Returns:
100  *	Return true if the address is valid.
101  */
102 int eth_env_get_enetaddr_by_index(const char *base_name, int index,
103 				  uchar *enetaddr);
104 
105 int eth_send(void *packet, int length);	   /* Send a packet */
106 
107 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
108 int eth_receive(void *packet, int length); /* Receive a packet*/
109 extern void (*push_packet)(void *packet, int length);
110 #endif
111 int eth_mcast_join(struct in_addr mcast_addr, int join);
112 
113 /**********************************************************************/
114 /*
115  *	Protocol headers.
116  */
117 
118 #define ETH_FCS_LEN	4		/* Octets in the FCS		*/
119 
120 struct e802_hdr {
121 	u8		et_dest[ARP_HLEN];	/* Destination node	*/
122 	u8		et_src[ARP_HLEN];	/* Source node		*/
123 	u16		et_protlen;		/* Protocol or length	*/
124 	u8		et_dsap;		/* 802 DSAP		*/
125 	u8		et_ssap;		/* 802 SSAP		*/
126 	u8		et_ctl;			/* 802 control		*/
127 	u8		et_snap1;		/* SNAP			*/
128 	u8		et_snap2;
129 	u8		et_snap3;
130 	u16		et_prot;		/* 802 protocol		*/
131 } __packed;
132 
133 /* 802 + SNAP + ethernet header size */
134 #define E802_HDR_SIZE	(sizeof(struct e802_hdr))
135 
136 /*
137  *	Virtual LAN Ethernet header
138  */
139 struct vlan_ethernet_hdr {
140 	u8		vet_dest[ARP_HLEN];	/* Destination node	*/
141 	u8		vet_src[ARP_HLEN];	/* Source node		*/
142 	u16		vet_vlan_type;		/* PROT_VLAN		*/
143 	u16		vet_tag;		/* TAG of VLAN		*/
144 	u16		vet_type;		/* protocol type	*/
145 } __packed;
146 
147 /* VLAN Ethernet header size */
148 #define VLAN_ETHER_HDR_SIZE	(sizeof(struct vlan_ethernet_hdr))
149 
150 /*
151  *	Internet Protocol (IP) header.
152  */
153 struct ip_hdr {
154 	u8		ip_hl_v;	/* header length and version	*/
155 	u8		ip_tos;		/* type of service		*/
156 	u16		ip_len;		/* total length			*/
157 	u16		ip_id;		/* identification		*/
158 	u16		ip_off;		/* fragment offset field	*/
159 	u8		ip_ttl;		/* time to live			*/
160 	u8		ip_p;		/* protocol			*/
161 	u16		ip_sum;		/* checksum			*/
162 	struct in_addr	ip_src;		/* Source IP address		*/
163 	struct in_addr	ip_dst;		/* Destination IP address	*/
164 } __packed;
165 
166 #define IP_OFFS		0x1fff /* ip offset *= 8 */
167 #define IP_FLAGS	0xe000 /* first 3 bits */
168 #define IP_FLAGS_RES	0x8000 /* reserved */
169 #define IP_FLAGS_DFRAG	0x4000 /* don't fragments */
170 #define IP_FLAGS_MFRAG	0x2000 /* more fragments */
171 
172 #define IP_HDR_SIZE		(sizeof(struct ip_hdr))
173 
174 #define IP_MIN_FRAG_DATAGRAM_SIZE	(IP_HDR_SIZE + 8)
175 
176 /*
177  *	Address Resolution Protocol (ARP) header.
178  */
179 struct arp_hdr {
180 	u16		ar_hrd;		/* Format of hardware address	*/
181 #   define ARP_ETHER	    1		/* Ethernet  hardware address	*/
182 	u16		ar_pro;		/* Format of protocol address	*/
183 	u8		ar_hln;		/* Length of hardware address	*/
184 	u8		ar_pln;		/* Length of protocol address	*/
185 #   define ARP_PLEN	4
186 	u16		ar_op;		/* Operation			*/
187 #   define ARPOP_REQUEST    1		/* Request  to resolve  address	*/
188 #   define ARPOP_REPLY	    2		/* Response to previous request	*/
189 
190 #   define RARPOP_REQUEST   3		/* Request  to resolve  address	*/
191 #   define RARPOP_REPLY	    4		/* Response to previous request */
192 
193 	/*
194 	 * The remaining fields are variable in size, according to
195 	 * the sizes above, and are defined as appropriate for
196 	 * specific hardware/protocol combinations.
197 	 */
198 	u8		ar_data[0];
199 #define ar_sha		ar_data[0]
200 #define ar_spa		ar_data[ARP_HLEN]
201 #define ar_tha		ar_data[ARP_HLEN + ARP_PLEN]
202 #define ar_tpa		ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
203 #if 0
204 	u8		ar_sha[];	/* Sender hardware address	*/
205 	u8		ar_spa[];	/* Sender protocol address	*/
206 	u8		ar_tha[];	/* Target hardware address	*/
207 	u8		ar_tpa[];	/* Target protocol address	*/
208 #endif /* 0 */
209 } __packed;
210 
211 /*
212  * ICMP stuff (just enough to handle (host) redirect messages)
213  */
214 #define ICMP_ECHO_REPLY		0	/* Echo reply			*/
215 #define ICMP_NOT_REACH		3	/* Detination unreachable	*/
216 #define ICMP_REDIRECT		5	/* Redirect (change route)	*/
217 #define ICMP_ECHO_REQUEST	8	/* Echo request			*/
218 
219 /* Codes for REDIRECT. */
220 #define ICMP_REDIR_NET		0	/* Redirect Net			*/
221 #define ICMP_REDIR_HOST		1	/* Redirect Host		*/
222 
223 /* Codes for NOT_REACH */
224 #define ICMP_NOT_REACH_PORT	3	/* Port unreachable		*/
225 
226 struct icmp_hdr {
227 	u8		type;
228 	u8		code;
229 	u16		checksum;
230 	union {
231 		struct {
232 			u16	id;
233 			u16	sequence;
234 		} echo;
235 		u32	gateway;
236 		struct {
237 			u16	unused;
238 			u16	mtu;
239 		} frag;
240 		u8 data[0];
241 	} un;
242 } __packed;
243 
244 #define ICMP_HDR_SIZE		(sizeof(struct icmp_hdr))
245 #define IP_ICMP_HDR_SIZE	(IP_HDR_SIZE + ICMP_HDR_SIZE)
246 
247 /*
248  * Maximum packet size; used to allocate packet storage. Use
249  * the maximum Ethernet frame size as specified by the Ethernet
250  * standard including the 802.1Q tag (VLAN tagging).
251  * maximum packet size =  1522
252  * maximum packet size and multiple of 32 bytes =  1536
253  */
254 #define PKTSIZE			1522
255 #ifndef CONFIG_DM_DSA
256 #define PKTSIZE_ALIGN		1536
257 #else
258 /* Maximum DSA tagging overhead (headroom and/or tailroom) */
259 #define DSA_MAX_OVR		256
260 #define PKTSIZE_ALIGN		(1536 + DSA_MAX_OVR)
261 #endif
262 
263 /**********************************************************************/
264 /*
265  *	Globals.
266  *
267  * Note:
268  *
269  * All variables of type struct in_addr are stored in NETWORK byte order
270  * (big endian).
271  */
272 
273 /* net.c */
274 /** BOOTP EXTENTIONS **/
275 extern struct in_addr net_gateway;	/* Our gateway IP address */
276 extern struct in_addr net_netmask;	/* Our subnet mask (0 = unknown) */
277 /* Our Domain Name Server (0 = unknown) */
278 extern struct in_addr net_dns_server;
279 #if defined(CONFIG_BOOTP_DNS2)
280 /* Our 2nd Domain Name Server (0 = unknown) */
281 extern struct in_addr net_dns_server2;
282 #endif
283 extern char	net_nis_domain[32];	/* Our IS domain */
284 extern char	net_hostname[32];	/* Our hostname */
285 #ifdef CONFIG_NET
286 extern char	net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN];	/* Our root path */
287 #endif
288 /** END OF BOOTP EXTENTIONS **/
289 extern u8		net_ethaddr[ARP_HLEN];		/* Our ethernet address */
290 extern u8		net_server_ethaddr[ARP_HLEN];	/* Boot server enet address */
291 extern struct in_addr	net_server_ip;	/* Server IP addr (0 = unknown) */
292 extern uchar		*net_tx_packet;		/* THE transmit packet */
293 extern uchar		*net_rx_packets[PKTBUFSRX]; /* Receive packets */
294 extern uchar		*net_rx_packet;		/* Current receive packet */
295 extern int		net_rx_packet_len;	/* Current rx packet length */
296 extern const u8		net_null_ethaddr[ARP_HLEN];
297 
298 #define VLAN_NONE	4095			/* untagged */
299 #define VLAN_IDMASK	0x0fff			/* mask of valid vlan id */
300 extern ushort		net_our_vlan;		/* Our VLAN */
301 extern ushort		net_native_vlan;	/* Our Native VLAN */
302 
303 extern int		net_restart_wrap;	/* Tried all network devices */
304 
305 enum proto_t {
306 	BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
307 	NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT_UDP, FASTBOOT_TCP,
308 	WOL, UDP, NCSI, WGET, RS
309 };
310 
311 /* Indicates whether the file name was specified on the command line */
312 extern bool	net_boot_file_name_explicit;
313 /* The actual transferred size of the bootfile (in bytes) */
314 extern u32	net_boot_file_size;
315 /* Boot file size in blocks as reported by the DHCP server */
316 extern u32	net_boot_file_expected_size_in_blocks;
317 
318 #if defined(CONFIG_DNS)
319 extern char *net_dns_resolve;		/* The host to resolve  */
320 extern char *net_dns_env_var;		/* the env var to put the ip into */
321 #endif
322 
323 #if defined(CONFIG_CMD_PING)
324 extern struct in_addr net_ping_ip;	/* the ip address to ping */
325 #endif
326 
327 #if defined(CONFIG_CMD_CDP)
328 /* when CDP completes these hold the return values */
329 extern ushort cdp_native_vlan;		/* CDP returned native VLAN */
330 extern ushort cdp_appliance_vlan;	/* CDP returned appliance VLAN */
331 
332 /*
333  * Check for a CDP packet by examining the received MAC address field
334  */
is_cdp_packet(const uchar * ethaddr)335 static inline int is_cdp_packet(const uchar *ethaddr)
336 {
337 	extern const u8 net_cdp_ethaddr[ARP_HLEN];
338 
339 	return memcmp(ethaddr, net_cdp_ethaddr, ARP_HLEN) == 0;
340 }
341 #endif
342 
343 #if defined(CONFIG_CMD_SNTP)
344 extern struct in_addr	net_ntp_server;		/* the ip address to NTP */
345 extern int net_ntp_time_offset;			/* offset time from UTC */
346 #endif
347 
348 int net_loop(enum proto_t);
349 
350 /* Get size of the ethernet header when we send */
351 int net_eth_hdr_size(void);
352 
353 /* Set ethernet header; returns the size of the header */
354 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot);
355 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
356 
357 /* Set IP header */
358 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
359 		       u16 pkt_len, u8 proto);
360 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport,
361 			int sport, int len);
362 
363 /* Callbacks */
364 rxhand_f *net_get_udp_handler(void);	/* Get UDP RX packet handler */
365 void net_set_udp_handler(rxhand_f *f);	/* Set UDP RX packet handler */
366 rxhand_f *net_get_arp_handler(void);	/* Get ARP RX packet handler */
367 void net_set_arp_handler(rxhand_f *f);	/* Set ARP RX packet handler */
368 bool arp_is_waiting(void);		/* Waiting for ARP reply? */
369 void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
370 void net_set_timeout_handler(ulong t, thand_f *f);/* Set timeout handler */
371 
372 /* Network loop state */
373 enum net_loop_state {
374 	NETLOOP_CONTINUE,
375 	NETLOOP_RESTART,
376 	NETLOOP_SUCCESS,
377 	NETLOOP_FAIL
378 };
379 
380 extern enum net_loop_state net_state;
381 
net_set_state(enum net_loop_state state)382 static inline void net_set_state(enum net_loop_state state)
383 {
384 	debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
385 	net_state = state;
386 }
387 
388 /*
389  * net_get_async_tx_pkt_buf - Get a packet buffer that is not in use for
390  *			      sending an asynchronous reply
391  *
392  * returns - ptr to packet buffer
393  */
394 uchar * net_get_async_tx_pkt_buf(void);
395 
396 /**
397  * net_send_ip_packet() - Transmit "net_tx_packet" as UDP or TCP packet,
398  *                        send ARP request if needed (ether will be populated)
399  * @ether: Raw packet buffer
400  * @dest: IP address to send the datagram to
401  * @dport: Destination UDP port
402  * @sport: Source UDP port
403  * @payload_len: Length of data after the UDP header
404  * @action: TCP action to be performed
405  * @tcp_seq_num: TCP sequence number of this transmission
406  * @tcp_ack_num: TCP stream acknolegement number
407  *
408  * Return: 0 on success, other value on failure
409  */
410 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
411 		       int payload_len, int proto, u8 action, u32 tcp_seq_num,
412 		       u32 tcp_ack_num);
413 /**
414  * net_send_tcp_packet() - Transmit TCP packet.
415  * @payload_len: length of payload
416  * @dhost: Destination host
417  * @dport: Destination TCP port
418  * @sport: Source TCP port
419  * @action: TCP action to be performed
420  * @tcp_seq_num: TCP sequence number of this transmission
421  * @tcp_ack_num: TCP stream acknolegement number
422  *
423  * Return: 0 on success, other value on failure
424  */
425 int net_send_tcp_packet(int payload_len, struct in_addr dhost, int dport,
426 			int sport, u8 action, u32 tcp_seq_num, u32 tcp_ack_num);
427 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport,
428 			int sport, int payload_len);
429 
430 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
431 void nc_start(void);
432 int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned int dest_port,
433 		    unsigned int src_port, unsigned int len);
434 #endif
435 
eth_is_on_demand_init(void)436 static __always_inline int eth_is_on_demand_init(void)
437 {
438 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
439 	extern enum proto_t net_loop_last_protocol;
440 
441 	return net_loop_last_protocol != NETCONS;
442 #else
443 	return 1;
444 #endif
445 }
446 
eth_set_last_protocol(int protocol)447 static inline void eth_set_last_protocol(int protocol)
448 {
449 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
450 	extern enum proto_t net_loop_last_protocol;
451 
452 	net_loop_last_protocol = protocol;
453 #endif
454 }
455 
456 /*
457  * Check if autoload is enabled. If so, use either NFS or TFTP to download
458  * the boot file.
459  */
460 void net_auto_load(void);
461 
462 /*
463  * The following functions are a bit ugly, but necessary to deal with
464  * alignment restrictions on ARM.
465  *
466  * We're using inline functions, which had the smallest memory
467  * footprint in our tests.
468  */
469 /* return IP *in network byteorder* */
net_read_ip(void * from)470 static inline struct in_addr net_read_ip(void *from)
471 {
472 	struct in_addr ip;
473 
474 	memcpy((void *)&ip, (void *)from, sizeof(ip));
475 	return ip;
476 }
477 
478 /* return ulong *in network byteorder* */
net_read_u32(void * from)479 static inline u32 net_read_u32(void *from)
480 {
481 	u32 l;
482 
483 	memcpy((void *)&l, (void *)from, sizeof(l));
484 	return l;
485 }
486 
487 /* write IP *in network byteorder* */
net_write_ip(void * to,struct in_addr ip)488 static inline void net_write_ip(void *to, struct in_addr ip)
489 {
490 	memcpy(to, (void *)&ip, sizeof(ip));
491 }
492 
493 /* copy IP */
net_copy_ip(void * to,void * from)494 static inline void net_copy_ip(void *to, void *from)
495 {
496 	memcpy((void *)to, from, sizeof(struct in_addr));
497 }
498 
499 /* copy ulong */
net_copy_u32(void * to,void * from)500 static inline void net_copy_u32(void *to, void *from)
501 {
502 	memcpy((void *)to, (void *)from, sizeof(u32));
503 }
504 
505 /* Convert an IP address to a string */
506 void ip_to_string(struct in_addr x, char *s);
507 
508 /**
509  * string_to_ip() - Convert a string to ip address
510  *
511  * Implemented in lib/net_utils.c (built unconditionally)
512  *
513  * @s: Input string to parse
514  * @return: in_addr struct containing the parsed IP address
515  */
516 struct in_addr string_to_ip(const char *s);
517 
518 /* Convert a VLAN id to a string */
519 void vlan_to_string(ushort x, char *s);
520 
521 /* Convert a string to a vlan id */
522 ushort string_to_vlan(const char *s);
523 
524 /* read a VLAN id from an environment variable */
525 ushort env_get_vlan(char *var);
526 
527 /* check if serverip is specified in filename from the command line */
528 int is_serverip_in_cmd(void);
529 
530 /**
531  * net_parse_bootfile - Parse the bootfile env var / cmd line param
532  *
533  * @param ipaddr - a pointer to the ipaddr to populate if included in bootfile
534  * @param filename - a pointer to the string to save the filename part
535  * @param max_len - The longest - 1 that the filename part can be
536  *
537  * return 1 if parsed, 0 if bootfile is empty
538  */
539 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len);
540 
541 #endif /* __NET_LEGACY_H__ */
542