clip.c 25 KB

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