11da177e4SLinus Torvalds /* net/atm/clip.c - RFC1577 Classical IP over ATM */ 21da177e4SLinus Torvalds 31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <linux/string.h> 61da177e4SLinus Torvalds #include <linux/errno.h> 71da177e4SLinus Torvalds #include <linux/kernel.h> /* for UINT_MAX */ 81da177e4SLinus Torvalds #include <linux/module.h> 91da177e4SLinus Torvalds #include <linux/init.h> 101da177e4SLinus Torvalds #include <linux/netdevice.h> 111da177e4SLinus Torvalds #include <linux/skbuff.h> 121da177e4SLinus Torvalds #include <linux/wait.h> 131da177e4SLinus Torvalds #include <linux/timer.h> 141da177e4SLinus Torvalds #include <linux/if_arp.h> /* for some manifest constants */ 151da177e4SLinus Torvalds #include <linux/notifier.h> 161da177e4SLinus Torvalds #include <linux/atm.h> 171da177e4SLinus Torvalds #include <linux/atmdev.h> 181da177e4SLinus Torvalds #include <linux/atmclip.h> 191da177e4SLinus Torvalds #include <linux/atmarp.h> 204fc268d2SRandy Dunlap #include <linux/capability.h> 211da177e4SLinus Torvalds #include <linux/ip.h> /* for net/route.h */ 221da177e4SLinus Torvalds #include <linux/in.h> /* for struct sockaddr_in */ 231da177e4SLinus Torvalds #include <linux/if.h> /* for IFF_UP */ 241da177e4SLinus Torvalds #include <linux/inetdevice.h> 251da177e4SLinus Torvalds #include <linux/bitops.h> 264bdbf6c0SRandy Dunlap #include <linux/poison.h> 271da177e4SLinus Torvalds #include <linux/proc_fs.h> 281da177e4SLinus Torvalds #include <linux/seq_file.h> 291da177e4SLinus Torvalds #include <linux/rcupdate.h> 301da177e4SLinus Torvalds #include <linux/jhash.h> 311da177e4SLinus Torvalds #include <net/route.h> /* for struct rtable and routing */ 321da177e4SLinus Torvalds #include <net/icmp.h> /* icmp_send */ 331da177e4SLinus Torvalds #include <asm/param.h> /* for HZ */ 341da177e4SLinus Torvalds #include <asm/byteorder.h> /* for htons etc. */ 351da177e4SLinus Torvalds #include <asm/system.h> /* save/restore_flags */ 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds #include <asm/atomic.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include "common.h" 401da177e4SLinus Torvalds #include "resources.h" 411da177e4SLinus Torvalds #include <net/atmclip.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static struct net_device *clip_devs; 441da177e4SLinus Torvalds static struct atm_vcc *atmarpd; 451da177e4SLinus Torvalds static struct neigh_table clip_tbl; 461da177e4SLinus Torvalds static struct timer_list idle_timer; 471da177e4SLinus Torvalds 4830d492daSAl Viro static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds struct sock *sk; 511da177e4SLinus Torvalds struct atmarp_ctrl *ctrl; 521da177e4SLinus Torvalds struct sk_buff *skb; 531da177e4SLinus Torvalds 5452240062SStephen Hemminger pr_debug("to_atmarpd(%d)\n", type); 55e49e76dbSStephen Hemminger if (!atmarpd) 56e49e76dbSStephen Hemminger return -EUNATCH; 571da177e4SLinus Torvalds skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC); 58e49e76dbSStephen Hemminger if (!skb) 59e49e76dbSStephen Hemminger return -ENOMEM; 601da177e4SLinus Torvalds ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl)); 611da177e4SLinus Torvalds ctrl->type = type; 621da177e4SLinus Torvalds ctrl->itf_num = itf; 631da177e4SLinus Torvalds ctrl->ip = ip; 641da177e4SLinus Torvalds atm_force_charge(atmarpd, skb->truesize); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds sk = sk_atm(atmarpd); 671da177e4SLinus Torvalds skb_queue_tail(&sk->sk_receive_queue, skb); 681da177e4SLinus Torvalds sk->sk_data_ready(sk, skb->len); 691da177e4SLinus Torvalds return 0; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry) 731da177e4SLinus Torvalds { 7452240062SStephen Hemminger pr_debug("link_vcc %p to entry %p (neigh %p)\n", clip_vcc, entry, 751da177e4SLinus Torvalds entry->neigh); 761da177e4SLinus Torvalds clip_vcc->entry = entry; 771da177e4SLinus Torvalds clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */ 781da177e4SLinus Torvalds clip_vcc->next = entry->vccs; 791da177e4SLinus Torvalds entry->vccs = clip_vcc; 801da177e4SLinus Torvalds entry->neigh->used = jiffies; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static void unlink_clip_vcc(struct clip_vcc *clip_vcc) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds struct atmarp_entry *entry = clip_vcc->entry; 861da177e4SLinus Torvalds struct clip_vcc **walk; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds if (!entry) { 891da177e4SLinus Torvalds printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n", clip_vcc); 901da177e4SLinus Torvalds return; 911da177e4SLinus Torvalds } 92932ff279SHerbert Xu netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */ 931da177e4SLinus Torvalds entry->neigh->used = jiffies; 941da177e4SLinus Torvalds for (walk = &entry->vccs; *walk; walk = &(*walk)->next) 951da177e4SLinus Torvalds if (*walk == clip_vcc) { 961da177e4SLinus Torvalds int error; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds *walk = clip_vcc->next; /* atomic */ 991da177e4SLinus Torvalds clip_vcc->entry = NULL; 1001da177e4SLinus Torvalds if (clip_vcc->xoff) 1011da177e4SLinus Torvalds netif_wake_queue(entry->neigh->dev); 1021da177e4SLinus Torvalds if (entry->vccs) 1031da177e4SLinus Torvalds goto out; 1041da177e4SLinus Torvalds entry->expires = jiffies - 1; 1051da177e4SLinus Torvalds /* force resolution or expiration */ 1061da177e4SLinus Torvalds error = neigh_update(entry->neigh, NULL, NUD_NONE, 1071da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1081da177e4SLinus Torvalds if (error) 1091da177e4SLinus Torvalds printk(KERN_CRIT "unlink_clip_vcc: " 1101da177e4SLinus Torvalds "neigh_update failed with %d\n", error); 1111da177e4SLinus Torvalds goto out; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc " 1141da177e4SLinus Torvalds "0x%p)\n", entry, clip_vcc); 1151da177e4SLinus Torvalds out: 116932ff279SHerbert Xu netif_tx_unlock_bh(entry->neigh->dev); 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* The neighbour entry n->lock is held. */ 1201da177e4SLinus Torvalds static int neigh_check_cb(struct neighbour *n) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds struct atmarp_entry *entry = NEIGH2ENTRY(n); 1231da177e4SLinus Torvalds struct clip_vcc *cv; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds for (cv = entry->vccs; cv; cv = cv->next) { 1261da177e4SLinus Torvalds unsigned long exp = cv->last_use + cv->idle_timeout; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds if (cv->idle_timeout && time_after(jiffies, exp)) { 12952240062SStephen Hemminger pr_debug("releasing vcc %p->%p of entry %p\n", 1301da177e4SLinus Torvalds cv, cv->vcc, entry); 1311da177e4SLinus Torvalds vcc_release_async(cv->vcc, -ETIMEDOUT); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds if (entry->vccs || time_before(jiffies, entry->expires)) 1361da177e4SLinus Torvalds return 0; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds if (atomic_read(&n->refcnt) > 1) { 1391da177e4SLinus Torvalds struct sk_buff *skb; 1401da177e4SLinus Torvalds 14152240062SStephen Hemminger pr_debug("destruction postponed with ref %d\n", 1421da177e4SLinus Torvalds atomic_read(&n->refcnt)); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds while ((skb = skb_dequeue(&n->arp_queue)) != NULL) 1451da177e4SLinus Torvalds dev_kfree_skb(skb); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds return 0; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 15052240062SStephen Hemminger pr_debug("expired neigh %p\n", n); 1511da177e4SLinus Torvalds return 1; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds static void idle_timer_check(unsigned long dummy) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds write_lock(&clip_tbl.lock); 1571da177e4SLinus Torvalds __neigh_for_each_release(&clip_tbl, neigh_check_cb); 1581da177e4SLinus Torvalds mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ); 1591da177e4SLinus Torvalds write_unlock(&clip_tbl.lock); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static int clip_arp_rcv(struct sk_buff *skb) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds struct atm_vcc *vcc; 1651da177e4SLinus Torvalds 16652240062SStephen Hemminger pr_debug("clip_arp_rcv\n"); 1671da177e4SLinus Torvalds vcc = ATM_SKB(skb)->vcc; 1681da177e4SLinus Torvalds if (!vcc || !atm_charge(vcc, skb->truesize)) { 1691da177e4SLinus Torvalds dev_kfree_skb_any(skb); 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds } 17252240062SStephen Hemminger pr_debug("pushing to %p\n", vcc); 17352240062SStephen Hemminger pr_debug("using %p\n", CLIP_VCC(vcc)->old_push); 1741da177e4SLinus Torvalds CLIP_VCC(vcc)->old_push(vcc, skb); 1751da177e4SLinus Torvalds return 0; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static const unsigned char llc_oui[] = { 1791da177e4SLinus Torvalds 0xaa, /* DSAP: non-ISO */ 1801da177e4SLinus Torvalds 0xaa, /* SSAP: non-ISO */ 1811da177e4SLinus Torvalds 0x03, /* Ctrl: Unnumbered Information Command PDU */ 1821da177e4SLinus Torvalds 0x00, /* OUI: EtherType */ 1831da177e4SLinus Torvalds 0x00, 184e49e76dbSStephen Hemminger 0x00 185e49e76dbSStephen Hemminger }; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds struct clip_vcc *clip_vcc = CLIP_VCC(vcc); 1901da177e4SLinus Torvalds 19152240062SStephen Hemminger pr_debug("clip push\n"); 1921da177e4SLinus Torvalds if (!skb) { 19352240062SStephen Hemminger pr_debug("removing VCC %p\n", clip_vcc); 194e49e76dbSStephen Hemminger if (clip_vcc->entry) 195e49e76dbSStephen Hemminger unlink_clip_vcc(clip_vcc); 1961da177e4SLinus Torvalds clip_vcc->old_push(vcc, NULL); /* pass on the bad news */ 1971da177e4SLinus Torvalds kfree(clip_vcc); 1981da177e4SLinus Torvalds return; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 2011da177e4SLinus Torvalds skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs; 2021da177e4SLinus Torvalds /* clip_vcc->entry == NULL if we don't have an IP address yet */ 2031da177e4SLinus Torvalds if (!skb->dev) { 2041da177e4SLinus Torvalds dev_kfree_skb_any(skb); 2051da177e4SLinus Torvalds return; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds ATM_SKB(skb)->vcc = vcc; 208459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 209e49e76dbSStephen Hemminger if (!clip_vcc->encap 210e49e76dbSStephen Hemminger || skb->len < RFC1483LLC_LEN 211e49e76dbSStephen Hemminger || memcmp(skb->data, llc_oui, sizeof (llc_oui))) 212e49e76dbSStephen Hemminger skb->protocol = htons(ETH_P_IP); 2131da177e4SLinus Torvalds else { 21430d492daSAl Viro skb->protocol = ((__be16 *) skb->data)[3]; 2151da177e4SLinus Torvalds skb_pull(skb, RFC1483LLC_LEN); 2161da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_ARP)) { 2171a6afe8aSStephen Hemminger skb->dev->stats.rx_packets++; 2181a6afe8aSStephen Hemminger skb->dev->stats.rx_bytes += skb->len; 2191da177e4SLinus Torvalds clip_arp_rcv(skb); 2201da177e4SLinus Torvalds return; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds clip_vcc->last_use = jiffies; 2241a6afe8aSStephen Hemminger skb->dev->stats.rx_packets++; 2251a6afe8aSStephen Hemminger skb->dev->stats.rx_bytes += skb->len; 2261da177e4SLinus Torvalds memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data)); 2271da177e4SLinus Torvalds netif_rx(skb); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* 2311da177e4SLinus Torvalds * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that 2321da177e4SLinus Torvalds * clip_pop is atomic with respect to the critical section in clip_start_xmit. 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct clip_vcc *clip_vcc = CLIP_VCC(vcc); 2381da177e4SLinus Torvalds struct net_device *dev = skb->dev; 2391da177e4SLinus Torvalds int old; 2401da177e4SLinus Torvalds unsigned long flags; 2411da177e4SLinus Torvalds 24252240062SStephen Hemminger pr_debug("clip_pop(vcc %p)\n", vcc); 2431da177e4SLinus Torvalds clip_vcc->old_pop(vcc, skb); 2441da177e4SLinus Torvalds /* skb->dev == NULL in outbound ARP packets */ 245e49e76dbSStephen Hemminger if (!dev) 246e49e76dbSStephen Hemminger return; 2471da177e4SLinus Torvalds spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags); 2481da177e4SLinus Torvalds if (atm_may_send(vcc, 0)) { 2491da177e4SLinus Torvalds old = xchg(&clip_vcc->xoff, 0); 250e49e76dbSStephen Hemminger if (old) 251e49e76dbSStephen Hemminger netif_wake_queue(dev); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb) 2571da177e4SLinus Torvalds { 25852240062SStephen Hemminger pr_debug("clip_neigh_solicit (neigh %p, skb %p)\n", neigh, skb); 2591da177e4SLinus Torvalds to_atmarpd(act_need, PRIV(neigh->dev)->number, NEIGH2ENTRY(neigh)->ip); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds #ifndef CONFIG_ATM_CLIP_NO_ICMP 2651da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 2661da177e4SLinus Torvalds #endif 2671da177e4SLinus Torvalds kfree_skb(skb); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds static struct neigh_ops clip_neigh_ops = { 2711da177e4SLinus Torvalds .family = AF_INET, 2721da177e4SLinus Torvalds .solicit = clip_neigh_solicit, 2731da177e4SLinus Torvalds .error_report = clip_neigh_error, 2741da177e4SLinus Torvalds .output = dev_queue_xmit, 2751da177e4SLinus Torvalds .connected_output = dev_queue_xmit, 2761da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 2771da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 2781da177e4SLinus Torvalds }; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds static int clip_constructor(struct neighbour *neigh) 2811da177e4SLinus Torvalds { 2821da177e4SLinus Torvalds struct atmarp_entry *entry = NEIGH2ENTRY(neigh); 2831da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 2841da177e4SLinus Torvalds struct in_device *in_dev; 2851da177e4SLinus Torvalds struct neigh_parms *parms; 2861da177e4SLinus Torvalds 28752240062SStephen Hemminger pr_debug("clip_constructor (neigh %p, entry %p)\n", neigh, entry); 2886b175b26SEric W. Biederman neigh->type = inet_addr_type(&init_net, entry->ip); 289e49e76dbSStephen Hemminger if (neigh->type != RTN_UNICAST) 290e49e76dbSStephen Hemminger return -EINVAL; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds rcu_read_lock(); 293e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 2941da177e4SLinus Torvalds if (!in_dev) { 2951da177e4SLinus Torvalds rcu_read_unlock(); 2961da177e4SLinus Torvalds return -EINVAL; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds parms = in_dev->arp_parms; 3001da177e4SLinus Torvalds __neigh_parms_put(neigh->parms); 3011da177e4SLinus Torvalds neigh->parms = neigh_parms_clone(parms); 3021da177e4SLinus Torvalds rcu_read_unlock(); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds neigh->ops = &clip_neigh_ops; 3051da177e4SLinus Torvalds neigh->output = neigh->nud_state & NUD_VALID ? 3061da177e4SLinus Torvalds neigh->ops->connected_output : neigh->ops->output; 3071da177e4SLinus Torvalds entry->neigh = neigh; 3081da177e4SLinus Torvalds entry->vccs = NULL; 3091da177e4SLinus Torvalds entry->expires = jiffies - 1; 3101da177e4SLinus Torvalds return 0; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds static u32 clip_hash(const void *pkey, const struct net_device *dev) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds return jhash_2words(*(u32 *) pkey, dev->ifindex, clip_tbl.hash_rnd); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static struct neigh_table clip_tbl = { 3191da177e4SLinus Torvalds .family = AF_INET, 3201da177e4SLinus Torvalds .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry), 3211da177e4SLinus Torvalds .key_len = 4, 3221da177e4SLinus Torvalds .hash = clip_hash, 3231da177e4SLinus Torvalds .constructor = clip_constructor, 3241da177e4SLinus Torvalds .id = "clip_arp_cache", 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* parameters are copied from ARP ... */ 3271da177e4SLinus Torvalds .parms = { 3281da177e4SLinus Torvalds .tbl = &clip_tbl, 3291da177e4SLinus Torvalds .base_reachable_time = 30 * HZ, 3301da177e4SLinus Torvalds .retrans_time = 1 * HZ, 3311da177e4SLinus Torvalds .gc_staletime = 60 * HZ, 3321da177e4SLinus Torvalds .reachable_time = 30 * HZ, 3331da177e4SLinus Torvalds .delay_probe_time = 5 * HZ, 3341da177e4SLinus Torvalds .queue_len = 3, 3351da177e4SLinus Torvalds .ucast_probes = 3, 3361da177e4SLinus Torvalds .mcast_probes = 3, 3371da177e4SLinus Torvalds .anycast_delay = 1 * HZ, 3381da177e4SLinus Torvalds .proxy_delay = (8 * HZ) / 10, 3391da177e4SLinus Torvalds .proxy_qlen = 64, 3401da177e4SLinus Torvalds .locktime = 1 * HZ, 3411da177e4SLinus Torvalds }, 3421da177e4SLinus Torvalds .gc_interval = 30 * HZ, 3431da177e4SLinus Torvalds .gc_thresh1 = 128, 3441da177e4SLinus Torvalds .gc_thresh2 = 512, 3451da177e4SLinus Torvalds .gc_thresh3 = 1024, 3461da177e4SLinus Torvalds }; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */ 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* 3511da177e4SLinus Torvalds * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means 3521da177e4SLinus Torvalds * to allocate the neighbour entry but not to ask atmarpd for resolution. Also, 3531da177e4SLinus Torvalds * don't increment the usage count. This is used to create entries in 3541da177e4SLinus Torvalds * clip_setentry. 3551da177e4SLinus Torvalds */ 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds static int clip_encap(struct atm_vcc *vcc, int mode) 3581da177e4SLinus Torvalds { 3591da177e4SLinus Torvalds CLIP_VCC(vcc)->encap = mode; 3601da177e4SLinus Torvalds return 0; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static int clip_start_xmit(struct sk_buff *skb, struct net_device *dev) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct clip_priv *clip_priv = PRIV(dev); 3661da177e4SLinus Torvalds struct atmarp_entry *entry; 3671da177e4SLinus Torvalds struct atm_vcc *vcc; 3681da177e4SLinus Torvalds int old; 3691da177e4SLinus Torvalds unsigned long flags; 3701da177e4SLinus Torvalds 37152240062SStephen Hemminger pr_debug("clip_start_xmit (skb %p)\n", skb); 3721da177e4SLinus Torvalds if (!skb->dst) { 3731da177e4SLinus Torvalds printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n"); 3741da177e4SLinus Torvalds dev_kfree_skb(skb); 3751a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds if (!skb->dst->neighbour) { 3791da177e4SLinus Torvalds #if 0 3801da177e4SLinus Torvalds skb->dst->neighbour = clip_find_neighbour(skb->dst, 1); 3811da177e4SLinus Torvalds if (!skb->dst->neighbour) { 3821da177e4SLinus Torvalds dev_kfree_skb(skb); /* lost that one */ 3831a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3841da177e4SLinus Torvalds return 0; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds #endif 3871da177e4SLinus Torvalds printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n"); 3881da177e4SLinus Torvalds dev_kfree_skb(skb); 3891a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3901da177e4SLinus Torvalds return 0; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds entry = NEIGH2ENTRY(skb->dst->neighbour); 3931da177e4SLinus Torvalds if (!entry->vccs) { 3941da177e4SLinus Torvalds if (time_after(jiffies, entry->expires)) { 3951da177e4SLinus Torvalds /* should be resolved */ 3961da177e4SLinus Torvalds entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ; 3971da177e4SLinus Torvalds to_atmarpd(act_need, PRIV(dev)->number, entry->ip); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS) 4001da177e4SLinus Torvalds skb_queue_tail(&entry->neigh->arp_queue, skb); 4011da177e4SLinus Torvalds else { 4021da177e4SLinus Torvalds dev_kfree_skb(skb); 4031a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds return 0; 4061da177e4SLinus Torvalds } 40752240062SStephen Hemminger pr_debug("neigh %p, vccs %p\n", entry, entry->vccs); 4081da177e4SLinus Torvalds ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc; 40952240062SStephen Hemminger pr_debug("using neighbour %p, vcc %p\n", skb->dst->neighbour, vcc); 4101da177e4SLinus Torvalds if (entry->vccs->encap) { 4111da177e4SLinus Torvalds void *here; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds here = skb_push(skb, RFC1483LLC_LEN); 4141da177e4SLinus Torvalds memcpy(here, llc_oui, sizeof(llc_oui)); 41530d492daSAl Viro ((__be16 *) here)[3] = skb->protocol; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc); 4181da177e4SLinus Torvalds ATM_SKB(skb)->atm_options = vcc->atm_options; 4191da177e4SLinus Torvalds entry->vccs->last_use = jiffies; 42052240062SStephen Hemminger pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev); 4211da177e4SLinus Torvalds old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */ 4221da177e4SLinus Torvalds if (old) { 4231da177e4SLinus Torvalds printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n"); 4241da177e4SLinus Torvalds return 0; 4251da177e4SLinus Torvalds } 4261a6afe8aSStephen Hemminger dev->stats.tx_packets++; 4271a6afe8aSStephen Hemminger dev->stats.tx_bytes += skb->len; 4285ff765f3SStephen Hemminger vcc->send(vcc, skb); 4291da177e4SLinus Torvalds if (atm_may_send(vcc, 0)) { 4301da177e4SLinus Torvalds entry->vccs->xoff = 0; 4311da177e4SLinus Torvalds return 0; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds spin_lock_irqsave(&clip_priv->xoff_lock, flags); 4341da177e4SLinus Torvalds netif_stop_queue(dev); /* XOFF -> throttle immediately */ 4351da177e4SLinus Torvalds barrier(); 4361da177e4SLinus Torvalds if (!entry->vccs->xoff) 4371da177e4SLinus Torvalds netif_start_queue(dev); 4381da177e4SLinus Torvalds /* Oh, we just raced with clip_pop. netif_start_queue should be 4391da177e4SLinus Torvalds good enough, because nothing should really be asleep because 4401da177e4SLinus Torvalds of the brief netif_stop_queue. If this isn't true or if it 4411da177e4SLinus Torvalds changes, use netif_wake_queue instead. */ 4421da177e4SLinus Torvalds spin_unlock_irqrestore(&clip_priv->xoff_lock, flags); 4431da177e4SLinus Torvalds return 0; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds static int clip_mkip(struct atm_vcc *vcc, int timeout) 4471da177e4SLinus Torvalds { 448*b6211ae7SDavid S. Miller struct sk_buff_head *rq, queue; 4491da177e4SLinus Torvalds struct clip_vcc *clip_vcc; 450*b6211ae7SDavid S. Miller struct sk_buff *skb, *tmp; 451c40a27f4SDavid S. Miller unsigned long flags; 4521da177e4SLinus Torvalds 453e49e76dbSStephen Hemminger if (!vcc->push) 454e49e76dbSStephen Hemminger return -EBADFD; 4551da177e4SLinus Torvalds clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL); 456e49e76dbSStephen Hemminger if (!clip_vcc) 457e49e76dbSStephen Hemminger return -ENOMEM; 45852240062SStephen Hemminger pr_debug("mkip clip_vcc %p vcc %p\n", clip_vcc, vcc); 4591da177e4SLinus Torvalds clip_vcc->vcc = vcc; 4601da177e4SLinus Torvalds vcc->user_back = clip_vcc; 4611da177e4SLinus Torvalds set_bit(ATM_VF_IS_CLIP, &vcc->flags); 4621da177e4SLinus Torvalds clip_vcc->entry = NULL; 4631da177e4SLinus Torvalds clip_vcc->xoff = 0; 4641da177e4SLinus Torvalds clip_vcc->encap = 1; 4651da177e4SLinus Torvalds clip_vcc->last_use = jiffies; 4661da177e4SLinus Torvalds clip_vcc->idle_timeout = timeout * HZ; 4671da177e4SLinus Torvalds clip_vcc->old_push = vcc->push; 4681da177e4SLinus Torvalds clip_vcc->old_pop = vcc->pop; 4691da177e4SLinus Torvalds vcc->push = clip_push; 4701da177e4SLinus Torvalds vcc->pop = clip_pop; 471c40a27f4SDavid S. Miller 472*b6211ae7SDavid S. Miller __skb_queue_head_init(&queue); 473c40a27f4SDavid S. Miller rq = &sk_atm(vcc)->sk_receive_queue; 474c40a27f4SDavid S. Miller 475c40a27f4SDavid S. Miller spin_lock_irqsave(&rq->lock, flags); 476*b6211ae7SDavid S. Miller skb_queue_splice_init(rq, &queue); 477c40a27f4SDavid S. Miller spin_unlock_irqrestore(&rq->lock, flags); 478c40a27f4SDavid S. Miller 4791da177e4SLinus Torvalds /* re-process everything received between connection setup and MKIP */ 480*b6211ae7SDavid S. Miller skb_queue_walk_safe(&queue, skb, tmp) { 4811da177e4SLinus Torvalds if (!clip_devs) { 4821da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 4831da177e4SLinus Torvalds kfree_skb(skb); 484e49e76dbSStephen Hemminger } else { 485*b6211ae7SDavid S. Miller struct net_device *dev = skb->dev; 4861da177e4SLinus Torvalds unsigned int len = skb->len; 4871da177e4SLinus Torvalds 488fe26109aSYOSHIFUJI Hideaki skb_get(skb); 4891da177e4SLinus Torvalds clip_push(vcc, skb); 490*b6211ae7SDavid S. Miller dev->stats.rx_packets--; 491*b6211ae7SDavid S. Miller dev->stats.rx_bytes -= len; 492fe26109aSYOSHIFUJI Hideaki kfree_skb(skb); 4931da177e4SLinus Torvalds } 494c40a27f4SDavid S. Miller } 4951da177e4SLinus Torvalds return 0; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 49830d492daSAl Viro static int clip_setentry(struct atm_vcc *vcc, __be32 ip) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct neighbour *neigh; 5011da177e4SLinus Torvalds struct atmarp_entry *entry; 5021da177e4SLinus Torvalds int error; 5031da177e4SLinus Torvalds struct clip_vcc *clip_vcc; 5041da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} }; 5051da177e4SLinus Torvalds struct rtable *rt; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (vcc->push != clip_push) { 5081da177e4SLinus Torvalds printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n"); 5091da177e4SLinus Torvalds return -EBADF; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds clip_vcc = CLIP_VCC(vcc); 5121da177e4SLinus Torvalds if (!ip) { 5131da177e4SLinus Torvalds if (!clip_vcc->entry) { 5141da177e4SLinus Torvalds printk(KERN_ERR "hiding hidden ATMARP entry\n"); 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 51752240062SStephen Hemminger pr_debug("setentry: remove\n"); 5181da177e4SLinus Torvalds unlink_clip_vcc(clip_vcc); 5191da177e4SLinus Torvalds return 0; 5201da177e4SLinus Torvalds } 521f206351aSDenis V. Lunev error = ip_route_output_key(&init_net, &rt, &fl); 522e49e76dbSStephen Hemminger if (error) 523e49e76dbSStephen Hemminger return error; 5241da177e4SLinus Torvalds neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1); 5251da177e4SLinus Torvalds ip_rt_put(rt); 5261da177e4SLinus Torvalds if (!neigh) 5271da177e4SLinus Torvalds return -ENOMEM; 5281da177e4SLinus Torvalds entry = NEIGH2ENTRY(neigh); 5291da177e4SLinus Torvalds if (entry != clip_vcc->entry) { 530e49e76dbSStephen Hemminger if (!clip_vcc->entry) 53152240062SStephen Hemminger pr_debug("setentry: add\n"); 5321da177e4SLinus Torvalds else { 53352240062SStephen Hemminger pr_debug("setentry: update\n"); 5341da177e4SLinus Torvalds unlink_clip_vcc(clip_vcc); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds link_vcc(clip_vcc, entry); 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds error = neigh_update(neigh, llc_oui, NUD_PERMANENT, 5391da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN); 5401da177e4SLinus Torvalds neigh_release(neigh); 5411da177e4SLinus Torvalds return error; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 544dde09758SStephen Hemminger static const struct net_device_ops clip_netdev_ops = { 545dde09758SStephen Hemminger .ndo_start_xmit = clip_start_xmit, 546dde09758SStephen Hemminger }; 547dde09758SStephen Hemminger 5481da177e4SLinus Torvalds static void clip_setup(struct net_device *dev) 5491da177e4SLinus Torvalds { 550dde09758SStephen Hemminger dev->netdev_ops = &clip_netdev_ops; 5511da177e4SLinus Torvalds dev->type = ARPHRD_ATM; 5521da177e4SLinus Torvalds dev->hard_header_len = RFC1483LLC_LEN; 5531da177e4SLinus Torvalds dev->mtu = RFC1626_MTU; 5541da177e4SLinus Torvalds dev->tx_queue_len = 100; /* "normal" queue (packets) */ 5551da177e4SLinus Torvalds /* When using a "real" qdisc, the qdisc determines the queue */ 5561da177e4SLinus Torvalds /* length. tx_queue_len is only used for the default case, */ 5571da177e4SLinus Torvalds /* without any more elaborate queuing. 100 is a reasonable */ 5581da177e4SLinus Torvalds /* compromise between decent burst-tolerance and protection */ 5591da177e4SLinus Torvalds /* against memory hogs. */ 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds static int clip_create(int number) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct net_device *dev; 5651da177e4SLinus Torvalds struct clip_priv *clip_priv; 5661da177e4SLinus Torvalds int error; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds if (number != -1) { 5691da177e4SLinus Torvalds for (dev = clip_devs; dev; dev = PRIV(dev)->next) 570e49e76dbSStephen Hemminger if (PRIV(dev)->number == number) 571e49e76dbSStephen Hemminger return -EEXIST; 572e49e76dbSStephen Hemminger } else { 5731da177e4SLinus Torvalds number = 0; 5741da177e4SLinus Torvalds for (dev = clip_devs; dev; dev = PRIV(dev)->next) 5751da177e4SLinus Torvalds if (PRIV(dev)->number >= number) 5761da177e4SLinus Torvalds number = PRIV(dev)->number + 1; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup); 5791da177e4SLinus Torvalds if (!dev) 5801da177e4SLinus Torvalds return -ENOMEM; 5811da177e4SLinus Torvalds clip_priv = PRIV(dev); 5821da177e4SLinus Torvalds sprintf(dev->name, "atm%d", number); 5831da177e4SLinus Torvalds spin_lock_init(&clip_priv->xoff_lock); 5841da177e4SLinus Torvalds clip_priv->number = number; 5851da177e4SLinus Torvalds error = register_netdev(dev); 5861da177e4SLinus Torvalds if (error) { 5871da177e4SLinus Torvalds free_netdev(dev); 5881da177e4SLinus Torvalds return error; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds clip_priv->next = clip_devs; 5911da177e4SLinus Torvalds clip_devs = dev; 59252240062SStephen Hemminger pr_debug("registered (net:%s)\n", dev->name); 5931da177e4SLinus Torvalds return number; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds static int clip_device_event(struct notifier_block *this, unsigned long event, 597f3a0592bSStephen Hemminger void *arg) 5981da177e4SLinus Torvalds { 599f3a0592bSStephen Hemminger struct net_device *dev = arg; 600f3a0592bSStephen Hemminger 601721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 602e9dc8653SEric W. Biederman return NOTIFY_DONE; 603e9dc8653SEric W. Biederman 604f3a0592bSStephen Hemminger if (event == NETDEV_UNREGISTER) { 605f3a0592bSStephen Hemminger neigh_ifdown(&clip_tbl, dev); 6061da177e4SLinus Torvalds return NOTIFY_DONE; 607f3a0592bSStephen Hemminger } 608f3a0592bSStephen Hemminger 609f3a0592bSStephen Hemminger /* ignore non-CLIP devices */ 610dde09758SStephen Hemminger if (dev->type != ARPHRD_ATM || dev->netdev_ops != &clip_netdev_ops) 611f3a0592bSStephen Hemminger return NOTIFY_DONE; 612f3a0592bSStephen Hemminger 6131da177e4SLinus Torvalds switch (event) { 6141da177e4SLinus Torvalds case NETDEV_UP: 61552240062SStephen Hemminger pr_debug("clip_device_event NETDEV_UP\n"); 6165ff765f3SStephen Hemminger to_atmarpd(act_up, PRIV(dev)->number, 0); 6171da177e4SLinus Torvalds break; 6181da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 61952240062SStephen Hemminger pr_debug("clip_device_event NETDEV_DOWN\n"); 6205ff765f3SStephen Hemminger to_atmarpd(act_down, PRIV(dev)->number, 0); 6211da177e4SLinus Torvalds break; 6221da177e4SLinus Torvalds case NETDEV_CHANGE: 6231da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 62452240062SStephen Hemminger pr_debug("clip_device_event NETDEV_CHANGE*\n"); 6255ff765f3SStephen Hemminger to_atmarpd(act_change, PRIV(dev)->number, 0); 6261da177e4SLinus Torvalds break; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds return NOTIFY_DONE; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds static int clip_inet_event(struct notifier_block *this, unsigned long event, 6321da177e4SLinus Torvalds void *ifa) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds struct in_device *in_dev; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds in_dev = ((struct in_ifaddr *)ifa)->ifa_dev; 6371da177e4SLinus Torvalds /* 6381da177e4SLinus Torvalds * Transitions are of the down-change-up type, so it's sufficient to 6391da177e4SLinus Torvalds * handle the change on up. 6401da177e4SLinus Torvalds */ 641e49e76dbSStephen Hemminger if (event != NETDEV_UP) 642e49e76dbSStephen Hemminger return NOTIFY_DONE; 6431da177e4SLinus Torvalds return clip_device_event(this, NETDEV_CHANGE, in_dev->dev); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds static struct notifier_block clip_dev_notifier = { 6485ff765f3SStephen Hemminger .notifier_call = clip_device_event, 6491da177e4SLinus Torvalds }; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds static struct notifier_block clip_inet_notifier = { 6545ff765f3SStephen Hemminger .notifier_call = clip_inet_event, 6551da177e4SLinus Torvalds }; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds static void atmarpd_close(struct atm_vcc *vcc) 6601da177e4SLinus Torvalds { 66152240062SStephen Hemminger pr_debug("atmarpd_close\n"); 662f3a0592bSStephen Hemminger 663f3a0592bSStephen Hemminger rtnl_lock(); 664f3a0592bSStephen Hemminger atmarpd = NULL; 6651da177e4SLinus Torvalds skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); 666f3a0592bSStephen Hemminger rtnl_unlock(); 667f3a0592bSStephen Hemminger 66852240062SStephen Hemminger pr_debug("(done)\n"); 6691da177e4SLinus Torvalds module_put(THIS_MODULE); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds static struct atmdev_ops atmarpd_dev_ops = { 6741da177e4SLinus Torvalds .close = atmarpd_close 6751da177e4SLinus Torvalds }; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds static struct atm_dev atmarpd_dev = { 6791da177e4SLinus Torvalds .ops = &atmarpd_dev_ops, 6801da177e4SLinus Torvalds .type = "arpd", 6811da177e4SLinus Torvalds .number = 999, 6824ef8d0aeSMilind Arun Choudhary .lock = __SPIN_LOCK_UNLOCKED(atmarpd_dev.lock) 6831da177e4SLinus Torvalds }; 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds static int atm_init_atmarp(struct atm_vcc *vcc) 6871da177e4SLinus Torvalds { 688f3a0592bSStephen Hemminger rtnl_lock(); 689f3a0592bSStephen Hemminger if (atmarpd) { 690f3a0592bSStephen Hemminger rtnl_unlock(); 691f3a0592bSStephen Hemminger return -EADDRINUSE; 692f3a0592bSStephen Hemminger } 693f3a0592bSStephen Hemminger 6942d907392SStephen Hemminger mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ); 6952d907392SStephen Hemminger 6961da177e4SLinus Torvalds atmarpd = vcc; 6971da177e4SLinus Torvalds set_bit(ATM_VF_META,&vcc->flags); 6981da177e4SLinus Torvalds set_bit(ATM_VF_READY,&vcc->flags); 6991da177e4SLinus Torvalds /* allow replies and avoid getting closed if signaling dies */ 7001da177e4SLinus Torvalds vcc->dev = &atmarpd_dev; 7011da177e4SLinus Torvalds vcc_insert_socket(sk_atm(vcc)); 7021da177e4SLinus Torvalds vcc->push = NULL; 7031da177e4SLinus Torvalds vcc->pop = NULL; /* crash */ 7041da177e4SLinus Torvalds vcc->push_oam = NULL; /* crash */ 705f3a0592bSStephen Hemminger rtnl_unlock(); 7061da177e4SLinus Torvalds return 0; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds struct atm_vcc *vcc = ATM_SD(sock); 7121da177e4SLinus Torvalds int err = 0; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds switch (cmd) { 7151da177e4SLinus Torvalds case SIOCMKCLIP: 7161da177e4SLinus Torvalds case ATMARPD_CTRL: 7171da177e4SLinus Torvalds case ATMARP_MKIP: 7181da177e4SLinus Torvalds case ATMARP_SETENTRY: 7191da177e4SLinus Torvalds case ATMARP_ENCAP: 7201da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 7211da177e4SLinus Torvalds return -EPERM; 7221da177e4SLinus Torvalds break; 7231da177e4SLinus Torvalds default: 7241da177e4SLinus Torvalds return -ENOIOCTLCMD; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds switch (cmd) { 7281da177e4SLinus Torvalds case SIOCMKCLIP: 7291da177e4SLinus Torvalds err = clip_create(arg); 7301da177e4SLinus Torvalds break; 7311da177e4SLinus Torvalds case ATMARPD_CTRL: 7321da177e4SLinus Torvalds err = atm_init_atmarp(vcc); 7331da177e4SLinus Torvalds if (!err) { 7341da177e4SLinus Torvalds sock->state = SS_CONNECTED; 7351da177e4SLinus Torvalds __module_get(THIS_MODULE); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds break; 7381da177e4SLinus Torvalds case ATMARP_MKIP: 7391da177e4SLinus Torvalds err = clip_mkip(vcc, arg); 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds case ATMARP_SETENTRY: 74230d492daSAl Viro err = clip_setentry(vcc, (__force __be32)arg); 7431da177e4SLinus Torvalds break; 7441da177e4SLinus Torvalds case ATMARP_ENCAP: 7451da177e4SLinus Torvalds err = clip_encap(vcc, arg); 7461da177e4SLinus Torvalds break; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds return err; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds static struct atm_ioctl clip_ioctl_ops = { 7521da177e4SLinus Torvalds .owner = THIS_MODULE, 7531da177e4SLinus Torvalds .ioctl = clip_ioctl, 7541da177e4SLinus Torvalds }; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds static int code[] = { 1, 2, 10, 6, 1, 0 }; 7611da177e4SLinus Torvalds static int e164[] = { 1, 8, 4, 6, 1, 0 }; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (*addr->sas_addr.pub) { 7641da177e4SLinus Torvalds seq_printf(seq, "%s", addr->sas_addr.pub); 7651da177e4SLinus Torvalds if (*addr->sas_addr.prv) 7661da177e4SLinus Torvalds seq_putc(seq, '+'); 7671da177e4SLinus Torvalds } else if (!*addr->sas_addr.prv) { 7681da177e4SLinus Torvalds seq_printf(seq, "%s", "(none)"); 7691da177e4SLinus Torvalds return; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds if (*addr->sas_addr.prv) { 7721da177e4SLinus Torvalds unsigned char *prv = addr->sas_addr.prv; 7731da177e4SLinus Torvalds int *fields; 7741da177e4SLinus Torvalds int i, j; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds fields = *prv == ATM_AFI_E164 ? e164 : code; 7771da177e4SLinus Torvalds for (i = 0; fields[i]; i++) { 7781da177e4SLinus Torvalds for (j = fields[i]; j; j--) 7791da177e4SLinus Torvalds seq_printf(seq, "%02X", *prv++); 7801da177e4SLinus Torvalds if (fields[i + 1]) 7811da177e4SLinus Torvalds seq_putc(seq, '.'); 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* This means the neighbour entry has no attached VCC objects. */ 7871da177e4SLinus Torvalds #define SEQ_NO_VCC_TOKEN ((void *) 2) 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds static void atmarp_info(struct seq_file *seq, struct net_device *dev, 7901da177e4SLinus Torvalds struct atmarp_entry *entry, struct clip_vcc *clip_vcc) 7911da177e4SLinus Torvalds { 7921da177e4SLinus Torvalds unsigned long exp; 7931da177e4SLinus Torvalds char buf[17]; 7941da177e4SLinus Torvalds int svc, llc, off; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || 7971da177e4SLinus Torvalds (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC)); 7981da177e4SLinus Torvalds 799e49e76dbSStephen Hemminger llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds if (clip_vcc == SEQ_NO_VCC_TOKEN) 8021da177e4SLinus Torvalds exp = entry->neigh->used; 8031da177e4SLinus Torvalds else 8041da177e4SLinus Torvalds exp = clip_vcc->last_use; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds exp = (jiffies - exp) / HZ; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds seq_printf(seq, "%-6s%-4s%-4s%5ld ", 809e49e76dbSStephen Hemminger dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp); 8101da177e4SLinus Torvalds 81121454aaaSHarvey Harrison off = scnprintf(buf, sizeof(buf) - 1, "%pI4", 81221454aaaSHarvey Harrison &entry->ip); 8131da177e4SLinus Torvalds while (off < 16) 8141da177e4SLinus Torvalds buf[off++] = ' '; 8151da177e4SLinus Torvalds buf[off] = '\0'; 8161da177e4SLinus Torvalds seq_printf(seq, "%s", buf); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds if (clip_vcc == SEQ_NO_VCC_TOKEN) { 8191da177e4SLinus Torvalds if (time_before(jiffies, entry->expires)) 8201da177e4SLinus Torvalds seq_printf(seq, "(resolving)\n"); 8211da177e4SLinus Torvalds else 8221da177e4SLinus Torvalds seq_printf(seq, "(expired, ref %d)\n", 8231da177e4SLinus Torvalds atomic_read(&entry->neigh->refcnt)); 8241da177e4SLinus Torvalds } else if (!svc) { 8251da177e4SLinus Torvalds seq_printf(seq, "%d.%d.%d\n", 8261da177e4SLinus Torvalds clip_vcc->vcc->dev->number, 827e49e76dbSStephen Hemminger clip_vcc->vcc->vpi, clip_vcc->vcc->vci); 8281da177e4SLinus Torvalds } else { 8291da177e4SLinus Torvalds svc_addr(seq, &clip_vcc->vcc->remote); 8301da177e4SLinus Torvalds seq_putc(seq, '\n'); 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds struct clip_seq_state { 8351da177e4SLinus Torvalds /* This member must be first. */ 8361da177e4SLinus Torvalds struct neigh_seq_state ns; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /* Local to clip specific iteration. */ 8391da177e4SLinus Torvalds struct clip_vcc *vcc; 8401da177e4SLinus Torvalds }; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e, 8431da177e4SLinus Torvalds struct clip_vcc *curr) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds if (!curr) { 8461da177e4SLinus Torvalds curr = e->vccs; 8471da177e4SLinus Torvalds if (!curr) 8481da177e4SLinus Torvalds return SEQ_NO_VCC_TOKEN; 8491da177e4SLinus Torvalds return curr; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds if (curr == SEQ_NO_VCC_TOKEN) 8521da177e4SLinus Torvalds return NULL; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds curr = curr->next; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds return curr; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds static void *clip_seq_vcc_walk(struct clip_seq_state *state, 8601da177e4SLinus Torvalds struct atmarp_entry *e, loff_t * pos) 8611da177e4SLinus Torvalds { 8621da177e4SLinus Torvalds struct clip_vcc *vcc = state->vcc; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds vcc = clip_seq_next_vcc(e, vcc); 8651da177e4SLinus Torvalds if (vcc && pos != NULL) { 8661da177e4SLinus Torvalds while (*pos) { 8671da177e4SLinus Torvalds vcc = clip_seq_next_vcc(e, vcc); 8681da177e4SLinus Torvalds if (!vcc) 8691da177e4SLinus Torvalds break; 8701da177e4SLinus Torvalds --(*pos); 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds state->vcc = vcc; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds return vcc; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds static void *clip_seq_sub_iter(struct neigh_seq_state *_state, 8791da177e4SLinus Torvalds struct neighbour *n, loff_t * pos) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds struct clip_seq_state *state = (struct clip_seq_state *)_state; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos); 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds static void *clip_seq_start(struct seq_file *seq, loff_t * pos) 8871da177e4SLinus Torvalds { 888ae22120aSDenis V. Lunev struct clip_seq_state *state = seq->private; 889ae22120aSDenis V. Lunev state->ns.neigh_sub_iter = clip_seq_sub_iter; 8901da177e4SLinus Torvalds return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY); 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds static int clip_seq_show(struct seq_file *seq, void *v) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds static char atm_arp_banner[] = 8961da177e4SLinus Torvalds "IPitf TypeEncp Idle IP address ATM address\n"; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 8991da177e4SLinus Torvalds seq_puts(seq, atm_arp_banner); 9001da177e4SLinus Torvalds } else { 9011da177e4SLinus Torvalds struct clip_seq_state *state = seq->private; 9021da177e4SLinus Torvalds struct neighbour *n = v; 9031da177e4SLinus Torvalds struct clip_vcc *vcc = state->vcc; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc); 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds return 0; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 91056b3d975SPhilippe De Muyter static const struct seq_operations arp_seq_ops = { 9111da177e4SLinus Torvalds .start = clip_seq_start, 9121da177e4SLinus Torvalds .next = neigh_seq_next, 9131da177e4SLinus Torvalds .stop = neigh_seq_stop, 9141da177e4SLinus Torvalds .show = clip_seq_show, 9151da177e4SLinus Torvalds }; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds static int arp_seq_open(struct inode *inode, struct file *file) 9181da177e4SLinus Torvalds { 919ae22120aSDenis V. Lunev return seq_open_net(inode, file, &arp_seq_ops, 920ae22120aSDenis V. Lunev sizeof(struct clip_seq_state)); 921426b5303SEric W. Biederman } 922426b5303SEric W. Biederman 9239a32144eSArjan van de Ven static const struct file_operations arp_seq_fops = { 9241da177e4SLinus Torvalds .open = arp_seq_open, 9251da177e4SLinus Torvalds .read = seq_read, 9261da177e4SLinus Torvalds .llseek = seq_lseek, 927ae22120aSDenis V. Lunev .release = seq_release_net, 9281da177e4SLinus Torvalds .owner = THIS_MODULE 9291da177e4SLinus Torvalds }; 9301da177e4SLinus Torvalds #endif 9311da177e4SLinus Torvalds 932dbee0d3fSWang Chen static void atm_clip_exit_noproc(void); 933dbee0d3fSWang Chen 9341da177e4SLinus Torvalds static int __init atm_clip_init(void) 9351da177e4SLinus Torvalds { 936bd89efc5SSimon Kelley neigh_table_init_no_netlink(&clip_tbl); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds clip_tbl_hook = &clip_tbl; 9391da177e4SLinus Torvalds register_atm_ioctl(&clip_ioctl_ops); 940f3a0592bSStephen Hemminger register_netdevice_notifier(&clip_dev_notifier); 941f3a0592bSStephen Hemminger register_inetaddr_notifier(&clip_inet_notifier); 9421da177e4SLinus Torvalds 9432d907392SStephen Hemminger setup_timer(&idle_timer, idle_timer_check, 0); 9442d907392SStephen Hemminger 94524781734SAdrian Bunk #ifdef CONFIG_PROC_FS 94624781734SAdrian Bunk { 94724781734SAdrian Bunk struct proc_dir_entry *p; 94824781734SAdrian Bunk 94916e297b3SWang Chen p = proc_create("arp", S_IRUGO, atm_proc_root, &arp_seq_fops); 950dbee0d3fSWang Chen if (!p) { 951dbee0d3fSWang Chen printk(KERN_ERR "Unable to initialize " 952dbee0d3fSWang Chen "/proc/net/atm/arp\n"); 953dbee0d3fSWang Chen atm_clip_exit_noproc(); 954dbee0d3fSWang Chen return -ENOMEM; 955dbee0d3fSWang Chen } 95624781734SAdrian Bunk } 95724781734SAdrian Bunk #endif 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds return 0; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 962dbee0d3fSWang Chen static void atm_clip_exit_noproc(void) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds struct net_device *dev, *next; 9651da177e4SLinus Torvalds 966f3a0592bSStephen Hemminger unregister_inetaddr_notifier(&clip_inet_notifier); 967f3a0592bSStephen Hemminger unregister_netdevice_notifier(&clip_dev_notifier); 968f3a0592bSStephen Hemminger 9691da177e4SLinus Torvalds deregister_atm_ioctl(&clip_ioctl_ops); 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds /* First, stop the idle timer, so it stops banging 9721da177e4SLinus Torvalds * on the table. 9731da177e4SLinus Torvalds */ 9742d907392SStephen Hemminger del_timer_sync(&idle_timer); 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* Next, purge the table, so that the device 9771da177e4SLinus Torvalds * unregister loop below does not hang due to 9781da177e4SLinus Torvalds * device references remaining in the table. 9791da177e4SLinus Torvalds */ 9801da177e4SLinus Torvalds neigh_ifdown(&clip_tbl, NULL); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds dev = clip_devs; 9831da177e4SLinus Torvalds while (dev) { 9841da177e4SLinus Torvalds next = PRIV(dev)->next; 9851da177e4SLinus Torvalds unregister_netdev(dev); 9861da177e4SLinus Torvalds free_netdev(dev); 9871da177e4SLinus Torvalds dev = next; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* Now it is safe to fully shutdown whole table. */ 9911da177e4SLinus Torvalds neigh_table_clear(&clip_tbl); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds clip_tbl_hook = NULL; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996dbee0d3fSWang Chen static void __exit atm_clip_exit(void) 997dbee0d3fSWang Chen { 998dbee0d3fSWang Chen remove_proc_entry("arp", atm_proc_root); 999dbee0d3fSWang Chen 1000dbee0d3fSWang Chen atm_clip_exit_noproc(); 1001dbee0d3fSWang Chen } 1002dbee0d3fSWang Chen 10031da177e4SLinus Torvalds module_init(atm_clip_init); 10041da177e4SLinus Torvalds module_exit(atm_clip_exit); 10054909e488SStephen Hemminger MODULE_AUTHOR("Werner Almesberger"); 10064909e488SStephen Hemminger MODULE_DESCRIPTION("Classical/IP over ATM interface"); 10071da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1008