1
2
3
4
5
6
7
8
9
10#include "libbb.h"
11#include "rt_names.h"
12#include "utils.h"
13
14#include <netinet/if_ether.h>
15
16
17
18static const uint16_t llproto_ids[] ALIGN2 = {
19#define __PF(f,n) ETH_P_##f,
20__PF(LOOP,loop)
21__PF(PUP,pup)
22#ifdef ETH_P_PUPAT
23__PF(PUPAT,pupat)
24#endif
25__PF(IP,ip)
26__PF(X25,x25)
27__PF(ARP,arp)
28__PF(BPQ,bpq)
29#ifdef ETH_P_IEEEPUP
30__PF(IEEEPUP,ieeepup)
31#endif
32#ifdef ETH_P_IEEEPUPAT
33__PF(IEEEPUPAT,ieeepupat)
34#endif
35__PF(DEC,dec)
36__PF(DNA_DL,dna_dl)
37__PF(DNA_RC,dna_rc)
38__PF(DNA_RT,dna_rt)
39__PF(LAT,lat)
40__PF(DIAG,diag)
41__PF(CUST,cust)
42__PF(SCA,sca)
43__PF(RARP,rarp)
44__PF(ATALK,atalk)
45__PF(AARP,aarp)
46__PF(IPX,ipx)
47__PF(IPV6,ipv6)
48#ifdef ETH_P_PPP_DISC
49__PF(PPP_DISC,ppp_disc)
50#endif
51#ifdef ETH_P_PPP_SES
52__PF(PPP_SES,ppp_ses)
53#endif
54#ifdef ETH_P_ATMMPOA
55__PF(ATMMPOA,atmmpoa)
56#endif
57#ifdef ETH_P_ATMFATE
58__PF(ATMFATE,atmfate)
59#endif
60
61__PF(802_3,802_3)
62__PF(AX25,ax25)
63__PF(ALL,all)
64__PF(802_2,802_2)
65__PF(SNAP,snap)
66__PF(DDCMP,ddcmp)
67__PF(WAN_PPP,wan_ppp)
68__PF(PPP_MP,ppp_mp)
69__PF(LOCALTALK,localtalk)
70__PF(PPPTALK,ppptalk)
71__PF(TR_802_2,tr_802_2)
72__PF(MOBITEX,mobitex)
73__PF(CONTROL,control)
74__PF(IRDA,irda)
75#ifdef ETH_P_ECONET
76__PF(ECONET,econet)
77#endif
78
790x8100,
80ETH_P_IP
81};
82#undef __PF
83
84
85
86static const char llproto_names[] ALIGN1 =
87#define __PF(f,n) #n "\0"
88__PF(LOOP,loop)
89__PF(PUP,pup)
90#ifdef ETH_P_PUPAT
91__PF(PUPAT,pupat)
92#endif
93__PF(IP,ip)
94__PF(X25,x25)
95__PF(ARP,arp)
96__PF(BPQ,bpq)
97#ifdef ETH_P_IEEEPUP
98__PF(IEEEPUP,ieeepup)
99#endif
100#ifdef ETH_P_IEEEPUPAT
101__PF(IEEEPUPAT,ieeepupat)
102#endif
103__PF(DEC,dec)
104__PF(DNA_DL,dna_dl)
105__PF(DNA_RC,dna_rc)
106__PF(DNA_RT,dna_rt)
107__PF(LAT,lat)
108__PF(DIAG,diag)
109__PF(CUST,cust)
110__PF(SCA,sca)
111__PF(RARP,rarp)
112__PF(ATALK,atalk)
113__PF(AARP,aarp)
114__PF(IPX,ipx)
115__PF(IPV6,ipv6)
116#ifdef ETH_P_PPP_DISC
117__PF(PPP_DISC,ppp_disc)
118#endif
119#ifdef ETH_P_PPP_SES
120__PF(PPP_SES,ppp_ses)
121#endif
122#ifdef ETH_P_ATMMPOA
123__PF(ATMMPOA,atmmpoa)
124#endif
125#ifdef ETH_P_ATMFATE
126__PF(ATMFATE,atmfate)
127#endif
128
129__PF(802_3,802_3)
130__PF(AX25,ax25)
131__PF(ALL,all)
132__PF(802_2,802_2)
133__PF(SNAP,snap)
134__PF(DDCMP,ddcmp)
135__PF(WAN_PPP,wan_ppp)
136__PF(PPP_MP,ppp_mp)
137__PF(LOCALTALK,localtalk)
138__PF(PPPTALK,ppptalk)
139__PF(TR_802_2,tr_802_2)
140__PF(MOBITEX,mobitex)
141__PF(CONTROL,control)
142__PF(IRDA,irda)
143#ifdef ETH_P_ECONET
144__PF(ECONET,econet)
145#endif
146
147"802.1Q" "\0"
148"ipv4" "\0"
149;
150#undef __PF
151
152
153const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
154{
155 unsigned i;
156 id = ntohs(id);
157 for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
158 if (llproto_ids[i] == id)
159 return nth_string(llproto_names, i);
160 }
161 snprintf(buf, len, "[%u]", id);
162 return buf;
163}
164
165int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
166{
167 unsigned i;
168 const char *name = llproto_names;
169 for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
170 if (strcasecmp(name, buf) == 0) {
171 i = llproto_ids[i];
172 goto good;
173 }
174 name += strlen(name) + 1;
175 }
176 errno = 0;
177 i = bb_strtou(buf, NULL, 0);
178 if (errno || i > 0xffff)
179 return -1;
180 good:
181 *id = htons(i);
182 return 0;
183}
184