0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/errno.h>
0010 #include <linux/hdlc.h>
0011 #include <linux/if_arp.h>
0012 #include <linux/inetdevice.h>
0013 #include <linux/init.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/pkt_sched.h>
0017 #include <linux/poll.h>
0018 #include <linux/rtnetlink.h>
0019 #include <linux/skbuff.h>
0020
0021 #undef DEBUG_HARD_HEADER
0022
0023 #define CISCO_MULTICAST 0x8F
0024 #define CISCO_UNICAST 0x0F
0025 #define CISCO_KEEPALIVE 0x8035
0026 #define CISCO_SYS_INFO 0x2000
0027 #define CISCO_ADDR_REQ 0
0028 #define CISCO_ADDR_REPLY 1
0029 #define CISCO_KEEPALIVE_REQ 2
0030
0031 struct hdlc_header {
0032 u8 address;
0033 u8 control;
0034 __be16 protocol;
0035 } __packed;
0036
0037 struct cisco_packet {
0038 __be32 type;
0039 __be32 par1;
0040 __be32 par2;
0041 __be16 rel;
0042 __be32 time;
0043 } __packed;
0044 #define CISCO_PACKET_LEN 18
0045 #define CISCO_BIG_PACKET_LEN 20
0046
0047 struct cisco_state {
0048 cisco_proto settings;
0049
0050 struct timer_list timer;
0051 struct net_device *dev;
0052 spinlock_t lock;
0053 unsigned long last_poll;
0054 int up;
0055 u32 txseq;
0056 u32 rxseq;
0057 };
0058
0059 static int cisco_ioctl(struct net_device *dev, struct if_settings *ifs);
0060
0061 static inline struct cisco_state *state(hdlc_device *hdlc)
0062 {
0063 return (struct cisco_state *)hdlc->state;
0064 }
0065
0066 static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
0067 u16 type, const void *daddr, const void *saddr,
0068 unsigned int len)
0069 {
0070 struct hdlc_header *data;
0071 #ifdef DEBUG_HARD_HEADER
0072 netdev_dbg(dev, "%s called\n", __func__);
0073 #endif
0074
0075 skb_push(skb, sizeof(struct hdlc_header));
0076 data = (struct hdlc_header *)skb->data;
0077 if (type == CISCO_KEEPALIVE)
0078 data->address = CISCO_MULTICAST;
0079 else
0080 data->address = CISCO_UNICAST;
0081 data->control = 0;
0082 data->protocol = htons(type);
0083
0084 return sizeof(struct hdlc_header);
0085 }
0086
0087 static void cisco_keepalive_send(struct net_device *dev, u32 type,
0088 __be32 par1, __be32 par2)
0089 {
0090 struct sk_buff *skb;
0091 struct cisco_packet *data;
0092
0093 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
0094 sizeof(struct cisco_packet));
0095 if (!skb)
0096 return;
0097
0098 skb_reserve(skb, 4);
0099 cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
0100 data = (struct cisco_packet *)(skb->data + 4);
0101
0102 data->type = htonl(type);
0103 data->par1 = par1;
0104 data->par2 = par2;
0105 data->rel = cpu_to_be16(0xFFFF);
0106
0107 data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
0108
0109 skb_put(skb, sizeof(struct cisco_packet));
0110 skb->priority = TC_PRIO_CONTROL;
0111 skb->dev = dev;
0112 skb->protocol = htons(ETH_P_HDLC);
0113 skb_reset_network_header(skb);
0114
0115 dev_queue_xmit(skb);
0116 }
0117
0118 static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
0119 {
0120 struct hdlc_header *data = (struct hdlc_header *)skb->data;
0121
0122 if (skb->len < sizeof(struct hdlc_header))
0123 return cpu_to_be16(ETH_P_HDLC);
0124
0125 if (data->address != CISCO_MULTICAST &&
0126 data->address != CISCO_UNICAST)
0127 return cpu_to_be16(ETH_P_HDLC);
0128
0129 switch (data->protocol) {
0130 case cpu_to_be16(ETH_P_IP):
0131 case cpu_to_be16(ETH_P_IPX):
0132 case cpu_to_be16(ETH_P_IPV6):
0133 skb_pull(skb, sizeof(struct hdlc_header));
0134 return data->protocol;
0135 default:
0136 return cpu_to_be16(ETH_P_HDLC);
0137 }
0138 }
0139
0140 static int cisco_rx(struct sk_buff *skb)
0141 {
0142 struct net_device *dev = skb->dev;
0143 hdlc_device *hdlc = dev_to_hdlc(dev);
0144 struct cisco_state *st = state(hdlc);
0145 struct hdlc_header *data = (struct hdlc_header *)skb->data;
0146 struct cisco_packet *cisco_data;
0147 struct in_device *in_dev;
0148 __be32 addr, mask;
0149 u32 ack;
0150
0151 if (skb->len < sizeof(struct hdlc_header))
0152 goto rx_error;
0153
0154 if (data->address != CISCO_MULTICAST &&
0155 data->address != CISCO_UNICAST)
0156 goto rx_error;
0157
0158 switch (ntohs(data->protocol)) {
0159 case CISCO_SYS_INFO:
0160
0161 dev_kfree_skb_any(skb);
0162 return NET_RX_SUCCESS;
0163
0164 case CISCO_KEEPALIVE:
0165 if ((skb->len != sizeof(struct hdlc_header) +
0166 CISCO_PACKET_LEN) &&
0167 (skb->len != sizeof(struct hdlc_header) +
0168 CISCO_BIG_PACKET_LEN)) {
0169 netdev_info(dev, "Invalid length of Cisco control packet (%d bytes)\n",
0170 skb->len);
0171 goto rx_error;
0172 }
0173
0174 cisco_data = (struct cisco_packet *)(skb->data + sizeof
0175 (struct hdlc_header));
0176
0177 switch (ntohl(cisco_data->type)) {
0178 case CISCO_ADDR_REQ:
0179 rcu_read_lock();
0180 in_dev = __in_dev_get_rcu(dev);
0181 addr = 0;
0182 mask = ~cpu_to_be32(0);
0183
0184 if (in_dev != NULL) {
0185 const struct in_ifaddr *ifa;
0186
0187 in_dev_for_each_ifa_rcu(ifa, in_dev) {
0188 if (strcmp(dev->name,
0189 ifa->ifa_label) == 0) {
0190 addr = ifa->ifa_local;
0191 mask = ifa->ifa_mask;
0192 break;
0193 }
0194 }
0195
0196 cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
0197 addr, mask);
0198 }
0199 rcu_read_unlock();
0200 dev_kfree_skb_any(skb);
0201 return NET_RX_SUCCESS;
0202
0203 case CISCO_ADDR_REPLY:
0204 netdev_info(dev, "Unexpected Cisco IP address reply\n");
0205 goto rx_error;
0206
0207 case CISCO_KEEPALIVE_REQ:
0208 spin_lock(&st->lock);
0209 st->rxseq = ntohl(cisco_data->par1);
0210 ack = ntohl(cisco_data->par2);
0211 if (ack && (ack == st->txseq ||
0212
0213 ack == st->txseq - 1)) {
0214 st->last_poll = jiffies;
0215 if (!st->up) {
0216 u32 sec, min, hrs, days;
0217
0218 sec = ntohl(cisco_data->time) / 1000;
0219 min = sec / 60; sec -= min * 60;
0220 hrs = min / 60; min -= hrs * 60;
0221 days = hrs / 24; hrs -= days * 24;
0222 netdev_info(dev, "Link up (peer uptime %ud%uh%um%us)\n",
0223 days, hrs, min, sec);
0224 netif_dormant_off(dev);
0225 st->up = 1;
0226 }
0227 }
0228 spin_unlock(&st->lock);
0229
0230 dev_kfree_skb_any(skb);
0231 return NET_RX_SUCCESS;
0232 }
0233 }
0234
0235 netdev_info(dev, "Unsupported protocol %x\n", ntohs(data->protocol));
0236 dev_kfree_skb_any(skb);
0237 return NET_RX_DROP;
0238
0239 rx_error:
0240 dev->stats.rx_errors++;
0241 dev_kfree_skb_any(skb);
0242 return NET_RX_DROP;
0243 }
0244
0245 static void cisco_timer(struct timer_list *t)
0246 {
0247 struct cisco_state *st = from_timer(st, t, timer);
0248 struct net_device *dev = st->dev;
0249
0250 spin_lock(&st->lock);
0251 if (st->up &&
0252 time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
0253 st->up = 0;
0254 netdev_info(dev, "Link down\n");
0255 netif_dormant_on(dev);
0256 }
0257
0258 cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
0259 htonl(st->rxseq));
0260 spin_unlock(&st->lock);
0261
0262 st->timer.expires = jiffies + st->settings.interval * HZ;
0263 add_timer(&st->timer);
0264 }
0265
0266 static void cisco_start(struct net_device *dev)
0267 {
0268 hdlc_device *hdlc = dev_to_hdlc(dev);
0269 struct cisco_state *st = state(hdlc);
0270 unsigned long flags;
0271
0272 spin_lock_irqsave(&st->lock, flags);
0273 st->up = st->txseq = st->rxseq = 0;
0274 spin_unlock_irqrestore(&st->lock, flags);
0275
0276 st->dev = dev;
0277 timer_setup(&st->timer, cisco_timer, 0);
0278 st->timer.expires = jiffies + HZ;
0279 add_timer(&st->timer);
0280 }
0281
0282 static void cisco_stop(struct net_device *dev)
0283 {
0284 hdlc_device *hdlc = dev_to_hdlc(dev);
0285 struct cisco_state *st = state(hdlc);
0286 unsigned long flags;
0287
0288 del_timer_sync(&st->timer);
0289
0290 spin_lock_irqsave(&st->lock, flags);
0291 netif_dormant_on(dev);
0292 st->up = st->txseq = 0;
0293 spin_unlock_irqrestore(&st->lock, flags);
0294 }
0295
0296 static struct hdlc_proto proto = {
0297 .start = cisco_start,
0298 .stop = cisco_stop,
0299 .type_trans = cisco_type_trans,
0300 .ioctl = cisco_ioctl,
0301 .netif_rx = cisco_rx,
0302 .module = THIS_MODULE,
0303 };
0304
0305 static const struct header_ops cisco_header_ops = {
0306 .create = cisco_hard_header,
0307 };
0308
0309 static int cisco_ioctl(struct net_device *dev, struct if_settings *ifs)
0310 {
0311 cisco_proto __user *cisco_s = ifs->ifs_ifsu.cisco;
0312 const size_t size = sizeof(cisco_proto);
0313 cisco_proto new_settings;
0314 hdlc_device *hdlc = dev_to_hdlc(dev);
0315 int result;
0316
0317 switch (ifs->type) {
0318 case IF_GET_PROTO:
0319 if (dev_to_hdlc(dev)->proto != &proto)
0320 return -EINVAL;
0321 ifs->type = IF_PROTO_CISCO;
0322 if (ifs->size < size) {
0323 ifs->size = size;
0324 return -ENOBUFS;
0325 }
0326 if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
0327 return -EFAULT;
0328 return 0;
0329
0330 case IF_PROTO_CISCO:
0331 if (!capable(CAP_NET_ADMIN))
0332 return -EPERM;
0333
0334 if (dev->flags & IFF_UP)
0335 return -EBUSY;
0336
0337 if (copy_from_user(&new_settings, cisco_s, size))
0338 return -EFAULT;
0339
0340 if (new_settings.interval < 1 ||
0341 new_settings.timeout < 2)
0342 return -EINVAL;
0343
0344 result = hdlc->attach(dev, ENCODING_NRZ,
0345 PARITY_CRC16_PR1_CCITT);
0346 if (result)
0347 return result;
0348
0349 result = attach_hdlc_protocol(dev, &proto,
0350 sizeof(struct cisco_state));
0351 if (result)
0352 return result;
0353
0354 memcpy(&state(hdlc)->settings, &new_settings, size);
0355 spin_lock_init(&state(hdlc)->lock);
0356 dev->header_ops = &cisco_header_ops;
0357 dev->hard_header_len = sizeof(struct hdlc_header);
0358 dev->type = ARPHRD_CISCO;
0359 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
0360 netif_dormant_on(dev);
0361 return 0;
0362 }
0363
0364 return -EINVAL;
0365 }
0366
0367 static int __init hdlc_cisco_init(void)
0368 {
0369 register_hdlc_protocol(&proto);
0370 return 0;
0371 }
0372
0373 static void __exit hdlc_cisco_exit(void)
0374 {
0375 unregister_hdlc_protocol(&proto);
0376 }
0377
0378 module_init(hdlc_cisco_init);
0379 module_exit(hdlc_cisco_exit);
0380
0381 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
0382 MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
0383 MODULE_LICENSE("GPL v2");