Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[linux-2.6] / drivers / net / wan / hdlc_ppp.c
1 /*
2  * Generic HDLC support routines for Linux
3  * Point-to-point protocol support
4  *
5  * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License
9  * as published by the Free Software Foundation.
10  */
11
12 #include <linux/errno.h>
13 #include <linux/hdlc.h>
14 #include <linux/if_arp.h>
15 #include <linux/inetdevice.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pkt_sched.h>
20 #include <linux/poll.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/skbuff.h>
23 #include <linux/slab.h>
24 #include <net/syncppp.h>
25
26 struct ppp_state {
27         struct ppp_device pppdev;
28         struct ppp_device *syncppp_ptr;
29         int (*old_change_mtu)(struct net_device *dev, int new_mtu);
30 };
31
32 static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
33
34
35 static inline struct ppp_state* state(hdlc_device *hdlc)
36 {
37         return(struct ppp_state *)(hdlc->state);
38 }
39
40
41 static int ppp_open(struct net_device *dev)
42 {
43         hdlc_device *hdlc = dev_to_hdlc(dev);
44         int (*old_ioctl)(struct net_device *, struct ifreq *, int);
45         int result;
46
47         dev->ml_priv = &state(hdlc)->syncppp_ptr;
48         state(hdlc)->syncppp_ptr = &state(hdlc)->pppdev;
49         state(hdlc)->pppdev.dev = dev;
50
51         old_ioctl = dev->do_ioctl;
52         state(hdlc)->old_change_mtu = dev->change_mtu;
53         sppp_attach(&state(hdlc)->pppdev);
54         /* sppp_attach nukes them. We don't need syncppp's ioctl */
55         dev->do_ioctl = old_ioctl;
56         state(hdlc)->pppdev.sppp.pp_flags &= ~PP_CISCO;
57         dev->type = ARPHRD_PPP;
58         result = sppp_open(dev);
59         if (result) {
60                 sppp_detach(dev);
61                 return result;
62         }
63
64         return 0;
65 }
66
67
68
69 static void ppp_close(struct net_device *dev)
70 {
71         hdlc_device *hdlc = dev_to_hdlc(dev);
72
73         sppp_close(dev);
74         sppp_detach(dev);
75
76         dev->change_mtu = state(hdlc)->old_change_mtu;
77         dev->mtu = HDLC_MAX_MTU;
78         dev->hard_header_len = 16;
79 }
80
81
82
83 static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
84 {
85         return __constant_htons(ETH_P_WAN_PPP);
86 }
87
88
89
90 static struct hdlc_proto proto = {
91         .open           = ppp_open,
92         .close          = ppp_close,
93         .type_trans     = ppp_type_trans,
94         .ioctl          = ppp_ioctl,
95         .module         = THIS_MODULE,
96 };
97
98
99 static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
100 {
101         hdlc_device *hdlc = dev_to_hdlc(dev);
102         int result;
103
104         switch (ifr->ifr_settings.type) {
105         case IF_GET_PROTO:
106                 if (dev_to_hdlc(dev)->proto != &proto)
107                         return -EINVAL;
108                 ifr->ifr_settings.type = IF_PROTO_PPP;
109                 return 0; /* return protocol only, no settable parameters */
110
111         case IF_PROTO_PPP:
112                 if(!capable(CAP_NET_ADMIN))
113                         return -EPERM;
114
115                 if(dev->flags & IFF_UP)
116                         return -EBUSY;
117
118                 /* no settable parameters */
119
120                 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
121                 if (result)
122                         return result;
123
124                 result = attach_hdlc_protocol(dev, &proto,
125                                               sizeof(struct ppp_state));
126                 if (result)
127                         return result;
128                 dev->hard_start_xmit = hdlc->xmit;
129                 dev->type = ARPHRD_PPP;
130                 netif_dormant_off(dev);
131                 return 0;
132         }
133
134         return -EINVAL;
135 }
136
137
138 static int __init mod_init(void)
139 {
140         register_hdlc_protocol(&proto);
141         return 0;
142 }
143
144
145
146 static void __exit mod_exit(void)
147 {
148         unregister_hdlc_protocol(&proto);
149 }
150
151
152 module_init(mod_init);
153 module_exit(mod_exit);
154
155 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
156 MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
157 MODULE_LICENSE("GPL v2");