Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * Cisco HDLC support |
| 4 | * |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000 - 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/poll.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/if_arp.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/skbuff.h> |
| 20 | #include <linux/pkt_sched.h> |
| 21 | #include <linux/inetdevice.h> |
| 22 | #include <linux/lapb.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | #include <linux/hdlc.h> |
| 25 | |
| 26 | #undef DEBUG_HARD_HEADER |
| 27 | |
| 28 | #define CISCO_MULTICAST 0x8F /* Cisco multicast address */ |
| 29 | #define CISCO_UNICAST 0x0F /* Cisco unicast address */ |
| 30 | #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ |
| 31 | #define CISCO_SYS_INFO 0x2000 /* Cisco interface/system info */ |
| 32 | #define CISCO_ADDR_REQ 0 /* Cisco address request */ |
| 33 | #define CISCO_ADDR_REPLY 1 /* Cisco address reply */ |
| 34 | #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ |
| 35 | |
| 36 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 37 | struct hdlc_header { |
| 38 | u8 address; |
| 39 | u8 control; |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 40 | __be16 protocol; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 41 | }__attribute__ ((packed)); |
| 42 | |
| 43 | |
| 44 | struct cisco_packet { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 45 | __be32 type; /* code */ |
| 46 | __be32 par1; |
| 47 | __be32 par2; |
| 48 | __be16 rel; /* reliability */ |
| 49 | __be32 time; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 50 | }__attribute__ ((packed)); |
| 51 | #define CISCO_PACKET_LEN 18 |
| 52 | #define CISCO_BIG_PACKET_LEN 20 |
| 53 | |
| 54 | |
| 55 | struct cisco_state { |
| 56 | cisco_proto settings; |
| 57 | |
| 58 | struct timer_list timer; |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 59 | spinlock_t lock; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 60 | unsigned long last_poll; |
| 61 | int up; |
| 62 | int request_sent; |
| 63 | u32 txseq; /* TX sequence number */ |
| 64 | u32 rxseq; /* RX sequence number */ |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr); |
| 69 | |
| 70 | |
| 71 | static inline struct cisco_state * state(hdlc_device *hdlc) |
| 72 | { |
| 73 | return(struct cisco_state *)(hdlc->state); |
| 74 | } |
| 75 | |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 78 | u16 type, const void *daddr, const void *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | unsigned int len) |
| 80 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 81 | struct hdlc_header *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #ifdef DEBUG_HARD_HEADER |
| 83 | printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name); |
| 84 | #endif |
| 85 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 86 | skb_push(skb, sizeof(struct hdlc_header)); |
| 87 | data = (struct hdlc_header*)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (type == CISCO_KEEPALIVE) |
| 89 | data->address = CISCO_MULTICAST; |
| 90 | else |
| 91 | data->address = CISCO_UNICAST; |
| 92 | data->control = 0; |
| 93 | data->protocol = htons(type); |
| 94 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 95 | return sizeof(struct hdlc_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | static void cisco_keepalive_send(struct net_device *dev, u32 type, |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 101 | __be32 par1, __be32 par2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
| 103 | struct sk_buff *skb; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 104 | struct cisco_packet *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 106 | skb = dev_alloc_skb(sizeof(struct hdlc_header) + |
| 107 | sizeof(struct cisco_packet)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (!skb) { |
| 109 | printk(KERN_WARNING |
| 110 | "%s: Memory squeeze on cisco_keepalive_send()\n", |
| 111 | dev->name); |
| 112 | return; |
| 113 | } |
| 114 | skb_reserve(skb, 4); |
| 115 | cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 116 | data = (struct cisco_packet*)(skb->data + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | data->type = htonl(type); |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 119 | data->par1 = par1; |
| 120 | data->par2 = par2; |
| 121 | data->rel = __constant_htons(0xFFFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* we will need do_div here if 1000 % HZ != 0 */ |
| 123 | data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ)); |
| 124 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 125 | skb_put(skb, sizeof(struct cisco_packet)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | skb->priority = TC_PRIO_CONTROL; |
| 127 | skb->dev = dev; |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 128 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | dev_queue_xmit(skb); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
Alexey Dobriyan | ab61148 | 2005-07-12 12:08:43 -0700 | [diff] [blame] | 135 | static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 137 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 139 | if (skb->len < sizeof(struct hdlc_header)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return __constant_htons(ETH_P_HDLC); |
| 141 | |
| 142 | if (data->address != CISCO_MULTICAST && |
| 143 | data->address != CISCO_UNICAST) |
| 144 | return __constant_htons(ETH_P_HDLC); |
| 145 | |
| 146 | switch(data->protocol) { |
| 147 | case __constant_htons(ETH_P_IP): |
| 148 | case __constant_htons(ETH_P_IPX): |
| 149 | case __constant_htons(ETH_P_IPV6): |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 150 | skb_pull(skb, sizeof(struct hdlc_header)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return data->protocol; |
| 152 | default: |
| 153 | return __constant_htons(ETH_P_HDLC); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static int cisco_rx(struct sk_buff *skb) |
| 159 | { |
| 160 | struct net_device *dev = skb->dev; |
| 161 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 162 | struct cisco_state *st = state(hdlc); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 163 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
| 164 | struct cisco_packet *cisco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | struct in_device *in_dev; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 166 | __be32 addr, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 168 | if (skb->len < sizeof(struct hdlc_header)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | goto rx_error; |
| 170 | |
| 171 | if (data->address != CISCO_MULTICAST && |
| 172 | data->address != CISCO_UNICAST) |
| 173 | goto rx_error; |
| 174 | |
| 175 | switch(ntohs(data->protocol)) { |
| 176 | case CISCO_SYS_INFO: |
| 177 | /* Packet is not needed, drop it. */ |
| 178 | dev_kfree_skb_any(skb); |
| 179 | return NET_RX_SUCCESS; |
| 180 | |
| 181 | case CISCO_KEEPALIVE: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 182 | if ((skb->len != sizeof(struct hdlc_header) + |
| 183 | CISCO_PACKET_LEN) && |
| 184 | (skb->len != sizeof(struct hdlc_header) + |
| 185 | CISCO_BIG_PACKET_LEN)) { |
| 186 | printk(KERN_INFO "%s: Invalid length of Cisco control" |
| 187 | " packet (%d bytes)\n", dev->name, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | goto rx_error; |
| 189 | } |
| 190 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 191 | cisco_data = (struct cisco_packet*)(skb->data + sizeof |
| 192 | (struct hdlc_header)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | switch(ntohl (cisco_data->type)) { |
| 195 | case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */ |
| 196 | in_dev = dev->ip_ptr; |
| 197 | addr = 0; |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 198 | mask = __constant_htonl(~0); /* is the mask correct? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (in_dev != NULL) { |
| 201 | struct in_ifaddr **ifap = &in_dev->ifa_list; |
| 202 | |
| 203 | while (*ifap != NULL) { |
| 204 | if (strcmp(dev->name, |
| 205 | (*ifap)->ifa_label) == 0) { |
| 206 | addr = (*ifap)->ifa_local; |
| 207 | mask = (*ifap)->ifa_mask; |
| 208 | break; |
| 209 | } |
| 210 | ifap = &(*ifap)->ifa_next; |
| 211 | } |
| 212 | |
| 213 | cisco_keepalive_send(dev, CISCO_ADDR_REPLY, |
| 214 | addr, mask); |
| 215 | } |
| 216 | dev_kfree_skb_any(skb); |
| 217 | return NET_RX_SUCCESS; |
| 218 | |
| 219 | case CISCO_ADDR_REPLY: |
| 220 | printk(KERN_INFO "%s: Unexpected Cisco IP address " |
| 221 | "reply\n", dev->name); |
| 222 | goto rx_error; |
| 223 | |
| 224 | case CISCO_KEEPALIVE_REQ: |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 225 | spin_lock(&st->lock); |
| 226 | st->rxseq = ntohl(cisco_data->par1); |
| 227 | if (st->request_sent && |
| 228 | ntohl(cisco_data->par2) == st->txseq) { |
| 229 | st->last_poll = jiffies; |
| 230 | if (!st->up) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | u32 sec, min, hrs, days; |
| 232 | sec = ntohl(cisco_data->time) / 1000; |
| 233 | min = sec / 60; sec -= min * 60; |
| 234 | hrs = min / 60; min -= hrs * 60; |
| 235 | days = hrs / 24; hrs -= days * 24; |
| 236 | printk(KERN_INFO "%s: Link up (peer " |
| 237 | "uptime %ud%uh%um%us)\n", |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 238 | dev->name, days, hrs, min, sec); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 239 | netif_dormant_off(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 240 | st->up = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 243 | spin_unlock(&st->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | dev_kfree_skb_any(skb); |
| 246 | return NET_RX_SUCCESS; |
| 247 | } /* switch(keepalive type) */ |
| 248 | } /* switch(protocol) */ |
| 249 | |
| 250 | printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name, |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 251 | ntohs(data->protocol)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | dev_kfree_skb_any(skb); |
| 253 | return NET_RX_DROP; |
| 254 | |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 255 | rx_error: |
| 256 | dev->stats.rx_errors++; /* Mark error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | dev_kfree_skb_any(skb); |
| 258 | return NET_RX_DROP; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | |
| 263 | static void cisco_timer(unsigned long arg) |
| 264 | { |
| 265 | struct net_device *dev = (struct net_device *)arg; |
| 266 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 267 | struct cisco_state *st = state(hdlc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 269 | spin_lock(&st->lock); |
| 270 | if (st->up && |
| 271 | time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) { |
| 272 | st->up = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | printk(KERN_INFO "%s: Link down\n", dev->name); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 274 | netif_dormant_on(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 277 | cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq), |
| 278 | htonl(st->rxseq)); |
| 279 | st->request_sent = 1; |
| 280 | spin_unlock(&st->lock); |
| 281 | |
| 282 | st->timer.expires = jiffies + st->settings.interval * HZ; |
| 283 | st->timer.function = cisco_timer; |
| 284 | st->timer.data = arg; |
| 285 | add_timer(&st->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | static void cisco_start(struct net_device *dev) |
| 291 | { |
| 292 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 293 | struct cisco_state *st = state(hdlc); |
| 294 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 296 | spin_lock_irqsave(&st->lock, flags); |
| 297 | st->up = 0; |
| 298 | st->request_sent = 0; |
| 299 | st->txseq = st->rxseq = 0; |
| 300 | spin_unlock_irqrestore(&st->lock, flags); |
| 301 | |
| 302 | init_timer(&st->timer); |
| 303 | st->timer.expires = jiffies + HZ; /* First poll after 1 s */ |
| 304 | st->timer.function = cisco_timer; |
| 305 | st->timer.data = (unsigned long)dev; |
| 306 | add_timer(&st->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | static void cisco_stop(struct net_device *dev) |
| 312 | { |
| 313 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 314 | struct cisco_state *st = state(hdlc); |
| 315 | unsigned long flags; |
| 316 | |
| 317 | del_timer_sync(&st->timer); |
| 318 | |
| 319 | spin_lock_irqsave(&st->lock, flags); |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 320 | netif_dormant_on(dev); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 321 | st->up = 0; |
| 322 | st->request_sent = 0; |
| 323 | spin_unlock_irqrestore(&st->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 327 | static struct hdlc_proto proto = { |
| 328 | .start = cisco_start, |
| 329 | .stop = cisco_stop, |
| 330 | .type_trans = cisco_type_trans, |
| 331 | .ioctl = cisco_ioctl, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 332 | .netif_rx = cisco_rx, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 333 | .module = THIS_MODULE, |
| 334 | }; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 335 | |
| 336 | static const struct header_ops cisco_header_ops = { |
| 337 | .create = cisco_hard_header, |
| 338 | }; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 339 | |
| 340 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco; |
| 343 | const size_t size = sizeof(cisco_proto); |
| 344 | cisco_proto new_settings; |
| 345 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 346 | int result; |
| 347 | |
| 348 | switch (ifr->ifr_settings.type) { |
| 349 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 350 | if (dev_to_hdlc(dev)->proto != &proto) |
| 351 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | ifr->ifr_settings.type = IF_PROTO_CISCO; |
| 353 | if (ifr->ifr_settings.size < size) { |
| 354 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 355 | return -ENOBUFS; |
| 356 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 357 | if (copy_to_user(cisco_s, &state(hdlc)->settings, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return -EFAULT; |
| 359 | return 0; |
| 360 | |
| 361 | case IF_PROTO_CISCO: |
| 362 | if(!capable(CAP_NET_ADMIN)) |
| 363 | return -EPERM; |
| 364 | |
| 365 | if(dev->flags & IFF_UP) |
| 366 | return -EBUSY; |
| 367 | |
| 368 | if (copy_from_user(&new_settings, cisco_s, size)) |
| 369 | return -EFAULT; |
| 370 | |
| 371 | if (new_settings.interval < 1 || |
| 372 | new_settings.timeout < 2) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (result) |
| 377 | return result; |
| 378 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 379 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 380 | sizeof(struct cisco_state)); |
| 381 | if (result) |
| 382 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 384 | memcpy(&state(hdlc)->settings, &new_settings, size); |
Krzysztof Halasa | aff26e2 | 2008-05-19 19:11:08 +0200 | [diff] [blame] | 385 | spin_lock_init(&state(hdlc)->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | dev->hard_start_xmit = hdlc->xmit; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 387 | dev->header_ops = &cisco_header_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | dev->type = ARPHRD_CISCO; |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 389 | netif_dormant_on(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | return -EINVAL; |
| 394 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 395 | |
| 396 | |
| 397 | static int __init mod_init(void) |
| 398 | { |
| 399 | register_hdlc_protocol(&proto); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | static void __exit mod_exit(void) |
| 406 | { |
| 407 | unregister_hdlc_protocol(&proto); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | module_init(mod_init); |
| 412 | module_exit(mod_exit); |
| 413 | |
| 414 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 415 | MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC"); |
| 416 | MODULE_LICENSE("GPL v2"); |