net_kern.c 19 KB

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