net_kern.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. static 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. 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, &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. list_del(&lp->list);
  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 void uml_net_get_drvinfo(struct net_device *dev,
  201. struct ethtool_drvinfo *info)
  202. {
  203. strcpy(info->driver, DRIVER_NAME);
  204. strcpy(info->version, "42");
  205. }
  206. static struct ethtool_ops uml_net_ethtool_ops = {
  207. .get_drvinfo = uml_net_get_drvinfo,
  208. .get_link = ethtool_op_get_link,
  209. };
  210. void uml_net_user_timer_expire(unsigned long _conn)
  211. {
  212. #ifdef undef
  213. struct connection *conn = (struct connection *)_conn;
  214. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  215. do_connect(conn);
  216. #endif
  217. }
  218. static DEFINE_SPINLOCK(devices_lock);
  219. static LIST_HEAD(devices);
  220. static struct platform_driver uml_net_driver = {
  221. .driver = {
  222. .name = DRIVER_NAME,
  223. },
  224. };
  225. static int driver_registered;
  226. static int eth_configure(int n, void *init, char *mac,
  227. struct transport *transport)
  228. {
  229. struct uml_net *device;
  230. struct net_device *dev;
  231. struct uml_net_private *lp;
  232. int save, err, size;
  233. size = transport->private_size + sizeof(struct uml_net_private) +
  234. sizeof(((struct uml_net_private *) 0)->user);
  235. device = kmalloc(sizeof(*device), GFP_KERNEL);
  236. if (device == NULL) {
  237. printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
  238. return(1);
  239. }
  240. memset(device, 0, sizeof(*device));
  241. INIT_LIST_HEAD(&device->list);
  242. device->index = n;
  243. spin_lock(&devices_lock);
  244. list_add(&device->list, &devices);
  245. spin_unlock(&devices_lock);
  246. if (setup_etheraddr(mac, device->mac))
  247. device->have_mac = 1;
  248. printk(KERN_INFO "Netdevice %d ", n);
  249. if (device->have_mac)
  250. printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
  251. device->mac[0], device->mac[1],
  252. device->mac[2], device->mac[3],
  253. device->mac[4], device->mac[5]);
  254. printk(": ");
  255. dev = alloc_etherdev(size);
  256. if (dev == NULL) {
  257. printk(KERN_ERR "eth_configure: failed to allocate device\n");
  258. return 1;
  259. }
  260. lp = dev->priv;
  261. /* This points to the transport private data. It's still clear, but we
  262. * must memset it to 0 *now*. Let's help the drivers. */
  263. memset(lp, 0, size);
  264. /* sysfs register */
  265. if (!driver_registered) {
  266. platform_driver_register(&uml_net_driver);
  267. driver_registered = 1;
  268. }
  269. device->pdev.id = n;
  270. device->pdev.name = DRIVER_NAME;
  271. platform_device_register(&device->pdev);
  272. SET_NETDEV_DEV(dev,&device->pdev.dev);
  273. /* If this name ends up conflicting with an existing registered
  274. * netdevice, that is OK, register_netdev{,ice}() will notice this
  275. * and fail.
  276. */
  277. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  278. device->dev = dev;
  279. (*transport->kern->init)(dev, init);
  280. dev->mtu = transport->user->max_packet;
  281. dev->open = uml_net_open;
  282. dev->hard_start_xmit = uml_net_start_xmit;
  283. dev->stop = uml_net_close;
  284. dev->get_stats = uml_net_get_stats;
  285. dev->set_multicast_list = uml_net_set_multicast_list;
  286. dev->tx_timeout = uml_net_tx_timeout;
  287. dev->set_mac_address = uml_net_set_mac;
  288. dev->change_mtu = uml_net_change_mtu;
  289. dev->ethtool_ops = &uml_net_ethtool_ops;
  290. dev->watchdog_timeo = (HZ >> 1);
  291. dev->irq = UM_ETH_IRQ;
  292. rtnl_lock();
  293. err = register_netdevice(dev);
  294. rtnl_unlock();
  295. if (err) {
  296. device->dev = NULL;
  297. /* XXX: should we call ->remove() here? */
  298. free_netdev(dev);
  299. return 1;
  300. }
  301. /* lp.user is the first four bytes of the transport data, which
  302. * has already been initialized. This structure assignment will
  303. * overwrite that, so we make sure that .user gets overwritten with
  304. * what it already has.
  305. */
  306. save = lp->user[0];
  307. *lp = ((struct uml_net_private)
  308. { .list = LIST_HEAD_INIT(lp->list),
  309. .dev = dev,
  310. .fd = -1,
  311. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  312. .have_mac = device->have_mac,
  313. .protocol = transport->kern->protocol,
  314. .open = transport->user->open,
  315. .close = transport->user->close,
  316. .remove = transport->user->remove,
  317. .read = transport->kern->read,
  318. .write = transport->kern->write,
  319. .add_address = transport->user->add_address,
  320. .delete_address = transport->user->delete_address,
  321. .set_mtu = transport->user->set_mtu,
  322. .user = { save } });
  323. init_timer(&lp->tl);
  324. spin_lock_init(&lp->lock);
  325. lp->tl.function = uml_net_user_timer_expire;
  326. if (lp->have_mac)
  327. memcpy(lp->mac, device->mac, sizeof(lp->mac));
  328. if (transport->user->init)
  329. (*transport->user->init)(&lp->user, dev);
  330. if (device->have_mac)
  331. set_ether_mac(dev, device->mac);
  332. spin_lock(&opened_lock);
  333. list_add(&lp->list, &opened);
  334. spin_unlock(&opened_lock);
  335. return(0);
  336. }
  337. static struct uml_net *find_device(int n)
  338. {
  339. struct uml_net *device;
  340. struct list_head *ele;
  341. spin_lock(&devices_lock);
  342. list_for_each(ele, &devices){
  343. device = list_entry(ele, struct uml_net, list);
  344. if(device->index == n)
  345. goto out;
  346. }
  347. device = NULL;
  348. out:
  349. spin_unlock(&devices_lock);
  350. return(device);
  351. }
  352. static int eth_parse(char *str, int *index_out, char **str_out)
  353. {
  354. char *end;
  355. int n;
  356. n = simple_strtoul(str, &end, 0);
  357. if(end == str){
  358. printk(KERN_ERR "eth_setup: Failed to parse '%s'\n", str);
  359. return(1);
  360. }
  361. if(n < 0){
  362. printk(KERN_ERR "eth_setup: device %d is negative\n", n);
  363. return(1);
  364. }
  365. str = end;
  366. if(*str != '='){
  367. printk(KERN_ERR
  368. "eth_setup: expected '=' after device number\n");
  369. return(1);
  370. }
  371. str++;
  372. if(find_device(n)){
  373. printk(KERN_ERR "eth_setup: Device %d already configured\n",
  374. n);
  375. return(1);
  376. }
  377. if(index_out) *index_out = n;
  378. *str_out = str;
  379. return(0);
  380. }
  381. struct eth_init {
  382. struct list_head list;
  383. char *init;
  384. int index;
  385. };
  386. /* Filled in at boot time. Will need locking if the transports become
  387. * modular.
  388. */
  389. struct list_head transports = LIST_HEAD_INIT(transports);
  390. /* Filled in during early boot */
  391. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  392. static int check_transport(struct transport *transport, char *eth, int n,
  393. void **init_out, char **mac_out)
  394. {
  395. int len;
  396. len = strlen(transport->name);
  397. if(strncmp(eth, transport->name, len))
  398. return(0);
  399. eth += len;
  400. if(*eth == ',')
  401. eth++;
  402. else if(*eth != '\0')
  403. return(0);
  404. *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
  405. if(*init_out == NULL)
  406. return(1);
  407. if(!transport->setup(eth, mac_out, *init_out)){
  408. kfree(*init_out);
  409. *init_out = NULL;
  410. }
  411. return(1);
  412. }
  413. void register_transport(struct transport *new)
  414. {
  415. struct list_head *ele, *next;
  416. struct eth_init *eth;
  417. void *init;
  418. char *mac = NULL;
  419. int match;
  420. list_add(&new->list, &transports);
  421. list_for_each_safe(ele, next, &eth_cmd_line){
  422. eth = list_entry(ele, struct eth_init, list);
  423. match = check_transport(new, eth->init, eth->index, &init,
  424. &mac);
  425. if(!match)
  426. continue;
  427. else if(init != NULL){
  428. eth_configure(eth->index, init, mac, new);
  429. kfree(init);
  430. }
  431. list_del(&eth->list);
  432. }
  433. }
  434. static int eth_setup_common(char *str, int index)
  435. {
  436. struct list_head *ele;
  437. struct transport *transport;
  438. void *init;
  439. char *mac = NULL;
  440. list_for_each(ele, &transports){
  441. transport = list_entry(ele, struct transport, list);
  442. if(!check_transport(transport, str, index, &init, &mac))
  443. continue;
  444. if(init != NULL){
  445. eth_configure(index, init, mac, transport);
  446. kfree(init);
  447. }
  448. return(1);
  449. }
  450. return(0);
  451. }
  452. static int eth_setup(char *str)
  453. {
  454. struct eth_init *new;
  455. int n, err;
  456. err = eth_parse(str, &n, &str);
  457. if(err) return(1);
  458. new = alloc_bootmem(sizeof(new));
  459. if (new == NULL){
  460. printk("eth_init : alloc_bootmem failed\n");
  461. return(1);
  462. }
  463. INIT_LIST_HEAD(&new->list);
  464. new->index = n;
  465. new->init = str;
  466. list_add_tail(&new->list, &eth_cmd_line);
  467. return(1);
  468. }
  469. __setup("eth", eth_setup);
  470. __uml_help(eth_setup,
  471. "eth[0-9]+=<transport>,<options>\n"
  472. " Configure a network device.\n\n"
  473. );
  474. #if 0
  475. static int eth_init(void)
  476. {
  477. struct list_head *ele, *next;
  478. struct eth_init *eth;
  479. list_for_each_safe(ele, next, &eth_cmd_line){
  480. eth = list_entry(ele, struct eth_init, list);
  481. if(eth_setup_common(eth->init, eth->index))
  482. list_del(&eth->list);
  483. }
  484. return(1);
  485. }
  486. __initcall(eth_init);
  487. #endif
  488. static int net_config(char *str)
  489. {
  490. int n, err;
  491. err = eth_parse(str, &n, &str);
  492. if(err) return(err);
  493. str = kstrdup(str, GFP_KERNEL);
  494. if(str == NULL){
  495. printk(KERN_ERR "net_config failed to strdup string\n");
  496. return(-1);
  497. }
  498. err = !eth_setup_common(str, n);
  499. if(err)
  500. kfree(str);
  501. return(err);
  502. }
  503. static int net_id(char **str, int *start_out, int *end_out)
  504. {
  505. char *end;
  506. int n;
  507. n = simple_strtoul(*str, &end, 0);
  508. if((*end != '\0') || (end == *str))
  509. return -1;
  510. *start_out = n;
  511. *end_out = n;
  512. *str = end;
  513. return n;
  514. }
  515. static int net_remove(int n)
  516. {
  517. struct uml_net *device;
  518. struct net_device *dev;
  519. struct uml_net_private *lp;
  520. device = find_device(n);
  521. if(device == NULL)
  522. return -ENODEV;
  523. dev = device->dev;
  524. lp = dev->priv;
  525. if(lp->fd > 0)
  526. return -EBUSY;
  527. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  528. unregister_netdev(dev);
  529. platform_device_unregister(&device->pdev);
  530. list_del(&device->list);
  531. kfree(device);
  532. free_netdev(dev);
  533. return 0;
  534. }
  535. static struct mc_device net_mc = {
  536. .name = "eth",
  537. .config = net_config,
  538. .get_config = NULL,
  539. .id = net_id,
  540. .remove = net_remove,
  541. };
  542. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  543. void *ptr)
  544. {
  545. struct in_ifaddr *ifa = ptr;
  546. struct net_device *dev = ifa->ifa_dev->dev;
  547. struct uml_net_private *lp;
  548. void (*proc)(unsigned char *, unsigned char *, void *);
  549. unsigned char addr_buf[4], netmask_buf[4];
  550. if(dev->open != uml_net_open) return(NOTIFY_DONE);
  551. lp = dev->priv;
  552. proc = NULL;
  553. switch (event){
  554. case NETDEV_UP:
  555. proc = lp->add_address;
  556. break;
  557. case NETDEV_DOWN:
  558. proc = lp->delete_address;
  559. break;
  560. }
  561. if(proc != NULL){
  562. memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
  563. memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
  564. (*proc)(addr_buf, netmask_buf, &lp->user);
  565. }
  566. return(NOTIFY_DONE);
  567. }
  568. struct notifier_block uml_inetaddr_notifier = {
  569. .notifier_call = uml_inetaddr_event,
  570. };
  571. static int uml_net_init(void)
  572. {
  573. struct list_head *ele;
  574. struct uml_net_private *lp;
  575. struct in_device *ip;
  576. struct in_ifaddr *in;
  577. mconsole_register_dev(&net_mc);
  578. register_inetaddr_notifier(&uml_inetaddr_notifier);
  579. /* Devices may have been opened already, so the uml_inetaddr_notifier
  580. * didn't get a chance to run for them. This fakes it so that
  581. * addresses which have already been set up get handled properly.
  582. */
  583. list_for_each(ele, &opened){
  584. lp = list_entry(ele, struct uml_net_private, list);
  585. ip = lp->dev->ip_ptr;
  586. if(ip == NULL) continue;
  587. in = ip->ifa_list;
  588. while(in != NULL){
  589. uml_inetaddr_event(NULL, NETDEV_UP, in);
  590. in = in->ifa_next;
  591. }
  592. }
  593. return(0);
  594. }
  595. __initcall(uml_net_init);
  596. static void close_devices(void)
  597. {
  598. struct list_head *ele;
  599. struct uml_net_private *lp;
  600. list_for_each(ele, &opened){
  601. lp = list_entry(ele, struct uml_net_private, list);
  602. free_irq(lp->dev->irq, lp->dev);
  603. if((lp->close != NULL) && (lp->fd >= 0))
  604. (*lp->close)(lp->fd, &lp->user);
  605. if(lp->remove != NULL) (*lp->remove)(&lp->user);
  606. }
  607. }
  608. __uml_exitcall(close_devices);
  609. int setup_etheraddr(char *str, unsigned char *addr)
  610. {
  611. char *end;
  612. int i;
  613. if(str == NULL)
  614. return(0);
  615. for(i=0;i<6;i++){
  616. addr[i] = simple_strtoul(str, &end, 16);
  617. if((end == str) ||
  618. ((*end != ':') && (*end != ',') && (*end != '\0'))){
  619. printk(KERN_ERR
  620. "setup_etheraddr: failed to parse '%s' "
  621. "as an ethernet address\n", str);
  622. return(0);
  623. }
  624. str = end + 1;
  625. }
  626. if(addr[0] & 1){
  627. printk(KERN_ERR
  628. "Attempt to assign a broadcast ethernet address to a "
  629. "device disallowed\n");
  630. return(0);
  631. }
  632. return(1);
  633. }
  634. void dev_ip_addr(void *d, unsigned char *bin_buf)
  635. {
  636. struct net_device *dev = d;
  637. struct in_device *ip = dev->ip_ptr;
  638. struct in_ifaddr *in;
  639. if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
  640. printk(KERN_WARNING "dev_ip_addr - device not assigned an "
  641. "IP address\n");
  642. return;
  643. }
  644. memcpy(bin_buf, &in->ifa_address, sizeof(in->ifa_address));
  645. }
  646. void set_ether_mac(void *d, unsigned char *addr)
  647. {
  648. struct net_device *dev = d;
  649. memcpy(dev->dev_addr, addr, ETH_ALEN);
  650. }
  651. struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
  652. {
  653. if((skb != NULL) && (skb_tailroom(skb) < extra)){
  654. struct sk_buff *skb2;
  655. skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
  656. dev_kfree_skb(skb);
  657. skb = skb2;
  658. }
  659. if(skb != NULL) skb_put(skb, extra);
  660. return(skb);
  661. }
  662. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  663. void *),
  664. void *arg)
  665. {
  666. struct net_device *dev = d;
  667. struct in_device *ip = dev->ip_ptr;
  668. struct in_ifaddr *in;
  669. unsigned char address[4], netmask[4];
  670. if(ip == NULL) return;
  671. in = ip->ifa_list;
  672. while(in != NULL){
  673. memcpy(address, &in->ifa_address, sizeof(address));
  674. memcpy(netmask, &in->ifa_mask, sizeof(netmask));
  675. (*cb)(address, netmask, arg);
  676. in = in->ifa_next;
  677. }
  678. }
  679. int dev_netmask(void *d, void *m)
  680. {
  681. struct net_device *dev = d;
  682. struct in_device *ip = dev->ip_ptr;
  683. struct in_ifaddr *in;
  684. __u32 *mask_out = m;
  685. if(ip == NULL)
  686. return(1);
  687. in = ip->ifa_list;
  688. if(in == NULL)
  689. return(1);
  690. *mask_out = in->ifa_mask;
  691. return(0);
  692. }
  693. void *get_output_buffer(int *len_out)
  694. {
  695. void *ret;
  696. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  697. if(ret) *len_out = PAGE_SIZE;
  698. else *len_out = 0;
  699. return(ret);
  700. }
  701. void free_output_buffer(void *buffer)
  702. {
  703. free_pages((unsigned long) buffer, 0);
  704. }
  705. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  706. char **gate_addr)
  707. {
  708. char *remain;
  709. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  710. if(remain != NULL){
  711. printk("tap_setup_common - Extra garbage on specification : "
  712. "'%s'\n", remain);
  713. return(1);
  714. }
  715. return(0);
  716. }
  717. unsigned short eth_protocol(struct sk_buff *skb)
  718. {
  719. return(eth_type_trans(skb, skb->dev));
  720. }
  721. /*
  722. * Overrides for Emacs so that we follow Linus's tabbing style.
  723. * Emacs will notice this stuff at the end of the file and automatically
  724. * adjust the settings for this buffer only. This must remain at the end
  725. * of the file.
  726. * ---------------------------------------------------------------------------
  727. * Local variables:
  728. * c-file-style: "linux"
  729. * End:
  730. */