1*8352e4cbSmiod /* $OpenBSD: dev_net.c,v 1.6 2023/01/10 17:10:57 miod Exp $ */
296f141a8Smiod /* $NetBSD: dev_net.c,v 1.26 2011/07/17 20:54:52 joerg Exp $ */
396f141a8Smiod
496f141a8Smiod /*-
596f141a8Smiod * Copyright (c) 1997 The NetBSD Foundation, Inc.
696f141a8Smiod * All rights reserved.
796f141a8Smiod *
896f141a8Smiod * This code is derived from software contributed to The NetBSD Foundation
996f141a8Smiod * by Gordon W. Ross.
1096f141a8Smiod *
1196f141a8Smiod * Redistribution and use in source and binary forms, with or without
1296f141a8Smiod * modification, are permitted provided that the following conditions
1396f141a8Smiod * are met:
1496f141a8Smiod * 1. Redistributions of source code must retain the above copyright
1596f141a8Smiod * notice, this list of conditions and the following disclaimer.
1696f141a8Smiod * 2. Redistributions in binary form must reproduce the above copyright
1796f141a8Smiod * notice, this list of conditions and the following disclaimer in the
1896f141a8Smiod * documentation and/or other materials provided with the distribution.
1996f141a8Smiod *
2096f141a8Smiod * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2196f141a8Smiod * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2296f141a8Smiod * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2396f141a8Smiod * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2496f141a8Smiod * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2596f141a8Smiod * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2696f141a8Smiod * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2796f141a8Smiod * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2896f141a8Smiod * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2996f141a8Smiod * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3096f141a8Smiod * POSSIBILITY OF SUCH DAMAGE.
3196f141a8Smiod */
3296f141a8Smiod
3396f141a8Smiod /*
3496f141a8Smiod * This module implements a "raw device" interface suitable for
3596f141a8Smiod * use by the stand-alone I/O library NFS code. This interface
3696f141a8Smiod * does not support any "block" access, and exists only for the
3796f141a8Smiod * purpose of initializing the network interface, getting boot
3896f141a8Smiod * parameters, and performing the NFS mount.
3996f141a8Smiod *
4096f141a8Smiod * At open time, this does:
4196f141a8Smiod *
4296f141a8Smiod * find interface - netif_open()
4396f141a8Smiod * RARP for IP address - rarp_getipaddress()
4496f141a8Smiod * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
4596f141a8Smiod * RPC/mountd - nfs_mount(sock, ip, path)
4696f141a8Smiod *
4796f141a8Smiod * the root file handle from mountd is saved in a global
4896f141a8Smiod * for use by the NFS open code (NFS/lookup).
4996f141a8Smiod */
5096f141a8Smiod
5196f141a8Smiod #include <sys/param.h>
5296f141a8Smiod #include <sys/socket.h>
5396f141a8Smiod #include <netinet/in.h>
5496f141a8Smiod
5596f141a8Smiod #include <lib/libkern/libkern.h>
5696f141a8Smiod
5796f141a8Smiod #include <lib/libsa/stand.h>
5896f141a8Smiod #include <lib/libsa/net.h>
5996f141a8Smiod #include <lib/libsa/netif.h>
6096f141a8Smiod #include <lib/libsa/nfs.h>
6196f141a8Smiod #include <lib/libsa/bootparam.h>
6296f141a8Smiod #include "dev_net.h"
6396f141a8Smiod #ifdef SUPPORT_BOOTP
6496f141a8Smiod #include <lib/libsa/bootp.h>
6596f141a8Smiod #endif
6696f141a8Smiod
6796f141a8Smiod extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
6896f141a8Smiod
6996f141a8Smiod static int netdev_sock = -1;
7096f141a8Smiod static int netdev_opens;
7196f141a8Smiod
7296f141a8Smiod static int net_getparams(int);
7396f141a8Smiod
74d2f66e2eSmiod #ifdef DEBUG
75*8352e4cbSmiod extern int debug;
76d2f66e2eSmiod #endif
77d2f66e2eSmiod
7896f141a8Smiod /*
7996f141a8Smiod * Called by devopen after it sets f->f_dev to our devsw entry.
8096f141a8Smiod * This opens the low-level device and sets f->f_devdata.
8196f141a8Smiod * This is declared with variable arguments...
8296f141a8Smiod */
8396f141a8Smiod int
net_open(struct open_file * f,...)8496f141a8Smiod net_open(struct open_file *f, ...)
8596f141a8Smiod {
8696f141a8Smiod va_list ap;
8796f141a8Smiod char *devname; /* Device part of file name (or NULL). */
8896f141a8Smiod int error = 0;
8996f141a8Smiod
9096f141a8Smiod va_start(ap, f);
9196f141a8Smiod devname = va_arg(ap, char *);
9296f141a8Smiod va_end(ap);
9396f141a8Smiod
9496f141a8Smiod #ifdef NETIF_DEBUG
9596f141a8Smiod if (debug)
9696f141a8Smiod printf("%s\n", devname);
9796f141a8Smiod #endif
9896f141a8Smiod
9996f141a8Smiod /* On first open, do netif open, mount, etc. */
10096f141a8Smiod if (netdev_opens == 0) {
10196f141a8Smiod /* Find network interface. */
10296f141a8Smiod if (netdev_sock < 0) {
10396f141a8Smiod netdev_sock = netif_open(devname);
10496f141a8Smiod if (netdev_sock < 0) {
10596f141a8Smiod printf("netif_open() failed\n");
10696f141a8Smiod return ENXIO;
10796f141a8Smiod }
10896f141a8Smiod #ifdef NETIF_DEBUG
10996f141a8Smiod if (debug)
11096f141a8Smiod printf("netif_open() succeeded\n");
11196f141a8Smiod #endif
11296f141a8Smiod }
11396f141a8Smiod if (rootip.s_addr == 0) {
11496f141a8Smiod /* Get root IP address, and path, etc. */
11596f141a8Smiod error = net_getparams(netdev_sock);
11696f141a8Smiod if (error) {
11796f141a8Smiod /* getparams makes its own noise */
11896f141a8Smiod goto fail;
11996f141a8Smiod }
12096f141a8Smiod /* Get the NFS file handle (mountd). */
12196f141a8Smiod error = nfs_mount(netdev_sock, rootip, rootpath);
12296f141a8Smiod if (error) {
12396f141a8Smiod printf("NFS mount error=%d\n", errno);
12496f141a8Smiod rootip.s_addr = 0;
12596f141a8Smiod fail:
12696f141a8Smiod netif_close(netdev_sock);
12796f141a8Smiod netdev_sock = -1;
12896f141a8Smiod return error;
12996f141a8Smiod }
13096f141a8Smiod #ifdef NETIF_DEBUG
13196f141a8Smiod if (debug)
13296f141a8Smiod printf("NFS mount succeeded\n");
13396f141a8Smiod #endif
13496f141a8Smiod }
13596f141a8Smiod }
13696f141a8Smiod netdev_opens++;
13796f141a8Smiod f->f_devdata = nfs_root_node;
13896f141a8Smiod return error;
13996f141a8Smiod }
14096f141a8Smiod
14196f141a8Smiod int
net_close(struct open_file * f)14296f141a8Smiod net_close(struct open_file *f)
14396f141a8Smiod {
14496f141a8Smiod
14596f141a8Smiod #ifdef NETIF_DEBUG
14696f141a8Smiod if (debug)
14796f141a8Smiod printf("net_close: opens=%d\n", netdev_opens);
14896f141a8Smiod #endif
14996f141a8Smiod
15096f141a8Smiod /* On last close, do netif close, etc. */
15196f141a8Smiod f->f_devdata = NULL;
15296f141a8Smiod /* Extra close call? */
15396f141a8Smiod if (netdev_opens <= 0)
15496f141a8Smiod return 0;
15596f141a8Smiod netdev_opens--;
15696f141a8Smiod /* Not last close? */
15796f141a8Smiod if (netdev_opens > 0)
15896f141a8Smiod return 0;
15996f141a8Smiod rootip.s_addr = 0;
16096f141a8Smiod if (netdev_sock >= 0) {
16196f141a8Smiod #ifdef NETIF_DEBUG
16296f141a8Smiod if (debug)
16396f141a8Smiod printf("net_close: calling netif_close()\n");
16496f141a8Smiod #endif
16596f141a8Smiod netif_close(netdev_sock);
16696f141a8Smiod netdev_sock = -1;
16796f141a8Smiod }
16896f141a8Smiod return 0;
16996f141a8Smiod }
17096f141a8Smiod
17196f141a8Smiod int
net_ioctl(struct open_file * f,u_long cmd,void * data)17296f141a8Smiod net_ioctl(struct open_file *f, u_long cmd, void *data)
17396f141a8Smiod {
17496f141a8Smiod
17596f141a8Smiod return EIO;
17696f141a8Smiod }
17796f141a8Smiod
17896f141a8Smiod int
net_strategy(void * devdata,int rw,daddr_t blk,size_t size,void * buf,size_t * rsize)1793e58d19eSkrw net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
18096f141a8Smiod size_t *rsize)
18196f141a8Smiod {
18296f141a8Smiod
18396f141a8Smiod return EIO;
18496f141a8Smiod }
18596f141a8Smiod
18696f141a8Smiod
18796f141a8Smiod /*
18896f141a8Smiod * Get info for NFS boot: our IP address, our hostname,
18996f141a8Smiod * server IP address, and our root path on the server.
19096f141a8Smiod * There are two ways to do this: The old, Sun way,
19196f141a8Smiod * and the more modern, BOOTP way. (RFC951, RFC1048)
19296f141a8Smiod *
19396f141a8Smiod * The default is to use the Sun bootparams RPC
19496f141a8Smiod * (because that is what the kernel will do).
19536fd90dcSjsg * MD code can make try_bootp initialized data,
19696f141a8Smiod * which will override this common definition.
19796f141a8Smiod */
19896f141a8Smiod #ifdef SUPPORT_BOOTP
19996f141a8Smiod int try_bootp;
20096f141a8Smiod #endif
20196f141a8Smiod
20296f141a8Smiod static int
net_getparams(int sock)20396f141a8Smiod net_getparams(int sock)
20496f141a8Smiod {
20596f141a8Smiod char buf[MAXHOSTNAMELEN];
206eb76c208Smpi u_int32_t smask;
20796f141a8Smiod
20896f141a8Smiod #ifdef SUPPORT_BOOTP
20996f141a8Smiod /*
21096f141a8Smiod * Try to get boot info using BOOTP. If we succeed, then
21196f141a8Smiod * the server IP address, gateway, and root path will all
21296f141a8Smiod * be initialized. If any remain uninitialized, we will
21396f141a8Smiod * use RARP and RPC/bootparam (the Sun way) to get them.
21496f141a8Smiod */
21596f141a8Smiod if (try_bootp)
21696f141a8Smiod bootp(sock);
21796f141a8Smiod if (myip.s_addr != 0)
21896f141a8Smiod return 0;
21996f141a8Smiod #ifdef NETIF_DEBUG
22096f141a8Smiod if (debug)
22196f141a8Smiod printf("BOOTP failed, trying RARP/RPC...\n");
22296f141a8Smiod #endif
22396f141a8Smiod #endif
22496f141a8Smiod
22596f141a8Smiod /*
22696f141a8Smiod * Use RARP to get our IP address. This also sets our
22796f141a8Smiod * netmask to the "natural" default for our address.
22896f141a8Smiod */
22996f141a8Smiod if (rarp_getipaddress(sock)) {
23096f141a8Smiod printf("RARP failed\n");
23196f141a8Smiod return EIO;
23296f141a8Smiod }
23396f141a8Smiod #ifdef NETIF_DEBUG
23496f141a8Smiod if (debug)
23596f141a8Smiod printf("client addr: %s\n", inet_ntoa(myip));
23696f141a8Smiod #endif
23796f141a8Smiod
23896f141a8Smiod /* Get our hostname, server IP address, gateway. */
23996f141a8Smiod if (bp_whoami(sock)) {
24096f141a8Smiod printf("bootparam/whoami RPC failed\n");
24196f141a8Smiod return EIO;
24296f141a8Smiod }
24396f141a8Smiod #ifdef NETIF_DEBUG
24496f141a8Smiod if (debug)
24596f141a8Smiod printf("client name: %s\n", hostname);
24696f141a8Smiod #endif
24796f141a8Smiod
24896f141a8Smiod /*
24996f141a8Smiod * Ignore the gateway from whoami (unreliable).
25096f141a8Smiod * Use the "gateway" parameter instead.
25196f141a8Smiod */
25296f141a8Smiod smask = 0;
25396f141a8Smiod gateip.s_addr = 0;
25496f141a8Smiod if (bp_getfile(sock, "gateway", &gateip, buf)) {
25596f141a8Smiod printf("nfs_open: gateway bootparam missing\n");
25696f141a8Smiod } else {
25796f141a8Smiod /* Got it! Parse the netmask. */
25896f141a8Smiod smask = inet_addr(buf);
25996f141a8Smiod }
26096f141a8Smiod if (smask) {
26196f141a8Smiod netmask = smask;
26296f141a8Smiod #ifdef NETIF_DEBUG
26396f141a8Smiod if (debug)
26496f141a8Smiod printf("subnet mask: %s\n", intoa(netmask));
26596f141a8Smiod #endif
26696f141a8Smiod }
26796f141a8Smiod #ifdef NETIF_DEBUG
26896f141a8Smiod if (debug)
26996f141a8Smiod if (gateip.s_addr)
27096f141a8Smiod printf("net gateway: %s\n", inet_ntoa(gateip));
27196f141a8Smiod #endif
27296f141a8Smiod
27396f141a8Smiod /* Get the root server and pathname. */
27496f141a8Smiod if (bp_getfile(sock, "root", &rootip, rootpath)) {
27596f141a8Smiod printf("bootparam/getfile RPC failed\n");
27696f141a8Smiod return EIO;
27796f141a8Smiod }
27896f141a8Smiod
27996f141a8Smiod #ifdef NETIF_DEBUG
28096f141a8Smiod if (debug) {
28196f141a8Smiod printf("server addr: %s\n", inet_ntoa(rootip));
28296f141a8Smiod printf("server path: %s\n", rootpath);
28396f141a8Smiod }
28496f141a8Smiod #endif
28596f141a8Smiod
28696f141a8Smiod return 0;
28796f141a8Smiod }
288