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