1/* vi: set sw=4 ts=4: */ 2/* 3 * ll_proto.c 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 8 * 2 of the License, or (at your option) any later version. 9 * 10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 11 */ 12 13#include "libbb.h" 14#include "rt_names.h" 15#include "utils.h" 16 17#if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 18#include <net/ethernet.h> 19#else 20#include <linux/if_ether.h> 21#endif 22 23#if !ENABLE_WERROR 24#warning de-bloat 25#endif 26/* Before re-enabling this, please (1) conditionalize exotic protocols 27 * on CONFIG_something, and (2) decouple strings and numbers 28 * (use llproto_ids[] = n,n,n..; and llproto_names[] = "loop\0" "pup\0" ...;) 29 */ 30 31#define __PF(f,n) { ETH_P_##f, #n }, 32static struct { 33 int id; 34 const char *name; 35} llproto_names[] = { 36__PF(LOOP,loop) 37__PF(PUP,pup) 38#ifdef ETH_P_PUPAT 39__PF(PUPAT,pupat) 40#endif 41__PF(IP,ip) 42__PF(X25,x25) 43__PF(ARP,arp) 44__PF(BPQ,bpq) 45#ifdef ETH_P_IEEEPUP 46__PF(IEEEPUP,ieeepup) 47#endif 48#ifdef ETH_P_IEEEPUPAT 49__PF(IEEEPUPAT,ieeepupat) 50#endif 51__PF(DEC,dec) 52__PF(DNA_DL,dna_dl) 53__PF(DNA_RC,dna_rc) 54__PF(DNA_RT,dna_rt) 55__PF(LAT,lat) 56__PF(DIAG,diag) 57__PF(CUST,cust) 58__PF(SCA,sca) 59__PF(RARP,rarp) 60__PF(ATALK,atalk) 61__PF(AARP,aarp) 62__PF(IPX,ipx) 63__PF(IPV6,ipv6) 64#ifdef ETH_P_PPP_DISC 65__PF(PPP_DISC,ppp_disc) 66#endif 67#ifdef ETH_P_PPP_SES 68__PF(PPP_SES,ppp_ses) 69#endif 70#ifdef ETH_P_ATMMPOA 71__PF(ATMMPOA,atmmpoa) 72#endif 73#ifdef ETH_P_ATMFATE 74__PF(ATMFATE,atmfate) 75#endif 76 77__PF(802_3,802_3) 78__PF(AX25,ax25) 79__PF(ALL,all) 80__PF(802_2,802_2) 81__PF(SNAP,snap) 82__PF(DDCMP,ddcmp) 83__PF(WAN_PPP,wan_ppp) 84__PF(PPP_MP,ppp_mp) 85__PF(LOCALTALK,localtalk) 86__PF(PPPTALK,ppptalk) 87__PF(TR_802_2,tr_802_2) 88__PF(MOBITEX,mobitex) 89__PF(CONTROL,control) 90__PF(IRDA,irda) 91#ifdef ETH_P_ECONET 92__PF(ECONET,econet) 93#endif 94 95{ 0x8100, "802.1Q" }, 96{ ETH_P_IP, "ipv4" }, 97}; 98#undef __PF 99 100 101const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len) 102{ 103 unsigned i; 104 id = ntohs(id); 105 for (i = 0; i < ARRAY_SIZE(llproto_names); i++) { 106 if (llproto_names[i].id == id) 107 return llproto_names[i].name; 108 } 109 snprintf(buf, len, "[%d]", id); 110 return buf; 111} 112 113int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf) 114{ 115 unsigned i; 116 for (i = 0; i < ARRAY_SIZE(llproto_names); i++) { 117 if (strcasecmp(llproto_names[i].name, buf) == 0) { 118 i = llproto_names[i].id; 119 goto good; 120 } 121 } 122 i = bb_strtou(buf, NULL, 0); 123 if (errno || i > 0xffff) 124 return -1; 125 good: 126 *id = htons(i); 127 return 0; 128} 129 130