Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* net/atm/clip.c - RFC1577 Classical IP over ATM */
0003 
0004 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
0005 
0006 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
0007 
0008 #include <linux/string.h>
0009 #include <linux/errno.h>
0010 #include <linux/kernel.h> /* for UINT_MAX */
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/netdevice.h>
0014 #include <linux/skbuff.h>
0015 #include <linux/wait.h>
0016 #include <linux/timer.h>
0017 #include <linux/if_arp.h> /* for some manifest constants */
0018 #include <linux/notifier.h>
0019 #include <linux/atm.h>
0020 #include <linux/atmdev.h>
0021 #include <linux/atmclip.h>
0022 #include <linux/atmarp.h>
0023 #include <linux/capability.h>
0024 #include <linux/ip.h> /* for net/route.h */
0025 #include <linux/in.h> /* for struct sockaddr_in */
0026 #include <linux/if.h> /* for IFF_UP */
0027 #include <linux/inetdevice.h>
0028 #include <linux/bitops.h>
0029 #include <linux/poison.h>
0030 #include <linux/proc_fs.h>
0031 #include <linux/seq_file.h>
0032 #include <linux/rcupdate.h>
0033 #include <linux/jhash.h>
0034 #include <linux/slab.h>
0035 #include <net/route.h> /* for struct rtable and routing */
0036 #include <net/icmp.h> /* icmp_send */
0037 #include <net/arp.h>
0038 #include <linux/param.h> /* for HZ */
0039 #include <linux/uaccess.h>
0040 #include <asm/byteorder.h> /* for htons etc. */
0041 #include <linux/atomic.h>
0042 
0043 #include "common.h"
0044 #include "resources.h"
0045 #include <net/atmclip.h>
0046 
0047 static struct net_device *clip_devs;
0048 static struct atm_vcc *atmarpd;
0049 static struct timer_list idle_timer;
0050 static const struct neigh_ops clip_neigh_ops;
0051 
0052 static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip)
0053 {
0054     struct sock *sk;
0055     struct atmarp_ctrl *ctrl;
0056     struct sk_buff *skb;
0057 
0058     pr_debug("(%d)\n", type);
0059     if (!atmarpd)
0060         return -EUNATCH;
0061     skb = alloc_skb(sizeof(struct atmarp_ctrl), GFP_ATOMIC);
0062     if (!skb)
0063         return -ENOMEM;
0064     ctrl = skb_put(skb, sizeof(struct atmarp_ctrl));
0065     ctrl->type = type;
0066     ctrl->itf_num = itf;
0067     ctrl->ip = ip;
0068     atm_force_charge(atmarpd, skb->truesize);
0069 
0070     sk = sk_atm(atmarpd);
0071     skb_queue_tail(&sk->sk_receive_queue, skb);
0072     sk->sk_data_ready(sk);
0073     return 0;
0074 }
0075 
0076 static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry)
0077 {
0078     pr_debug("%p to entry %p (neigh %p)\n", clip_vcc, entry, entry->neigh);
0079     clip_vcc->entry = entry;
0080     clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
0081     clip_vcc->next = entry->vccs;
0082     entry->vccs = clip_vcc;
0083     entry->neigh->used = jiffies;
0084 }
0085 
0086 static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
0087 {
0088     struct atmarp_entry *entry = clip_vcc->entry;
0089     struct clip_vcc **walk;
0090 
0091     if (!entry) {
0092         pr_err("!clip_vcc->entry (clip_vcc %p)\n", clip_vcc);
0093         return;
0094     }
0095     netif_tx_lock_bh(entry->neigh->dev);    /* block clip_start_xmit() */
0096     entry->neigh->used = jiffies;
0097     for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
0098         if (*walk == clip_vcc) {
0099             int error;
0100 
0101             *walk = clip_vcc->next; /* atomic */
0102             clip_vcc->entry = NULL;
0103             if (clip_vcc->xoff)
0104                 netif_wake_queue(entry->neigh->dev);
0105             if (entry->vccs)
0106                 goto out;
0107             entry->expires = jiffies - 1;
0108             /* force resolution or expiration */
0109             error = neigh_update(entry->neigh, NULL, NUD_NONE,
0110                          NEIGH_UPDATE_F_ADMIN, 0);
0111             if (error)
0112                 pr_err("neigh_update failed with %d\n", error);
0113             goto out;
0114         }
0115     pr_err("ATMARP: failed (entry %p, vcc 0x%p)\n", entry, clip_vcc);
0116 out:
0117     netif_tx_unlock_bh(entry->neigh->dev);
0118 }
0119 
0120 /* The neighbour entry n->lock is held. */
0121 static int neigh_check_cb(struct neighbour *n)
0122 {
0123     struct atmarp_entry *entry = neighbour_priv(n);
0124     struct clip_vcc *cv;
0125 
0126     if (n->ops != &clip_neigh_ops)
0127         return 0;
0128     for (cv = entry->vccs; cv; cv = cv->next) {
0129         unsigned long exp = cv->last_use + cv->idle_timeout;
0130 
0131         if (cv->idle_timeout && time_after(jiffies, exp)) {
0132             pr_debug("releasing vcc %p->%p of entry %p\n",
0133                  cv, cv->vcc, entry);
0134             vcc_release_async(cv->vcc, -ETIMEDOUT);
0135         }
0136     }
0137 
0138     if (entry->vccs || time_before(jiffies, entry->expires))
0139         return 0;
0140 
0141     if (refcount_read(&n->refcnt) > 1) {
0142         struct sk_buff *skb;
0143 
0144         pr_debug("destruction postponed with ref %d\n",
0145              refcount_read(&n->refcnt));
0146 
0147         while ((skb = skb_dequeue(&n->arp_queue)) != NULL)
0148             dev_kfree_skb(skb);
0149 
0150         return 0;
0151     }
0152 
0153     pr_debug("expired neigh %p\n", n);
0154     return 1;
0155 }
0156 
0157 static void idle_timer_check(struct timer_list *unused)
0158 {
0159     write_lock(&arp_tbl.lock);
0160     __neigh_for_each_release(&arp_tbl, neigh_check_cb);
0161     mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
0162     write_unlock(&arp_tbl.lock);
0163 }
0164 
0165 static int clip_arp_rcv(struct sk_buff *skb)
0166 {
0167     struct atm_vcc *vcc;
0168 
0169     pr_debug("\n");
0170     vcc = ATM_SKB(skb)->vcc;
0171     if (!vcc || !atm_charge(vcc, skb->truesize)) {
0172         dev_kfree_skb_any(skb);
0173         return 0;
0174     }
0175     pr_debug("pushing to %p\n", vcc);
0176     pr_debug("using %p\n", CLIP_VCC(vcc)->old_push);
0177     CLIP_VCC(vcc)->old_push(vcc, skb);
0178     return 0;
0179 }
0180 
0181 static const unsigned char llc_oui[] = {
0182     0xaa,   /* DSAP: non-ISO */
0183     0xaa,   /* SSAP: non-ISO */
0184     0x03,   /* Ctrl: Unnumbered Information Command PDU */
0185     0x00,   /* OUI: EtherType */
0186     0x00,
0187     0x00
0188 };
0189 
0190 static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb)
0191 {
0192     struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
0193 
0194     pr_debug("\n");
0195 
0196     if (!clip_devs) {
0197         atm_return(vcc, skb->truesize);
0198         kfree_skb(skb);
0199         return;
0200     }
0201 
0202     if (!skb) {
0203         pr_debug("removing VCC %p\n", clip_vcc);
0204         if (clip_vcc->entry)
0205             unlink_clip_vcc(clip_vcc);
0206         clip_vcc->old_push(vcc, NULL);  /* pass on the bad news */
0207         kfree(clip_vcc);
0208         return;
0209     }
0210     atm_return(vcc, skb->truesize);
0211     skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
0212     /* clip_vcc->entry == NULL if we don't have an IP address yet */
0213     if (!skb->dev) {
0214         dev_kfree_skb_any(skb);
0215         return;
0216     }
0217     ATM_SKB(skb)->vcc = vcc;
0218     skb_reset_mac_header(skb);
0219     if (!clip_vcc->encap ||
0220         skb->len < RFC1483LLC_LEN ||
0221         memcmp(skb->data, llc_oui, sizeof(llc_oui)))
0222         skb->protocol = htons(ETH_P_IP);
0223     else {
0224         skb->protocol = ((__be16 *)skb->data)[3];
0225         skb_pull(skb, RFC1483LLC_LEN);
0226         if (skb->protocol == htons(ETH_P_ARP)) {
0227             skb->dev->stats.rx_packets++;
0228             skb->dev->stats.rx_bytes += skb->len;
0229             clip_arp_rcv(skb);
0230             return;
0231         }
0232     }
0233     clip_vcc->last_use = jiffies;
0234     skb->dev->stats.rx_packets++;
0235     skb->dev->stats.rx_bytes += skb->len;
0236     memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
0237     netif_rx(skb);
0238 }
0239 
0240 /*
0241  * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
0242  * clip_pop is atomic with respect to the critical section in clip_start_xmit.
0243  */
0244 
0245 static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb)
0246 {
0247     struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
0248     struct net_device *dev = skb->dev;
0249     int old;
0250     unsigned long flags;
0251 
0252     pr_debug("(vcc %p)\n", vcc);
0253     clip_vcc->old_pop(vcc, skb);
0254     /* skb->dev == NULL in outbound ARP packets */
0255     if (!dev)
0256         return;
0257     spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags);
0258     if (atm_may_send(vcc, 0)) {
0259         old = xchg(&clip_vcc->xoff, 0);
0260         if (old)
0261             netif_wake_queue(dev);
0262     }
0263     spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags);
0264 }
0265 
0266 static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb)
0267 {
0268     __be32 *ip = (__be32 *) neigh->primary_key;
0269 
0270     pr_debug("(neigh %p, skb %p)\n", neigh, skb);
0271     to_atmarpd(act_need, PRIV(neigh->dev)->number, *ip);
0272 }
0273 
0274 static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb)
0275 {
0276 #ifndef CONFIG_ATM_CLIP_NO_ICMP
0277     icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
0278 #endif
0279     kfree_skb(skb);
0280 }
0281 
0282 static const struct neigh_ops clip_neigh_ops = {
0283     .family =       AF_INET,
0284     .solicit =      clip_neigh_solicit,
0285     .error_report =     clip_neigh_error,
0286     .output =       neigh_direct_output,
0287     .connected_output = neigh_direct_output,
0288 };
0289 
0290 static int clip_constructor(struct net_device *dev, struct neighbour *neigh)
0291 {
0292     struct atmarp_entry *entry = neighbour_priv(neigh);
0293 
0294     if (neigh->tbl->family != AF_INET)
0295         return -EINVAL;
0296 
0297     if (neigh->type != RTN_UNICAST)
0298         return -EINVAL;
0299 
0300     neigh->nud_state = NUD_NONE;
0301     neigh->ops = &clip_neigh_ops;
0302     neigh->output = neigh->ops->output;
0303     entry->neigh = neigh;
0304     entry->vccs = NULL;
0305     entry->expires = jiffies - 1;
0306 
0307     return 0;
0308 }
0309 
0310 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
0311 
0312 /*
0313  * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
0314  * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
0315  * don't increment the usage count. This is used to create entries in
0316  * clip_setentry.
0317  */
0318 
0319 static int clip_encap(struct atm_vcc *vcc, int mode)
0320 {
0321     if (!CLIP_VCC(vcc))
0322         return -EBADFD;
0323 
0324     CLIP_VCC(vcc)->encap = mode;
0325     return 0;
0326 }
0327 
0328 static netdev_tx_t clip_start_xmit(struct sk_buff *skb,
0329                    struct net_device *dev)
0330 {
0331     struct clip_priv *clip_priv = PRIV(dev);
0332     struct dst_entry *dst = skb_dst(skb);
0333     struct atmarp_entry *entry;
0334     struct neighbour *n;
0335     struct atm_vcc *vcc;
0336     struct rtable *rt;
0337     __be32 *daddr;
0338     int old;
0339     unsigned long flags;
0340 
0341     pr_debug("(skb %p)\n", skb);
0342     if (!dst) {
0343         pr_err("skb_dst(skb) == NULL\n");
0344         dev_kfree_skb(skb);
0345         dev->stats.tx_dropped++;
0346         return NETDEV_TX_OK;
0347     }
0348     rt = (struct rtable *) dst;
0349     if (rt->rt_gw_family == AF_INET)
0350         daddr = &rt->rt_gw4;
0351     else
0352         daddr = &ip_hdr(skb)->daddr;
0353     n = dst_neigh_lookup(dst, daddr);
0354     if (!n) {
0355         pr_err("NO NEIGHBOUR !\n");
0356         dev_kfree_skb(skb);
0357         dev->stats.tx_dropped++;
0358         return NETDEV_TX_OK;
0359     }
0360     entry = neighbour_priv(n);
0361     if (!entry->vccs) {
0362         if (time_after(jiffies, entry->expires)) {
0363             /* should be resolved */
0364             entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ;
0365             to_atmarpd(act_need, PRIV(dev)->number, *((__be32 *)n->primary_key));
0366         }
0367         if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
0368             skb_queue_tail(&entry->neigh->arp_queue, skb);
0369         else {
0370             dev_kfree_skb(skb);
0371             dev->stats.tx_dropped++;
0372         }
0373         goto out_release_neigh;
0374     }
0375     pr_debug("neigh %p, vccs %p\n", entry, entry->vccs);
0376     ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
0377     pr_debug("using neighbour %p, vcc %p\n", n, vcc);
0378     if (entry->vccs->encap) {
0379         void *here;
0380 
0381         here = skb_push(skb, RFC1483LLC_LEN);
0382         memcpy(here, llc_oui, sizeof(llc_oui));
0383         ((__be16 *) here)[3] = skb->protocol;
0384     }
0385     atm_account_tx(vcc, skb);
0386     entry->vccs->last_use = jiffies;
0387     pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev);
0388     old = xchg(&entry->vccs->xoff, 1);  /* assume XOFF ... */
0389     if (old) {
0390         pr_warn("XOFF->XOFF transition\n");
0391         goto out_release_neigh;
0392     }
0393     dev->stats.tx_packets++;
0394     dev->stats.tx_bytes += skb->len;
0395     vcc->send(vcc, skb);
0396     if (atm_may_send(vcc, 0)) {
0397         entry->vccs->xoff = 0;
0398         goto out_release_neigh;
0399     }
0400     spin_lock_irqsave(&clip_priv->xoff_lock, flags);
0401     netif_stop_queue(dev);  /* XOFF -> throttle immediately */
0402     barrier();
0403     if (!entry->vccs->xoff)
0404         netif_start_queue(dev);
0405     /* Oh, we just raced with clip_pop. netif_start_queue should be
0406        good enough, because nothing should really be asleep because
0407        of the brief netif_stop_queue. If this isn't true or if it
0408        changes, use netif_wake_queue instead. */
0409     spin_unlock_irqrestore(&clip_priv->xoff_lock, flags);
0410 out_release_neigh:
0411     neigh_release(n);
0412     return NETDEV_TX_OK;
0413 }
0414 
0415 static int clip_mkip(struct atm_vcc *vcc, int timeout)
0416 {
0417     struct clip_vcc *clip_vcc;
0418 
0419     if (!vcc->push)
0420         return -EBADFD;
0421     clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL);
0422     if (!clip_vcc)
0423         return -ENOMEM;
0424     pr_debug("%p vcc %p\n", clip_vcc, vcc);
0425     clip_vcc->vcc = vcc;
0426     vcc->user_back = clip_vcc;
0427     set_bit(ATM_VF_IS_CLIP, &vcc->flags);
0428     clip_vcc->entry = NULL;
0429     clip_vcc->xoff = 0;
0430     clip_vcc->encap = 1;
0431     clip_vcc->last_use = jiffies;
0432     clip_vcc->idle_timeout = timeout * HZ;
0433     clip_vcc->old_push = vcc->push;
0434     clip_vcc->old_pop = vcc->pop;
0435     vcc->push = clip_push;
0436     vcc->pop = clip_pop;
0437 
0438     /* re-process everything received between connection setup and MKIP */
0439     vcc_process_recv_queue(vcc);
0440 
0441     return 0;
0442 }
0443 
0444 static int clip_setentry(struct atm_vcc *vcc, __be32 ip)
0445 {
0446     struct neighbour *neigh;
0447     struct atmarp_entry *entry;
0448     int error;
0449     struct clip_vcc *clip_vcc;
0450     struct rtable *rt;
0451 
0452     if (vcc->push != clip_push) {
0453         pr_warn("non-CLIP VCC\n");
0454         return -EBADF;
0455     }
0456     clip_vcc = CLIP_VCC(vcc);
0457     if (!ip) {
0458         if (!clip_vcc->entry) {
0459             pr_err("hiding hidden ATMARP entry\n");
0460             return 0;
0461         }
0462         pr_debug("remove\n");
0463         unlink_clip_vcc(clip_vcc);
0464         return 0;
0465     }
0466     rt = ip_route_output(&init_net, ip, 0, 1, 0);
0467     if (IS_ERR(rt))
0468         return PTR_ERR(rt);
0469     neigh = __neigh_lookup(&arp_tbl, &ip, rt->dst.dev, 1);
0470     ip_rt_put(rt);
0471     if (!neigh)
0472         return -ENOMEM;
0473     entry = neighbour_priv(neigh);
0474     if (entry != clip_vcc->entry) {
0475         if (!clip_vcc->entry)
0476             pr_debug("add\n");
0477         else {
0478             pr_debug("update\n");
0479             unlink_clip_vcc(clip_vcc);
0480         }
0481         link_vcc(clip_vcc, entry);
0482     }
0483     error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
0484                  NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN, 0);
0485     neigh_release(neigh);
0486     return error;
0487 }
0488 
0489 static const struct net_device_ops clip_netdev_ops = {
0490     .ndo_start_xmit     = clip_start_xmit,
0491     .ndo_neigh_construct    = clip_constructor,
0492 };
0493 
0494 static void clip_setup(struct net_device *dev)
0495 {
0496     dev->netdev_ops = &clip_netdev_ops;
0497     dev->type = ARPHRD_ATM;
0498     dev->neigh_priv_len = sizeof(struct atmarp_entry);
0499     dev->hard_header_len = RFC1483LLC_LEN;
0500     dev->mtu = RFC1626_MTU;
0501     dev->tx_queue_len = 100;    /* "normal" queue (packets) */
0502     /* When using a "real" qdisc, the qdisc determines the queue */
0503     /* length. tx_queue_len is only used for the default case, */
0504     /* without any more elaborate queuing. 100 is a reasonable */
0505     /* compromise between decent burst-tolerance and protection */
0506     /* against memory hogs. */
0507     netif_keep_dst(dev);
0508 }
0509 
0510 static int clip_create(int number)
0511 {
0512     struct net_device *dev;
0513     struct clip_priv *clip_priv;
0514     int error;
0515 
0516     if (number != -1) {
0517         for (dev = clip_devs; dev; dev = PRIV(dev)->next)
0518             if (PRIV(dev)->number == number)
0519                 return -EEXIST;
0520     } else {
0521         number = 0;
0522         for (dev = clip_devs; dev; dev = PRIV(dev)->next)
0523             if (PRIV(dev)->number >= number)
0524                 number = PRIV(dev)->number + 1;
0525     }
0526     dev = alloc_netdev(sizeof(struct clip_priv), "", NET_NAME_UNKNOWN,
0527                clip_setup);
0528     if (!dev)
0529         return -ENOMEM;
0530     clip_priv = PRIV(dev);
0531     sprintf(dev->name, "atm%d", number);
0532     spin_lock_init(&clip_priv->xoff_lock);
0533     clip_priv->number = number;
0534     error = register_netdev(dev);
0535     if (error) {
0536         free_netdev(dev);
0537         return error;
0538     }
0539     clip_priv->next = clip_devs;
0540     clip_devs = dev;
0541     pr_debug("registered (net:%s)\n", dev->name);
0542     return number;
0543 }
0544 
0545 static int clip_device_event(struct notifier_block *this, unsigned long event,
0546                  void *ptr)
0547 {
0548     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0549 
0550     if (!net_eq(dev_net(dev), &init_net))
0551         return NOTIFY_DONE;
0552 
0553     if (event == NETDEV_UNREGISTER)
0554         return NOTIFY_DONE;
0555 
0556     /* ignore non-CLIP devices */
0557     if (dev->type != ARPHRD_ATM || dev->netdev_ops != &clip_netdev_ops)
0558         return NOTIFY_DONE;
0559 
0560     switch (event) {
0561     case NETDEV_UP:
0562         pr_debug("NETDEV_UP\n");
0563         to_atmarpd(act_up, PRIV(dev)->number, 0);
0564         break;
0565     case NETDEV_GOING_DOWN:
0566         pr_debug("NETDEV_DOWN\n");
0567         to_atmarpd(act_down, PRIV(dev)->number, 0);
0568         break;
0569     case NETDEV_CHANGE:
0570     case NETDEV_CHANGEMTU:
0571         pr_debug("NETDEV_CHANGE*\n");
0572         to_atmarpd(act_change, PRIV(dev)->number, 0);
0573         break;
0574     }
0575     return NOTIFY_DONE;
0576 }
0577 
0578 static int clip_inet_event(struct notifier_block *this, unsigned long event,
0579                void *ifa)
0580 {
0581     struct in_device *in_dev;
0582     struct netdev_notifier_info info;
0583 
0584     in_dev = ((struct in_ifaddr *)ifa)->ifa_dev;
0585     /*
0586      * Transitions are of the down-change-up type, so it's sufficient to
0587      * handle the change on up.
0588      */
0589     if (event != NETDEV_UP)
0590         return NOTIFY_DONE;
0591     netdev_notifier_info_init(&info, in_dev->dev);
0592     return clip_device_event(this, NETDEV_CHANGE, &info);
0593 }
0594 
0595 static struct notifier_block clip_dev_notifier = {
0596     .notifier_call = clip_device_event,
0597 };
0598 
0599 
0600 
0601 static struct notifier_block clip_inet_notifier = {
0602     .notifier_call = clip_inet_event,
0603 };
0604 
0605 
0606 
0607 static void atmarpd_close(struct atm_vcc *vcc)
0608 {
0609     pr_debug("\n");
0610 
0611     rtnl_lock();
0612     atmarpd = NULL;
0613     skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
0614     rtnl_unlock();
0615 
0616     pr_debug("(done)\n");
0617     module_put(THIS_MODULE);
0618 }
0619 
0620 static const struct atmdev_ops atmarpd_dev_ops = {
0621     .close = atmarpd_close
0622 };
0623 
0624 
0625 static struct atm_dev atmarpd_dev = {
0626     .ops =          &atmarpd_dev_ops,
0627     .type =         "arpd",
0628     .number =       999,
0629     .lock =         __SPIN_LOCK_UNLOCKED(atmarpd_dev.lock)
0630 };
0631 
0632 
0633 static int atm_init_atmarp(struct atm_vcc *vcc)
0634 {
0635     rtnl_lock();
0636     if (atmarpd) {
0637         rtnl_unlock();
0638         return -EADDRINUSE;
0639     }
0640 
0641     mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
0642 
0643     atmarpd = vcc;
0644     set_bit(ATM_VF_META, &vcc->flags);
0645     set_bit(ATM_VF_READY, &vcc->flags);
0646         /* allow replies and avoid getting closed if signaling dies */
0647     vcc->dev = &atmarpd_dev;
0648     vcc_insert_socket(sk_atm(vcc));
0649     vcc->push = NULL;
0650     vcc->pop = NULL; /* crash */
0651     vcc->push_oam = NULL; /* crash */
0652     rtnl_unlock();
0653     return 0;
0654 }
0655 
0656 static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
0657 {
0658     struct atm_vcc *vcc = ATM_SD(sock);
0659     int err = 0;
0660 
0661     switch (cmd) {
0662     case SIOCMKCLIP:
0663     case ATMARPD_CTRL:
0664     case ATMARP_MKIP:
0665     case ATMARP_SETENTRY:
0666     case ATMARP_ENCAP:
0667         if (!capable(CAP_NET_ADMIN))
0668             return -EPERM;
0669         break;
0670     default:
0671         return -ENOIOCTLCMD;
0672     }
0673 
0674     switch (cmd) {
0675     case SIOCMKCLIP:
0676         err = clip_create(arg);
0677         break;
0678     case ATMARPD_CTRL:
0679         err = atm_init_atmarp(vcc);
0680         if (!err) {
0681             sock->state = SS_CONNECTED;
0682             __module_get(THIS_MODULE);
0683         }
0684         break;
0685     case ATMARP_MKIP:
0686         err = clip_mkip(vcc, arg);
0687         break;
0688     case ATMARP_SETENTRY:
0689         err = clip_setentry(vcc, (__force __be32)arg);
0690         break;
0691     case ATMARP_ENCAP:
0692         err = clip_encap(vcc, arg);
0693         break;
0694     }
0695     return err;
0696 }
0697 
0698 static struct atm_ioctl clip_ioctl_ops = {
0699     .owner = THIS_MODULE,
0700     .ioctl = clip_ioctl,
0701 };
0702 
0703 #ifdef CONFIG_PROC_FS
0704 
0705 static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
0706 {
0707     static int code[] = { 1, 2, 10, 6, 1, 0 };
0708     static int e164[] = { 1, 8, 4, 6, 1, 0 };
0709 
0710     if (*addr->sas_addr.pub) {
0711         seq_printf(seq, "%s", addr->sas_addr.pub);
0712         if (*addr->sas_addr.prv)
0713             seq_putc(seq, '+');
0714     } else if (!*addr->sas_addr.prv) {
0715         seq_printf(seq, "%s", "(none)");
0716         return;
0717     }
0718     if (*addr->sas_addr.prv) {
0719         unsigned char *prv = addr->sas_addr.prv;
0720         int *fields;
0721         int i, j;
0722 
0723         fields = *prv == ATM_AFI_E164 ? e164 : code;
0724         for (i = 0; fields[i]; i++) {
0725             for (j = fields[i]; j; j--)
0726                 seq_printf(seq, "%02X", *prv++);
0727             if (fields[i + 1])
0728                 seq_putc(seq, '.');
0729         }
0730     }
0731 }
0732 
0733 /* This means the neighbour entry has no attached VCC objects. */
0734 #define SEQ_NO_VCC_TOKEN    ((void *) 2)
0735 
0736 static void atmarp_info(struct seq_file *seq, struct neighbour *n,
0737             struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
0738 {
0739     struct net_device *dev = n->dev;
0740     unsigned long exp;
0741     char buf[17];
0742     int svc, llc, off;
0743 
0744     svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
0745            (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
0746 
0747     llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap);
0748 
0749     if (clip_vcc == SEQ_NO_VCC_TOKEN)
0750         exp = entry->neigh->used;
0751     else
0752         exp = clip_vcc->last_use;
0753 
0754     exp = (jiffies - exp) / HZ;
0755 
0756     seq_printf(seq, "%-6s%-4s%-4s%5ld ",
0757            dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp);
0758 
0759     off = scnprintf(buf, sizeof(buf) - 1, "%pI4", n->primary_key);
0760     while (off < 16)
0761         buf[off++] = ' ';
0762     buf[off] = '\0';
0763     seq_printf(seq, "%s", buf);
0764 
0765     if (clip_vcc == SEQ_NO_VCC_TOKEN) {
0766         if (time_before(jiffies, entry->expires))
0767             seq_printf(seq, "(resolving)\n");
0768         else
0769             seq_printf(seq, "(expired, ref %d)\n",
0770                    refcount_read(&entry->neigh->refcnt));
0771     } else if (!svc) {
0772         seq_printf(seq, "%d.%d.%d\n",
0773                clip_vcc->vcc->dev->number,
0774                clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
0775     } else {
0776         svc_addr(seq, &clip_vcc->vcc->remote);
0777         seq_putc(seq, '\n');
0778     }
0779 }
0780 
0781 struct clip_seq_state {
0782     /* This member must be first. */
0783     struct neigh_seq_state ns;
0784 
0785     /* Local to clip specific iteration. */
0786     struct clip_vcc *vcc;
0787 };
0788 
0789 static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
0790                       struct clip_vcc *curr)
0791 {
0792     if (!curr) {
0793         curr = e->vccs;
0794         if (!curr)
0795             return SEQ_NO_VCC_TOKEN;
0796         return curr;
0797     }
0798     if (curr == SEQ_NO_VCC_TOKEN)
0799         return NULL;
0800 
0801     curr = curr->next;
0802 
0803     return curr;
0804 }
0805 
0806 static void *clip_seq_vcc_walk(struct clip_seq_state *state,
0807                    struct atmarp_entry *e, loff_t * pos)
0808 {
0809     struct clip_vcc *vcc = state->vcc;
0810 
0811     vcc = clip_seq_next_vcc(e, vcc);
0812     if (vcc && pos != NULL) {
0813         while (*pos) {
0814             vcc = clip_seq_next_vcc(e, vcc);
0815             if (!vcc)
0816                 break;
0817             --(*pos);
0818         }
0819     }
0820     state->vcc = vcc;
0821 
0822     return vcc;
0823 }
0824 
0825 static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
0826                    struct neighbour *n, loff_t * pos)
0827 {
0828     struct clip_seq_state *state = (struct clip_seq_state *)_state;
0829 
0830     if (n->dev->type != ARPHRD_ATM)
0831         return NULL;
0832 
0833     return clip_seq_vcc_walk(state, neighbour_priv(n), pos);
0834 }
0835 
0836 static void *clip_seq_start(struct seq_file *seq, loff_t * pos)
0837 {
0838     struct clip_seq_state *state = seq->private;
0839     state->ns.neigh_sub_iter = clip_seq_sub_iter;
0840     return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_NEIGH_ONLY);
0841 }
0842 
0843 static int clip_seq_show(struct seq_file *seq, void *v)
0844 {
0845     static char atm_arp_banner[] =
0846         "IPitf TypeEncp Idle IP address      ATM address\n";
0847 
0848     if (v == SEQ_START_TOKEN) {
0849         seq_puts(seq, atm_arp_banner);
0850     } else {
0851         struct clip_seq_state *state = seq->private;
0852         struct clip_vcc *vcc = state->vcc;
0853         struct neighbour *n = v;
0854 
0855         atmarp_info(seq, n, neighbour_priv(n), vcc);
0856     }
0857     return 0;
0858 }
0859 
0860 static const struct seq_operations arp_seq_ops = {
0861     .start  = clip_seq_start,
0862     .next   = neigh_seq_next,
0863     .stop   = neigh_seq_stop,
0864     .show   = clip_seq_show,
0865 };
0866 #endif
0867 
0868 static void atm_clip_exit_noproc(void);
0869 
0870 static int __init atm_clip_init(void)
0871 {
0872     register_atm_ioctl(&clip_ioctl_ops);
0873     register_netdevice_notifier(&clip_dev_notifier);
0874     register_inetaddr_notifier(&clip_inet_notifier);
0875 
0876     timer_setup(&idle_timer, idle_timer_check, 0);
0877 
0878 #ifdef CONFIG_PROC_FS
0879     {
0880         struct proc_dir_entry *p;
0881 
0882         p = proc_create_net("arp", 0444, atm_proc_root, &arp_seq_ops,
0883                 sizeof(struct clip_seq_state));
0884         if (!p) {
0885             pr_err("Unable to initialize /proc/net/atm/arp\n");
0886             atm_clip_exit_noproc();
0887             return -ENOMEM;
0888         }
0889     }
0890 #endif
0891 
0892     return 0;
0893 }
0894 
0895 static void atm_clip_exit_noproc(void)
0896 {
0897     struct net_device *dev, *next;
0898 
0899     unregister_inetaddr_notifier(&clip_inet_notifier);
0900     unregister_netdevice_notifier(&clip_dev_notifier);
0901 
0902     deregister_atm_ioctl(&clip_ioctl_ops);
0903 
0904     /* First, stop the idle timer, so it stops banging
0905      * on the table.
0906      */
0907     del_timer_sync(&idle_timer);
0908 
0909     dev = clip_devs;
0910     while (dev) {
0911         next = PRIV(dev)->next;
0912         unregister_netdev(dev);
0913         free_netdev(dev);
0914         dev = next;
0915     }
0916 }
0917 
0918 static void __exit atm_clip_exit(void)
0919 {
0920     remove_proc_entry("arp", atm_proc_root);
0921 
0922     atm_clip_exit_noproc();
0923 }
0924 
0925 module_init(atm_clip_init);
0926 module_exit(atm_clip_exit);
0927 MODULE_AUTHOR("Werner Almesberger");
0928 MODULE_DESCRIPTION("Classical/IP over ATM interface");
0929 MODULE_LICENSE("GPL");