1105760Srwatson/*-
2105760Srwatson * Copyright (c) 2001 Networks Associates Technology, Inc.
3105760Srwatson * All rights reserved.
4105760Srwatson *
5105760Srwatson * This software was developed for the FreeBSD Project by NAI Labs, the
6105760Srwatson * Security Research Division of Network Associates, Inc. under
7105760Srwatson * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
8105760Srwatson * CHATS research program.
9105760Srwatson *
10105760Srwatson * Redistribution and use in source and binary forms, with or without
11105760Srwatson * modification, are permitted provided that the following conditions
12105760Srwatson * are met:
13105760Srwatson * 1. Redistributions of source code must retain the above copyright
14105760Srwatson *    notice, this list of conditions and the following disclaimer.
15105760Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16105760Srwatson *    notice, this list of conditions and the following disclaimer in the
17105760Srwatson *    documentation and/or other materials provided with the distribution.
18105760Srwatson * 3. The name of the author may not be used to endorse or promote
19105760Srwatson *    products derived from this software without specific prior written
20105760Srwatson *    permission.
21105760Srwatson *
22105760Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23105760Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24105760Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25105760Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26105760Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27105760Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28105760Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29105760Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30105760Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31105760Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32105760Srwatson * SUCH DAMAGE.
33105760Srwatson *
34105760Srwatson * $FreeBSD$
35105760Srwatson */
36105760Srwatson
37105760Srwatson#include <sys/param.h>
38105760Srwatson#include <sys/ioctl.h>
39105760Srwatson#include <sys/mac.h>
40105760Srwatson#include <sys/socket.h>
41105760Srwatson#include <sys/sockio.h>
42105760Srwatson
43105760Srwatson#include <net/if.h>
44105760Srwatson#include <net/route.h>
45105760Srwatson
46105760Srwatson#include <stdio.h>
47105760Srwatson#include <stdlib.h>
48105760Srwatson#include <string.h>
49105760Srwatson
50105760Srwatson#include "ifconfig.h"
51105760Srwatson
52138593Ssamstatic void
53139494Ssammaclabel_status(int s)
54105760Srwatson{
55105760Srwatson	struct ifreq ifr;
56105760Srwatson	mac_t label;
57105760Srwatson	char *label_text;
58105760Srwatson
59105760Srwatson	memset(&ifr, 0, sizeof(ifr));
60300285Struckman	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
61105760Srwatson
62105760Srwatson	if (mac_prepare_ifnet_label(&label) == -1)
63105760Srwatson		return;
64105760Srwatson	ifr.ifr_ifru.ifru_data = (void *)label;
65105760Srwatson	if (ioctl(s, SIOCGIFMAC, &ifr) == -1)
66105760Srwatson		goto mac_free;
67105760Srwatson
68105760Srwatson
69105760Srwatson	if (mac_to_text(label, &label_text) == -1)
70105760Srwatson		goto mac_free;
71105760Srwatson
72105760Srwatson	if (strlen(label_text) != 0)
73105825Srwatson		printf("\tmaclabel %s\n", label_text);
74105760Srwatson	free(label_text);
75105760Srwatson
76105760Srwatsonmac_free:
77105760Srwatson	mac_free(label);
78105760Srwatson}
79105760Srwatson
80138593Ssamstatic void
81105825Srwatsonsetifmaclabel(const char *val, int d, int s, const struct afswtch *rafp)
82105760Srwatson{
83105760Srwatson	struct ifreq ifr;
84105760Srwatson	mac_t label;
85105760Srwatson	int error;
86105760Srwatson
87105760Srwatson	if (mac_from_text(&label, val) == -1) {
88105760Srwatson		perror(val);
89105760Srwatson		return;
90105760Srwatson	}
91105760Srwatson
92105760Srwatson	memset(&ifr, 0, sizeof(ifr));
93300285Struckman	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
94105760Srwatson	ifr.ifr_ifru.ifru_data = (void *)label;
95105760Srwatson
96105760Srwatson	error = ioctl(s, SIOCSIFMAC, &ifr);
97105760Srwatson	mac_free(label);
98105760Srwatson	if (error == -1)
99105760Srwatson		perror("setifmac");
100105760Srwatson}
101138593Ssam
102138593Ssamstatic struct cmd mac_cmds[] = {
103138593Ssam	DEF_CMD_ARG("maclabel",	setifmaclabel),
104138593Ssam};
105138593Ssamstatic struct afswtch af_mac = {
106138593Ssam	.af_name	= "af_maclabel",
107138593Ssam	.af_af		= AF_UNSPEC,
108139494Ssam	.af_other_status = maclabel_status,
109138593Ssam};
110138593Ssam
111138593Ssamstatic __constructor void
112138593Ssammac_ctor(void)
113138593Ssam{
114194799Sdelphij	size_t i;
115138593Ssam
116289986Sngie	for (i = 0; i < nitems(mac_cmds);  i++)
117138593Ssam		cmd_register(&mac_cmds[i]);
118138593Ssam	af_register(&af_mac);
119138593Ssam}
120