1/*
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>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
17#include <sys/ioctl.h>
18#include <sys/socket.h>
19#include <sys/ioctl.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23
24#include <linux/netdevice.h>
25#include <linux/if_arp.h>
26#include <linux/sockios.h>
27
28#include "utils.h"
29#include "rt_names.h"
30
31
32#define __PF(f,n) { ETH_P_##f, #n },
33static struct {
34	int id;
35	const char *name;
36} llproto_names[] = {
37__PF(LOOP,loop)
38__PF(PUP,pup)
39#ifdef ETH_P_PUPAT
40__PF(PUPAT,pupat)
41#endif
42__PF(IP,ip)
43__PF(X25,x25)
44__PF(ARP,arp)
45__PF(BPQ,bpq)
46#ifdef ETH_P_IEEEPUP
47__PF(IEEEPUP,ieeepup)
48#endif
49#ifdef ETH_P_IEEEPUPAT
50__PF(IEEEPUPAT,ieeepupat)
51#endif
52__PF(DEC,dec)
53__PF(DNA_DL,dna_dl)
54__PF(DNA_RC,dna_rc)
55__PF(DNA_RT,dna_rt)
56__PF(LAT,lat)
57__PF(DIAG,diag)
58__PF(CUST,cust)
59__PF(SCA,sca)
60__PF(RARP,rarp)
61__PF(ATALK,atalk)
62__PF(AARP,aarp)
63__PF(IPX,ipx)
64__PF(IPV6,ipv6)
65#ifdef ETH_P_PPP_DISC
66__PF(PPP_DISC,ppp_disc)
67#endif
68#ifdef ETH_P_PPP_SES
69__PF(PPP_SES,ppp_ses)
70#endif
71#ifdef ETH_P_ATMMPOA
72__PF(ATMMPOA,atmmpoa)
73#endif
74#ifdef ETH_P_ATMFATE
75__PF(ATMFATE,atmfate)
76#endif
77
78__PF(802_3,802_3)
79__PF(AX25,ax25)
80__PF(ALL,all)
81__PF(802_2,802_2)
82__PF(SNAP,snap)
83__PF(DDCMP,ddcmp)
84__PF(WAN_PPP,wan_ppp)
85__PF(PPP_MP,ppp_mp)
86__PF(LOCALTALK,localtalk)
87__PF(CAN,can)
88__PF(PPPTALK,ppptalk)
89__PF(TR_802_2,tr_802_2)
90__PF(MOBITEX,mobitex)
91__PF(CONTROL,control)
92__PF(IRDA,irda)
93#ifdef ETH_P_ECONET
94__PF(ECONET,econet)
95#endif
96__PF(TIPC,tipc)
97__PF(AOE,aoe)
98
99{ 0x8100, "802.1Q" },
100{ ETH_P_IP, "ipv4" },
101};
102#undef __PF
103
104
105const char * ll_proto_n2a(unsigned short id, char *buf, int len)
106{
107        int i;
108
109	id = ntohs(id);
110
111        for (i=0; i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
112                 if (llproto_names[i].id == id)
113			return llproto_names[i].name;
114	}
115        snprintf(buf, len, "[%d]", id);
116        return buf;
117}
118
119int ll_proto_a2n(unsigned short *id, char *buf)
120{
121        int i;
122        for (i=0; i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
123                 if (strcasecmp(llproto_names[i].name, buf) == 0) {
124			 *id = htons(llproto_names[i].id);
125			 return 0;
126		 }
127	}
128	if (get_u16(id, buf, 0))
129		return -1;
130	*id = htons(*id);
131	return 0;
132}
133