net_kern.c 20 KB

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