1*17122161Sderaadt /* $OpenBSD: net.c,v 1.7 2021/01/30 14:37:01 deraadt Exp $ */
24c64bf9eSdrahn /* $NetBSD: net.c,v 1.1 1997/04/16 20:29:18 thorpej Exp $ */
34c64bf9eSdrahn
44c64bf9eSdrahn /*
54c64bf9eSdrahn * Copyright (C) 1995 Wolfgang Solfrank.
64c64bf9eSdrahn * Copyright (C) 1995 TooLs GmbH.
74c64bf9eSdrahn * All rights reserved.
84c64bf9eSdrahn *
94c64bf9eSdrahn * Redistribution and use in source and binary forms, with or without
104c64bf9eSdrahn * modification, are permitted provided that the following conditions
114c64bf9eSdrahn * are met:
124c64bf9eSdrahn * 1. Redistributions of source code must retain the above copyright
134c64bf9eSdrahn * notice, this list of conditions and the following disclaimer.
144c64bf9eSdrahn * 2. Redistributions in binary form must reproduce the above copyright
154c64bf9eSdrahn * notice, this list of conditions and the following disclaimer in the
164c64bf9eSdrahn * documentation and/or other materials provided with the distribution.
174c64bf9eSdrahn * 3. All advertising materials mentioning features or use of this software
184c64bf9eSdrahn * must display the following acknowledgement:
194c64bf9eSdrahn * This product includes software developed by TooLs GmbH.
204c64bf9eSdrahn * 4. The name of TooLs GmbH may not be used to endorse or promote products
214c64bf9eSdrahn * derived from this software without specific prior written permission.
224c64bf9eSdrahn *
234c64bf9eSdrahn * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
244c64bf9eSdrahn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
254c64bf9eSdrahn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
264c64bf9eSdrahn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
274c64bf9eSdrahn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
284c64bf9eSdrahn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
294c64bf9eSdrahn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
304c64bf9eSdrahn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
314c64bf9eSdrahn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
324c64bf9eSdrahn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
334c64bf9eSdrahn */
344c64bf9eSdrahn
354c64bf9eSdrahn /*
364c64bf9eSdrahn * This module implements a "raw device" interface suitable for
374c64bf9eSdrahn * use by the stand-alone I/O library NFS code. This interface
384c64bf9eSdrahn * does not support any "block" access, and exists only for the
394c64bf9eSdrahn * purpose of initializing the network interface, getting boot
404c64bf9eSdrahn * parameters, and performing the NFS mount.
414c64bf9eSdrahn *
424c64bf9eSdrahn * At open time, this does:
434c64bf9eSdrahn *
444c64bf9eSdrahn * find interface - netif_open()
454c64bf9eSdrahn * BOOTP - bootp()
464c64bf9eSdrahn * RPC/mountd - nfs_mount()
474c64bf9eSdrahn *
484c64bf9eSdrahn * The root file handle from mountd is saved in a global
494c64bf9eSdrahn * for use by the NFS open code (NFS/lookup).
504c64bf9eSdrahn *
514c64bf9eSdrahn * Note: this is based in part on sys/arch/sparc/stand/net.c
524c64bf9eSdrahn */
534c64bf9eSdrahn
544c64bf9eSdrahn #include <sys/param.h>
554c64bf9eSdrahn #include <sys/socket.h>
564c64bf9eSdrahn #include <net/if.h>
574c64bf9eSdrahn #include <netinet/in.h>
584c64bf9eSdrahn #include <netinet/if_ether.h>
594c64bf9eSdrahn
604c64bf9eSdrahn #include <lib/libsa/stand.h>
614c64bf9eSdrahn #include <lib/libsa/net.h>
624c64bf9eSdrahn #include <lib/libsa/netif.h>
63a3c0427bSkettenis #include <lib/libsa/bootp.h>
64a3c0427bSkettenis #include <lib/libsa/nfs.h>
65a3c0427bSkettenis
66a3c0427bSkettenis #include "ofdev.h"
67a3c0427bSkettenis
68a3c0427bSkettenis int net_mountroot(void);
694c64bf9eSdrahn
70*17122161Sderaadt extern char rootpath[FNAME_SIZE];
714c64bf9eSdrahn
724c64bf9eSdrahn static int netdev_sock = -1;
734c64bf9eSdrahn static int open_count;
744c64bf9eSdrahn
754c64bf9eSdrahn /*
764c64bf9eSdrahn * Called by devopen after it sets f->f_dev to our devsw entry.
774c64bf9eSdrahn * This opens the low-level device and sets f->f_devdata.
784c64bf9eSdrahn */
794c64bf9eSdrahn int
net_open(struct of_dev * op)8080fcf0a6Sdrahn net_open(struct of_dev *op)
814c64bf9eSdrahn {
824c64bf9eSdrahn int error = 0;
834c64bf9eSdrahn
844c64bf9eSdrahn /*
854c64bf9eSdrahn * On first open, do netif open, mount, etc.
864c64bf9eSdrahn */
874c64bf9eSdrahn if (open_count == 0) {
884c64bf9eSdrahn /* Find network interface. */
894c64bf9eSdrahn if ((netdev_sock = netif_open(op)) < 0) {
904c64bf9eSdrahn error = errno;
914c64bf9eSdrahn goto bad;
924c64bf9eSdrahn }
934c64bf9eSdrahn if ((error = net_mountroot()) != 0)
944c64bf9eSdrahn goto bad;
954c64bf9eSdrahn }
964c64bf9eSdrahn open_count++;
974c64bf9eSdrahn bad:
984c64bf9eSdrahn if (netdev_sock >= 0 && open_count == 0) {
994c64bf9eSdrahn netif_close(netdev_sock);
1004c64bf9eSdrahn netdev_sock = -1;
1014c64bf9eSdrahn }
1024c64bf9eSdrahn return error;
1034c64bf9eSdrahn }
1044c64bf9eSdrahn
105a3c0427bSkettenis void
net_close(struct of_dev * op)10680fcf0a6Sdrahn net_close(struct of_dev *op)
1074c64bf9eSdrahn {
1084c64bf9eSdrahn /*
1094c64bf9eSdrahn * On last close, do netif close, etc.
1104c64bf9eSdrahn */
1114c64bf9eSdrahn if (open_count > 0)
1124c64bf9eSdrahn if (--open_count == 0) {
1134c64bf9eSdrahn netif_close(netdev_sock);
1144c64bf9eSdrahn netdev_sock = -1;
1154c64bf9eSdrahn }
1164c64bf9eSdrahn }
1174c64bf9eSdrahn
1184c64bf9eSdrahn int
net_mountroot()1194c64bf9eSdrahn net_mountroot()
1204c64bf9eSdrahn {
1214c64bf9eSdrahn
1224c64bf9eSdrahn #ifdef DEBUG
1234c64bf9eSdrahn printf("net_mountroot\n");
1244c64bf9eSdrahn #endif
1254c64bf9eSdrahn
1264c64bf9eSdrahn /*
1274c64bf9eSdrahn * Get info for NFS boot: our IP address, out hostname,
1284c64bf9eSdrahn * server IP address, and our root path on the server.
1294c64bf9eSdrahn * We use BOOTP (RFC951, RFC1532) exclusively as mandated
1304c64bf9eSdrahn * by PowerPC Reference Platform Specification I.4.2
1314c64bf9eSdrahn */
1324c64bf9eSdrahn
1334c64bf9eSdrahn bootp(netdev_sock);
1344c64bf9eSdrahn
1354c64bf9eSdrahn if (myip.s_addr == 0)
1364c64bf9eSdrahn return ETIMEDOUT;
1374c64bf9eSdrahn
1384c64bf9eSdrahn printf("Using IP address: %s\n", inet_ntoa(myip));
1394c64bf9eSdrahn
1404c64bf9eSdrahn #ifdef DEBUG
1414c64bf9eSdrahn printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1424c64bf9eSdrahn if (gateip.s_addr)
1434c64bf9eSdrahn printf(", gateip: %s", inet_ntoa(gateip));
1444c64bf9eSdrahn if (netmask)
1454c64bf9eSdrahn printf(", netmask: %s", intoa(netmask));
1464c64bf9eSdrahn printf("\n");
1474c64bf9eSdrahn #endif
1484c64bf9eSdrahn printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
1494c64bf9eSdrahn
1504c64bf9eSdrahn /*
1514c64bf9eSdrahn * Get the NFS file handle (mount).
1524c64bf9eSdrahn */
1534c64bf9eSdrahn if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
1544c64bf9eSdrahn return errno;
1554c64bf9eSdrahn return 0;
1564c64bf9eSdrahn }
157