1*6adfa96cSmrg /* $NetBSD: net.c,v 1.5 2021/04/12 03:55:40 mrg Exp $ */
280675955Sthorpej
380675955Sthorpej /*
480675955Sthorpej * Copyright (C) 1995 Wolfgang Solfrank.
580675955Sthorpej * Copyright (C) 1995 TooLs GmbH.
680675955Sthorpej * All rights reserved.
780675955Sthorpej *
880675955Sthorpej * Redistribution and use in source and binary forms, with or without
980675955Sthorpej * modification, are permitted provided that the following conditions
1080675955Sthorpej * are met:
1180675955Sthorpej * 1. Redistributions of source code must retain the above copyright
1280675955Sthorpej * notice, this list of conditions and the following disclaimer.
1380675955Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
1480675955Sthorpej * notice, this list of conditions and the following disclaimer in the
1580675955Sthorpej * documentation and/or other materials provided with the distribution.
1680675955Sthorpej * 3. All advertising materials mentioning features or use of this software
1780675955Sthorpej * must display the following acknowledgement:
1880675955Sthorpej * This product includes software developed by TooLs GmbH.
1980675955Sthorpej * 4. The name of TooLs GmbH may not be used to endorse or promote products
2080675955Sthorpej * derived from this software without specific prior written permission.
2180675955Sthorpej *
2280675955Sthorpej * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2380675955Sthorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2480675955Sthorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2580675955Sthorpej * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2680675955Sthorpej * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2780675955Sthorpej * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2880675955Sthorpej * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2980675955Sthorpej * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3080675955Sthorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3180675955Sthorpej * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3280675955Sthorpej */
3380675955Sthorpej
3480675955Sthorpej /*
3580675955Sthorpej * This module implements a "raw device" interface suitable for
3680675955Sthorpej * use by the stand-alone I/O library NFS code. This interface
3780675955Sthorpej * does not support any "block" access, and exists only for the
3880675955Sthorpej * purpose of initializing the network interface, getting boot
3980675955Sthorpej * parameters, and performing the NFS mount.
4080675955Sthorpej *
4180675955Sthorpej * At open time, this does:
4280675955Sthorpej *
4366a5580cSdrochner * find interface - netif_of_open()
4480675955Sthorpej * BOOTP - bootp()
4580675955Sthorpej * RPC/mountd - nfs_mount()
4680675955Sthorpej *
4780675955Sthorpej * The root file handle from mountd is saved in a global
4880675955Sthorpej * for use by the NFS open code (NFS/lookup).
4980675955Sthorpej *
5080675955Sthorpej * Note: this is based in part on sys/arch/sparc/stand/net.c
5180675955Sthorpej */
5280675955Sthorpej
5380675955Sthorpej #include <sys/param.h>
5480675955Sthorpej #include <sys/socket.h>
5580675955Sthorpej
5680675955Sthorpej #include <net/if.h>
5780675955Sthorpej #include <netinet/in.h>
5880675955Sthorpej #include <netinet/in_systm.h>
5980675955Sthorpej
6080675955Sthorpej #include <lib/libsa/stand.h>
6180675955Sthorpej #include <lib/libsa/net.h>
6245879fd7Schristos #include <lib/libsa/bootp.h>
6345879fd7Schristos #include <lib/libsa/nfs.h>
6480675955Sthorpej
6580675955Sthorpej #include <lib/libkern/libkern.h>
6680675955Sthorpej
6745879fd7Schristos #include "extern.h"
6866a5580cSdrochner #include "ofdev.h"
6966a5580cSdrochner #include "netif_of.h"
7066a5580cSdrochner
7180675955Sthorpej static int netdev_sock = -1;
7280675955Sthorpej static int open_count;
7380675955Sthorpej
7480675955Sthorpej /*
7580675955Sthorpej * Called by devopen after it sets f->f_dev to our devsw entry.
7680675955Sthorpej * This opens the low-level device and sets f->f_devdata.
7780675955Sthorpej */
7880675955Sthorpej int
net_open(struct of_dev * op)7945879fd7Schristos net_open(struct of_dev *op)
8080675955Sthorpej {
8180675955Sthorpej int error = 0;
8280675955Sthorpej
8380675955Sthorpej /*
8480675955Sthorpej * On first open, do netif open, mount, etc.
8580675955Sthorpej */
8680675955Sthorpej if (open_count == 0) {
8780675955Sthorpej /* Find network interface. */
8866a5580cSdrochner if ((netdev_sock = netif_of_open(op)) < 0) {
8980675955Sthorpej error = errno;
9080675955Sthorpej goto bad;
9180675955Sthorpej }
9280675955Sthorpej if ((error = net_mountroot()) != 0)
9380675955Sthorpej goto bad;
9480675955Sthorpej }
9580675955Sthorpej open_count++;
9680675955Sthorpej bad:
9780675955Sthorpej if (netdev_sock >= 0 && open_count == 0) {
9866a5580cSdrochner netif_of_close(netdev_sock);
9980675955Sthorpej netdev_sock = -1;
10080675955Sthorpej }
10180675955Sthorpej return error;
10280675955Sthorpej }
10380675955Sthorpej
10480675955Sthorpej int
net_close(struct of_dev * op)10545879fd7Schristos net_close(struct of_dev *op)
10680675955Sthorpej {
10780675955Sthorpej /*
10880675955Sthorpej * On last close, do netif close, etc.
10980675955Sthorpej */
11080675955Sthorpej if (open_count > 0)
11180675955Sthorpej if (--open_count == 0) {
11266a5580cSdrochner netif_of_close(netdev_sock);
11380675955Sthorpej netdev_sock = -1;
11480675955Sthorpej }
11545879fd7Schristos return 0;
11680675955Sthorpej }
11780675955Sthorpej
11880675955Sthorpej int
net_mountroot(void)11945879fd7Schristos net_mountroot(void)
12080675955Sthorpej {
12180675955Sthorpej
12280675955Sthorpej #ifdef DEBUG
12380675955Sthorpej printf("net_mountroot\n");
12480675955Sthorpej #endif
12580675955Sthorpej
12680675955Sthorpej /*
12780675955Sthorpej * Get info for NFS boot: our IP address, out hostname,
12880675955Sthorpej * server IP address, and our root path on the server.
12980675955Sthorpej * We use BOOTP (RFC951, RFC1532) exclusively as mandated
13080675955Sthorpej * by PowerPC Reference Platform Specification I.4.2
13180675955Sthorpej */
13280675955Sthorpej
13380675955Sthorpej bootp(netdev_sock);
13480675955Sthorpej
13580675955Sthorpej if (myip.s_addr == 0)
13680675955Sthorpej return ETIMEDOUT;
13780675955Sthorpej
13880675955Sthorpej printf("Using IP address: %s\n", inet_ntoa(myip));
13980675955Sthorpej
14080675955Sthorpej #ifdef DEBUG
14180675955Sthorpej printf("myip: %s (%s)", hostname, inet_ntoa(myip));
14280675955Sthorpej if (gateip.s_addr)
14380675955Sthorpej printf(", gateip: %s", inet_ntoa(gateip));
14480675955Sthorpej if (netmask)
14580675955Sthorpej printf(", netmask: %s", intoa(netmask));
14680675955Sthorpej printf("\n");
14780675955Sthorpej #endif
14880675955Sthorpej printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
14980675955Sthorpej
15080675955Sthorpej /*
15180675955Sthorpej * Get the NFS file handle (mount).
15280675955Sthorpej */
15380675955Sthorpej if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
15480675955Sthorpej return errno;
15580675955Sthorpej return 0;
15680675955Sthorpej }
157