Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 4 | * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * Currently supported: |
| 11 | * * raw IP-in-HDLC |
| 12 | * * Cisco HDLC |
| 13 | * * Frame Relay with ANSI or CCITT LMI (both user and network side) |
| 14 | * * PPP |
| 15 | * * X.25 |
| 16 | * |
| 17 | * Use sethdlc utility to set line parameters, protocol and PVCs |
| 18 | * |
| 19 | * How does it work: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 20 | * - proto->open(), close(), start(), stop() calls are serialized. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * The order is: open, [ start, stop ... ] close ... |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 22 | * - proto->start() and stop() are called with spin_lock_irq held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/hdlc.h> |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 27 | #include <linux/if_arp.h> |
| 28 | #include <linux/inetdevice.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/notifier.h> |
| 33 | #include <linux/pkt_sched.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/rtnetlink.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | #include <linux/slab.h> |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 38 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 41 | static const char* version = "HDLC support module revision 1.22"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #undef DEBUG_LINK |
| 44 | |
Krzysztof Halasa | fa701bd | 2008-05-19 19:00:51 +0200 | [diff] [blame] | 45 | static struct hdlc_proto *first_proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | static int hdlc_change_mtu(struct net_device *dev, int new_mtu) |
| 48 | { |
| 49 | if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU)) |
| 50 | return -EINVAL; |
| 51 | dev->mtu = new_mtu; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | static struct net_device_stats *hdlc_get_stats(struct net_device *dev) |
| 58 | { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 59 | return &dev->stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 65 | struct packet_type *p, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 67 | struct hdlc_device *hdlc = dev_to_hdlc(dev); |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 68 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 69 | if (dev_net(dev) != &init_net) { |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 70 | kfree_skb(skb); |
| 71 | return 0; |
| 72 | } |
| 73 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 74 | BUG_ON(!hdlc->proto->netif_rx); |
| 75 | return hdlc->proto->netif_rx(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 80 | static inline void hdlc_proto_start(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 83 | if (hdlc->proto->start) |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 84 | hdlc->proto->start(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 89 | static inline void hdlc_proto_stop(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 92 | if (hdlc->proto->stop) |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 93 | hdlc->proto->stop(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 98 | static int hdlc_device_event(struct notifier_block *this, unsigned long event, |
| 99 | void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 101 | struct net_device *dev = ptr; |
| 102 | hdlc_device *hdlc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unsigned long flags; |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 104 | int on; |
| 105 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 106 | if (dev_net(dev) != &init_net) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 107 | return NOTIFY_DONE; |
| 108 | |
Krzysztof Hałasa | 7cdc15f | 2009-01-08 19:46:54 +0100 | [diff] [blame^] | 109 | if (!(dev->priv_flags & IFF_WAN_HDLC)) |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 110 | return NOTIFY_DONE; /* not an HDLC device */ |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 111 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 112 | if (event != NETDEV_CHANGE) |
| 113 | return NOTIFY_DONE; /* Only interrested in carrier changes */ |
| 114 | |
| 115 | on = netif_carrier_ok(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | #ifdef DEBUG_LINK |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 118 | printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n", |
| 119 | dev->name, on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | #endif |
| 121 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 122 | hdlc = dev_to_hdlc(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | spin_lock_irqsave(&hdlc->state_lock, flags); |
| 124 | |
| 125 | if (hdlc->carrier == on) |
| 126 | goto carrier_exit; /* no change in DCD line level */ |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | hdlc->carrier = on; |
| 129 | |
| 130 | if (!hdlc->open) |
| 131 | goto carrier_exit; |
| 132 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 133 | if (hdlc->carrier) { |
| 134 | printk(KERN_INFO "%s: Carrier detected\n", dev->name); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 135 | hdlc_proto_start(dev); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 136 | } else { |
| 137 | printk(KERN_INFO "%s: Carrier lost\n", dev->name); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 138 | hdlc_proto_stop(dev); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | carrier_exit: |
| 142 | spin_unlock_irqrestore(&hdlc->state_lock, flags); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 143 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | /* Must be called by hardware driver when HDLC device is being opened */ |
| 149 | int hdlc_open(struct net_device *dev) |
| 150 | { |
| 151 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 152 | #ifdef DEBUG_LINK |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 153 | printk(KERN_DEBUG "%s: hdlc_open() carrier %i open %i\n", dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | hdlc->carrier, hdlc->open); |
| 155 | #endif |
| 156 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 157 | if (hdlc->proto == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return -ENOSYS; /* no protocol attached */ |
| 159 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 160 | if (hdlc->proto->open) { |
| 161 | int result = hdlc->proto->open(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (result) |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | spin_lock_irq(&hdlc->state_lock); |
| 167 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 168 | if (hdlc->carrier) { |
| 169 | printk(KERN_INFO "%s: Carrier detected\n", dev->name); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 170 | hdlc_proto_start(dev); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 171 | } else |
| 172 | printk(KERN_INFO "%s: No carrier\n", dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | hdlc->open = 1; |
| 175 | |
| 176 | spin_unlock_irq(&hdlc->state_lock); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | /* Must be called by hardware driver when HDLC device is being closed */ |
| 183 | void hdlc_close(struct net_device *dev) |
| 184 | { |
| 185 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 186 | #ifdef DEBUG_LINK |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 187 | printk(KERN_DEBUG "%s: hdlc_close() carrier %i open %i\n", dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | hdlc->carrier, hdlc->open); |
| 189 | #endif |
| 190 | |
| 191 | spin_lock_irq(&hdlc->state_lock); |
| 192 | |
| 193 | hdlc->open = 0; |
| 194 | if (hdlc->carrier) |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 195 | hdlc_proto_stop(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | spin_unlock_irq(&hdlc->state_lock); |
| 198 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 199 | if (hdlc->proto->close) |
| 200 | hdlc->proto->close(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 206 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 207 | struct hdlc_proto *proto = first_proto; |
| 208 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | if (cmd != SIOCWANDEV) |
| 211 | return -EINVAL; |
| 212 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 213 | if (dev_to_hdlc(dev)->proto) { |
| 214 | result = dev_to_hdlc(dev)->proto->ioctl(dev, ifr); |
| 215 | if (result != -EINVAL) |
| 216 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 219 | /* Not handled by currently attached protocol (if any) */ |
| 220 | |
| 221 | while (proto) { |
| 222 | if ((result = proto->ioctl(dev, ifr)) != -EINVAL) |
| 223 | return result; |
| 224 | proto = proto->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 226 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 229 | static const struct header_ops hdlc_null_ops; |
| 230 | |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 231 | static void hdlc_setup_dev(struct net_device *dev) |
| 232 | { |
| 233 | /* Re-init all variables changed by HDLC protocol drivers, |
| 234 | * including ether_setup() called from hdlc_raw_eth.c. |
| 235 | */ |
| 236 | dev->get_stats = hdlc_get_stats; |
| 237 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; |
Krzysztof Hałasa | 7cdc15f | 2009-01-08 19:46:54 +0100 | [diff] [blame^] | 238 | dev->priv_flags = IFF_WAN_HDLC; |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 239 | dev->mtu = HDLC_MAX_MTU; |
| 240 | dev->type = ARPHRD_RAWHDLC; |
| 241 | dev->hard_header_len = 16; |
| 242 | dev->addr_len = 0; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 243 | dev->header_ops = &hdlc_null_ops; |
| 244 | |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 245 | dev->change_mtu = hdlc_change_mtu; |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Adrian Bunk | 0bf94fa | 2007-01-11 14:48:59 +0100 | [diff] [blame] | 248 | static void hdlc_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 251 | |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 252 | hdlc_setup_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | hdlc->carrier = 1; |
| 254 | hdlc->open = 0; |
| 255 | spin_lock_init(&hdlc->state_lock); |
| 256 | } |
| 257 | |
| 258 | struct net_device *alloc_hdlcdev(void *priv) |
| 259 | { |
| 260 | struct net_device *dev; |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 261 | dev = alloc_netdev(sizeof(struct hdlc_device), "hdlc%d", hdlc_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (dev) |
| 263 | dev_to_hdlc(dev)->priv = priv; |
| 264 | return dev; |
| 265 | } |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | void unregister_hdlc_device(struct net_device *dev) |
| 268 | { |
| 269 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | unregister_netdevice(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 271 | detach_hdlc_protocol(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | rtnl_unlock(); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 277 | int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 278 | size_t size) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 279 | { |
| 280 | detach_hdlc_protocol(dev); |
| 281 | |
| 282 | if (!try_module_get(proto->module)) |
| 283 | return -ENOSYS; |
| 284 | |
| 285 | if (size) |
| 286 | if ((dev_to_hdlc(dev)->state = kmalloc(size, |
| 287 | GFP_KERNEL)) == NULL) { |
| 288 | printk(KERN_WARNING "Memory squeeze on" |
| 289 | " hdlc_proto_attach()\n"); |
| 290 | module_put(proto->module); |
| 291 | return -ENOBUFS; |
| 292 | } |
| 293 | dev_to_hdlc(dev)->proto = proto; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | void detach_hdlc_protocol(struct net_device *dev) |
| 299 | { |
| 300 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 301 | |
| 302 | if (hdlc->proto) { |
| 303 | if (hdlc->proto->detach) |
| 304 | hdlc->proto->detach(dev); |
| 305 | module_put(hdlc->proto->module); |
| 306 | hdlc->proto = NULL; |
| 307 | } |
| 308 | kfree(hdlc->state); |
| 309 | hdlc->state = NULL; |
Krzysztof Halasa | b5284e5 | 2007-03-02 15:52:22 -0800 | [diff] [blame] | 310 | hdlc_setup_dev(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
| 314 | void register_hdlc_protocol(struct hdlc_proto *proto) |
| 315 | { |
Krzysztof Halasa | fa701bd | 2008-05-19 19:00:51 +0200 | [diff] [blame] | 316 | rtnl_lock(); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 317 | proto->next = first_proto; |
| 318 | first_proto = proto; |
Krzysztof Halasa | fa701bd | 2008-05-19 19:00:51 +0200 | [diff] [blame] | 319 | rtnl_unlock(); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | void unregister_hdlc_protocol(struct hdlc_proto *proto) |
| 324 | { |
Krzysztof Halasa | fa701bd | 2008-05-19 19:00:51 +0200 | [diff] [blame] | 325 | struct hdlc_proto **p; |
| 326 | |
| 327 | rtnl_lock(); |
| 328 | p = &first_proto; |
| 329 | while (*p != proto) { |
| 330 | BUG_ON(!*p); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 331 | p = &((*p)->next); |
| 332 | } |
Krzysztof Halasa | fa701bd | 2008-05-19 19:00:51 +0200 | [diff] [blame] | 333 | *p = proto->next; |
| 334 | rtnl_unlock(); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 340 | MODULE_DESCRIPTION("HDLC support module"); |
| 341 | MODULE_LICENSE("GPL v2"); |
| 342 | |
| 343 | EXPORT_SYMBOL(hdlc_open); |
| 344 | EXPORT_SYMBOL(hdlc_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | EXPORT_SYMBOL(hdlc_ioctl); |
| 346 | EXPORT_SYMBOL(alloc_hdlcdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | EXPORT_SYMBOL(unregister_hdlc_device); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 348 | EXPORT_SYMBOL(register_hdlc_protocol); |
| 349 | EXPORT_SYMBOL(unregister_hdlc_protocol); |
| 350 | EXPORT_SYMBOL(attach_hdlc_protocol); |
| 351 | EXPORT_SYMBOL(detach_hdlc_protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | static struct packet_type hdlc_packet_type = { |
| 354 | .type = __constant_htons(ETH_P_HDLC), |
| 355 | .func = hdlc_rcv, |
| 356 | }; |
| 357 | |
| 358 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 359 | static struct notifier_block hdlc_notifier = { |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 360 | .notifier_call = hdlc_device_event, |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | static int __init hdlc_module_init(void) |
| 365 | { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 366 | int result; |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | printk(KERN_INFO "%s\n", version); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 369 | if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0) |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 370 | return result; |
| 371 | dev_add_pack(&hdlc_packet_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | static void __exit hdlc_module_exit(void) |
| 378 | { |
| 379 | dev_remove_pack(&hdlc_packet_type); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 380 | unregister_netdevice_notifier(&hdlc_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | module_init(hdlc_module_init); |
| 385 | module_exit(hdlc_module_exit); |