blob: 611e0a2c9f2f89b172d7120652915fd232c978a2 [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 */
Rob Landleyea224be2006-06-18 20:20:07 +000010#include "libbb.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000011#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012#include "utils.h"
13
Dan Fandrichf533ec82011-06-10 05:17:59 +020014#include <netinet/if_ether.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Denys Vlasenko405daff2012-09-02 14:29:45 +020016/* Please conditionalize exotic protocols on CONFIG_something */
Denis Vlasenkodfc07402007-10-29 19:33:26 +000017
Denys Vlasenko84fc6452019-05-21 17:29:24 +020018static const uint16_t llproto_ids[] ALIGN2 = {
Denys Vlasenko405daff2012-09-02 14:29:45 +020019#define __PF(f,n) ETH_P_##f,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020__PF(LOOP,loop)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000021__PF(PUP,pup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022#ifdef ETH_P_PUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000023__PF(PUPAT,pupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024#endif
25__PF(IP,ip)
26__PF(X25,x25)
27__PF(ARP,arp)
28__PF(BPQ,bpq)
29#ifdef ETH_P_IEEEPUP
Eric Andersenc7bda1c2004-03-15 08:29:22 +000030__PF(IEEEPUP,ieeepup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031#endif
32#ifdef ETH_P_IEEEPUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033__PF(IEEEPUPAT,ieeepupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000035__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)
Eric Andersen0f08e532003-06-20 09:05:00 +000048#ifdef ETH_P_PPP_DISC
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049__PF(PPP_DISC,ppp_disc)
Eric Andersen0f08e532003-06-20 09:05:00 +000050#endif
51#ifdef ETH_P_PPP_SES
Eric Andersenc7bda1c2004-03-15 08:29:22 +000052__PF(PPP_SES,ppp_ses)
Eric Andersen0f08e532003-06-20 09:05:00 +000053#endif
54#ifdef ETH_P_ATMMPOA
Eric Andersenc7bda1c2004-03-15 08:29:22 +000055__PF(ATMMPOA,atmmpoa)
Eric Andersen0f08e532003-06-20 09:05:00 +000056#endif
57#ifdef ETH_P_ATMFATE
Eric Andersenc7bda1c2004-03-15 08:29:22 +000058__PF(ATMFATE,atmfate)
Eric Andersen0f08e532003-06-20 09:05:00 +000059#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061__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)
Eric Andersen0f08e532003-06-20 09:05:00 +000075#ifdef ETH_P_ECONET
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076__PF(ECONET,econet)
Eric Andersen0f08e532003-06-20 09:05:00 +000077#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078
Denys Vlasenko405daff2012-09-02 14:29:45 +0200790x8100,
80ETH_P_IP
Denys Vlasenko5f10fdf2012-09-03 11:41:30 +020081};
Denys Vlasenko405daff2012-09-02 14:29:45 +020082#undef __PF
83
84/* Keep declarations above and below in sync! */
85
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020086static const char llproto_names[] ALIGN1 =
Denys Vlasenko405daff2012-09-02 14:29:45 +020087#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;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150#undef __PF
151
152
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200153const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000155 unsigned i;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156 id = ntohs(id);
Denys Vlasenko405daff2012-09-02 14:29:45 +0200157 for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
158 if (llproto_ids[i] == id)
159 return nth_string(llproto_names, i);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160 }
Denys Vlasenko405daff2012-09-02 14:29:45 +0200161 snprintf(buf, len, "[%u]", id);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000162 return buf;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163}
164
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200165int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000167 unsigned i;
Denys Vlasenko405daff2012-09-02 14:29:45 +0200168 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;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 }
Denys Vlasenko405daff2012-09-02 14:29:45 +0200176 errno = 0;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000177 i = bb_strtou(buf, NULL, 0);
178 if (errno || i > 0xffff)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179 return -1;
Denis Vlasenko76140a72009-03-05 09:21:57 +0000180 good:
181 *id = htons(i);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 return 0;
183}