Home | History | Annotate | Line # | Download | only in common
rtsock_70.c revision 1.2.2.1
      1 /*	$NetBSD: rtsock_70.c,v 1.2.2.1 2018/03/15 05:10:05 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.2.2.1 2018/03/15 05:10:05 pgoyette Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_compat_netbsd.h"
     37 #endif
     38 
     39 #include <sys/mbuf.h>
     40 #include <net/if.h>
     41 #include <net/route.h>
     42 
     43 #include <compat/net/if.h>
     44 #include <compat/net/route.h>
     45 #include <compat/net/route_70.h>
     46 
     47 static void (*orig_70_rt_newaddrmsg1)(int, struct ifaddr *);
     48 
     49 void
     50 rtsock_70_init(void)
     51 {
     52 
     53 	orig_70_rt_newaddrmsg1 = vec_70_rt_newaddrmsg1;
     54 	vec_70_rt_newaddrmsg1 = compat_70_rt_newaddrmsg1;
     55 }
     56 
     57 void
     58 rtsock_70_fini(void)
     59 {
     60 
     61 	vec_70_rt_newaddrmsg1 = orig_70_rt_newaddrmsg1;
     62 }
     63 
     64 void
     65 compat_70_rt_newaddrmsg1(int cmd, struct ifaddr *ifa)
     66 {
     67 	struct rt_addrinfo info;
     68 	const struct sockaddr *sa;
     69 	struct mbuf *m;
     70 	struct ifnet *ifp;
     71 	struct ifa_msghdr70 ifam;
     72 	int ncmd;
     73 
     74 	KASSERT(ifa != NULL);
     75 	ifp = ifa->ifa_ifp;
     76 
     77 	switch (cmd) {
     78 	case RTM_NEWADDR:
     79 		ncmd = RTM_ONEWADDR;
     80 		break;
     81 	case RTM_DELADDR:
     82 		ncmd = RTM_ODELADDR;
     83 		break;
     84 	case RTM_CHGADDR:
     85 		ncmd = RTM_OCHGADDR;
     86 		break;
     87 	default:
     88 		panic("%s: called with wrong command", __func__);
     89 	}
     90 
     91 	memset(&info, 0, sizeof(info));
     92 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
     93 	KASSERT(ifp->if_dl != NULL);
     94 	info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
     95 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
     96 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
     97 
     98 	memset(&ifam, 0, sizeof(ifam));
     99 	ifam.ifam_index = ifp->if_index;
    100 	ifam.ifam_metric = ifa->ifa_metric;
    101 	ifam.ifam_flags = ifa->ifa_flags;
    102 
    103 	m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
    104 	if (m == NULL)
    105 		return;
    106 
    107 	mtod(m, struct ifa_msghdr70 *)->ifam_addrs = info.rti_addrs;
    108 	route_enqueue(m, sa ? sa->sa_family : 0);
    109 }
    110 
    111 int
    112 compat_70_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
    113      struct rt_addrinfo *info)
    114 {
    115 	int len, error;
    116 
    117 	if ((error = rt_msg3(RTM_ONEWADDR, info, 0, w, &len)))
    118 		return error;
    119 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    120 		struct ifa_msghdr70 *ifam;
    121 
    122 		ifam = (struct ifa_msghdr70 *)w->w_tmem;
    123 		ifam->ifam_index = ifa->ifa_ifp->if_index;
    124 		ifam->ifam_flags = ifa->ifa_flags;
    125 		ifam->ifam_metric = ifa->ifa_metric;
    126 		ifam->ifam_addrs = info->rti_addrs;
    127 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
    128 			w->w_where = (char *)w->w_where + len;
    129 	}
    130 	return error;
    131 }
    132