xref: /openbsd-src/sys/arch/i386/stand/libsa/pxe_net.c (revision 3e58d19e89b8645264ea84c2f797d77942ef298e)
1*3e58d19eSkrw /*	$OpenBSD: pxe_net.c,v 1.7 2020/12/09 18:10:18 krw Exp $	*/
2ef7aef7bStom /*	$NetBSD: dev_net.c,v 1.4 2003/03/12 13:15:08 drochner Exp $	*/
3ef7aef7bStom 
4ef7aef7bStom /*-
5ef7aef7bStom  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6ef7aef7bStom  * All rights reserved.
7ef7aef7bStom  *
8ef7aef7bStom  * This code is derived from software contributed to The NetBSD Foundation
9ef7aef7bStom  * by Gordon W. Ross.
10ef7aef7bStom  *
11ef7aef7bStom  * Redistribution and use in source and binary forms, with or without
12ef7aef7bStom  * modification, are permitted provided that the following conditions
13ef7aef7bStom  * are met:
14ef7aef7bStom  * 1. Redistributions of source code must retain the above copyright
15ef7aef7bStom  *    notice, this list of conditions and the following disclaimer.
16ef7aef7bStom  * 2. Redistributions in binary form must reproduce the above copyright
17ef7aef7bStom  *    notice, this list of conditions and the following disclaimer in the
18ef7aef7bStom  *    documentation and/or other materials provided with the distribution.
19ef7aef7bStom  *
20ef7aef7bStom  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ef7aef7bStom  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ef7aef7bStom  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ef7aef7bStom  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ef7aef7bStom  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ef7aef7bStom  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ef7aef7bStom  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ef7aef7bStom  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ef7aef7bStom  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ef7aef7bStom  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ef7aef7bStom  * POSSIBILITY OF SUCH DAMAGE.
31ef7aef7bStom  */
32ef7aef7bStom 
33ef7aef7bStom /*
34ef7aef7bStom  * This module implements a "raw device" interface suitable for
35ef7aef7bStom  * use by the stand-alone I/O library NFS and TFTP code.  This interface
36ef7aef7bStom  * does not support any "block" access, and exists only for the
37ef7aef7bStom  * purpose of initializing the network interface and getting boot
38ef7aef7bStom  * parameters.
39ef7aef7bStom  *
40ef7aef7bStom  * At open time, this does:
41ef7aef7bStom  *
42ef7aef7bStom  * find interface       - netif_open()
43ef7aef7bStom  * BOOTP for IP address - bootp()
44ef7aef7bStom  */
45ef7aef7bStom 
46ef7aef7bStom #include <sys/param.h>
47ef7aef7bStom #include <sys/socket.h>
48ef7aef7bStom #include <net/if.h>
49ef7aef7bStom #include <netinet/in.h>
50ef7aef7bStom 
51ef7aef7bStom #include <lib/libkern/libkern.h>
52ef7aef7bStom 
53ef7aef7bStom #include <lib/libsa/stand.h>
54ef7aef7bStom #include <lib/libsa/net.h>
55ef7aef7bStom #include "pxe_netif.h"
56ef7aef7bStom #include "pxe_net.h"
57ef7aef7bStom 
58ef7aef7bStom static int netdev_sock = -1;
59ef7aef7bStom static int netdev_opens;
60ef7aef7bStom 
61ef7aef7bStom int net_getparams(int);
62ef7aef7bStom 
63ef7aef7bStom /*
64ef7aef7bStom  * Called by devopen after it sets f->f_dev to our devsw entry.
65ef7aef7bStom  * This opens the low-level device and sets f->f_devdata.
66ef7aef7bStom  * This is declared with variable arguments...
67ef7aef7bStom  */
68ef7aef7bStom int
net_open(struct open_file * f,...)69ef7aef7bStom net_open(struct open_file *f, ...)
70ef7aef7bStom {
71ef7aef7bStom 	int error = 0;
72ef7aef7bStom 
73ef7aef7bStom #ifdef	NETIF_DEBUG
74ef7aef7bStom 	if (debug)
75ef7aef7bStom 		printf("net_open()\n");
76ef7aef7bStom #endif
77ef7aef7bStom 
78ef7aef7bStom 	/* On first open, do netif open, mount, etc. */
79ef7aef7bStom 	if (netdev_opens == 0) {
80ef7aef7bStom 		/* Find network interface. */
81ef7aef7bStom 		if (netdev_sock < 0) {
82ef7aef7bStom 			netdev_sock = pxe_netif_open();
83ef7aef7bStom 			if (netdev_sock < 0) {
84ef7aef7bStom 				printf("net_open: netif_open() failed\n");
85ef7aef7bStom 				return ENXIO;
86ef7aef7bStom 			}
87ef7aef7bStom #ifdef	NETIF_DEBUG
88ef7aef7bStom 			if (debug)
89ef7aef7bStom 				printf("net_open: netif_open() succeeded\n");
90ef7aef7bStom #endif
91ef7aef7bStom 		}
92ef7aef7bStom #ifdef notyet
93ef7aef7bStom 		if (rootip.s_addr == 0) {
94ef7aef7bStom 			/* Get root IP address, and path, etc. */
95ef7aef7bStom 			error = net_getparams(netdev_sock);
96ef7aef7bStom 			if (error) {
97ef7aef7bStom 				/* getparams makes its own noise */
98ef7aef7bStom 				pxe_netif_close(netdev_sock);
99ef7aef7bStom 				netdev_sock = -1;
100ef7aef7bStom 				return error;
101ef7aef7bStom 			}
102ef7aef7bStom 		}
103ef7aef7bStom #endif
104ef7aef7bStom 	}
105ef7aef7bStom 	netdev_opens++;
106ef7aef7bStom 	f->f_devdata = &netdev_sock;
107ef7aef7bStom 	return error;
108ef7aef7bStom }
109ef7aef7bStom 
110ef7aef7bStom int
net_close(struct open_file * f)111ef7aef7bStom net_close(struct open_file *f)
112ef7aef7bStom {
113ef7aef7bStom #ifdef	NETIF_DEBUG
114ef7aef7bStom 	if (debug)
115ef7aef7bStom 		printf("net_close: opens=%d\n", netdev_opens);
116ef7aef7bStom #endif
117ef7aef7bStom 
118ef7aef7bStom 	/* On last close, do netif close, etc. */
119ef7aef7bStom 	f->f_devdata = NULL;
120ef7aef7bStom 	/* Extra close call? */
121ef7aef7bStom 	if (netdev_opens <= 0)
122ef7aef7bStom 		return 0;
123ef7aef7bStom 	netdev_opens--;
124ef7aef7bStom 	/* Not last close? */
125ef7aef7bStom 	if (netdev_opens > 0)
126ef7aef7bStom 		return 0;
127ef7aef7bStom 	rootip.s_addr = 0;
128ef7aef7bStom 	if (netdev_sock >= 0) {
129ef7aef7bStom #ifdef	NETIF_DEBUG
130ef7aef7bStom 		if (debug)
131ef7aef7bStom 			printf("net_close: calling netif_close()\n");
132ef7aef7bStom #endif
133ef7aef7bStom 		pxe_netif_close(netdev_sock);
134ef7aef7bStom 		netdev_sock = -1;
135ef7aef7bStom 	}
136ef7aef7bStom 	return 0;
137ef7aef7bStom }
138ef7aef7bStom 
139ef7aef7bStom int
net_ioctl(struct open_file * f,u_long cmd,void * data)140ef7aef7bStom net_ioctl(struct open_file *f, u_long cmd, void *data)
141ef7aef7bStom {
142ef7aef7bStom 	return EIO;
143ef7aef7bStom }
144ef7aef7bStom 
145ef7aef7bStom int
net_strategy(void * devdata,int rw,daddr_t blk,size_t size,void * buf,size_t * rsize)146*3e58d19eSkrw net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
147ef7aef7bStom     size_t *rsize)
148ef7aef7bStom {
149ef7aef7bStom 	return EIO;
150ef7aef7bStom }
151ef7aef7bStom 
152ef7aef7bStom 
153ef7aef7bStom /*
154ef7aef7bStom  * Get info for network boot: our IP address, our hostname,
155ef7aef7bStom  * server IP address, and our root path on the server.
156ef7aef7bStom  */
157ef7aef7bStom extern int bootp(int);
158ef7aef7bStom 
159ef7aef7bStom int
net_getparams(int sock)160ef7aef7bStom net_getparams(int sock)
161ef7aef7bStom {
162ef7aef7bStom 	/*
163ef7aef7bStom 	 * Try to get boot info using BOOTP.  If we succeed, then
164ef7aef7bStom 	 * the server IP address, gateway, and root path will all
165ef7aef7bStom 	 * be initialized.  If any remain uninitialized, we will
166ef7aef7bStom 	 * use RARP and RPC/bootparam (the Sun way) to get them.
167ef7aef7bStom 	 */
168ef7aef7bStom 	bootp(sock);
169ef7aef7bStom 	if (myip.s_addr != 0)
170ef7aef7bStom 		return 0;
171ee2d84a2Spatrick #ifdef	NETIF_DEBUG
172ef7aef7bStom 	if (debug)
173ef7aef7bStom 		printf("net_getparams: BOOTP failed\n");
174ee2d84a2Spatrick #endif
175ef7aef7bStom 
176ef7aef7bStom 	return EIO;
177ef7aef7bStom }
178