Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * Point-to-point protocol support |
| 4 | * |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 5 | * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/hdlc.h> |
Krzysztof HaĆasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame^] | 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> |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | static int ppp_open(struct net_device *dev) |
| 42 | { |
| 43 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Al Viro | 9045840 | 2007-12-22 17:52:52 +0000 | [diff] [blame] | 44 | int (*old_ioctl)(struct net_device *, struct ifreq *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | int result; |
| 46 | |
David S. Miller | 4951704 | 2008-05-12 03:29:11 -0700 | [diff] [blame] | 47 | dev->ml_priv = &state(hdlc)->syncppp_ptr; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 48 | state(hdlc)->syncppp_ptr = &state(hdlc)->pppdev; |
| 49 | state(hdlc)->pppdev.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | old_ioctl = dev->do_ioctl; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 52 | state(hdlc)->old_change_mtu = dev->change_mtu; |
| 53 | sppp_attach(&state(hdlc)->pppdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* sppp_attach nukes them. We don't need syncppp's ioctl */ |
| 55 | dev->do_ioctl = old_ioctl; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 56 | state(hdlc)->pppdev.sppp.pp_flags &= ~PP_CISCO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 75 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 76 | dev->change_mtu = state(hdlc)->old_change_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | dev->mtu = HDLC_MAX_MTU; |
| 78 | dev->hard_header_len = 16; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
Alexey Dobriyan | ab61148 | 2005-07-12 12:08:43 -0700 | [diff] [blame] | 83 | static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | return __constant_htons(ETH_P_WAN_PPP); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 102 | int result; |
| 103 | |
| 104 | switch (ifr->ifr_settings.type) { |
| 105 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 106 | if (dev_to_hdlc(dev)->proto != &proto) |
| 107 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 124 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 125 | sizeof(struct ppp_state)); |
| 126 | if (result) |
| 127 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | dev->hard_start_xmit = hdlc->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | dev->type = ARPHRD_PPP; |
Krzysztof Halasa | 4bc83b4 | 2006-07-21 14:41:01 -0700 | [diff] [blame] | 130 | netif_dormant_off(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | return -EINVAL; |
| 135 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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"); |