clip.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /* net/atm/clip.c - RFC1577 Classical IP over ATM */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  4. #include <linux/string.h>
  5. #include <linux/errno.h>
  6. #include <linux/kernel.h> /* for UINT_MAX */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/wait.h>
  12. #include <linux/timer.h>
  13. #include <linux/if_arp.h> /* for some manifest constants */
  14. #include <linux/notifier.h>
  15. #include <linux/atm.h>
  16. #include <linux/atmdev.h>
  17. #include <linux/atmclip.h>
  18. #include <linux/atmarp.h>
  19. #include <linux/capability.h>
  20. #include <linux/ip.h> /* for net/route.h */
  21. #include <linux/in.h> /* for struct sockaddr_in */
  22. #include <linux/if.h> /* for IFF_UP */
  23. #include <linux/inetdevice.h>
  24. #include <linux/bitops.h>
  25. #include <linux/poison.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 <linux/slab.h>
  31. #include <net/route.h> /* for struct rtable and routing */
  32. #include <net/icmp.h> /* icmp_send */
  33. #include <net/arp.h>
  34. #include <linux/param.h> /* for HZ */
  35. #include <linux/uaccess.h>
  36. #include <asm/byteorder.h> /* for htons etc. */
  37. #include <asm/system.h> /* save/restore_flags */
  38. #include <linux/atomic.h>
  39. #include "common.h"
  40. #include "resources.h"
  41. #include <net/atmclip.h>
  42. static struct net_device *clip_devs;
  43. static struct atm_vcc *atmarpd;
  44. static struct timer_list idle_timer;
  45. static const struct neigh_ops clip_neigh_ops;
  46. static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip)
  47. {
  48. struct sock *sk;
  49. struct atmarp_ctrl *ctrl;
  50. struct sk_buff *skb;
  51. pr_debug("(%d)\n", type);
  52. if (!atmarpd)
  53. return -EUNATCH;
  54. skb = alloc_skb(sizeof(struct atmarp_ctrl), GFP_ATOMIC);
  55. if (!skb)
  56. return -ENOMEM;
  57. ctrl = (struct atmarp_ctrl *)skb_put(skb, sizeof(struct atmarp_ctrl));
  58. ctrl->type = type;
  59. ctrl->itf_num = itf;
  60. ctrl->ip = ip;
  61. atm_force_charge(atmarpd, skb->truesize);
  62. sk = sk_atm(atmarpd);
  63. skb_queue_tail(&sk->sk_receive_queue, skb);
  64. sk->sk_data_ready(sk, skb->len);
  65. return 0;
  66. }
  67. static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry)
  68. {
  69. pr_debug("%p to entry %p (neigh %p)\n", clip_vcc, entry, entry->neigh);
  70. clip_vcc->entry = entry;
  71. clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
  72. clip_vcc->next = entry->vccs;
  73. entry->vccs = clip_vcc;
  74. entry->neigh->used = jiffies;
  75. }
  76. static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
  77. {
  78. struct atmarp_entry *entry = clip_vcc->entry;
  79. struct clip_vcc **walk;
  80. if (!entry) {
  81. pr_crit("!clip_vcc->entry (clip_vcc %p)\n", clip_vcc);
  82. return;
  83. }
  84. netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */
  85. entry->neigh->used = jiffies;
  86. for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
  87. if (*walk == clip_vcc) {
  88. int error;
  89. *walk = clip_vcc->next; /* atomic */
  90. clip_vcc->entry = NULL;
  91. if (clip_vcc->xoff)
  92. netif_wake_queue(entry->neigh->dev);
  93. if (entry->vccs)
  94. goto out;
  95. entry->expires = jiffies - 1;
  96. /* force resolution or expiration */
  97. error = neigh_update(entry->neigh, NULL, NUD_NONE,
  98. NEIGH_UPDATE_F_ADMIN);
  99. if (error)
  100. pr_crit("neigh_update failed with %d\n", error);
  101. goto out;
  102. }
  103. pr_crit("ATMARP: failed (entry %p, vcc 0x%p)\n", entry, clip_vcc);
  104. out:
  105. netif_tx_unlock_bh(entry->neigh->dev);
  106. }
  107. /* The neighbour entry n->lock is held. */
  108. static int neigh_check_cb(struct neighbour *n)
  109. {
  110. struct atmarp_entry *entry = neighbour_priv(n);
  111. struct clip_vcc *cv;
  112. if (n->ops != &clip_neigh_ops)
  113. return 0;
  114. for (cv = entry->vccs; cv; cv = cv->next) {
  115. unsigned long exp = cv->last_use + cv->idle_timeout;
  116. if (cv->idle_timeout && time_after(jiffies, exp)) {
  117. pr_debug("releasing vcc %p->%p of entry %p\n",
  118. cv, cv->vcc, entry);
  119. vcc_release_async(cv->vcc, -ETIMEDOUT);
  120. }
  121. }
  122. if (entry->vccs || time_before(jiffies, entry->expires))
  123. return 0;
  124. if (atomic_read(&n->refcnt) > 1) {
  125. struct sk_buff *skb;
  126. pr_debug("destruction postponed with ref %d\n",
  127. atomic_read(&n->refcnt));
  128. while ((skb = skb_dequeue(&n->arp_queue)) != NULL)
  129. dev_kfree_skb(skb);
  130. return 0;
  131. }
  132. pr_debug("expired neigh %p\n", n);
  133. return 1;
  134. }
  135. static void idle_timer_check(unsigned long dummy)
  136. {
  137. write_lock(&arp_tbl.lock);
  138. __neigh_for_each_release(&arp_tbl, neigh_check_cb);
  139. mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
  140. write_unlock(&arp_tbl.lock);
  141. }
  142. static int clip_arp_rcv(struct sk_buff *skb)
  143. {
  144. struct atm_vcc *vcc;
  145. pr_debug("\n");
  146. vcc = ATM_SKB(skb)->vcc;
  147. if (!vcc || !atm_charge(vcc, skb->truesize)) {
  148. dev_kfree_skb_any(skb);
  149. return 0;
  150. }
  151. pr_debug("pushing to %p\n", vcc);
  152. pr_debug("using %p\n", CLIP_VCC(vcc)->old_push);
  153. CLIP_VCC(vcc)->old_push(vcc, skb);
  154. return 0;
  155. }
  156. static const unsigned char llc_oui[] = {
  157. 0xaa, /* DSAP: non-ISO */
  158. 0xaa, /* SSAP: non-ISO */
  159. 0x03, /* Ctrl: Unnumbered Information Command PDU */
  160. 0x00, /* OUI: EtherType */
  161. 0x00,
  162. 0x00
  163. };
  164. static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb)
  165. {
  166. struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
  167. pr_debug("\n");
  168. if (!clip_devs) {
  169. atm_return(vcc, skb->truesize);
  170. kfree_skb(skb);
  171. return;
  172. }
  173. if (!skb) {
  174. pr_debug("removing VCC %p\n", clip_vcc);
  175. if (clip_vcc->entry)
  176. unlink_clip_vcc(clip_vcc);
  177. clip_vcc->old_push(vcc, NULL); /* pass on the bad news */
  178. kfree(clip_vcc);
  179. return;
  180. }
  181. atm_return(vcc, skb->truesize);
  182. skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
  183. /* clip_vcc->entry == NULL if we don't have an IP address yet */
  184. if (!skb->dev) {
  185. dev_kfree_skb_any(skb);
  186. return;
  187. }
  188. ATM_SKB(skb)->vcc = vcc;
  189. skb_reset_mac_header(skb);
  190. if (!clip_vcc->encap ||
  191. skb->len < RFC1483LLC_LEN ||
  192. memcmp(skb->data, llc_oui, sizeof(llc_oui)))
  193. skb->protocol = htons(ETH_P_IP);
  194. else {
  195. skb->protocol = ((__be16 *)skb->data)[3];
  196. skb_pull(skb, RFC1483LLC_LEN);
  197. if (skb->protocol == htons(ETH_P_ARP)) {
  198. skb->dev->stats.rx_packets++;
  199. skb->dev->stats.rx_bytes += skb->len;
  200. clip_arp_rcv(skb);
  201. return;
  202. }
  203. }
  204. clip_vcc->last_use = jiffies;
  205. skb->dev->stats.rx_packets++;
  206. skb->dev->stats.rx_bytes += skb->len;
  207. memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
  208. netif_rx(skb);
  209. }
  210. /*
  211. * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
  212. * clip_pop is atomic with respect to the critical section in clip_start_xmit.
  213. */
  214. static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  215. {
  216. struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
  217. struct net_device *dev = skb->dev;
  218. int old;
  219. unsigned long flags;
  220. pr_debug("(vcc %p)\n", vcc);
  221. clip_vcc->old_pop(vcc, skb);
  222. /* skb->dev == NULL in outbound ARP packets */
  223. if (!dev)
  224. return;
  225. spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags);
  226. if (atm_may_send(vcc, 0)) {
  227. old = xchg(&clip_vcc->xoff, 0);
  228. if (old)
  229. netif_wake_queue(dev);
  230. }
  231. spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags);
  232. }
  233. static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb)
  234. {
  235. __be32 *ip = (__be32 *) neigh->primary_key;
  236. pr_debug("(neigh %p, skb %p)\n", neigh, skb);
  237. to_atmarpd(act_need, PRIV(neigh->dev)->number, *ip);
  238. }
  239. static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb)
  240. {
  241. #ifndef CONFIG_ATM_CLIP_NO_ICMP
  242. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
  243. #endif
  244. kfree_skb(skb);
  245. }
  246. static const struct neigh_ops clip_neigh_ops = {
  247. .family = AF_INET,
  248. .solicit = clip_neigh_solicit,
  249. .error_report = clip_neigh_error,
  250. .output = neigh_direct_output,
  251. .connected_output = neigh_direct_output,
  252. };
  253. static int clip_constructor(struct neighbour *neigh)
  254. {
  255. struct atmarp_entry *entry = neighbour_priv(neigh);
  256. if (neigh->tbl->family != AF_INET)
  257. return -EINVAL;
  258. if (neigh->type != RTN_UNICAST)
  259. return -EINVAL;
  260. neigh->nud_state = NUD_NONE;
  261. neigh->ops = &clip_neigh_ops;
  262. neigh->output = neigh->ops->output;
  263. entry->neigh = neigh;
  264. entry->vccs = NULL;
  265. entry->expires = jiffies - 1;
  266. return 0;
  267. }
  268. /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
  269. /*
  270. * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
  271. * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
  272. * don't increment the usage count. This is used to create entries in
  273. * clip_setentry.
  274. */
  275. static int clip_encap(struct atm_vcc *vcc, int mode)
  276. {
  277. CLIP_VCC(vcc)->encap = mode;
  278. return 0;
  279. }
  280. static netdev_tx_t clip_start_xmit(struct sk_buff *skb,
  281. struct net_device *dev)
  282. {
  283. struct clip_priv *clip_priv = PRIV(dev);
  284. struct dst_entry *dst = skb_dst(skb);
  285. struct atmarp_entry *entry;
  286. struct neighbour *n;
  287. struct atm_vcc *vcc;
  288. int old;
  289. unsigned long flags;
  290. pr_debug("(skb %p)\n", skb);
  291. if (!dst) {
  292. pr_err("skb_dst(skb) == NULL\n");
  293. dev_kfree_skb(skb);
  294. dev->stats.tx_dropped++;
  295. return NETDEV_TX_OK;
  296. }
  297. n = dst_get_neighbour_noref(dst);
  298. if (!n) {
  299. pr_err("NO NEIGHBOUR !\n");
  300. dev_kfree_skb(skb);
  301. dev->stats.tx_dropped++;
  302. return NETDEV_TX_OK;
  303. }
  304. entry = neighbour_priv(n);
  305. if (!entry->vccs) {
  306. if (time_after(jiffies, entry->expires)) {
  307. /* should be resolved */
  308. entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ;
  309. to_atmarpd(act_need, PRIV(dev)->number, *((__be32 *)n->primary_key));
  310. }
  311. if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
  312. skb_queue_tail(&entry->neigh->arp_queue, skb);
  313. else {
  314. dev_kfree_skb(skb);
  315. dev->stats.tx_dropped++;
  316. }
  317. return NETDEV_TX_OK;
  318. }
  319. pr_debug("neigh %p, vccs %p\n", entry, entry->vccs);
  320. ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
  321. pr_debug("using neighbour %p, vcc %p\n", n, vcc);
  322. if (entry->vccs->encap) {
  323. void *here;
  324. here = skb_push(skb, RFC1483LLC_LEN);
  325. memcpy(here, llc_oui, sizeof(llc_oui));
  326. ((__be16 *) here)[3] = skb->protocol;
  327. }
  328. atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
  329. ATM_SKB(skb)->atm_options = vcc->atm_options;
  330. entry->vccs->last_use = jiffies;
  331. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev);
  332. old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */
  333. if (old) {
  334. pr_warning("XOFF->XOFF transition\n");
  335. return NETDEV_TX_OK;
  336. }
  337. dev->stats.tx_packets++;
  338. dev->stats.tx_bytes += skb->len;
  339. vcc->send(vcc, skb);
  340. if (atm_may_send(vcc, 0)) {
  341. entry->vccs->xoff = 0;
  342. return NETDEV_TX_OK;
  343. }
  344. spin_lock_irqsave(&clip_priv->xoff_lock, flags);
  345. netif_stop_queue(dev); /* XOFF -> throttle immediately */
  346. barrier();
  347. if (!entry->vccs->xoff)
  348. netif_start_queue(dev);
  349. /* Oh, we just raced with clip_pop. netif_start_queue should be
  350. good enough, because nothing should really be asleep because
  351. of the brief netif_stop_queue. If this isn't true or if it
  352. changes, use netif_wake_queue instead. */
  353. spin_unlock_irqrestore(&clip_priv->xoff_lock, flags);
  354. return NETDEV_TX_OK;
  355. }
  356. static int clip_mkip(struct atm_vcc *vcc, int timeout)
  357. {
  358. struct clip_vcc *clip_vcc;
  359. if (!vcc->push)
  360. return -EBADFD;
  361. clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL);
  362. if (!clip_vcc)
  363. return -ENOMEM;
  364. pr_debug("%p vcc %p\n", clip_vcc, vcc);
  365. clip_vcc->vcc = vcc;
  366. vcc->user_back = clip_vcc;
  367. set_bit(ATM_VF_IS_CLIP, &vcc->flags);
  368. clip_vcc->entry = NULL;
  369. clip_vcc->xoff = 0;
  370. clip_vcc->encap = 1;
  371. clip_vcc->last_use = jiffies;
  372. clip_vcc->idle_timeout = timeout * HZ;
  373. clip_vcc->old_push = vcc->push;
  374. clip_vcc->old_pop = vcc->pop;
  375. vcc->push = clip_push;
  376. vcc->pop = clip_pop;
  377. /* re-process everything received between connection setup and MKIP */
  378. vcc_process_recv_queue(vcc);
  379. return 0;
  380. }
  381. static int clip_setentry(struct atm_vcc *vcc, __be32 ip)
  382. {
  383. struct neighbour *neigh;
  384. struct atmarp_entry *entry;
  385. int error;
  386. struct clip_vcc *clip_vcc;
  387. struct rtable *rt;
  388. if (vcc->push != clip_push) {
  389. pr_warning("non-CLIP VCC\n");
  390. return -EBADF;
  391. }
  392. clip_vcc = CLIP_VCC(vcc);
  393. if (!ip) {
  394. if (!clip_vcc->entry) {
  395. pr_err("hiding hidden ATMARP entry\n");
  396. return 0;
  397. }
  398. pr_debug("remove\n");
  399. unlink_clip_vcc(clip_vcc);
  400. return 0;
  401. }
  402. rt = ip_route_output(&init_net, ip, 0, 1, 0);
  403. if (IS_ERR(rt))
  404. return PTR_ERR(rt);
  405. neigh = __neigh_lookup(&arp_tbl, &ip, rt->dst.dev, 1);
  406. ip_rt_put(rt);
  407. if (!neigh)
  408. return -ENOMEM;
  409. entry = neighbour_priv(neigh);
  410. if (entry != clip_vcc->entry) {
  411. if (!clip_vcc->entry)
  412. pr_debug("add\n");
  413. else {
  414. pr_debug("update\n");
  415. unlink_clip_vcc(clip_vcc);
  416. }
  417. link_vcc(clip_vcc, entry);
  418. }
  419. error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
  420. NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN);
  421. neigh_release(neigh);
  422. return error;
  423. }
  424. static const struct net_device_ops clip_netdev_ops = {
  425. .ndo_start_xmit = clip_start_xmit,
  426. .ndo_neigh_construct = clip_constructor,
  427. };
  428. static void clip_setup(struct net_device *dev)
  429. {
  430. dev->netdev_ops = &clip_netdev_ops;
  431. dev->type = ARPHRD_ATM;
  432. dev->neigh_priv_len = sizeof(struct atmarp_entry);
  433. dev->hard_header_len = RFC1483LLC_LEN;
  434. dev->mtu = RFC1626_MTU;
  435. dev->tx_queue_len = 100; /* "normal" queue (packets) */
  436. /* When using a "real" qdisc, the qdisc determines the queue */
  437. /* length. tx_queue_len is only used for the default case, */
  438. /* without any more elaborate queuing. 100 is a reasonable */
  439. /* compromise between decent burst-tolerance and protection */
  440. /* against memory hogs. */
  441. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  442. }
  443. static int clip_create(int number)
  444. {
  445. struct net_device *dev;
  446. struct clip_priv *clip_priv;
  447. int error;
  448. if (number != -1) {
  449. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  450. if (PRIV(dev)->number == number)
  451. return -EEXIST;
  452. } else {
  453. number = 0;
  454. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  455. if (PRIV(dev)->number >= number)
  456. number = PRIV(dev)->number + 1;
  457. }
  458. dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
  459. if (!dev)
  460. return -ENOMEM;
  461. clip_priv = PRIV(dev);
  462. sprintf(dev->name, "atm%d", number);
  463. spin_lock_init(&clip_priv->xoff_lock);
  464. clip_priv->number = number;
  465. error = register_netdev(dev);
  466. if (error) {
  467. free_netdev(dev);
  468. return error;
  469. }
  470. clip_priv->next = clip_devs;
  471. clip_devs = dev;
  472. pr_debug("registered (net:%s)\n", dev->name);
  473. return number;
  474. }
  475. static int clip_device_event(struct notifier_block *this, unsigned long event,
  476. void *arg)
  477. {
  478. struct net_device *dev = arg;
  479. if (!net_eq(dev_net(dev), &init_net))
  480. return NOTIFY_DONE;
  481. if (event == NETDEV_UNREGISTER)
  482. return NOTIFY_DONE;
  483. /* ignore non-CLIP devices */
  484. if (dev->type != ARPHRD_ATM || dev->netdev_ops != &clip_netdev_ops)
  485. return NOTIFY_DONE;
  486. switch (event) {
  487. case NETDEV_UP:
  488. pr_debug("NETDEV_UP\n");
  489. to_atmarpd(act_up, PRIV(dev)->number, 0);
  490. break;
  491. case NETDEV_GOING_DOWN:
  492. pr_debug("NETDEV_DOWN\n");
  493. to_atmarpd(act_down, PRIV(dev)->number, 0);
  494. break;
  495. case NETDEV_CHANGE:
  496. case NETDEV_CHANGEMTU:
  497. pr_debug("NETDEV_CHANGE*\n");
  498. to_atmarpd(act_change, PRIV(dev)->number, 0);
  499. break;
  500. }
  501. return NOTIFY_DONE;
  502. }
  503. static int clip_inet_event(struct notifier_block *this, unsigned long event,
  504. void *ifa)
  505. {
  506. struct in_device *in_dev;
  507. in_dev = ((struct in_ifaddr *)ifa)->ifa_dev;
  508. /*
  509. * Transitions are of the down-change-up type, so it's sufficient to
  510. * handle the change on up.
  511. */
  512. if (event != NETDEV_UP)
  513. return NOTIFY_DONE;
  514. return clip_device_event(this, NETDEV_CHANGE, in_dev->dev);
  515. }
  516. static struct notifier_block clip_dev_notifier = {
  517. .notifier_call = clip_device_event,
  518. };
  519. static struct notifier_block clip_inet_notifier = {
  520. .notifier_call = clip_inet_event,
  521. };
  522. static void atmarpd_close(struct atm_vcc *vcc)
  523. {
  524. pr_debug("\n");
  525. rtnl_lock();
  526. atmarpd = NULL;
  527. skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
  528. rtnl_unlock();
  529. pr_debug("(done)\n");
  530. module_put(THIS_MODULE);
  531. }
  532. static struct atmdev_ops atmarpd_dev_ops = {
  533. .close = atmarpd_close
  534. };
  535. static struct atm_dev atmarpd_dev = {
  536. .ops = &atmarpd_dev_ops,
  537. .type = "arpd",
  538. .number = 999,
  539. .lock = __SPIN_LOCK_UNLOCKED(atmarpd_dev.lock)
  540. };
  541. static int atm_init_atmarp(struct atm_vcc *vcc)
  542. {
  543. rtnl_lock();
  544. if (atmarpd) {
  545. rtnl_unlock();
  546. return -EADDRINUSE;
  547. }
  548. mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
  549. atmarpd = vcc;
  550. set_bit(ATM_VF_META, &vcc->flags);
  551. set_bit(ATM_VF_READY, &vcc->flags);
  552. /* allow replies and avoid getting closed if signaling dies */
  553. vcc->dev = &atmarpd_dev;
  554. vcc_insert_socket(sk_atm(vcc));
  555. vcc->push = NULL;
  556. vcc->pop = NULL; /* crash */
  557. vcc->push_oam = NULL; /* crash */
  558. rtnl_unlock();
  559. return 0;
  560. }
  561. static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  562. {
  563. struct atm_vcc *vcc = ATM_SD(sock);
  564. int err = 0;
  565. switch (cmd) {
  566. case SIOCMKCLIP:
  567. case ATMARPD_CTRL:
  568. case ATMARP_MKIP:
  569. case ATMARP_SETENTRY:
  570. case ATMARP_ENCAP:
  571. if (!capable(CAP_NET_ADMIN))
  572. return -EPERM;
  573. break;
  574. default:
  575. return -ENOIOCTLCMD;
  576. }
  577. switch (cmd) {
  578. case SIOCMKCLIP:
  579. err = clip_create(arg);
  580. break;
  581. case ATMARPD_CTRL:
  582. err = atm_init_atmarp(vcc);
  583. if (!err) {
  584. sock->state = SS_CONNECTED;
  585. __module_get(THIS_MODULE);
  586. }
  587. break;
  588. case ATMARP_MKIP:
  589. err = clip_mkip(vcc, arg);
  590. break;
  591. case ATMARP_SETENTRY:
  592. err = clip_setentry(vcc, (__force __be32)arg);
  593. break;
  594. case ATMARP_ENCAP:
  595. err = clip_encap(vcc, arg);
  596. break;
  597. }
  598. return err;
  599. }
  600. static struct atm_ioctl clip_ioctl_ops = {
  601. .owner = THIS_MODULE,
  602. .ioctl = clip_ioctl,
  603. };
  604. #ifdef CONFIG_PROC_FS
  605. static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
  606. {
  607. static int code[] = { 1, 2, 10, 6, 1, 0 };
  608. static int e164[] = { 1, 8, 4, 6, 1, 0 };
  609. if (*addr->sas_addr.pub) {
  610. seq_printf(seq, "%s", addr->sas_addr.pub);
  611. if (*addr->sas_addr.prv)
  612. seq_putc(seq, '+');
  613. } else if (!*addr->sas_addr.prv) {
  614. seq_printf(seq, "%s", "(none)");
  615. return;
  616. }
  617. if (*addr->sas_addr.prv) {
  618. unsigned char *prv = addr->sas_addr.prv;
  619. int *fields;
  620. int i, j;
  621. fields = *prv == ATM_AFI_E164 ? e164 : code;
  622. for (i = 0; fields[i]; i++) {
  623. for (j = fields[i]; j; j--)
  624. seq_printf(seq, "%02X", *prv++);
  625. if (fields[i + 1])
  626. seq_putc(seq, '.');
  627. }
  628. }
  629. }
  630. /* This means the neighbour entry has no attached VCC objects. */
  631. #define SEQ_NO_VCC_TOKEN ((void *) 2)
  632. static void atmarp_info(struct seq_file *seq, struct neighbour *n,
  633. struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
  634. {
  635. struct net_device *dev = n->dev;
  636. unsigned long exp;
  637. char buf[17];
  638. int svc, llc, off;
  639. svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
  640. (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
  641. llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap);
  642. if (clip_vcc == SEQ_NO_VCC_TOKEN)
  643. exp = entry->neigh->used;
  644. else
  645. exp = clip_vcc->last_use;
  646. exp = (jiffies - exp) / HZ;
  647. seq_printf(seq, "%-6s%-4s%-4s%5ld ",
  648. dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp);
  649. off = scnprintf(buf, sizeof(buf) - 1, "%pI4", n->primary_key);
  650. while (off < 16)
  651. buf[off++] = ' ';
  652. buf[off] = '\0';
  653. seq_printf(seq, "%s", buf);
  654. if (clip_vcc == SEQ_NO_VCC_TOKEN) {
  655. if (time_before(jiffies, entry->expires))
  656. seq_printf(seq, "(resolving)\n");
  657. else
  658. seq_printf(seq, "(expired, ref %d)\n",
  659. atomic_read(&entry->neigh->refcnt));
  660. } else if (!svc) {
  661. seq_printf(seq, "%d.%d.%d\n",
  662. clip_vcc->vcc->dev->number,
  663. clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
  664. } else {
  665. svc_addr(seq, &clip_vcc->vcc->remote);
  666. seq_putc(seq, '\n');
  667. }
  668. }
  669. struct clip_seq_state {
  670. /* This member must be first. */
  671. struct neigh_seq_state ns;
  672. /* Local to clip specific iteration. */
  673. struct clip_vcc *vcc;
  674. };
  675. static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
  676. struct clip_vcc *curr)
  677. {
  678. if (!curr) {
  679. curr = e->vccs;
  680. if (!curr)
  681. return SEQ_NO_VCC_TOKEN;
  682. return curr;
  683. }
  684. if (curr == SEQ_NO_VCC_TOKEN)
  685. return NULL;
  686. curr = curr->next;
  687. return curr;
  688. }
  689. static void *clip_seq_vcc_walk(struct clip_seq_state *state,
  690. struct atmarp_entry *e, loff_t * pos)
  691. {
  692. struct clip_vcc *vcc = state->vcc;
  693. vcc = clip_seq_next_vcc(e, vcc);
  694. if (vcc && pos != NULL) {
  695. while (*pos) {
  696. vcc = clip_seq_next_vcc(e, vcc);
  697. if (!vcc)
  698. break;
  699. --(*pos);
  700. }
  701. }
  702. state->vcc = vcc;
  703. return vcc;
  704. }
  705. static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
  706. struct neighbour *n, loff_t * pos)
  707. {
  708. struct clip_seq_state *state = (struct clip_seq_state *)_state;
  709. if (n->dev->type != ARPHRD_ATM)
  710. return NULL;
  711. return clip_seq_vcc_walk(state, neighbour_priv(n), pos);
  712. }
  713. static void *clip_seq_start(struct seq_file *seq, loff_t * pos)
  714. {
  715. struct clip_seq_state *state = seq->private;
  716. state->ns.neigh_sub_iter = clip_seq_sub_iter;
  717. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_NEIGH_ONLY);
  718. }
  719. static int clip_seq_show(struct seq_file *seq, void *v)
  720. {
  721. static char atm_arp_banner[] =
  722. "IPitf TypeEncp Idle IP address ATM address\n";
  723. if (v == SEQ_START_TOKEN) {
  724. seq_puts(seq, atm_arp_banner);
  725. } else {
  726. struct clip_seq_state *state = seq->private;
  727. struct clip_vcc *vcc = state->vcc;
  728. struct neighbour *n = v;
  729. atmarp_info(seq, n, neighbour_priv(n), vcc);
  730. }
  731. return 0;
  732. }
  733. static const struct seq_operations arp_seq_ops = {
  734. .start = clip_seq_start,
  735. .next = neigh_seq_next,
  736. .stop = neigh_seq_stop,
  737. .show = clip_seq_show,
  738. };
  739. static int arp_seq_open(struct inode *inode, struct file *file)
  740. {
  741. return seq_open_net(inode, file, &arp_seq_ops,
  742. sizeof(struct clip_seq_state));
  743. }
  744. static const struct file_operations arp_seq_fops = {
  745. .open = arp_seq_open,
  746. .read = seq_read,
  747. .llseek = seq_lseek,
  748. .release = seq_release_net,
  749. .owner = THIS_MODULE
  750. };
  751. #endif
  752. static void atm_clip_exit_noproc(void);
  753. static int __init atm_clip_init(void)
  754. {
  755. register_atm_ioctl(&clip_ioctl_ops);
  756. register_netdevice_notifier(&clip_dev_notifier);
  757. register_inetaddr_notifier(&clip_inet_notifier);
  758. setup_timer(&idle_timer, idle_timer_check, 0);
  759. #ifdef CONFIG_PROC_FS
  760. {
  761. struct proc_dir_entry *p;
  762. p = proc_create("arp", S_IRUGO, atm_proc_root, &arp_seq_fops);
  763. if (!p) {
  764. pr_err("Unable to initialize /proc/net/atm/arp\n");
  765. atm_clip_exit_noproc();
  766. return -ENOMEM;
  767. }
  768. }
  769. #endif
  770. return 0;
  771. }
  772. static void atm_clip_exit_noproc(void)
  773. {
  774. struct net_device *dev, *next;
  775. unregister_inetaddr_notifier(&clip_inet_notifier);
  776. unregister_netdevice_notifier(&clip_dev_notifier);
  777. deregister_atm_ioctl(&clip_ioctl_ops);
  778. /* First, stop the idle timer, so it stops banging
  779. * on the table.
  780. */
  781. del_timer_sync(&idle_timer);
  782. dev = clip_devs;
  783. while (dev) {
  784. next = PRIV(dev)->next;
  785. unregister_netdev(dev);
  786. free_netdev(dev);
  787. dev = next;
  788. }
  789. }
  790. static void __exit atm_clip_exit(void)
  791. {
  792. remove_proc_entry("arp", atm_proc_root);
  793. atm_clip_exit_noproc();
  794. }
  795. module_init(atm_clip_init);
  796. module_exit(atm_clip_exit);
  797. MODULE_AUTHOR("Werner Almesberger");
  798. MODULE_DESCRIPTION("Classical/IP over ATM interface");
  799. MODULE_LICENSE("GPL");