clip.c 24 KB

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