1*3e58d19eSkrw /* $OpenBSD: pxe_net.c,v 1.5 2020/12/09 18:10:18 krw Exp $ */
28641b11fStom /* $NetBSD: dev_net.c,v 1.4 2003/03/12 13:15:08 drochner Exp $ */
38641b11fStom
48641b11fStom /*-
58641b11fStom * Copyright (c) 1997 The NetBSD Foundation, Inc.
68641b11fStom * All rights reserved.
78641b11fStom *
88641b11fStom * This code is derived from software contributed to The NetBSD Foundation
98641b11fStom * by Gordon W. Ross.
108641b11fStom *
118641b11fStom * Redistribution and use in source and binary forms, with or without
128641b11fStom * modification, are permitted provided that the following conditions
138641b11fStom * are met:
148641b11fStom * 1. Redistributions of source code must retain the above copyright
158641b11fStom * notice, this list of conditions and the following disclaimer.
168641b11fStom * 2. Redistributions in binary form must reproduce the above copyright
178641b11fStom * notice, this list of conditions and the following disclaimer in the
188641b11fStom * documentation and/or other materials provided with the distribution.
198641b11fStom *
208641b11fStom * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
218641b11fStom * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
228641b11fStom * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
238641b11fStom * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
248641b11fStom * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
258641b11fStom * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
268641b11fStom * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
278641b11fStom * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
288641b11fStom * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
298641b11fStom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
308641b11fStom * POSSIBILITY OF SUCH DAMAGE.
318641b11fStom */
328641b11fStom
338641b11fStom /*
348641b11fStom * This module implements a "raw device" interface suitable for
358641b11fStom * use by the stand-alone I/O library NFS and TFTP code. This interface
368641b11fStom * does not support any "block" access, and exists only for the
378641b11fStom * purpose of initializing the network interface and getting boot
388641b11fStom * parameters.
398641b11fStom *
408641b11fStom * At open time, this does:
418641b11fStom *
428641b11fStom * find interface - netif_open()
438641b11fStom * BOOTP for IP address - bootp()
448641b11fStom */
458641b11fStom
468641b11fStom #include <sys/param.h>
478641b11fStom #include <sys/socket.h>
488641b11fStom #include <net/if.h>
498641b11fStom #include <netinet/in.h>
508641b11fStom
518641b11fStom #include <lib/libkern/libkern.h>
528641b11fStom
538641b11fStom #include <lib/libsa/stand.h>
548641b11fStom #include <lib/libsa/net.h>
558641b11fStom #include "pxe_netif.h"
568641b11fStom #include "pxe_net.h"
578641b11fStom
588641b11fStom static int netdev_sock = -1;
598641b11fStom static int netdev_opens;
608641b11fStom
618641b11fStom int net_getparams(int);
628641b11fStom
638641b11fStom /*
648641b11fStom * Called by devopen after it sets f->f_dev to our devsw entry.
658641b11fStom * This opens the low-level device and sets f->f_devdata.
668641b11fStom * This is declared with variable arguments...
678641b11fStom */
688641b11fStom int
net_open(struct open_file * f,...)698641b11fStom net_open(struct open_file *f, ...)
708641b11fStom {
718641b11fStom int error = 0;
728641b11fStom
738641b11fStom #ifdef NETIF_DEBUG
748641b11fStom if (debug)
758641b11fStom printf("net_open()\n");
768641b11fStom #endif
778641b11fStom
788641b11fStom /* On first open, do netif open, mount, etc. */
798641b11fStom if (netdev_opens == 0) {
808641b11fStom /* Find network interface. */
818641b11fStom if (netdev_sock < 0) {
828641b11fStom netdev_sock = pxe_netif_open();
838641b11fStom if (netdev_sock < 0) {
848641b11fStom printf("net_open: netif_open() failed\n");
858641b11fStom return ENXIO;
868641b11fStom }
878641b11fStom #ifdef NETIF_DEBUG
888641b11fStom if (debug)
898641b11fStom printf("net_open: netif_open() succeeded\n");
908641b11fStom #endif
918641b11fStom }
928641b11fStom #ifdef notyet
938641b11fStom if (rootip.s_addr == 0) {
948641b11fStom /* Get root IP address, and path, etc. */
958641b11fStom error = net_getparams(netdev_sock);
968641b11fStom if (error) {
978641b11fStom /* getparams makes its own noise */
988641b11fStom pxe_netif_close(netdev_sock);
998641b11fStom netdev_sock = -1;
1008641b11fStom return error;
1018641b11fStom }
1028641b11fStom }
1038641b11fStom #endif
1048641b11fStom }
1058641b11fStom netdev_opens++;
1068641b11fStom f->f_devdata = &netdev_sock;
1078641b11fStom return error;
1088641b11fStom }
1098641b11fStom
1108641b11fStom int
net_close(struct open_file * f)1118641b11fStom net_close(struct open_file *f)
1128641b11fStom {
1138641b11fStom #ifdef NETIF_DEBUG
1148641b11fStom if (debug)
1158641b11fStom printf("net_close: opens=%d\n", netdev_opens);
1168641b11fStom #endif
1178641b11fStom
1188641b11fStom /* On last close, do netif close, etc. */
1198641b11fStom f->f_devdata = NULL;
1208641b11fStom /* Extra close call? */
1218641b11fStom if (netdev_opens <= 0)
1228641b11fStom return 0;
1238641b11fStom netdev_opens--;
1248641b11fStom /* Not last close? */
1258641b11fStom if (netdev_opens > 0)
1268641b11fStom return 0;
1278641b11fStom rootip.s_addr = 0;
1288641b11fStom if (netdev_sock >= 0) {
1298641b11fStom #ifdef NETIF_DEBUG
1308641b11fStom if (debug)
1318641b11fStom printf("net_close: calling netif_close()\n");
1328641b11fStom #endif
1338641b11fStom pxe_netif_close(netdev_sock);
1348641b11fStom netdev_sock = -1;
1358641b11fStom }
1368641b11fStom return 0;
1378641b11fStom }
1388641b11fStom
1398641b11fStom int
net_ioctl(struct open_file * f,u_long cmd,void * data)1408641b11fStom net_ioctl(struct open_file *f, u_long cmd, void *data)
1418641b11fStom {
1428641b11fStom return EIO;
1438641b11fStom }
1448641b11fStom
1458641b11fStom 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,
1478641b11fStom size_t *rsize)
1488641b11fStom {
1498641b11fStom return EIO;
1508641b11fStom }
1518641b11fStom
1528641b11fStom
1538641b11fStom /*
1548641b11fStom * Get info for network boot: our IP address, our hostname,
1558641b11fStom * server IP address, and our root path on the server.
1568641b11fStom */
1578641b11fStom extern int bootp(int);
1588641b11fStom
1598641b11fStom int
net_getparams(int sock)1608641b11fStom net_getparams(int sock)
1618641b11fStom {
1628641b11fStom /*
1638641b11fStom * Try to get boot info using BOOTP. If we succeed, then
1648641b11fStom * the server IP address, gateway, and root path will all
1658641b11fStom * be initialized. If any remain uninitialized, we will
1668641b11fStom * use RARP and RPC/bootparam (the Sun way) to get them.
1678641b11fStom */
1688641b11fStom bootp(sock);
1698641b11fStom if (myip.s_addr != 0)
1708641b11fStom return 0;
1718641b11fStom if (debug)
1728641b11fStom printf("net_getparams: BOOTP failed\n");
1738641b11fStom
1748641b11fStom return EIO;
1758641b11fStom }
176