ifmac.c revision 105760
1249259Sdim/*-
2249259Sdim * Copyright (c) 2001 Networks Associates Technology, Inc.
3249259Sdim * All rights reserved.
4249259Sdim *
5249259Sdim * This software was developed for the FreeBSD Project by NAI Labs, the
6249259Sdim * Security Research Division of Network Associates, Inc. under
7249259Sdim * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
8249259Sdim * CHATS research program.
9249259Sdim *
10249259Sdim * Redistribution and use in source and binary forms, with or without
11249259Sdim * modification, are permitted provided that the following conditions
12249259Sdim * are met:
13249259Sdim * 1. Redistributions of source code must retain the above copyright
14249259Sdim *    notice, this list of conditions and the following disclaimer.
15249259Sdim * 2. Redistributions in binary form must reproduce the above copyright
16249259Sdim *    notice, this list of conditions and the following disclaimer in the
17249259Sdim *    documentation and/or other materials provided with the distribution.
18249259Sdim * 3. The name of the author may not be used to endorse or promote
19249259Sdim *    products derived from this software without specific prior written
20249259Sdim *    permission.
21249259Sdim *
22249259Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25249259Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27249259Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32249259Sdim * SUCH DAMAGE.
33249259Sdim *
34249259Sdim * $FreeBSD: head/sbin/ifconfig/ifmac.c 105760 2002-10-23 03:40:47Z rwatson $
35249259Sdim */
36249259Sdim
37249259Sdim#include <sys/param.h>
38249259Sdim#include <sys/ioctl.h>
39249259Sdim#include <sys/mac.h>
40249259Sdim#include <sys/socket.h>
41249259Sdim#include <sys/sockio.h>
42249259Sdim
43249259Sdim#include <net/if.h>
44249259Sdim#include <net/route.h>
45249259Sdim
46249259Sdim#include <stdio.h>
47249259Sdim#include <stdlib.h>
48249259Sdim#include <string.h>
49249259Sdim
50249259Sdim#include "ifconfig.h"
51249259Sdim
52249259Sdimvoid
53249259Sdimmac_status(int s, struct rt_addrinfo *info)
54249259Sdim{
55249259Sdim	struct ifreq ifr;
56249259Sdim	mac_t label;
57249259Sdim	char *label_text;
58249259Sdim
59249259Sdim	memset(&ifr, 0, sizeof(ifr));
60249259Sdim	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
61249259Sdim
62249259Sdim	if (mac_prepare_ifnet_label(&label) == -1)
63249259Sdim		return;
64249259Sdim	ifr.ifr_ifru.ifru_data = (void *)label;
65249259Sdim	if (ioctl(s, SIOCGIFMAC, &ifr) == -1)
66249259Sdim		goto mac_free;
67249259Sdim
68249259Sdim
69249259Sdim	if (mac_to_text(label, &label_text) == -1)
70249259Sdim		goto mac_free;
71249259Sdim
72249259Sdim	if (strlen(label_text) != 0)
73249259Sdim		printf("\tmac %s\n", label_text);
74249259Sdim	free(label_text);
75249259Sdim
76249259Sdimmac_free:
77249259Sdim	mac_free(label);
78249259Sdim}
79249259Sdim
80249259Sdimvoid
81249259Sdimsetifmac(const char *val, int d, int s, const struct afswtch *rafp)
82249259Sdim{
83249259Sdim	struct ifreq ifr;
84249259Sdim	mac_t label;
85249259Sdim	int error;
86249259Sdim
87249259Sdim	if (mac_from_text(&label, val) == -1) {
88249259Sdim		perror(val);
89249259Sdim		return;
90249259Sdim	}
91249259Sdim
92249259Sdim	memset(&ifr, 0, sizeof(ifr));
93249259Sdim	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
94249259Sdim	ifr.ifr_ifru.ifru_data = (void *)label;
95249259Sdim
96249259Sdim	error = ioctl(s, SIOCSIFMAC, &ifr);
97249259Sdim	mac_free(label);
98249259Sdim	if (error == -1)
99249259Sdim		perror("setifmac");
100249259Sdim}
101249259Sdim