net_kern.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  3. * James Leu (jleu@mindspring.net).
  4. * Copyright (C) 2001 by various other people who didn't put their name here.
  5. * Licensed under the GPL.
  6. */
  7. #include "linux/config.h"
  8. #include "linux/kernel.h"
  9. #include "linux/netdevice.h"
  10. #include "linux/rtnetlink.h"
  11. #include "linux/skbuff.h"
  12. #include "linux/socket.h"
  13. #include "linux/spinlock.h"
  14. #include "linux/module.h"
  15. #include "linux/init.h"
  16. #include "linux/etherdevice.h"
  17. #include "linux/list.h"
  18. #include "linux/inetdevice.h"
  19. #include "linux/ctype.h"
  20. #include "linux/bootmem.h"
  21. #include "linux/ethtool.h"
  22. #include "asm/uaccess.h"
  23. #include "user_util.h"
  24. #include "kern_util.h"
  25. #include "net_kern.h"
  26. #include "net_user.h"
  27. #include "mconsole_kern.h"
  28. #include "init.h"
  29. #include "irq_user.h"
  30. #include "irq_kern.h"
  31. #define DRIVER_NAME "uml-netdev"
  32. static DEFINE_SPINLOCK(opened_lock);
  33. LIST_HEAD(opened);
  34. static int uml_net_rx(struct net_device *dev)
  35. {
  36. struct uml_net_private *lp = dev->priv;
  37. int pkt_len;
  38. struct sk_buff *skb;
  39. /* If we can't allocate memory, try again next round. */
  40. skb = dev_alloc_skb(dev->mtu);
  41. if (skb == NULL) {
  42. lp->stats.rx_dropped++;
  43. return 0;
  44. }
  45. skb->dev = dev;
  46. skb_put(skb, dev->mtu);
  47. skb->mac.raw = skb->data;
  48. pkt_len = (*lp->read)(lp->fd, &skb, lp);
  49. if (pkt_len > 0) {
  50. skb_trim(skb, pkt_len);
  51. skb->protocol = (*lp->protocol)(skb);
  52. netif_rx(skb);
  53. lp->stats.rx_bytes += skb->len;
  54. lp->stats.rx_packets++;
  55. return pkt_len;
  56. }
  57. kfree_skb(skb);
  58. return pkt_len;
  59. }
  60. irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  61. {
  62. struct net_device *dev = dev_id;
  63. struct uml_net_private *lp = dev->priv;
  64. int err;
  65. if(!netif_running(dev))
  66. return(IRQ_NONE);
  67. spin_lock(&lp->lock);
  68. while((err = uml_net_rx(dev)) > 0) ;
  69. if(err < 0) {
  70. printk(KERN_ERR
  71. "Device '%s' read returned %d, shutting it down\n",
  72. dev->name, err);
  73. dev_close(dev);
  74. goto out;
  75. }
  76. reactivate_fd(lp->fd, UM_ETH_IRQ);
  77. out:
  78. spin_unlock(&lp->lock);
  79. return(IRQ_HANDLED);
  80. }
  81. static int uml_net_open(struct net_device *dev)
  82. {
  83. struct uml_net_private *lp = dev->priv;
  84. char addr[sizeof("255.255.255.255\0")];
  85. int err;
  86. spin_lock(&lp->lock);
  87. if(lp->fd >= 0){
  88. err = -ENXIO;
  89. goto out;
  90. }
  91. if(!lp->have_mac){
  92. dev_ip_addr(dev, addr, &lp->mac[2]);
  93. set_ether_mac(dev, lp->mac);
  94. }
  95. lp->fd = (*lp->open)(&lp->user);
  96. if(lp->fd < 0){
  97. err = lp->fd;
  98. goto out;
  99. }
  100. err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
  101. SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
  102. if(err != 0){
  103. printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
  104. if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
  105. lp->fd = -1;
  106. err = -ENETUNREACH;
  107. }
  108. lp->tl.data = (unsigned long) &lp->user;
  109. netif_start_queue(dev);
  110. /* clear buffer - it can happen that the host side of the interface
  111. * is full when we get here. In this case, new data is never queued,
  112. * SIGIOs never arrive, and the net never works.
  113. */
  114. while((err = uml_net_rx(dev)) > 0) ;
  115. out:
  116. spin_unlock(&lp->lock);
  117. return(err);
  118. }
  119. static int uml_net_close(struct net_device *dev)
  120. {
  121. struct uml_net_private *lp = dev->priv;
  122. netif_stop_queue(dev);
  123. spin_lock(&lp->lock);
  124. free_irq(dev->irq, dev);
  125. if(lp->close != NULL)
  126. (*lp->close)(lp->fd, &lp->user);
  127. lp->fd = -1;
  128. spin_unlock(&lp->lock);
  129. return 0;
  130. }
  131. static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  132. {
  133. struct uml_net_private *lp = dev->priv;
  134. unsigned long flags;
  135. int len;
  136. netif_stop_queue(dev);
  137. spin_lock_irqsave(&lp->lock, flags);
  138. len = (*lp->write)(lp->fd, &skb, lp);
  139. if(len == skb->len) {
  140. lp->stats.tx_packets++;
  141. lp->stats.tx_bytes += skb->len;
  142. dev->trans_start = jiffies;
  143. netif_start_queue(dev);
  144. /* this is normally done in the interrupt when tx finishes */
  145. netif_wake_queue(dev);
  146. }
  147. else if(len == 0){
  148. netif_start_queue(dev);
  149. lp->stats.tx_dropped++;
  150. }
  151. else {
  152. netif_start_queue(dev);
  153. printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
  154. }
  155. spin_unlock_irqrestore(&lp->lock, flags);
  156. dev_kfree_skb(skb);
  157. return 0;
  158. }
  159. static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
  160. {
  161. struct uml_net_private *lp = dev->priv;
  162. return &lp->stats;
  163. }
  164. static void uml_net_set_multicast_list(struct net_device *dev)
  165. {
  166. if (dev->flags & IFF_PROMISC) return;
  167. else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
  168. else dev->flags &= ~IFF_ALLMULTI;
  169. }
  170. static void uml_net_tx_timeout(struct net_device *dev)
  171. {
  172. dev->trans_start = jiffies;
  173. netif_wake_queue(dev);
  174. }
  175. static int uml_net_set_mac(struct net_device *dev, void *addr)
  176. {
  177. struct uml_net_private *lp = dev->priv;
  178. struct sockaddr *hwaddr = addr;
  179. spin_lock(&lp->lock);
  180. memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
  181. spin_unlock(&lp->lock);
  182. return(0);
  183. }
  184. static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
  185. {
  186. struct uml_net_private *lp = dev->priv;
  187. int err = 0;
  188. spin_lock(&lp->lock);
  189. new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
  190. if(new_mtu < 0){
  191. err = new_mtu;
  192. goto out;
  193. }
  194. dev->mtu = new_mtu;
  195. out:
  196. spin_unlock(&lp->lock);
  197. return err;
  198. }
  199. static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  200. {
  201. static const struct ethtool_drvinfo info = {
  202. .cmd = ETHTOOL_GDRVINFO,
  203. .driver = DRIVER_NAME,
  204. .version = "42",
  205. };
  206. void *useraddr;
  207. u32 ethcmd;
  208. switch (cmd) {
  209. case SIOCETHTOOL:
  210. useraddr = ifr->ifr_data;
  211. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  212. return -EFAULT;
  213. switch (ethcmd) {
  214. case ETHTOOL_GDRVINFO:
  215. if (copy_to_user(useraddr, &info, sizeof(info)))
  216. return -EFAULT;
  217. return 0;
  218. default:
  219. return -EOPNOTSUPP;
  220. }
  221. default:
  222. return -EINVAL;
  223. }
  224. }
  225. void uml_net_user_timer_expire(unsigned long _conn)
  226. {
  227. #ifdef undef
  228. struct connection *conn = (struct connection *)_conn;
  229. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  230. do_connect(conn);
  231. #endif
  232. }
  233. static DEFINE_SPINLOCK(devices_lock);
  234. static struct list_head devices = LIST_HEAD_INIT(devices);
  235. static struct device_driver uml_net_driver = {
  236. .name = DRIVER_NAME,
  237. .bus = &platform_bus_type,
  238. };
  239. static int driver_registered;
  240. static int eth_configure(int n, void *init, char *mac,
  241. struct transport *transport)
  242. {
  243. struct uml_net *device;
  244. struct net_device *dev;
  245. struct uml_net_private *lp;
  246. int save, err, size;
  247. size = transport->private_size + sizeof(struct uml_net_private) +
  248. sizeof(((struct uml_net_private *) 0)->user);
  249. device = kmalloc(sizeof(*device), GFP_KERNEL);
  250. if (device == NULL) {
  251. printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
  252. return(1);
  253. }
  254. memset(device, 0, sizeof(*device));
  255. INIT_LIST_HEAD(&device->list);
  256. device->index = n;
  257. spin_lock(&devices_lock);
  258. list_add(&device->list, &devices);
  259. spin_unlock(&devices_lock);
  260. if (setup_etheraddr(mac, device->mac))
  261. device->have_mac = 1;
  262. printk(KERN_INFO "Netdevice %d ", n);
  263. if (device->have_mac)
  264. printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
  265. device->mac[0], device->mac[1],
  266. device->mac[2], device->mac[3],
  267. device->mac[4], device->mac[5]);
  268. printk(": ");
  269. dev = alloc_etherdev(size);
  270. if (dev == NULL) {
  271. printk(KERN_ERR "eth_configure: failed to allocate device\n");
  272. return 1;
  273. }
  274. /* sysfs register */
  275. if (!driver_registered) {
  276. driver_register(&uml_net_driver);
  277. driver_registered = 1;
  278. }
  279. device->pdev.id = n;
  280. device->pdev.name = DRIVER_NAME;
  281. platform_device_register(&device->pdev);
  282. SET_NETDEV_DEV(dev,&device->pdev.dev);
  283. /* If this name ends up conflicting with an existing registered
  284. * netdevice, that is OK, register_netdev{,ice}() will notice this
  285. * and fail.
  286. */
  287. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  288. device->dev = dev;
  289. (*transport->kern->init)(dev, init);
  290. dev->mtu = transport->user->max_packet;
  291. dev->open = uml_net_open;
  292. dev->hard_start_xmit = uml_net_start_xmit;
  293. dev->stop = uml_net_close;
  294. dev->get_stats = uml_net_get_stats;
  295. dev->set_multicast_list = uml_net_set_multicast_list;
  296. dev->tx_timeout = uml_net_tx_timeout;
  297. dev->set_mac_address = uml_net_set_mac;
  298. dev->change_mtu = uml_net_change_mtu;
  299. dev->do_ioctl = uml_net_ioctl;
  300. dev->watchdog_timeo = (HZ >> 1);
  301. dev->irq = UM_ETH_IRQ;
  302. rtnl_lock();
  303. err = register_netdevice(dev);
  304. rtnl_unlock();
  305. if (err) {
  306. device->dev = NULL;
  307. /* XXX: should we call ->remove() here? */
  308. free_netdev(dev);
  309. return 1;
  310. }
  311. lp = dev->priv;
  312. /* lp.user is the first four bytes of the transport data, which
  313. * has already been initialized. This structure assignment will
  314. * overwrite that, so we make sure that .user gets overwritten with
  315. * what it already has.
  316. */
  317. save = lp->user[0];
  318. *lp = ((struct uml_net_private)
  319. { .list = LIST_HEAD_INIT(lp->list),
  320. .dev = dev,
  321. .fd = -1,
  322. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  323. .have_mac = device->have_mac,
  324. .protocol = transport->kern->protocol,
  325. .open = transport->user->open,
  326. .close = transport->user->close,
  327. .remove = transport->user->remove,
  328. .read = transport->kern->read,
  329. .write = transport->kern->write,
  330. .add_address = transport->user->add_address,
  331. .delete_address = transport->user->delete_address,
  332. .set_mtu = transport->user->set_mtu,
  333. .user = { save } });
  334. init_timer(&lp->tl);
  335. spin_lock_init(&lp->lock);
  336. lp->tl.function = uml_net_user_timer_expire;
  337. if (lp->have_mac)
  338. memcpy(lp->mac, device->mac, sizeof(lp->mac));
  339. if (transport->user->init)
  340. (*transport->user->init)(&lp->user, dev);
  341. if (device->have_mac)
  342. set_ether_mac(dev, device->mac);
  343. spin_lock(&opened_lock);
  344. list_add(&lp->list, &opened);
  345. spin_unlock(&opened_lock);
  346. return(0);
  347. }
  348. static struct uml_net *find_device(int n)
  349. {
  350. struct uml_net *device;
  351. struct list_head *ele;
  352. spin_lock(&devices_lock);
  353. list_for_each(ele, &devices){
  354. device = list_entry(ele, struct uml_net, list);
  355. if(device->index == n)
  356. goto out;
  357. }
  358. device = NULL;
  359. out:
  360. spin_unlock(&devices_lock);
  361. return(device);
  362. }
  363. static int eth_parse(char *str, int *index_out, char **str_out)
  364. {
  365. char *end;
  366. int n;
  367. n = simple_strtoul(str, &end, 0);
  368. if(end == str){
  369. printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
  370. return(1);
  371. }
  372. if(n < 0){
  373. printk(KERN_ERR "eth_setup: device %d is negative\n", n);
  374. return(1);
  375. }
  376. str = end;
  377. if(*str != '='){
  378. printk(KERN_ERR
  379. "eth_setup: expected '=' after device number\n");
  380. return(1);
  381. }
  382. str++;
  383. if(find_device(n)){
  384. printk(KERN_ERR "eth_setup: Device %d already configured\n",
  385. n);
  386. return(1);
  387. }
  388. if(index_out) *index_out = n;
  389. *str_out = str;
  390. return(0);
  391. }
  392. struct eth_init {
  393. struct list_head list;
  394. char *init;
  395. int index;
  396. };
  397. /* Filled in at boot time. Will need locking if the transports become
  398. * modular.
  399. */
  400. struct list_head transports = LIST_HEAD_INIT(transports);
  401. /* Filled in during early boot */
  402. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  403. static int check_transport(struct transport *transport, char *eth, int n,
  404. void **init_out, char **mac_out)
  405. {
  406. int len;
  407. len = strlen(transport->name);
  408. if(strncmp(eth, transport->name, len))
  409. return(0);
  410. eth += len;
  411. if(*eth == ',')
  412. eth++;
  413. else if(*eth != '\0')
  414. return(0);
  415. *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
  416. if(*init_out == NULL)
  417. return(1);
  418. if(!transport->setup(eth, mac_out, *init_out)){
  419. kfree(*init_out);
  420. *init_out = NULL;
  421. }
  422. return(1);
  423. }
  424. void register_transport(struct transport *new)
  425. {
  426. struct list_head *ele, *next;
  427. struct eth_init *eth;
  428. void *init;
  429. char *mac = NULL;
  430. int match;
  431. list_add(&new->list, &transports);
  432. list_for_each_safe(ele, next, &eth_cmd_line){
  433. eth = list_entry(ele, struct eth_init, list);
  434. match = check_transport(new, eth->init, eth->index, &init,
  435. &mac);
  436. if(!match)
  437. continue;
  438. else if(init != NULL){
  439. eth_configure(eth->index, init, mac, new);
  440. kfree(init);
  441. }
  442. list_del(&eth->list);
  443. }
  444. }
  445. static int eth_setup_common(char *str, int index)
  446. {
  447. struct list_head *ele;
  448. struct transport *transport;
  449. void *init;
  450. char *mac = NULL;
  451. list_for_each(ele, &transports){
  452. transport = list_entry(ele, struct transport, list);
  453. if(!check_transport(transport, str, index, &init, &mac))
  454. continue;
  455. if(init != NULL){
  456. eth_configure(index, init, mac, transport);
  457. kfree(init);
  458. }
  459. return(1);
  460. }
  461. return(0);
  462. }
  463. static int eth_setup(char *str)
  464. {
  465. struct eth_init *new;
  466. int n, err;
  467. err = eth_parse(str, &n, &str);
  468. if(err) return(1);
  469. new = alloc_bootmem(sizeof(new));
  470. if (new == NULL){
  471. printk("eth_init : alloc_bootmem failed\n");
  472. return(1);
  473. }
  474. INIT_LIST_HEAD(&new->list);
  475. new->index = n;
  476. new->init = str;
  477. list_add_tail(&new->list, &eth_cmd_line);
  478. return(1);
  479. }
  480. __setup("eth", eth_setup);
  481. __uml_help(eth_setup,
  482. "eth[0-9]+=<transport>,<options>\n"
  483. " Configure a network device.\n\n"
  484. );
  485. #if 0
  486. static int eth_init(void)
  487. {
  488. struct list_head *ele, *next;
  489. struct eth_init *eth;
  490. list_for_each_safe(ele, next, &eth_cmd_line){
  491. eth = list_entry(ele, struct eth_init, list);
  492. if(eth_setup_common(eth->init, eth->index))
  493. list_del(&eth->list);
  494. }
  495. return(1);
  496. }
  497. __initcall(eth_init);
  498. #endif
  499. static int net_config(char *str)
  500. {
  501. int n, err;
  502. err = eth_parse(str, &n, &str);
  503. if(err) return(err);
  504. str = uml_strdup(str);
  505. if(str == NULL){
  506. printk(KERN_ERR "net_config failed to strdup string\n");
  507. return(-1);
  508. }
  509. err = !eth_setup_common(str, n);
  510. if(err)
  511. kfree(str);
  512. return(err);
  513. }
  514. static int net_id(char **str, int *start_out, int *end_out)
  515. {
  516. char *end;
  517. int n;
  518. n = simple_strtoul(*str, &end, 0);
  519. if((*end != '\0') || (end == *str))
  520. return -1;
  521. *start_out = n;
  522. *end_out = n;
  523. *str = end;
  524. return n;
  525. }
  526. static int net_remove(int n)
  527. {
  528. struct uml_net *device;
  529. struct net_device *dev;
  530. struct uml_net_private *lp;
  531. device = find_device(n);
  532. if(device == NULL)
  533. return -ENODEV;
  534. dev = device->dev;
  535. lp = dev->priv;
  536. if(lp->fd > 0)
  537. return -EBUSY;
  538. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  539. unregister_netdev(dev);
  540. platform_device_unregister(&device->pdev);
  541. list_del(&device->list);
  542. kfree(device);
  543. free_netdev(dev);
  544. return 0;
  545. }
  546. static struct mc_device net_mc = {
  547. .name = "eth",
  548. .config = net_config,
  549. .get_config = NULL,
  550. .id = net_id,
  551. .remove = net_remove,
  552. };
  553. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  554. void *ptr)
  555. {
  556. struct in_ifaddr *ifa = ptr;
  557. u32 addr = ifa->ifa_address;
  558. u32 netmask = ifa->ifa_mask;
  559. struct net_device *dev = ifa->ifa_dev->dev;
  560. struct uml_net_private *lp;
  561. void (*proc)(unsigned char *, unsigned char *, void *);
  562. unsigned char addr_buf[4], netmask_buf[4];
  563. if(dev->open != uml_net_open) return(NOTIFY_DONE);
  564. lp = dev->priv;
  565. proc = NULL;
  566. switch (event){
  567. case NETDEV_UP:
  568. proc = lp->add_address;
  569. break;
  570. case NETDEV_DOWN:
  571. proc = lp->delete_address;
  572. break;
  573. }
  574. if(proc != NULL){
  575. addr_buf[0] = addr & 0xff;
  576. addr_buf[1] = (addr >> 8) & 0xff;
  577. addr_buf[2] = (addr >> 16) & 0xff;
  578. addr_buf[3] = addr >> 24;
  579. netmask_buf[0] = netmask & 0xff;
  580. netmask_buf[1] = (netmask >> 8) & 0xff;
  581. netmask_buf[2] = (netmask >> 16) & 0xff;
  582. netmask_buf[3] = netmask >> 24;
  583. (*proc)(addr_buf, netmask_buf, &lp->user);
  584. }
  585. return(NOTIFY_DONE);
  586. }
  587. struct notifier_block uml_inetaddr_notifier = {
  588. .notifier_call = uml_inetaddr_event,
  589. };
  590. static int uml_net_init(void)
  591. {
  592. struct list_head *ele;
  593. struct uml_net_private *lp;
  594. struct in_device *ip;
  595. struct in_ifaddr *in;
  596. mconsole_register_dev(&net_mc);
  597. register_inetaddr_notifier(&uml_inetaddr_notifier);
  598. /* Devices may have been opened already, so the uml_inetaddr_notifier
  599. * didn't get a chance to run for them. This fakes it so that
  600. * addresses which have already been set up get handled properly.
  601. */
  602. list_for_each(ele, &opened){
  603. lp = list_entry(ele, struct uml_net_private, list);
  604. ip = lp->dev->ip_ptr;
  605. if(ip == NULL) continue;
  606. in = ip->ifa_list;
  607. while(in != NULL){
  608. uml_inetaddr_event(NULL, NETDEV_UP, in);
  609. in = in->ifa_next;
  610. }
  611. }
  612. return(0);
  613. }
  614. __initcall(uml_net_init);
  615. static void close_devices(void)
  616. {
  617. struct list_head *ele;
  618. struct uml_net_private *lp;
  619. list_for_each(ele, &opened){
  620. lp = list_entry(ele, struct uml_net_private, list);
  621. if((lp->close != NULL) && (lp->fd >= 0))
  622. (*lp->close)(lp->fd, &lp->user);
  623. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  624. }
  625. }
  626. __uml_exitcall(close_devices);
  627. int setup_etheraddr(char *str, unsigned char *addr)
  628. {
  629. char *end;
  630. int i;
  631. if(str == NULL)
  632. return(0);
  633. for(i=0;i<6;i++){
  634. addr[i] = simple_strtoul(str, &end, 16);
  635. if((end == str) ||
  636. ((*end != ':') && (*end != ',') && (*end != '\0'))){
  637. printk(KERN_ERR
  638. "setup_etheraddr: failed to parse '%s' "
  639. "as an ethernet address\n", str);
  640. return(0);
  641. }
  642. str = end + 1;
  643. }
  644. if(addr[0] & 1){
  645. printk(KERN_ERR
  646. "Attempt to assign a broadcast ethernet address to a "
  647. "device disallowed\n");
  648. return(0);
  649. }
  650. return(1);
  651. }
  652. void dev_ip_addr(void *d, char *buf, char *bin_buf)
  653. {
  654. struct net_device *dev = d;
  655. struct in_device *ip = dev->ip_ptr;
  656. struct in_ifaddr *in;
  657. u32 addr;
  658. if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
  659. printk(KERN_WARNING "dev_ip_addr - device not assigned an "
  660. "IP address\n");
  661. return;
  662. }
  663. addr = in->ifa_address;
  664. sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
  665. (addr >> 16) & 0xff, addr >> 24);
  666. if(bin_buf){
  667. bin_buf[0] = addr & 0xff;
  668. bin_buf[1] = (addr >> 8) & 0xff;
  669. bin_buf[2] = (addr >> 16) & 0xff;
  670. bin_buf[3] = addr >> 24;
  671. }
  672. }
  673. void set_ether_mac(void *d, unsigned char *addr)
  674. {
  675. struct net_device *dev = d;
  676. memcpy(dev->dev_addr, addr, ETH_ALEN);
  677. }
  678. struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
  679. {
  680. if((skb != NULL) && (skb_tailroom(skb) < extra)){
  681. struct sk_buff *skb2;
  682. skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
  683. dev_kfree_skb(skb);
  684. skb = skb2;
  685. }
  686. if(skb != NULL) skb_put(skb, extra);
  687. return(skb);
  688. }
  689. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  690. void *),
  691. void *arg)
  692. {
  693. struct net_device *dev = d;
  694. struct in_device *ip = dev->ip_ptr;
  695. struct in_ifaddr *in;
  696. unsigned char address[4], netmask[4];
  697. if(ip == NULL) return;
  698. in = ip->ifa_list;
  699. while(in != NULL){
  700. address[0] = in->ifa_address & 0xff;
  701. address[1] = (in->ifa_address >> 8) & 0xff;
  702. address[2] = (in->ifa_address >> 16) & 0xff;
  703. address[3] = in->ifa_address >> 24;
  704. netmask[0] = in->ifa_mask & 0xff;
  705. netmask[1] = (in->ifa_mask >> 8) & 0xff;
  706. netmask[2] = (in->ifa_mask >> 16) & 0xff;
  707. netmask[3] = in->ifa_mask >> 24;
  708. (*cb)(address, netmask, arg);
  709. in = in->ifa_next;
  710. }
  711. }
  712. int dev_netmask(void *d, void *m)
  713. {
  714. struct net_device *dev = d;
  715. struct in_device *ip = dev->ip_ptr;
  716. struct in_ifaddr *in;
  717. __u32 *mask_out = m;
  718. if(ip == NULL)
  719. return(1);
  720. in = ip->ifa_list;
  721. if(in == NULL)
  722. return(1);
  723. *mask_out = in->ifa_mask;
  724. return(0);
  725. }
  726. void *get_output_buffer(int *len_out)
  727. {
  728. void *ret;
  729. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  730. if(ret) *len_out = PAGE_SIZE;
  731. else *len_out = 0;
  732. return(ret);
  733. }
  734. void free_output_buffer(void *buffer)
  735. {
  736. free_pages((unsigned long) buffer, 0);
  737. }
  738. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  739. char **gate_addr)
  740. {
  741. char *remain;
  742. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  743. if(remain != NULL){
  744. printk("tap_setup_common - Extra garbage on specification : "
  745. "'%s'\n", remain);
  746. return(1);
  747. }
  748. return(0);
  749. }
  750. unsigned short eth_protocol(struct sk_buff *skb)
  751. {
  752. return(eth_type_trans(skb, skb->dev));
  753. }
  754. /*
  755. * Overrides for Emacs so that we follow Linus's tabbing style.
  756. * Emacs will notice this stuff at the end of the file and automatically
  757. * adjust the settings for this buffer only. This must remain at the end
  758. * of the file.
  759. * ---------------------------------------------------------------------------
  760. * Local variables:
  761. * c-file-style: "linux"
  762. * End:
  763. */