clip.c 23 KB

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