# 4G usb modem



## Snail (Aug 21, 2018)

Hello,

I bought a Huawei E3372 modem and flashed its firmware to "stick mode", so it acts as a modem, not as a router.
This way, I have access to NCM connection instead of PPP.
I also modified the multi-mode so I do not need to use usb_modeswitch(8).

Manually, I :

launch an AT^ command to /dev/cuaU0 to initialize the modem
run dhclient on ue0 (the ncm interface)
That's it, the connection does work flawlessly.

What I would like to achieve is doing this automatically on boot (the USB key will remain connected).
So far, I :

set `ifconfig_ue0="DHCP"` (or SYNCDHCP) in /etc/rc.conf
created a devd entry to launch the AT^ command
But /etc/rc.d/devd is run after /etc/rc.d/netif.
So on boot, FreeBSD tries to get an IP address on ue0 (which fails because the modem is down at this very moment).
Then devd is started, the modem is initialized and ue0 is ready to  get an IP address. But it is too late.

Does anyone know how it would be possible to launch the initialization command before the start of the network ?


----------



## Snail (Aug 21, 2018)

It seems that I misinterpreted my tests.
I might misunderstand it, but I do not think that netwait is the solution, because the interface (ue0) is present early. 
What I could have to wait for is /dev/cuaU0, but it is present early too.

My problem is maybe relative to the order of devd rules, where netif would start before my rule is fired. 
Thanks for the information.


----------



## balanga (Aug 21, 2018)

Did you need to do anything with sysutils/usb_modeswitch ?  I spent months trying to get my Huawei E3372 working. 
How did you go about flashing the device


----------



## Snail (Aug 22, 2018)

Do you have a thread open on this subject ? I think that a dedicated one would be a better place for an answer;  it could be more technical and useful for anyone looking for this.
In the meantime, here are some explanations, but I have been owning it for a few days only.

By default, there are two consecutive modes. In the first one, the key acts as a cdrom containing drivers for Windows. 
Once those drivers are installed, the key switches to the second mode, where it acts like a modem or router. 
This depends on its firmware :

router <-> hilink
modem <-> stick
What you want is your key to be in this second mode. The purpose of usb_modeswitch is to allow you force this switch, for both types of firmwares.

If you own a stick version , It is possible to change the behavior of each mode ( you can choose what will be present or not - rndis card, ncm ,modem etc - ), even disabling the first one. in this case usb_modeswitch is not needed.

if you own a hilink, the only solution is usb_modeswitch. This is the simpler solution because there is a dhcp server in it, but it is more limited; you can not send sms etc.
You can flash a stick firmware on it, but the way depends on the model.


----------



## balanga (Aug 22, 2018)

Snail said:


> Do you have a thread open on this subject ?



It might by an idea to start a new thread entitled  *Huawei E3372* in the Howto forum...


----------



## Nicola Mingotti (Aug 22, 2018)

Damn interesting Snail ! 
I support balanga idea of starting an Howto about this.

BTW, i just discovered the "Howto forum", great addition ! how could i miss that


----------



## Nicola Mingotti (Aug 22, 2018)

Snail , coming to your question.

The dirty solution is:
-] use a @reboot line in cron and run your configurations in a custom script
where you decide the order of services and config you want to launch.

The clean solution probably is:
-] make your own rc.script called e.g. "at-conf" then change dhclient rc.script
in such a way that it runs after "at-conf".
CAVEAT. I never tried to do this so far, it is a guess.


----------



## Snail (Aug 23, 2018)

Phishfry : yes, it seems you did. I might still create an how-to, that would help people feeling as lost as I was when I began to try to use this key on FreeBSD.

Nicola Mingotti : that is near what I did .
I created an rc script to lauch my command, and I specified "# BEFORE: netif" in the header.

It does work, but I am not totally satisfied.
I might be wrong but I think that the cleanest way would be to able to launch a command prior the configuration of an interface. Something like pre-up, post-up found in Debian.


----------



## balanga (Sep 2, 2018)

Snail said:


> Do you have a thread open on this subject ? I think that a dedicated one would be a better place for an answer;  it could be more technical and useful for anyone looking for this.



I very much agree and keep looking to see if you have starteed an E3372 thread in the Howto section


----------



## Snail (Sep 7, 2018)

Hello,
I will write it, but I need to solve remaining problems (disconnections, FIB routing tables, firewall behavior...) first as the machine is my router.


----------



## ronaldlees (Sep 15, 2018)

balanga said:


> Did you need to do anything with sysutils/usb_modeswitch ?  I spent months trying to get my Huawei E3372 working.
> How did you go about flashing the device



I've looked briefly at this device, as I'm thinking about a 4G failover setup.  I don't own the E3372, since I've been looking at more recent stuff that has better LTE band coverage, etc.  Anyway, in looking at various posts about the E3372, I came across an item that speaks to putting the E3372 into modem mode _without flashing it,_ by using the web admin pages of the router when it's in HiLink mode, and switching the device mode by going to http://192.168.8.1, and then subsequently to http://192.168.8.1/html/SwitchProjectMode.html .

I guess that's stating the obvious for people on this thread .  I see that OpenBSD has a driver for MBIM, which seems to be a better choice - maybe. Perhaps FreeBSD can have an MBIM driver, patterned after the OpenBSD one.  Many of the recent/new modems are supporting AT, MBIM, and QMI protocols because Microsoft is pushing MBIM and also for MBIM's better performance.  I'm just starting with 4G modems, so for me there are way too many choices to make ...


----------



## balanga (Sep 22, 2018)

ronaldlees said:


> I've looked briefly at this device, as I'm thinking about a 4G failover setup.  I don't own the E3372, since I've been looking at more recent stuff that has better LTE band coverage, etc.



I'd be interested in knowing how you identify 'better LTE band coverage, etc'....



> Anyway, in looking at various posts about the E3372, I came across an item that speaks to putting the E3372 into modem mode _without flashing it,_ by using the web admin pages of the router when it's in HiLink mode, and switching the device mode by going to http://192.168.8.1, and then subsequently to http://192.168.8.1/html/SwitchProjectMode.html .



I read that somewhere but could never get it to work.


----------



## balanga (Sep 26, 2018)

Snail said:


> Do you have a thread open on this subject ? I think that a dedicated one would be a better place for an answer;  it could be more technical and useful for anyone looking for this.
> In the meantime, here are some explanations, but I have been owning it for a few days only.
> 
> By default, there are two consecutive modes. In the first one, the key acts as a cdrom containing drivers for Windows.
> ...



I have both a hilink and a stick version and did have my hilink version working at one point using usb_modeswitch, but the modem was locked until a few days ago and I hadn't used it for a while. Unfortunately I'm away from home at the moment and the system I have with me is not configured to use it and I can't remember what I did to get it working. 

I look forward to your post in the Howto section


----------



## balanga (Sep 26, 2018)

Snail said:


> Hello,
> 
> I bought a Huawei E3372 modem and flashed its firmware to "stick mode", so it acts as a modem, not as a router.
> This way, I have access to NCM connection instead of PPP.
> ...



I'm trying to figure out exactly what you did since I have 'stick mode' Huawei E3372. When I insert it, it presents itself as a 
Modem (Mass Storage Mode). As such a /dev/cuaU0 does not get created and no ue0 interface appears.


----------



## Snail (Sep 30, 2018)

Hello ,

balanga : I just saw your posts.
As I say, There are two consecutives modes, the first mode being, by default, a virtual cdrom.
The second mode is the one you are interested in; it brings the modem mode among other things. You can switch to this mode by using the well named usb_modeswitch.
I think that usb_modeswitch should recognize your stick and switch it on its own. If that is not the case, I created a file named /etc/usb_modeswitch.d/12d1:14fe reading 

```
TargetVendor=0x12d1
TargetProductList="1506,150f,151d,1c1e"
HuaweiNewMode=1
```

I tried a lot of things at the beginning, so I can not ensure you that this will work.

Once there, you should modify those 2 modes by using codes, which are:

FF : disable first mode
1 : 3G modem
10 :  4G modem
12 : port COM "PC UI Interface" 

16 :  NCM network card
A1 :  virtual CD-ROM
A2 : microSD
A3 : RNDIS network card
Those codes are sent to the modem though the AT command AT^SETPORT.
The first mode can only accept FF, A2 and A1.
A mode can accept several codes separated by "," , while the modes are separated by ";".

/!\ /!\/!\/!\ Do never remove the code 12, or your modem would be bricked /!\ /!\/!\/!\

I just needed the NCM network card so I :

```
1) connected to the port com
cu -s 115200 -l cuaU0

2) sent the codes
AT^SETPORT="FF;12,16"

# explanations
FF-> disable first mode
; -> codes are now dedicated to the second mode
12 -> com port ( cuaU0 )
16 -> ncm ( ue0 interface)
Note that if you activate 3G or 4G modem, you will get cuaU1 etc...

3) reset the modem
AT^RESET
```

The modem will reboot and you won't need usb_modeswitch anymore.


----------



## balanga (Oct 4, 2018)

Snail said:


> Hello ,
> 
> balanga : I just saw your posts.
> As I say, There are two consecutives modes, the first mode being, by default, a virtual cdrom.
> ...



Thanks for the help but I don't seem to be able to activate this usb_modeswitch command...

I have created a /etc/usb_modeswitch.d/12d1:14fe with the contents you suggest but can't figure out how to run it.

I tried simply rebooting hoping usb_modeswitch would run on boot, but that didn't work. I also tried `usb_modeswitch -W -c /etc/usb_modeswitch.d/12d1:14fe` but got 





> Read config file: /etc/usb_modeswitch.d/12d1:14fe
> 
> * usb_modeswitch: handle USB devices with multiple modes
> * Version 2.5.2 (C) Josua Dietze 2017
> ...



`lsusb` shows:





> Bus /dev/usb Device /dev/ugen1.4: ID 0a5c:21e6 Broadcom Corp. BCM20702 Bluetooth 4.0 [ThinkPad]
> Bus /dev/usb Device /dev/ugen1.3: ID 17ef:1003 Lenovo Integrated Smart Card Reader
> Bus /dev/usb Device /dev/ugen0.2: ID 12d1:14fe Huawei Technologies Co., Ltd. Modem (Mass Storage Mode)
> Bus /dev/usb Device /dev/ugen1.2: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
> ...


----------



## balanga (Oct 4, 2018)

After reading some posts here I came up with `usb_modeswitch -W -v 0x12d1 -p 0x14fe -c /etc/usb_modeswitch.d/12d1:14fe`
 which generated:-





> Read config file: /etc/usb_modeswitch.d/12d1:14fe
> 
> * usb_modeswitch: handle USB devices with multiple modes
> * Version 2.5.2 (C) Josua Dietze 2017
> ...



Not sure what the last msg means but the command does not seem to have finished, however `lsusb`  now shows:





> Bus /dev/usb Device /dev/ugen0.2: ID 12d1:1506 Huawei Technologies Co., Ltd. Modem/Networkcard


----------



## Snail (Oct 4, 2018)

Hello,

This seemed to work for you in one of your topics: https://forums.freebsd.org/threads/usb_modeswitch.63677/post-369848
Isn't it the case anymore ?

The switch seems to work on your linux box, so at worst, you can send the codes with your modem connected on it.


----------



## Snail (Oct 4, 2018)

I just re-read your upper post and got lost.
Are you trying to configure your hilink or your stick modem ?
If stick one, then you need to load the u3g kernel module *BEFORE* inserting the device . Read especially the BUGS part of u3g().
Or you can built the module into the kernel, which I did, as for if_cdce().


----------



## Snail (Oct 4, 2018)

balanga : I am glad it worked for you. 
I do not understand why, but your post beginning with "After reading some posts here I came up..." was absent when I wrote my answers. 
Just curious, will you stick with the usb_modeswitch method or will you change the configuration of the modem ?


----------



## balanga (Oct 5, 2018)

Snail said:


> Hello,
> 
> This seemed to work for you in one of your topics: https://forums.freebsd.org/threads/usb_modeswitch.63677/post-369848
> Isn't it the case anymore ?
> ...



I have now managed to get sysutils/usb_modeswitch to recognise my E3372 stick modem as Modem/Networkcard

`lsusb` shows:-


> Bus /dev/usb Device /dev/ugen1.4: ID 12d1:1506 Huawei Technologies Co., Ltd. Modem/Networkcard


 but I don't see a ue0 network interface appear so am obviously missing something...


----------



## balanga (Oct 5, 2018)

Snail said:


> I just re-read your upper post and got lost.
> Are you trying to configure your hilink or your stick modem ?
> If stick one, then you need to load the u3g kernel module *BEFORE* inserting the device . Read especially the BUGS part of u3g().
> Or you can built the module into the kernel, which I did, as for if_cdce().




Just to clarify things.... I have two E3372 USB modems - one is a stick modem and the the other is a hilink modem.

As a result of this post of yours in which you give instructions for enabling the ue0 interface on the stick modem, I'm trying to configure my stick modem which is actually labelled a MegaFon M150-2

Connecting to /dev/cuaU0 and running ATI shows:-


> Manufacturer: huawei
> Model: E3372
> Revision: 21.180.01.00.00
> IMEI: 866119027727057
> +GCAP: +CGSM,+DS,+ES



One thing I observe is that there is continuous serial activity similar to:-





> ^RSSI:26
> 
> ^HCSQ:"LTE",60,55,186,26
> 
> ...



What does this tell me?

The main thing is that don't know how to enable the ue0 interface.


----------



## Snail (Oct 5, 2018)

I think this means that your modem is connected or receiving a signal.
In order to get the ue0 interface, you need to load the if_cdce kernel module. Read the synopsis of if_cdce().


----------



## balanga (Oct 6, 2018)

Snail said:


> I think this means that your modem is connected or receiving a signal.
> In order to get the ue0 interface, you need to load the if_cdce kernel module. Read the synopsis of if_cdce().



Many thanks for this. I now have a ue0 interface but can't get an IP addressed assigned....

`ifconfig ue0`:-


> ue0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
> ether 2a:d7:ea:f6:7f:00
> hwaddr 2a:d7:ea:f6:7f:00
> inet 0.0.0.0 netmask 0xff000000 broadcast 255.255.255.255
> nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>



Is there some way I can monitor /dev/cuaU0 to see what is actually happening? 

The SIM connects fine when it's in my mobile phone.


----------



## Snail (Oct 6, 2018)

You need to send the NDISDUP command to your modem.

`echo 'AT^NDISDUP= 1,1,"<APN>"' > /dev/cuaU0`

Of course, you must replace <APN>  by the APN of your ISP.


----------



## balanga (Oct 6, 2018)

Snail said:


> You need to send the NDISDUP command to your modem.
> 
> `echo 'AT^NDISDUP= 1,1,"<APN>"' > /dev/cuaU0`
> 
> Of course, you must replace <APN>  by the APN of your ISP.



Many thanks!

`echo 'AT^NDISDUP= 1,1,"internet"' > /dev/cuaU0`

did the trick for me. 

Is this something I need to do everytime I insert the modem and how would I automate it?

Also how do accommodate routing to this ISP?


----------



## Snail (Oct 6, 2018)

> and how would I automate it?


That is why I opened this thread at the beginning . I wanted someone to tell me how to do it right, in the FreeBSD way.

If you just want to plug this modem from time to time, creating a devd rule should be enough.



> Also how do accommodate routing to this ISP?


That depends on what you want to achieve.
My ADSL connection is very slow and I added this modem on my nanoBSD router to increase the bandwith. That means that I need to deal with two "default gateways", and that is why I opened this other thread : https://forums.freebsd.org/threads/multiple-default-gateways.67299

If you do need to use two different routing tables, you will have to be more specific because I do not see what your problem could be.


----------



## balanga (Oct 9, 2018)

I've temporarily given up on trying to get my stick modem configured properly and just tried my Hilink modem which I've been unable to get to get working on my current FreeBSD installation. To my amazement it worked once I ran `dhclient ue0` and got the following result using speedtest:-


> root@Vbox:~ # speedtest
> Retrieving speedtest.net configuration...
> Testing from Play (5.173.168.128)...
> Retrieving speedtest.net server list...
> ...



This is 20 times!!! faster than my home ISP in the UK. 

I managed to get it working following the usb_modeswitch cmd mentioned earlier in this thread.


----------



## arktos (Jun 29, 2019)

Snail said:


> That is why I opened this thread at the beginning . I wanted someone to tell me how to do it right, in the FreeBSD way.
> 
> If you just want to plug this modem from time to time, creating a devd rule should be enough.



I recently ran into the same problem, I solved it by putting something like:


```
case $reason in
PREINIT)
    echo 'AT^NDISDUP=1,1,"<APN>"' > /dev/cuaU0
    ;;
esac
```

…in /etc/dhclient-enter-hooks. While this might be a bit of a late reply for the original questioner, it might be some help for others looking for a solution to this problem.


----------

