Skip to content

Latest commit

 

History

History
127 lines (107 loc) · 3.29 KB

usb_moded-mac.c

File metadata and controls

127 lines (107 loc) · 3.29 KB
 
Aug 24, 2018
Aug 24, 2018
2
3
* @file usb_moded-mac.c
*
Apr 9, 2019
Apr 9, 2019
4
* Copyright (C) 2013-2019 Jolla. All rights reserved.
Aug 24, 2018
Aug 24, 2018
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
Aug 24, 2018
Aug 24, 2018
24
#include "usb_moded-mac.h"
Sep 5, 2018
Sep 5, 2018
25
Aug 24, 2018
Aug 24, 2018
26
#include "usb_moded-log.h"
Sep 5, 2018
Sep 5, 2018
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Aug 24, 2018
Aug 24, 2018
32
33
34
/* ========================================================================= *
* Prototypes
* ========================================================================= */
Apr 9, 2019
Apr 9, 2019
36
37
38
/* ------------------------------------------------------------------------- *
* MAC
* ------------------------------------------------------------------------- */
Aug 24, 2018
Aug 24, 2018
40
41
42
static void mac_random_ether_addr (unsigned char *addr);
void mac_generate_random_mac(void);
char *mac_read_mac (void);
Aug 24, 2018
Aug 24, 2018
44
45
46
/* ========================================================================= *
* Functions
* ========================================================================= */
Aug 24, 2018
Aug 24, 2018
48
static void mac_random_ether_addr(unsigned char *addr)
Feb 20, 2019
Feb 20, 2019
50
51
LOG_REGISTER_CONTEXT;
Aug 24, 2018
Aug 24, 2018
52
53
FILE *random;
size_t count = 0;
Aug 24, 2018
Aug 24, 2018
55
56
57
random = fopen("/dev/urandom", "r");
count = fread(addr, 1, 6, random);
fclose(random);
Aug 24, 2018
Aug 24, 2018
59
60
61
62
63
64
65
if(count > 0 )
{
addr [0] &= 0xfe; /* clear multicast bit */
addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
}
else
log_warning("MAC generation failed!\n");
Aug 24, 2018
Aug 24, 2018
68
void mac_generate_random_mac (void)
Feb 20, 2019
Feb 20, 2019
70
71
LOG_REGISTER_CONTEXT;
Aug 24, 2018
Aug 24, 2018
72
73
74
75
76
77
78
79
80
81
82
83
84
85
unsigned char addr[6];
int i;
FILE *g_ether;
log_debug("Getting random usb ethernet mac\n");
mac_random_ether_addr(addr);
g_ether = fopen("/etc/modprobe.d/g_ether.conf", "w");
if(!g_ether)
{
log_warning("Failed to write mac address to /etc/modprobe.d/g_ether.conf\n");
return;
}
fprintf(g_ether, "options g_ether host_addr=");
Aug 24, 2018
Aug 24, 2018
87
88
89
90
91
92
for(i=0; i<5; i++)
{
fprintf(g_ether, "%02x:",addr[i]);
}
fprintf(g_ether, "%02x\n",addr[i]);
fclose(g_ether);
Aug 14, 2013
Aug 14, 2013
94
Aug 24, 2018
Aug 24, 2018
95
char * mac_read_mac(void)
Aug 14, 2013
Aug 14, 2013
96
{
Feb 20, 2019
Feb 20, 2019
97
98
LOG_REGISTER_CONTEXT;
Aug 24, 2018
Aug 24, 2018
99
100
101
102
FILE *g_ether;
char *mac = NULL, *ret = NULL;
size_t read = 0;
int test = 0;
Aug 14, 2013
Aug 14, 2013
103
Aug 24, 2018
Aug 24, 2018
104
105
106
107
g_ether = fopen("/etc/modprobe.d/g_ether.conf", "r");
if(!g_ether)
{
log_warning("Failed to read mac address from /etc/modprobe.d/g_ether.conf\n");
Sep 5, 2018
Sep 5, 2018
108
return NULL;
Aug 24, 2018
Aug 24, 2018
109
110
111
112
113
114
115
}
test = fseek(g_ether, 26, SEEK_SET);
if(test == -1)
{
fclose(g_ether);
return 0;
}
Sep 5, 2018
Sep 5, 2018
116
mac = malloc(17);
Aug 24, 2018
Aug 24, 2018
117
118
119
120
121
122
if(mac)
read = fread(mac, 1, 17, g_ether);
if(read == 17)
ret = strndup(mac,17);
else
ret = 0;
Oct 29, 2013
Oct 29, 2013
123
Aug 24, 2018
Aug 24, 2018
124
125
free(mac);
fclose(g_ether);
Sep 5, 2018
Sep 5, 2018
126
return ret;
Aug 14, 2013
Aug 14, 2013
127
}