Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * Frame Relay 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 | |
| 12 | Theory of PVC state |
| 13 | |
| 14 | DCE mode: |
| 15 | |
| 16 | (exist,new) -> 0,0 when "PVC create" or if "link unreliable" |
| 17 | 0,x -> 1,1 if "link reliable" when sending FULL STATUS |
| 18 | 1,1 -> 1,0 if received FULL STATUS ACK |
| 19 | |
| 20 | (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create" |
| 21 | -> 1 when "PVC up" and (exist,new) = 1,0 |
| 22 | |
| 23 | DTE mode: |
| 24 | (exist,new,active) = FULL STATUS if "link reliable" |
| 25 | = 0, 0, 0 if "link unreliable" |
| 26 | No LMI: |
| 27 | active = open and "link reliable" |
| 28 | exist = new = not used |
| 29 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 30 | CCITT LMI: ITU-T Q.933 Annex A |
| 31 | ANSI LMI: ANSI T1.617 Annex D |
| 32 | CISCO LMI: the original, aka "Gang of Four" LMI |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/poll.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/if_arp.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/skbuff.h> |
| 44 | #include <linux/pkt_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/inetdevice.h> |
| 46 | #include <linux/lapb.h> |
| 47 | #include <linux/rtnetlink.h> |
| 48 | #include <linux/etherdevice.h> |
| 49 | #include <linux/hdlc.h> |
| 50 | |
| 51 | #undef DEBUG_PKT |
| 52 | #undef DEBUG_ECN |
| 53 | #undef DEBUG_LINK |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 54 | #undef DEBUG_PROTO |
| 55 | #undef DEBUG_PVC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 57 | #define FR_UI 0x03 |
| 58 | #define FR_PAD 0x00 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 60 | #define NLPID_IP 0xCC |
| 61 | #define NLPID_IPV6 0x8E |
| 62 | #define NLPID_SNAP 0x80 |
| 63 | #define NLPID_PAD 0x00 |
| 64 | #define NLPID_CCITT_ANSI_LMI 0x08 |
| 65 | #define NLPID_CISCO_LMI 0x09 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 68 | #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */ |
| 69 | #define LMI_CISCO_DLCI 1023 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 71 | #define LMI_CALLREF 0x00 /* Call Reference */ |
| 72 | #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */ |
| 73 | #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */ |
| 74 | #define LMI_CCITT_REPTYPE 0x51 |
| 75 | #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */ |
| 76 | #define LMI_CCITT_ALIVE 0x53 |
| 77 | #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */ |
| 78 | #define LMI_CCITT_PVCSTAT 0x57 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 80 | #define LMI_FULLREP 0x00 /* full report */ |
| 81 | #define LMI_INTEGRITY 0x01 /* link integrity report */ |
| 82 | #define LMI_SINGLE 0x02 /* single PVC report */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #define LMI_STATUS_ENQUIRY 0x75 |
| 85 | #define LMI_STATUS 0x7D /* reply */ |
| 86 | |
| 87 | #define LMI_REPT_LEN 1 /* report type element length */ |
| 88 | #define LMI_INTEG_LEN 2 /* link integrity element length */ |
| 89 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 90 | #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */ |
| 91 | #define LMI_ANSI_LENGTH 14 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | |
| 94 | typedef struct { |
| 95 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 96 | unsigned ea1: 1; |
| 97 | unsigned cr: 1; |
| 98 | unsigned dlcih: 6; |
| 99 | |
| 100 | unsigned ea2: 1; |
| 101 | unsigned de: 1; |
| 102 | unsigned becn: 1; |
| 103 | unsigned fecn: 1; |
| 104 | unsigned dlcil: 4; |
| 105 | #else |
| 106 | unsigned dlcih: 6; |
| 107 | unsigned cr: 1; |
| 108 | unsigned ea1: 1; |
| 109 | |
| 110 | unsigned dlcil: 4; |
| 111 | unsigned fecn: 1; |
| 112 | unsigned becn: 1; |
| 113 | unsigned de: 1; |
| 114 | unsigned ea2: 1; |
| 115 | #endif |
| 116 | }__attribute__ ((packed)) fr_hdr; |
| 117 | |
| 118 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 119 | typedef struct pvc_device_struct { |
| 120 | struct net_device *frad; |
| 121 | struct net_device *main; |
| 122 | struct net_device *ether; /* bridged Ethernet interface */ |
| 123 | struct pvc_device_struct *next; /* Sorted in ascending DLCI order */ |
| 124 | int dlci; |
| 125 | int open_count; |
| 126 | |
| 127 | struct { |
| 128 | unsigned int new: 1; |
| 129 | unsigned int active: 1; |
| 130 | unsigned int exist: 1; |
| 131 | unsigned int deleted: 1; |
| 132 | unsigned int fecn: 1; |
| 133 | unsigned int becn: 1; |
| 134 | unsigned int bandwidth; /* Cisco LMI reporting only */ |
| 135 | }state; |
| 136 | }pvc_device; |
| 137 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 138 | struct frad_state { |
| 139 | fr_proto settings; |
| 140 | pvc_device *first_pvc; |
| 141 | int dce_pvc_count; |
| 142 | |
| 143 | struct timer_list timer; |
| 144 | unsigned long last_poll; |
| 145 | int reliable; |
| 146 | int dce_changed; |
| 147 | int request; |
| 148 | int fullrep_sent; |
| 149 | u32 last_errors; /* last errors bit list */ |
| 150 | u8 n391cnt; |
| 151 | u8 txseq; /* TX sequence number */ |
| 152 | u8 rxseq; /* RX sequence number */ |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | static int fr_ioctl(struct net_device *dev, struct ifreq *ifr); |
| 157 | |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | static inline u16 q922_to_dlci(u8 *hdr) |
| 160 | { |
| 161 | return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4); |
| 162 | } |
| 163 | |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static inline void dlci_to_q922(u8 *hdr, u16 dlci) |
| 166 | { |
| 167 | hdr[0] = (dlci >> 2) & 0xFC; |
| 168 | hdr[1] = ((dlci << 4) & 0xF0) | 0x01; |
| 169 | } |
| 170 | |
| 171 | |
Krzysztof Halasa | 983e230 | 2008-02-01 22:34:30 +0100 | [diff] [blame] | 172 | static inline struct frad_state* state(hdlc_device *hdlc) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 173 | { |
| 174 | return(struct frad_state *)(hdlc->state); |
| 175 | } |
| 176 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci) |
| 179 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 180 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | while (pvc) { |
| 183 | if (pvc->dlci == dlci) |
| 184 | return pvc; |
| 185 | if (pvc->dlci > dlci) |
| 186 | return NULL; /* the listed is sorted */ |
| 187 | pvc = pvc->next; |
| 188 | } |
| 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 194 | static pvc_device* add_pvc(struct net_device *dev, u16 dlci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 197 | pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | while (*pvc_p) { |
| 200 | if ((*pvc_p)->dlci == dlci) |
| 201 | return *pvc_p; |
| 202 | if ((*pvc_p)->dlci > dlci) |
| 203 | break; /* the list is sorted */ |
| 204 | pvc_p = &(*pvc_p)->next; |
| 205 | } |
| 206 | |
Mariusz Kozlowski | 1ee3254 | 2007-08-10 15:24:50 -0700 | [diff] [blame] | 207 | pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 208 | #ifdef DEBUG_PVC |
| 209 | printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev); |
| 210 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (!pvc) |
| 212 | return NULL; |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | pvc->dlci = dlci; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 215 | pvc->frad = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | pvc->next = *pvc_p; /* Put it in the chain */ |
| 217 | *pvc_p = pvc; |
| 218 | return pvc; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static inline int pvc_is_used(pvc_device *pvc) |
| 223 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 224 | return pvc->main || pvc->ether; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | static inline void pvc_carrier(int on, pvc_device *pvc) |
| 229 | { |
| 230 | if (on) { |
| 231 | if (pvc->main) |
| 232 | if (!netif_carrier_ok(pvc->main)) |
| 233 | netif_carrier_on(pvc->main); |
| 234 | if (pvc->ether) |
| 235 | if (!netif_carrier_ok(pvc->ether)) |
| 236 | netif_carrier_on(pvc->ether); |
| 237 | } else { |
| 238 | if (pvc->main) |
| 239 | if (netif_carrier_ok(pvc->main)) |
| 240 | netif_carrier_off(pvc->main); |
| 241 | if (pvc->ether) |
| 242 | if (netif_carrier_ok(pvc->ether)) |
| 243 | netif_carrier_off(pvc->ether); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static inline void delete_unused_pvcs(hdlc_device *hdlc) |
| 249 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 250 | pvc_device **pvc_p = &state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | while (*pvc_p) { |
| 253 | if (!pvc_is_used(*pvc_p)) { |
| 254 | pvc_device *pvc = *pvc_p; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 255 | #ifdef DEBUG_PVC |
| 256 | printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc); |
| 257 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | *pvc_p = pvc->next; |
| 259 | kfree(pvc); |
| 260 | continue; |
| 261 | } |
| 262 | pvc_p = &(*pvc_p)->next; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static inline struct net_device** get_dev_p(pvc_device *pvc, int type) |
| 268 | { |
| 269 | if (type == ARPHRD_ETHER) |
| 270 | return &pvc->ether; |
| 271 | else |
| 272 | return &pvc->main; |
| 273 | } |
| 274 | |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | static int fr_hard_header(struct sk_buff **skb_p, u16 dlci) |
| 277 | { |
| 278 | u16 head_len; |
| 279 | struct sk_buff *skb = *skb_p; |
| 280 | |
| 281 | switch (skb->protocol) { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 282 | case __constant_htons(NLPID_CCITT_ANSI_LMI): |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 283 | head_len = 4; |
| 284 | skb_push(skb, head_len); |
| 285 | skb->data[3] = NLPID_CCITT_ANSI_LMI; |
| 286 | break; |
| 287 | |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 288 | case __constant_htons(NLPID_CISCO_LMI): |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 289 | head_len = 4; |
| 290 | skb_push(skb, head_len); |
| 291 | skb->data[3] = NLPID_CISCO_LMI; |
| 292 | break; |
| 293 | |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 294 | case __constant_htons(ETH_P_IP): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | head_len = 4; |
| 296 | skb_push(skb, head_len); |
| 297 | skb->data[3] = NLPID_IP; |
| 298 | break; |
| 299 | |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 300 | case __constant_htons(ETH_P_IPV6): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | head_len = 4; |
| 302 | skb_push(skb, head_len); |
| 303 | skb->data[3] = NLPID_IPV6; |
| 304 | break; |
| 305 | |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 306 | case __constant_htons(ETH_P_802_3): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | head_len = 10; |
| 308 | if (skb_headroom(skb) < head_len) { |
| 309 | struct sk_buff *skb2 = skb_realloc_headroom(skb, |
| 310 | head_len); |
| 311 | if (!skb2) |
| 312 | return -ENOBUFS; |
| 313 | dev_kfree_skb(skb); |
| 314 | skb = *skb_p = skb2; |
| 315 | } |
| 316 | skb_push(skb, head_len); |
| 317 | skb->data[3] = FR_PAD; |
| 318 | skb->data[4] = NLPID_SNAP; |
| 319 | skb->data[5] = FR_PAD; |
| 320 | skb->data[6] = 0x80; |
| 321 | skb->data[7] = 0xC2; |
| 322 | skb->data[8] = 0x00; |
| 323 | skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */ |
| 324 | break; |
| 325 | |
| 326 | default: |
| 327 | head_len = 10; |
| 328 | skb_push(skb, head_len); |
| 329 | skb->data[3] = FR_PAD; |
| 330 | skb->data[4] = NLPID_SNAP; |
| 331 | skb->data[5] = FR_PAD; |
| 332 | skb->data[6] = FR_PAD; |
| 333 | skb->data[7] = FR_PAD; |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 334 | *(__be16*)(skb->data + 8) = skb->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | dlci_to_q922(skb->data, dlci); |
| 338 | skb->data[2] = FR_UI; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | static int pvc_open(struct net_device *dev) |
| 345 | { |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 346 | pvc_device *pvc = dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 348 | if ((pvc->frad->flags & IFF_UP) == 0) |
| 349 | return -EIO; /* Frad must be UP in order to activate PVC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | if (pvc->open_count++ == 0) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 352 | hdlc_device *hdlc = dev_to_hdlc(pvc->frad); |
| 353 | if (state(hdlc)->settings.lmi == LMI_NONE) |
| 354 | pvc->state.active = netif_carrier_ok(pvc->frad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | pvc_carrier(pvc->state.active, pvc); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 357 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | static int pvc_close(struct net_device *dev) |
| 365 | { |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 366 | pvc_device *pvc = dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | if (--pvc->open_count == 0) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 369 | hdlc_device *hdlc = dev_to_hdlc(pvc->frad); |
| 370 | if (state(hdlc)->settings.lmi == LMI_NONE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | pvc->state.active = 0; |
| 372 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 373 | if (state(hdlc)->settings.dce) { |
| 374 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | pvc->state.active = 0; |
| 376 | } |
| 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
Adrian Bunk | 7665a08 | 2005-09-09 23:17:28 -0700 | [diff] [blame] | 383 | static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 385 | pvc_device *pvc = dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | fr_proto_pvc_info info; |
| 387 | |
| 388 | if (ifr->ifr_settings.type == IF_GET_PROTO) { |
| 389 | if (dev->type == ARPHRD_ETHER) |
| 390 | ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC; |
| 391 | else |
| 392 | ifr->ifr_settings.type = IF_PROTO_FR_PVC; |
| 393 | |
| 394 | if (ifr->ifr_settings.size < sizeof(info)) { |
| 395 | /* data size wanted */ |
| 396 | ifr->ifr_settings.size = sizeof(info); |
| 397 | return -ENOBUFS; |
| 398 | } |
| 399 | |
| 400 | info.dlci = pvc->dlci; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 401 | memcpy(info.master, pvc->frad->name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info, |
| 403 | &info, sizeof(info))) |
| 404 | return -EFAULT; |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | static int pvc_xmit(struct sk_buff *skb, struct net_device *dev) |
| 412 | { |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 413 | pvc_device *pvc = dev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | if (pvc->state.active) { |
| 416 | if (dev->type == ARPHRD_ETHER) { |
| 417 | int pad = ETH_ZLEN - skb->len; |
| 418 | if (pad > 0) { /* Pad the frame with zeros */ |
| 419 | int len = skb->len; |
| 420 | if (skb_tailroom(skb) < pad) |
| 421 | if (pskb_expand_head(skb, 0, pad, |
| 422 | GFP_ATOMIC)) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 423 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | dev_kfree_skb(skb); |
| 425 | return 0; |
| 426 | } |
| 427 | skb_put(skb, pad); |
| 428 | memset(skb->data + len, 0, pad); |
| 429 | } |
| 430 | skb->protocol = __constant_htons(ETH_P_802_3); |
| 431 | } |
| 432 | if (!fr_hard_header(&skb, pvc->dlci)) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 433 | dev->stats.tx_bytes += skb->len; |
| 434 | dev->stats.tx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (pvc->state.fecn) /* TX Congestion counter */ |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 436 | dev->stats.tx_compressed++; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 437 | skb->dev = pvc->frad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | dev_queue_xmit(skb); |
| 439 | return 0; |
| 440 | } |
| 441 | } |
| 442 | |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 443 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | dev_kfree_skb(skb); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | static int pvc_change_mtu(struct net_device *dev, int new_mtu) |
| 451 | { |
| 452 | if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU)) |
| 453 | return -EINVAL; |
| 454 | dev->mtu = new_mtu; |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | static inline void fr_log_dlci_active(pvc_device *pvc) |
| 461 | { |
| 462 | printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 463 | pvc->frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | pvc->dlci, |
| 465 | pvc->main ? pvc->main->name : "", |
| 466 | pvc->main && pvc->ether ? " " : "", |
| 467 | pvc->ether ? pvc->ether->name : "", |
| 468 | pvc->state.new ? " new" : "", |
| 469 | !pvc->state.exist ? "deleted" : |
| 470 | pvc->state.active ? "active" : "inactive"); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | static inline u8 fr_lmi_nextseq(u8 x) |
| 476 | { |
| 477 | x++; |
| 478 | return x ? x : 1; |
| 479 | } |
| 480 | |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | static void fr_lmi_send(struct net_device *dev, int fullrep) |
| 483 | { |
| 484 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 485 | struct sk_buff *skb; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 486 | pvc_device *pvc = state(hdlc)->first_pvc; |
| 487 | int lmi = state(hdlc)->settings.lmi; |
| 488 | int dce = state(hdlc)->settings.dce; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 489 | int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH; |
| 490 | int stat_len = (lmi == LMI_CISCO) ? 6 : 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | u8 *data; |
| 492 | int i = 0; |
| 493 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 494 | if (dce && fullrep) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 495 | len += state(hdlc)->dce_pvc_count * (2 + stat_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (len > HDLC_MAX_MRU) { |
| 497 | printk(KERN_WARNING "%s: Too many PVCs while sending " |
| 498 | "LMI full report\n", dev->name); |
| 499 | return; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | skb = dev_alloc_skb(len); |
| 504 | if (!skb) { |
| 505 | printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n", |
| 506 | dev->name); |
| 507 | return; |
| 508 | } |
| 509 | memset(skb->data, 0, len); |
| 510 | skb_reserve(skb, 4); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 511 | if (lmi == LMI_CISCO) { |
| 512 | skb->protocol = __constant_htons(NLPID_CISCO_LMI); |
| 513 | fr_hard_header(&skb, LMI_CISCO_DLCI); |
| 514 | } else { |
| 515 | skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI); |
| 516 | fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI); |
| 517 | } |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 518 | data = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | data[i++] = LMI_CALLREF; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 520 | data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY; |
| 521 | if (lmi == LMI_ANSI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | data[i++] = LMI_ANSI_LOCKSHIFT; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 523 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE : |
| 524 | LMI_ANSI_CISCO_REPTYPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | data[i++] = LMI_REPT_LEN; |
| 526 | data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 527 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | data[i++] = LMI_INTEG_LEN; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 529 | data[i++] = state(hdlc)->txseq = |
| 530 | fr_lmi_nextseq(state(hdlc)->txseq); |
| 531 | data[i++] = state(hdlc)->rxseq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 533 | if (dce && fullrep) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | while (pvc) { |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 535 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT : |
| 536 | LMI_ANSI_CISCO_PVCSTAT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | data[i++] = stat_len; |
| 538 | |
| 539 | /* LMI start/restart */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 540 | if (state(hdlc)->reliable && !pvc->state.exist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | pvc->state.exist = pvc->state.new = 1; |
| 542 | fr_log_dlci_active(pvc); |
| 543 | } |
| 544 | |
| 545 | /* ifconfig PVC up */ |
| 546 | if (pvc->open_count && !pvc->state.active && |
| 547 | pvc->state.exist && !pvc->state.new) { |
| 548 | pvc_carrier(1, pvc); |
| 549 | pvc->state.active = 1; |
| 550 | fr_log_dlci_active(pvc); |
| 551 | } |
| 552 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 553 | if (lmi == LMI_CISCO) { |
| 554 | data[i] = pvc->dlci >> 8; |
| 555 | data[i + 1] = pvc->dlci & 0xFF; |
| 556 | } else { |
| 557 | data[i] = (pvc->dlci >> 4) & 0x3F; |
| 558 | data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80; |
| 559 | data[i + 2] = 0x80; |
| 560 | } |
| 561 | |
| 562 | if (pvc->state.new) |
| 563 | data[i + 2] |= 0x08; |
| 564 | else if (pvc->state.active) |
| 565 | data[i + 2] |= 0x02; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | i += stat_len; |
| 568 | pvc = pvc->next; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | skb_put(skb, i); |
| 573 | skb->priority = TC_PRIO_CONTROL; |
| 574 | skb->dev = dev; |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 575 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | dev_queue_xmit(skb); |
| 578 | } |
| 579 | |
| 580 | |
| 581 | |
| 582 | static void fr_set_link_state(int reliable, struct net_device *dev) |
| 583 | { |
| 584 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 585 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 587 | state(hdlc)->reliable = reliable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (reliable) { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 589 | netif_dormant_off(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 590 | state(hdlc)->n391cnt = 0; /* Request full status */ |
| 591 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 593 | if (state(hdlc)->settings.lmi == LMI_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | while (pvc) { /* Activate all PVCs */ |
| 595 | pvc_carrier(1, pvc); |
| 596 | pvc->state.exist = pvc->state.active = 1; |
| 597 | pvc->state.new = 0; |
| 598 | pvc = pvc->next; |
| 599 | } |
| 600 | } |
| 601 | } else { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 602 | netif_dormant_on(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | while (pvc) { /* Deactivate all PVCs */ |
| 604 | pvc_carrier(0, pvc); |
| 605 | pvc->state.exist = pvc->state.active = 0; |
| 606 | pvc->state.new = 0; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 607 | if (!state(hdlc)->settings.dce) |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 608 | pvc->state.bandwidth = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | pvc = pvc->next; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | static void fr_timer(unsigned long arg) |
| 616 | { |
| 617 | struct net_device *dev = (struct net_device *)arg; |
| 618 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 619 | int i, cnt = 0, reliable; |
| 620 | u32 list; |
| 621 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 622 | if (state(hdlc)->settings.dce) { |
| 623 | reliable = state(hdlc)->request && |
| 624 | time_before(jiffies, state(hdlc)->last_poll + |
| 625 | state(hdlc)->settings.t392 * HZ); |
| 626 | state(hdlc)->request = 0; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 627 | } else { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 628 | state(hdlc)->last_errors <<= 1; /* Shift the list */ |
| 629 | if (state(hdlc)->request) { |
| 630 | if (state(hdlc)->reliable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | printk(KERN_INFO "%s: No LMI status reply " |
| 632 | "received\n", dev->name); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 633 | state(hdlc)->last_errors |= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 636 | list = state(hdlc)->last_errors; |
| 637 | for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | cnt += (list & 1); /* errors count */ |
| 639 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 640 | reliable = (cnt < state(hdlc)->settings.n392); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 643 | if (state(hdlc)->reliable != reliable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | printk(KERN_INFO "%s: Link %sreliable\n", dev->name, |
| 645 | reliable ? "" : "un"); |
| 646 | fr_set_link_state(reliable, dev); |
| 647 | } |
| 648 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 649 | if (state(hdlc)->settings.dce) |
| 650 | state(hdlc)->timer.expires = jiffies + |
| 651 | state(hdlc)->settings.t392 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | else { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 653 | if (state(hdlc)->n391cnt) |
| 654 | state(hdlc)->n391cnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 656 | fr_lmi_send(dev, state(hdlc)->n391cnt == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 658 | state(hdlc)->last_poll = jiffies; |
| 659 | state(hdlc)->request = 1; |
| 660 | state(hdlc)->timer.expires = jiffies + |
| 661 | state(hdlc)->settings.t391 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 664 | state(hdlc)->timer.function = fr_timer; |
| 665 | state(hdlc)->timer.data = arg; |
| 666 | add_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb) |
| 671 | { |
| 672 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | pvc_device *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | u8 rxseq, txseq; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 675 | int lmi = state(hdlc)->settings.lmi; |
| 676 | int dce = state(hdlc)->settings.dce; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 677 | int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 679 | if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH : |
| 680 | LMI_CCITT_CISCO_LENGTH)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | printk(KERN_INFO "%s: Short LMI frame\n", dev->name); |
| 682 | return 1; |
| 683 | } |
| 684 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 685 | if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI : |
| 686 | NLPID_CCITT_ANSI_LMI)) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 687 | printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n", |
| 688 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return 1; |
| 690 | } |
| 691 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 692 | if (skb->data[4] != LMI_CALLREF) { |
| 693 | printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n", |
| 694 | dev->name, skb->data[4]); |
| 695 | return 1; |
| 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 698 | if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) { |
| 699 | printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n", |
| 700 | dev->name, skb->data[5]); |
| 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | if (lmi == LMI_ANSI) { |
| 705 | if (skb->data[6] != LMI_ANSI_LOCKSHIFT) { |
| 706 | printk(KERN_INFO "%s: Not ANSI locking shift in LMI" |
| 707 | " message (0x%02X)\n", dev->name, skb->data[6]); |
| 708 | return 1; |
| 709 | } |
| 710 | i = 7; |
| 711 | } else |
| 712 | i = 6; |
| 713 | |
| 714 | if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE : |
| 715 | LMI_ANSI_CISCO_REPTYPE)) { |
| 716 | printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | dev->name, skb->data[i]); |
| 718 | return 1; |
| 719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 721 | if (skb->data[++i] != LMI_REPT_LEN) { |
| 722 | printk(KERN_INFO "%s: Invalid LMI Report type IE length" |
| 723 | " (%u)\n", dev->name, skb->data[i]); |
| 724 | return 1; |
| 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 727 | reptype = skb->data[++i]; |
| 728 | if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) { |
| 729 | printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n", |
| 730 | dev->name, reptype); |
| 731 | return 1; |
| 732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 734 | if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE : |
| 735 | LMI_ANSI_CISCO_ALIVE)) { |
| 736 | printk(KERN_INFO "%s: Not an LMI Link integrity verification" |
| 737 | " IE (0x%02X)\n", dev->name, skb->data[i]); |
| 738 | return 1; |
| 739 | } |
| 740 | |
| 741 | if (skb->data[++i] != LMI_INTEG_LEN) { |
| 742 | printk(KERN_INFO "%s: Invalid LMI Link integrity verification" |
| 743 | " IE length (%u)\n", dev->name, skb->data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return 1; |
| 745 | } |
| 746 | i++; |
| 747 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 748 | state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | rxseq = skb->data[i++]; /* Should confirm our sequence */ |
| 750 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 751 | txseq = state(hdlc)->txseq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 753 | if (dce) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 754 | state(hdlc)->last_poll = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | error = 0; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 757 | if (!state(hdlc)->reliable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | error = 1; |
| 759 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 760 | if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */ |
| 761 | state(hdlc)->n391cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | error = 1; |
| 763 | } |
| 764 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 765 | if (dce) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 766 | if (state(hdlc)->fullrep_sent && !error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | /* Stop sending full report - the last one has been confirmed by DTE */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 768 | state(hdlc)->fullrep_sent = 0; |
| 769 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | while (pvc) { |
| 771 | if (pvc->state.new) { |
| 772 | pvc->state.new = 0; |
| 773 | |
| 774 | /* Tell DTE that new PVC is now active */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 775 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | pvc = pvc->next; |
| 778 | } |
| 779 | } |
| 780 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 781 | if (state(hdlc)->dce_changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | reptype = LMI_FULLREP; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 783 | state(hdlc)->fullrep_sent = 1; |
| 784 | state(hdlc)->dce_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 787 | state(hdlc)->request = 1; /* got request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* DTE */ |
| 793 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 794 | state(hdlc)->request = 0; /* got response, no request pending */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | if (error) |
| 797 | return 0; |
| 798 | |
| 799 | if (reptype != LMI_FULLREP) |
| 800 | return 0; |
| 801 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 802 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | while (pvc) { |
| 805 | pvc->state.deleted = 1; |
| 806 | pvc = pvc->next; |
| 807 | } |
| 808 | |
| 809 | no_ram = 0; |
| 810 | while (skb->len >= i + 2 + stat_len) { |
| 811 | u16 dlci; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 812 | u32 bw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | unsigned int active, new; |
| 814 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 815 | if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT : |
| 816 | LMI_ANSI_CISCO_PVCSTAT)) { |
| 817 | printk(KERN_INFO "%s: Not an LMI PVC status IE" |
| 818 | " (0x%02X)\n", dev->name, skb->data[i]); |
| 819 | return 1; |
| 820 | } |
| 821 | |
| 822 | if (skb->data[++i] != stat_len) { |
| 823 | printk(KERN_INFO "%s: Invalid LMI PVC status IE length" |
| 824 | " (%u)\n", dev->name, skb->data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | return 1; |
| 826 | } |
| 827 | i++; |
| 828 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 829 | new = !! (skb->data[i + 2] & 0x08); |
| 830 | active = !! (skb->data[i + 2] & 0x02); |
| 831 | if (lmi == LMI_CISCO) { |
| 832 | dlci = (skb->data[i] << 8) | skb->data[i + 1]; |
| 833 | bw = (skb->data[i + 3] << 16) | |
| 834 | (skb->data[i + 4] << 8) | |
| 835 | (skb->data[i + 5]); |
| 836 | } else { |
| 837 | dlci = ((skb->data[i] & 0x3F) << 4) | |
| 838 | ((skb->data[i + 1] & 0x78) >> 3); |
| 839 | bw = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | pvc = add_pvc(dev, dlci); |
| 843 | |
| 844 | if (!pvc && !no_ram) { |
| 845 | printk(KERN_WARNING |
| 846 | "%s: Memory squeeze on fr_lmi_recv()\n", |
| 847 | dev->name); |
| 848 | no_ram = 1; |
| 849 | } |
| 850 | |
| 851 | if (pvc) { |
| 852 | pvc->state.exist = 1; |
| 853 | pvc->state.deleted = 0; |
| 854 | if (active != pvc->state.active || |
| 855 | new != pvc->state.new || |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 856 | bw != pvc->state.bandwidth || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | !pvc->state.exist) { |
| 858 | pvc->state.new = new; |
| 859 | pvc->state.active = active; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 860 | pvc->state.bandwidth = bw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | pvc_carrier(active, pvc); |
| 862 | fr_log_dlci_active(pvc); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | i += stat_len; |
| 867 | } |
| 868 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 869 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | while (pvc) { |
| 872 | if (pvc->state.deleted && pvc->state.exist) { |
| 873 | pvc_carrier(0, pvc); |
| 874 | pvc->state.active = pvc->state.new = 0; |
| 875 | pvc->state.exist = 0; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 876 | pvc->state.bandwidth = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | fr_log_dlci_active(pvc); |
| 878 | } |
| 879 | pvc = pvc->next; |
| 880 | } |
| 881 | |
| 882 | /* Next full report after N391 polls */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 883 | state(hdlc)->n391cnt = state(hdlc)->settings.n391; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | static int fr_rx(struct sk_buff *skb) |
| 890 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 891 | struct net_device *frad = skb->dev; |
| 892 | hdlc_device *hdlc = dev_to_hdlc(frad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | fr_hdr *fh = (fr_hdr*)skb->data; |
| 894 | u8 *data = skb->data; |
| 895 | u16 dlci; |
| 896 | pvc_device *pvc; |
| 897 | struct net_device *dev = NULL; |
| 898 | |
| 899 | if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI) |
| 900 | goto rx_error; |
| 901 | |
| 902 | dlci = q922_to_dlci(skb->data); |
| 903 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 904 | if ((dlci == LMI_CCITT_ANSI_DLCI && |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 905 | (state(hdlc)->settings.lmi == LMI_ANSI || |
| 906 | state(hdlc)->settings.lmi == LMI_CCITT)) || |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 907 | (dlci == LMI_CISCO_DLCI && |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 908 | state(hdlc)->settings.lmi == LMI_CISCO)) { |
| 909 | if (fr_lmi_recv(frad, skb)) |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 910 | goto rx_error; |
| 911 | dev_kfree_skb_any(skb); |
| 912 | return NET_RX_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | pvc = find_pvc(hdlc, dlci); |
| 916 | if (!pvc) { |
| 917 | #ifdef DEBUG_PKT |
| 918 | printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 919 | frad->name, dlci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | #endif |
| 921 | dev_kfree_skb_any(skb); |
| 922 | return NET_RX_DROP; |
| 923 | } |
| 924 | |
| 925 | if (pvc->state.fecn != fh->fecn) { |
| 926 | #ifdef DEBUG_ECN |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 927 | printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | dlci, fh->fecn ? "N" : "FF"); |
| 929 | #endif |
| 930 | pvc->state.fecn ^= 1; |
| 931 | } |
| 932 | |
| 933 | if (pvc->state.becn != fh->becn) { |
| 934 | #ifdef DEBUG_ECN |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 935 | printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | dlci, fh->becn ? "N" : "FF"); |
| 937 | #endif |
| 938 | pvc->state.becn ^= 1; |
| 939 | } |
| 940 | |
| 941 | |
| 942 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 943 | frad->stats.rx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return NET_RX_DROP; |
| 945 | } |
| 946 | |
| 947 | if (data[3] == NLPID_IP) { |
| 948 | skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */ |
| 949 | dev = pvc->main; |
| 950 | skb->protocol = htons(ETH_P_IP); |
| 951 | |
| 952 | } else if (data[3] == NLPID_IPV6) { |
| 953 | skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */ |
| 954 | dev = pvc->main; |
| 955 | skb->protocol = htons(ETH_P_IPV6); |
| 956 | |
| 957 | } else if (skb->len > 10 && data[3] == FR_PAD && |
| 958 | data[4] == NLPID_SNAP && data[5] == FR_PAD) { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 959 | u16 oui = ntohs(*(__be16*)(data + 6)); |
| 960 | u16 pid = ntohs(*(__be16*)(data + 8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | skb_pull(skb, 10); |
| 962 | |
| 963 | switch ((((u32)oui) << 16) | pid) { |
| 964 | case ETH_P_ARP: /* routed frame with SNAP */ |
| 965 | case ETH_P_IPX: |
| 966 | case ETH_P_IP: /* a long variant */ |
| 967 | case ETH_P_IPV6: |
| 968 | dev = pvc->main; |
| 969 | skb->protocol = htons(pid); |
| 970 | break; |
| 971 | |
| 972 | case 0x80C20007: /* bridged Ethernet frame */ |
| 973 | if ((dev = pvc->ether) != NULL) |
| 974 | skb->protocol = eth_type_trans(skb, dev); |
| 975 | break; |
| 976 | |
| 977 | default: |
| 978 | printk(KERN_INFO "%s: Unsupported protocol, OUI=%x " |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 979 | "PID=%x\n", frad->name, oui, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | dev_kfree_skb_any(skb); |
| 981 | return NET_RX_DROP; |
| 982 | } |
| 983 | } else { |
| 984 | printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x " |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 985 | "length = %i\n", frad->name, data[3], skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | dev_kfree_skb_any(skb); |
| 987 | return NET_RX_DROP; |
| 988 | } |
| 989 | |
| 990 | if (dev) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 991 | dev->stats.rx_packets++; /* PVC traffic */ |
| 992 | dev->stats.rx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (pvc->state.becn) |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 994 | dev->stats.rx_compressed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | netif_rx(skb); |
| 996 | return NET_RX_SUCCESS; |
| 997 | } else { |
| 998 | dev_kfree_skb_any(skb); |
| 999 | return NET_RX_DROP; |
| 1000 | } |
| 1001 | |
| 1002 | rx_error: |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 1003 | frad->stats.rx_errors++; /* Mark error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | dev_kfree_skb_any(skb); |
| 1005 | return NET_RX_DROP; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | static void fr_start(struct net_device *dev) |
| 1011 | { |
| 1012 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1013 | #ifdef DEBUG_LINK |
| 1014 | printk(KERN_DEBUG "fr_start\n"); |
| 1015 | #endif |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1016 | if (state(hdlc)->settings.lmi != LMI_NONE) { |
| 1017 | state(hdlc)->reliable = 0; |
| 1018 | state(hdlc)->dce_changed = 1; |
| 1019 | state(hdlc)->request = 0; |
| 1020 | state(hdlc)->fullrep_sent = 0; |
| 1021 | state(hdlc)->last_errors = 0xFFFFFFFF; |
| 1022 | state(hdlc)->n391cnt = 0; |
| 1023 | state(hdlc)->txseq = state(hdlc)->rxseq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1025 | init_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | /* First poll after 1 s */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1027 | state(hdlc)->timer.expires = jiffies + HZ; |
| 1028 | state(hdlc)->timer.function = fr_timer; |
| 1029 | state(hdlc)->timer.data = (unsigned long)dev; |
| 1030 | add_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } else |
| 1032 | fr_set_link_state(1, dev); |
| 1033 | } |
| 1034 | |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | static void fr_stop(struct net_device *dev) |
| 1037 | { |
| 1038 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1039 | #ifdef DEBUG_LINK |
| 1040 | printk(KERN_DEBUG "fr_stop\n"); |
| 1041 | #endif |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1042 | if (state(hdlc)->settings.lmi != LMI_NONE) |
| 1043 | del_timer_sync(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | fr_set_link_state(0, dev); |
| 1045 | } |
| 1046 | |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | static void fr_close(struct net_device *dev) |
| 1049 | { |
| 1050 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1051 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | while (pvc) { /* Shutdown all PVCs for this FRAD */ |
| 1054 | if (pvc->main) |
| 1055 | dev_close(pvc->main); |
| 1056 | if (pvc->ether) |
| 1057 | dev_close(pvc->ether); |
| 1058 | pvc = pvc->next; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1062 | |
| 1063 | static void pvc_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | { |
| 1065 | dev->type = ARPHRD_DLCI; |
| 1066 | dev->flags = IFF_POINTOPOINT; |
| 1067 | dev->hard_header_len = 10; |
| 1068 | dev->addr_len = 2; |
| 1069 | } |
| 1070 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1071 | static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1073 | hdlc_device *hdlc = dev_to_hdlc(frad); |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1074 | pvc_device *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | struct net_device *dev; |
| 1076 | int result, used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1078 | if ((pvc = add_pvc(frad, dlci)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1080 | frad->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | return -ENOBUFS; |
| 1082 | } |
| 1083 | |
| 1084 | if (*get_dev_p(pvc, type)) |
| 1085 | return -EEXIST; |
| 1086 | |
| 1087 | used = pvc_is_used(pvc); |
| 1088 | |
| 1089 | if (type == ARPHRD_ETHER) |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1090 | dev = alloc_netdev(0, "pvceth%d", ether_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | else |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1092 | dev = alloc_netdev(0, "pvc%d", pvc_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
| 1094 | if (!dev) { |
| 1095 | printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1096 | frad->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | delete_unused_pvcs(hdlc); |
| 1098 | return -ENOBUFS; |
| 1099 | } |
| 1100 | |
Krzysztof Halasa | 47eaa26 | 2008-02-01 22:39:50 +0100 | [diff] [blame] | 1101 | if (type == ARPHRD_ETHER) |
| 1102 | random_ether_addr(dev->dev_addr); |
| 1103 | else { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 1104 | *(__be16*)dev->dev_addr = htons(dlci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | dlci_to_q922(dev->broadcast, dlci); |
| 1106 | } |
| 1107 | dev->hard_start_xmit = pvc_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | dev->open = pvc_open; |
| 1109 | dev->stop = pvc_close; |
| 1110 | dev->do_ioctl = pvc_ioctl; |
| 1111 | dev->change_mtu = pvc_change_mtu; |
| 1112 | dev->mtu = HDLC_MAX_MTU; |
| 1113 | dev->tx_queue_len = 0; |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1114 | dev->priv = pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | result = dev_alloc_name(dev, dev->name); |
| 1117 | if (result < 0) { |
| 1118 | free_netdev(dev); |
| 1119 | delete_unused_pvcs(hdlc); |
| 1120 | return result; |
| 1121 | } |
| 1122 | |
| 1123 | if (register_netdevice(dev) != 0) { |
| 1124 | free_netdev(dev); |
| 1125 | delete_unused_pvcs(hdlc); |
| 1126 | return -EIO; |
| 1127 | } |
| 1128 | |
| 1129 | dev->destructor = free_netdev; |
| 1130 | *get_dev_p(pvc, type) = dev; |
| 1131 | if (!used) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1132 | state(hdlc)->dce_changed = 1; |
| 1133 | state(hdlc)->dce_pvc_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type) |
| 1141 | { |
| 1142 | pvc_device *pvc; |
| 1143 | struct net_device *dev; |
| 1144 | |
| 1145 | if ((pvc = find_pvc(hdlc, dlci)) == NULL) |
| 1146 | return -ENOENT; |
| 1147 | |
| 1148 | if ((dev = *get_dev_p(pvc, type)) == NULL) |
| 1149 | return -ENOENT; |
| 1150 | |
| 1151 | if (dev->flags & IFF_UP) |
| 1152 | return -EBUSY; /* PVC in use */ |
| 1153 | |
| 1154 | unregister_netdevice(dev); /* the destructor will free_netdev(dev) */ |
| 1155 | *get_dev_p(pvc, type) = NULL; |
| 1156 | |
| 1157 | if (!pvc_is_used(pvc)) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1158 | state(hdlc)->dce_pvc_count--; |
| 1159 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
| 1161 | delete_unused_pvcs(hdlc); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1167 | static void fr_destroy(struct net_device *frad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1169 | hdlc_device *hdlc = dev_to_hdlc(frad); |
| 1170 | pvc_device *pvc = state(hdlc)->first_pvc; |
| 1171 | state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */ |
| 1172 | state(hdlc)->dce_pvc_count = 0; |
| 1173 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
| 1175 | while (pvc) { |
| 1176 | pvc_device *next = pvc->next; |
| 1177 | /* destructors will free_netdev() main and ether */ |
| 1178 | if (pvc->main) |
| 1179 | unregister_netdevice(pvc->main); |
| 1180 | |
| 1181 | if (pvc->ether) |
| 1182 | unregister_netdevice(pvc->ether); |
| 1183 | |
| 1184 | kfree(pvc); |
| 1185 | pvc = next; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1190 | static struct hdlc_proto proto = { |
| 1191 | .close = fr_close, |
| 1192 | .start = fr_start, |
| 1193 | .stop = fr_stop, |
| 1194 | .detach = fr_destroy, |
| 1195 | .ioctl = fr_ioctl, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 1196 | .netif_rx = fr_rx, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1197 | .module = THIS_MODULE, |
| 1198 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1200 | |
| 1201 | static int fr_ioctl(struct net_device *dev, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | { |
| 1203 | fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr; |
| 1204 | const size_t size = sizeof(fr_proto); |
| 1205 | fr_proto new_settings; |
| 1206 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1207 | fr_proto_pvc pvc; |
| 1208 | int result; |
| 1209 | |
| 1210 | switch (ifr->ifr_settings.type) { |
| 1211 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1212 | if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */ |
| 1213 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | ifr->ifr_settings.type = IF_PROTO_FR; |
| 1215 | if (ifr->ifr_settings.size < size) { |
| 1216 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 1217 | return -ENOBUFS; |
| 1218 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1219 | if (copy_to_user(fr_s, &state(hdlc)->settings, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return -EFAULT; |
| 1221 | return 0; |
| 1222 | |
| 1223 | case IF_PROTO_FR: |
| 1224 | if(!capable(CAP_NET_ADMIN)) |
| 1225 | return -EPERM; |
| 1226 | |
| 1227 | if(dev->flags & IFF_UP) |
| 1228 | return -EBUSY; |
| 1229 | |
| 1230 | if (copy_from_user(&new_settings, fr_s, size)) |
| 1231 | return -EFAULT; |
| 1232 | |
| 1233 | if (new_settings.lmi == LMI_DEFAULT) |
| 1234 | new_settings.lmi = LMI_ANSI; |
| 1235 | |
| 1236 | if ((new_settings.lmi != LMI_NONE && |
| 1237 | new_settings.lmi != LMI_ANSI && |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 1238 | new_settings.lmi != LMI_CCITT && |
| 1239 | new_settings.lmi != LMI_CISCO) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | new_settings.t391 < 1 || |
| 1241 | new_settings.t392 < 2 || |
| 1242 | new_settings.n391 < 1 || |
| 1243 | new_settings.n392 < 1 || |
| 1244 | new_settings.n393 < new_settings.n392 || |
| 1245 | new_settings.n393 > 32 || |
| 1246 | (new_settings.dce != 0 && |
| 1247 | new_settings.dce != 1)) |
| 1248 | return -EINVAL; |
| 1249 | |
| 1250 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); |
| 1251 | if (result) |
| 1252 | return result; |
| 1253 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1254 | if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */ |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 1255 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1256 | sizeof(struct frad_state)); |
| 1257 | if (result) |
| 1258 | return result; |
| 1259 | state(hdlc)->first_pvc = NULL; |
| 1260 | state(hdlc)->dce_pvc_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1262 | memcpy(&state(hdlc)->settings, &new_settings, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | dev->hard_start_xmit = hdlc->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | dev->type = ARPHRD_FRAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return 0; |
| 1267 | |
| 1268 | case IF_PROTO_FR_ADD_PVC: |
| 1269 | case IF_PROTO_FR_DEL_PVC: |
| 1270 | case IF_PROTO_FR_ADD_ETH_PVC: |
| 1271 | case IF_PROTO_FR_DEL_ETH_PVC: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1272 | if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */ |
| 1273 | return -EINVAL; |
| 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | if(!capable(CAP_NET_ADMIN)) |
| 1276 | return -EPERM; |
| 1277 | |
| 1278 | if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc, |
| 1279 | sizeof(fr_proto_pvc))) |
| 1280 | return -EFAULT; |
| 1281 | |
| 1282 | if (pvc.dlci <= 0 || pvc.dlci >= 1024) |
| 1283 | return -EINVAL; /* Only 10 bits, DLCI 0 reserved */ |
| 1284 | |
| 1285 | if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC || |
| 1286 | ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC) |
| 1287 | result = ARPHRD_ETHER; /* bridged Ethernet device */ |
| 1288 | else |
| 1289 | result = ARPHRD_DLCI; |
| 1290 | |
| 1291 | if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC || |
| 1292 | ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC) |
| 1293 | return fr_add_pvc(dev, pvc.dlci, result); |
| 1294 | else |
| 1295 | return fr_del_pvc(hdlc, pvc.dlci, result); |
| 1296 | } |
| 1297 | |
| 1298 | return -EINVAL; |
| 1299 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1300 | |
| 1301 | |
| 1302 | static int __init mod_init(void) |
| 1303 | { |
| 1304 | register_hdlc_protocol(&proto); |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | static void __exit mod_exit(void) |
| 1310 | { |
| 1311 | unregister_hdlc_protocol(&proto); |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | module_init(mod_init); |
| 1316 | module_exit(mod_exit); |
| 1317 | |
| 1318 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 1319 | MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC"); |
| 1320 | MODULE_LICENSE("GPL v2"); |