1 1.8 msaitoh /* $NetBSD: af_link.c,v 1.8 2019/08/16 10:33:17 msaitoh Exp $ */ 2 1.1 dyoung 3 1.1 dyoung /*- 4 1.1 dyoung * Copyright (c) 2008 David Young. All rights reserved. 5 1.1 dyoung * 6 1.1 dyoung * Redistribution and use in source and binary forms, with or without 7 1.1 dyoung * modification, are permitted provided that the following conditions 8 1.1 dyoung * are met: 9 1.1 dyoung * 1. Redistributions of source code must retain the above copyright 10 1.1 dyoung * notice, this list of conditions and the following disclaimer. 11 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 dyoung * notice, this list of conditions and the following disclaimer in the 13 1.1 dyoung * documentation and/or other materials provided with the distribution. 14 1.1 dyoung * 15 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 1.1 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 1.1 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 1.1 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 1.1 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 1.1 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 1.1 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 1.1 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 1.1 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 dyoung * SUCH DAMAGE. 26 1.1 dyoung */ 27 1.1 dyoung 28 1.1 dyoung #include <sys/cdefs.h> 29 1.1 dyoung #ifndef lint 30 1.8 msaitoh __RCSID("$NetBSD: af_link.c,v 1.8 2019/08/16 10:33:17 msaitoh Exp $"); 31 1.1 dyoung #endif /* not lint */ 32 1.1 dyoung 33 1.8 msaitoh #include <sys/param.h> 34 1.8 msaitoh #include <sys/ioctl.h> 35 1.1 dyoung #include <sys/socket.h> 36 1.1 dyoung 37 1.8 msaitoh #include <net/if.h> 38 1.8 msaitoh #include <net/if_dl.h> 39 1.1 dyoung 40 1.1 dyoung #include <assert.h> 41 1.1 dyoung #include <err.h> 42 1.1 dyoung #include <errno.h> 43 1.1 dyoung #include <ifaddrs.h> 44 1.1 dyoung #include <netdb.h> 45 1.1 dyoung #include <string.h> 46 1.1 dyoung #include <stdlib.h> 47 1.1 dyoung #include <stdio.h> 48 1.1 dyoung #include <util.h> 49 1.1 dyoung 50 1.1 dyoung #include "env.h" 51 1.1 dyoung #include "extern.h" 52 1.1 dyoung #include "af_inetany.h" 53 1.1 dyoung 54 1.3 dyoung static void link_status(prop_dictionary_t, prop_dictionary_t, bool); 55 1.3 dyoung static void link_commit_address(prop_dictionary_t, prop_dictionary_t); 56 1.3 dyoung 57 1.3 dyoung static const struct kwinst linkkw[] = { 58 1.3 dyoung {.k_word = "active", .k_key = "active", .k_type = KW_T_BOOL, 59 1.3 dyoung .k_bool = true, .k_nextparser = &command_root.pb_parser} 60 1.3 dyoung }; 61 1.3 dyoung 62 1.7 matt struct pkw link_pkw = PKW_INITIALIZER(&link_pkw, "link", NULL, NULL, 63 1.3 dyoung linkkw, __arraycount(linkkw), NULL); 64 1.3 dyoung 65 1.3 dyoung static struct afswtch af = { 66 1.3 dyoung .af_name = "link", .af_af = AF_LINK, .af_status = link_status, 67 1.3 dyoung .af_addr_commit = link_commit_address 68 1.3 dyoung }; 69 1.3 dyoung 70 1.3 dyoung static cmdloop_branch_t branch; 71 1.3 dyoung 72 1.3 dyoung static void link_constructor(void) __attribute__((constructor)); 73 1.3 dyoung 74 1.3 dyoung static void 75 1.1 dyoung link_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) 76 1.1 dyoung { 77 1.6 dyoung print_link_addresses(env, false); 78 1.1 dyoung } 79 1.1 dyoung 80 1.1 dyoung static int 81 1.3 dyoung link_pre_aifaddr(prop_dictionary_t env, const struct afparam *param) 82 1.1 dyoung { 83 1.1 dyoung bool active; 84 1.1 dyoung struct if_laddrreq *iflr = param->req.buf; 85 1.1 dyoung 86 1.1 dyoung if (prop_dictionary_get_bool(env, "active", &active) && active) 87 1.1 dyoung iflr->flags |= IFLR_ACTIVE; 88 1.1 dyoung 89 1.1 dyoung return 0; 90 1.1 dyoung } 91 1.1 dyoung 92 1.3 dyoung static void 93 1.1 dyoung link_commit_address(prop_dictionary_t env, prop_dictionary_t oenv) 94 1.1 dyoung { 95 1.1 dyoung struct if_laddrreq dgreq = { 96 1.1 dyoung .addr = { 97 1.1 dyoung .ss_family = AF_LINK, 98 1.1 dyoung .ss_len = sizeof(dgreq.addr), 99 1.1 dyoung }, 100 1.1 dyoung }; 101 1.1 dyoung struct if_laddrreq req = { 102 1.1 dyoung .addr = { 103 1.1 dyoung .ss_family = AF_LINK, 104 1.1 dyoung .ss_len = sizeof(req.addr), 105 1.1 dyoung } 106 1.1 dyoung }; 107 1.1 dyoung struct afparam linkparam = { 108 1.1 dyoung .req = BUFPARAM(req) 109 1.1 dyoung , .dgreq = BUFPARAM(dgreq) 110 1.1 dyoung , .name = { 111 1.1 dyoung {.buf = dgreq.iflr_name, 112 1.1 dyoung .buflen = sizeof(dgreq.iflr_name)}, 113 1.1 dyoung {.buf = req.iflr_name, 114 1.1 dyoung .buflen = sizeof(req.iflr_name)} 115 1.1 dyoung } 116 1.1 dyoung , .dgaddr = BUFPARAM(dgreq.addr) 117 1.1 dyoung , .addr = BUFPARAM(req.addr) 118 1.1 dyoung , .aifaddr = IFADDR_PARAM(SIOCALIFADDR) 119 1.1 dyoung , .difaddr = IFADDR_PARAM(SIOCDLIFADDR) 120 1.1 dyoung , .gifaddr = IFADDR_PARAM(0) 121 1.1 dyoung , .pre_aifaddr = link_pre_aifaddr 122 1.1 dyoung }; 123 1.1 dyoung commit_address(env, oenv, &linkparam); 124 1.1 dyoung } 125 1.3 dyoung 126 1.3 dyoung static void 127 1.3 dyoung link_constructor(void) 128 1.3 dyoung { 129 1.3 dyoung register_family(&af); 130 1.7 matt cmdloop_branch_init(&branch, &link_pkw.pk_parser); 131 1.3 dyoung register_cmdloop_branch(&branch); 132 1.3 dyoung } 133