ll_proto.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "libbb.h"
  13. #include "rt_names.h"
  14. #include "utils.h"
  15. #if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1
  16. #include <net/ethernet.h>
  17. #else
  18. #include <linux/if_ether.h>
  19. #endif
  20. #if !ENABLE_WERROR
  21. #warning de-bloat
  22. #endif
  23. /* Before re-enabling this, please (1) conditionalize exotic protocols
  24. * on CONFIG_something, and (2) decouple strings and numbers
  25. * (use llproto_ids[] = n,n,n..; and llproto_names[] = "loop\0" "pup\0" ...;)
  26. */
  27. #define __PF(f,n) { ETH_P_##f, #n },
  28. static struct {
  29. int id;
  30. const char *name;
  31. } llproto_names[] = {
  32. __PF(LOOP,loop)
  33. __PF(PUP,pup)
  34. #ifdef ETH_P_PUPAT
  35. __PF(PUPAT,pupat)
  36. #endif
  37. __PF(IP,ip)
  38. __PF(X25,x25)
  39. __PF(ARP,arp)
  40. __PF(BPQ,bpq)
  41. #ifdef ETH_P_IEEEPUP
  42. __PF(IEEEPUP,ieeepup)
  43. #endif
  44. #ifdef ETH_P_IEEEPUPAT
  45. __PF(IEEEPUPAT,ieeepupat)
  46. #endif
  47. __PF(DEC,dec)
  48. __PF(DNA_DL,dna_dl)
  49. __PF(DNA_RC,dna_rc)
  50. __PF(DNA_RT,dna_rt)
  51. __PF(LAT,lat)
  52. __PF(DIAG,diag)
  53. __PF(CUST,cust)
  54. __PF(SCA,sca)
  55. __PF(RARP,rarp)
  56. __PF(ATALK,atalk)
  57. __PF(AARP,aarp)
  58. __PF(IPX,ipx)
  59. __PF(IPV6,ipv6)
  60. #ifdef ETH_P_PPP_DISC
  61. __PF(PPP_DISC,ppp_disc)
  62. #endif
  63. #ifdef ETH_P_PPP_SES
  64. __PF(PPP_SES,ppp_ses)
  65. #endif
  66. #ifdef ETH_P_ATMMPOA
  67. __PF(ATMMPOA,atmmpoa)
  68. #endif
  69. #ifdef ETH_P_ATMFATE
  70. __PF(ATMFATE,atmfate)
  71. #endif
  72. __PF(802_3,802_3)
  73. __PF(AX25,ax25)
  74. __PF(ALL,all)
  75. __PF(802_2,802_2)
  76. __PF(SNAP,snap)
  77. __PF(DDCMP,ddcmp)
  78. __PF(WAN_PPP,wan_ppp)
  79. __PF(PPP_MP,ppp_mp)
  80. __PF(LOCALTALK,localtalk)
  81. __PF(PPPTALK,ppptalk)
  82. __PF(TR_802_2,tr_802_2)
  83. __PF(MOBITEX,mobitex)
  84. __PF(CONTROL,control)
  85. __PF(IRDA,irda)
  86. #ifdef ETH_P_ECONET
  87. __PF(ECONET,econet)
  88. #endif
  89. { 0x8100, "802.1Q" },
  90. { ETH_P_IP, "ipv4" },
  91. };
  92. #undef __PF
  93. const char *ll_proto_n2a(unsigned short id, char *buf, int len)
  94. {
  95. unsigned i;
  96. id = ntohs(id);
  97. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  98. if (llproto_names[i].id == id)
  99. return llproto_names[i].name;
  100. }
  101. snprintf(buf, len, "[%d]", id);
  102. return buf;
  103. }
  104. int ll_proto_a2n(unsigned short *id, char *buf)
  105. {
  106. unsigned i;
  107. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  108. if (strcasecmp(llproto_names[i].name, buf) == 0) {
  109. *id = htons(llproto_names[i].id);
  110. return 0;
  111. }
  112. }
  113. if (get_u16(id, buf, 0))
  114. return -1;
  115. *id = htons(*id);
  116. return 0;
  117. }