blob: 9b5260b3285a5ea95796be1a4d56653a681a6df5 [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * ll_proto.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12#include <stdio.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include <arpa/inet.h>
14#include <string.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "utils.h"
16
Eric Andersenfeb283a2003-07-14 08:49:43 +000017#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
18#include <net/ethernet.h>
19#else
20#include <linux/if_ether.h>
21#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022
23#define __PF(f,n) { ETH_P_##f, #n },
24static struct {
25 int id;
26 char *name;
27} llproto_names[] = {
28__PF(LOOP,loop)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029__PF(PUP,pup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030#ifdef ETH_P_PUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000031__PF(PUPAT,pupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032#endif
33__PF(IP,ip)
34__PF(X25,x25)
35__PF(ARP,arp)
36__PF(BPQ,bpq)
37#ifdef ETH_P_IEEEPUP
Eric Andersenc7bda1c2004-03-15 08:29:22 +000038__PF(IEEEPUP,ieeepup)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039#endif
40#ifdef ETH_P_IEEEPUPAT
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041__PF(IEEEPUPAT,ieeepupat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043__PF(DEC,dec)
44__PF(DNA_DL,dna_dl)
45__PF(DNA_RC,dna_rc)
46__PF(DNA_RT,dna_rt)
47__PF(LAT,lat)
48__PF(DIAG,diag)
49__PF(CUST,cust)
50__PF(SCA,sca)
51__PF(RARP,rarp)
52__PF(ATALK,atalk)
53__PF(AARP,aarp)
54__PF(IPX,ipx)
55__PF(IPV6,ipv6)
Eric Andersen0f08e532003-06-20 09:05:00 +000056#ifdef ETH_P_PPP_DISC
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057__PF(PPP_DISC,ppp_disc)
Eric Andersen0f08e532003-06-20 09:05:00 +000058#endif
59#ifdef ETH_P_PPP_SES
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060__PF(PPP_SES,ppp_ses)
Eric Andersen0f08e532003-06-20 09:05:00 +000061#endif
62#ifdef ETH_P_ATMMPOA
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063__PF(ATMMPOA,atmmpoa)
Eric Andersen0f08e532003-06-20 09:05:00 +000064#endif
65#ifdef ETH_P_ATMFATE
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066__PF(ATMFATE,atmfate)
Eric Andersen0f08e532003-06-20 09:05:00 +000067#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068
Eric Andersenc7bda1c2004-03-15 08:29:22 +000069__PF(802_3,802_3)
70__PF(AX25,ax25)
71__PF(ALL,all)
72__PF(802_2,802_2)
73__PF(SNAP,snap)
74__PF(DDCMP,ddcmp)
75__PF(WAN_PPP,wan_ppp)
76__PF(PPP_MP,ppp_mp)
77__PF(LOCALTALK,localtalk)
78__PF(PPPTALK,ppptalk)
79__PF(TR_802_2,tr_802_2)
80__PF(MOBITEX,mobitex)
81__PF(CONTROL,control)
82__PF(IRDA,irda)
Eric Andersen0f08e532003-06-20 09:05:00 +000083#ifdef ETH_P_ECONET
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084__PF(ECONET,econet)
Eric Andersen0f08e532003-06-20 09:05:00 +000085#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086
87{ 0x8100, "802.1Q" },
88{ ETH_P_IP, "ipv4" },
89};
90#undef __PF
91
92
93char * ll_proto_n2a(unsigned short id, char *buf, int len)
94{
95 int i;
96
97 id = ntohs(id);
98
99 for (i=0; i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
100 if (llproto_names[i].id == id)
101 return llproto_names[i].name;
102 }
103 snprintf(buf, len, "[%d]", id);
104 return buf;
105}
106
107int ll_proto_a2n(unsigned short *id, char *buf)
108{
109 int i;
110 for (i=0; i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
111 if (strcasecmp(llproto_names[i].name, buf) == 0) {
112 *id = htons(llproto_names[i].id);
113 return 0;
114 }
115 }
116 if (get_u16(id, buf, 0))
117 return -1;
118 *id = htons(*id);
119 return 0;
120}