ll_proto.c 3.3 KB

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