ifconfig(8): Add '-f' option to print in more formats
[dragonfly.git] / sbin / ifconfig / af_link.c
CommitLineData
ca74a0a2
SZ
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
ca74a0a2
SZ
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: src/sbin/ifconfig/af_link.c,v 1.2 2004/12/31 19:46:27 sam Exp $
ca74a0a2
SZ
30 */
31
32#include <sys/types.h>
33#include <sys/ioctl.h>
ca74a0a2
SZ
34#include <net/if.h>
35#include <net/route.h> /* for RTX_IFA */
46158ff5
AL
36#include <net/if_dl.h>
37#include <net/if_types.h>
38#include <net/ethernet.h>
ca74a0a2
SZ
39
40#include <err.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
ca74a0a2
SZ
45#include "ifconfig.h"
46
47static struct ifreq link_ridreq;
48
49static void
50link_status(int s __unused, const struct rt_addrinfo *info)
51{
52 const struct sockaddr_dl *sdl =
53 (const struct sockaddr_dl *) info->rti_info[RTAX_IFA];
54
55 if (sdl != NULL && sdl->sdl_alen > 0) {
56 if (sdl->sdl_type == IFT_ETHER &&
51a3d09e
AL
57 sdl->sdl_alen == ETHER_ADDR_LEN) {
58 char *ether_addr = ether_ntoa(
59 (const struct ether_addr *)LLADDR(sdl));
60
61 if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
62 char *fchar = ether_addr;
63 while ((fchar = strchr(fchar, ':')) != NULL)
64 *fchar = '-';
65 }
66 printf("\tether %s\n", ether_addr);
67 } else {
ca74a0a2
SZ
68 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
69
70 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
71 }
72 }
73}
74
75static void
76link_getaddr(const char *addr, int which)
77{
78 char *temp;
79 struct sockaddr_dl sdl;
80 struct sockaddr *sa = &link_ridreq.ifr_addr;
81
82 if (which != ADDR)
83 errx(1, "can't set link-level netmask or broadcast");
b1359e8e 84 if ((temp = malloc(strlen(addr) + 2)) == NULL)
ca74a0a2
SZ
85 errx(1, "malloc failed");
86 temp[0] = ':';
87 strcpy(temp + 1, addr);
88 sdl.sdl_len = sizeof(sdl);
89 link_addr(temp, &sdl);
90 free(temp);
91 if (sdl.sdl_alen > sizeof(sa->sa_data))
92 errx(1, "malformed link-level address");
93 sa->sa_family = AF_LINK;
94 sa->sa_len = sdl.sdl_alen;
95 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
96}
97
98static struct afswtch af_link = {
99 .af_name = "link",
100 .af_af = AF_LINK,
101 .af_status = link_status,
102 .af_getaddr = link_getaddr,
103 .af_aifaddr = SIOCSIFLLADDR,
104 .af_addreq = &link_ridreq,
105};
106static struct afswtch af_ether = {
107 .af_name = "ether",
108 .af_af = AF_LINK,
109 .af_status = link_status,
110 .af_getaddr = link_getaddr,
111 .af_aifaddr = SIOCSIFLLADDR,
112 .af_addreq = &link_ridreq,
113};
114static struct afswtch af_lladdr = {
115 .af_name = "lladdr",
116 .af_af = AF_LINK,
117 .af_status = link_status,
118 .af_getaddr = link_getaddr,
119 .af_aifaddr = SIOCSIFLLADDR,
120 .af_addreq = &link_ridreq,
121};
122
d44b223a 123static __constructor(101) void
ca74a0a2
SZ
124link_ctor(void)
125{
126 af_register(&af_link);
127 af_register(&af_ether);
128 af_register(&af_lladdr);
129}