Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* net/atm/clip.c - RFC1577 Classical IP over ATM */ |
| 2 | |
| 3 | /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/string.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/kernel.h> /* for UINT_MAX */ |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/netdevice.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/wait.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/if_arp.h> /* for some manifest constants */ |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/atm.h> |
| 17 | #include <linux/atmdev.h> |
| 18 | #include <linux/atmclip.h> |
| 19 | #include <linux/atmarp.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/ip.h> /* for net/route.h */ |
| 22 | #include <linux/in.h> /* for struct sockaddr_in */ |
| 23 | #include <linux/if.h> /* for IFF_UP */ |
| 24 | #include <linux/inetdevice.h> |
| 25 | #include <linux/bitops.h> |
| 26 | #include <linux/proc_fs.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/rcupdate.h> |
| 29 | #include <linux/jhash.h> |
| 30 | #include <net/route.h> /* for struct rtable and routing */ |
| 31 | #include <net/icmp.h> /* icmp_send */ |
| 32 | #include <asm/param.h> /* for HZ */ |
| 33 | #include <asm/byteorder.h> /* for htons etc. */ |
| 34 | #include <asm/system.h> /* save/restore_flags */ |
| 35 | #include <asm/uaccess.h> |
| 36 | #include <asm/atomic.h> |
| 37 | |
| 38 | #include "common.h" |
| 39 | #include "resources.h" |
| 40 | #include "ipcommon.h" |
| 41 | #include <net/atmclip.h> |
| 42 | |
| 43 | |
| 44 | #if 0 |
| 45 | #define DPRINTK(format,args...) printk(format,##args) |
| 46 | #else |
| 47 | #define DPRINTK(format,args...) |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | static struct net_device *clip_devs; |
| 52 | static struct atm_vcc *atmarpd; |
| 53 | static struct neigh_table clip_tbl; |
| 54 | static struct timer_list idle_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 56 | static int to_atmarpd(enum atmarp_ctrl_type type, int itf, unsigned long ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | struct sock *sk; |
| 59 | struct atmarp_ctrl *ctrl; |
| 60 | struct sk_buff *skb; |
| 61 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 62 | DPRINTK("to_atmarpd(%d)\n", type); |
| 63 | if (!atmarpd) |
| 64 | return -EUNATCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 66 | if (!skb) |
| 67 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl)); |
| 69 | ctrl->type = type; |
| 70 | ctrl->itf_num = itf; |
| 71 | ctrl->ip = ip; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 72 | atm_force_charge(atmarpd, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | sk = sk_atm(atmarpd); |
| 75 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 76 | sk->sk_data_ready(sk, skb->len); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 80 | static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 82 | DPRINTK("link_vcc %p to entry %p (neigh %p)\n", clip_vcc, entry, |
| 83 | entry->neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | clip_vcc->entry = entry; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 85 | clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | clip_vcc->next = entry->vccs; |
| 87 | entry->vccs = clip_vcc; |
| 88 | entry->neigh->used = jiffies; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static void unlink_clip_vcc(struct clip_vcc *clip_vcc) |
| 92 | { |
| 93 | struct atmarp_entry *entry = clip_vcc->entry; |
| 94 | struct clip_vcc **walk; |
| 95 | |
| 96 | if (!entry) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 97 | printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n", clip_vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 100 | netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | entry->neigh->used = jiffies; |
| 102 | for (walk = &entry->vccs; *walk; walk = &(*walk)->next) |
| 103 | if (*walk == clip_vcc) { |
| 104 | int error; |
| 105 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 106 | *walk = clip_vcc->next; /* atomic */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | clip_vcc->entry = NULL; |
| 108 | if (clip_vcc->xoff) |
| 109 | netif_wake_queue(entry->neigh->dev); |
| 110 | if (entry->vccs) |
| 111 | goto out; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 112 | entry->expires = jiffies - 1; |
| 113 | /* force resolution or expiration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | error = neigh_update(entry->neigh, NULL, NUD_NONE, |
| 115 | NEIGH_UPDATE_F_ADMIN); |
| 116 | if (error) |
| 117 | printk(KERN_CRIT "unlink_clip_vcc: " |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 118 | "neigh_update failed with %d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | goto out; |
| 120 | } |
| 121 | printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc " |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 122 | "0x%p)\n", entry, clip_vcc); |
| 123 | out: |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 124 | netif_tx_unlock_bh(entry->neigh->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* The neighbour entry n->lock is held. */ |
| 128 | static int neigh_check_cb(struct neighbour *n) |
| 129 | { |
| 130 | struct atmarp_entry *entry = NEIGH2ENTRY(n); |
| 131 | struct clip_vcc *cv; |
| 132 | |
| 133 | for (cv = entry->vccs; cv; cv = cv->next) { |
| 134 | unsigned long exp = cv->last_use + cv->idle_timeout; |
| 135 | |
| 136 | if (cv->idle_timeout && time_after(jiffies, exp)) { |
| 137 | DPRINTK("releasing vcc %p->%p of entry %p\n", |
| 138 | cv, cv->vcc, entry); |
| 139 | vcc_release_async(cv->vcc, -ETIMEDOUT); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (entry->vccs || time_before(jiffies, entry->expires)) |
| 144 | return 0; |
| 145 | |
| 146 | if (atomic_read(&n->refcnt) > 1) { |
| 147 | struct sk_buff *skb; |
| 148 | |
| 149 | DPRINTK("destruction postponed with ref %d\n", |
| 150 | atomic_read(&n->refcnt)); |
| 151 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 152 | while ((skb = skb_dequeue(&n->arp_queue)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | dev_kfree_skb(skb); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 158 | DPRINTK("expired neigh %p\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | static void idle_timer_check(unsigned long dummy) |
| 163 | { |
| 164 | write_lock(&clip_tbl.lock); |
| 165 | __neigh_for_each_release(&clip_tbl, neigh_check_cb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 166 | mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | write_unlock(&clip_tbl.lock); |
| 168 | } |
| 169 | |
| 170 | static int clip_arp_rcv(struct sk_buff *skb) |
| 171 | { |
| 172 | struct atm_vcc *vcc; |
| 173 | |
| 174 | DPRINTK("clip_arp_rcv\n"); |
| 175 | vcc = ATM_SKB(skb)->vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 176 | if (!vcc || !atm_charge(vcc, skb->truesize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | dev_kfree_skb_any(skb); |
| 178 | return 0; |
| 179 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 180 | DPRINTK("pushing to %p\n", vcc); |
| 181 | DPRINTK("using %p\n", CLIP_VCC(vcc)->old_push); |
| 182 | CLIP_VCC(vcc)->old_push(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static const unsigned char llc_oui[] = { |
| 187 | 0xaa, /* DSAP: non-ISO */ |
| 188 | 0xaa, /* SSAP: non-ISO */ |
| 189 | 0x03, /* Ctrl: Unnumbered Information Command PDU */ |
| 190 | 0x00, /* OUI: EtherType */ |
| 191 | 0x00, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 192 | 0x00 |
| 193 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 195 | static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | struct clip_vcc *clip_vcc = CLIP_VCC(vcc); |
| 198 | |
| 199 | DPRINTK("clip push\n"); |
| 200 | if (!skb) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 201 | DPRINTK("removing VCC %p\n", clip_vcc); |
| 202 | if (clip_vcc->entry) |
| 203 | unlink_clip_vcc(clip_vcc); |
| 204 | clip_vcc->old_push(vcc, NULL); /* pass on the bad news */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | kfree(clip_vcc); |
| 206 | return; |
| 207 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 208 | atm_return(vcc, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 210 | /* clip_vcc->entry == NULL if we don't have an IP address yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (!skb->dev) { |
| 212 | dev_kfree_skb_any(skb); |
| 213 | return; |
| 214 | } |
| 215 | ATM_SKB(skb)->vcc = vcc; |
| 216 | skb->mac.raw = skb->data; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 217 | if (!clip_vcc->encap |
| 218 | || skb->len < RFC1483LLC_LEN |
| 219 | || memcmp(skb->data, llc_oui, sizeof (llc_oui))) |
| 220 | skb->protocol = htons(ETH_P_IP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | else { |
| 222 | skb->protocol = ((u16 *) skb->data)[3]; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 223 | skb_pull(skb, RFC1483LLC_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (skb->protocol == htons(ETH_P_ARP)) { |
| 225 | PRIV(skb->dev)->stats.rx_packets++; |
| 226 | PRIV(skb->dev)->stats.rx_bytes += skb->len; |
| 227 | clip_arp_rcv(skb); |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | clip_vcc->last_use = jiffies; |
| 232 | PRIV(skb->dev)->stats.rx_packets++; |
| 233 | PRIV(skb->dev)->stats.rx_bytes += skb->len; |
| 234 | memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data)); |
| 235 | netif_rx(skb); |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that |
| 240 | * clip_pop is atomic with respect to the critical section in clip_start_xmit. |
| 241 | */ |
| 242 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 243 | static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
| 245 | struct clip_vcc *clip_vcc = CLIP_VCC(vcc); |
| 246 | struct net_device *dev = skb->dev; |
| 247 | int old; |
| 248 | unsigned long flags; |
| 249 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 250 | DPRINTK("clip_pop(vcc %p)\n", vcc); |
| 251 | clip_vcc->old_pop(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* skb->dev == NULL in outbound ARP packets */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 253 | if (!dev) |
| 254 | return; |
| 255 | spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags); |
| 256 | if (atm_may_send(vcc, 0)) { |
| 257 | old = xchg(&clip_vcc->xoff, 0); |
| 258 | if (old) |
| 259 | netif_wake_queue(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 261 | spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | static void clip_neigh_destroy(struct neighbour *neigh) |
| 265 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 266 | DPRINTK("clip_neigh_destroy (neigh %p)\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (NEIGH2ENTRY(neigh)->vccs) |
| 268 | printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n"); |
| 269 | NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef; |
| 270 | } |
| 271 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 272 | static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 274 | DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n", neigh, skb); |
| 275 | to_atmarpd(act_need, PRIV(neigh->dev)->number, NEIGH2ENTRY(neigh)->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 278 | static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | #ifndef CONFIG_ATM_CLIP_NO_ICMP |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 281 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | #endif |
| 283 | kfree_skb(skb); |
| 284 | } |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | static struct neigh_ops clip_neigh_ops = { |
| 287 | .family = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | .solicit = clip_neigh_solicit, |
| 289 | .error_report = clip_neigh_error, |
| 290 | .output = dev_queue_xmit, |
| 291 | .connected_output = dev_queue_xmit, |
| 292 | .hh_output = dev_queue_xmit, |
| 293 | .queue_xmit = dev_queue_xmit, |
| 294 | }; |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | static int clip_constructor(struct neighbour *neigh) |
| 297 | { |
| 298 | struct atmarp_entry *entry = NEIGH2ENTRY(neigh); |
| 299 | struct net_device *dev = neigh->dev; |
| 300 | struct in_device *in_dev; |
| 301 | struct neigh_parms *parms; |
| 302 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 303 | DPRINTK("clip_constructor (neigh %p, entry %p)\n", neigh, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | neigh->type = inet_addr_type(entry->ip); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 305 | if (neigh->type != RTN_UNICAST) |
| 306 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 309 | in_dev = __in_dev_get_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (!in_dev) { |
| 311 | rcu_read_unlock(); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | parms = in_dev->arp_parms; |
| 316 | __neigh_parms_put(neigh->parms); |
| 317 | neigh->parms = neigh_parms_clone(parms); |
| 318 | rcu_read_unlock(); |
| 319 | |
| 320 | neigh->ops = &clip_neigh_ops; |
| 321 | neigh->output = neigh->nud_state & NUD_VALID ? |
| 322 | neigh->ops->connected_output : neigh->ops->output; |
| 323 | entry->neigh = neigh; |
| 324 | entry->vccs = NULL; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 325 | entry->expires = jiffies - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static u32 clip_hash(const void *pkey, const struct net_device *dev) |
| 330 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 331 | return jhash_2words(*(u32 *) pkey, dev->ifindex, clip_tbl.hash_rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static struct neigh_table clip_tbl = { |
| 335 | .family = AF_INET, |
| 336 | .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry), |
| 337 | .key_len = 4, |
| 338 | .hash = clip_hash, |
| 339 | .constructor = clip_constructor, |
| 340 | .id = "clip_arp_cache", |
| 341 | |
| 342 | /* parameters are copied from ARP ... */ |
| 343 | .parms = { |
| 344 | .tbl = &clip_tbl, |
David S. Miller | aa837b5 | 2006-03-20 22:30:23 -0800 | [diff] [blame] | 345 | .neigh_destructor = clip_neigh_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .base_reachable_time = 30 * HZ, |
| 347 | .retrans_time = 1 * HZ, |
| 348 | .gc_staletime = 60 * HZ, |
| 349 | .reachable_time = 30 * HZ, |
| 350 | .delay_probe_time = 5 * HZ, |
| 351 | .queue_len = 3, |
| 352 | .ucast_probes = 3, |
| 353 | .mcast_probes = 3, |
| 354 | .anycast_delay = 1 * HZ, |
| 355 | .proxy_delay = (8 * HZ) / 10, |
| 356 | .proxy_qlen = 64, |
| 357 | .locktime = 1 * HZ, |
| 358 | }, |
| 359 | .gc_interval = 30 * HZ, |
| 360 | .gc_thresh1 = 128, |
| 361 | .gc_thresh2 = 512, |
| 362 | .gc_thresh3 = 1024, |
| 363 | }; |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */ |
| 366 | |
| 367 | /* |
| 368 | * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means |
| 369 | * to allocate the neighbour entry but not to ask atmarpd for resolution. Also, |
| 370 | * don't increment the usage count. This is used to create entries in |
| 371 | * clip_setentry. |
| 372 | */ |
| 373 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 374 | static int clip_encap(struct atm_vcc *vcc, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | CLIP_VCC(vcc)->encap = mode; |
| 377 | return 0; |
| 378 | } |
| 379 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 380 | static int clip_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | struct clip_priv *clip_priv = PRIV(dev); |
| 383 | struct atmarp_entry *entry; |
| 384 | struct atm_vcc *vcc; |
| 385 | int old; |
| 386 | unsigned long flags; |
| 387 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 388 | DPRINTK("clip_start_xmit (skb %p)\n", skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (!skb->dst) { |
| 390 | printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n"); |
| 391 | dev_kfree_skb(skb); |
| 392 | clip_priv->stats.tx_dropped++; |
| 393 | return 0; |
| 394 | } |
| 395 | if (!skb->dst->neighbour) { |
| 396 | #if 0 |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 397 | skb->dst->neighbour = clip_find_neighbour(skb->dst, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!skb->dst->neighbour) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 399 | dev_kfree_skb(skb); /* lost that one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | clip_priv->stats.tx_dropped++; |
| 401 | return 0; |
| 402 | } |
| 403 | #endif |
| 404 | printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n"); |
| 405 | dev_kfree_skb(skb); |
| 406 | clip_priv->stats.tx_dropped++; |
| 407 | return 0; |
| 408 | } |
| 409 | entry = NEIGH2ENTRY(skb->dst->neighbour); |
| 410 | if (!entry->vccs) { |
| 411 | if (time_after(jiffies, entry->expires)) { |
| 412 | /* should be resolved */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 413 | entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ; |
| 414 | to_atmarpd(act_need, PRIV(dev)->number, entry->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 417 | skb_queue_tail(&entry->neigh->arp_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | else { |
| 419 | dev_kfree_skb(skb); |
| 420 | clip_priv->stats.tx_dropped++; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 424 | DPRINTK("neigh %p, vccs %p\n", entry, entry->vccs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 426 | DPRINTK("using neighbour %p, vcc %p\n", skb->dst->neighbour, vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (entry->vccs->encap) { |
| 428 | void *here; |
| 429 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 430 | here = skb_push(skb, RFC1483LLC_LEN); |
| 431 | memcpy(here, llc_oui, sizeof(llc_oui)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | ((u16 *) here)[3] = skb->protocol; |
| 433 | } |
| 434 | atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc); |
| 435 | ATM_SKB(skb)->atm_options = vcc->atm_options; |
| 436 | entry->vccs->last_use = jiffies; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 437 | DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev); |
| 438 | old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (old) { |
| 440 | printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n"); |
| 441 | return 0; |
| 442 | } |
| 443 | clip_priv->stats.tx_packets++; |
| 444 | clip_priv->stats.tx_bytes += skb->len; |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 445 | vcc->send(vcc, skb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 446 | if (atm_may_send(vcc, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | entry->vccs->xoff = 0; |
| 448 | return 0; |
| 449 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 450 | spin_lock_irqsave(&clip_priv->xoff_lock, flags); |
| 451 | netif_stop_queue(dev); /* XOFF -> throttle immediately */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | barrier(); |
| 453 | if (!entry->vccs->xoff) |
| 454 | netif_start_queue(dev); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 455 | /* Oh, we just raced with clip_pop. netif_start_queue should be |
| 456 | good enough, because nothing should really be asleep because |
| 457 | of the brief netif_stop_queue. If this isn't true or if it |
| 458 | changes, use netif_wake_queue instead. */ |
| 459 | spin_unlock_irqrestore(&clip_priv->xoff_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | static struct net_device_stats *clip_get_stats(struct net_device *dev) |
| 464 | { |
| 465 | return &PRIV(dev)->stats; |
| 466 | } |
| 467 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 468 | static int clip_mkip(struct atm_vcc *vcc, int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | struct clip_vcc *clip_vcc; |
| 471 | struct sk_buff_head copy; |
| 472 | struct sk_buff *skb; |
| 473 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 474 | if (!vcc->push) |
| 475 | return -EBADFD; |
| 476 | clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL); |
| 477 | if (!clip_vcc) |
| 478 | return -ENOMEM; |
| 479 | DPRINTK("mkip clip_vcc %p vcc %p\n", clip_vcc, vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | clip_vcc->vcc = vcc; |
| 481 | vcc->user_back = clip_vcc; |
| 482 | set_bit(ATM_VF_IS_CLIP, &vcc->flags); |
| 483 | clip_vcc->entry = NULL; |
| 484 | clip_vcc->xoff = 0; |
| 485 | clip_vcc->encap = 1; |
| 486 | clip_vcc->last_use = jiffies; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 487 | clip_vcc->idle_timeout = timeout * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | clip_vcc->old_push = vcc->push; |
| 489 | clip_vcc->old_pop = vcc->pop; |
| 490 | vcc->push = clip_push; |
| 491 | vcc->pop = clip_pop; |
| 492 | skb_queue_head_init(©); |
| 493 | skb_migrate(&sk_atm(vcc)->sk_receive_queue, ©); |
| 494 | /* re-process everything received between connection setup and MKIP */ |
| 495 | while ((skb = skb_dequeue(©)) != NULL) |
| 496 | if (!clip_devs) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 497 | atm_return(vcc, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | kfree_skb(skb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 499 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | unsigned int len = skb->len; |
| 501 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 502 | clip_push(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | PRIV(skb->dev)->stats.rx_packets--; |
| 504 | PRIV(skb->dev)->stats.rx_bytes -= len; |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 509 | static int clip_setentry(struct atm_vcc *vcc, u32 ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | struct neighbour *neigh; |
| 512 | struct atmarp_entry *entry; |
| 513 | int error; |
| 514 | struct clip_vcc *clip_vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 515 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | struct rtable *rt; |
| 517 | |
| 518 | if (vcc->push != clip_push) { |
| 519 | printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n"); |
| 520 | return -EBADF; |
| 521 | } |
| 522 | clip_vcc = CLIP_VCC(vcc); |
| 523 | if (!ip) { |
| 524 | if (!clip_vcc->entry) { |
| 525 | printk(KERN_ERR "hiding hidden ATMARP entry\n"); |
| 526 | return 0; |
| 527 | } |
| 528 | DPRINTK("setentry: remove\n"); |
| 529 | unlink_clip_vcc(clip_vcc); |
| 530 | return 0; |
| 531 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 532 | error = ip_route_output_key(&rt, &fl); |
| 533 | if (error) |
| 534 | return error; |
| 535 | neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | ip_rt_put(rt); |
| 537 | if (!neigh) |
| 538 | return -ENOMEM; |
| 539 | entry = NEIGH2ENTRY(neigh); |
| 540 | if (entry != clip_vcc->entry) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 541 | if (!clip_vcc->entry) |
| 542 | DPRINTK("setentry: add\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | else { |
| 544 | DPRINTK("setentry: update\n"); |
| 545 | unlink_clip_vcc(clip_vcc); |
| 546 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 547 | link_vcc(clip_vcc, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 549 | error = neigh_update(neigh, llc_oui, NUD_PERMANENT, |
| 550 | NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | neigh_release(neigh); |
| 552 | return error; |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static void clip_setup(struct net_device *dev) |
| 556 | { |
| 557 | dev->hard_start_xmit = clip_start_xmit; |
| 558 | /* sg_xmit ... */ |
| 559 | dev->get_stats = clip_get_stats; |
| 560 | dev->type = ARPHRD_ATM; |
| 561 | dev->hard_header_len = RFC1483LLC_LEN; |
| 562 | dev->mtu = RFC1626_MTU; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 563 | dev->tx_queue_len = 100; /* "normal" queue (packets) */ |
| 564 | /* When using a "real" qdisc, the qdisc determines the queue */ |
| 565 | /* length. tx_queue_len is only used for the default case, */ |
| 566 | /* without any more elaborate queuing. 100 is a reasonable */ |
| 567 | /* compromise between decent burst-tolerance and protection */ |
| 568 | /* against memory hogs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | static int clip_create(int number) |
| 572 | { |
| 573 | struct net_device *dev; |
| 574 | struct clip_priv *clip_priv; |
| 575 | int error; |
| 576 | |
| 577 | if (number != -1) { |
| 578 | for (dev = clip_devs; dev; dev = PRIV(dev)->next) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 579 | if (PRIV(dev)->number == number) |
| 580 | return -EEXIST; |
| 581 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | number = 0; |
| 583 | for (dev = clip_devs; dev; dev = PRIV(dev)->next) |
| 584 | if (PRIV(dev)->number >= number) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 585 | number = PRIV(dev)->number + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup); |
| 588 | if (!dev) |
| 589 | return -ENOMEM; |
| 590 | clip_priv = PRIV(dev); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 591 | sprintf(dev->name, "atm%d", number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | spin_lock_init(&clip_priv->xoff_lock); |
| 593 | clip_priv->number = number; |
| 594 | error = register_netdev(dev); |
| 595 | if (error) { |
| 596 | free_netdev(dev); |
| 597 | return error; |
| 598 | } |
| 599 | clip_priv->next = clip_devs; |
| 600 | clip_devs = dev; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 601 | DPRINTK("registered (net:%s)\n", dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | return number; |
| 603 | } |
| 604 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 605 | static int clip_device_event(struct notifier_block *this, unsigned long event, |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 606 | void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 608 | struct net_device *dev = arg; |
| 609 | |
| 610 | if (event == NETDEV_UNREGISTER) { |
| 611 | neigh_ifdown(&clip_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return NOTIFY_DONE; |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* ignore non-CLIP devices */ |
| 616 | if (dev->type != ARPHRD_ATM || dev->hard_start_xmit != clip_start_xmit) |
| 617 | return NOTIFY_DONE; |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | switch (event) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 620 | case NETDEV_UP: |
| 621 | DPRINTK("clip_device_event NETDEV_UP\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 622 | to_atmarpd(act_up, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 623 | break; |
| 624 | case NETDEV_GOING_DOWN: |
| 625 | DPRINTK("clip_device_event NETDEV_DOWN\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 626 | to_atmarpd(act_down, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 627 | break; |
| 628 | case NETDEV_CHANGE: |
| 629 | case NETDEV_CHANGEMTU: |
| 630 | DPRINTK("clip_device_event NETDEV_CHANGE*\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 631 | to_atmarpd(act_change, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 632 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | return NOTIFY_DONE; |
| 635 | } |
| 636 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 637 | static int clip_inet_event(struct notifier_block *this, unsigned long event, |
| 638 | void *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
| 640 | struct in_device *in_dev; |
| 641 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 642 | in_dev = ((struct in_ifaddr *)ifa)->ifa_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (!in_dev || !in_dev->dev) { |
| 644 | printk(KERN_WARNING "clip_inet_event: no device\n"); |
| 645 | return NOTIFY_DONE; |
| 646 | } |
| 647 | /* |
| 648 | * Transitions are of the down-change-up type, so it's sufficient to |
| 649 | * handle the change on up. |
| 650 | */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 651 | if (event != NETDEV_UP) |
| 652 | return NOTIFY_DONE; |
| 653 | return clip_device_event(this, NETDEV_CHANGE, in_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
| 657 | static struct notifier_block clip_dev_notifier = { |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 658 | .notifier_call = clip_device_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | |
| 662 | |
| 663 | static struct notifier_block clip_inet_notifier = { |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 664 | .notifier_call = clip_inet_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | }; |
| 666 | |
| 667 | |
| 668 | |
| 669 | static void atmarpd_close(struct atm_vcc *vcc) |
| 670 | { |
| 671 | DPRINTK("atmarpd_close\n"); |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 672 | |
| 673 | rtnl_lock(); |
| 674 | atmarpd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 676 | rtnl_unlock(); |
| 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | DPRINTK("(done)\n"); |
| 679 | module_put(THIS_MODULE); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | static struct atmdev_ops atmarpd_dev_ops = { |
| 684 | .close = atmarpd_close |
| 685 | }; |
| 686 | |
| 687 | |
| 688 | static struct atm_dev atmarpd_dev = { |
| 689 | .ops = &atmarpd_dev_ops, |
| 690 | .type = "arpd", |
| 691 | .number = 999, |
| 692 | .lock = SPIN_LOCK_UNLOCKED |
| 693 | }; |
| 694 | |
| 695 | |
| 696 | static int atm_init_atmarp(struct atm_vcc *vcc) |
| 697 | { |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 698 | rtnl_lock(); |
| 699 | if (atmarpd) { |
| 700 | rtnl_unlock(); |
| 701 | return -EADDRINUSE; |
| 702 | } |
| 703 | |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 704 | mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ); |
| 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | atmarpd = vcc; |
| 707 | set_bit(ATM_VF_META,&vcc->flags); |
| 708 | set_bit(ATM_VF_READY,&vcc->flags); |
| 709 | /* allow replies and avoid getting closed if signaling dies */ |
| 710 | vcc->dev = &atmarpd_dev; |
| 711 | vcc_insert_socket(sk_atm(vcc)); |
| 712 | vcc->push = NULL; |
| 713 | vcc->pop = NULL; /* crash */ |
| 714 | vcc->push_oam = NULL; /* crash */ |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 715 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 720 | { |
| 721 | struct atm_vcc *vcc = ATM_SD(sock); |
| 722 | int err = 0; |
| 723 | |
| 724 | switch (cmd) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 725 | case SIOCMKCLIP: |
| 726 | case ATMARPD_CTRL: |
| 727 | case ATMARP_MKIP: |
| 728 | case ATMARP_SETENTRY: |
| 729 | case ATMARP_ENCAP: |
| 730 | if (!capable(CAP_NET_ADMIN)) |
| 731 | return -EPERM; |
| 732 | break; |
| 733 | default: |
| 734 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | switch (cmd) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 738 | case SIOCMKCLIP: |
| 739 | err = clip_create(arg); |
| 740 | break; |
| 741 | case ATMARPD_CTRL: |
| 742 | err = atm_init_atmarp(vcc); |
| 743 | if (!err) { |
| 744 | sock->state = SS_CONNECTED; |
| 745 | __module_get(THIS_MODULE); |
| 746 | } |
| 747 | break; |
| 748 | case ATMARP_MKIP: |
| 749 | err = clip_mkip(vcc, arg); |
| 750 | break; |
| 751 | case ATMARP_SETENTRY: |
| 752 | err = clip_setentry(vcc, arg); |
| 753 | break; |
| 754 | case ATMARP_ENCAP: |
| 755 | err = clip_encap(vcc, arg); |
| 756 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
| 758 | return err; |
| 759 | } |
| 760 | |
| 761 | static struct atm_ioctl clip_ioctl_ops = { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 762 | .owner = THIS_MODULE, |
| 763 | .ioctl = clip_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | #ifdef CONFIG_PROC_FS |
| 767 | |
| 768 | static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr) |
| 769 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 770 | static int code[] = { 1, 2, 10, 6, 1, 0 }; |
| 771 | static int e164[] = { 1, 8, 4, 6, 1, 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | if (*addr->sas_addr.pub) { |
| 774 | seq_printf(seq, "%s", addr->sas_addr.pub); |
| 775 | if (*addr->sas_addr.prv) |
| 776 | seq_putc(seq, '+'); |
| 777 | } else if (!*addr->sas_addr.prv) { |
| 778 | seq_printf(seq, "%s", "(none)"); |
| 779 | return; |
| 780 | } |
| 781 | if (*addr->sas_addr.prv) { |
| 782 | unsigned char *prv = addr->sas_addr.prv; |
| 783 | int *fields; |
| 784 | int i, j; |
| 785 | |
| 786 | fields = *prv == ATM_AFI_E164 ? e164 : code; |
| 787 | for (i = 0; fields[i]; i++) { |
| 788 | for (j = fields[i]; j; j--) |
| 789 | seq_printf(seq, "%02X", *prv++); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 790 | if (fields[i + 1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | seq_putc(seq, '.'); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /* This means the neighbour entry has no attached VCC objects. */ |
| 797 | #define SEQ_NO_VCC_TOKEN ((void *) 2) |
| 798 | |
| 799 | static void atmarp_info(struct seq_file *seq, struct net_device *dev, |
| 800 | struct atmarp_entry *entry, struct clip_vcc *clip_vcc) |
| 801 | { |
| 802 | unsigned long exp; |
| 803 | char buf[17]; |
| 804 | int svc, llc, off; |
| 805 | |
| 806 | svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || |
| 807 | (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC)); |
| 808 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 809 | llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | if (clip_vcc == SEQ_NO_VCC_TOKEN) |
| 812 | exp = entry->neigh->used; |
| 813 | else |
| 814 | exp = clip_vcc->last_use; |
| 815 | |
| 816 | exp = (jiffies - exp) / HZ; |
| 817 | |
| 818 | seq_printf(seq, "%-6s%-4s%-4s%5ld ", |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 819 | dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | off = scnprintf(buf, sizeof(buf) - 1, "%d.%d.%d.%d", |
| 822 | NIPQUAD(entry->ip)); |
| 823 | while (off < 16) |
| 824 | buf[off++] = ' '; |
| 825 | buf[off] = '\0'; |
| 826 | seq_printf(seq, "%s", buf); |
| 827 | |
| 828 | if (clip_vcc == SEQ_NO_VCC_TOKEN) { |
| 829 | if (time_before(jiffies, entry->expires)) |
| 830 | seq_printf(seq, "(resolving)\n"); |
| 831 | else |
| 832 | seq_printf(seq, "(expired, ref %d)\n", |
| 833 | atomic_read(&entry->neigh->refcnt)); |
| 834 | } else if (!svc) { |
| 835 | seq_printf(seq, "%d.%d.%d\n", |
| 836 | clip_vcc->vcc->dev->number, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 837 | clip_vcc->vcc->vpi, clip_vcc->vcc->vci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } else { |
| 839 | svc_addr(seq, &clip_vcc->vcc->remote); |
| 840 | seq_putc(seq, '\n'); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | struct clip_seq_state { |
| 845 | /* This member must be first. */ |
| 846 | struct neigh_seq_state ns; |
| 847 | |
| 848 | /* Local to clip specific iteration. */ |
| 849 | struct clip_vcc *vcc; |
| 850 | }; |
| 851 | |
| 852 | static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e, |
| 853 | struct clip_vcc *curr) |
| 854 | { |
| 855 | if (!curr) { |
| 856 | curr = e->vccs; |
| 857 | if (!curr) |
| 858 | return SEQ_NO_VCC_TOKEN; |
| 859 | return curr; |
| 860 | } |
| 861 | if (curr == SEQ_NO_VCC_TOKEN) |
| 862 | return NULL; |
| 863 | |
| 864 | curr = curr->next; |
| 865 | |
| 866 | return curr; |
| 867 | } |
| 868 | |
| 869 | static void *clip_seq_vcc_walk(struct clip_seq_state *state, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 870 | struct atmarp_entry *e, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
| 872 | struct clip_vcc *vcc = state->vcc; |
| 873 | |
| 874 | vcc = clip_seq_next_vcc(e, vcc); |
| 875 | if (vcc && pos != NULL) { |
| 876 | while (*pos) { |
| 877 | vcc = clip_seq_next_vcc(e, vcc); |
| 878 | if (!vcc) |
| 879 | break; |
| 880 | --(*pos); |
| 881 | } |
| 882 | } |
| 883 | state->vcc = vcc; |
| 884 | |
| 885 | return vcc; |
| 886 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | static void *clip_seq_sub_iter(struct neigh_seq_state *_state, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 889 | struct neighbour *n, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 891 | struct clip_seq_state *state = (struct clip_seq_state *)_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos); |
| 894 | } |
| 895 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 896 | static void *clip_seq_start(struct seq_file *seq, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | { |
| 898 | return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY); |
| 899 | } |
| 900 | |
| 901 | static int clip_seq_show(struct seq_file *seq, void *v) |
| 902 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 903 | static char atm_arp_banner[] = |
| 904 | "IPitf TypeEncp Idle IP address ATM address\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | if (v == SEQ_START_TOKEN) { |
| 907 | seq_puts(seq, atm_arp_banner); |
| 908 | } else { |
| 909 | struct clip_seq_state *state = seq->private; |
| 910 | struct neighbour *n = v; |
| 911 | struct clip_vcc *vcc = state->vcc; |
| 912 | |
| 913 | atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc); |
| 914 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 915 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static struct seq_operations arp_seq_ops = { |
| 919 | .start = clip_seq_start, |
| 920 | .next = neigh_seq_next, |
| 921 | .stop = neigh_seq_stop, |
| 922 | .show = clip_seq_show, |
| 923 | }; |
| 924 | |
| 925 | static int arp_seq_open(struct inode *inode, struct file *file) |
| 926 | { |
| 927 | struct clip_seq_state *state; |
| 928 | struct seq_file *seq; |
| 929 | int rc = -EAGAIN; |
| 930 | |
| 931 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
| 932 | if (!state) { |
| 933 | rc = -ENOMEM; |
| 934 | goto out_kfree; |
| 935 | } |
| 936 | memset(state, 0, sizeof(*state)); |
| 937 | state->ns.neigh_sub_iter = clip_seq_sub_iter; |
| 938 | |
| 939 | rc = seq_open(file, &arp_seq_ops); |
| 940 | if (rc) |
| 941 | goto out_kfree; |
| 942 | |
| 943 | seq = file->private_data; |
| 944 | seq->private = state; |
| 945 | out: |
| 946 | return rc; |
| 947 | |
| 948 | out_kfree: |
| 949 | kfree(state); |
| 950 | goto out; |
| 951 | } |
| 952 | |
| 953 | static struct file_operations arp_seq_fops = { |
| 954 | .open = arp_seq_open, |
| 955 | .read = seq_read, |
| 956 | .llseek = seq_lseek, |
| 957 | .release = seq_release_private, |
| 958 | .owner = THIS_MODULE |
| 959 | }; |
| 960 | #endif |
| 961 | |
| 962 | static int __init atm_clip_init(void) |
| 963 | { |
Stephen Hemminger | dcdb027 | 2006-04-14 16:00:25 -0700 | [diff] [blame] | 964 | struct proc_dir_entry *p; |
Simon Kelley | bd89efc | 2006-05-12 14:56:08 -0700 | [diff] [blame] | 965 | neigh_table_init_no_netlink(&clip_tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
| 967 | clip_tbl_hook = &clip_tbl; |
| 968 | register_atm_ioctl(&clip_ioctl_ops); |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 969 | register_netdevice_notifier(&clip_dev_notifier); |
| 970 | register_inetaddr_notifier(&clip_inet_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 972 | setup_timer(&idle_timer, idle_timer_check, 0); |
| 973 | |
Stephen Hemminger | dcdb027 | 2006-04-14 16:00:25 -0700 | [diff] [blame] | 974 | p = create_proc_entry("arp", S_IRUGO, atm_proc_root); |
| 975 | if (p) |
| 976 | p->proc_fops = &arp_seq_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static void __exit atm_clip_exit(void) |
| 982 | { |
| 983 | struct net_device *dev, *next; |
| 984 | |
| 985 | remove_proc_entry("arp", atm_proc_root); |
| 986 | |
Stephen Hemminger | f3a0592b | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 987 | unregister_inetaddr_notifier(&clip_inet_notifier); |
| 988 | unregister_netdevice_notifier(&clip_dev_notifier); |
| 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | deregister_atm_ioctl(&clip_ioctl_ops); |
| 991 | |
| 992 | /* First, stop the idle timer, so it stops banging |
| 993 | * on the table. |
| 994 | */ |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 995 | del_timer_sync(&idle_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | /* Next, purge the table, so that the device |
| 998 | * unregister loop below does not hang due to |
| 999 | * device references remaining in the table. |
| 1000 | */ |
| 1001 | neigh_ifdown(&clip_tbl, NULL); |
| 1002 | |
| 1003 | dev = clip_devs; |
| 1004 | while (dev) { |
| 1005 | next = PRIV(dev)->next; |
| 1006 | unregister_netdev(dev); |
| 1007 | free_netdev(dev); |
| 1008 | dev = next; |
| 1009 | } |
| 1010 | |
| 1011 | /* Now it is safe to fully shutdown whole table. */ |
| 1012 | neigh_table_clear(&clip_tbl); |
| 1013 | |
| 1014 | clip_tbl_hook = NULL; |
| 1015 | } |
| 1016 | |
| 1017 | module_init(atm_clip_init); |
| 1018 | module_exit(atm_clip_exit); |
Stephen Hemminger | 4909e488 | 2006-04-14 16:01:26 -0700 | [diff] [blame] | 1019 | MODULE_AUTHOR("Werner Almesberger"); |
| 1020 | MODULE_DESCRIPTION("Classical/IP over ATM interface"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | MODULE_LICENSE("GPL"); |