xref: /netbsd-src/sys/compat/common/rtsock_70.c (revision 8a031a1d1e0aac23d43015c71ae6350b917c8347)
1 /*	$NetBSD: rtsock_70.c,v 1.8 2019/12/12 02:15:42 pgoyette Exp $	*/
2 
3 /*
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roy Marples.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rtsock_70.c,v 1.8 2019/12/12 02:15:42 pgoyette Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/mbuf.h>
40 #include <sys/compat_stub.h>
41 
42 #include <net/if.h>
43 #include <net/route.h>
44 
45 #include <compat/net/if.h>
46 #include <compat/net/route.h>
47 #include <compat/net/route_70.h>
48 
49 void
compat_70_rt_newaddrmsg1(int cmd,struct ifaddr * ifa)50 compat_70_rt_newaddrmsg1(int cmd, struct ifaddr *ifa)
51 {
52 	struct rt_addrinfo info;
53 	const struct sockaddr *sa;
54 	struct mbuf *m;
55 	struct ifnet *ifp;
56 	struct ifa_msghdr70 ifam;
57 	int ncmd;
58 
59 	KASSERT(ifa != NULL);
60 	ifp = ifa->ifa_ifp;
61 
62 	switch (cmd) {
63 	case RTM_NEWADDR:
64 		ncmd = RTM_ONEWADDR;
65 		break;
66 	case RTM_DELADDR:
67 		ncmd = RTM_ODELADDR;
68 		break;
69 	case RTM_CHGADDR:
70 		ncmd = RTM_OCHGADDR;
71 		break;
72 	default:
73 		panic("%s: called with wrong command", __func__);
74 	}
75 
76 	memset(&info, 0, sizeof(info));
77 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
78 	KASSERT(ifp->if_dl != NULL);
79 	info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
80 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
81 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
82 
83 	memset(&ifam, 0, sizeof(ifam));
84 	ifam.ifam_index = ifp->if_index;
85 	ifam.ifam_metric = ifa->ifa_metric;
86 	ifam.ifam_flags = ifa->ifa_flags;
87 
88 	m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
89 	if (m == NULL)
90 		return;
91 
92 	mtod(m, struct ifa_msghdr70 *)->ifam_addrs = info.rti_addrs;
93 	route_enqueue(m, sa ? sa->sa_family : 0);
94 }
95 
96 int
compat_70_iflist_addr(struct rt_walkarg * w,struct ifaddr * ifa,struct rt_addrinfo * info)97 compat_70_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
98      struct rt_addrinfo *info)
99 {
100 	int len, error;
101 
102 	if ((error = rt_msg3(RTM_ONEWADDR, info, 0, w, &len)))
103 		return error;
104 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
105 		struct ifa_msghdr70 *ifam;
106 
107 		ifam = (struct ifa_msghdr70 *)w->w_tmem;
108 		ifam->ifam_index = ifa->ifa_ifp->if_index;
109 		ifam->ifam_flags = ifa->ifa_flags;
110 		ifam->ifam_metric = ifa->ifa_metric;
111 		ifam->ifam_addrs = info->rti_addrs;
112 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
113 			w->w_where = (char *)w->w_where + len;
114 	}
115 	return error;
116 }
117 
118 void
rtsock_70_init(void)119 rtsock_70_init(void)
120 {
121 
122 	MODULE_HOOK_SET(rtsock_newaddr_70_hook, compat_70_rt_newaddrmsg1);
123 	MODULE_HOOK_SET(rtsock_iflist_70_hook, compat_70_iflist_addr);
124 }
125 
126 void
rtsock_70_fini(void)127 rtsock_70_fini(void)
128 {
129 
130 	MODULE_HOOK_UNSET(rtsock_newaddr_70_hook);
131 	MODULE_HOOK_UNSET(rtsock_iflist_70_hook);
132 }
133