Home | History | Annotate | Download | only in tc
      1 /*
      2  * m_vlan.c		vlan manipulation module
      3  *
      4  *              This program is free software; you can redistribute it and/or
      5  *              modify it under the terms of the GNU General Public License
      6  *              as published by the Free Software Foundation; either version
      7  *              2 of the License, or (at your option) any later version.
      8  *
      9  * Authors:     Jiri Pirko <jiri (at) resnulli.us>
     10  */
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <unistd.h>
     15 #include <string.h>
     16 #include <linux/if_ether.h>
     17 #include "utils.h"
     18 #include "rt_names.h"
     19 #include "tc_util.h"
     20 #include <linux/tc_act/tc_vlan.h>
     21 
     22 static const char * const action_names[] = {
     23 	[TCA_VLAN_ACT_POP] = "pop",
     24 	[TCA_VLAN_ACT_PUSH] = "push",
     25 	[TCA_VLAN_ACT_MODIFY] = "modify",
     26 };
     27 
     28 static void explain(void)
     29 {
     30 	fprintf(stderr, "Usage: vlan pop\n");
     31 	fprintf(stderr, "       vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n");
     32 	fprintf(stderr, "       vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n");
     33 	fprintf(stderr, "       VLANPROTO is one of 802.1Q or 802.1AD\n");
     34 	fprintf(stderr, "            with default: 802.1Q\n");
     35 	fprintf(stderr, "       CONTROL := reclassify | pipe | drop | continue | pass |\n");
     36 	fprintf(stderr, "                  goto chain <CHAIN_INDEX>\n");
     37 }
     38 
     39 static void usage(void)
     40 {
     41 	explain();
     42 	exit(-1);
     43 }
     44 
     45 static bool has_push_attribs(int action)
     46 {
     47 	return action == TCA_VLAN_ACT_PUSH || action == TCA_VLAN_ACT_MODIFY;
     48 }
     49 
     50 static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
     51 		      int tca_id, struct nlmsghdr *n)
     52 {
     53 	int argc = *argc_p;
     54 	char **argv = *argv_p;
     55 	struct rtattr *tail;
     56 	int action = 0;
     57 	__u16 id;
     58 	int id_set = 0;
     59 	__u16 proto;
     60 	int proto_set = 0;
     61 	__u8 prio;
     62 	int prio_set = 0;
     63 	struct tc_vlan parm = {};
     64 
     65 	if (matches(*argv, "vlan") != 0)
     66 		return -1;
     67 
     68 	NEXT_ARG();
     69 
     70 	while (argc > 0) {
     71 		if (matches(*argv, "pop") == 0) {
     72 			if (action) {
     73 				fprintf(stderr, "unexpected \"%s\" - action already specified\n",
     74 					*argv);
     75 				explain();
     76 				return -1;
     77 			}
     78 			action = TCA_VLAN_ACT_POP;
     79 		} else if (matches(*argv, "push") == 0) {
     80 			if (action) {
     81 				fprintf(stderr, "unexpected \"%s\" - action already specified\n",
     82 					*argv);
     83 				explain();
     84 				return -1;
     85 			}
     86 			action = TCA_VLAN_ACT_PUSH;
     87 		} else if (matches(*argv, "modify") == 0) {
     88 			if (action) {
     89 				fprintf(stderr, "unexpected \"%s\" - action already specified\n",
     90 					*argv);
     91 				explain();
     92 				return -1;
     93 			}
     94 			action = TCA_VLAN_ACT_MODIFY;
     95 		} else if (matches(*argv, "id") == 0) {
     96 			if (!has_push_attribs(action)) {
     97 				fprintf(stderr, "\"%s\" is only valid for push/modify\n",
     98 					*argv);
     99 				explain();
    100 				return -1;
    101 			}
    102 			NEXT_ARG();
    103 			if (get_u16(&id, *argv, 0))
    104 				invarg("id is invalid", *argv);
    105 			id_set = 1;
    106 		} else if (matches(*argv, "protocol") == 0) {
    107 			if (!has_push_attribs(action)) {
    108 				fprintf(stderr, "\"%s\" is only valid for push/modify\n",
    109 					*argv);
    110 				explain();
    111 				return -1;
    112 			}
    113 			NEXT_ARG();
    114 			if (ll_proto_a2n(&proto, *argv))
    115 				invarg("protocol is invalid", *argv);
    116 			proto_set = 1;
    117 		} else if (matches(*argv, "priority") == 0) {
    118 			if (!has_push_attribs(action)) {
    119 				fprintf(stderr, "\"%s\" is only valid for push/modify\n",
    120 					*argv);
    121 				explain();
    122 				return -1;
    123 			}
    124 			NEXT_ARG();
    125 			if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
    126 				invarg("prio is invalid", *argv);
    127 			prio_set = 1;
    128 		} else if (matches(*argv, "help") == 0) {
    129 			usage();
    130 		} else {
    131 			break;
    132 		}
    133 		argc--;
    134 		argv++;
    135 	}
    136 
    137 	parse_action_control_dflt(&argc, &argv, &parm.action,
    138 				  false, TC_ACT_PIPE);
    139 
    140 	if (argc) {
    141 		if (matches(*argv, "index") == 0) {
    142 			NEXT_ARG();
    143 			if (get_u32(&parm.index, *argv, 10)) {
    144 				fprintf(stderr, "vlan: Illegal \"index\"\n");
    145 				return -1;
    146 			}
    147 			argc--;
    148 			argv++;
    149 		}
    150 	}
    151 
    152 	if (has_push_attribs(action) && !id_set) {
    153 		fprintf(stderr, "id needs to be set for %s\n",
    154 			action_names[action]);
    155 		explain();
    156 		return -1;
    157 	}
    158 
    159 	parm.v_action = action;
    160 	tail = NLMSG_TAIL(n);
    161 	addattr_l(n, MAX_MSG, tca_id, NULL, 0);
    162 	addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
    163 	if (id_set)
    164 		addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
    165 	if (proto_set) {
    166 		if (proto != htons(ETH_P_8021Q) &&
    167 		    proto != htons(ETH_P_8021AD)) {
    168 			fprintf(stderr, "protocol not supported\n");
    169 			explain();
    170 			return -1;
    171 		}
    172 
    173 		addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
    174 	}
    175 	if (prio_set)
    176 		addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
    177 
    178 	tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
    179 
    180 	*argc_p = argc;
    181 	*argv_p = argv;
    182 	return 0;
    183 }
    184 
    185 static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
    186 {
    187 	SPRINT_BUF(b1);
    188 	struct rtattr *tb[TCA_VLAN_MAX + 1];
    189 	__u16 val;
    190 	struct tc_vlan *parm;
    191 
    192 	if (arg == NULL)
    193 		return -1;
    194 
    195 	parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
    196 
    197 	if (!tb[TCA_VLAN_PARMS]) {
    198 		fprintf(f, "[NULL vlan parameters]");
    199 		return -1;
    200 	}
    201 	parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
    202 
    203 	fprintf(f, " vlan");
    204 
    205 	switch (parm->v_action) {
    206 	case TCA_VLAN_ACT_POP:
    207 		fprintf(f, " pop");
    208 		break;
    209 	case TCA_VLAN_ACT_PUSH:
    210 	case TCA_VLAN_ACT_MODIFY:
    211 		fprintf(f, " %s", action_names[parm->v_action]);
    212 		if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
    213 			val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
    214 			fprintf(f, " id %u", val);
    215 		}
    216 		if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
    217 			fprintf(f, " protocol %s",
    218 				ll_proto_n2a(rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]),
    219 					     b1, sizeof(b1)));
    220 		}
    221 		if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) {
    222 			val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
    223 			fprintf(f, " priority %u", val);
    224 		}
    225 		break;
    226 	}
    227 	print_action_control(f, " ", parm->action, "");
    228 
    229 	fprintf(f, "\n\t index %u ref %d bind %d", parm->index, parm->refcnt,
    230 		parm->bindcnt);
    231 
    232 	if (show_stats) {
    233 		if (tb[TCA_VLAN_TM]) {
    234 			struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
    235 
    236 			print_tm(f, tm);
    237 		}
    238 	}
    239 
    240 	fprintf(f, "\n ");
    241 
    242 	return 0;
    243 }
    244 
    245 struct action_util vlan_action_util = {
    246 	.id = "vlan",
    247 	.parse_aopt = parse_vlan,
    248 	.print_aopt = print_vlan,
    249 };
    250