summaryrefslogtreecommitdiff
path: root/sys/compat/common/rtsock_70.c
blob: 7a44fa9d8913ec13e755e22ab879a050efb33a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*	$NetBSD: rtsock_70.c,v 1.8 2019/12/12 02:15:42 pgoyette Exp $	*/

/*
 * Copyright (c) 2016 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Roy Marples.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rtsock_70.c,v 1.8 2019/12/12 02:15:42 pgoyette Exp $");

#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
#endif

#include <sys/mbuf.h>
#include <sys/compat_stub.h>

#include <net/if.h>
#include <net/route.h>

#include <compat/net/if.h>
#include <compat/net/route.h>
#include <compat/net/route_70.h>

void
compat_70_rt_newaddrmsg1(int cmd, struct ifaddr *ifa)
{
	struct rt_addrinfo info;
	const struct sockaddr *sa;
	struct mbuf *m;
	struct ifnet *ifp;
	struct ifa_msghdr70 ifam;
	int ncmd;

	KASSERT(ifa != NULL);
	ifp = ifa->ifa_ifp;

	switch (cmd) {
	case RTM_NEWADDR:
		ncmd = RTM_ONEWADDR;
		break;
	case RTM_DELADDR:
		ncmd = RTM_ODELADDR;
		break;
	case RTM_CHGADDR:
		ncmd = RTM_OCHGADDR;
		break;
	default:
		panic("%s: called with wrong command", __func__);
	}

	memset(&info, 0, sizeof(info));
	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
	KASSERT(ifp->if_dl != NULL);
	info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;

	memset(&ifam, 0, sizeof(ifam));
	ifam.ifam_index = ifp->if_index;
	ifam.ifam_metric = ifa->ifa_metric;
	ifam.ifam_flags = ifa->ifa_flags;

	m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
	if (m == NULL)
		return;

	mtod(m, struct ifa_msghdr70 *)->ifam_addrs = info.rti_addrs;
	route_enqueue(m, sa ? sa->sa_family : 0);
}

int
compat_70_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
     struct rt_addrinfo *info)
{
	int len, error;

	if ((error = rt_msg3(RTM_ONEWADDR, info, 0, w, &len)))
		return error;
	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
		struct ifa_msghdr70 *ifam;

		ifam = (struct ifa_msghdr70 *)w->w_tmem;
		ifam->ifam_index = ifa->ifa_ifp->if_index;
		ifam->ifam_flags = ifa->ifa_flags;
		ifam->ifam_metric = ifa->ifa_metric;
		ifam->ifam_addrs = info->rti_addrs;
		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
			w->w_where = (char *)w->w_where + len;
	}
	return error;
}

void
rtsock_70_init(void)
{

	MODULE_HOOK_SET(rtsock_newaddr_70_hook, compat_70_rt_newaddrmsg1);
	MODULE_HOOK_SET(rtsock_iflist_70_hook, compat_70_iflist_addr);
}

void
rtsock_70_fini(void)
{

	MODULE_HOOK_UNSET(rtsock_newaddr_70_hook);
	MODULE_HOOK_UNSET(rtsock_iflist_70_hook);
}