blob: 4efe9e6d32d5fb5b37b7397a77d4898e3e7db4f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * Point-to-point protocol support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hdlc.h>
Krzysztof HaƂasa4dfce402008-06-30 19:06:40 +020014#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>
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020024#include <net/syncppp.h>
25
26struct 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
32static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
33
34
35static inline struct ppp_state* state(hdlc_device *hdlc)
36{
37 return(struct ppp_state *)(hdlc->state);
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
41static int ppp_open(struct net_device *dev)
42{
43 hdlc_device *hdlc = dev_to_hdlc(dev);
Al Viro90458402007-12-22 17:52:52 +000044 int (*old_ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 int result;
46
David S. Miller49517042008-05-12 03:29:11 -070047 dev->ml_priv = &state(hdlc)->syncppp_ptr;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020048 state(hdlc)->syncppp_ptr = &state(hdlc)->pppdev;
49 state(hdlc)->pppdev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 old_ioctl = dev->do_ioctl;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020052 state(hdlc)->old_change_mtu = dev->change_mtu;
53 sppp_attach(&state(hdlc)->pppdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* sppp_attach nukes them. We don't need syncppp's ioctl */
55 dev->do_ioctl = old_ioctl;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020056 state(hdlc)->pppdev.sppp.pp_flags &= ~PP_CISCO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 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
69static 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);
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -070075
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020076 dev->change_mtu = state(hdlc)->old_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 dev->mtu = HDLC_MAX_MTU;
78 dev->hard_header_len = 16;
79}
80
81
82
Alexey Dobriyanab611482005-07-12 12:08:43 -070083static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 return __constant_htons(ETH_P_WAN_PPP);
86}
87
88
89
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020090static 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
99static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 hdlc_device *hdlc = dev_to_hdlc(dev);
102 int result;
103
104 switch (ifr->ifr_settings.type) {
105 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200106 if (dev_to_hdlc(dev)->proto != &proto)
107 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100124 result = attach_hdlc_protocol(dev, &proto,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200125 sizeof(struct ppp_state));
126 if (result)
127 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dev->hard_start_xmit = hdlc->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 dev->type = ARPHRD_PPP;
Krzysztof Halasa4bc83b42006-07-21 14:41:01 -0700130 netif_dormant_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132 }
133
134 return -EINVAL;
135}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200136
137
138static int __init mod_init(void)
139{
140 register_hdlc_protocol(&proto);
141 return 0;
142}
143
144
145
146static void __exit mod_exit(void)
147{
148 unregister_hdlc_protocol(&proto);
149}
150
151
152module_init(mod_init);
153module_exit(mod_exit);
154
155MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
156MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
157MODULE_LICENSE("GPL v2");