clip.c 25 KB

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