clip.c 22 KB

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