blob: 4c32ae5741383c5ab70f53c2afd63b0f0123b344 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
10
Rob Landleyea224be2006-06-18 20:20:07 +000011#include "libbb.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000012#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include "utils.h"
14
Dan Fandrichf533ec82011-06-10 05:17:59 +020015#include <netinet/if_ether.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016
Denys Vlasenko405daff2012-09-02 14:29:45 +020017/* Please conditionalize exotic protocols on CONFIG_something */
Denis Vlasenkodfc07402007-10-29 19:33:26 +000018
Denys Vlasenko5f10fdf2012-09-03 11:41:30 +020019static const uint16_t llproto_ids[] = {
Denys Vlasenko405daff2012-09-02 14:29:45 +020020#define __PF(f,n) ETH_P_##f,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021__PF(LOOP,loop)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000022__PF(PUP,pup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023#ifdef ETH_P_PUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000024__PF(PUPAT,pupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025#endif
26__PF(IP,ip)
27__PF(X25,x25)
28__PF(ARP,arp)
29__PF(BPQ,bpq)
30#ifdef ETH_P_IEEEPUP
Eric Andersenc7bda1c2004-03-15 08:29:22 +000031__PF(IEEEPUP,ieeepup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032#endif
33#ifdef ETH_P_IEEEPUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000034__PF(IEEEPUPAT,ieeepupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000035#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000036__PF(DEC,dec)
37__PF(DNA_DL,dna_dl)
38__PF(DNA_RC,dna_rc)
39__PF(DNA_RT,dna_rt)
40__PF(LAT,lat)
41__PF(DIAG,diag)
42__PF(CUST,cust)
43__PF(SCA,sca)
44__PF(RARP,rarp)
45__PF(ATALK,atalk)
46__PF(AARP,aarp)
47__PF(IPX,ipx)
48__PF(IPV6,ipv6)
Eric Andersen0f08e532003-06-20 09:05:00 +000049#ifdef ETH_P_PPP_DISC
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050__PF(PPP_DISC,ppp_disc)
Eric Andersen0f08e532003-06-20 09:05:00 +000051#endif
52#ifdef ETH_P_PPP_SES
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053__PF(PPP_SES,ppp_ses)
Eric Andersen0f08e532003-06-20 09:05:00 +000054#endif
55#ifdef ETH_P_ATMMPOA
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056__PF(ATMMPOA,atmmpoa)
Eric Andersen0f08e532003-06-20 09:05:00 +000057#endif
58#ifdef ETH_P_ATMFATE
Eric Andersenc7bda1c2004-03-15 08:29:22 +000059__PF(ATMFATE,atmfate)
Eric Andersen0f08e532003-06-20 09:05:00 +000060#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061
Eric Andersenc7bda1c2004-03-15 08:29:22 +000062__PF(802_3,802_3)
63__PF(AX25,ax25)
64__PF(ALL,all)
65__PF(802_2,802_2)
66__PF(SNAP,snap)
67__PF(DDCMP,ddcmp)
68__PF(WAN_PPP,wan_ppp)
69__PF(PPP_MP,ppp_mp)
70__PF(LOCALTALK,localtalk)
71__PF(PPPTALK,ppptalk)
72__PF(TR_802_2,tr_802_2)
73__PF(MOBITEX,mobitex)
74__PF(CONTROL,control)
75__PF(IRDA,irda)
Eric Andersen0f08e532003-06-20 09:05:00 +000076#ifdef ETH_P_ECONET
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077__PF(ECONET,econet)
Eric Andersen0f08e532003-06-20 09:05:00 +000078#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079
Denys Vlasenko405daff2012-09-02 14:29:45 +0200800x8100,
81ETH_P_IP
Denys Vlasenko5f10fdf2012-09-03 11:41:30 +020082};
Denys Vlasenko405daff2012-09-02 14:29:45 +020083#undef __PF
84
85/* Keep declarations above and below in sync! */
86
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020087static const char llproto_names[] ALIGN1 =
Denys Vlasenko405daff2012-09-02 14:29:45 +020088#define __PF(f,n) #n "\0"
89__PF(LOOP,loop)
90__PF(PUP,pup)
91#ifdef ETH_P_PUPAT
92__PF(PUPAT,pupat)
93#endif
94__PF(IP,ip)
95__PF(X25,x25)
96__PF(ARP,arp)
97__PF(BPQ,bpq)
98#ifdef ETH_P_IEEEPUP
99__PF(IEEEPUP,ieeepup)
100#endif
101#ifdef ETH_P_IEEEPUPAT
102__PF(IEEEPUPAT,ieeepupat)
103#endif
104__PF(DEC,dec)
105__PF(DNA_DL,dna_dl)
106__PF(DNA_RC,dna_rc)
107__PF(DNA_RT,dna_rt)
108__PF(LAT,lat)
109__PF(DIAG,diag)
110__PF(CUST,cust)
111__PF(SCA,sca)
112__PF(RARP,rarp)
113__PF(ATALK,atalk)
114__PF(AARP,aarp)
115__PF(IPX,ipx)
116__PF(IPV6,ipv6)
117#ifdef ETH_P_PPP_DISC
118__PF(PPP_DISC,ppp_disc)
119#endif
120#ifdef ETH_P_PPP_SES
121__PF(PPP_SES,ppp_ses)
122#endif
123#ifdef ETH_P_ATMMPOA
124__PF(ATMMPOA,atmmpoa)
125#endif
126#ifdef ETH_P_ATMFATE
127__PF(ATMFATE,atmfate)
128#endif
129
130__PF(802_3,802_3)
131__PF(AX25,ax25)
132__PF(ALL,all)
133__PF(802_2,802_2)
134__PF(SNAP,snap)
135__PF(DDCMP,ddcmp)
136__PF(WAN_PPP,wan_ppp)
137__PF(PPP_MP,ppp_mp)
138__PF(LOCALTALK,localtalk)
139__PF(PPPTALK,ppptalk)
140__PF(TR_802_2,tr_802_2)
141__PF(MOBITEX,mobitex)
142__PF(CONTROL,control)
143__PF(IRDA,irda)
144#ifdef ETH_P_ECONET
145__PF(ECONET,econet)
146#endif
147
148"802.1Q" "\0"
149"ipv4" "\0"
150;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151#undef __PF
152
153
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200154const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000156 unsigned i;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 id = ntohs(id);
Denys Vlasenko405daff2012-09-02 14:29:45 +0200158 for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
159 if (llproto_ids[i] == id)
160 return nth_string(llproto_names, i);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 }
Denys Vlasenko405daff2012-09-02 14:29:45 +0200162 snprintf(buf, len, "[%u]", id);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000163 return buf;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164}
165
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200166int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000168 unsigned i;
Denys Vlasenko405daff2012-09-02 14:29:45 +0200169 const char *name = llproto_names;
170 for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
171 if (strcasecmp(name, buf) == 0) {
172 i = llproto_ids[i];
173 goto good;
174 }
175 name += strlen(name) + 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000176 }
Denys Vlasenko405daff2012-09-02 14:29:45 +0200177 errno = 0;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000178 i = bb_strtou(buf, NULL, 0);
179 if (errno || i > 0xffff)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 return -1;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000181 good:
182 *id = htons(i);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000183 return 0;
184}